]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/NCR5380.c
scsi: NCR5380: use sg helper to iterate over scatterlist
[linux.git] / drivers / scsi / NCR5380.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NCR 5380 generic driver routines.  These should make it *trivial*
4  * to implement 5380 SCSI drivers under Linux with a non-trantor
5  * architecture.
6  *
7  * Note that these routines also work with NR53c400 family chips.
8  *
9  * Copyright 1993, Drew Eckhardt
10  * Visionary Computing
11  * (Unix and Linux consulting and custom programming)
12  * drew@colorado.edu
13  * +1 (303) 666-5836
14  *
15  * For more information, please consult
16  *
17  * NCR 5380 Family
18  * SCSI Protocol Controller
19  * Databook
20  *
21  * NCR Microelectronics
22  * 1635 Aeroplaza Drive
23  * Colorado Springs, CO 80916
24  * 1+ (719) 578-3400
25  * 1+ (800) 334-5454
26  */
27
28 /*
29  * With contributions from Ray Van Tassle, Ingmar Baumgart,
30  * Ronald van Cuijlenborg, Alan Cox and others.
31  */
32
33 /* Ported to Atari by Roman Hodek and others. */
34
35 /* Adapted for the Sun 3 by Sam Creasey. */
36
37 /*
38  * Design
39  *
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.
44  *
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.
53  *
54  *
55  * Issues specific to the NCR5380 :
56  *
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.
64  *
65  * Architecture :
66  *
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.
72  *
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.
79  *
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.
83  *
84  * On command termination, the done function will be called as
85  * appropriate.
86  *
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.
92  */
93
94 /*
95  * Using this file :
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.
99  *
100  * These macros MUST be defined :
101  *
102  * NCR5380_read(register)  - read from the specified register
103  *
104  * NCR5380_write(register, value) - write to the specific register
105  *
106  * NCR5380_implementation_fields  - additional fields needed for this
107  * specific implementation of the NCR5380
108  *
109  * Either real DMA *or* pseudo DMA may be implemented
110  *
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
115  *
116  * The generic driver is initialized by calling NCR5380_init(instance),
117  * after setting the appropriate host specific fields and ID.
118  */
119
120 #ifndef NCR5380_io_delay
121 #define NCR5380_io_delay(x)
122 #endif
123
124 #ifndef NCR5380_acquire_dma_irq
125 #define NCR5380_acquire_dma_irq(x)      (1)
126 #endif
127
128 #ifndef NCR5380_release_dma_irq
129 #define NCR5380_release_dma_irq(x)
130 #endif
131
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 *);
135
136 /**
137  * initialize_SCp - init the scsi pointer field
138  * @cmd: command block to set up
139  *
140  * Set up the internal fields in the SCSI command.
141  */
142
143 static inline void initialize_SCp(struct scsi_cmnd *cmd)
144 {
145         /*
146          * Initialize the Scsi Pointer field so that all of the commands in the
147          * various queues are valid.
148          */
149
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;
154         } else {
155                 cmd->SCp.buffer = NULL;
156                 cmd->SCp.ptr = NULL;
157                 cmd->SCp.this_residual = 0;
158         }
159
160         cmd->SCp.Status = 0;
161         cmd->SCp.Message = 0;
162 }
163
164 static inline void advance_sg_buffer(struct scsi_cmnd *cmd)
165 {
166         struct scatterlist *s = cmd->SCp.buffer;
167
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;
172         }
173 }
174
175 /**
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
185  *
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.
190  *
191  * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
192  */
193
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,
197                                   unsigned long wait)
198 {
199         unsigned long n = hostdata->poll_loops;
200         unsigned long deadline = jiffies + wait;
201
202         do {
203                 if ((NCR5380_read(reg1) & bit1) == val1)
204                         return 0;
205                 if ((NCR5380_read(reg2) & bit2) == val2)
206                         return 0;
207                 cpu_relax();
208         } while (n--);
209
210         if (irqs_disabled() || in_interrupt())
211                 return -ETIMEDOUT;
212
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)
217                         return 0;
218                 if ((NCR5380_read(reg2) & bit2) == val2)
219                         return 0;
220         }
221
222         return -ETIMEDOUT;
223 }
224
225 #if NDEBUG
226 static struct {
227         unsigned char mask;
228         const char *name;
229 } signals[] = {
230         {SR_DBP, "PARITY"},
231         {SR_RST, "RST"},
232         {SR_BSY, "BSY"},
233         {SR_REQ, "REQ"},
234         {SR_MSG, "MSG"},
235         {SR_CD, "CD"},
236         {SR_IO, "IO"},
237         {SR_SEL, "SEL"},
238         {0, NULL}
239 },
240 basrs[] = {
241         {BASR_END_DMA_TRANSFER, "END OF DMA"},
242         {BASR_DRQ, "DRQ"},
243         {BASR_PARITY_ERROR, "PARITY ERROR"},
244         {BASR_IRQ, "IRQ"},
245         {BASR_PHASE_MATCH, "PHASE MATCH"},
246         {BASR_BUSY_ERROR, "BUSY ERROR"},
247         {BASR_ATN, "ATN"},
248         {BASR_ACK, "ACK"},
249         {0, NULL}
250 },
251 icrs[] = {
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"},
260         {0, NULL}
261 },
262 mrs[] = {
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"},
271         {0, NULL}
272 };
273
274 /**
275  * NCR5380_print - print scsi bus signals
276  * @instance: adapter state to dump
277  *
278  * Print the SCSI bus signals for debugging purposes
279  */
280
281 static void NCR5380_print(struct Scsi_Host *instance)
282 {
283         struct NCR5380_hostdata *hostdata = shost_priv(instance);
284         unsigned char status, basr, mr, icr, i;
285
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);
290
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");
308 }
309
310 static struct {
311         unsigned char value;
312         const char *name;
313 } phases[] = {
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"}
321 };
322
323 /**
324  * NCR5380_print_phase - show SCSI phase
325  * @instance: adapter to dump
326  *
327  * Print the current SCSI phase for debugging purposes
328  */
329
330 static void NCR5380_print_phase(struct Scsi_Host *instance)
331 {
332         struct NCR5380_hostdata *hostdata = shost_priv(instance);
333         unsigned char status;
334         int i;
335
336         status = NCR5380_read(STATUS_REG);
337         if (!(status & SR_REQ))
338                 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
339         else {
340                 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
341                      (phases[i].value != (status & PHASE_MASK)); ++i)
342                         ;
343                 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
344         }
345 }
346 #endif
347
348 /**
349  * NCR5380_info - report driver and host information
350  * @instance: relevant scsi host instance
351  *
352  * For use as the host template info() handler.
353  */
354
355 static const char *NCR5380_info(struct Scsi_Host *instance)
356 {
357         struct NCR5380_hostdata *hostdata = shost_priv(instance);
358
359         return hostdata->info;
360 }
361
362 /**
363  * NCR5380_init - initialise an NCR5380
364  * @instance: adapter to configure
365  * @flags: control flags
366  *
367  * Initializes *instance and corresponding 5380 chip,
368  * with flags OR'd into the initial flags value.
369  *
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.
372  *
373  * Returns 0 for success
374  */
375
376 static int NCR5380_init(struct Scsi_Host *instance, int flags)
377 {
378         struct NCR5380_hostdata *hostdata = shost_priv(instance);
379         int i;
380         unsigned long deadline;
381         unsigned long accesses_per_ms;
382
383         instance->max_lun = 7;
384
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;
394
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);
401
402         hostdata->flags = flags;
403
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)
409                 return -ENOMEM;
410
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 " : "");
419
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);
424
425         /* Calibrate register polling loop */
426         i = 0;
427         deadline = jiffies + 1;
428         do {
429                 cpu_relax();
430         } while (time_is_after_jiffies(deadline));
431         deadline += msecs_to_jiffies(256);
432         do {
433                 NCR5380_read(STATUS_REG);
434                 ++i;
435                 cpu_relax();
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;
439
440         return 0;
441 }
442
443 /**
444  * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
445  * @instance: adapter to check
446  *
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
450  * do a bus reset.
451  *
452  * Note that a bus reset will cause the chip to assert IRQ.
453  *
454  * Returns 0 if successful, otherwise -ENXIO.
455  */
456
457 static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
458 {
459         struct NCR5380_hostdata *hostdata = shost_priv(instance);
460         int pass;
461
462         for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
463                 switch (pass) {
464                 case 1:
465                 case 3:
466                 case 5:
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);
470                         break;
471                 case 2:
472                         shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
473                         do_abort(instance);
474                         break;
475                 case 4:
476                         shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
477                         do_reset(instance);
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.
481                          */
482                         if (hostdata->flags & FLAG_TOSHIBA_DELAY)
483                                 msleep(2500);
484                         else
485                                 msleep(500);
486                         break;
487                 case 6:
488                         shost_printk(KERN_ERR, instance, "bus locked solid\n");
489                         return -ENXIO;
490                 }
491         }
492         return 0;
493 }
494
495 /**
496  * NCR5380_exit - remove an NCR5380
497  * @instance: adapter to remove
498  *
499  * Assumes that no more work can be queued (e.g. by NCR5380_intr).
500  */
501
502 static void NCR5380_exit(struct Scsi_Host *instance)
503 {
504         struct NCR5380_hostdata *hostdata = shost_priv(instance);
505
506         cancel_work_sync(&hostdata->main_task);
507         destroy_workqueue(hostdata->work_q);
508 }
509
510 /**
511  * complete_cmd - finish processing a command and return it to the SCSI ML
512  * @instance: the host instance
513  * @cmd: command to complete
514  */
515
516 static void complete_cmd(struct Scsi_Host *instance,
517                          struct scsi_cmnd *cmd)
518 {
519         struct NCR5380_hostdata *hostdata = shost_priv(instance);
520
521         dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
522
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);
527                 } else {
528                         scsi_eh_restore_cmnd(cmd, &hostdata->ses);
529                         set_driver_byte(cmd, DRIVER_SENSE);
530                 }
531                 hostdata->sensing = NULL;
532         }
533
534         cmd->scsi_done(cmd);
535 }
536
537 /**
538  * NCR5380_queue_command - queue a command
539  * @instance: the relevant SCSI adapter
540  * @cmd: SCSI command
541  *
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.
545  */
546
547 static int NCR5380_queue_command(struct Scsi_Host *instance,
548                                  struct scsi_cmnd *cmd)
549 {
550         struct NCR5380_hostdata *hostdata = shost_priv(instance);
551         struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
552         unsigned long flags;
553
554 #if (NDEBUG & NDEBUG_NO_WRITE)
555         switch (cmd->cmnd[0]) {
556         case WRITE_6:
557         case WRITE_10:
558                 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
559                 cmd->result = (DID_ERROR << 16);
560                 cmd->scsi_done(cmd);
561                 return 0;
562         }
563 #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
564
565         cmd->result = 0;
566
567         if (!NCR5380_acquire_dma_irq(instance))
568                 return SCSI_MLQUEUE_HOST_BUSY;
569
570         spin_lock_irqsave(&hostdata->lock, flags);
571
572         /*
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.
577          */
578
579         if (cmd->cmnd[0] == REQUEST_SENSE)
580                 list_add(&ncmd->list, &hostdata->unissued);
581         else
582                 list_add_tail(&ncmd->list, &hostdata->unissued);
583
584         spin_unlock_irqrestore(&hostdata->lock, flags);
585
586         dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
587                  cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
588
589         /* Kick off command processing */
590         queue_work(hostdata->work_q, &hostdata->main_task);
591         return 0;
592 }
593
594 static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
595 {
596         struct NCR5380_hostdata *hostdata = shost_priv(instance);
597
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);
605         }
606 }
607
608 /**
609  * dequeue_next_cmd - dequeue a command for processing
610  * @instance: the scsi host instance
611  *
612  * Priority is given to commands on the autosense queue. These commands
613  * need autosense because of a CHECK CONDITION result.
614  *
615  * Returns a command pointer if a command is found for a target that is
616  * not already busy. Otherwise returns NULL.
617  */
618
619 static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
620 {
621         struct NCR5380_hostdata *hostdata = shost_priv(instance);
622         struct NCR5380_cmd *ncmd;
623         struct scsi_cmnd *cmd;
624
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);
630
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);
635                                 return cmd;
636                         }
637                 }
638         } else {
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;
648                 return cmd;
649         }
650         return NULL;
651 }
652
653 static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
654 {
655         struct NCR5380_hostdata *hostdata = shost_priv(instance);
656         struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
657
658         if (hostdata->sensing == cmd) {
659                 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
660                 list_add(&ncmd->list, &hostdata->autosense);
661                 hostdata->sensing = NULL;
662         } else
663                 list_add(&ncmd->list, &hostdata->unissued);
664 }
665
666 /**
667  * NCR5380_main - NCR state machines
668  *
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.
673  */
674
675 static void NCR5380_main(struct work_struct *work)
676 {
677         struct NCR5380_hostdata *hostdata =
678                 container_of(work, struct NCR5380_hostdata, main_task);
679         struct Scsi_Host *instance = hostdata->host;
680         int done;
681
682         do {
683                 done = 1;
684
685                 spin_lock_irq(&hostdata->lock);
686                 while (!hostdata->connected && !hostdata->selecting) {
687                         struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
688
689                         if (!cmd)
690                                 break;
691
692                         dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
693
694                         /*
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.
699                          */
700                         /*
701                          * REQUEST SENSE commands are issued without tagged
702                          * queueing, even on SCSI-II devices because the
703                          * contingent allegiance condition exists for the
704                          * entire unit.
705                          */
706
707                         if (!NCR5380_select(instance, cmd)) {
708                                 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
709                                 maybe_release_dma_irq(instance);
710                         } else {
711                                 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
712                                          "main: select failed, returning %p to queue\n", cmd);
713                                 requeue_cmd(instance, cmd);
714                         }
715                 }
716                 if (hostdata->connected && !hostdata->dma_len) {
717                         dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
718                         NCR5380_information_transfer(instance);
719                         done = 0;
720                 }
721                 spin_unlock_irq(&hostdata->lock);
722                 if (!done)
723                         cond_resched();
724         } while (!done);
725 }
726
727 /*
728  * NCR5380_dma_complete - finish DMA transfer
729  * @instance: the scsi host instance
730  *
731  * Called by the interrupt handler when DMA finishes or a phase
732  * mismatch occurs (which would end the DMA transfer).
733  */
734
735 static void NCR5380_dma_complete(struct Scsi_Host *instance)
736 {
737         struct NCR5380_hostdata *hostdata = shost_priv(instance);
738         int transferred;
739         unsigned char **data;
740         int *count;
741         int saved_data = 0, overrun = 0;
742         unsigned char p;
743
744         if (hostdata->read_overruns) {
745                 p = hostdata->connected->SCp.phase;
746                 if (p & SR_IO) {
747                         udelay(10);
748                         if ((NCR5380_read(BUS_AND_STATUS_REG) &
749                              (BASR_PHASE_MATCH | BASR_ACK)) ==
750                             (BASR_PHASE_MATCH | BASR_ACK)) {
751                                 saved_data = NCR5380_read(INPUT_DATA_REG);
752                                 overrun = 1;
753                                 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
754                         }
755                 }
756         }
757
758 #ifdef CONFIG_SUN3
759         if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
760                 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
761                        instance->host_no);
762                 BUG();
763         }
764
765         if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
766             (BASR_PHASE_MATCH | BASR_ACK)) {
767                 pr_err("scsi%d: BASR %02x\n", instance->host_no,
768                        NCR5380_read(BUS_AND_STATUS_REG));
769                 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
770                        instance->host_no);
771                 BUG();
772         }
773 #endif
774
775         NCR5380_write(MODE_REG, MR_BASE);
776         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
777         NCR5380_read(RESET_PARITY_INTERRUPT_REG);
778
779         transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata);
780         hostdata->dma_len = 0;
781
782         data = (unsigned char **)&hostdata->connected->SCp.ptr;
783         count = &hostdata->connected->SCp.this_residual;
784         *data += transferred;
785         *count -= transferred;
786
787         if (hostdata->read_overruns) {
788                 int cnt, toPIO;
789
790                 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
791                         cnt = toPIO = hostdata->read_overruns;
792                         if (overrun) {
793                                 dsprintk(NDEBUG_DMA, instance,
794                                          "Got an input overrun, using saved byte\n");
795                                 *(*data)++ = saved_data;
796                                 (*count)--;
797                                 cnt--;
798                                 toPIO--;
799                         }
800                         if (toPIO > 0) {
801                                 dsprintk(NDEBUG_DMA, instance,
802                                          "Doing %d byte PIO to 0x%p\n", cnt, *data);
803                                 NCR5380_transfer_pio(instance, &p, &cnt, data);
804                                 *count -= toPIO - cnt;
805                         }
806                 }
807         }
808 }
809
810 /**
811  * NCR5380_intr - generic NCR5380 irq handler
812  * @irq: interrupt number
813  * @dev_id: device info
814  *
815  * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
816  * from the disconnected queue, and restarting NCR5380_main()
817  * as required.
818  *
819  * The chip can assert IRQ in any of six different conditions. The IRQ flag
820  * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
821  * Three of these six conditions are latched in the Bus and Status Register:
822  * - End of DMA (cleared by ending DMA Mode)
823  * - Parity error (cleared by reading RPIR)
824  * - Loss of BSY (cleared by reading RPIR)
825  * Two conditions have flag bits that are not latched:
826  * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
827  * - Bus reset (non-maskable)
828  * The remaining condition has no flag bit at all:
829  * - Selection/reselection
830  *
831  * Hence, establishing the cause(s) of any interrupt is partly guesswork.
832  * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
833  * claimed that "the design of the [DP8490] interrupt logic ensures
834  * interrupts will not be lost (they can be on the DP5380)."
835  * The L5380/53C80 datasheet from LOGIC Devices has more details.
836  *
837  * Checking for bus reset by reading RST is futile because of interrupt
838  * latency, but a bus reset will reset chip logic. Checking for parity error
839  * is unnecessary because that interrupt is never enabled. A Loss of BSY
840  * condition will clear DMA Mode. We can tell when this occurs because the
841  * the Busy Monitor interrupt is enabled together with DMA Mode.
842  */
843
844 static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
845 {
846         struct Scsi_Host *instance = dev_id;
847         struct NCR5380_hostdata *hostdata = shost_priv(instance);
848         int handled = 0;
849         unsigned char basr;
850         unsigned long flags;
851
852         spin_lock_irqsave(&hostdata->lock, flags);
853
854         basr = NCR5380_read(BUS_AND_STATUS_REG);
855         if (basr & BASR_IRQ) {
856                 unsigned char mr = NCR5380_read(MODE_REG);
857                 unsigned char sr = NCR5380_read(STATUS_REG);
858
859                 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
860                          irq, basr, sr, mr);
861
862                 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
863                         /* Probably End of DMA, Phase Mismatch or Loss of BSY.
864                          * We ack IRQ after clearing Mode Register. Workarounds
865                          * for End of DMA errata need to happen in DMA Mode.
866                          */
867
868                         dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
869
870                         if (hostdata->connected) {
871                                 NCR5380_dma_complete(instance);
872                                 queue_work(hostdata->work_q, &hostdata->main_task);
873                         } else {
874                                 NCR5380_write(MODE_REG, MR_BASE);
875                                 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
876                         }
877                 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
878                     (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
879                         /* Probably reselected */
880                         NCR5380_write(SELECT_ENABLE_REG, 0);
881                         NCR5380_read(RESET_PARITY_INTERRUPT_REG);
882
883                         dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
884
885                         if (!hostdata->connected) {
886                                 NCR5380_reselect(instance);
887                                 queue_work(hostdata->work_q, &hostdata->main_task);
888                         }
889                         if (!hostdata->connected)
890                                 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
891                 } else {
892                         /* Probably Bus Reset */
893                         NCR5380_read(RESET_PARITY_INTERRUPT_REG);
894
895                         if (sr & SR_RST) {
896                                 /* Certainly Bus Reset */
897                                 shost_printk(KERN_WARNING, instance,
898                                              "bus reset interrupt\n");
899                                 bus_reset_cleanup(instance);
900                         } else {
901                                 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
902                         }
903 #ifdef SUN3_SCSI_VME
904                         dregs->csr |= CSR_DMA_ENABLE;
905 #endif
906                 }
907                 handled = 1;
908         } else {
909                 dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n");
910 #ifdef SUN3_SCSI_VME
911                 dregs->csr |= CSR_DMA_ENABLE;
912 #endif
913         }
914
915         spin_unlock_irqrestore(&hostdata->lock, flags);
916
917         return IRQ_RETVAL(handled);
918 }
919
920 /**
921  * NCR5380_select - attempt arbitration and selection for a given command
922  * @instance: the Scsi_Host instance
923  * @cmd: the scsi_cmnd to execute
924  *
925  * This routine establishes an I_T_L nexus for a SCSI command. This involves
926  * ARBITRATION, SELECTION and MESSAGE OUT phases and an IDENTIFY message.
927  *
928  * Returns true if the operation should be retried.
929  * Returns false if it should not be retried.
930  *
931  * Side effects :
932  * If bus busy, arbitration failed, etc, NCR5380_select() will exit
933  * with registers as they should have been on entry - ie
934  * SELECT_ENABLE will be set appropriately, the NCR5380
935  * will cease to drive any SCSI bus signals.
936  *
937  * If successful : the I_T_L nexus will be established, and
938  * hostdata->connected will be set to cmd.
939  * SELECT interrupt will be disabled.
940  *
941  * If failed (no target) : cmd->scsi_done() will be called, and the
942  * cmd->result host byte set to DID_BAD_TARGET.
943  */
944
945 static bool NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
946         __releases(&hostdata->lock) __acquires(&hostdata->lock)
947 {
948         struct NCR5380_hostdata *hostdata = shost_priv(instance);
949         unsigned char tmp[3], phase;
950         unsigned char *data;
951         int len;
952         int err;
953         bool ret = true;
954         bool can_disconnect = instance->irq != NO_IRQ &&
955                               cmd->cmnd[0] != REQUEST_SENSE;
956
957         NCR5380_dprint(NDEBUG_ARBITRATION, instance);
958         dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
959                  instance->this_id);
960
961         /*
962          * Arbitration and selection phases are slow and involve dropping the
963          * lock, so we have to watch out for EH. An exception handler may
964          * change 'selecting' to NULL. This function will then return false
965          * so that the caller will forget about 'cmd'. (During information
966          * transfer phases, EH may change 'connected' to NULL.)
967          */
968         hostdata->selecting = cmd;
969
970         /*
971          * Set the phase bits to 0, otherwise the NCR5380 won't drive the
972          * data bus during SELECTION.
973          */
974
975         NCR5380_write(TARGET_COMMAND_REG, 0);
976
977         /*
978          * Start arbitration.
979          */
980
981         NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
982         NCR5380_write(MODE_REG, MR_ARBITRATE);
983
984         /* The chip now waits for BUS FREE phase. Then after the 800 ns
985          * Bus Free Delay, arbitration will begin.
986          */
987
988         spin_unlock_irq(&hostdata->lock);
989         err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0,
990                         INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
991                                                ICR_ARBITRATION_PROGRESS, HZ);
992         spin_lock_irq(&hostdata->lock);
993         if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
994                 /* Reselection interrupt */
995                 goto out;
996         }
997         if (!hostdata->selecting) {
998                 /* Command was aborted */
999                 NCR5380_write(MODE_REG, MR_BASE);
1000                 return false;
1001         }
1002         if (err < 0) {
1003                 NCR5380_write(MODE_REG, MR_BASE);
1004                 shost_printk(KERN_ERR, instance,
1005                              "select: arbitration timeout\n");
1006                 goto out;
1007         }
1008         spin_unlock_irq(&hostdata->lock);
1009
1010         /* The SCSI-2 arbitration delay is 2.4 us */
1011         udelay(3);
1012
1013         /* Check for lost arbitration */
1014         if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1015             (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1016             (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1017                 NCR5380_write(MODE_REG, MR_BASE);
1018                 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
1019                 spin_lock_irq(&hostdata->lock);
1020                 goto out;
1021         }
1022
1023         /* After/during arbitration, BSY should be asserted.
1024          * IBM DPES-31080 Version S31Q works now
1025          * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1026          */
1027         NCR5380_write(INITIATOR_COMMAND_REG,
1028                       ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
1029
1030         /*
1031          * Again, bus clear + bus settle time is 1.2us, however, this is
1032          * a minimum so we'll udelay ceil(1.2)
1033          */
1034
1035         if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1036                 udelay(15);
1037         else
1038                 udelay(2);
1039
1040         spin_lock_irq(&hostdata->lock);
1041
1042         /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1043         if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
1044                 goto out;
1045
1046         if (!hostdata->selecting) {
1047                 NCR5380_write(MODE_REG, MR_BASE);
1048                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1049                 return false;
1050         }
1051
1052         dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
1053
1054         /*
1055          * Now that we have won arbitration, start Selection process, asserting
1056          * the host and target ID's on the SCSI bus.
1057          */
1058
1059         NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
1060
1061         /*
1062          * Raise ATN while SEL is true before BSY goes false from arbitration,
1063          * since this is the only way to guarantee that we'll get a MESSAGE OUT
1064          * phase immediately after selection.
1065          */
1066
1067         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1068                       ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
1069         NCR5380_write(MODE_REG, MR_BASE);
1070
1071         /*
1072          * Reselect interrupts must be turned off prior to the dropping of BSY,
1073          * otherwise we will trigger an interrupt.
1074          */
1075         NCR5380_write(SELECT_ENABLE_REG, 0);
1076
1077         spin_unlock_irq(&hostdata->lock);
1078
1079         /*
1080          * The initiator shall then wait at least two deskew delays and release
1081          * the BSY signal.
1082          */
1083         udelay(1);        /* wingel -- wait two bus deskew delay >2*45ns */
1084
1085         /* Reset BSY */
1086         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1087                       ICR_ASSERT_ATN | ICR_ASSERT_SEL);
1088
1089         /*
1090          * Something weird happens when we cease to drive BSY - looks
1091          * like the board/chip is letting us do another read before the
1092          * appropriate propagation delay has expired, and we're confusing
1093          * a BSY signal from ourselves as the target's response to SELECTION.
1094          *
1095          * A small delay (the 'C++' frontend breaks the pipeline with an
1096          * unnecessary jump, making it work on my 386-33/Trantor T128, the
1097          * tighter 'C' code breaks and requires this) solves the problem -
1098          * the 1 us delay is arbitrary, and only used because this delay will
1099          * be the same on other platforms and since it works here, it should
1100          * work there.
1101          *
1102          * wingel suggests that this could be due to failing to wait
1103          * one deskew delay.
1104          */
1105
1106         udelay(1);
1107
1108         dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
1109
1110         /*
1111          * The SCSI specification calls for a 250 ms timeout for the actual
1112          * selection.
1113          */
1114
1115         err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY,
1116                                     msecs_to_jiffies(250));
1117
1118         if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1119                 spin_lock_irq(&hostdata->lock);
1120                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1121                 NCR5380_reselect(instance);
1122                 if (!hostdata->connected)
1123                         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1124                 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
1125                 goto out;
1126         }
1127
1128         if (err < 0) {
1129                 spin_lock_irq(&hostdata->lock);
1130                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1131                 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1132
1133                 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1134                 if (!hostdata->selecting)
1135                         return false;
1136
1137                 cmd->result = DID_BAD_TARGET << 16;
1138                 complete_cmd(instance, cmd);
1139                 dsprintk(NDEBUG_SELECTION, instance,
1140                         "target did not respond within 250ms\n");
1141                 ret = false;
1142                 goto out;
1143         }
1144
1145         /*
1146          * No less than two deskew delays after the initiator detects the
1147          * BSY signal is true, it shall release the SEL signal and may
1148          * change the DATA BUS.                                     -wingel
1149          */
1150
1151         udelay(1);
1152
1153         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1154
1155         /*
1156          * Since we followed the SCSI spec, and raised ATN while SEL
1157          * was true but before BSY was false during selection, the information
1158          * transfer phase should be a MESSAGE OUT phase so that we can send the
1159          * IDENTIFY message.
1160          */
1161
1162         /* Wait for start of REQ/ACK handshake */
1163
1164         err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
1165         spin_lock_irq(&hostdata->lock);
1166         if (err < 0) {
1167                 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1168                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1169                 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1170                 goto out;
1171         }
1172         if (!hostdata->selecting) {
1173                 do_abort(instance);
1174                 return false;
1175         }
1176
1177         dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1178                  scmd_id(cmd));
1179         tmp[0] = IDENTIFY(can_disconnect, cmd->device->lun);
1180
1181         len = 1;
1182         data = tmp;
1183         phase = PHASE_MSGOUT;
1184         NCR5380_transfer_pio(instance, &phase, &len, &data);
1185         if (len) {
1186                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1187                 cmd->result = DID_ERROR << 16;
1188                 complete_cmd(instance, cmd);
1189                 dsprintk(NDEBUG_SELECTION, instance, "IDENTIFY message transfer failed\n");
1190                 ret = false;
1191                 goto out;
1192         }
1193
1194         dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
1195
1196         hostdata->connected = cmd;
1197         hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
1198
1199 #ifdef SUN3_SCSI_VME
1200         dregs->csr |= CSR_INTR;
1201 #endif
1202
1203         initialize_SCp(cmd);
1204
1205         ret = false;
1206
1207 out:
1208         if (!hostdata->selecting)
1209                 return false;
1210         hostdata->selecting = NULL;
1211         return ret;
1212 }
1213
1214 /*
1215  * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1216  * unsigned char *phase, int *count, unsigned char **data)
1217  *
1218  * Purpose : transfers data in given phase using polled I/O
1219  *
1220  * Inputs : instance - instance of driver, *phase - pointer to
1221  * what phase is expected, *count - pointer to number of
1222  * bytes to transfer, **data - pointer to data pointer.
1223  *
1224  * Returns : -1 when different phase is entered without transferring
1225  * maximum number of bytes, 0 if all bytes are transferred or exit
1226  * is in same phase.
1227  *
1228  * Also, *phase, *count, *data are modified in place.
1229  *
1230  * XXX Note : handling for bus free may be useful.
1231  */
1232
1233 /*
1234  * Note : this code is not as quick as it could be, however it
1235  * IS 100% reliable, and for the actual data transfer where speed
1236  * counts, we will always do a pseudo DMA or DMA transfer.
1237  */
1238
1239 static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1240                                 unsigned char *phase, int *count,
1241                                 unsigned char **data)
1242 {
1243         struct NCR5380_hostdata *hostdata = shost_priv(instance);
1244         unsigned char p = *phase, tmp;
1245         int c = *count;
1246         unsigned char *d = *data;
1247
1248         /*
1249          * The NCR5380 chip will only drive the SCSI bus when the
1250          * phase specified in the appropriate bits of the TARGET COMMAND
1251          * REGISTER match the STATUS REGISTER
1252          */
1253
1254         NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1255
1256         do {
1257                 /*
1258                  * Wait for assertion of REQ, after which the phase bits will be
1259                  * valid
1260                  */
1261
1262                 if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1263                         break;
1264
1265                 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
1266
1267                 /* Check for phase mismatch */
1268                 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
1269                         dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1270                         NCR5380_dprint_phase(NDEBUG_PIO, instance);
1271                         break;
1272                 }
1273
1274                 /* Do actual transfer from SCSI bus to / from memory */
1275                 if (!(p & SR_IO))
1276                         NCR5380_write(OUTPUT_DATA_REG, *d);
1277                 else
1278                         *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1279
1280                 ++d;
1281
1282                 /*
1283                  * The SCSI standard suggests that in MSGOUT phase, the initiator
1284                  * should drop ATN on the last byte of the message phase
1285                  * after REQ has been asserted for the handshake but before
1286                  * the initiator raises ACK.
1287                  */
1288
1289                 if (!(p & SR_IO)) {
1290                         if (!((p & SR_MSG) && c > 1)) {
1291                                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1292                                 NCR5380_dprint(NDEBUG_PIO, instance);
1293                                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1294                                               ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1295                         } else {
1296                                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1297                                               ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1298                                 NCR5380_dprint(NDEBUG_PIO, instance);
1299                                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1300                                               ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1301                         }
1302                 } else {
1303                         NCR5380_dprint(NDEBUG_PIO, instance);
1304                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1305                 }
1306
1307                 if (NCR5380_poll_politely(hostdata,
1308                                           STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1309                         break;
1310
1311                 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
1312
1313 /*
1314  * We have several special cases to consider during REQ/ACK handshaking :
1315  * 1.  We were in MSGOUT phase, and we are on the last byte of the
1316  * message.  ATN must be dropped as ACK is dropped.
1317  *
1318  * 2.  We are in a MSGIN phase, and we are on the last byte of the
1319  * message.  We must exit with ACK asserted, so that the calling
1320  * code may raise ATN before dropping ACK to reject the message.
1321  *
1322  * 3.  ACK and ATN are clear and the target may proceed as normal.
1323  */
1324                 if (!(p == PHASE_MSGIN && c == 1)) {
1325                         if (p == PHASE_MSGOUT && c > 1)
1326                                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1327                         else
1328                                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1329                 }
1330         } while (--c);
1331
1332         dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
1333
1334         *count = c;
1335         *data = d;
1336         tmp = NCR5380_read(STATUS_REG);
1337         /* The phase read from the bus is valid if either REQ is (already)
1338          * asserted or if ACK hasn't been released yet. The latter applies if
1339          * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1340          */
1341         if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
1342                 *phase = tmp & PHASE_MASK;
1343         else
1344                 *phase = PHASE_UNKNOWN;
1345
1346         if (!c || (*phase == p))
1347                 return 0;
1348         else
1349                 return -1;
1350 }
1351
1352 /**
1353  * do_reset - issue a reset command
1354  * @instance: adapter to reset
1355  *
1356  * Issue a reset sequence to the NCR5380 and try and get the bus
1357  * back into sane shape.
1358  *
1359  * This clears the reset interrupt flag because there may be no handler for
1360  * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1361  * been installed. And when in EH we may have released the ST DMA interrupt.
1362  */
1363
1364 static void do_reset(struct Scsi_Host *instance)
1365 {
1366         struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
1367         unsigned long flags;
1368
1369         local_irq_save(flags);
1370         NCR5380_write(TARGET_COMMAND_REG,
1371                       PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1372         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1373         udelay(50);
1374         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1375         (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1376         local_irq_restore(flags);
1377 }
1378
1379 /**
1380  * do_abort - abort the currently established nexus by going to
1381  * MESSAGE OUT phase and sending an ABORT message.
1382  * @instance: relevant scsi host instance
1383  *
1384  * Returns 0 on success, -1 on failure.
1385  */
1386
1387 static int do_abort(struct Scsi_Host *instance)
1388 {
1389         struct NCR5380_hostdata *hostdata = shost_priv(instance);
1390         unsigned char *msgptr, phase, tmp;
1391         int len;
1392         int rc;
1393
1394         /* Request message out phase */
1395         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1396
1397         /*
1398          * Wait for the target to indicate a valid phase by asserting
1399          * REQ.  Once this happens, we'll have either a MSGOUT phase
1400          * and can immediately send the ABORT message, or we'll have some
1401          * other phase and will have to source/sink data.
1402          *
1403          * We really don't care what value was on the bus or what value
1404          * the target sees, so we just handshake.
1405          */
1406
1407         rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1408         if (rc < 0)
1409                 goto timeout;
1410
1411         tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
1412
1413         NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1414
1415         if (tmp != PHASE_MSGOUT) {
1416                 NCR5380_write(INITIATOR_COMMAND_REG,
1417                               ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1418                 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0, 3 * HZ);
1419                 if (rc < 0)
1420                         goto timeout;
1421                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1422         }
1423
1424         tmp = ABORT;
1425         msgptr = &tmp;
1426         len = 1;
1427         phase = PHASE_MSGOUT;
1428         NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
1429
1430         /*
1431          * If we got here, and the command completed successfully,
1432          * we're about to go into bus free state.
1433          */
1434
1435         return len ? -1 : 0;
1436
1437 timeout:
1438         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1439         return -1;
1440 }
1441
1442 /*
1443  * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1444  * unsigned char *phase, int *count, unsigned char **data)
1445  *
1446  * Purpose : transfers data in given phase using either real
1447  * or pseudo DMA.
1448  *
1449  * Inputs : instance - instance of driver, *phase - pointer to
1450  * what phase is expected, *count - pointer to number of
1451  * bytes to transfer, **data - pointer to data pointer.
1452  *
1453  * Returns : -1 when different phase is entered without transferring
1454  * maximum number of bytes, 0 if all bytes or transferred or exit
1455  * is in same phase.
1456  *
1457  * Also, *phase, *count, *data are modified in place.
1458  */
1459
1460
1461 static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1462                                 unsigned char *phase, int *count,
1463                                 unsigned char **data)
1464 {
1465         struct NCR5380_hostdata *hostdata = shost_priv(instance);
1466         int c = *count;
1467         unsigned char p = *phase;
1468         unsigned char *d = *data;
1469         unsigned char tmp;
1470         int result = 0;
1471
1472         if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1473                 *phase = tmp;
1474                 return -1;
1475         }
1476
1477         hostdata->connected->SCp.phase = p;
1478
1479         if (p & SR_IO) {
1480                 if (hostdata->read_overruns)
1481                         c -= hostdata->read_overruns;
1482                 else if (hostdata->flags & FLAG_DMA_FIXUP)
1483                         --c;
1484         }
1485
1486         dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1487                  (p & SR_IO) ? "receive" : "send", c, d);
1488
1489 #ifdef CONFIG_SUN3
1490         /* send start chain */
1491         sun3scsi_dma_start(c, *data);
1492 #endif
1493
1494         NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1495         NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1496                                 MR_ENABLE_EOP_INTR);
1497
1498         if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1499                 /* On the Medusa, it is a must to initialize the DMA before
1500                  * starting the NCR. This is also the cleaner way for the TT.
1501                  */
1502                 if (p & SR_IO)
1503                         result = NCR5380_dma_recv_setup(hostdata, d, c);
1504                 else
1505                         result = NCR5380_dma_send_setup(hostdata, d, c);
1506         }
1507
1508         /*
1509          * On the PAS16 at least I/O recovery delays are not needed here.
1510          * Everyone else seems to want them.
1511          */
1512
1513         if (p & SR_IO) {
1514                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1515                 NCR5380_io_delay(1);
1516                 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1517         } else {
1518                 NCR5380_io_delay(1);
1519                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1520                 NCR5380_io_delay(1);
1521                 NCR5380_write(START_DMA_SEND_REG, 0);
1522                 NCR5380_io_delay(1);
1523         }
1524
1525 #ifdef CONFIG_SUN3
1526 #ifdef SUN3_SCSI_VME
1527         dregs->csr |= CSR_DMA_ENABLE;
1528 #endif
1529         sun3_dma_active = 1;
1530 #endif
1531
1532         if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1533                 /* On the Falcon, the DMA setup must be done after the last
1534                  * NCR access, else the DMA setup gets trashed!
1535                  */
1536                 if (p & SR_IO)
1537                         result = NCR5380_dma_recv_setup(hostdata, d, c);
1538                 else
1539                         result = NCR5380_dma_send_setup(hostdata, d, c);
1540         }
1541
1542         /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1543         if (result < 0)
1544                 return result;
1545
1546         /* For real DMA, result is the byte count. DMA interrupt is expected. */
1547         if (result > 0) {
1548                 hostdata->dma_len = result;
1549                 return 0;
1550         }
1551
1552         /* The result is zero iff pseudo DMA send/receive was completed. */
1553         hostdata->dma_len = c;
1554
1555 /*
1556  * A note regarding the DMA errata workarounds for early NMOS silicon.
1557  *
1558  * For DMA sends, we want to wait until the last byte has been
1559  * transferred out over the bus before we turn off DMA mode.  Alas, there
1560  * seems to be no terribly good way of doing this on a 5380 under all
1561  * conditions.  For non-scatter-gather operations, we can wait until REQ
1562  * and ACK both go false, or until a phase mismatch occurs.  Gather-sends
1563  * are nastier, since the device will be expecting more data than we
1564  * are prepared to send it, and REQ will remain asserted.  On a 53C8[01] we
1565  * could test Last Byte Sent to assure transfer (I imagine this is precisely
1566  * why this signal was added to the newer chips) but on the older 538[01]
1567  * this signal does not exist.  The workaround for this lack is a watchdog;
1568  * we bail out of the wait-loop after a modest amount of wait-time if
1569  * the usual exit conditions are not met.  Not a terribly clean or
1570  * correct solution :-%
1571  *
1572  * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1573  * If the chip is in DMA receive mode, it will respond to a target's
1574  * REQ by latching the SCSI data into the INPUT DATA register and asserting
1575  * ACK, even if it has _already_ been notified by the DMA controller that
1576  * the current DMA transfer has completed!  If the NCR5380 is then taken
1577  * out of DMA mode, this already-acknowledged byte is lost. This is
1578  * not a problem for "one DMA transfer per READ command", because
1579  * the situation will never arise... either all of the data is DMA'ed
1580  * properly, or the target switches to MESSAGE IN phase to signal a
1581  * disconnection (either operation bringing the DMA to a clean halt).
1582  * However, in order to handle scatter-receive, we must work around the
1583  * problem.  The chosen fix is to DMA fewer bytes, then check for the
1584  * condition before taking the NCR5380 out of DMA mode.  One or two extra
1585  * bytes are transferred via PIO as necessary to fill out the original
1586  * request.
1587  */
1588
1589         if (hostdata->flags & FLAG_DMA_FIXUP) {
1590                 if (p & SR_IO) {
1591                         /*
1592                          * The workaround was to transfer fewer bytes than we
1593                          * intended to with the pseudo-DMA read function, wait for
1594                          * the chip to latch the last byte, read it, and then disable
1595                          * pseudo-DMA mode.
1596                          *
1597                          * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1598                          * REQ is deasserted when ACK is asserted, and not reasserted
1599                          * until ACK goes false.  Since the NCR5380 won't lower ACK
1600                          * until DACK is asserted, which won't happen unless we twiddle
1601                          * the DMA port or we take the NCR5380 out of DMA mode, we
1602                          * can guarantee that we won't handshake another extra
1603                          * byte.
1604                          */
1605
1606                         if (NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
1607                                                   BASR_DRQ, BASR_DRQ, HZ) < 0) {
1608                                 result = -1;
1609                                 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
1610                         }
1611                         if (NCR5380_poll_politely(hostdata, STATUS_REG,
1612                                                   SR_REQ, 0, HZ) < 0) {
1613                                 result = -1;
1614                                 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1615                         }
1616                         d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1617                 } else {
1618                         /*
1619                          * Wait for the last byte to be sent.  If REQ is being asserted for
1620                          * the byte we're interested, we'll ACK it and it will go false.
1621                          */
1622                         if (NCR5380_poll_politely2(hostdata,
1623                              BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1624                              BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1625                                 result = -1;
1626                                 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
1627                         }
1628                 }
1629         }
1630
1631         NCR5380_dma_complete(instance);
1632         return result;
1633 }
1634
1635 /*
1636  * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1637  *
1638  * Purpose : run through the various SCSI phases and do as the target
1639  * directs us to.  Operates on the currently connected command,
1640  * instance->connected.
1641  *
1642  * Inputs : instance, instance for which we are doing commands
1643  *
1644  * Side effects : SCSI things happen, the disconnected queue will be
1645  * modified if a command disconnects, *instance->connected will
1646  * change.
1647  *
1648  * XXX Note : we need to watch for bus free or a reset condition here
1649  * to recover from an unexpected bus free condition.
1650  */
1651
1652 static void NCR5380_information_transfer(struct Scsi_Host *instance)
1653         __releases(&hostdata->lock) __acquires(&hostdata->lock)
1654 {
1655         struct NCR5380_hostdata *hostdata = shost_priv(instance);
1656         unsigned char msgout = NOP;
1657         int sink = 0;
1658         int len;
1659         int transfersize;
1660         unsigned char *data;
1661         unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
1662         struct scsi_cmnd *cmd;
1663
1664 #ifdef SUN3_SCSI_VME
1665         dregs->csr |= CSR_INTR;
1666 #endif
1667
1668         while ((cmd = hostdata->connected)) {
1669                 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1670
1671                 tmp = NCR5380_read(STATUS_REG);
1672                 /* We only have a valid SCSI phase when REQ is asserted */
1673                 if (tmp & SR_REQ) {
1674                         phase = (tmp & PHASE_MASK);
1675                         if (phase != old_phase) {
1676                                 old_phase = phase;
1677                                 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1678                         }
1679 #ifdef CONFIG_SUN3
1680                         if (phase == PHASE_CMDOUT &&
1681                             sun3_dma_setup_done != cmd) {
1682                                 int count;
1683
1684                                 advance_sg_buffer(cmd);
1685
1686                                 count = sun3scsi_dma_xfer_len(hostdata, cmd);
1687
1688                                 if (count > 0) {
1689                                         if (rq_data_dir(cmd->request))
1690                                                 sun3scsi_dma_send_setup(hostdata,
1691                                                                         cmd->SCp.ptr, count);
1692                                         else
1693                                                 sun3scsi_dma_recv_setup(hostdata,
1694                                                                         cmd->SCp.ptr, count);
1695                                         sun3_dma_setup_done = cmd;
1696                                 }
1697 #ifdef SUN3_SCSI_VME
1698                                 dregs->csr |= CSR_INTR;
1699 #endif
1700                         }
1701 #endif /* CONFIG_SUN3 */
1702
1703                         if (sink && (phase != PHASE_MSGOUT)) {
1704                                 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1705
1706                                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1707                                               ICR_ASSERT_ACK);
1708                                 while (NCR5380_read(STATUS_REG) & SR_REQ)
1709                                         ;
1710                                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1711                                               ICR_ASSERT_ATN);
1712                                 sink = 0;
1713                                 continue;
1714                         }
1715
1716                         switch (phase) {
1717                         case PHASE_DATAOUT:
1718 #if (NDEBUG & NDEBUG_NO_DATAOUT)
1719                                 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
1720                                 sink = 1;
1721                                 do_abort(instance);
1722                                 cmd->result = DID_ERROR << 16;
1723                                 complete_cmd(instance, cmd);
1724                                 hostdata->connected = NULL;
1725                                 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
1726                                 return;
1727 #endif
1728                         case PHASE_DATAIN:
1729                                 /*
1730                                  * If there is no room left in the current buffer in the
1731                                  * scatter-gather list, move onto the next one.
1732                                  */
1733
1734                                 advance_sg_buffer(cmd);
1735                                 dsprintk(NDEBUG_INFORMATION, instance,
1736                                         "this residual %d, sg ents %d\n",
1737                                         cmd->SCp.this_residual,
1738                                         sg_nents(cmd->SCp.buffer));
1739
1740                                 /*
1741                                  * The preferred transfer method is going to be
1742                                  * PSEUDO-DMA for systems that are strictly PIO,
1743                                  * since we can let the hardware do the handshaking.
1744                                  *
1745                                  * For this to work, we need to know the transfersize
1746                                  * ahead of time, since the pseudo-DMA code will sit
1747                                  * in an unconditional loop.
1748                                  */
1749
1750                                 transfersize = 0;
1751                                 if (!cmd->device->borken)
1752                                         transfersize = NCR5380_dma_xfer_len(hostdata, cmd);
1753
1754                                 if (transfersize > 0) {
1755                                         len = transfersize;
1756                                         if (NCR5380_transfer_dma(instance, &phase,
1757                                             &len, (unsigned char **)&cmd->SCp.ptr)) {
1758                                                 /*
1759                                                  * If the watchdog timer fires, all future
1760                                                  * accesses to this device will use the
1761                                                  * polled-IO.
1762                                                  */
1763                                                 scmd_printk(KERN_INFO, cmd,
1764                                                         "switching to slow handshake\n");
1765                                                 cmd->device->borken = 1;
1766                                                 sink = 1;
1767                                                 do_abort(instance);
1768                                                 cmd->result = DID_ERROR << 16;
1769                                                 /* XXX - need to source or sink data here, as appropriate */
1770                                         }
1771                                 } else {
1772                                         /* Transfer a small chunk so that the
1773                                          * irq mode lock is not held too long.
1774                                          */
1775                                         transfersize = min(cmd->SCp.this_residual,
1776                                                            NCR5380_PIO_CHUNK_SIZE);
1777                                         len = transfersize;
1778                                         NCR5380_transfer_pio(instance, &phase, &len,
1779                                                              (unsigned char **)&cmd->SCp.ptr);
1780                                         cmd->SCp.this_residual -= transfersize - len;
1781                                 }
1782 #ifdef CONFIG_SUN3
1783                                 if (sun3_dma_setup_done == cmd)
1784                                         sun3_dma_setup_done = NULL;
1785 #endif
1786                                 return;
1787                         case PHASE_MSGIN:
1788                                 len = 1;
1789                                 data = &tmp;
1790                                 NCR5380_transfer_pio(instance, &phase, &len, &data);
1791                                 cmd->SCp.Message = tmp;
1792
1793                                 switch (tmp) {
1794                                 case ABORT:
1795                                 case COMMAND_COMPLETE:
1796                                         /* Accept message by clearing ACK */
1797                                         sink = 1;
1798                                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1799                                         dsprintk(NDEBUG_QUEUES, instance,
1800                                                  "COMMAND COMPLETE %p target %d lun %llu\n",
1801                                                  cmd, scmd_id(cmd), cmd->device->lun);
1802
1803                                         hostdata->connected = NULL;
1804                                         hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
1805
1806                                         cmd->result &= ~0xffff;
1807                                         cmd->result |= cmd->SCp.Status;
1808                                         cmd->result |= cmd->SCp.Message << 8;
1809
1810                                         if (cmd->cmnd[0] == REQUEST_SENSE)
1811                                                 complete_cmd(instance, cmd);
1812                                         else {
1813                                                 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1814                                                     cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1815                                                         dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1816                                                                  cmd);
1817                                                         list_add_tail(&ncmd->list,
1818                                                                       &hostdata->autosense);
1819                                                 } else
1820                                                         complete_cmd(instance, cmd);
1821                                         }
1822
1823                                         /*
1824                                          * Restore phase bits to 0 so an interrupted selection,
1825                                          * arbitration can resume.
1826                                          */
1827                                         NCR5380_write(TARGET_COMMAND_REG, 0);
1828
1829                                         /* Enable reselect interrupts */
1830                                         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1831
1832                                         maybe_release_dma_irq(instance);
1833                                         return;
1834                                 case MESSAGE_REJECT:
1835                                         /* Accept message by clearing ACK */
1836                                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1837                                         switch (hostdata->last_message) {
1838                                         case HEAD_OF_QUEUE_TAG:
1839                                         case ORDERED_QUEUE_TAG:
1840                                         case SIMPLE_QUEUE_TAG:
1841                                                 cmd->device->simple_tags = 0;
1842                                                 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
1843                                                 break;
1844                                         default:
1845                                                 break;
1846                                         }
1847                                         break;
1848                                 case DISCONNECT:
1849                                         /* Accept message by clearing ACK */
1850                                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1851                                         hostdata->connected = NULL;
1852                                         list_add(&ncmd->list, &hostdata->disconnected);
1853                                         dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1854                                                  instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1855                                                  cmd, scmd_id(cmd), cmd->device->lun);
1856
1857                                         /*
1858                                          * Restore phase bits to 0 so an interrupted selection,
1859                                          * arbitration can resume.
1860                                          */
1861                                         NCR5380_write(TARGET_COMMAND_REG, 0);
1862
1863                                         /* Enable reselect interrupts */
1864                                         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1865 #ifdef SUN3_SCSI_VME
1866                                         dregs->csr |= CSR_DMA_ENABLE;
1867 #endif
1868                                         return;
1869                                         /*
1870                                          * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
1871                                          * operation, in violation of the SCSI spec so we can safely
1872                                          * ignore SAVE/RESTORE pointers calls.
1873                                          *
1874                                          * Unfortunately, some disks violate the SCSI spec and
1875                                          * don't issue the required SAVE_POINTERS message before
1876                                          * disconnecting, and we have to break spec to remain
1877                                          * compatible.
1878                                          */
1879                                 case SAVE_POINTERS:
1880                                 case RESTORE_POINTERS:
1881                                         /* Accept message by clearing ACK */
1882                                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1883                                         break;
1884                                 case EXTENDED_MESSAGE:
1885                                         /*
1886                                          * Start the message buffer with the EXTENDED_MESSAGE
1887                                          * byte, since spi_print_msg() wants the whole thing.
1888                                          */
1889                                         extended_msg[0] = EXTENDED_MESSAGE;
1890                                         /* Accept first byte by clearing ACK */
1891                                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1892
1893                                         spin_unlock_irq(&hostdata->lock);
1894
1895                                         dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
1896
1897                                         len = 2;
1898                                         data = extended_msg + 1;
1899                                         phase = PHASE_MSGIN;
1900                                         NCR5380_transfer_pio(instance, &phase, &len, &data);
1901                                         dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1902                                                  (int)extended_msg[1],
1903                                                  (int)extended_msg[2]);
1904
1905                                         if (!len && extended_msg[1] > 0 &&
1906                                             extended_msg[1] <= sizeof(extended_msg) - 2) {
1907                                                 /* Accept third byte by clearing ACK */
1908                                                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1909                                                 len = extended_msg[1] - 1;
1910                                                 data = extended_msg + 3;
1911                                                 phase = PHASE_MSGIN;
1912
1913                                                 NCR5380_transfer_pio(instance, &phase, &len, &data);
1914                                                 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1915                                                          len);
1916
1917                                                 switch (extended_msg[2]) {
1918                                                 case EXTENDED_SDTR:
1919                                                 case EXTENDED_WDTR:
1920                                                         tmp = 0;
1921                                                 }
1922                                         } else if (len) {
1923                                                 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
1924                                                 tmp = 0;
1925                                         } else {
1926                                                 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1927                                                              extended_msg[2], extended_msg[1]);
1928                                                 tmp = 0;
1929                                         }
1930
1931                                         spin_lock_irq(&hostdata->lock);
1932                                         if (!hostdata->connected)
1933                                                 return;
1934
1935                                         /* Reject message */
1936                                         /* Fall through */
1937                                 default:
1938                                         /*
1939                                          * If we get something weird that we aren't expecting,
1940                                          * log it.
1941                                          */
1942                                         if (tmp == EXTENDED_MESSAGE)
1943                                                 scmd_printk(KERN_INFO, cmd,
1944                                                             "rejecting unknown extended message code %02x, length %d\n",
1945                                                             extended_msg[2], extended_msg[1]);
1946                                         else if (tmp)
1947                                                 scmd_printk(KERN_INFO, cmd,
1948                                                             "rejecting unknown message code %02x\n",
1949                                                             tmp);
1950
1951                                         msgout = MESSAGE_REJECT;
1952                                         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1953                                         break;
1954                                 } /* switch (tmp) */
1955                                 break;
1956                         case PHASE_MSGOUT:
1957                                 len = 1;
1958                                 data = &msgout;
1959                                 hostdata->last_message = msgout;
1960                                 NCR5380_transfer_pio(instance, &phase, &len, &data);
1961                                 if (msgout == ABORT) {
1962                                         hostdata->connected = NULL;
1963                                         hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
1964                                         cmd->result = DID_ERROR << 16;
1965                                         complete_cmd(instance, cmd);
1966                                         maybe_release_dma_irq(instance);
1967                                         NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1968                                         return;
1969                                 }
1970                                 msgout = NOP;
1971                                 break;
1972                         case PHASE_CMDOUT:
1973                                 len = cmd->cmd_len;
1974                                 data = cmd->cmnd;
1975                                 /*
1976                                  * XXX for performance reasons, on machines with a
1977                                  * PSEUDO-DMA architecture we should probably
1978                                  * use the dma transfer function.
1979                                  */
1980                                 NCR5380_transfer_pio(instance, &phase, &len, &data);
1981                                 break;
1982                         case PHASE_STATIN:
1983                                 len = 1;
1984                                 data = &tmp;
1985                                 NCR5380_transfer_pio(instance, &phase, &len, &data);
1986                                 cmd->SCp.Status = tmp;
1987                                 break;
1988                         default:
1989                                 shost_printk(KERN_ERR, instance, "unknown phase\n");
1990                                 NCR5380_dprint(NDEBUG_ANY, instance);
1991                         } /* switch(phase) */
1992                 } else {
1993                         spin_unlock_irq(&hostdata->lock);
1994                         NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
1995                         spin_lock_irq(&hostdata->lock);
1996                 }
1997         }
1998 }
1999
2000 /*
2001  * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2002  *
2003  * Purpose : does reselection, initializing the instance->connected
2004  * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2005  * nexus has been reestablished,
2006  *
2007  * Inputs : instance - this instance of the NCR5380.
2008  */
2009
2010 static void NCR5380_reselect(struct Scsi_Host *instance)
2011 {
2012         struct NCR5380_hostdata *hostdata = shost_priv(instance);
2013         unsigned char target_mask;
2014         unsigned char lun;
2015         unsigned char msg[3];
2016         struct NCR5380_cmd *ncmd;
2017         struct scsi_cmnd *tmp;
2018
2019         /*
2020          * Disable arbitration, etc. since the host adapter obviously
2021          * lost, and tell an interrupted NCR5380_select() to restart.
2022          */
2023
2024         NCR5380_write(MODE_REG, MR_BASE);
2025
2026         target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2027         if (!target_mask || target_mask & (target_mask - 1)) {
2028                 shost_printk(KERN_WARNING, instance,
2029                              "reselect: bad target_mask 0x%02x\n", target_mask);
2030                 return;
2031         }
2032
2033         /*
2034          * At this point, we have detected that our SCSI ID is on the bus,
2035          * SEL is true and BSY was false for at least one bus settle delay
2036          * (400 ns).
2037          *
2038          * We must assert BSY ourselves, until the target drops the SEL
2039          * signal.
2040          */
2041
2042         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2043         if (NCR5380_poll_politely(hostdata,
2044                                   STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2045                 shost_printk(KERN_ERR, instance, "reselect: !SEL timeout\n");
2046                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2047                 return;
2048         }
2049         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2050
2051         /*
2052          * Wait for target to go into MSGIN.
2053          */
2054
2055         if (NCR5380_poll_politely(hostdata,
2056                                   STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2057                 if ((NCR5380_read(STATUS_REG) & (SR_BSY | SR_SEL)) == 0)
2058                         /* BUS FREE phase */
2059                         return;
2060                 shost_printk(KERN_ERR, instance, "reselect: REQ timeout\n");
2061                 do_abort(instance);
2062                 return;
2063         }
2064
2065 #ifdef CONFIG_SUN3
2066         /* acknowledge toggle to MSGIN */
2067         NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2068
2069         /* peek at the byte without really hitting the bus */
2070         msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2071 #else
2072         {
2073                 int len = 1;
2074                 unsigned char *data = msg;
2075                 unsigned char phase = PHASE_MSGIN;
2076
2077                 NCR5380_transfer_pio(instance, &phase, &len, &data);
2078
2079                 if (len) {
2080                         do_abort(instance);
2081                         return;
2082                 }
2083         }
2084 #endif /* CONFIG_SUN3 */
2085
2086         if (!(msg[0] & 0x80)) {
2087                 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
2088                 spi_print_msg(msg);
2089                 printk("\n");
2090                 do_abort(instance);
2091                 return;
2092         }
2093         lun = msg[0] & 0x07;
2094
2095         /*
2096          * We need to add code for SCSI-II to track which devices have
2097          * I_T_L_Q nexuses established, and which have simple I_T_L
2098          * nexuses so we can chose to do additional data transfer.
2099          */
2100
2101         /*
2102          * Find the command corresponding to the I_T_L or I_T_L_Q  nexus we
2103          * just reestablished, and remove it from the disconnected queue.
2104          */
2105
2106         tmp = NULL;
2107         list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2108                 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2109
2110                 if (target_mask == (1 << scmd_id(cmd)) &&
2111                     lun == (u8)cmd->device->lun) {
2112                         list_del(&ncmd->list);
2113                         tmp = cmd;
2114                         break;
2115                 }
2116         }
2117
2118         if (tmp) {
2119                 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2120                          "reselect: removed %p from disconnected queue\n", tmp);
2121         } else {
2122                 int target = ffs(target_mask) - 1;
2123
2124                 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2125                              target_mask, lun);
2126                 /*
2127                  * Since we have an established nexus that we can't do anything
2128                  * with, we must abort it.
2129                  */
2130                 if (do_abort(instance) == 0)
2131                         hostdata->busy[target] &= ~(1 << lun);
2132                 return;
2133         }
2134
2135 #ifdef CONFIG_SUN3
2136         if (sun3_dma_setup_done != tmp) {
2137                 int count;
2138
2139                 advance_sg_buffer(tmp);
2140
2141                 count = sun3scsi_dma_xfer_len(hostdata, tmp);
2142
2143                 if (count > 0) {
2144                         if (rq_data_dir(tmp->request))
2145                                 sun3scsi_dma_send_setup(hostdata,
2146                                                         tmp->SCp.ptr, count);
2147                         else
2148                                 sun3scsi_dma_recv_setup(hostdata,
2149                                                         tmp->SCp.ptr, count);
2150                         sun3_dma_setup_done = tmp;
2151                 }
2152         }
2153
2154         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2155 #endif /* CONFIG_SUN3 */
2156
2157         /* Accept message by clearing ACK */
2158         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2159
2160         hostdata->connected = tmp;
2161         dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2162                  scmd_id(tmp), tmp->device->lun);
2163 }
2164
2165 /**
2166  * list_find_cmd - test for presence of a command in a linked list
2167  * @haystack: list of commands
2168  * @needle: command to search for
2169  */
2170
2171 static bool list_find_cmd(struct list_head *haystack,
2172                           struct scsi_cmnd *needle)
2173 {
2174         struct NCR5380_cmd *ncmd;
2175
2176         list_for_each_entry(ncmd, haystack, list)
2177                 if (NCR5380_to_scmd(ncmd) == needle)
2178                         return true;
2179         return false;
2180 }
2181
2182 /**
2183  * list_remove_cmd - remove a command from linked list
2184  * @haystack: list of commands
2185  * @needle: command to remove
2186  */
2187
2188 static bool list_del_cmd(struct list_head *haystack,
2189                          struct scsi_cmnd *needle)
2190 {
2191         if (list_find_cmd(haystack, needle)) {
2192                 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2193
2194                 list_del(&ncmd->list);
2195                 return true;
2196         }
2197         return false;
2198 }
2199
2200 /**
2201  * NCR5380_abort - scsi host eh_abort_handler() method
2202  * @cmd: the command to be aborted
2203  *
2204  * Try to abort a given command by removing it from queues and/or sending
2205  * the target an abort message. This may not succeed in causing a target
2206  * to abort the command. Nonetheless, the low-level driver must forget about
2207  * the command because the mid-layer reclaims it and it may be re-issued.
2208  *
2209  * The normal path taken by a command is as follows. For EH we trace this
2210  * same path to locate and abort the command.
2211  *
2212  * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2213  * [disconnected -> connected ->]...
2214  * [autosense -> connected ->] done
2215  *
2216  * If cmd was not found at all then presumably it has already been completed,
2217  * in which case return SUCCESS to try to avoid further EH measures.
2218  *
2219  * If the command has not completed yet, we must not fail to find it.
2220  * We have no option but to forget the aborted command (even if it still
2221  * lacks sense data). The mid-layer may re-issue a command that is in error
2222  * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2223  * this driver are such that a command can appear on one queue only.
2224  *
2225  * The lock protects driver data structures, but EH handlers also use it
2226  * to serialize their own execution and prevent their own re-entry.
2227  */
2228
2229 static int NCR5380_abort(struct scsi_cmnd *cmd)
2230 {
2231         struct Scsi_Host *instance = cmd->device->host;
2232         struct NCR5380_hostdata *hostdata = shost_priv(instance);
2233         unsigned long flags;
2234         int result = SUCCESS;
2235
2236         spin_lock_irqsave(&hostdata->lock, flags);
2237
2238 #if (NDEBUG & NDEBUG_ANY)
2239         scmd_printk(KERN_INFO, cmd, __func__);
2240 #endif
2241         NCR5380_dprint(NDEBUG_ANY, instance);
2242         NCR5380_dprint_phase(NDEBUG_ANY, instance);
2243
2244         if (list_del_cmd(&hostdata->unissued, cmd)) {
2245                 dsprintk(NDEBUG_ABORT, instance,
2246                          "abort: removed %p from issue queue\n", cmd);
2247                 cmd->result = DID_ABORT << 16;
2248                 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
2249                 goto out;
2250         }
2251
2252         if (hostdata->selecting == cmd) {
2253                 dsprintk(NDEBUG_ABORT, instance,
2254                          "abort: cmd %p == selecting\n", cmd);
2255                 hostdata->selecting = NULL;
2256                 cmd->result = DID_ABORT << 16;
2257                 complete_cmd(instance, cmd);
2258                 goto out;
2259         }
2260
2261         if (list_del_cmd(&hostdata->disconnected, cmd)) {
2262                 dsprintk(NDEBUG_ABORT, instance,
2263                          "abort: removed %p from disconnected list\n", cmd);
2264                 /* Can't call NCR5380_select() and send ABORT because that
2265                  * means releasing the lock. Need a bus reset.
2266                  */
2267                 set_host_byte(cmd, DID_ERROR);
2268                 complete_cmd(instance, cmd);
2269                 result = FAILED;
2270                 goto out;
2271         }
2272
2273         if (hostdata->connected == cmd) {
2274                 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2275                 hostdata->connected = NULL;
2276                 hostdata->dma_len = 0;
2277                 if (do_abort(instance)) {
2278                         set_host_byte(cmd, DID_ERROR);
2279                         complete_cmd(instance, cmd);
2280                         result = FAILED;
2281                         goto out;
2282                 }
2283                 set_host_byte(cmd, DID_ABORT);
2284                 complete_cmd(instance, cmd);
2285                 goto out;
2286         }
2287
2288         if (list_del_cmd(&hostdata->autosense, cmd)) {
2289                 dsprintk(NDEBUG_ABORT, instance,
2290                          "abort: removed %p from sense queue\n", cmd);
2291                 complete_cmd(instance, cmd);
2292         }
2293
2294 out:
2295         if (result == FAILED)
2296                 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2297         else {
2298                 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
2299                 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2300         }
2301
2302         queue_work(hostdata->work_q, &hostdata->main_task);
2303         maybe_release_dma_irq(instance);
2304         spin_unlock_irqrestore(&hostdata->lock, flags);
2305
2306         return result;
2307 }
2308
2309
2310 static void bus_reset_cleanup(struct Scsi_Host *instance)
2311 {
2312         struct NCR5380_hostdata *hostdata = shost_priv(instance);
2313         int i;
2314         struct NCR5380_cmd *ncmd;
2315
2316         /* reset NCR registers */
2317         NCR5380_write(MODE_REG, MR_BASE);
2318         NCR5380_write(TARGET_COMMAND_REG, 0);
2319         NCR5380_write(SELECT_ENABLE_REG, 0);
2320
2321         /* After the reset, there are no more connected or disconnected commands
2322          * and no busy units; so clear the low-level status here to avoid
2323          * conflicts when the mid-level code tries to wake up the affected
2324          * commands!
2325          */
2326
2327         if (hostdata->selecting) {
2328                 hostdata->selecting->result = DID_RESET << 16;
2329                 complete_cmd(instance, hostdata->selecting);
2330                 hostdata->selecting = NULL;
2331         }
2332
2333         list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2334                 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2335
2336                 set_host_byte(cmd, DID_RESET);
2337                 complete_cmd(instance, cmd);
2338         }
2339         INIT_LIST_HEAD(&hostdata->disconnected);
2340
2341         list_for_each_entry(ncmd, &hostdata->autosense, list) {
2342                 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2343
2344                 cmd->scsi_done(cmd);
2345         }
2346         INIT_LIST_HEAD(&hostdata->autosense);
2347
2348         if (hostdata->connected) {
2349                 set_host_byte(hostdata->connected, DID_RESET);
2350                 complete_cmd(instance, hostdata->connected);
2351                 hostdata->connected = NULL;
2352         }
2353
2354         for (i = 0; i < 8; ++i)
2355                 hostdata->busy[i] = 0;
2356         hostdata->dma_len = 0;
2357
2358         queue_work(hostdata->work_q, &hostdata->main_task);
2359         maybe_release_dma_irq(instance);
2360 }
2361
2362 /**
2363  * NCR5380_host_reset - reset the SCSI host
2364  * @cmd: SCSI command undergoing EH
2365  *
2366  * Returns SUCCESS
2367  */
2368
2369 static int NCR5380_host_reset(struct scsi_cmnd *cmd)
2370 {
2371         struct Scsi_Host *instance = cmd->device->host;
2372         struct NCR5380_hostdata *hostdata = shost_priv(instance);
2373         unsigned long flags;
2374         struct NCR5380_cmd *ncmd;
2375
2376         spin_lock_irqsave(&hostdata->lock, flags);
2377
2378 #if (NDEBUG & NDEBUG_ANY)
2379         shost_printk(KERN_INFO, instance, __func__);
2380 #endif
2381         NCR5380_dprint(NDEBUG_ANY, instance);
2382         NCR5380_dprint_phase(NDEBUG_ANY, instance);
2383
2384         list_for_each_entry(ncmd, &hostdata->unissued, list) {
2385                 struct scsi_cmnd *scmd = NCR5380_to_scmd(ncmd);
2386
2387                 scmd->result = DID_RESET << 16;
2388                 scmd->scsi_done(scmd);
2389         }
2390         INIT_LIST_HEAD(&hostdata->unissued);
2391
2392         do_reset(instance);
2393         bus_reset_cleanup(instance);
2394
2395         spin_unlock_irqrestore(&hostdata->lock, flags);
2396
2397         return SUCCESS;
2398 }