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