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