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