2 * wd33c93.c - Linux-68k device driver for the Commodore
3 * Amiga A2091/590 SCSI controller card
5 * Copyright (c) 1996 John Shifflett, GeoLog Consulting
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)
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.
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!
26 * _This_ driver is (I feel) an improvement over the old one in
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".
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.
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.
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,
60 * more speed. linked commands.
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".
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
76 #include <linux/config.h>
77 #include <linux/module.h>
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>
86 #include <linux/blk.h>
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 */
97 * Note - the following defines have been moved to 'wd33c93.h':
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:
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.
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
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.
157 * Some amiboot examples (for insmod, use 'setup_strings' instead of 'wd33c93'):
158 * - wd33c93=nosync:255
161 * - wd33c93=disconnect:2,nosync:0x08,period:250
162 * - wd33c93=debug:0x1c
165 /* Normally, no defaults are specified */
166 static char *setup_args[] =
167 {"","","","","","","","",""};
169 /* filled in by 'insmod' */
170 static char *setup_strings = 0;
173 MODULE_PARM(setup_strings, "s");
178 static inline uchar read_wd33c93(const wd33c93_regs regs, uchar reg_num)
180 *regs.SASR = reg_num;
186 #define READ_AUX_STAT() (*regs.SASR)
189 static inline void write_wd33c93(const wd33c93_regs regs, uchar reg_num,
192 *regs.SASR = reg_num;
199 static inline void write_wd33c93_cmd(const wd33c93_regs regs, uchar cmd)
201 *regs.SASR = WD_COMMAND;
208 static inline uchar read_1_byte(const wd33c93_regs regs)
213 write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
214 write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO|0x80);
216 asr = READ_AUX_STAT();
218 x = read_wd33c93(regs, WD_DATA);
219 } while (!(asr & ASR_INT));
224 static void write_wd33c93_count(const wd33c93_regs regs, unsigned long value)
226 *regs.SASR = WD_TRANSFER_COUNT_MSB;
228 *regs.SCMD = value >> 16;
229 *regs.SCMD = value >> 8;
235 static unsigned long read_wd33c93_count(const wd33c93_regs regs)
239 *regs.SASR = WD_TRANSFER_COUNT_MSB;
241 value = *regs.SCMD << 16;
242 value |= *regs.SCMD << 8;
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.)
254 static int is_dir_out(Scsi_Cmnd *cmd)
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:
276 static struct sx_period sx_table[] = {
287 static int round_period(unsigned int period)
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)) {
300 static uchar calc_sync_xfer(unsigned int period, unsigned int offset)
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;
312 static void wd33c93_execute(struct Scsi_Host *instance);
314 int wd33c93_queuecommand (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
316 struct WD33C93_hostdata *hostdata;
320 hostdata = (struct WD33C93_hostdata *)cmd->host->hostdata;
322 DB(DB_QUEUE_COMMAND,printk("Q-%d-%02x-%ld( ",cmd->target,cmd->cmnd[0],cmd->pid))
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
330 cmd->host_scribble = NULL;
331 cmd->scsi_done = done;
334 /* We use the Scsi_Pointer structure that's included with each command
335 * as a scratchpad (as it's intended to be used!). The handy thing about
336 * the SCp.xxx fields is that they're always associated with a given
337 * cmd, and are preserved across disconnect-reselect. This means we
338 * can pretty much ignore SAVE_POINTERS and RESTORE_POINTERS messages
339 * if we keep all the critical pointers and counters in SCp:
340 * - SCp.ptr is the pointer into the RAM buffer
341 * - SCp.this_residual is the size of that buffer
342 * - SCp.buffer points to the current scatter-gather buffer
343 * - SCp.buffers_residual tells us how many S.G. buffers there are
344 * - SCp.have_data_in is not used
345 * - SCp.sent_command is not used
346 * - SCp.phase records this command's SRCID_ER bit setting
350 cmd->SCp.buffer = (struct scatterlist *)cmd->buffer;
351 cmd->SCp.buffers_residual = cmd->use_sg - 1;
352 cmd->SCp.ptr = (char *)cmd->SCp.buffer->address;
353 cmd->SCp.this_residual = cmd->SCp.buffer->length;
356 cmd->SCp.buffer = NULL;
357 cmd->SCp.buffers_residual = 0;
358 cmd->SCp.ptr = (char *)cmd->request_buffer;
359 cmd->SCp.this_residual = cmd->request_bufflen;
362 /* WD docs state that at the conclusion of a "LEVEL2" command, the
363 * status byte can be retrieved from the LUN register. Apparently,
364 * this is the case only for *uninterrupted* LEVEL2 commands! If
365 * there are any unexpected phases entered, even if they are 100%
366 * legal (different devices may choose to do things differently),
367 * the LEVEL2 command sequence is exited. This often occurs prior
368 * to receiving the status byte, in which case the driver does a
369 * status phase interrupt and gets the status byte on its own.
370 * While such a command can then be "resumed" (ie restarted to
371 * finish up as a LEVEL2 command), the LUN register will NOT be
372 * a valid status byte at the command's conclusion, and we must
373 * use the byte obtained during the earlier interrupt. Here, we
374 * preset SCp.Status to an illegal value (0xff) so that when
375 * this command finally completes, we can tell where the actual
376 * status byte is stored.
379 cmd->SCp.Status = ILLEGAL_STATUS_BYTE;
382 * Add the cmd to the end of 'input_Q'. Note that REQUEST SENSE
383 * commands are added to the head of the queue so that the desired
384 * sense data is not lost before REQUEST_SENSE executes.
390 if (!(hostdata->input_Q) || (cmd->cmnd[0] == REQUEST_SENSE)) {
391 cmd->host_scribble = (uchar *)hostdata->input_Q;
392 hostdata->input_Q = cmd;
394 else { /* find the end of the queue */
395 for (tmp=(Scsi_Cmnd *)hostdata->input_Q; tmp->host_scribble;
396 tmp=(Scsi_Cmnd *)tmp->host_scribble)
398 tmp->host_scribble = (uchar *)cmd;
401 /* We know that there's at least one command in 'input_Q' now.
402 * Go see if any of them are runnable!
405 wd33c93_execute(cmd->host);
407 DB(DB_QUEUE_COMMAND,printk(")Q-%ld ",cmd->pid))
409 restore_flags(flags);
416 * This routine attempts to start a scsi command. If the host_card is
417 * already connected, we give up immediately. Otherwise, look through
418 * the input_Q, using the first command we find that's intended
419 * for a currently non-busy target/lun.
421 * wd33c93_execute() is always called with interrupts disabled or from
422 * the wd33c93_intr itself, which means that a wd33c93 interrupt
423 * cannot occur while we are in here.
425 static void wd33c93_execute (struct Scsi_Host *instance)
427 struct WD33C93_hostdata *hostdata = (struct WD33C93_hostdata *)instance->hostdata;
428 const wd33c93_regs regs = hostdata->regs;
429 Scsi_Cmnd *cmd, *prev;
432 DB(DB_EXECUTE,printk("EX("))
434 if (hostdata->selecting || hostdata->connected) {
436 DB(DB_EXECUTE,printk(")EX-0 "))
442 * Search through the input_Q for a command destined
443 * for an idle target/lun.
446 cmd = (Scsi_Cmnd *)hostdata->input_Q;
449 if (!(hostdata->busy[cmd->target] & (1 << cmd->lun)))
452 cmd = (Scsi_Cmnd *)cmd->host_scribble;
455 /* quit if queue empty or all possible targets are busy */
459 DB(DB_EXECUTE,printk(")EX-1 "))
464 /* remove command from queue */
467 prev->host_scribble = cmd->host_scribble;
469 hostdata->input_Q = (Scsi_Cmnd *)cmd->host_scribble;
471 #ifdef PROC_STATISTICS
472 hostdata->cmd_cnt[cmd->target]++;
476 * Start the selection process
480 write_wd33c93(regs, WD_DESTINATION_ID, cmd->target);
482 write_wd33c93(regs, WD_DESTINATION_ID, cmd->target | DSTID_DPD);
484 /* Now we need to figure out whether or not this command is a good
485 * candidate for disconnect/reselect. We guess to the best of our
486 * ability, based on a set of hierarchical rules. When several
487 * devices are operating simultaneously, disconnects are usually
488 * an advantage. In a single device system, or if only 1 device
489 * is being accessed, transfers usually go faster if disconnects
492 * + Commands should NEVER disconnect if hostdata->disconnect =
493 * DIS_NEVER (this holds for tape drives also), and ALWAYS
494 * disconnect if hostdata->disconnect = DIS_ALWAYS.
495 * + Tape drive commands should always be allowed to disconnect.
496 * + Disconnect should be allowed if disconnected_Q isn't empty.
497 * + Commands should NOT disconnect if input_Q is empty.
498 * + Disconnect should be allowed if there are commands in input_Q
499 * for a different target/lun. In this case, the other commands
500 * should be made disconnect-able, if not already.
502 * I know, I know - this code would flunk me out of any
503 * "C Programming 101" class ever offered. But it's easy
504 * to change around and experiment with for now.
507 cmd->SCp.phase = 0; /* assume no disconnect */
508 if (hostdata->disconnect == DIS_NEVER)
510 if (hostdata->disconnect == DIS_ALWAYS)
512 if (cmd->device->type == 1) /* tape drive? */
514 if (hostdata->disconnected_Q) /* other commands disconnected? */
516 if (!(hostdata->input_Q)) /* input_Q empty? */
518 for (prev=(Scsi_Cmnd *)hostdata->input_Q; prev;
519 prev=(Scsi_Cmnd *)prev->host_scribble) {
520 if ((prev->target != cmd->target) || (prev->lun != cmd->lun)) {
521 for (prev=(Scsi_Cmnd *)hostdata->input_Q; prev;
522 prev=(Scsi_Cmnd *)prev->host_scribble)
532 #ifdef PROC_STATISTICS
533 hostdata->disc_allowed_cnt[cmd->target]++;
538 write_wd33c93(regs, WD_SOURCE_ID, ((cmd->SCp.phase)?SRCID_ER:0));
540 write_wd33c93(regs, WD_TARGET_LUN, cmd->lun);
541 write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,hostdata->sync_xfer[cmd->target]);
542 hostdata->busy[cmd->target] |= (1 << cmd->lun);
544 if ((hostdata->level2 == L2_NONE) ||
545 (hostdata->sync_stat[cmd->target] == SS_UNSET)) {
548 * Do a 'Select-With-ATN' command. This will end with
549 * one of the following interrupts:
550 * CSR_RESEL_AM: failure - can try again later.
551 * CSR_TIMEOUT: failure - give up.
552 * CSR_SELECT: success - proceed.
555 hostdata->selecting = cmd;
557 /* Every target has its own synchronous transfer setting, kept in the
558 * sync_xfer array, and a corresponding status byte in sync_stat[].
559 * Each target's sync_stat[] entry is initialized to SX_UNSET, and its
560 * sync_xfer[] entry is initialized to the default/safe value. SS_UNSET
561 * means that the parameters are undetermined as yet, and that we
562 * need to send an SDTR message to this device after selection is
563 * complete: We set SS_FIRST to tell the interrupt routine to do so.
564 * If we've been asked not to try synchronous transfers on this
565 * target (and _all_ luns within it), we'll still send the SDTR message
566 * later, but at that time we'll negotiate for async by specifying a
567 * sync fifo depth of 0.
569 if (hostdata->sync_stat[cmd->target] == SS_UNSET)
570 hostdata->sync_stat[cmd->target] = SS_FIRST;
571 hostdata->state = S_SELECTING;
572 write_wd33c93_count(regs, 0); /* guarantee a DATA_PHASE interrupt */
573 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN);
579 * Do a 'Select-With-ATN-Xfer' command. This will end with
580 * one of the following interrupts:
581 * CSR_RESEL_AM: failure - can try again later.
582 * CSR_TIMEOUT: failure - give up.
583 * anything else: success - proceed.
586 hostdata->connected = cmd;
587 write_wd33c93(regs, WD_COMMAND_PHASE, 0);
589 /* copy command_descriptor_block into WD chip
590 * (take advantage of auto-incrementing)
593 *regs.SASR = WD_CDB_1;
594 for (i=0; i<cmd->cmd_len; i++)
595 *regs.SCMD = cmd->cmnd[i];
597 /* The wd33c93 only knows about Group 0, 1, and 5 commands when
598 * it's doing a 'select-and-transfer'. To be safe, we write the
599 * size of the CDB into the OWN_ID register for every case. This
600 * way there won't be problems with vendor-unique, audio, etc.
603 write_wd33c93(regs, WD_OWN_ID, cmd->cmd_len);
605 /* When doing a non-disconnect command with DMA, we can save
606 * ourselves a DATA phase interrupt later by setting everything
610 if ((cmd->SCp.phase == 0) && (hostdata->no_dma == 0)) {
611 if (hostdata->dma_setup(cmd,
612 (is_dir_out(cmd))?DATA_OUT_DIR:DATA_IN_DIR))
613 write_wd33c93_count(regs, 0); /* guarantee a DATA_PHASE interrupt */
615 write_wd33c93_count(regs, cmd->SCp.this_residual);
616 write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_DMA);
617 hostdata->dma = D_DMA_RUNNING;
621 write_wd33c93_count(regs, 0); /* guarantee a DATA_PHASE interrupt */
623 hostdata->state = S_RUNNING_LEVEL2;
624 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
628 * Since the SCSI bus can handle only 1 connection at a time,
629 * we get out of here now. If the selection fails, or when
630 * the command disconnects, we'll come back to this routine
631 * to search the input_Q again...
634 DB(DB_EXECUTE,printk("%s%ld)EX-2 ",(cmd->SCp.phase)?"d:":"",cmd->pid))
639 static void transfer_pio(const wd33c93_regs regs, uchar *buf, int cnt,
640 int data_in_dir, struct WD33C93_hostdata *hostdata)
644 DB(DB_TRANSFER,printk("(%p,%d,%s:",buf,cnt,data_in_dir?"in":"out"))
646 write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
647 write_wd33c93_count(regs, cnt);
648 write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO);
651 asr = READ_AUX_STAT();
653 *buf++ = read_wd33c93(regs, WD_DATA);
654 } while (!(asr & ASR_INT));
658 asr = READ_AUX_STAT();
660 write_wd33c93(regs, WD_DATA, *buf++);
661 } while (!(asr & ASR_INT));
664 /* Note: we are returning with the interrupt UN-cleared.
665 * Since (presumably) an entire I/O operation has
666 * completed, the bus phase is probably different, and
667 * the interrupt routine will discover this when it
668 * responds to the uncleared int.
675 static void transfer_bytes(const wd33c93_regs regs, Scsi_Cmnd *cmd,
678 struct WD33C93_hostdata *hostdata;
679 unsigned long length;
681 hostdata = (struct WD33C93_hostdata *)cmd->host->hostdata;
683 /* Normally, you'd expect 'this_residual' to be non-zero here.
684 * In a series of scatter-gather transfers, however, this
685 * routine will usually be called with 'this_residual' equal
686 * to 0 and 'buffers_residual' non-zero. This means that a
687 * previous transfer completed, clearing 'this_residual', and
688 * now we need to setup the next scatter-gather buffer as the
689 * source or destination for THIS transfer.
691 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
693 --cmd->SCp.buffers_residual;
694 cmd->SCp.this_residual = cmd->SCp.buffer->length;
695 cmd->SCp.ptr = cmd->SCp.buffer->address;
698 write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,hostdata->sync_xfer[cmd->target]);
700 /* 'hostdata->no_dma' is TRUE if we don't even want to try DMA.
701 * Update 'this_residual' and 'ptr' after 'transfer_pio()' returns.
704 if (hostdata->no_dma)
705 goto use_transfer_pio;
707 /* 'dma_setup()' will return TRUE if we can't do DMA.
708 * Update 'this_residual' and 'ptr' after 'transfer_pio()' returns.
711 else if (hostdata->dma_setup(cmd, data_in_dir)) {
713 #ifdef PROC_STATISTICS
716 transfer_pio(regs, (uchar *)cmd->SCp.ptr, cmd->SCp.this_residual,
717 data_in_dir, hostdata);
718 length = cmd->SCp.this_residual;
719 cmd->SCp.this_residual = read_wd33c93_count(regs);
720 cmd->SCp.ptr += (length - cmd->SCp.this_residual);
723 /* We are able to do DMA (in fact, the Amiga hardware is
724 * already going!), so start up the wd33c93 in DMA mode.
725 * We set 'hostdata->dma' = D_DMA_RUNNING so that when the
726 * transfer completes and causes an interrupt, we're
727 * reminded to tell the Amiga to shut down its end. We'll
728 * postpone the updating of 'this_residual' and 'ptr'
733 #ifdef PROC_STATISTICS
736 write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_DMA);
737 write_wd33c93_count(regs, cmd->SCp.this_residual);
739 if ((hostdata->level2 >= L2_DATA) ||
740 (hostdata->level2 == L2_BASIC && cmd->SCp.phase == 0)) {
741 write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
742 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
743 hostdata->state = S_RUNNING_LEVEL2;
746 write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO);
748 hostdata->dma = D_DMA_RUNNING;
754 void wd33c93_intr (struct Scsi_Host *instance)
756 struct WD33C93_hostdata *hostdata = (struct WD33C93_hostdata *)instance->hostdata;
757 const wd33c93_regs regs = hostdata->regs;
758 Scsi_Cmnd *patch, *cmd;
759 uchar asr, sr, phs, id, lun, *ucp, msg;
760 unsigned long length, flags;
762 asr = READ_AUX_STAT();
763 if (!(asr & ASR_INT) || (asr & ASR_BSY))
768 #ifdef PROC_STATISTICS
772 cmd = (Scsi_Cmnd *)hostdata->connected; /* assume we're connected */
773 sr = read_wd33c93(regs, WD_SCSI_STATUS); /* clear the interrupt */
774 phs = read_wd33c93(regs, WD_COMMAND_PHASE);
776 DB(DB_INTR,printk("{%02x:%02x-",asr,sr))
778 /* After starting a DMA transfer, the next interrupt
779 * is guaranteed to be in response to completion of
780 * the transfer. Since the Amiga DMA hardware runs in
781 * in an open-ended fashion, it needs to be told when
782 * to stop; do that here if D_DMA_RUNNING is true.
783 * Also, we have to update 'this_residual' and 'ptr'
784 * based on the contents of the TRANSFER_COUNT register,
785 * in case the device decided to do an intermediate
786 * disconnect (a device may do this if it has to do a
787 * seek, or just to be nice and let other devices have
788 * some bus time during long transfers). After doing
789 * whatever is needed, we go on and service the WD3393
790 * interrupt normally.
793 if (hostdata->dma == D_DMA_RUNNING) {
794 DB(DB_TRANSFER,printk("[%p/%d:",cmd->SCp.ptr,cmd->SCp.this_residual))
795 hostdata->dma_stop(cmd->host, cmd, 1);
796 hostdata->dma = D_DMA_OFF;
797 length = cmd->SCp.this_residual;
798 cmd->SCp.this_residual = read_wd33c93_count(regs);
799 cmd->SCp.ptr += (length - cmd->SCp.this_residual);
800 DB(DB_TRANSFER,printk("%p/%d]",cmd->SCp.ptr,cmd->SCp.this_residual))
803 /* Respond to the specific WD3393 interrupt - there are quite a few! */
808 DB(DB_INTR,printk("TIMEOUT"))
810 if (hostdata->state == S_RUNNING_LEVEL2)
811 hostdata->connected = NULL;
813 cmd = (Scsi_Cmnd *)hostdata->selecting; /* get a valid cmd */
814 hostdata->selecting = NULL;
817 cmd->result = DID_NO_CONNECT << 16;
818 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
819 hostdata->state = S_UNCONNECTED;
823 * There is a window of time within the scsi_done() path
824 * of execution where interrupts are turned back on full
825 * blast and left that way. During that time we could
826 * reconnect to a disconnected command, then we'd bomb
827 * out below. We could also end up executing two commands
828 * at _once_. ...just so you know why the restore_flags()
832 restore_flags(flags);
834 /* We are not connected to a target - check to see if there
835 * are commands waiting to be executed.
838 wd33c93_execute(instance);
842 /* Note: this interrupt should not occur in a LEVEL2 command */
846 DB(DB_INTR,printk("SELECT"))
847 hostdata->connected = cmd = (Scsi_Cmnd *)hostdata->selecting;
848 hostdata->selecting = NULL;
850 /* construct an IDENTIFY message with correct disconnect bit */
852 hostdata->outgoing_msg[0] = (0x80 | 0x00 | cmd->lun);
854 hostdata->outgoing_msg[0] |= 0x40;
856 if (hostdata->sync_stat[cmd->target] == SS_FIRST) {
858 printk(" sending SDTR ");
861 hostdata->sync_stat[cmd->target] = SS_WAITING;
863 /* Tack on a 2nd message to ask about synchronous transfers. If we've
864 * been asked to do only asynchronous transfers on this device, we
865 * request a fifo depth of 0, which is equivalent to async - should
866 * solve the problems some people have had with GVP's Guru ROM.
869 hostdata->outgoing_msg[1] = EXTENDED_MESSAGE;
870 hostdata->outgoing_msg[2] = 3;
871 hostdata->outgoing_msg[3] = EXTENDED_SDTR;
872 if (hostdata->no_sync & (1 << cmd->target)) {
873 hostdata->outgoing_msg[4] = hostdata->default_sx_per/4;
874 hostdata->outgoing_msg[5] = 0;
877 hostdata->outgoing_msg[4] = OPTIMUM_SX_PER/4;
878 hostdata->outgoing_msg[5] = OPTIMUM_SX_OFF;
880 hostdata->outgoing_len = 6;
883 hostdata->outgoing_len = 1;
885 hostdata->state = S_CONNECTED;
889 case CSR_XFER_DONE|PHS_DATA_IN:
890 case CSR_UNEXP |PHS_DATA_IN:
891 case CSR_SRV_REQ |PHS_DATA_IN:
892 DB(DB_INTR,printk("IN-%d.%d",cmd->SCp.this_residual,cmd->SCp.buffers_residual))
893 transfer_bytes(regs, cmd, DATA_IN_DIR);
894 if (hostdata->state != S_RUNNING_LEVEL2)
895 hostdata->state = S_CONNECTED;
899 case CSR_XFER_DONE|PHS_DATA_OUT:
900 case CSR_UNEXP |PHS_DATA_OUT:
901 case CSR_SRV_REQ |PHS_DATA_OUT:
902 DB(DB_INTR,printk("OUT-%d.%d",cmd->SCp.this_residual,cmd->SCp.buffers_residual))
903 transfer_bytes(regs, cmd, DATA_OUT_DIR);
904 if (hostdata->state != S_RUNNING_LEVEL2)
905 hostdata->state = S_CONNECTED;
909 /* Note: this interrupt should not occur in a LEVEL2 command */
911 case CSR_XFER_DONE|PHS_COMMAND:
912 case CSR_UNEXP |PHS_COMMAND:
913 case CSR_SRV_REQ |PHS_COMMAND:
914 DB(DB_INTR,printk("CMND-%02x,%ld",cmd->cmnd[0],cmd->pid))
915 transfer_pio(regs, cmd->cmnd, cmd->cmd_len, DATA_OUT_DIR, hostdata);
916 hostdata->state = S_CONNECTED;
920 case CSR_XFER_DONE|PHS_STATUS:
921 case CSR_UNEXP |PHS_STATUS:
922 case CSR_SRV_REQ |PHS_STATUS:
923 DB(DB_INTR,printk("STATUS="))
925 cmd->SCp.Status = read_1_byte(regs);
926 DB(DB_INTR,printk("%02x",cmd->SCp.Status))
927 if (hostdata->level2 >= L2_BASIC) {
928 sr = read_wd33c93(regs, WD_SCSI_STATUS); /* clear interrupt */
929 hostdata->state = S_RUNNING_LEVEL2;
930 write_wd33c93(regs, WD_COMMAND_PHASE, 0x50);
931 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
934 hostdata->state = S_CONNECTED;
939 case CSR_XFER_DONE|PHS_MESS_IN:
940 case CSR_UNEXP |PHS_MESS_IN:
941 case CSR_SRV_REQ |PHS_MESS_IN:
942 DB(DB_INTR,printk("MSG_IN="))
944 msg = read_1_byte(regs);
945 sr = read_wd33c93(regs, WD_SCSI_STATUS); /* clear interrupt */
947 hostdata->incoming_msg[hostdata->incoming_ptr] = msg;
948 if (hostdata->incoming_msg[0] == EXTENDED_MESSAGE)
949 msg = EXTENDED_MESSAGE;
951 hostdata->incoming_ptr = 0;
953 cmd->SCp.Message = msg;
956 case COMMAND_COMPLETE:
957 DB(DB_INTR,printk("CCMP-%ld",cmd->pid))
958 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
959 hostdata->state = S_PRE_CMP_DISC;
963 DB(DB_INTR,printk("SDP"))
964 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
965 hostdata->state = S_CONNECTED;
968 case RESTORE_POINTERS:
969 DB(DB_INTR,printk("RDP"))
970 if (hostdata->level2 >= L2_BASIC) {
971 write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
972 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
973 hostdata->state = S_RUNNING_LEVEL2;
976 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
977 hostdata->state = S_CONNECTED;
982 DB(DB_INTR,printk("DIS"))
983 cmd->device->disconnect = 1;
984 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
985 hostdata->state = S_PRE_TMP_DISC;
989 DB(DB_INTR,printk("REJ"))
993 if (hostdata->sync_stat[cmd->target] == SS_WAITING)
994 hostdata->sync_stat[cmd->target] = SS_SET;
995 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
996 hostdata->state = S_CONNECTED;
999 case EXTENDED_MESSAGE:
1000 DB(DB_INTR,printk("EXT"))
1002 ucp = hostdata->incoming_msg;
1005 printk("%02x",ucp[hostdata->incoming_ptr]);
1007 /* Is this the last byte of the extended message? */
1009 if ((hostdata->incoming_ptr >= 2) &&
1010 (hostdata->incoming_ptr == (ucp[1] + 1))) {
1012 switch (ucp[2]) { /* what's the EXTENDED code? */
1014 id = calc_sync_xfer(ucp[3],ucp[4]);
1015 if (hostdata->sync_stat[cmd->target] != SS_WAITING) {
1017 /* A device has sent an unsolicited SDTR message; rather than go
1018 * through the effort of decoding it and then figuring out what
1019 * our reply should be, we're just gonna say that we have a
1020 * synchronous fifo depth of 0. This will result in asynchronous
1021 * transfers - not ideal but so much easier.
1022 * Actually, this is OK because it assures us that if we don't
1023 * specifically ask for sync transfers, we won't do any.
1026 write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
1027 hostdata->outgoing_msg[0] = EXTENDED_MESSAGE;
1028 hostdata->outgoing_msg[1] = 3;
1029 hostdata->outgoing_msg[2] = EXTENDED_SDTR;
1030 hostdata->outgoing_msg[3] = hostdata->default_sx_per/4;
1031 hostdata->outgoing_msg[4] = 0;
1032 hostdata->outgoing_len = 5;
1033 hostdata->sync_xfer[cmd->target] =
1034 calc_sync_xfer(hostdata->default_sx_per/4,0);
1037 hostdata->sync_xfer[cmd->target] = id;
1040 printk("sync_xfer=%02x",hostdata->sync_xfer[cmd->target]);
1042 hostdata->sync_stat[cmd->target] = SS_SET;
1043 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1044 hostdata->state = S_CONNECTED;
1047 write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
1048 printk("sending WDTR ");
1049 hostdata->outgoing_msg[0] = EXTENDED_MESSAGE;
1050 hostdata->outgoing_msg[1] = 2;
1051 hostdata->outgoing_msg[2] = EXTENDED_WDTR;
1052 hostdata->outgoing_msg[3] = 0; /* 8 bit transfer width */
1053 hostdata->outgoing_len = 4;
1054 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1055 hostdata->state = S_CONNECTED;
1058 write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
1059 printk("Rejecting Unknown Extended Message(%02x). ",ucp[2]);
1060 hostdata->outgoing_msg[0] = MESSAGE_REJECT;
1061 hostdata->outgoing_len = 1;
1062 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1063 hostdata->state = S_CONNECTED;
1066 hostdata->incoming_ptr = 0;
1069 /* We need to read more MESS_IN bytes for the extended message */
1072 hostdata->incoming_ptr++;
1073 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1074 hostdata->state = S_CONNECTED;
1079 printk("Rejecting Unknown Message(%02x) ",msg);
1080 write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
1081 hostdata->outgoing_msg[0] = MESSAGE_REJECT;
1082 hostdata->outgoing_len = 1;
1083 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1084 hostdata->state = S_CONNECTED;
1086 restore_flags(flags);
1090 /* Note: this interrupt will occur only after a LEVEL2 command */
1092 case CSR_SEL_XFER_DONE:
1094 /* Make sure that reselection is enabled at this point - it may
1095 * have been turned off for the command that just completed.
1098 write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
1100 DB(DB_INTR,printk("SX-DONE-%ld",cmd->pid))
1101 cmd->SCp.Message = COMMAND_COMPLETE;
1102 lun = read_wd33c93(regs, WD_TARGET_LUN);
1103 DB(DB_INTR,printk(":%d.%d",cmd->SCp.Status,lun))
1104 hostdata->connected = NULL;
1105 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
1106 hostdata->state = S_UNCONNECTED;
1107 if (cmd->SCp.Status == ILLEGAL_STATUS_BYTE)
1108 cmd->SCp.Status = lun;
1109 if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
1110 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1112 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1113 cmd->scsi_done(cmd);
1115 /* We are no longer connected to a target - check to see if
1116 * there are commands waiting to be executed.
1118 restore_flags(flags);
1119 wd33c93_execute(instance);
1122 printk("%02x:%02x:%02x-%ld: Unknown SEL_XFER_DONE phase!!---",asr,sr,phs,cmd->pid);
1127 /* Note: this interrupt will occur only after a LEVEL2 command */
1130 DB(DB_INTR,printk("SDP"))
1131 hostdata->state = S_RUNNING_LEVEL2;
1132 write_wd33c93(regs, WD_COMMAND_PHASE, 0x41);
1133 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
1137 case CSR_XFER_DONE|PHS_MESS_OUT:
1138 case CSR_UNEXP |PHS_MESS_OUT:
1139 case CSR_SRV_REQ |PHS_MESS_OUT:
1140 DB(DB_INTR,printk("MSG_OUT="))
1142 /* To get here, we've probably requested MESSAGE_OUT and have
1143 * already put the correct bytes in outgoing_msg[] and filled
1144 * in outgoing_len. We simply send them out to the SCSI bus.
1145 * Sometimes we get MESSAGE_OUT phase when we're not expecting
1146 * it - like when our SDTR message is rejected by a target. Some
1147 * targets send the REJECT before receiving all of the extended
1148 * message, and then seem to go back to MESSAGE_OUT for a byte
1149 * or two. Not sure why, or if I'm doing something wrong to
1150 * cause this to happen. Regardless, it seems that sending
1151 * NOP messages in these situations results in no harm and
1152 * makes everyone happy.
1155 if (hostdata->outgoing_len == 0) {
1156 hostdata->outgoing_len = 1;
1157 hostdata->outgoing_msg[0] = NOP;
1159 transfer_pio(regs, hostdata->outgoing_msg, hostdata->outgoing_len,
1160 DATA_OUT_DIR, hostdata);
1161 DB(DB_INTR,printk("%02x",hostdata->outgoing_msg[0]))
1162 hostdata->outgoing_len = 0;
1163 hostdata->state = S_CONNECTED;
1167 case CSR_UNEXP_DISC:
1169 /* I think I've seen this after a request-sense that was in response
1170 * to an error condition, but not sure. We certainly need to do
1171 * something when we get this interrupt - the question is 'what?'.
1172 * Let's think positively, and assume some command has finished
1173 * in a legal manner (like a command that provokes a request-sense),
1174 * so we treat it as a normal command-complete-disconnect.
1177 /* Make sure that reselection is enabled at this point - it may
1178 * have been turned off for the command that just completed.
1181 write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
1183 printk(" - Already disconnected! ");
1184 hostdata->state = S_UNCONNECTED;
1187 DB(DB_INTR,printk("UNEXP_DISC-%ld",cmd->pid))
1188 hostdata->connected = NULL;
1189 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
1190 hostdata->state = S_UNCONNECTED;
1191 if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
1192 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1194 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1195 cmd->scsi_done(cmd);
1197 /* We are no longer connected to a target - check to see if
1198 * there are commands waiting to be executed.
1200 /* look above for comments on scsi_done() */
1201 restore_flags(flags);
1202 wd33c93_execute(instance);
1208 /* Make sure that reselection is enabled at this point - it may
1209 * have been turned off for the command that just completed.
1212 write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
1213 DB(DB_INTR,printk("DISC-%ld",cmd->pid))
1215 printk(" - Already disconnected! ");
1216 hostdata->state = S_UNCONNECTED;
1218 switch (hostdata->state) {
1219 case S_PRE_CMP_DISC:
1220 hostdata->connected = NULL;
1221 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
1222 hostdata->state = S_UNCONNECTED;
1223 DB(DB_INTR,printk(":%d",cmd->SCp.Status))
1224 if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
1225 cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1227 cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1228 cmd->scsi_done(cmd);
1229 restore_flags(flags);
1231 case S_PRE_TMP_DISC:
1232 case S_RUNNING_LEVEL2:
1233 cmd->host_scribble = (uchar *)hostdata->disconnected_Q;
1234 hostdata->disconnected_Q = cmd;
1235 hostdata->connected = NULL;
1236 hostdata->state = S_UNCONNECTED;
1238 #ifdef PROC_STATISTICS
1239 hostdata->disc_done_cnt[cmd->target]++;
1244 printk("*** Unexpected DISCONNECT interrupt! ***");
1245 hostdata->state = S_UNCONNECTED;
1248 /* We are no longer connected to a target - check to see if
1249 * there are commands waiting to be executed.
1251 wd33c93_execute(instance);
1257 DB(DB_INTR,printk("RESEL%s", sr == CSR_RESEL_AM ? "_AM" : ""))
1259 /* Old chips (pre -A ???) don't have advanced features and will
1260 * generate CSR_RESEL. In that case we have to extract the LUN the
1261 * hard way (see below).
1262 * First we have to make sure this reselection didn't
1263 * happen during Arbitration/Selection of some other device.
1264 * If yes, put losing command back on top of input_Q.
1267 if (hostdata->level2 <= L2_NONE) {
1269 if (hostdata->selecting) {
1270 cmd = (Scsi_Cmnd *)hostdata->selecting;
1271 hostdata->selecting = NULL;
1272 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
1273 cmd->host_scribble = (uchar *)hostdata->input_Q;
1274 hostdata->input_Q = cmd;
1282 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
1283 cmd->host_scribble = (uchar *)hostdata->input_Q;
1284 hostdata->input_Q = cmd;
1287 printk("---%02x:%02x:%02x-TROUBLE: Intrusive ReSelect!---",asr,sr,phs);
1295 /* OK - find out which device reselected us. */
1297 id = read_wd33c93(regs, WD_SOURCE_ID);
1300 /* and extract the lun from the ID message. (Note that we don't
1301 * bother to check for a valid message here - I guess this is
1302 * not the right way to go, but...)
1305 if (sr == CSR_RESEL_AM) {
1306 lun = read_wd33c93(regs, WD_DATA);
1307 if (hostdata->level2 < L2_RESELECT)
1308 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1312 /* Old chip; wait for msgin phase to pick up the LUN. */
1313 for (lun = 255; lun; lun--) {
1314 if ((asr = READ_AUX_STAT()) & ASR_INT)
1318 if (!(asr & ASR_INT)) {
1319 printk("wd33c93: Reselected without IDENTIFY\n");
1323 /* Verify this is a change to MSG_IN and read the message */
1324 sr = read_wd33c93(regs, WD_SCSI_STATUS);
1325 if (sr == (CSR_ABORT | PHS_MESS_IN) ||
1326 sr == (CSR_UNEXP | PHS_MESS_IN) ||
1327 sr == (CSR_SRV_REQ | PHS_MESS_IN)) {
1328 /* Got MSG_IN, grab target LUN */
1329 lun = read_1_byte(regs);
1330 /* Now we expect a 'paused with ACK asserted' int.. */
1331 asr = READ_AUX_STAT();
1332 if (!(asr & ASR_INT)) {
1334 asr = READ_AUX_STAT();
1335 if (!(asr & ASR_INT))
1336 printk("wd33c93: No int after LUN on RESEL (%02x)\n",
1339 sr = read_wd33c93(regs, WD_SCSI_STATUS);
1340 if (sr != CSR_MSGIN)
1341 printk("wd33c93: Not paused with ACK on RESEL (%02x)\n",
1344 write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
1347 printk("wd33c93: Not MSG_IN on reselect (%02x)\n", sr);
1353 /* Now we look for the command that's reconnecting. */
1355 cmd = (Scsi_Cmnd *)hostdata->disconnected_Q;
1358 if (id == cmd->target && lun == cmd->lun)
1361 cmd = (Scsi_Cmnd *)cmd->host_scribble;
1364 /* Hmm. Couldn't find a valid command.... What to do? */
1367 printk("---TROUBLE: target %d.%d not in disconnect queue---",id,lun);
1371 /* Ok, found the command - now start it up again. */
1374 patch->host_scribble = cmd->host_scribble;
1376 hostdata->disconnected_Q = (Scsi_Cmnd *)cmd->host_scribble;
1377 hostdata->connected = cmd;
1379 /* We don't need to worry about 'initialize_SCp()' or 'hostdata->busy[]'
1380 * because these things are preserved over a disconnect.
1381 * But we DO need to fix the DPD bit so it's correct for this command.
1384 if (is_dir_out(cmd))
1385 write_wd33c93(regs, WD_DESTINATION_ID, cmd->target);
1387 write_wd33c93(regs, WD_DESTINATION_ID, cmd->target | DSTID_DPD);
1388 if (hostdata->level2 >= L2_RESELECT) {
1389 write_wd33c93_count(regs, 0); /* we want a DATA_PHASE interrupt */
1390 write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
1391 write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
1392 hostdata->state = S_RUNNING_LEVEL2;
1395 hostdata->state = S_CONNECTED;
1397 DB(DB_INTR,printk("-%ld",cmd->pid))
1401 printk("--UNKNOWN INTERRUPT:%02x:%02x:%02x--",asr,sr,phs);
1404 DB(DB_INTR,printk("} "))
1410 void reset_wd33c93(struct Scsi_Host *instance)
1412 struct WD33C93_hostdata *hostdata = (struct WD33C93_hostdata *)instance->hostdata;
1413 const wd33c93_regs regs = hostdata->regs;
1416 #ifdef CONFIG_SGI_IP22
1419 extern void sgiwd93_reset(unsigned long);
1420 /* wait 'til the chip gets some time for us */
1421 while ((READ_AUX_STAT() & ASR_BSY) && busycount++ < 100)
1424 * there are scsi devices out there, which manage to lock up
1425 * the wd33c93 in a busy condition. In this state it won't
1426 * accept the reset command. The only way to solve this is to
1427 * give the chip a hardware reset (if possible). The code below
1428 * does this for the SGI Indy, where this is possible
1431 if (READ_AUX_STAT() & ASR_BSY)
1432 sgiwd93_reset(instance->base); /* yeah, give it the hard one */
1436 write_wd33c93(regs, WD_OWN_ID, OWNID_EAF | OWNID_RAF |
1437 instance->this_id | hostdata->clock_freq);
1438 write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1439 write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,
1440 calc_sync_xfer(hostdata->default_sx_per/4,DEFAULT_SX_OFF));
1441 write_wd33c93(regs, WD_COMMAND, WD_CMD_RESET);
1442 #ifdef CONFIG_MVME147_SCSI
1443 udelay(25); /* The old wd33c93 on MVME147 needs this, at least */
1446 while (!(READ_AUX_STAT() & ASR_INT))
1448 sr = read_wd33c93(regs, WD_SCSI_STATUS);
1450 hostdata->microcode = read_wd33c93(regs, WD_CDB_1);
1452 hostdata->chip = C_WD33C93;
1453 else if (sr == 0x01) {
1454 write_wd33c93(regs, WD_QUEUE_TAG, 0xa5); /* any random number */
1455 sr = read_wd33c93(regs, WD_QUEUE_TAG);
1457 hostdata->chip = C_WD33C93B;
1458 write_wd33c93(regs, WD_QUEUE_TAG, 0);
1461 hostdata->chip = C_WD33C93A;
1464 hostdata->chip = C_UNKNOWN_CHIP;
1466 write_wd33c93(regs, WD_TIMEOUT_PERIOD, TIMEOUT_PERIOD_VALUE);
1467 write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1472 int wd33c93_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
1474 struct Scsi_Host *instance;
1475 struct WD33C93_hostdata *hostdata;
1478 instance = SCpnt->host;
1479 hostdata = (struct WD33C93_hostdata *)instance->hostdata;
1481 printk("scsi%d: reset. ", instance->host_no);
1482 disable_irq(instance->irq);
1484 ((struct WD33C93_hostdata *)instance->hostdata)->dma_stop(instance,NULL,0);
1485 for (i = 0; i < 8; i++) {
1486 hostdata->busy[i] = 0;
1487 hostdata->sync_xfer[i] = calc_sync_xfer(DEFAULT_SX_PER/4,DEFAULT_SX_OFF);
1488 hostdata->sync_stat[i] = SS_UNSET; /* using default sync values */
1490 hostdata->input_Q = NULL;
1491 hostdata->selecting = NULL;
1492 hostdata->connected = NULL;
1493 hostdata->disconnected_Q = NULL;
1494 hostdata->state = S_UNCONNECTED;
1495 hostdata->dma = D_DMA_OFF;
1496 hostdata->incoming_ptr = 0;
1497 hostdata->outgoing_len = 0;
1499 reset_wd33c93(instance);
1500 SCpnt->result = DID_RESET << 16;
1501 enable_irq(instance->irq);
1507 int wd33c93_abort (Scsi_Cmnd *cmd)
1509 struct Scsi_Host *instance;
1510 struct WD33C93_hostdata *hostdata;
1512 Scsi_Cmnd *tmp, *prev;
1514 disable_irq(cmd->host->irq);
1516 instance = cmd->host;
1517 hostdata = (struct WD33C93_hostdata *)instance->hostdata;
1518 regs = hostdata->regs;
1521 * Case 1 : If the command hasn't been issued yet, we simply remove it
1525 tmp = (Scsi_Cmnd *)hostdata->input_Q;
1530 prev->host_scribble = cmd->host_scribble;
1532 hostdata->input_Q = (Scsi_Cmnd *)cmd->host_scribble;
1533 cmd->host_scribble = NULL;
1534 cmd->result = DID_ABORT << 16;
1535 printk("scsi%d: Abort - removing command %ld from input_Q. ",
1536 instance->host_no, cmd->pid);
1537 enable_irq(cmd->host->irq);
1538 cmd->scsi_done(cmd);
1539 return SCSI_ABORT_SUCCESS;
1542 tmp = (Scsi_Cmnd *)tmp->host_scribble;
1546 * Case 2 : If the command is connected, we're going to fail the abort
1547 * and let the high level SCSI driver retry at a later time or
1550 * Timeouts, and therefore aborted commands, will be highly unlikely
1551 * and handling them cleanly in this situation would make the common
1552 * case of noresets less efficient, and would pollute our code. So,
1556 if (hostdata->connected == cmd) {
1558 unsigned long timeout;
1560 printk("scsi%d: Aborting connected command %ld - ",
1561 instance->host_no, cmd->pid);
1563 printk("stopping DMA - ");
1564 if (hostdata->dma == D_DMA_RUNNING) {
1565 hostdata->dma_stop(instance, cmd, 0);
1566 hostdata->dma = D_DMA_OFF;
1569 printk("sending wd33c93 ABORT command - ");
1570 write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1571 write_wd33c93_cmd(regs, WD_CMD_ABORT);
1573 /* Now we have to attempt to flush out the FIFO... */
1575 printk("flushing fifo - ");
1578 asr = READ_AUX_STAT();
1580 read_wd33c93(regs, WD_DATA);
1581 } while (!(asr & ASR_INT) && timeout-- > 0);
1582 sr = read_wd33c93(regs, WD_SCSI_STATUS);
1583 printk("asr=%02x, sr=%02x, %ld bytes un-transferred (timeout=%ld) - ",
1584 asr, sr, read_wd33c93_count(regs), timeout);
1587 * Abort command processed.
1589 * We must disconnect.
1592 printk("sending wd33c93 DISCONNECT command - ");
1593 write_wd33c93_cmd(regs, WD_CMD_DISCONNECT);
1596 asr = READ_AUX_STAT();
1597 while ((asr & ASR_CIP) && timeout-- > 0)
1598 asr = READ_AUX_STAT();
1599 sr = read_wd33c93(regs, WD_SCSI_STATUS);
1600 printk("asr=%02x, sr=%02x.",asr,sr);
1602 hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
1603 hostdata->connected = NULL;
1604 hostdata->state = S_UNCONNECTED;
1605 cmd->result = DID_ABORT << 16;
1608 wd33c93_execute (instance);
1610 enable_irq(cmd->host->irq);
1611 cmd->scsi_done(cmd);
1612 return SCSI_ABORT_SUCCESS;
1616 * Case 3: If the command is currently disconnected from the bus,
1617 * we're not going to expend much effort here: Let's just return
1618 * an ABORT_SNOOZE and hope for the best...
1621 tmp = (Scsi_Cmnd *)hostdata->disconnected_Q;
1624 printk("scsi%d: Abort - command %ld found on disconnected_Q - ",
1625 instance->host_no, cmd->pid);
1626 printk("returning ABORT_SNOOZE. ");
1627 enable_irq(cmd->host->irq);
1628 return SCSI_ABORT_SNOOZE;
1630 tmp = (Scsi_Cmnd *)tmp->host_scribble;
1634 * Case 4 : If we reached this point, the command was not found in any of
1637 * We probably reached this point because of an unlikely race condition
1638 * between the command completing successfully and the abortion code,
1639 * so we won't panic, but we will notify the user in case something really
1644 wd33c93_execute (instance);
1646 enable_irq(cmd->host->irq);
1647 printk("scsi%d: warning : SCSI command probably completed successfully"
1648 " before abortion. ", instance->host_no);
1649 return SCSI_ABORT_NOT_RUNNING;
1654 #define MAX_WD33C93_HOSTS 4
1655 #define MAX_SETUP_ARGS ((int)(sizeof(setup_args) / sizeof(char *)))
1656 #define SETUP_BUFFER_SIZE 200
1657 static char setup_buffer[SETUP_BUFFER_SIZE];
1658 static char setup_used[MAX_SETUP_ARGS];
1659 static int done_setup = 0;
1661 int wd33c93_setup (char *str)
1666 /* The kernel does some processing of the command-line before calling
1667 * this function: If it begins with any decimal or hex number arguments,
1668 * ints[0] = how many numbers found and ints[1] through [n] are the values
1669 * themselves. str points to where the non-numeric arguments (if any)
1670 * start: We do our own parsing of those. We construct synthetic 'nosync'
1671 * keywords out of numeric args (to maintain compatibility with older
1672 * versions) and then add the rest of the arguments.
1679 * Old style command line arguments are now dead
1682 for (i=0; i<ints[0]; i++) {
1683 x = vsprintf(p1,"nosync:0x%02x,",&(ints[i+1]));
1689 strncpy(p1, str, SETUP_BUFFER_SIZE - strlen(setup_buffer));
1690 setup_buffer[SETUP_BUFFER_SIZE - 1] = '\0';
1693 while (*p1 && (i < MAX_SETUP_ARGS)) {
1694 p2 = strchr(p1, ',');
1707 for (i=0; i<MAX_SETUP_ARGS; i++)
1714 __setup("wd33c93", wd33c93_setup);
1717 /* check_setup_args() returns index if key found, 0 if not
1720 static int check_setup_args(char *key, int *flags, int *val, char *buf)
1725 for (x=0; x<MAX_SETUP_ARGS; x++) {
1728 if (!strncmp(setup_args[x], key, strlen(key)))
1730 if (!strncmp(setup_args[x], "next", strlen("next")))
1733 if (x == MAX_SETUP_ARGS)
1736 cp = setup_args[x] + strlen(key);
1741 if ((*cp >= '0') && (*cp <= '9')) {
1742 *val = simple_strtoul(cp,NULL,0);
1749 void wd33c93_init(struct Scsi_Host *instance, const wd33c93_regs regs,
1750 dma_setup_t setup, dma_stop_t stop, int clock_freq)
1752 struct WD33C93_hostdata *hostdata;
1758 if (!done_setup && setup_strings)
1759 wd33c93_setup(setup_strings);
1761 hostdata = (struct WD33C93_hostdata *)instance->hostdata;
1763 hostdata->regs = regs;
1764 hostdata->clock_freq = clock_freq;
1765 hostdata->dma_setup = setup;
1766 hostdata->dma_stop = stop;
1767 hostdata->dma_bounce_buffer = NULL;
1768 hostdata->dma_bounce_len = 0;
1769 for (i = 0; i < 8; i++) {
1770 hostdata->busy[i] = 0;
1771 hostdata->sync_xfer[i] = calc_sync_xfer(DEFAULT_SX_PER/4,DEFAULT_SX_OFF);
1772 hostdata->sync_stat[i] = SS_UNSET; /* using default sync values */
1773 #ifdef PROC_STATISTICS
1774 hostdata->cmd_cnt[i] = 0;
1775 hostdata->disc_allowed_cnt[i] = 0;
1776 hostdata->disc_done_cnt[i] = 0;
1779 hostdata->input_Q = NULL;
1780 hostdata->selecting = NULL;
1781 hostdata->connected = NULL;
1782 hostdata->disconnected_Q = NULL;
1783 hostdata->state = S_UNCONNECTED;
1784 hostdata->dma = D_DMA_OFF;
1785 hostdata->level2 = L2_BASIC;
1786 hostdata->disconnect = DIS_ADAPTIVE;
1787 hostdata->args = DEBUG_DEFAULTS;
1788 hostdata->incoming_ptr = 0;
1789 hostdata->outgoing_len = 0;
1790 hostdata->default_sx_per = DEFAULT_SX_PER;
1791 hostdata->no_sync = 0xff; /* sync defaults to off */
1792 hostdata->no_dma = 0; /* default is DMA enabled */
1794 #ifdef PROC_INTERFACE
1795 hostdata->proc = PR_VERSION|PR_INFO|PR_STATISTICS|
1796 PR_CONNECTED|PR_INPUTQ|PR_DISCQ|
1798 #ifdef PROC_STATISTICS
1799 hostdata->dma_cnt = 0;
1800 hostdata->pio_cnt = 0;
1801 hostdata->int_cnt = 0;
1806 if (check_setup_args("nosync",&flags,&val,buf))
1807 hostdata->no_sync = val;
1809 if (check_setup_args("nodma",&flags,&val,buf))
1810 hostdata->no_dma = (val == -1) ? 1 : val;
1812 if (check_setup_args("period",&flags,&val,buf))
1813 hostdata->default_sx_per = sx_table[round_period((unsigned int)val)].period_ns;
1815 if (check_setup_args("disconnect",&flags,&val,buf)) {
1816 if ((val >= DIS_NEVER) && (val <= DIS_ALWAYS))
1817 hostdata->disconnect = val;
1819 hostdata->disconnect = DIS_ADAPTIVE;
1822 if (check_setup_args("level2",&flags,&val,buf))
1823 hostdata->level2 = val;
1825 if (check_setup_args("debug",&flags,&val,buf))
1826 hostdata->args = val & DB_MASK;
1828 if (check_setup_args("clock",&flags,&val,buf)) {
1829 if (val>7 && val<11)
1830 val = WD33C93_FS_8_10;
1831 else if (val>11 && val<16)
1832 val = WD33C93_FS_12_15;
1833 else if (val>15 && val<21)
1834 val = WD33C93_FS_16_20;
1836 val = WD33C93_FS_8_10;
1837 hostdata->clock_freq = val;
1840 if ((i = check_setup_args("next",&flags,&val,buf))) {
1842 setup_used[--i] = 1;
1845 #ifdef PROC_INTERFACE
1846 if (check_setup_args("proc",&flags,&val,buf))
1847 hostdata->proc = val;
1851 { unsigned long flags;
1854 reset_wd33c93(instance);
1855 restore_flags(flags);
1858 printk("wd33c93-%d: chip=%s/%d no_sync=0x%x no_dma=%d",instance->host_no,
1859 (hostdata->chip==C_WD33C93)?"WD33c93":
1860 (hostdata->chip==C_WD33C93A)?"WD33c93A":
1861 (hostdata->chip==C_WD33C93B)?"WD33c93B":"unknown",
1862 hostdata->microcode,hostdata->no_sync,hostdata->no_dma);
1864 printk(" debug_flags=0x%02x\n",hostdata->args);
1866 printk(" debugging=OFF\n");
1868 printk(" setup_args=");
1869 for (i=0; i<MAX_SETUP_ARGS; i++)
1870 printk("%s,",setup_args[i]);
1872 printk(" Version %s - %s, Compiled %s at %s\n",
1873 WD33C93_VERSION,WD33C93_DATE,__DATE__,__TIME__);
1878 int wd33c93_proc_info(char *buf, char **start, off_t off, int len, int hn, int in)
1881 #ifdef PROC_INTERFACE
1885 unsigned long flags;
1886 struct Scsi_Host *instance;
1887 struct WD33C93_hostdata *hd;
1890 static int stop = 0;
1892 for (instance=scsi_hostlist; instance; instance=instance->next) {
1893 if (instance->host_no == hn)
1897 printk("*** Hmm... Can't find host #%d!\n",hn);
1900 hd = (struct WD33C93_hostdata *)instance->hostdata;
1902 /* If 'in' is TRUE we need to _read_ the proc file. We accept the following
1903 * keywords (same format as command-line, but only ONE per read):
1915 if (!strncmp(bp,"debug:",6)) {
1917 hd->args = simple_strtoul(bp,NULL,0) & DB_MASK;
1919 else if (!strncmp(bp,"disconnect:",11)) {
1921 x = simple_strtoul(bp,NULL,0);
1922 if (x < DIS_NEVER || x > DIS_ALWAYS)
1926 else if (!strncmp(bp,"period:",7)) {
1928 x = simple_strtoul(bp,NULL,0);
1929 hd->default_sx_per = sx_table[round_period((unsigned int)x)].period_ns;
1931 else if (!strncmp(bp,"resync:",7)) {
1933 x = simple_strtoul(bp,NULL,0);
1936 hd->sync_stat[i] = SS_UNSET;
1938 else if (!strncmp(bp,"proc:",5)) {
1940 hd->proc = simple_strtoul(bp,NULL,0);
1942 else if (!strncmp(bp,"nodma:",6)) {
1944 hd->no_dma = simple_strtoul(bp,NULL,0);
1946 else if (!strncmp(bp,"level2:",7)) {
1948 hd->level2 = simple_strtoul(bp,NULL,0);
1957 if (hd->proc & PR_VERSION) {
1958 sprintf(tbuf,"\nVersion %s - %s. Compiled %s %s",
1959 WD33C93_VERSION,WD33C93_DATE,__DATE__,__TIME__);
1962 if (hd->proc & PR_INFO) {
1963 sprintf(tbuf,"\nclock_freq=%02x no_sync=%02x no_dma=%d",
1964 hd->clock_freq,hd->no_sync,hd->no_dma);
1966 strcat(bp,"\nsync_xfer[] = ");
1967 for (x=0; x<7; x++) {
1968 sprintf(tbuf,"\t%02x",hd->sync_xfer[x]);
1971 strcat(bp,"\nsync_stat[] = ");
1972 for (x=0; x<7; x++) {
1973 sprintf(tbuf,"\t%02x",hd->sync_stat[x]);
1977 #ifdef PROC_STATISTICS
1978 if (hd->proc & PR_STATISTICS) {
1979 strcat(bp,"\ncommands issued: ");
1980 for (x=0; x<7; x++) {
1981 sprintf(tbuf,"\t%ld",hd->cmd_cnt[x]);
1984 strcat(bp,"\ndisconnects allowed:");
1985 for (x=0; x<7; x++) {
1986 sprintf(tbuf,"\t%ld",hd->disc_allowed_cnt[x]);
1989 strcat(bp,"\ndisconnects done: ");
1990 for (x=0; x<7; x++) {
1991 sprintf(tbuf,"\t%ld",hd->disc_done_cnt[x]);
1994 sprintf(tbuf,"\ninterrupts: %ld, DATA_PHASE ints: %ld DMA, %ld PIO",
1995 hd->int_cnt,hd->dma_cnt,hd->pio_cnt);
1999 if (hd->proc & PR_CONNECTED) {
2000 strcat(bp,"\nconnected: ");
2001 if (hd->connected) {
2002 cmd = (Scsi_Cmnd *)hd->connected;
2003 sprintf(tbuf," %ld-%d:%d(%02x)",
2004 cmd->pid, cmd->target, cmd->lun, cmd->cmnd[0]);
2008 if (hd->proc & PR_INPUTQ) {
2009 strcat(bp,"\ninput_Q: ");
2010 cmd = (Scsi_Cmnd *)hd->input_Q;
2012 sprintf(tbuf," %ld-%d:%d(%02x)",
2013 cmd->pid, cmd->target, cmd->lun, cmd->cmnd[0]);
2015 cmd = (Scsi_Cmnd *)cmd->host_scribble;
2018 if (hd->proc & PR_DISCQ) {
2019 strcat(bp,"\ndisconnected_Q:");
2020 cmd = (Scsi_Cmnd *)hd->disconnected_Q;
2022 sprintf(tbuf," %ld-%d:%d(%02x)",
2023 cmd->pid, cmd->target, cmd->lun, cmd->cmnd[0]);
2025 cmd = (Scsi_Cmnd *)cmd->host_scribble;
2029 restore_flags(flags);
2035 if (off > 0x40000) /* ALWAYS stop after 256k bytes have been read */
2037 if (hd->proc & PR_STOP) /* stop every other time */
2041 #else /* PROC_INTERFACE */
2045 #endif /* PROC_INTERFACE */
2050 int init_module(void) { return 0; }
2051 void cleanup_module(void) {}
2053 void wd33c93_release(void)
2058 MODULE_LICENSE("GPL");