[POWERPC] Only offer CONFIG_BRIQ_PANEL if CONFIG_PPC_CHRP is enabled
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / aha1542.c
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
24 * Modified by Adam Fritzler <mid@auk.cx>
25 * Added proper detection of the AHA-1640 (MCA version of AHA-1540)
26 */
27
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>
38 #include <linux/pci.h>
39 #include <linux/isapnp.h>
40 #include <linux/blkdev.h>
41 #include <linux/mca.h>
42 #include <linux/mca-legacy.h>
43
44 #include <asm/dma.h>
45 #include <asm/system.h>
46 #include <asm/io.h>
47
48 #include "scsi.h"
49 #include <scsi/scsi_host.h>
50 #include "aha1542.h"
51
52 #define SCSI_BUF_PA(address) isa_virt_to_bus(address)
53 #define SCSI_SG_PA(sgent) (isa_page_to_bus((sgent)->page) + (sgent)->offset)
54
55 static void BAD_DMA(void *address, unsigned int length)
56 {
57 printk(KERN_CRIT "buf vaddress %p paddress 0x%lx length %d\n",
58 address,
59 SCSI_BUF_PA(address),
60 length);
61 panic("Buffer at physical address > 16Mb used for aha1542");
62 }
63
64 static void BAD_SG_DMA(Scsi_Cmnd * SCpnt,
65 struct scatterlist *sgpnt,
66 int nseg,
67 int badseg)
68 {
69 printk(KERN_CRIT "sgpnt[%d:%d] page %p/0x%llx length %u\n",
70 badseg, nseg,
71 page_address(sgpnt[badseg].page) + sgpnt[badseg].offset,
72 (unsigned long long)SCSI_SG_PA(&sgpnt[badseg]),
73 sgpnt[badseg].length);
74
75 /*
76 * Not safe to continue.
77 */
78 panic("Buffer at physical address > 16Mb used for aha1542");
79 }
80
81 #include<linux/stat.h>
82
83 #ifdef DEBUG
84 #define DEB(x) x
85 #else
86 #define DEB(x)
87 #endif
88
89 /*
90 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 $";
91 */
92
93 /* The adaptec can be configured for quite a number of addresses, but
94 I generally do not want the card poking around at random. We allow
95 two addresses - this allows people to use the Adaptec with a Midi
96 card, which also used 0x330 -- can be overridden with LILO! */
97
98 #define MAXBOARDS 4 /* Increase this and the sizes of the
99 arrays below, if you need more.. */
100
101 /* Boards 3,4 slots are reserved for ISAPnP/MCA scans */
102
103 static unsigned int bases[MAXBOARDS] __initdata = {0x330, 0x334, 0, 0};
104
105 /* set by aha1542_setup according to the command line; they also may
106 be marked __initdata, but require zero initializers then */
107
108 static int setup_called[MAXBOARDS];
109 static int setup_buson[MAXBOARDS];
110 static int setup_busoff[MAXBOARDS];
111 static int setup_dmaspeed[MAXBOARDS] __initdata = { -1, -1, -1, -1 };
112
113 /*
114 * LILO/Module params: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]
115 *
116 * Where: <PORTBASE> is any of the valid AHA addresses:
117 * 0x130, 0x134, 0x230, 0x234, 0x330, 0x334
118 * <BUSON> is the time (in microsecs) that AHA spends on the AT-bus
119 * when transferring data. 1542A power-on default is 11us,
120 * valid values are in range: 2..15 (decimal)
121 * <BUSOFF> is the time that AHA spends OFF THE BUS after while
122 * it is transferring data (not to monopolize the bus).
123 * Power-on default is 4us, valid range: 1..64 microseconds.
124 * <DMASPEED> Default is jumper selected (1542A: on the J1),
125 * but experimenter can alter it with this.
126 * Valid values: 5, 6, 7, 8, 10 (MB/s)
127 * Factory default is 5 MB/s.
128 */
129
130 #if defined(MODULE)
131 static int isapnp = 0;
132 static int aha1542[] = {0x330, 11, 4, -1};
133 module_param_array(aha1542, int, NULL, 0);
134 module_param(isapnp, bool, 0);
135
136 static struct isapnp_device_id id_table[] __initdata = {
137 {
138 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
139 ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1542),
140 0
141 },
142 {0}
143 };
144
145 MODULE_DEVICE_TABLE(isapnp, id_table);
146
147 #else
148 static int isapnp = 1;
149 #endif
150
151 #define BIOS_TRANSLATION_1632 0 /* Used by some old 1542A boards */
152 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
153 #define BIOS_TRANSLATION_25563 2 /* Big disk case */
154
155 struct aha1542_hostdata {
156 /* This will effectively start both of them at the first mailbox */
157 int bios_translation; /* Mapping bios uses - for compatibility */
158 int aha1542_last_mbi_used;
159 int aha1542_last_mbo_used;
160 Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
161 struct mailbox mb[2 * AHA1542_MAILBOXES];
162 struct ccb ccb[AHA1542_MAILBOXES];
163 };
164
165 #define HOSTDATA(host) ((struct aha1542_hostdata *) &host->hostdata)
166
167 static struct Scsi_Host *aha_host[7]; /* One for each IRQ level (9-15) */
168
169 static DEFINE_SPINLOCK(aha1542_lock);
170
171
172
173 #define WAITnexttimeout 3000000
174
175 static void setup_mailboxes(int base_io, struct Scsi_Host *shpnt);
176 static int aha1542_restart(struct Scsi_Host *shost);
177 static void aha1542_intr_handle(struct Scsi_Host *shost, void *dev_id, struct pt_regs *regs);
178 static irqreturn_t do_aha1542_intr_handle(int irq, void *dev_id,
179 struct pt_regs *regs);
180
181 #define aha1542_intr_reset(base) outb(IRST, CONTROL(base))
182
183 #define WAIT(port, mask, allof, noneof) \
184 { register int WAITbits; \
185 register int WAITtimeout = WAITnexttimeout; \
186 while (1) { \
187 WAITbits = inb(port) & (mask); \
188 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
189 break; \
190 if (--WAITtimeout == 0) goto fail; \
191 } \
192 }
193
194 /* Similar to WAIT, except we use the udelay call to regulate the
195 amount of time we wait. */
196 #define WAITd(port, mask, allof, noneof, timeout) \
197 { register int WAITbits; \
198 register int WAITtimeout = timeout; \
199 while (1) { \
200 WAITbits = inb(port) & (mask); \
201 if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
202 break; \
203 mdelay(1); \
204 if (--WAITtimeout == 0) goto fail; \
205 } \
206 }
207
208 static void aha1542_stat(void)
209 {
210 /* int s = inb(STATUS), i = inb(INTRFLAGS);
211 printk("status=%x intrflags=%x\n", s, i, WAITnexttimeout-WAITtimeout); */
212 }
213
214 /* This is a bit complicated, but we need to make sure that an interrupt
215 routine does not send something out while we are in the middle of this.
216 Fortunately, it is only at boot time that multi-byte messages
217 are ever sent. */
218 static int aha1542_out(unsigned int base, unchar * cmdp, int len)
219 {
220 unsigned long flags = 0;
221 int got_lock;
222
223 if (len == 1) {
224 got_lock = 0;
225 while (1 == 1) {
226 WAIT(STATUS(base), CDF, 0, CDF);
227 spin_lock_irqsave(&aha1542_lock, flags);
228 if (inb(STATUS(base)) & CDF) {
229 spin_unlock_irqrestore(&aha1542_lock, flags);
230 continue;
231 }
232 outb(*cmdp, DATA(base));
233 spin_unlock_irqrestore(&aha1542_lock, flags);
234 return 0;
235 }
236 } else {
237 spin_lock_irqsave(&aha1542_lock, flags);
238 got_lock = 1;
239 while (len--) {
240 WAIT(STATUS(base), CDF, 0, CDF);
241 outb(*cmdp++, DATA(base));
242 }
243 spin_unlock_irqrestore(&aha1542_lock, flags);
244 }
245 return 0;
246 fail:
247 if (got_lock)
248 spin_unlock_irqrestore(&aha1542_lock, flags);
249 printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
250 aha1542_stat();
251 return 1;
252 }
253
254 /* Only used at boot time, so we do not need to worry about latency as much
255 here */
256
257 static int __init aha1542_in(unsigned int base, unchar * cmdp, int len)
258 {
259 unsigned long flags;
260
261 spin_lock_irqsave(&aha1542_lock, flags);
262 while (len--) {
263 WAIT(STATUS(base), DF, DF, 0);
264 *cmdp++ = inb(DATA(base));
265 }
266 spin_unlock_irqrestore(&aha1542_lock, flags);
267 return 0;
268 fail:
269 spin_unlock_irqrestore(&aha1542_lock, flags);
270 printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
271 aha1542_stat();
272 return 1;
273 }
274
275 /* Similar to aha1542_in, except that we wait a very short period of time.
276 We use this if we know the board is alive and awake, but we are not sure
277 if the board will respond to the command we are about to send or not */
278 static int __init aha1542_in1(unsigned int base, unchar * cmdp, int len)
279 {
280 unsigned long flags;
281
282 spin_lock_irqsave(&aha1542_lock, flags);
283 while (len--) {
284 WAITd(STATUS(base), DF, DF, 0, 100);
285 *cmdp++ = inb(DATA(base));
286 }
287 spin_unlock_irqrestore(&aha1542_lock, flags);
288 return 0;
289 fail:
290 spin_unlock_irqrestore(&aha1542_lock, flags);
291 return 1;
292 }
293
294 static int makecode(unsigned hosterr, unsigned scsierr)
295 {
296 switch (hosterr) {
297 case 0x0:
298 case 0xa: /* Linked command complete without error and linked normally */
299 case 0xb: /* Linked command complete without error, interrupt generated */
300 hosterr = 0;
301 break;
302
303 case 0x11: /* Selection time out-The initiator selection or target
304 reselection was not complete within the SCSI Time out period */
305 hosterr = DID_TIME_OUT;
306 break;
307
308 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
309 than was allocated by the Data Length field or the sum of the
310 Scatter / Gather Data Length fields. */
311
312 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
313
314 case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
315 invalid. This usually indicates a software failure. */
316
317 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
318 This usually indicates a software failure. */
319
320 case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
321 of linked CCB's does not specify the same logical unit number as
322 the first. */
323 case 0x18: /* Invalid Target Direction received from Host-The direction of a
324 Target Mode CCB was invalid. */
325
326 case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
327 received to service data transfer between the same target LUN
328 and initiator SCSI ID in the same direction. */
329
330 case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
331 length segment or invalid segment list boundaries was received.
332 A CCB parameter was invalid. */
333 DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
334 hosterr = DID_ERROR; /* Couldn't find any better */
335 break;
336
337 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
338 phase sequence was requested by the target. The host adapter
339 will generate a SCSI Reset Condition, notifying the host with
340 a SCRD interrupt */
341 hosterr = DID_RESET;
342 break;
343 default:
344 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
345 break;
346 }
347 return scsierr | (hosterr << 16);
348 }
349
350 static int __init aha1542_test_port(int bse, struct Scsi_Host *shpnt)
351 {
352 unchar inquiry_cmd[] = {CMD_INQUIRY};
353 unchar inquiry_result[4];
354 unchar *cmdp;
355 int len;
356 volatile int debug = 0;
357
358 /* Quick and dirty test for presence of the card. */
359 if (inb(STATUS(bse)) == 0xff)
360 return 0;
361
362 /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
363
364 /* DEB(printk("aha1542_test_port called \n")); */
365
366 /* In case some other card was probing here, reset interrupts */
367 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
368
369 outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
370
371 mdelay(20); /* Wait a little bit for things to settle down. */
372
373 debug = 1;
374 /* Expect INIT and IDLE, any of the others are bad */
375 WAIT(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
376
377 debug = 2;
378 /* Shouldn't have generated any interrupts during reset */
379 if (inb(INTRFLAGS(bse)) & INTRMASK)
380 goto fail;
381
382
383 /* Perform a host adapter inquiry instead so we do not need to set
384 up the mailboxes ahead of time */
385
386 aha1542_out(bse, inquiry_cmd, 1);
387
388 debug = 3;
389 len = 4;
390 cmdp = &inquiry_result[0];
391
392 while (len--) {
393 WAIT(STATUS(bse), DF, DF, 0);
394 *cmdp++ = inb(DATA(bse));
395 }
396
397 debug = 8;
398 /* Reading port should reset DF */
399 if (inb(STATUS(bse)) & DF)
400 goto fail;
401
402 debug = 9;
403 /* When HACC, command is completed, and we're though testing */
404 WAIT(INTRFLAGS(bse), HACC, HACC, 0);
405 /* now initialize adapter */
406
407 debug = 10;
408 /* Clear interrupts */
409 outb(IRST, CONTROL(bse));
410
411 debug = 11;
412
413 return debug; /* 1 = ok */
414 fail:
415 return 0; /* 0 = not ok */
416 }
417
418 /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
419 static irqreturn_t do_aha1542_intr_handle(int irq, void *dev_id,
420 struct pt_regs *regs)
421 {
422 unsigned long flags;
423 struct Scsi_Host *shost;
424
425 shost = aha_host[irq - 9];
426 if (!shost)
427 panic("Splunge!");
428
429 spin_lock_irqsave(shost->host_lock, flags);
430 aha1542_intr_handle(shost, dev_id, regs);
431 spin_unlock_irqrestore(shost->host_lock, flags);
432 return IRQ_HANDLED;
433 }
434
435 /* A "high" level interrupt handler */
436 static void aha1542_intr_handle(struct Scsi_Host *shost, void *dev_id, struct pt_regs *regs)
437 {
438 void (*my_done) (Scsi_Cmnd *) = NULL;
439 int errstatus, mbi, mbo, mbistatus;
440 int number_serviced;
441 unsigned long flags;
442 Scsi_Cmnd *SCtmp;
443 int flag;
444 int needs_restart;
445 struct mailbox *mb;
446 struct ccb *ccb;
447
448 mb = HOSTDATA(shost)->mb;
449 ccb = HOSTDATA(shost)->ccb;
450
451 #ifdef DEBUG
452 {
453 flag = inb(INTRFLAGS(shost->io_port));
454 printk(KERN_DEBUG "aha1542_intr_handle: ");
455 if (!(flag & ANYINTR))
456 printk("no interrupt?");
457 if (flag & MBIF)
458 printk("MBIF ");
459 if (flag & MBOA)
460 printk("MBOF ");
461 if (flag & HACC)
462 printk("HACC ");
463 if (flag & SCRD)
464 printk("SCRD ");
465 printk("status %02x\n", inb(STATUS(shost->io_port)));
466 };
467 #endif
468 number_serviced = 0;
469 needs_restart = 0;
470
471 while (1 == 1) {
472 flag = inb(INTRFLAGS(shost->io_port));
473
474 /* Check for unusual interrupts. If any of these happen, we should
475 probably do something special, but for now just printing a message
476 is sufficient. A SCSI reset detected is something that we really
477 need to deal with in some way. */
478 if (flag & ~MBIF) {
479 if (flag & MBOA)
480 printk("MBOF ");
481 if (flag & HACC)
482 printk("HACC ");
483 if (flag & SCRD) {
484 needs_restart = 1;
485 printk("SCRD ");
486 }
487 }
488 aha1542_intr_reset(shost->io_port);
489
490 spin_lock_irqsave(&aha1542_lock, flags);
491 mbi = HOSTDATA(shost)->aha1542_last_mbi_used + 1;
492 if (mbi >= 2 * AHA1542_MAILBOXES)
493 mbi = AHA1542_MAILBOXES;
494
495 do {
496 if (mb[mbi].status != 0)
497 break;
498 mbi++;
499 if (mbi >= 2 * AHA1542_MAILBOXES)
500 mbi = AHA1542_MAILBOXES;
501 } while (mbi != HOSTDATA(shost)->aha1542_last_mbi_used);
502
503 if (mb[mbi].status == 0) {
504 spin_unlock_irqrestore(&aha1542_lock, flags);
505 /* Hmm, no mail. Must have read it the last time around */
506 if (!number_serviced && !needs_restart)
507 printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
508 /* We detected a reset. Restart all pending commands for
509 devices that use the hard reset option */
510 if (needs_restart)
511 aha1542_restart(shost);
512 return;
513 };
514
515 mbo = (scsi2int(mb[mbi].ccbptr) - (SCSI_BUF_PA(&ccb[0]))) / sizeof(struct ccb);
516 mbistatus = mb[mbi].status;
517 mb[mbi].status = 0;
518 HOSTDATA(shost)->aha1542_last_mbi_used = mbi;
519 spin_unlock_irqrestore(&aha1542_lock, flags);
520
521 #ifdef DEBUG
522 {
523 if (ccb[mbo].tarstat | ccb[mbo].hastat)
524 printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
525 ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
526 };
527 #endif
528
529 if (mbistatus == 3)
530 continue; /* Aborted command not found */
531
532 #ifdef DEBUG
533 printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
534 #endif
535
536 SCtmp = HOSTDATA(shost)->SCint[mbo];
537
538 if (!SCtmp || !SCtmp->scsi_done) {
539 printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
540 printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
541 ccb[mbo].hastat, ccb[mbo].idlun, mbo);
542 return;
543 }
544 my_done = SCtmp->scsi_done;
545 kfree(SCtmp->host_scribble);
546 SCtmp->host_scribble = NULL;
547 /* Fetch the sense data, and tuck it away, in the required slot. The
548 Adaptec automatically fetches it, and there is no guarantee that
549 we will still have it in the cdb when we come back */
550 if (ccb[mbo].tarstat == 2)
551 memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
552 sizeof(SCtmp->sense_buffer));
553
554
555 /* is there mail :-) */
556
557 /* more error checking left out here */
558 if (mbistatus != 1)
559 /* This is surely wrong, but I don't know what's right */
560 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
561 else
562 errstatus = 0;
563
564 #ifdef DEBUG
565 if (errstatus)
566 printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
567 ccb[mbo].hastat, ccb[mbo].tarstat);
568 #endif
569
570 if (ccb[mbo].tarstat == 2) {
571 #ifdef DEBUG
572 int i;
573 #endif
574 DEB(printk("aha1542_intr_handle: sense:"));
575 #ifdef DEBUG
576 for (i = 0; i < 12; i++)
577 printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
578 printk("\n");
579 #endif
580 /*
581 DEB(printk("aha1542_intr_handle: buf:"));
582 for (i = 0; i < bufflen; i++)
583 printk("%02x ", ((unchar *)buff)[i]);
584 printk("\n");
585 */
586 }
587 DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
588 SCtmp->result = errstatus;
589 HOSTDATA(shost)->SCint[mbo] = NULL; /* This effectively frees up the mailbox slot, as
590 far as queuecommand is concerned */
591 my_done(SCtmp);
592 number_serviced++;
593 };
594 }
595
596 static int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
597 {
598 unchar ahacmd = CMD_START_SCSI;
599 unchar direction;
600 unchar *cmd = (unchar *) SCpnt->cmnd;
601 unchar target = SCpnt->device->id;
602 unchar lun = SCpnt->device->lun;
603 unsigned long flags;
604 void *buff = SCpnt->request_buffer;
605 int bufflen = SCpnt->request_bufflen;
606 int mbo;
607 struct mailbox *mb;
608 struct ccb *ccb;
609
610 DEB(int i);
611
612 mb = HOSTDATA(SCpnt->device->host)->mb;
613 ccb = HOSTDATA(SCpnt->device->host)->ccb;
614
615 DEB(if (target > 1) {
616 SCpnt->result = DID_TIME_OUT << 16;
617 done(SCpnt); return 0;
618 }
619 );
620
621 if (*cmd == REQUEST_SENSE) {
622 /* Don't do the command - we have the sense data already */
623 #if 0
624 /* scsi_request_sense() provides a buffer of size 256,
625 so there is no reason to expect equality */
626 if (bufflen != sizeof(SCpnt->sense_buffer))
627 printk(KERN_CRIT "aha1542: Wrong buffer length supplied "
628 "for request sense (%d)\n", bufflen);
629 #endif
630 SCpnt->result = 0;
631 done(SCpnt);
632 return 0;
633 }
634 #ifdef DEBUG
635 if (*cmd == READ_10 || *cmd == WRITE_10)
636 i = xscsi2int(cmd + 2);
637 else if (*cmd == READ_6 || *cmd == WRITE_6)
638 i = scsi2int(cmd + 2);
639 else
640 i = -1;
641 if (done)
642 printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
643 else
644 printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
645 aha1542_stat();
646 printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
647 for (i = 0; i < SCpnt->cmd_len; i++)
648 printk("%02x ", cmd[i]);
649 printk("\n");
650 if (*cmd == WRITE_10 || *cmd == WRITE_6)
651 return 0; /* we are still testing, so *don't* write */
652 #endif
653 /* Use the outgoing mailboxes in a round-robin fashion, because this
654 is how the host adapter will scan for them */
655
656 spin_lock_irqsave(&aha1542_lock, flags);
657 mbo = HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used + 1;
658 if (mbo >= AHA1542_MAILBOXES)
659 mbo = 0;
660
661 do {
662 if (mb[mbo].status == 0 && HOSTDATA(SCpnt->device->host)->SCint[mbo] == NULL)
663 break;
664 mbo++;
665 if (mbo >= AHA1542_MAILBOXES)
666 mbo = 0;
667 } while (mbo != HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used);
668
669 if (mb[mbo].status || HOSTDATA(SCpnt->device->host)->SCint[mbo])
670 panic("Unable to find empty mailbox for aha1542.\n");
671
672 HOSTDATA(SCpnt->device->host)->SCint[mbo] = SCpnt; /* This will effectively prevent someone else from
673 screwing with this cdb. */
674
675 HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used = mbo;
676 spin_unlock_irqrestore(&aha1542_lock, flags);
677
678 #ifdef DEBUG
679 printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
680 #endif
681
682 any2scsi(mb[mbo].ccbptr, SCSI_BUF_PA(&ccb[mbo])); /* This gets trashed for some reason */
683
684 memset(&ccb[mbo], 0, sizeof(struct ccb));
685
686 ccb[mbo].cdblen = SCpnt->cmd_len;
687
688 direction = 0;
689 if (*cmd == READ_10 || *cmd == READ_6)
690 direction = 8;
691 else if (*cmd == WRITE_10 || *cmd == WRITE_6)
692 direction = 16;
693
694 memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
695
696 if (SCpnt->use_sg) {
697 struct scatterlist *sgpnt;
698 struct chain *cptr;
699 #ifdef DEBUG
700 unsigned char *ptr;
701 #endif
702 int i;
703 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
704 SCpnt->host_scribble = (unsigned char *) kmalloc(512, GFP_KERNEL | GFP_DMA);
705 sgpnt = (struct scatterlist *) SCpnt->request_buffer;
706 cptr = (struct chain *) SCpnt->host_scribble;
707 if (cptr == NULL) {
708 /* free the claimed mailbox slot */
709 HOSTDATA(SCpnt->device->host)->SCint[mbo] = NULL;
710 return SCSI_MLQUEUE_HOST_BUSY;
711 }
712 for (i = 0; i < SCpnt->use_sg; i++) {
713 if (sgpnt[i].length == 0 || SCpnt->use_sg > 16 ||
714 (((int) sgpnt[i].offset) & 1) || (sgpnt[i].length & 1)) {
715 unsigned char *ptr;
716 printk(KERN_CRIT "Bad segment list supplied to aha1542.c (%d, %d)\n", SCpnt->use_sg, i);
717 for (i = 0; i < SCpnt->use_sg; i++) {
718 printk(KERN_CRIT "%d: %p %d\n", i,
719 (page_address(sgpnt[i].page) +
720 sgpnt[i].offset),
721 sgpnt[i].length);
722 };
723 printk(KERN_CRIT "cptr %x: ", (unsigned int) cptr);
724 ptr = (unsigned char *) &cptr[i];
725 for (i = 0; i < 18; i++)
726 printk("%02x ", ptr[i]);
727 panic("Foooooooood fight!");
728 };
729 any2scsi(cptr[i].dataptr, SCSI_SG_PA(&sgpnt[i]));
730 if (SCSI_SG_PA(&sgpnt[i]) + sgpnt[i].length - 1 > ISA_DMA_THRESHOLD)
731 BAD_SG_DMA(SCpnt, sgpnt, SCpnt->use_sg, i);
732 any2scsi(cptr[i].datalen, sgpnt[i].length);
733 };
734 any2scsi(ccb[mbo].datalen, SCpnt->use_sg * sizeof(struct chain));
735 any2scsi(ccb[mbo].dataptr, SCSI_BUF_PA(cptr));
736 #ifdef DEBUG
737 printk("cptr %x: ", cptr);
738 ptr = (unsigned char *) cptr;
739 for (i = 0; i < 18; i++)
740 printk("%02x ", ptr[i]);
741 #endif
742 } else {
743 ccb[mbo].op = 0; /* SCSI Initiator Command */
744 SCpnt->host_scribble = NULL;
745 any2scsi(ccb[mbo].datalen, bufflen);
746 if (buff && SCSI_BUF_PA(buff + bufflen - 1) > ISA_DMA_THRESHOLD)
747 BAD_DMA(buff, bufflen);
748 any2scsi(ccb[mbo].dataptr, SCSI_BUF_PA(buff));
749 };
750 ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
751 ccb[mbo].rsalen = 16;
752 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
753 ccb[mbo].commlinkid = 0;
754
755 #ifdef DEBUG
756 {
757 int i;
758 printk(KERN_DEBUG "aha1542_command: sending.. ");
759 for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
760 printk("%02x ", ((unchar *) & ccb[mbo])[i]);
761 };
762 #endif
763
764 if (done) {
765 DEB(printk("aha1542_queuecommand: now waiting for interrupt ");
766 aha1542_stat());
767 SCpnt->scsi_done = done;
768 mb[mbo].status = 1;
769 aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1); /* start scsi command */
770 DEB(aha1542_stat());
771 } else
772 printk("aha1542_queuecommand: done can't be NULL\n");
773
774 return 0;
775 }
776
777 /* Initialize mailboxes */
778 static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
779 {
780 int i;
781 struct mailbox *mb;
782 struct ccb *ccb;
783
784 unchar cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
785
786 mb = HOSTDATA(shpnt)->mb;
787 ccb = HOSTDATA(shpnt)->ccb;
788
789 for (i = 0; i < AHA1542_MAILBOXES; i++) {
790 mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
791 any2scsi(mb[i].ccbptr, SCSI_BUF_PA(&ccb[i]));
792 };
793 aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
794 any2scsi((cmd + 2), SCSI_BUF_PA(mb));
795 aha1542_out(bse, cmd, 5);
796 WAIT(INTRFLAGS(bse), INTRMASK, HACC, 0);
797 while (0) {
798 fail:
799 printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
800 }
801 aha1542_intr_reset(bse);
802 }
803
804 static int __init aha1542_getconfig(int base_io, unsigned char *irq_level, unsigned char *dma_chan, unsigned char *scsi_id)
805 {
806 unchar inquiry_cmd[] = {CMD_RETCONF};
807 unchar inquiry_result[3];
808 int i;
809 i = inb(STATUS(base_io));
810 if (i & DF) {
811 i = inb(DATA(base_io));
812 };
813 aha1542_out(base_io, inquiry_cmd, 1);
814 aha1542_in(base_io, inquiry_result, 3);
815 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
816 while (0) {
817 fail:
818 printk(KERN_ERR "aha1542_detect: query board settings\n");
819 }
820 aha1542_intr_reset(base_io);
821 switch (inquiry_result[0]) {
822 case 0x80:
823 *dma_chan = 7;
824 break;
825 case 0x40:
826 *dma_chan = 6;
827 break;
828 case 0x20:
829 *dma_chan = 5;
830 break;
831 case 0x01:
832 *dma_chan = 0;
833 break;
834 case 0:
835 /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
836 Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
837 *dma_chan = 0xFF;
838 break;
839 default:
840 printk(KERN_ERR "Unable to determine Adaptec DMA priority. Disabling board\n");
841 return -1;
842 };
843 switch (inquiry_result[1]) {
844 case 0x40:
845 *irq_level = 15;
846 break;
847 case 0x20:
848 *irq_level = 14;
849 break;
850 case 0x8:
851 *irq_level = 12;
852 break;
853 case 0x4:
854 *irq_level = 11;
855 break;
856 case 0x2:
857 *irq_level = 10;
858 break;
859 case 0x1:
860 *irq_level = 9;
861 break;
862 default:
863 printk(KERN_ERR "Unable to determine Adaptec IRQ level. Disabling board\n");
864 return -1;
865 };
866 *scsi_id = inquiry_result[2] & 7;
867 return 0;
868 }
869
870 /* This function should only be called for 1542C boards - we can detect
871 the special firmware settings and unlock the board */
872
873 static int __init aha1542_mbenable(int base)
874 {
875 static unchar mbenable_cmd[3];
876 static unchar mbenable_result[2];
877 int retval;
878
879 retval = BIOS_TRANSLATION_6432;
880
881 mbenable_cmd[0] = CMD_EXTBIOS;
882 aha1542_out(base, mbenable_cmd, 1);
883 if (aha1542_in1(base, mbenable_result, 2))
884 return retval;
885 WAITd(INTRFLAGS(base), INTRMASK, HACC, 0, 100);
886 aha1542_intr_reset(base);
887
888 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
889 mbenable_cmd[0] = CMD_MBENABLE;
890 mbenable_cmd[1] = 0;
891 mbenable_cmd[2] = mbenable_result[1];
892
893 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
894 retval = BIOS_TRANSLATION_25563;
895
896 aha1542_out(base, mbenable_cmd, 3);
897 WAIT(INTRFLAGS(base), INTRMASK, HACC, 0);
898 };
899 while (0) {
900 fail:
901 printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
902 }
903 aha1542_intr_reset(base);
904 return retval;
905 }
906
907 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
908 static int __init aha1542_query(int base_io, int *transl)
909 {
910 unchar inquiry_cmd[] = {CMD_INQUIRY};
911 unchar inquiry_result[4];
912 int i;
913 i = inb(STATUS(base_io));
914 if (i & DF) {
915 i = inb(DATA(base_io));
916 };
917 aha1542_out(base_io, inquiry_cmd, 1);
918 aha1542_in(base_io, inquiry_result, 4);
919 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
920 while (0) {
921 fail:
922 printk(KERN_ERR "aha1542_detect: query card type\n");
923 }
924 aha1542_intr_reset(base_io);
925
926 *transl = BIOS_TRANSLATION_6432; /* Default case */
927
928 /* For an AHA1740 series board, we ignore the board since there is a
929 hardware bug which can lead to wrong blocks being returned if the board
930 is operating in the 1542 emulation mode. Since there is an extended mode
931 driver, we simply ignore the board and let the 1740 driver pick it up.
932 */
933
934 if (inquiry_result[0] == 0x43) {
935 printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
936 return 1;
937 };
938
939 /* Always call this - boards that do not support extended bios translation
940 will ignore the command, and we will set the proper default */
941
942 *transl = aha1542_mbenable(base_io);
943
944 return 0;
945 }
946
947 #ifndef MODULE
948 static char *setup_str[MAXBOARDS] __initdata;
949 static int setup_idx = 0;
950
951 static void __init aha1542_setup(char *str, int *ints)
952 {
953 const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
954 int setup_portbase;
955
956 if (setup_idx >= MAXBOARDS) {
957 printk(KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
958 printk(KERN_ERR " Entryline 1: %s\n", setup_str[0]);
959 printk(KERN_ERR " Entryline 2: %s\n", setup_str[1]);
960 printk(KERN_ERR " This line: %s\n", str);
961 return;
962 }
963 if (ints[0] < 1 || ints[0] > 4) {
964 printk(KERN_ERR "aha1542: %s\n", str);
965 printk(ahausage);
966 printk(KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
967 }
968 setup_called[setup_idx] = ints[0];
969 setup_str[setup_idx] = str;
970
971 setup_portbase = ints[0] >= 1 ? ints[1] : 0; /* Preserve the default value.. */
972 setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
973 setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
974 if (ints[0] >= 4)
975 {
976 int atbt = -1;
977 switch (ints[4]) {
978 case 5:
979 atbt = 0x00;
980 break;
981 case 6:
982 atbt = 0x04;
983 break;
984 case 7:
985 atbt = 0x01;
986 break;
987 case 8:
988 atbt = 0x02;
989 break;
990 case 10:
991 atbt = 0x03;
992 break;
993 default:
994 printk(KERN_ERR "aha1542: %s\n", str);
995 printk(ahausage);
996 printk(KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s. Using jumper defaults.\n");
997 break;
998 }
999 setup_dmaspeed[setup_idx] = atbt;
1000 }
1001 if (setup_portbase != 0)
1002 bases[setup_idx] = setup_portbase;
1003
1004 ++setup_idx;
1005 }
1006
1007 static int __init do_setup(char *str)
1008 {
1009 int ints[5];
1010
1011 int count=setup_idx;
1012
1013 get_options(str, ARRAY_SIZE(ints), ints);
1014 aha1542_setup(str,ints);
1015
1016 return count<setup_idx;
1017 }
1018
1019 __setup("aha1542=",do_setup);
1020 #endif
1021
1022 /* return non-zero on detection */
1023 static int __init aha1542_detect(struct scsi_host_template * tpnt)
1024 {
1025 unsigned char dma_chan;
1026 unsigned char irq_level;
1027 unsigned char scsi_id;
1028 unsigned long flags;
1029 unsigned int base_io;
1030 int trans;
1031 struct Scsi_Host *shpnt = NULL;
1032 int count = 0;
1033 int indx;
1034
1035 DEB(printk("aha1542_detect: \n"));
1036
1037 tpnt->proc_name = "aha1542";
1038
1039 #ifdef MODULE
1040 bases[0] = aha1542[0];
1041 setup_buson[0] = aha1542[1];
1042 setup_busoff[0] = aha1542[2];
1043 {
1044 int atbt = -1;
1045 switch (aha1542[3]) {
1046 case 5:
1047 atbt = 0x00;
1048 break;
1049 case 6:
1050 atbt = 0x04;
1051 break;
1052 case 7:
1053 atbt = 0x01;
1054 break;
1055 case 8:
1056 atbt = 0x02;
1057 break;
1058 case 10:
1059 atbt = 0x03;
1060 break;
1061 };
1062 setup_dmaspeed[0] = atbt;
1063 }
1064 #endif
1065
1066 /*
1067 * Find MicroChannel cards (AHA1640)
1068 */
1069 #ifdef CONFIG_MCA_LEGACY
1070 if(MCA_bus) {
1071 int slot = 0;
1072 int pos = 0;
1073
1074 for (indx = 0; (slot != MCA_NOTFOUND) && (indx < ARRAY_SIZE(bases)); indx++) {
1075
1076 if (bases[indx])
1077 continue;
1078
1079 /* Detect only AHA-1640 cards -- MCA ID 0F1F */
1080 slot = mca_find_unused_adapter(0x0f1f, slot);
1081 if (slot == MCA_NOTFOUND)
1082 break;
1083
1084 /* Found one */
1085 pos = mca_read_stored_pos(slot, 3);
1086
1087 /* Decode address */
1088 if (pos & 0x80) {
1089 if (pos & 0x02) {
1090 if (pos & 0x01)
1091 bases[indx] = 0x334;
1092 else
1093 bases[indx] = 0x234;
1094 } else {
1095 if (pos & 0x01)
1096 bases[indx] = 0x134;
1097 }
1098 } else {
1099 if (pos & 0x02) {
1100 if (pos & 0x01)
1101 bases[indx] = 0x330;
1102 else
1103 bases[indx] = 0x230;
1104 } else {
1105 if (pos & 0x01)
1106 bases[indx] = 0x130;
1107 }
1108 }
1109
1110 /* No need to decode IRQ and Arb level -- those are
1111 * read off the card later.
1112 */
1113 printk(KERN_INFO "Found an AHA-1640 in MCA slot %d, I/O 0x%04x\n", slot, bases[indx]);
1114
1115 mca_set_adapter_name(slot, "Adapter AHA-1640");
1116 mca_set_adapter_procfn(slot, NULL, NULL);
1117 mca_mark_as_used(slot);
1118
1119 /* Go on */
1120 slot++;
1121 }
1122
1123 }
1124 #endif
1125
1126 /*
1127 * Hunt for ISA Plug'n'Pray Adaptecs (AHA1535)
1128 */
1129
1130 if(isapnp)
1131 {
1132 struct pnp_dev *pdev = NULL;
1133 for(indx = 0; indx < ARRAY_SIZE(bases); indx++) {
1134 if(bases[indx])
1135 continue;
1136 pdev = pnp_find_dev(NULL, ISAPNP_VENDOR('A', 'D', 'P'),
1137 ISAPNP_FUNCTION(0x1542), pdev);
1138 if(pdev==NULL)
1139 break;
1140 /*
1141 * Activate the PnP card
1142 */
1143
1144 if(pnp_device_attach(pdev)<0)
1145 continue;
1146
1147 if(pnp_activate_dev(pdev)<0) {
1148 pnp_device_detach(pdev);
1149 continue;
1150 }
1151
1152 if(!pnp_port_valid(pdev, 0)) {
1153 pnp_device_detach(pdev);
1154 continue;
1155 }
1156
1157 bases[indx] = pnp_port_start(pdev, 0);
1158
1159 /* The card can be queried for its DMA, we have
1160 the DMA set up that is enough */
1161
1162 printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
1163 }
1164 }
1165 for (indx = 0; indx < ARRAY_SIZE(bases); indx++)
1166 if (bases[indx] != 0 && request_region(bases[indx], 4, "aha1542")) {
1167 shpnt = scsi_register(tpnt,
1168 sizeof(struct aha1542_hostdata));
1169
1170 if(shpnt==NULL) {
1171 release_region(bases[indx], 4);
1172 continue;
1173 }
1174 /* For now we do this - until kmalloc is more intelligent
1175 we are resigned to stupid hacks like this */
1176 if (SCSI_BUF_PA(shpnt) >= ISA_DMA_THRESHOLD) {
1177 printk(KERN_ERR "Invalid address for shpnt with 1542.\n");
1178 goto unregister;
1179 }
1180 if (!aha1542_test_port(bases[indx], shpnt))
1181 goto unregister;
1182
1183
1184 base_io = bases[indx];
1185
1186 /* Set the Bus on/off-times as not to ruin floppy performance */
1187 {
1188 unchar oncmd[] = {CMD_BUSON_TIME, 7};
1189 unchar offcmd[] = {CMD_BUSOFF_TIME, 5};
1190
1191 if (setup_called[indx]) {
1192 oncmd[1] = setup_buson[indx];
1193 offcmd[1] = setup_busoff[indx];
1194 }
1195 aha1542_intr_reset(base_io);
1196 aha1542_out(base_io, oncmd, 2);
1197 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1198 aha1542_intr_reset(base_io);
1199 aha1542_out(base_io, offcmd, 2);
1200 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1201 if (setup_dmaspeed[indx] >= 0) {
1202 unchar dmacmd[] = {CMD_DMASPEED, 0};
1203 dmacmd[1] = setup_dmaspeed[indx];
1204 aha1542_intr_reset(base_io);
1205 aha1542_out(base_io, dmacmd, 2);
1206 WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
1207 }
1208 while (0) {
1209 fail:
1210 printk(KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
1211 }
1212 aha1542_intr_reset(base_io);
1213 }
1214 if (aha1542_query(base_io, &trans))
1215 goto unregister;
1216
1217 if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
1218 goto unregister;
1219
1220 printk(KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
1221 if (dma_chan != 0xFF)
1222 printk(", DMA priority %d", dma_chan);
1223 printk("\n");
1224
1225 DEB(aha1542_stat());
1226 setup_mailboxes(base_io, shpnt);
1227
1228 DEB(aha1542_stat());
1229
1230 DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
1231 spin_lock_irqsave(&aha1542_lock, flags);
1232 if (request_irq(irq_level, do_aha1542_intr_handle, 0, "aha1542", NULL)) {
1233 printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
1234 spin_unlock_irqrestore(&aha1542_lock, flags);
1235 goto unregister;
1236 }
1237 if (dma_chan != 0xFF) {
1238 if (request_dma(dma_chan, "aha1542")) {
1239 printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
1240 free_irq(irq_level, NULL);
1241 spin_unlock_irqrestore(&aha1542_lock, flags);
1242 goto unregister;
1243 }
1244 if (dma_chan == 0 || dma_chan >= 5) {
1245 set_dma_mode(dma_chan, DMA_MODE_CASCADE);
1246 enable_dma(dma_chan);
1247 }
1248 }
1249 aha_host[irq_level - 9] = shpnt;
1250 shpnt->this_id = scsi_id;
1251 shpnt->unique_id = base_io;
1252 shpnt->io_port = base_io;
1253 shpnt->n_io_port = 4; /* Number of bytes of I/O space used */
1254 shpnt->dma_channel = dma_chan;
1255 shpnt->irq = irq_level;
1256 HOSTDATA(shpnt)->bios_translation = trans;
1257 if (trans == BIOS_TRANSLATION_25563)
1258 printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
1259 HOSTDATA(shpnt)->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
1260 HOSTDATA(shpnt)->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
1261 memset(HOSTDATA(shpnt)->SCint, 0, sizeof(HOSTDATA(shpnt)->SCint));
1262 spin_unlock_irqrestore(&aha1542_lock, flags);
1263 #if 0
1264 DEB(printk(" *** READ CAPACITY ***\n"));
1265
1266 {
1267 unchar buf[8];
1268 static unchar cmd[] = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1269 int i;
1270
1271 for (i = 0; i < sizeof(buf); ++i)
1272 buf[i] = 0x87;
1273 for (i = 0; i < 2; ++i)
1274 if (!aha1542_command(i, cmd, buf, sizeof(buf))) {
1275 printk(KERN_DEBUG "aha_detect: LU %d sector_size %d device_size %d\n",
1276 i, xscsi2int(buf + 4), xscsi2int(buf));
1277 }
1278 }
1279
1280 DEB(printk(" *** NOW RUNNING MY OWN TEST *** \n"));
1281
1282 for (i = 0; i < 4; ++i) {
1283 unsigned char cmd[10];
1284 static buffer[512];
1285
1286 cmd[0] = READ_10;
1287 cmd[1] = 0;
1288 xany2scsi(cmd + 2, i);
1289 cmd[6] = 0;
1290 cmd[7] = 0;
1291 cmd[8] = 1;
1292 cmd[9] = 0;
1293 aha1542_command(0, cmd, buffer, 512);
1294 }
1295 #endif
1296 count++;
1297 continue;
1298 unregister:
1299 release_region(bases[indx], 4);
1300 scsi_unregister(shpnt);
1301 continue;
1302
1303 };
1304
1305 return count;
1306 }
1307
1308 static int aha1542_release(struct Scsi_Host *shost)
1309 {
1310 if (shost->irq)
1311 free_irq(shost->irq, NULL);
1312 if (shost->dma_channel != 0xff)
1313 free_dma(shost->dma_channel);
1314 if (shost->io_port && shost->n_io_port)
1315 release_region(shost->io_port, shost->n_io_port);
1316 scsi_unregister(shost);
1317 return 0;
1318 }
1319
1320 static int aha1542_restart(struct Scsi_Host *shost)
1321 {
1322 int i;
1323 int count = 0;
1324 #if 0
1325 unchar ahacmd = CMD_START_SCSI;
1326 #endif
1327
1328 for (i = 0; i < AHA1542_MAILBOXES; i++)
1329 if (HOSTDATA(shost)->SCint[i] &&
1330 !(HOSTDATA(shost)->SCint[i]->device->soft_reset)) {
1331 #if 0
1332 HOSTDATA(shost)->mb[i].status = 1; /* Indicate ready to restart... */
1333 #endif
1334 count++;
1335 }
1336 printk(KERN_DEBUG "Potential to restart %d stalled commands...\n", count);
1337 #if 0
1338 /* start scsi command */
1339 if (count)
1340 aha1542_out(shost->io_port, &ahacmd, 1);
1341 #endif
1342 return 0;
1343 }
1344
1345 /*
1346 * This is a device reset. This is handled by sending a special command
1347 * to the device.
1348 */
1349 static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
1350 {
1351 unsigned long flags;
1352 struct mailbox *mb;
1353 unchar target = SCpnt->device->id;
1354 unchar lun = SCpnt->device->lun;
1355 int mbo;
1356 struct ccb *ccb;
1357 unchar ahacmd = CMD_START_SCSI;
1358
1359 ccb = HOSTDATA(SCpnt->device->host)->ccb;
1360 mb = HOSTDATA(SCpnt->device->host)->mb;
1361
1362 spin_lock_irqsave(&aha1542_lock, flags);
1363 mbo = HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used + 1;
1364 if (mbo >= AHA1542_MAILBOXES)
1365 mbo = 0;
1366
1367 do {
1368 if (mb[mbo].status == 0 && HOSTDATA(SCpnt->device->host)->SCint[mbo] == NULL)
1369 break;
1370 mbo++;
1371 if (mbo >= AHA1542_MAILBOXES)
1372 mbo = 0;
1373 } while (mbo != HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used);
1374
1375 if (mb[mbo].status || HOSTDATA(SCpnt->device->host)->SCint[mbo])
1376 panic("Unable to find empty mailbox for aha1542.\n");
1377
1378 HOSTDATA(SCpnt->device->host)->SCint[mbo] = SCpnt; /* This will effectively
1379 prevent someone else from
1380 screwing with this cdb. */
1381
1382 HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used = mbo;
1383 spin_unlock_irqrestore(&aha1542_lock, flags);
1384
1385 any2scsi(mb[mbo].ccbptr, SCSI_BUF_PA(&ccb[mbo])); /* This gets trashed for some reason */
1386
1387 memset(&ccb[mbo], 0, sizeof(struct ccb));
1388
1389 ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
1390
1391 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
1392
1393 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
1394 ccb[mbo].commlinkid = 0;
1395
1396 /*
1397 * Now tell the 1542 to flush all pending commands for this
1398 * target
1399 */
1400 aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1);
1401
1402 scmd_printk(KERN_WARNING, SCpnt,
1403 "Trying device reset for target\n");
1404
1405 return SUCCESS;
1406
1407
1408 #ifdef ERIC_neverdef
1409 /*
1410 * With the 1542 we apparently never get an interrupt to
1411 * acknowledge a device reset being sent. Then again, Leonard
1412 * says we are doing this wrong in the first place...
1413 *
1414 * Take a wait and see attitude. If we get spurious interrupts,
1415 * then the device reset is doing something sane and useful, and
1416 * we will wait for the interrupt to post completion.
1417 */
1418 printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1419
1420 /*
1421 * Free the command block for all commands running on this
1422 * target...
1423 */
1424 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1425 if (HOSTDATA(SCpnt->host)->SCint[i] &&
1426 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1427 Scsi_Cmnd *SCtmp;
1428 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1429 kfree(SCtmp->host_scribble);
1430 SCtmp->host_scribble = NULL;
1431 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1432 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1433 }
1434 }
1435 return SUCCESS;
1436
1437 return FAILED;
1438 #endif /* ERIC_neverdef */
1439 }
1440
1441 static int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
1442 {
1443 int i;
1444
1445 /*
1446 * This does a scsi reset for all devices on the bus.
1447 * In principle, we could also reset the 1542 - should
1448 * we do this? Try this first, and we can add that later
1449 * if it turns out to be useful.
1450 */
1451 outb(SCRST, CONTROL(SCpnt->device->host->io_port));
1452
1453 /*
1454 * Wait for the thing to settle down a bit. Unfortunately
1455 * this is going to basically lock up the machine while we
1456 * wait for this to complete. To be 100% correct, we need to
1457 * check for timeout, and if we are doing something like this
1458 * we are pretty desperate anyways.
1459 */
1460 ssleep(4);
1461
1462 spin_lock_irq(SCpnt->device->host->host_lock);
1463
1464 WAIT(STATUS(SCpnt->device->host->io_port),
1465 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1466
1467 /*
1468 * Now try to pick up the pieces. For all pending commands,
1469 * free any internal data structures, and basically clear things
1470 * out. We do not try and restart any commands or anything -
1471 * the strategy handler takes care of that crap.
1472 */
1473 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1474
1475 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1476 if (HOSTDATA(SCpnt->device->host)->SCint[i] != NULL) {
1477 Scsi_Cmnd *SCtmp;
1478 SCtmp = HOSTDATA(SCpnt->device->host)->SCint[i];
1479
1480
1481 if (SCtmp->device->soft_reset) {
1482 /*
1483 * If this device implements the soft reset option,
1484 * then it is still holding onto the command, and
1485 * may yet complete it. In this case, we don't
1486 * flush the data.
1487 */
1488 continue;
1489 }
1490 kfree(SCtmp->host_scribble);
1491 SCtmp->host_scribble = NULL;
1492 HOSTDATA(SCpnt->device->host)->SCint[i] = NULL;
1493 HOSTDATA(SCpnt->device->host)->mb[i].status = 0;
1494 }
1495 }
1496
1497 spin_unlock_irq(SCpnt->device->host->host_lock);
1498 return SUCCESS;
1499
1500 fail:
1501 spin_unlock_irq(SCpnt->device->host->host_lock);
1502 return FAILED;
1503 }
1504
1505 static int aha1542_host_reset(Scsi_Cmnd * SCpnt)
1506 {
1507 int i;
1508
1509 /*
1510 * This does a scsi reset for all devices on the bus.
1511 * In principle, we could also reset the 1542 - should
1512 * we do this? Try this first, and we can add that later
1513 * if it turns out to be useful.
1514 */
1515 outb(HRST | SCRST, CONTROL(SCpnt->device->host->io_port));
1516
1517 /*
1518 * Wait for the thing to settle down a bit. Unfortunately
1519 * this is going to basically lock up the machine while we
1520 * wait for this to complete. To be 100% correct, we need to
1521 * check for timeout, and if we are doing something like this
1522 * we are pretty desperate anyways.
1523 */
1524 ssleep(4);
1525 spin_lock_irq(SCpnt->device->host->host_lock);
1526
1527 WAIT(STATUS(SCpnt->device->host->io_port),
1528 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1529
1530 /*
1531 * We need to do this too before the 1542 can interact with
1532 * us again.
1533 */
1534 setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
1535
1536 /*
1537 * Now try to pick up the pieces. For all pending commands,
1538 * free any internal data structures, and basically clear things
1539 * out. We do not try and restart any commands or anything -
1540 * the strategy handler takes care of that crap.
1541 */
1542 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
1543
1544 for (i = 0; i < AHA1542_MAILBOXES; i++) {
1545 if (HOSTDATA(SCpnt->device->host)->SCint[i] != NULL) {
1546 Scsi_Cmnd *SCtmp;
1547 SCtmp = HOSTDATA(SCpnt->device->host)->SCint[i];
1548
1549 if (SCtmp->device->soft_reset) {
1550 /*
1551 * If this device implements the soft reset option,
1552 * then it is still holding onto the command, and
1553 * may yet complete it. In this case, we don't
1554 * flush the data.
1555 */
1556 continue;
1557 }
1558 kfree(SCtmp->host_scribble);
1559 SCtmp->host_scribble = NULL;
1560 HOSTDATA(SCpnt->device->host)->SCint[i] = NULL;
1561 HOSTDATA(SCpnt->device->host)->mb[i].status = 0;
1562 }
1563 }
1564
1565 spin_unlock_irq(SCpnt->device->host->host_lock);
1566 return SUCCESS;
1567
1568 fail:
1569 spin_unlock_irq(SCpnt->device->host->host_lock);
1570 return FAILED;
1571 }
1572
1573 #if 0
1574 /*
1575 * These are the old error handling routines. They are only temporarily
1576 * here while we play with the new error handling code.
1577 */
1578 static int aha1542_old_abort(Scsi_Cmnd * SCpnt)
1579 {
1580 #if 0
1581 unchar ahacmd = CMD_START_SCSI;
1582 unsigned long flags;
1583 struct mailbox *mb;
1584 int mbi, mbo, i;
1585
1586 printk(KERN_DEBUG "In aha1542_abort: %x %x\n",
1587 inb(STATUS(SCpnt->host->io_port)),
1588 inb(INTRFLAGS(SCpnt->host->io_port)));
1589
1590 spin_lock_irqsave(&aha1542_lock, flags);
1591 mb = HOSTDATA(SCpnt->host)->mb;
1592 mbi = HOSTDATA(SCpnt->host)->aha1542_last_mbi_used + 1;
1593 if (mbi >= 2 * AHA1542_MAILBOXES)
1594 mbi = AHA1542_MAILBOXES;
1595
1596 do {
1597 if (mb[mbi].status != 0)
1598 break;
1599 mbi++;
1600 if (mbi >= 2 * AHA1542_MAILBOXES)
1601 mbi = AHA1542_MAILBOXES;
1602 } while (mbi != HOSTDATA(SCpnt->host)->aha1542_last_mbi_used);
1603 spin_unlock_irqrestore(&aha1542_lock, flags);
1604
1605 if (mb[mbi].status) {
1606 printk(KERN_ERR "Lost interrupt discovered on irq %d - attempting to recover\n",
1607 SCpnt->host->irq);
1608 aha1542_intr_handle(SCpnt->host, NULL);
1609 return 0;
1610 }
1611 /* OK, no lost interrupt. Try looking to see how many pending commands
1612 we think we have. */
1613
1614 for (i = 0; i < AHA1542_MAILBOXES; i++)
1615 if (HOSTDATA(SCpnt->host)->SCint[i]) {
1616 if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1617 printk(KERN_ERR "Timed out command pending for %s\n",
1618 SCpnt->request->rq_disk ?
1619 SCpnt->request->rq_disk->disk_name : "?"
1620 );
1621 if (HOSTDATA(SCpnt->host)->mb[i].status) {
1622 printk(KERN_ERR "OGMB still full - restarting\n");
1623 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1624 };
1625 } else
1626 printk(KERN_ERR "Other pending command %s\n",
1627 SCpnt->request->rq_disk ?
1628 SCpnt->request->rq_disk->disk_name : "?"
1629 );
1630 }
1631 #endif
1632
1633 DEB(printk("aha1542_abort\n"));
1634 #if 0
1635 spin_lock_irqsave(&aha1542_lock, flags);
1636 for (mbo = 0; mbo < AHA1542_MAILBOXES; mbo++) {
1637 if (SCpnt == HOSTDATA(SCpnt->host)->SCint[mbo]) {
1638 mb[mbo].status = 2; /* Abort command */
1639 aha1542_out(SCpnt->host->io_port, &ahacmd, 1); /* start scsi command */
1640 spin_unlock_irqrestore(&aha1542_lock, flags);
1641 break;
1642 }
1643 }
1644 if (AHA1542_MAILBOXES == mbo)
1645 spin_unlock_irqrestore(&aha1542_lock, flags);
1646 #endif
1647 return SCSI_ABORT_SNOOZE;
1648 }
1649
1650 /* We do not implement a reset function here, but the upper level code
1651 assumes that it will get some kind of response for the command in
1652 SCpnt. We must oblige, or the command will hang the scsi system.
1653 For a first go, we assume that the 1542 notifies us with all of the
1654 pending commands (it does implement soft reset, after all). */
1655
1656 static int aha1542_old_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1657 {
1658 unchar ahacmd = CMD_START_SCSI;
1659 int i;
1660
1661 /*
1662 * See if a bus reset was suggested.
1663 */
1664 if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
1665 /*
1666 * This does a scsi reset for all devices on the bus.
1667 * In principle, we could also reset the 1542 - should
1668 * we do this? Try this first, and we can add that later
1669 * if it turns out to be useful.
1670 */
1671 outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
1672
1673 /*
1674 * Wait for the thing to settle down a bit. Unfortunately
1675 * this is going to basically lock up the machine while we
1676 * wait for this to complete. To be 100% correct, we need to
1677 * check for timeout, and if we are doing something like this
1678 * we are pretty desperate anyways.
1679 */
1680 WAIT(STATUS(SCpnt->host->io_port),
1681 STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
1682
1683 /*
1684 * We need to do this too before the 1542 can interact with
1685 * us again.
1686 */
1687 setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
1688
1689 /*
1690 * Now try to pick up the pieces. Restart all commands
1691 * that are currently active on the bus, and reset all of
1692 * the datastructures. We have some time to kill while
1693 * things settle down, so print a nice message.
1694 */
1695 printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
1696
1697 for (i = 0; i < AHA1542_MAILBOXES; i++)
1698 if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
1699 Scsi_Cmnd *SCtmp;
1700 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1701 SCtmp->result = DID_RESET << 16;
1702 kfree(SCtmp->host_scribble);
1703 SCtmp->host_scribble = NULL;
1704 printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
1705 SCtmp->scsi_done(SCpnt);
1706
1707 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1708 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1709 }
1710 /*
1711 * Now tell the mid-level code what we did here. Since
1712 * we have restarted all of the outstanding commands,
1713 * then report SUCCESS.
1714 */
1715 return (SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET);
1716 fail:
1717 printk(KERN_CRIT "aha1542.c: Unable to perform hard reset.\n");
1718 printk(KERN_CRIT "Power cycle machine to reset\n");
1719 return (SCSI_RESET_ERROR | SCSI_RESET_BUS_RESET);
1720
1721
1722 } else {
1723 /* This does a selective reset of just the one device */
1724 /* First locate the ccb for this command */
1725 for (i = 0; i < AHA1542_MAILBOXES; i++)
1726 if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
1727 HOSTDATA(SCpnt->host)->ccb[i].op = 0x81; /* BUS DEVICE RESET */
1728 /* Now tell the 1542 to flush all pending commands for this target */
1729 aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1730
1731 /* Here is the tricky part. What to do next. Do we get an interrupt
1732 for the commands that we aborted with the specified target, or
1733 do we generate this on our own? Try it without first and see
1734 what happens */
1735 printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1736
1737 /* If the first does not work, then try the second. I think the
1738 first option is more likely to be correct. Free the command
1739 block for all commands running on this target... */
1740 for (i = 0; i < AHA1542_MAILBOXES; i++)
1741 if (HOSTDATA(SCpnt->host)->SCint[i] &&
1742 HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
1743 Scsi_Cmnd *SCtmp;
1744 SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1745 SCtmp->result = DID_RESET << 16;
1746 kfree(SCtmp->host_scribble);
1747 SCtmp->host_scribble = NULL;
1748 printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
1749 SCtmp->scsi_done(SCpnt);
1750
1751 HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1752 HOSTDATA(SCpnt->host)->mb[i].status = 0;
1753 }
1754 return SCSI_RESET_SUCCESS;
1755 }
1756 }
1757 /* No active command at this time, so this means that each time we got
1758 some kind of response the last time through. Tell the mid-level code
1759 to request sense information in order to decide what to do next. */
1760 return SCSI_RESET_PUNT;
1761 }
1762 #endif /* end of big comment block around old_abort + old_reset */
1763
1764 static int aha1542_biosparam(struct scsi_device *sdev,
1765 struct block_device *bdev, sector_t capacity, int *ip)
1766 {
1767 int translation_algorithm;
1768 int size = capacity;
1769
1770 translation_algorithm = HOSTDATA(sdev->host)->bios_translation;
1771
1772 if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
1773 /* Please verify that this is the same as what DOS returns */
1774 ip[0] = 255;
1775 ip[1] = 63;
1776 ip[2] = size / 255 / 63;
1777 } else {
1778 ip[0] = 64;
1779 ip[1] = 32;
1780 ip[2] = size >> 11;
1781 }
1782
1783 return 0;
1784 }
1785 MODULE_LICENSE("GPL");
1786
1787
1788 static struct scsi_host_template driver_template = {
1789 .proc_name = "aha1542",
1790 .name = "Adaptec 1542",
1791 .detect = aha1542_detect,
1792 .release = aha1542_release,
1793 .queuecommand = aha1542_queuecommand,
1794 .eh_device_reset_handler= aha1542_dev_reset,
1795 .eh_bus_reset_handler = aha1542_bus_reset,
1796 .eh_host_reset_handler = aha1542_host_reset,
1797 .bios_param = aha1542_biosparam,
1798 .can_queue = AHA1542_MAILBOXES,
1799 .this_id = 7,
1800 .sg_tablesize = AHA1542_SCATTER,
1801 .cmd_per_lun = AHA1542_CMDLUN,
1802 .unchecked_isa_dma = 1,
1803 .use_clustering = ENABLE_CLUSTERING,
1804 };
1805 #include "scsi_module.c"