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