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