PNP: stop using the subsystem rwsem
[linux/fpc-iii.git] / drivers / scsi / aha1542.c
blob1d239f6c01030e4450df42918fd313f362e1da5c
1 /* $Id: aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $
2 * linux/kernel/aha1542.c
4 * Copyright (C) 1992 Tommy Thorn
5 * Copyright (C) 1993, 1994, 1995 Eric Youngdale
7 * Modified by Eric Youngdale
8 * Use request_irq and request_dma to help prevent unexpected conflicts
9 * Set up on-board DMA controller, such that we do not have to
10 * have the bios enabled to use the aha1542.
11 * Modified by David Gentzel
12 * Don't call request_dma if dma mask is 0 (for BusLogic BT-445S VL-Bus
13 * controller).
14 * Modified by Matti Aarnio
15 * Accept parameters from LILO cmd-line. -- 1-Oct-94
16 * Modified by Mike McLagan <mike.mclagan@linux.org>
17 * Recognise extended mode on AHA1542CP, different bit than 1542CF
18 * 1-Jan-97
19 * Modified by Bjorn L. Thordarson and Einar Thor Einarsson
20 * Recognize that DMA0 is valid DMA channel -- 13-Jul-98
21 * Modified by Chris Faulhaber <jedgar@fxp.org>
22 * Added module command-line options
23 * 19-Jul-99
24 * Modified by Adam Fritzler <mid@auk.cx>
25 * Added proper detection of the AHA-1640 (MCA version of AHA-1540)
28 #include <linux/module.h>
29 #include <linux/interrupt.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/ioport.h>
34 #include <linux/delay.h>
35 #include <linux/proc_fs.h>
36 #include <linux/init.h>
37 #include <linux/spinlock.h>
38 #include <linux/pci.h>
39 #include <linux/isapnp.h>
40 #include <linux/blkdev.h>
41 #include <linux/mca.h>
42 #include <linux/mca-legacy.h>
44 #include <asm/dma.h>
45 #include <asm/system.h>
46 #include <asm/io.h>
48 #include "scsi.h"
49 #include <scsi/scsi_host.h>
50 #include "aha1542.h"
52 #define SCSI_BUF_PA(address) isa_virt_to_bus(address)
53 #define SCSI_SG_PA(sgent) (isa_page_to_bus((sgent)->page) + (sgent)->offset)
55 static void BAD_DMA(void *address, unsigned int length)
57 printk(KERN_CRIT "buf vaddress %p paddress 0x%lx length %d\n",
58 address,
59 SCSI_BUF_PA(address),
60 length);
61 panic("Buffer at physical address > 16Mb used for aha1542");
64 static void BAD_SG_DMA(Scsi_Cmnd * SCpnt,
65 struct scatterlist *sgpnt,
66 int nseg,
67 int badseg)
69 printk(KERN_CRIT "sgpnt[%d:%d] page %p/0x%llx length %u\n",
70 badseg, nseg,
71 page_address(sgpnt[badseg].page) + sgpnt[badseg].offset,
72 (unsigned long long)SCSI_SG_PA(&sgpnt[badseg]),
73 sgpnt[badseg].length);
76 * Not safe to continue.
78 panic("Buffer at physical address > 16Mb used for aha1542");
81 #include<linux/stat.h>
83 #ifdef DEBUG
84 #define DEB(x) x
85 #else
86 #define DEB(x)
87 #endif
90 static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $";
93 /* The adaptec can be configured for quite a number of addresses, but
94 I generally do not want the card poking around at random. We allow
95 two addresses - this allows people to use the Adaptec with a Midi
96 card, which also used 0x330 -- can be overridden with LILO! */
98 #define MAXBOARDS 4 /* Increase this and the sizes of the
99 arrays below, if you need more.. */
101 /* Boards 3,4 slots are reserved for ISAPnP/MCA scans */
103 static unsigned int bases[MAXBOARDS] __initdata = {0x330, 0x334, 0, 0};
105 /* set by aha1542_setup according to the command line; they also may
106 be marked __initdata, but require zero initializers then */
108 static int setup_called[MAXBOARDS];
109 static int setup_buson[MAXBOARDS];
110 static int setup_busoff[MAXBOARDS];
111 static int setup_dmaspeed[MAXBOARDS] __initdata = { -1, -1, -1, -1 };
114 * LILO/Module params: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]
116 * Where: <PORTBASE> is any of the valid AHA addresses:
117 * 0x130, 0x134, 0x230, 0x234, 0x330, 0x334
118 * <BUSON> is the time (in microsecs) that AHA spends on the AT-bus
119 * when transferring data. 1542A power-on default is 11us,
120 * valid values are in range: 2..15 (decimal)
121 * <BUSOFF> is the time that AHA spends OFF THE BUS after while
122 * it is transferring data (not to monopolize the bus).
123 * Power-on default is 4us, valid range: 1..64 microseconds.
124 * <DMASPEED> Default is jumper selected (1542A: on the J1),
125 * but experimenter can alter it with this.
126 * Valid values: 5, 6, 7, 8, 10 (MB/s)
127 * Factory default is 5 MB/s.
130 #if defined(MODULE)
131 static int isapnp = 0;
132 static int aha1542[] = {0x330, 11, 4, -1};
133 module_param_array(aha1542, int, NULL, 0);
134 module_param(isapnp, bool, 0);
136 static struct isapnp_device_id id_table[] __initdata = {
138 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
139 ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1542),
145 MODULE_DEVICE_TABLE(isapnp, id_table);
147 #else
148 static int isapnp = 1;
149 #endif
151 #define BIOS_TRANSLATION_1632 0 /* Used by some old 1542A boards */
152 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
153 #define BIOS_TRANSLATION_25563 2 /* Big disk case */
155 struct aha1542_hostdata {
156 /* This will effectively start both of them at the first mailbox */
157 int bios_translation; /* Mapping bios uses - for compatibility */
158 int aha1542_last_mbi_used;
159 int aha1542_last_mbo_used;
160 Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
161 struct mailbox mb[2 * AHA1542_MAILBOXES];
162 struct ccb ccb[AHA1542_MAILBOXES];
165 #define HOSTDATA(host) ((struct aha1542_hostdata *) &host->hostdata)
167 static struct Scsi_Host *aha_host[7]; /* One for each IRQ level (9-15) */
169 static DEFINE_SPINLOCK(aha1542_lock);
173 #define WAITnexttimeout 3000000
175 static void setup_mailboxes(int base_io, struct Scsi_Host *shpnt);
176 static int aha1542_restart(struct Scsi_Host *shost);
177 static void aha1542_intr_handle(struct Scsi_Host *shost, void *dev_id);
178 static irqreturn_t do_aha1542_intr_handle(int irq, void *dev_id);
180 #define aha1542_intr_reset(base) outb(IRST, CONTROL(base))
182 #define WAIT(port, mask, allof, noneof) \
183 { register int WAITbits; \
184 register int WAITtimeout = WAITnexttimeout; \
185 while (1) { \
186 WAITbits = inb(port) & (mask); \
187 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
188 break; \
189 if (--WAITtimeout == 0) goto fail; \
193 /* Similar to WAIT, except we use the udelay call to regulate the
194 amount of time we wait. */
195 #define WAITd(port, mask, allof, noneof, timeout) \
196 { register int WAITbits; \
197 register int WAITtimeout = timeout; \
198 while (1) { \
199 WAITbits = inb(port) & (mask); \
200 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
201 break; \
202 mdelay(1); \
203 if (--WAITtimeout == 0) goto fail; \
207 static void aha1542_stat(void)
209 /* int s = inb(STATUS), i = inb(INTRFLAGS);
210 printk("status=%x intrflags=%x\n", s, i, WAITnexttimeout-WAITtimeout); */
213 /* This is a bit complicated, but we need to make sure that an interrupt
214 routine does not send something out while we are in the middle of this.
215 Fortunately, it is only at boot time that multi-byte messages
216 are ever sent. */
217 static int aha1542_out(unsigned int base, unchar * cmdp, int len)
219 unsigned long flags = 0;
220 int got_lock;
222 if (len == 1) {
223 got_lock = 0;
224 while (1 == 1) {
225 WAIT(STATUS(base), CDF, 0, CDF);
226 spin_lock_irqsave(&aha1542_lock, flags);
227 if (inb(STATUS(base)) & CDF) {
228 spin_unlock_irqrestore(&aha1542_lock, flags);
229 continue;
231 outb(*cmdp, DATA(base));
232 spin_unlock_irqrestore(&aha1542_lock, flags);
233 return 0;
235 } else {
236 spin_lock_irqsave(&aha1542_lock, flags);
237 got_lock = 1;
238 while (len--) {
239 WAIT(STATUS(base), CDF, 0, CDF);
240 outb(*cmdp++, DATA(base));
242 spin_unlock_irqrestore(&aha1542_lock, flags);
244 return 0;
245 fail:
246 if (got_lock)
247 spin_unlock_irqrestore(&aha1542_lock, flags);
248 printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
249 aha1542_stat();
250 return 1;
253 /* Only used at boot time, so we do not need to worry about latency as much
254 here */
256 static int __init aha1542_in(unsigned int base, unchar * cmdp, int len)
258 unsigned long flags;
260 spin_lock_irqsave(&aha1542_lock, flags);
261 while (len--) {
262 WAIT(STATUS(base), DF, DF, 0);
263 *cmdp++ = inb(DATA(base));
265 spin_unlock_irqrestore(&aha1542_lock, flags);
266 return 0;
267 fail:
268 spin_unlock_irqrestore(&aha1542_lock, flags);
269 printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
270 aha1542_stat();
271 return 1;
274 /* Similar to aha1542_in, except that we wait a very short period of time.
275 We use this if we know the board is alive and awake, but we are not sure
276 if the board will respond to the command we are about to send or not */
277 static int __init aha1542_in1(unsigned int base, unchar * cmdp, int len)
279 unsigned long flags;
281 spin_lock_irqsave(&aha1542_lock, flags);
282 while (len--) {
283 WAITd(STATUS(base), DF, DF, 0, 100);
284 *cmdp++ = inb(DATA(base));
286 spin_unlock_irqrestore(&aha1542_lock, flags);
287 return 0;
288 fail:
289 spin_unlock_irqrestore(&aha1542_lock, flags);
290 return 1;
293 static int makecode(unsigned hosterr, unsigned scsierr)
295 switch (hosterr) {
296 case 0x0:
297 case 0xa: /* Linked command complete without error and linked normally */
298 case 0xb: /* Linked command complete without error, interrupt generated */
299 hosterr = 0;
300 break;
302 case 0x11: /* Selection time out-The initiator selection or target
303 reselection was not complete within the SCSI Time out period */
304 hosterr = DID_TIME_OUT;
305 break;
307 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
308 than was allocated by the Data Length field or the sum of the
309 Scatter / Gather Data Length fields. */
311 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
313 case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
314 invalid. This usually indicates a software failure. */
316 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
317 This usually indicates a software failure. */
319 case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
320 of linked CCB's does not specify the same logical unit number as
321 the first. */
322 case 0x18: /* Invalid Target Direction received from Host-The direction of a
323 Target Mode CCB was invalid. */
325 case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
326 received to service data transfer between the same target LUN
327 and initiator SCSI ID in the same direction. */
329 case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
330 length segment or invalid segment list boundaries was received.
331 A CCB parameter was invalid. */
332 DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
333 hosterr = DID_ERROR; /* Couldn't find any better */
334 break;
336 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
337 phase sequence was requested by the target. The host adapter
338 will generate a SCSI Reset Condition, notifying the host with
339 a SCRD interrupt */
340 hosterr = DID_RESET;
341 break;
342 default:
343 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
344 break;
346 return scsierr | (hosterr << 16);
349 static int __init aha1542_test_port(int bse, struct Scsi_Host *shpnt)
351 unchar inquiry_cmd[] = {CMD_INQUIRY};
352 unchar inquiry_result[4];
353 unchar *cmdp;
354 int len;
355 volatile int debug = 0;
357 /* Quick and dirty test for presence of the card. */
358 if (inb(STATUS(bse)) == 0xff)
359 return 0;
361 /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
363 /* DEB(printk("aha1542_test_port called \n")); */
365 /* In case some other card was probing here, reset interrupts */
366 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
368 outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
370 mdelay(20); /* Wait a little bit for things to settle down. */
372 debug = 1;
373 /* Expect INIT and IDLE, any of the others are bad */
374 WAIT(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
376 debug = 2;
377 /* Shouldn't have generated any interrupts during reset */
378 if (inb(INTRFLAGS(bse)) & INTRMASK)
379 goto fail;
382 /* Perform a host adapter inquiry instead so we do not need to set
383 up the mailboxes ahead of time */
385 aha1542_out(bse, inquiry_cmd, 1);
387 debug = 3;
388 len = 4;
389 cmdp = &inquiry_result[0];
391 while (len--) {
392 WAIT(STATUS(bse), DF, DF, 0);
393 *cmdp++ = inb(DATA(bse));
396 debug = 8;
397 /* Reading port should reset DF */
398 if (inb(STATUS(bse)) & DF)
399 goto fail;
401 debug = 9;
402 /* When HACC, command is completed, and we're though testing */
403 WAIT(INTRFLAGS(bse), HACC, HACC, 0);
404 /* now initialize adapter */
406 debug = 10;
407 /* Clear interrupts */
408 outb(IRST, CONTROL(bse));
410 debug = 11;
412 return debug; /* 1 = ok */
413 fail:
414 return 0; /* 0 = not ok */
417 /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
418 static irqreturn_t do_aha1542_intr_handle(int irq, void *dev_id)
420 unsigned long flags;
421 struct Scsi_Host *shost;
423 shost = aha_host[irq - 9];
424 if (!shost)
425 panic("Splunge!");
427 spin_lock_irqsave(shost->host_lock, flags);
428 aha1542_intr_handle(shost, dev_id);
429 spin_unlock_irqrestore(shost->host_lock, flags);
430 return IRQ_HANDLED;
433 /* A "high" level interrupt handler */
434 static void aha1542_intr_handle(struct Scsi_Host *shost, void *dev_id)
436 void (*my_done) (Scsi_Cmnd *) = NULL;
437 int errstatus, mbi, mbo, mbistatus;
438 int number_serviced;
439 unsigned long flags;
440 Scsi_Cmnd *SCtmp;
441 int flag;
442 int needs_restart;
443 struct mailbox *mb;
444 struct ccb *ccb;
446 mb = HOSTDATA(shost)->mb;
447 ccb = HOSTDATA(shost)->ccb;
449 #ifdef DEBUG
451 flag = inb(INTRFLAGS(shost->io_port));
452 printk(KERN_DEBUG "aha1542_intr_handle: ");
453 if (!(flag & ANYINTR))
454 printk("no interrupt?");
455 if (flag & MBIF)
456 printk("MBIF ");
457 if (flag & MBOA)
458 printk("MBOF ");
459 if (flag & HACC)
460 printk("HACC ");
461 if (flag & SCRD)
462 printk("SCRD ");
463 printk("status %02x\n", inb(STATUS(shost->io_port)));
465 #endif
466 number_serviced = 0;
467 needs_restart = 0;
469 while (1 == 1) {
470 flag = inb(INTRFLAGS(shost->io_port));
472 /* Check for unusual interrupts. If any of these happen, we should
473 probably do something special, but for now just printing a message
474 is sufficient. A SCSI reset detected is something that we really
475 need to deal with in some way. */
476 if (flag & ~MBIF) {
477 if (flag & MBOA)
478 printk("MBOF ");
479 if (flag & HACC)
480 printk("HACC ");
481 if (flag & SCRD) {
482 needs_restart = 1;
483 printk("SCRD ");
486 aha1542_intr_reset(shost->io_port);
488 spin_lock_irqsave(&aha1542_lock, flags);
489 mbi = HOSTDATA(shost)->aha1542_last_mbi_used + 1;
490 if (mbi >= 2 * AHA1542_MAILBOXES)
491 mbi = AHA1542_MAILBOXES;
493 do {
494 if (mb[mbi].status != 0)
495 break;
496 mbi++;
497 if (mbi >= 2 * AHA1542_MAILBOXES)
498 mbi = AHA1542_MAILBOXES;
499 } while (mbi != HOSTDATA(shost)->aha1542_last_mbi_used);
501 if (mb[mbi].status == 0) {
502 spin_unlock_irqrestore(&aha1542_lock, flags);
503 /* Hmm, no mail. Must have read it the last time around */
504 if (!number_serviced && !needs_restart)
505 printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
506 /* We detected a reset. Restart all pending commands for
507 devices that use the hard reset option */
508 if (needs_restart)
509 aha1542_restart(shost);
510 return;
513 mbo = (scsi2int(mb[mbi].ccbptr) - (SCSI_BUF_PA(&ccb[0]))) / sizeof(struct ccb);
514 mbistatus = mb[mbi].status;
515 mb[mbi].status = 0;
516 HOSTDATA(shost)->aha1542_last_mbi_used = mbi;
517 spin_unlock_irqrestore(&aha1542_lock, flags);
519 #ifdef DEBUG
521 if (ccb[mbo].tarstat | ccb[mbo].hastat)
522 printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
523 ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
525 #endif
527 if (mbistatus == 3)
528 continue; /* Aborted command not found */
530 #ifdef DEBUG
531 printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
532 #endif
534 SCtmp = HOSTDATA(shost)->SCint[mbo];
536 if (!SCtmp || !SCtmp->scsi_done) {
537 printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
538 printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
539 ccb[mbo].hastat, ccb[mbo].idlun, mbo);
540 return;
542 my_done = SCtmp->scsi_done;
543 kfree(SCtmp->host_scribble);
544 SCtmp->host_scribble = NULL;
545 /* Fetch the sense data, and tuck it away, in the required slot. The
546 Adaptec automatically fetches it, and there is no guarantee that
547 we will still have it in the cdb when we come back */
548 if (ccb[mbo].tarstat == 2)
549 memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
550 sizeof(SCtmp->sense_buffer));
553 /* is there mail :-) */
555 /* more error checking left out here */
556 if (mbistatus != 1)
557 /* This is surely wrong, but I don't know what's right */
558 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
559 else
560 errstatus = 0;
562 #ifdef DEBUG
563 if (errstatus)
564 printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
565 ccb[mbo].hastat, ccb[mbo].tarstat);
566 #endif
568 if (ccb[mbo].tarstat == 2) {
569 #ifdef DEBUG
570 int i;
571 #endif
572 DEB(printk("aha1542_intr_handle: sense:"));
573 #ifdef DEBUG
574 for (i = 0; i < 12; i++)
575 printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
576 printk("\n");
577 #endif
579 DEB(printk("aha1542_intr_handle: buf:"));
580 for (i = 0; i < bufflen; i++)
581 printk("%02x ", ((unchar *)buff)[i]);
582 printk("\n");
585 DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
586 SCtmp->result = errstatus;
587 HOSTDATA(shost)->SCint[mbo] = NULL; /* This effectively frees up the mailbox slot, as
588 far as queuecommand is concerned */
589 my_done(SCtmp);
590 number_serviced++;
594 static int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
596 unchar ahacmd = CMD_START_SCSI;
597 unchar direction;
598 unchar *cmd = (unchar *) SCpnt->cmnd;
599 unchar target = SCpnt->device->id;
600 unchar lun = SCpnt->device->lun;
601 unsigned long flags;
602 void *buff = SCpnt->request_buffer;
603 int bufflen = SCpnt->request_bufflen;
604 int mbo;
605 struct mailbox *mb;
606 struct ccb *ccb;
608 DEB(int i);
610 mb = HOSTDATA(SCpnt->device->host)->mb;
611 ccb = HOSTDATA(SCpnt->device->host)->ccb;
613 DEB(if (target > 1) {
614 SCpnt->result = DID_TIME_OUT << 16;
615 done(SCpnt); return 0;
619 if (*cmd == REQUEST_SENSE) {
620 /* Don't do the command - we have the sense data already */
621 #if 0
622 /* scsi_request_sense() provides a buffer of size 256,
623 so there is no reason to expect equality */
624 if (bufflen != sizeof(SCpnt->sense_buffer))
625 printk(KERN_CRIT "aha1542: Wrong buffer length supplied "
626 "for request sense (%d)\n", bufflen);
627 #endif
628 SCpnt->result = 0;
629 done(SCpnt);
630 return 0;
632 #ifdef DEBUG
633 if (*cmd == READ_10 || *cmd == WRITE_10)
634 i = xscsi2int(cmd + 2);
635 else if (*cmd == READ_6 || *cmd == WRITE_6)
636 i = scsi2int(cmd + 2);
637 else
638 i = -1;
639 if (done)
640 printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
641 else
642 printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
643 aha1542_stat();
644 printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
645 for (i = 0; i < SCpnt->cmd_len; i++)
646 printk("%02x ", cmd[i]);
647 printk("\n");
648 if (*cmd == WRITE_10 || *cmd == WRITE_6)
649 return 0; /* we are still testing, so *don't* write */
650 #endif
651 /* Use the outgoing mailboxes in a round-robin fashion, because this
652 is how the host adapter will scan for them */
654 spin_lock_irqsave(&aha1542_lock, flags);
655 mbo = HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used + 1;
656 if (mbo >= AHA1542_MAILBOXES)
657 mbo = 0;
659 do {
660 if (mb[mbo].status == 0 && HOSTDATA(SCpnt->device->host)->SCint[mbo] == NULL)
661 break;
662 mbo++;
663 if (mbo >= AHA1542_MAILBOXES)
664 mbo = 0;
665 } while (mbo != HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used);
667 if (mb[mbo].status || HOSTDATA(SCpnt->device->host)->SCint[mbo])
668 panic("Unable to find empty mailbox for aha1542.\n");
670 HOSTDATA(SCpnt->device->host)->SCint[mbo] = SCpnt; /* This will effectively prevent someone else from
671 screwing with this cdb. */
673 HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used = mbo;
674 spin_unlock_irqrestore(&aha1542_lock, flags);
676 #ifdef DEBUG
677 printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
678 #endif
680 any2scsi(mb[mbo].ccbptr, SCSI_BUF_PA(&ccb[mbo])); /* This gets trashed for some reason */
682 memset(&ccb[mbo], 0, sizeof(struct ccb));
684 ccb[mbo].cdblen = SCpnt->cmd_len;
686 direction = 0;
687 if (*cmd == READ_10 || *cmd == READ_6)
688 direction = 8;
689 else if (*cmd == WRITE_10 || *cmd == WRITE_6)
690 direction = 16;
692 memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
694 if (SCpnt->use_sg) {
695 struct scatterlist *sgpnt;
696 struct chain *cptr;
697 #ifdef DEBUG
698 unsigned char *ptr;
699 #endif
700 int i;
701 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
702 SCpnt->host_scribble = kmalloc(512, GFP_KERNEL | GFP_DMA);
703 sgpnt = (struct scatterlist *) SCpnt->request_buffer;
704 cptr = (struct chain *) SCpnt->host_scribble;
705 if (cptr == NULL) {
706 /* free the claimed mailbox slot */
707 HOSTDATA(SCpnt->device->host)->SCint[mbo] = NULL;
708 return SCSI_MLQUEUE_HOST_BUSY;
710 for (i = 0; i < SCpnt->use_sg; i++) {
711 if (sgpnt[i].length == 0 || SCpnt->use_sg > 16 ||
712 (((int) sgpnt[i].offset) & 1) || (sgpnt[i].length & 1)) {
713 unsigned char *ptr;
714 printk(KERN_CRIT "Bad segment list supplied to aha1542.c (%d, %d)\n", SCpnt->use_sg, i);
715 for (i = 0; i < SCpnt->use_sg; i++) {
716 printk(KERN_CRIT "%d: %p %d\n", i,
717 (page_address(sgpnt[i].page) +
718 sgpnt[i].offset),
719 sgpnt[i].length);
721 printk(KERN_CRIT "cptr %x: ", (unsigned int) cptr);
722 ptr = (unsigned char *) &cptr[i];
723 for (i = 0; i < 18; i++)
724 printk("%02x ", ptr[i]);
725 panic("Foooooooood fight!");
727 any2scsi(cptr[i].dataptr, SCSI_SG_PA(&sgpnt[i]));
728 if (SCSI_SG_PA(&sgpnt[i]) + sgpnt[i].length - 1 > ISA_DMA_THRESHOLD)
729 BAD_SG_DMA(SCpnt, sgpnt, SCpnt->use_sg, i);
730 any2scsi(cptr[i].datalen, sgpnt[i].length);
732 any2scsi(ccb[mbo].datalen, SCpnt->use_sg * sizeof(struct chain));
733 any2scsi(ccb[mbo].dataptr, SCSI_BUF_PA(cptr));
734 #ifdef DEBUG
735 printk("cptr %x: ", cptr);
736 ptr = (unsigned char *) cptr;
737 for (i = 0; i < 18; i++)
738 printk("%02x ", ptr[i]);
739 #endif
740 } else {
741 ccb[mbo].op = 0; /* SCSI Initiator Command */
742 SCpnt->host_scribble = NULL;
743 any2scsi(ccb[mbo].datalen, bufflen);
744 if (buff && SCSI_BUF_PA(buff + bufflen - 1) > ISA_DMA_THRESHOLD)
745 BAD_DMA(buff, bufflen);
746 any2scsi(ccb[mbo].dataptr, SCSI_BUF_PA(buff));
748 ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
749 ccb[mbo].rsalen = 16;
750 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
751 ccb[mbo].commlinkid = 0;
753 #ifdef DEBUG
755 int i;
756 printk(KERN_DEBUG "aha1542_command: sending.. ");
757 for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
758 printk("%02x ", ((unchar *) & ccb[mbo])[i]);
760 #endif
762 if (done) {
763 DEB(printk("aha1542_queuecommand: now waiting for interrupt ");
764 aha1542_stat());
765 SCpnt->scsi_done = done;
766 mb[mbo].status = 1;
767 aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1); /* start scsi command */
768 DEB(aha1542_stat());
769 } else
770 printk("aha1542_queuecommand: done can't be NULL\n");
772 return 0;
775 /* Initialize mailboxes */
776 static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
778 int i;
779 struct mailbox *mb;
780 struct ccb *ccb;
782 unchar cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
784 mb = HOSTDATA(shpnt)->mb;
785 ccb = HOSTDATA(shpnt)->ccb;
787 for (i = 0; i < AHA1542_MAILBOXES; i++) {
788 mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
789 any2scsi(mb[i].ccbptr, SCSI_BUF_PA(&ccb[i]));
791 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
792 any2scsi((cmd + 2), SCSI_BUF_PA(mb));
793 aha1542_out(bse, cmd, 5);
794 WAIT(INTRFLAGS(bse), INTRMASK, HACC, 0);
795 while (0) {
796 fail:
797 printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
799 aha1542_intr_reset(bse);
802 static int __init aha1542_getconfig(int base_io, unsigned char *irq_level, unsigned char *dma_chan, unsigned char *scsi_id)
804 unchar inquiry_cmd[] = {CMD_RETCONF};
805 unchar inquiry_result[3];
806 int i;
807 i = inb(STATUS(base_io));
808 if (i & DF) {
809 i = inb(DATA(base_io));
811 aha1542_out(base_io, inquiry_cmd, 1);
812 aha1542_in(base_io, inquiry_result, 3);
813 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
814 while (0) {
815 fail:
816 printk(KERN_ERR "aha1542_detect: query board settings\n");
818 aha1542_intr_reset(base_io);
819 switch (inquiry_result[0]) {
820 case 0x80:
821 *dma_chan = 7;
822 break;
823 case 0x40:
824 *dma_chan = 6;
825 break;
826 case 0x20:
827 *dma_chan = 5;
828 break;
829 case 0x01:
830 *dma_chan = 0;
831 break;
832 case 0:
833 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
834 Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
835 *dma_chan = 0xFF;
836 break;
837 default:
838 printk(KERN_ERR "Unable to determine Adaptec DMA priority. Disabling board\n");
839 return -1;
841 switch (inquiry_result[1]) {
842 case 0x40:
843 *irq_level = 15;
844 break;
845 case 0x20:
846 *irq_level = 14;
847 break;
848 case 0x8:
849 *irq_level = 12;
850 break;
851 case 0x4:
852 *irq_level = 11;
853 break;
854 case 0x2:
855 *irq_level = 10;
856 break;
857 case 0x1:
858 *irq_level = 9;
859 break;
860 default:
861 printk(KERN_ERR "Unable to determine Adaptec IRQ level. Disabling board\n");
862 return -1;
864 *scsi_id = inquiry_result[2] & 7;
865 return 0;
868 /* This function should only be called for 1542C boards - we can detect
869 the special firmware settings and unlock the board */
871 static int __init aha1542_mbenable(int base)
873 static unchar mbenable_cmd[3];
874 static unchar mbenable_result[2];
875 int retval;
877 retval = BIOS_TRANSLATION_6432;
879 mbenable_cmd[0] = CMD_EXTBIOS;
880 aha1542_out(base, mbenable_cmd, 1);
881 if (aha1542_in1(base, mbenable_result, 2))
882 return retval;
883 WAITd(INTRFLAGS(base), INTRMASK, HACC, 0, 100);
884 aha1542_intr_reset(base);
886 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
887 mbenable_cmd[0] = CMD_MBENABLE;
888 mbenable_cmd[1] = 0;
889 mbenable_cmd[2] = mbenable_result[1];
891 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
892 retval = BIOS_TRANSLATION_25563;
894 aha1542_out(base, mbenable_cmd, 3);
895 WAIT(INTRFLAGS(base), INTRMASK, HACC, 0);
897 while (0) {
898 fail:
899 printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
901 aha1542_intr_reset(base);
902 return retval;
905 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
906 static int __init aha1542_query(int base_io, int *transl)
908 unchar inquiry_cmd[] = {CMD_INQUIRY};
909 unchar inquiry_result[4];
910 int i;
911 i = inb(STATUS(base_io));
912 if (i & DF) {
913 i = inb(DATA(base_io));
915 aha1542_out(base_io, inquiry_cmd, 1);
916 aha1542_in(base_io, inquiry_result, 4);
917 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
918 while (0) {
919 fail:
920 printk(KERN_ERR "aha1542_detect: query card type\n");
922 aha1542_intr_reset(base_io);
924 *transl = BIOS_TRANSLATION_6432; /* Default case */
926 /* For an AHA1740 series board, we ignore the board since there is a
927 hardware bug which can lead to wrong blocks being returned if the board
928 is operating in the 1542 emulation mode. Since there is an extended mode
929 driver, we simply ignore the board and let the 1740 driver pick it up.
932 if (inquiry_result[0] == 0x43) {
933 printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
934 return 1;
937 /* Always call this - boards that do not support extended bios translation
938 will ignore the command, and we will set the proper default */
940 *transl = aha1542_mbenable(base_io);
942 return 0;
945 #ifndef MODULE
946 static char *setup_str[MAXBOARDS] __initdata;
947 static int setup_idx = 0;
949 static void __init aha1542_setup(char *str, int *ints)
951 const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
952 int setup_portbase;
954 if (setup_idx >= MAXBOARDS) {
955 printk(KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
956 printk(KERN_ERR " Entryline 1: %s\n", setup_str[0]);
957 printk(KERN_ERR " Entryline 2: %s\n", setup_str[1]);
958 printk(KERN_ERR " This line: %s\n", str);
959 return;
961 if (ints[0] < 1 || ints[0] > 4) {
962 printk(KERN_ERR "aha1542: %s\n", str);
963 printk(ahausage);
964 printk(KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
966 setup_called[setup_idx] = ints[0];
967 setup_str[setup_idx] = str;
969 setup_portbase = ints[0] >= 1 ? ints[1] : 0; /* Preserve the default value.. */
970 setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
971 setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
972 if (ints[0] >= 4)
974 int atbt = -1;
975 switch (ints[4]) {
976 case 5:
977 atbt = 0x00;
978 break;
979 case 6:
980 atbt = 0x04;
981 break;
982 case 7:
983 atbt = 0x01;
984 break;
985 case 8:
986 atbt = 0x02;
987 break;
988 case 10:
989 atbt = 0x03;
990 break;
991 default:
992 printk(KERN_ERR "aha1542: %s\n", str);
993 printk(ahausage);
994 printk(KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s. Using jumper defaults.\n");
995 break;
997 setup_dmaspeed[setup_idx] = atbt;
999 if (setup_portbase != 0)
1000 bases[setup_idx] = setup_portbase;
1002 ++setup_idx;
1005 static int __init do_setup(char *str)
1007 int ints[5];
1009 int count=setup_idx;
1011 get_options(str, ARRAY_SIZE(ints), ints);
1012 aha1542_setup(str,ints);
1014 return count<setup_idx;
1017 __setup("aha1542=",do_setup);
1018 #endif
1020 /* return non-zero on detection */
1021 static int __init aha1542_detect(struct scsi_host_template * tpnt)
1023 unsigned char dma_chan;
1024 unsigned char irq_level;
1025 unsigned char scsi_id;
1026 unsigned long flags;
1027 unsigned int base_io;
1028 int trans;
1029 struct Scsi_Host *shpnt = NULL;
1030 int count = 0;
1031 int indx;
1033 DEB(printk("aha1542_detect: \n"));
1035 tpnt->proc_name = "aha1542";
1037 #ifdef MODULE
1038 bases[0] = aha1542[0];
1039 setup_buson[0] = aha1542[1];
1040 setup_busoff[0] = aha1542[2];
1042 int atbt = -1;
1043 switch (aha1542[3]) {
1044 case 5:
1045 atbt = 0x00;
1046 break;
1047 case 6:
1048 atbt = 0x04;
1049 break;
1050 case 7:
1051 atbt = 0x01;
1052 break;
1053 case 8:
1054 atbt = 0x02;
1055 break;
1056 case 10:
1057 atbt = 0x03;
1058 break;
1060 setup_dmaspeed[0] = atbt;
1062 #endif
1065 * Find MicroChannel cards (AHA1640)
1067 #ifdef CONFIG_MCA_LEGACY
1068 if(MCA_bus) {
1069 int slot = 0;
1070 int pos = 0;
1072 for (indx = 0; (slot != MCA_NOTFOUND) && (indx < ARRAY_SIZE(bases)); indx++) {
1074 if (bases[indx])
1075 continue;
1077 /* Detect only AHA-1640 cards -- MCA ID 0F1F */
1078 slot = mca_find_unused_adapter(0x0f1f, slot);
1079 if (slot == MCA_NOTFOUND)
1080 break;
1082 /* Found one */
1083 pos = mca_read_stored_pos(slot, 3);
1085 /* Decode address */
1086 if (pos & 0x80) {
1087 if (pos & 0x02) {
1088 if (pos & 0x01)
1089 bases[indx] = 0x334;
1090 else
1091 bases[indx] = 0x234;
1092 } else {
1093 if (pos & 0x01)
1094 bases[indx] = 0x134;
1096 } else {
1097 if (pos & 0x02) {
1098 if (pos & 0x01)
1099 bases[indx] = 0x330;
1100 else
1101 bases[indx] = 0x230;
1102 } else {
1103 if (pos & 0x01)
1104 bases[indx] = 0x130;
1108 /* No need to decode IRQ and Arb level -- those are
1109 * read off the card later.
1111 printk(KERN_INFO "Found an AHA-1640 in MCA slot %d, I/O 0x%04x\n", slot, bases[indx]);
1113 mca_set_adapter_name(slot, "Adapter AHA-1640");
1114 mca_set_adapter_procfn(slot, NULL, NULL);
1115 mca_mark_as_used(slot);
1117 /* Go on */
1118 slot++;
1122 #endif
1125 * Hunt for ISA Plug'n'Pray Adaptecs (AHA1535)
1128 if(isapnp)
1130 struct pnp_dev *pdev = NULL;
1131 for(indx = 0; indx < ARRAY_SIZE(bases); indx++) {
1132 if(bases[indx])
1133 continue;
1134 pdev = pnp_find_dev(NULL, ISAPNP_VENDOR('A', 'D', 'P'),
1135 ISAPNP_FUNCTION(0x1542), pdev);
1136 if(pdev==NULL)
1137 break;
1139 * Activate the PnP card
1142 if(pnp_device_attach(pdev)<0)
1143 continue;
1145 if(pnp_activate_dev(pdev)<0) {
1146 pnp_device_detach(pdev);
1147 continue;
1150 if(!pnp_port_valid(pdev, 0)) {
1151 pnp_device_detach(pdev);
1152 continue;
1155 bases[indx] = pnp_port_start(pdev, 0);
1157 /* The card can be queried for its DMA, we have
1158 the DMA set up that is enough */
1160 printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
1163 for (indx = 0; indx < ARRAY_SIZE(bases); indx++)
1164 if (bases[indx] != 0 && request_region(bases[indx], 4, "aha1542")) {
1165 shpnt = scsi_register(tpnt,
1166 sizeof(struct aha1542_hostdata));
1168 if(shpnt==NULL) {
1169 release_region(bases[indx], 4);
1170 continue;
1172 /* For now we do this - until kmalloc is more intelligent
1173 we are resigned to stupid hacks like this */
1174 if (SCSI_BUF_PA(shpnt) >= ISA_DMA_THRESHOLD) {
1175 printk(KERN_ERR "Invalid address for shpnt with 1542.\n");
1176 goto unregister;
1178 if (!aha1542_test_port(bases[indx], shpnt))
1179 goto unregister;
1182 base_io = bases[indx];
1184 /* Set the Bus on/off-times as not to ruin floppy performance */
1186 unchar oncmd[] = {CMD_BUSON_TIME, 7};
1187 unchar offcmd[] = {CMD_BUSOFF_TIME, 5};
1189 if (setup_called[indx]) {
1190 oncmd[1] = setup_buson[indx];
1191 offcmd[1] = setup_busoff[indx];
1193 aha1542_intr_reset(base_io);
1194 aha1542_out(base_io, oncmd, 2);
1195 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1196 aha1542_intr_reset(base_io);
1197 aha1542_out(base_io, offcmd, 2);
1198 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1199 if (setup_dmaspeed[indx] >= 0) {
1200 unchar dmacmd[] = {CMD_DMASPEED, 0};
1201 dmacmd[1] = setup_dmaspeed[indx];
1202 aha1542_intr_reset(base_io);
1203 aha1542_out(base_io, dmacmd, 2);
1204 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1206 while (0) {
1207 fail:
1208 printk(KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
1210 aha1542_intr_reset(base_io);
1212 if (aha1542_query(base_io, &trans))
1213 goto unregister;
1215 if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
1216 goto unregister;
1218 printk(KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
1219 if (dma_chan != 0xFF)
1220 printk(", DMA priority %d", dma_chan);
1221 printk("\n");
1223 DEB(aha1542_stat());
1224 setup_mailboxes(base_io, shpnt);
1226 DEB(aha1542_stat());
1228 DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
1229 spin_lock_irqsave(&aha1542_lock, flags);
1230 if (request_irq(irq_level, do_aha1542_intr_handle, 0, "aha1542", NULL)) {
1231 printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
1232 spin_unlock_irqrestore(&aha1542_lock, flags);
1233 goto unregister;
1235 if (dma_chan != 0xFF) {
1236 if (request_dma(dma_chan, "aha1542")) {
1237 printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
1238 free_irq(irq_level, NULL);
1239 spin_unlock_irqrestore(&aha1542_lock, flags);
1240 goto unregister;
1242 if (dma_chan == 0 || dma_chan >= 5) {
1243 set_dma_mode(dma_chan, DMA_MODE_CASCADE);
1244 enable_dma(dma_chan);
1247 aha_host[irq_level - 9] = shpnt;
1248 shpnt->this_id = scsi_id;
1249 shpnt->unique_id = base_io;
1250 shpnt->io_port = base_io;
1251 shpnt->n_io_port = 4; /* Number of bytes of I/O space used */
1252 shpnt->dma_channel = dma_chan;
1253 shpnt->irq = irq_level;
1254 HOSTDATA(shpnt)->bios_translation = trans;
1255 if (trans == BIOS_TRANSLATION_25563)
1256 printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
1257 HOSTDATA(shpnt)->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
1258 HOSTDATA(shpnt)->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
1259 memset(HOSTDATA(shpnt)->SCint, 0, sizeof(HOSTDATA(shpnt)->SCint));
1260 spin_unlock_irqrestore(&aha1542_lock, flags);
1261 #if 0
1262 DEB(printk(" *** READ CAPACITY ***\n"));
1265 unchar buf[8];
1266 static unchar cmd[] = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1267 int i;
1269 for (i = 0; i < sizeof(buf); ++i)
1270 buf[i] = 0x87;
1271 for (i = 0; i < 2; ++i)
1272 if (!aha1542_command(i, cmd, buf, sizeof(buf))) {
1273 printk(KERN_DEBUG "aha_detect: LU %d sector_size %d device_size %d\n",
1274 i, xscsi2int(buf + 4), xscsi2int(buf));
1278 DEB(printk(" *** NOW RUNNING MY OWN TEST *** \n"));
1280 for (i = 0; i < 4; ++i) {
1281 unsigned char cmd[10];
1282 static buffer[512];
1284 cmd[0] = READ_10;
1285 cmd[1] = 0;
1286 xany2scsi(cmd + 2, i);
1287 cmd[6] = 0;
1288 cmd[7] = 0;
1289 cmd[8] = 1;
1290 cmd[9] = 0;
1291 aha1542_command(0, cmd, buffer, 512);
1293 #endif
1294 count++;
1295 continue;
1296 unregister:
1297 release_region(bases[indx], 4);
1298 scsi_unregister(shpnt);
1299 continue;
1303 return count;
1306 static int aha1542_release(struct Scsi_Host *shost)
1308 if (shost->irq)
1309 free_irq(shost->irq, NULL);
1310 if (shost->dma_channel != 0xff)
1311 free_dma(shost->dma_channel);
1312 if (shost->io_port && shost->n_io_port)
1313 release_region(shost->io_port, shost->n_io_port);
1314 scsi_unregister(shost);
1315 return 0;
1318 static int aha1542_restart(struct Scsi_Host *shost)
1320 int i;
1321 int count = 0;
1322 #if 0
1323 unchar ahacmd = CMD_START_SCSI;
1324 #endif
1326 for (i = 0; i < AHA1542_MAILBOXES; i++)
1327 if (HOSTDATA(shost)->SCint[i] &&
1328 !(HOSTDATA(shost)->SCint[i]->device->soft_reset)) {
1329 #if 0
1330 HOSTDATA(shost)->mb[i].status = 1; /* Indicate ready to restart... */
1331 #endif
1332 count++;
1334 printk(KERN_DEBUG "Potential to restart %d stalled commands...\n", count);
1335 #if 0
1336 /* start scsi command */
1337 if (count)
1338 aha1542_out(shost->io_port, &ahacmd, 1);
1339 #endif
1340 return 0;
1344 * This is a device reset. This is handled by sending a special command
1345 * to the device.
1347 static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
1349 unsigned long flags;
1350 struct mailbox *mb;
1351 unchar target = SCpnt->device->id;
1352 unchar lun = SCpnt->device->lun;
1353 int mbo;
1354 struct ccb *ccb;
1355 unchar ahacmd = CMD_START_SCSI;
1357 ccb = HOSTDATA(SCpnt->device->host)->ccb;
1358 mb = HOSTDATA(SCpnt->device->host)->mb;
1360 spin_lock_irqsave(&aha1542_lock, flags);
1361 mbo = HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used + 1;
1362 if (mbo >= AHA1542_MAILBOXES)
1363 mbo = 0;
1365 do {
1366 if (mb[mbo].status == 0 && HOSTDATA(SCpnt->device->host)->SCint[mbo] == NULL)
1367 break;
1368 mbo++;
1369 if (mbo >= AHA1542_MAILBOXES)
1370 mbo = 0;
1371 } while (mbo != HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used);
1373 if (mb[mbo].status || HOSTDATA(SCpnt->device->host)->SCint[mbo])
1374 panic("Unable to find empty mailbox for aha1542.\n");
1376 HOSTDATA(SCpnt->device->host)->SCint[mbo] = SCpnt; /* This will effectively
1377 prevent someone else from
1378 screwing with this cdb. */
1380 HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used = mbo;
1381 spin_unlock_irqrestore(&aha1542_lock, flags);
1383 any2scsi(mb[mbo].ccbptr, SCSI_BUF_PA(&ccb[mbo])); /* This gets trashed for some reason */
1385 memset(&ccb[mbo], 0, sizeof(struct ccb));
1387 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
1389 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
1391 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
1392 ccb[mbo].commlinkid = 0;
1395 * Now tell the 1542 to flush all pending commands for this
1396 * target
1398 aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1);
1400 scmd_printk(KERN_WARNING, SCpnt,
1401 "Trying device reset for target\n");
1403 return SUCCESS;
1406 #ifdef ERIC_neverdef
1408 * With the 1542 we apparently never get an interrupt to
1409 * acknowledge a device reset being sent. Then again, Leonard
1410 * says we are doing this wrong in the first place...
1412 * Take a wait and see attitude. If we get spurious interrupts,
1413 * then the device reset is doing something sane and useful, and
1414 * we will wait for the interrupt to post completion.
1416 printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1419 * Free the command block for all commands running on this
1420 * target...
1422 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1423 if (HOSTDATA(SCpnt->host)->SCint[i] &&
1424 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1425 Scsi_Cmnd *SCtmp;
1426 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1427 kfree(SCtmp->host_scribble);
1428 SCtmp->host_scribble = NULL;
1429 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1430 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1433 return SUCCESS;
1435 return FAILED;
1436 #endif /* ERIC_neverdef */
1439 static int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
1441 int i;
1444 * This does a scsi reset for all devices on the bus.
1445 * In principle, we could also reset the 1542 - should
1446 * we do this? Try this first, and we can add that later
1447 * if it turns out to be useful.
1449 outb(SCRST, CONTROL(SCpnt->device->host->io_port));
1452 * Wait for the thing to settle down a bit. Unfortunately
1453 * this is going to basically lock up the machine while we
1454 * wait for this to complete. To be 100% correct, we need to
1455 * check for timeout, and if we are doing something like this
1456 * we are pretty desperate anyways.
1458 ssleep(4);
1460 spin_lock_irq(SCpnt->device->host->host_lock);
1462 WAIT(STATUS(SCpnt->device->host->io_port),
1463 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1466 * Now try to pick up the pieces. For all pending commands,
1467 * free any internal data structures, and basically clear things
1468 * out. We do not try and restart any commands or anything -
1469 * the strategy handler takes care of that crap.
1471 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1473 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1474 if (HOSTDATA(SCpnt->device->host)->SCint[i] != NULL) {
1475 Scsi_Cmnd *SCtmp;
1476 SCtmp = HOSTDATA(SCpnt->device->host)->SCint[i];
1479 if (SCtmp->device->soft_reset) {
1481 * If this device implements the soft reset option,
1482 * then it is still holding onto the command, and
1483 * may yet complete it. In this case, we don't
1484 * flush the data.
1486 continue;
1488 kfree(SCtmp->host_scribble);
1489 SCtmp->host_scribble = NULL;
1490 HOSTDATA(SCpnt->device->host)->SCint[i] = NULL;
1491 HOSTDATA(SCpnt->device->host)->mb[i].status = 0;
1495 spin_unlock_irq(SCpnt->device->host->host_lock);
1496 return SUCCESS;
1498 fail:
1499 spin_unlock_irq(SCpnt->device->host->host_lock);
1500 return FAILED;
1503 static int aha1542_host_reset(Scsi_Cmnd * SCpnt)
1505 int i;
1508 * This does a scsi reset for all devices on the bus.
1509 * In principle, we could also reset the 1542 - should
1510 * we do this? Try this first, and we can add that later
1511 * if it turns out to be useful.
1513 outb(HRST | SCRST, CONTROL(SCpnt->device->host->io_port));
1516 * Wait for the thing to settle down a bit. Unfortunately
1517 * this is going to basically lock up the machine while we
1518 * wait for this to complete. To be 100% correct, we need to
1519 * check for timeout, and if we are doing something like this
1520 * we are pretty desperate anyways.
1522 ssleep(4);
1523 spin_lock_irq(SCpnt->device->host->host_lock);
1525 WAIT(STATUS(SCpnt->device->host->io_port),
1526 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1529 * We need to do this too before the 1542 can interact with
1530 * us again.
1532 setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
1535 * Now try to pick up the pieces. For all pending commands,
1536 * free any internal data structures, and basically clear things
1537 * out. We do not try and restart any commands or anything -
1538 * the strategy handler takes care of that crap.
1540 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1542 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1543 if (HOSTDATA(SCpnt->device->host)->SCint[i] != NULL) {
1544 Scsi_Cmnd *SCtmp;
1545 SCtmp = HOSTDATA(SCpnt->device->host)->SCint[i];
1547 if (SCtmp->device->soft_reset) {
1549 * If this device implements the soft reset option,
1550 * then it is still holding onto the command, and
1551 * may yet complete it. In this case, we don't
1552 * flush the data.
1554 continue;
1556 kfree(SCtmp->host_scribble);
1557 SCtmp->host_scribble = NULL;
1558 HOSTDATA(SCpnt->device->host)->SCint[i] = NULL;
1559 HOSTDATA(SCpnt->device->host)->mb[i].status = 0;
1563 spin_unlock_irq(SCpnt->device->host->host_lock);
1564 return SUCCESS;
1566 fail:
1567 spin_unlock_irq(SCpnt->device->host->host_lock);
1568 return FAILED;
1571 #if 0
1573 * These are the old error handling routines. They are only temporarily
1574 * here while we play with the new error handling code.
1576 static int aha1542_old_abort(Scsi_Cmnd * SCpnt)
1578 #if 0
1579 unchar ahacmd = CMD_START_SCSI;
1580 unsigned long flags;
1581 struct mailbox *mb;
1582 int mbi, mbo, i;
1584 printk(KERN_DEBUG "In aha1542_abort: %x %x\n",
1585 inb(STATUS(SCpnt->host->io_port)),
1586 inb(INTRFLAGS(SCpnt->host->io_port)));
1588 spin_lock_irqsave(&aha1542_lock, flags);
1589 mb = HOSTDATA(SCpnt->host)->mb;
1590 mbi = HOSTDATA(SCpnt->host)->aha1542_last_mbi_used + 1;
1591 if (mbi >= 2 * AHA1542_MAILBOXES)
1592 mbi = AHA1542_MAILBOXES;
1594 do {
1595 if (mb[mbi].status != 0)
1596 break;
1597 mbi++;
1598 if (mbi >= 2 * AHA1542_MAILBOXES)
1599 mbi = AHA1542_MAILBOXES;
1600 } while (mbi != HOSTDATA(SCpnt->host)->aha1542_last_mbi_used);
1601 spin_unlock_irqrestore(&aha1542_lock, flags);
1603 if (mb[mbi].status) {
1604 printk(KERN_ERR "Lost interrupt discovered on irq %d - attempting to recover\n",
1605 SCpnt->host->irq);
1606 aha1542_intr_handle(SCpnt->host, NULL);
1607 return 0;
1609 /* OK, no lost interrupt. Try looking to see how many pending commands
1610 we think we have. */
1612 for (i = 0; i < AHA1542_MAILBOXES; i++)
1613 if (HOSTDATA(SCpnt->host)->SCint[i]) {
1614 if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1615 printk(KERN_ERR "Timed out command pending for %s\n",
1616 SCpnt->request->rq_disk ?
1617 SCpnt->request->rq_disk->disk_name : "?"
1619 if (HOSTDATA(SCpnt->host)->mb[i].status) {
1620 printk(KERN_ERR "OGMB still full - restarting\n");
1621 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1623 } else
1624 printk(KERN_ERR "Other pending command %s\n",
1625 SCpnt->request->rq_disk ?
1626 SCpnt->request->rq_disk->disk_name : "?"
1629 #endif
1631 DEB(printk("aha1542_abort\n"));
1632 #if 0
1633 spin_lock_irqsave(&aha1542_lock, flags);
1634 for (mbo = 0; mbo < AHA1542_MAILBOXES; mbo++) {
1635 if (SCpnt == HOSTDATA(SCpnt->host)->SCint[mbo]) {
1636 mb[mbo].status = 2; /* Abort command */
1637 aha1542_out(SCpnt->host->io_port, &ahacmd, 1); /* start scsi command */
1638 spin_unlock_irqrestore(&aha1542_lock, flags);
1639 break;
1642 if (AHA1542_MAILBOXES == mbo)
1643 spin_unlock_irqrestore(&aha1542_lock, flags);
1644 #endif
1645 return SCSI_ABORT_SNOOZE;
1648 /* We do not implement a reset function here, but the upper level code
1649 assumes that it will get some kind of response for the command in
1650 SCpnt. We must oblige, or the command will hang the scsi system.
1651 For a first go, we assume that the 1542 notifies us with all of the
1652 pending commands (it does implement soft reset, after all). */
1654 static int aha1542_old_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1656 unchar ahacmd = CMD_START_SCSI;
1657 int i;
1660 * See if a bus reset was suggested.
1662 if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
1664 * This does a scsi reset for all devices on the bus.
1665 * In principle, we could also reset the 1542 - should
1666 * we do this? Try this first, and we can add that later
1667 * if it turns out to be useful.
1669 outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
1672 * Wait for the thing to settle down a bit. Unfortunately
1673 * this is going to basically lock up the machine while we
1674 * wait for this to complete. To be 100% correct, we need to
1675 * check for timeout, and if we are doing something like this
1676 * we are pretty desperate anyways.
1678 WAIT(STATUS(SCpnt->host->io_port),
1679 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1682 * We need to do this too before the 1542 can interact with
1683 * us again.
1685 setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
1688 * Now try to pick up the pieces. Restart all commands
1689 * that are currently active on the bus, and reset all of
1690 * the datastructures. We have some time to kill while
1691 * things settle down, so print a nice message.
1693 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1695 for (i = 0; i < AHA1542_MAILBOXES; i++)
1696 if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
1697 Scsi_Cmnd *SCtmp;
1698 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1699 SCtmp->result = DID_RESET << 16;
1700 kfree(SCtmp->host_scribble);
1701 SCtmp->host_scribble = NULL;
1702 printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
1703 SCtmp->scsi_done(SCpnt);
1705 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1706 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1709 * Now tell the mid-level code what we did here. Since
1710 * we have restarted all of the outstanding commands,
1711 * then report SUCCESS.
1713 return (SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET);
1714 fail:
1715 printk(KERN_CRIT "aha1542.c: Unable to perform hard reset.\n");
1716 printk(KERN_CRIT "Power cycle machine to reset\n");
1717 return (SCSI_RESET_ERROR | SCSI_RESET_BUS_RESET);
1720 } else {
1721 /* This does a selective reset of just the one device */
1722 /* First locate the ccb for this command */
1723 for (i = 0; i < AHA1542_MAILBOXES; i++)
1724 if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1725 HOSTDATA(SCpnt->host)->ccb[i].op = 0x81; /* BUS DEVICE RESET */
1726 /* Now tell the 1542 to flush all pending commands for this target */
1727 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1729 /* Here is the tricky part. What to do next. Do we get an interrupt
1730 for the commands that we aborted with the specified target, or
1731 do we generate this on our own? Try it without first and see
1732 what happens */
1733 printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1735 /* If the first does not work, then try the second. I think the
1736 first option is more likely to be correct. Free the command
1737 block for all commands running on this target... */
1738 for (i = 0; i < AHA1542_MAILBOXES; i++)
1739 if (HOSTDATA(SCpnt->host)->SCint[i] &&
1740 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1741 Scsi_Cmnd *SCtmp;
1742 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1743 SCtmp->result = DID_RESET << 16;
1744 kfree(SCtmp->host_scribble);
1745 SCtmp->host_scribble = NULL;
1746 printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
1747 SCtmp->scsi_done(SCpnt);
1749 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1750 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1752 return SCSI_RESET_SUCCESS;
1755 /* No active command at this time, so this means that each time we got
1756 some kind of response the last time through. Tell the mid-level code
1757 to request sense information in order to decide what to do next. */
1758 return SCSI_RESET_PUNT;
1760 #endif /* end of big comment block around old_abort + old_reset */
1762 static int aha1542_biosparam(struct scsi_device *sdev,
1763 struct block_device *bdev, sector_t capacity, int *ip)
1765 int translation_algorithm;
1766 int size = capacity;
1768 translation_algorithm = HOSTDATA(sdev->host)->bios_translation;
1770 if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
1771 /* Please verify that this is the same as what DOS returns */
1772 ip[0] = 255;
1773 ip[1] = 63;
1774 ip[2] = size / 255 / 63;
1775 } else {
1776 ip[0] = 64;
1777 ip[1] = 32;
1778 ip[2] = size >> 11;
1781 return 0;
1783 MODULE_LICENSE("GPL");
1786 static struct scsi_host_template driver_template = {
1787 .proc_name = "aha1542",
1788 .name = "Adaptec 1542",
1789 .detect = aha1542_detect,
1790 .release = aha1542_release,
1791 .queuecommand = aha1542_queuecommand,
1792 .eh_device_reset_handler= aha1542_dev_reset,
1793 .eh_bus_reset_handler = aha1542_bus_reset,
1794 .eh_host_reset_handler = aha1542_host_reset,
1795 .bios_param = aha1542_biosparam,
1796 .can_queue = AHA1542_MAILBOXES,
1797 .this_id = 7,
1798 .sg_tablesize = AHA1542_SCATTER,
1799 .cmd_per_lun = AHA1542_CMDLUN,
1800 .unchecked_isa_dma = 1,
1801 .use_clustering = ENABLE_CLUSTERING,
1803 #include "scsi_module.c"