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 * Revision 1.10 1998/9/2 Alan Cox
29 * (alan@lxorguk.ukuu.org.uk)
30 * Fixed up the timer lockups reported so far. Things still suck. Looking
31 * forward to 2.3 and per device request queues. Then it'll be possible to
32 * SMP thread this beast and improve life no end.
34 * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
35 * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
36 * (hopefully) fixed and enhanced USLEEP
37 * added support for DTC3181E card (for Mustek scanner)
40 * Revision 1.8 Ingmar Baumgart
41 * (ingmar@gonzo.schwaben.de)
42 * added support for NCR53C400a card
45 * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
47 * added support needed for DTC 3180/3280
48 * fixed a couple of bugs
51 * Revision 1.5 1994/01/19 09:14:57 drew
52 * Fixed udelay() hack that was being used on DATAOUT phases
53 * instead of a proper wait for the final handshake.
55 * Revision 1.4 1994/01/19 06:44:25 drew
56 * *** empty log message ***
58 * Revision 1.3 1994/01/19 05:24:40 drew
59 * Added support for TCR LAST_BYTE_SENT bit.
61 * Revision 1.2 1994/01/15 06:14:11 drew
62 * REAL DMA support, bug fixes.
64 * Revision 1.1 1994/01/15 06:00:54 drew
70 * Further development / testing that should be done :
71 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
72 * code so that everything does the same thing that's done at the
73 * end of a pseudo-DMA read operation.
75 * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
76 * basically, transfer size needs to be reduced by one
77 * and the last byte read as is done with PSEUDO_DMA.
79 * 4. Test SCSI-II tagged queueing (I have no devices which support
87 #ifdef BOARD_REQUIRES_NO_DELAY
88 #define io_recovery_delay(x)
90 #define io_recovery_delay(x) udelay(x)
96 * This is a generic 5380 driver. To use it on a different platform,
97 * one simply writes appropriate system specific macros (ie, data
98 * transfer - some PC's will use the I/O bus, 68K's must use
99 * memory mapped) and drops this file in their 'C' wrapper.
101 * (Note from hch: unfortunately it was not enough for the different
102 * m68k folks and instead of improving this driver they copied it
103 * and hacked it up for their needs. As a consequence they lost
104 * most updates to this driver. Maybe someone will fix all these
105 * drivers to use a common core one day..)
107 * As far as command queueing, two queues are maintained for
108 * each 5380 in the system - commands that haven't been issued yet,
109 * and commands that are currently executing. This means that an
110 * unlimited number of commands may be queued, letting
111 * more commands propagate from the higher driver levels giving higher
112 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
113 * allowing multiple commands to propagate all the way to a SCSI-II device
114 * while a command is already executing.
117 * Issues specific to the NCR5380 :
119 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
120 * piece of hardware that requires you to sit in a loop polling for
121 * the REQ signal as long as you are connected. Some devices are
122 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
123 * while doing long seek operations. [...] These
124 * broken devices are the exception rather than the rule and I'd rather
125 * spend my time optimizing for the normal case.
129 * At the heart of the design is a coroutine, NCR5380_main,
130 * which is started from a workqueue for each NCR5380 host in the
131 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
132 * removing the commands from the issue queue and calling
133 * NCR5380_select() if a nexus is not established.
135 * Once a nexus is established, the NCR5380_information_transfer()
136 * phase goes through the various phases as instructed by the target.
137 * if the target goes into MSG IN and sends a DISCONNECT message,
138 * the command structure is placed into the per instance disconnected
139 * queue, and NCR5380_main tries to find more work. If the target is
140 * idle for too long, the system will try to sleep.
142 * If a command has disconnected, eventually an interrupt will trigger,
143 * calling NCR5380_intr() which will in turn call NCR5380_reselect
144 * to reestablish a nexus. This will run main if necessary.
146 * On command termination, the done function will be called as
149 * SCSI pointers are maintained in the SCp field of SCSI command
150 * structures, being initialized after the command is connected
151 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
152 * Note that in violation of the standard, an implicit SAVE POINTERS operation
153 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
158 * This file a skeleton Linux SCSI driver for the NCR 5380 series
159 * of chips. To use it, you write an architecture specific functions
160 * and macros and include this file in your driver.
162 * These macros control options :
163 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
166 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
167 * for commands that return with a CHECK CONDITION status.
169 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
172 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
173 * override-configure an IRQ.
175 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
177 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
179 * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
180 * rely on phase mismatch and EOP interrupts to determine end
183 * Defaults for these will be provided although the user may want to adjust
184 * these to allocate CPU resources to the SCSI driver or "real" code.
186 * These macros MUST be defined :
188 * NCR5380_read(register) - read from the specified register
190 * NCR5380_write(register, value) - write to the specific register
192 * NCR5380_implementation_fields - additional fields needed for this
193 * specific implementation of the NCR5380
195 * Either real DMA *or* pseudo DMA may be implemented
197 * NCR5380_REAL_DMA should be defined if real DMA is to be used.
198 * Note that the DMA setup functions should return the number of bytes
199 * that they were able to program the controller for.
201 * Also note that generic i386/PC versions of these macros are
202 * available as NCR5380_i386_dma_write_setup,
203 * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
205 * NCR5380_dma_write_setup(instance, src, count) - initialize
206 * NCR5380_dma_read_setup(instance, dst, count) - initialize
207 * NCR5380_dma_residual(instance); - residual count
210 * NCR5380_pwrite(instance, src, count)
211 * NCR5380_pread(instance, dst, count);
213 * The generic driver is initialized by calling NCR5380_init(instance),
214 * after setting the appropriate host specific fields and ID. If the
215 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
216 * possible) function may be used.
219 static int do_abort(struct Scsi_Host *);
220 static void do_reset(struct Scsi_Host *);
223 * initialize_SCp - init the scsi pointer field
224 * @cmd: command block to set up
226 * Set up the internal fields in the SCSI command.
229 static inline void initialize_SCp(struct scsi_cmnd *cmd)
232 * Initialize the Scsi Pointer field so that all of the commands in the
233 * various queues are valid.
236 if (scsi_bufflen(cmd)) {
237 cmd->SCp.buffer = scsi_sglist(cmd);
238 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
239 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
240 cmd->SCp.this_residual = cmd->SCp.buffer->length;
242 cmd->SCp.buffer = NULL;
243 cmd->SCp.buffers_residual = 0;
245 cmd->SCp.this_residual = 0;
249 cmd->SCp.Message = 0;
253 * NCR5380_poll_politely2 - wait for two chip register values
254 * @instance: controller to poll
255 * @reg1: 5380 register to poll
256 * @bit1: Bitmask to check
257 * @val1: Expected value
258 * @reg2: Second 5380 register to poll
259 * @bit2: Second bitmask to check
260 * @val2: Second expected value
261 * @wait: Time-out in jiffies
263 * Polls the chip in a reasonably efficient manner waiting for an
264 * event to occur. After a short quick poll we begin to yield the CPU
265 * (if possible). In irq contexts the time-out is arbitrarily limited.
266 * Callers may hold locks as long as they are held in irq mode.
268 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
271 static int NCR5380_poll_politely2(struct Scsi_Host *instance,
272 int reg1, int bit1, int val1,
273 int reg2, int bit2, int val2, int wait)
275 struct NCR5380_hostdata *hostdata = shost_priv(instance);
276 unsigned long deadline = jiffies + wait;
279 /* Busy-wait for up to 10 ms */
280 n = min(10000U, jiffies_to_usecs(wait));
281 n *= hostdata->accesses_per_ms;
284 if ((NCR5380_read(reg1) & bit1) == val1)
286 if ((NCR5380_read(reg2) & bit2) == val2)
291 if (irqs_disabled() || in_interrupt())
294 /* Repeatedly sleep for 1 ms until deadline */
295 while (time_is_after_jiffies(deadline)) {
296 schedule_timeout_uninterruptible(1);
297 if ((NCR5380_read(reg1) & bit1) == val1)
299 if ((NCR5380_read(reg2) & bit2) == val2)
306 static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
307 int reg, int bit, int val, int wait)
309 return NCR5380_poll_politely2(instance, reg, bit, val,
310 reg, bit, val, wait);
316 } phases[] __maybe_unused = {
317 {PHASE_DATAOUT, "DATAOUT"},
318 {PHASE_DATAIN, "DATAIN"},
319 {PHASE_CMDOUT, "CMDOUT"},
320 {PHASE_STATIN, "STATIN"},
321 {PHASE_MSGOUT, "MSGOUT"},
322 {PHASE_MSGIN, "MSGIN"},
323 {PHASE_UNKNOWN, "UNKNOWN"}
347 {ICR_ASSERT_RST, "ASSERT RST"},
348 {ICR_ASSERT_ACK, "ASSERT ACK"},
349 {ICR_ASSERT_BSY, "ASSERT BSY"},
350 {ICR_ASSERT_SEL, "ASSERT SEL"},
351 {ICR_ASSERT_ATN, "ASSERT ATN"},
352 {ICR_ASSERT_DATA, "ASSERT DATA"},
356 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
357 {MR_TARGET, "MODE TARGET"},
358 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
359 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
360 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
361 {MR_DMA_MODE, "MODE DMA"},
362 {MR_ARBITRATE, "MODE ARBITRATION"},
367 * NCR5380_print - print scsi bus signals
368 * @instance: adapter state to dump
370 * Print the SCSI bus signals for debugging purposes
372 * Locks: caller holds hostdata lock (not essential)
375 static void NCR5380_print(struct Scsi_Host *instance)
377 unsigned char status, data, basr, mr, icr, i;
379 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
380 status = NCR5380_read(STATUS_REG);
381 mr = NCR5380_read(MODE_REG);
382 icr = NCR5380_read(INITIATOR_COMMAND_REG);
383 basr = NCR5380_read(BUS_AND_STATUS_REG);
385 printk("STATUS_REG: %02x ", status);
386 for (i = 0; signals[i].mask; ++i)
387 if (status & signals[i].mask)
388 printk(",%s", signals[i].name);
389 printk("\nBASR: %02x ", basr);
390 for (i = 0; basrs[i].mask; ++i)
391 if (basr & basrs[i].mask)
392 printk(",%s", basrs[i].name);
393 printk("\nICR: %02x ", icr);
394 for (i = 0; icrs[i].mask; ++i)
395 if (icr & icrs[i].mask)
396 printk(",%s", icrs[i].name);
397 printk("\nMODE: %02x ", mr);
398 for (i = 0; mrs[i].mask; ++i)
399 if (mr & mrs[i].mask)
400 printk(",%s", mrs[i].name);
406 * NCR5380_print_phase - show SCSI phase
407 * @instance: adapter to dump
409 * Print the current SCSI phase for debugging purposes
414 static void NCR5380_print_phase(struct Scsi_Host *instance)
416 unsigned char status;
419 status = NCR5380_read(STATUS_REG);
420 if (!(status & SR_REQ))
421 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
423 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
424 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
430 static int probe_irq __initdata;
433 * probe_intr - helper for IRQ autoprobe
434 * @irq: interrupt number
438 * Set a flag to indicate the IRQ in question was received. This is
439 * used by the IRQ probe code.
442 static irqreturn_t __init probe_intr(int irq, void *dev_id)
449 * NCR5380_probe_irq - find the IRQ of an NCR5380
450 * @instance: NCR5380 controller
451 * @possible: bitmask of ISA IRQ lines
453 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
454 * and then looking to see what interrupt actually turned up.
456 * Locks: none, irqs must be enabled on entry
459 static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
462 struct NCR5380_hostdata *hostdata = shost_priv(instance);
463 unsigned long timeout;
464 int trying_irqs, i, mask;
466 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
467 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
470 timeout = jiffies + msecs_to_jiffies(250);
474 * A interrupt is triggered whenever BSY = false, SEL = true
475 * and a bit set in the SELECT_ENABLE_REG is asserted on the
478 * Note that the bus is only driven when the phase control signals
479 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
483 NCR5380_write(TARGET_COMMAND_REG, 0);
484 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
485 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
486 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
488 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
489 schedule_timeout_uninterruptible(1);
491 NCR5380_write(SELECT_ENABLE_REG, 0);
492 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
494 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
495 if (trying_irqs & mask)
502 * NCR58380_info - report driver and host information
503 * @instance: relevant scsi host instance
505 * For use as the host template info() handler.
510 static const char *NCR5380_info(struct Scsi_Host *instance)
512 struct NCR5380_hostdata *hostdata = shost_priv(instance);
514 return hostdata->info;
517 static void prepare_info(struct Scsi_Host *instance)
519 struct NCR5380_hostdata *hostdata = shost_priv(instance);
521 snprintf(hostdata->info, sizeof(hostdata->info),
522 "%s, io_port 0x%lx, n_io_port %d, "
523 "base 0x%lx, irq %d, "
524 "can_queue %d, cmd_per_lun %d, "
525 "sg_tablesize %d, this_id %d, "
528 instance->hostt->name, instance->io_port, instance->n_io_port,
529 instance->base, instance->irq,
530 instance->can_queue, instance->cmd_per_lun,
531 instance->sg_tablesize, instance->this_id,
532 hostdata->flags & FLAG_NO_DMA_FIXUP ? "NO_DMA_FIXUP " : "",
533 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
534 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
557 /******************************************/
559 * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
561 * *buffer: I/O buffer
562 * **start: if inout == FALSE pointer into buffer where user read should start
563 * offset: current offset
564 * length: length of buffer
565 * hostno: Scsi_Host host_no
566 * inout: TRUE - user is writing; FALSE - user is reading
568 * Return the number of bytes read from or written
571 static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
572 char *buffer, int length)
574 struct NCR5380_hostdata *hostdata = shost_priv(instance);
576 hostdata->spin_max_r = 0;
577 hostdata->spin_max_w = 0;
581 static int __maybe_unused NCR5380_show_info(struct seq_file *m,
582 struct Scsi_Host *instance)
584 struct NCR5380_hostdata *hostdata = shost_priv(instance);
586 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
587 hostdata->spin_max_w, hostdata->spin_max_r);
593 * NCR5380_init - initialise an NCR5380
594 * @instance: adapter to configure
595 * @flags: control flags
597 * Initializes *instance and corresponding 5380 chip,
598 * with flags OR'd into the initial flags value.
600 * Notes : I assume that the host, hostno, and id bits have been
601 * set correctly. I don't care about the irq and other fields.
603 * Returns 0 for success
605 * Locks: interrupts must be enabled when we are called
608 static int NCR5380_init(struct Scsi_Host *instance, int flags)
610 struct NCR5380_hostdata *hostdata = shost_priv(instance);
612 unsigned long deadline;
615 printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
617 hostdata->id_mask = 1 << instance->this_id;
618 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
619 if (i > hostdata->id_mask)
620 hostdata->id_higher_mask |= i;
621 for (i = 0; i < 8; ++i)
622 hostdata->busy[i] = 0;
624 hostdata->dmalen = 0;
626 spin_lock_init(&hostdata->lock);
627 hostdata->connected = NULL;
628 hostdata->sensing = NULL;
629 INIT_LIST_HEAD(&hostdata->autosense);
630 INIT_LIST_HEAD(&hostdata->unissued);
631 INIT_LIST_HEAD(&hostdata->disconnected);
633 hostdata->flags = flags;
635 INIT_WORK(&hostdata->main_task, NCR5380_main);
636 hostdata->work_q = alloc_workqueue("ncr5380_%d",
637 WQ_UNBOUND | WQ_MEM_RECLAIM,
638 1, instance->host_no);
639 if (!hostdata->work_q)
642 hostdata->host = instance;
644 prepare_info(instance);
646 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
647 NCR5380_write(MODE_REG, MR_BASE);
648 NCR5380_write(TARGET_COMMAND_REG, 0);
649 NCR5380_write(SELECT_ENABLE_REG, 0);
651 /* Calibrate register polling loop */
653 deadline = jiffies + 1;
656 } while (time_is_after_jiffies(deadline));
657 deadline += msecs_to_jiffies(256);
659 NCR5380_read(STATUS_REG);
662 } while (time_is_after_jiffies(deadline));
663 hostdata->accesses_per_ms = i / 256;
669 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
670 * @instance: adapter to check
672 * If the system crashed, it may have crashed with a connected target and
673 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
674 * currently established nexus, which we know nothing about. Failing that
677 * Note that a bus reset will cause the chip to assert IRQ.
679 * Returns 0 if successful, otherwise -ENXIO.
682 static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
684 struct NCR5380_hostdata *hostdata = shost_priv(instance);
687 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
692 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
693 NCR5380_poll_politely(instance,
694 STATUS_REG, SR_BSY, 0, 5 * HZ);
697 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
701 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
703 /* Wait after a reset; the SCSI standard calls for
704 * 250ms, we wait 500ms to be on the safe side.
705 * But some Toshiba CD-ROMs need ten times that.
707 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
713 shost_printk(KERN_ERR, instance, "bus locked solid\n");
721 * NCR5380_exit - remove an NCR5380
722 * @instance: adapter to remove
725 static void NCR5380_exit(struct Scsi_Host *instance)
727 struct NCR5380_hostdata *hostdata = shost_priv(instance);
729 cancel_work_sync(&hostdata->main_task);
730 destroy_workqueue(hostdata->work_q);
734 * complete_cmd - finish processing a command and return it to the SCSI ML
735 * @instance: the host instance
736 * @cmd: command to complete
739 static void complete_cmd(struct Scsi_Host *instance,
740 struct scsi_cmnd *cmd)
742 struct NCR5380_hostdata *hostdata = shost_priv(instance);
744 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
746 if (hostdata->sensing == cmd) {
747 /* Autosense processing ends here */
748 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
749 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
750 set_host_byte(cmd, DID_ERROR);
752 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
753 hostdata->sensing = NULL;
756 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
762 * NCR5380_queue_command - queue a command
763 * @instance: the relevant SCSI adapter
766 * cmd is added to the per-instance issue queue, with minor
767 * twiddling done to the host specific fields of cmd. If the
768 * main coroutine is not running, it is restarted.
771 static int NCR5380_queue_command(struct Scsi_Host *instance,
772 struct scsi_cmnd *cmd)
774 struct NCR5380_hostdata *hostdata = shost_priv(instance);
775 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
778 #if (NDEBUG & NDEBUG_NO_WRITE)
779 switch (cmd->cmnd[0]) {
782 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
783 cmd->result = (DID_ERROR << 16);
787 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
791 spin_lock_irqsave(&hostdata->lock, flags);
794 * Insert the cmd into the issue queue. Note that REQUEST SENSE
795 * commands are added to the head of the queue since any command will
796 * clear the contingent allegiance condition that exists and the
797 * sense data is only guaranteed to be valid while the condition exists.
800 if (cmd->cmnd[0] == REQUEST_SENSE)
801 list_add(&ncmd->list, &hostdata->unissued);
803 list_add_tail(&ncmd->list, &hostdata->unissued);
805 spin_unlock_irqrestore(&hostdata->lock, flags);
807 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
808 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
810 /* Kick off command processing */
811 queue_work(hostdata->work_q, &hostdata->main_task);
816 * dequeue_next_cmd - dequeue a command for processing
817 * @instance: the scsi host instance
819 * Priority is given to commands on the autosense queue. These commands
820 * need autosense because of a CHECK CONDITION result.
822 * Returns a command pointer if a command is found for a target that is
823 * not already busy. Otherwise returns NULL.
826 static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
828 struct NCR5380_hostdata *hostdata = shost_priv(instance);
829 struct NCR5380_cmd *ncmd;
830 struct scsi_cmnd *cmd;
832 if (list_empty(&hostdata->autosense)) {
833 list_for_each_entry(ncmd, &hostdata->unissued, list) {
834 cmd = NCR5380_to_scmd(ncmd);
835 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
836 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
838 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
839 list_del(&ncmd->list);
840 dsprintk(NDEBUG_QUEUES, instance,
841 "dequeue: removed %p from issue queue\n", cmd);
846 /* Autosense processing begins here */
847 ncmd = list_first_entry(&hostdata->autosense,
848 struct NCR5380_cmd, list);
849 list_del(&ncmd->list);
850 cmd = NCR5380_to_scmd(ncmd);
851 dsprintk(NDEBUG_QUEUES, instance,
852 "dequeue: removed %p from autosense queue\n", cmd);
853 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
854 hostdata->sensing = cmd;
860 static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
862 struct NCR5380_hostdata *hostdata = shost_priv(instance);
863 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
865 if (hostdata->sensing) {
866 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
867 list_add(&ncmd->list, &hostdata->autosense);
868 hostdata->sensing = NULL;
870 list_add(&ncmd->list, &hostdata->unissued);
874 * NCR5380_main - NCR state machines
876 * NCR5380_main is a coroutine that runs as long as more work can
877 * be done on the NCR5380 host adapters in a system. Both
878 * NCR5380_queue_command() and NCR5380_intr() will try to start it
879 * in case it is not running.
881 * Locks: called as its own thread with no locks held. Takes the
882 * host lock and called routines may take the isa dma lock.
885 static void NCR5380_main(struct work_struct *work)
887 struct NCR5380_hostdata *hostdata =
888 container_of(work, struct NCR5380_hostdata, main_task);
889 struct Scsi_Host *instance = hostdata->host;
890 struct scsi_cmnd *cmd;
893 spin_lock_irq(&hostdata->lock);
897 while (!hostdata->connected &&
898 (cmd = dequeue_next_cmd(instance))) {
900 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
903 * Attempt to establish an I_T_L nexus here.
904 * On success, instance->hostdata->connected is set.
905 * On failure, we must add the command back to the
906 * issue queue so we can keep trying.
909 * REQUEST SENSE commands are issued without tagged
910 * queueing, even on SCSI-II devices because the
911 * contingent allegiance condition exists for the
915 cmd = NCR5380_select(instance, cmd);
917 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
919 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
920 "main: select failed, returning %p to queue\n", cmd);
921 requeue_cmd(instance, cmd);
924 if (hostdata->connected
929 dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no);
930 NCR5380_information_transfer(instance);
931 dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no);
935 spin_unlock_irq(&hostdata->lock);
938 #ifndef DONT_USE_INTR
941 * NCR5380_intr - generic NCR5380 irq handler
942 * @irq: interrupt number
943 * @dev_id: device info
945 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
946 * from the disconnected queue, and restarting NCR5380_main()
949 * The chip can assert IRQ in any of six different conditions. The IRQ flag
950 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
951 * Three of these six conditions are latched in the Bus and Status Register:
952 * - End of DMA (cleared by ending DMA Mode)
953 * - Parity error (cleared by reading RPIR)
954 * - Loss of BSY (cleared by reading RPIR)
955 * Two conditions have flag bits that are not latched:
956 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
957 * - Bus reset (non-maskable)
958 * The remaining condition has no flag bit at all:
959 * - Selection/reselection
961 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
962 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
963 * claimed that "the design of the [DP8490] interrupt logic ensures
964 * interrupts will not be lost (they can be on the DP5380)."
965 * The L5380/53C80 datasheet from LOGIC Devices has more details.
967 * Checking for bus reset by reading RST is futile because of interrupt
968 * latency, but a bus reset will reset chip logic. Checking for parity error
969 * is unnecessary because that interrupt is never enabled. A Loss of BSY
970 * condition will clear DMA Mode. We can tell when this occurs because the
971 * the Busy Monitor interrupt is enabled together with DMA Mode.
974 static irqreturn_t NCR5380_intr(int irq, void *dev_id)
976 struct Scsi_Host *instance = dev_id;
977 struct NCR5380_hostdata *hostdata = shost_priv(instance);
982 spin_lock_irqsave(&hostdata->lock, flags);
984 basr = NCR5380_read(BUS_AND_STATUS_REG);
985 if (basr & BASR_IRQ) {
986 unsigned char mr = NCR5380_read(MODE_REG);
987 unsigned char sr = NCR5380_read(STATUS_REG);
989 dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
990 instance->host_no, irq, basr, sr, mr);
992 #if defined(REAL_DMA)
993 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
994 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
995 * We ack IRQ after clearing Mode Register. Workarounds
996 * for End of DMA errata need to happen in DMA Mode.
999 dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", intance->host_no);
1003 if (!hostdata->connected)
1004 panic("scsi%d : DMA interrupt with no connected cmd\n",
1007 transferred = hostdata->dmalen - NCR5380_dma_residual(instance);
1008 hostdata->connected->SCp.this_residual -= transferred;
1009 hostdata->connected->SCp.ptr += transferred;
1010 hostdata->dmalen = 0;
1012 /* FIXME: we need to poll briefly then defer a workqueue task ! */
1013 NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2 * HZ);
1015 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1016 NCR5380_write(MODE_REG, MR_BASE);
1017 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1019 #endif /* REAL_DMA */
1020 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1021 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1022 /* Probably reselected */
1023 NCR5380_write(SELECT_ENABLE_REG, 0);
1024 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1026 dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n",
1029 if (!hostdata->connected) {
1030 NCR5380_reselect(instance);
1031 queue_work(hostdata->work_q, &hostdata->main_task);
1033 if (!hostdata->connected)
1034 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1036 /* Probably Bus Reset */
1037 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1039 dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", instance->host_no);
1043 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
1046 spin_unlock_irqrestore(&hostdata->lock, flags);
1048 return IRQ_RETVAL(handled);
1054 * Function : int NCR5380_select(struct Scsi_Host *instance,
1055 * struct scsi_cmnd *cmd)
1057 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1058 * including ARBITRATION, SELECTION, and initial message out for
1059 * IDENTIFY and queue messages.
1061 * Inputs : instance - instantiation of the 5380 driver on which this
1062 * target lives, cmd - SCSI command to execute.
1064 * Returns cmd if selection failed but should be retried,
1065 * NULL if selection failed and should not be retried, or
1066 * NULL if selection succeeded (hostdata->connected == cmd).
1069 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1070 * with registers as they should have been on entry - ie
1071 * SELECT_ENABLE will be set appropriately, the NCR5380
1072 * will cease to drive any SCSI bus signals.
1074 * If successful : I_T_L or I_T_L_Q nexus will be established,
1075 * instance->connected will be set to cmd.
1076 * SELECT interrupt will be disabled.
1078 * If failed (no target) : cmd->scsi_done() will be called, and the
1079 * cmd->result host byte set to DID_BAD_TARGET.
1081 * Locks: caller holds hostdata lock in IRQ mode
1084 static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
1085 struct scsi_cmnd *cmd)
1087 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1088 unsigned char tmp[3], phase;
1089 unsigned char *data;
1093 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
1094 dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id);
1097 * Arbitration and selection phases are slow and involve dropping the
1098 * lock, so we have to watch out for EH. An exception handler may
1099 * change 'selecting' to NULL. This function will then return NULL
1100 * so that the caller will forget about 'cmd'. (During information
1101 * transfer phases, EH may change 'connected' to NULL.)
1103 hostdata->selecting = cmd;
1106 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1107 * data bus during SELECTION.
1110 NCR5380_write(TARGET_COMMAND_REG, 0);
1113 * Start arbitration.
1116 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1117 NCR5380_write(MODE_REG, MR_ARBITRATE);
1119 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1120 * Bus Free Delay, arbitration will begin.
1123 spin_unlock_irq(&hostdata->lock);
1124 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1125 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1126 ICR_ARBITRATION_PROGRESS, HZ);
1127 spin_lock_irq(&hostdata->lock);
1128 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1129 /* Reselection interrupt */
1133 NCR5380_write(MODE_REG, MR_BASE);
1134 shost_printk(KERN_ERR, instance,
1135 "select: arbitration timeout\n");
1138 spin_unlock_irq(&hostdata->lock);
1140 /* The SCSI-2 arbitration delay is 2.4 us */
1143 /* Check for lost arbitration */
1144 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) || (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1145 NCR5380_write(MODE_REG, MR_BASE);
1146 dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no);
1147 spin_lock_irq(&hostdata->lock);
1151 /* After/during arbitration, BSY should be asserted.
1152 * IBM DPES-31080 Version S31Q works now
1153 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1155 NCR5380_write(INITIATOR_COMMAND_REG,
1156 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
1159 * Again, bus clear + bus settle time is 1.2us, however, this is
1160 * a minimum so we'll udelay ceil(1.2)
1163 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1168 spin_lock_irq(&hostdata->lock);
1170 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1171 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
1174 if (!hostdata->selecting) {
1175 NCR5380_write(MODE_REG, MR_BASE);
1176 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1180 dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no);
1183 * Now that we have won arbitration, start Selection process, asserting
1184 * the host and target ID's on the SCSI bus.
1187 NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
1190 * Raise ATN while SEL is true before BSY goes false from arbitration,
1191 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1192 * phase immediately after selection.
1195 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1196 NCR5380_write(MODE_REG, MR_BASE);
1199 * Reselect interrupts must be turned off prior to the dropping of BSY,
1200 * otherwise we will trigger an interrupt.
1202 NCR5380_write(SELECT_ENABLE_REG, 0);
1204 spin_unlock_irq(&hostdata->lock);
1207 * The initiator shall then wait at least two deskew delays and release
1210 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1213 NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
1216 * Something weird happens when we cease to drive BSY - looks
1217 * like the board/chip is letting us do another read before the
1218 * appropriate propagation delay has expired, and we're confusing
1219 * a BSY signal from ourselves as the target's response to SELECTION.
1221 * A small delay (the 'C++' frontend breaks the pipeline with an
1222 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1223 * tighter 'C' code breaks and requires this) solves the problem -
1224 * the 1 us delay is arbitrary, and only used because this delay will
1225 * be the same on other platforms and since it works here, it should
1228 * wingel suggests that this could be due to failing to wait
1234 dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd));
1237 * The SCSI specification calls for a 250 ms timeout for the actual
1241 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1242 msecs_to_jiffies(250));
1244 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1245 spin_lock_irq(&hostdata->lock);
1246 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1247 NCR5380_reselect(instance);
1248 if (!hostdata->connected)
1249 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1250 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
1255 spin_lock_irq(&hostdata->lock);
1256 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1257 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1258 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1259 if (hostdata->selecting) {
1260 cmd->result = DID_BAD_TARGET << 16;
1261 complete_cmd(instance, cmd);
1262 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1269 * No less than two deskew delays after the initiator detects the
1270 * BSY signal is true, it shall release the SEL signal and may
1271 * change the DATA BUS. -wingel
1276 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1279 * Since we followed the SCSI spec, and raised ATN while SEL
1280 * was true but before BSY was false during selection, the information
1281 * transfer phase should be a MESSAGE OUT phase so that we can send the
1284 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1285 * message (2 bytes) with a tag ID that we increment with every command
1286 * until it wraps back to 0.
1288 * XXX - it turns out that there are some broken SCSI-II devices,
1289 * which claim to support tagged queuing but fail when more than
1290 * some number of commands are issued at once.
1293 /* Wait for start of REQ/ACK handshake */
1295 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1296 spin_lock_irq(&hostdata->lock);
1298 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1299 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1300 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1303 if (!hostdata->selecting) {
1308 dprintk(NDEBUG_SELECTION, "scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id);
1309 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
1314 /* Send message(s) */
1316 phase = PHASE_MSGOUT;
1317 NCR5380_transfer_pio(instance, &phase, &len, &data);
1318 dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no);
1319 /* XXX need to handle errors here */
1321 hostdata->connected = cmd;
1322 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
1324 initialize_SCp(cmd);
1329 if (!hostdata->selecting)
1331 hostdata->selecting = NULL;
1336 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1337 * unsigned char *phase, int *count, unsigned char **data)
1339 * Purpose : transfers data in given phase using polled I/O
1341 * Inputs : instance - instance of driver, *phase - pointer to
1342 * what phase is expected, *count - pointer to number of
1343 * bytes to transfer, **data - pointer to data pointer.
1345 * Returns : -1 when different phase is entered without transferring
1346 * maximum number of bytes, 0 if all bytes or transferred or exit
1349 * Also, *phase, *count, *data are modified in place.
1351 * XXX Note : handling for bus free may be useful.
1355 * Note : this code is not as quick as it could be, however it
1356 * IS 100% reliable, and for the actual data transfer where speed
1357 * counts, we will always do a pseudo DMA or DMA transfer.
1360 static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1361 unsigned char p = *phase, tmp;
1363 unsigned char *d = *data;
1366 dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c);
1368 dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c);
1371 * The NCR5380 chip will only drive the SCSI bus when the
1372 * phase specified in the appropriate bits of the TARGET COMMAND
1373 * REGISTER match the STATUS REGISTER
1376 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1380 * Wait for assertion of REQ, after which the phase bits will be
1384 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1387 dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no);
1389 /* Check for phase mismatch */
1390 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
1391 dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no);
1392 NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
1395 /* Do actual transfer from SCSI bus to / from memory */
1397 NCR5380_write(OUTPUT_DATA_REG, *d);
1399 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1404 * The SCSI standard suggests that in MSGOUT phase, the initiator
1405 * should drop ATN on the last byte of the message phase
1406 * after REQ has been asserted for the handshake but before
1407 * the initiator raises ACK.
1411 if (!((p & SR_MSG) && c > 1)) {
1412 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1413 NCR5380_dprint(NDEBUG_PIO, instance);
1414 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1416 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1417 NCR5380_dprint(NDEBUG_PIO, instance);
1418 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1421 NCR5380_dprint(NDEBUG_PIO, instance);
1422 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1425 if (NCR5380_poll_politely(instance,
1426 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1429 dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no);
1432 * We have several special cases to consider during REQ/ACK handshaking :
1433 * 1. We were in MSGOUT phase, and we are on the last byte of the
1434 * message. ATN must be dropped as ACK is dropped.
1436 * 2. We are in a MSGIN phase, and we are on the last byte of the
1437 * message. We must exit with ACK asserted, so that the calling
1438 * code may raise ATN before dropping ACK to reject the message.
1440 * 3. ACK and ATN are clear and the target may proceed as normal.
1442 if (!(p == PHASE_MSGIN && c == 1)) {
1443 if (p == PHASE_MSGOUT && c > 1)
1444 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1446 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1450 dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c);
1454 tmp = NCR5380_read(STATUS_REG);
1455 /* The phase read from the bus is valid if either REQ is (already)
1456 * asserted or if ACK hasn't been released yet. The latter applies if
1457 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1459 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
1460 *phase = tmp & PHASE_MASK;
1462 *phase = PHASE_UNKNOWN;
1464 if (!c || (*phase == p))
1471 * do_reset - issue a reset command
1472 * @instance: adapter to reset
1474 * Issue a reset sequence to the NCR5380 and try and get the bus
1475 * back into sane shape.
1477 * This clears the reset interrupt flag because there may be no handler for
1478 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1479 * been installed. And when in EH we may have released the ST DMA interrupt.
1482 static void do_reset(struct Scsi_Host *instance)
1484 unsigned long flags;
1486 local_irq_save(flags);
1487 NCR5380_write(TARGET_COMMAND_REG,
1488 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1489 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1491 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1492 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1493 local_irq_restore(flags);
1497 * do_abort - abort the currently established nexus by going to
1498 * MESSAGE OUT phase and sending an ABORT message.
1499 * @instance: relevant scsi host instance
1501 * Returns 0 on success, -1 on failure.
1504 static int do_abort(struct Scsi_Host *instance)
1506 unsigned char *msgptr, phase, tmp;
1510 /* Request message out phase */
1511 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1514 * Wait for the target to indicate a valid phase by asserting
1515 * REQ. Once this happens, we'll have either a MSGOUT phase
1516 * and can immediately send the ABORT message, or we'll have some
1517 * other phase and will have to source/sink data.
1519 * We really don't care what value was on the bus or what value
1520 * the target sees, so we just handshake.
1523 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1527 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
1529 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1531 if (tmp != PHASE_MSGOUT) {
1532 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1533 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1536 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1541 phase = PHASE_MSGOUT;
1542 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
1545 * If we got here, and the command completed successfully,
1546 * we're about to go into bus free state.
1549 return len ? -1 : 0;
1552 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1556 #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
1558 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1559 * unsigned char *phase, int *count, unsigned char **data)
1561 * Purpose : transfers data in given phase using either real
1564 * Inputs : instance - instance of driver, *phase - pointer to
1565 * what phase is expected, *count - pointer to number of
1566 * bytes to transfer, **data - pointer to data pointer.
1568 * Returns : -1 when different phase is entered without transferring
1569 * maximum number of bytes, 0 if all bytes or transferred or exit
1572 * Also, *phase, *count, *data are modified in place.
1574 * Locks: io_request lock held by caller
1578 static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
1579 register int c = *count;
1580 register unsigned char p = *phase;
1581 register unsigned char *d = *data;
1584 #if defined(REAL_DMA_POLL)
1586 unsigned char saved_data = 0, overrun = 0, residue;
1589 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1591 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1595 #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
1597 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS))
1600 dprintk(NDEBUG_DMA, "scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
1601 hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
1604 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1607 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1608 MR_ENABLE_EOP_INTR);
1609 #elif defined(REAL_DMA_POLL)
1610 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
1613 * Note : on my sample board, watch-dog timeouts occurred when interrupts
1614 * were not disabled for the duration of a single DMA transfer, from
1615 * before the setting of DMA mode to after transfer of the last byte.
1618 if (hostdata->flags & FLAG_NO_DMA_FIXUP)
1619 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1620 MR_ENABLE_EOP_INTR);
1622 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
1623 #endif /* def REAL_DMA */
1625 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
1628 * On the PAS16 at least I/O recovery delays are not needed here.
1629 * Everyone else seems to want them.
1633 io_recovery_delay(1);
1634 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1636 io_recovery_delay(1);
1637 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1638 io_recovery_delay(1);
1639 NCR5380_write(START_DMA_SEND_REG, 0);
1640 io_recovery_delay(1);
1643 #if defined(REAL_DMA_POLL)
1645 tmp = NCR5380_read(BUS_AND_STATUS_REG);
1646 } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
1649 At this point, either we've completed DMA, or we have a phase mismatch,
1650 or we've unexpectedly lost BUSY (which is a real error).
1652 For write DMAs, we want to wait until the last byte has been
1653 transferred out over the bus before we turn off DMA mode. Alas, there
1654 seems to be no terribly good way of doing this on a 5380 under all
1655 conditions. For non-scatter-gather operations, we can wait until REQ
1656 and ACK both go false, or until a phase mismatch occurs. Gather-writes
1657 are nastier, since the device will be expecting more data than we
1658 are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1659 could test LAST BIT SENT to assure transfer (I imagine this is precisely
1660 why this signal was added to the newer chips) but on the older 538[01]
1661 this signal does not exist. The workaround for this lack is a watchdog;
1662 we bail out of the wait-loop after a modest amount of wait-time if
1663 the usual exit conditions are not met. Not a terribly clean or
1664 correct solution :-%
1666 Reads are equally tricky due to a nasty characteristic of the NCR5380.
1667 If the chip is in DMA mode for an READ, it will respond to a target's
1668 REQ by latching the SCSI data into the INPUT DATA register and asserting
1669 ACK, even if it has _already_ been notified by the DMA controller that
1670 the current DMA transfer has completed! If the NCR5380 is then taken
1671 out of DMA mode, this already-acknowledged byte is lost.
1673 This is not a problem for "one DMA transfer per command" reads, because
1674 the situation will never arise... either all of the data is DMA'ed
1675 properly, or the target switches to MESSAGE IN phase to signal a
1676 disconnection (either operation bringing the DMA to a clean halt).
1677 However, in order to handle scatter-reads, we must work around the
1678 problem. The chosen fix is to DMA N-2 bytes, then check for the
1679 condition before taking the NCR5380 out of DMA mode. One or two extra
1680 bytes are transferred via PIO as necessary to fill out the original
1685 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS)) {
1687 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1688 (BASR_PHASE_MATCH | BASR_ACK)) {
1689 saved_data = NCR5380_read(INPUT_DATA_REGISTER);
1695 while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
1696 if (!(tmp & BASR_PHASE_MATCH))
1703 dprintk(NDEBUG_DMA, "scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG));
1705 NCR5380_write(MODE_REG, MR_BASE);
1706 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1708 residue = NCR5380_dma_residual(instance);
1712 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1714 if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS) &&
1715 *phase == p && (p & SR_IO) && residue == 0) {
1717 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
1718 **data = saved_data;
1723 printk("No overrun??\n");
1726 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data);
1727 NCR5380_transfer_pio(instance, phase, &cnt, data);
1728 *count -= toPIO - cnt;
1731 dprintk(NDEBUG_DMA, "Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n", *data, *count, *(*data + *count - 1), *(*data + *count));
1734 #elif defined(REAL_DMA)
1736 #else /* defined(REAL_DMA_POLL) */
1738 foo = NCR5380_pread(instance, d,
1739 hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1);
1740 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
1742 * We can't disable DMA mode after successfully transferring
1743 * what we plan to be the last byte, since that would open up
1744 * a race condition where if the target asserted REQ before
1745 * we got the DMA mode reset, the NCR5380 would have latched
1746 * an additional byte into the INPUT DATA register and we'd
1749 * The workaround was to transfer one fewer bytes than we
1750 * intended to with the pseudo-DMA read function, wait for
1751 * the chip to latch the last byte, read it, and then disable
1754 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1755 * REQ is deasserted when ACK is asserted, and not reasserted
1756 * until ACK goes false. Since the NCR5380 won't lower ACK
1757 * until DACK is asserted, which won't happen unless we twiddle
1758 * the DMA port or we take the NCR5380 out of DMA mode, we
1759 * can guarantee that we won't handshake another extra
1763 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1764 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1766 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
1768 if (NCR5380_poll_politely(instance, STATUS_REG,
1769 SR_REQ, 0, HZ) < 0) {
1771 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1773 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
1776 foo = NCR5380_pwrite(instance, d, c);
1777 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
1779 * Wait for the last byte to be sent. If REQ is being asserted for
1780 * the byte we're interested, we'll ACK it and it will go false.
1782 if (NCR5380_poll_politely2(instance,
1783 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1784 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1786 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
1790 NCR5380_write(MODE_REG, MR_BASE);
1791 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1792 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1795 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
1797 #endif /* def REAL_DMA */
1799 #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
1802 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1804 * Purpose : run through the various SCSI phases and do as the target
1805 * directs us to. Operates on the currently connected command,
1806 * instance->connected.
1808 * Inputs : instance, instance for which we are doing commands
1810 * Side effects : SCSI things happen, the disconnected queue will be
1811 * modified if a command disconnects, *instance->connected will
1814 * XXX Note : we need to watch for bus free or a reset condition here
1815 * to recover from an unexpected bus free condition.
1817 * Locks: io_request_lock held by caller in IRQ mode
1820 static void NCR5380_information_transfer(struct Scsi_Host *instance) {
1821 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1822 unsigned char msgout = NOP;
1825 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1828 unsigned char *data;
1829 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
1830 struct scsi_cmnd *cmd;
1832 while ((cmd = hostdata->connected)) {
1833 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1835 tmp = NCR5380_read(STATUS_REG);
1836 /* We only have a valid SCSI phase when REQ is asserted */
1838 phase = (tmp & PHASE_MASK);
1839 if (phase != old_phase) {
1841 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1843 if (sink && (phase != PHASE_MSGOUT)) {
1844 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1846 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1847 while (NCR5380_read(STATUS_REG) & SR_REQ);
1848 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1854 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1855 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
1858 cmd->result = DID_ERROR << 16;
1859 complete_cmd(instance, cmd);
1864 * If there is no room left in the current buffer in the
1865 * scatter-gather list, move onto the next one.
1868 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1870 --cmd->SCp.buffers_residual;
1871 cmd->SCp.this_residual = cmd->SCp.buffer->length;
1872 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
1873 dprintk(NDEBUG_INFORMATION, "scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual);
1876 * The preferred transfer method is going to be
1877 * PSEUDO-DMA for systems that are strictly PIO,
1878 * since we can let the hardware do the handshaking.
1880 * For this to work, we need to know the transfersize
1881 * ahead of time, since the pseudo-DMA code will sit
1882 * in an unconditional loop.
1885 #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
1887 if (!cmd->device->borken &&
1888 !(hostdata->flags & FLAG_NO_PSEUDO_DMA))
1889 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
1893 if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
1895 * If the watchdog timer fires, all future accesses to this
1896 * device will use the polled-IO.
1898 scmd_printk(KERN_INFO, cmd,
1899 "switching to slow handshake\n");
1900 cmd->device->borken = 1;
1903 cmd->result = DID_ERROR << 16;
1904 complete_cmd(instance, cmd);
1905 /* XXX - need to source or sink data here, as appropriate */
1907 cmd->SCp.this_residual -= transfersize - len;
1909 #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
1911 spin_unlock_irq(&hostdata->lock);
1912 NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
1914 spin_lock_irq(&hostdata->lock);
1920 NCR5380_transfer_pio(instance, &phase, &len, &data);
1921 cmd->SCp.Message = tmp;
1925 case COMMAND_COMPLETE:
1926 /* Accept message by clearing ACK */
1928 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1929 dsprintk(NDEBUG_QUEUES, instance,
1930 "COMMAND COMPLETE %p target %d lun %llu\n",
1931 cmd, scmd_id(cmd), cmd->device->lun);
1933 hostdata->connected = NULL;
1935 cmd->result &= ~0xffff;
1936 cmd->result |= cmd->SCp.Status;
1937 cmd->result |= cmd->SCp.Message << 8;
1939 if (cmd->cmnd[0] == REQUEST_SENSE)
1940 complete_cmd(instance, cmd);
1942 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1943 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1944 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1946 list_add_tail(&ncmd->list,
1947 &hostdata->autosense);
1949 complete_cmd(instance, cmd);
1953 * Restore phase bits to 0 so an interrupted selection,
1954 * arbitration can resume.
1956 NCR5380_write(TARGET_COMMAND_REG, 0);
1958 /* Enable reselect interrupts */
1959 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1961 case MESSAGE_REJECT:
1962 /* Accept message by clearing ACK */
1963 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1964 switch (hostdata->last_message) {
1965 case HEAD_OF_QUEUE_TAG:
1966 case ORDERED_QUEUE_TAG:
1967 case SIMPLE_QUEUE_TAG:
1968 cmd->device->simple_tags = 0;
1969 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
1976 /* Accept message by clearing ACK */
1977 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1978 hostdata->connected = NULL;
1979 list_add(&ncmd->list, &hostdata->disconnected);
1980 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1981 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1982 cmd, scmd_id(cmd), cmd->device->lun);
1985 * Restore phase bits to 0 so an interrupted selection,
1986 * arbitration can resume.
1988 NCR5380_write(TARGET_COMMAND_REG, 0);
1990 /* Enable reselect interrupts */
1991 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1995 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1996 * operation, in violation of the SCSI spec so we can safely
1997 * ignore SAVE/RESTORE pointers calls.
1999 * Unfortunately, some disks violate the SCSI spec and
2000 * don't issue the required SAVE_POINTERS message before
2001 * disconnecting, and we have to break spec to remain
2005 case RESTORE_POINTERS:
2006 /* Accept message by clearing ACK */
2007 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2009 case EXTENDED_MESSAGE:
2011 * Extended messages are sent in the following format :
2013 * 0 EXTENDED_MESSAGE == 1
2014 * 1 length (includes one byte for code, doesn't
2015 * include first two bytes)
2017 * 3..length+1 arguments
2019 * Start the extended message buffer with the EXTENDED_MESSAGE
2020 * byte, since spi_print_msg() wants the whole thing.
2022 extended_msg[0] = EXTENDED_MESSAGE;
2023 /* Accept first byte by clearing ACK */
2024 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2026 spin_unlock_irq(&hostdata->lock);
2028 dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no);
2031 data = extended_msg + 1;
2032 phase = PHASE_MSGIN;
2033 NCR5380_transfer_pio(instance, &phase, &len, &data);
2035 dprintk(NDEBUG_EXTENDED, "scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]);
2037 if (!len && extended_msg[1] > 0 &&
2038 extended_msg[1] <= sizeof(extended_msg) - 2) {
2039 /* Accept third byte by clearing ACK */
2040 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2041 len = extended_msg[1] - 1;
2042 data = extended_msg + 3;
2043 phase = PHASE_MSGIN;
2045 NCR5380_transfer_pio(instance, &phase, &len, &data);
2046 dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len);
2048 switch (extended_msg[2]) {
2051 case EXTENDED_MODIFY_DATA_POINTER:
2052 case EXTENDED_EXTENDED_IDENTIFY:
2056 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
2059 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
2060 extended_msg[2], extended_msg[1]);
2064 spin_lock_irq(&hostdata->lock);
2065 if (!hostdata->connected)
2068 /* Fall through to reject message */
2071 * If we get something weird that we aren't expecting,
2076 shost_printk(KERN_ERR, instance, "rejecting message ");
2077 spi_print_msg(extended_msg);
2079 } else if (tmp != EXTENDED_MESSAGE)
2080 scmd_printk(KERN_INFO, cmd,
2081 "rejecting unknown message %02x\n",tmp);
2083 scmd_printk(KERN_INFO, cmd,
2084 "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
2086 msgout = MESSAGE_REJECT;
2087 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2089 } /* switch (tmp) */
2094 hostdata->last_message = msgout;
2095 NCR5380_transfer_pio(instance, &phase, &len, &data);
2096 if (msgout == ABORT) {
2097 hostdata->connected = NULL;
2098 cmd->result = DID_ERROR << 16;
2099 complete_cmd(instance, cmd);
2100 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2109 * XXX for performance reasons, on machines with a
2110 * PSEUDO-DMA architecture we should probably
2111 * use the dma transfer function.
2113 NCR5380_transfer_pio(instance, &phase, &len, &data);
2118 NCR5380_transfer_pio(instance, &phase, &len, &data);
2119 cmd->SCp.Status = tmp;
2122 shost_printk(KERN_ERR, instance, "unknown phase\n");
2123 NCR5380_dprint(NDEBUG_ANY, instance);
2124 } /* switch(phase) */
2126 spin_unlock_irq(&hostdata->lock);
2127 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2128 spin_lock_irq(&hostdata->lock);
2134 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2136 * Purpose : does reselection, initializing the instance->connected
2137 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2138 * nexus has been reestablished,
2140 * Inputs : instance - this instance of the NCR5380.
2142 * Locks: io_request_lock held by caller if IRQ driven
2145 static void NCR5380_reselect(struct Scsi_Host *instance) {
2146 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2147 unsigned char target_mask;
2148 unsigned char lun, phase;
2150 unsigned char msg[3];
2151 unsigned char *data;
2152 struct NCR5380_cmd *ncmd;
2153 struct scsi_cmnd *tmp;
2156 * Disable arbitration, etc. since the host adapter obviously
2157 * lost, and tell an interrupted NCR5380_select() to restart.
2160 NCR5380_write(MODE_REG, MR_BASE);
2162 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2163 dprintk(NDEBUG_RESELECTION, "scsi%d : reselect\n", instance->host_no);
2166 * At this point, we have detected that our SCSI ID is on the bus,
2167 * SEL is true and BSY was false for at least one bus settle delay
2170 * We must assert BSY ourselves, until the target drops the SEL
2174 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2175 if (NCR5380_poll_politely(instance,
2176 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2177 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2180 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2183 * Wait for target to go into MSGIN.
2186 if (NCR5380_poll_politely(instance,
2187 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2194 phase = PHASE_MSGIN;
2195 NCR5380_transfer_pio(instance, &phase, &len, &data);
2202 if (!(msg[0] & 0x80)) {
2203 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
2209 lun = msg[0] & 0x07;
2212 * We need to add code for SCSI-II to track which devices have
2213 * I_T_L_Q nexuses established, and which have simple I_T_L
2214 * nexuses so we can chose to do additional data transfer.
2218 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2219 * just reestablished, and remove it from the disconnected queue.
2223 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2224 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2226 if (target_mask == (1 << scmd_id(cmd)) &&
2227 lun == (u8)cmd->device->lun) {
2228 list_del(&ncmd->list);
2235 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2236 "reselect: removed %p from disconnected queue\n", tmp);
2238 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2241 * Since we have an established nexus that we can't do anything with,
2248 /* Accept message by clearing ACK */
2249 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2251 hostdata->connected = tmp;
2252 dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n",
2253 instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag);
2257 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
2259 * Purpose : called by interrupt handler when DMA finishes or a phase
2260 * mismatch occurs (which would finish the DMA transfer).
2262 * Inputs : instance - this instance of the NCR5380.
2264 * Returns : pointer to the scsi_cmnd structure for which the I_T_L
2265 * nexus has been reestablished, on failure NULL is returned.
2269 static void NCR5380_dma_complete(NCR5380_instance * instance) {
2270 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2274 * XXX this might not be right.
2276 * Wait for final byte to transfer, ie wait for ACK to go false.
2278 * We should use the Last Byte Sent bit, unfortunately this is
2279 * not available on the 5380/5381 (only the various CMOS chips)
2281 * FIXME: timeout, and need to handle long timeout/irq case
2284 NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
2286 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2289 * The only places we should see a phase mismatch and have to send
2290 * data from the same set of pointers will be the data transfer
2291 * phases. So, residual, requested length are only important here.
2294 if (!(hostdata->connected->SCp.phase & SR_CD)) {
2295 transferred = instance->dmalen - NCR5380_dma_residual();
2296 hostdata->connected->SCp.this_residual -= transferred;
2297 hostdata->connected->SCp.ptr += transferred;
2300 #endif /* def REAL_DMA */
2303 * list_find_cmd - test for presence of a command in a linked list
2304 * @haystack: list of commands
2305 * @needle: command to search for
2308 static bool list_find_cmd(struct list_head *haystack,
2309 struct scsi_cmnd *needle)
2311 struct NCR5380_cmd *ncmd;
2313 list_for_each_entry(ncmd, haystack, list)
2314 if (NCR5380_to_scmd(ncmd) == needle)
2320 * list_remove_cmd - remove a command from linked list
2321 * @haystack: list of commands
2322 * @needle: command to remove
2325 static bool list_del_cmd(struct list_head *haystack,
2326 struct scsi_cmnd *needle)
2328 if (list_find_cmd(haystack, needle)) {
2329 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2331 list_del(&ncmd->list);
2338 * NCR5380_abort - scsi host eh_abort_handler() method
2339 * @cmd: the command to be aborted
2341 * Try to abort a given command by removing it from queues and/or sending
2342 * the target an abort message. This may not succeed in causing a target
2343 * to abort the command. Nonetheless, the low-level driver must forget about
2344 * the command because the mid-layer reclaims it and it may be re-issued.
2346 * The normal path taken by a command is as follows. For EH we trace this
2347 * same path to locate and abort the command.
2349 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2350 * [disconnected -> connected ->]...
2351 * [autosense -> connected ->] done
2353 * If cmd is unissued then just remove it.
2354 * If cmd is disconnected, try to select the target.
2355 * If cmd is connected, try to send an abort message.
2356 * If cmd is waiting for autosense, give it a chance to complete but check
2357 * that it isn't left connected.
2358 * If cmd was not found at all then presumably it has already been completed,
2359 * in which case return SUCCESS to try to avoid further EH measures.
2360 * If the command has not completed yet, we must not fail to find it.
2363 static int NCR5380_abort(struct scsi_cmnd *cmd)
2365 struct Scsi_Host *instance = cmd->device->host;
2366 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2367 unsigned long flags;
2368 int result = SUCCESS;
2370 spin_lock_irqsave(&hostdata->lock, flags);
2372 #if (NDEBUG & NDEBUG_ANY)
2373 scmd_printk(KERN_INFO, cmd, __func__);
2375 NCR5380_dprint(NDEBUG_ANY, instance);
2376 NCR5380_dprint_phase(NDEBUG_ANY, instance);
2378 if (list_del_cmd(&hostdata->unissued, cmd)) {
2379 dsprintk(NDEBUG_ABORT, instance,
2380 "abort: removed %p from issue queue\n", cmd);
2381 cmd->result = DID_ABORT << 16;
2382 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
2385 if (hostdata->selecting == cmd) {
2386 dsprintk(NDEBUG_ABORT, instance,
2387 "abort: cmd %p == selecting\n", cmd);
2388 hostdata->selecting = NULL;
2389 cmd->result = DID_ABORT << 16;
2390 complete_cmd(instance, cmd);
2394 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2395 dsprintk(NDEBUG_ABORT, instance,
2396 "abort: removed %p from disconnected list\n", cmd);
2397 cmd->result = DID_ERROR << 16;
2398 if (!hostdata->connected)
2399 NCR5380_select(instance, cmd);
2400 if (hostdata->connected != cmd) {
2401 complete_cmd(instance, cmd);
2407 if (hostdata->connected == cmd) {
2408 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2409 hostdata->connected = NULL;
2410 if (do_abort(instance)) {
2411 set_host_byte(cmd, DID_ERROR);
2412 complete_cmd(instance, cmd);
2416 set_host_byte(cmd, DID_ABORT);
2418 hostdata->dma_len = 0;
2420 if (cmd->cmnd[0] == REQUEST_SENSE)
2421 complete_cmd(instance, cmd);
2423 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
2425 /* Perform autosense for this command */
2426 list_add(&ncmd->list, &hostdata->autosense);
2430 if (list_find_cmd(&hostdata->autosense, cmd)) {
2431 dsprintk(NDEBUG_ABORT, instance,
2432 "abort: found %p on sense queue\n", cmd);
2433 spin_unlock_irqrestore(&hostdata->lock, flags);
2434 queue_work(hostdata->work_q, &hostdata->main_task);
2436 spin_lock_irqsave(&hostdata->lock, flags);
2437 if (list_del_cmd(&hostdata->autosense, cmd)) {
2438 dsprintk(NDEBUG_ABORT, instance,
2439 "abort: removed %p from sense queue\n", cmd);
2440 set_host_byte(cmd, DID_ABORT);
2441 complete_cmd(instance, cmd);
2446 if (hostdata->connected == cmd) {
2447 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2448 hostdata->connected = NULL;
2449 if (do_abort(instance)) {
2450 set_host_byte(cmd, DID_ERROR);
2451 complete_cmd(instance, cmd);
2455 set_host_byte(cmd, DID_ABORT);
2457 hostdata->dma_len = 0;
2459 complete_cmd(instance, cmd);
2463 if (result == FAILED)
2464 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2466 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2468 queue_work(hostdata->work_q, &hostdata->main_task);
2469 spin_unlock_irqrestore(&hostdata->lock, flags);
2476 * NCR5380_bus_reset - reset the SCSI bus
2477 * @cmd: SCSI command undergoing EH
2482 static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
2484 struct Scsi_Host *instance = cmd->device->host;
2485 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2487 unsigned long flags;
2488 struct NCR5380_cmd *ncmd;
2490 spin_lock_irqsave(&hostdata->lock, flags);
2492 #if (NDEBUG & NDEBUG_ANY)
2493 scmd_printk(KERN_INFO, cmd, __func__);
2495 NCR5380_dprint(NDEBUG_ANY, instance);
2496 NCR5380_dprint_phase(NDEBUG_ANY, instance);
2500 /* reset NCR registers */
2501 NCR5380_write(MODE_REG, MR_BASE);
2502 NCR5380_write(TARGET_COMMAND_REG, 0);
2503 NCR5380_write(SELECT_ENABLE_REG, 0);
2505 /* After the reset, there are no more connected or disconnected commands
2506 * and no busy units; so clear the low-level status here to avoid
2507 * conflicts when the mid-level code tries to wake up the affected
2511 hostdata->selecting = NULL;
2513 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2514 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2516 set_host_byte(cmd, DID_RESET);
2517 cmd->scsi_done(cmd);
2520 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2521 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2523 set_host_byte(cmd, DID_RESET);
2524 cmd->scsi_done(cmd);
2527 if (hostdata->connected) {
2528 set_host_byte(hostdata->connected, DID_RESET);
2529 complete_cmd(instance, hostdata->connected);
2530 hostdata->connected = NULL;
2533 if (hostdata->sensing) {
2534 set_host_byte(hostdata->connected, DID_RESET);
2535 complete_cmd(instance, hostdata->sensing);
2536 hostdata->sensing = NULL;
2539 for (i = 0; i < 8; ++i)
2540 hostdata->busy[i] = 0;
2542 hostdata->dma_len = 0;
2545 queue_work(hostdata->work_q, &hostdata->main_task);
2546 spin_unlock_irqrestore(&hostdata->lock, flags);