]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/aha1542.c
aha1542: Split aha1542_out
[mirror_ubuntu-artful-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. */
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);
188
1da177e4 189 return 0;
1da177e4
LT
190}
191
192/* Only used at boot time, so we do not need to worry about latency as much
193 here */
194
f8846be3 195static int aha1542_in(unsigned int base, u8 *cmdp, int len, int timeout)
1da177e4
LT
196{
197 unsigned long flags;
198
199 spin_lock_irqsave(&aha1542_lock, flags);
200 while (len--) {
f8846be3 201 if (!wait_mask(STATUS(base), DF, DF, 0, timeout))
2093bfa1 202 goto fail;
1da177e4
LT
203 *cmdp++ = inb(DATA(base));
204 }
205 spin_unlock_irqrestore(&aha1542_lock, flags);
206 return 0;
207fail:
208 spin_unlock_irqrestore(&aha1542_lock, flags);
f8846be3
OZ
209 if (timeout == 0)
210 printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
1da177e4
LT
211 return 1;
212}
213
214static int makecode(unsigned hosterr, unsigned scsierr)
215{
216 switch (hosterr) {
217 case 0x0:
218 case 0xa: /* Linked command complete without error and linked normally */
219 case 0xb: /* Linked command complete without error, interrupt generated */
220 hosterr = 0;
221 break;
222
223 case 0x11: /* Selection time out-The initiator selection or target
224 reselection was not complete within the SCSI Time out period */
225 hosterr = DID_TIME_OUT;
226 break;
227
228 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
229 than was allocated by the Data Length field or the sum of the
230 Scatter / Gather Data Length fields. */
231
232 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
233
234 case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
235 invalid. This usually indicates a software failure. */
236
237 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
238 This usually indicates a software failure. */
239
240 case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
241 of linked CCB's does not specify the same logical unit number as
242 the first. */
243 case 0x18: /* Invalid Target Direction received from Host-The direction of a
244 Target Mode CCB was invalid. */
245
246 case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
247 received to service data transfer between the same target LUN
248 and initiator SCSI ID in the same direction. */
249
250 case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
251 length segment or invalid segment list boundaries was received.
252 A CCB parameter was invalid. */
253 DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
254 hosterr = DID_ERROR; /* Couldn't find any better */
255 break;
256
257 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
258 phase sequence was requested by the target. The host adapter
259 will generate a SCSI Reset Condition, notifying the host with
260 a SCRD interrupt */
261 hosterr = DID_RESET;
262 break;
263 default:
264 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
265 break;
266 }
267 return scsierr | (hosterr << 16);
268}
269
643a7c43 270static int aha1542_test_port(int bse, struct Scsi_Host *shpnt)
1da177e4 271{
cb5b570c
OZ
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
0c2b6481 306 aha1542_outb(bse, CMD_INQUIRY);
1da177e4
LT
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 direction;
531 u8 *cmd = (u8 *) SCpnt->cmnd;
532 u8 target = SCpnt->device->id;
533 u8 lun = SCpnt->device->lun;
1da177e4 534 unsigned long flags;
fc3fdfcc 535 int bufflen = scsi_bufflen(SCpnt);
1da177e4 536 int mbo;
e98878f7
OZ
537 struct mailbox *mb = aha1542->mb;
538 struct ccb *ccb = aha1542->ccb;
1da177e4
LT
539
540 DEB(int i);
541
1da177e4
LT
542 DEB(if (target > 1) {
543 SCpnt->result = DID_TIME_OUT << 16;
544 done(SCpnt); return 0;
545 }
546 );
547
548 if (*cmd == REQUEST_SENSE) {
549 /* Don't do the command - we have the sense data already */
1da177e4
LT
550 SCpnt->result = 0;
551 done(SCpnt);
552 return 0;
553 }
554#ifdef DEBUG
555 if (*cmd == READ_10 || *cmd == WRITE_10)
556 i = xscsi2int(cmd + 2);
557 else if (*cmd == READ_6 || *cmd == WRITE_6)
558 i = scsi2int(cmd + 2);
559 else
560 i = -1;
561 if (done)
562 printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
563 else
564 printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
1da177e4
LT
565 printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
566 for (i = 0; i < SCpnt->cmd_len; i++)
567 printk("%02x ", cmd[i]);
568 printk("\n");
569 if (*cmd == WRITE_10 || *cmd == WRITE_6)
570 return 0; /* we are still testing, so *don't* write */
571#endif
572 /* Use the outgoing mailboxes in a round-robin fashion, because this
573 is how the host adapter will scan for them */
574
575 spin_lock_irqsave(&aha1542_lock, flags);
e98878f7 576 mbo = aha1542->aha1542_last_mbo_used + 1;
1da177e4
LT
577 if (mbo >= AHA1542_MAILBOXES)
578 mbo = 0;
579
580 do {
e98878f7 581 if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
1da177e4
LT
582 break;
583 mbo++;
584 if (mbo >= AHA1542_MAILBOXES)
585 mbo = 0;
e98878f7 586 } while (mbo != aha1542->aha1542_last_mbo_used);
1da177e4 587
e98878f7 588 if (mb[mbo].status || aha1542->SCint[mbo])
1da177e4
LT
589 panic("Unable to find empty mailbox for aha1542.\n");
590
e98878f7
OZ
591 aha1542->SCint[mbo] = SCpnt; /* This will effectively prevent someone else from
592 screwing with this cdb. */
1da177e4 593
e98878f7 594 aha1542->aha1542_last_mbo_used = mbo;
1da177e4
LT
595 spin_unlock_irqrestore(&aha1542_lock, flags);
596
597#ifdef DEBUG
598 printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
599#endif
600
10be6250 601 any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */
1da177e4
LT
602
603 memset(&ccb[mbo], 0, sizeof(struct ccb));
604
605 ccb[mbo].cdblen = SCpnt->cmd_len;
606
607 direction = 0;
608 if (*cmd == READ_10 || *cmd == READ_6)
609 direction = 8;
610 else if (*cmd == WRITE_10 || *cmd == WRITE_6)
611 direction = 16;
612
613 memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
614
fc3fdfcc 615 if (bufflen) {
51cf2249 616 struct scatterlist *sg;
1da177e4
LT
617 struct chain *cptr;
618#ifdef DEBUG
619 unsigned char *ptr;
620#endif
fc3fdfcc 621 int i, sg_count = scsi_sg_count(SCpnt);
1da177e4 622 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
fc3fdfcc
BH
623 SCpnt->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
624 GFP_KERNEL | GFP_DMA);
1da177e4
LT
625 cptr = (struct chain *) SCpnt->host_scribble;
626 if (cptr == NULL) {
627 /* free the claimed mailbox slot */
e98878f7 628 aha1542->SCint[mbo] = NULL;
1da177e4
LT
629 return SCSI_MLQUEUE_HOST_BUSY;
630 }
fc3fdfcc 631 scsi_for_each_sg(SCpnt, sg, sg_count, i) {
10be6250
OZ
632 any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
633 + sg->offset);
51cf2249 634 any2scsi(cptr[i].datalen, sg->length);
1da177e4 635 };
fc3fdfcc 636 any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
10be6250 637 any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
1da177e4
LT
638#ifdef DEBUG
639 printk("cptr %x: ", cptr);
640 ptr = (unsigned char *) cptr;
641 for (i = 0; i < 18; i++)
642 printk("%02x ", ptr[i]);
643#endif
644 } else {
645 ccb[mbo].op = 0; /* SCSI Initiator Command */
646 SCpnt->host_scribble = NULL;
fc3fdfcc
BH
647 any2scsi(ccb[mbo].datalen, 0);
648 any2scsi(ccb[mbo].dataptr, 0);
1da177e4
LT
649 };
650 ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
651 ccb[mbo].rsalen = 16;
652 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
653 ccb[mbo].commlinkid = 0;
654
655#ifdef DEBUG
656 {
657 int i;
658 printk(KERN_DEBUG "aha1542_command: sending.. ");
659 for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
cb5b570c 660 printk("%02x ", ((u8 *) &ccb[mbo])[i]);
1da177e4
LT
661 };
662#endif
663
664 if (done) {
f232d538 665 DEB(printk("aha1542_queuecommand: now waiting for interrupt "));
1da177e4
LT
666 SCpnt->scsi_done = done;
667 mb[mbo].status = 1;
0c2b6481 668 aha1542_outb(SCpnt->device->host->io_port, CMD_START_SCSI);
1da177e4
LT
669 } else
670 printk("aha1542_queuecommand: done can't be NULL\n");
671
672 return 0;
673}
674
f281233d
JG
675static DEF_SCSI_QCMD(aha1542_queuecommand)
676
1da177e4
LT
677/* Initialize mailboxes */
678static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
679{
e98878f7 680 struct aha1542_hostdata *aha1542 = shost_priv(shpnt);
1da177e4 681 int i;
e98878f7
OZ
682 struct mailbox *mb = aha1542->mb;
683 struct ccb *ccb = aha1542->ccb;
1da177e4 684
cb5b570c 685 u8 cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
1da177e4 686
1da177e4
LT
687 for (i = 0; i < AHA1542_MAILBOXES; i++) {
688 mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
10be6250 689 any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
1da177e4
LT
690 };
691 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
10be6250 692 any2scsi((cmd + 2), isa_virt_to_bus(mb));
1da177e4 693 aha1542_out(bse, cmd, 5);
2093bfa1
OZ
694 if (!wait_mask(INTRFLAGS(bse), INTRMASK, HACC, 0, 0))
695 goto fail;
1da177e4
LT
696 while (0) {
697fail:
698 printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
699 }
700 aha1542_intr_reset(bse);
701}
702
643a7c43 703static int aha1542_getconfig(int base_io, unsigned char *irq_level, unsigned char *dma_chan, unsigned char *scsi_id)
1da177e4 704{
cb5b570c 705 u8 inquiry_result[3];
1da177e4
LT
706 int i;
707 i = inb(STATUS(base_io));
708 if (i & DF) {
709 i = inb(DATA(base_io));
710 };
0c2b6481 711 aha1542_outb(base_io, CMD_RETCONF);
f8846be3 712 aha1542_in(base_io, inquiry_result, 3, 0);
2093bfa1
OZ
713 if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
714 goto fail;
1da177e4
LT
715 while (0) {
716fail:
717 printk(KERN_ERR "aha1542_detect: query board settings\n");
718 }
719 aha1542_intr_reset(base_io);
720 switch (inquiry_result[0]) {
721 case 0x80:
722 *dma_chan = 7;
723 break;
724 case 0x40:
725 *dma_chan = 6;
726 break;
727 case 0x20:
728 *dma_chan = 5;
729 break;
730 case 0x01:
731 *dma_chan = 0;
732 break;
733 case 0:
734 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
735 Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
736 *dma_chan = 0xFF;
737 break;
738 default:
739 printk(KERN_ERR "Unable to determine Adaptec DMA priority. Disabling board\n");
740 return -1;
741 };
742 switch (inquiry_result[1]) {
743 case 0x40:
744 *irq_level = 15;
745 break;
746 case 0x20:
747 *irq_level = 14;
748 break;
749 case 0x8:
750 *irq_level = 12;
751 break;
752 case 0x4:
753 *irq_level = 11;
754 break;
755 case 0x2:
756 *irq_level = 10;
757 break;
758 case 0x1:
759 *irq_level = 9;
760 break;
761 default:
762 printk(KERN_ERR "Unable to determine Adaptec IRQ level. Disabling board\n");
763 return -1;
764 };
765 *scsi_id = inquiry_result[2] & 7;
766 return 0;
767}
768
769/* This function should only be called for 1542C boards - we can detect
770 the special firmware settings and unlock the board */
771
643a7c43 772static int aha1542_mbenable(int base)
1da177e4 773{
cb5b570c
OZ
774 static u8 mbenable_cmd[3];
775 static u8 mbenable_result[2];
1da177e4
LT
776 int retval;
777
778 retval = BIOS_TRANSLATION_6432;
779
0c2b6481 780 aha1542_outb(base, CMD_EXTBIOS);
f8846be3 781 if (aha1542_in(base, mbenable_result, 2, 100))
1da177e4 782 return retval;
2093bfa1
OZ
783 if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 100))
784 goto fail;
1da177e4
LT
785 aha1542_intr_reset(base);
786
787 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
788 mbenable_cmd[0] = CMD_MBENABLE;
789 mbenable_cmd[1] = 0;
790 mbenable_cmd[2] = mbenable_result[1];
791
792 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
793 retval = BIOS_TRANSLATION_25563;
794
795 aha1542_out(base, mbenable_cmd, 3);
2093bfa1
OZ
796 if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
797 goto fail;
1da177e4
LT
798 };
799 while (0) {
800fail:
801 printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
802 }
803 aha1542_intr_reset(base);
804 return retval;
805}
806
807/* Query the board to find out if it is a 1542 or a 1740, or whatever. */
643a7c43 808static int aha1542_query(int base_io, int *transl)
1da177e4 809{
cb5b570c 810 u8 inquiry_result[4];
1da177e4
LT
811 int i;
812 i = inb(STATUS(base_io));
813 if (i & DF) {
814 i = inb(DATA(base_io));
815 };
0c2b6481 816 aha1542_outb(base_io, CMD_INQUIRY);
f8846be3 817 aha1542_in(base_io, inquiry_result, 4, 0);
2093bfa1
OZ
818 if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
819 goto fail;
1da177e4
LT
820 while (0) {
821fail:
822 printk(KERN_ERR "aha1542_detect: query card type\n");
823 }
824 aha1542_intr_reset(base_io);
825
826 *transl = BIOS_TRANSLATION_6432; /* Default case */
827
828 /* For an AHA1740 series board, we ignore the board since there is a
829 hardware bug which can lead to wrong blocks being returned if the board
830 is operating in the 1542 emulation mode. Since there is an extended mode
831 driver, we simply ignore the board and let the 1740 driver pick it up.
832 */
833
834 if (inquiry_result[0] == 0x43) {
835 printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
836 return 1;
837 };
838
839 /* Always call this - boards that do not support extended bios translation
840 will ignore the command, and we will set the proper default */
841
842 *transl = aha1542_mbenable(base_io);
843
844 return 0;
845}
846
847#ifndef MODULE
848static char *setup_str[MAXBOARDS] __initdata;
849static int setup_idx = 0;
850
851static void __init aha1542_setup(char *str, int *ints)
852{
853 const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
854 int setup_portbase;
855
856 if (setup_idx >= MAXBOARDS) {
857 printk(KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
858 printk(KERN_ERR " Entryline 1: %s\n", setup_str[0]);
859 printk(KERN_ERR " Entryline 2: %s\n", setup_str[1]);
860 printk(KERN_ERR " This line: %s\n", str);
861 return;
862 }
863 if (ints[0] < 1 || ints[0] > 4) {
864 printk(KERN_ERR "aha1542: %s\n", str);
865 printk(ahausage);
866 printk(KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
867 }
868 setup_called[setup_idx] = ints[0];
869 setup_str[setup_idx] = str;
870
871 setup_portbase = ints[0] >= 1 ? ints[1] : 0; /* Preserve the default value.. */
872 setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
873 setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
874 if (ints[0] >= 4)
875 {
876 int atbt = -1;
877 switch (ints[4]) {
878 case 5:
879 atbt = 0x00;
880 break;
881 case 6:
882 atbt = 0x04;
883 break;
884 case 7:
885 atbt = 0x01;
886 break;
887 case 8:
888 atbt = 0x02;
889 break;
890 case 10:
891 atbt = 0x03;
892 break;
893 default:
894 printk(KERN_ERR "aha1542: %s\n", str);
895 printk(ahausage);
896 printk(KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s. Using jumper defaults.\n");
897 break;
898 }
899 setup_dmaspeed[setup_idx] = atbt;
900 }
901 if (setup_portbase != 0)
902 bases[setup_idx] = setup_portbase;
903
904 ++setup_idx;
905}
906
907static int __init do_setup(char *str)
908{
909 int ints[5];
910
911 int count=setup_idx;
912
6391a113 913 get_options(str, ARRAY_SIZE(ints), ints);
1da177e4
LT
914 aha1542_setup(str,ints);
915
916 return count<setup_idx;
917}
918
919__setup("aha1542=",do_setup);
920#endif
921
922/* return non-zero on detection */
643a7c43 923static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
1da177e4
LT
924{
925 unsigned char dma_chan;
926 unsigned char irq_level;
927 unsigned char scsi_id;
928 unsigned long flags;
929 unsigned int base_io;
930 int trans;
931 struct Scsi_Host *shpnt = NULL;
e98878f7 932 struct aha1542_hostdata *aha1542;
1da177e4
LT
933
934 DEB(printk("aha1542_detect: \n"));
935
936 tpnt->proc_name = "aha1542";
937
1da177e4 938 if (bases[indx] != 0 && request_region(bases[indx], 4, "aha1542")) {
643a7c43 939 shpnt = scsi_host_alloc(tpnt,
1da177e4
LT
940 sizeof(struct aha1542_hostdata));
941
942 if(shpnt==NULL) {
943 release_region(bases[indx], 4);
643a7c43 944 return NULL;
1da177e4 945 }
e98878f7 946 aha1542 = shost_priv(shpnt);
1da177e4
LT
947 if (!aha1542_test_port(bases[indx], shpnt))
948 goto unregister;
949
1da177e4
LT
950 base_io = bases[indx];
951
952 /* Set the Bus on/off-times as not to ruin floppy performance */
953 {
cb5b570c
OZ
954 u8 oncmd[] = {CMD_BUSON_TIME, 7};
955 u8 offcmd[] = {CMD_BUSOFF_TIME, 5};
1da177e4
LT
956
957 if (setup_called[indx]) {
958 oncmd[1] = setup_buson[indx];
959 offcmd[1] = setup_busoff[indx];
960 }
961 aha1542_intr_reset(base_io);
962 aha1542_out(base_io, oncmd, 2);
2093bfa1
OZ
963 if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
964 goto fail;
1da177e4
LT
965 aha1542_intr_reset(base_io);
966 aha1542_out(base_io, offcmd, 2);
2093bfa1
OZ
967 if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
968 goto fail;
1da177e4 969 if (setup_dmaspeed[indx] >= 0) {
cb5b570c 970 u8 dmacmd[] = {CMD_DMASPEED, 0};
1da177e4
LT
971 dmacmd[1] = setup_dmaspeed[indx];
972 aha1542_intr_reset(base_io);
973 aha1542_out(base_io, dmacmd, 2);
2093bfa1
OZ
974 if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
975 goto fail;
1da177e4
LT
976 }
977 while (0) {
978fail:
979 printk(KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
980 }
981 aha1542_intr_reset(base_io);
982 }
983 if (aha1542_query(base_io, &trans))
984 goto unregister;
985
986 if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
987 goto unregister;
988
989 printk(KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
990 if (dma_chan != 0xFF)
991 printk(", DMA priority %d", dma_chan);
992 printk("\n");
993
1da177e4
LT
994 setup_mailboxes(base_io, shpnt);
995
1da177e4
LT
996 DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
997 spin_lock_irqsave(&aha1542_lock, flags);
87c4d7bc
JG
998 if (request_irq(irq_level, do_aha1542_intr_handle, 0,
999 "aha1542", shpnt)) {
1da177e4
LT
1000 printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
1001 spin_unlock_irqrestore(&aha1542_lock, flags);
1002 goto unregister;
1003 }
1004 if (dma_chan != 0xFF) {
1005 if (request_dma(dma_chan, "aha1542")) {
1006 printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
87c4d7bc 1007 free_irq(irq_level, shpnt);
1da177e4
LT
1008 spin_unlock_irqrestore(&aha1542_lock, flags);
1009 goto unregister;
1010 }
1011 if (dma_chan == 0 || dma_chan >= 5) {
1012 set_dma_mode(dma_chan, DMA_MODE_CASCADE);
1013 enable_dma(dma_chan);
1014 }
1015 }
87c4d7bc 1016
1da177e4
LT
1017 shpnt->this_id = scsi_id;
1018 shpnt->unique_id = base_io;
1019 shpnt->io_port = base_io;
1020 shpnt->n_io_port = 4; /* Number of bytes of I/O space used */
1021 shpnt->dma_channel = dma_chan;
1022 shpnt->irq = irq_level;
e98878f7 1023 aha1542->bios_translation = trans;
1da177e4
LT
1024 if (trans == BIOS_TRANSLATION_25563)
1025 printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
e98878f7
OZ
1026 aha1542->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
1027 aha1542->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
1028 memset(aha1542->SCint, 0, sizeof(aha1542->SCint));
1da177e4 1029 spin_unlock_irqrestore(&aha1542_lock, flags);
1da177e4 1030
643a7c43
OZ
1031 if (scsi_add_host(shpnt, pdev)) {
1032 if (shpnt->dma_channel != 0xff)
1033 free_dma(shpnt->dma_channel);
1034 free_irq(irq_level, shpnt);
1035 goto unregister;
1da177e4
LT
1036 }
1037
643a7c43 1038 scsi_scan_host(shpnt);
1da177e4 1039
643a7c43 1040 return shpnt;
1da177e4
LT
1041unregister:
1042 release_region(bases[indx], 4);
643a7c43
OZ
1043 scsi_host_put(shpnt);
1044 return NULL;
1da177e4
LT
1045
1046 };
1047
643a7c43 1048 return NULL;
1da177e4
LT
1049}
1050
1051static int aha1542_release(struct Scsi_Host *shost)
1052{
643a7c43 1053 scsi_remove_host(shost);
1da177e4 1054 if (shost->irq)
87c4d7bc 1055 free_irq(shost->irq, shost);
1da177e4
LT
1056 if (shost->dma_channel != 0xff)
1057 free_dma(shost->dma_channel);
1058 if (shost->io_port && shost->n_io_port)
1059 release_region(shost->io_port, shost->n_io_port);
643a7c43 1060 scsi_host_put(shost);
1da177e4
LT
1061 return 0;
1062}
1063
1da177e4 1064
1da177e4
LT
1065/*
1066 * This is a device reset. This is handled by sending a special command
1067 * to the device.
1068 */
1069static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
1070{
e98878f7 1071 struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
1da177e4 1072 unsigned long flags;
e98878f7 1073 struct mailbox *mb = aha1542->mb;
cb5b570c
OZ
1074 u8 target = SCpnt->device->id;
1075 u8 lun = SCpnt->device->lun;
1da177e4 1076 int mbo;
e98878f7 1077 struct ccb *ccb = aha1542->ccb;
1da177e4 1078
1da177e4 1079 spin_lock_irqsave(&aha1542_lock, flags);
e98878f7 1080 mbo = aha1542->aha1542_last_mbo_used + 1;
1da177e4
LT
1081 if (mbo >= AHA1542_MAILBOXES)
1082 mbo = 0;
1083
1084 do {
e98878f7 1085 if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
1da177e4
LT
1086 break;
1087 mbo++;
1088 if (mbo >= AHA1542_MAILBOXES)
1089 mbo = 0;
e98878f7 1090 } while (mbo != aha1542->aha1542_last_mbo_used);
1da177e4 1091
e98878f7 1092 if (mb[mbo].status || aha1542->SCint[mbo])
1da177e4
LT
1093 panic("Unable to find empty mailbox for aha1542.\n");
1094
e98878f7
OZ
1095 aha1542->SCint[mbo] = SCpnt; /* This will effectively
1096 prevent someone else from
1097 screwing with this cdb. */
1da177e4 1098
e98878f7 1099 aha1542->aha1542_last_mbo_used = mbo;
1da177e4
LT
1100 spin_unlock_irqrestore(&aha1542_lock, flags);
1101
10be6250 1102 any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */
1da177e4
LT
1103
1104 memset(&ccb[mbo], 0, sizeof(struct ccb));
1105
1106 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
1107
1108 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
1109
1110 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
1111 ccb[mbo].commlinkid = 0;
1112
1113 /*
1114 * Now tell the 1542 to flush all pending commands for this
1115 * target
1116 */
0c2b6481 1117 aha1542_outb(SCpnt->device->host->io_port, CMD_START_SCSI);
1da177e4 1118
017560fc
JG
1119 scmd_printk(KERN_WARNING, SCpnt,
1120 "Trying device reset for target\n");
1da177e4
LT
1121
1122 return SUCCESS;
1da177e4
LT
1123}
1124
1125static int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
1126{
e98878f7 1127 struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
1da177e4
LT
1128 int i;
1129
1130 /*
1131 * This does a scsi reset for all devices on the bus.
1132 * In principle, we could also reset the 1542 - should
1133 * we do this? Try this first, and we can add that later
1134 * if it turns out to be useful.
1135 */
1136 outb(SCRST, CONTROL(SCpnt->device->host->io_port));
1137
1138 /*
1139 * Wait for the thing to settle down a bit. Unfortunately
1140 * this is going to basically lock up the machine while we
1141 * wait for this to complete. To be 100% correct, we need to
1142 * check for timeout, and if we are doing something like this
1143 * we are pretty desperate anyways.
1144 */
1da177e4 1145 ssleep(4);
68b3aa7c 1146
1da177e4
LT
1147 spin_lock_irq(SCpnt->device->host->host_lock);
1148
2093bfa1
OZ
1149 if (!wait_mask(STATUS(SCpnt->device->host->io_port),
1150 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
1151 goto fail;
1da177e4
LT
1152
1153 /*
1154 * Now try to pick up the pieces. For all pending commands,
1155 * free any internal data structures, and basically clear things
1156 * out. We do not try and restart any commands or anything -
1157 * the strategy handler takes care of that crap.
1158 */
1159 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1160
1161 for (i = 0; i < AHA1542_MAILBOXES; i++) {
e98878f7 1162 if (aha1542->SCint[i] != NULL) {
1da177e4 1163 Scsi_Cmnd *SCtmp;
e98878f7 1164 SCtmp = aha1542->SCint[i];
1da177e4
LT
1165
1166
1167 if (SCtmp->device->soft_reset) {
1168 /*
1169 * If this device implements the soft reset option,
1170 * then it is still holding onto the command, and
1171 * may yet complete it. In this case, we don't
1172 * flush the data.
1173 */
1174 continue;
1175 }
c9475cb0
JJ
1176 kfree(SCtmp->host_scribble);
1177 SCtmp->host_scribble = NULL;
e98878f7
OZ
1178 aha1542->SCint[i] = NULL;
1179 aha1542->mb[i].status = 0;
1da177e4
LT
1180 }
1181 }
1182
68b3aa7c 1183 spin_unlock_irq(SCpnt->device->host->host_lock);
1da177e4
LT
1184 return SUCCESS;
1185
1186fail:
68b3aa7c 1187 spin_unlock_irq(SCpnt->device->host->host_lock);
1da177e4
LT
1188 return FAILED;
1189}
1190
1191static int aha1542_host_reset(Scsi_Cmnd * SCpnt)
1192{
e98878f7 1193 struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
1da177e4
LT
1194 int i;
1195
1196 /*
1197 * This does a scsi reset for all devices on the bus.
1198 * In principle, we could also reset the 1542 - should
1199 * we do this? Try this first, and we can add that later
1200 * if it turns out to be useful.
1201 */
1202 outb(HRST | SCRST, CONTROL(SCpnt->device->host->io_port));
1203
1204 /*
1205 * Wait for the thing to settle down a bit. Unfortunately
1206 * this is going to basically lock up the machine while we
1207 * wait for this to complete. To be 100% correct, we need to
1208 * check for timeout, and if we are doing something like this
1209 * we are pretty desperate anyways.
1210 */
1da177e4
LT
1211 ssleep(4);
1212 spin_lock_irq(SCpnt->device->host->host_lock);
1213
2093bfa1
OZ
1214 if (!wait_mask(STATUS(SCpnt->device->host->io_port),
1215 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
1216 goto fail;
1da177e4
LT
1217
1218 /*
1219 * We need to do this too before the 1542 can interact with
1220 * us again.
1221 */
1222 setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
1223
1224 /*
1225 * Now try to pick up the pieces. For all pending commands,
1226 * free any internal data structures, and basically clear things
1227 * out. We do not try and restart any commands or anything -
1228 * the strategy handler takes care of that crap.
1229 */
1230 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1231
1232 for (i = 0; i < AHA1542_MAILBOXES; i++) {
e98878f7 1233 if (aha1542->SCint[i] != NULL) {
1da177e4 1234 Scsi_Cmnd *SCtmp;
e98878f7 1235 SCtmp = aha1542->SCint[i];
1da177e4
LT
1236
1237 if (SCtmp->device->soft_reset) {
1238 /*
1239 * If this device implements the soft reset option,
1240 * then it is still holding onto the command, and
1241 * may yet complete it. In this case, we don't
1242 * flush the data.
1243 */
1244 continue;
1245 }
c9475cb0
JJ
1246 kfree(SCtmp->host_scribble);
1247 SCtmp->host_scribble = NULL;
e98878f7
OZ
1248 aha1542->SCint[i] = NULL;
1249 aha1542->mb[i].status = 0;
1da177e4
LT
1250 }
1251 }
1252
df0ae249 1253 spin_unlock_irq(SCpnt->device->host->host_lock);
1da177e4
LT
1254 return SUCCESS;
1255
1256fail:
df0ae249 1257 spin_unlock_irq(SCpnt->device->host->host_lock);
1da177e4
LT
1258 return FAILED;
1259}
1260
1da177e4
LT
1261static int aha1542_biosparam(struct scsi_device *sdev,
1262 struct block_device *bdev, sector_t capacity, int *ip)
1263{
e98878f7 1264 struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
1da177e4
LT
1265 int translation_algorithm;
1266 int size = capacity;
1267
e98878f7 1268 translation_algorithm = aha1542->bios_translation;
1da177e4
LT
1269
1270 if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
1271 /* Please verify that this is the same as what DOS returns */
1272 ip[0] = 255;
1273 ip[1] = 63;
1274 ip[2] = size / 255 / 63;
1275 } else {
1276 ip[0] = 64;
1277 ip[1] = 32;
1278 ip[2] = size >> 11;
1279 }
1280
1281 return 0;
1282}
1283MODULE_LICENSE("GPL");
1284
d0be4a7d 1285static struct scsi_host_template driver_template = {
643a7c43 1286 .module = THIS_MODULE,
1da177e4
LT
1287 .proc_name = "aha1542",
1288 .name = "Adaptec 1542",
1da177e4 1289 .queuecommand = aha1542_queuecommand,
1da177e4
LT
1290 .eh_device_reset_handler= aha1542_dev_reset,
1291 .eh_bus_reset_handler = aha1542_bus_reset,
1292 .eh_host_reset_handler = aha1542_host_reset,
1293 .bios_param = aha1542_biosparam,
1294 .can_queue = AHA1542_MAILBOXES,
1295 .this_id = 7,
10be6250
OZ
1296 .sg_tablesize = 16,
1297 .cmd_per_lun = 1,
1da177e4
LT
1298 .unchecked_isa_dma = 1,
1299 .use_clustering = ENABLE_CLUSTERING,
1300};
643a7c43
OZ
1301
1302static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1303{
1304 struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1305
1306 if (!sh)
1307 return 0;
1308
1309 dev_set_drvdata(pdev, sh);
1310 return 1;
1311}
1312
1313static int aha1542_isa_remove(struct device *pdev,
1314 unsigned int ndev)
1315{
1316 aha1542_release(dev_get_drvdata(pdev));
1317 dev_set_drvdata(pdev, NULL);
1318 return 0;
1319}
1320
1321static struct isa_driver aha1542_isa_driver = {
1322 .match = aha1542_isa_match,
1323 .remove = aha1542_isa_remove,
1324 .driver = {
1325 .name = "aha1542"
1326 },
1327};
1328static int isa_registered;
1329
1330#ifdef CONFIG_PNP
1331static struct pnp_device_id aha1542_pnp_ids[] = {
1332 { .id = "ADP1542" },
1333 { .id = "" }
1334};
1335MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1336
1337static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1338{
1339 int indx;
1340 struct Scsi_Host *sh;
1341
1342 for (indx = 0; indx < ARRAY_SIZE(bases); indx++) {
1343 if (bases[indx])
1344 continue;
1345
1346 if (pnp_activate_dev(pdev) < 0)
1347 continue;
1348
1349 bases[indx] = pnp_port_start(pdev, 0);
1350
1351 /* The card can be queried for its DMA, we have
1352 the DMA set up that is enough */
1353
1354 printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
1355 }
1356
1357 sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1358 if (!sh)
1359 return -ENODEV;
1360
1361 pnp_set_drvdata(pdev, sh);
1362 return 0;
1363}
1364
1365static void aha1542_pnp_remove(struct pnp_dev *pdev)
1366{
1367 aha1542_release(pnp_get_drvdata(pdev));
1368 pnp_set_drvdata(pdev, NULL);
1369}
1370
1371static struct pnp_driver aha1542_pnp_driver = {
1372 .name = "aha1542",
1373 .id_table = aha1542_pnp_ids,
1374 .probe = aha1542_pnp_probe,
1375 .remove = aha1542_pnp_remove,
1376};
1377static int pnp_registered;
1378#endif /* CONFIG_PNP */
1379
1380static int __init aha1542_init(void)
1381{
1382 int ret = 0;
1383#ifdef MODULE
1384 int atbt = -1;
1385
1386 bases[0] = aha1542[0];
1387 setup_buson[0] = aha1542[1];
1388 setup_busoff[0] = aha1542[2];
1389
1390 switch (aha1542[3]) {
1391 case 5:
1392 atbt = 0x00;
1393 break;
1394 case 6:
1395 atbt = 0x04;
1396 break;
1397 case 7:
1398 atbt = 0x01;
1399 break;
1400 case 8:
1401 atbt = 0x02;
1402 break;
1403 case 10:
1404 atbt = 0x03;
1405 break;
1406 };
1407 setup_dmaspeed[0] = atbt;
1408#endif
1409
1410#ifdef CONFIG_PNP
1411 if (isapnp) {
1412 ret = pnp_register_driver(&aha1542_pnp_driver);
1413 if (!ret)
1414 pnp_registered = 1;
1415 }
1416#endif
1417 ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1418 if (!ret)
1419 isa_registered = 1;
1420
1421#ifdef CONFIG_PNP
1422 if (pnp_registered)
1423 ret = 0;
1424#endif
1425 if (isa_registered)
1426 ret = 0;
1427
1428 return ret;
1429}
1430
1431static void __exit aha1542_exit(void)
1432{
1433#ifdef CONFIG_PNP
1434 if (pnp_registered)
1435 pnp_unregister_driver(&aha1542_pnp_driver);
1436#endif
1437 if (isa_registered)
1438 isa_unregister_driver(&aha1542_isa_driver);
1439}
1440
1441module_init(aha1542_init);
1442module_exit(aha1542_exit);