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