1 // SPDX-License-Identifier: GPL-2.0
3 * NCR 5380 generic driver routines. These should make it *trivial*
4 * to implement 5380 SCSI drivers under Linux with a non-trantor
7 * Note that these routines also work with NR53c400 family chips.
9 * Copyright 1993, Drew Eckhardt
11 * (Unix and Linux consulting and custom programming)
15 * For more information, please consult
18 * SCSI Protocol Controller
21 * NCR Microelectronics
22 * 1635 Aeroplaza Drive
23 * Colorado Springs, CO 80916
29 * With contributions from Ray Van Tassle, Ingmar Baumgart,
30 * Ronald van Cuijlenborg, Alan Cox and others.
33 /* Ported to Atari by Roman Hodek and others. */
35 /* Adapted for the Sun 3 by Sam Creasey. */
40 * This is a generic 5380 driver. To use it on a different platform,
41 * one simply writes appropriate system specific macros (ie, data
42 * transfer - some PC's will use the I/O bus, 68K's must use
43 * memory mapped) and drops this file in their 'C' wrapper.
45 * As far as command queueing, two queues are maintained for
46 * each 5380 in the system - commands that haven't been issued yet,
47 * and commands that are currently executing. This means that an
48 * unlimited number of commands may be queued, letting
49 * more commands propagate from the higher driver levels giving higher
50 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
51 * allowing multiple commands to propagate all the way to a SCSI-II device
52 * while a command is already executing.
55 * Issues specific to the NCR5380 :
57 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
58 * piece of hardware that requires you to sit in a loop polling for
59 * the REQ signal as long as you are connected. Some devices are
60 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
61 * while doing long seek operations. [...] These
62 * broken devices are the exception rather than the rule and I'd rather
63 * spend my time optimizing for the normal case.
67 * At the heart of the design is a coroutine, NCR5380_main,
68 * which is started from a workqueue for each NCR5380 host in the
69 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
70 * removing the commands from the issue queue and calling
71 * NCR5380_select() if a nexus is not established.
73 * Once a nexus is established, the NCR5380_information_transfer()
74 * phase goes through the various phases as instructed by the target.
75 * if the target goes into MSG IN and sends a DISCONNECT message,
76 * the command structure is placed into the per instance disconnected
77 * queue, and NCR5380_main tries to find more work. If the target is
78 * idle for too long, the system will try to sleep.
80 * If a command has disconnected, eventually an interrupt will trigger,
81 * calling NCR5380_intr() which will in turn call NCR5380_reselect
82 * to reestablish a nexus. This will run main if necessary.
84 * On command termination, the done function will be called as
87 * SCSI pointers are maintained in the SCp field of SCSI command
88 * structures, being initialized after the command is connected
89 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
90 * Note that in violation of the standard, an implicit SAVE POINTERS operation
91 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
96 * This file a skeleton Linux SCSI driver for the NCR 5380 series
97 * of chips. To use it, you write an architecture specific functions
98 * and macros and include this file in your driver.
100 * These macros MUST be defined :
102 * NCR5380_read(register) - read from the specified register
104 * NCR5380_write(register, value) - write to the specific register
106 * NCR5380_implementation_fields - additional fields needed for this
107 * specific implementation of the NCR5380
109 * Either real DMA *or* pseudo DMA may be implemented
111 * NCR5380_dma_xfer_len - determine size of DMA/PDMA transfer
112 * NCR5380_dma_send_setup - execute DMA/PDMA from memory to 5380
113 * NCR5380_dma_recv_setup - execute DMA/PDMA from 5380 to memory
114 * NCR5380_dma_residual - residual byte count
116 * The generic driver is initialized by calling NCR5380_init(instance),
117 * after setting the appropriate host specific fields and ID.
120 #ifndef NCR5380_io_delay
121 #define NCR5380_io_delay(x)
124 #ifndef NCR5380_acquire_dma_irq
125 #define NCR5380_acquire_dma_irq(x) (1)
128 #ifndef NCR5380_release_dma_irq
129 #define NCR5380_release_dma_irq(x)
132 static int do_abort(struct Scsi_Host *);
133 static void do_reset(struct Scsi_Host *);
134 static void bus_reset_cleanup(struct Scsi_Host *);
137 * initialize_SCp - init the scsi pointer field
138 * @cmd: command block to set up
140 * Set up the internal fields in the SCSI command.
143 static inline void initialize_SCp(struct scsi_cmnd *cmd)
146 * Initialize the Scsi Pointer field so that all of the commands in the
147 * various queues are valid.
150 if (scsi_bufflen(cmd)) {
151 cmd->SCp.buffer = scsi_sglist(cmd);
152 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
153 cmd->SCp.this_residual = cmd->SCp.buffer->length;
155 cmd->SCp.buffer = NULL;
157 cmd->SCp.this_residual = 0;
161 cmd->SCp.Message = 0;
164 static inline void advance_sg_buffer(struct scsi_cmnd *cmd)
166 struct scatterlist *s = cmd->SCp.buffer;
168 if (!cmd->SCp.this_residual && s && !sg_is_last(s)) {
169 cmd->SCp.buffer = sg_next(s);
170 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
171 cmd->SCp.this_residual = cmd->SCp.buffer->length;
176 * NCR5380_poll_politely2 - wait for two chip register values
177 * @hostdata: host private data
178 * @reg1: 5380 register to poll
179 * @bit1: Bitmask to check
180 * @val1: Expected value
181 * @reg2: Second 5380 register to poll
182 * @bit2: Second bitmask to check
183 * @val2: Second expected value
184 * @wait: Time-out in jiffies
186 * Polls the chip in a reasonably efficient manner waiting for an
187 * event to occur. After a short quick poll we begin to yield the CPU
188 * (if possible). In irq contexts the time-out is arbitrarily limited.
189 * Callers may hold locks as long as they are held in irq mode.
191 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
194 static int NCR5380_poll_politely2(struct NCR5380_hostdata *hostdata,
195 unsigned int reg1, u8 bit1, u8 val1,
196 unsigned int reg2, u8 bit2, u8 val2,
199 unsigned long n = hostdata->poll_loops;
200 unsigned long deadline = jiffies + wait;
203 if ((NCR5380_read(reg1) & bit1) == val1)
205 if ((NCR5380_read(reg2) & bit2) == val2)
210 if (irqs_disabled() || in_interrupt())
213 /* Repeatedly sleep for 1 ms until deadline */
214 while (time_is_after_jiffies(deadline)) {
215 schedule_timeout_uninterruptible(1);
216 if ((NCR5380_read(reg1) & bit1) == val1)
218 if ((NCR5380_read(reg2) & bit2) == val2)
241 {BASR_END_DMA_TRANSFER, "END OF DMA"},
243 {BASR_PARITY_ERROR, "PARITY ERROR"},
245 {BASR_PHASE_MATCH, "PHASE MATCH"},
246 {BASR_BUSY_ERROR, "BUSY ERROR"},
252 {ICR_ASSERT_RST, "ASSERT RST"},
253 {ICR_ARBITRATION_PROGRESS, "ARB. IN PROGRESS"},
254 {ICR_ARBITRATION_LOST, "LOST ARB."},
255 {ICR_ASSERT_ACK, "ASSERT ACK"},
256 {ICR_ASSERT_BSY, "ASSERT BSY"},
257 {ICR_ASSERT_SEL, "ASSERT SEL"},
258 {ICR_ASSERT_ATN, "ASSERT ATN"},
259 {ICR_ASSERT_DATA, "ASSERT DATA"},
263 {MR_BLOCK_DMA_MODE, "BLOCK DMA MODE"},
264 {MR_TARGET, "TARGET"},
265 {MR_ENABLE_PAR_CHECK, "PARITY CHECK"},
266 {MR_ENABLE_PAR_INTR, "PARITY INTR"},
267 {MR_ENABLE_EOP_INTR, "EOP INTR"},
268 {MR_MONITOR_BSY, "MONITOR BSY"},
269 {MR_DMA_MODE, "DMA MODE"},
270 {MR_ARBITRATE, "ARBITRATE"},
275 * NCR5380_print - print scsi bus signals
276 * @instance: adapter state to dump
278 * Print the SCSI bus signals for debugging purposes
281 static void NCR5380_print(struct Scsi_Host *instance)
283 struct NCR5380_hostdata *hostdata = shost_priv(instance);
284 unsigned char status, basr, mr, icr, i;
286 status = NCR5380_read(STATUS_REG);
287 mr = NCR5380_read(MODE_REG);
288 icr = NCR5380_read(INITIATOR_COMMAND_REG);
289 basr = NCR5380_read(BUS_AND_STATUS_REG);
291 printk(KERN_DEBUG "SR = 0x%02x : ", status);
292 for (i = 0; signals[i].mask; ++i)
293 if (status & signals[i].mask)
294 printk(KERN_CONT "%s, ", signals[i].name);
295 printk(KERN_CONT "\nBASR = 0x%02x : ", basr);
296 for (i = 0; basrs[i].mask; ++i)
297 if (basr & basrs[i].mask)
298 printk(KERN_CONT "%s, ", basrs[i].name);
299 printk(KERN_CONT "\nICR = 0x%02x : ", icr);
300 for (i = 0; icrs[i].mask; ++i)
301 if (icr & icrs[i].mask)
302 printk(KERN_CONT "%s, ", icrs[i].name);
303 printk(KERN_CONT "\nMR = 0x%02x : ", mr);
304 for (i = 0; mrs[i].mask; ++i)
305 if (mr & mrs[i].mask)
306 printk(KERN_CONT "%s, ", mrs[i].name);
307 printk(KERN_CONT "\n");
314 {PHASE_DATAOUT, "DATAOUT"},
315 {PHASE_DATAIN, "DATAIN"},
316 {PHASE_CMDOUT, "CMDOUT"},
317 {PHASE_STATIN, "STATIN"},
318 {PHASE_MSGOUT, "MSGOUT"},
319 {PHASE_MSGIN, "MSGIN"},
320 {PHASE_UNKNOWN, "UNKNOWN"}
324 * NCR5380_print_phase - show SCSI phase
325 * @instance: adapter to dump
327 * Print the current SCSI phase for debugging purposes
330 static void NCR5380_print_phase(struct Scsi_Host *instance)
332 struct NCR5380_hostdata *hostdata = shost_priv(instance);
333 unsigned char status;
336 status = NCR5380_read(STATUS_REG);
337 if (!(status & SR_REQ))
338 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
340 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
341 (phases[i].value != (status & PHASE_MASK)); ++i)
343 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
349 * NCR5380_info - report driver and host information
350 * @instance: relevant scsi host instance
352 * For use as the host template info() handler.
355 static const char *NCR5380_info(struct Scsi_Host *instance)
357 struct NCR5380_hostdata *hostdata = shost_priv(instance);
359 return hostdata->info;
363 * NCR5380_init - initialise an NCR5380
364 * @instance: adapter to configure
365 * @flags: control flags
367 * Initializes *instance and corresponding 5380 chip,
368 * with flags OR'd into the initial flags value.
370 * Notes : I assume that the host, hostno, and id bits have been
371 * set correctly. I don't care about the irq and other fields.
373 * Returns 0 for success
376 static int NCR5380_init(struct Scsi_Host *instance, int flags)
378 struct NCR5380_hostdata *hostdata = shost_priv(instance);
380 unsigned long deadline;
381 unsigned long accesses_per_ms;
383 instance->max_lun = 7;
385 hostdata->host = instance;
386 hostdata->id_mask = 1 << instance->this_id;
387 hostdata->id_higher_mask = 0;
388 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
389 if (i > hostdata->id_mask)
390 hostdata->id_higher_mask |= i;
391 for (i = 0; i < 8; ++i)
392 hostdata->busy[i] = 0;
393 hostdata->dma_len = 0;
395 spin_lock_init(&hostdata->lock);
396 hostdata->connected = NULL;
397 hostdata->sensing = NULL;
398 INIT_LIST_HEAD(&hostdata->autosense);
399 INIT_LIST_HEAD(&hostdata->unissued);
400 INIT_LIST_HEAD(&hostdata->disconnected);
402 hostdata->flags = flags;
404 INIT_WORK(&hostdata->main_task, NCR5380_main);
405 hostdata->work_q = alloc_workqueue("ncr5380_%d",
406 WQ_UNBOUND | WQ_MEM_RECLAIM,
407 1, instance->host_no);
408 if (!hostdata->work_q)
411 snprintf(hostdata->info, sizeof(hostdata->info),
412 "%s, irq %d, io_port 0x%lx, base 0x%lx, can_queue %d, cmd_per_lun %d, sg_tablesize %d, this_id %d, flags { %s%s%s}",
413 instance->hostt->name, instance->irq, hostdata->io_port,
414 hostdata->base, instance->can_queue, instance->cmd_per_lun,
415 instance->sg_tablesize, instance->this_id,
416 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
417 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
418 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "");
420 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
421 NCR5380_write(MODE_REG, MR_BASE);
422 NCR5380_write(TARGET_COMMAND_REG, 0);
423 NCR5380_write(SELECT_ENABLE_REG, 0);
425 /* Calibrate register polling loop */
427 deadline = jiffies + 1;
430 } while (time_is_after_jiffies(deadline));
431 deadline += msecs_to_jiffies(256);
433 NCR5380_read(STATUS_REG);
436 } while (time_is_after_jiffies(deadline));
437 accesses_per_ms = i / 256;
438 hostdata->poll_loops = NCR5380_REG_POLL_TIME * accesses_per_ms / 2;
444 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
445 * @instance: adapter to check
447 * If the system crashed, it may have crashed with a connected target and
448 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
449 * currently established nexus, which we know nothing about. Failing that
452 * Note that a bus reset will cause the chip to assert IRQ.
454 * Returns 0 if successful, otherwise -ENXIO.
457 static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
459 struct NCR5380_hostdata *hostdata = shost_priv(instance);
462 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
467 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
468 NCR5380_poll_politely(hostdata,
469 STATUS_REG, SR_BSY, 0, 5 * HZ);
472 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
476 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
478 /* Wait after a reset; the SCSI standard calls for
479 * 250ms, we wait 500ms to be on the safe side.
480 * But some Toshiba CD-ROMs need ten times that.
482 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
488 shost_printk(KERN_ERR, instance, "bus locked solid\n");
496 * NCR5380_exit - remove an NCR5380
497 * @instance: adapter to remove
499 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
502 static void NCR5380_exit(struct Scsi_Host *instance)
504 struct NCR5380_hostdata *hostdata = shost_priv(instance);
506 cancel_work_sync(&hostdata->main_task);
507 destroy_workqueue(hostdata->work_q);
511 * complete_cmd - finish processing a command and return it to the SCSI ML
512 * @instance: the host instance
513 * @cmd: command to complete
516 static void complete_cmd(struct Scsi_Host *instance,
517 struct scsi_cmnd *cmd)
519 struct NCR5380_hostdata *hostdata = shost_priv(instance);
521 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
523 if (hostdata->sensing == cmd) {
524 /* Autosense processing ends here */
525 if (status_byte(cmd->result) != GOOD) {
526 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
528 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
529 set_driver_byte(cmd, DRIVER_SENSE);
531 hostdata->sensing = NULL;
538 * NCR5380_queue_command - queue a command
539 * @instance: the relevant SCSI adapter
542 * cmd is added to the per-instance issue queue, with minor
543 * twiddling done to the host specific fields of cmd. If the
544 * main coroutine is not running, it is restarted.
547 static int NCR5380_queue_command(struct Scsi_Host *instance,
548 struct scsi_cmnd *cmd)
550 struct NCR5380_hostdata *hostdata = shost_priv(instance);
551 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
554 #if (NDEBUG & NDEBUG_NO_WRITE)
555 switch (cmd->cmnd[0]) {
558 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
559 cmd->result = (DID_ERROR << 16);
563 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
567 if (!NCR5380_acquire_dma_irq(instance))
568 return SCSI_MLQUEUE_HOST_BUSY;
570 spin_lock_irqsave(&hostdata->lock, flags);
573 * Insert the cmd into the issue queue. Note that REQUEST SENSE
574 * commands are added to the head of the queue since any command will
575 * clear the contingent allegiance condition that exists and the
576 * sense data is only guaranteed to be valid while the condition exists.
579 if (cmd->cmnd[0] == REQUEST_SENSE)
580 list_add(&ncmd->list, &hostdata->unissued);
582 list_add_tail(&ncmd->list, &hostdata->unissued);
584 spin_unlock_irqrestore(&hostdata->lock, flags);
586 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
587 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
589 /* Kick off command processing */
590 queue_work(hostdata->work_q, &hostdata->main_task);
594 static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
596 struct NCR5380_hostdata *hostdata = shost_priv(instance);
598 /* Caller does the locking needed to set & test these data atomically */
599 if (list_empty(&hostdata->disconnected) &&
600 list_empty(&hostdata->unissued) &&
601 list_empty(&hostdata->autosense) &&
602 !hostdata->connected &&
603 !hostdata->selecting) {
604 NCR5380_release_dma_irq(instance);
609 * dequeue_next_cmd - dequeue a command for processing
610 * @instance: the scsi host instance
612 * Priority is given to commands on the autosense queue. These commands
613 * need autosense because of a CHECK CONDITION result.
615 * Returns a command pointer if a command is found for a target that is
616 * not already busy. Otherwise returns NULL.
619 static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
621 struct NCR5380_hostdata *hostdata = shost_priv(instance);
622 struct NCR5380_cmd *ncmd;
623 struct scsi_cmnd *cmd;
625 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
626 list_for_each_entry(ncmd, &hostdata->unissued, list) {
627 cmd = NCR5380_to_scmd(ncmd);
628 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
629 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
631 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
632 list_del(&ncmd->list);
633 dsprintk(NDEBUG_QUEUES, instance,
634 "dequeue: removed %p from issue queue\n", cmd);
639 /* Autosense processing begins here */
640 ncmd = list_first_entry(&hostdata->autosense,
641 struct NCR5380_cmd, list);
642 list_del(&ncmd->list);
643 cmd = NCR5380_to_scmd(ncmd);
644 dsprintk(NDEBUG_QUEUES, instance,
645 "dequeue: removed %p from autosense queue\n", cmd);
646 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
647 hostdata->sensing = cmd;
653 static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
655 struct NCR5380_hostdata *hostdata = shost_priv(instance);
656 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
658 if (hostdata->sensing == cmd) {
659 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
660 list_add(&ncmd->list, &hostdata->autosense);
661 hostdata->sensing = NULL;
663 list_add(&ncmd->list, &hostdata->unissued);
667 * NCR5380_main - NCR state machines
669 * NCR5380_main is a coroutine that runs as long as more work can
670 * be done on the NCR5380 host adapters in a system. Both
671 * NCR5380_queue_command() and NCR5380_intr() will try to start it
672 * in case it is not running.
675 static void NCR5380_main(struct work_struct *work)
677 struct NCR5380_hostdata *hostdata =
678 container_of(work, struct NCR5380_hostdata, main_task);
679 struct Scsi_Host *instance = hostdata->host;
685 spin_lock_irq(&hostdata->lock);
686 while (!hostdata->connected && !hostdata->selecting) {
687 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
692 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
695 * Attempt to establish an I_T_L nexus here.
696 * On success, instance->hostdata->connected is set.
697 * On failure, we must add the command back to the
698 * issue queue so we can keep trying.
701 * REQUEST SENSE commands are issued without tagged
702 * queueing, even on SCSI-II devices because the
703 * contingent allegiance condition exists for the
707 if (!NCR5380_select(instance, cmd)) {
708 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
709 maybe_release_dma_irq(instance);
711 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
712 "main: select failed, returning %p to queue\n", cmd);
713 requeue_cmd(instance, cmd);
716 if (hostdata->connected && !hostdata->dma_len) {
717 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
718 NCR5380_information_transfer(instance);
721 if (!hostdata->connected)
722 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
723 spin_unlock_irq(&hostdata->lock);
730 * NCR5380_dma_complete - finish DMA transfer
731 * @instance: the scsi host instance
733 * Called by the interrupt handler when DMA finishes or a phase
734 * mismatch occurs (which would end the DMA transfer).
737 static void NCR5380_dma_complete(struct Scsi_Host *instance)
739 struct NCR5380_hostdata *hostdata = shost_priv(instance);
741 unsigned char **data;
743 int saved_data = 0, overrun = 0;
746 if (hostdata->read_overruns) {
747 p = hostdata->connected->SCp.phase;
750 if ((NCR5380_read(BUS_AND_STATUS_REG) &
751 (BASR_PHASE_MATCH | BASR_ACK)) ==
752 (BASR_PHASE_MATCH | BASR_ACK)) {
753 saved_data = NCR5380_read(INPUT_DATA_REG);
755 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
761 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
762 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
767 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
768 (BASR_PHASE_MATCH | BASR_ACK)) {
769 pr_err("scsi%d: BASR %02x\n", instance->host_no,
770 NCR5380_read(BUS_AND_STATUS_REG));
771 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
777 NCR5380_write(MODE_REG, MR_BASE);
778 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
779 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
781 transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata);
782 hostdata->dma_len = 0;
784 data = (unsigned char **)&hostdata->connected->SCp.ptr;
785 count = &hostdata->connected->SCp.this_residual;
786 *data += transferred;
787 *count -= transferred;
789 if (hostdata->read_overruns) {
792 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
793 cnt = toPIO = hostdata->read_overruns;
795 dsprintk(NDEBUG_DMA, instance,
796 "Got an input overrun, using saved byte\n");
797 *(*data)++ = saved_data;
803 dsprintk(NDEBUG_DMA, instance,
804 "Doing %d byte PIO to 0x%p\n", cnt, *data);
805 NCR5380_transfer_pio(instance, &p, &cnt, data);
806 *count -= toPIO - cnt;
813 * NCR5380_intr - generic NCR5380 irq handler
814 * @irq: interrupt number
815 * @dev_id: device info
817 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
818 * from the disconnected queue, and restarting NCR5380_main()
821 * The chip can assert IRQ in any of six different conditions. The IRQ flag
822 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
823 * Three of these six conditions are latched in the Bus and Status Register:
824 * - End of DMA (cleared by ending DMA Mode)
825 * - Parity error (cleared by reading RPIR)
826 * - Loss of BSY (cleared by reading RPIR)
827 * Two conditions have flag bits that are not latched:
828 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
829 * - Bus reset (non-maskable)
830 * The remaining condition has no flag bit at all:
831 * - Selection/reselection
833 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
834 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
835 * claimed that "the design of the [DP8490] interrupt logic ensures
836 * interrupts will not be lost (they can be on the DP5380)."
837 * The L5380/53C80 datasheet from LOGIC Devices has more details.
839 * Checking for bus reset by reading RST is futile because of interrupt
840 * latency, but a bus reset will reset chip logic. Checking for parity error
841 * is unnecessary because that interrupt is never enabled. A Loss of BSY
842 * condition will clear DMA Mode. We can tell when this occurs because the
843 * the Busy Monitor interrupt is enabled together with DMA Mode.
846 static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
848 struct Scsi_Host *instance = dev_id;
849 struct NCR5380_hostdata *hostdata = shost_priv(instance);
854 spin_lock_irqsave(&hostdata->lock, flags);
856 basr = NCR5380_read(BUS_AND_STATUS_REG);
857 if (basr & BASR_IRQ) {
858 unsigned char mr = NCR5380_read(MODE_REG);
859 unsigned char sr = NCR5380_read(STATUS_REG);
861 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
864 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
865 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
866 * We ack IRQ after clearing Mode Register. Workarounds
867 * for End of DMA errata need to happen in DMA Mode.
870 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
872 if (hostdata->connected) {
873 NCR5380_dma_complete(instance);
874 queue_work(hostdata->work_q, &hostdata->main_task);
876 NCR5380_write(MODE_REG, MR_BASE);
877 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
879 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
880 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
881 /* Probably reselected */
882 NCR5380_write(SELECT_ENABLE_REG, 0);
883 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
885 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
887 if (!hostdata->connected) {
888 NCR5380_reselect(instance);
889 queue_work(hostdata->work_q, &hostdata->main_task);
891 if (!hostdata->connected)
892 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
894 /* Probably Bus Reset */
895 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
898 /* Certainly Bus Reset */
899 shost_printk(KERN_WARNING, instance,
900 "bus reset interrupt\n");
901 bus_reset_cleanup(instance);
903 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
906 dregs->csr |= CSR_DMA_ENABLE;
911 dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n");
913 dregs->csr |= CSR_DMA_ENABLE;
917 spin_unlock_irqrestore(&hostdata->lock, flags);
919 return IRQ_RETVAL(handled);
923 * NCR5380_select - attempt arbitration and selection for a given command
924 * @instance: the Scsi_Host instance
925 * @cmd: the scsi_cmnd to execute
927 * This routine establishes an I_T_L nexus for a SCSI command. This involves
928 * ARBITRATION, SELECTION and MESSAGE OUT phases and an IDENTIFY message.
930 * Returns true if the operation should be retried.
931 * Returns false if it should not be retried.
934 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
935 * with registers as they should have been on entry - ie
936 * SELECT_ENABLE will be set appropriately, the NCR5380
937 * will cease to drive any SCSI bus signals.
939 * If successful : the I_T_L nexus will be established, and
940 * hostdata->connected will be set to cmd.
941 * SELECT interrupt will be disabled.
943 * If failed (no target) : cmd->scsi_done() will be called, and the
944 * cmd->result host byte set to DID_BAD_TARGET.
947 static bool NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
948 __releases(&hostdata->lock) __acquires(&hostdata->lock)
950 struct NCR5380_hostdata *hostdata = shost_priv(instance);
951 unsigned char tmp[3], phase;
956 bool can_disconnect = instance->irq != NO_IRQ &&
957 cmd->cmnd[0] != REQUEST_SENSE;
959 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
960 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
964 * Arbitration and selection phases are slow and involve dropping the
965 * lock, so we have to watch out for EH. An exception handler may
966 * change 'selecting' to NULL. This function will then return false
967 * so that the caller will forget about 'cmd'. (During information
968 * transfer phases, EH may change 'connected' to NULL.)
970 hostdata->selecting = cmd;
973 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
974 * data bus during SELECTION.
977 NCR5380_write(TARGET_COMMAND_REG, 0);
983 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
984 NCR5380_write(MODE_REG, MR_ARBITRATE);
986 /* The chip now waits for BUS FREE phase. Then after the 800 ns
987 * Bus Free Delay, arbitration will begin.
990 spin_unlock_irq(&hostdata->lock);
991 err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0,
992 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
993 ICR_ARBITRATION_PROGRESS, HZ);
994 spin_lock_irq(&hostdata->lock);
995 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
996 /* Reselection interrupt */
999 if (!hostdata->selecting) {
1000 /* Command was aborted */
1001 NCR5380_write(MODE_REG, MR_BASE);
1005 NCR5380_write(MODE_REG, MR_BASE);
1006 shost_printk(KERN_ERR, instance,
1007 "select: arbitration timeout\n");
1010 spin_unlock_irq(&hostdata->lock);
1012 /* The SCSI-2 arbitration delay is 2.4 us */
1015 /* Check for lost arbitration */
1016 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1017 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1018 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1019 NCR5380_write(MODE_REG, MR_BASE);
1020 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
1021 spin_lock_irq(&hostdata->lock);
1025 /* After/during arbitration, BSY should be asserted.
1026 * IBM DPES-31080 Version S31Q works now
1027 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1029 NCR5380_write(INITIATOR_COMMAND_REG,
1030 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
1033 * Again, bus clear + bus settle time is 1.2us, however, this is
1034 * a minimum so we'll udelay ceil(1.2)
1037 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1042 spin_lock_irq(&hostdata->lock);
1044 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1045 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
1048 if (!hostdata->selecting) {
1049 NCR5380_write(MODE_REG, MR_BASE);
1050 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1054 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
1057 * Now that we have won arbitration, start Selection process, asserting
1058 * the host and target ID's on the SCSI bus.
1061 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
1064 * Raise ATN while SEL is true before BSY goes false from arbitration,
1065 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1066 * phase immediately after selection.
1069 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1070 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
1071 NCR5380_write(MODE_REG, MR_BASE);
1074 * Reselect interrupts must be turned off prior to the dropping of BSY,
1075 * otherwise we will trigger an interrupt.
1077 NCR5380_write(SELECT_ENABLE_REG, 0);
1079 spin_unlock_irq(&hostdata->lock);
1082 * The initiator shall then wait at least two deskew delays and release
1085 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1088 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1089 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
1092 * Something weird happens when we cease to drive BSY - looks
1093 * like the board/chip is letting us do another read before the
1094 * appropriate propagation delay has expired, and we're confusing
1095 * a BSY signal from ourselves as the target's response to SELECTION.
1097 * A small delay (the 'C++' frontend breaks the pipeline with an
1098 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1099 * tighter 'C' code breaks and requires this) solves the problem -
1100 * the 1 us delay is arbitrary, and only used because this delay will
1101 * be the same on other platforms and since it works here, it should
1104 * wingel suggests that this could be due to failing to wait
1110 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
1113 * The SCSI specification calls for a 250 ms timeout for the actual
1117 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY,
1118 msecs_to_jiffies(250));
1120 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1121 spin_lock_irq(&hostdata->lock);
1122 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1123 NCR5380_reselect(instance);
1124 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
1129 spin_lock_irq(&hostdata->lock);
1130 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1132 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1133 if (!hostdata->selecting)
1136 cmd->result = DID_BAD_TARGET << 16;
1137 complete_cmd(instance, cmd);
1138 dsprintk(NDEBUG_SELECTION, instance,
1139 "target did not respond within 250ms\n");
1145 * No less than two deskew delays after the initiator detects the
1146 * BSY signal is true, it shall release the SEL signal and may
1147 * change the DATA BUS. -wingel
1152 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1155 * Since we followed the SCSI spec, and raised ATN while SEL
1156 * was true but before BSY was false during selection, the information
1157 * transfer phase should be a MESSAGE OUT phase so that we can send the
1161 /* Wait for start of REQ/ACK handshake */
1163 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
1164 spin_lock_irq(&hostdata->lock);
1166 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1167 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1170 if (!hostdata->selecting) {
1175 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1177 tmp[0] = IDENTIFY(can_disconnect, cmd->device->lun);
1181 phase = PHASE_MSGOUT;
1182 NCR5380_transfer_pio(instance, &phase, &len, &data);
1184 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1185 cmd->result = DID_ERROR << 16;
1186 complete_cmd(instance, cmd);
1187 dsprintk(NDEBUG_SELECTION, instance, "IDENTIFY message transfer failed\n");
1192 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
1194 hostdata->connected = cmd;
1195 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
1197 #ifdef SUN3_SCSI_VME
1198 dregs->csr |= CSR_INTR;
1201 initialize_SCp(cmd);
1206 if (!hostdata->selecting)
1208 hostdata->selecting = NULL;
1213 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1214 * unsigned char *phase, int *count, unsigned char **data)
1216 * Purpose : transfers data in given phase using polled I/O
1218 * Inputs : instance - instance of driver, *phase - pointer to
1219 * what phase is expected, *count - pointer to number of
1220 * bytes to transfer, **data - pointer to data pointer.
1222 * Returns : -1 when different phase is entered without transferring
1223 * maximum number of bytes, 0 if all bytes are transferred or exit
1226 * Also, *phase, *count, *data are modified in place.
1228 * XXX Note : handling for bus free may be useful.
1232 * Note : this code is not as quick as it could be, however it
1233 * IS 100% reliable, and for the actual data transfer where speed
1234 * counts, we will always do a pseudo DMA or DMA transfer.
1237 static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1238 unsigned char *phase, int *count,
1239 unsigned char **data)
1241 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1242 unsigned char p = *phase, tmp;
1244 unsigned char *d = *data;
1247 * The NCR5380 chip will only drive the SCSI bus when the
1248 * phase specified in the appropriate bits of the TARGET COMMAND
1249 * REGISTER match the STATUS REGISTER
1252 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1256 * Wait for assertion of REQ, after which the phase bits will be
1260 if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1263 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
1265 /* Check for phase mismatch */
1266 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
1267 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1268 NCR5380_dprint_phase(NDEBUG_PIO, instance);
1272 /* Do actual transfer from SCSI bus to / from memory */
1274 NCR5380_write(OUTPUT_DATA_REG, *d);
1276 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1281 * The SCSI standard suggests that in MSGOUT phase, the initiator
1282 * should drop ATN on the last byte of the message phase
1283 * after REQ has been asserted for the handshake but before
1284 * the initiator raises ACK.
1288 if (!((p & SR_MSG) && c > 1)) {
1289 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1290 NCR5380_dprint(NDEBUG_PIO, instance);
1291 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1292 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1294 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1295 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1296 NCR5380_dprint(NDEBUG_PIO, instance);
1297 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1298 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1301 NCR5380_dprint(NDEBUG_PIO, instance);
1302 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1305 if (NCR5380_poll_politely(hostdata,
1306 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1309 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
1312 * We have several special cases to consider during REQ/ACK handshaking :
1313 * 1. We were in MSGOUT phase, and we are on the last byte of the
1314 * message. ATN must be dropped as ACK is dropped.
1316 * 2. We are in a MSGIN phase, and we are on the last byte of the
1317 * message. We must exit with ACK asserted, so that the calling
1318 * code may raise ATN before dropping ACK to reject the message.
1320 * 3. ACK and ATN are clear and the target may proceed as normal.
1322 if (!(p == PHASE_MSGIN && c == 1)) {
1323 if (p == PHASE_MSGOUT && c > 1)
1324 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1326 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1330 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
1334 tmp = NCR5380_read(STATUS_REG);
1335 /* The phase read from the bus is valid if either REQ is (already)
1336 * asserted or if ACK hasn't been released yet. The latter applies if
1337 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1339 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
1340 *phase = tmp & PHASE_MASK;
1342 *phase = PHASE_UNKNOWN;
1344 if (!c || (*phase == p))
1351 * do_reset - issue a reset command
1352 * @instance: adapter to reset
1354 * Issue a reset sequence to the NCR5380 and try and get the bus
1355 * back into sane shape.
1357 * This clears the reset interrupt flag because there may be no handler for
1358 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1359 * been installed. And when in EH we may have released the ST DMA interrupt.
1362 static void do_reset(struct Scsi_Host *instance)
1364 struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
1365 unsigned long flags;
1367 local_irq_save(flags);
1368 NCR5380_write(TARGET_COMMAND_REG,
1369 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1370 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1372 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1373 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1374 local_irq_restore(flags);
1378 * do_abort - abort the currently established nexus by going to
1379 * MESSAGE OUT phase and sending an ABORT message.
1380 * @instance: relevant scsi host instance
1382 * Returns 0 on success, -1 on failure.
1385 static int do_abort(struct Scsi_Host *instance)
1387 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1388 unsigned char *msgptr, phase, tmp;
1392 /* Request message out phase */
1393 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1396 * Wait for the target to indicate a valid phase by asserting
1397 * REQ. Once this happens, we'll have either a MSGOUT phase
1398 * and can immediately send the ABORT message, or we'll have some
1399 * other phase and will have to source/sink data.
1401 * We really don't care what value was on the bus or what value
1402 * the target sees, so we just handshake.
1405 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1409 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
1411 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1413 if (tmp != PHASE_MSGOUT) {
1414 NCR5380_write(INITIATOR_COMMAND_REG,
1415 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1416 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0, 3 * HZ);
1419 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1425 phase = PHASE_MSGOUT;
1426 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
1429 * If we got here, and the command completed successfully,
1430 * we're about to go into bus free state.
1433 return len ? -1 : 0;
1436 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1441 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1442 * unsigned char *phase, int *count, unsigned char **data)
1444 * Purpose : transfers data in given phase using either real
1447 * Inputs : instance - instance of driver, *phase - pointer to
1448 * what phase is expected, *count - pointer to number of
1449 * bytes to transfer, **data - pointer to data pointer.
1451 * Returns : -1 when different phase is entered without transferring
1452 * maximum number of bytes, 0 if all bytes or transferred or exit
1455 * Also, *phase, *count, *data are modified in place.
1459 static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1460 unsigned char *phase, int *count,
1461 unsigned char **data)
1463 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1465 unsigned char p = *phase;
1466 unsigned char *d = *data;
1470 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1475 hostdata->connected->SCp.phase = p;
1478 if (hostdata->read_overruns)
1479 c -= hostdata->read_overruns;
1480 else if (hostdata->flags & FLAG_DMA_FIXUP)
1484 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1485 (p & SR_IO) ? "receive" : "send", c, d);
1488 /* send start chain */
1489 sun3scsi_dma_start(c, *data);
1492 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1493 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1494 MR_ENABLE_EOP_INTR);
1496 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1497 /* On the Medusa, it is a must to initialize the DMA before
1498 * starting the NCR. This is also the cleaner way for the TT.
1501 result = NCR5380_dma_recv_setup(hostdata, d, c);
1503 result = NCR5380_dma_send_setup(hostdata, d, c);
1507 * On the PAS16 at least I/O recovery delays are not needed here.
1508 * Everyone else seems to want them.
1512 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1513 NCR5380_io_delay(1);
1514 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1516 NCR5380_io_delay(1);
1517 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1518 NCR5380_io_delay(1);
1519 NCR5380_write(START_DMA_SEND_REG, 0);
1520 NCR5380_io_delay(1);
1524 #ifdef SUN3_SCSI_VME
1525 dregs->csr |= CSR_DMA_ENABLE;
1527 sun3_dma_active = 1;
1530 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1531 /* On the Falcon, the DMA setup must be done after the last
1532 * NCR access, else the DMA setup gets trashed!
1535 result = NCR5380_dma_recv_setup(hostdata, d, c);
1537 result = NCR5380_dma_send_setup(hostdata, d, c);
1540 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1544 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1546 hostdata->dma_len = result;
1550 /* The result is zero iff pseudo DMA send/receive was completed. */
1551 hostdata->dma_len = c;
1554 * A note regarding the DMA errata workarounds for early NMOS silicon.
1556 * For DMA sends, we want to wait until the last byte has been
1557 * transferred out over the bus before we turn off DMA mode. Alas, there
1558 * seems to be no terribly good way of doing this on a 5380 under all
1559 * conditions. For non-scatter-gather operations, we can wait until REQ
1560 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1561 * are nastier, since the device will be expecting more data than we
1562 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1563 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1564 * why this signal was added to the newer chips) but on the older 538[01]
1565 * this signal does not exist. The workaround for this lack is a watchdog;
1566 * we bail out of the wait-loop after a modest amount of wait-time if
1567 * the usual exit conditions are not met. Not a terribly clean or
1568 * correct solution :-%
1570 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1571 * If the chip is in DMA receive mode, it will respond to a target's
1572 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1573 * ACK, even if it has _already_ been notified by the DMA controller that
1574 * the current DMA transfer has completed! If the NCR5380 is then taken
1575 * out of DMA mode, this already-acknowledged byte is lost. This is
1576 * not a problem for "one DMA transfer per READ command", because
1577 * the situation will never arise... either all of the data is DMA'ed
1578 * properly, or the target switches to MESSAGE IN phase to signal a
1579 * disconnection (either operation bringing the DMA to a clean halt).
1580 * However, in order to handle scatter-receive, we must work around the
1581 * problem. The chosen fix is to DMA fewer bytes, then check for the
1582 * condition before taking the NCR5380 out of DMA mode. One or two extra
1583 * bytes are transferred via PIO as necessary to fill out the original
1587 if (hostdata->flags & FLAG_DMA_FIXUP) {
1590 * The workaround was to transfer fewer bytes than we
1591 * intended to with the pseudo-DMA read function, wait for
1592 * the chip to latch the last byte, read it, and then disable
1595 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1596 * REQ is deasserted when ACK is asserted, and not reasserted
1597 * until ACK goes false. Since the NCR5380 won't lower ACK
1598 * until DACK is asserted, which won't happen unless we twiddle
1599 * the DMA port or we take the NCR5380 out of DMA mode, we
1600 * can guarantee that we won't handshake another extra
1604 if (NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
1605 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1607 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
1609 if (NCR5380_poll_politely(hostdata, STATUS_REG,
1610 SR_REQ, 0, HZ) < 0) {
1612 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1614 d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1617 * Wait for the last byte to be sent. If REQ is being asserted for
1618 * the byte we're interested, we'll ACK it and it will go false.
1620 if (NCR5380_poll_politely2(hostdata,
1621 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1622 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1624 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
1629 NCR5380_dma_complete(instance);
1634 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1636 * Purpose : run through the various SCSI phases and do as the target
1637 * directs us to. Operates on the currently connected command,
1638 * instance->connected.
1640 * Inputs : instance, instance for which we are doing commands
1642 * Side effects : SCSI things happen, the disconnected queue will be
1643 * modified if a command disconnects, *instance->connected will
1646 * XXX Note : we need to watch for bus free or a reset condition here
1647 * to recover from an unexpected bus free condition.
1650 static void NCR5380_information_transfer(struct Scsi_Host *instance)
1651 __releases(&hostdata->lock) __acquires(&hostdata->lock)
1653 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1654 unsigned char msgout = NOP;
1658 unsigned char *data;
1659 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
1660 struct scsi_cmnd *cmd;
1662 #ifdef SUN3_SCSI_VME
1663 dregs->csr |= CSR_INTR;
1666 while ((cmd = hostdata->connected)) {
1667 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1669 tmp = NCR5380_read(STATUS_REG);
1670 /* We only have a valid SCSI phase when REQ is asserted */
1672 phase = (tmp & PHASE_MASK);
1673 if (phase != old_phase) {
1675 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1678 if (phase == PHASE_CMDOUT &&
1679 sun3_dma_setup_done != cmd) {
1682 advance_sg_buffer(cmd);
1684 count = sun3scsi_dma_xfer_len(hostdata, cmd);
1687 if (rq_data_dir(cmd->request))
1688 sun3scsi_dma_send_setup(hostdata,
1689 cmd->SCp.ptr, count);
1691 sun3scsi_dma_recv_setup(hostdata,
1692 cmd->SCp.ptr, count);
1693 sun3_dma_setup_done = cmd;
1695 #ifdef SUN3_SCSI_VME
1696 dregs->csr |= CSR_INTR;
1699 #endif /* CONFIG_SUN3 */
1701 if (sink && (phase != PHASE_MSGOUT)) {
1702 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1704 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1706 while (NCR5380_read(STATUS_REG) & SR_REQ)
1708 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1716 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1717 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
1720 cmd->result = DID_ERROR << 16;
1721 complete_cmd(instance, cmd);
1722 hostdata->connected = NULL;
1723 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
1728 * If there is no room left in the current buffer in the
1729 * scatter-gather list, move onto the next one.
1732 advance_sg_buffer(cmd);
1733 dsprintk(NDEBUG_INFORMATION, instance,
1734 "this residual %d, sg ents %d\n",
1735 cmd->SCp.this_residual,
1736 sg_nents(cmd->SCp.buffer));
1739 * The preferred transfer method is going to be
1740 * PSEUDO-DMA for systems that are strictly PIO,
1741 * since we can let the hardware do the handshaking.
1743 * For this to work, we need to know the transfersize
1744 * ahead of time, since the pseudo-DMA code will sit
1745 * in an unconditional loop.
1749 if (!cmd->device->borken)
1750 transfersize = NCR5380_dma_xfer_len(hostdata, cmd);
1752 if (transfersize > 0) {
1754 if (NCR5380_transfer_dma(instance, &phase,
1755 &len, (unsigned char **)&cmd->SCp.ptr)) {
1757 * If the watchdog timer fires, all future
1758 * accesses to this device will use the
1761 scmd_printk(KERN_INFO, cmd,
1762 "switching to slow handshake\n");
1763 cmd->device->borken = 1;
1765 bus_reset_cleanup(instance);
1768 /* Transfer a small chunk so that the
1769 * irq mode lock is not held too long.
1771 transfersize = min(cmd->SCp.this_residual,
1772 NCR5380_PIO_CHUNK_SIZE);
1774 NCR5380_transfer_pio(instance, &phase, &len,
1775 (unsigned char **)&cmd->SCp.ptr);
1776 cmd->SCp.this_residual -= transfersize - len;
1779 if (sun3_dma_setup_done == cmd)
1780 sun3_dma_setup_done = NULL;
1786 NCR5380_transfer_pio(instance, &phase, &len, &data);
1787 cmd->SCp.Message = tmp;
1791 case COMMAND_COMPLETE:
1792 /* Accept message by clearing ACK */
1794 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1795 dsprintk(NDEBUG_QUEUES, instance,
1796 "COMMAND COMPLETE %p target %d lun %llu\n",
1797 cmd, scmd_id(cmd), cmd->device->lun);
1799 hostdata->connected = NULL;
1800 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
1802 cmd->result &= ~0xffff;
1803 cmd->result |= cmd->SCp.Status;
1804 cmd->result |= cmd->SCp.Message << 8;
1806 if (cmd->cmnd[0] == REQUEST_SENSE)
1807 complete_cmd(instance, cmd);
1809 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1810 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1811 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1813 list_add_tail(&ncmd->list,
1814 &hostdata->autosense);
1816 complete_cmd(instance, cmd);
1820 * Restore phase bits to 0 so an interrupted selection,
1821 * arbitration can resume.
1823 NCR5380_write(TARGET_COMMAND_REG, 0);
1825 maybe_release_dma_irq(instance);
1827 case MESSAGE_REJECT:
1828 /* Accept message by clearing ACK */
1829 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1830 switch (hostdata->last_message) {
1831 case HEAD_OF_QUEUE_TAG:
1832 case ORDERED_QUEUE_TAG:
1833 case SIMPLE_QUEUE_TAG:
1834 cmd->device->simple_tags = 0;
1835 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
1842 /* Accept message by clearing ACK */
1843 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1844 hostdata->connected = NULL;
1845 list_add(&ncmd->list, &hostdata->disconnected);
1846 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1847 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1848 cmd, scmd_id(cmd), cmd->device->lun);
1851 * Restore phase bits to 0 so an interrupted selection,
1852 * arbitration can resume.
1854 NCR5380_write(TARGET_COMMAND_REG, 0);
1856 #ifdef SUN3_SCSI_VME
1857 dregs->csr |= CSR_DMA_ENABLE;
1861 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1862 * operation, in violation of the SCSI spec so we can safely
1863 * ignore SAVE/RESTORE pointers calls.
1865 * Unfortunately, some disks violate the SCSI spec and
1866 * don't issue the required SAVE_POINTERS message before
1867 * disconnecting, and we have to break spec to remain
1871 case RESTORE_POINTERS:
1872 /* Accept message by clearing ACK */
1873 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1875 case EXTENDED_MESSAGE:
1877 * Start the message buffer with the EXTENDED_MESSAGE
1878 * byte, since spi_print_msg() wants the whole thing.
1880 extended_msg[0] = EXTENDED_MESSAGE;
1881 /* Accept first byte by clearing ACK */
1882 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1884 spin_unlock_irq(&hostdata->lock);
1886 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
1889 data = extended_msg + 1;
1890 phase = PHASE_MSGIN;
1891 NCR5380_transfer_pio(instance, &phase, &len, &data);
1892 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1893 (int)extended_msg[1],
1894 (int)extended_msg[2]);
1896 if (!len && extended_msg[1] > 0 &&
1897 extended_msg[1] <= sizeof(extended_msg) - 2) {
1898 /* Accept third byte by clearing ACK */
1899 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1900 len = extended_msg[1] - 1;
1901 data = extended_msg + 3;
1902 phase = PHASE_MSGIN;
1904 NCR5380_transfer_pio(instance, &phase, &len, &data);
1905 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1908 switch (extended_msg[2]) {
1914 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
1917 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1918 extended_msg[2], extended_msg[1]);
1922 spin_lock_irq(&hostdata->lock);
1923 if (!hostdata->connected)
1926 /* Reject message */
1930 * If we get something weird that we aren't expecting,
1933 if (tmp == EXTENDED_MESSAGE)
1934 scmd_printk(KERN_INFO, cmd,
1935 "rejecting unknown extended message code %02x, length %d\n",
1936 extended_msg[2], extended_msg[1]);
1938 scmd_printk(KERN_INFO, cmd,
1939 "rejecting unknown message code %02x\n",
1942 msgout = MESSAGE_REJECT;
1943 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1945 } /* switch (tmp) */
1950 hostdata->last_message = msgout;
1951 NCR5380_transfer_pio(instance, &phase, &len, &data);
1952 if (msgout == ABORT) {
1953 hostdata->connected = NULL;
1954 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
1955 cmd->result = DID_ERROR << 16;
1956 complete_cmd(instance, cmd);
1957 maybe_release_dma_irq(instance);
1966 * XXX for performance reasons, on machines with a
1967 * PSEUDO-DMA architecture we should probably
1968 * use the dma transfer function.
1970 NCR5380_transfer_pio(instance, &phase, &len, &data);
1975 NCR5380_transfer_pio(instance, &phase, &len, &data);
1976 cmd->SCp.Status = tmp;
1979 shost_printk(KERN_ERR, instance, "unknown phase\n");
1980 NCR5380_dprint(NDEBUG_ANY, instance);
1981 } /* switch(phase) */
1983 spin_unlock_irq(&hostdata->lock);
1984 NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
1985 spin_lock_irq(&hostdata->lock);
1991 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
1993 * Purpose : does reselection, initializing the instance->connected
1994 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1995 * nexus has been reestablished,
1997 * Inputs : instance - this instance of the NCR5380.
2000 static void NCR5380_reselect(struct Scsi_Host *instance)
2002 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2003 unsigned char target_mask;
2005 unsigned char msg[3];
2006 struct NCR5380_cmd *ncmd;
2007 struct scsi_cmnd *tmp;
2010 * Disable arbitration, etc. since the host adapter obviously
2011 * lost, and tell an interrupted NCR5380_select() to restart.
2014 NCR5380_write(MODE_REG, MR_BASE);
2016 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2017 if (!target_mask || target_mask & (target_mask - 1)) {
2018 shost_printk(KERN_WARNING, instance,
2019 "reselect: bad target_mask 0x%02x\n", target_mask);
2024 * At this point, we have detected that our SCSI ID is on the bus,
2025 * SEL is true and BSY was false for at least one bus settle delay
2028 * We must assert BSY ourselves, until the target drops the SEL
2032 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2033 if (NCR5380_poll_politely(hostdata,
2034 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2035 shost_printk(KERN_ERR, instance, "reselect: !SEL timeout\n");
2036 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2039 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2042 * Wait for target to go into MSGIN.
2045 if (NCR5380_poll_politely(hostdata,
2046 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2047 if ((NCR5380_read(STATUS_REG) & (SR_BSY | SR_SEL)) == 0)
2048 /* BUS FREE phase */
2050 shost_printk(KERN_ERR, instance, "reselect: REQ timeout\n");
2056 /* acknowledge toggle to MSGIN */
2057 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2059 /* peek at the byte without really hitting the bus */
2060 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2064 unsigned char *data = msg;
2065 unsigned char phase = PHASE_MSGIN;
2067 NCR5380_transfer_pio(instance, &phase, &len, &data);
2074 #endif /* CONFIG_SUN3 */
2076 if (!(msg[0] & 0x80)) {
2077 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
2083 lun = msg[0] & 0x07;
2086 * We need to add code for SCSI-II to track which devices have
2087 * I_T_L_Q nexuses established, and which have simple I_T_L
2088 * nexuses so we can chose to do additional data transfer.
2092 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2093 * just reestablished, and remove it from the disconnected queue.
2097 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2098 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2100 if (target_mask == (1 << scmd_id(cmd)) &&
2101 lun == (u8)cmd->device->lun) {
2102 list_del(&ncmd->list);
2109 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2110 "reselect: removed %p from disconnected queue\n", tmp);
2112 int target = ffs(target_mask) - 1;
2114 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2117 * Since we have an established nexus that we can't do anything
2118 * with, we must abort it.
2120 if (do_abort(instance) == 0)
2121 hostdata->busy[target] &= ~(1 << lun);
2126 if (sun3_dma_setup_done != tmp) {
2129 advance_sg_buffer(tmp);
2131 count = sun3scsi_dma_xfer_len(hostdata, tmp);
2134 if (rq_data_dir(tmp->request))
2135 sun3scsi_dma_send_setup(hostdata,
2136 tmp->SCp.ptr, count);
2138 sun3scsi_dma_recv_setup(hostdata,
2139 tmp->SCp.ptr, count);
2140 sun3_dma_setup_done = tmp;
2144 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2145 #endif /* CONFIG_SUN3 */
2147 /* Accept message by clearing ACK */
2148 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2150 hostdata->connected = tmp;
2151 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2152 scmd_id(tmp), tmp->device->lun);
2156 * list_find_cmd - test for presence of a command in a linked list
2157 * @haystack: list of commands
2158 * @needle: command to search for
2161 static bool list_find_cmd(struct list_head *haystack,
2162 struct scsi_cmnd *needle)
2164 struct NCR5380_cmd *ncmd;
2166 list_for_each_entry(ncmd, haystack, list)
2167 if (NCR5380_to_scmd(ncmd) == needle)
2173 * list_remove_cmd - remove a command from linked list
2174 * @haystack: list of commands
2175 * @needle: command to remove
2178 static bool list_del_cmd(struct list_head *haystack,
2179 struct scsi_cmnd *needle)
2181 if (list_find_cmd(haystack, needle)) {
2182 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2184 list_del(&ncmd->list);
2191 * NCR5380_abort - scsi host eh_abort_handler() method
2192 * @cmd: the command to be aborted
2194 * Try to abort a given command by removing it from queues and/or sending
2195 * the target an abort message. This may not succeed in causing a target
2196 * to abort the command. Nonetheless, the low-level driver must forget about
2197 * the command because the mid-layer reclaims it and it may be re-issued.
2199 * The normal path taken by a command is as follows. For EH we trace this
2200 * same path to locate and abort the command.
2202 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2203 * [disconnected -> connected ->]...
2204 * [autosense -> connected ->] done
2206 * If cmd was not found at all then presumably it has already been completed,
2207 * in which case return SUCCESS to try to avoid further EH measures.
2209 * If the command has not completed yet, we must not fail to find it.
2210 * We have no option but to forget the aborted command (even if it still
2211 * lacks sense data). The mid-layer may re-issue a command that is in error
2212 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2213 * this driver are such that a command can appear on one queue only.
2215 * The lock protects driver data structures, but EH handlers also use it
2216 * to serialize their own execution and prevent their own re-entry.
2219 static int NCR5380_abort(struct scsi_cmnd *cmd)
2221 struct Scsi_Host *instance = cmd->device->host;
2222 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2223 unsigned long flags;
2224 int result = SUCCESS;
2226 spin_lock_irqsave(&hostdata->lock, flags);
2228 #if (NDEBUG & NDEBUG_ANY)
2229 scmd_printk(KERN_INFO, cmd, __func__);
2231 NCR5380_dprint(NDEBUG_ANY, instance);
2232 NCR5380_dprint_phase(NDEBUG_ANY, instance);
2234 if (list_del_cmd(&hostdata->unissued, cmd)) {
2235 dsprintk(NDEBUG_ABORT, instance,
2236 "abort: removed %p from issue queue\n", cmd);
2237 cmd->result = DID_ABORT << 16;
2238 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
2242 if (hostdata->selecting == cmd) {
2243 dsprintk(NDEBUG_ABORT, instance,
2244 "abort: cmd %p == selecting\n", cmd);
2245 hostdata->selecting = NULL;
2246 cmd->result = DID_ABORT << 16;
2247 complete_cmd(instance, cmd);
2251 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2252 dsprintk(NDEBUG_ABORT, instance,
2253 "abort: removed %p from disconnected list\n", cmd);
2254 /* Can't call NCR5380_select() and send ABORT because that
2255 * means releasing the lock. Need a bus reset.
2257 set_host_byte(cmd, DID_ERROR);
2258 complete_cmd(instance, cmd);
2263 if (hostdata->connected == cmd) {
2264 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2265 hostdata->connected = NULL;
2266 hostdata->dma_len = 0;
2267 if (do_abort(instance)) {
2268 set_host_byte(cmd, DID_ERROR);
2269 complete_cmd(instance, cmd);
2273 set_host_byte(cmd, DID_ABORT);
2274 complete_cmd(instance, cmd);
2278 if (list_del_cmd(&hostdata->autosense, cmd)) {
2279 dsprintk(NDEBUG_ABORT, instance,
2280 "abort: removed %p from sense queue\n", cmd);
2281 complete_cmd(instance, cmd);
2285 if (result == FAILED)
2286 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2288 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
2289 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2292 queue_work(hostdata->work_q, &hostdata->main_task);
2293 maybe_release_dma_irq(instance);
2294 spin_unlock_irqrestore(&hostdata->lock, flags);
2300 static void bus_reset_cleanup(struct Scsi_Host *instance)
2302 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2304 struct NCR5380_cmd *ncmd;
2306 /* reset NCR registers */
2307 NCR5380_write(MODE_REG, MR_BASE);
2308 NCR5380_write(TARGET_COMMAND_REG, 0);
2309 NCR5380_write(SELECT_ENABLE_REG, 0);
2311 /* After the reset, there are no more connected or disconnected commands
2312 * and no busy units; so clear the low-level status here to avoid
2313 * conflicts when the mid-level code tries to wake up the affected
2317 if (hostdata->selecting) {
2318 hostdata->selecting->result = DID_RESET << 16;
2319 complete_cmd(instance, hostdata->selecting);
2320 hostdata->selecting = NULL;
2323 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2324 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2326 set_host_byte(cmd, DID_RESET);
2327 complete_cmd(instance, cmd);
2329 INIT_LIST_HEAD(&hostdata->disconnected);
2331 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2332 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2334 cmd->scsi_done(cmd);
2336 INIT_LIST_HEAD(&hostdata->autosense);
2338 if (hostdata->connected) {
2339 set_host_byte(hostdata->connected, DID_RESET);
2340 complete_cmd(instance, hostdata->connected);
2341 hostdata->connected = NULL;
2344 for (i = 0; i < 8; ++i)
2345 hostdata->busy[i] = 0;
2346 hostdata->dma_len = 0;
2348 queue_work(hostdata->work_q, &hostdata->main_task);
2349 maybe_release_dma_irq(instance);
2353 * NCR5380_host_reset - reset the SCSI host
2354 * @cmd: SCSI command undergoing EH
2359 static int NCR5380_host_reset(struct scsi_cmnd *cmd)
2361 struct Scsi_Host *instance = cmd->device->host;
2362 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2363 unsigned long flags;
2364 struct NCR5380_cmd *ncmd;
2366 spin_lock_irqsave(&hostdata->lock, flags);
2368 #if (NDEBUG & NDEBUG_ANY)
2369 shost_printk(KERN_INFO, instance, __func__);
2371 NCR5380_dprint(NDEBUG_ANY, instance);
2372 NCR5380_dprint_phase(NDEBUG_ANY, instance);
2374 list_for_each_entry(ncmd, &hostdata->unissued, list) {
2375 struct scsi_cmnd *scmd = NCR5380_to_scmd(ncmd);
2377 scmd->result = DID_RESET << 16;
2378 scmd->scsi_done(scmd);
2380 INIT_LIST_HEAD(&hostdata->unissued);
2383 bus_reset_cleanup(instance);
2385 spin_unlock_irqrestore(&hostdata->lock, flags);