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