Import 2.2.8pre2
[davej-history.git] / drivers / scsi / aha1542.c
blob94df0cb6ce495c0e68cdadc24aa6ea60919eba7f
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
23 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/ioport.h>
29 #include <linux/delay.h>
30 #include <linux/sched.h>
31 #include <linux/proc_fs.h>
32 #include <linux/init.h>
33 #include <asm/dma.h>
34 #include <asm/system.h>
35 #include <asm/spinlock.h>
36 #include <asm/io.h>
37 #include <linux/blk.h>
38 #include "scsi.h"
39 #include "hosts.h"
42 #include "aha1542.h"
44 #define SCSI_PA(address) virt_to_bus(address)
46 #define BAD_DMA(msg, address, length) \
47 { \
48 printk(KERN_CRIT "%s address %p length %d\n", msg, address, length); \
49 panic("Buffer at physical address > 16Mb used for aha1542"); \
52 #include<linux/stat.h>
54 struct proc_dir_entry proc_scsi_aha1542 = {
55 PROC_SCSI_AHA1542, 7, "aha1542",
56 S_IFDIR | S_IRUGO | S_IXUGO, 2
59 #ifdef DEBUG
60 #define DEB(x) x
61 #else
62 #define DEB(x)
63 #endif
65 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 $";
68 /* The adaptec can be configured for quite a number of addresses, but
69 I generally do not want the card poking around at random. We allow
70 two addresses - this allows people to use the Adaptec with a Midi
71 card, which also used 0x330 -- can be overridden with LILO! */
73 #define MAXBOARDS 2 /* Increase this and the sizes of the
74 arrays below, if you need more.. */
76 static unsigned int bases[MAXBOARDS]={0x330, 0x334};
78 /* set by aha1542_setup according to the command line */
79 static int setup_called[MAXBOARDS] = {0,0};
80 static int setup_buson[MAXBOARDS] = {0,0};
81 static int setup_busoff[MAXBOARDS] = {0,0};
82 static int setup_dmaspeed[MAXBOARDS] = {-1,-1};
84 static char *setup_str[MAXBOARDS] = {(char *)NULL,(char *)NULL};
87 * LILO params: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]
89 * Where: <PORTBASE> is any of the valid AHA addresses:
90 * 0x130, 0x134, 0x230, 0x234, 0x330, 0x334
91 * <BUSON> is the time (in microsecs) that AHA spends on the AT-bus
92 * when transferring data. 1542A power-on default is 11us,
93 * valid values are in range: 2..15 (decimal)
94 * <BUSOFF> is the time that AHA spends OFF THE BUS after while
95 * it is transferring data (not to monopolize the bus).
96 * Power-on default is 4us, valid range: 1..64 microseconds.
97 * <DMASPEED> Default is jumper selected (1542A: on the J1),
98 * but experimenter can alter it with this.
99 * Valid values: 5, 6, 7, 8, 10 (MB/s)
100 * Factory default is 5 MB/s.
103 #define BIOS_TRANSLATION_1632 0 /* Used by some old 1542A boards */
104 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
105 #define BIOS_TRANSLATION_25563 2 /* Big disk case */
107 struct aha1542_hostdata{
108 /* This will effectively start both of them at the first mailbox */
109 int bios_translation; /* Mapping bios uses - for compatibility */
110 int aha1542_last_mbi_used;
111 int aha1542_last_mbo_used;
112 Scsi_Cmnd * SCint[AHA1542_MAILBOXES];
113 struct mailbox mb[2*AHA1542_MAILBOXES];
114 struct ccb ccb[AHA1542_MAILBOXES];
117 #define HOSTDATA(host) ((struct aha1542_hostdata *) &host->hostdata)
119 static struct Scsi_Host * aha_host[7] = {NULL,}; /* One for each IRQ level (9-15) */
124 #define WAITnexttimeout 3000000
126 static void setup_mailboxes(int base_io, struct Scsi_Host * shpnt);
127 static int aha1542_restart(struct Scsi_Host * shost);
128 static void aha1542_intr_handle(int irq, void *dev_id, struct pt_regs *regs);
129 static void do_aha1542_intr_handle(int irq, void *dev_id, struct pt_regs *regs);
131 #define aha1542_intr_reset(base) outb(IRST, CONTROL(base))
133 #define WAIT(port, mask, allof, noneof) \
134 { register int WAITbits; \
135 register int WAITtimeout = WAITnexttimeout; \
136 while (1) { \
137 WAITbits = inb(port) & (mask); \
138 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
139 break; \
140 if (--WAITtimeout == 0) goto fail; \
144 /* Similar to WAIT, except we use the udelay call to regulate the
145 amount of time we wait. */
146 #define WAITd(port, mask, allof, noneof, timeout) \
147 { register int WAITbits; \
148 register int WAITtimeout = timeout; \
149 while (1) { \
150 WAITbits = inb(port) & (mask); \
151 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
152 break; \
153 mdelay(1); \
154 if (--WAITtimeout == 0) goto fail; \
158 static void aha1542_stat(void)
160 /* int s = inb(STATUS), i = inb(INTRFLAGS);
161 printk("status=%x intrflags=%x\n", s, i, WAITnexttimeout-WAITtimeout); */
164 /* This is a bit complicated, but we need to make sure that an interrupt
165 routine does not send something out while we are in the middle of this.
166 Fortunately, it is only at boot time that multi-byte messages
167 are ever sent. */
168 static int aha1542_out(unsigned int base, unchar *cmdp, int len)
170 unsigned long flags = 0;
172 save_flags(flags);
173 if(len == 1) {
174 while(1==1){
175 WAIT(STATUS(base), CDF, 0, CDF);
176 cli();
177 if(inb(STATUS(base)) & CDF) {restore_flags(flags); continue;}
178 outb(*cmdp, DATA(base));
179 restore_flags(flags);
180 return 0;
182 } else {
183 cli();
184 while (len--)
186 WAIT(STATUS(base), CDF, 0, CDF);
187 outb(*cmdp++, DATA(base));
189 restore_flags(flags);
191 return 0;
192 fail:
193 restore_flags(flags);
194 printk("aha1542_out failed(%d): ", len+1); aha1542_stat();
195 return 1;
198 /* Only used at boot time, so we do not need to worry about latency as much
199 here */
200 static int aha1542_in(unsigned int base, unchar *cmdp, int len)
202 unsigned long flags;
204 save_flags(flags);
205 cli();
206 while (len--)
208 WAIT(STATUS(base), DF, DF, 0);
209 *cmdp++ = inb(DATA(base));
211 restore_flags(flags);
212 return 0;
213 fail:
214 restore_flags(flags);
215 printk("aha1542_in failed(%d): ", len+1); aha1542_stat();
216 return 1;
219 /* Similar to aha1542_in, except that we wait a very short period of time.
220 We use this if we know the board is alive and awake, but we are not sure
221 if the board will respond to the command we are about to send or not */
222 static int aha1542_in1(unsigned int base, unchar *cmdp, int len)
224 unsigned long flags;
226 save_flags(flags);
227 cli();
228 while (len--)
230 WAITd(STATUS(base), DF, DF, 0, 100);
231 *cmdp++ = inb(DATA(base));
233 restore_flags(flags);
234 return 0;
235 fail:
236 restore_flags(flags);
237 return 1;
240 static int makecode(unsigned hosterr, unsigned scsierr)
242 switch (hosterr) {
243 case 0x0:
244 case 0xa: /* Linked command complete without error and linked normally */
245 case 0xb: /* Linked command complete without error, interrupt generated */
246 hosterr = 0;
247 break;
249 case 0x11: /* Selection time out-The initiator selection or target
250 reselection was not complete within the SCSI Time out period */
251 hosterr = DID_TIME_OUT;
252 break;
254 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
255 than was allocated by the Data Length field or the sum of the
256 Scatter / Gather Data Length fields. */
258 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
260 case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
261 invalid. This usually indicates a software failure. */
263 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
264 This usually indicates a software failure. */
266 case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
267 of linked CCB's does not specify the same logical unit number as
268 the first. */
269 case 0x18: /* Invalid Target Direction received from Host-The direction of a
270 Target Mode CCB was invalid. */
272 case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
273 received to service data transfer between the same target LUN
274 and initiator SCSI ID in the same direction. */
276 case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
277 length segment or invalid segment list boundaries was received.
278 A CCB parameter was invalid. */
279 DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
280 hosterr = DID_ERROR; /* Couldn't find any better */
281 break;
283 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
284 phase sequence was requested by the target. The host adapter
285 will generate a SCSI Reset Condition, notifying the host with
286 a SCRD interrupt */
287 hosterr = DID_RESET;
288 break;
289 default:
290 printk("makecode: unknown hoststatus %x\n", hosterr);
291 break;
293 return scsierr|(hosterr << 16);
296 static int aha1542_test_port(int bse, struct Scsi_Host * shpnt)
298 unchar inquiry_cmd[] = {CMD_INQUIRY };
299 unchar inquiry_result[4];
300 unchar *cmdp;
301 int len;
302 volatile int debug = 0;
304 /* Quick and dirty test for presence of the card. */
305 if(inb(STATUS(bse)) == 0xff) return 0;
307 /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
309 /* DEB(printk("aha1542_test_port called \n")); */
311 /* In case some other card was probing here, reset interrupts */
312 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
314 outb(SRST|IRST/*|SCRST*/, CONTROL(bse));
316 mdelay(20); /* Wait a little bit for things to settle down. */
318 debug = 1;
319 /* Expect INIT and IDLE, any of the others are bad */
320 WAIT(STATUS(bse), STATMASK, INIT|IDLE, STST|DIAGF|INVDCMD|DF|CDF);
322 debug = 2;
323 /* Shouldn't have generated any interrupts during reset */
324 if (inb(INTRFLAGS(bse))&INTRMASK) goto fail;
327 /* Perform a host adapter inquiry instead so we do not need to set
328 up the mailboxes ahead of time */
330 aha1542_out(bse, inquiry_cmd, 1);
332 debug = 3;
333 len = 4;
334 cmdp = &inquiry_result[0];
336 while (len--)
338 WAIT(STATUS(bse), DF, DF, 0);
339 *cmdp++ = inb(DATA(bse));
342 debug = 8;
343 /* Reading port should reset DF */
344 if (inb(STATUS(bse)) & DF) goto fail;
346 debug = 9;
347 /* When HACC, command is completed, and we're though testing */
348 WAIT(INTRFLAGS(bse), HACC, HACC, 0);
349 /* now initialize adapter */
351 debug = 10;
352 /* Clear interrupts */
353 outb(IRST, CONTROL(bse));
355 debug = 11;
357 return debug; /* 1 = ok */
358 fail:
359 return 0; /* 0 = not ok */
362 /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
363 static void do_aha1542_intr_handle(int irq, void *dev_id, struct pt_regs *regs)
365 unsigned long flags;
367 spin_lock_irqsave(&io_request_lock, flags);
368 aha1542_intr_handle(irq, dev_id, regs);
369 spin_unlock_irqrestore(&io_request_lock, flags);
372 /* A "high" level interrupt handler */
373 static void aha1542_intr_handle(int irq, void *dev_id, struct pt_regs *regs)
375 void (*my_done)(Scsi_Cmnd *) = NULL;
376 int errstatus, mbi, mbo, mbistatus;
377 int number_serviced;
378 unsigned int flags;
379 struct Scsi_Host * shost;
380 Scsi_Cmnd * SCtmp;
381 int flag;
382 int needs_restart;
383 struct mailbox * mb;
384 struct ccb *ccb;
386 shost = aha_host[irq - 9];
387 if(!shost) panic("Splunge!");
389 mb = HOSTDATA(shost)->mb;
390 ccb = HOSTDATA(shost)->ccb;
392 #ifdef DEBUG
394 flag = inb(INTRFLAGS(shost->io_port));
395 printk("aha1542_intr_handle: ");
396 if (!(flag&ANYINTR)) printk("no interrupt?");
397 if (flag&MBIF) printk("MBIF ");
398 if (flag&MBOA) printk("MBOF ");
399 if (flag&HACC) printk("HACC ");
400 if (flag&SCRD) printk("SCRD ");
401 printk("status %02x\n", inb(STATUS(shost->io_port)));
403 #endif
404 number_serviced = 0;
405 needs_restart = 0;
407 while(1==1){
408 flag = inb(INTRFLAGS(shost->io_port));
410 /* Check for unusual interrupts. If any of these happen, we should
411 probably do something special, but for now just printing a message
412 is sufficient. A SCSI reset detected is something that we really
413 need to deal with in some way. */
414 if (flag & ~MBIF) {
415 if (flag&MBOA) printk("MBOF ");
416 if (flag&HACC) printk("HACC ");
417 if (flag&SCRD) {
418 needs_restart = 1;
419 printk("SCRD ");
423 aha1542_intr_reset(shost->io_port);
425 save_flags(flags);
426 cli();
427 mbi = HOSTDATA(shost)->aha1542_last_mbi_used + 1;
428 if (mbi >= 2*AHA1542_MAILBOXES) mbi = AHA1542_MAILBOXES;
431 if(mb[mbi].status != 0) break;
432 mbi++;
433 if (mbi >= 2*AHA1542_MAILBOXES) mbi = AHA1542_MAILBOXES;
434 } while (mbi != HOSTDATA(shost)->aha1542_last_mbi_used);
436 if(mb[mbi].status == 0){
437 restore_flags(flags);
438 /* Hmm, no mail. Must have read it the last time around */
439 if (!number_serviced && !needs_restart)
440 printk("aha1542.c: interrupt received, but no mail.\n");
441 /* We detected a reset. Restart all pending commands for
442 devices that use the hard reset option */
443 if(needs_restart) aha1542_restart(shost);
444 return;
447 mbo = (scsi2int(mb[mbi].ccbptr) - (SCSI_PA(&ccb[0]))) / sizeof(struct ccb);
448 mbistatus = mb[mbi].status;
449 mb[mbi].status = 0;
450 HOSTDATA(shost)->aha1542_last_mbi_used = mbi;
451 restore_flags(flags);
453 #ifdef DEBUG
455 if (ccb[mbo].tarstat|ccb[mbo].hastat)
456 printk("aha1542_command: returning %x (status %d)\n",
457 ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
459 #endif
461 if(mbistatus == 3) continue; /* Aborted command not found */
463 #ifdef DEBUG
464 printk("...done %d %d\n",mbo, mbi);
465 #endif
467 SCtmp = HOSTDATA(shost)->SCint[mbo];
469 if (!SCtmp || !SCtmp->scsi_done) {
470 printk("aha1542_intr_handle: Unexpected interrupt\n");
471 printk("tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
472 ccb[mbo].hastat, ccb[mbo].idlun, mbo);
473 return;
476 my_done = SCtmp->scsi_done;
477 if (SCtmp->host_scribble) scsi_free(SCtmp->host_scribble, 512);
479 /* Fetch the sense data, and tuck it away, in the required slot. The
480 Adaptec automatically fetches it, and there is no guarantee that
481 we will still have it in the cdb when we come back */
482 if (ccb[mbo].tarstat == 2)
483 memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
484 sizeof(SCtmp->sense_buffer));
487 /* is there mail :-) */
489 /* more error checking left out here */
490 if (mbistatus != 1)
491 /* This is surely wrong, but I don't know what's right */
492 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
493 else
494 errstatus = 0;
496 #ifdef DEBUG
497 if(errstatus) printk("(aha1542 error:%x %x %x) ",errstatus,
498 ccb[mbo].hastat, ccb[mbo].tarstat);
499 #endif
501 if (ccb[mbo].tarstat == 2) {
502 #ifdef DEBUG
503 int i;
504 #endif
505 DEB(printk("aha1542_intr_handle: sense:"));
506 #ifdef DEBUG
507 for (i = 0; i < 12; i++)
508 printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen+i]);
509 printk("\n");
510 #endif
512 DEB(printk("aha1542_intr_handle: buf:"));
513 for (i = 0; i < bufflen; i++)
514 printk("%02x ", ((unchar *)buff)[i]);
515 printk("\n");
518 DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
519 SCtmp->result = errstatus;
520 HOSTDATA(shost)->SCint[mbo] = NULL; /* This effectively frees up the mailbox slot, as
521 far as queuecommand is concerned */
522 my_done(SCtmp);
523 number_serviced++;
527 int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
529 unchar ahacmd = CMD_START_SCSI;
530 unchar direction;
531 unchar *cmd = (unchar *) SCpnt->cmnd;
532 unchar target = SCpnt->target;
533 unchar lun = SCpnt->lun;
534 unsigned long flags;
535 void *buff = SCpnt->request_buffer;
536 int bufflen = SCpnt->request_bufflen;
537 int mbo;
538 struct mailbox * mb;
539 struct ccb *ccb;
541 DEB(int i);
543 mb = HOSTDATA(SCpnt->host)->mb;
544 ccb = HOSTDATA(SCpnt->host)->ccb;
546 DEB(if (target > 1) {
547 SCpnt->result = DID_TIME_OUT << 16;
548 done(SCpnt); return 0;});
550 if(*cmd == REQUEST_SENSE){
551 #ifndef DEBUG
552 if (bufflen != sizeof(SCpnt->sense_buffer)) {
553 printk("Wrong buffer length supplied for request sense (%d)\n",bufflen);
555 #endif
556 SCpnt->result = 0;
557 done(SCpnt);
558 return 0;
561 #ifdef DEBUG
562 if (*cmd == READ_10 || *cmd == WRITE_10)
563 i = xscsi2int(cmd+2);
564 else if (*cmd == READ_6 || *cmd == WRITE_6)
565 i = scsi2int(cmd+2);
566 else
567 i = -1;
568 if (done)
569 printk("aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
570 else
571 printk("aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
572 aha1542_stat();
573 printk("aha1542_queuecommand: dumping scsi cmd:");
574 for (i = 0; i < SCpnt->cmd_len; i++) printk("%02x ", cmd[i]);
575 printk("\n");
576 if (*cmd == WRITE_10 || *cmd == WRITE_6)
577 return 0; /* we are still testing, so *don't* write */
578 #endif
579 /* Use the outgoing mailboxes in a round-robin fashion, because this
580 is how the host adapter will scan for them */
582 save_flags(flags);
583 cli();
584 mbo = HOSTDATA(SCpnt->host)->aha1542_last_mbo_used + 1;
585 if (mbo >= AHA1542_MAILBOXES) mbo = 0;
588 if(mb[mbo].status == 0 && HOSTDATA(SCpnt->host)->SCint[mbo] == NULL)
589 break;
590 mbo++;
591 if (mbo >= AHA1542_MAILBOXES) mbo = 0;
592 } while (mbo != HOSTDATA(SCpnt->host)->aha1542_last_mbo_used);
594 if(mb[mbo].status || HOSTDATA(SCpnt->host)->SCint[mbo])
595 panic("Unable to find empty mailbox for aha1542.\n");
597 HOSTDATA(SCpnt->host)->SCint[mbo] = SCpnt; /* This will effectively prevent someone else from
598 screwing with this cdb. */
600 HOSTDATA(SCpnt->host)->aha1542_last_mbo_used = mbo;
601 restore_flags(flags);
603 #ifdef DEBUG
604 printk("Sending command (%d %x)...",mbo, done);
605 #endif
607 any2scsi(mb[mbo].ccbptr, SCSI_PA(&ccb[mbo])); /* This gets trashed for some reason*/
609 memset(&ccb[mbo], 0, sizeof(struct ccb));
611 ccb[mbo].cdblen = SCpnt->cmd_len;
613 direction = 0;
614 if (*cmd == READ_10 || *cmd == READ_6)
615 direction = 8;
616 else if (*cmd == WRITE_10 || *cmd == WRITE_6)
617 direction = 16;
619 memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
621 if (SCpnt->use_sg) {
622 struct scatterlist * sgpnt;
623 struct chain * cptr;
624 #ifdef DEBUG
625 unsigned char * ptr;
626 #endif
627 int i;
628 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather*/
629 SCpnt->host_scribble = (unsigned char *) scsi_malloc(512);
630 sgpnt = (struct scatterlist *) SCpnt->request_buffer;
631 cptr = (struct chain *) SCpnt->host_scribble;
632 if (cptr == NULL) panic("aha1542.c: unable to allocate DMA memory\n");
633 for(i=0; i<SCpnt->use_sg; i++) {
634 if(sgpnt[i].length == 0 || SCpnt->use_sg > 16 ||
635 (((int)sgpnt[i].address) & 1) || (sgpnt[i].length & 1)){
636 unsigned char * ptr;
637 printk("Bad segment list supplied to aha1542.c (%d, %d)\n",SCpnt->use_sg,i);
638 for(i=0;i<SCpnt->use_sg;i++){
639 printk("%d: %x %x %d\n",i,(unsigned int) sgpnt[i].address, (unsigned int) sgpnt[i].alt_address,
640 sgpnt[i].length);
642 printk("cptr %x: ",(unsigned int) cptr);
643 ptr = (unsigned char *) &cptr[i];
644 for(i=0;i<18;i++) printk("%02x ", ptr[i]);
645 panic("Foooooooood fight!");
647 any2scsi(cptr[i].dataptr, SCSI_PA(sgpnt[i].address));
648 if(SCSI_PA(sgpnt[i].address+sgpnt[i].length-1) > ISA_DMA_THRESHOLD)
649 BAD_DMA("sgpnt", sgpnt[i].address, sgpnt[i].length);
650 any2scsi(cptr[i].datalen, sgpnt[i].length);
652 any2scsi(ccb[mbo].datalen, SCpnt->use_sg * sizeof(struct chain));
653 any2scsi(ccb[mbo].dataptr, SCSI_PA(cptr));
654 #ifdef DEBUG
655 printk("cptr %x: ",cptr);
656 ptr = (unsigned char *) cptr;
657 for(i=0;i<18;i++) printk("%02x ", ptr[i]);
658 #endif
659 } else {
660 ccb[mbo].op = 0; /* SCSI Initiator Command */
661 SCpnt->host_scribble = NULL;
662 any2scsi(ccb[mbo].datalen, bufflen);
663 if(buff && SCSI_PA(buff+bufflen-1) > ISA_DMA_THRESHOLD)
664 BAD_DMA("buff", buff, bufflen);
665 any2scsi(ccb[mbo].dataptr, SCSI_PA(buff));
667 ccb[mbo].idlun = (target&7)<<5 | direction | (lun & 7); /*SCSI Target Id*/
668 ccb[mbo].rsalen = 16;
669 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
670 ccb[mbo].commlinkid = 0;
672 #ifdef DEBUG
673 { int i;
674 printk("aha1542_command: sending.. ");
675 for (i = 0; i < sizeof(ccb[mbo])-10; i++)
676 printk("%02x ", ((unchar *)&ccb[mbo])[i]);
678 #endif
680 if (done) {
681 DEB(printk("aha1542_queuecommand: now waiting for interrupt "); aha1542_stat());
682 SCpnt->scsi_done = done;
683 mb[mbo].status = 1;
684 aha1542_out(SCpnt->host->io_port, &ahacmd, 1); /* start scsi command */
685 DEB(aha1542_stat());
687 else
688 printk("aha1542_queuecommand: done can't be NULL\n");
690 return 0;
693 static void internal_done(Scsi_Cmnd * SCpnt)
695 SCpnt->SCp.Status++;
698 int aha1542_command(Scsi_Cmnd * SCpnt)
700 DEB(printk("aha1542_command: ..calling aha1542_queuecommand\n"));
702 aha1542_queuecommand(SCpnt, internal_done);
704 SCpnt->SCp.Status = 0;
705 while (!SCpnt->SCp.Status)
706 barrier();
707 return SCpnt->result;
710 /* Initialize mailboxes */
711 static void setup_mailboxes(int bse, struct Scsi_Host * shpnt)
713 int i;
714 struct mailbox * mb;
715 struct ccb *ccb;
717 unchar cmd[5] = {CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
719 mb = HOSTDATA(shpnt)->mb;
720 ccb = HOSTDATA(shpnt)->ccb;
722 for(i=0; i<AHA1542_MAILBOXES; i++){
723 mb[i].status = mb[AHA1542_MAILBOXES+i].status = 0;
724 any2scsi(mb[i].ccbptr, SCSI_PA(&ccb[i]));
726 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
727 any2scsi((cmd+2), SCSI_PA(mb));
728 aha1542_out(bse, cmd, 5);
729 WAIT(INTRFLAGS(bse), INTRMASK, HACC, 0);
730 while (0) {
731 fail:
732 printk("aha1542_detect: failed setting up mailboxes\n");
734 aha1542_intr_reset(bse);
737 static int aha1542_getconfig(int base_io, unsigned char * irq_level, unsigned char * dma_chan, unsigned char * scsi_id)
739 unchar inquiry_cmd[] = {CMD_RETCONF };
740 unchar inquiry_result[3];
741 int i;
742 i = inb(STATUS(base_io));
743 if (i & DF) {
744 i = inb(DATA(base_io));
746 aha1542_out(base_io, inquiry_cmd, 1);
747 aha1542_in(base_io, inquiry_result, 3);
748 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
749 while (0) {
750 fail:
751 printk("aha1542_detect: query board settings\n");
753 aha1542_intr_reset(base_io);
754 switch(inquiry_result[0]){
755 case 0x80:
756 *dma_chan = 7;
757 break;
758 case 0x40:
759 *dma_chan = 6;
760 break;
761 case 0x20:
762 *dma_chan = 5;
763 break;
764 case 0x01:
765 *dma_chan = 0;
766 break;
767 case 0:
768 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
769 Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
770 *dma_chan = 0xFF;
771 break;
772 default:
773 printk("Unable to determine Adaptec DMA priority. Disabling board\n");
774 return -1;
776 switch(inquiry_result[1]){
777 case 0x40:
778 *irq_level = 15;
779 break;
780 case 0x20:
781 *irq_level = 14;
782 break;
783 case 0x8:
784 *irq_level = 12;
785 break;
786 case 0x4:
787 *irq_level = 11;
788 break;
789 case 0x2:
790 *irq_level = 10;
791 break;
792 case 0x1:
793 *irq_level = 9;
794 break;
795 default:
796 printk("Unable to determine Adaptec IRQ level. Disabling board\n");
797 return -1;
799 *scsi_id=inquiry_result[2] & 7;
800 return 0;
803 /* This function should only be called for 1542C boards - we can detect
804 the special firmware settings and unlock the board */
806 static int aha1542_mbenable(int base)
808 static unchar mbenable_cmd[3];
809 static unchar mbenable_result[2];
810 int retval;
812 retval = BIOS_TRANSLATION_6432;
814 mbenable_cmd[0]=CMD_EXTBIOS;
815 aha1542_out(base,mbenable_cmd,1);
816 if(aha1542_in1(base,mbenable_result,2))
817 return retval;
818 WAITd(INTRFLAGS(base),INTRMASK,HACC,0,100);
819 aha1542_intr_reset(base);
821 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
822 mbenable_cmd[0]=CMD_MBENABLE;
823 mbenable_cmd[1]=0;
824 mbenable_cmd[2]=mbenable_result[1];
826 if(mbenable_result[1] & 0x03) retval = BIOS_TRANSLATION_25563;
828 aha1542_out(base,mbenable_cmd,3);
829 WAIT(INTRFLAGS(base),INTRMASK,HACC,0);
831 while(0) {
832 fail:
833 printk("aha1542_mbenable: Mailbox init failed\n");
835 aha1542_intr_reset(base);
836 return retval;
839 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
840 static int aha1542_query(int base_io, int * transl)
842 unchar inquiry_cmd[] = {CMD_INQUIRY };
843 unchar inquiry_result[4];
844 int i;
845 i = inb(STATUS(base_io));
846 if (i & DF) {
847 i = inb(DATA(base_io));
849 aha1542_out(base_io, inquiry_cmd, 1);
850 aha1542_in(base_io, inquiry_result, 4);
851 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
852 while (0) {
853 fail:
854 printk("aha1542_detect: query card type\n");
856 aha1542_intr_reset(base_io);
858 *transl = BIOS_TRANSLATION_6432; /* Default case */
860 /* For an AHA1740 series board, we ignore the board since there is a
861 hardware bug which can lead to wrong blocks being returned if the board
862 is operating in the 1542 emulation mode. Since there is an extended mode
863 driver, we simply ignore the board and let the 1740 driver pick it up.
866 if (inquiry_result[0] == 0x43) {
867 printk("aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
868 return 1;
871 /* Always call this - boards that do not support extended bios translation
872 will ignore the command, and we will set the proper default */
874 *transl = aha1542_mbenable(base_io);
876 return 0;
879 /* called from init/main.c */
880 __initfunc(void aha1542_setup( char *str, int *ints))
882 const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
883 static int setup_idx = 0;
884 int setup_portbase;
886 if(setup_idx >= MAXBOARDS)
888 printk("aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
889 printk(" Entryline 1: %s\n",setup_str[0]);
890 printk(" Entryline 2: %s\n",setup_str[1]);
891 printk(" This line: %s\n",str);
892 return;
894 if (ints[0] < 1 || ints[0] > 4)
896 printk("aha1542: %s\n", str );
897 printk(ahausage);
898 printk("aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
901 setup_called[setup_idx]=ints[0];
902 setup_str[setup_idx]=str;
904 setup_portbase = ints[0] >= 1 ? ints[1] : 0; /* Preserve the default value.. */
905 setup_buson [setup_idx] = ints[0] >= 2 ? ints[2] : 7;
906 setup_busoff [setup_idx] = ints[0] >= 3 ? ints[3] : 5;
907 if (ints[0] >= 4) {
908 int atbt = -1;
909 switch (ints[4]) {
910 case 5:
911 atbt = 0x00;
912 break;
913 case 6:
914 atbt = 0x04;
915 break;
916 case 7:
917 atbt = 0x01;
918 break;
919 case 8:
920 atbt = 0x02;
921 break;
922 case 10:
923 atbt = 0x03;
924 break;
925 default:
926 printk("aha1542: %s\n", str );
927 printk(ahausage);
928 printk("aha1542: Valid values for DMASPEED are 5-8, 10 MB/s. Using jumper defaults.\n");
929 break;
931 setup_dmaspeed[setup_idx] = atbt;
934 if (setup_portbase != 0)
935 bases[setup_idx] = setup_portbase;
937 ++setup_idx;
940 /* return non-zero on detection */
941 int aha1542_detect(Scsi_Host_Template * tpnt)
943 unsigned char dma_chan;
944 unsigned char irq_level;
945 unsigned char scsi_id;
946 unsigned long flags;
947 unsigned int base_io;
948 int trans;
949 struct Scsi_Host * shpnt = NULL;
950 int count = 0;
951 int indx;
953 DEB(printk("aha1542_detect: \n"));
955 tpnt->proc_dir = &proc_scsi_aha1542;
957 for(indx = 0; indx < sizeof(bases)/sizeof(bases[0]); indx++)
958 if(bases[indx] != 0 && !check_region(bases[indx], 4)) {
959 shpnt = scsi_register(tpnt,
960 sizeof(struct aha1542_hostdata));
962 /* For now we do this - until kmalloc is more intelligent
963 we are resigned to stupid hacks like this */
964 if (SCSI_PA(shpnt) >= ISA_DMA_THRESHOLD) {
965 printk("Invalid address for shpnt with 1542.\n");
966 goto unregister;
969 if(!aha1542_test_port(bases[indx], shpnt)) goto unregister;
972 base_io = bases[indx];
974 /* Set the Bus on/off-times as not to ruin floppy performance */
976 unchar oncmd[] = {CMD_BUSON_TIME, 7};
977 unchar offcmd[] = {CMD_BUSOFF_TIME, 5};
979 if(setup_called[indx])
981 oncmd[1] = setup_buson[indx];
982 offcmd[1] = setup_busoff[indx];
985 aha1542_intr_reset(base_io);
986 aha1542_out(base_io, oncmd, 2);
987 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
988 aha1542_intr_reset(base_io);
989 aha1542_out(base_io, offcmd, 2);
990 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
991 if (setup_dmaspeed[indx] >= 0)
993 unchar dmacmd[] = {CMD_DMASPEED, 0};
994 dmacmd[1] = setup_dmaspeed[indx];
995 aha1542_intr_reset(base_io);
996 aha1542_out(base_io, dmacmd, 2);
997 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
999 while (0) {
1000 fail:
1001 printk("aha1542_detect: setting bus on/off-time failed\n");
1003 aha1542_intr_reset(base_io);
1005 if(aha1542_query(base_io, &trans)) goto unregister;
1007 if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1) goto unregister;
1009 printk("Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
1010 if (dma_chan != 0xFF)
1011 printk(", DMA priority %d", dma_chan);
1012 printk("\n");
1014 DEB(aha1542_stat());
1015 setup_mailboxes(base_io, shpnt);
1017 DEB(aha1542_stat());
1019 DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
1020 save_flags(flags);
1021 cli();
1022 if (request_irq(irq_level,do_aha1542_intr_handle, 0, "aha1542", NULL)) {
1023 printk("Unable to allocate IRQ for adaptec controller.\n");
1024 restore_flags(flags);
1025 goto unregister;
1028 if (dma_chan != 0xFF) {
1029 if (request_dma(dma_chan,"aha1542")) {
1030 printk("Unable to allocate DMA channel for Adaptec.\n");
1031 free_irq(irq_level, NULL);
1032 restore_flags(flags);
1033 goto unregister;
1036 if (dma_chan == 0 || dma_chan >= 5) {
1037 set_dma_mode(dma_chan, DMA_MODE_CASCADE);
1038 enable_dma(dma_chan);
1041 aha_host[irq_level - 9] = shpnt;
1042 shpnt->this_id = scsi_id;
1043 shpnt->unique_id = base_io;
1044 shpnt->io_port = base_io;
1045 shpnt->n_io_port = 4; /* Number of bytes of I/O space used */
1046 shpnt->dma_channel = dma_chan;
1047 shpnt->irq = irq_level;
1048 HOSTDATA(shpnt)->bios_translation = trans;
1049 if(trans == BIOS_TRANSLATION_25563)
1050 printk("aha1542.c: Using extended bios translation\n");
1051 HOSTDATA(shpnt)->aha1542_last_mbi_used = (2*AHA1542_MAILBOXES - 1);
1052 HOSTDATA(shpnt)->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
1053 memset(HOSTDATA(shpnt)->SCint, 0, sizeof(HOSTDATA(shpnt)->SCint));
1054 restore_flags(flags);
1055 #if 0
1056 DEB(printk(" *** READ CAPACITY ***\n"));
1059 unchar buf[8];
1060 static unchar cmd[] = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1061 int i;
1063 for (i = 0; i < sizeof(buf); ++i) buf[i] = 0x87;
1064 for (i = 0; i < 2; ++i)
1065 if (!aha1542_command(i, cmd, buf, sizeof(buf))) {
1066 printk("aha_detect: LU %d sector_size %d device_size %d\n",
1067 i, xscsi2int(buf+4), xscsi2int(buf));
1071 DEB(printk(" *** NOW RUNNING MY OWN TEST *** \n"));
1073 for (i = 0; i < 4; ++i)
1075 unsigned char cmd[10];
1076 static buffer[512];
1078 cmd[0] = READ_10;
1079 cmd[1] = 0;
1080 xany2scsi(cmd+2, i);
1081 cmd[6] = 0;
1082 cmd[7] = 0;
1083 cmd[8] = 1;
1084 cmd[9] = 0;
1085 aha1542_command(0, cmd, buffer, 512);
1087 #endif
1088 request_region(bases[indx], 4,"aha1542"); /* Register the IO ports that we use */
1089 count++;
1090 continue;
1091 unregister:
1092 scsi_unregister(shpnt);
1093 continue;
1097 return count;
1100 static int aha1542_restart(struct Scsi_Host * shost)
1102 int i;
1103 int count = 0;
1104 #if 0
1105 unchar ahacmd = CMD_START_SCSI;
1106 #endif
1108 for(i=0; i< AHA1542_MAILBOXES; i++)
1109 if(HOSTDATA(shost)->SCint[i] &&
1110 !(HOSTDATA(shost)->SCint[i]->device->soft_reset))
1112 #if 0
1113 HOSTDATA(shost)->mb[i].status = 1; /* Indicate ready to restart... */
1114 #endif
1115 count++;
1118 printk("Potential to restart %d stalled commands...\n", count);
1119 #if 0
1120 /* start scsi command */
1121 if (count) aha1542_out(shost->io_port, &ahacmd, 1);
1122 #endif
1123 return 0;
1126 int aha1542_abort(Scsi_Cmnd * SCpnt)
1130 * The abort command does not leave the device in a clean state where
1131 * it is available to be used again. Until this gets worked out, we
1132 * will leave it commented out.
1135 printk("aha1542.c: Unable to abort command for target %d\n",
1136 SCpnt->target);
1137 return FAILED;
1141 * This is a device reset. This is handled by sending a special command
1142 * to the device.
1144 int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
1146 unsigned long flags;
1147 struct mailbox * mb;
1148 unchar target = SCpnt->target;
1149 unchar lun = SCpnt->lun;
1150 int mbo;
1151 struct ccb *ccb;
1152 unchar ahacmd = CMD_START_SCSI;
1154 ccb = HOSTDATA(SCpnt->host)->ccb;
1155 mb = HOSTDATA(SCpnt->host)->mb;
1157 save_flags(flags);
1158 cli();
1159 mbo = HOSTDATA(SCpnt->host)->aha1542_last_mbo_used + 1;
1160 if (mbo >= AHA1542_MAILBOXES) mbo = 0;
1163 if(mb[mbo].status == 0 && HOSTDATA(SCpnt->host)->SCint[mbo] == NULL)
1164 break;
1165 mbo++;
1166 if (mbo >= AHA1542_MAILBOXES) mbo = 0;
1167 } while (mbo != HOSTDATA(SCpnt->host)->aha1542_last_mbo_used);
1169 if(mb[mbo].status || HOSTDATA(SCpnt->host)->SCint[mbo])
1170 panic("Unable to find empty mailbox for aha1542.\n");
1172 HOSTDATA(SCpnt->host)->SCint[mbo] = SCpnt; /* This will effectively
1173 prevent someone else from
1174 screwing with this cdb. */
1176 HOSTDATA(SCpnt->host)->aha1542_last_mbo_used = mbo;
1177 restore_flags(flags);
1179 any2scsi(mb[mbo].ccbptr, SCSI_PA(&ccb[mbo])); /* This gets trashed for some reason*/
1181 memset(&ccb[mbo], 0, sizeof(struct ccb));
1183 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
1185 ccb[mbo].idlun = (target&7)<<5 | (lun & 7); /*SCSI Target Id*/
1187 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
1188 ccb[mbo].commlinkid = 0;
1191 * Now tell the 1542 to flush all pending commands for this
1192 * target
1194 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1196 printk("aha1542.c: Trying device reset for target %d\n", SCpnt->target);
1198 return SUCCESS;
1201 #ifdef ERIC_neverdef
1203 * With the 1542 we apparently never get an interrupt to
1204 * acknowledge a device reset being sent. Then again, Leonard
1205 * says we are doing this wrong in the first place...
1207 * Take a wait and see attitude. If we get spurious interrupts,
1208 * then the device reset is doing something sane and useful, and
1209 * we will wait for the interrupt to post completion.
1211 printk("Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1214 * Free the command block for all commands running on this
1215 * target...
1217 for(i=0; i< AHA1542_MAILBOXES; i++)
1219 if(HOSTDATA(SCpnt->host)->SCint[i] &&
1220 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target)
1222 Scsi_Cmnd * SCtmp;
1223 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1224 if (SCtmp->host_scribble)
1226 scsi_free(SCtmp->host_scribble, 512);
1229 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1230 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1233 return SUCCESS;
1235 return FAILED;
1236 #endif /* ERIC_neverdef */
1239 int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
1241 int i;
1244 * This does a scsi reset for all devices on the bus.
1245 * In principle, we could also reset the 1542 - should
1246 * we do this? Try this first, and we can add that later
1247 * if it turns out to be useful.
1249 outb(SCRST, CONTROL(SCpnt->host->io_port));
1252 * Wait for the thing to settle down a bit. Unfortunately
1253 * this is going to basically lock up the machine while we
1254 * wait for this to complete. To be 100% correct, we need to
1255 * check for timeout, and if we are doing something like this
1256 * we are pretty desperate anyways.
1258 scsi_sleep(4*HZ);
1260 WAIT(STATUS(SCpnt->host->io_port),
1261 STATMASK, INIT|IDLE, STST|DIAGF|INVDCMD|DF|CDF);
1264 * Now try to pick up the pieces. For all pending commands,
1265 * free any internal data structures, and basically clear things
1266 * out. We do not try and restart any commands or anything -
1267 * the strategy handler takes care of that crap.
1269 printk("Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1271 for(i=0; i< AHA1542_MAILBOXES; i++)
1273 if(HOSTDATA(SCpnt->host)->SCint[i] != NULL)
1275 Scsi_Cmnd * SCtmp;
1276 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1279 if( SCtmp->device->soft_reset )
1282 * If this device implements the soft reset option,
1283 * then it is still holding onto the command, and
1284 * may yet complete it. In this case, we don't
1285 * flush the data.
1287 continue;
1290 if (SCtmp->host_scribble)
1292 scsi_free(SCtmp->host_scribble, 512);
1295 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1296 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1300 return SUCCESS;
1302 fail:
1303 return FAILED;
1306 int aha1542_host_reset(Scsi_Cmnd * SCpnt)
1308 int i;
1311 * This does a scsi reset for all devices on the bus.
1312 * In principle, we could also reset the 1542 - should
1313 * we do this? Try this first, and we can add that later
1314 * if it turns out to be useful.
1316 outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
1319 * Wait for the thing to settle down a bit. Unfortunately
1320 * this is going to basically lock up the machine while we
1321 * wait for this to complete. To be 100% correct, we need to
1322 * check for timeout, and if we are doing something like this
1323 * we are pretty desperate anyways.
1325 scsi_sleep(4*HZ);
1327 WAIT(STATUS(SCpnt->host->io_port),
1328 STATMASK, INIT|IDLE, STST|DIAGF|INVDCMD|DF|CDF);
1331 * We need to do this too before the 1542 can interact with
1332 * us again.
1334 setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
1337 * Now try to pick up the pieces. For all pending commands,
1338 * free any internal data structures, and basically clear things
1339 * out. We do not try and restart any commands or anything -
1340 * the strategy handler takes care of that crap.
1342 printk("Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1344 for(i=0; i< AHA1542_MAILBOXES; i++)
1346 if(HOSTDATA(SCpnt->host)->SCint[i] != NULL)
1348 Scsi_Cmnd * SCtmp;
1349 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1351 if( SCtmp->device->soft_reset )
1354 * If this device implements the soft reset option,
1355 * then it is still holding onto the command, and
1356 * may yet complete it. In this case, we don't
1357 * flush the data.
1359 continue;
1362 if (SCtmp->host_scribble)
1364 scsi_free(SCtmp->host_scribble, 512);
1367 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1368 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1372 return SUCCESS;
1374 fail:
1375 return FAILED;
1379 * These are the old error handling routines. They are only temporarily
1380 * here while we play with the new error handling code.
1382 int aha1542_old_abort(Scsi_Cmnd * SCpnt)
1384 #if 0
1385 unchar ahacmd = CMD_START_SCSI;
1386 unsigned long flags;
1387 struct mailbox * mb;
1388 int mbi, mbo, i;
1390 printk("In aha1542_abort: %x %x\n",
1391 inb(STATUS(SCpnt->host->io_port)),
1392 inb(INTRFLAGS(SCpnt->host->io_port)));
1394 save_flags(flags);
1395 cli();
1396 mb = HOSTDATA(SCpnt->host)->mb;
1397 mbi = HOSTDATA(SCpnt->host)->aha1542_last_mbi_used + 1;
1398 if (mbi >= 2*AHA1542_MAILBOXES) mbi = AHA1542_MAILBOXES;
1401 if(mb[mbi].status != 0) break;
1402 mbi++;
1403 if (mbi >= 2*AHA1542_MAILBOXES) mbi = AHA1542_MAILBOXES;
1404 } while (mbi != HOSTDATA(SCpnt->host)->aha1542_last_mbi_used);
1405 restore_flags(flags);
1407 if(mb[mbi].status) {
1408 printk("Lost interrupt discovered on irq %d - attempting to recover\n",
1409 SCpnt->host->irq);
1410 aha1542_intr_handle(SCpnt->host->irq, NULL);
1411 return 0;
1414 /* OK, no lost interrupt. Try looking to see how many pending commands
1415 we think we have. */
1417 for(i=0; i< AHA1542_MAILBOXES; i++)
1418 if(HOSTDATA(SCpnt->host)->SCint[i])
1420 if(HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1421 printk("Timed out command pending for %s\n",
1422 kdevname(SCpnt->request.rq_dev));
1423 if (HOSTDATA(SCpnt->host)->mb[i].status) {
1424 printk("OGMB still full - restarting\n");
1425 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1427 } else
1428 printk("Other pending command %s\n",
1429 kdevname(SCpnt->request.rq_dev));
1432 #endif
1434 DEB(printk("aha1542_abort\n"));
1435 #if 0
1436 save_flags(flags);
1437 cli();
1438 for(mbo = 0; mbo < AHA1542_MAILBOXES; mbo++)
1439 if (SCpnt == HOSTDATA(SCpnt->host)->SCint[mbo]){
1440 mb[mbo].status = 2; /* Abort command */
1441 aha1542_out(SCpnt->host->io_port, &ahacmd, 1); /* start scsi command */
1442 restore_flags(flags);
1443 break;
1445 #endif
1446 return SCSI_ABORT_SNOOZE;
1449 /* We do not implement a reset function here, but the upper level code
1450 assumes that it will get some kind of response for the command in
1451 SCpnt. We must oblige, or the command will hang the scsi system.
1452 For a first go, we assume that the 1542 notifies us with all of the
1453 pending commands (it does implement soft reset, after all). */
1455 int aha1542_old_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1457 unchar ahacmd = CMD_START_SCSI;
1458 int i;
1461 * See if a bus reset was suggested.
1463 if( reset_flags & SCSI_RESET_SUGGEST_BUS_RESET )
1466 * This does a scsi reset for all devices on the bus.
1467 * In principle, we could also reset the 1542 - should
1468 * we do this? Try this first, and we can add that later
1469 * if it turns out to be useful.
1471 outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
1474 * Wait for the thing to settle down a bit. Unfortunately
1475 * this is going to basically lock up the machine while we
1476 * wait for this to complete. To be 100% correct, we need to
1477 * check for timeout, and if we are doing something like this
1478 * we are pretty desperate anyways.
1480 WAIT(STATUS(SCpnt->host->io_port),
1481 STATMASK, INIT|IDLE, STST|DIAGF|INVDCMD|DF|CDF);
1484 * We need to do this too before the 1542 can interact with
1485 * us again.
1487 setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
1490 * Now try to pick up the pieces. Restart all commands
1491 * that are currently active on the bus, and reset all of
1492 * the datastructures. We have some time to kill while
1493 * things settle down, so print a nice message.
1495 printk("Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1497 for(i=0; i< AHA1542_MAILBOXES; i++)
1498 if(HOSTDATA(SCpnt->host)->SCint[i] != NULL)
1500 Scsi_Cmnd * SCtmp;
1501 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1502 SCtmp->result = DID_RESET << 16;
1503 if (SCtmp->host_scribble) scsi_free(SCtmp->host_scribble, 512);
1504 printk("Sending DID_RESET for target %d\n", SCpnt->target);
1505 SCtmp->scsi_done(SCpnt);
1507 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1508 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1511 * Now tell the mid-level code what we did here. Since
1512 * we have restarted all of the outstanding commands,
1513 * then report SUCCESS.
1515 return (SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET);
1516 fail:
1517 printk("aha1542.c: Unable to perform hard reset.\n");
1518 printk("Power cycle machine to reset\n");
1519 return (SCSI_RESET_ERROR | SCSI_RESET_BUS_RESET);
1523 else
1525 /* This does a selective reset of just the one device */
1526 /* First locate the ccb for this command */
1527 for(i=0; i< AHA1542_MAILBOXES; i++)
1528 if(HOSTDATA(SCpnt->host)->SCint[i] == SCpnt)
1530 HOSTDATA(SCpnt->host)->ccb[i].op = 0x81; /* BUS DEVICE RESET */
1531 /* Now tell the 1542 to flush all pending commands for this target */
1532 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1534 /* Here is the tricky part. What to do next. Do we get an interrupt
1535 for the commands that we aborted with the specified target, or
1536 do we generate this on our own? Try it without first and see
1537 what happens */
1538 printk("Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1540 /* If the first does not work, then try the second. I think the
1541 first option is more likely to be correct. Free the command
1542 block for all commands running on this target... */
1543 for(i=0; i< AHA1542_MAILBOXES; i++)
1544 if(HOSTDATA(SCpnt->host)->SCint[i] &&
1545 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target)
1547 Scsi_Cmnd * SCtmp;
1548 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1549 SCtmp->result = DID_RESET << 16;
1550 if (SCtmp->host_scribble) scsi_free(SCtmp->host_scribble, 512);
1551 printk("Sending DID_RESET for target %d\n", SCpnt->target);
1552 SCtmp->scsi_done(SCpnt);
1554 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1555 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1557 return SCSI_RESET_SUCCESS;
1560 /* No active command at this time, so this means that each time we got
1561 some kind of response the last time through. Tell the mid-level code
1562 to request sense information in order to decide what to do next. */
1563 return SCSI_RESET_PUNT;
1566 #include "sd.h"
1568 int aha1542_biosparam(Scsi_Disk * disk, kdev_t dev, int * ip)
1570 int translation_algorithm;
1571 int size = disk->capacity;
1573 translation_algorithm = HOSTDATA(disk->device->host)->bios_translation;
1575 if((size>>11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
1576 /* Please verify that this is the same as what DOS returns */
1577 ip[0] = 255;
1578 ip[1] = 63;
1579 ip[2] = size /255/63;
1580 } else {
1581 ip[0] = 64;
1582 ip[1] = 32;
1583 ip[2] = size >> 11;
1586 return 0;
1590 #ifdef MODULE
1591 /* Eventually this will go into an include file, but this will be later */
1592 Scsi_Host_Template driver_template = AHA1542;
1594 #include "scsi_module.c"
1595 #endif