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 :
34 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
35 * code so that everything does the same thing that's done at the
36 * end of a pseudo-DMA read operation.
38 * 4. Test SCSI-II tagged queueing (I have no devices which support
45 * This is a generic 5380 driver. To use it on a different platform,
46 * one simply writes appropriate system specific macros (ie, data
47 * transfer - some PC's will use the I/O bus, 68K's must use
48 * memory mapped) and drops this file in their 'C' wrapper.
50 * As far as command queueing, two queues are maintained for
51 * each 5380 in the system - commands that haven't been issued yet,
52 * and commands that are currently executing. This means that an
53 * unlimited number of commands may be queued, letting
54 * more commands propagate from the higher driver levels giving higher
55 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
56 * allowing multiple commands to propagate all the way to a SCSI-II device
57 * while a command is already executing.
60 * Issues specific to the NCR5380 :
62 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
63 * piece of hardware that requires you to sit in a loop polling for
64 * the REQ signal as long as you are connected. Some devices are
65 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
66 * while doing long seek operations. [...] These
67 * broken devices are the exception rather than the rule and I'd rather
68 * spend my time optimizing for the normal case.
72 * At the heart of the design is a coroutine, NCR5380_main,
73 * which is started from a workqueue for each NCR5380 host in the
74 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
75 * removing the commands from the issue queue and calling
76 * NCR5380_select() if a nexus is not established.
78 * Once a nexus is established, the NCR5380_information_transfer()
79 * phase goes through the various phases as instructed by the target.
80 * if the target goes into MSG IN and sends a DISCONNECT message,
81 * the command structure is placed into the per instance disconnected
82 * queue, and NCR5380_main tries to find more work. If the target is
83 * idle for too long, the system will try to sleep.
85 * If a command has disconnected, eventually an interrupt will trigger,
86 * calling NCR5380_intr() which will in turn call NCR5380_reselect
87 * to reestablish a nexus. This will run main if necessary.
89 * On command termination, the done function will be called as
92 * SCSI pointers are maintained in the SCp field of SCSI command
93 * structures, being initialized after the command is connected
94 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
95 * Note that in violation of the standard, an implicit SAVE POINTERS operation
96 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
101 * This file a skeleton Linux SCSI driver for the NCR 5380 series
102 * of chips. To use it, you write an architecture specific functions
103 * and macros and include this file in your driver.
105 * These macros control options :
106 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
109 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
110 * for commands that return with a CHECK CONDITION status.
112 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
115 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
116 * override-configure an IRQ.
118 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
120 * These macros MUST be defined :
122 * NCR5380_read(register) - read from the specified register
124 * NCR5380_write(register, value) - write to the specific register
126 * NCR5380_implementation_fields - additional fields needed for this
127 * specific implementation of the NCR5380
129 * Either real DMA *or* pseudo DMA may be implemented
131 * NCR5380_dma_write_setup(instance, src, count) - initialize
132 * NCR5380_dma_read_setup(instance, dst, count) - initialize
133 * NCR5380_dma_residual(instance); - residual count
135 * The generic driver is initialized by calling NCR5380_init(instance),
136 * after setting the appropriate host specific fields and ID. If the
137 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
138 * possible) function may be used.
141 #ifndef NCR5380_io_delay
142 #define NCR5380_io_delay(x)
145 static int do_abort(struct Scsi_Host
*);
146 static void do_reset(struct Scsi_Host
*);
149 * initialize_SCp - init the scsi pointer field
150 * @cmd: command block to set up
152 * Set up the internal fields in the SCSI command.
155 static inline void initialize_SCp(struct scsi_cmnd
*cmd
)
158 * Initialize the Scsi Pointer field so that all of the commands in the
159 * various queues are valid.
162 if (scsi_bufflen(cmd
)) {
163 cmd
->SCp
.buffer
= scsi_sglist(cmd
);
164 cmd
->SCp
.buffers_residual
= scsi_sg_count(cmd
) - 1;
165 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
166 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
168 cmd
->SCp
.buffer
= NULL
;
169 cmd
->SCp
.buffers_residual
= 0;
171 cmd
->SCp
.this_residual
= 0;
175 cmd
->SCp
.Message
= 0;
179 * NCR5380_poll_politely2 - wait for two chip register values
180 * @instance: controller to poll
181 * @reg1: 5380 register to poll
182 * @bit1: Bitmask to check
183 * @val1: Expected value
184 * @reg2: Second 5380 register to poll
185 * @bit2: Second bitmask to check
186 * @val2: Second expected value
187 * @wait: Time-out in jiffies
189 * Polls the chip in a reasonably efficient manner waiting for an
190 * event to occur. After a short quick poll we begin to yield the CPU
191 * (if possible). In irq contexts the time-out is arbitrarily limited.
192 * Callers may hold locks as long as they are held in irq mode.
194 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
197 static int NCR5380_poll_politely2(struct Scsi_Host
*instance
,
198 int reg1
, int bit1
, int val1
,
199 int reg2
, int bit2
, int val2
, int wait
)
201 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
202 unsigned long deadline
= jiffies
+ wait
;
205 /* Busy-wait for up to 10 ms */
206 n
= min(10000U, jiffies_to_usecs(wait
));
207 n
*= hostdata
->accesses_per_ms
;
210 if ((NCR5380_read(reg1
) & bit1
) == val1
)
212 if ((NCR5380_read(reg2
) & bit2
) == val2
)
217 if (irqs_disabled() || in_interrupt())
220 /* Repeatedly sleep for 1 ms until deadline */
221 while (time_is_after_jiffies(deadline
)) {
222 schedule_timeout_uninterruptible(1);
223 if ((NCR5380_read(reg1
) & bit1
) == val1
)
225 if ((NCR5380_read(reg2
) & bit2
) == val2
)
232 static inline int NCR5380_poll_politely(struct Scsi_Host
*instance
,
233 int reg
, int bit
, int val
, int wait
)
235 return NCR5380_poll_politely2(instance
, reg
, bit
, val
,
236 reg
, bit
, val
, wait
);
260 {ICR_ASSERT_RST
, "ASSERT RST"},
261 {ICR_ASSERT_ACK
, "ASSERT ACK"},
262 {ICR_ASSERT_BSY
, "ASSERT BSY"},
263 {ICR_ASSERT_SEL
, "ASSERT SEL"},
264 {ICR_ASSERT_ATN
, "ASSERT ATN"},
265 {ICR_ASSERT_DATA
, "ASSERT DATA"},
269 {MR_BLOCK_DMA_MODE
, "MODE BLOCK DMA"},
270 {MR_TARGET
, "MODE TARGET"},
271 {MR_ENABLE_PAR_CHECK
, "MODE PARITY CHECK"},
272 {MR_ENABLE_PAR_INTR
, "MODE PARITY INTR"},
273 {MR_ENABLE_EOP_INTR
, "MODE EOP INTR"},
274 {MR_MONITOR_BSY
, "MODE MONITOR BSY"},
275 {MR_DMA_MODE
, "MODE DMA"},
276 {MR_ARBITRATE
, "MODE ARBITRATION"},
281 * NCR5380_print - print scsi bus signals
282 * @instance: adapter state to dump
284 * Print the SCSI bus signals for debugging purposes
287 static void NCR5380_print(struct Scsi_Host
*instance
)
289 unsigned char status
, data
, basr
, mr
, icr
, i
;
291 data
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
292 status
= NCR5380_read(STATUS_REG
);
293 mr
= NCR5380_read(MODE_REG
);
294 icr
= NCR5380_read(INITIATOR_COMMAND_REG
);
295 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
297 printk("STATUS_REG: %02x ", status
);
298 for (i
= 0; signals
[i
].mask
; ++i
)
299 if (status
& signals
[i
].mask
)
300 printk(",%s", signals
[i
].name
);
301 printk("\nBASR: %02x ", basr
);
302 for (i
= 0; basrs
[i
].mask
; ++i
)
303 if (basr
& basrs
[i
].mask
)
304 printk(",%s", basrs
[i
].name
);
305 printk("\nICR: %02x ", icr
);
306 for (i
= 0; icrs
[i
].mask
; ++i
)
307 if (icr
& icrs
[i
].mask
)
308 printk(",%s", icrs
[i
].name
);
309 printk("\nMODE: %02x ", mr
);
310 for (i
= 0; mrs
[i
].mask
; ++i
)
311 if (mr
& mrs
[i
].mask
)
312 printk(",%s", mrs
[i
].name
);
320 {PHASE_DATAOUT
, "DATAOUT"},
321 {PHASE_DATAIN
, "DATAIN"},
322 {PHASE_CMDOUT
, "CMDOUT"},
323 {PHASE_STATIN
, "STATIN"},
324 {PHASE_MSGOUT
, "MSGOUT"},
325 {PHASE_MSGIN
, "MSGIN"},
326 {PHASE_UNKNOWN
, "UNKNOWN"}
330 * NCR5380_print_phase - show SCSI phase
331 * @instance: adapter to dump
333 * Print the current SCSI phase for debugging purposes
336 static void NCR5380_print_phase(struct Scsi_Host
*instance
)
338 unsigned char status
;
341 status
= NCR5380_read(STATUS_REG
);
342 if (!(status
& SR_REQ
))
343 shost_printk(KERN_DEBUG
, instance
, "REQ not asserted, phase unknown.\n");
345 for (i
= 0; (phases
[i
].value
!= PHASE_UNKNOWN
) &&
346 (phases
[i
].value
!= (status
& PHASE_MASK
)); ++i
)
348 shost_printk(KERN_DEBUG
, instance
, "phase %s\n", phases
[i
].name
);
354 static int probe_irq __initdata
;
357 * probe_intr - helper for IRQ autoprobe
358 * @irq: interrupt number
362 * Set a flag to indicate the IRQ in question was received. This is
363 * used by the IRQ probe code.
366 static irqreturn_t __init
probe_intr(int irq
, void *dev_id
)
373 * NCR5380_probe_irq - find the IRQ of an NCR5380
374 * @instance: NCR5380 controller
375 * @possible: bitmask of ISA IRQ lines
377 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
378 * and then looking to see what interrupt actually turned up.
381 static int __init __maybe_unused
NCR5380_probe_irq(struct Scsi_Host
*instance
,
384 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
385 unsigned long timeout
;
386 int trying_irqs
, i
, mask
;
388 for (trying_irqs
= 0, i
= 1, mask
= 2; i
< 16; ++i
, mask
<<= 1)
389 if ((mask
& possible
) && (request_irq(i
, &probe_intr
, 0, "NCR-probe", NULL
) == 0))
392 timeout
= jiffies
+ msecs_to_jiffies(250);
396 * A interrupt is triggered whenever BSY = false, SEL = true
397 * and a bit set in the SELECT_ENABLE_REG is asserted on the
400 * Note that the bus is only driven when the phase control signals
401 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
405 NCR5380_write(TARGET_COMMAND_REG
, 0);
406 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
407 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
408 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
| ICR_ASSERT_SEL
);
410 while (probe_irq
== NO_IRQ
&& time_before(jiffies
, timeout
))
411 schedule_timeout_uninterruptible(1);
413 NCR5380_write(SELECT_ENABLE_REG
, 0);
414 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
416 for (i
= 1, mask
= 2; i
< 16; ++i
, mask
<<= 1)
417 if (trying_irqs
& mask
)
424 * NCR58380_info - report driver and host information
425 * @instance: relevant scsi host instance
427 * For use as the host template info() handler.
430 static const char *NCR5380_info(struct Scsi_Host
*instance
)
432 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
434 return hostdata
->info
;
437 static void prepare_info(struct Scsi_Host
*instance
)
439 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
441 snprintf(hostdata
->info
, sizeof(hostdata
->info
),
442 "%s, io_port 0x%lx, n_io_port %d, "
443 "base 0x%lx, irq %d, "
444 "can_queue %d, cmd_per_lun %d, "
445 "sg_tablesize %d, this_id %d, "
448 instance
->hostt
->name
, instance
->io_port
, instance
->n_io_port
,
449 instance
->base
, instance
->irq
,
450 instance
->can_queue
, instance
->cmd_per_lun
,
451 instance
->sg_tablesize
, instance
->this_id
,
452 hostdata
->flags
& FLAG_DMA_FIXUP
? "DMA_FIXUP " : "",
453 hostdata
->flags
& FLAG_NO_PSEUDO_DMA
? "NO_PSEUDO_DMA " : "",
454 hostdata
->flags
& FLAG_TOSHIBA_DELAY
? "TOSHIBA_DELAY " : "",
468 * NCR5380_init - initialise an NCR5380
469 * @instance: adapter to configure
470 * @flags: control flags
472 * Initializes *instance and corresponding 5380 chip,
473 * with flags OR'd into the initial flags value.
475 * Notes : I assume that the host, hostno, and id bits have been
476 * set correctly. I don't care about the irq and other fields.
478 * Returns 0 for success
481 static int NCR5380_init(struct Scsi_Host
*instance
, int flags
)
483 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
485 unsigned long deadline
;
487 hostdata
->host
= instance
;
488 hostdata
->id_mask
= 1 << instance
->this_id
;
489 hostdata
->id_higher_mask
= 0;
490 for (i
= hostdata
->id_mask
; i
<= 0x80; i
<<= 1)
491 if (i
> hostdata
->id_mask
)
492 hostdata
->id_higher_mask
|= i
;
493 for (i
= 0; i
< 8; ++i
)
494 hostdata
->busy
[i
] = 0;
495 hostdata
->dma_len
= 0;
497 spin_lock_init(&hostdata
->lock
);
498 hostdata
->connected
= NULL
;
499 hostdata
->sensing
= NULL
;
500 INIT_LIST_HEAD(&hostdata
->autosense
);
501 INIT_LIST_HEAD(&hostdata
->unissued
);
502 INIT_LIST_HEAD(&hostdata
->disconnected
);
504 hostdata
->flags
= flags
;
506 INIT_WORK(&hostdata
->main_task
, NCR5380_main
);
507 hostdata
->work_q
= alloc_workqueue("ncr5380_%d",
508 WQ_UNBOUND
| WQ_MEM_RECLAIM
,
509 1, instance
->host_no
);
510 if (!hostdata
->work_q
)
513 prepare_info(instance
);
515 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
516 NCR5380_write(MODE_REG
, MR_BASE
);
517 NCR5380_write(TARGET_COMMAND_REG
, 0);
518 NCR5380_write(SELECT_ENABLE_REG
, 0);
520 /* Calibrate register polling loop */
522 deadline
= jiffies
+ 1;
525 } while (time_is_after_jiffies(deadline
));
526 deadline
+= msecs_to_jiffies(256);
528 NCR5380_read(STATUS_REG
);
531 } while (time_is_after_jiffies(deadline
));
532 hostdata
->accesses_per_ms
= i
/ 256;
538 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
539 * @instance: adapter to check
541 * If the system crashed, it may have crashed with a connected target and
542 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
543 * currently established nexus, which we know nothing about. Failing that
546 * Note that a bus reset will cause the chip to assert IRQ.
548 * Returns 0 if successful, otherwise -ENXIO.
551 static int NCR5380_maybe_reset_bus(struct Scsi_Host
*instance
)
553 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
556 for (pass
= 1; (NCR5380_read(STATUS_REG
) & SR_BSY
) && pass
<= 6; ++pass
) {
561 shost_printk(KERN_ERR
, instance
, "SCSI bus busy, waiting up to five seconds\n");
562 NCR5380_poll_politely(instance
,
563 STATUS_REG
, SR_BSY
, 0, 5 * HZ
);
566 shost_printk(KERN_ERR
, instance
, "bus busy, attempting abort\n");
570 shost_printk(KERN_ERR
, instance
, "bus busy, attempting reset\n");
572 /* Wait after a reset; the SCSI standard calls for
573 * 250ms, we wait 500ms to be on the safe side.
574 * But some Toshiba CD-ROMs need ten times that.
576 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
582 shost_printk(KERN_ERR
, instance
, "bus locked solid\n");
590 * NCR5380_exit - remove an NCR5380
591 * @instance: adapter to remove
593 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
596 static void NCR5380_exit(struct Scsi_Host
*instance
)
598 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
600 cancel_work_sync(&hostdata
->main_task
);
601 destroy_workqueue(hostdata
->work_q
);
605 * complete_cmd - finish processing a command and return it to the SCSI ML
606 * @instance: the host instance
607 * @cmd: command to complete
610 static void complete_cmd(struct Scsi_Host
*instance
,
611 struct scsi_cmnd
*cmd
)
613 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
615 dsprintk(NDEBUG_QUEUES
, instance
, "complete_cmd: cmd %p\n", cmd
);
617 if (hostdata
->sensing
== cmd
) {
618 /* Autosense processing ends here */
619 if ((cmd
->result
& 0xff) != SAM_STAT_GOOD
) {
620 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
621 set_host_byte(cmd
, DID_ERROR
);
623 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
624 hostdata
->sensing
= NULL
;
627 hostdata
->busy
[scmd_id(cmd
)] &= ~(1 << cmd
->device
->lun
);
633 * NCR5380_queue_command - queue a command
634 * @instance: the relevant SCSI adapter
637 * cmd is added to the per-instance issue queue, with minor
638 * twiddling done to the host specific fields of cmd. If the
639 * main coroutine is not running, it is restarted.
642 static int NCR5380_queue_command(struct Scsi_Host
*instance
,
643 struct scsi_cmnd
*cmd
)
645 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
646 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(cmd
);
649 #if (NDEBUG & NDEBUG_NO_WRITE)
650 switch (cmd
->cmnd
[0]) {
653 shost_printk(KERN_DEBUG
, instance
, "WRITE attempted with NDEBUG_NO_WRITE set\n");
654 cmd
->result
= (DID_ERROR
<< 16);
658 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
662 spin_lock_irqsave(&hostdata
->lock
, flags
);
665 * Insert the cmd into the issue queue. Note that REQUEST SENSE
666 * commands are added to the head of the queue since any command will
667 * clear the contingent allegiance condition that exists and the
668 * sense data is only guaranteed to be valid while the condition exists.
671 if (cmd
->cmnd
[0] == REQUEST_SENSE
)
672 list_add(&ncmd
->list
, &hostdata
->unissued
);
674 list_add_tail(&ncmd
->list
, &hostdata
->unissued
);
676 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
678 dsprintk(NDEBUG_QUEUES
, instance
, "command %p added to %s of queue\n",
679 cmd
, (cmd
->cmnd
[0] == REQUEST_SENSE
) ? "head" : "tail");
681 /* Kick off command processing */
682 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
687 * dequeue_next_cmd - dequeue a command for processing
688 * @instance: the scsi host instance
690 * Priority is given to commands on the autosense queue. These commands
691 * need autosense because of a CHECK CONDITION result.
693 * Returns a command pointer if a command is found for a target that is
694 * not already busy. Otherwise returns NULL.
697 static struct scsi_cmnd
*dequeue_next_cmd(struct Scsi_Host
*instance
)
699 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
700 struct NCR5380_cmd
*ncmd
;
701 struct scsi_cmnd
*cmd
;
703 if (hostdata
->sensing
|| list_empty(&hostdata
->autosense
)) {
704 list_for_each_entry(ncmd
, &hostdata
->unissued
, list
) {
705 cmd
= NCR5380_to_scmd(ncmd
);
706 dsprintk(NDEBUG_QUEUES
, instance
, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
707 cmd
, scmd_id(cmd
), hostdata
->busy
[scmd_id(cmd
)], cmd
->device
->lun
);
709 if (!(hostdata
->busy
[scmd_id(cmd
)] & (1 << cmd
->device
->lun
))) {
710 list_del(&ncmd
->list
);
711 dsprintk(NDEBUG_QUEUES
, instance
,
712 "dequeue: removed %p from issue queue\n", cmd
);
717 /* Autosense processing begins here */
718 ncmd
= list_first_entry(&hostdata
->autosense
,
719 struct NCR5380_cmd
, list
);
720 list_del(&ncmd
->list
);
721 cmd
= NCR5380_to_scmd(ncmd
);
722 dsprintk(NDEBUG_QUEUES
, instance
,
723 "dequeue: removed %p from autosense queue\n", cmd
);
724 scsi_eh_prep_cmnd(cmd
, &hostdata
->ses
, NULL
, 0, ~0);
725 hostdata
->sensing
= cmd
;
731 static void requeue_cmd(struct Scsi_Host
*instance
, struct scsi_cmnd
*cmd
)
733 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
734 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(cmd
);
736 if (hostdata
->sensing
== cmd
) {
737 scsi_eh_restore_cmnd(cmd
, &hostdata
->ses
);
738 list_add(&ncmd
->list
, &hostdata
->autosense
);
739 hostdata
->sensing
= NULL
;
741 list_add(&ncmd
->list
, &hostdata
->unissued
);
745 * NCR5380_main - NCR state machines
747 * NCR5380_main is a coroutine that runs as long as more work can
748 * be done on the NCR5380 host adapters in a system. Both
749 * NCR5380_queue_command() and NCR5380_intr() will try to start it
750 * in case it is not running.
753 static void NCR5380_main(struct work_struct
*work
)
755 struct NCR5380_hostdata
*hostdata
=
756 container_of(work
, struct NCR5380_hostdata
, main_task
);
757 struct Scsi_Host
*instance
= hostdata
->host
;
763 spin_lock_irq(&hostdata
->lock
);
764 while (!hostdata
->connected
&& !hostdata
->selecting
) {
765 struct scsi_cmnd
*cmd
= dequeue_next_cmd(instance
);
770 dsprintk(NDEBUG_MAIN
, instance
, "main: dequeued %p\n", cmd
);
773 * Attempt to establish an I_T_L nexus here.
774 * On success, instance->hostdata->connected is set.
775 * On failure, we must add the command back to the
776 * issue queue so we can keep trying.
779 * REQUEST SENSE commands are issued without tagged
780 * queueing, even on SCSI-II devices because the
781 * contingent allegiance condition exists for the
785 if (!NCR5380_select(instance
, cmd
)) {
786 dsprintk(NDEBUG_MAIN
, instance
, "main: select complete\n");
788 dsprintk(NDEBUG_MAIN
| NDEBUG_QUEUES
, instance
,
789 "main: select failed, returning %p to queue\n", cmd
);
790 requeue_cmd(instance
, cmd
);
793 if (hostdata
->connected
&& !hostdata
->dma_len
) {
794 dsprintk(NDEBUG_MAIN
, instance
, "main: performing information transfer\n");
795 NCR5380_information_transfer(instance
);
798 spin_unlock_irq(&hostdata
->lock
);
804 #ifndef DONT_USE_INTR
807 * NCR5380_intr - generic NCR5380 irq handler
808 * @irq: interrupt number
809 * @dev_id: device info
811 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
812 * from the disconnected queue, and restarting NCR5380_main()
815 * The chip can assert IRQ in any of six different conditions. The IRQ flag
816 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
817 * Three of these six conditions are latched in the Bus and Status Register:
818 * - End of DMA (cleared by ending DMA Mode)
819 * - Parity error (cleared by reading RPIR)
820 * - Loss of BSY (cleared by reading RPIR)
821 * Two conditions have flag bits that are not latched:
822 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
823 * - Bus reset (non-maskable)
824 * The remaining condition has no flag bit at all:
825 * - Selection/reselection
827 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
828 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
829 * claimed that "the design of the [DP8490] interrupt logic ensures
830 * interrupts will not be lost (they can be on the DP5380)."
831 * The L5380/53C80 datasheet from LOGIC Devices has more details.
833 * Checking for bus reset by reading RST is futile because of interrupt
834 * latency, but a bus reset will reset chip logic. Checking for parity error
835 * is unnecessary because that interrupt is never enabled. A Loss of BSY
836 * condition will clear DMA Mode. We can tell when this occurs because the
837 * the Busy Monitor interrupt is enabled together with DMA Mode.
840 static irqreturn_t
NCR5380_intr(int irq
, void *dev_id
)
842 struct Scsi_Host
*instance
= dev_id
;
843 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
848 spin_lock_irqsave(&hostdata
->lock
, flags
);
850 basr
= NCR5380_read(BUS_AND_STATUS_REG
);
851 if (basr
& BASR_IRQ
) {
852 unsigned char mr
= NCR5380_read(MODE_REG
);
853 unsigned char sr
= NCR5380_read(STATUS_REG
);
855 dsprintk(NDEBUG_INTR
, instance
, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
858 if ((NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_mask
) &&
859 (sr
& (SR_SEL
| SR_IO
| SR_BSY
| SR_RST
)) == (SR_SEL
| SR_IO
)) {
860 /* Probably reselected */
861 NCR5380_write(SELECT_ENABLE_REG
, 0);
862 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
864 dsprintk(NDEBUG_INTR
, instance
, "interrupt with SEL and IO\n");
866 if (!hostdata
->connected
) {
867 NCR5380_reselect(instance
);
868 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
870 if (!hostdata
->connected
)
871 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
873 /* Probably Bus Reset */
874 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
876 dsprintk(NDEBUG_INTR
, instance
, "unknown interrupt\n");
880 shost_printk(KERN_NOTICE
, instance
, "interrupt without IRQ bit\n");
883 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
885 return IRQ_RETVAL(handled
);
891 * Function : int NCR5380_select(struct Scsi_Host *instance,
892 * struct scsi_cmnd *cmd)
894 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
895 * including ARBITRATION, SELECTION, and initial message out for
896 * IDENTIFY and queue messages.
898 * Inputs : instance - instantiation of the 5380 driver on which this
899 * target lives, cmd - SCSI command to execute.
901 * Returns cmd if selection failed but should be retried,
902 * NULL if selection failed and should not be retried, or
903 * NULL if selection succeeded (hostdata->connected == cmd).
906 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
907 * with registers as they should have been on entry - ie
908 * SELECT_ENABLE will be set appropriately, the NCR5380
909 * will cease to drive any SCSI bus signals.
911 * If successful : I_T_L or I_T_L_Q nexus will be established,
912 * instance->connected will be set to cmd.
913 * SELECT interrupt will be disabled.
915 * If failed (no target) : cmd->scsi_done() will be called, and the
916 * cmd->result host byte set to DID_BAD_TARGET.
919 static struct scsi_cmnd
*NCR5380_select(struct Scsi_Host
*instance
,
920 struct scsi_cmnd
*cmd
)
922 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
923 unsigned char tmp
[3], phase
;
928 NCR5380_dprint(NDEBUG_ARBITRATION
, instance
);
929 dsprintk(NDEBUG_ARBITRATION
, instance
, "starting arbitration, id = %d\n",
933 * Arbitration and selection phases are slow and involve dropping the
934 * lock, so we have to watch out for EH. An exception handler may
935 * change 'selecting' to NULL. This function will then return NULL
936 * so that the caller will forget about 'cmd'. (During information
937 * transfer phases, EH may change 'connected' to NULL.)
939 hostdata
->selecting
= cmd
;
942 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
943 * data bus during SELECTION.
946 NCR5380_write(TARGET_COMMAND_REG
, 0);
952 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
953 NCR5380_write(MODE_REG
, MR_ARBITRATE
);
955 /* The chip now waits for BUS FREE phase. Then after the 800 ns
956 * Bus Free Delay, arbitration will begin.
959 spin_unlock_irq(&hostdata
->lock
);
960 err
= NCR5380_poll_politely2(instance
, MODE_REG
, MR_ARBITRATE
, 0,
961 INITIATOR_COMMAND_REG
, ICR_ARBITRATION_PROGRESS
,
962 ICR_ARBITRATION_PROGRESS
, HZ
);
963 spin_lock_irq(&hostdata
->lock
);
964 if (!(NCR5380_read(MODE_REG
) & MR_ARBITRATE
)) {
965 /* Reselection interrupt */
968 if (!hostdata
->selecting
) {
969 /* Command was aborted */
970 NCR5380_write(MODE_REG
, MR_BASE
);
974 NCR5380_write(MODE_REG
, MR_BASE
);
975 shost_printk(KERN_ERR
, instance
,
976 "select: arbitration timeout\n");
979 spin_unlock_irq(&hostdata
->lock
);
981 /* The SCSI-2 arbitration delay is 2.4 us */
984 /* Check for lost arbitration */
985 if ((NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
) ||
986 (NCR5380_read(CURRENT_SCSI_DATA_REG
) & hostdata
->id_higher_mask
) ||
987 (NCR5380_read(INITIATOR_COMMAND_REG
) & ICR_ARBITRATION_LOST
)) {
988 NCR5380_write(MODE_REG
, MR_BASE
);
989 dsprintk(NDEBUG_ARBITRATION
, instance
, "lost arbitration, deasserting MR_ARBITRATE\n");
990 spin_lock_irq(&hostdata
->lock
);
994 /* After/during arbitration, BSY should be asserted.
995 * IBM DPES-31080 Version S31Q works now
996 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
998 NCR5380_write(INITIATOR_COMMAND_REG
,
999 ICR_BASE
| ICR_ASSERT_SEL
| ICR_ASSERT_BSY
);
1002 * Again, bus clear + bus settle time is 1.2us, however, this is
1003 * a minimum so we'll udelay ceil(1.2)
1006 if (hostdata
->flags
& FLAG_TOSHIBA_DELAY
)
1011 spin_lock_irq(&hostdata
->lock
);
1013 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1014 if (!(NCR5380_read(MODE_REG
) & MR_ARBITRATE
))
1017 if (!hostdata
->selecting
) {
1018 NCR5380_write(MODE_REG
, MR_BASE
);
1019 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1023 dsprintk(NDEBUG_ARBITRATION
, instance
, "won arbitration\n");
1026 * Now that we have won arbitration, start Selection process, asserting
1027 * the host and target ID's on the SCSI bus.
1030 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
| (1 << scmd_id(cmd
)));
1033 * Raise ATN while SEL is true before BSY goes false from arbitration,
1034 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1035 * phase immediately after selection.
1038 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
|
1039 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_SEL
);
1040 NCR5380_write(MODE_REG
, MR_BASE
);
1043 * Reselect interrupts must be turned off prior to the dropping of BSY,
1044 * otherwise we will trigger an interrupt.
1046 NCR5380_write(SELECT_ENABLE_REG
, 0);
1048 spin_unlock_irq(&hostdata
->lock
);
1051 * The initiator shall then wait at least two deskew delays and release
1054 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1057 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
|
1058 ICR_ASSERT_ATN
| ICR_ASSERT_SEL
);
1061 * Something weird happens when we cease to drive BSY - looks
1062 * like the board/chip is letting us do another read before the
1063 * appropriate propagation delay has expired, and we're confusing
1064 * a BSY signal from ourselves as the target's response to SELECTION.
1066 * A small delay (the 'C++' frontend breaks the pipeline with an
1067 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1068 * tighter 'C' code breaks and requires this) solves the problem -
1069 * the 1 us delay is arbitrary, and only used because this delay will
1070 * be the same on other platforms and since it works here, it should
1073 * wingel suggests that this could be due to failing to wait
1079 dsprintk(NDEBUG_SELECTION
, instance
, "selecting target %d\n", scmd_id(cmd
));
1082 * The SCSI specification calls for a 250 ms timeout for the actual
1086 err
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_BSY
, SR_BSY
,
1087 msecs_to_jiffies(250));
1089 if ((NCR5380_read(STATUS_REG
) & (SR_SEL
| SR_IO
)) == (SR_SEL
| SR_IO
)) {
1090 spin_lock_irq(&hostdata
->lock
);
1091 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1092 NCR5380_reselect(instance
);
1093 if (!hostdata
->connected
)
1094 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1095 shost_printk(KERN_ERR
, instance
, "reselection after won arbitration?\n");
1100 spin_lock_irq(&hostdata
->lock
);
1101 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1102 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1103 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1104 if (hostdata
->selecting
) {
1105 cmd
->result
= DID_BAD_TARGET
<< 16;
1106 complete_cmd(instance
, cmd
);
1107 dsprintk(NDEBUG_SELECTION
, instance
, "target did not respond within 250ms\n");
1114 * No less than two deskew delays after the initiator detects the
1115 * BSY signal is true, it shall release the SEL signal and may
1116 * change the DATA BUS. -wingel
1121 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1124 * Since we followed the SCSI spec, and raised ATN while SEL
1125 * was true but before BSY was false during selection, the information
1126 * transfer phase should be a MESSAGE OUT phase so that we can send the
1129 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1130 * message (2 bytes) with a tag ID that we increment with every command
1131 * until it wraps back to 0.
1133 * XXX - it turns out that there are some broken SCSI-II devices,
1134 * which claim to support tagged queuing but fail when more than
1135 * some number of commands are issued at once.
1138 /* Wait for start of REQ/ACK handshake */
1140 err
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
);
1141 spin_lock_irq(&hostdata
->lock
);
1143 shost_printk(KERN_ERR
, instance
, "select: REQ timeout\n");
1144 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1145 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1148 if (!hostdata
->selecting
) {
1153 dsprintk(NDEBUG_SELECTION
, instance
, "target %d selected, going into MESSAGE OUT phase.\n",
1155 tmp
[0] = IDENTIFY(((instance
->irq
== NO_IRQ
) ? 0 : 1), cmd
->device
->lun
);
1160 /* Send message(s) */
1162 phase
= PHASE_MSGOUT
;
1163 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1164 dsprintk(NDEBUG_SELECTION
, instance
, "nexus established.\n");
1165 /* XXX need to handle errors here */
1167 hostdata
->connected
= cmd
;
1168 hostdata
->busy
[cmd
->device
->id
] |= 1 << cmd
->device
->lun
;
1170 initialize_SCp(cmd
);
1175 if (!hostdata
->selecting
)
1177 hostdata
->selecting
= NULL
;
1182 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1183 * unsigned char *phase, int *count, unsigned char **data)
1185 * Purpose : transfers data in given phase using polled I/O
1187 * Inputs : instance - instance of driver, *phase - pointer to
1188 * what phase is expected, *count - pointer to number of
1189 * bytes to transfer, **data - pointer to data pointer.
1191 * Returns : -1 when different phase is entered without transferring
1192 * maximum number of bytes, 0 if all bytes are transferred or exit
1195 * Also, *phase, *count, *data are modified in place.
1197 * XXX Note : handling for bus free may be useful.
1201 * Note : this code is not as quick as it could be, however it
1202 * IS 100% reliable, and for the actual data transfer where speed
1203 * counts, we will always do a pseudo DMA or DMA transfer.
1206 static int NCR5380_transfer_pio(struct Scsi_Host
*instance
,
1207 unsigned char *phase
, int *count
,
1208 unsigned char **data
)
1210 unsigned char p
= *phase
, tmp
;
1212 unsigned char *d
= *data
;
1215 * The NCR5380 chip will only drive the SCSI bus when the
1216 * phase specified in the appropriate bits of the TARGET COMMAND
1217 * REGISTER match the STATUS REGISTER
1220 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1224 * Wait for assertion of REQ, after which the phase bits will be
1228 if (NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
) < 0)
1231 dsprintk(NDEBUG_HANDSHAKE
, instance
, "REQ asserted\n");
1233 /* Check for phase mismatch */
1234 if ((NCR5380_read(STATUS_REG
) & PHASE_MASK
) != p
) {
1235 dsprintk(NDEBUG_PIO
, instance
, "phase mismatch\n");
1236 NCR5380_dprint_phase(NDEBUG_PIO
, instance
);
1240 /* Do actual transfer from SCSI bus to / from memory */
1242 NCR5380_write(OUTPUT_DATA_REG
, *d
);
1244 *d
= NCR5380_read(CURRENT_SCSI_DATA_REG
);
1249 * The SCSI standard suggests that in MSGOUT phase, the initiator
1250 * should drop ATN on the last byte of the message phase
1251 * after REQ has been asserted for the handshake but before
1252 * the initiator raises ACK.
1256 if (!((p
& SR_MSG
) && c
> 1)) {
1257 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1258 NCR5380_dprint(NDEBUG_PIO
, instance
);
1259 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1260 ICR_ASSERT_DATA
| ICR_ASSERT_ACK
);
1262 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1263 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
);
1264 NCR5380_dprint(NDEBUG_PIO
, instance
);
1265 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1266 ICR_ASSERT_DATA
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1269 NCR5380_dprint(NDEBUG_PIO
, instance
);
1270 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
);
1273 if (NCR5380_poll_politely(instance
,
1274 STATUS_REG
, SR_REQ
, 0, 5 * HZ
) < 0)
1277 dsprintk(NDEBUG_HANDSHAKE
, instance
, "REQ negated, handshake complete\n");
1280 * We have several special cases to consider during REQ/ACK handshaking :
1281 * 1. We were in MSGOUT phase, and we are on the last byte of the
1282 * message. ATN must be dropped as ACK is dropped.
1284 * 2. We are in a MSGIN phase, and we are on the last byte of the
1285 * message. We must exit with ACK asserted, so that the calling
1286 * code may raise ATN before dropping ACK to reject the message.
1288 * 3. ACK and ATN are clear and the target may proceed as normal.
1290 if (!(p
== PHASE_MSGIN
&& c
== 1)) {
1291 if (p
== PHASE_MSGOUT
&& c
> 1)
1292 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1294 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1298 dsprintk(NDEBUG_PIO
, instance
, "residual %d\n", c
);
1302 tmp
= NCR5380_read(STATUS_REG
);
1303 /* The phase read from the bus is valid if either REQ is (already)
1304 * asserted or if ACK hasn't been released yet. The latter applies if
1305 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1307 if ((tmp
& SR_REQ
) || ((tmp
& SR_IO
) && c
== 0))
1308 *phase
= tmp
& PHASE_MASK
;
1310 *phase
= PHASE_UNKNOWN
;
1312 if (!c
|| (*phase
== p
))
1319 * do_reset - issue a reset command
1320 * @instance: adapter to reset
1322 * Issue a reset sequence to the NCR5380 and try and get the bus
1323 * back into sane shape.
1325 * This clears the reset interrupt flag because there may be no handler for
1326 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1327 * been installed. And when in EH we may have released the ST DMA interrupt.
1330 static void do_reset(struct Scsi_Host
*instance
)
1332 unsigned long flags
;
1334 local_irq_save(flags
);
1335 NCR5380_write(TARGET_COMMAND_REG
,
1336 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG
) & PHASE_MASK
));
1337 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
);
1339 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1340 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1341 local_irq_restore(flags
);
1345 * do_abort - abort the currently established nexus by going to
1346 * MESSAGE OUT phase and sending an ABORT message.
1347 * @instance: relevant scsi host instance
1349 * Returns 0 on success, -1 on failure.
1352 static int do_abort(struct Scsi_Host
*instance
)
1354 unsigned char *msgptr
, phase
, tmp
;
1358 /* Request message out phase */
1359 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1362 * Wait for the target to indicate a valid phase by asserting
1363 * REQ. Once this happens, we'll have either a MSGOUT phase
1364 * and can immediately send the ABORT message, or we'll have some
1365 * other phase and will have to source/sink data.
1367 * We really don't care what value was on the bus or what value
1368 * the target sees, so we just handshake.
1371 rc
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, 10 * HZ
);
1375 tmp
= NCR5380_read(STATUS_REG
) & PHASE_MASK
;
1377 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1379 if (tmp
!= PHASE_MSGOUT
) {
1380 NCR5380_write(INITIATOR_COMMAND_REG
,
1381 ICR_BASE
| ICR_ASSERT_ATN
| ICR_ASSERT_ACK
);
1382 rc
= NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, 0, 3 * HZ
);
1385 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1391 phase
= PHASE_MSGOUT
;
1392 NCR5380_transfer_pio(instance
, &phase
, &len
, &msgptr
);
1395 * If we got here, and the command completed successfully,
1396 * we're about to go into bus free state.
1399 return len
? -1 : 0;
1402 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1407 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1408 * unsigned char *phase, int *count, unsigned char **data)
1410 * Purpose : transfers data in given phase using either real
1413 * Inputs : instance - instance of driver, *phase - pointer to
1414 * what phase is expected, *count - pointer to number of
1415 * bytes to transfer, **data - pointer to data pointer.
1417 * Returns : -1 when different phase is entered without transferring
1418 * maximum number of bytes, 0 if all bytes or transferred or exit
1421 * Also, *phase, *count, *data are modified in place.
1425 static int NCR5380_transfer_dma(struct Scsi_Host
*instance
,
1426 unsigned char *phase
, int *count
,
1427 unsigned char **data
)
1429 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1430 register int c
= *count
;
1431 register unsigned char p
= *phase
;
1432 register unsigned char *d
= *data
;
1436 if ((tmp
= (NCR5380_read(STATUS_REG
) & PHASE_MASK
)) != p
) {
1441 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(p
));
1444 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1445 * were not disabled for the duration of a single DMA transfer, from
1446 * before the setting of DMA mode to after transfer of the last byte.
1449 if (hostdata
->flags
& FLAG_DMA_FIXUP
)
1450 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
| MR_MONITOR_BSY
);
1452 NCR5380_write(MODE_REG
, MR_BASE
| MR_DMA_MODE
| MR_MONITOR_BSY
|
1453 MR_ENABLE_EOP_INTR
);
1455 dprintk(NDEBUG_DMA
, "scsi%d : mode reg = 0x%X\n", instance
->host_no
, NCR5380_read(MODE_REG
));
1458 * On the PAS16 at least I/O recovery delays are not needed here.
1459 * Everyone else seems to want them.
1463 NCR5380_io_delay(1);
1464 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG
, 0);
1466 NCR5380_io_delay(1);
1467 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_DATA
);
1468 NCR5380_io_delay(1);
1469 NCR5380_write(START_DMA_SEND_REG
, 0);
1470 NCR5380_io_delay(1);
1474 * A note regarding the DMA errata workarounds for early NMOS silicon.
1476 * For DMA sends, we want to wait until the last byte has been
1477 * transferred out over the bus before we turn off DMA mode. Alas, there
1478 * seems to be no terribly good way of doing this on a 5380 under all
1479 * conditions. For non-scatter-gather operations, we can wait until REQ
1480 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1481 * are nastier, since the device will be expecting more data than we
1482 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1483 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1484 * why this signal was added to the newer chips) but on the older 538[01]
1485 * this signal does not exist. The workaround for this lack is a watchdog;
1486 * we bail out of the wait-loop after a modest amount of wait-time if
1487 * the usual exit conditions are not met. Not a terribly clean or
1488 * correct solution :-%
1490 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1491 * If the chip is in DMA receive mode, it will respond to a target's
1492 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1493 * ACK, even if it has _already_ been notified by the DMA controller that
1494 * the current DMA transfer has completed! If the NCR5380 is then taken
1495 * out of DMA mode, this already-acknowledged byte is lost. This is
1496 * not a problem for "one DMA transfer per READ command", because
1497 * the situation will never arise... either all of the data is DMA'ed
1498 * properly, or the target switches to MESSAGE IN phase to signal a
1499 * disconnection (either operation bringing the DMA to a clean halt).
1500 * However, in order to handle scatter-receive, we must work around the
1501 * problem. The chosen fix is to DMA fewer bytes, then check for the
1502 * condition before taking the NCR5380 out of DMA mode. One or two extra
1503 * bytes are transferred via PIO as necessary to fill out the original
1508 foo
= NCR5380_dma_recv_setup(instance
, d
,
1509 hostdata
->flags
& FLAG_DMA_FIXUP
? c
- 1 : c
);
1510 if (!foo
&& (hostdata
->flags
& FLAG_DMA_FIXUP
)) {
1512 * The workaround was to transfer fewer bytes than we
1513 * intended to with the pseudo-DMA read function, wait for
1514 * the chip to latch the last byte, read it, and then disable
1517 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1518 * REQ is deasserted when ACK is asserted, and not reasserted
1519 * until ACK goes false. Since the NCR5380 won't lower ACK
1520 * until DACK is asserted, which won't happen unless we twiddle
1521 * the DMA port or we take the NCR5380 out of DMA mode, we
1522 * can guarantee that we won't handshake another extra
1526 if (NCR5380_poll_politely(instance
, BUS_AND_STATUS_REG
,
1527 BASR_DRQ
, BASR_DRQ
, HZ
) < 0) {
1529 shost_printk(KERN_ERR
, instance
, "PDMA read: DRQ timeout\n");
1531 if (NCR5380_poll_politely(instance
, STATUS_REG
,
1532 SR_REQ
, 0, HZ
) < 0) {
1534 shost_printk(KERN_ERR
, instance
, "PDMA read: !REQ timeout\n");
1536 d
[c
- 1] = NCR5380_read(INPUT_DATA_REG
);
1539 foo
= NCR5380_dma_send_setup(instance
, d
, c
);
1540 if (!foo
&& (hostdata
->flags
& FLAG_DMA_FIXUP
)) {
1542 * Wait for the last byte to be sent. If REQ is being asserted for
1543 * the byte we're interested, we'll ACK it and it will go false.
1545 if (NCR5380_poll_politely2(instance
,
1546 BUS_AND_STATUS_REG
, BASR_DRQ
, BASR_DRQ
,
1547 BUS_AND_STATUS_REG
, BASR_PHASE_MATCH
, 0, HZ
) < 0) {
1549 shost_printk(KERN_ERR
, instance
, "PDMA write: DRQ and phase timeout\n");
1553 NCR5380_write(MODE_REG
, MR_BASE
);
1554 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1555 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
1558 *phase
= NCR5380_read(STATUS_REG
) & PHASE_MASK
;
1563 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1565 * Purpose : run through the various SCSI phases and do as the target
1566 * directs us to. Operates on the currently connected command,
1567 * instance->connected.
1569 * Inputs : instance, instance for which we are doing commands
1571 * Side effects : SCSI things happen, the disconnected queue will be
1572 * modified if a command disconnects, *instance->connected will
1575 * XXX Note : we need to watch for bus free or a reset condition here
1576 * to recover from an unexpected bus free condition.
1579 static void NCR5380_information_transfer(struct Scsi_Host
*instance
)
1581 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1582 unsigned char msgout
= NOP
;
1586 unsigned char *data
;
1587 unsigned char phase
, tmp
, extended_msg
[10], old_phase
= 0xff;
1588 struct scsi_cmnd
*cmd
;
1590 while ((cmd
= hostdata
->connected
)) {
1591 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(cmd
);
1593 tmp
= NCR5380_read(STATUS_REG
);
1594 /* We only have a valid SCSI phase when REQ is asserted */
1596 phase
= (tmp
& PHASE_MASK
);
1597 if (phase
!= old_phase
) {
1599 NCR5380_dprint_phase(NDEBUG_INFORMATION
, instance
);
1601 if (sink
&& (phase
!= PHASE_MSGOUT
)) {
1602 NCR5380_write(TARGET_COMMAND_REG
, PHASE_SR_TO_TCR(tmp
));
1604 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
|
1606 while (NCR5380_read(STATUS_REG
) & SR_REQ
)
1608 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
|
1616 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1617 shost_printk(KERN_DEBUG
, instance
, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
1620 cmd
->result
= DID_ERROR
<< 16;
1621 complete_cmd(instance
, cmd
);
1622 hostdata
->connected
= NULL
;
1627 * If there is no room left in the current buffer in the
1628 * scatter-gather list, move onto the next one.
1631 if (!cmd
->SCp
.this_residual
&& cmd
->SCp
.buffers_residual
) {
1633 --cmd
->SCp
.buffers_residual
;
1634 cmd
->SCp
.this_residual
= cmd
->SCp
.buffer
->length
;
1635 cmd
->SCp
.ptr
= sg_virt(cmd
->SCp
.buffer
);
1636 dsprintk(NDEBUG_INFORMATION
, instance
, "%d bytes and %d buffers left\n",
1637 cmd
->SCp
.this_residual
,
1638 cmd
->SCp
.buffers_residual
);
1642 * The preferred transfer method is going to be
1643 * PSEUDO-DMA for systems that are strictly PIO,
1644 * since we can let the hardware do the handshaking.
1646 * For this to work, we need to know the transfersize
1647 * ahead of time, since the pseudo-DMA code will sit
1648 * in an unconditional loop.
1652 if (!cmd
->device
->borken
)
1653 transfersize
= NCR5380_dma_xfer_len(instance
, cmd
, phase
);
1657 if (NCR5380_transfer_dma(instance
, &phase
,
1658 &len
, (unsigned char **)&cmd
->SCp
.ptr
)) {
1660 * If the watchdog timer fires, all future
1661 * accesses to this device will use the
1664 scmd_printk(KERN_INFO
, cmd
,
1665 "switching to slow handshake\n");
1666 cmd
->device
->borken
= 1;
1669 cmd
->result
= DID_ERROR
<< 16;
1670 /* XXX - need to source or sink data here, as appropriate */
1672 cmd
->SCp
.this_residual
-= transfersize
- len
;
1674 /* Break up transfer into 3 ms chunks,
1675 * presuming 6 accesses per handshake.
1677 transfersize
= min((unsigned long)cmd
->SCp
.this_residual
,
1678 hostdata
->accesses_per_ms
/ 2);
1680 NCR5380_transfer_pio(instance
, &phase
, &len
,
1681 (unsigned char **)&cmd
->SCp
.ptr
);
1682 cmd
->SCp
.this_residual
-= transfersize
- len
;
1688 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1689 cmd
->SCp
.Message
= tmp
;
1693 case COMMAND_COMPLETE
:
1694 /* Accept message by clearing ACK */
1696 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1697 dsprintk(NDEBUG_QUEUES
, instance
,
1698 "COMMAND COMPLETE %p target %d lun %llu\n",
1699 cmd
, scmd_id(cmd
), cmd
->device
->lun
);
1701 hostdata
->connected
= NULL
;
1703 cmd
->result
&= ~0xffff;
1704 cmd
->result
|= cmd
->SCp
.Status
;
1705 cmd
->result
|= cmd
->SCp
.Message
<< 8;
1707 if (cmd
->cmnd
[0] == REQUEST_SENSE
)
1708 complete_cmd(instance
, cmd
);
1710 if (cmd
->SCp
.Status
== SAM_STAT_CHECK_CONDITION
||
1711 cmd
->SCp
.Status
== SAM_STAT_COMMAND_TERMINATED
) {
1712 dsprintk(NDEBUG_QUEUES
, instance
, "autosense: adding cmd %p to tail of autosense queue\n",
1714 list_add_tail(&ncmd
->list
,
1715 &hostdata
->autosense
);
1717 complete_cmd(instance
, cmd
);
1721 * Restore phase bits to 0 so an interrupted selection,
1722 * arbitration can resume.
1724 NCR5380_write(TARGET_COMMAND_REG
, 0);
1726 /* Enable reselect interrupts */
1727 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1729 case MESSAGE_REJECT
:
1730 /* Accept message by clearing ACK */
1731 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1732 switch (hostdata
->last_message
) {
1733 case HEAD_OF_QUEUE_TAG
:
1734 case ORDERED_QUEUE_TAG
:
1735 case SIMPLE_QUEUE_TAG
:
1736 cmd
->device
->simple_tags
= 0;
1737 hostdata
->busy
[cmd
->device
->id
] |= (1 << (cmd
->device
->lun
& 0xFF));
1744 /* Accept message by clearing ACK */
1745 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1746 hostdata
->connected
= NULL
;
1747 list_add(&ncmd
->list
, &hostdata
->disconnected
);
1748 dsprintk(NDEBUG_INFORMATION
| NDEBUG_QUEUES
,
1749 instance
, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1750 cmd
, scmd_id(cmd
), cmd
->device
->lun
);
1753 * Restore phase bits to 0 so an interrupted selection,
1754 * arbitration can resume.
1756 NCR5380_write(TARGET_COMMAND_REG
, 0);
1758 /* Enable reselect interrupts */
1759 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1762 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1763 * operation, in violation of the SCSI spec so we can safely
1764 * ignore SAVE/RESTORE pointers calls.
1766 * Unfortunately, some disks violate the SCSI spec and
1767 * don't issue the required SAVE_POINTERS message before
1768 * disconnecting, and we have to break spec to remain
1772 case RESTORE_POINTERS
:
1773 /* Accept message by clearing ACK */
1774 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1776 case EXTENDED_MESSAGE
:
1778 * Start the message buffer with the EXTENDED_MESSAGE
1779 * byte, since spi_print_msg() wants the whole thing.
1781 extended_msg
[0] = EXTENDED_MESSAGE
;
1782 /* Accept first byte by clearing ACK */
1783 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1785 spin_unlock_irq(&hostdata
->lock
);
1787 dsprintk(NDEBUG_EXTENDED
, instance
, "receiving extended message\n");
1790 data
= extended_msg
+ 1;
1791 phase
= PHASE_MSGIN
;
1792 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1793 dsprintk(NDEBUG_EXTENDED
, instance
, "length %d, code 0x%02x\n",
1794 (int)extended_msg
[1],
1795 (int)extended_msg
[2]);
1797 if (!len
&& extended_msg
[1] > 0 &&
1798 extended_msg
[1] <= sizeof(extended_msg
) - 2) {
1799 /* Accept third byte by clearing ACK */
1800 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1801 len
= extended_msg
[1] - 1;
1802 data
= extended_msg
+ 3;
1803 phase
= PHASE_MSGIN
;
1805 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1806 dsprintk(NDEBUG_EXTENDED
, instance
, "message received, residual %d\n",
1809 switch (extended_msg
[2]) {
1812 case EXTENDED_MODIFY_DATA_POINTER
:
1813 case EXTENDED_EXTENDED_IDENTIFY
:
1817 shost_printk(KERN_ERR
, instance
, "error receiving extended message\n");
1820 shost_printk(KERN_NOTICE
, instance
, "extended message code %02x length %d is too long\n",
1821 extended_msg
[2], extended_msg
[1]);
1825 spin_lock_irq(&hostdata
->lock
);
1826 if (!hostdata
->connected
)
1829 /* Fall through to reject message */
1832 * If we get something weird that we aren't expecting,
1837 shost_printk(KERN_ERR
, instance
, "rejecting message ");
1838 spi_print_msg(extended_msg
);
1840 } else if (tmp
!= EXTENDED_MESSAGE
)
1841 scmd_printk(KERN_INFO
, cmd
,
1842 "rejecting unknown message %02x\n",
1845 scmd_printk(KERN_INFO
, cmd
,
1846 "rejecting unknown extended message code %02x, length %d\n",
1847 extended_msg
[1], extended_msg
[0]);
1849 msgout
= MESSAGE_REJECT
;
1850 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ATN
);
1852 } /* switch (tmp) */
1857 hostdata
->last_message
= msgout
;
1858 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1859 if (msgout
== ABORT
) {
1860 hostdata
->connected
= NULL
;
1861 cmd
->result
= DID_ERROR
<< 16;
1862 complete_cmd(instance
, cmd
);
1863 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
1872 * XXX for performance reasons, on machines with a
1873 * PSEUDO-DMA architecture we should probably
1874 * use the dma transfer function.
1876 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1881 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1882 cmd
->SCp
.Status
= tmp
;
1885 shost_printk(KERN_ERR
, instance
, "unknown phase\n");
1886 NCR5380_dprint(NDEBUG_ANY
, instance
);
1887 } /* switch(phase) */
1889 spin_unlock_irq(&hostdata
->lock
);
1890 NCR5380_poll_politely(instance
, STATUS_REG
, SR_REQ
, SR_REQ
, HZ
);
1891 spin_lock_irq(&hostdata
->lock
);
1897 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
1899 * Purpose : does reselection, initializing the instance->connected
1900 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1901 * nexus has been reestablished,
1903 * Inputs : instance - this instance of the NCR5380.
1906 static void NCR5380_reselect(struct Scsi_Host
*instance
)
1908 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
1909 unsigned char target_mask
;
1910 unsigned char lun
, phase
;
1912 unsigned char msg
[3];
1913 unsigned char *data
;
1914 struct NCR5380_cmd
*ncmd
;
1915 struct scsi_cmnd
*tmp
;
1918 * Disable arbitration, etc. since the host adapter obviously
1919 * lost, and tell an interrupted NCR5380_select() to restart.
1922 NCR5380_write(MODE_REG
, MR_BASE
);
1924 target_mask
= NCR5380_read(CURRENT_SCSI_DATA_REG
) & ~(hostdata
->id_mask
);
1926 dsprintk(NDEBUG_RESELECTION
, instance
, "reselect\n");
1929 * At this point, we have detected that our SCSI ID is on the bus,
1930 * SEL is true and BSY was false for at least one bus settle delay
1933 * We must assert BSY ourselves, until the target drops the SEL
1937 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_BSY
);
1938 if (NCR5380_poll_politely(instance
,
1939 STATUS_REG
, SR_SEL
, 0, 2 * HZ
) < 0) {
1940 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1943 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
1946 * Wait for target to go into MSGIN.
1949 if (NCR5380_poll_politely(instance
,
1950 STATUS_REG
, SR_REQ
, SR_REQ
, 2 * HZ
) < 0) {
1957 phase
= PHASE_MSGIN
;
1958 NCR5380_transfer_pio(instance
, &phase
, &len
, &data
);
1965 if (!(msg
[0] & 0x80)) {
1966 shost_printk(KERN_ERR
, instance
, "expecting IDENTIFY message, got ");
1972 lun
= msg
[0] & 0x07;
1975 * We need to add code for SCSI-II to track which devices have
1976 * I_T_L_Q nexuses established, and which have simple I_T_L
1977 * nexuses so we can chose to do additional data transfer.
1981 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
1982 * just reestablished, and remove it from the disconnected queue.
1986 list_for_each_entry(ncmd
, &hostdata
->disconnected
, list
) {
1987 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
1989 if (target_mask
== (1 << scmd_id(cmd
)) &&
1990 lun
== (u8
)cmd
->device
->lun
) {
1991 list_del(&ncmd
->list
);
1998 dsprintk(NDEBUG_RESELECTION
| NDEBUG_QUEUES
, instance
,
1999 "reselect: removed %p from disconnected queue\n", tmp
);
2001 shost_printk(KERN_ERR
, instance
, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2004 * Since we have an established nexus that we can't do anything
2005 * with, we must abort it.
2011 /* Accept message by clearing ACK */
2012 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
2014 hostdata
->connected
= tmp
;
2015 dsprintk(NDEBUG_RESELECTION
, instance
, "nexus established, target %d, lun %llu, tag %d\n",
2016 scmd_id(tmp
), tmp
->device
->lun
, tmp
->tag
);
2020 * list_find_cmd - test for presence of a command in a linked list
2021 * @haystack: list of commands
2022 * @needle: command to search for
2025 static bool list_find_cmd(struct list_head
*haystack
,
2026 struct scsi_cmnd
*needle
)
2028 struct NCR5380_cmd
*ncmd
;
2030 list_for_each_entry(ncmd
, haystack
, list
)
2031 if (NCR5380_to_scmd(ncmd
) == needle
)
2037 * list_remove_cmd - remove a command from linked list
2038 * @haystack: list of commands
2039 * @needle: command to remove
2042 static bool list_del_cmd(struct list_head
*haystack
,
2043 struct scsi_cmnd
*needle
)
2045 if (list_find_cmd(haystack
, needle
)) {
2046 struct NCR5380_cmd
*ncmd
= scsi_cmd_priv(needle
);
2048 list_del(&ncmd
->list
);
2055 * NCR5380_abort - scsi host eh_abort_handler() method
2056 * @cmd: the command to be aborted
2058 * Try to abort a given command by removing it from queues and/or sending
2059 * the target an abort message. This may not succeed in causing a target
2060 * to abort the command. Nonetheless, the low-level driver must forget about
2061 * the command because the mid-layer reclaims it and it may be re-issued.
2063 * The normal path taken by a command is as follows. For EH we trace this
2064 * same path to locate and abort the command.
2066 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2067 * [disconnected -> connected ->]...
2068 * [autosense -> connected ->] done
2070 * If cmd was not found at all then presumably it has already been completed,
2071 * in which case return SUCCESS to try to avoid further EH measures.
2073 * If the command has not completed yet, we must not fail to find it.
2074 * We have no option but to forget the aborted command (even if it still
2075 * lacks sense data). The mid-layer may re-issue a command that is in error
2076 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2077 * this driver are such that a command can appear on one queue only.
2079 * The lock protects driver data structures, but EH handlers also use it
2080 * to serialize their own execution and prevent their own re-entry.
2083 static int NCR5380_abort(struct scsi_cmnd
*cmd
)
2085 struct Scsi_Host
*instance
= cmd
->device
->host
;
2086 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2087 unsigned long flags
;
2088 int result
= SUCCESS
;
2090 spin_lock_irqsave(&hostdata
->lock
, flags
);
2092 #if (NDEBUG & NDEBUG_ANY)
2093 scmd_printk(KERN_INFO
, cmd
, __func__
);
2095 NCR5380_dprint(NDEBUG_ANY
, instance
);
2096 NCR5380_dprint_phase(NDEBUG_ANY
, instance
);
2098 if (list_del_cmd(&hostdata
->unissued
, cmd
)) {
2099 dsprintk(NDEBUG_ABORT
, instance
,
2100 "abort: removed %p from issue queue\n", cmd
);
2101 cmd
->result
= DID_ABORT
<< 16;
2102 cmd
->scsi_done(cmd
); /* No tag or busy flag to worry about */
2106 if (hostdata
->selecting
== cmd
) {
2107 dsprintk(NDEBUG_ABORT
, instance
,
2108 "abort: cmd %p == selecting\n", cmd
);
2109 hostdata
->selecting
= NULL
;
2110 cmd
->result
= DID_ABORT
<< 16;
2111 complete_cmd(instance
, cmd
);
2115 if (list_del_cmd(&hostdata
->disconnected
, cmd
)) {
2116 dsprintk(NDEBUG_ABORT
, instance
,
2117 "abort: removed %p from disconnected list\n", cmd
);
2118 /* Can't call NCR5380_select() and send ABORT because that
2119 * means releasing the lock. Need a bus reset.
2121 set_host_byte(cmd
, DID_ERROR
);
2122 complete_cmd(instance
, cmd
);
2127 if (hostdata
->connected
== cmd
) {
2128 dsprintk(NDEBUG_ABORT
, instance
, "abort: cmd %p is connected\n", cmd
);
2129 hostdata
->connected
= NULL
;
2130 hostdata
->dma_len
= 0;
2131 if (do_abort(instance
)) {
2132 set_host_byte(cmd
, DID_ERROR
);
2133 complete_cmd(instance
, cmd
);
2137 set_host_byte(cmd
, DID_ABORT
);
2138 complete_cmd(instance
, cmd
);
2142 if (list_del_cmd(&hostdata
->autosense
, cmd
)) {
2143 dsprintk(NDEBUG_ABORT
, instance
,
2144 "abort: removed %p from sense queue\n", cmd
);
2145 set_host_byte(cmd
, DID_ERROR
);
2146 complete_cmd(instance
, cmd
);
2150 if (result
== FAILED
)
2151 dsprintk(NDEBUG_ABORT
, instance
, "abort: failed to abort %p\n", cmd
);
2153 dsprintk(NDEBUG_ABORT
, instance
, "abort: successfully aborted %p\n", cmd
);
2155 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
2156 spin_unlock_irqrestore(&hostdata
->lock
, flags
);
2163 * NCR5380_bus_reset - reset the SCSI bus
2164 * @cmd: SCSI command undergoing EH
2169 static int NCR5380_bus_reset(struct scsi_cmnd
*cmd
)
2171 struct Scsi_Host
*instance
= cmd
->device
->host
;
2172 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
2174 unsigned long flags
;
2175 struct NCR5380_cmd
*ncmd
;
2177 spin_lock_irqsave(&hostdata
->lock
, flags
);
2179 #if (NDEBUG & NDEBUG_ANY)
2180 scmd_printk(KERN_INFO
, cmd
, __func__
);
2182 NCR5380_dprint(NDEBUG_ANY
, instance
);
2183 NCR5380_dprint_phase(NDEBUG_ANY
, instance
);
2187 /* reset NCR registers */
2188 NCR5380_write(MODE_REG
, MR_BASE
);
2189 NCR5380_write(TARGET_COMMAND_REG
, 0);
2190 NCR5380_write(SELECT_ENABLE_REG
, 0);
2192 /* After the reset, there are no more connected or disconnected commands
2193 * and no busy units; so clear the low-level status here to avoid
2194 * conflicts when the mid-level code tries to wake up the affected
2198 if (list_del_cmd(&hostdata
->unissued
, cmd
)) {
2199 cmd
->result
= DID_RESET
<< 16;
2200 cmd
->scsi_done(cmd
);
2203 if (hostdata
->selecting
) {
2204 hostdata
->selecting
->result
= DID_RESET
<< 16;
2205 complete_cmd(instance
, hostdata
->selecting
);
2206 hostdata
->selecting
= NULL
;
2209 list_for_each_entry(ncmd
, &hostdata
->disconnected
, list
) {
2210 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
2212 set_host_byte(cmd
, DID_RESET
);
2213 cmd
->scsi_done(cmd
);
2215 INIT_LIST_HEAD(&hostdata
->disconnected
);
2217 list_for_each_entry(ncmd
, &hostdata
->autosense
, list
) {
2218 struct scsi_cmnd
*cmd
= NCR5380_to_scmd(ncmd
);
2220 set_host_byte(cmd
, DID_RESET
);
2221 cmd
->scsi_done(cmd
);
2223 INIT_LIST_HEAD(&hostdata
->autosense
);
2225 if (hostdata
->connected
) {
2226 set_host_byte(hostdata
->connected
, DID_RESET
);
2227 complete_cmd(instance
, hostdata
->connected
);
2228 hostdata
->connected
= NULL
;
2231 for (i
= 0; i
< 8; ++i
)
2232 hostdata
->busy
[i
] = 0;
2233 hostdata
->dma_len
= 0;
2235 queue_work(hostdata
->work_q
, &hostdata
->main_task
);
2236 spin_unlock_irqrestore(&hostdata
->lock
, flags
);