- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / scsi / aha1542.c
blobdd5f627d3ce07e9885819172f93ab4e2624a967c
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/config.h>
29 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/ioport.h>
35 #include <linux/delay.h>
36 #include <linux/sched.h>
37 #include <linux/proc_fs.h>
38 #include <linux/init.h>
39 #include <linux/spinlock.h>
40 #include <linux/pci.h>
41 #include <linux/isapnp.h>
42 #include <asm/dma.h>
43 #include <asm/system.h>
44 #include <asm/io.h>
45 #include <linux/blk.h>
46 #include <linux/mca.h>
48 #include "scsi.h"
49 #include "hosts.h"
52 #include "aha1542.h"
54 #define SCSI_PA(address) virt_to_bus(address)
56 static void BAD_DMA(void *address, unsigned int length)
58 printk(KERN_CRIT "buf vaddress %p paddress 0x%lx length %d\n",
59 address,
60 SCSI_PA(address),
61 length);
62 panic("Buffer at physical address > 16Mb used for aha1542");
65 static void BAD_SG_DMA(Scsi_Cmnd * SCpnt,
66 struct scatterlist *sgpnt,
67 int nseg,
68 int badseg)
70 printk(KERN_CRIT "sgpnt[%d:%d] addr %p/0x%lx alt %p/0x%lx length %d\n",
71 badseg, nseg,
72 sgpnt[badseg].address,
73 SCSI_PA(sgpnt[badseg].address),
74 sgpnt[badseg].alt_address,
75 sgpnt[badseg].alt_address ? SCSI_PA(sgpnt[badseg].alt_address) : 0,
76 sgpnt[badseg].length);
79 * Not safe to continue.
81 panic("Buffer at physical address > 16Mb used for aha1542");
84 #include<linux/stat.h>
86 #ifdef DEBUG
87 #define DEB(x) x
88 #else
89 #define DEB(x)
90 #endif
93 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 $";
96 /* The adaptec can be configured for quite a number of addresses, but
97 I generally do not want the card poking around at random. We allow
98 two addresses - this allows people to use the Adaptec with a Midi
99 card, which also used 0x330 -- can be overridden with LILO! */
101 #define MAXBOARDS 4 /* Increase this and the sizes of the
102 arrays below, if you need more.. */
104 /* Boards 3,4 slots are reserved for ISAPnP/MCA scans */
106 static unsigned int bases[MAXBOARDS] = {0x330, 0x334, 0, 0};
108 /* set by aha1542_setup according to the command line */
110 static int setup_called[MAXBOARDS];
111 static int setup_buson[MAXBOARDS];
112 static int setup_busoff[MAXBOARDS];
113 static int setup_dmaspeed[MAXBOARDS] = { -1, -1, -1, -1 };
115 static char *setup_str[MAXBOARDS];
118 * LILO/Module params: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]
120 * Where: <PORTBASE> is any of the valid AHA addresses:
121 * 0x130, 0x134, 0x230, 0x234, 0x330, 0x334
122 * <BUSON> is the time (in microsecs) that AHA spends on the AT-bus
123 * when transferring data. 1542A power-on default is 11us,
124 * valid values are in range: 2..15 (decimal)
125 * <BUSOFF> is the time that AHA spends OFF THE BUS after while
126 * it is transferring data (not to monopolize the bus).
127 * Power-on default is 4us, valid range: 1..64 microseconds.
128 * <DMASPEED> Default is jumper selected (1542A: on the J1),
129 * but experimenter can alter it with this.
130 * Valid values: 5, 6, 7, 8, 10 (MB/s)
131 * Factory default is 5 MB/s.
134 #if defined(MODULE)
135 int isapnp=0;
136 int aha1542[] = {0x330, 11, 4, -1};
137 MODULE_PARM(aha1542, "1-4i");
138 MODULE_PARM(isapnp, "i");
139 #else
140 int isapnp=1;
141 #endif
143 #define BIOS_TRANSLATION_1632 0 /* Used by some old 1542A boards */
144 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
145 #define BIOS_TRANSLATION_25563 2 /* Big disk case */
147 struct aha1542_hostdata {
148 /* This will effectively start both of them at the first mailbox */
149 int bios_translation; /* Mapping bios uses - for compatibility */
150 int aha1542_last_mbi_used;
151 int aha1542_last_mbo_used;
152 Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
153 struct mailbox mb[2 * AHA1542_MAILBOXES];
154 struct ccb ccb[AHA1542_MAILBOXES];
157 #define HOSTDATA(host) ((struct aha1542_hostdata *) &host->hostdata)
159 static struct Scsi_Host *aha_host[7]; /* One for each IRQ level (9-15) */
164 #define WAITnexttimeout 3000000
166 static void setup_mailboxes(int base_io, struct Scsi_Host *shpnt);
167 static int aha1542_restart(struct Scsi_Host *shost);
168 static void aha1542_intr_handle(int irq, void *dev_id, struct pt_regs *regs);
169 static void do_aha1542_intr_handle(int irq, void *dev_id, struct pt_regs *regs);
171 #define aha1542_intr_reset(base) outb(IRST, CONTROL(base))
173 #define WAIT(port, mask, allof, noneof) \
174 { register int WAITbits; \
175 register int WAITtimeout = WAITnexttimeout; \
176 while (1) { \
177 WAITbits = inb(port) & (mask); \
178 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
179 break; \
180 if (--WAITtimeout == 0) goto fail; \
184 /* Similar to WAIT, except we use the udelay call to regulate the
185 amount of time we wait. */
186 #define WAITd(port, mask, allof, noneof, timeout) \
187 { register int WAITbits; \
188 register int WAITtimeout = timeout; \
189 while (1) { \
190 WAITbits = inb(port) & (mask); \
191 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
192 break; \
193 mdelay(1); \
194 if (--WAITtimeout == 0) goto fail; \
198 static void aha1542_stat(void)
200 /* int s = inb(STATUS), i = inb(INTRFLAGS);
201 printk("status=%x intrflags=%x\n", s, i, WAITnexttimeout-WAITtimeout); */
204 /* This is a bit complicated, but we need to make sure that an interrupt
205 routine does not send something out while we are in the middle of this.
206 Fortunately, it is only at boot time that multi-byte messages
207 are ever sent. */
208 static int aha1542_out(unsigned int base, unchar * cmdp, int len)
210 unsigned long flags = 0;
212 save_flags(flags);
213 if (len == 1) {
214 while (1 == 1) {
215 WAIT(STATUS(base), CDF, 0, CDF);
216 cli();
217 if (inb(STATUS(base)) & CDF) {
218 restore_flags(flags);
219 continue;
221 outb(*cmdp, DATA(base));
222 restore_flags(flags);
223 return 0;
225 } else {
226 cli();
227 while (len--) {
228 WAIT(STATUS(base), CDF, 0, CDF);
229 outb(*cmdp++, DATA(base));
231 restore_flags(flags);
233 return 0;
234 fail:
235 restore_flags(flags);
236 printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
237 aha1542_stat();
238 return 1;
241 /* Only used at boot time, so we do not need to worry about latency as much
242 here */
244 static int aha1542_in(unsigned int base, unchar * cmdp, int len)
246 unsigned long flags;
248 save_flags(flags);
249 cli();
250 while (len--) {
251 WAIT(STATUS(base), DF, DF, 0);
252 *cmdp++ = inb(DATA(base));
254 restore_flags(flags);
255 return 0;
256 fail:
257 restore_flags(flags);
258 printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
259 aha1542_stat();
260 return 1;
263 /* Similar to aha1542_in, except that we wait a very short period of time.
264 We use this if we know the board is alive and awake, but we are not sure
265 if the board will respond to the command we are about to send or not */
266 static int aha1542_in1(unsigned int base, unchar * cmdp, int len)
268 unsigned long flags;
270 save_flags(flags);
271 cli();
272 while (len--) {
273 WAITd(STATUS(base), DF, DF, 0, 100);
274 *cmdp++ = inb(DATA(base));
276 restore_flags(flags);
277 return 0;
278 fail:
279 restore_flags(flags);
280 return 1;
283 static int makecode(unsigned hosterr, unsigned scsierr)
285 switch (hosterr) {
286 case 0x0:
287 case 0xa: /* Linked command complete without error and linked normally */
288 case 0xb: /* Linked command complete without error, interrupt generated */
289 hosterr = 0;
290 break;
292 case 0x11: /* Selection time out-The initiator selection or target
293 reselection was not complete within the SCSI Time out period */
294 hosterr = DID_TIME_OUT;
295 break;
297 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
298 than was allocated by the Data Length field or the sum of the
299 Scatter / Gather Data Length fields. */
301 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
303 case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
304 invalid. This usually indicates a software failure. */
306 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
307 This usually indicates a software failure. */
309 case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
310 of linked CCB's does not specify the same logical unit number as
311 the first. */
312 case 0x18: /* Invalid Target Direction received from Host-The direction of a
313 Target Mode CCB was invalid. */
315 case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
316 received to service data transfer between the same target LUN
317 and initiator SCSI ID in the same direction. */
319 case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
320 length segment or invalid segment list boundaries was received.
321 A CCB parameter was invalid. */
322 DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
323 hosterr = DID_ERROR; /* Couldn't find any better */
324 break;
326 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
327 phase sequence was requested by the target. The host adapter
328 will generate a SCSI Reset Condition, notifying the host with
329 a SCRD interrupt */
330 hosterr = DID_RESET;
331 break;
332 default:
333 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
334 break;
336 return scsierr | (hosterr << 16);
339 static int aha1542_test_port(int bse, struct Scsi_Host *shpnt)
341 unchar inquiry_cmd[] = {CMD_INQUIRY};
342 unchar inquiry_result[4];
343 unchar *cmdp;
344 int len;
345 volatile int debug = 0;
347 /* Quick and dirty test for presence of the card. */
348 if (inb(STATUS(bse)) == 0xff)
349 return 0;
351 /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
353 /* DEB(printk("aha1542_test_port called \n")); */
355 /* In case some other card was probing here, reset interrupts */
356 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
358 outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
360 mdelay(20); /* Wait a little bit for things to settle down. */
362 debug = 1;
363 /* Expect INIT and IDLE, any of the others are bad */
364 WAIT(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
366 debug = 2;
367 /* Shouldn't have generated any interrupts during reset */
368 if (inb(INTRFLAGS(bse)) & INTRMASK)
369 goto fail;
372 /* Perform a host adapter inquiry instead so we do not need to set
373 up the mailboxes ahead of time */
375 aha1542_out(bse, inquiry_cmd, 1);
377 debug = 3;
378 len = 4;
379 cmdp = &inquiry_result[0];
381 while (len--) {
382 WAIT(STATUS(bse), DF, DF, 0);
383 *cmdp++ = inb(DATA(bse));
386 debug = 8;
387 /* Reading port should reset DF */
388 if (inb(STATUS(bse)) & DF)
389 goto fail;
391 debug = 9;
392 /* When HACC, command is completed, and we're though testing */
393 WAIT(INTRFLAGS(bse), HACC, HACC, 0);
394 /* now initialize adapter */
396 debug = 10;
397 /* Clear interrupts */
398 outb(IRST, CONTROL(bse));
400 debug = 11;
402 return debug; /* 1 = ok */
403 fail:
404 return 0; /* 0 = not ok */
407 /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
408 static void do_aha1542_intr_handle(int irq, void *dev_id, struct pt_regs *regs)
410 unsigned long flags;
412 spin_lock_irqsave(&io_request_lock, flags);
413 aha1542_intr_handle(irq, dev_id, regs);
414 spin_unlock_irqrestore(&io_request_lock, flags);
417 /* A "high" level interrupt handler */
418 static void aha1542_intr_handle(int irq, void *dev_id, struct pt_regs *regs)
420 void (*my_done) (Scsi_Cmnd *) = NULL;
421 int errstatus, mbi, mbo, mbistatus;
422 int number_serviced;
423 unsigned int flags;
424 struct Scsi_Host *shost;
425 Scsi_Cmnd *SCtmp;
426 int flag;
427 int needs_restart;
428 struct mailbox *mb;
429 struct ccb *ccb;
431 shost = aha_host[irq - 9];
432 if (!shost)
433 panic("Splunge!");
435 mb = HOSTDATA(shost)->mb;
436 ccb = HOSTDATA(shost)->ccb;
438 #ifdef DEBUG
440 flag = inb(INTRFLAGS(shost->io_port));
441 printk(KERN_DEBUG "aha1542_intr_handle: ");
442 if (!(flag & ANYINTR))
443 printk("no interrupt?");
444 if (flag & MBIF)
445 printk("MBIF ");
446 if (flag & MBOA)
447 printk("MBOF ");
448 if (flag & HACC)
449 printk("HACC ");
450 if (flag & SCRD)
451 printk("SCRD ");
452 printk("status %02x\n", inb(STATUS(shost->io_port)));
454 #endif
455 number_serviced = 0;
456 needs_restart = 0;
458 while (1 == 1) {
459 flag = inb(INTRFLAGS(shost->io_port));
461 /* Check for unusual interrupts. If any of these happen, we should
462 probably do something special, but for now just printing a message
463 is sufficient. A SCSI reset detected is something that we really
464 need to deal with in some way. */
465 if (flag & ~MBIF) {
466 if (flag & MBOA)
467 printk("MBOF ");
468 if (flag & HACC)
469 printk("HACC ");
470 if (flag & SCRD) {
471 needs_restart = 1;
472 printk("SCRD ");
475 aha1542_intr_reset(shost->io_port);
477 save_flags(flags);
478 cli();
479 mbi = HOSTDATA(shost)->aha1542_last_mbi_used + 1;
480 if (mbi >= 2 * AHA1542_MAILBOXES)
481 mbi = AHA1542_MAILBOXES;
483 do {
484 if (mb[mbi].status != 0)
485 break;
486 mbi++;
487 if (mbi >= 2 * AHA1542_MAILBOXES)
488 mbi = AHA1542_MAILBOXES;
489 } while (mbi != HOSTDATA(shost)->aha1542_last_mbi_used);
491 if (mb[mbi].status == 0) {
492 restore_flags(flags);
493 /* Hmm, no mail. Must have read it the last time around */
494 if (!number_serviced && !needs_restart)
495 printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
496 /* We detected a reset. Restart all pending commands for
497 devices that use the hard reset option */
498 if (needs_restart)
499 aha1542_restart(shost);
500 return;
503 mbo = (scsi2int(mb[mbi].ccbptr) - (SCSI_PA(&ccb[0]))) / sizeof(struct ccb);
504 mbistatus = mb[mbi].status;
505 mb[mbi].status = 0;
506 HOSTDATA(shost)->aha1542_last_mbi_used = mbi;
507 restore_flags(flags);
509 #ifdef DEBUG
511 if (ccb[mbo].tarstat | ccb[mbo].hastat)
512 printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
513 ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
515 #endif
517 if (mbistatus == 3)
518 continue; /* Aborted command not found */
520 #ifdef DEBUG
521 printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
522 #endif
524 SCtmp = HOSTDATA(shost)->SCint[mbo];
526 if (!SCtmp || !SCtmp->scsi_done) {
527 printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
528 printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
529 ccb[mbo].hastat, ccb[mbo].idlun, mbo);
530 return;
532 my_done = SCtmp->scsi_done;
533 if (SCtmp->host_scribble) {
534 scsi_free(SCtmp->host_scribble, 512);
535 SCtmp->host_scribble = 0;
537 /* Fetch the sense data, and tuck it away, in the required slot. The
538 Adaptec automatically fetches it, and there is no guarantee that
539 we will still have it in the cdb when we come back */
540 if (ccb[mbo].tarstat == 2)
541 memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
542 sizeof(SCtmp->sense_buffer));
545 /* is there mail :-) */
547 /* more error checking left out here */
548 if (mbistatus != 1)
549 /* This is surely wrong, but I don't know what's right */
550 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
551 else
552 errstatus = 0;
554 #ifdef DEBUG
555 if (errstatus)
556 printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
557 ccb[mbo].hastat, ccb[mbo].tarstat);
558 #endif
560 if (ccb[mbo].tarstat == 2) {
561 #ifdef DEBUG
562 int i;
563 #endif
564 DEB(printk("aha1542_intr_handle: sense:"));
565 #ifdef DEBUG
566 for (i = 0; i < 12; i++)
567 printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
568 printk("\n");
569 #endif
571 DEB(printk("aha1542_intr_handle: buf:"));
572 for (i = 0; i < bufflen; i++)
573 printk("%02x ", ((unchar *)buff)[i]);
574 printk("\n");
577 DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
578 SCtmp->result = errstatus;
579 HOSTDATA(shost)->SCint[mbo] = NULL; /* This effectively frees up the mailbox slot, as
580 far as queuecommand is concerned */
581 my_done(SCtmp);
582 number_serviced++;
586 int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
588 unchar ahacmd = CMD_START_SCSI;
589 unchar direction;
590 unchar *cmd = (unchar *) SCpnt->cmnd;
591 unchar target = SCpnt->target;
592 unchar lun = SCpnt->lun;
593 unsigned long flags;
594 void *buff = SCpnt->request_buffer;
595 int bufflen = SCpnt->request_bufflen;
596 int mbo;
597 struct mailbox *mb;
598 struct ccb *ccb;
600 DEB(int i);
602 mb = HOSTDATA(SCpnt->host)->mb;
603 ccb = HOSTDATA(SCpnt->host)->ccb;
605 DEB(if (target > 1) {
606 SCpnt->result = DID_TIME_OUT << 16;
607 done(SCpnt); return 0;
611 if (*cmd == REQUEST_SENSE) {
612 /* Don't do the command - we have the sense data already */
613 #if 0
614 /* scsi_request_sense() provides a buffer of size 256,
615 so there is no reason to expect equality */
616 if (bufflen != sizeof(SCpnt->sense_buffer))
617 printk(KERN_CRIT "aha1542: Wrong buffer length supplied "
618 "for request sense (%d)\n", bufflen);
619 #endif
620 SCpnt->result = 0;
621 done(SCpnt);
622 return 0;
624 #ifdef DEBUG
625 if (*cmd == READ_10 || *cmd == WRITE_10)
626 i = xscsi2int(cmd + 2);
627 else if (*cmd == READ_6 || *cmd == WRITE_6)
628 i = scsi2int(cmd + 2);
629 else
630 i = -1;
631 if (done)
632 printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
633 else
634 printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
635 aha1542_stat();
636 printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
637 for (i = 0; i < SCpnt->cmd_len; i++)
638 printk("%02x ", cmd[i]);
639 printk("\n");
640 if (*cmd == WRITE_10 || *cmd == WRITE_6)
641 return 0; /* we are still testing, so *don't* write */
642 #endif
643 /* Use the outgoing mailboxes in a round-robin fashion, because this
644 is how the host adapter will scan for them */
646 save_flags(flags);
647 cli();
648 mbo = HOSTDATA(SCpnt->host)->aha1542_last_mbo_used + 1;
649 if (mbo >= AHA1542_MAILBOXES)
650 mbo = 0;
652 do {
653 if (mb[mbo].status == 0 && HOSTDATA(SCpnt->host)->SCint[mbo] == NULL)
654 break;
655 mbo++;
656 if (mbo >= AHA1542_MAILBOXES)
657 mbo = 0;
658 } while (mbo != HOSTDATA(SCpnt->host)->aha1542_last_mbo_used);
660 if (mb[mbo].status || HOSTDATA(SCpnt->host)->SCint[mbo])
661 panic("Unable to find empty mailbox for aha1542.\n");
663 HOSTDATA(SCpnt->host)->SCint[mbo] = SCpnt; /* This will effectively prevent someone else from
664 screwing with this cdb. */
666 HOSTDATA(SCpnt->host)->aha1542_last_mbo_used = mbo;
667 restore_flags(flags);
669 #ifdef DEBUG
670 printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
671 #endif
673 any2scsi(mb[mbo].ccbptr, SCSI_PA(&ccb[mbo])); /* This gets trashed for some reason */
675 memset(&ccb[mbo], 0, sizeof(struct ccb));
677 ccb[mbo].cdblen = SCpnt->cmd_len;
679 direction = 0;
680 if (*cmd == READ_10 || *cmd == READ_6)
681 direction = 8;
682 else if (*cmd == WRITE_10 || *cmd == WRITE_6)
683 direction = 16;
685 memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
687 if (SCpnt->use_sg) {
688 struct scatterlist *sgpnt;
689 struct chain *cptr;
690 #ifdef DEBUG
691 unsigned char *ptr;
692 #endif
693 int i;
694 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
695 SCpnt->host_scribble = (unsigned char *) scsi_malloc(512);
696 sgpnt = (struct scatterlist *) SCpnt->request_buffer;
697 cptr = (struct chain *) SCpnt->host_scribble;
698 if (cptr == NULL)
699 panic("aha1542.c: unable to allocate DMA memory\n");
700 for (i = 0; i < SCpnt->use_sg; i++) {
701 if (sgpnt[i].length == 0 || SCpnt->use_sg > 16 ||
702 (((int) sgpnt[i].address) & 1) || (sgpnt[i].length & 1)) {
703 unsigned char *ptr;
704 printk(KERN_CRIT "Bad segment list supplied to aha1542.c (%d, %d)\n", SCpnt->use_sg, i);
705 for (i = 0; i < SCpnt->use_sg; i++) {
706 printk(KERN_CRIT "%d: %x %x %d\n", i, (unsigned int) sgpnt[i].address, (unsigned int) sgpnt[i].alt_address,
707 sgpnt[i].length);
709 printk(KERN_CRIT "cptr %x: ", (unsigned int) cptr);
710 ptr = (unsigned char *) &cptr[i];
711 for (i = 0; i < 18; i++)
712 printk("%02x ", ptr[i]);
713 panic("Foooooooood fight!");
715 any2scsi(cptr[i].dataptr, SCSI_PA(sgpnt[i].address));
716 if (SCSI_PA(sgpnt[i].address + sgpnt[i].length - 1) > ISA_DMA_THRESHOLD)
717 BAD_SG_DMA(SCpnt, sgpnt, SCpnt->use_sg, i);
718 any2scsi(cptr[i].datalen, sgpnt[i].length);
720 any2scsi(ccb[mbo].datalen, SCpnt->use_sg * sizeof(struct chain));
721 any2scsi(ccb[mbo].dataptr, SCSI_PA(cptr));
722 #ifdef DEBUG
723 printk("cptr %x: ", cptr);
724 ptr = (unsigned char *) cptr;
725 for (i = 0; i < 18; i++)
726 printk("%02x ", ptr[i]);
727 #endif
728 } else {
729 ccb[mbo].op = 0; /* SCSI Initiator Command */
730 SCpnt->host_scribble = NULL;
731 any2scsi(ccb[mbo].datalen, bufflen);
732 if (buff && SCSI_PA(buff + bufflen - 1) > ISA_DMA_THRESHOLD)
733 BAD_DMA(buff, bufflen);
734 any2scsi(ccb[mbo].dataptr, SCSI_PA(buff));
736 ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
737 ccb[mbo].rsalen = 16;
738 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
739 ccb[mbo].commlinkid = 0;
741 #ifdef DEBUG
743 int i;
744 printk(KERN_DEBUG "aha1542_command: sending.. ");
745 for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
746 printk("%02x ", ((unchar *) & ccb[mbo])[i]);
748 #endif
750 if (done) {
751 DEB(printk("aha1542_queuecommand: now waiting for interrupt ");
752 aha1542_stat());
753 SCpnt->scsi_done = done;
754 mb[mbo].status = 1;
755 aha1542_out(SCpnt->host->io_port, &ahacmd, 1); /* start scsi command */
756 DEB(aha1542_stat());
757 } else
758 printk("aha1542_queuecommand: done can't be NULL\n");
760 return 0;
763 static void internal_done(Scsi_Cmnd * SCpnt)
765 SCpnt->SCp.Status++;
768 int aha1542_command(Scsi_Cmnd * SCpnt)
770 DEB(printk("aha1542_command: ..calling aha1542_queuecommand\n"));
772 aha1542_queuecommand(SCpnt, internal_done);
774 SCpnt->SCp.Status = 0;
775 while (!SCpnt->SCp.Status)
776 barrier();
777 return SCpnt->result;
780 /* Initialize mailboxes */
781 static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
783 int i;
784 struct mailbox *mb;
785 struct ccb *ccb;
787 unchar cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
789 mb = HOSTDATA(shpnt)->mb;
790 ccb = HOSTDATA(shpnt)->ccb;
792 for (i = 0; i < AHA1542_MAILBOXES; i++) {
793 mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
794 any2scsi(mb[i].ccbptr, SCSI_PA(&ccb[i]));
796 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
797 any2scsi((cmd + 2), SCSI_PA(mb));
798 aha1542_out(bse, cmd, 5);
799 WAIT(INTRFLAGS(bse), INTRMASK, HACC, 0);
800 while (0) {
801 fail:
802 printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
804 aha1542_intr_reset(bse);
807 static int aha1542_getconfig(int base_io, unsigned char *irq_level, unsigned char *dma_chan, unsigned char *scsi_id)
809 unchar inquiry_cmd[] = {CMD_RETCONF};
810 unchar inquiry_result[3];
811 int i;
812 i = inb(STATUS(base_io));
813 if (i & DF) {
814 i = inb(DATA(base_io));
816 aha1542_out(base_io, inquiry_cmd, 1);
817 aha1542_in(base_io, inquiry_result, 3);
818 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
819 while (0) {
820 fail:
821 printk(KERN_ERR "aha1542_detect: query board settings\n");
823 aha1542_intr_reset(base_io);
824 switch (inquiry_result[0]) {
825 case 0x80:
826 *dma_chan = 7;
827 break;
828 case 0x40:
829 *dma_chan = 6;
830 break;
831 case 0x20:
832 *dma_chan = 5;
833 break;
834 case 0x01:
835 *dma_chan = 0;
836 break;
837 case 0:
838 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
839 Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
840 *dma_chan = 0xFF;
841 break;
842 default:
843 printk(KERN_ERR "Unable to determine Adaptec DMA priority. Disabling board\n");
844 return -1;
846 switch (inquiry_result[1]) {
847 case 0x40:
848 *irq_level = 15;
849 break;
850 case 0x20:
851 *irq_level = 14;
852 break;
853 case 0x8:
854 *irq_level = 12;
855 break;
856 case 0x4:
857 *irq_level = 11;
858 break;
859 case 0x2:
860 *irq_level = 10;
861 break;
862 case 0x1:
863 *irq_level = 9;
864 break;
865 default:
866 printk(KERN_ERR "Unable to determine Adaptec IRQ level. Disabling board\n");
867 return -1;
869 *scsi_id = inquiry_result[2] & 7;
870 return 0;
873 /* This function should only be called for 1542C boards - we can detect
874 the special firmware settings and unlock the board */
876 static int aha1542_mbenable(int base)
878 static unchar mbenable_cmd[3];
879 static unchar mbenable_result[2];
880 int retval;
882 retval = BIOS_TRANSLATION_6432;
884 mbenable_cmd[0] = CMD_EXTBIOS;
885 aha1542_out(base, mbenable_cmd, 1);
886 if (aha1542_in1(base, mbenable_result, 2))
887 return retval;
888 WAITd(INTRFLAGS(base), INTRMASK, HACC, 0, 100);
889 aha1542_intr_reset(base);
891 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
892 mbenable_cmd[0] = CMD_MBENABLE;
893 mbenable_cmd[1] = 0;
894 mbenable_cmd[2] = mbenable_result[1];
896 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
897 retval = BIOS_TRANSLATION_25563;
899 aha1542_out(base, mbenable_cmd, 3);
900 WAIT(INTRFLAGS(base), INTRMASK, HACC, 0);
902 while (0) {
903 fail:
904 printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
906 aha1542_intr_reset(base);
907 return retval;
910 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
911 static int aha1542_query(int base_io, int *transl)
913 unchar inquiry_cmd[] = {CMD_INQUIRY};
914 unchar inquiry_result[4];
915 int i;
916 i = inb(STATUS(base_io));
917 if (i & DF) {
918 i = inb(DATA(base_io));
920 aha1542_out(base_io, inquiry_cmd, 1);
921 aha1542_in(base_io, inquiry_result, 4);
922 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
923 while (0) {
924 fail:
925 printk(KERN_ERR "aha1542_detect: query card type\n");
927 aha1542_intr_reset(base_io);
929 *transl = BIOS_TRANSLATION_6432; /* Default case */
931 /* For an AHA1740 series board, we ignore the board since there is a
932 hardware bug which can lead to wrong blocks being returned if the board
933 is operating in the 1542 emulation mode. Since there is an extended mode
934 driver, we simply ignore the board and let the 1740 driver pick it up.
937 if (inquiry_result[0] == 0x43) {
938 printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
939 return 1;
942 /* Always call this - boards that do not support extended bios translation
943 will ignore the command, and we will set the proper default */
945 *transl = aha1542_mbenable(base_io);
947 return 0;
950 /* called from init/main.c */
951 void __init aha1542_setup(char *str, int *ints)
953 const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
954 static int setup_idx = 0;
955 int setup_portbase;
957 if (setup_idx >= MAXBOARDS) {
958 printk(KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
959 printk(KERN_ERR " Entryline 1: %s\n", setup_str[0]);
960 printk(KERN_ERR " Entryline 2: %s\n", setup_str[1]);
961 printk(KERN_ERR " This line: %s\n", str);
962 return;
964 if (ints[0] < 1 || ints[0] > 4) {
965 printk(KERN_ERR "aha1542: %s\n", str);
966 printk(ahausage);
967 printk(KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
969 setup_called[setup_idx] = ints[0];
970 setup_str[setup_idx] = str;
972 setup_portbase = ints[0] >= 1 ? ints[1] : 0; /* Preserve the default value.. */
973 setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
974 setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
975 if (ints[0] >= 4)
977 int atbt = -1;
978 switch (ints[4]) {
979 case 5:
980 atbt = 0x00;
981 break;
982 case 6:
983 atbt = 0x04;
984 break;
985 case 7:
986 atbt = 0x01;
987 break;
988 case 8:
989 atbt = 0x02;
990 break;
991 case 10:
992 atbt = 0x03;
993 break;
994 default:
995 printk(KERN_ERR "aha1542: %s\n", str);
996 printk(ahausage);
997 printk(KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s. Using jumper defaults.\n");
998 break;
1000 setup_dmaspeed[setup_idx] = atbt;
1002 if (setup_portbase != 0)
1003 bases[setup_idx] = setup_portbase;
1005 ++setup_idx;
1008 /* return non-zero on detection */
1009 int aha1542_detect(Scsi_Host_Template * tpnt)
1011 unsigned char dma_chan;
1012 unsigned char irq_level;
1013 unsigned char scsi_id;
1014 unsigned long flags;
1015 unsigned int base_io;
1016 int trans;
1017 struct Scsi_Host *shpnt = NULL;
1018 int count = 0;
1019 int indx;
1021 DEB(printk("aha1542_detect: \n"));
1023 tpnt->proc_name = "aha1542";
1025 #ifdef MODULE
1026 bases[0] = aha1542[0];
1027 setup_buson[0]=aha1542[1];
1028 setup_busoff[0] = aha1542[2];
1030 int atbt = -1;
1031 switch (aha1542[3]) {
1032 case 5:
1033 atbt = 0x00;
1034 break;
1035 case 6:
1036 atbt = 0x04;
1037 break;
1038 case 7:
1039 atbt = 0x01;
1040 break;
1041 case 8:
1042 atbt = 0x02;
1043 break;
1044 case 10:
1045 atbt = 0x03;
1046 break;
1048 setup_dmaspeed[0] = atbt;
1050 #endif
1053 * Find MicroChannel cards (AHA1640)
1055 #ifdef CONFIG_MCA
1056 if(MCA_bus) {
1057 int slot = 0;
1058 int pos = 0;
1060 for (indx = 0; (slot != MCA_NOTFOUND) &&
1061 (indx < sizeof(bases)/sizeof(bases[0])); indx++) {
1063 if (bases[indx])
1064 continue;
1066 /* Detect only AHA-1640 cards -- MCA ID 0F1F */
1067 slot = mca_find_unused_adapter(0x0f1f, slot);
1068 if (slot == MCA_NOTFOUND)
1069 break;
1072 /* Found one */
1073 pos = mca_read_stored_pos(slot, 3);
1075 /* Decode address */
1076 if (pos & 0x80) {
1077 if (pos & 0x02) {
1078 if (pos & 0x01)
1079 bases[indx] = 0x334;
1080 else
1081 bases[indx] = 0x234;
1082 } else {
1083 if (pos & 0x01)
1084 bases[indx] = 0x134;
1086 } else {
1087 if (pos & 0x02) {
1088 if (pos & 0x01)
1089 bases[indx] = 0x330;
1090 else
1091 bases[indx] = 0x230;
1092 } else {
1093 if (pos & 0x01)
1094 bases[indx] = 0x130;
1098 /* No need to decode IRQ and Arb level -- those are
1099 * read off the card later.
1101 printk(KERN_INFO "Found an AHA-1640 in MCA slot %d, I/O 0x%04x\n", slot, bases[indx]);
1103 mca_set_adapter_name(slot, "Adapter AHA-1640");
1104 mca_set_adapter_procfn(slot, NULL, NULL);
1105 mca_mark_as_used(slot);
1107 /* Go on */
1108 slot++;
1112 #endif
1115 * Hunt for ISA Plug'n'Pray Adaptecs (AHA1535)
1118 if(isapnp)
1120 struct pci_dev *pdev = NULL;
1121 for(indx = 0; indx <sizeof(bases)/sizeof(bases[0]);indx++)
1123 if(bases[indx])
1124 continue;
1125 pdev = isapnp_find_dev(NULL, ISAPNP_VENDOR('A', 'D', 'P'),
1126 ISAPNP_FUNCTION(0x1542), pdev);
1127 if(pdev==NULL)
1128 break;
1130 * Activate the PnP card
1133 if(pdev->prepare(pdev)<0)
1134 continue;
1136 if(!(pdev->resource[0].flags&IORESOURCE_IO))
1137 continue;
1139 pdev->resource[0].flags|=IORESOURCE_AUTO;
1141 if(pdev->activate(pdev)<0)
1142 continue;
1144 bases[indx] = pdev->resource[0].start;
1146 /* The card can be queried for its DMA, we have
1147 the DMA set up that is enough */
1149 printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
1152 for (indx = 0; indx < sizeof(bases) / sizeof(bases[0]); indx++)
1153 if (bases[indx] != 0 && !check_region(bases[indx], 4)) {
1154 shpnt = scsi_register(tpnt,
1155 sizeof(struct aha1542_hostdata));
1157 if(shpnt==NULL)
1158 continue;
1159 /* For now we do this - until kmalloc is more intelligent
1160 we are resigned to stupid hacks like this */
1161 if (SCSI_PA(shpnt) >= ISA_DMA_THRESHOLD) {
1162 printk(KERN_ERR "Invalid address for shpnt with 1542.\n");
1163 goto unregister;
1165 if (!aha1542_test_port(bases[indx], shpnt))
1166 goto unregister;
1169 base_io = bases[indx];
1171 /* Set the Bus on/off-times as not to ruin floppy performance */
1173 unchar oncmd[] = {CMD_BUSON_TIME, 7};
1174 unchar offcmd[] = {CMD_BUSOFF_TIME, 5};
1176 if (setup_called[indx]) {
1177 oncmd[1] = setup_buson[indx];
1178 offcmd[1] = setup_busoff[indx];
1180 aha1542_intr_reset(base_io);
1181 aha1542_out(base_io, oncmd, 2);
1182 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1183 aha1542_intr_reset(base_io);
1184 aha1542_out(base_io, offcmd, 2);
1185 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1186 if (setup_dmaspeed[indx] >= 0) {
1187 unchar dmacmd[] = {CMD_DMASPEED, 0};
1188 dmacmd[1] = setup_dmaspeed[indx];
1189 aha1542_intr_reset(base_io);
1190 aha1542_out(base_io, dmacmd, 2);
1191 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1193 while (0) {
1194 fail:
1195 printk(KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
1197 aha1542_intr_reset(base_io);
1199 if (aha1542_query(base_io, &trans))
1200 goto unregister;
1202 if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
1203 goto unregister;
1205 printk(KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
1206 if (dma_chan != 0xFF)
1207 printk(", DMA priority %d", dma_chan);
1208 printk("\n");
1210 DEB(aha1542_stat());
1211 setup_mailboxes(base_io, shpnt);
1213 DEB(aha1542_stat());
1215 DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
1216 save_flags(flags);
1217 cli();
1218 if (request_irq(irq_level, do_aha1542_intr_handle, 0, "aha1542", NULL)) {
1219 printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
1220 restore_flags(flags);
1221 goto unregister;
1223 if (dma_chan != 0xFF) {
1224 if (request_dma(dma_chan, "aha1542")) {
1225 printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
1226 free_irq(irq_level, NULL);
1227 restore_flags(flags);
1228 goto unregister;
1230 if (dma_chan == 0 || dma_chan >= 5) {
1231 set_dma_mode(dma_chan, DMA_MODE_CASCADE);
1232 enable_dma(dma_chan);
1235 aha_host[irq_level - 9] = shpnt;
1236 shpnt->this_id = scsi_id;
1237 shpnt->unique_id = base_io;
1238 shpnt->io_port = base_io;
1239 shpnt->n_io_port = 4; /* Number of bytes of I/O space used */
1240 shpnt->dma_channel = dma_chan;
1241 shpnt->irq = irq_level;
1242 HOSTDATA(shpnt)->bios_translation = trans;
1243 if (trans == BIOS_TRANSLATION_25563)
1244 printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
1245 HOSTDATA(shpnt)->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
1246 HOSTDATA(shpnt)->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
1247 memset(HOSTDATA(shpnt)->SCint, 0, sizeof(HOSTDATA(shpnt)->SCint));
1248 restore_flags(flags);
1249 #if 0
1250 DEB(printk(" *** READ CAPACITY ***\n"));
1253 unchar buf[8];
1254 static unchar cmd[] = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1255 int i;
1257 for (i = 0; i < sizeof(buf); ++i)
1258 buf[i] = 0x87;
1259 for (i = 0; i < 2; ++i)
1260 if (!aha1542_command(i, cmd, buf, sizeof(buf))) {
1261 printk(KERN_DEBUG "aha_detect: LU %d sector_size %d device_size %d\n",
1262 i, xscsi2int(buf + 4), xscsi2int(buf));
1266 DEB(printk(" *** NOW RUNNING MY OWN TEST *** \n"));
1268 for (i = 0; i < 4; ++i) {
1269 unsigned char cmd[10];
1270 static buffer[512];
1272 cmd[0] = READ_10;
1273 cmd[1] = 0;
1274 xany2scsi(cmd + 2, i);
1275 cmd[6] = 0;
1276 cmd[7] = 0;
1277 cmd[8] = 1;
1278 cmd[9] = 0;
1279 aha1542_command(0, cmd, buffer, 512);
1281 #endif
1282 request_region(bases[indx], 4, "aha1542"); /* Register the IO ports that we use */
1283 count++;
1284 continue;
1285 unregister:
1286 scsi_unregister(shpnt);
1287 continue;
1291 return count;
1294 static int aha1542_restart(struct Scsi_Host *shost)
1296 int i;
1297 int count = 0;
1298 #if 0
1299 unchar ahacmd = CMD_START_SCSI;
1300 #endif
1302 for (i = 0; i < AHA1542_MAILBOXES; i++)
1303 if (HOSTDATA(shost)->SCint[i] &&
1304 !(HOSTDATA(shost)->SCint[i]->device->soft_reset)) {
1305 #if 0
1306 HOSTDATA(shost)->mb[i].status = 1; /* Indicate ready to restart... */
1307 #endif
1308 count++;
1310 printk(KERN_DEBUG "Potential to restart %d stalled commands...\n", count);
1311 #if 0
1312 /* start scsi command */
1313 if (count)
1314 aha1542_out(shost->io_port, &ahacmd, 1);
1315 #endif
1316 return 0;
1319 int aha1542_abort(Scsi_Cmnd * SCpnt)
1323 * The abort command does not leave the device in a clean state where
1324 * it is available to be used again. Until this gets worked out, we
1325 * will leave it commented out.
1328 printk(KERN_ERR "aha1542.c: Unable to abort command for target %d\n",
1329 SCpnt->target);
1330 return FAILED;
1334 * This is a device reset. This is handled by sending a special command
1335 * to the device.
1337 int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
1339 unsigned long flags;
1340 struct mailbox *mb;
1341 unchar target = SCpnt->target;
1342 unchar lun = SCpnt->lun;
1343 int mbo;
1344 struct ccb *ccb;
1345 unchar ahacmd = CMD_START_SCSI;
1347 ccb = HOSTDATA(SCpnt->host)->ccb;
1348 mb = HOSTDATA(SCpnt->host)->mb;
1350 save_flags(flags);
1351 cli();
1352 mbo = HOSTDATA(SCpnt->host)->aha1542_last_mbo_used + 1;
1353 if (mbo >= AHA1542_MAILBOXES)
1354 mbo = 0;
1356 do {
1357 if (mb[mbo].status == 0 && HOSTDATA(SCpnt->host)->SCint[mbo] == NULL)
1358 break;
1359 mbo++;
1360 if (mbo >= AHA1542_MAILBOXES)
1361 mbo = 0;
1362 } while (mbo != HOSTDATA(SCpnt->host)->aha1542_last_mbo_used);
1364 if (mb[mbo].status || HOSTDATA(SCpnt->host)->SCint[mbo])
1365 panic("Unable to find empty mailbox for aha1542.\n");
1367 HOSTDATA(SCpnt->host)->SCint[mbo] = SCpnt; /* This will effectively
1368 prevent someone else from
1369 screwing with this cdb. */
1371 HOSTDATA(SCpnt->host)->aha1542_last_mbo_used = mbo;
1372 restore_flags(flags);
1374 any2scsi(mb[mbo].ccbptr, SCSI_PA(&ccb[mbo])); /* This gets trashed for some reason */
1376 memset(&ccb[mbo], 0, sizeof(struct ccb));
1378 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
1380 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
1382 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
1383 ccb[mbo].commlinkid = 0;
1386 * Now tell the 1542 to flush all pending commands for this
1387 * target
1389 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1391 printk(KERN_WARNING "aha1542.c: Trying device reset for target %d\n", SCpnt->target);
1393 return SUCCESS;
1396 #ifdef ERIC_neverdef
1398 * With the 1542 we apparently never get an interrupt to
1399 * acknowledge a device reset being sent. Then again, Leonard
1400 * says we are doing this wrong in the first place...
1402 * Take a wait and see attitude. If we get spurious interrupts,
1403 * then the device reset is doing something sane and useful, and
1404 * we will wait for the interrupt to post completion.
1406 printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1409 * Free the command block for all commands running on this
1410 * target...
1412 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1413 if (HOSTDATA(SCpnt->host)->SCint[i] &&
1414 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1415 Scsi_Cmnd *SCtmp;
1416 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1417 if (SCtmp->host_scribble) {
1418 scsi_free(SCtmp->host_scribble, 512);
1419 SCtmp->host_scribble = NULL;
1421 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1422 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1425 return SUCCESS;
1427 return FAILED;
1428 #endif /* ERIC_neverdef */
1431 int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
1433 int i;
1436 * This does a scsi reset for all devices on the bus.
1437 * In principle, we could also reset the 1542 - should
1438 * we do this? Try this first, and we can add that later
1439 * if it turns out to be useful.
1441 outb(SCRST, CONTROL(SCpnt->host->io_port));
1444 * Wait for the thing to settle down a bit. Unfortunately
1445 * this is going to basically lock up the machine while we
1446 * wait for this to complete. To be 100% correct, we need to
1447 * check for timeout, and if we are doing something like this
1448 * we are pretty desperate anyways.
1450 spin_unlock_irq(&io_request_lock);
1451 scsi_sleep(4 * HZ);
1452 spin_lock_irq(&io_request_lock);
1454 WAIT(STATUS(SCpnt->host->io_port),
1455 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1458 * Now try to pick up the pieces. For all pending commands,
1459 * free any internal data structures, and basically clear things
1460 * out. We do not try and restart any commands or anything -
1461 * the strategy handler takes care of that crap.
1463 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1465 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1466 if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
1467 Scsi_Cmnd *SCtmp;
1468 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1471 if (SCtmp->device->soft_reset) {
1473 * If this device implements the soft reset option,
1474 * then it is still holding onto the command, and
1475 * may yet complete it. In this case, we don't
1476 * flush the data.
1478 continue;
1480 if (SCtmp->host_scribble) {
1481 scsi_free(SCtmp->host_scribble, 512);
1482 SCtmp->host_scribble = NULL;
1484 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1485 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1489 return SUCCESS;
1491 fail:
1492 return FAILED;
1495 int aha1542_host_reset(Scsi_Cmnd * SCpnt)
1497 int i;
1500 * This does a scsi reset for all devices on the bus.
1501 * In principle, we could also reset the 1542 - should
1502 * we do this? Try this first, and we can add that later
1503 * if it turns out to be useful.
1505 outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
1508 * Wait for the thing to settle down a bit. Unfortunately
1509 * this is going to basically lock up the machine while we
1510 * wait for this to complete. To be 100% correct, we need to
1511 * check for timeout, and if we are doing something like this
1512 * we are pretty desperate anyways.
1514 spin_unlock_irq(&io_request_lock);
1515 scsi_sleep(4 * HZ);
1516 spin_lock_irq(&io_request_lock);
1518 WAIT(STATUS(SCpnt->host->io_port),
1519 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1522 * We need to do this too before the 1542 can interact with
1523 * us again.
1525 setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
1528 * Now try to pick up the pieces. For all pending commands,
1529 * free any internal data structures, and basically clear things
1530 * out. We do not try and restart any commands or anything -
1531 * the strategy handler takes care of that crap.
1533 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1535 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1536 if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
1537 Scsi_Cmnd *SCtmp;
1538 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1540 if (SCtmp->device->soft_reset) {
1542 * If this device implements the soft reset option,
1543 * then it is still holding onto the command, and
1544 * may yet complete it. In this case, we don't
1545 * flush the data.
1547 continue;
1549 if (SCtmp->host_scribble) {
1550 scsi_free(SCtmp->host_scribble, 512);
1551 SCtmp->host_scribble = NULL;
1553 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1554 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1558 return SUCCESS;
1560 fail:
1561 return FAILED;
1565 * These are the old error handling routines. They are only temporarily
1566 * here while we play with the new error handling code.
1568 int aha1542_old_abort(Scsi_Cmnd * SCpnt)
1570 #if 0
1571 unchar ahacmd = CMD_START_SCSI;
1572 unsigned long flags;
1573 struct mailbox *mb;
1574 int mbi, mbo, i;
1576 printk(KERN_DEBUG "In aha1542_abort: %x %x\n",
1577 inb(STATUS(SCpnt->host->io_port)),
1578 inb(INTRFLAGS(SCpnt->host->io_port)));
1580 save_flags(flags);
1581 cli();
1582 mb = HOSTDATA(SCpnt->host)->mb;
1583 mbi = HOSTDATA(SCpnt->host)->aha1542_last_mbi_used + 1;
1584 if (mbi >= 2 * AHA1542_MAILBOXES)
1585 mbi = AHA1542_MAILBOXES;
1587 do {
1588 if (mb[mbi].status != 0)
1589 break;
1590 mbi++;
1591 if (mbi >= 2 * AHA1542_MAILBOXES)
1592 mbi = AHA1542_MAILBOXES;
1593 } while (mbi != HOSTDATA(SCpnt->host)->aha1542_last_mbi_used);
1594 restore_flags(flags);
1596 if (mb[mbi].status) {
1597 printk(KERN_ERR "Lost interrupt discovered on irq %d - attempting to recover\n",
1598 SCpnt->host->irq);
1599 aha1542_intr_handle(SCpnt->host->irq, NULL);
1600 return 0;
1602 /* OK, no lost interrupt. Try looking to see how many pending commands
1603 we think we have. */
1605 for (i = 0; i < AHA1542_MAILBOXES; i++)
1606 if (HOSTDATA(SCpnt->host)->SCint[i]) {
1607 if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1608 printk(KERN_ERR "Timed out command pending for %s\n",
1609 kdevname(SCpnt->request.rq_dev));
1610 if (HOSTDATA(SCpnt->host)->mb[i].status) {
1611 printk(KERN_ERR "OGMB still full - restarting\n");
1612 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1614 } else
1615 printk(KERN_ERR "Other pending command %s\n",
1616 kdevname(SCpnt->request.rq_dev));
1618 #endif
1620 DEB(printk("aha1542_abort\n"));
1621 #if 0
1622 save_flags(flags);
1623 cli();
1624 for (mbo = 0; mbo < AHA1542_MAILBOXES; mbo++)
1625 if (SCpnt == HOSTDATA(SCpnt->host)->SCint[mbo]) {
1626 mb[mbo].status = 2; /* Abort command */
1627 aha1542_out(SCpnt->host->io_port, &ahacmd, 1); /* start scsi command */
1628 restore_flags(flags);
1629 break;
1631 #endif
1632 return SCSI_ABORT_SNOOZE;
1635 /* We do not implement a reset function here, but the upper level code
1636 assumes that it will get some kind of response for the command in
1637 SCpnt. We must oblige, or the command will hang the scsi system.
1638 For a first go, we assume that the 1542 notifies us with all of the
1639 pending commands (it does implement soft reset, after all). */
1641 int aha1542_old_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1643 unchar ahacmd = CMD_START_SCSI;
1644 int i;
1647 * See if a bus reset was suggested.
1649 if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
1651 * This does a scsi reset for all devices on the bus.
1652 * In principle, we could also reset the 1542 - should
1653 * we do this? Try this first, and we can add that later
1654 * if it turns out to be useful.
1656 outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
1659 * Wait for the thing to settle down a bit. Unfortunately
1660 * this is going to basically lock up the machine while we
1661 * wait for this to complete. To be 100% correct, we need to
1662 * check for timeout, and if we are doing something like this
1663 * we are pretty desperate anyways.
1665 WAIT(STATUS(SCpnt->host->io_port),
1666 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1669 * We need to do this too before the 1542 can interact with
1670 * us again.
1672 setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
1675 * Now try to pick up the pieces. Restart all commands
1676 * that are currently active on the bus, and reset all of
1677 * the datastructures. We have some time to kill while
1678 * things settle down, so print a nice message.
1680 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1682 for (i = 0; i < AHA1542_MAILBOXES; i++)
1683 if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
1684 Scsi_Cmnd *SCtmp;
1685 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1686 SCtmp->result = DID_RESET << 16;
1687 if (SCtmp->host_scribble) {
1688 scsi_free(SCtmp->host_scribble, 512);
1689 SCtmp->host_scribble = NULL;
1691 printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
1692 SCtmp->scsi_done(SCpnt);
1694 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1695 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1698 * Now tell the mid-level code what we did here. Since
1699 * we have restarted all of the outstanding commands,
1700 * then report SUCCESS.
1702 return (SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET);
1703 fail:
1704 printk(KERN_CRIT "aha1542.c: Unable to perform hard reset.\n");
1705 printk(KERN_CRIT "Power cycle machine to reset\n");
1706 return (SCSI_RESET_ERROR | SCSI_RESET_BUS_RESET);
1709 } else {
1710 /* This does a selective reset of just the one device */
1711 /* First locate the ccb for this command */
1712 for (i = 0; i < AHA1542_MAILBOXES; i++)
1713 if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1714 HOSTDATA(SCpnt->host)->ccb[i].op = 0x81; /* BUS DEVICE RESET */
1715 /* Now tell the 1542 to flush all pending commands for this target */
1716 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1718 /* Here is the tricky part. What to do next. Do we get an interrupt
1719 for the commands that we aborted with the specified target, or
1720 do we generate this on our own? Try it without first and see
1721 what happens */
1722 printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1724 /* If the first does not work, then try the second. I think the
1725 first option is more likely to be correct. Free the command
1726 block for all commands running on this target... */
1727 for (i = 0; i < AHA1542_MAILBOXES; i++)
1728 if (HOSTDATA(SCpnt->host)->SCint[i] &&
1729 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1730 Scsi_Cmnd *SCtmp;
1731 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1732 SCtmp->result = DID_RESET << 16;
1733 if (SCtmp->host_scribble) {
1734 scsi_free(SCtmp->host_scribble, 512);
1735 SCtmp->host_scribble = NULL;
1737 printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
1738 SCtmp->scsi_done(SCpnt);
1740 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1741 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1743 return SCSI_RESET_SUCCESS;
1746 /* No active command at this time, so this means that each time we got
1747 some kind of response the last time through. Tell the mid-level code
1748 to request sense information in order to decide what to do next. */
1749 return SCSI_RESET_PUNT;
1752 #include "sd.h"
1754 int aha1542_biosparam(Scsi_Disk * disk, kdev_t dev, int *ip)
1756 int translation_algorithm;
1757 int size = disk->capacity;
1759 translation_algorithm = HOSTDATA(disk->device->host)->bios_translation;
1761 if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
1762 /* Please verify that this is the same as what DOS returns */
1763 ip[0] = 255;
1764 ip[1] = 63;
1765 ip[2] = size / 255 / 63;
1766 } else {
1767 ip[0] = 64;
1768 ip[1] = 32;
1769 ip[2] = size >> 11;
1772 return 0;
1776 /* Eventually this will go into an include file, but this will be later */
1777 static Scsi_Host_Template driver_template = AHA1542;
1779 #include "scsi_module.c"