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