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