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