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