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