Pull acpi_os_free into release branch
[linux-2.6] / drivers / scsi / wd33c93.c
1 /*
2  * Copyright (c) 1996 John Shifflett, GeoLog Consulting
3  *    john@geolog.com
4  *    jshiffle@netcom.com
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 /*
18  * Drew Eckhardt's excellent 'Generic NCR5380' sources from Linux-PC
19  * provided much of the inspiration and some of the code for this
20  * driver. Everything I know about Amiga DMA was gleaned from careful
21  * reading of Hamish Mcdonald's original wd33c93 driver; in fact, I
22  * borrowed shamelessly from all over that source. Thanks Hamish!
23  *
24  * _This_ driver is (I feel) an improvement over the old one in
25  * several respects:
26  *
27  *    -  Target Disconnection/Reconnection  is now supported. Any
28  *          system with more than one device active on the SCSI bus
29  *          will benefit from this. The driver defaults to what I
30  *          call 'adaptive disconnect' - meaning that each command
31  *          is evaluated individually as to whether or not it should
32  *          be run with the option to disconnect/reselect (if the
33  *          device chooses), or as a "SCSI-bus-hog".
34  *
35  *    -  Synchronous data transfers are now supported. Because of
36  *          a few devices that choke after telling the driver that
37  *          they can do sync transfers, we don't automatically use
38  *          this faster protocol - it can be enabled via the command-
39  *          line on a device-by-device basis.
40  *
41  *    -  Runtime operating parameters can now be specified through
42  *       the 'amiboot' or the 'insmod' command line. For amiboot do:
43  *          "amiboot [usual stuff] wd33c93=blah,blah,blah"
44  *       The defaults should be good for most people. See the comment
45  *       for 'setup_strings' below for more details.
46  *
47  *    -  The old driver relied exclusively on what the Western Digital
48  *          docs call "Combination Level 2 Commands", which are a great
49  *          idea in that the CPU is relieved of a lot of interrupt
50  *          overhead. However, by accepting a certain (user-settable)
51  *          amount of additional interrupts, this driver achieves
52  *          better control over the SCSI bus, and data transfers are
53  *          almost as fast while being much easier to define, track,
54  *          and debug.
55  *
56  *
57  * TODO:
58  *       more speed. linked commands.
59  *
60  *
61  * People with bug reports, wish-lists, complaints, comments,
62  * or improvements are asked to pah-leeez email me (John Shifflett)
63  * at john@geolog.com or jshiffle@netcom.com! I'm anxious to get
64  * this thing into as good a shape as possible, and I'm positive
65  * there are lots of lurking bugs and "Stupid Places".
66  *
67  * Updates:
68  *
69  * Added support for pre -A chips, which don't have advanced features
70  * and will generate CSR_RESEL rather than CSR_RESEL_AM.
71  *      Richard Hirst <richard@sleepie.demon.co.uk>  August 2000
72  */
73
74 #include <linux/module.h>
75
76 #include <linux/sched.h>
77 #include <linux/string.h>
78 #include <linux/delay.h>
79 #include <linux/init.h>
80 #include <linux/interrupt.h>
81 #include <linux/blkdev.h>
82
83 #include <scsi/scsi.h>
84 #include <scsi/scsi_cmnd.h>
85 #include <scsi/scsi_device.h>
86 #include <scsi/scsi_host.h>
87
88 #include "wd33c93.h"
89
90
91 #define WD33C93_VERSION    "1.26"
92 #define WD33C93_DATE       "22/Feb/2003"
93
94 MODULE_AUTHOR("John Shifflett");
95 MODULE_DESCRIPTION("Generic WD33C93 SCSI driver");
96 MODULE_LICENSE("GPL");
97
98 /*
99  * 'setup_strings' is a single string used to pass operating parameters and
100  * settings from the kernel/module command-line to the driver. 'setup_args[]'
101  * is an array of strings that define the compile-time default values for
102  * these settings. If Linux boots with an amiboot or insmod command-line,
103  * those settings are combined with 'setup_args[]'. Note that amiboot
104  * command-lines are prefixed with "wd33c93=" while insmod uses a
105  * "setup_strings=" prefix. The driver recognizes the following keywords
106  * (lower case required) and arguments:
107  *
108  * -  nosync:bitmask -bitmask is a byte where the 1st 7 bits correspond with
109  *                    the 7 possible SCSI devices. Set a bit to negotiate for
110  *                    asynchronous transfers on that device. To maintain
111  *                    backwards compatibility, a command-line such as
112  *                    "wd33c93=255" will be automatically translated to
113  *                    "wd33c93=nosync:0xff".
114  * -  nodma:x        -x = 1 to disable DMA, x = 0 to enable it. Argument is
115  *                    optional - if not present, same as "nodma:1".
116  * -  period:ns      -ns is the minimum # of nanoseconds in a SCSI data transfer
117  *                    period. Default is 500; acceptable values are 250 - 1000.
118  * -  disconnect:x   -x = 0 to never allow disconnects, 2 to always allow them.
119  *                    x = 1 does 'adaptive' disconnects, which is the default
120  *                    and generally the best choice.
121  * -  debug:x        -If 'DEBUGGING_ON' is defined, x is a bit mask that causes
122  *                    various types of debug output to printed - see the DB_xxx
123  *                    defines in wd33c93.h
124  * -  clock:x        -x = clock input in MHz for WD33c93 chip. Normal values
125  *                    would be from 8 through 20. Default is 8.
126  * -  next           -No argument. Used to separate blocks of keywords when
127  *                    there's more than one host adapter in the system.
128  *
129  * Syntax Notes:
130  * -  Numeric arguments can be decimal or the '0x' form of hex notation. There
131  *    _must_ be a colon between a keyword and its numeric argument, with no
132  *    spaces.
133  * -  Keywords are separated by commas, no spaces, in the standard kernel
134  *    command-line manner.
135  * -  A keyword in the 'nth' comma-separated command-line member will overwrite
136  *    the 'nth' element of setup_args[]. A blank command-line member (in
137  *    other words, a comma with no preceding keyword) will _not_ overwrite
138  *    the corresponding setup_args[] element.
139  * -  If a keyword is used more than once, the first one applies to the first
140  *    SCSI host found, the second to the second card, etc, unless the 'next'
141  *    keyword is used to change the order.
142  *
143  * Some amiboot examples (for insmod, use 'setup_strings' instead of 'wd33c93'):
144  * -  wd33c93=nosync:255
145  * -  wd33c93=nodma
146  * -  wd33c93=nodma:1
147  * -  wd33c93=disconnect:2,nosync:0x08,period:250
148  * -  wd33c93=debug:0x1c
149  */
150
151 /* Normally, no defaults are specified */
152 static char *setup_args[] = { "", "", "", "", "", "", "", "", "" };
153
154 static char *setup_strings;
155 module_param(setup_strings, charp, 0);
156
157 static void wd33c93_execute(struct Scsi_Host *instance);
158
159 #ifdef CONFIG_WD33C93_PIO
160 static inline uchar
161 read_wd33c93(const wd33c93_regs regs, uchar reg_num)
162 {
163         uchar data;
164
165         outb(reg_num, regs.SASR);
166         data = inb(regs.SCMD);
167         return data;
168 }
169
170 static inline unsigned long
171 read_wd33c93_count(const wd33c93_regs regs)
172 {
173         unsigned long value;
174
175         outb(WD_TRANSFER_COUNT_MSB, regs.SASR);
176         value = inb(regs.SCMD) << 16;
177         value |= inb(regs.SCMD) << 8;
178         value |= inb(regs.SCMD);
179         return value;
180 }
181
182 static inline uchar
183 read_aux_stat(const wd33c93_regs regs)
184 {
185         return inb(regs.SASR);
186 }
187
188 static inline void
189 write_wd33c93(const wd33c93_regs regs, uchar reg_num, uchar value)
190 {
191       outb(reg_num, regs.SASR);
192       outb(value, regs.SCMD);
193 }
194
195 static inline void
196 write_wd33c93_count(const wd33c93_regs regs, unsigned long value)
197 {
198         outb(WD_TRANSFER_COUNT_MSB, regs.SASR);
199         outb((value >> 16) & 0xff, regs.SCMD);
200         outb((value >> 8) & 0xff, regs.SCMD);
201         outb( value & 0xff, regs.SCMD);
202 }
203
204 #define write_wd33c93_cmd(regs, cmd) \
205         write_wd33c93((regs), WD_COMMAND, (cmd))
206
207 static inline void
208 write_wd33c93_cdb(const wd33c93_regs regs, uint len, uchar cmnd[])
209 {
210         int i;
211
212         outb(WD_CDB_1, regs.SASR);
213         for (i=0; i<len; i++)
214                 outb(cmnd[i], regs.SCMD);
215 }
216
217 #else /* CONFIG_WD33C93_PIO */
218 static inline uchar
219 read_wd33c93(const wd33c93_regs regs, uchar reg_num)
220 {
221         *regs.SASR = reg_num;
222         mb();
223         return (*regs.SCMD);
224 }
225
226 static unsigned long
227 read_wd33c93_count(const wd33c93_regs regs)
228 {
229         unsigned long value;
230
231         *regs.SASR = WD_TRANSFER_COUNT_MSB;
232         mb();
233         value = *regs.SCMD << 16;
234         value |= *regs.SCMD << 8;
235         value |= *regs.SCMD;
236         mb();
237         return value;
238 }
239
240 static inline uchar
241 read_aux_stat(const wd33c93_regs regs)
242 {
243         return *regs.SASR;
244 }
245
246 static inline void
247 write_wd33c93(const wd33c93_regs regs, uchar reg_num, uchar value)
248 {
249         *regs.SASR = reg_num;
250         mb();
251         *regs.SCMD = value;
252         mb();
253 }
254
255 static void
256 write_wd33c93_count(const wd33c93_regs regs, unsigned long value)
257 {
258         *regs.SASR = WD_TRANSFER_COUNT_MSB;
259         mb();
260         *regs.SCMD = value >> 16;
261         *regs.SCMD = value >> 8;
262         *regs.SCMD = value;
263         mb();
264 }
265
266 static inline void
267 write_wd33c93_cmd(const wd33c93_regs regs, uchar cmd)
268 {
269         *regs.SASR = WD_COMMAND;
270         mb();
271         *regs.SCMD = cmd;
272         mb();
273 }
274
275 static inline void
276 write_wd33c93_cdb(const wd33c93_regs regs, uint len, uchar cmnd[])
277 {
278         int i;
279
280         *regs.SASR = WD_CDB_1;
281         for (i = 0; i < len; i++)
282                 *regs.SCMD = cmnd[i];
283 }
284 #endif /* CONFIG_WD33C93_PIO */
285
286 static inline uchar
287 read_1_byte(const wd33c93_regs regs)
288 {
289         uchar asr;
290         uchar x = 0;
291
292         write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
293         write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO | 0x80);
294         do {
295                 asr = read_aux_stat(regs);
296                 if (asr & ASR_DBR)
297                         x = read_wd33c93(regs, WD_DATA);
298         } while (!(asr & ASR_INT));
299         return x;
300 }
301
302 static struct sx_period sx_table[] = {
303         {1, 0x20},
304         {252, 0x20},
305         {376, 0x30},
306         {500, 0x40},
307         {624, 0x50},
308         {752, 0x60},
309         {876, 0x70},
310         {1000, 0x00},
311         {0, 0}
312 };
313
314 static int
315 round_period(unsigned int period)
316 {
317         int x;
318
319         for (x = 1; sx_table[x].period_ns; x++) {
320                 if ((period <= sx_table[x - 0].period_ns) &&
321                     (period > sx_table[x - 1].period_ns)) {
322                         return x;
323                 }
324         }
325         return 7;
326 }
327
328 static uchar
329 calc_sync_xfer(unsigned int period, unsigned int offset)
330 {
331         uchar result;
332
333         period *= 4;            /* convert SDTR code to ns */
334         result = sx_table[round_period(period)].reg_value;
335         result |= (offset < OPTIMUM_SX_OFF) ? offset : OPTIMUM_SX_OFF;
336         return result;
337 }
338
339 int
340 wd33c93_queuecommand(struct scsi_cmnd *cmd,
341                 void (*done)(struct scsi_cmnd *))
342 {
343         struct WD33C93_hostdata *hostdata;
344         struct scsi_cmnd *tmp;
345
346         hostdata = (struct WD33C93_hostdata *) cmd->device->host->hostdata;
347
348         DB(DB_QUEUE_COMMAND,
349            printk("Q-%d-%02x-%ld( ", cmd->device->id, cmd->cmnd[0], cmd->pid))
350
351 /* Set up a few fields in the scsi_cmnd structure for our own use:
352  *  - host_scribble is the pointer to the next cmd in the input queue
353  *  - scsi_done points to the routine we call when a cmd is finished
354  *  - result is what you'd expect
355  */
356         cmd->host_scribble = NULL;
357         cmd->scsi_done = done;
358         cmd->result = 0;
359
360 /* We use the Scsi_Pointer structure that's included with each command
361  * as a scratchpad (as it's intended to be used!). The handy thing about
362  * the SCp.xxx fields is that they're always associated with a given
363  * cmd, and are preserved across disconnect-reselect. This means we
364  * can pretty much ignore SAVE_POINTERS and RESTORE_POINTERS messages
365  * if we keep all the critical pointers and counters in SCp:
366  *  - SCp.ptr is the pointer into the RAM buffer
367  *  - SCp.this_residual is the size of that buffer
368  *  - SCp.buffer points to the current scatter-gather buffer
369  *  - SCp.buffers_residual tells us how many S.G. buffers there are
370  *  - SCp.have_data_in is not used
371  *  - SCp.sent_command is not used
372  *  - SCp.phase records this command's SRCID_ER bit setting
373  */
374
375         if (cmd->use_sg) {
376                 cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
377                 cmd->SCp.buffers_residual = cmd->use_sg - 1;
378                 cmd->SCp.ptr = page_address(cmd->SCp.buffer->page) +
379                     cmd->SCp.buffer->offset;
380                 cmd->SCp.this_residual = cmd->SCp.buffer->length;
381         } else {
382                 cmd->SCp.buffer = NULL;
383                 cmd->SCp.buffers_residual = 0;
384                 cmd->SCp.ptr = (char *) cmd->request_buffer;
385                 cmd->SCp.this_residual = cmd->request_bufflen;
386         }
387
388 /* WD docs state that at the conclusion of a "LEVEL2" command, the
389  * status byte can be retrieved from the LUN register. Apparently,
390  * this is the case only for *uninterrupted* LEVEL2 commands! If
391  * there are any unexpected phases entered, even if they are 100%
392  * legal (different devices may choose to do things differently),
393  * the LEVEL2 command sequence is exited. This often occurs prior
394  * to receiving the status byte, in which case the driver does a
395  * status phase interrupt and gets the status byte on its own.
396  * While such a command can then be "resumed" (ie restarted to
397  * finish up as a LEVEL2 command), the LUN register will NOT be
398  * a valid status byte at the command's conclusion, and we must
399  * use the byte obtained during the earlier interrupt. Here, we
400  * preset SCp.Status to an illegal value (0xff) so that when
401  * this command finally completes, we can tell where the actual
402  * status byte is stored.
403  */
404
405         cmd->SCp.Status = ILLEGAL_STATUS_BYTE;
406
407         /*
408          * Add the cmd to the end of 'input_Q'. Note that REQUEST SENSE
409          * commands are added to the head of the queue so that the desired
410          * sense data is not lost before REQUEST_SENSE executes.
411          */
412
413         spin_lock_irq(&hostdata->lock);
414
415         if (!(hostdata->input_Q) || (cmd->cmnd[0] == REQUEST_SENSE)) {
416                 cmd->host_scribble = (uchar *) hostdata->input_Q;
417                 hostdata->input_Q = cmd;
418         } else {                /* find the end of the queue */
419                 for (tmp = (struct scsi_cmnd *) hostdata->input_Q;
420                      tmp->host_scribble;
421                      tmp = (struct scsi_cmnd *) tmp->host_scribble) ;
422                 tmp->host_scribble = (uchar *) cmd;
423         }
424
425 /* We know that there's at least one command in 'input_Q' now.
426  * Go see if any of them are runnable!
427  */
428
429         wd33c93_execute(cmd->device->host);
430
431         DB(DB_QUEUE_COMMAND, printk(")Q-%ld ", cmd->pid))
432
433         spin_unlock_irq(&hostdata->lock);
434         return 0;
435 }
436
437 /*
438  * This routine attempts to start a scsi command. If the host_card is
439  * already connected, we give up immediately. Otherwise, look through
440  * the input_Q, using the first command we find that's intended
441  * for a currently non-busy target/lun.
442  *
443  * wd33c93_execute() is always called with interrupts disabled or from
444  * the wd33c93_intr itself, which means that a wd33c93 interrupt
445  * cannot occur while we are in here.
446  */
447 static void
448 wd33c93_execute(struct Scsi_Host *instance)
449 {
450         struct WD33C93_hostdata *hostdata =
451             (struct WD33C93_hostdata *) instance->hostdata;
452         const wd33c93_regs regs = hostdata->regs;
453         struct scsi_cmnd *cmd, *prev;
454
455         DB(DB_EXECUTE, printk("EX("))
456         if (hostdata->selecting || hostdata->connected) {
457                 DB(DB_EXECUTE, printk(")EX-0 "))
458                 return;
459         }
460
461         /*
462          * Search through the input_Q for a command destined
463          * for an idle target/lun.
464          */
465
466         cmd = (struct scsi_cmnd *) hostdata->input_Q;
467         prev = NULL;
468         while (cmd) {
469                 if (!(hostdata->busy[cmd->device->id] & (1 << cmd->device->lun)))
470                         break;
471                 prev = cmd;
472                 cmd = (struct scsi_cmnd *) cmd->host_scribble;
473         }
474
475         /* quit if queue empty or all possible targets are busy */
476
477         if (!cmd) {
478                 DB(DB_EXECUTE, printk(")EX-1 "))
479                 return;
480         }
481
482         /*  remove command from queue */
483
484         if (prev)
485                 prev->host_scribble = cmd->host_scribble;
486         else
487                 hostdata->input_Q = (struct scsi_cmnd *) cmd->host_scribble;
488
489 #ifdef PROC_STATISTICS
490         hostdata->cmd_cnt[cmd->device->id]++;
491 #endif
492
493         /*
494          * Start the selection process
495          */
496
497         if (cmd->sc_data_direction == DMA_TO_DEVICE)
498                 write_wd33c93(regs, WD_DESTINATION_ID, cmd->device->id);
499         else
500                 write_wd33c93(regs, WD_DESTINATION_ID, cmd->device->id | DSTID_DPD);
501
502 /* Now we need to figure out whether or not this command is a good
503  * candidate for disconnect/reselect. We guess to the best of our
504  * ability, based on a set of hierarchical rules. When several
505  * devices are operating simultaneously, disconnects are usually
506  * an advantage. In a single device system, or if only 1 device
507  * is being accessed, transfers usually go faster if disconnects
508  * are not allowed:
509  *
510  * + Commands should NEVER disconnect if hostdata->disconnect =
511  *   DIS_NEVER (this holds for tape drives also), and ALWAYS
512  *   disconnect if hostdata->disconnect = DIS_ALWAYS.
513  * + Tape drive commands should always be allowed to disconnect.
514  * + Disconnect should be allowed if disconnected_Q isn't empty.
515  * + Commands should NOT disconnect if input_Q is empty.
516  * + Disconnect should be allowed if there are commands in input_Q
517  *   for a different target/lun. In this case, the other commands
518  *   should be made disconnect-able, if not already.
519  *
520  * I know, I know - this code would flunk me out of any
521  * "C Programming 101" class ever offered. But it's easy
522  * to change around and experiment with for now.
523  */
524
525         cmd->SCp.phase = 0;     /* assume no disconnect */
526         if (hostdata->disconnect == DIS_NEVER)
527                 goto no;
528         if (hostdata->disconnect == DIS_ALWAYS)
529                 goto yes;
530         if (cmd->device->type == 1)     /* tape drive? */
531                 goto yes;
532         if (hostdata->disconnected_Q)   /* other commands disconnected? */
533                 goto yes;
534         if (!(hostdata->input_Q))       /* input_Q empty? */
535                 goto no;
536         for (prev = (struct scsi_cmnd *) hostdata->input_Q; prev;
537              prev = (struct scsi_cmnd *) prev->host_scribble) {
538                 if ((prev->device->id != cmd->device->id) ||
539                     (prev->device->lun != cmd->device->lun)) {
540                         for (prev = (struct scsi_cmnd *) hostdata->input_Q; prev;
541                              prev = (struct scsi_cmnd *) prev->host_scribble)
542                                 prev->SCp.phase = 1;
543                         goto yes;
544                 }
545         }
546
547         goto no;
548
549  yes:
550         cmd->SCp.phase = 1;
551
552 #ifdef PROC_STATISTICS
553         hostdata->disc_allowed_cnt[cmd->device->id]++;
554 #endif
555
556  no:
557
558         write_wd33c93(regs, WD_SOURCE_ID, ((cmd->SCp.phase) ? SRCID_ER : 0));
559
560         write_wd33c93(regs, WD_TARGET_LUN, cmd->device->lun);
561         write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,
562                       hostdata->sync_xfer[cmd->device->id]);
563         hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
564
565         if ((hostdata->level2 == L2_NONE) ||
566             (hostdata->sync_stat[cmd->device->id] == SS_UNSET)) {
567
568                 /*
569                  * Do a 'Select-With-ATN' command. This will end with
570                  * one of the following interrupts:
571                  *    CSR_RESEL_AM:  failure - can try again later.
572                  *    CSR_TIMEOUT:   failure - give up.
573                  *    CSR_SELECT:    success - proceed.
574                  */
575
576                 hostdata->selecting = cmd;
577
578 /* Every target has its own synchronous transfer setting, kept in the
579  * sync_xfer array, and a corresponding status byte in sync_stat[].
580  * Each target's sync_stat[] entry is initialized to SX_UNSET, and its
581  * sync_xfer[] entry is initialized to the default/safe value. SS_UNSET
582  * means that the parameters are undetermined as yet, and that we
583  * need to send an SDTR message to this device after selection is
584  * complete: We set SS_FIRST to tell the interrupt routine to do so.
585  * If we've been asked not to try synchronous transfers on this
586  * target (and _all_ luns within it), we'll still send the SDTR message
587  * later, but at that time we'll negotiate for async by specifying a
588  * sync fifo depth of 0.
589  */
590                 if (hostdata->sync_stat[cmd->device->id] == SS_UNSET)
591                         hostdata->sync_stat[cmd->device->id] = SS_FIRST;
592                 hostdata->state = S_SELECTING;
593                 write_wd33c93_count(regs, 0);   /* guarantee a DATA_PHASE interrupt */
594                 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN);
595         } else {
596
597                 /*
598                  * Do a 'Select-With-ATN-Xfer' command. This will end with
599                  * one of the following interrupts:
600                  *    CSR_RESEL_AM:  failure - can try again later.
601                  *    CSR_TIMEOUT:   failure - give up.
602                  *    anything else: success - proceed.
603                  */
604
605                 hostdata->connected = cmd;
606                 write_wd33c93(regs, WD_COMMAND_PHASE, 0);
607
608                 /* copy command_descriptor_block into WD chip
609                  * (take advantage of auto-incrementing)
610                  */
611
612                 write_wd33c93_cdb(regs, cmd->cmd_len, cmd->cmnd);
613
614                 /* The wd33c93 only knows about Group 0, 1, and 5 commands when
615                  * it's doing a 'select-and-transfer'. To be safe, we write the
616                  * size of the CDB into the OWN_ID register for every case. This
617                  * way there won't be problems with vendor-unique, audio, etc.
618                  */
619
620                 write_wd33c93(regs, WD_OWN_ID, cmd->cmd_len);
621
622                 /* When doing a non-disconnect command with DMA, we can save
623                  * ourselves a DATA phase interrupt later by setting everything
624                  * up ahead of time.
625                  */
626
627                 if ((cmd->SCp.phase == 0) && (hostdata->no_dma == 0)) {
628                         if (hostdata->dma_setup(cmd,
629                             (cmd->sc_data_direction == DMA_TO_DEVICE) ?
630                              DATA_OUT_DIR : DATA_IN_DIR))
631                                 write_wd33c93_count(regs, 0);   /* guarantee a DATA_PHASE interrupt */
632                         else {
633                                 write_wd33c93_count(regs,
634                                                     cmd->SCp.this_residual);
635                                 write_wd33c93(regs, WD_CONTROL,
636                                               CTRL_IDI | CTRL_EDI | CTRL_DMA);
637                                 hostdata->dma = D_DMA_RUNNING;
638                         }
639                 } else
640                         write_wd33c93_count(regs, 0);   /* guarantee a DATA_PHASE interrupt */
641
642                 hostdata->state = S_RUNNING_LEVEL2;
643                 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
644         }
645
646         /*
647          * Since the SCSI bus can handle only 1 connection at a time,
648          * we get out of here now. If the selection fails, or when
649          * the command disconnects, we'll come back to this routine
650          * to search the input_Q again...
651          */
652
653         DB(DB_EXECUTE,
654            printk("%s%ld)EX-2 ", (cmd->SCp.phase) ? "d:" : "", cmd->pid))
655 }
656
657 static void
658 transfer_pio(const wd33c93_regs regs, uchar * buf, int cnt,
659              int data_in_dir, struct WD33C93_hostdata *hostdata)
660 {
661         uchar asr;
662
663         DB(DB_TRANSFER,
664            printk("(%p,%d,%s:", buf, cnt, data_in_dir ? "in" : "out"))
665
666         write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
667         write_wd33c93_count(regs, cnt);
668         write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO);
669         if (data_in_dir) {
670                 do {
671                         asr = read_aux_stat(regs);
672                         if (asr & ASR_DBR)
673                                 *buf++ = read_wd33c93(regs, WD_DATA);
674                 } while (!(asr & ASR_INT));
675         } else {
676                 do {
677                         asr = read_aux_stat(regs);
678                         if (asr & ASR_DBR)
679                                 write_wd33c93(regs, WD_DATA, *buf++);
680                 } while (!(asr & ASR_INT));
681         }
682
683         /* Note: we are returning with the interrupt UN-cleared.
684          * Since (presumably) an entire I/O operation has
685          * completed, the bus phase is probably different, and
686          * the interrupt routine will discover this when it
687          * responds to the uncleared int.
688          */
689
690 }
691
692 static void
693 transfer_bytes(const wd33c93_regs regs, struct scsi_cmnd *cmd,
694                 int data_in_dir)
695 {
696         struct WD33C93_hostdata *hostdata;
697         unsigned long length;
698
699         hostdata = (struct WD33C93_hostdata *) cmd->device->host->hostdata;
700
701 /* Normally, you'd expect 'this_residual' to be non-zero here.
702  * In a series of scatter-gather transfers, however, this
703  * routine will usually be called with 'this_residual' equal
704  * to 0 and 'buffers_residual' non-zero. This means that a
705  * previous transfer completed, clearing 'this_residual', and
706  * now we need to setup the next scatter-gather buffer as the
707  * source or destination for THIS transfer.
708  */
709         if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
710                 ++cmd->SCp.buffer;
711                 --cmd->SCp.buffers_residual;
712                 cmd->SCp.this_residual = cmd->SCp.buffer->length;
713                 cmd->SCp.ptr = page_address(cmd->SCp.buffer->page) +
714                     cmd->SCp.buffer->offset;
715         }
716
717         write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,
718                       hostdata->sync_xfer[cmd->device->id]);
719
720 /* 'hostdata->no_dma' is TRUE if we don't even want to try DMA.
721  * Update 'this_residual' and 'ptr' after 'transfer_pio()' returns.
722  */
723
724         if (hostdata->no_dma || hostdata->dma_setup(cmd, data_in_dir)) {
725 #ifdef PROC_STATISTICS
726                 hostdata->pio_cnt++;
727 #endif
728                 transfer_pio(regs, (uchar *) cmd->SCp.ptr,
729                              cmd->SCp.this_residual, data_in_dir, hostdata);
730                 length = cmd->SCp.this_residual;
731                 cmd->SCp.this_residual = read_wd33c93_count(regs);
732                 cmd->SCp.ptr += (length - cmd->SCp.this_residual);
733         }
734
735 /* We are able to do DMA (in fact, the Amiga hardware is
736  * already going!), so start up the wd33c93 in DMA mode.
737  * We set 'hostdata->dma' = D_DMA_RUNNING so that when the
738  * transfer completes and causes an interrupt, we're
739  * reminded to tell the Amiga to shut down its end. We'll
740  * postpone the updating of 'this_residual' and 'ptr'
741  * until then.
742  */
743
744         else {
745 #ifdef PROC_STATISTICS
746                 hostdata->dma_cnt++;
747 #endif
748                 write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_DMA);
749                 write_wd33c93_count(regs, cmd->SCp.this_residual);
750
751                 if ((hostdata->level2 >= L2_DATA) ||
752                     (hostdata->level2 == L2_BASIC && cmd->SCp.phase == 0)) {
753                         write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
754                         write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
755                         hostdata->state = S_RUNNING_LEVEL2;
756                 } else
757                         write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO);
758
759                 hostdata->dma = D_DMA_RUNNING;
760         }
761 }
762
763 void
764 wd33c93_intr(struct Scsi_Host *instance)
765 {
766         struct WD33C93_hostdata *hostdata =
767             (struct WD33C93_hostdata *) instance->hostdata;
768         const wd33c93_regs regs = hostdata->regs;
769         struct scsi_cmnd *patch, *cmd;
770         uchar asr, sr, phs, id, lun, *ucp, msg;
771         unsigned long length, flags;
772
773         asr = read_aux_stat(regs);
774         if (!(asr & ASR_INT) || (asr & ASR_BSY))
775                 return;
776
777         spin_lock_irqsave(&hostdata->lock, flags);
778
779 #ifdef PROC_STATISTICS
780         hostdata->int_cnt++;
781 #endif
782
783         cmd = (struct scsi_cmnd *) hostdata->connected; /* assume we're connected */
784         sr = read_wd33c93(regs, WD_SCSI_STATUS);        /* clear the interrupt */
785         phs = read_wd33c93(regs, WD_COMMAND_PHASE);
786
787         DB(DB_INTR, printk("{%02x:%02x-", asr, sr))
788
789 /* After starting a DMA transfer, the next interrupt
790  * is guaranteed to be in response to completion of
791  * the transfer. Since the Amiga DMA hardware runs in
792  * in an open-ended fashion, it needs to be told when
793  * to stop; do that here if D_DMA_RUNNING is true.
794  * Also, we have to update 'this_residual' and 'ptr'
795  * based on the contents of the TRANSFER_COUNT register,
796  * in case the device decided to do an intermediate
797  * disconnect (a device may do this if it has to do a
798  * seek, or just to be nice and let other devices have
799  * some bus time during long transfers). After doing
800  * whatever is needed, we go on and service the WD3393
801  * interrupt normally.
802  */
803             if (hostdata->dma == D_DMA_RUNNING) {
804                 DB(DB_TRANSFER,
805                    printk("[%p/%d:", cmd->SCp.ptr, cmd->SCp.this_residual))
806                     hostdata->dma_stop(cmd->device->host, cmd, 1);
807                 hostdata->dma = D_DMA_OFF;
808                 length = cmd->SCp.this_residual;
809                 cmd->SCp.this_residual = read_wd33c93_count(regs);
810                 cmd->SCp.ptr += (length - cmd->SCp.this_residual);
811                 DB(DB_TRANSFER,
812                    printk("%p/%d]", cmd->SCp.ptr, cmd->SCp.this_residual))
813         }
814
815 /* Respond to the specific WD3393 interrupt - there are quite a few! */
816         switch (sr) {
817         case CSR_TIMEOUT:
818                 DB(DB_INTR, printk("TIMEOUT"))
819
820                     if (hostdata->state == S_RUNNING_LEVEL2)
821                         hostdata->connected = NULL;
822                 else {
823                         cmd = (struct scsi_cmnd *) hostdata->selecting; /* get a valid cmd */
824                         hostdata->selecting = NULL;
825                 }
826
827                 cmd->result = DID_NO_CONNECT << 16;
828                 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
829                 hostdata->state = S_UNCONNECTED;
830                 cmd->scsi_done(cmd);
831
832                 /* From esp.c:
833                  * There is a window of time within the scsi_done() path
834                  * of execution where interrupts are turned back on full
835                  * blast and left that way.  During that time we could
836                  * reconnect to a disconnected command, then we'd bomb
837                  * out below.  We could also end up executing two commands
838                  * at _once_.  ...just so you know why the restore_flags()
839                  * is here...
840                  */
841
842                 spin_unlock_irqrestore(&hostdata->lock, flags);
843
844 /* We are not connected to a target - check to see if there
845  * are commands waiting to be executed.
846  */
847
848                 wd33c93_execute(instance);
849                 break;
850
851 /* Note: this interrupt should not occur in a LEVEL2 command */
852
853         case CSR_SELECT:
854                 DB(DB_INTR, printk("SELECT"))
855                     hostdata->connected = cmd =
856                     (struct scsi_cmnd *) hostdata->selecting;
857                 hostdata->selecting = NULL;
858
859                 /* construct an IDENTIFY message with correct disconnect bit */
860
861                 hostdata->outgoing_msg[0] = (0x80 | 0x00 | cmd->device->lun);
862                 if (cmd->SCp.phase)
863                         hostdata->outgoing_msg[0] |= 0x40;
864
865                 if (hostdata->sync_stat[cmd->device->id] == SS_FIRST) {
866 #ifdef SYNC_DEBUG
867                         printk(" sending SDTR ");
868 #endif
869
870                         hostdata->sync_stat[cmd->device->id] = SS_WAITING;
871
872 /* Tack on a 2nd message to ask about synchronous transfers. If we've
873  * been asked to do only asynchronous transfers on this device, we
874  * request a fifo depth of 0, which is equivalent to async - should
875  * solve the problems some people have had with GVP's Guru ROM.
876  */
877
878                         hostdata->outgoing_msg[1] = EXTENDED_MESSAGE;
879                         hostdata->outgoing_msg[2] = 3;
880                         hostdata->outgoing_msg[3] = EXTENDED_SDTR;
881                         if (hostdata->no_sync & (1 << cmd->device->id)) {
882                                 hostdata->outgoing_msg[4] =
883                                     hostdata->default_sx_per / 4;
884                                 hostdata->outgoing_msg[5] = 0;
885                         } else {
886                                 hostdata->outgoing_msg[4] = OPTIMUM_SX_PER / 4;
887                                 hostdata->outgoing_msg[5] = OPTIMUM_SX_OFF;
888                         }
889                         hostdata->outgoing_len = 6;
890                 } else
891                         hostdata->outgoing_len = 1;
892
893                 hostdata->state = S_CONNECTED;
894                 spin_unlock_irqrestore(&hostdata->lock, flags);
895                 break;
896
897         case CSR_XFER_DONE | PHS_DATA_IN:
898         case CSR_UNEXP | PHS_DATA_IN:
899         case CSR_SRV_REQ | PHS_DATA_IN:
900                 DB(DB_INTR,
901                    printk("IN-%d.%d", cmd->SCp.this_residual,
902                           cmd->SCp.buffers_residual))
903                     transfer_bytes(regs, cmd, DATA_IN_DIR);
904                 if (hostdata->state != S_RUNNING_LEVEL2)
905                         hostdata->state = S_CONNECTED;
906                 spin_unlock_irqrestore(&hostdata->lock, flags);
907                 break;
908
909         case CSR_XFER_DONE | PHS_DATA_OUT:
910         case CSR_UNEXP | PHS_DATA_OUT:
911         case CSR_SRV_REQ | PHS_DATA_OUT:
912                 DB(DB_INTR,
913                    printk("OUT-%d.%d", cmd->SCp.this_residual,
914                           cmd->SCp.buffers_residual))
915                     transfer_bytes(regs, cmd, DATA_OUT_DIR);
916                 if (hostdata->state != S_RUNNING_LEVEL2)
917                         hostdata->state = S_CONNECTED;
918                 spin_unlock_irqrestore(&hostdata->lock, flags);
919                 break;
920
921 /* Note: this interrupt should not occur in a LEVEL2 command */
922
923         case CSR_XFER_DONE | PHS_COMMAND:
924         case CSR_UNEXP | PHS_COMMAND:
925         case CSR_SRV_REQ | PHS_COMMAND:
926                 DB(DB_INTR, printk("CMND-%02x,%ld", cmd->cmnd[0], cmd->pid))
927                     transfer_pio(regs, cmd->cmnd, cmd->cmd_len, DATA_OUT_DIR,
928                                  hostdata);
929                 hostdata->state = S_CONNECTED;
930                 spin_unlock_irqrestore(&hostdata->lock, flags);
931                 break;
932
933         case CSR_XFER_DONE | PHS_STATUS:
934         case CSR_UNEXP | PHS_STATUS:
935         case CSR_SRV_REQ | PHS_STATUS:
936                 DB(DB_INTR, printk("STATUS="))
937                 cmd->SCp.Status = read_1_byte(regs);
938                 DB(DB_INTR, printk("%02x", cmd->SCp.Status))
939                     if (hostdata->level2 >= L2_BASIC) {
940                         sr = read_wd33c93(regs, WD_SCSI_STATUS);        /* clear interrupt */
941                         udelay(7);
942                         hostdata->state = S_RUNNING_LEVEL2;
943                         write_wd33c93(regs, WD_COMMAND_PHASE, 0x50);
944                         write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
945                 } else {
946                         hostdata->state = S_CONNECTED;
947                 }
948                 spin_unlock_irqrestore(&hostdata->lock, flags);
949                 break;
950
951         case CSR_XFER_DONE | PHS_MESS_IN:
952         case CSR_UNEXP | PHS_MESS_IN:
953         case CSR_SRV_REQ | PHS_MESS_IN:
954                 DB(DB_INTR, printk("MSG_IN="))
955
956                 msg = read_1_byte(regs);
957                 sr = read_wd33c93(regs, WD_SCSI_STATUS);        /* clear interrupt */
958                 udelay(7);
959
960                 hostdata->incoming_msg[hostdata->incoming_ptr] = msg;
961                 if (hostdata->incoming_msg[0] == EXTENDED_MESSAGE)
962                         msg = EXTENDED_MESSAGE;
963                 else
964                         hostdata->incoming_ptr = 0;
965
966                 cmd->SCp.Message = msg;
967                 switch (msg) {
968
969                 case COMMAND_COMPLETE:
970                         DB(DB_INTR, printk("CCMP-%ld", cmd->pid))
971                             write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
972                         hostdata->state = S_PRE_CMP_DISC;
973                         break;
974
975                 case SAVE_POINTERS:
976                         DB(DB_INTR, printk("SDP"))
977                             write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
978                         hostdata->state = S_CONNECTED;
979                         break;
980
981                 case RESTORE_POINTERS:
982                         DB(DB_INTR, printk("RDP"))
983                             if (hostdata->level2 >= L2_BASIC) {
984                                 write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
985                                 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
986                                 hostdata->state = S_RUNNING_LEVEL2;
987                         } else {
988                                 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
989                                 hostdata->state = S_CONNECTED;
990                         }
991                         break;
992
993                 case DISCONNECT:
994                         DB(DB_INTR, printk("DIS"))
995                             cmd->device->disconnect = 1;
996                         write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
997                         hostdata->state = S_PRE_TMP_DISC;
998                         break;
999
1000                 case MESSAGE_REJECT:
1001                         DB(DB_INTR, printk("REJ"))
1002 #ifdef SYNC_DEBUG
1003                             printk("-REJ-");
1004 #endif
1005                         if (hostdata->sync_stat[cmd->device->id] == SS_WAITING)
1006                                 hostdata->sync_stat[cmd->device->id] = SS_SET;
1007                         write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1008                         hostdata->state = S_CONNECTED;
1009                         break;
1010
1011                 case EXTENDED_MESSAGE:
1012                         DB(DB_INTR, printk("EXT"))
1013
1014                             ucp = hostdata->incoming_msg;
1015
1016 #ifdef SYNC_DEBUG
1017                         printk("%02x", ucp[hostdata->incoming_ptr]);
1018 #endif
1019                         /* Is this the last byte of the extended message? */
1020
1021                         if ((hostdata->incoming_ptr >= 2) &&
1022                             (hostdata->incoming_ptr == (ucp[1] + 1))) {
1023
1024                                 switch (ucp[2]) {       /* what's the EXTENDED code? */
1025                                 case EXTENDED_SDTR:
1026                                         id = calc_sync_xfer(ucp[3], ucp[4]);
1027                                         if (hostdata->sync_stat[cmd->device->id] !=
1028                                             SS_WAITING) {
1029
1030 /* A device has sent an unsolicited SDTR message; rather than go
1031  * through the effort of decoding it and then figuring out what
1032  * our reply should be, we're just gonna say that we have a
1033  * synchronous fifo depth of 0. This will result in asynchronous
1034  * transfers - not ideal but so much easier.
1035  * Actually, this is OK because it assures us that if we don't
1036  * specifically ask for sync transfers, we won't do any.
1037  */
1038
1039                                                 write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN);     /* want MESS_OUT */
1040                                                 hostdata->outgoing_msg[0] =
1041                                                     EXTENDED_MESSAGE;
1042                                                 hostdata->outgoing_msg[1] = 3;
1043                                                 hostdata->outgoing_msg[2] =
1044                                                     EXTENDED_SDTR;
1045                                                 hostdata->outgoing_msg[3] =
1046                                                     hostdata->default_sx_per /
1047                                                     4;
1048                                                 hostdata->outgoing_msg[4] = 0;
1049                                                 hostdata->outgoing_len = 5;
1050                                                 hostdata->sync_xfer[cmd->device->id] =
1051                                                     calc_sync_xfer(hostdata->
1052                                                                    default_sx_per
1053                                                                    / 4, 0);
1054                                         } else {
1055                                                 hostdata->sync_xfer[cmd->device->id] = id;
1056                                         }
1057 #ifdef SYNC_DEBUG
1058                                         printk("sync_xfer=%02x",
1059                                                hostdata->sync_xfer[cmd->device->id]);
1060 #endif
1061                                         hostdata->sync_stat[cmd->device->id] =
1062                                             SS_SET;
1063                                         write_wd33c93_cmd(regs,
1064                                                           WD_CMD_NEGATE_ACK);
1065                                         hostdata->state = S_CONNECTED;
1066                                         break;
1067                                 case EXTENDED_WDTR:
1068                                         write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN);     /* want MESS_OUT */
1069                                         printk("sending WDTR ");
1070                                         hostdata->outgoing_msg[0] =
1071                                             EXTENDED_MESSAGE;
1072                                         hostdata->outgoing_msg[1] = 2;
1073                                         hostdata->outgoing_msg[2] =
1074                                             EXTENDED_WDTR;
1075                                         hostdata->outgoing_msg[3] = 0;  /* 8 bit transfer width */
1076                                         hostdata->outgoing_len = 4;
1077                                         write_wd33c93_cmd(regs,
1078                                                           WD_CMD_NEGATE_ACK);
1079                                         hostdata->state = S_CONNECTED;
1080                                         break;
1081                                 default:
1082                                         write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN);     /* want MESS_OUT */
1083                                         printk
1084                                             ("Rejecting Unknown Extended Message(%02x). ",
1085                                              ucp[2]);
1086                                         hostdata->outgoing_msg[0] =
1087                                             MESSAGE_REJECT;
1088                                         hostdata->outgoing_len = 1;
1089                                         write_wd33c93_cmd(regs,
1090                                                           WD_CMD_NEGATE_ACK);
1091                                         hostdata->state = S_CONNECTED;
1092                                         break;
1093                                 }
1094                                 hostdata->incoming_ptr = 0;
1095                         }
1096
1097                         /* We need to read more MESS_IN bytes for the extended message */
1098
1099                         else {
1100                                 hostdata->incoming_ptr++;
1101                                 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1102                                 hostdata->state = S_CONNECTED;
1103                         }
1104                         break;
1105
1106                 default:
1107                         printk("Rejecting Unknown Message(%02x) ", msg);
1108                         write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN);     /* want MESS_OUT */
1109                         hostdata->outgoing_msg[0] = MESSAGE_REJECT;
1110                         hostdata->outgoing_len = 1;
1111                         write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1112                         hostdata->state = S_CONNECTED;
1113                 }
1114                 spin_unlock_irqrestore(&hostdata->lock, flags);
1115                 break;
1116
1117 /* Note: this interrupt will occur only after a LEVEL2 command */
1118
1119         case CSR_SEL_XFER_DONE:
1120
1121 /* Make sure that reselection is enabled at this point - it may
1122  * have been turned off for the command that just completed.
1123  */
1124
1125                 write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
1126                 if (phs == 0x60) {
1127                         DB(DB_INTR, printk("SX-DONE-%ld", cmd->pid))
1128                             cmd->SCp.Message = COMMAND_COMPLETE;
1129                         lun = read_wd33c93(regs, WD_TARGET_LUN);
1130                         DB(DB_INTR, printk(":%d.%d", cmd->SCp.Status, lun))
1131                             hostdata->connected = NULL;
1132                         hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1133                         hostdata->state = S_UNCONNECTED;
1134                         if (cmd->SCp.Status == ILLEGAL_STATUS_BYTE)
1135                                 cmd->SCp.Status = lun;
1136                         if (cmd->cmnd[0] == REQUEST_SENSE
1137                             && cmd->SCp.Status != GOOD)
1138                                 cmd->result =
1139                                     (cmd->
1140                                      result & 0x00ffff) | (DID_ERROR << 16);
1141                         else
1142                                 cmd->result =
1143                                     cmd->SCp.Status | (cmd->SCp.Message << 8);
1144                         cmd->scsi_done(cmd);
1145
1146 /* We are no longer  connected to a target - check to see if
1147  * there are commands waiting to be executed.
1148  */
1149                         spin_unlock_irqrestore(&hostdata->lock, flags);
1150                         wd33c93_execute(instance);
1151                 } else {
1152                         printk
1153                             ("%02x:%02x:%02x-%ld: Unknown SEL_XFER_DONE phase!!---",
1154                              asr, sr, phs, cmd->pid);
1155                         spin_unlock_irqrestore(&hostdata->lock, flags);
1156                 }
1157                 break;
1158
1159 /* Note: this interrupt will occur only after a LEVEL2 command */
1160
1161         case CSR_SDP:
1162                 DB(DB_INTR, printk("SDP"))
1163                     hostdata->state = S_RUNNING_LEVEL2;
1164                 write_wd33c93(regs, WD_COMMAND_PHASE, 0x41);
1165                 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
1166                 spin_unlock_irqrestore(&hostdata->lock, flags);
1167                 break;
1168
1169         case CSR_XFER_DONE | PHS_MESS_OUT:
1170         case CSR_UNEXP | PHS_MESS_OUT:
1171         case CSR_SRV_REQ | PHS_MESS_OUT:
1172                 DB(DB_INTR, printk("MSG_OUT="))
1173
1174 /* To get here, we've probably requested MESSAGE_OUT and have
1175  * already put the correct bytes in outgoing_msg[] and filled
1176  * in outgoing_len. We simply send them out to the SCSI bus.
1177  * Sometimes we get MESSAGE_OUT phase when we're not expecting
1178  * it - like when our SDTR message is rejected by a target. Some
1179  * targets send the REJECT before receiving all of the extended
1180  * message, and then seem to go back to MESSAGE_OUT for a byte
1181  * or two. Not sure why, or if I'm doing something wrong to
1182  * cause this to happen. Regardless, it seems that sending
1183  * NOP messages in these situations results in no harm and
1184  * makes everyone happy.
1185  */
1186                     if (hostdata->outgoing_len == 0) {
1187                         hostdata->outgoing_len = 1;
1188                         hostdata->outgoing_msg[0] = NOP;
1189                 }
1190                 transfer_pio(regs, hostdata->outgoing_msg,
1191                              hostdata->outgoing_len, DATA_OUT_DIR, hostdata);
1192                 DB(DB_INTR, printk("%02x", hostdata->outgoing_msg[0]))
1193                     hostdata->outgoing_len = 0;
1194                 hostdata->state = S_CONNECTED;
1195                 spin_unlock_irqrestore(&hostdata->lock, flags);
1196                 break;
1197
1198         case CSR_UNEXP_DISC:
1199
1200 /* I think I've seen this after a request-sense that was in response
1201  * to an error condition, but not sure. We certainly need to do
1202  * something when we get this interrupt - the question is 'what?'.
1203  * Let's think positively, and assume some command has finished
1204  * in a legal manner (like a command that provokes a request-sense),
1205  * so we treat it as a normal command-complete-disconnect.
1206  */
1207
1208 /* Make sure that reselection is enabled at this point - it may
1209  * have been turned off for the command that just completed.
1210  */
1211
1212                 write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
1213                 if (cmd == NULL) {
1214                         printk(" - Already disconnected! ");
1215                         hostdata->state = S_UNCONNECTED;
1216                         spin_unlock_irqrestore(&hostdata->lock, flags);
1217                         return;
1218                 }
1219                 DB(DB_INTR, printk("UNEXP_DISC-%ld", cmd->pid))
1220                     hostdata->connected = NULL;
1221                 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1222                 hostdata->state = S_UNCONNECTED;
1223                 if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
1224                         cmd->result =
1225                             (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1226                 else
1227                         cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1228                 cmd->scsi_done(cmd);
1229
1230 /* We are no longer connected to a target - check to see if
1231  * there are commands waiting to be executed.
1232  */
1233                 /* look above for comments on scsi_done() */
1234                 spin_unlock_irqrestore(&hostdata->lock, flags);
1235                 wd33c93_execute(instance);
1236                 break;
1237
1238         case CSR_DISC:
1239
1240 /* Make sure that reselection is enabled at this point - it may
1241  * have been turned off for the command that just completed.
1242  */
1243
1244                 write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
1245                 DB(DB_INTR, printk("DISC-%ld", cmd->pid))
1246                     if (cmd == NULL) {
1247                         printk(" - Already disconnected! ");
1248                         hostdata->state = S_UNCONNECTED;
1249                 }
1250                 switch (hostdata->state) {
1251                 case S_PRE_CMP_DISC:
1252                         hostdata->connected = NULL;
1253                         hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1254                         hostdata->state = S_UNCONNECTED;
1255                         DB(DB_INTR, printk(":%d", cmd->SCp.Status))
1256                             if (cmd->cmnd[0] == REQUEST_SENSE
1257                                 && cmd->SCp.Status != GOOD)
1258                                 cmd->result =
1259                                     (cmd->
1260                                      result & 0x00ffff) | (DID_ERROR << 16);
1261                         else
1262                                 cmd->result =
1263                                     cmd->SCp.Status | (cmd->SCp.Message << 8);
1264                         cmd->scsi_done(cmd);
1265                         break;
1266                 case S_PRE_TMP_DISC:
1267                 case S_RUNNING_LEVEL2:
1268                         cmd->host_scribble = (uchar *) hostdata->disconnected_Q;
1269                         hostdata->disconnected_Q = cmd;
1270                         hostdata->connected = NULL;
1271                         hostdata->state = S_UNCONNECTED;
1272
1273 #ifdef PROC_STATISTICS
1274                         hostdata->disc_done_cnt[cmd->device->id]++;
1275 #endif
1276
1277                         break;
1278                 default:
1279                         printk("*** Unexpected DISCONNECT interrupt! ***");
1280                         hostdata->state = S_UNCONNECTED;
1281                 }
1282
1283 /* We are no longer connected to a target - check to see if
1284  * there are commands waiting to be executed.
1285  */
1286                 spin_unlock_irqrestore(&hostdata->lock, flags);
1287                 wd33c93_execute(instance);
1288                 break;
1289
1290         case CSR_RESEL_AM:
1291         case CSR_RESEL:
1292                 DB(DB_INTR, printk("RESEL%s", sr == CSR_RESEL_AM ? "_AM" : ""))
1293
1294                     /* Old chips (pre -A ???) don't have advanced features and will
1295                      * generate CSR_RESEL.  In that case we have to extract the LUN the
1296                      * hard way (see below).
1297                      * First we have to make sure this reselection didn't
1298                      * happen during Arbitration/Selection of some other device.
1299                      * If yes, put losing command back on top of input_Q.
1300                      */
1301                     if (hostdata->level2 <= L2_NONE) {
1302
1303                         if (hostdata->selecting) {
1304                                 cmd = (struct scsi_cmnd *) hostdata->selecting;
1305                                 hostdata->selecting = NULL;
1306                                 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1307                                 cmd->host_scribble =
1308                                     (uchar *) hostdata->input_Q;
1309                                 hostdata->input_Q = cmd;
1310                         }
1311                 }
1312
1313                 else {
1314
1315                         if (cmd) {
1316                                 if (phs == 0x00) {
1317                                         hostdata->busy[cmd->device->id] &=
1318                                             ~(1 << cmd->device->lun);
1319                                         cmd->host_scribble =
1320                                             (uchar *) hostdata->input_Q;
1321                                         hostdata->input_Q = cmd;
1322                                 } else {
1323                                         printk
1324                                             ("---%02x:%02x:%02x-TROUBLE: Intrusive ReSelect!---",
1325                                              asr, sr, phs);
1326                                         while (1)
1327                                                 printk("\r");
1328                                 }
1329                         }
1330
1331                 }
1332
1333                 /* OK - find out which device reselected us. */
1334
1335                 id = read_wd33c93(regs, WD_SOURCE_ID);
1336                 id &= SRCID_MASK;
1337
1338                 /* and extract the lun from the ID message. (Note that we don't
1339                  * bother to check for a valid message here - I guess this is
1340                  * not the right way to go, but...)
1341                  */
1342
1343                 if (sr == CSR_RESEL_AM) {
1344                         lun = read_wd33c93(regs, WD_DATA);
1345                         if (hostdata->level2 < L2_RESELECT)
1346                                 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1347                         lun &= 7;
1348                 } else {
1349                         /* Old chip; wait for msgin phase to pick up the LUN. */
1350                         for (lun = 255; lun; lun--) {
1351                                 if ((asr = read_aux_stat(regs)) & ASR_INT)
1352                                         break;
1353                                 udelay(10);
1354                         }
1355                         if (!(asr & ASR_INT)) {
1356                                 printk
1357                                     ("wd33c93: Reselected without IDENTIFY\n");
1358                                 lun = 0;
1359                         } else {
1360                                 /* Verify this is a change to MSG_IN and read the message */
1361                                 sr = read_wd33c93(regs, WD_SCSI_STATUS);
1362                                 udelay(7);
1363                                 if (sr == (CSR_ABORT | PHS_MESS_IN) ||
1364                                     sr == (CSR_UNEXP | PHS_MESS_IN) ||
1365                                     sr == (CSR_SRV_REQ | PHS_MESS_IN)) {
1366                                         /* Got MSG_IN, grab target LUN */
1367                                         lun = read_1_byte(regs);
1368                                         /* Now we expect a 'paused with ACK asserted' int.. */
1369                                         asr = read_aux_stat(regs);
1370                                         if (!(asr & ASR_INT)) {
1371                                                 udelay(10);
1372                                                 asr = read_aux_stat(regs);
1373                                                 if (!(asr & ASR_INT))
1374                                                         printk
1375                                                             ("wd33c93: No int after LUN on RESEL (%02x)\n",
1376                                                              asr);
1377                                         }
1378                                         sr = read_wd33c93(regs, WD_SCSI_STATUS);
1379                                         udelay(7);
1380                                         if (sr != CSR_MSGIN)
1381                                                 printk
1382                                                     ("wd33c93: Not paused with ACK on RESEL (%02x)\n",
1383                                                      sr);
1384                                         lun &= 7;
1385                                         write_wd33c93_cmd(regs,
1386                                                           WD_CMD_NEGATE_ACK);
1387                                 } else {
1388                                         printk
1389                                             ("wd33c93: Not MSG_IN on reselect (%02x)\n",
1390                                              sr);
1391                                         lun = 0;
1392                                 }
1393                         }
1394                 }
1395
1396                 /* Now we look for the command that's reconnecting. */
1397
1398                 cmd = (struct scsi_cmnd *) hostdata->disconnected_Q;
1399                 patch = NULL;
1400                 while (cmd) {
1401                         if (id == cmd->device->id && lun == cmd->device->lun)
1402                                 break;
1403                         patch = cmd;
1404                         cmd = (struct scsi_cmnd *) cmd->host_scribble;
1405                 }
1406
1407                 /* Hmm. Couldn't find a valid command.... What to do? */
1408
1409                 if (!cmd) {
1410                         printk
1411                             ("---TROUBLE: target %d.%d not in disconnect queue---",
1412                              id, lun);
1413                         spin_unlock_irqrestore(&hostdata->lock, flags);
1414                         return;
1415                 }
1416
1417                 /* Ok, found the command - now start it up again. */
1418
1419                 if (patch)
1420                         patch->host_scribble = cmd->host_scribble;
1421                 else
1422                         hostdata->disconnected_Q =
1423                             (struct scsi_cmnd *) cmd->host_scribble;
1424                 hostdata->connected = cmd;
1425
1426                 /* We don't need to worry about 'initialize_SCp()' or 'hostdata->busy[]'
1427                  * because these things are preserved over a disconnect.
1428                  * But we DO need to fix the DPD bit so it's correct for this command.
1429                  */
1430
1431                 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1432                         write_wd33c93(regs, WD_DESTINATION_ID, cmd->device->id);
1433                 else
1434                         write_wd33c93(regs, WD_DESTINATION_ID,
1435                                       cmd->device->id | DSTID_DPD);
1436                 if (hostdata->level2 >= L2_RESELECT) {
1437                         write_wd33c93_count(regs, 0);   /* we want a DATA_PHASE interrupt */
1438                         write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
1439                         write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
1440                         hostdata->state = S_RUNNING_LEVEL2;
1441                 } else
1442                         hostdata->state = S_CONNECTED;
1443
1444                 DB(DB_INTR, printk("-%ld", cmd->pid))
1445                     spin_unlock_irqrestore(&hostdata->lock, flags);
1446                 break;
1447
1448         default:
1449                 printk("--UNKNOWN INTERRUPT:%02x:%02x:%02x--", asr, sr, phs);
1450                 spin_unlock_irqrestore(&hostdata->lock, flags);
1451         }
1452
1453         DB(DB_INTR, printk("} "))
1454
1455 }
1456
1457 static void
1458 reset_wd33c93(struct Scsi_Host *instance)
1459 {
1460         struct WD33C93_hostdata *hostdata =
1461             (struct WD33C93_hostdata *) instance->hostdata;
1462         const wd33c93_regs regs = hostdata->regs;
1463         uchar sr;
1464
1465 #ifdef CONFIG_SGI_IP22
1466         {
1467                 int busycount = 0;
1468                 extern void sgiwd93_reset(unsigned long);
1469                 /* wait 'til the chip gets some time for us */
1470                 while ((read_aux_stat(regs) & ASR_BSY) && busycount++ < 100)
1471                         udelay (10);
1472         /*
1473          * there are scsi devices out there, which manage to lock up
1474          * the wd33c93 in a busy condition. In this state it won't
1475          * accept the reset command. The only way to solve this is to
1476          * give the chip a hardware reset (if possible). The code below
1477          * does this for the SGI Indy, where this is possible
1478          */
1479         /* still busy ? */
1480         if (read_aux_stat(regs) & ASR_BSY)
1481                 sgiwd93_reset(instance->base); /* yeah, give it the hard one */
1482         }
1483 #endif
1484
1485         write_wd33c93(regs, WD_OWN_ID, OWNID_EAF | OWNID_RAF |
1486                       instance->this_id | hostdata->clock_freq);
1487         write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1488         write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,
1489                       calc_sync_xfer(hostdata->default_sx_per / 4,
1490                                      DEFAULT_SX_OFF));
1491         write_wd33c93(regs, WD_COMMAND, WD_CMD_RESET);
1492
1493
1494 #ifdef CONFIG_MVME147_SCSI
1495         udelay(25);             /* The old wd33c93 on MVME147 needs this, at least */
1496 #endif
1497
1498         while (!(read_aux_stat(regs) & ASR_INT))
1499                 ;
1500         sr = read_wd33c93(regs, WD_SCSI_STATUS);
1501
1502         hostdata->microcode = read_wd33c93(regs, WD_CDB_1);
1503         if (sr == 0x00)
1504                 hostdata->chip = C_WD33C93;
1505         else if (sr == 0x01) {
1506                 write_wd33c93(regs, WD_QUEUE_TAG, 0xa5);        /* any random number */
1507                 sr = read_wd33c93(regs, WD_QUEUE_TAG);
1508                 if (sr == 0xa5) {
1509                         hostdata->chip = C_WD33C93B;
1510                         write_wd33c93(regs, WD_QUEUE_TAG, 0);
1511                 } else
1512                         hostdata->chip = C_WD33C93A;
1513         } else
1514                 hostdata->chip = C_UNKNOWN_CHIP;
1515
1516         write_wd33c93(regs, WD_TIMEOUT_PERIOD, TIMEOUT_PERIOD_VALUE);
1517         write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1518 }
1519
1520 int
1521 wd33c93_host_reset(struct scsi_cmnd * SCpnt)
1522 {
1523         struct Scsi_Host *instance;
1524         struct WD33C93_hostdata *hostdata;
1525         int i;
1526
1527         instance = SCpnt->device->host;
1528         hostdata = (struct WD33C93_hostdata *) instance->hostdata;
1529
1530         printk("scsi%d: reset. ", instance->host_no);
1531         disable_irq(instance->irq);
1532
1533         hostdata->dma_stop(instance, NULL, 0);
1534         for (i = 0; i < 8; i++) {
1535                 hostdata->busy[i] = 0;
1536                 hostdata->sync_xfer[i] =
1537                     calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF);
1538                 hostdata->sync_stat[i] = SS_UNSET;      /* using default sync values */
1539         }
1540         hostdata->input_Q = NULL;
1541         hostdata->selecting = NULL;
1542         hostdata->connected = NULL;
1543         hostdata->disconnected_Q = NULL;
1544         hostdata->state = S_UNCONNECTED;
1545         hostdata->dma = D_DMA_OFF;
1546         hostdata->incoming_ptr = 0;
1547         hostdata->outgoing_len = 0;
1548
1549         reset_wd33c93(instance);
1550         SCpnt->result = DID_RESET << 16;
1551         enable_irq(instance->irq);
1552         return SUCCESS;
1553 }
1554
1555 int
1556 wd33c93_abort(struct scsi_cmnd * cmd)
1557 {
1558         struct Scsi_Host *instance;
1559         struct WD33C93_hostdata *hostdata;
1560         wd33c93_regs regs;
1561         struct scsi_cmnd *tmp, *prev;
1562
1563         disable_irq(cmd->device->host->irq);
1564
1565         instance = cmd->device->host;
1566         hostdata = (struct WD33C93_hostdata *) instance->hostdata;
1567         regs = hostdata->regs;
1568
1569 /*
1570  * Case 1 : If the command hasn't been issued yet, we simply remove it
1571  *     from the input_Q.
1572  */
1573
1574         tmp = (struct scsi_cmnd *) hostdata->input_Q;
1575         prev = NULL;
1576         while (tmp) {
1577                 if (tmp == cmd) {
1578                         if (prev)
1579                                 prev->host_scribble = cmd->host_scribble;
1580                         else
1581                                 hostdata->input_Q =
1582                                     (struct scsi_cmnd *) cmd->host_scribble;
1583                         cmd->host_scribble = NULL;
1584                         cmd->result = DID_ABORT << 16;
1585                         printk
1586                             ("scsi%d: Abort - removing command %ld from input_Q. ",
1587                              instance->host_no, cmd->pid);
1588                         enable_irq(cmd->device->host->irq);
1589                         cmd->scsi_done(cmd);
1590                         return SUCCESS;
1591                 }
1592                 prev = tmp;
1593                 tmp = (struct scsi_cmnd *) tmp->host_scribble;
1594         }
1595
1596 /*
1597  * Case 2 : If the command is connected, we're going to fail the abort
1598  *     and let the high level SCSI driver retry at a later time or
1599  *     issue a reset.
1600  *
1601  *     Timeouts, and therefore aborted commands, will be highly unlikely
1602  *     and handling them cleanly in this situation would make the common
1603  *     case of noresets less efficient, and would pollute our code.  So,
1604  *     we fail.
1605  */
1606
1607         if (hostdata->connected == cmd) {
1608                 uchar sr, asr;
1609                 unsigned long timeout;
1610
1611                 printk("scsi%d: Aborting connected command %ld - ",
1612                        instance->host_no, cmd->pid);
1613
1614                 printk("stopping DMA - ");
1615                 if (hostdata->dma == D_DMA_RUNNING) {
1616                         hostdata->dma_stop(instance, cmd, 0);
1617                         hostdata->dma = D_DMA_OFF;
1618                 }
1619
1620                 printk("sending wd33c93 ABORT command - ");
1621                 write_wd33c93(regs, WD_CONTROL,
1622                               CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1623                 write_wd33c93_cmd(regs, WD_CMD_ABORT);
1624
1625 /* Now we have to attempt to flush out the FIFO... */
1626
1627                 printk("flushing fifo - ");
1628                 timeout = 1000000;
1629                 do {
1630                         asr = read_aux_stat(regs);
1631                         if (asr & ASR_DBR)
1632                                 read_wd33c93(regs, WD_DATA);
1633                 } while (!(asr & ASR_INT) && timeout-- > 0);
1634                 sr = read_wd33c93(regs, WD_SCSI_STATUS);
1635                 printk
1636                     ("asr=%02x, sr=%02x, %ld bytes un-transferred (timeout=%ld) - ",
1637                      asr, sr, read_wd33c93_count(regs), timeout);
1638
1639                 /*
1640                  * Abort command processed.
1641                  * Still connected.
1642                  * We must disconnect.
1643                  */
1644
1645                 printk("sending wd33c93 DISCONNECT command - ");
1646                 write_wd33c93_cmd(regs, WD_CMD_DISCONNECT);
1647
1648                 timeout = 1000000;
1649                 asr = read_aux_stat(regs);
1650                 while ((asr & ASR_CIP) && timeout-- > 0)
1651                         asr = read_aux_stat(regs);
1652                 sr = read_wd33c93(regs, WD_SCSI_STATUS);
1653                 printk("asr=%02x, sr=%02x.", asr, sr);
1654
1655                 hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1656                 hostdata->connected = NULL;
1657                 hostdata->state = S_UNCONNECTED;
1658                 cmd->result = DID_ABORT << 16;
1659
1660 /*      sti();*/
1661                 wd33c93_execute(instance);
1662
1663                 enable_irq(cmd->device->host->irq);
1664                 cmd->scsi_done(cmd);
1665                 return SUCCESS;
1666         }
1667
1668 /*
1669  * Case 3: If the command is currently disconnected from the bus,
1670  * we're not going to expend much effort here: Let's just return
1671  * an ABORT_SNOOZE and hope for the best...
1672  */
1673
1674         tmp = (struct scsi_cmnd *) hostdata->disconnected_Q;
1675         while (tmp) {
1676                 if (tmp == cmd) {
1677                         printk
1678                             ("scsi%d: Abort - command %ld found on disconnected_Q - ",
1679                              instance->host_no, cmd->pid);
1680                         printk("Abort SNOOZE. ");
1681                         enable_irq(cmd->device->host->irq);
1682                         return FAILED;
1683                 }
1684                 tmp = (struct scsi_cmnd *) tmp->host_scribble;
1685         }
1686
1687 /*
1688  * Case 4 : If we reached this point, the command was not found in any of
1689  *     the queues.
1690  *
1691  * We probably reached this point because of an unlikely race condition
1692  * between the command completing successfully and the abortion code,
1693  * so we won't panic, but we will notify the user in case something really
1694  * broke.
1695  */
1696
1697 /*   sti();*/
1698         wd33c93_execute(instance);
1699
1700         enable_irq(cmd->device->host->irq);
1701         printk("scsi%d: warning : SCSI command probably completed successfully"
1702                "         before abortion. ", instance->host_no);
1703         return FAILED;
1704 }
1705
1706 #define MAX_WD33C93_HOSTS 4
1707 #define MAX_SETUP_ARGS ARRAY_SIZE(setup_args)
1708 #define SETUP_BUFFER_SIZE 200
1709 static char setup_buffer[SETUP_BUFFER_SIZE];
1710 static char setup_used[MAX_SETUP_ARGS];
1711 static int done_setup = 0;
1712
1713 int
1714 wd33c93_setup(char *str)
1715 {
1716         int i;
1717         char *p1, *p2;
1718
1719         /* The kernel does some processing of the command-line before calling
1720          * this function: If it begins with any decimal or hex number arguments,
1721          * ints[0] = how many numbers found and ints[1] through [n] are the values
1722          * themselves. str points to where the non-numeric arguments (if any)
1723          * start: We do our own parsing of those. We construct synthetic 'nosync'
1724          * keywords out of numeric args (to maintain compatibility with older
1725          * versions) and then add the rest of the arguments.
1726          */
1727
1728         p1 = setup_buffer;
1729         *p1 = '\0';
1730         if (str)
1731                 strncpy(p1, str, SETUP_BUFFER_SIZE - strlen(setup_buffer));
1732         setup_buffer[SETUP_BUFFER_SIZE - 1] = '\0';
1733         p1 = setup_buffer;
1734         i = 0;
1735         while (*p1 && (i < MAX_SETUP_ARGS)) {
1736                 p2 = strchr(p1, ',');
1737                 if (p2) {
1738                         *p2 = '\0';
1739                         if (p1 != p2)
1740                                 setup_args[i] = p1;
1741                         p1 = p2 + 1;
1742                         i++;
1743                 } else {
1744                         setup_args[i] = p1;
1745                         break;
1746                 }
1747         }
1748         for (i = 0; i < MAX_SETUP_ARGS; i++)
1749                 setup_used[i] = 0;
1750         done_setup = 1;
1751
1752         return 1;
1753 }
1754 __setup("wd33c93=", wd33c93_setup);
1755
1756 /* check_setup_args() returns index if key found, 0 if not
1757  */
1758 static int
1759 check_setup_args(char *key, int *flags, int *val, char *buf)
1760 {
1761         int x;
1762         char *cp;
1763
1764         for (x = 0; x < MAX_SETUP_ARGS; x++) {
1765                 if (setup_used[x])
1766                         continue;
1767                 if (!strncmp(setup_args[x], key, strlen(key)))
1768                         break;
1769                 if (!strncmp(setup_args[x], "next", strlen("next")))
1770                         return 0;
1771         }
1772         if (x == MAX_SETUP_ARGS)
1773                 return 0;
1774         setup_used[x] = 1;
1775         cp = setup_args[x] + strlen(key);
1776         *val = -1;
1777         if (*cp != ':')
1778                 return ++x;
1779         cp++;
1780         if ((*cp >= '0') && (*cp <= '9')) {
1781                 *val = simple_strtoul(cp, NULL, 0);
1782         }
1783         return ++x;
1784 }
1785
1786 void
1787 wd33c93_init(struct Scsi_Host *instance, const wd33c93_regs regs,
1788              dma_setup_t setup, dma_stop_t stop, int clock_freq)
1789 {
1790         struct WD33C93_hostdata *hostdata;
1791         int i;
1792         int flags;
1793         int val;
1794         char buf[32];
1795
1796         if (!done_setup && setup_strings)
1797                 wd33c93_setup(setup_strings);
1798
1799         hostdata = (struct WD33C93_hostdata *) instance->hostdata;
1800
1801         hostdata->regs = regs;
1802         hostdata->clock_freq = clock_freq;
1803         hostdata->dma_setup = setup;
1804         hostdata->dma_stop = stop;
1805         hostdata->dma_bounce_buffer = NULL;
1806         hostdata->dma_bounce_len = 0;
1807         for (i = 0; i < 8; i++) {
1808                 hostdata->busy[i] = 0;
1809                 hostdata->sync_xfer[i] =
1810                     calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF);
1811                 hostdata->sync_stat[i] = SS_UNSET;      /* using default sync values */
1812 #ifdef PROC_STATISTICS
1813                 hostdata->cmd_cnt[i] = 0;
1814                 hostdata->disc_allowed_cnt[i] = 0;
1815                 hostdata->disc_done_cnt[i] = 0;
1816 #endif
1817         }
1818         hostdata->input_Q = NULL;
1819         hostdata->selecting = NULL;
1820         hostdata->connected = NULL;
1821         hostdata->disconnected_Q = NULL;
1822         hostdata->state = S_UNCONNECTED;
1823         hostdata->dma = D_DMA_OFF;
1824         hostdata->level2 = L2_BASIC;
1825         hostdata->disconnect = DIS_ADAPTIVE;
1826         hostdata->args = DEBUG_DEFAULTS;
1827         hostdata->incoming_ptr = 0;
1828         hostdata->outgoing_len = 0;
1829         hostdata->default_sx_per = DEFAULT_SX_PER;
1830         hostdata->no_sync = 0xff;       /* sync defaults to off */
1831         hostdata->no_dma = 0;   /* default is DMA enabled */
1832
1833 #ifdef PROC_INTERFACE
1834         hostdata->proc = PR_VERSION | PR_INFO | PR_STATISTICS |
1835             PR_CONNECTED | PR_INPUTQ | PR_DISCQ | PR_STOP;
1836 #ifdef PROC_STATISTICS
1837         hostdata->dma_cnt = 0;
1838         hostdata->pio_cnt = 0;
1839         hostdata->int_cnt = 0;
1840 #endif
1841 #endif
1842
1843         if (check_setup_args("nosync", &flags, &val, buf))
1844                 hostdata->no_sync = val;
1845
1846         if (check_setup_args("nodma", &flags, &val, buf))
1847                 hostdata->no_dma = (val == -1) ? 1 : val;
1848
1849         if (check_setup_args("period", &flags, &val, buf))
1850                 hostdata->default_sx_per =
1851                     sx_table[round_period((unsigned int) val)].period_ns;
1852
1853         if (check_setup_args("disconnect", &flags, &val, buf)) {
1854                 if ((val >= DIS_NEVER) && (val <= DIS_ALWAYS))
1855                         hostdata->disconnect = val;
1856                 else
1857                         hostdata->disconnect = DIS_ADAPTIVE;
1858         }
1859
1860         if (check_setup_args("level2", &flags, &val, buf))
1861                 hostdata->level2 = val;
1862
1863         if (check_setup_args("debug", &flags, &val, buf))
1864                 hostdata->args = val & DB_MASK;
1865
1866         if (check_setup_args("clock", &flags, &val, buf)) {
1867                 if (val > 7 && val < 11)
1868                         val = WD33C93_FS_8_10;
1869                 else if (val > 11 && val < 16)
1870                         val = WD33C93_FS_12_15;
1871                 else if (val > 15 && val < 21)
1872                         val = WD33C93_FS_16_20;
1873                 else
1874                         val = WD33C93_FS_8_10;
1875                 hostdata->clock_freq = val;
1876         }
1877
1878         if ((i = check_setup_args("next", &flags, &val, buf))) {
1879                 while (i)
1880                         setup_used[--i] = 1;
1881         }
1882 #ifdef PROC_INTERFACE
1883         if (check_setup_args("proc", &flags, &val, buf))
1884                 hostdata->proc = val;
1885 #endif
1886
1887         spin_lock_irq(&hostdata->lock);
1888         reset_wd33c93(instance);
1889         spin_unlock_irq(&hostdata->lock);
1890
1891         printk("wd33c93-%d: chip=%s/%d no_sync=0x%x no_dma=%d",
1892                instance->host_no,
1893                (hostdata->chip == C_WD33C93) ? "WD33c93" : (hostdata->chip ==
1894                                                             C_WD33C93A) ?
1895                "WD33c93A" : (hostdata->chip ==
1896                              C_WD33C93B) ? "WD33c93B" : "unknown",
1897                hostdata->microcode, hostdata->no_sync, hostdata->no_dma);
1898 #ifdef DEBUGGING_ON
1899         printk(" debug_flags=0x%02x\n", hostdata->args);
1900 #else
1901         printk(" debugging=OFF\n");
1902 #endif
1903         printk("           setup_args=");
1904         for (i = 0; i < MAX_SETUP_ARGS; i++)
1905                 printk("%s,", setup_args[i]);
1906         printk("\n");
1907         printk("           Version %s - %s, Compiled %s at %s\n",
1908                WD33C93_VERSION, WD33C93_DATE, __DATE__, __TIME__);
1909 }
1910
1911 int
1912 wd33c93_proc_info(struct Scsi_Host *instance, char *buf, char **start, off_t off, int len, int in)
1913 {
1914
1915 #ifdef PROC_INTERFACE
1916
1917         char *bp;
1918         char tbuf[128];
1919         struct WD33C93_hostdata *hd;
1920         struct scsi_cmnd *cmd;
1921         int x, i;
1922         static int stop = 0;
1923
1924         hd = (struct WD33C93_hostdata *) instance->hostdata;
1925
1926 /* If 'in' is TRUE we need to _read_ the proc file. We accept the following
1927  * keywords (same format as command-line, but only ONE per read):
1928  *    debug
1929  *    disconnect
1930  *    period
1931  *    resync
1932  *    proc
1933  *    nodma
1934  */
1935
1936         if (in) {
1937                 buf[len] = '\0';
1938                 bp = buf;
1939                 if (!strncmp(bp, "debug:", 6)) {
1940                         bp += 6;
1941                         hd->args = simple_strtoul(bp, NULL, 0) & DB_MASK;
1942                 } else if (!strncmp(bp, "disconnect:", 11)) {
1943                         bp += 11;
1944                         x = simple_strtoul(bp, NULL, 0);
1945                         if (x < DIS_NEVER || x > DIS_ALWAYS)
1946                                 x = DIS_ADAPTIVE;
1947                         hd->disconnect = x;
1948                 } else if (!strncmp(bp, "period:", 7)) {
1949                         bp += 7;
1950                         x = simple_strtoul(bp, NULL, 0);
1951                         hd->default_sx_per =
1952                             sx_table[round_period((unsigned int) x)].period_ns;
1953                 } else if (!strncmp(bp, "resync:", 7)) {
1954                         bp += 7;
1955                         x = simple_strtoul(bp, NULL, 0);
1956                         for (i = 0; i < 7; i++)
1957                                 if (x & (1 << i))
1958                                         hd->sync_stat[i] = SS_UNSET;
1959                 } else if (!strncmp(bp, "proc:", 5)) {
1960                         bp += 5;
1961                         hd->proc = simple_strtoul(bp, NULL, 0);
1962                 } else if (!strncmp(bp, "nodma:", 6)) {
1963                         bp += 6;
1964                         hd->no_dma = simple_strtoul(bp, NULL, 0);
1965                 } else if (!strncmp(bp, "level2:", 7)) {
1966                         bp += 7;
1967                         hd->level2 = simple_strtoul(bp, NULL, 0);
1968                 }
1969                 return len;
1970         }
1971
1972         spin_lock_irq(&hd->lock);
1973         bp = buf;
1974         *bp = '\0';
1975         if (hd->proc & PR_VERSION) {
1976                 sprintf(tbuf, "\nVersion %s - %s. Compiled %s %s",
1977                         WD33C93_VERSION, WD33C93_DATE, __DATE__, __TIME__);
1978                 strcat(bp, tbuf);
1979         }
1980         if (hd->proc & PR_INFO) {
1981                 sprintf(tbuf, "\nclock_freq=%02x no_sync=%02x no_dma=%d",
1982                         hd->clock_freq, hd->no_sync, hd->no_dma);
1983                 strcat(bp, tbuf);
1984                 strcat(bp, "\nsync_xfer[] =       ");
1985                 for (x = 0; x < 7; x++) {
1986                         sprintf(tbuf, "\t%02x", hd->sync_xfer[x]);
1987                         strcat(bp, tbuf);
1988                 }
1989                 strcat(bp, "\nsync_stat[] =       ");
1990                 for (x = 0; x < 7; x++) {
1991                         sprintf(tbuf, "\t%02x", hd->sync_stat[x]);
1992                         strcat(bp, tbuf);
1993                 }
1994         }
1995 #ifdef PROC_STATISTICS
1996         if (hd->proc & PR_STATISTICS) {
1997                 strcat(bp, "\ncommands issued:    ");
1998                 for (x = 0; x < 7; x++) {
1999                         sprintf(tbuf, "\t%ld", hd->cmd_cnt[x]);
2000                         strcat(bp, tbuf);
2001                 }
2002                 strcat(bp, "\ndisconnects allowed:");
2003                 for (x = 0; x < 7; x++) {
2004                         sprintf(tbuf, "\t%ld", hd->disc_allowed_cnt[x]);
2005                         strcat(bp, tbuf);
2006                 }
2007                 strcat(bp, "\ndisconnects done:   ");
2008                 for (x = 0; x < 7; x++) {
2009                         sprintf(tbuf, "\t%ld", hd->disc_done_cnt[x]);
2010                         strcat(bp, tbuf);
2011                 }
2012                 sprintf(tbuf,
2013                         "\ninterrupts: %ld, DATA_PHASE ints: %ld DMA, %ld PIO",
2014                         hd->int_cnt, hd->dma_cnt, hd->pio_cnt);
2015                 strcat(bp, tbuf);
2016         }
2017 #endif
2018         if (hd->proc & PR_CONNECTED) {
2019                 strcat(bp, "\nconnected:     ");
2020                 if (hd->connected) {
2021                         cmd = (struct scsi_cmnd *) hd->connected;
2022                         sprintf(tbuf, " %ld-%d:%d(%02x)",
2023                                 cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
2024                         strcat(bp, tbuf);
2025                 }
2026         }
2027         if (hd->proc & PR_INPUTQ) {
2028                 strcat(bp, "\ninput_Q:       ");
2029                 cmd = (struct scsi_cmnd *) hd->input_Q;
2030                 while (cmd) {
2031                         sprintf(tbuf, " %ld-%d:%d(%02x)",
2032                                 cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
2033                         strcat(bp, tbuf);
2034                         cmd = (struct scsi_cmnd *) cmd->host_scribble;
2035                 }
2036         }
2037         if (hd->proc & PR_DISCQ) {
2038                 strcat(bp, "\ndisconnected_Q:");
2039                 cmd = (struct scsi_cmnd *) hd->disconnected_Q;
2040                 while (cmd) {
2041                         sprintf(tbuf, " %ld-%d:%d(%02x)",
2042                                 cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
2043                         strcat(bp, tbuf);
2044                         cmd = (struct scsi_cmnd *) cmd->host_scribble;
2045                 }
2046         }
2047         strcat(bp, "\n");
2048         spin_unlock_irq(&hd->lock);
2049         *start = buf;
2050         if (stop) {
2051                 stop = 0;
2052                 return 0;
2053         }
2054         if (off > 0x40000)      /* ALWAYS stop after 256k bytes have been read */
2055                 stop = 1;
2056         if (hd->proc & PR_STOP) /* stop every other time */
2057                 stop = 1;
2058         return strlen(bp);
2059
2060 #else                           /* PROC_INTERFACE */
2061
2062         return 0;
2063
2064 #endif                          /* PROC_INTERFACE */
2065
2066 }
2067
2068 void
2069 wd33c93_release(void)
2070 {
2071 }
2072
2073 EXPORT_SYMBOL(wd33c93_host_reset);
2074 EXPORT_SYMBOL(wd33c93_init);
2075 EXPORT_SYMBOL(wd33c93_release);
2076 EXPORT_SYMBOL(wd33c93_abort);
2077 EXPORT_SYMBOL(wd33c93_queuecommand);
2078 EXPORT_SYMBOL(wd33c93_intr);
2079 EXPORT_SYMBOL(wd33c93_proc_info);