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