2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
6 * Note that these routines also work with NR53c400 family chips.
8 * Copyright 1993, Drew Eckhardt
10 * (Unix and Linux consulting and custom programming)
14 * For more information, please consult
17 * SCSI Protocol Controller
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
28 * With contributions from Ray Van Tassle, Ingmar Baumgart,
29 * Ronald van Cuijlenborg, Alan Cox and others.
33 * Further development / testing that should be done :
35 * 4. Test SCSI-II tagged queueing (I have no devices which support
42 * This is a generic 5380 driver. To use it on a different platform,
43 * one simply writes appropriate system specific macros (ie, data
44 * transfer - some PC's will use the I/O bus, 68K's must use
45 * memory mapped) and drops this file in their 'C' wrapper.
47 * As far as command queueing, two queues are maintained for
48 * each 5380 in the system - commands that haven't been issued yet,
49 * and commands that are currently executing. This means that an
50 * unlimited number of commands may be queued, letting
51 * more commands propagate from the higher driver levels giving higher
52 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
53 * allowing multiple commands to propagate all the way to a SCSI-II device
54 * while a command is already executing.
57 * Issues specific to the NCR5380 :
59 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
60 * piece of hardware that requires you to sit in a loop polling for
61 * the REQ signal as long as you are connected. Some devices are
62 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
63 * while doing long seek operations. [...] These
64 * broken devices are the exception rather than the rule and I'd rather
65 * spend my time optimizing for the normal case.
69 * At the heart of the design is a coroutine, NCR5380_main,
70 * which is started from a workqueue for each NCR5380 host in the
71 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
72 * removing the commands from the issue queue and calling
73 * NCR5380_select() if a nexus is not established.
75 * Once a nexus is established, the NCR5380_information_transfer()
76 * phase goes through the various phases as instructed by the target.
77 * if the target goes into MSG IN and sends a DISCONNECT message,
78 * the command structure is placed into the per instance disconnected
79 * queue, and NCR5380_main tries to find more work. If the target is
80 * idle for too long, the system will try to sleep.
82 * If a command has disconnected, eventually an interrupt will trigger,
83 * calling NCR5380_intr() which will in turn call NCR5380_reselect
84 * to reestablish a nexus. This will run main if necessary.
86 * On command termination, the done function will be called as
89 * SCSI pointers are maintained in the SCp field of SCSI command
90 * structures, being initialized after the command is connected
91 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
92 * Note that in violation of the standard, an implicit SAVE POINTERS operation
93 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
98 * This file a skeleton Linux SCSI driver for the NCR 5380 series
99 * of chips. To use it, you write an architecture specific functions
100 * and macros and include this file in your driver.
102 * These macros control options :
103 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
106 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
107 * for commands that return with a CHECK CONDITION status.
109 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
112 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
113 * override-configure an IRQ.
115 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
117 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
119 * These macros MUST be defined :
121 * NCR5380_read(register) - read from the specified register
123 * NCR5380_write(register, value) - write to the specific register
125 * NCR5380_implementation_fields - additional fields needed for this
126 * specific implementation of the NCR5380
128 * Either real DMA *or* pseudo DMA may be implemented
130 * NCR5380_dma_write_setup(instance, src, count) - initialize
131 * NCR5380_dma_read_setup(instance, dst, count) - initialize
132 * NCR5380_dma_residual(instance); - residual count
134 * The generic driver is initialized by calling NCR5380_init(instance),
135 * after setting the appropriate host specific fields and ID. If the
136 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
137 * possible) function may be used.
140 #ifndef NCR5380_io_delay
141 #define NCR5380_io_delay(x)
144 static int do_abort(struct Scsi_Host
*);
145 static void do_reset(struct Scsi_Host
*);
148 * initialize_SCp - init the scsi pointer field
149 * @cmd: command block to set up
151 * Set up the internal fields in the SCSI command.
154 static inline void initialize_SCp(struct scsi_cmnd
*cmd
)
157 * Initialize the Scsi Pointer field so that all of the commands in the
158 * various queues are valid.
161 if (scsi_bufflen(cmd
)) {
162 cmd
->SCp
.buffer
= scsi_sglist(cmd
);
163 cmd
->SCp
.buffers_residual
= scsi_sg_count(cmd
) - 1;
164 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
165 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
167 cmd
->SCp
.buffer
= NULL
;
168 cmd
->SCp
.buffers_residual
= 0;
170 cmd
->SCp
.this_residual
= 0;
174 cmd
->SCp
.Message
= 0;
178 * NCR5380_poll_politely2 - wait for two chip register values
179 * @instance: controller to poll
180 * @reg1: 5380 register to poll
181 * @bit1: Bitmask to check
182 * @val1: Expected value
183 * @reg2: Second 5380 register to poll
184 * @bit2: Second bitmask to check
185 * @val2: Second expected value
186 * @wait: Time-out in jiffies
188 * Polls the chip in a reasonably efficient manner waiting for an
189 * event to occur. After a short quick poll we begin to yield the CPU
190 * (if possible). In irq contexts the time-out is arbitrarily limited.
191 * Callers may hold locks as long as they are held in irq mode.
193 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
196 static int NCR5380_poll_politely2(struct Scsi_Host
*instance
,
197 int reg1
, int bit1
, int val1
,
198 int reg2
, int bit2
, int val2
, int wait
)
200 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
201 unsigned long deadline
= jiffies
+ wait
;
204 /* Busy-wait for up to 10 ms */
205 n
= min(10000U, jiffies_to_usecs(wait
));
206 n
*= hostdata
->accesses_per_ms
;
209 if ((NCR5380_read(reg1
) & bit1
) == val1
)
211 if ((NCR5380_read(reg2
) & bit2
) == val2
)
216 if (irqs_disabled() || in_interrupt())
219 /* Repeatedly sleep for 1 ms until deadline */
220 while (time_is_after_jiffies(deadline
)) {
221 schedule_timeout_uninterruptible(1);
222 if ((NCR5380_read(reg1
) & bit1
) == val1
)
224 if ((NCR5380_read(reg2
) & bit2
) == val2
)
231 static inline int NCR5380_poll_politely(struct Scsi_Host
*instance
,
232 int reg
, int bit
, int val
, int wait
)
234 return NCR5380_poll_politely2(instance
, reg
, bit
, val
,
235 reg
, bit
, val
, wait
);
259 {ICR_ASSERT_RST
, "ASSERT RST"},
260 {ICR_ASSERT_ACK
, "ASSERT ACK"},
261 {ICR_ASSERT_BSY
, "ASSERT BSY"},
262 {ICR_ASSERT_SEL
, "ASSERT SEL"},
263 {ICR_ASSERT_ATN
, "ASSERT ATN"},
264 {ICR_ASSERT_DATA
, "ASSERT DATA"},
268 {MR_BLOCK_DMA_MODE
, "MODE BLOCK DMA"},
269 {MR_TARGET
, "MODE TARGET"},
270 {MR_ENABLE_PAR_CHECK
, "MODE PARITY CHECK"},
271 {MR_ENABLE_PAR_INTR
, "MODE PARITY INTR"},
272 {MR_ENABLE_EOP_INTR
, "MODE EOP INTR"},
273 {MR_MONITOR_BSY
, "MODE MONITOR BSY"},
274 {MR_DMA_MODE
, "MODE DMA"},
275 {MR_ARBITRATE
, "MODE ARBITRATION"},
280 * NCR5380_print - print scsi bus signals
281 * @instance: adapter state to dump
283 * Print the SCSI bus signals for debugging purposes
286 static void NCR5380_print(struct Scsi_Host
*instance
)
288 unsigned char status
, data
, basr
, mr
, icr
, i
;
290 data
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
291 status
= NCR5380_read(STATUS_REG
);
292 mr
= NCR5380_read(MODE_REG
);
293 icr
= NCR5380_read(INITIATOR_COMMAND_REG
);
294 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
296 printk("STATUS_REG: %02x ", status
);
297 for (i
= 0; signals
[i
].mask
; ++i
)
298 if (status
& signals
[i
].mask
)
299 printk(",%s", signals
[i
].name
);
300 printk("\nBASR: %02x ", basr
);
301 for (i
= 0; basrs
[i
].mask
; ++i
)
302 if (basr
& basrs
[i
].mask
)
303 printk(",%s", basrs
[i
].name
);
304 printk("\nICR: %02x ", icr
);
305 for (i
= 0; icrs
[i
].mask
; ++i
)
306 if (icr
& icrs
[i
].mask
)
307 printk(",%s", icrs
[i
].name
);
308 printk("\nMODE: %02x ", mr
);
309 for (i
= 0; mrs
[i
].mask
; ++i
)
310 if (mr
& mrs
[i
].mask
)
311 printk(",%s", mrs
[i
].name
);
319 {PHASE_DATAOUT
, "DATAOUT"},
320 {PHASE_DATAIN
, "DATAIN"},
321 {PHASE_CMDOUT
, "CMDOUT"},
322 {PHASE_STATIN
, "STATIN"},
323 {PHASE_MSGOUT
, "MSGOUT"},
324 {PHASE_MSGIN
, "MSGIN"},
325 {PHASE_UNKNOWN
, "UNKNOWN"}
329 * NCR5380_print_phase - show SCSI phase
330 * @instance: adapter to dump
332 * Print the current SCSI phase for debugging purposes
335 static void NCR5380_print_phase(struct Scsi_Host
*instance
)
337 unsigned char status
;
340 status
= NCR5380_read(STATUS_REG
);
341 if (!(status
& SR_REQ
))
342 shost_printk(KERN_DEBUG
, instance
, "REQ not asserted, phase unknown.\n");
344 for (i
= 0; (phases
[i
].value
!= PHASE_UNKNOWN
) &&
345 (phases
[i
].value
!= (status
& PHASE_MASK
)); ++i
)
347 shost_printk(KERN_DEBUG
, instance
, "phase %s\n", phases
[i
].name
);
353 static int probe_irq __initdata
;
356 * probe_intr - helper for IRQ autoprobe
357 * @irq: interrupt number
361 * Set a flag to indicate the IRQ in question was received. This is
362 * used by the IRQ probe code.
365 static irqreturn_t __init
probe_intr(int irq
, void *dev_id
)
372 * NCR5380_probe_irq - find the IRQ of an NCR5380
373 * @instance: NCR5380 controller
374 * @possible: bitmask of ISA IRQ lines
376 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
377 * and then looking to see what interrupt actually turned up.
380 static int __init __maybe_unused
NCR5380_probe_irq(struct Scsi_Host
*instance
,
383 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
384 unsigned long timeout
;
385 int trying_irqs
, i
, mask
;
387 for (trying_irqs
= 0, i
= 1, mask
= 2; i
< 16; ++i
, mask
<<= 1)
388 if ((mask
& possible
) && (request_irq(i
, &probe_intr
, 0, "NCR-probe", NULL
) == 0))
391 timeout
= jiffies
+ msecs_to_jiffies(250);
395 * A interrupt is triggered whenever BSY = false, SEL = true
396 * and a bit set in the SELECT_ENABLE_REG is asserted on the
399 * Note that the bus is only driven when the phase control signals
400 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
404 NCR5380_write(TARGET_COMMAND_REG
, 0);
405 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
406 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
407 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
| ICR_ASSERT_SEL
);
409 while (probe_irq
== NO_IRQ
&& time_before(jiffies
, timeout
))
410 schedule_timeout_uninterruptible(1);
412 NCR5380_write(SELECT_ENABLE_REG
, 0);
413 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
415 for (i
= 1, mask
= 2; i
< 16; ++i
, mask
<<= 1)
416 if (trying_irqs
& mask
)
423 * NCR58380_info - report driver and host information
424 * @instance: relevant scsi host instance
426 * For use as the host template info() handler.
429 static const char *NCR5380_info(struct Scsi_Host
*instance
)
431 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
433 return hostdata
->info
;
436 static void prepare_info(struct Scsi_Host
*instance
)
438 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
440 snprintf(hostdata
->info
, sizeof(hostdata
->info
),
441 "%s, io_port 0x%lx, n_io_port %d, "
442 "base 0x%lx, irq %d, "
443 "can_queue %d, cmd_per_lun %d, "
444 "sg_tablesize %d, this_id %d, "
447 instance
->hostt
->name
, instance
->io_port
, instance
->n_io_port
,
448 instance
->base
, instance
->irq
,
449 instance
->can_queue
, instance
->cmd_per_lun
,
450 instance
->sg_tablesize
, instance
->this_id
,
451 hostdata
->flags
& FLAG_DMA_FIXUP
? "DMA_FIXUP " : "",
452 hostdata
->flags
& FLAG_NO_PSEUDO_DMA
? "NO_PSEUDO_DMA " : "",
453 hostdata
->flags
& FLAG_TOSHIBA_DELAY
? "TOSHIBA_DELAY " : "",
467 * NCR5380_init - initialise an NCR5380
468 * @instance: adapter to configure
469 * @flags: control flags
471 * Initializes *instance and corresponding 5380 chip,
472 * with flags OR'd into the initial flags value.
474 * Notes : I assume that the host, hostno, and id bits have been
475 * set correctly. I don't care about the irq and other fields.
477 * Returns 0 for success
480 static int NCR5380_init(struct Scsi_Host
*instance
, int flags
)
482 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
484 unsigned long deadline
;
486 hostdata
->host
= instance
;
487 hostdata
->id_mask
= 1 << instance
->this_id
;
488 hostdata
->id_higher_mask
= 0;
489 for (i
= hostdata
->id_mask
; i
<= 0x80; i
<<= 1)
490 if (i
> hostdata
->id_mask
)
491 hostdata
->id_higher_mask
|= i
;
492 for (i
= 0; i
< 8; ++i
)
493 hostdata
->busy
[i
] = 0;
494 hostdata
->dma_len
= 0;
496 spin_lock_init(&hostdata
->lock
);
497 hostdata
->connected
= NULL
;
498 hostdata
->sensing
= NULL
;
499 INIT_LIST_HEAD(&hostdata
->autosense
);
500 INIT_LIST_HEAD(&hostdata
->unissued
);
501 INIT_LIST_HEAD(&hostdata
->disconnected
);
503 hostdata
->flags
= flags
;
505 INIT_WORK(&hostdata
->main_task
, NCR5380_main
);
506 hostdata
->work_q
= alloc_workqueue("ncr5380_%d",
507 WQ_UNBOUND
| WQ_MEM_RECLAIM
,
508 1, instance
->host_no
);
509 if (!hostdata
->work_q
)
512 prepare_info(instance
);
514 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
515 NCR5380_write(MODE_REG
, MR_BASE
);
516 NCR5380_write(TARGET_COMMAND_REG
, 0);
517 NCR5380_write(SELECT_ENABLE_REG
, 0);
519 /* Calibrate register polling loop */
521 deadline
= jiffies
+ 1;
524 } while (time_is_after_jiffies(deadline
));
525 deadline
+= msecs_to_jiffies(256);
527 NCR5380_read(STATUS_REG
);
530 } while (time_is_after_jiffies(deadline
));
531 hostdata
->accesses_per_ms
= i
/ 256;
537 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
538 * @instance: adapter to check
540 * If the system crashed, it may have crashed with a connected target and
541 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
542 * currently established nexus, which we know nothing about. Failing that
545 * Note that a bus reset will cause the chip to assert IRQ.
547 * Returns 0 if successful, otherwise -ENXIO.
550 static int NCR5380_maybe_reset_bus(struct Scsi_Host
*instance
)
552 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
555 for (pass
= 1; (NCR5380_read(STATUS_REG
) & SR_BSY
) && pass
<= 6; ++pass
) {
560 shost_printk(KERN_ERR
, instance
, "SCSI bus busy, waiting up to five seconds\n");
561 NCR5380_poll_politely(instance
,
562 STATUS_REG
, SR_BSY
, 0, 5 * HZ
);
565 shost_printk(KERN_ERR
, instance
, "bus busy, attempting abort\n");
569 shost_printk(KERN_ERR
, instance
, "bus busy, attempting reset\n");
571 /* Wait after a reset; the SCSI standard calls for
572 * 250ms, we wait 500ms to be on the safe side.
573 * But some Toshiba CD-ROMs need ten times that.
575 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
581 shost_printk(KERN_ERR
, instance
, "bus locked solid\n");
589 * NCR5380_exit - remove an NCR5380
590 * @instance: adapter to remove
592 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
595 static void NCR5380_exit(struct Scsi_Host
*instance
)
597 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
599 cancel_work_sync(&hostdata
->main_task
);
600 destroy_workqueue(hostdata
->work_q
);
604 * complete_cmd - finish processing a command and return it to the SCSI ML
605 * @instance: the host instance
606 * @cmd: command to complete
609 static void complete_cmd(struct Scsi_Host
*instance
,
610 struct scsi_cmnd
*cmd
)
612 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
614 dsprintk(NDEBUG_QUEUES
, instance
, "complete_cmd: cmd %p\n", cmd
);
616 if (hostdata
->sensing
== cmd
) {
617 /* Autosense processing ends here */
618 if ((cmd
->result
& 0xff) != SAM_STAT_GOOD
) {
619 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
620 set_host_byte(cmd
, DID_ERROR
);
622 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
623 hostdata
->sensing
= NULL
;
626 hostdata
->busy
[scmd_id(cmd
)] &= ~(1 << cmd
->device
->lun
);
632 * NCR5380_queue_command - queue a command
633 * @instance: the relevant SCSI adapter
636 * cmd is added to the per-instance issue queue, with minor
637 * twiddling done to the host specific fields of cmd. If the
638 * main coroutine is not running, it is restarted.
641 static int NCR5380_queue_command(struct Scsi_Host
*instance
,
642 struct scsi_cmnd
*cmd
)
644 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
645 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(cmd
);
648 #if (NDEBUG & NDEBUG_NO_WRITE)
649 switch (cmd
->cmnd
[0]) {
652 shost_printk(KERN_DEBUG
, instance
, "WRITE attempted with NDEBUG_NO_WRITE set\n");
653 cmd
->result
= (DID_ERROR
<< 16);
657 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
661 spin_lock_irqsave(&hostdata
->lock
, flags
);
664 * Insert the cmd into the issue queue. Note that REQUEST SENSE
665 * commands are added to the head of the queue since any command will
666 * clear the contingent allegiance condition that exists and the
667 * sense data is only guaranteed to be valid while the condition exists.
670 if (cmd
->cmnd
[0] == REQUEST_SENSE
)
671 list_add(&ncmd
->list
, &hostdata
->unissued
);
673 list_add_tail(&ncmd
->list
, &hostdata
->unissued
);
675 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
677 dsprintk(NDEBUG_QUEUES
, instance
, "command %p added to %s of queue\n",
678 cmd
, (cmd
->cmnd
[0] == REQUEST_SENSE
) ? "head" : "tail");
680 /* Kick off command processing */
681 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
686 * dequeue_next_cmd - dequeue a command for processing
687 * @instance: the scsi host instance
689 * Priority is given to commands on the autosense queue. These commands
690 * need autosense because of a CHECK CONDITION result.
692 * Returns a command pointer if a command is found for a target that is
693 * not already busy. Otherwise returns NULL.
696 static struct scsi_cmnd
*dequeue_next_cmd(struct Scsi_Host
*instance
)
698 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
699 struct NCR5380_cmd
*ncmd
;
700 struct scsi_cmnd
*cmd
;
702 if (hostdata
->sensing
|| list_empty(&hostdata
->autosense
)) {
703 list_for_each_entry(ncmd
, &hostdata
->unissued
, list
) {
704 cmd
= NCR5380_to_scmd(ncmd
);
705 dsprintk(NDEBUG_QUEUES
, instance
, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
706 cmd
, scmd_id(cmd
), hostdata
->busy
[scmd_id(cmd
)], cmd
->device
->lun
);
708 if (!(hostdata
->busy
[scmd_id(cmd
)] & (1 << cmd
->device
->lun
))) {
709 list_del(&ncmd
->list
);
710 dsprintk(NDEBUG_QUEUES
, instance
,
711 "dequeue: removed %p from issue queue\n", cmd
);
716 /* Autosense processing begins here */
717 ncmd
= list_first_entry(&hostdata
->autosense
,
718 struct NCR5380_cmd
, list
);
719 list_del(&ncmd
->list
);
720 cmd
= NCR5380_to_scmd(ncmd
);
721 dsprintk(NDEBUG_QUEUES
, instance
,
722 "dequeue: removed %p from autosense queue\n", cmd
);
723 scsi_eh_prep_cmnd(cmd
, &hostdata
->ses
, NULL
, 0, ~0);
724 hostdata
->sensing
= cmd
;
730 static void requeue_cmd(struct Scsi_Host
*instance
, struct scsi_cmnd
*cmd
)
732 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
733 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(cmd
);
735 if (hostdata
->sensing
== cmd
) {
736 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
737 list_add(&ncmd
->list
, &hostdata
->autosense
);
738 hostdata
->sensing
= NULL
;
740 list_add(&ncmd
->list
, &hostdata
->unissued
);
744 * NCR5380_main - NCR state machines
746 * NCR5380_main is a coroutine that runs as long as more work can
747 * be done on the NCR5380 host adapters in a system. Both
748 * NCR5380_queue_command() and NCR5380_intr() will try to start it
749 * in case it is not running.
752 static void NCR5380_main(struct work_struct
*work
)
754 struct NCR5380_hostdata
*hostdata
=
755 container_of(work
, struct NCR5380_hostdata
, main_task
);
756 struct Scsi_Host
*instance
= hostdata
->host
;
762 spin_lock_irq(&hostdata
->lock
);
763 while (!hostdata
->connected
&& !hostdata
->selecting
) {
764 struct scsi_cmnd
*cmd
= dequeue_next_cmd(instance
);
769 dsprintk(NDEBUG_MAIN
, instance
, "main: dequeued %p\n", cmd
);
772 * Attempt to establish an I_T_L nexus here.
773 * On success, instance->hostdata->connected is set.
774 * On failure, we must add the command back to the
775 * issue queue so we can keep trying.
778 * REQUEST SENSE commands are issued without tagged
779 * queueing, even on SCSI-II devices because the
780 * contingent allegiance condition exists for the
784 if (!NCR5380_select(instance
, cmd
)) {
785 dsprintk(NDEBUG_MAIN
, instance
, "main: select complete\n");
787 dsprintk(NDEBUG_MAIN
| NDEBUG_QUEUES
, instance
,
788 "main: select failed, returning %p to queue\n", cmd
);
789 requeue_cmd(instance
, cmd
);
792 if (hostdata
->connected
&& !hostdata
->dma_len
) {
793 dsprintk(NDEBUG_MAIN
, instance
, "main: performing information transfer\n");
794 NCR5380_information_transfer(instance
);
797 spin_unlock_irq(&hostdata
->lock
);
804 * NCR5380_dma_complete - finish DMA transfer
805 * @instance: the scsi host instance
807 * Called by the interrupt handler when DMA finishes or a phase
808 * mismatch occurs (which would end the DMA transfer).
811 static void NCR5380_dma_complete(struct Scsi_Host
*instance
)
813 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
815 unsigned char **data
;
817 int saved_data
= 0, overrun
= 0;
820 if (hostdata
->read_overruns
) {
821 p
= hostdata
->connected
->SCp
.phase
;
824 if ((NCR5380_read(BUS_AND_STATUS_REG
) &
825 (BASR_PHASE_MATCH
| BASR_ACK
)) ==
826 (BASR_PHASE_MATCH
| BASR_ACK
)) {
827 saved_data
= NCR5380_read(INPUT_DATA_REG
);
829 dsprintk(NDEBUG_DMA
, instance
, "read overrun handled\n");
834 NCR5380_write(MODE_REG
, MR_BASE
);
835 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
836 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
838 transferred
= hostdata
->dma_len
- NCR5380_dma_residual(instance
);
839 hostdata
->dma_len
= 0;
841 data
= (unsigned char **)&hostdata
->connected
->SCp
.ptr
;
842 count
= &hostdata
->connected
->SCp
.this_residual
;
843 *data
+= transferred
;
844 *count
-= transferred
;
846 if (hostdata
->read_overruns
) {
849 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) == p
&& (p
& SR_IO
)) {
850 cnt
= toPIO
= hostdata
->read_overruns
;
852 dsprintk(NDEBUG_DMA
, instance
,
853 "Got an input overrun, using saved byte\n");
854 *(*data
)++ = saved_data
;
860 dsprintk(NDEBUG_DMA
, instance
,
861 "Doing %d byte PIO to 0x%p\n", cnt
, *data
);
862 NCR5380_transfer_pio(instance
, &p
, &cnt
, data
);
863 *count
-= toPIO
- cnt
;
869 #ifndef DONT_USE_INTR
872 * NCR5380_intr - generic NCR5380 irq handler
873 * @irq: interrupt number
874 * @dev_id: device info
876 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
877 * from the disconnected queue, and restarting NCR5380_main()
880 * The chip can assert IRQ in any of six different conditions. The IRQ flag
881 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
882 * Three of these six conditions are latched in the Bus and Status Register:
883 * - End of DMA (cleared by ending DMA Mode)
884 * - Parity error (cleared by reading RPIR)
885 * - Loss of BSY (cleared by reading RPIR)
886 * Two conditions have flag bits that are not latched:
887 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
888 * - Bus reset (non-maskable)
889 * The remaining condition has no flag bit at all:
890 * - Selection/reselection
892 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
893 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
894 * claimed that "the design of the [DP8490] interrupt logic ensures
895 * interrupts will not be lost (they can be on the DP5380)."
896 * The L5380/53C80 datasheet from LOGIC Devices has more details.
898 * Checking for bus reset by reading RST is futile because of interrupt
899 * latency, but a bus reset will reset chip logic. Checking for parity error
900 * is unnecessary because that interrupt is never enabled. A Loss of BSY
901 * condition will clear DMA Mode. We can tell when this occurs because the
902 * the Busy Monitor interrupt is enabled together with DMA Mode.
905 static irqreturn_t
NCR5380_intr(int irq
, void *dev_id
)
907 struct Scsi_Host
*instance
= dev_id
;
908 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
913 spin_lock_irqsave(&hostdata
->lock
, flags
);
915 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
916 if (basr
& BASR_IRQ
) {
917 unsigned char mr
= NCR5380_read(MODE_REG
);
918 unsigned char sr
= NCR5380_read(STATUS_REG
);
920 dsprintk(NDEBUG_INTR
, instance
, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
923 if ((mr
& MR_DMA_MODE
) || (mr
& MR_MONITOR_BSY
)) {
924 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
925 * We ack IRQ after clearing Mode Register. Workarounds
926 * for End of DMA errata need to happen in DMA Mode.
929 dsprintk(NDEBUG_INTR
, instance
, "interrupt in DMA mode\n");
931 if (hostdata
->connected
) {
932 NCR5380_dma_complete(instance
);
933 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
935 NCR5380_write(MODE_REG
, MR_BASE
);
936 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
938 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_mask
) &&
939 (sr
& (SR_SEL
| SR_IO
| SR_BSY
| SR_RST
)) == (SR_SEL
| SR_IO
)) {
940 /* Probably reselected */
941 NCR5380_write(SELECT_ENABLE_REG
, 0);
942 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
944 dsprintk(NDEBUG_INTR
, instance
, "interrupt with SEL and IO\n");
946 if (!hostdata
->connected
) {
947 NCR5380_reselect(instance
);
948 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
950 if (!hostdata
->connected
)
951 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
953 /* Probably Bus Reset */
954 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
956 dsprintk(NDEBUG_INTR
, instance
, "unknown interrupt\n");
960 shost_printk(KERN_NOTICE
, instance
, "interrupt without IRQ bit\n");
963 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
965 return IRQ_RETVAL(handled
);
971 * Function : int NCR5380_select(struct Scsi_Host *instance,
972 * struct scsi_cmnd *cmd)
974 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
975 * including ARBITRATION, SELECTION, and initial message out for
976 * IDENTIFY and queue messages.
978 * Inputs : instance - instantiation of the 5380 driver on which this
979 * target lives, cmd - SCSI command to execute.
981 * Returns cmd if selection failed but should be retried,
982 * NULL if selection failed and should not be retried, or
983 * NULL if selection succeeded (hostdata->connected == cmd).
986 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
987 * with registers as they should have been on entry - ie
988 * SELECT_ENABLE will be set appropriately, the NCR5380
989 * will cease to drive any SCSI bus signals.
991 * If successful : I_T_L or I_T_L_Q nexus will be established,
992 * instance->connected will be set to cmd.
993 * SELECT interrupt will be disabled.
995 * If failed (no target) : cmd->scsi_done() will be called, and the
996 * cmd->result host byte set to DID_BAD_TARGET.
999 static struct scsi_cmnd
*NCR5380_select(struct Scsi_Host
*instance
,
1000 struct scsi_cmnd
*cmd
)
1002 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1003 unsigned char tmp
[3], phase
;
1004 unsigned char *data
;
1008 NCR5380_dprint(NDEBUG_ARBITRATION
, instance
);
1009 dsprintk(NDEBUG_ARBITRATION
, instance
, "starting arbitration, id = %d\n",
1013 * Arbitration and selection phases are slow and involve dropping the
1014 * lock, so we have to watch out for EH. An exception handler may
1015 * change 'selecting' to NULL. This function will then return NULL
1016 * so that the caller will forget about 'cmd'. (During information
1017 * transfer phases, EH may change 'connected' to NULL.)
1019 hostdata
->selecting
= cmd
;
1022 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1023 * data bus during SELECTION.
1026 NCR5380_write(TARGET_COMMAND_REG
, 0);
1029 * Start arbitration.
1032 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
1033 NCR5380_write(MODE_REG
, MR_ARBITRATE
);
1035 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1036 * Bus Free Delay, arbitration will begin.
1039 spin_unlock_irq(&hostdata
->lock
);
1040 err
= NCR5380_poll_politely2(instance
, MODE_REG
, MR_ARBITRATE
, 0,
1041 INITIATOR_COMMAND_REG
, ICR_ARBITRATION_PROGRESS
,
1042 ICR_ARBITRATION_PROGRESS
, HZ
);
1043 spin_lock_irq(&hostdata
->lock
);
1044 if (!(NCR5380_read(MODE_REG
) & MR_ARBITRATE
)) {
1045 /* Reselection interrupt */
1048 if (!hostdata
->selecting
) {
1049 /* Command was aborted */
1050 NCR5380_write(MODE_REG
, MR_BASE
);
1054 NCR5380_write(MODE_REG
, MR_BASE
);
1055 shost_printk(KERN_ERR
, instance
,
1056 "select: arbitration timeout\n");
1059 spin_unlock_irq(&hostdata
->lock
);
1061 /* The SCSI-2 arbitration delay is 2.4 us */
1064 /* Check for lost arbitration */
1065 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
1066 (NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_higher_mask
) ||
1067 (NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
)) {
1068 NCR5380_write(MODE_REG
, MR_BASE
);
1069 dsprintk(NDEBUG_ARBITRATION
, instance
, "lost arbitration, deasserting MR_ARBITRATE\n");
1070 spin_lock_irq(&hostdata
->lock
);
1074 /* After/during arbitration, BSY should be asserted.
1075 * IBM DPES-31080 Version S31Q works now
1076 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1078 NCR5380_write(INITIATOR_COMMAND_REG
,
1079 ICR_BASE
| ICR_ASSERT_SEL
| ICR_ASSERT_BSY
);
1082 * Again, bus clear + bus settle time is 1.2us, however, this is
1083 * a minimum so we'll udelay ceil(1.2)
1086 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
1091 spin_lock_irq(&hostdata
->lock
);
1093 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1094 if (!(NCR5380_read(MODE_REG
) & MR_ARBITRATE
))
1097 if (!hostdata
->selecting
) {
1098 NCR5380_write(MODE_REG
, MR_BASE
);
1099 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1103 dsprintk(NDEBUG_ARBITRATION
, instance
, "won arbitration\n");
1106 * Now that we have won arbitration, start Selection process, asserting
1107 * the host and target ID's on the SCSI bus.
1110 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
| (1 << scmd_id(cmd
)));
1113 * Raise ATN while SEL is true before BSY goes false from arbitration,
1114 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1115 * phase immediately after selection.
1118 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
|
1119 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_SEL
);
1120 NCR5380_write(MODE_REG
, MR_BASE
);
1123 * Reselect interrupts must be turned off prior to the dropping of BSY,
1124 * otherwise we will trigger an interrupt.
1126 NCR5380_write(SELECT_ENABLE_REG
, 0);
1128 spin_unlock_irq(&hostdata
->lock
);
1131 * The initiator shall then wait at least two deskew delays and release
1134 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1137 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
|
1138 ICR_ASSERT_ATN
| ICR_ASSERT_SEL
);
1141 * Something weird happens when we cease to drive BSY - looks
1142 * like the board/chip is letting us do another read before the
1143 * appropriate propagation delay has expired, and we're confusing
1144 * a BSY signal from ourselves as the target's response to SELECTION.
1146 * A small delay (the 'C++' frontend breaks the pipeline with an
1147 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1148 * tighter 'C' code breaks and requires this) solves the problem -
1149 * the 1 us delay is arbitrary, and only used because this delay will
1150 * be the same on other platforms and since it works here, it should
1153 * wingel suggests that this could be due to failing to wait
1159 dsprintk(NDEBUG_SELECTION
, instance
, "selecting target %d\n", scmd_id(cmd
));
1162 * The SCSI specification calls for a 250 ms timeout for the actual
1166 err
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_BSY
, SR_BSY
,
1167 msecs_to_jiffies(250));
1169 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
| SR_IO
)) == (SR_SEL
| SR_IO
)) {
1170 spin_lock_irq(&hostdata
->lock
);
1171 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1172 NCR5380_reselect(instance
);
1173 if (!hostdata
->connected
)
1174 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1175 shost_printk(KERN_ERR
, instance
, "reselection after won arbitration?\n");
1180 spin_lock_irq(&hostdata
->lock
);
1181 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1182 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1183 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1184 if (hostdata
->selecting
) {
1185 cmd
->result
= DID_BAD_TARGET
<< 16;
1186 complete_cmd(instance
, cmd
);
1187 dsprintk(NDEBUG_SELECTION
, instance
, "target did not respond within 250ms\n");
1194 * No less than two deskew delays after the initiator detects the
1195 * BSY signal is true, it shall release the SEL signal and may
1196 * change the DATA BUS. -wingel
1201 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1204 * Since we followed the SCSI spec, and raised ATN while SEL
1205 * was true but before BSY was false during selection, the information
1206 * transfer phase should be a MESSAGE OUT phase so that we can send the
1209 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1210 * message (2 bytes) with a tag ID that we increment with every command
1211 * until it wraps back to 0.
1213 * XXX - it turns out that there are some broken SCSI-II devices,
1214 * which claim to support tagged queuing but fail when more than
1215 * some number of commands are issued at once.
1218 /* Wait for start of REQ/ACK handshake */
1220 err
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
);
1221 spin_lock_irq(&hostdata
->lock
);
1223 shost_printk(KERN_ERR
, instance
, "select: REQ timeout\n");
1224 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1225 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1228 if (!hostdata
->selecting
) {
1233 dsprintk(NDEBUG_SELECTION
, instance
, "target %d selected, going into MESSAGE OUT phase.\n",
1235 tmp
[0] = IDENTIFY(((instance
->irq
== NO_IRQ
) ? 0 : 1), cmd
->device
->lun
);
1240 /* Send message(s) */
1242 phase
= PHASE_MSGOUT
;
1243 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1244 dsprintk(NDEBUG_SELECTION
, instance
, "nexus established.\n");
1245 /* XXX need to handle errors here */
1247 hostdata
->connected
= cmd
;
1248 hostdata
->busy
[cmd
->device
->id
] |= 1 << cmd
->device
->lun
;
1250 initialize_SCp(cmd
);
1255 if (!hostdata
->selecting
)
1257 hostdata
->selecting
= NULL
;
1262 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1263 * unsigned char *phase, int *count, unsigned char **data)
1265 * Purpose : transfers data in given phase using polled I/O
1267 * Inputs : instance - instance of driver, *phase - pointer to
1268 * what phase is expected, *count - pointer to number of
1269 * bytes to transfer, **data - pointer to data pointer.
1271 * Returns : -1 when different phase is entered without transferring
1272 * maximum number of bytes, 0 if all bytes are transferred or exit
1275 * Also, *phase, *count, *data are modified in place.
1277 * XXX Note : handling for bus free may be useful.
1281 * Note : this code is not as quick as it could be, however it
1282 * IS 100% reliable, and for the actual data transfer where speed
1283 * counts, we will always do a pseudo DMA or DMA transfer.
1286 static int NCR5380_transfer_pio(struct Scsi_Host
*instance
,
1287 unsigned char *phase
, int *count
,
1288 unsigned char **data
)
1290 unsigned char p
= *phase
, tmp
;
1292 unsigned char *d
= *data
;
1295 * The NCR5380 chip will only drive the SCSI bus when the
1296 * phase specified in the appropriate bits of the TARGET COMMAND
1297 * REGISTER match the STATUS REGISTER
1300 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1304 * Wait for assertion of REQ, after which the phase bits will be
1308 if (NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
) < 0)
1311 dsprintk(NDEBUG_HANDSHAKE
, instance
, "REQ asserted\n");
1313 /* Check for phase mismatch */
1314 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) != p
) {
1315 dsprintk(NDEBUG_PIO
, instance
, "phase mismatch\n");
1316 NCR5380_dprint_phase(NDEBUG_PIO
, instance
);
1320 /* Do actual transfer from SCSI bus to / from memory */
1322 NCR5380_write(OUTPUT_DATA_REG
, *d
);
1324 *d
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
1329 * The SCSI standard suggests that in MSGOUT phase, the initiator
1330 * should drop ATN on the last byte of the message phase
1331 * after REQ has been asserted for the handshake but before
1332 * the initiator raises ACK.
1336 if (!((p
& SR_MSG
) && c
> 1)) {
1337 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1338 NCR5380_dprint(NDEBUG_PIO
, instance
);
1339 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1340 ICR_ASSERT_DATA
| ICR_ASSERT_ACK
);
1342 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1343 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
);
1344 NCR5380_dprint(NDEBUG_PIO
, instance
);
1345 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1346 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1349 NCR5380_dprint(NDEBUG_PIO
, instance
);
1350 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
1353 if (NCR5380_poll_politely(instance
,
1354 STATUS_REG
, SR_REQ
, 0, 5 * HZ
) < 0)
1357 dsprintk(NDEBUG_HANDSHAKE
, instance
, "REQ negated, handshake complete\n");
1360 * We have several special cases to consider during REQ/ACK handshaking :
1361 * 1. We were in MSGOUT phase, and we are on the last byte of the
1362 * message. ATN must be dropped as ACK is dropped.
1364 * 2. We are in a MSGIN phase, and we are on the last byte of the
1365 * message. We must exit with ACK asserted, so that the calling
1366 * code may raise ATN before dropping ACK to reject the message.
1368 * 3. ACK and ATN are clear and the target may proceed as normal.
1370 if (!(p
== PHASE_MSGIN
&& c
== 1)) {
1371 if (p
== PHASE_MSGOUT
&& c
> 1)
1372 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1374 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1378 dsprintk(NDEBUG_PIO
, instance
, "residual %d\n", c
);
1382 tmp
= NCR5380_read(STATUS_REG
);
1383 /* The phase read from the bus is valid if either REQ is (already)
1384 * asserted or if ACK hasn't been released yet. The latter applies if
1385 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1387 if ((tmp
& SR_REQ
) || ((tmp
& SR_IO
) && c
== 0))
1388 *phase
= tmp
& PHASE_MASK
;
1390 *phase
= PHASE_UNKNOWN
;
1392 if (!c
|| (*phase
== p
))
1399 * do_reset - issue a reset command
1400 * @instance: adapter to reset
1402 * Issue a reset sequence to the NCR5380 and try and get the bus
1403 * back into sane shape.
1405 * This clears the reset interrupt flag because there may be no handler for
1406 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1407 * been installed. And when in EH we may have released the ST DMA interrupt.
1410 static void do_reset(struct Scsi_Host
*instance
)
1412 unsigned long flags
;
1414 local_irq_save(flags
);
1415 NCR5380_write(TARGET_COMMAND_REG
,
1416 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG
) & PHASE_MASK
));
1417 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
1419 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1420 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1421 local_irq_restore(flags
);
1425 * do_abort - abort the currently established nexus by going to
1426 * MESSAGE OUT phase and sending an ABORT message.
1427 * @instance: relevant scsi host instance
1429 * Returns 0 on success, -1 on failure.
1432 static int do_abort(struct Scsi_Host
*instance
)
1434 unsigned char *msgptr
, phase
, tmp
;
1438 /* Request message out phase */
1439 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1442 * Wait for the target to indicate a valid phase by asserting
1443 * REQ. Once this happens, we'll have either a MSGOUT phase
1444 * and can immediately send the ABORT message, or we'll have some
1445 * other phase and will have to source/sink data.
1447 * We really don't care what value was on the bus or what value
1448 * the target sees, so we just handshake.
1451 rc
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, 10 * HZ
);
1455 tmp
= NCR5380_read(STATUS_REG
) & PHASE_MASK
;
1457 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1459 if (tmp
!= PHASE_MSGOUT
) {
1460 NCR5380_write(INITIATOR_COMMAND_REG
,
1461 ICR_BASE
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1462 rc
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, 0, 3 * HZ
);
1465 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1471 phase
= PHASE_MSGOUT
;
1472 NCR5380_transfer_pio(instance
, &phase
, &len
, &msgptr
);
1475 * If we got here, and the command completed successfully,
1476 * we're about to go into bus free state.
1479 return len
? -1 : 0;
1482 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1487 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1488 * unsigned char *phase, int *count, unsigned char **data)
1490 * Purpose : transfers data in given phase using either real
1493 * Inputs : instance - instance of driver, *phase - pointer to
1494 * what phase is expected, *count - pointer to number of
1495 * bytes to transfer, **data - pointer to data pointer.
1497 * Returns : -1 when different phase is entered without transferring
1498 * maximum number of bytes, 0 if all bytes or transferred or exit
1501 * Also, *phase, *count, *data are modified in place.
1505 static int NCR5380_transfer_dma(struct Scsi_Host
*instance
,
1506 unsigned char *phase
, int *count
,
1507 unsigned char **data
)
1509 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1510 register int c
= *count
;
1511 register unsigned char p
= *phase
;
1512 register unsigned char *d
= *data
;
1516 if ((tmp
= (NCR5380_read(STATUS_REG
) & PHASE_MASK
)) != p
) {
1521 hostdata
->connected
->SCp
.phase
= p
;
1524 if (hostdata
->read_overruns
)
1525 c
-= hostdata
->read_overruns
;
1526 else if (hostdata
->flags
& FLAG_DMA_FIXUP
)
1530 dsprintk(NDEBUG_DMA
, instance
, "initializing DMA %s: length %d, address %p\n",
1531 (p
& SR_IO
) ? "receive" : "send", c
, d
);
1533 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1534 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
| MR_MONITOR_BSY
|
1535 MR_ENABLE_EOP_INTR
);
1537 if (!(hostdata
->flags
& FLAG_LATE_DMA_SETUP
)) {
1538 /* On the Medusa, it is a must to initialize the DMA before
1539 * starting the NCR. This is also the cleaner way for the TT.
1542 result
= NCR5380_dma_recv_setup(instance
, d
, c
);
1544 result
= NCR5380_dma_send_setup(instance
, d
, c
);
1548 * On the PAS16 at least I/O recovery delays are not needed here.
1549 * Everyone else seems to want them.
1553 NCR5380_io_delay(1);
1554 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
1556 NCR5380_io_delay(1);
1557 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1558 NCR5380_io_delay(1);
1559 NCR5380_write(START_DMA_SEND_REG
, 0);
1560 NCR5380_io_delay(1);
1563 if (hostdata
->flags
& FLAG_LATE_DMA_SETUP
) {
1564 /* On the Falcon, the DMA setup must be done after the last
1565 * NCR access, else the DMA setup gets trashed!
1568 result
= NCR5380_dma_recv_setup(instance
, d
, c
);
1570 result
= NCR5380_dma_send_setup(instance
, d
, c
);
1573 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1577 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1579 hostdata
->dma_len
= result
;
1583 /* The result is zero iff pseudo DMA send/receive was completed. */
1584 hostdata
->dma_len
= c
;
1587 * A note regarding the DMA errata workarounds for early NMOS silicon.
1589 * For DMA sends, we want to wait until the last byte has been
1590 * transferred out over the bus before we turn off DMA mode. Alas, there
1591 * seems to be no terribly good way of doing this on a 5380 under all
1592 * conditions. For non-scatter-gather operations, we can wait until REQ
1593 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1594 * are nastier, since the device will be expecting more data than we
1595 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1596 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1597 * why this signal was added to the newer chips) but on the older 538[01]
1598 * this signal does not exist. The workaround for this lack is a watchdog;
1599 * we bail out of the wait-loop after a modest amount of wait-time if
1600 * the usual exit conditions are not met. Not a terribly clean or
1601 * correct solution :-%
1603 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1604 * If the chip is in DMA receive mode, it will respond to a target's
1605 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1606 * ACK, even if it has _already_ been notified by the DMA controller that
1607 * the current DMA transfer has completed! If the NCR5380 is then taken
1608 * out of DMA mode, this already-acknowledged byte is lost. This is
1609 * not a problem for "one DMA transfer per READ command", because
1610 * the situation will never arise... either all of the data is DMA'ed
1611 * properly, or the target switches to MESSAGE IN phase to signal a
1612 * disconnection (either operation bringing the DMA to a clean halt).
1613 * However, in order to handle scatter-receive, we must work around the
1614 * problem. The chosen fix is to DMA fewer bytes, then check for the
1615 * condition before taking the NCR5380 out of DMA mode. One or two extra
1616 * bytes are transferred via PIO as necessary to fill out the original
1620 if (hostdata
->flags
& FLAG_DMA_FIXUP
) {
1623 * The workaround was to transfer fewer bytes than we
1624 * intended to with the pseudo-DMA read function, wait for
1625 * the chip to latch the last byte, read it, and then disable
1628 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1629 * REQ is deasserted when ACK is asserted, and not reasserted
1630 * until ACK goes false. Since the NCR5380 won't lower ACK
1631 * until DACK is asserted, which won't happen unless we twiddle
1632 * the DMA port or we take the NCR5380 out of DMA mode, we
1633 * can guarantee that we won't handshake another extra
1637 if (NCR5380_poll_politely(instance
, BUS_AND_STATUS_REG
,
1638 BASR_DRQ
, BASR_DRQ
, HZ
) < 0) {
1640 shost_printk(KERN_ERR
, instance
, "PDMA read: DRQ timeout\n");
1642 if (NCR5380_poll_politely(instance
, STATUS_REG
,
1643 SR_REQ
, 0, HZ
) < 0) {
1645 shost_printk(KERN_ERR
, instance
, "PDMA read: !REQ timeout\n");
1647 d
[*count
- 1] = NCR5380_read(INPUT_DATA_REG
);
1650 * Wait for the last byte to be sent. If REQ is being asserted for
1651 * the byte we're interested, we'll ACK it and it will go false.
1653 if (NCR5380_poll_politely2(instance
,
1654 BUS_AND_STATUS_REG
, BASR_DRQ
, BASR_DRQ
,
1655 BUS_AND_STATUS_REG
, BASR_PHASE_MATCH
, 0, HZ
) < 0) {
1657 shost_printk(KERN_ERR
, instance
, "PDMA write: DRQ and phase timeout\n");
1662 NCR5380_dma_complete(instance
);
1667 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1669 * Purpose : run through the various SCSI phases and do as the target
1670 * directs us to. Operates on the currently connected command,
1671 * instance->connected.
1673 * Inputs : instance, instance for which we are doing commands
1675 * Side effects : SCSI things happen, the disconnected queue will be
1676 * modified if a command disconnects, *instance->connected will
1679 * XXX Note : we need to watch for bus free or a reset condition here
1680 * to recover from an unexpected bus free condition.
1683 static void NCR5380_information_transfer(struct Scsi_Host
*instance
)
1685 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1686 unsigned char msgout
= NOP
;
1690 unsigned char *data
;
1691 unsigned char phase
, tmp
, extended_msg
[10], old_phase
= 0xff;
1692 struct scsi_cmnd
*cmd
;
1694 while ((cmd
= hostdata
->connected
)) {
1695 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(cmd
);
1697 tmp
= NCR5380_read(STATUS_REG
);
1698 /* We only have a valid SCSI phase when REQ is asserted */
1700 phase
= (tmp
& PHASE_MASK
);
1701 if (phase
!= old_phase
) {
1703 NCR5380_dprint_phase(NDEBUG_INFORMATION
, instance
);
1705 if (sink
&& (phase
!= PHASE_MSGOUT
)) {
1706 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1708 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
|
1710 while (NCR5380_read(STATUS_REG
) & SR_REQ
)
1712 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1720 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1721 shost_printk(KERN_DEBUG
, instance
, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
1724 cmd
->result
= DID_ERROR
<< 16;
1725 complete_cmd(instance
, cmd
);
1726 hostdata
->connected
= NULL
;
1731 * If there is no room left in the current buffer in the
1732 * scatter-gather list, move onto the next one.
1735 if (!cmd
->SCp
.this_residual
&& cmd
->SCp
.buffers_residual
) {
1737 --cmd
->SCp
.buffers_residual
;
1738 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
1739 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
1740 dsprintk(NDEBUG_INFORMATION
, instance
, "%d bytes and %d buffers left\n",
1741 cmd
->SCp
.this_residual
,
1742 cmd
->SCp
.buffers_residual
);
1746 * The preferred transfer method is going to be
1747 * PSEUDO-DMA for systems that are strictly PIO,
1748 * since we can let the hardware do the handshaking.
1750 * For this to work, we need to know the transfersize
1751 * ahead of time, since the pseudo-DMA code will sit
1752 * in an unconditional loop.
1756 if (!cmd
->device
->borken
)
1757 transfersize
= NCR5380_dma_xfer_len(instance
, cmd
, phase
);
1759 if (transfersize
> 0) {
1761 if (NCR5380_transfer_dma(instance
, &phase
,
1762 &len
, (unsigned char **)&cmd
->SCp
.ptr
)) {
1764 * If the watchdog timer fires, all future
1765 * accesses to this device will use the
1768 scmd_printk(KERN_INFO
, cmd
,
1769 "switching to slow handshake\n");
1770 cmd
->device
->borken
= 1;
1773 cmd
->result
= DID_ERROR
<< 16;
1774 /* XXX - need to source or sink data here, as appropriate */
1777 /* Break up transfer into 3 ms chunks,
1778 * presuming 6 accesses per handshake.
1780 transfersize
= min((unsigned long)cmd
->SCp
.this_residual
,
1781 hostdata
->accesses_per_ms
/ 2);
1783 NCR5380_transfer_pio(instance
, &phase
, &len
,
1784 (unsigned char **)&cmd
->SCp
.ptr
);
1785 cmd
->SCp
.this_residual
-= transfersize
- len
;
1791 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1792 cmd
->SCp
.Message
= tmp
;
1796 case COMMAND_COMPLETE
:
1797 /* Accept message by clearing ACK */
1799 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1800 dsprintk(NDEBUG_QUEUES
, instance
,
1801 "COMMAND COMPLETE %p target %d lun %llu\n",
1802 cmd
, scmd_id(cmd
), cmd
->device
->lun
);
1804 hostdata
->connected
= NULL
;
1806 cmd
->result
&= ~0xffff;
1807 cmd
->result
|= cmd
->SCp
.Status
;
1808 cmd
->result
|= cmd
->SCp
.Message
<< 8;
1810 if (cmd
->cmnd
[0] == REQUEST_SENSE
)
1811 complete_cmd(instance
, cmd
);
1813 if (cmd
->SCp
.Status
== SAM_STAT_CHECK_CONDITION
||
1814 cmd
->SCp
.Status
== SAM_STAT_COMMAND_TERMINATED
) {
1815 dsprintk(NDEBUG_QUEUES
, instance
, "autosense: adding cmd %p to tail of autosense queue\n",
1817 list_add_tail(&ncmd
->list
,
1818 &hostdata
->autosense
);
1820 complete_cmd(instance
, cmd
);
1824 * Restore phase bits to 0 so an interrupted selection,
1825 * arbitration can resume.
1827 NCR5380_write(TARGET_COMMAND_REG
, 0);
1829 /* Enable reselect interrupts */
1830 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1832 case MESSAGE_REJECT
:
1833 /* Accept message by clearing ACK */
1834 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1835 switch (hostdata
->last_message
) {
1836 case HEAD_OF_QUEUE_TAG
:
1837 case ORDERED_QUEUE_TAG
:
1838 case SIMPLE_QUEUE_TAG
:
1839 cmd
->device
->simple_tags
= 0;
1840 hostdata
->busy
[cmd
->device
->id
] |= (1 << (cmd
->device
->lun
& 0xFF));
1847 /* Accept message by clearing ACK */
1848 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1849 hostdata
->connected
= NULL
;
1850 list_add(&ncmd
->list
, &hostdata
->disconnected
);
1851 dsprintk(NDEBUG_INFORMATION
| NDEBUG_QUEUES
,
1852 instance
, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1853 cmd
, scmd_id(cmd
), cmd
->device
->lun
);
1856 * Restore phase bits to 0 so an interrupted selection,
1857 * arbitration can resume.
1859 NCR5380_write(TARGET_COMMAND_REG
, 0);
1861 /* Enable reselect interrupts */
1862 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1865 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1866 * operation, in violation of the SCSI spec so we can safely
1867 * ignore SAVE/RESTORE pointers calls.
1869 * Unfortunately, some disks violate the SCSI spec and
1870 * don't issue the required SAVE_POINTERS message before
1871 * disconnecting, and we have to break spec to remain
1875 case RESTORE_POINTERS
:
1876 /* Accept message by clearing ACK */
1877 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1879 case EXTENDED_MESSAGE
:
1881 * Start the message buffer with the EXTENDED_MESSAGE
1882 * byte, since spi_print_msg() wants the whole thing.
1884 extended_msg
[0] = EXTENDED_MESSAGE
;
1885 /* Accept first byte by clearing ACK */
1886 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1888 spin_unlock_irq(&hostdata
->lock
);
1890 dsprintk(NDEBUG_EXTENDED
, instance
, "receiving extended message\n");
1893 data
= extended_msg
+ 1;
1894 phase
= PHASE_MSGIN
;
1895 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1896 dsprintk(NDEBUG_EXTENDED
, instance
, "length %d, code 0x%02x\n",
1897 (int)extended_msg
[1],
1898 (int)extended_msg
[2]);
1900 if (!len
&& extended_msg
[1] > 0 &&
1901 extended_msg
[1] <= sizeof(extended_msg
) - 2) {
1902 /* Accept third byte by clearing ACK */
1903 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1904 len
= extended_msg
[1] - 1;
1905 data
= extended_msg
+ 3;
1906 phase
= PHASE_MSGIN
;
1908 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1909 dsprintk(NDEBUG_EXTENDED
, instance
, "message received, residual %d\n",
1912 switch (extended_msg
[2]) {
1915 case EXTENDED_MODIFY_DATA_POINTER
:
1916 case EXTENDED_EXTENDED_IDENTIFY
:
1920 shost_printk(KERN_ERR
, instance
, "error receiving extended message\n");
1923 shost_printk(KERN_NOTICE
, instance
, "extended message code %02x length %d is too long\n",
1924 extended_msg
[2], extended_msg
[1]);
1928 spin_lock_irq(&hostdata
->lock
);
1929 if (!hostdata
->connected
)
1932 /* Fall through to reject message */
1935 * If we get something weird that we aren't expecting,
1940 shost_printk(KERN_ERR
, instance
, "rejecting message ");
1941 spi_print_msg(extended_msg
);
1943 } else if (tmp
!= EXTENDED_MESSAGE
)
1944 scmd_printk(KERN_INFO
, cmd
,
1945 "rejecting unknown message %02x\n",
1948 scmd_printk(KERN_INFO
, cmd
,
1949 "rejecting unknown extended message code %02x, length %d\n",
1950 extended_msg
[1], extended_msg
[0]);
1952 msgout
= MESSAGE_REJECT
;
1953 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1955 } /* switch (tmp) */
1960 hostdata
->last_message
= msgout
;
1961 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1962 if (msgout
== ABORT
) {
1963 hostdata
->connected
= NULL
;
1964 cmd
->result
= DID_ERROR
<< 16;
1965 complete_cmd(instance
, cmd
);
1966 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1975 * XXX for performance reasons, on machines with a
1976 * PSEUDO-DMA architecture we should probably
1977 * use the dma transfer function.
1979 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1984 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1985 cmd
->SCp
.Status
= tmp
;
1988 shost_printk(KERN_ERR
, instance
, "unknown phase\n");
1989 NCR5380_dprint(NDEBUG_ANY
, instance
);
1990 } /* switch(phase) */
1992 spin_unlock_irq(&hostdata
->lock
);
1993 NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
);
1994 spin_lock_irq(&hostdata
->lock
);
2000 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2002 * Purpose : does reselection, initializing the instance->connected
2003 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2004 * nexus has been reestablished,
2006 * Inputs : instance - this instance of the NCR5380.
2009 static void NCR5380_reselect(struct Scsi_Host
*instance
)
2011 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2012 unsigned char target_mask
;
2013 unsigned char lun
, phase
;
2015 unsigned char msg
[3];
2016 unsigned char *data
;
2017 struct NCR5380_cmd
*ncmd
;
2018 struct scsi_cmnd
*tmp
;
2021 * Disable arbitration, etc. since the host adapter obviously
2022 * lost, and tell an interrupted NCR5380_select() to restart.
2025 NCR5380_write(MODE_REG
, MR_BASE
);
2027 target_mask
= NCR5380_read(CURRENT_SCSI_DATA_REG
) & ~(hostdata
->id_mask
);
2029 dsprintk(NDEBUG_RESELECTION
, instance
, "reselect\n");
2032 * At this point, we have detected that our SCSI ID is on the bus,
2033 * SEL is true and BSY was false for at least one bus settle delay
2036 * We must assert BSY ourselves, until the target drops the SEL
2040 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
);
2041 if (NCR5380_poll_politely(instance
,
2042 STATUS_REG
, SR_SEL
, 0, 2 * HZ
) < 0) {
2043 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2046 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2049 * Wait for target to go into MSGIN.
2052 if (NCR5380_poll_politely(instance
,
2053 STATUS_REG
, SR_REQ
, SR_REQ
, 2 * HZ
) < 0) {
2060 phase
= PHASE_MSGIN
;
2061 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
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.
2114 /* Accept message by clearing ACK */
2115 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2117 hostdata
->connected
= tmp
;
2118 dsprintk(NDEBUG_RESELECTION
, instance
, "nexus established, target %d, lun %llu, tag %d\n",
2119 scmd_id(tmp
), tmp
->device
->lun
, tmp
->tag
);
2123 * list_find_cmd - test for presence of a command in a linked list
2124 * @haystack: list of commands
2125 * @needle: command to search for
2128 static bool list_find_cmd(struct list_head
*haystack
,
2129 struct scsi_cmnd
*needle
)
2131 struct NCR5380_cmd
*ncmd
;
2133 list_for_each_entry(ncmd
, haystack
, list
)
2134 if (NCR5380_to_scmd(ncmd
) == needle
)
2140 * list_remove_cmd - remove a command from linked list
2141 * @haystack: list of commands
2142 * @needle: command to remove
2145 static bool list_del_cmd(struct list_head
*haystack
,
2146 struct scsi_cmnd
*needle
)
2148 if (list_find_cmd(haystack
, needle
)) {
2149 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(needle
);
2151 list_del(&ncmd
->list
);
2158 * NCR5380_abort - scsi host eh_abort_handler() method
2159 * @cmd: the command to be aborted
2161 * Try to abort a given command by removing it from queues and/or sending
2162 * the target an abort message. This may not succeed in causing a target
2163 * to abort the command. Nonetheless, the low-level driver must forget about
2164 * the command because the mid-layer reclaims it and it may be re-issued.
2166 * The normal path taken by a command is as follows. For EH we trace this
2167 * same path to locate and abort the command.
2169 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2170 * [disconnected -> connected ->]...
2171 * [autosense -> connected ->] done
2173 * If cmd was not found at all then presumably it has already been completed,
2174 * in which case return SUCCESS to try to avoid further EH measures.
2176 * If the command has not completed yet, we must not fail to find it.
2177 * We have no option but to forget the aborted command (even if it still
2178 * lacks sense data). The mid-layer may re-issue a command that is in error
2179 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2180 * this driver are such that a command can appear on one queue only.
2182 * The lock protects driver data structures, but EH handlers also use it
2183 * to serialize their own execution and prevent their own re-entry.
2186 static int NCR5380_abort(struct scsi_cmnd
*cmd
)
2188 struct Scsi_Host
*instance
= cmd
->device
->host
;
2189 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2190 unsigned long flags
;
2191 int result
= SUCCESS
;
2193 spin_lock_irqsave(&hostdata
->lock
, flags
);
2195 #if (NDEBUG & NDEBUG_ANY)
2196 scmd_printk(KERN_INFO
, cmd
, __func__
);
2198 NCR5380_dprint(NDEBUG_ANY
, instance
);
2199 NCR5380_dprint_phase(NDEBUG_ANY
, instance
);
2201 if (list_del_cmd(&hostdata
->unissued
, cmd
)) {
2202 dsprintk(NDEBUG_ABORT
, instance
,
2203 "abort: removed %p from issue queue\n", cmd
);
2204 cmd
->result
= DID_ABORT
<< 16;
2205 cmd
->scsi_done(cmd
); /* No tag or busy flag to worry about */
2209 if (hostdata
->selecting
== cmd
) {
2210 dsprintk(NDEBUG_ABORT
, instance
,
2211 "abort: cmd %p == selecting\n", cmd
);
2212 hostdata
->selecting
= NULL
;
2213 cmd
->result
= DID_ABORT
<< 16;
2214 complete_cmd(instance
, cmd
);
2218 if (list_del_cmd(&hostdata
->disconnected
, cmd
)) {
2219 dsprintk(NDEBUG_ABORT
, instance
,
2220 "abort: removed %p from disconnected list\n", cmd
);
2221 /* Can't call NCR5380_select() and send ABORT because that
2222 * means releasing the lock. Need a bus reset.
2224 set_host_byte(cmd
, DID_ERROR
);
2225 complete_cmd(instance
, cmd
);
2230 if (hostdata
->connected
== cmd
) {
2231 dsprintk(NDEBUG_ABORT
, instance
, "abort: cmd %p is connected\n", cmd
);
2232 hostdata
->connected
= NULL
;
2233 hostdata
->dma_len
= 0;
2234 if (do_abort(instance
)) {
2235 set_host_byte(cmd
, DID_ERROR
);
2236 complete_cmd(instance
, cmd
);
2240 set_host_byte(cmd
, DID_ABORT
);
2241 complete_cmd(instance
, cmd
);
2245 if (list_del_cmd(&hostdata
->autosense
, cmd
)) {
2246 dsprintk(NDEBUG_ABORT
, instance
,
2247 "abort: removed %p from sense queue\n", cmd
);
2248 set_host_byte(cmd
, DID_ERROR
);
2249 complete_cmd(instance
, cmd
);
2253 if (result
== FAILED
)
2254 dsprintk(NDEBUG_ABORT
, instance
, "abort: failed to abort %p\n", cmd
);
2256 dsprintk(NDEBUG_ABORT
, instance
, "abort: successfully aborted %p\n", cmd
);
2258 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
2259 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
2266 * NCR5380_bus_reset - reset the SCSI bus
2267 * @cmd: SCSI command undergoing EH
2272 static int NCR5380_bus_reset(struct scsi_cmnd
*cmd
)
2274 struct Scsi_Host
*instance
= cmd
->device
->host
;
2275 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2277 unsigned long flags
;
2278 struct NCR5380_cmd
*ncmd
;
2280 spin_lock_irqsave(&hostdata
->lock
, flags
);
2282 #if (NDEBUG & NDEBUG_ANY)
2283 scmd_printk(KERN_INFO
, cmd
, __func__
);
2285 NCR5380_dprint(NDEBUG_ANY
, instance
);
2286 NCR5380_dprint_phase(NDEBUG_ANY
, instance
);
2290 /* reset NCR registers */
2291 NCR5380_write(MODE_REG
, MR_BASE
);
2292 NCR5380_write(TARGET_COMMAND_REG
, 0);
2293 NCR5380_write(SELECT_ENABLE_REG
, 0);
2295 /* After the reset, there are no more connected or disconnected commands
2296 * and no busy units; so clear the low-level status here to avoid
2297 * conflicts when the mid-level code tries to wake up the affected
2301 if (list_del_cmd(&hostdata
->unissued
, cmd
)) {
2302 cmd
->result
= DID_RESET
<< 16;
2303 cmd
->scsi_done(cmd
);
2306 if (hostdata
->selecting
) {
2307 hostdata
->selecting
->result
= DID_RESET
<< 16;
2308 complete_cmd(instance
, hostdata
->selecting
);
2309 hostdata
->selecting
= NULL
;
2312 list_for_each_entry(ncmd
, &hostdata
->disconnected
, list
) {
2313 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
2315 set_host_byte(cmd
, DID_RESET
);
2316 cmd
->scsi_done(cmd
);
2318 INIT_LIST_HEAD(&hostdata
->disconnected
);
2320 list_for_each_entry(ncmd
, &hostdata
->autosense
, list
) {
2321 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
2323 set_host_byte(cmd
, DID_RESET
);
2324 cmd
->scsi_done(cmd
);
2326 INIT_LIST_HEAD(&hostdata
->autosense
);
2328 if (hostdata
->connected
) {
2329 set_host_byte(hostdata
->connected
, DID_RESET
);
2330 complete_cmd(instance
, hostdata
->connected
);
2331 hostdata
->connected
= NULL
;
2334 for (i
= 0; i
< 8; ++i
)
2335 hostdata
->busy
[i
] = 0;
2336 hostdata
->dma_len
= 0;
2338 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
2339 spin_unlock_irqrestore(&hostdata
->lock
, flags
);