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