1 // SPDX-License-Identifier: GPL-2.0
3 * NCR 5380 generic driver routines. These should make it *trivial*
4 * to implement 5380 SCSI drivers under Linux with a non-trantor
7 * Note that these routines also work with NR53c400 family chips.
9 * Copyright 1993, Drew Eckhardt
11 * (Unix and Linux consulting and custom programming)
15 * For more information, please consult
18 * SCSI Protocol Controller
21 * NCR Microelectronics
22 * 1635 Aeroplaza Drive
23 * Colorado Springs, CO 80916
29 * With contributions from Ray Van Tassle, Ingmar Baumgart,
30 * Ronald van Cuijlenborg, Alan Cox and others.
33 /* Ported to Atari by Roman Hodek and others. */
35 /* Adapted for the Sun 3 by Sam Creasey. */
40 * This is a generic 5380 driver. To use it on a different platform,
41 * one simply writes appropriate system specific macros (ie, data
42 * transfer - some PC's will use the I/O bus, 68K's must use
43 * memory mapped) and drops this file in their 'C' wrapper.
45 * As far as command queueing, two queues are maintained for
46 * each 5380 in the system - commands that haven't been issued yet,
47 * and commands that are currently executing. This means that an
48 * unlimited number of commands may be queued, letting
49 * more commands propagate from the higher driver levels giving higher
50 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
51 * allowing multiple commands to propagate all the way to a SCSI-II device
52 * while a command is already executing.
55 * Issues specific to the NCR5380 :
57 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
58 * piece of hardware that requires you to sit in a loop polling for
59 * the REQ signal as long as you are connected. Some devices are
60 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
61 * while doing long seek operations. [...] These
62 * broken devices are the exception rather than the rule and I'd rather
63 * spend my time optimizing for the normal case.
67 * At the heart of the design is a coroutine, NCR5380_main,
68 * which is started from a workqueue for each NCR5380 host in the
69 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
70 * removing the commands from the issue queue and calling
71 * NCR5380_select() if a nexus is not established.
73 * Once a nexus is established, the NCR5380_information_transfer()
74 * phase goes through the various phases as instructed by the target.
75 * if the target goes into MSG IN and sends a DISCONNECT message,
76 * the command structure is placed into the per instance disconnected
77 * queue, and NCR5380_main tries to find more work. If the target is
78 * idle for too long, the system will try to sleep.
80 * If a command has disconnected, eventually an interrupt will trigger,
81 * calling NCR5380_intr() which will in turn call NCR5380_reselect
82 * to reestablish a nexus. This will run main if necessary.
84 * On command termination, the done function will be called as
87 * SCSI pointers are maintained in the SCp field of SCSI command
88 * structures, being initialized after the command is connected
89 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
90 * Note that in violation of the standard, an implicit SAVE POINTERS operation
91 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
96 * This file a skeleton Linux SCSI driver for the NCR 5380 series
97 * of chips. To use it, you write an architecture specific functions
98 * and macros and include this file in your driver.
100 * These macros MUST be defined :
102 * NCR5380_read(register) - read from the specified register
104 * NCR5380_write(register, value) - write to the specific register
106 * NCR5380_implementation_fields - additional fields needed for this
107 * specific implementation of the NCR5380
109 * Either real DMA *or* pseudo DMA may be implemented
111 * NCR5380_dma_xfer_len - determine size of DMA/PDMA transfer
112 * NCR5380_dma_send_setup - execute DMA/PDMA from memory to 5380
113 * NCR5380_dma_recv_setup - execute DMA/PDMA from 5380 to memory
114 * NCR5380_dma_residual - residual byte count
116 * The generic driver is initialized by calling NCR5380_init(instance),
117 * after setting the appropriate host specific fields and ID.
120 #ifndef NCR5380_io_delay
121 #define NCR5380_io_delay(x)
124 #ifndef NCR5380_acquire_dma_irq
125 #define NCR5380_acquire_dma_irq(x) (1)
128 #ifndef NCR5380_release_dma_irq
129 #define NCR5380_release_dma_irq(x)
132 static int do_abort(struct Scsi_Host
*);
133 static void do_reset(struct Scsi_Host
*);
136 * initialize_SCp - init the scsi pointer field
137 * @cmd: command block to set up
139 * Set up the internal fields in the SCSI command.
142 static inline void initialize_SCp(struct scsi_cmnd
*cmd
)
145 * Initialize the Scsi Pointer field so that all of the commands in the
146 * various queues are valid.
149 if (scsi_bufflen(cmd
)) {
150 cmd
->SCp
.buffer
= scsi_sglist(cmd
);
151 cmd
->SCp
.buffers_residual
= scsi_sg_count(cmd
) - 1;
152 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
153 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
155 cmd
->SCp
.buffer
= NULL
;
156 cmd
->SCp
.buffers_residual
= 0;
158 cmd
->SCp
.this_residual
= 0;
162 cmd
->SCp
.Message
= 0;
166 * NCR5380_poll_politely2 - wait for two chip register values
167 * @hostdata: host private data
168 * @reg1: 5380 register to poll
169 * @bit1: Bitmask to check
170 * @val1: Expected value
171 * @reg2: Second 5380 register to poll
172 * @bit2: Second bitmask to check
173 * @val2: Second expected value
174 * @wait: Time-out in jiffies
176 * Polls the chip in a reasonably efficient manner waiting for an
177 * event to occur. After a short quick poll we begin to yield the CPU
178 * (if possible). In irq contexts the time-out is arbitrarily limited.
179 * Callers may hold locks as long as they are held in irq mode.
181 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
184 static int NCR5380_poll_politely2(struct NCR5380_hostdata
*hostdata
,
185 unsigned int reg1
, u8 bit1
, u8 val1
,
186 unsigned int reg2
, u8 bit2
, u8 val2
,
189 unsigned long n
= hostdata
->poll_loops
;
190 unsigned long deadline
= jiffies
+ wait
;
193 if ((NCR5380_read(reg1
) & bit1
) == val1
)
195 if ((NCR5380_read(reg2
) & bit2
) == val2
)
200 if (irqs_disabled() || in_interrupt())
203 /* Repeatedly sleep for 1 ms until deadline */
204 while (time_is_after_jiffies(deadline
)) {
205 schedule_timeout_uninterruptible(1);
206 if ((NCR5380_read(reg1
) & bit1
) == val1
)
208 if ((NCR5380_read(reg2
) & bit2
) == val2
)
231 {BASR_END_DMA_TRANSFER
, "END OF DMA"},
233 {BASR_PARITY_ERROR
, "PARITY ERROR"},
235 {BASR_PHASE_MATCH
, "PHASE MATCH"},
236 {BASR_BUSY_ERROR
, "BUSY ERROR"},
242 {ICR_ASSERT_RST
, "ASSERT RST"},
243 {ICR_ARBITRATION_PROGRESS
, "ARB. IN PROGRESS"},
244 {ICR_ARBITRATION_LOST
, "LOST ARB."},
245 {ICR_ASSERT_ACK
, "ASSERT ACK"},
246 {ICR_ASSERT_BSY
, "ASSERT BSY"},
247 {ICR_ASSERT_SEL
, "ASSERT SEL"},
248 {ICR_ASSERT_ATN
, "ASSERT ATN"},
249 {ICR_ASSERT_DATA
, "ASSERT DATA"},
253 {MR_BLOCK_DMA_MODE
, "BLOCK DMA MODE"},
254 {MR_TARGET
, "TARGET"},
255 {MR_ENABLE_PAR_CHECK
, "PARITY CHECK"},
256 {MR_ENABLE_PAR_INTR
, "PARITY INTR"},
257 {MR_ENABLE_EOP_INTR
, "EOP INTR"},
258 {MR_MONITOR_BSY
, "MONITOR BSY"},
259 {MR_DMA_MODE
, "DMA MODE"},
260 {MR_ARBITRATE
, "ARBITRATE"},
265 * NCR5380_print - print scsi bus signals
266 * @instance: adapter state to dump
268 * Print the SCSI bus signals for debugging purposes
271 static void NCR5380_print(struct Scsi_Host
*instance
)
273 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
274 unsigned char status
, data
, basr
, mr
, icr
, i
;
276 data
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
277 status
= NCR5380_read(STATUS_REG
);
278 mr
= NCR5380_read(MODE_REG
);
279 icr
= NCR5380_read(INITIATOR_COMMAND_REG
);
280 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
282 printk(KERN_DEBUG
"SR = 0x%02x : ", status
);
283 for (i
= 0; signals
[i
].mask
; ++i
)
284 if (status
& signals
[i
].mask
)
285 printk(KERN_CONT
"%s, ", signals
[i
].name
);
286 printk(KERN_CONT
"\nBASR = 0x%02x : ", basr
);
287 for (i
= 0; basrs
[i
].mask
; ++i
)
288 if (basr
& basrs
[i
].mask
)
289 printk(KERN_CONT
"%s, ", basrs
[i
].name
);
290 printk(KERN_CONT
"\nICR = 0x%02x : ", icr
);
291 for (i
= 0; icrs
[i
].mask
; ++i
)
292 if (icr
& icrs
[i
].mask
)
293 printk(KERN_CONT
"%s, ", icrs
[i
].name
);
294 printk(KERN_CONT
"\nMR = 0x%02x : ", mr
);
295 for (i
= 0; mrs
[i
].mask
; ++i
)
296 if (mr
& mrs
[i
].mask
)
297 printk(KERN_CONT
"%s, ", mrs
[i
].name
);
298 printk(KERN_CONT
"\n");
305 {PHASE_DATAOUT
, "DATAOUT"},
306 {PHASE_DATAIN
, "DATAIN"},
307 {PHASE_CMDOUT
, "CMDOUT"},
308 {PHASE_STATIN
, "STATIN"},
309 {PHASE_MSGOUT
, "MSGOUT"},
310 {PHASE_MSGIN
, "MSGIN"},
311 {PHASE_UNKNOWN
, "UNKNOWN"}
315 * NCR5380_print_phase - show SCSI phase
316 * @instance: adapter to dump
318 * Print the current SCSI phase for debugging purposes
321 static void NCR5380_print_phase(struct Scsi_Host
*instance
)
323 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
324 unsigned char status
;
327 status
= NCR5380_read(STATUS_REG
);
328 if (!(status
& SR_REQ
))
329 shost_printk(KERN_DEBUG
, instance
, "REQ not asserted, phase unknown.\n");
331 for (i
= 0; (phases
[i
].value
!= PHASE_UNKNOWN
) &&
332 (phases
[i
].value
!= (status
& PHASE_MASK
)); ++i
)
334 shost_printk(KERN_DEBUG
, instance
, "phase %s\n", phases
[i
].name
);
340 * NCR5380_info - report driver and host information
341 * @instance: relevant scsi host instance
343 * For use as the host template info() handler.
346 static const char *NCR5380_info(struct Scsi_Host
*instance
)
348 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
350 return hostdata
->info
;
354 * NCR5380_init - initialise an NCR5380
355 * @instance: adapter to configure
356 * @flags: control flags
358 * Initializes *instance and corresponding 5380 chip,
359 * with flags OR'd into the initial flags value.
361 * Notes : I assume that the host, hostno, and id bits have been
362 * set correctly. I don't care about the irq and other fields.
364 * Returns 0 for success
367 static int NCR5380_init(struct Scsi_Host
*instance
, int flags
)
369 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
371 unsigned long deadline
;
372 unsigned long accesses_per_ms
;
374 instance
->max_lun
= 7;
376 hostdata
->host
= instance
;
377 hostdata
->id_mask
= 1 << instance
->this_id
;
378 hostdata
->id_higher_mask
= 0;
379 for (i
= hostdata
->id_mask
; i
<= 0x80; i
<<= 1)
380 if (i
> hostdata
->id_mask
)
381 hostdata
->id_higher_mask
|= i
;
382 for (i
= 0; i
< 8; ++i
)
383 hostdata
->busy
[i
] = 0;
384 hostdata
->dma_len
= 0;
386 spin_lock_init(&hostdata
->lock
);
387 hostdata
->connected
= NULL
;
388 hostdata
->sensing
= NULL
;
389 INIT_LIST_HEAD(&hostdata
->autosense
);
390 INIT_LIST_HEAD(&hostdata
->unissued
);
391 INIT_LIST_HEAD(&hostdata
->disconnected
);
393 hostdata
->flags
= flags
;
395 INIT_WORK(&hostdata
->main_task
, NCR5380_main
);
396 hostdata
->work_q
= alloc_workqueue("ncr5380_%d",
397 WQ_UNBOUND
| WQ_MEM_RECLAIM
,
398 1, instance
->host_no
);
399 if (!hostdata
->work_q
)
402 snprintf(hostdata
->info
, sizeof(hostdata
->info
),
403 "%s, irq %d, io_port 0x%lx, base 0x%lx, can_queue %d, cmd_per_lun %d, sg_tablesize %d, this_id %d, flags { %s%s%s}",
404 instance
->hostt
->name
, instance
->irq
, hostdata
->io_port
,
405 hostdata
->base
, instance
->can_queue
, instance
->cmd_per_lun
,
406 instance
->sg_tablesize
, instance
->this_id
,
407 hostdata
->flags
& FLAG_DMA_FIXUP
? "DMA_FIXUP " : "",
408 hostdata
->flags
& FLAG_NO_PSEUDO_DMA
? "NO_PSEUDO_DMA " : "",
409 hostdata
->flags
& FLAG_TOSHIBA_DELAY
? "TOSHIBA_DELAY " : "");
411 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
412 NCR5380_write(MODE_REG
, MR_BASE
);
413 NCR5380_write(TARGET_COMMAND_REG
, 0);
414 NCR5380_write(SELECT_ENABLE_REG
, 0);
416 /* Calibrate register polling loop */
418 deadline
= jiffies
+ 1;
421 } while (time_is_after_jiffies(deadline
));
422 deadline
+= msecs_to_jiffies(256);
424 NCR5380_read(STATUS_REG
);
427 } while (time_is_after_jiffies(deadline
));
428 accesses_per_ms
= i
/ 256;
429 hostdata
->poll_loops
= NCR5380_REG_POLL_TIME
* accesses_per_ms
/ 2;
435 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
436 * @instance: adapter to check
438 * If the system crashed, it may have crashed with a connected target and
439 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
440 * currently established nexus, which we know nothing about. Failing that
443 * Note that a bus reset will cause the chip to assert IRQ.
445 * Returns 0 if successful, otherwise -ENXIO.
448 static int NCR5380_maybe_reset_bus(struct Scsi_Host
*instance
)
450 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
453 for (pass
= 1; (NCR5380_read(STATUS_REG
) & SR_BSY
) && pass
<= 6; ++pass
) {
458 shost_printk(KERN_ERR
, instance
, "SCSI bus busy, waiting up to five seconds\n");
459 NCR5380_poll_politely(hostdata
,
460 STATUS_REG
, SR_BSY
, 0, 5 * HZ
);
463 shost_printk(KERN_ERR
, instance
, "bus busy, attempting abort\n");
467 shost_printk(KERN_ERR
, instance
, "bus busy, attempting reset\n");
469 /* Wait after a reset; the SCSI standard calls for
470 * 250ms, we wait 500ms to be on the safe side.
471 * But some Toshiba CD-ROMs need ten times that.
473 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
479 shost_printk(KERN_ERR
, instance
, "bus locked solid\n");
487 * NCR5380_exit - remove an NCR5380
488 * @instance: adapter to remove
490 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
493 static void NCR5380_exit(struct Scsi_Host
*instance
)
495 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
497 cancel_work_sync(&hostdata
->main_task
);
498 destroy_workqueue(hostdata
->work_q
);
502 * complete_cmd - finish processing a command and return it to the SCSI ML
503 * @instance: the host instance
504 * @cmd: command to complete
507 static void complete_cmd(struct Scsi_Host
*instance
,
508 struct scsi_cmnd
*cmd
)
510 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
512 dsprintk(NDEBUG_QUEUES
, instance
, "complete_cmd: cmd %p\n", cmd
);
514 if (hostdata
->sensing
== cmd
) {
515 /* Autosense processing ends here */
516 if ((cmd
->result
& 0xff) != SAM_STAT_GOOD
) {
517 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
518 set_host_byte(cmd
, DID_ERROR
);
520 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
521 hostdata
->sensing
= NULL
;
524 hostdata
->busy
[scmd_id(cmd
)] &= ~(1 << cmd
->device
->lun
);
530 * NCR5380_queue_command - queue a command
531 * @instance: the relevant SCSI adapter
534 * cmd is added to the per-instance issue queue, with minor
535 * twiddling done to the host specific fields of cmd. If the
536 * main coroutine is not running, it is restarted.
539 static int NCR5380_queue_command(struct Scsi_Host
*instance
,
540 struct scsi_cmnd
*cmd
)
542 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
543 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(cmd
);
546 #if (NDEBUG & NDEBUG_NO_WRITE)
547 switch (cmd
->cmnd
[0]) {
550 shost_printk(KERN_DEBUG
, instance
, "WRITE attempted with NDEBUG_NO_WRITE set\n");
551 cmd
->result
= (DID_ERROR
<< 16);
555 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
559 if (!NCR5380_acquire_dma_irq(instance
))
560 return SCSI_MLQUEUE_HOST_BUSY
;
562 spin_lock_irqsave(&hostdata
->lock
, flags
);
565 * Insert the cmd into the issue queue. Note that REQUEST SENSE
566 * commands are added to the head of the queue since any command will
567 * clear the contingent allegiance condition that exists and the
568 * sense data is only guaranteed to be valid while the condition exists.
571 if (cmd
->cmnd
[0] == REQUEST_SENSE
)
572 list_add(&ncmd
->list
, &hostdata
->unissued
);
574 list_add_tail(&ncmd
->list
, &hostdata
->unissued
);
576 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
578 dsprintk(NDEBUG_QUEUES
, instance
, "command %p added to %s of queue\n",
579 cmd
, (cmd
->cmnd
[0] == REQUEST_SENSE
) ? "head" : "tail");
581 /* Kick off command processing */
582 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
586 static inline void maybe_release_dma_irq(struct Scsi_Host
*instance
)
588 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
590 /* Caller does the locking needed to set & test these data atomically */
591 if (list_empty(&hostdata
->disconnected
) &&
592 list_empty(&hostdata
->unissued
) &&
593 list_empty(&hostdata
->autosense
) &&
594 !hostdata
->connected
&&
595 !hostdata
->selecting
) {
596 NCR5380_release_dma_irq(instance
);
601 * dequeue_next_cmd - dequeue a command for processing
602 * @instance: the scsi host instance
604 * Priority is given to commands on the autosense queue. These commands
605 * need autosense because of a CHECK CONDITION result.
607 * Returns a command pointer if a command is found for a target that is
608 * not already busy. Otherwise returns NULL.
611 static struct scsi_cmnd
*dequeue_next_cmd(struct Scsi_Host
*instance
)
613 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
614 struct NCR5380_cmd
*ncmd
;
615 struct scsi_cmnd
*cmd
;
617 if (hostdata
->sensing
|| list_empty(&hostdata
->autosense
)) {
618 list_for_each_entry(ncmd
, &hostdata
->unissued
, list
) {
619 cmd
= NCR5380_to_scmd(ncmd
);
620 dsprintk(NDEBUG_QUEUES
, instance
, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
621 cmd
, scmd_id(cmd
), hostdata
->busy
[scmd_id(cmd
)], cmd
->device
->lun
);
623 if (!(hostdata
->busy
[scmd_id(cmd
)] & (1 << cmd
->device
->lun
))) {
624 list_del(&ncmd
->list
);
625 dsprintk(NDEBUG_QUEUES
, instance
,
626 "dequeue: removed %p from issue queue\n", cmd
);
631 /* Autosense processing begins here */
632 ncmd
= list_first_entry(&hostdata
->autosense
,
633 struct NCR5380_cmd
, list
);
634 list_del(&ncmd
->list
);
635 cmd
= NCR5380_to_scmd(ncmd
);
636 dsprintk(NDEBUG_QUEUES
, instance
,
637 "dequeue: removed %p from autosense queue\n", cmd
);
638 scsi_eh_prep_cmnd(cmd
, &hostdata
->ses
, NULL
, 0, ~0);
639 hostdata
->sensing
= cmd
;
645 static void requeue_cmd(struct Scsi_Host
*instance
, struct scsi_cmnd
*cmd
)
647 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
648 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(cmd
);
650 if (hostdata
->sensing
== cmd
) {
651 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
652 list_add(&ncmd
->list
, &hostdata
->autosense
);
653 hostdata
->sensing
= NULL
;
655 list_add(&ncmd
->list
, &hostdata
->unissued
);
659 * NCR5380_main - NCR state machines
661 * NCR5380_main is a coroutine that runs as long as more work can
662 * be done on the NCR5380 host adapters in a system. Both
663 * NCR5380_queue_command() and NCR5380_intr() will try to start it
664 * in case it is not running.
667 static void NCR5380_main(struct work_struct
*work
)
669 struct NCR5380_hostdata
*hostdata
=
670 container_of(work
, struct NCR5380_hostdata
, main_task
);
671 struct Scsi_Host
*instance
= hostdata
->host
;
677 spin_lock_irq(&hostdata
->lock
);
678 while (!hostdata
->connected
&& !hostdata
->selecting
) {
679 struct scsi_cmnd
*cmd
= dequeue_next_cmd(instance
);
684 dsprintk(NDEBUG_MAIN
, instance
, "main: dequeued %p\n", cmd
);
687 * Attempt to establish an I_T_L nexus here.
688 * On success, instance->hostdata->connected is set.
689 * On failure, we must add the command back to the
690 * issue queue so we can keep trying.
693 * REQUEST SENSE commands are issued without tagged
694 * queueing, even on SCSI-II devices because the
695 * contingent allegiance condition exists for the
699 if (!NCR5380_select(instance
, cmd
)) {
700 dsprintk(NDEBUG_MAIN
, instance
, "main: select complete\n");
701 maybe_release_dma_irq(instance
);
703 dsprintk(NDEBUG_MAIN
| NDEBUG_QUEUES
, instance
,
704 "main: select failed, returning %p to queue\n", cmd
);
705 requeue_cmd(instance
, cmd
);
708 if (hostdata
->connected
&& !hostdata
->dma_len
) {
709 dsprintk(NDEBUG_MAIN
, instance
, "main: performing information transfer\n");
710 NCR5380_information_transfer(instance
);
713 spin_unlock_irq(&hostdata
->lock
);
720 * NCR5380_dma_complete - finish DMA transfer
721 * @instance: the scsi host instance
723 * Called by the interrupt handler when DMA finishes or a phase
724 * mismatch occurs (which would end the DMA transfer).
727 static void NCR5380_dma_complete(struct Scsi_Host
*instance
)
729 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
731 unsigned char **data
;
733 int saved_data
= 0, overrun
= 0;
736 if (hostdata
->read_overruns
) {
737 p
= hostdata
->connected
->SCp
.phase
;
740 if ((NCR5380_read(BUS_AND_STATUS_REG
) &
741 (BASR_PHASE_MATCH
| BASR_ACK
)) ==
742 (BASR_PHASE_MATCH
| BASR_ACK
)) {
743 saved_data
= NCR5380_read(INPUT_DATA_REG
);
745 dsprintk(NDEBUG_DMA
, instance
, "read overrun handled\n");
751 if ((sun3scsi_dma_finish(rq_data_dir(hostdata
->connected
->request
)))) {
752 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
757 if ((NCR5380_read(BUS_AND_STATUS_REG
) & (BASR_PHASE_MATCH
| BASR_ACK
)) ==
758 (BASR_PHASE_MATCH
| BASR_ACK
)) {
759 pr_err("scsi%d: BASR %02x\n", instance
->host_no
,
760 NCR5380_read(BUS_AND_STATUS_REG
));
761 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
767 NCR5380_write(MODE_REG
, MR_BASE
);
768 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
769 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
771 transferred
= hostdata
->dma_len
- NCR5380_dma_residual(hostdata
);
772 hostdata
->dma_len
= 0;
774 data
= (unsigned char **)&hostdata
->connected
->SCp
.ptr
;
775 count
= &hostdata
->connected
->SCp
.this_residual
;
776 *data
+= transferred
;
777 *count
-= transferred
;
779 if (hostdata
->read_overruns
) {
782 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) == p
&& (p
& SR_IO
)) {
783 cnt
= toPIO
= hostdata
->read_overruns
;
785 dsprintk(NDEBUG_DMA
, instance
,
786 "Got an input overrun, using saved byte\n");
787 *(*data
)++ = saved_data
;
793 dsprintk(NDEBUG_DMA
, instance
,
794 "Doing %d byte PIO to 0x%p\n", cnt
, *data
);
795 NCR5380_transfer_pio(instance
, &p
, &cnt
, data
);
796 *count
-= toPIO
- cnt
;
803 * NCR5380_intr - generic NCR5380 irq handler
804 * @irq: interrupt number
805 * @dev_id: device info
807 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
808 * from the disconnected queue, and restarting NCR5380_main()
811 * The chip can assert IRQ in any of six different conditions. The IRQ flag
812 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
813 * Three of these six conditions are latched in the Bus and Status Register:
814 * - End of DMA (cleared by ending DMA Mode)
815 * - Parity error (cleared by reading RPIR)
816 * - Loss of BSY (cleared by reading RPIR)
817 * Two conditions have flag bits that are not latched:
818 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
819 * - Bus reset (non-maskable)
820 * The remaining condition has no flag bit at all:
821 * - Selection/reselection
823 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
824 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
825 * claimed that "the design of the [DP8490] interrupt logic ensures
826 * interrupts will not be lost (they can be on the DP5380)."
827 * The L5380/53C80 datasheet from LOGIC Devices has more details.
829 * Checking for bus reset by reading RST is futile because of interrupt
830 * latency, but a bus reset will reset chip logic. Checking for parity error
831 * is unnecessary because that interrupt is never enabled. A Loss of BSY
832 * condition will clear DMA Mode. We can tell when this occurs because the
833 * the Busy Monitor interrupt is enabled together with DMA Mode.
836 static irqreturn_t __maybe_unused
NCR5380_intr(int irq
, void *dev_id
)
838 struct Scsi_Host
*instance
= dev_id
;
839 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
844 spin_lock_irqsave(&hostdata
->lock
, flags
);
846 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
847 if (basr
& BASR_IRQ
) {
848 unsigned char mr
= NCR5380_read(MODE_REG
);
849 unsigned char sr
= NCR5380_read(STATUS_REG
);
851 dsprintk(NDEBUG_INTR
, instance
, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
854 if ((mr
& MR_DMA_MODE
) || (mr
& MR_MONITOR_BSY
)) {
855 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
856 * We ack IRQ after clearing Mode Register. Workarounds
857 * for End of DMA errata need to happen in DMA Mode.
860 dsprintk(NDEBUG_INTR
, instance
, "interrupt in DMA mode\n");
862 if (hostdata
->connected
) {
863 NCR5380_dma_complete(instance
);
864 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
866 NCR5380_write(MODE_REG
, MR_BASE
);
867 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
869 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_mask
) &&
870 (sr
& (SR_SEL
| SR_IO
| SR_BSY
| SR_RST
)) == (SR_SEL
| SR_IO
)) {
871 /* Probably reselected */
872 NCR5380_write(SELECT_ENABLE_REG
, 0);
873 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
875 dsprintk(NDEBUG_INTR
, instance
, "interrupt with SEL and IO\n");
877 if (!hostdata
->connected
) {
878 NCR5380_reselect(instance
);
879 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
881 if (!hostdata
->connected
)
882 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
884 /* Probably Bus Reset */
885 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
887 dsprintk(NDEBUG_INTR
, instance
, "unknown interrupt\n");
889 dregs
->csr
|= CSR_DMA_ENABLE
;
894 dsprintk(NDEBUG_INTR
, instance
, "interrupt without IRQ bit\n");
896 dregs
->csr
|= CSR_DMA_ENABLE
;
900 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
902 return IRQ_RETVAL(handled
);
906 * Function : int NCR5380_select(struct Scsi_Host *instance,
907 * struct scsi_cmnd *cmd)
909 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
910 * including ARBITRATION, SELECTION, and initial message out for
911 * IDENTIFY and queue messages.
913 * Inputs : instance - instantiation of the 5380 driver on which this
914 * target lives, cmd - SCSI command to execute.
916 * Returns cmd if selection failed but should be retried,
917 * NULL if selection failed and should not be retried, or
918 * NULL if selection succeeded (hostdata->connected == cmd).
921 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
922 * with registers as they should have been on entry - ie
923 * SELECT_ENABLE will be set appropriately, the NCR5380
924 * will cease to drive any SCSI bus signals.
926 * If successful : I_T_L or I_T_L_Q nexus will be established,
927 * instance->connected will be set to cmd.
928 * SELECT interrupt will be disabled.
930 * If failed (no target) : cmd->scsi_done() will be called, and the
931 * cmd->result host byte set to DID_BAD_TARGET.
934 static struct scsi_cmnd
*NCR5380_select(struct Scsi_Host
*instance
,
935 struct scsi_cmnd
*cmd
)
936 __releases(&hostdata
->lock
) __acquires(&hostdata
->lock
)
938 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
939 unsigned char tmp
[3], phase
;
944 NCR5380_dprint(NDEBUG_ARBITRATION
, instance
);
945 dsprintk(NDEBUG_ARBITRATION
, instance
, "starting arbitration, id = %d\n",
949 * Arbitration and selection phases are slow and involve dropping the
950 * lock, so we have to watch out for EH. An exception handler may
951 * change 'selecting' to NULL. This function will then return NULL
952 * so that the caller will forget about 'cmd'. (During information
953 * transfer phases, EH may change 'connected' to NULL.)
955 hostdata
->selecting
= cmd
;
958 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
959 * data bus during SELECTION.
962 NCR5380_write(TARGET_COMMAND_REG
, 0);
968 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
969 NCR5380_write(MODE_REG
, MR_ARBITRATE
);
971 /* The chip now waits for BUS FREE phase. Then after the 800 ns
972 * Bus Free Delay, arbitration will begin.
975 spin_unlock_irq(&hostdata
->lock
);
976 err
= NCR5380_poll_politely2(hostdata
, MODE_REG
, MR_ARBITRATE
, 0,
977 INITIATOR_COMMAND_REG
, ICR_ARBITRATION_PROGRESS
,
978 ICR_ARBITRATION_PROGRESS
, HZ
);
979 spin_lock_irq(&hostdata
->lock
);
980 if (!(NCR5380_read(MODE_REG
) & MR_ARBITRATE
)) {
981 /* Reselection interrupt */
984 if (!hostdata
->selecting
) {
985 /* Command was aborted */
986 NCR5380_write(MODE_REG
, MR_BASE
);
990 NCR5380_write(MODE_REG
, MR_BASE
);
991 shost_printk(KERN_ERR
, instance
,
992 "select: arbitration timeout\n");
995 spin_unlock_irq(&hostdata
->lock
);
997 /* The SCSI-2 arbitration delay is 2.4 us */
1000 /* Check for lost arbitration */
1001 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1002 (NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_higher_mask
) ||
1003 (NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
)) {
1004 NCR5380_write(MODE_REG
, MR_BASE
);
1005 dsprintk(NDEBUG_ARBITRATION
, instance
, "lost arbitration, deasserting MR_ARBITRATE\n");
1006 spin_lock_irq(&hostdata
->lock
);
1010 /* After/during arbitration, BSY should be asserted.
1011 * IBM DPES-31080 Version S31Q works now
1012 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1014 NCR5380_write(INITIATOR_COMMAND_REG
,
1015 ICR_BASE
| ICR_ASSERT_SEL
| ICR_ASSERT_BSY
);
1018 * Again, bus clear + bus settle time is 1.2us, however, this is
1019 * a minimum so we'll udelay ceil(1.2)
1022 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
1027 spin_lock_irq(&hostdata
->lock
);
1029 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1030 if (!(NCR5380_read(MODE_REG
) & MR_ARBITRATE
))
1033 if (!hostdata
->selecting
) {
1034 NCR5380_write(MODE_REG
, MR_BASE
);
1035 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1039 dsprintk(NDEBUG_ARBITRATION
, instance
, "won arbitration\n");
1042 * Now that we have won arbitration, start Selection process, asserting
1043 * the host and target ID's on the SCSI bus.
1046 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
| (1 << scmd_id(cmd
)));
1049 * Raise ATN while SEL is true before BSY goes false from arbitration,
1050 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1051 * phase immediately after selection.
1054 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
|
1055 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_SEL
);
1056 NCR5380_write(MODE_REG
, MR_BASE
);
1059 * Reselect interrupts must be turned off prior to the dropping of BSY,
1060 * otherwise we will trigger an interrupt.
1062 NCR5380_write(SELECT_ENABLE_REG
, 0);
1064 spin_unlock_irq(&hostdata
->lock
);
1067 * The initiator shall then wait at least two deskew delays and release
1070 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1073 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
|
1074 ICR_ASSERT_ATN
| ICR_ASSERT_SEL
);
1077 * Something weird happens when we cease to drive BSY - looks
1078 * like the board/chip is letting us do another read before the
1079 * appropriate propagation delay has expired, and we're confusing
1080 * a BSY signal from ourselves as the target's response to SELECTION.
1082 * A small delay (the 'C++' frontend breaks the pipeline with an
1083 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1084 * tighter 'C' code breaks and requires this) solves the problem -
1085 * the 1 us delay is arbitrary, and only used because this delay will
1086 * be the same on other platforms and since it works here, it should
1089 * wingel suggests that this could be due to failing to wait
1095 dsprintk(NDEBUG_SELECTION
, instance
, "selecting target %d\n", scmd_id(cmd
));
1098 * The SCSI specification calls for a 250 ms timeout for the actual
1102 err
= NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_BSY
, SR_BSY
,
1103 msecs_to_jiffies(250));
1105 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
| SR_IO
)) == (SR_SEL
| SR_IO
)) {
1106 spin_lock_irq(&hostdata
->lock
);
1107 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1108 NCR5380_reselect(instance
);
1109 if (!hostdata
->connected
)
1110 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1111 shost_printk(KERN_ERR
, instance
, "reselection after won arbitration?\n");
1116 spin_lock_irq(&hostdata
->lock
);
1117 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1118 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1119 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1120 if (hostdata
->selecting
) {
1121 cmd
->result
= DID_BAD_TARGET
<< 16;
1122 complete_cmd(instance
, cmd
);
1123 dsprintk(NDEBUG_SELECTION
, instance
, "target did not respond within 250ms\n");
1130 * No less than two deskew delays after the initiator detects the
1131 * BSY signal is true, it shall release the SEL signal and may
1132 * change the DATA BUS. -wingel
1137 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1140 * Since we followed the SCSI spec, and raised ATN while SEL
1141 * was true but before BSY was false during selection, the information
1142 * transfer phase should be a MESSAGE OUT phase so that we can send the
1146 /* Wait for start of REQ/ACK handshake */
1148 err
= NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
);
1149 spin_lock_irq(&hostdata
->lock
);
1151 shost_printk(KERN_ERR
, instance
, "select: REQ timeout\n");
1152 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1153 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1156 if (!hostdata
->selecting
) {
1161 dsprintk(NDEBUG_SELECTION
, instance
, "target %d selected, going into MESSAGE OUT phase.\n",
1163 tmp
[0] = IDENTIFY(((instance
->irq
== NO_IRQ
) ? 0 : 1), cmd
->device
->lun
);
1167 phase
= PHASE_MSGOUT
;
1168 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1170 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1171 cmd
->result
= DID_ERROR
<< 16;
1172 complete_cmd(instance
, cmd
);
1173 dsprintk(NDEBUG_SELECTION
, instance
, "IDENTIFY message transfer failed\n");
1178 dsprintk(NDEBUG_SELECTION
, instance
, "nexus established.\n");
1180 hostdata
->connected
= cmd
;
1181 hostdata
->busy
[cmd
->device
->id
] |= 1 << cmd
->device
->lun
;
1183 #ifdef SUN3_SCSI_VME
1184 dregs
->csr
|= CSR_INTR
;
1187 initialize_SCp(cmd
);
1192 if (!hostdata
->selecting
)
1194 hostdata
->selecting
= NULL
;
1199 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1200 * unsigned char *phase, int *count, unsigned char **data)
1202 * Purpose : transfers data in given phase using polled I/O
1204 * Inputs : instance - instance of driver, *phase - pointer to
1205 * what phase is expected, *count - pointer to number of
1206 * bytes to transfer, **data - pointer to data pointer.
1208 * Returns : -1 when different phase is entered without transferring
1209 * maximum number of bytes, 0 if all bytes are transferred or exit
1212 * Also, *phase, *count, *data are modified in place.
1214 * XXX Note : handling for bus free may be useful.
1218 * Note : this code is not as quick as it could be, however it
1219 * IS 100% reliable, and for the actual data transfer where speed
1220 * counts, we will always do a pseudo DMA or DMA transfer.
1223 static int NCR5380_transfer_pio(struct Scsi_Host
*instance
,
1224 unsigned char *phase
, int *count
,
1225 unsigned char **data
)
1227 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1228 unsigned char p
= *phase
, tmp
;
1230 unsigned char *d
= *data
;
1233 * The NCR5380 chip will only drive the SCSI bus when the
1234 * phase specified in the appropriate bits of the TARGET COMMAND
1235 * REGISTER match the STATUS REGISTER
1238 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1242 * Wait for assertion of REQ, after which the phase bits will be
1246 if (NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
) < 0)
1249 dsprintk(NDEBUG_HANDSHAKE
, instance
, "REQ asserted\n");
1251 /* Check for phase mismatch */
1252 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) != p
) {
1253 dsprintk(NDEBUG_PIO
, instance
, "phase mismatch\n");
1254 NCR5380_dprint_phase(NDEBUG_PIO
, instance
);
1258 /* Do actual transfer from SCSI bus to / from memory */
1260 NCR5380_write(OUTPUT_DATA_REG
, *d
);
1262 *d
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
1267 * The SCSI standard suggests that in MSGOUT phase, the initiator
1268 * should drop ATN on the last byte of the message phase
1269 * after REQ has been asserted for the handshake but before
1270 * the initiator raises ACK.
1274 if (!((p
& SR_MSG
) && c
> 1)) {
1275 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1276 NCR5380_dprint(NDEBUG_PIO
, instance
);
1277 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1278 ICR_ASSERT_DATA
| ICR_ASSERT_ACK
);
1280 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1281 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
);
1282 NCR5380_dprint(NDEBUG_PIO
, instance
);
1283 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1284 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1287 NCR5380_dprint(NDEBUG_PIO
, instance
);
1288 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
1291 if (NCR5380_poll_politely(hostdata
,
1292 STATUS_REG
, SR_REQ
, 0, 5 * HZ
) < 0)
1295 dsprintk(NDEBUG_HANDSHAKE
, instance
, "REQ negated, handshake complete\n");
1298 * We have several special cases to consider during REQ/ACK handshaking :
1299 * 1. We were in MSGOUT phase, and we are on the last byte of the
1300 * message. ATN must be dropped as ACK is dropped.
1302 * 2. We are in a MSGIN phase, and we are on the last byte of the
1303 * message. We must exit with ACK asserted, so that the calling
1304 * code may raise ATN before dropping ACK to reject the message.
1306 * 3. ACK and ATN are clear and the target may proceed as normal.
1308 if (!(p
== PHASE_MSGIN
&& c
== 1)) {
1309 if (p
== PHASE_MSGOUT
&& c
> 1)
1310 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1312 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1316 dsprintk(NDEBUG_PIO
, instance
, "residual %d\n", c
);
1320 tmp
= NCR5380_read(STATUS_REG
);
1321 /* The phase read from the bus is valid if either REQ is (already)
1322 * asserted or if ACK hasn't been released yet. The latter applies if
1323 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1325 if ((tmp
& SR_REQ
) || ((tmp
& SR_IO
) && c
== 0))
1326 *phase
= tmp
& PHASE_MASK
;
1328 *phase
= PHASE_UNKNOWN
;
1330 if (!c
|| (*phase
== p
))
1337 * do_reset - issue a reset command
1338 * @instance: adapter to reset
1340 * Issue a reset sequence to the NCR5380 and try and get the bus
1341 * back into sane shape.
1343 * This clears the reset interrupt flag because there may be no handler for
1344 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1345 * been installed. And when in EH we may have released the ST DMA interrupt.
1348 static void do_reset(struct Scsi_Host
*instance
)
1350 struct NCR5380_hostdata __maybe_unused
*hostdata
= shost_priv(instance
);
1351 unsigned long flags
;
1353 local_irq_save(flags
);
1354 NCR5380_write(TARGET_COMMAND_REG
,
1355 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG
) & PHASE_MASK
));
1356 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
1358 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1359 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1360 local_irq_restore(flags
);
1364 * do_abort - abort the currently established nexus by going to
1365 * MESSAGE OUT phase and sending an ABORT message.
1366 * @instance: relevant scsi host instance
1368 * Returns 0 on success, -1 on failure.
1371 static int do_abort(struct Scsi_Host
*instance
)
1373 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1374 unsigned char *msgptr
, phase
, tmp
;
1378 /* Request message out phase */
1379 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1382 * Wait for the target to indicate a valid phase by asserting
1383 * REQ. Once this happens, we'll have either a MSGOUT phase
1384 * and can immediately send the ABORT message, or we'll have some
1385 * other phase and will have to source/sink data.
1387 * We really don't care what value was on the bus or what value
1388 * the target sees, so we just handshake.
1391 rc
= NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_REQ
, SR_REQ
, 10 * HZ
);
1395 tmp
= NCR5380_read(STATUS_REG
) & PHASE_MASK
;
1397 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1399 if (tmp
!= PHASE_MSGOUT
) {
1400 NCR5380_write(INITIATOR_COMMAND_REG
,
1401 ICR_BASE
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1402 rc
= NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_REQ
, 0, 3 * HZ
);
1405 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1411 phase
= PHASE_MSGOUT
;
1412 NCR5380_transfer_pio(instance
, &phase
, &len
, &msgptr
);
1415 * If we got here, and the command completed successfully,
1416 * we're about to go into bus free state.
1419 return len
? -1 : 0;
1422 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1427 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1428 * unsigned char *phase, int *count, unsigned char **data)
1430 * Purpose : transfers data in given phase using either real
1433 * Inputs : instance - instance of driver, *phase - pointer to
1434 * what phase is expected, *count - pointer to number of
1435 * bytes to transfer, **data - pointer to data pointer.
1437 * Returns : -1 when different phase is entered without transferring
1438 * maximum number of bytes, 0 if all bytes or transferred or exit
1441 * Also, *phase, *count, *data are modified in place.
1445 static int NCR5380_transfer_dma(struct Scsi_Host
*instance
,
1446 unsigned char *phase
, int *count
,
1447 unsigned char **data
)
1449 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1451 unsigned char p
= *phase
;
1452 unsigned char *d
= *data
;
1456 if ((tmp
= (NCR5380_read(STATUS_REG
) & PHASE_MASK
)) != p
) {
1461 hostdata
->connected
->SCp
.phase
= p
;
1464 if (hostdata
->read_overruns
)
1465 c
-= hostdata
->read_overruns
;
1466 else if (hostdata
->flags
& FLAG_DMA_FIXUP
)
1470 dsprintk(NDEBUG_DMA
, instance
, "initializing DMA %s: length %d, address %p\n",
1471 (p
& SR_IO
) ? "receive" : "send", c
, d
);
1474 /* send start chain */
1475 sun3scsi_dma_start(c
, *data
);
1478 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1479 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
| MR_MONITOR_BSY
|
1480 MR_ENABLE_EOP_INTR
);
1482 if (!(hostdata
->flags
& FLAG_LATE_DMA_SETUP
)) {
1483 /* On the Medusa, it is a must to initialize the DMA before
1484 * starting the NCR. This is also the cleaner way for the TT.
1487 result
= NCR5380_dma_recv_setup(hostdata
, d
, c
);
1489 result
= NCR5380_dma_send_setup(hostdata
, d
, c
);
1493 * On the PAS16 at least I/O recovery delays are not needed here.
1494 * Everyone else seems to want them.
1498 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1499 NCR5380_io_delay(1);
1500 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
1502 NCR5380_io_delay(1);
1503 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1504 NCR5380_io_delay(1);
1505 NCR5380_write(START_DMA_SEND_REG
, 0);
1506 NCR5380_io_delay(1);
1510 #ifdef SUN3_SCSI_VME
1511 dregs
->csr
|= CSR_DMA_ENABLE
;
1513 sun3_dma_active
= 1;
1516 if (hostdata
->flags
& FLAG_LATE_DMA_SETUP
) {
1517 /* On the Falcon, the DMA setup must be done after the last
1518 * NCR access, else the DMA setup gets trashed!
1521 result
= NCR5380_dma_recv_setup(hostdata
, d
, c
);
1523 result
= NCR5380_dma_send_setup(hostdata
, d
, c
);
1526 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1530 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1532 hostdata
->dma_len
= result
;
1536 /* The result is zero iff pseudo DMA send/receive was completed. */
1537 hostdata
->dma_len
= c
;
1540 * A note regarding the DMA errata workarounds for early NMOS silicon.
1542 * For DMA sends, we want to wait until the last byte has been
1543 * transferred out over the bus before we turn off DMA mode. Alas, there
1544 * seems to be no terribly good way of doing this on a 5380 under all
1545 * conditions. For non-scatter-gather operations, we can wait until REQ
1546 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1547 * are nastier, since the device will be expecting more data than we
1548 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1549 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1550 * why this signal was added to the newer chips) but on the older 538[01]
1551 * this signal does not exist. The workaround for this lack is a watchdog;
1552 * we bail out of the wait-loop after a modest amount of wait-time if
1553 * the usual exit conditions are not met. Not a terribly clean or
1554 * correct solution :-%
1556 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1557 * If the chip is in DMA receive mode, it will respond to a target's
1558 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1559 * ACK, even if it has _already_ been notified by the DMA controller that
1560 * the current DMA transfer has completed! If the NCR5380 is then taken
1561 * out of DMA mode, this already-acknowledged byte is lost. This is
1562 * not a problem for "one DMA transfer per READ command", because
1563 * the situation will never arise... either all of the data is DMA'ed
1564 * properly, or the target switches to MESSAGE IN phase to signal a
1565 * disconnection (either operation bringing the DMA to a clean halt).
1566 * However, in order to handle scatter-receive, we must work around the
1567 * problem. The chosen fix is to DMA fewer bytes, then check for the
1568 * condition before taking the NCR5380 out of DMA mode. One or two extra
1569 * bytes are transferred via PIO as necessary to fill out the original
1573 if (hostdata
->flags
& FLAG_DMA_FIXUP
) {
1576 * The workaround was to transfer fewer bytes than we
1577 * intended to with the pseudo-DMA read function, wait for
1578 * the chip to latch the last byte, read it, and then disable
1581 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1582 * REQ is deasserted when ACK is asserted, and not reasserted
1583 * until ACK goes false. Since the NCR5380 won't lower ACK
1584 * until DACK is asserted, which won't happen unless we twiddle
1585 * the DMA port or we take the NCR5380 out of DMA mode, we
1586 * can guarantee that we won't handshake another extra
1590 if (NCR5380_poll_politely(hostdata
, BUS_AND_STATUS_REG
,
1591 BASR_DRQ
, BASR_DRQ
, HZ
) < 0) {
1593 shost_printk(KERN_ERR
, instance
, "PDMA read: DRQ timeout\n");
1595 if (NCR5380_poll_politely(hostdata
, STATUS_REG
,
1596 SR_REQ
, 0, HZ
) < 0) {
1598 shost_printk(KERN_ERR
, instance
, "PDMA read: !REQ timeout\n");
1600 d
[*count
- 1] = NCR5380_read(INPUT_DATA_REG
);
1603 * Wait for the last byte to be sent. If REQ is being asserted for
1604 * the byte we're interested, we'll ACK it and it will go false.
1606 if (NCR5380_poll_politely2(hostdata
,
1607 BUS_AND_STATUS_REG
, BASR_DRQ
, BASR_DRQ
,
1608 BUS_AND_STATUS_REG
, BASR_PHASE_MATCH
, 0, HZ
) < 0) {
1610 shost_printk(KERN_ERR
, instance
, "PDMA write: DRQ and phase timeout\n");
1615 NCR5380_dma_complete(instance
);
1620 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1622 * Purpose : run through the various SCSI phases and do as the target
1623 * directs us to. Operates on the currently connected command,
1624 * instance->connected.
1626 * Inputs : instance, instance for which we are doing commands
1628 * Side effects : SCSI things happen, the disconnected queue will be
1629 * modified if a command disconnects, *instance->connected will
1632 * XXX Note : we need to watch for bus free or a reset condition here
1633 * to recover from an unexpected bus free condition.
1636 static void NCR5380_information_transfer(struct Scsi_Host
*instance
)
1637 __releases(&hostdata
->lock
) __acquires(&hostdata
->lock
)
1639 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1640 unsigned char msgout
= NOP
;
1644 unsigned char *data
;
1645 unsigned char phase
, tmp
, extended_msg
[10], old_phase
= 0xff;
1646 struct scsi_cmnd
*cmd
;
1648 #ifdef SUN3_SCSI_VME
1649 dregs
->csr
|= CSR_INTR
;
1652 while ((cmd
= hostdata
->connected
)) {
1653 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(cmd
);
1655 tmp
= NCR5380_read(STATUS_REG
);
1656 /* We only have a valid SCSI phase when REQ is asserted */
1658 phase
= (tmp
& PHASE_MASK
);
1659 if (phase
!= old_phase
) {
1661 NCR5380_dprint_phase(NDEBUG_INFORMATION
, instance
);
1664 if (phase
== PHASE_CMDOUT
&&
1665 sun3_dma_setup_done
!= cmd
) {
1668 if (!cmd
->SCp
.this_residual
&& cmd
->SCp
.buffers_residual
) {
1670 --cmd
->SCp
.buffers_residual
;
1671 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
1672 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
1675 count
= sun3scsi_dma_xfer_len(hostdata
, cmd
);
1678 if (rq_data_dir(cmd
->request
))
1679 sun3scsi_dma_send_setup(hostdata
,
1680 cmd
->SCp
.ptr
, count
);
1682 sun3scsi_dma_recv_setup(hostdata
,
1683 cmd
->SCp
.ptr
, count
);
1684 sun3_dma_setup_done
= cmd
;
1686 #ifdef SUN3_SCSI_VME
1687 dregs
->csr
|= CSR_INTR
;
1690 #endif /* CONFIG_SUN3 */
1692 if (sink
&& (phase
!= PHASE_MSGOUT
)) {
1693 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1695 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
|
1697 while (NCR5380_read(STATUS_REG
) & SR_REQ
)
1699 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1707 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1708 shost_printk(KERN_DEBUG
, instance
, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
1711 cmd
->result
= DID_ERROR
<< 16;
1712 complete_cmd(instance
, cmd
);
1713 hostdata
->connected
= NULL
;
1718 * If there is no room left in the current buffer in the
1719 * scatter-gather list, move onto the next one.
1722 if (!cmd
->SCp
.this_residual
&& cmd
->SCp
.buffers_residual
) {
1724 --cmd
->SCp
.buffers_residual
;
1725 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
1726 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
1727 dsprintk(NDEBUG_INFORMATION
, instance
, "%d bytes and %d buffers left\n",
1728 cmd
->SCp
.this_residual
,
1729 cmd
->SCp
.buffers_residual
);
1733 * The preferred transfer method is going to be
1734 * PSEUDO-DMA for systems that are strictly PIO,
1735 * since we can let the hardware do the handshaking.
1737 * For this to work, we need to know the transfersize
1738 * ahead of time, since the pseudo-DMA code will sit
1739 * in an unconditional loop.
1743 if (!cmd
->device
->borken
)
1744 transfersize
= NCR5380_dma_xfer_len(hostdata
, cmd
);
1746 if (transfersize
> 0) {
1748 if (NCR5380_transfer_dma(instance
, &phase
,
1749 &len
, (unsigned char **)&cmd
->SCp
.ptr
)) {
1751 * If the watchdog timer fires, all future
1752 * accesses to this device will use the
1755 scmd_printk(KERN_INFO
, cmd
,
1756 "switching to slow handshake\n");
1757 cmd
->device
->borken
= 1;
1760 cmd
->result
= DID_ERROR
<< 16;
1761 /* XXX - need to source or sink data here, as appropriate */
1764 /* Transfer a small chunk so that the
1765 * irq mode lock is not held too long.
1767 transfersize
= min(cmd
->SCp
.this_residual
,
1768 NCR5380_PIO_CHUNK_SIZE
);
1770 NCR5380_transfer_pio(instance
, &phase
, &len
,
1771 (unsigned char **)&cmd
->SCp
.ptr
);
1772 cmd
->SCp
.this_residual
-= transfersize
- len
;
1775 if (sun3_dma_setup_done
== cmd
)
1776 sun3_dma_setup_done
= NULL
;
1782 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1783 cmd
->SCp
.Message
= tmp
;
1787 case COMMAND_COMPLETE
:
1788 /* Accept message by clearing ACK */
1790 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1791 dsprintk(NDEBUG_QUEUES
, instance
,
1792 "COMMAND COMPLETE %p target %d lun %llu\n",
1793 cmd
, scmd_id(cmd
), cmd
->device
->lun
);
1795 hostdata
->connected
= NULL
;
1797 cmd
->result
&= ~0xffff;
1798 cmd
->result
|= cmd
->SCp
.Status
;
1799 cmd
->result
|= cmd
->SCp
.Message
<< 8;
1801 if (cmd
->cmnd
[0] == REQUEST_SENSE
)
1802 complete_cmd(instance
, cmd
);
1804 if (cmd
->SCp
.Status
== SAM_STAT_CHECK_CONDITION
||
1805 cmd
->SCp
.Status
== SAM_STAT_COMMAND_TERMINATED
) {
1806 dsprintk(NDEBUG_QUEUES
, instance
, "autosense: adding cmd %p to tail of autosense queue\n",
1808 list_add_tail(&ncmd
->list
,
1809 &hostdata
->autosense
);
1811 complete_cmd(instance
, cmd
);
1815 * Restore phase bits to 0 so an interrupted selection,
1816 * arbitration can resume.
1818 NCR5380_write(TARGET_COMMAND_REG
, 0);
1820 /* Enable reselect interrupts */
1821 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1823 maybe_release_dma_irq(instance
);
1825 case MESSAGE_REJECT
:
1826 /* Accept message by clearing ACK */
1827 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1828 switch (hostdata
->last_message
) {
1829 case HEAD_OF_QUEUE_TAG
:
1830 case ORDERED_QUEUE_TAG
:
1831 case SIMPLE_QUEUE_TAG
:
1832 cmd
->device
->simple_tags
= 0;
1833 hostdata
->busy
[cmd
->device
->id
] |= (1 << (cmd
->device
->lun
& 0xFF));
1840 /* Accept message by clearing ACK */
1841 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1842 hostdata
->connected
= NULL
;
1843 list_add(&ncmd
->list
, &hostdata
->disconnected
);
1844 dsprintk(NDEBUG_INFORMATION
| NDEBUG_QUEUES
,
1845 instance
, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1846 cmd
, scmd_id(cmd
), cmd
->device
->lun
);
1849 * Restore phase bits to 0 so an interrupted selection,
1850 * arbitration can resume.
1852 NCR5380_write(TARGET_COMMAND_REG
, 0);
1854 /* Enable reselect interrupts */
1855 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1856 #ifdef SUN3_SCSI_VME
1857 dregs
->csr
|= CSR_DMA_ENABLE
;
1861 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1862 * operation, in violation of the SCSI spec so we can safely
1863 * ignore SAVE/RESTORE pointers calls.
1865 * Unfortunately, some disks violate the SCSI spec and
1866 * don't issue the required SAVE_POINTERS message before
1867 * disconnecting, and we have to break spec to remain
1871 case RESTORE_POINTERS
:
1872 /* Accept message by clearing ACK */
1873 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1875 case EXTENDED_MESSAGE
:
1877 * Start the message buffer with the EXTENDED_MESSAGE
1878 * byte, since spi_print_msg() wants the whole thing.
1880 extended_msg
[0] = EXTENDED_MESSAGE
;
1881 /* Accept first byte by clearing ACK */
1882 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1884 spin_unlock_irq(&hostdata
->lock
);
1886 dsprintk(NDEBUG_EXTENDED
, instance
, "receiving extended message\n");
1889 data
= extended_msg
+ 1;
1890 phase
= PHASE_MSGIN
;
1891 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1892 dsprintk(NDEBUG_EXTENDED
, instance
, "length %d, code 0x%02x\n",
1893 (int)extended_msg
[1],
1894 (int)extended_msg
[2]);
1896 if (!len
&& extended_msg
[1] > 0 &&
1897 extended_msg
[1] <= sizeof(extended_msg
) - 2) {
1898 /* Accept third byte by clearing ACK */
1899 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1900 len
= extended_msg
[1] - 1;
1901 data
= extended_msg
+ 3;
1902 phase
= PHASE_MSGIN
;
1904 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1905 dsprintk(NDEBUG_EXTENDED
, instance
, "message received, residual %d\n",
1908 switch (extended_msg
[2]) {
1914 shost_printk(KERN_ERR
, instance
, "error receiving extended message\n");
1917 shost_printk(KERN_NOTICE
, instance
, "extended message code %02x length %d is too long\n",
1918 extended_msg
[2], extended_msg
[1]);
1922 spin_lock_irq(&hostdata
->lock
);
1923 if (!hostdata
->connected
)
1926 /* Fall through to reject message */
1929 * If we get something weird that we aren't expecting,
1933 if (tmp
== EXTENDED_MESSAGE
)
1934 scmd_printk(KERN_INFO
, cmd
,
1935 "rejecting unknown extended message code %02x, length %d\n",
1936 extended_msg
[2], extended_msg
[1]);
1938 scmd_printk(KERN_INFO
, cmd
,
1939 "rejecting unknown message code %02x\n",
1942 msgout
= MESSAGE_REJECT
;
1943 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1945 } /* switch (tmp) */
1950 hostdata
->last_message
= msgout
;
1951 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1952 if (msgout
== ABORT
) {
1953 hostdata
->connected
= NULL
;
1954 cmd
->result
= DID_ERROR
<< 16;
1955 complete_cmd(instance
, cmd
);
1956 maybe_release_dma_irq(instance
);
1957 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1966 * XXX for performance reasons, on machines with a
1967 * PSEUDO-DMA architecture we should probably
1968 * use the dma transfer function.
1970 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1975 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1976 cmd
->SCp
.Status
= tmp
;
1979 shost_printk(KERN_ERR
, instance
, "unknown phase\n");
1980 NCR5380_dprint(NDEBUG_ANY
, instance
);
1981 } /* switch(phase) */
1983 spin_unlock_irq(&hostdata
->lock
);
1984 NCR5380_poll_politely(hostdata
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
);
1985 spin_lock_irq(&hostdata
->lock
);
1991 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
1993 * Purpose : does reselection, initializing the instance->connected
1994 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1995 * nexus has been reestablished,
1997 * Inputs : instance - this instance of the NCR5380.
2000 static void NCR5380_reselect(struct Scsi_Host
*instance
)
2002 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2003 unsigned char target_mask
;
2005 unsigned char msg
[3];
2006 struct NCR5380_cmd
*ncmd
;
2007 struct scsi_cmnd
*tmp
;
2010 * Disable arbitration, etc. since the host adapter obviously
2011 * lost, and tell an interrupted NCR5380_select() to restart.
2014 NCR5380_write(MODE_REG
, MR_BASE
);
2016 target_mask
= NCR5380_read(CURRENT_SCSI_DATA_REG
) & ~(hostdata
->id_mask
);
2018 dsprintk(NDEBUG_RESELECTION
, instance
, "reselect\n");
2021 * At this point, we have detected that our SCSI ID is on the bus,
2022 * SEL is true and BSY was false for at least one bus settle delay
2025 * We must assert BSY ourselves, until the target drops the SEL
2029 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
);
2030 if (NCR5380_poll_politely(hostdata
,
2031 STATUS_REG
, SR_SEL
, 0, 2 * HZ
) < 0) {
2032 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2035 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2038 * Wait for target to go into MSGIN.
2041 if (NCR5380_poll_politely(hostdata
,
2042 STATUS_REG
, SR_REQ
, SR_REQ
, 2 * HZ
) < 0) {
2048 /* acknowledge toggle to MSGIN */
2049 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(PHASE_MSGIN
));
2051 /* peek at the byte without really hitting the bus */
2052 msg
[0] = NCR5380_read(CURRENT_SCSI_DATA_REG
);
2056 unsigned char *data
= msg
;
2057 unsigned char phase
= PHASE_MSGIN
;
2059 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
2066 #endif /* CONFIG_SUN3 */
2068 if (!(msg
[0] & 0x80)) {
2069 shost_printk(KERN_ERR
, instance
, "expecting IDENTIFY message, got ");
2075 lun
= msg
[0] & 0x07;
2078 * We need to add code for SCSI-II to track which devices have
2079 * I_T_L_Q nexuses established, and which have simple I_T_L
2080 * nexuses so we can chose to do additional data transfer.
2084 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2085 * just reestablished, and remove it from the disconnected queue.
2089 list_for_each_entry(ncmd
, &hostdata
->disconnected
, list
) {
2090 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
2092 if (target_mask
== (1 << scmd_id(cmd
)) &&
2093 lun
== (u8
)cmd
->device
->lun
) {
2094 list_del(&ncmd
->list
);
2101 dsprintk(NDEBUG_RESELECTION
| NDEBUG_QUEUES
, instance
,
2102 "reselect: removed %p from disconnected queue\n", tmp
);
2104 shost_printk(KERN_ERR
, instance
, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2107 * Since we have an established nexus that we can't do anything
2108 * with, we must abort it.
2115 if (sun3_dma_setup_done
!= tmp
) {
2118 if (!tmp
->SCp
.this_residual
&& tmp
->SCp
.buffers_residual
) {
2120 --tmp
->SCp
.buffers_residual
;
2121 tmp
->SCp
.this_residual
= tmp
->SCp
.buffer
->length
;
2122 tmp
->SCp
.ptr
= sg_virt(tmp
->SCp
.buffer
);
2125 count
= sun3scsi_dma_xfer_len(hostdata
, tmp
);
2128 if (rq_data_dir(tmp
->request
))
2129 sun3scsi_dma_send_setup(hostdata
,
2130 tmp
->SCp
.ptr
, count
);
2132 sun3scsi_dma_recv_setup(hostdata
,
2133 tmp
->SCp
.ptr
, count
);
2134 sun3_dma_setup_done
= tmp
;
2138 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
2139 #endif /* CONFIG_SUN3 */
2141 /* Accept message by clearing ACK */
2142 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2144 hostdata
->connected
= tmp
;
2145 dsprintk(NDEBUG_RESELECTION
, instance
, "nexus established, target %d, lun %llu\n",
2146 scmd_id(tmp
), tmp
->device
->lun
);
2150 * list_find_cmd - test for presence of a command in a linked list
2151 * @haystack: list of commands
2152 * @needle: command to search for
2155 static bool list_find_cmd(struct list_head
*haystack
,
2156 struct scsi_cmnd
*needle
)
2158 struct NCR5380_cmd
*ncmd
;
2160 list_for_each_entry(ncmd
, haystack
, list
)
2161 if (NCR5380_to_scmd(ncmd
) == needle
)
2167 * list_remove_cmd - remove a command from linked list
2168 * @haystack: list of commands
2169 * @needle: command to remove
2172 static bool list_del_cmd(struct list_head
*haystack
,
2173 struct scsi_cmnd
*needle
)
2175 if (list_find_cmd(haystack
, needle
)) {
2176 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(needle
);
2178 list_del(&ncmd
->list
);
2185 * NCR5380_abort - scsi host eh_abort_handler() method
2186 * @cmd: the command to be aborted
2188 * Try to abort a given command by removing it from queues and/or sending
2189 * the target an abort message. This may not succeed in causing a target
2190 * to abort the command. Nonetheless, the low-level driver must forget about
2191 * the command because the mid-layer reclaims it and it may be re-issued.
2193 * The normal path taken by a command is as follows. For EH we trace this
2194 * same path to locate and abort the command.
2196 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2197 * [disconnected -> connected ->]...
2198 * [autosense -> connected ->] done
2200 * If cmd was not found at all then presumably it has already been completed,
2201 * in which case return SUCCESS to try to avoid further EH measures.
2203 * If the command has not completed yet, we must not fail to find it.
2204 * We have no option but to forget the aborted command (even if it still
2205 * lacks sense data). The mid-layer may re-issue a command that is in error
2206 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2207 * this driver are such that a command can appear on one queue only.
2209 * The lock protects driver data structures, but EH handlers also use it
2210 * to serialize their own execution and prevent their own re-entry.
2213 static int NCR5380_abort(struct scsi_cmnd
*cmd
)
2215 struct Scsi_Host
*instance
= cmd
->device
->host
;
2216 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2217 unsigned long flags
;
2218 int result
= SUCCESS
;
2220 spin_lock_irqsave(&hostdata
->lock
, flags
);
2222 #if (NDEBUG & NDEBUG_ANY)
2223 scmd_printk(KERN_INFO
, cmd
, __func__
);
2225 NCR5380_dprint(NDEBUG_ANY
, instance
);
2226 NCR5380_dprint_phase(NDEBUG_ANY
, instance
);
2228 if (list_del_cmd(&hostdata
->unissued
, cmd
)) {
2229 dsprintk(NDEBUG_ABORT
, instance
,
2230 "abort: removed %p from issue queue\n", cmd
);
2231 cmd
->result
= DID_ABORT
<< 16;
2232 cmd
->scsi_done(cmd
); /* No tag or busy flag to worry about */
2236 if (hostdata
->selecting
== cmd
) {
2237 dsprintk(NDEBUG_ABORT
, instance
,
2238 "abort: cmd %p == selecting\n", cmd
);
2239 hostdata
->selecting
= NULL
;
2240 cmd
->result
= DID_ABORT
<< 16;
2241 complete_cmd(instance
, cmd
);
2245 if (list_del_cmd(&hostdata
->disconnected
, cmd
)) {
2246 dsprintk(NDEBUG_ABORT
, instance
,
2247 "abort: removed %p from disconnected list\n", cmd
);
2248 /* Can't call NCR5380_select() and send ABORT because that
2249 * means releasing the lock. Need a bus reset.
2251 set_host_byte(cmd
, DID_ERROR
);
2252 complete_cmd(instance
, cmd
);
2257 if (hostdata
->connected
== cmd
) {
2258 dsprintk(NDEBUG_ABORT
, instance
, "abort: cmd %p is connected\n", cmd
);
2259 hostdata
->connected
= NULL
;
2260 hostdata
->dma_len
= 0;
2261 if (do_abort(instance
)) {
2262 set_host_byte(cmd
, DID_ERROR
);
2263 complete_cmd(instance
, cmd
);
2267 set_host_byte(cmd
, DID_ABORT
);
2268 complete_cmd(instance
, cmd
);
2272 if (list_del_cmd(&hostdata
->autosense
, cmd
)) {
2273 dsprintk(NDEBUG_ABORT
, instance
,
2274 "abort: removed %p from sense queue\n", cmd
);
2275 set_host_byte(cmd
, DID_ERROR
);
2276 complete_cmd(instance
, cmd
);
2280 if (result
== FAILED
)
2281 dsprintk(NDEBUG_ABORT
, instance
, "abort: failed to abort %p\n", cmd
);
2283 dsprintk(NDEBUG_ABORT
, instance
, "abort: successfully aborted %p\n", cmd
);
2285 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
2286 maybe_release_dma_irq(instance
);
2287 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
2294 * NCR5380_host_reset - reset the SCSI host
2295 * @cmd: SCSI command undergoing EH
2300 static int NCR5380_host_reset(struct scsi_cmnd
*cmd
)
2302 struct Scsi_Host
*instance
= cmd
->device
->host
;
2303 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2305 unsigned long flags
;
2306 struct NCR5380_cmd
*ncmd
;
2308 spin_lock_irqsave(&hostdata
->lock
, flags
);
2310 #if (NDEBUG & NDEBUG_ANY)
2311 scmd_printk(KERN_INFO
, cmd
, __func__
);
2313 NCR5380_dprint(NDEBUG_ANY
, instance
);
2314 NCR5380_dprint_phase(NDEBUG_ANY
, instance
);
2318 /* reset NCR registers */
2319 NCR5380_write(MODE_REG
, MR_BASE
);
2320 NCR5380_write(TARGET_COMMAND_REG
, 0);
2321 NCR5380_write(SELECT_ENABLE_REG
, 0);
2323 /* After the reset, there are no more connected or disconnected commands
2324 * and no busy units; so clear the low-level status here to avoid
2325 * conflicts when the mid-level code tries to wake up the affected
2329 if (list_del_cmd(&hostdata
->unissued
, cmd
)) {
2330 cmd
->result
= DID_RESET
<< 16;
2331 cmd
->scsi_done(cmd
);
2334 if (hostdata
->selecting
) {
2335 hostdata
->selecting
->result
= DID_RESET
<< 16;
2336 complete_cmd(instance
, hostdata
->selecting
);
2337 hostdata
->selecting
= NULL
;
2340 list_for_each_entry(ncmd
, &hostdata
->disconnected
, list
) {
2341 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
2343 set_host_byte(cmd
, DID_RESET
);
2344 complete_cmd(instance
, cmd
);
2346 INIT_LIST_HEAD(&hostdata
->disconnected
);
2348 list_for_each_entry(ncmd
, &hostdata
->autosense
, list
) {
2349 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
2351 set_host_byte(cmd
, DID_RESET
);
2352 cmd
->scsi_done(cmd
);
2354 INIT_LIST_HEAD(&hostdata
->autosense
);
2356 if (hostdata
->connected
) {
2357 set_host_byte(hostdata
->connected
, DID_RESET
);
2358 complete_cmd(instance
, hostdata
->connected
);
2359 hostdata
->connected
= NULL
;
2362 for (i
= 0; i
< 8; ++i
)
2363 hostdata
->busy
[i
] = 0;
2364 hostdata
->dma_len
= 0;
2366 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
2367 maybe_release_dma_irq(instance
);
2368 spin_unlock_irqrestore(&hostdata
->lock
, flags
);