- pre4:
[davej-history.git] / drivers / scsi / aha1542.c
blobf4834a51f0a7e4142743f4f42fc5f2ed42c564b7
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 /* For now we do this - until kmalloc is more intelligent
1158 we are resigned to stupid hacks like this */
1159 if (SCSI_PA(shpnt) >= ISA_DMA_THRESHOLD) {
1160 printk(KERN_ERR "Invalid address for shpnt with 1542.\n");
1161 goto unregister;
1163 if (!aha1542_test_port(bases[indx], shpnt))
1164 goto unregister;
1167 base_io = bases[indx];
1169 /* Set the Bus on/off-times as not to ruin floppy performance */
1171 unchar oncmd[] = {CMD_BUSON_TIME, 7};
1172 unchar offcmd[] = {CMD_BUSOFF_TIME, 5};
1174 if (setup_called[indx]) {
1175 oncmd[1] = setup_buson[indx];
1176 offcmd[1] = setup_busoff[indx];
1178 aha1542_intr_reset(base_io);
1179 aha1542_out(base_io, oncmd, 2);
1180 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1181 aha1542_intr_reset(base_io);
1182 aha1542_out(base_io, offcmd, 2);
1183 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1184 if (setup_dmaspeed[indx] >= 0) {
1185 unchar dmacmd[] = {CMD_DMASPEED, 0};
1186 dmacmd[1] = setup_dmaspeed[indx];
1187 aha1542_intr_reset(base_io);
1188 aha1542_out(base_io, dmacmd, 2);
1189 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1191 while (0) {
1192 fail:
1193 printk(KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
1195 aha1542_intr_reset(base_io);
1197 if (aha1542_query(base_io, &trans))
1198 goto unregister;
1200 if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
1201 goto unregister;
1203 printk(KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
1204 if (dma_chan != 0xFF)
1205 printk(", DMA priority %d", dma_chan);
1206 printk("\n");
1208 DEB(aha1542_stat());
1209 setup_mailboxes(base_io, shpnt);
1211 DEB(aha1542_stat());
1213 DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
1214 save_flags(flags);
1215 cli();
1216 if (request_irq(irq_level, do_aha1542_intr_handle, 0, "aha1542", NULL)) {
1217 printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
1218 restore_flags(flags);
1219 goto unregister;
1221 if (dma_chan != 0xFF) {
1222 if (request_dma(dma_chan, "aha1542")) {
1223 printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
1224 free_irq(irq_level, NULL);
1225 restore_flags(flags);
1226 goto unregister;
1228 if (dma_chan == 0 || dma_chan >= 5) {
1229 set_dma_mode(dma_chan, DMA_MODE_CASCADE);
1230 enable_dma(dma_chan);
1233 aha_host[irq_level - 9] = shpnt;
1234 shpnt->this_id = scsi_id;
1235 shpnt->unique_id = base_io;
1236 shpnt->io_port = base_io;
1237 shpnt->n_io_port = 4; /* Number of bytes of I/O space used */
1238 shpnt->dma_channel = dma_chan;
1239 shpnt->irq = irq_level;
1240 HOSTDATA(shpnt)->bios_translation = trans;
1241 if (trans == BIOS_TRANSLATION_25563)
1242 printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
1243 HOSTDATA(shpnt)->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
1244 HOSTDATA(shpnt)->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
1245 memset(HOSTDATA(shpnt)->SCint, 0, sizeof(HOSTDATA(shpnt)->SCint));
1246 restore_flags(flags);
1247 #if 0
1248 DEB(printk(" *** READ CAPACITY ***\n"));
1251 unchar buf[8];
1252 static unchar cmd[] = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1253 int i;
1255 for (i = 0; i < sizeof(buf); ++i)
1256 buf[i] = 0x87;
1257 for (i = 0; i < 2; ++i)
1258 if (!aha1542_command(i, cmd, buf, sizeof(buf))) {
1259 printk(KERN_DEBUG "aha_detect: LU %d sector_size %d device_size %d\n",
1260 i, xscsi2int(buf + 4), xscsi2int(buf));
1264 DEB(printk(" *** NOW RUNNING MY OWN TEST *** \n"));
1266 for (i = 0; i < 4; ++i) {
1267 unsigned char cmd[10];
1268 static buffer[512];
1270 cmd[0] = READ_10;
1271 cmd[1] = 0;
1272 xany2scsi(cmd + 2, i);
1273 cmd[6] = 0;
1274 cmd[7] = 0;
1275 cmd[8] = 1;
1276 cmd[9] = 0;
1277 aha1542_command(0, cmd, buffer, 512);
1279 #endif
1280 request_region(bases[indx], 4, "aha1542"); /* Register the IO ports that we use */
1281 count++;
1282 continue;
1283 unregister:
1284 scsi_unregister(shpnt);
1285 continue;
1289 return count;
1292 static int aha1542_restart(struct Scsi_Host *shost)
1294 int i;
1295 int count = 0;
1296 #if 0
1297 unchar ahacmd = CMD_START_SCSI;
1298 #endif
1300 for (i = 0; i < AHA1542_MAILBOXES; i++)
1301 if (HOSTDATA(shost)->SCint[i] &&
1302 !(HOSTDATA(shost)->SCint[i]->device->soft_reset)) {
1303 #if 0
1304 HOSTDATA(shost)->mb[i].status = 1; /* Indicate ready to restart... */
1305 #endif
1306 count++;
1308 printk(KERN_DEBUG "Potential to restart %d stalled commands...\n", count);
1309 #if 0
1310 /* start scsi command */
1311 if (count)
1312 aha1542_out(shost->io_port, &ahacmd, 1);
1313 #endif
1314 return 0;
1317 int aha1542_abort(Scsi_Cmnd * SCpnt)
1321 * The abort command does not leave the device in a clean state where
1322 * it is available to be used again. Until this gets worked out, we
1323 * will leave it commented out.
1326 printk(KERN_ERR "aha1542.c: Unable to abort command for target %d\n",
1327 SCpnt->target);
1328 return FAILED;
1332 * This is a device reset. This is handled by sending a special command
1333 * to the device.
1335 int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
1337 unsigned long flags;
1338 struct mailbox *mb;
1339 unchar target = SCpnt->target;
1340 unchar lun = SCpnt->lun;
1341 int mbo;
1342 struct ccb *ccb;
1343 unchar ahacmd = CMD_START_SCSI;
1345 ccb = HOSTDATA(SCpnt->host)->ccb;
1346 mb = HOSTDATA(SCpnt->host)->mb;
1348 save_flags(flags);
1349 cli();
1350 mbo = HOSTDATA(SCpnt->host)->aha1542_last_mbo_used + 1;
1351 if (mbo >= AHA1542_MAILBOXES)
1352 mbo = 0;
1354 do {
1355 if (mb[mbo].status == 0 && HOSTDATA(SCpnt->host)->SCint[mbo] == NULL)
1356 break;
1357 mbo++;
1358 if (mbo >= AHA1542_MAILBOXES)
1359 mbo = 0;
1360 } while (mbo != HOSTDATA(SCpnt->host)->aha1542_last_mbo_used);
1362 if (mb[mbo].status || HOSTDATA(SCpnt->host)->SCint[mbo])
1363 panic("Unable to find empty mailbox for aha1542.\n");
1365 HOSTDATA(SCpnt->host)->SCint[mbo] = SCpnt; /* This will effectively
1366 prevent someone else from
1367 screwing with this cdb. */
1369 HOSTDATA(SCpnt->host)->aha1542_last_mbo_used = mbo;
1370 restore_flags(flags);
1372 any2scsi(mb[mbo].ccbptr, SCSI_PA(&ccb[mbo])); /* This gets trashed for some reason */
1374 memset(&ccb[mbo], 0, sizeof(struct ccb));
1376 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
1378 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
1380 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
1381 ccb[mbo].commlinkid = 0;
1384 * Now tell the 1542 to flush all pending commands for this
1385 * target
1387 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1389 printk(KERN_WARNING "aha1542.c: Trying device reset for target %d\n", SCpnt->target);
1391 return SUCCESS;
1394 #ifdef ERIC_neverdef
1396 * With the 1542 we apparently never get an interrupt to
1397 * acknowledge a device reset being sent. Then again, Leonard
1398 * says we are doing this wrong in the first place...
1400 * Take a wait and see attitude. If we get spurious interrupts,
1401 * then the device reset is doing something sane and useful, and
1402 * we will wait for the interrupt to post completion.
1404 printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1407 * Free the command block for all commands running on this
1408 * target...
1410 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1411 if (HOSTDATA(SCpnt->host)->SCint[i] &&
1412 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1413 Scsi_Cmnd *SCtmp;
1414 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1415 if (SCtmp->host_scribble) {
1416 scsi_free(SCtmp->host_scribble, 512);
1418 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1419 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1422 return SUCCESS;
1424 return FAILED;
1425 #endif /* ERIC_neverdef */
1428 int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
1430 int i;
1433 * This does a scsi reset for all devices on the bus.
1434 * In principle, we could also reset the 1542 - should
1435 * we do this? Try this first, and we can add that later
1436 * if it turns out to be useful.
1438 outb(SCRST, CONTROL(SCpnt->host->io_port));
1441 * Wait for the thing to settle down a bit. Unfortunately
1442 * this is going to basically lock up the machine while we
1443 * wait for this to complete. To be 100% correct, we need to
1444 * check for timeout, and if we are doing something like this
1445 * we are pretty desperate anyways.
1447 spin_unlock_irq(&io_request_lock);
1448 scsi_sleep(4 * HZ);
1449 spin_lock_irq(&io_request_lock);
1451 WAIT(STATUS(SCpnt->host->io_port),
1452 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1455 * Now try to pick up the pieces. For all pending commands,
1456 * free any internal data structures, and basically clear things
1457 * out. We do not try and restart any commands or anything -
1458 * the strategy handler takes care of that crap.
1460 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1462 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1463 if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
1464 Scsi_Cmnd *SCtmp;
1465 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1468 if (SCtmp->device->soft_reset) {
1470 * If this device implements the soft reset option,
1471 * then it is still holding onto the command, and
1472 * may yet complete it. In this case, we don't
1473 * flush the data.
1475 continue;
1477 if (SCtmp->host_scribble) {
1478 scsi_free(SCtmp->host_scribble, 512);
1480 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1481 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1485 return SUCCESS;
1487 fail:
1488 return FAILED;
1491 int aha1542_host_reset(Scsi_Cmnd * SCpnt)
1493 int i;
1496 * This does a scsi reset for all devices on the bus.
1497 * In principle, we could also reset the 1542 - should
1498 * we do this? Try this first, and we can add that later
1499 * if it turns out to be useful.
1501 outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
1504 * Wait for the thing to settle down a bit. Unfortunately
1505 * this is going to basically lock up the machine while we
1506 * wait for this to complete. To be 100% correct, we need to
1507 * check for timeout, and if we are doing something like this
1508 * we are pretty desperate anyways.
1510 spin_unlock_irq(&io_request_lock);
1511 scsi_sleep(4 * HZ);
1512 spin_lock_irq(&io_request_lock);
1514 WAIT(STATUS(SCpnt->host->io_port),
1515 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1518 * We need to do this too before the 1542 can interact with
1519 * us again.
1521 setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
1524 * Now try to pick up the pieces. For all pending commands,
1525 * free any internal data structures, and basically clear things
1526 * out. We do not try and restart any commands or anything -
1527 * the strategy handler takes care of that crap.
1529 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1531 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1532 if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
1533 Scsi_Cmnd *SCtmp;
1534 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1536 if (SCtmp->device->soft_reset) {
1538 * If this device implements the soft reset option,
1539 * then it is still holding onto the command, and
1540 * may yet complete it. In this case, we don't
1541 * flush the data.
1543 continue;
1545 if (SCtmp->host_scribble) {
1546 scsi_free(SCtmp->host_scribble, 512);
1548 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1549 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1553 return SUCCESS;
1555 fail:
1556 return FAILED;
1560 * These are the old error handling routines. They are only temporarily
1561 * here while we play with the new error handling code.
1563 int aha1542_old_abort(Scsi_Cmnd * SCpnt)
1565 #if 0
1566 unchar ahacmd = CMD_START_SCSI;
1567 unsigned long flags;
1568 struct mailbox *mb;
1569 int mbi, mbo, i;
1571 printk(KERN_DEBUG "In aha1542_abort: %x %x\n",
1572 inb(STATUS(SCpnt->host->io_port)),
1573 inb(INTRFLAGS(SCpnt->host->io_port)));
1575 save_flags(flags);
1576 cli();
1577 mb = HOSTDATA(SCpnt->host)->mb;
1578 mbi = HOSTDATA(SCpnt->host)->aha1542_last_mbi_used + 1;
1579 if (mbi >= 2 * AHA1542_MAILBOXES)
1580 mbi = AHA1542_MAILBOXES;
1582 do {
1583 if (mb[mbi].status != 0)
1584 break;
1585 mbi++;
1586 if (mbi >= 2 * AHA1542_MAILBOXES)
1587 mbi = AHA1542_MAILBOXES;
1588 } while (mbi != HOSTDATA(SCpnt->host)->aha1542_last_mbi_used);
1589 restore_flags(flags);
1591 if (mb[mbi].status) {
1592 printk(KERN_ERR "Lost interrupt discovered on irq %d - attempting to recover\n",
1593 SCpnt->host->irq);
1594 aha1542_intr_handle(SCpnt->host->irq, NULL);
1595 return 0;
1597 /* OK, no lost interrupt. Try looking to see how many pending commands
1598 we think we have. */
1600 for (i = 0; i < AHA1542_MAILBOXES; i++)
1601 if (HOSTDATA(SCpnt->host)->SCint[i]) {
1602 if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1603 printk(KERN_ERR "Timed out command pending for %s\n",
1604 kdevname(SCpnt->request.rq_dev));
1605 if (HOSTDATA(SCpnt->host)->mb[i].status) {
1606 printk(KERN_ERR "OGMB still full - restarting\n");
1607 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1609 } else
1610 printk(KERN_ERR "Other pending command %s\n",
1611 kdevname(SCpnt->request.rq_dev));
1613 #endif
1615 DEB(printk("aha1542_abort\n"));
1616 #if 0
1617 save_flags(flags);
1618 cli();
1619 for (mbo = 0; mbo < AHA1542_MAILBOXES; mbo++)
1620 if (SCpnt == HOSTDATA(SCpnt->host)->SCint[mbo]) {
1621 mb[mbo].status = 2; /* Abort command */
1622 aha1542_out(SCpnt->host->io_port, &ahacmd, 1); /* start scsi command */
1623 restore_flags(flags);
1624 break;
1626 #endif
1627 return SCSI_ABORT_SNOOZE;
1630 /* We do not implement a reset function here, but the upper level code
1631 assumes that it will get some kind of response for the command in
1632 SCpnt. We must oblige, or the command will hang the scsi system.
1633 For a first go, we assume that the 1542 notifies us with all of the
1634 pending commands (it does implement soft reset, after all). */
1636 int aha1542_old_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1638 unchar ahacmd = CMD_START_SCSI;
1639 int i;
1642 * See if a bus reset was suggested.
1644 if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
1646 * This does a scsi reset for all devices on the bus.
1647 * In principle, we could also reset the 1542 - should
1648 * we do this? Try this first, and we can add that later
1649 * if it turns out to be useful.
1651 outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
1654 * Wait for the thing to settle down a bit. Unfortunately
1655 * this is going to basically lock up the machine while we
1656 * wait for this to complete. To be 100% correct, we need to
1657 * check for timeout, and if we are doing something like this
1658 * we are pretty desperate anyways.
1660 WAIT(STATUS(SCpnt->host->io_port),
1661 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1664 * We need to do this too before the 1542 can interact with
1665 * us again.
1667 setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
1670 * Now try to pick up the pieces. Restart all commands
1671 * that are currently active on the bus, and reset all of
1672 * the datastructures. We have some time to kill while
1673 * things settle down, so print a nice message.
1675 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1677 for (i = 0; i < AHA1542_MAILBOXES; i++)
1678 if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
1679 Scsi_Cmnd *SCtmp;
1680 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1681 SCtmp->result = DID_RESET << 16;
1682 if (SCtmp->host_scribble)
1683 scsi_free(SCtmp->host_scribble, 512);
1684 printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
1685 SCtmp->scsi_done(SCpnt);
1687 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1688 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1691 * Now tell the mid-level code what we did here. Since
1692 * we have restarted all of the outstanding commands,
1693 * then report SUCCESS.
1695 return (SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET);
1696 fail:
1697 printk(KERN_CRIT "aha1542.c: Unable to perform hard reset.\n");
1698 printk(KERN_CRIT "Power cycle machine to reset\n");
1699 return (SCSI_RESET_ERROR | SCSI_RESET_BUS_RESET);
1702 } else {
1703 /* This does a selective reset of just the one device */
1704 /* First locate the ccb for this command */
1705 for (i = 0; i < AHA1542_MAILBOXES; i++)
1706 if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1707 HOSTDATA(SCpnt->host)->ccb[i].op = 0x81; /* BUS DEVICE RESET */
1708 /* Now tell the 1542 to flush all pending commands for this target */
1709 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1711 /* Here is the tricky part. What to do next. Do we get an interrupt
1712 for the commands that we aborted with the specified target, or
1713 do we generate this on our own? Try it without first and see
1714 what happens */
1715 printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1717 /* If the first does not work, then try the second. I think the
1718 first option is more likely to be correct. Free the command
1719 block for all commands running on this target... */
1720 for (i = 0; i < AHA1542_MAILBOXES; i++)
1721 if (HOSTDATA(SCpnt->host)->SCint[i] &&
1722 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1723 Scsi_Cmnd *SCtmp;
1724 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1725 SCtmp->result = DID_RESET << 16;
1726 if (SCtmp->host_scribble)
1727 scsi_free(SCtmp->host_scribble, 512);
1728 printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
1729 SCtmp->scsi_done(SCpnt);
1731 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1732 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1734 return SCSI_RESET_SUCCESS;
1737 /* No active command at this time, so this means that each time we got
1738 some kind of response the last time through. Tell the mid-level code
1739 to request sense information in order to decide what to do next. */
1740 return SCSI_RESET_PUNT;
1743 #include "sd.h"
1745 int aha1542_biosparam(Scsi_Disk * disk, kdev_t dev, int *ip)
1747 int translation_algorithm;
1748 int size = disk->capacity;
1750 translation_algorithm = HOSTDATA(disk->device->host)->bios_translation;
1752 if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
1753 /* Please verify that this is the same as what DOS returns */
1754 ip[0] = 255;
1755 ip[1] = 63;
1756 ip[2] = size / 255 / 63;
1757 } else {
1758 ip[0] = 64;
1759 ip[1] = 32;
1760 ip[2] = size >> 11;
1763 return 0;
1767 #ifdef MODULE
1768 /* Eventually this will go into an include file, but this will be later */
1769 Scsi_Host_Template driver_template = AHA1542;
1771 #include "scsi_module.c"
1772 #endif