aha1542: Use BIT() macro
[linux-2.6-block.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
726a6459 24 * Modified by Adam Fritzler
a88dc06c 25 * Added proper detection of the AHA-1640 (MCA, now deleted)
1da177e4
LT
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>
643a7c43
OZ
38#include <linux/isa.h>
39#include <linux/pnp.h>
1da177e4 40#include <linux/blkdev.h>
5a0e3ad6 41#include <linux/slab.h>
1da177e4
LT
42
43#include <asm/dma.h>
1da177e4
LT
44#include <asm/io.h>
45
46#include "scsi.h"
47#include <scsi/scsi_host.h>
48#include "aha1542.h"
6ac7d115 49#include <linux/stat.h>
1da177e4
LT
50
51#ifdef DEBUG
52#define DEB(x) x
53#else
54#define DEB(x)
55#endif
56
57/*
58 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 $";
59 */
60
61/* The adaptec can be configured for quite a number of addresses, but
62 I generally do not want the card poking around at random. We allow
63 two addresses - this allows people to use the Adaptec with a Midi
64 card, which also used 0x330 -- can be overridden with LILO! */
65
66#define MAXBOARDS 4 /* Increase this and the sizes of the
67 arrays below, if you need more.. */
68
a88dc06c 69/* Boards 3,4 slots are reserved for ISAPnP scans */
1da177e4 70
643a7c43 71static unsigned int bases[MAXBOARDS] = {0x330, 0x334, 0, 0};
1da177e4
LT
72
73/* set by aha1542_setup according to the command line; they also may
74 be marked __initdata, but require zero initializers then */
75
76static int setup_called[MAXBOARDS];
77static int setup_buson[MAXBOARDS];
78static int setup_busoff[MAXBOARDS];
643a7c43 79static int setup_dmaspeed[MAXBOARDS] = { -1, -1, -1, -1 };
1da177e4
LT
80
81/*
82 * LILO/Module params: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]
83 *
84 * Where: <PORTBASE> is any of the valid AHA addresses:
85 * 0x130, 0x134, 0x230, 0x234, 0x330, 0x334
86 * <BUSON> is the time (in microsecs) that AHA spends on the AT-bus
87 * when transferring data. 1542A power-on default is 11us,
88 * valid values are in range: 2..15 (decimal)
89 * <BUSOFF> is the time that AHA spends OFF THE BUS after while
90 * it is transferring data (not to monopolize the bus).
91 * Power-on default is 4us, valid range: 1..64 microseconds.
92 * <DMASPEED> Default is jumper selected (1542A: on the J1),
93 * but experimenter can alter it with this.
94 * Valid values: 5, 6, 7, 8, 10 (MB/s)
95 * Factory default is 5 MB/s.
96 */
97
98#if defined(MODULE)
90ab5ee9 99static bool isapnp = 0;
1da177e4
LT
100static int aha1542[] = {0x330, 11, 4, -1};
101module_param_array(aha1542, int, NULL, 0);
102module_param(isapnp, bool, 0);
1da177e4
LT
103#else
104static int isapnp = 1;
105#endif
106
107#define BIOS_TRANSLATION_1632 0 /* Used by some old 1542A boards */
108#define BIOS_TRANSLATION_6432 1 /* Default case these days */
109#define BIOS_TRANSLATION_25563 2 /* Big disk case */
110
111struct aha1542_hostdata {
112 /* This will effectively start both of them at the first mailbox */
113 int bios_translation; /* Mapping bios uses - for compatibility */
114 int aha1542_last_mbi_used;
115 int aha1542_last_mbo_used;
116 Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
117 struct mailbox mb[2 * AHA1542_MAILBOXES];
118 struct ccb ccb[AHA1542_MAILBOXES];
119};
120
1da177e4
LT
121static DEFINE_SPINLOCK(aha1542_lock);
122
123
124
125#define WAITnexttimeout 3000000
126
f1bbef63
OZ
127static inline void aha1542_intr_reset(u16 base)
128{
129 outb(IRST, CONTROL(base));
130}
1da177e4
LT
131
132#define WAIT(port, mask, allof, noneof) \
133 { register int WAITbits; \
134 register int WAITtimeout = WAITnexttimeout; \
135 while (1) { \
136 WAITbits = inb(port) & (mask); \
137 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
138 break; \
139 if (--WAITtimeout == 0) goto fail; \
140 } \
141 }
142
143/* Similar to WAIT, except we use the udelay call to regulate the
144 amount of time we wait. */
145#define WAITd(port, mask, allof, noneof, timeout) \
146 { register int WAITbits; \
147 register int WAITtimeout = timeout; \
148 while (1) { \
149 WAITbits = inb(port) & (mask); \
150 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
151 break; \
152 mdelay(1); \
153 if (--WAITtimeout == 0) goto fail; \
154 } \
155 }
156
1da177e4
LT
157/* This is a bit complicated, but we need to make sure that an interrupt
158 routine does not send something out while we are in the middle of this.
159 Fortunately, it is only at boot time that multi-byte messages
160 are ever sent. */
cb5b570c 161static int aha1542_out(unsigned int base, u8 *cmdp, int len)
1da177e4
LT
162{
163 unsigned long flags = 0;
164 int got_lock;
165
166 if (len == 1) {
167 got_lock = 0;
168 while (1 == 1) {
169 WAIT(STATUS(base), CDF, 0, CDF);
170 spin_lock_irqsave(&aha1542_lock, flags);
171 if (inb(STATUS(base)) & CDF) {
172 spin_unlock_irqrestore(&aha1542_lock, flags);
173 continue;
174 }
175 outb(*cmdp, DATA(base));
176 spin_unlock_irqrestore(&aha1542_lock, flags);
177 return 0;
178 }
179 } else {
180 spin_lock_irqsave(&aha1542_lock, flags);
181 got_lock = 1;
182 while (len--) {
183 WAIT(STATUS(base), CDF, 0, CDF);
184 outb(*cmdp++, DATA(base));
185 }
186 spin_unlock_irqrestore(&aha1542_lock, flags);
187 }
188 return 0;
189fail:
190 if (got_lock)
191 spin_unlock_irqrestore(&aha1542_lock, flags);
192 printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
1da177e4
LT
193 return 1;
194}
195
196/* Only used at boot time, so we do not need to worry about latency as much
197 here */
198
cb5b570c 199static int aha1542_in(unsigned int base, u8 *cmdp, int len)
1da177e4
LT
200{
201 unsigned long flags;
202
203 spin_lock_irqsave(&aha1542_lock, flags);
204 while (len--) {
205 WAIT(STATUS(base), DF, DF, 0);
206 *cmdp++ = inb(DATA(base));
207 }
208 spin_unlock_irqrestore(&aha1542_lock, flags);
209 return 0;
210fail:
211 spin_unlock_irqrestore(&aha1542_lock, flags);
212 printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
1da177e4
LT
213 return 1;
214}
215
216/* Similar to aha1542_in, except that we wait a very short period of time.
217 We use this if we know the board is alive and awake, but we are not sure
218 if the board will respond to the command we are about to send or not */
cb5b570c 219static int aha1542_in1(unsigned int base, u8 *cmdp, int len)
1da177e4
LT
220{
221 unsigned long flags;
222
223 spin_lock_irqsave(&aha1542_lock, flags);
224 while (len--) {
225 WAITd(STATUS(base), DF, DF, 0, 100);
226 *cmdp++ = inb(DATA(base));
227 }
228 spin_unlock_irqrestore(&aha1542_lock, flags);
229 return 0;
230fail:
231 spin_unlock_irqrestore(&aha1542_lock, flags);
232 return 1;
233}
234
235static int makecode(unsigned hosterr, unsigned scsierr)
236{
237 switch (hosterr) {
238 case 0x0:
239 case 0xa: /* Linked command complete without error and linked normally */
240 case 0xb: /* Linked command complete without error, interrupt generated */
241 hosterr = 0;
242 break;
243
244 case 0x11: /* Selection time out-The initiator selection or target
245 reselection was not complete within the SCSI Time out period */
246 hosterr = DID_TIME_OUT;
247 break;
248
249 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
250 than was allocated by the Data Length field or the sum of the
251 Scatter / Gather Data Length fields. */
252
253 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
254
255 case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
256 invalid. This usually indicates a software failure. */
257
258 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
259 This usually indicates a software failure. */
260
261 case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
262 of linked CCB's does not specify the same logical unit number as
263 the first. */
264 case 0x18: /* Invalid Target Direction received from Host-The direction of a
265 Target Mode CCB was invalid. */
266
267 case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
268 received to service data transfer between the same target LUN
269 and initiator SCSI ID in the same direction. */
270
271 case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
272 length segment or invalid segment list boundaries was received.
273 A CCB parameter was invalid. */
274 DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
275 hosterr = DID_ERROR; /* Couldn't find any better */
276 break;
277
278 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
279 phase sequence was requested by the target. The host adapter
280 will generate a SCSI Reset Condition, notifying the host with
281 a SCRD interrupt */
282 hosterr = DID_RESET;
283 break;
284 default:
285 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
286 break;
287 }
288 return scsierr | (hosterr << 16);
289}
290
643a7c43 291static int aha1542_test_port(int bse, struct Scsi_Host *shpnt)
1da177e4 292{
cb5b570c
OZ
293 u8 inquiry_cmd[] = {CMD_INQUIRY};
294 u8 inquiry_result[4];
295 u8 *cmdp;
1da177e4
LT
296 int len;
297 volatile int debug = 0;
298
299 /* Quick and dirty test for presence of the card. */
300 if (inb(STATUS(bse)) == 0xff)
301 return 0;
302
303 /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
304
305 /* DEB(printk("aha1542_test_port called \n")); */
306
307 /* In case some other card was probing here, reset interrupts */
308 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
309
310 outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
311
312 mdelay(20); /* Wait a little bit for things to settle down. */
313
314 debug = 1;
315 /* Expect INIT and IDLE, any of the others are bad */
316 WAIT(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
317
318 debug = 2;
319 /* Shouldn't have generated any interrupts during reset */
320 if (inb(INTRFLAGS(bse)) & INTRMASK)
321 goto fail;
322
323
324 /* Perform a host adapter inquiry instead so we do not need to set
325 up the mailboxes ahead of time */
326
327 aha1542_out(bse, inquiry_cmd, 1);
328
329 debug = 3;
330 len = 4;
331 cmdp = &inquiry_result[0];
332
333 while (len--) {
334 WAIT(STATUS(bse), DF, DF, 0);
335 *cmdp++ = inb(DATA(bse));
336 }
337
338 debug = 8;
339 /* Reading port should reset DF */
340 if (inb(STATUS(bse)) & DF)
341 goto fail;
342
343 debug = 9;
344 /* When HACC, command is completed, and we're though testing */
345 WAIT(INTRFLAGS(bse), HACC, HACC, 0);
346 /* now initialize adapter */
347
348 debug = 10;
349 /* Clear interrupts */
350 outb(IRST, CONTROL(bse));
351
352 debug = 11;
353
354 return debug; /* 1 = ok */
355fail:
356 return 0; /* 0 = not ok */
357}
358
09a44833 359static int aha1542_restart(struct Scsi_Host *shost)
1da177e4 360{
09a44833
OZ
361 struct aha1542_hostdata *aha1542 = shost_priv(shost);
362 int i;
363 int count = 0;
1da177e4 364
09a44833
OZ
365 for (i = 0; i < AHA1542_MAILBOXES; i++)
366 if (aha1542->SCint[i] &&
367 !(aha1542->SCint[i]->device->soft_reset)) {
368 count++;
369 }
370 printk(KERN_DEBUG "Potential to restart %d stalled commands...\n", count);
371
372 return 0;
1da177e4
LT
373}
374
375/* A "high" level interrupt handler */
87c4d7bc 376static void aha1542_intr_handle(struct Scsi_Host *shost)
1da177e4 377{
e98878f7 378 struct aha1542_hostdata *aha1542 = shost_priv(shost);
1da177e4
LT
379 void (*my_done) (Scsi_Cmnd *) = NULL;
380 int errstatus, mbi, mbo, mbistatus;
381 int number_serviced;
382 unsigned long flags;
383 Scsi_Cmnd *SCtmp;
384 int flag;
385 int needs_restart;
e98878f7
OZ
386 struct mailbox *mb = aha1542->mb;
387 struct ccb *ccb = aha1542->ccb;
1da177e4
LT
388
389#ifdef DEBUG
390 {
391 flag = inb(INTRFLAGS(shost->io_port));
392 printk(KERN_DEBUG "aha1542_intr_handle: ");
393 if (!(flag & ANYINTR))
394 printk("no interrupt?");
395 if (flag & MBIF)
396 printk("MBIF ");
397 if (flag & MBOA)
398 printk("MBOF ");
399 if (flag & HACC)
400 printk("HACC ");
401 if (flag & SCRD)
402 printk("SCRD ");
403 printk("status %02x\n", inb(STATUS(shost->io_port)));
404 };
405#endif
406 number_serviced = 0;
407 needs_restart = 0;
408
409 while (1 == 1) {
410 flag = inb(INTRFLAGS(shost->io_port));
411
412 /* Check for unusual interrupts. If any of these happen, we should
413 probably do something special, but for now just printing a message
414 is sufficient. A SCSI reset detected is something that we really
415 need to deal with in some way. */
416 if (flag & ~MBIF) {
417 if (flag & MBOA)
418 printk("MBOF ");
419 if (flag & HACC)
420 printk("HACC ");
421 if (flag & SCRD) {
422 needs_restart = 1;
423 printk("SCRD ");
424 }
425 }
426 aha1542_intr_reset(shost->io_port);
427
428 spin_lock_irqsave(&aha1542_lock, flags);
e98878f7 429 mbi = aha1542->aha1542_last_mbi_used + 1;
1da177e4
LT
430 if (mbi >= 2 * AHA1542_MAILBOXES)
431 mbi = AHA1542_MAILBOXES;
432
433 do {
434 if (mb[mbi].status != 0)
435 break;
436 mbi++;
437 if (mbi >= 2 * AHA1542_MAILBOXES)
438 mbi = AHA1542_MAILBOXES;
e98878f7 439 } while (mbi != aha1542->aha1542_last_mbi_used);
1da177e4
LT
440
441 if (mb[mbi].status == 0) {
442 spin_unlock_irqrestore(&aha1542_lock, flags);
443 /* Hmm, no mail. Must have read it the last time around */
444 if (!number_serviced && !needs_restart)
445 printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
446 /* We detected a reset. Restart all pending commands for
447 devices that use the hard reset option */
448 if (needs_restart)
449 aha1542_restart(shost);
450 return;
451 };
452
10be6250 453 mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
1da177e4
LT
454 mbistatus = mb[mbi].status;
455 mb[mbi].status = 0;
e98878f7 456 aha1542->aha1542_last_mbi_used = mbi;
1da177e4
LT
457 spin_unlock_irqrestore(&aha1542_lock, flags);
458
459#ifdef DEBUG
460 {
461 if (ccb[mbo].tarstat | ccb[mbo].hastat)
462 printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
463 ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
464 };
465#endif
466
467 if (mbistatus == 3)
468 continue; /* Aborted command not found */
469
470#ifdef DEBUG
471 printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
472#endif
473
e98878f7 474 SCtmp = aha1542->SCint[mbo];
1da177e4
LT
475
476 if (!SCtmp || !SCtmp->scsi_done) {
477 printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
478 printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
479 ccb[mbo].hastat, ccb[mbo].idlun, mbo);
480 return;
481 }
482 my_done = SCtmp->scsi_done;
c9475cb0
JJ
483 kfree(SCtmp->host_scribble);
484 SCtmp->host_scribble = NULL;
1da177e4
LT
485 /* Fetch the sense data, and tuck it away, in the required slot. The
486 Adaptec automatically fetches it, and there is no guarantee that
487 we will still have it in the cdb when we come back */
488 if (ccb[mbo].tarstat == 2)
489 memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
b80ca4f7 490 SCSI_SENSE_BUFFERSIZE);
1da177e4
LT
491
492
493 /* is there mail :-) */
494
495 /* more error checking left out here */
496 if (mbistatus != 1)
497 /* This is surely wrong, but I don't know what's right */
498 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
499 else
500 errstatus = 0;
501
502#ifdef DEBUG
503 if (errstatus)
504 printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
505 ccb[mbo].hastat, ccb[mbo].tarstat);
506#endif
507
508 if (ccb[mbo].tarstat == 2) {
509#ifdef DEBUG
510 int i;
511#endif
512 DEB(printk("aha1542_intr_handle: sense:"));
513#ifdef DEBUG
514 for (i = 0; i < 12; i++)
515 printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
516 printk("\n");
517#endif
518 /*
519 DEB(printk("aha1542_intr_handle: buf:"));
520 for (i = 0; i < bufflen; i++)
521 printk("%02x ", ((unchar *)buff)[i]);
522 printk("\n");
523 */
524 }
525 DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
526 SCtmp->result = errstatus;
e98878f7
OZ
527 aha1542->SCint[mbo] = NULL; /* This effectively frees up the mailbox slot, as
528 far as queuecommand is concerned */
1da177e4
LT
529 my_done(SCtmp);
530 number_serviced++;
531 };
532}
533
09a44833
OZ
534/* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
535static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id)
536{
537 unsigned long flags;
538 struct Scsi_Host *shost = dev_id;
539
540 spin_lock_irqsave(shost->host_lock, flags);
541 aha1542_intr_handle(shost);
542 spin_unlock_irqrestore(shost->host_lock, flags);
543 return IRQ_HANDLED;
544}
545
f281233d 546static int aha1542_queuecommand_lck(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
1da177e4 547{
e98878f7 548 struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
cb5b570c
OZ
549 u8 ahacmd = CMD_START_SCSI;
550 u8 direction;
551 u8 *cmd = (u8 *) SCpnt->cmnd;
552 u8 target = SCpnt->device->id;
553 u8 lun = SCpnt->device->lun;
1da177e4 554 unsigned long flags;
fc3fdfcc 555 int bufflen = scsi_bufflen(SCpnt);
1da177e4 556 int mbo;
e98878f7
OZ
557 struct mailbox *mb = aha1542->mb;
558 struct ccb *ccb = aha1542->ccb;
1da177e4
LT
559
560 DEB(int i);
561
1da177e4
LT
562 DEB(if (target > 1) {
563 SCpnt->result = DID_TIME_OUT << 16;
564 done(SCpnt); return 0;
565 }
566 );
567
568 if (*cmd == REQUEST_SENSE) {
569 /* Don't do the command - we have the sense data already */
1da177e4
LT
570 SCpnt->result = 0;
571 done(SCpnt);
572 return 0;
573 }
574#ifdef DEBUG
575 if (*cmd == READ_10 || *cmd == WRITE_10)
576 i = xscsi2int(cmd + 2);
577 else if (*cmd == READ_6 || *cmd == WRITE_6)
578 i = scsi2int(cmd + 2);
579 else
580 i = -1;
581 if (done)
582 printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
583 else
584 printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
1da177e4
LT
585 printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
586 for (i = 0; i < SCpnt->cmd_len; i++)
587 printk("%02x ", cmd[i]);
588 printk("\n");
589 if (*cmd == WRITE_10 || *cmd == WRITE_6)
590 return 0; /* we are still testing, so *don't* write */
591#endif
592 /* Use the outgoing mailboxes in a round-robin fashion, because this
593 is how the host adapter will scan for them */
594
595 spin_lock_irqsave(&aha1542_lock, flags);
e98878f7 596 mbo = aha1542->aha1542_last_mbo_used + 1;
1da177e4
LT
597 if (mbo >= AHA1542_MAILBOXES)
598 mbo = 0;
599
600 do {
e98878f7 601 if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
1da177e4
LT
602 break;
603 mbo++;
604 if (mbo >= AHA1542_MAILBOXES)
605 mbo = 0;
e98878f7 606 } while (mbo != aha1542->aha1542_last_mbo_used);
1da177e4 607
e98878f7 608 if (mb[mbo].status || aha1542->SCint[mbo])
1da177e4
LT
609 panic("Unable to find empty mailbox for aha1542.\n");
610
e98878f7
OZ
611 aha1542->SCint[mbo] = SCpnt; /* This will effectively prevent someone else from
612 screwing with this cdb. */
1da177e4 613
e98878f7 614 aha1542->aha1542_last_mbo_used = mbo;
1da177e4
LT
615 spin_unlock_irqrestore(&aha1542_lock, flags);
616
617#ifdef DEBUG
618 printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
619#endif
620
10be6250 621 any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */
1da177e4
LT
622
623 memset(&ccb[mbo], 0, sizeof(struct ccb));
624
625 ccb[mbo].cdblen = SCpnt->cmd_len;
626
627 direction = 0;
628 if (*cmd == READ_10 || *cmd == READ_6)
629 direction = 8;
630 else if (*cmd == WRITE_10 || *cmd == WRITE_6)
631 direction = 16;
632
633 memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
634
fc3fdfcc 635 if (bufflen) {
51cf2249 636 struct scatterlist *sg;
1da177e4
LT
637 struct chain *cptr;
638#ifdef DEBUG
639 unsigned char *ptr;
640#endif
fc3fdfcc 641 int i, sg_count = scsi_sg_count(SCpnt);
1da177e4 642 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
fc3fdfcc
BH
643 SCpnt->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
644 GFP_KERNEL | GFP_DMA);
1da177e4
LT
645 cptr = (struct chain *) SCpnt->host_scribble;
646 if (cptr == NULL) {
647 /* free the claimed mailbox slot */
e98878f7 648 aha1542->SCint[mbo] = NULL;
1da177e4
LT
649 return SCSI_MLQUEUE_HOST_BUSY;
650 }
fc3fdfcc 651 scsi_for_each_sg(SCpnt, sg, sg_count, i) {
10be6250
OZ
652 any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
653 + sg->offset);
51cf2249 654 any2scsi(cptr[i].datalen, sg->length);
1da177e4 655 };
fc3fdfcc 656 any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
10be6250 657 any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
1da177e4
LT
658#ifdef DEBUG
659 printk("cptr %x: ", cptr);
660 ptr = (unsigned char *) cptr;
661 for (i = 0; i < 18; i++)
662 printk("%02x ", ptr[i]);
663#endif
664 } else {
665 ccb[mbo].op = 0; /* SCSI Initiator Command */
666 SCpnt->host_scribble = NULL;
fc3fdfcc
BH
667 any2scsi(ccb[mbo].datalen, 0);
668 any2scsi(ccb[mbo].dataptr, 0);
1da177e4
LT
669 };
670 ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
671 ccb[mbo].rsalen = 16;
672 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
673 ccb[mbo].commlinkid = 0;
674
675#ifdef DEBUG
676 {
677 int i;
678 printk(KERN_DEBUG "aha1542_command: sending.. ");
679 for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
cb5b570c 680 printk("%02x ", ((u8 *) &ccb[mbo])[i]);
1da177e4
LT
681 };
682#endif
683
684 if (done) {
f232d538 685 DEB(printk("aha1542_queuecommand: now waiting for interrupt "));
1da177e4
LT
686 SCpnt->scsi_done = done;
687 mb[mbo].status = 1;
688 aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1); /* start scsi command */
1da177e4
LT
689 } else
690 printk("aha1542_queuecommand: done can't be NULL\n");
691
692 return 0;
693}
694
f281233d
JG
695static DEF_SCSI_QCMD(aha1542_queuecommand)
696
1da177e4
LT
697/* Initialize mailboxes */
698static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
699{
e98878f7 700 struct aha1542_hostdata *aha1542 = shost_priv(shpnt);
1da177e4 701 int i;
e98878f7
OZ
702 struct mailbox *mb = aha1542->mb;
703 struct ccb *ccb = aha1542->ccb;
1da177e4 704
cb5b570c 705 u8 cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
1da177e4 706
1da177e4
LT
707 for (i = 0; i < AHA1542_MAILBOXES; i++) {
708 mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
10be6250 709 any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
1da177e4
LT
710 };
711 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
10be6250 712 any2scsi((cmd + 2), isa_virt_to_bus(mb));
1da177e4
LT
713 aha1542_out(bse, cmd, 5);
714 WAIT(INTRFLAGS(bse), INTRMASK, HACC, 0);
715 while (0) {
716fail:
717 printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
718 }
719 aha1542_intr_reset(bse);
720}
721
643a7c43 722static int aha1542_getconfig(int base_io, unsigned char *irq_level, unsigned char *dma_chan, unsigned char *scsi_id)
1da177e4 723{
cb5b570c
OZ
724 u8 inquiry_cmd[] = {CMD_RETCONF};
725 u8 inquiry_result[3];
1da177e4
LT
726 int i;
727 i = inb(STATUS(base_io));
728 if (i & DF) {
729 i = inb(DATA(base_io));
730 };
731 aha1542_out(base_io, inquiry_cmd, 1);
732 aha1542_in(base_io, inquiry_result, 3);
733 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
734 while (0) {
735fail:
736 printk(KERN_ERR "aha1542_detect: query board settings\n");
737 }
738 aha1542_intr_reset(base_io);
739 switch (inquiry_result[0]) {
740 case 0x80:
741 *dma_chan = 7;
742 break;
743 case 0x40:
744 *dma_chan = 6;
745 break;
746 case 0x20:
747 *dma_chan = 5;
748 break;
749 case 0x01:
750 *dma_chan = 0;
751 break;
752 case 0:
753 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
754 Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
755 *dma_chan = 0xFF;
756 break;
757 default:
758 printk(KERN_ERR "Unable to determine Adaptec DMA priority. Disabling board\n");
759 return -1;
760 };
761 switch (inquiry_result[1]) {
762 case 0x40:
763 *irq_level = 15;
764 break;
765 case 0x20:
766 *irq_level = 14;
767 break;
768 case 0x8:
769 *irq_level = 12;
770 break;
771 case 0x4:
772 *irq_level = 11;
773 break;
774 case 0x2:
775 *irq_level = 10;
776 break;
777 case 0x1:
778 *irq_level = 9;
779 break;
780 default:
781 printk(KERN_ERR "Unable to determine Adaptec IRQ level. Disabling board\n");
782 return -1;
783 };
784 *scsi_id = inquiry_result[2] & 7;
785 return 0;
786}
787
788/* This function should only be called for 1542C boards - we can detect
789 the special firmware settings and unlock the board */
790
643a7c43 791static int aha1542_mbenable(int base)
1da177e4 792{
cb5b570c
OZ
793 static u8 mbenable_cmd[3];
794 static u8 mbenable_result[2];
1da177e4
LT
795 int retval;
796
797 retval = BIOS_TRANSLATION_6432;
798
799 mbenable_cmd[0] = CMD_EXTBIOS;
800 aha1542_out(base, mbenable_cmd, 1);
801 if (aha1542_in1(base, mbenable_result, 2))
802 return retval;
803 WAITd(INTRFLAGS(base), INTRMASK, HACC, 0, 100);
804 aha1542_intr_reset(base);
805
806 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
807 mbenable_cmd[0] = CMD_MBENABLE;
808 mbenable_cmd[1] = 0;
809 mbenable_cmd[2] = mbenable_result[1];
810
811 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
812 retval = BIOS_TRANSLATION_25563;
813
814 aha1542_out(base, mbenable_cmd, 3);
815 WAIT(INTRFLAGS(base), INTRMASK, HACC, 0);
816 };
817 while (0) {
818fail:
819 printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
820 }
821 aha1542_intr_reset(base);
822 return retval;
823}
824
825/* Query the board to find out if it is a 1542 or a 1740, or whatever. */
643a7c43 826static int aha1542_query(int base_io, int *transl)
1da177e4 827{
cb5b570c
OZ
828 u8 inquiry_cmd[] = {CMD_INQUIRY};
829 u8 inquiry_result[4];
1da177e4
LT
830 int i;
831 i = inb(STATUS(base_io));
832 if (i & DF) {
833 i = inb(DATA(base_io));
834 };
835 aha1542_out(base_io, inquiry_cmd, 1);
836 aha1542_in(base_io, inquiry_result, 4);
837 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
838 while (0) {
839fail:
840 printk(KERN_ERR "aha1542_detect: query card type\n");
841 }
842 aha1542_intr_reset(base_io);
843
844 *transl = BIOS_TRANSLATION_6432; /* Default case */
845
846 /* For an AHA1740 series board, we ignore the board since there is a
847 hardware bug which can lead to wrong blocks being returned if the board
848 is operating in the 1542 emulation mode. Since there is an extended mode
849 driver, we simply ignore the board and let the 1740 driver pick it up.
850 */
851
852 if (inquiry_result[0] == 0x43) {
853 printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
854 return 1;
855 };
856
857 /* Always call this - boards that do not support extended bios translation
858 will ignore the command, and we will set the proper default */
859
860 *transl = aha1542_mbenable(base_io);
861
862 return 0;
863}
864
865#ifndef MODULE
866static char *setup_str[MAXBOARDS] __initdata;
867static int setup_idx = 0;
868
869static void __init aha1542_setup(char *str, int *ints)
870{
871 const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
872 int setup_portbase;
873
874 if (setup_idx >= MAXBOARDS) {
875 printk(KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
876 printk(KERN_ERR " Entryline 1: %s\n", setup_str[0]);
877 printk(KERN_ERR " Entryline 2: %s\n", setup_str[1]);
878 printk(KERN_ERR " This line: %s\n", str);
879 return;
880 }
881 if (ints[0] < 1 || ints[0] > 4) {
882 printk(KERN_ERR "aha1542: %s\n", str);
883 printk(ahausage);
884 printk(KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
885 }
886 setup_called[setup_idx] = ints[0];
887 setup_str[setup_idx] = str;
888
889 setup_portbase = ints[0] >= 1 ? ints[1] : 0; /* Preserve the default value.. */
890 setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
891 setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
892 if (ints[0] >= 4)
893 {
894 int atbt = -1;
895 switch (ints[4]) {
896 case 5:
897 atbt = 0x00;
898 break;
899 case 6:
900 atbt = 0x04;
901 break;
902 case 7:
903 atbt = 0x01;
904 break;
905 case 8:
906 atbt = 0x02;
907 break;
908 case 10:
909 atbt = 0x03;
910 break;
911 default:
912 printk(KERN_ERR "aha1542: %s\n", str);
913 printk(ahausage);
914 printk(KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s. Using jumper defaults.\n");
915 break;
916 }
917 setup_dmaspeed[setup_idx] = atbt;
918 }
919 if (setup_portbase != 0)
920 bases[setup_idx] = setup_portbase;
921
922 ++setup_idx;
923}
924
925static int __init do_setup(char *str)
926{
927 int ints[5];
928
929 int count=setup_idx;
930
6391a113 931 get_options(str, ARRAY_SIZE(ints), ints);
1da177e4
LT
932 aha1542_setup(str,ints);
933
934 return count<setup_idx;
935}
936
937__setup("aha1542=",do_setup);
938#endif
939
940/* return non-zero on detection */
643a7c43 941static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
1da177e4
LT
942{
943 unsigned char dma_chan;
944 unsigned char irq_level;
945 unsigned char scsi_id;
946 unsigned long flags;
947 unsigned int base_io;
948 int trans;
949 struct Scsi_Host *shpnt = NULL;
e98878f7 950 struct aha1542_hostdata *aha1542;
1da177e4
LT
951
952 DEB(printk("aha1542_detect: \n"));
953
954 tpnt->proc_name = "aha1542";
955
1da177e4 956 if (bases[indx] != 0 && request_region(bases[indx], 4, "aha1542")) {
643a7c43 957 shpnt = scsi_host_alloc(tpnt,
1da177e4
LT
958 sizeof(struct aha1542_hostdata));
959
960 if(shpnt==NULL) {
961 release_region(bases[indx], 4);
643a7c43 962 return NULL;
1da177e4 963 }
e98878f7 964 aha1542 = shost_priv(shpnt);
1da177e4
LT
965 if (!aha1542_test_port(bases[indx], shpnt))
966 goto unregister;
967
1da177e4
LT
968 base_io = bases[indx];
969
970 /* Set the Bus on/off-times as not to ruin floppy performance */
971 {
cb5b570c
OZ
972 u8 oncmd[] = {CMD_BUSON_TIME, 7};
973 u8 offcmd[] = {CMD_BUSOFF_TIME, 5};
1da177e4
LT
974
975 if (setup_called[indx]) {
976 oncmd[1] = setup_buson[indx];
977 offcmd[1] = setup_busoff[indx];
978 }
979 aha1542_intr_reset(base_io);
980 aha1542_out(base_io, oncmd, 2);
981 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
982 aha1542_intr_reset(base_io);
983 aha1542_out(base_io, offcmd, 2);
984 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
985 if (setup_dmaspeed[indx] >= 0) {
cb5b570c 986 u8 dmacmd[] = {CMD_DMASPEED, 0};
1da177e4
LT
987 dmacmd[1] = setup_dmaspeed[indx];
988 aha1542_intr_reset(base_io);
989 aha1542_out(base_io, dmacmd, 2);
990 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
991 }
992 while (0) {
993fail:
994 printk(KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
995 }
996 aha1542_intr_reset(base_io);
997 }
998 if (aha1542_query(base_io, &trans))
999 goto unregister;
1000
1001 if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
1002 goto unregister;
1003
1004 printk(KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
1005 if (dma_chan != 0xFF)
1006 printk(", DMA priority %d", dma_chan);
1007 printk("\n");
1008
1da177e4
LT
1009 setup_mailboxes(base_io, shpnt);
1010
1da177e4
LT
1011 DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
1012 spin_lock_irqsave(&aha1542_lock, flags);
87c4d7bc
JG
1013 if (request_irq(irq_level, do_aha1542_intr_handle, 0,
1014 "aha1542", shpnt)) {
1da177e4
LT
1015 printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
1016 spin_unlock_irqrestore(&aha1542_lock, flags);
1017 goto unregister;
1018 }
1019 if (dma_chan != 0xFF) {
1020 if (request_dma(dma_chan, "aha1542")) {
1021 printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
87c4d7bc 1022 free_irq(irq_level, shpnt);
1da177e4
LT
1023 spin_unlock_irqrestore(&aha1542_lock, flags);
1024 goto unregister;
1025 }
1026 if (dma_chan == 0 || dma_chan >= 5) {
1027 set_dma_mode(dma_chan, DMA_MODE_CASCADE);
1028 enable_dma(dma_chan);
1029 }
1030 }
87c4d7bc 1031
1da177e4
LT
1032 shpnt->this_id = scsi_id;
1033 shpnt->unique_id = base_io;
1034 shpnt->io_port = base_io;
1035 shpnt->n_io_port = 4; /* Number of bytes of I/O space used */
1036 shpnt->dma_channel = dma_chan;
1037 shpnt->irq = irq_level;
e98878f7 1038 aha1542->bios_translation = trans;
1da177e4
LT
1039 if (trans == BIOS_TRANSLATION_25563)
1040 printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
e98878f7
OZ
1041 aha1542->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
1042 aha1542->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
1043 memset(aha1542->SCint, 0, sizeof(aha1542->SCint));
1da177e4 1044 spin_unlock_irqrestore(&aha1542_lock, flags);
1da177e4 1045
643a7c43
OZ
1046 if (scsi_add_host(shpnt, pdev)) {
1047 if (shpnt->dma_channel != 0xff)
1048 free_dma(shpnt->dma_channel);
1049 free_irq(irq_level, shpnt);
1050 goto unregister;
1da177e4
LT
1051 }
1052
643a7c43 1053 scsi_scan_host(shpnt);
1da177e4 1054
643a7c43 1055 return shpnt;
1da177e4
LT
1056unregister:
1057 release_region(bases[indx], 4);
643a7c43
OZ
1058 scsi_host_put(shpnt);
1059 return NULL;
1da177e4
LT
1060
1061 };
1062
643a7c43 1063 return NULL;
1da177e4
LT
1064}
1065
1066static int aha1542_release(struct Scsi_Host *shost)
1067{
643a7c43 1068 scsi_remove_host(shost);
1da177e4 1069 if (shost->irq)
87c4d7bc 1070 free_irq(shost->irq, shost);
1da177e4
LT
1071 if (shost->dma_channel != 0xff)
1072 free_dma(shost->dma_channel);
1073 if (shost->io_port && shost->n_io_port)
1074 release_region(shost->io_port, shost->n_io_port);
643a7c43 1075 scsi_host_put(shost);
1da177e4
LT
1076 return 0;
1077}
1078
1da177e4 1079
1da177e4
LT
1080/*
1081 * This is a device reset. This is handled by sending a special command
1082 * to the device.
1083 */
1084static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
1085{
e98878f7 1086 struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
1da177e4 1087 unsigned long flags;
e98878f7 1088 struct mailbox *mb = aha1542->mb;
cb5b570c
OZ
1089 u8 target = SCpnt->device->id;
1090 u8 lun = SCpnt->device->lun;
1da177e4 1091 int mbo;
e98878f7 1092 struct ccb *ccb = aha1542->ccb;
cb5b570c 1093 u8 ahacmd = CMD_START_SCSI;
1da177e4 1094
1da177e4 1095 spin_lock_irqsave(&aha1542_lock, flags);
e98878f7 1096 mbo = aha1542->aha1542_last_mbo_used + 1;
1da177e4
LT
1097 if (mbo >= AHA1542_MAILBOXES)
1098 mbo = 0;
1099
1100 do {
e98878f7 1101 if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
1da177e4
LT
1102 break;
1103 mbo++;
1104 if (mbo >= AHA1542_MAILBOXES)
1105 mbo = 0;
e98878f7 1106 } while (mbo != aha1542->aha1542_last_mbo_used);
1da177e4 1107
e98878f7 1108 if (mb[mbo].status || aha1542->SCint[mbo])
1da177e4
LT
1109 panic("Unable to find empty mailbox for aha1542.\n");
1110
e98878f7
OZ
1111 aha1542->SCint[mbo] = SCpnt; /* This will effectively
1112 prevent someone else from
1113 screwing with this cdb. */
1da177e4 1114
e98878f7 1115 aha1542->aha1542_last_mbo_used = mbo;
1da177e4
LT
1116 spin_unlock_irqrestore(&aha1542_lock, flags);
1117
10be6250 1118 any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */
1da177e4
LT
1119
1120 memset(&ccb[mbo], 0, sizeof(struct ccb));
1121
1122 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
1123
1124 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
1125
1126 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
1127 ccb[mbo].commlinkid = 0;
1128
1129 /*
1130 * Now tell the 1542 to flush all pending commands for this
1131 * target
1132 */
1133 aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1);
1134
017560fc
JG
1135 scmd_printk(KERN_WARNING, SCpnt,
1136 "Trying device reset for target\n");
1da177e4
LT
1137
1138 return SUCCESS;
1da177e4
LT
1139}
1140
1141static int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
1142{
e98878f7 1143 struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
1da177e4
LT
1144 int i;
1145
1146 /*
1147 * This does a scsi reset for all devices on the bus.
1148 * In principle, we could also reset the 1542 - should
1149 * we do this? Try this first, and we can add that later
1150 * if it turns out to be useful.
1151 */
1152 outb(SCRST, CONTROL(SCpnt->device->host->io_port));
1153
1154 /*
1155 * Wait for the thing to settle down a bit. Unfortunately
1156 * this is going to basically lock up the machine while we
1157 * wait for this to complete. To be 100% correct, we need to
1158 * check for timeout, and if we are doing something like this
1159 * we are pretty desperate anyways.
1160 */
1da177e4 1161 ssleep(4);
68b3aa7c 1162
1da177e4
LT
1163 spin_lock_irq(SCpnt->device->host->host_lock);
1164
1165 WAIT(STATUS(SCpnt->device->host->io_port),
1166 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1167
1168 /*
1169 * Now try to pick up the pieces. For all pending commands,
1170 * free any internal data structures, and basically clear things
1171 * out. We do not try and restart any commands or anything -
1172 * the strategy handler takes care of that crap.
1173 */
1174 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1175
1176 for (i = 0; i < AHA1542_MAILBOXES; i++) {
e98878f7 1177 if (aha1542->SCint[i] != NULL) {
1da177e4 1178 Scsi_Cmnd *SCtmp;
e98878f7 1179 SCtmp = aha1542->SCint[i];
1da177e4
LT
1180
1181
1182 if (SCtmp->device->soft_reset) {
1183 /*
1184 * If this device implements the soft reset option,
1185 * then it is still holding onto the command, and
1186 * may yet complete it. In this case, we don't
1187 * flush the data.
1188 */
1189 continue;
1190 }
c9475cb0
JJ
1191 kfree(SCtmp->host_scribble);
1192 SCtmp->host_scribble = NULL;
e98878f7
OZ
1193 aha1542->SCint[i] = NULL;
1194 aha1542->mb[i].status = 0;
1da177e4
LT
1195 }
1196 }
1197
68b3aa7c 1198 spin_unlock_irq(SCpnt->device->host->host_lock);
1da177e4
LT
1199 return SUCCESS;
1200
1201fail:
68b3aa7c 1202 spin_unlock_irq(SCpnt->device->host->host_lock);
1da177e4
LT
1203 return FAILED;
1204}
1205
1206static int aha1542_host_reset(Scsi_Cmnd * SCpnt)
1207{
e98878f7 1208 struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
1da177e4
LT
1209 int i;
1210
1211 /*
1212 * This does a scsi reset for all devices on the bus.
1213 * In principle, we could also reset the 1542 - should
1214 * we do this? Try this first, and we can add that later
1215 * if it turns out to be useful.
1216 */
1217 outb(HRST | SCRST, CONTROL(SCpnt->device->host->io_port));
1218
1219 /*
1220 * Wait for the thing to settle down a bit. Unfortunately
1221 * this is going to basically lock up the machine while we
1222 * wait for this to complete. To be 100% correct, we need to
1223 * check for timeout, and if we are doing something like this
1224 * we are pretty desperate anyways.
1225 */
1da177e4
LT
1226 ssleep(4);
1227 spin_lock_irq(SCpnt->device->host->host_lock);
1228
1229 WAIT(STATUS(SCpnt->device->host->io_port),
1230 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1231
1232 /*
1233 * We need to do this too before the 1542 can interact with
1234 * us again.
1235 */
1236 setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
1237
1238 /*
1239 * Now try to pick up the pieces. For all pending commands,
1240 * free any internal data structures, and basically clear things
1241 * out. We do not try and restart any commands or anything -
1242 * the strategy handler takes care of that crap.
1243 */
1244 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1245
1246 for (i = 0; i < AHA1542_MAILBOXES; i++) {
e98878f7 1247 if (aha1542->SCint[i] != NULL) {
1da177e4 1248 Scsi_Cmnd *SCtmp;
e98878f7 1249 SCtmp = aha1542->SCint[i];
1da177e4
LT
1250
1251 if (SCtmp->device->soft_reset) {
1252 /*
1253 * If this device implements the soft reset option,
1254 * then it is still holding onto the command, and
1255 * may yet complete it. In this case, we don't
1256 * flush the data.
1257 */
1258 continue;
1259 }
c9475cb0
JJ
1260 kfree(SCtmp->host_scribble);
1261 SCtmp->host_scribble = NULL;
e98878f7
OZ
1262 aha1542->SCint[i] = NULL;
1263 aha1542->mb[i].status = 0;
1da177e4
LT
1264 }
1265 }
1266
df0ae249 1267 spin_unlock_irq(SCpnt->device->host->host_lock);
1da177e4
LT
1268 return SUCCESS;
1269
1270fail:
df0ae249 1271 spin_unlock_irq(SCpnt->device->host->host_lock);
1da177e4
LT
1272 return FAILED;
1273}
1274
1da177e4
LT
1275static int aha1542_biosparam(struct scsi_device *sdev,
1276 struct block_device *bdev, sector_t capacity, int *ip)
1277{
e98878f7 1278 struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
1da177e4
LT
1279 int translation_algorithm;
1280 int size = capacity;
1281
e98878f7 1282 translation_algorithm = aha1542->bios_translation;
1da177e4
LT
1283
1284 if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
1285 /* Please verify that this is the same as what DOS returns */
1286 ip[0] = 255;
1287 ip[1] = 63;
1288 ip[2] = size / 255 / 63;
1289 } else {
1290 ip[0] = 64;
1291 ip[1] = 32;
1292 ip[2] = size >> 11;
1293 }
1294
1295 return 0;
1296}
1297MODULE_LICENSE("GPL");
1298
d0be4a7d 1299static struct scsi_host_template driver_template = {
643a7c43 1300 .module = THIS_MODULE,
1da177e4
LT
1301 .proc_name = "aha1542",
1302 .name = "Adaptec 1542",
1da177e4 1303 .queuecommand = aha1542_queuecommand,
1da177e4
LT
1304 .eh_device_reset_handler= aha1542_dev_reset,
1305 .eh_bus_reset_handler = aha1542_bus_reset,
1306 .eh_host_reset_handler = aha1542_host_reset,
1307 .bios_param = aha1542_biosparam,
1308 .can_queue = AHA1542_MAILBOXES,
1309 .this_id = 7,
10be6250
OZ
1310 .sg_tablesize = 16,
1311 .cmd_per_lun = 1,
1da177e4
LT
1312 .unchecked_isa_dma = 1,
1313 .use_clustering = ENABLE_CLUSTERING,
1314};
643a7c43
OZ
1315
1316static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1317{
1318 struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1319
1320 if (!sh)
1321 return 0;
1322
1323 dev_set_drvdata(pdev, sh);
1324 return 1;
1325}
1326
1327static int aha1542_isa_remove(struct device *pdev,
1328 unsigned int ndev)
1329{
1330 aha1542_release(dev_get_drvdata(pdev));
1331 dev_set_drvdata(pdev, NULL);
1332 return 0;
1333}
1334
1335static struct isa_driver aha1542_isa_driver = {
1336 .match = aha1542_isa_match,
1337 .remove = aha1542_isa_remove,
1338 .driver = {
1339 .name = "aha1542"
1340 },
1341};
1342static int isa_registered;
1343
1344#ifdef CONFIG_PNP
1345static struct pnp_device_id aha1542_pnp_ids[] = {
1346 { .id = "ADP1542" },
1347 { .id = "" }
1348};
1349MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1350
1351static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1352{
1353 int indx;
1354 struct Scsi_Host *sh;
1355
1356 for (indx = 0; indx < ARRAY_SIZE(bases); indx++) {
1357 if (bases[indx])
1358 continue;
1359
1360 if (pnp_activate_dev(pdev) < 0)
1361 continue;
1362
1363 bases[indx] = pnp_port_start(pdev, 0);
1364
1365 /* The card can be queried for its DMA, we have
1366 the DMA set up that is enough */
1367
1368 printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
1369 }
1370
1371 sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1372 if (!sh)
1373 return -ENODEV;
1374
1375 pnp_set_drvdata(pdev, sh);
1376 return 0;
1377}
1378
1379static void aha1542_pnp_remove(struct pnp_dev *pdev)
1380{
1381 aha1542_release(pnp_get_drvdata(pdev));
1382 pnp_set_drvdata(pdev, NULL);
1383}
1384
1385static struct pnp_driver aha1542_pnp_driver = {
1386 .name = "aha1542",
1387 .id_table = aha1542_pnp_ids,
1388 .probe = aha1542_pnp_probe,
1389 .remove = aha1542_pnp_remove,
1390};
1391static int pnp_registered;
1392#endif /* CONFIG_PNP */
1393
1394static int __init aha1542_init(void)
1395{
1396 int ret = 0;
1397#ifdef MODULE
1398 int atbt = -1;
1399
1400 bases[0] = aha1542[0];
1401 setup_buson[0] = aha1542[1];
1402 setup_busoff[0] = aha1542[2];
1403
1404 switch (aha1542[3]) {
1405 case 5:
1406 atbt = 0x00;
1407 break;
1408 case 6:
1409 atbt = 0x04;
1410 break;
1411 case 7:
1412 atbt = 0x01;
1413 break;
1414 case 8:
1415 atbt = 0x02;
1416 break;
1417 case 10:
1418 atbt = 0x03;
1419 break;
1420 };
1421 setup_dmaspeed[0] = atbt;
1422#endif
1423
1424#ifdef CONFIG_PNP
1425 if (isapnp) {
1426 ret = pnp_register_driver(&aha1542_pnp_driver);
1427 if (!ret)
1428 pnp_registered = 1;
1429 }
1430#endif
1431 ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1432 if (!ret)
1433 isa_registered = 1;
1434
1435#ifdef CONFIG_PNP
1436 if (pnp_registered)
1437 ret = 0;
1438#endif
1439 if (isa_registered)
1440 ret = 0;
1441
1442 return ret;
1443}
1444
1445static void __exit aha1542_exit(void)
1446{
1447#ifdef CONFIG_PNP
1448 if (pnp_registered)
1449 pnp_unregister_driver(&aha1542_pnp_driver);
1450#endif
1451 if (isa_registered)
1452 isa_unregister_driver(&aha1542_isa_driver);
1453}
1454
1455module_init(aha1542_init);
1456module_exit(aha1542_exit);