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