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