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