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