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