Commit | Line | Data |
---|---|---|
1d084d20 OZ |
1 | /* |
2 | * Driver for Adaptec AHA-1542 SCSI host adapters | |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 1992 Tommy Thorn | |
5 | * Copyright (C) 1993, 1994, 1995 Eric Youngdale | |
1d084d20 | 6 | * Copyright (C) 2015 Ondrej Zary |
1da177e4 LT |
7 | */ |
8 | ||
1da177e4 LT |
9 | #include <linux/module.h> |
10 | #include <linux/interrupt.h> | |
11 | #include <linux/kernel.h> | |
12 | #include <linux/types.h> | |
13 | #include <linux/string.h> | |
14 | #include <linux/ioport.h> | |
15 | #include <linux/delay.h> | |
16 | #include <linux/proc_fs.h> | |
17 | #include <linux/init.h> | |
18 | #include <linux/spinlock.h> | |
643a7c43 OZ |
19 | #include <linux/isa.h> |
20 | #include <linux/pnp.h> | |
1da177e4 | 21 | #include <linux/blkdev.h> |
5a0e3ad6 | 22 | #include <linux/slab.h> |
1da177e4 LT |
23 | |
24 | #include <asm/dma.h> | |
1da177e4 LT |
25 | #include <asm/io.h> |
26 | ||
27 | #include "scsi.h" | |
28 | #include <scsi/scsi_host.h> | |
29 | #include "aha1542.h" | |
6ac7d115 | 30 | #include <linux/stat.h> |
1da177e4 LT |
31 | |
32 | #ifdef DEBUG | |
33 | #define DEB(x) x | |
34 | #else | |
35 | #define DEB(x) | |
36 | #endif | |
f71429ab | 37 | #define MAXBOARDS 4 |
1da177e4 | 38 | |
f71429ab OZ |
39 | static bool isapnp = 1; |
40 | module_param(isapnp, bool, 0); | |
41 | MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)"); | |
1da177e4 | 42 | |
f71429ab OZ |
43 | static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 }; |
44 | module_param_array(io, int, NULL, 0); | |
45 | MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)"); | |
1da177e4 | 46 | |
f71429ab OZ |
47 | /* time AHA spends on the AT-bus during data transfer */ |
48 | static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */ | |
49 | module_param_array(bus_on, int, NULL, 0); | |
50 | MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])"); | |
1da177e4 | 51 | |
f71429ab OZ |
52 | /* time AHA spends off the bus (not to monopolize it) during data transfer */ |
53 | static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */ | |
54 | module_param_array(bus_off, int, NULL, 0); | |
55 | MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])"); | |
1da177e4 | 56 | |
f71429ab OZ |
57 | /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */ |
58 | static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 }; | |
59 | module_param_array(dma_speed, int, NULL, 0); | |
60 | MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])"); | |
1da177e4 | 61 | |
1da177e4 LT |
62 | #define BIOS_TRANSLATION_6432 1 /* Default case these days */ |
63 | #define BIOS_TRANSLATION_25563 2 /* Big disk case */ | |
64 | ||
65 | struct aha1542_hostdata { | |
66 | /* This will effectively start both of them at the first mailbox */ | |
67 | int bios_translation; /* Mapping bios uses - for compatibility */ | |
68 | int aha1542_last_mbi_used; | |
69 | int aha1542_last_mbo_used; | |
55b28f9f | 70 | struct scsi_cmnd *int_cmds[AHA1542_MAILBOXES]; |
1da177e4 LT |
71 | struct mailbox mb[2 * AHA1542_MAILBOXES]; |
72 | struct ccb ccb[AHA1542_MAILBOXES]; | |
73 | }; | |
74 | ||
1da177e4 LT |
75 | static DEFINE_SPINLOCK(aha1542_lock); |
76 | ||
f1bbef63 OZ |
77 | static inline void aha1542_intr_reset(u16 base) |
78 | { | |
79 | outb(IRST, CONTROL(base)); | |
80 | } | |
1da177e4 | 81 | |
2093bfa1 OZ |
82 | static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout) |
83 | { | |
84 | bool delayed = true; | |
85 | ||
86 | if (timeout == 0) { | |
87 | timeout = 3000000; | |
88 | delayed = false; | |
89 | } | |
90 | ||
91 | while (1) { | |
92 | u8 bits = inb(port) & mask; | |
93 | if ((bits & allof) == allof && ((bits & noneof) == 0)) | |
94 | break; | |
95 | if (delayed) | |
96 | mdelay(1); | |
97 | if (--timeout == 0) | |
98 | return false; | |
99 | } | |
100 | ||
101 | return true; | |
102 | } | |
1da177e4 | 103 | |
1da177e4 LT |
104 | /* This is a bit complicated, but we need to make sure that an interrupt |
105 | routine does not send something out while we are in the middle of this. | |
106 | Fortunately, it is only at boot time that multi-byte messages | |
107 | are ever sent. */ | |
cad2fc72 | 108 | static int aha1542_outb(unsigned int base, u8 val) |
1da177e4 | 109 | { |
0c2b6481 OZ |
110 | unsigned long flags; |
111 | ||
112 | while (1) { | |
113 | if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) { | |
114 | printk(KERN_ERR "aha1542_outb failed"); | |
115 | return 1; | |
1da177e4 | 116 | } |
1da177e4 | 117 | spin_lock_irqsave(&aha1542_lock, flags); |
0c2b6481 OZ |
118 | if (inb(STATUS(base)) & CDF) { |
119 | spin_unlock_irqrestore(&aha1542_lock, flags); | |
120 | continue; | |
1da177e4 | 121 | } |
cad2fc72 | 122 | outb(val, DATA(base)); |
1da177e4 | 123 | spin_unlock_irqrestore(&aha1542_lock, flags); |
0c2b6481 | 124 | return 0; |
1da177e4 | 125 | } |
0c2b6481 OZ |
126 | } |
127 | ||
cad2fc72 | 128 | static int aha1542_out(unsigned int base, u8 *buf, int len) |
0c2b6481 OZ |
129 | { |
130 | unsigned long flags; | |
131 | ||
132 | spin_lock_irqsave(&aha1542_lock, flags); | |
133 | while (len--) { | |
134 | if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) { | |
135 | spin_unlock_irqrestore(&aha1542_lock, flags); | |
136 | printk(KERN_ERR "aha1542_out failed(%d): ", len + 1); | |
137 | return 1; | |
138 | } | |
cad2fc72 | 139 | outb(*buf++, DATA(base)); |
0c2b6481 OZ |
140 | } |
141 | spin_unlock_irqrestore(&aha1542_lock, flags); | |
23e6940a OZ |
142 | if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0)) |
143 | return 1; | |
0c2b6481 | 144 | |
1da177e4 | 145 | return 0; |
1da177e4 LT |
146 | } |
147 | ||
148 | /* Only used at boot time, so we do not need to worry about latency as much | |
149 | here */ | |
150 | ||
cad2fc72 | 151 | static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout) |
1da177e4 LT |
152 | { |
153 | unsigned long flags; | |
154 | ||
155 | spin_lock_irqsave(&aha1542_lock, flags); | |
156 | while (len--) { | |
a13b3722 OZ |
157 | if (!wait_mask(STATUS(base), DF, DF, 0, timeout)) { |
158 | spin_unlock_irqrestore(&aha1542_lock, flags); | |
159 | if (timeout == 0) | |
160 | printk(KERN_ERR "aha1542_in failed(%d): ", len + 1); | |
161 | return 1; | |
162 | } | |
cad2fc72 | 163 | *buf++ = inb(DATA(base)); |
1da177e4 LT |
164 | } |
165 | spin_unlock_irqrestore(&aha1542_lock, flags); | |
166 | return 0; | |
1da177e4 LT |
167 | } |
168 | ||
169 | static int makecode(unsigned hosterr, unsigned scsierr) | |
170 | { | |
171 | switch (hosterr) { | |
172 | case 0x0: | |
173 | case 0xa: /* Linked command complete without error and linked normally */ | |
174 | case 0xb: /* Linked command complete without error, interrupt generated */ | |
175 | hosterr = 0; | |
176 | break; | |
177 | ||
178 | case 0x11: /* Selection time out-The initiator selection or target | |
179 | reselection was not complete within the SCSI Time out period */ | |
180 | hosterr = DID_TIME_OUT; | |
181 | break; | |
182 | ||
183 | case 0x12: /* Data overrun/underrun-The target attempted to transfer more data | |
184 | than was allocated by the Data Length field or the sum of the | |
185 | Scatter / Gather Data Length fields. */ | |
186 | ||
187 | case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */ | |
188 | ||
189 | case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was | |
190 | invalid. This usually indicates a software failure. */ | |
191 | ||
192 | case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid. | |
193 | This usually indicates a software failure. */ | |
194 | ||
195 | case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set | |
196 | of linked CCB's does not specify the same logical unit number as | |
197 | the first. */ | |
198 | case 0x18: /* Invalid Target Direction received from Host-The direction of a | |
199 | Target Mode CCB was invalid. */ | |
200 | ||
201 | case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was | |
202 | received to service data transfer between the same target LUN | |
203 | and initiator SCSI ID in the same direction. */ | |
204 | ||
205 | case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero | |
206 | length segment or invalid segment list boundaries was received. | |
207 | A CCB parameter was invalid. */ | |
208 | DEB(printk("Aha1542: %x %x\n", hosterr, scsierr)); | |
209 | hosterr = DID_ERROR; /* Couldn't find any better */ | |
210 | break; | |
211 | ||
212 | case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus | |
213 | phase sequence was requested by the target. The host adapter | |
214 | will generate a SCSI Reset Condition, notifying the host with | |
215 | a SCRD interrupt */ | |
216 | hosterr = DID_RESET; | |
217 | break; | |
218 | default: | |
219 | printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr); | |
220 | break; | |
221 | } | |
222 | return scsierr | (hosterr << 16); | |
223 | } | |
224 | ||
c2532f68 | 225 | static int aha1542_test_port(int bse, struct Scsi_Host *sh) |
1da177e4 | 226 | { |
cb5b570c | 227 | u8 inquiry_result[4]; |
cad2fc72 | 228 | int i; |
1da177e4 LT |
229 | |
230 | /* Quick and dirty test for presence of the card. */ | |
231 | if (inb(STATUS(bse)) == 0xff) | |
232 | return 0; | |
233 | ||
234 | /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */ | |
235 | ||
1da177e4 LT |
236 | /* In case some other card was probing here, reset interrupts */ |
237 | aha1542_intr_reset(bse); /* reset interrupts, so they don't block */ | |
238 | ||
239 | outb(SRST | IRST /*|SCRST */ , CONTROL(bse)); | |
240 | ||
241 | mdelay(20); /* Wait a little bit for things to settle down. */ | |
242 | ||
1da177e4 | 243 | /* Expect INIT and IDLE, any of the others are bad */ |
2093bfa1 | 244 | if (!wait_mask(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) |
a13b3722 | 245 | return 0; |
1da177e4 | 246 | |
1da177e4 LT |
247 | /* Shouldn't have generated any interrupts during reset */ |
248 | if (inb(INTRFLAGS(bse)) & INTRMASK) | |
a13b3722 | 249 | return 0; |
1da177e4 | 250 | |
1da177e4 LT |
251 | /* Perform a host adapter inquiry instead so we do not need to set |
252 | up the mailboxes ahead of time */ | |
253 | ||
0c2b6481 | 254 | aha1542_outb(bse, CMD_INQUIRY); |
1da177e4 | 255 | |
cad2fc72 | 256 | for (i = 0; i < 4; i++) { |
2093bfa1 | 257 | if (!wait_mask(STATUS(bse), DF, DF, 0, 0)) |
a13b3722 | 258 | return 0; |
cad2fc72 | 259 | inquiry_result[i] = inb(DATA(bse)); |
1da177e4 LT |
260 | } |
261 | ||
1da177e4 LT |
262 | /* Reading port should reset DF */ |
263 | if (inb(STATUS(bse)) & DF) | |
a13b3722 | 264 | return 0; |
1da177e4 | 265 | |
1da177e4 | 266 | /* When HACC, command is completed, and we're though testing */ |
2093bfa1 | 267 | if (!wait_mask(INTRFLAGS(bse), HACC, HACC, 0, 0)) |
a13b3722 | 268 | return 0; |
1da177e4 | 269 | |
1da177e4 LT |
270 | /* Clear interrupts */ |
271 | outb(IRST, CONTROL(bse)); | |
272 | ||
bdebe224 | 273 | return 1; |
1da177e4 LT |
274 | } |
275 | ||
1da177e4 | 276 | /* A "high" level interrupt handler */ |
c2532f68 | 277 | static void aha1542_intr_handle(struct Scsi_Host *sh) |
1da177e4 | 278 | { |
c2532f68 | 279 | struct aha1542_hostdata *aha1542 = shost_priv(sh); |
55b28f9f | 280 | void (*my_done)(struct scsi_cmnd *) = NULL; |
1da177e4 LT |
281 | int errstatus, mbi, mbo, mbistatus; |
282 | int number_serviced; | |
283 | unsigned long flags; | |
55b28f9f | 284 | struct scsi_cmnd *tmp_cmd; |
1da177e4 | 285 | int flag; |
e98878f7 OZ |
286 | struct mailbox *mb = aha1542->mb; |
287 | struct ccb *ccb = aha1542->ccb; | |
1da177e4 LT |
288 | |
289 | #ifdef DEBUG | |
290 | { | |
c2532f68 | 291 | flag = inb(INTRFLAGS(sh->io_port)); |
1da177e4 LT |
292 | printk(KERN_DEBUG "aha1542_intr_handle: "); |
293 | if (!(flag & ANYINTR)) | |
294 | printk("no interrupt?"); | |
295 | if (flag & MBIF) | |
296 | printk("MBIF "); | |
297 | if (flag & MBOA) | |
298 | printk("MBOF "); | |
299 | if (flag & HACC) | |
300 | printk("HACC "); | |
301 | if (flag & SCRD) | |
302 | printk("SCRD "); | |
c2532f68 | 303 | printk("status %02x\n", inb(STATUS(sh->io_port))); |
1da177e4 LT |
304 | }; |
305 | #endif | |
306 | number_serviced = 0; | |
1da177e4 LT |
307 | |
308 | while (1 == 1) { | |
c2532f68 | 309 | flag = inb(INTRFLAGS(sh->io_port)); |
1da177e4 LT |
310 | |
311 | /* Check for unusual interrupts. If any of these happen, we should | |
312 | probably do something special, but for now just printing a message | |
313 | is sufficient. A SCSI reset detected is something that we really | |
314 | need to deal with in some way. */ | |
315 | if (flag & ~MBIF) { | |
316 | if (flag & MBOA) | |
317 | printk("MBOF "); | |
318 | if (flag & HACC) | |
319 | printk("HACC "); | |
dfd7c991 | 320 | if (flag & SCRD) |
1da177e4 | 321 | printk("SCRD "); |
1da177e4 | 322 | } |
c2532f68 | 323 | aha1542_intr_reset(sh->io_port); |
1da177e4 LT |
324 | |
325 | spin_lock_irqsave(&aha1542_lock, flags); | |
e98878f7 | 326 | mbi = aha1542->aha1542_last_mbi_used + 1; |
1da177e4 LT |
327 | if (mbi >= 2 * AHA1542_MAILBOXES) |
328 | mbi = AHA1542_MAILBOXES; | |
329 | ||
330 | do { | |
331 | if (mb[mbi].status != 0) | |
332 | break; | |
333 | mbi++; | |
334 | if (mbi >= 2 * AHA1542_MAILBOXES) | |
335 | mbi = AHA1542_MAILBOXES; | |
e98878f7 | 336 | } while (mbi != aha1542->aha1542_last_mbi_used); |
1da177e4 LT |
337 | |
338 | if (mb[mbi].status == 0) { | |
339 | spin_unlock_irqrestore(&aha1542_lock, flags); | |
340 | /* Hmm, no mail. Must have read it the last time around */ | |
dfd7c991 | 341 | if (!number_serviced) |
1da177e4 | 342 | printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n"); |
1da177e4 LT |
343 | return; |
344 | }; | |
345 | ||
10be6250 | 346 | mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb); |
1da177e4 LT |
347 | mbistatus = mb[mbi].status; |
348 | mb[mbi].status = 0; | |
e98878f7 | 349 | aha1542->aha1542_last_mbi_used = mbi; |
1da177e4 LT |
350 | spin_unlock_irqrestore(&aha1542_lock, flags); |
351 | ||
352 | #ifdef DEBUG | |
353 | { | |
354 | if (ccb[mbo].tarstat | ccb[mbo].hastat) | |
355 | printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n", | |
356 | ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status); | |
357 | }; | |
358 | #endif | |
359 | ||
360 | if (mbistatus == 3) | |
361 | continue; /* Aborted command not found */ | |
362 | ||
363 | #ifdef DEBUG | |
364 | printk(KERN_DEBUG "...done %d %d\n", mbo, mbi); | |
365 | #endif | |
366 | ||
55b28f9f | 367 | tmp_cmd = aha1542->int_cmds[mbo]; |
1da177e4 | 368 | |
55b28f9f | 369 | if (!tmp_cmd || !tmp_cmd->scsi_done) { |
1da177e4 LT |
370 | printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n"); |
371 | printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat, | |
372 | ccb[mbo].hastat, ccb[mbo].idlun, mbo); | |
373 | return; | |
374 | } | |
55b28f9f OZ |
375 | my_done = tmp_cmd->scsi_done; |
376 | kfree(tmp_cmd->host_scribble); | |
377 | tmp_cmd->host_scribble = NULL; | |
1da177e4 LT |
378 | /* Fetch the sense data, and tuck it away, in the required slot. The |
379 | Adaptec automatically fetches it, and there is no guarantee that | |
380 | we will still have it in the cdb when we come back */ | |
381 | if (ccb[mbo].tarstat == 2) | |
55b28f9f | 382 | memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen], |
b80ca4f7 | 383 | SCSI_SENSE_BUFFERSIZE); |
1da177e4 LT |
384 | |
385 | ||
386 | /* is there mail :-) */ | |
387 | ||
388 | /* more error checking left out here */ | |
389 | if (mbistatus != 1) | |
390 | /* This is surely wrong, but I don't know what's right */ | |
391 | errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat); | |
392 | else | |
393 | errstatus = 0; | |
394 | ||
395 | #ifdef DEBUG | |
396 | if (errstatus) | |
397 | printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus, | |
398 | ccb[mbo].hastat, ccb[mbo].tarstat); | |
399 | #endif | |
400 | ||
401 | if (ccb[mbo].tarstat == 2) { | |
402 | #ifdef DEBUG | |
403 | int i; | |
404 | #endif | |
405 | DEB(printk("aha1542_intr_handle: sense:")); | |
406 | #ifdef DEBUG | |
407 | for (i = 0; i < 12; i++) | |
408 | printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]); | |
409 | printk("\n"); | |
410 | #endif | |
411 | /* | |
412 | DEB(printk("aha1542_intr_handle: buf:")); | |
413 | for (i = 0; i < bufflen; i++) | |
414 | printk("%02x ", ((unchar *)buff)[i]); | |
415 | printk("\n"); | |
416 | */ | |
417 | } | |
418 | DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus)); | |
55b28f9f OZ |
419 | tmp_cmd->result = errstatus; |
420 | aha1542->int_cmds[mbo] = NULL; /* This effectively frees up the mailbox slot, as | |
e98878f7 | 421 | far as queuecommand is concerned */ |
55b28f9f | 422 | my_done(tmp_cmd); |
1da177e4 LT |
423 | number_serviced++; |
424 | }; | |
425 | } | |
426 | ||
09a44833 OZ |
427 | /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */ |
428 | static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id) | |
429 | { | |
430 | unsigned long flags; | |
c2532f68 | 431 | struct Scsi_Host *sh = dev_id; |
09a44833 | 432 | |
c2532f68 OZ |
433 | spin_lock_irqsave(sh->host_lock, flags); |
434 | aha1542_intr_handle(sh); | |
435 | spin_unlock_irqrestore(sh->host_lock, flags); | |
09a44833 OZ |
436 | return IRQ_HANDLED; |
437 | } | |
438 | ||
55b28f9f | 439 | static int aha1542_queuecommand_lck(struct scsi_cmnd *cmd, void (*done) (struct scsi_cmnd *)) |
1da177e4 | 440 | { |
55b28f9f | 441 | struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host); |
cb5b570c | 442 | u8 direction; |
55b28f9f OZ |
443 | u8 target = cmd->device->id; |
444 | u8 lun = cmd->device->lun; | |
1da177e4 | 445 | unsigned long flags; |
55b28f9f | 446 | int bufflen = scsi_bufflen(cmd); |
1da177e4 | 447 | int mbo; |
e98878f7 OZ |
448 | struct mailbox *mb = aha1542->mb; |
449 | struct ccb *ccb = aha1542->ccb; | |
1da177e4 LT |
450 | |
451 | DEB(int i); | |
452 | ||
1da177e4 | 453 | DEB(if (target > 1) { |
55b28f9f OZ |
454 | cmd->result = DID_TIME_OUT << 16; |
455 | done(cmd); return 0; | |
1da177e4 LT |
456 | } |
457 | ); | |
458 | ||
55b28f9f | 459 | if (*cmd->cmnd == REQUEST_SENSE) { |
1da177e4 | 460 | /* Don't do the command - we have the sense data already */ |
55b28f9f OZ |
461 | cmd->result = 0; |
462 | done(cmd); | |
1da177e4 LT |
463 | return 0; |
464 | } | |
465 | #ifdef DEBUG | |
55b28f9f OZ |
466 | if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10) |
467 | i = xscsi2int(cmd->cmnd + 2); | |
468 | else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6) | |
469 | i = scsi2int(cmd->cmnd + 2); | |
1da177e4 LT |
470 | else |
471 | i = -1; | |
472 | if (done) | |
55b28f9f | 473 | printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd->cmnd, i, bufflen); |
1da177e4 | 474 | else |
55b28f9f | 475 | printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd->cmnd, i, bufflen); |
1da177e4 | 476 | printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:"); |
55b28f9f OZ |
477 | for (i = 0; i < cmd->cmd_len; i++) |
478 | printk("%02x ", cmd->cmnd[i]); | |
1da177e4 | 479 | printk("\n"); |
55b28f9f | 480 | if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6) |
1da177e4 LT |
481 | return 0; /* we are still testing, so *don't* write */ |
482 | #endif | |
483 | /* Use the outgoing mailboxes in a round-robin fashion, because this | |
484 | is how the host adapter will scan for them */ | |
485 | ||
486 | spin_lock_irqsave(&aha1542_lock, flags); | |
e98878f7 | 487 | mbo = aha1542->aha1542_last_mbo_used + 1; |
1da177e4 LT |
488 | if (mbo >= AHA1542_MAILBOXES) |
489 | mbo = 0; | |
490 | ||
491 | do { | |
55b28f9f | 492 | if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL) |
1da177e4 LT |
493 | break; |
494 | mbo++; | |
495 | if (mbo >= AHA1542_MAILBOXES) | |
496 | mbo = 0; | |
e98878f7 | 497 | } while (mbo != aha1542->aha1542_last_mbo_used); |
1da177e4 | 498 | |
55b28f9f | 499 | if (mb[mbo].status || aha1542->int_cmds[mbo]) |
1da177e4 LT |
500 | panic("Unable to find empty mailbox for aha1542.\n"); |
501 | ||
55b28f9f | 502 | aha1542->int_cmds[mbo] = cmd; /* This will effectively prevent someone else from |
e98878f7 | 503 | screwing with this cdb. */ |
1da177e4 | 504 | |
e98878f7 | 505 | aha1542->aha1542_last_mbo_used = mbo; |
1da177e4 LT |
506 | spin_unlock_irqrestore(&aha1542_lock, flags); |
507 | ||
508 | #ifdef DEBUG | |
509 | printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done); | |
510 | #endif | |
511 | ||
10be6250 | 512 | any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */ |
1da177e4 LT |
513 | |
514 | memset(&ccb[mbo], 0, sizeof(struct ccb)); | |
515 | ||
55b28f9f | 516 | ccb[mbo].cdblen = cmd->cmd_len; |
1da177e4 LT |
517 | |
518 | direction = 0; | |
55b28f9f | 519 | if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6) |
1da177e4 | 520 | direction = 8; |
55b28f9f | 521 | else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6) |
1da177e4 LT |
522 | direction = 16; |
523 | ||
55b28f9f | 524 | memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen); |
1da177e4 | 525 | |
fc3fdfcc | 526 | if (bufflen) { |
51cf2249 | 527 | struct scatterlist *sg; |
1da177e4 LT |
528 | struct chain *cptr; |
529 | #ifdef DEBUG | |
530 | unsigned char *ptr; | |
531 | #endif | |
55b28f9f | 532 | int i, sg_count = scsi_sg_count(cmd); |
1da177e4 | 533 | ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */ |
55b28f9f | 534 | cmd->host_scribble = kmalloc(sizeof(*cptr)*sg_count, |
fc3fdfcc | 535 | GFP_KERNEL | GFP_DMA); |
55b28f9f | 536 | cptr = (struct chain *) cmd->host_scribble; |
1da177e4 LT |
537 | if (cptr == NULL) { |
538 | /* free the claimed mailbox slot */ | |
55b28f9f | 539 | aha1542->int_cmds[mbo] = NULL; |
1da177e4 LT |
540 | return SCSI_MLQUEUE_HOST_BUSY; |
541 | } | |
55b28f9f | 542 | scsi_for_each_sg(cmd, sg, sg_count, i) { |
10be6250 OZ |
543 | any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg)) |
544 | + sg->offset); | |
51cf2249 | 545 | any2scsi(cptr[i].datalen, sg->length); |
1da177e4 | 546 | }; |
fc3fdfcc | 547 | any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain)); |
10be6250 | 548 | any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr)); |
1da177e4 LT |
549 | #ifdef DEBUG |
550 | printk("cptr %x: ", cptr); | |
551 | ptr = (unsigned char *) cptr; | |
552 | for (i = 0; i < 18; i++) | |
553 | printk("%02x ", ptr[i]); | |
554 | #endif | |
555 | } else { | |
556 | ccb[mbo].op = 0; /* SCSI Initiator Command */ | |
55b28f9f | 557 | cmd->host_scribble = NULL; |
fc3fdfcc BH |
558 | any2scsi(ccb[mbo].datalen, 0); |
559 | any2scsi(ccb[mbo].dataptr, 0); | |
1da177e4 LT |
560 | }; |
561 | ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */ | |
562 | ccb[mbo].rsalen = 16; | |
563 | ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0; | |
564 | ccb[mbo].commlinkid = 0; | |
565 | ||
566 | #ifdef DEBUG | |
567 | { | |
568 | int i; | |
569 | printk(KERN_DEBUG "aha1542_command: sending.. "); | |
570 | for (i = 0; i < sizeof(ccb[mbo]) - 10; i++) | |
cb5b570c | 571 | printk("%02x ", ((u8 *) &ccb[mbo])[i]); |
1da177e4 LT |
572 | }; |
573 | #endif | |
574 | ||
575 | if (done) { | |
f232d538 | 576 | DEB(printk("aha1542_queuecommand: now waiting for interrupt ")); |
55b28f9f | 577 | cmd->scsi_done = done; |
1da177e4 | 578 | mb[mbo].status = 1; |
55b28f9f | 579 | aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI); |
1da177e4 LT |
580 | } else |
581 | printk("aha1542_queuecommand: done can't be NULL\n"); | |
582 | ||
583 | return 0; | |
584 | } | |
585 | ||
f281233d JG |
586 | static DEF_SCSI_QCMD(aha1542_queuecommand) |
587 | ||
1da177e4 | 588 | /* Initialize mailboxes */ |
c2532f68 | 589 | static void setup_mailboxes(int bse, struct Scsi_Host *sh) |
1da177e4 | 590 | { |
c2532f68 | 591 | struct aha1542_hostdata *aha1542 = shost_priv(sh); |
1da177e4 | 592 | int i; |
e98878f7 OZ |
593 | struct mailbox *mb = aha1542->mb; |
594 | struct ccb *ccb = aha1542->ccb; | |
1da177e4 | 595 | |
cad2fc72 | 596 | u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0}; |
1da177e4 | 597 | |
1da177e4 LT |
598 | for (i = 0; i < AHA1542_MAILBOXES; i++) { |
599 | mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0; | |
10be6250 | 600 | any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i])); |
1da177e4 LT |
601 | }; |
602 | aha1542_intr_reset(bse); /* reset interrupts, so they don't block */ | |
cad2fc72 OZ |
603 | any2scsi((mb_cmd + 2), isa_virt_to_bus(mb)); |
604 | if (aha1542_out(bse, mb_cmd, 5)) | |
1da177e4 | 605 | printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n"); |
1da177e4 LT |
606 | aha1542_intr_reset(bse); |
607 | } | |
608 | ||
3a70c006 | 609 | static int aha1542_getconfig(int base_io, unsigned int *irq_level, unsigned char *dma_chan, unsigned int *scsi_id) |
1da177e4 | 610 | { |
cb5b570c | 611 | u8 inquiry_result[3]; |
1da177e4 LT |
612 | int i; |
613 | i = inb(STATUS(base_io)); | |
614 | if (i & DF) { | |
615 | i = inb(DATA(base_io)); | |
616 | }; | |
0c2b6481 | 617 | aha1542_outb(base_io, CMD_RETCONF); |
f8846be3 | 618 | aha1542_in(base_io, inquiry_result, 3, 0); |
2093bfa1 | 619 | if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0)) |
1da177e4 | 620 | printk(KERN_ERR "aha1542_detect: query board settings\n"); |
1da177e4 LT |
621 | aha1542_intr_reset(base_io); |
622 | switch (inquiry_result[0]) { | |
623 | case 0x80: | |
624 | *dma_chan = 7; | |
625 | break; | |
626 | case 0x40: | |
627 | *dma_chan = 6; | |
628 | break; | |
629 | case 0x20: | |
630 | *dma_chan = 5; | |
631 | break; | |
632 | case 0x01: | |
633 | *dma_chan = 0; | |
634 | break; | |
635 | case 0: | |
636 | /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel. | |
637 | Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */ | |
638 | *dma_chan = 0xFF; | |
639 | break; | |
640 | default: | |
641 | printk(KERN_ERR "Unable to determine Adaptec DMA priority. Disabling board\n"); | |
642 | return -1; | |
643 | }; | |
644 | switch (inquiry_result[1]) { | |
645 | case 0x40: | |
646 | *irq_level = 15; | |
647 | break; | |
648 | case 0x20: | |
649 | *irq_level = 14; | |
650 | break; | |
651 | case 0x8: | |
652 | *irq_level = 12; | |
653 | break; | |
654 | case 0x4: | |
655 | *irq_level = 11; | |
656 | break; | |
657 | case 0x2: | |
658 | *irq_level = 10; | |
659 | break; | |
660 | case 0x1: | |
661 | *irq_level = 9; | |
662 | break; | |
663 | default: | |
664 | printk(KERN_ERR "Unable to determine Adaptec IRQ level. Disabling board\n"); | |
665 | return -1; | |
666 | }; | |
667 | *scsi_id = inquiry_result[2] & 7; | |
668 | return 0; | |
669 | } | |
670 | ||
671 | /* This function should only be called for 1542C boards - we can detect | |
672 | the special firmware settings and unlock the board */ | |
673 | ||
643a7c43 | 674 | static int aha1542_mbenable(int base) |
1da177e4 | 675 | { |
cb5b570c OZ |
676 | static u8 mbenable_cmd[3]; |
677 | static u8 mbenable_result[2]; | |
1da177e4 LT |
678 | int retval; |
679 | ||
680 | retval = BIOS_TRANSLATION_6432; | |
681 | ||
0c2b6481 | 682 | aha1542_outb(base, CMD_EXTBIOS); |
f8846be3 | 683 | if (aha1542_in(base, mbenable_result, 2, 100)) |
1da177e4 | 684 | return retval; |
2093bfa1 OZ |
685 | if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 100)) |
686 | goto fail; | |
1da177e4 LT |
687 | aha1542_intr_reset(base); |
688 | ||
689 | if ((mbenable_result[0] & 0x08) || mbenable_result[1]) { | |
690 | mbenable_cmd[0] = CMD_MBENABLE; | |
691 | mbenable_cmd[1] = 0; | |
692 | mbenable_cmd[2] = mbenable_result[1]; | |
693 | ||
694 | if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03)) | |
695 | retval = BIOS_TRANSLATION_25563; | |
696 | ||
23e6940a | 697 | if (aha1542_out(base, mbenable_cmd, 3)) |
2093bfa1 | 698 | goto fail; |
1da177e4 LT |
699 | }; |
700 | while (0) { | |
701 | fail: | |
702 | printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n"); | |
703 | } | |
704 | aha1542_intr_reset(base); | |
705 | return retval; | |
706 | } | |
707 | ||
708 | /* Query the board to find out if it is a 1542 or a 1740, or whatever. */ | |
643a7c43 | 709 | static int aha1542_query(int base_io, int *transl) |
1da177e4 | 710 | { |
cb5b570c | 711 | u8 inquiry_result[4]; |
1da177e4 LT |
712 | int i; |
713 | i = inb(STATUS(base_io)); | |
714 | if (i & DF) { | |
715 | i = inb(DATA(base_io)); | |
716 | }; | |
0c2b6481 | 717 | aha1542_outb(base_io, CMD_INQUIRY); |
f8846be3 | 718 | aha1542_in(base_io, inquiry_result, 4, 0); |
2093bfa1 | 719 | if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0)) |
1da177e4 | 720 | printk(KERN_ERR "aha1542_detect: query card type\n"); |
1da177e4 LT |
721 | aha1542_intr_reset(base_io); |
722 | ||
723 | *transl = BIOS_TRANSLATION_6432; /* Default case */ | |
724 | ||
725 | /* For an AHA1740 series board, we ignore the board since there is a | |
726 | hardware bug which can lead to wrong blocks being returned if the board | |
727 | is operating in the 1542 emulation mode. Since there is an extended mode | |
728 | driver, we simply ignore the board and let the 1740 driver pick it up. | |
729 | */ | |
730 | ||
731 | if (inquiry_result[0] == 0x43) { | |
732 | printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n"); | |
733 | return 1; | |
734 | }; | |
735 | ||
736 | /* Always call this - boards that do not support extended bios translation | |
737 | will ignore the command, and we will set the proper default */ | |
738 | ||
739 | *transl = aha1542_mbenable(base_io); | |
740 | ||
741 | return 0; | |
742 | } | |
743 | ||
f71429ab | 744 | static u8 dma_speed_hw(int dma_speed) |
1da177e4 | 745 | { |
f71429ab OZ |
746 | switch (dma_speed) { |
747 | case 5: | |
748 | return 0x00; | |
749 | case 6: | |
750 | return 0x04; | |
751 | case 7: | |
752 | return 0x01; | |
753 | case 8: | |
754 | return 0x02; | |
755 | case 10: | |
756 | return 0x03; | |
1da177e4 | 757 | } |
1da177e4 | 758 | |
f71429ab | 759 | return 0xff; /* invalid */ |
1da177e4 LT |
760 | } |
761 | ||
f71429ab OZ |
762 | /* Set the Bus on/off-times as not to ruin floppy performance */ |
763 | static void aha1542_set_bus_times(int indx) | |
1da177e4 | 764 | { |
f71429ab | 765 | unsigned int base_io = io[indx]; |
1da177e4 | 766 | |
f71429ab OZ |
767 | if (bus_on[indx] > 0) { |
768 | u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on[indx], 2, 15) }; | |
1da177e4 | 769 | |
f71429ab OZ |
770 | aha1542_intr_reset(base_io); |
771 | if (aha1542_out(base_io, oncmd, 2)) | |
772 | goto fail; | |
773 | } | |
1da177e4 | 774 | |
f71429ab OZ |
775 | if (bus_off[indx] > 0) { |
776 | u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off[indx], 1, 64) }; | |
1da177e4 | 777 | |
f71429ab OZ |
778 | aha1542_intr_reset(base_io); |
779 | if (aha1542_out(base_io, offcmd, 2)) | |
780 | goto fail; | |
781 | } | |
1da177e4 | 782 | |
f71429ab OZ |
783 | if (dma_speed_hw(dma_speed[indx]) != 0xff) { |
784 | u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed[indx]) }; | |
b847fd0d | 785 | |
b847fd0d | 786 | aha1542_intr_reset(base_io); |
23e6940a | 787 | if (aha1542_out(base_io, dmacmd, 2)) |
b847fd0d OZ |
788 | goto fail; |
789 | } | |
790 | aha1542_intr_reset(base_io); | |
791 | return; | |
792 | fail: | |
793 | printk(KERN_ERR "setting bus on/off-time failed\n"); | |
794 | aha1542_intr_reset(base_io); | |
795 | } | |
796 | ||
1da177e4 | 797 | /* return non-zero on detection */ |
643a7c43 | 798 | static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx) |
1da177e4 | 799 | { |
f71429ab | 800 | unsigned int base_io = io[indx]; |
c2532f68 | 801 | struct Scsi_Host *sh; |
e98878f7 | 802 | struct aha1542_hostdata *aha1542; |
1da177e4 | 803 | |
3a70c006 OZ |
804 | if (base_io == 0) |
805 | return NULL; | |
1da177e4 | 806 | |
3a70c006 OZ |
807 | if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542")) |
808 | return NULL; | |
1da177e4 | 809 | |
c2532f68 OZ |
810 | sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata)); |
811 | if (!sh) | |
3a70c006 | 812 | goto release; |
c2532f68 | 813 | aha1542 = shost_priv(sh); |
b847fd0d | 814 | |
c2532f68 | 815 | if (!aha1542_test_port(base_io, sh)) |
3a70c006 | 816 | goto unregister; |
1da177e4 | 817 | |
3a70c006 OZ |
818 | aha1542_set_bus_times(indx); |
819 | if (aha1542_query(base_io, &aha1542->bios_translation)) | |
820 | goto unregister; | |
c2532f68 | 821 | if (aha1542_getconfig(base_io, &sh->irq, &sh->dma_channel, &sh->this_id) == -1) |
3a70c006 | 822 | goto unregister; |
1da177e4 | 823 | |
c2532f68 OZ |
824 | printk(KERN_INFO "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d", sh->this_id, base_io, sh->irq); |
825 | if (sh->dma_channel != 0xFF) | |
826 | printk(", DMA %d", sh->dma_channel); | |
3a70c006 OZ |
827 | printk("\n"); |
828 | if (aha1542->bios_translation == BIOS_TRANSLATION_25563) | |
829 | printk(KERN_INFO "aha1542.c: Using extended bios translation\n"); | |
1da177e4 | 830 | |
c2532f68 | 831 | setup_mailboxes(base_io, sh); |
1da177e4 | 832 | |
c2532f68 OZ |
833 | if (request_irq(sh->irq, do_aha1542_intr_handle, 0, |
834 | "aha1542", sh)) { | |
3a70c006 OZ |
835 | printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n"); |
836 | goto unregister; | |
837 | } | |
c2532f68 OZ |
838 | if (sh->dma_channel != 0xFF) { |
839 | if (request_dma(sh->dma_channel, "aha1542")) { | |
3a70c006 OZ |
840 | printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n"); |
841 | goto free_irq; | |
842 | } | |
c2532f68 OZ |
843 | if (sh->dma_channel == 0 || sh->dma_channel >= 5) { |
844 | set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE); | |
845 | enable_dma(sh->dma_channel); | |
3a70c006 OZ |
846 | } |
847 | } | |
1da177e4 | 848 | |
c2532f68 OZ |
849 | sh->unique_id = base_io; |
850 | sh->io_port = base_io; | |
851 | sh->n_io_port = AHA1542_REGION_SIZE; | |
3a70c006 OZ |
852 | aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1; |
853 | aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1; | |
1da177e4 | 854 | |
c2532f68 | 855 | if (scsi_add_host(sh, pdev)) |
3a70c006 | 856 | goto free_dma; |
1da177e4 | 857 | |
c2532f68 | 858 | scsi_scan_host(sh); |
1da177e4 | 859 | |
c2532f68 | 860 | return sh; |
3a70c006 | 861 | free_dma: |
c2532f68 OZ |
862 | if (sh->dma_channel != 0xff) |
863 | free_dma(sh->dma_channel); | |
3a70c006 | 864 | free_irq: |
c2532f68 | 865 | free_irq(sh->irq, sh); |
3a70c006 | 866 | unregister: |
c2532f68 | 867 | scsi_host_put(sh); |
3a70c006 OZ |
868 | release: |
869 | release_region(base_io, AHA1542_REGION_SIZE); | |
1da177e4 | 870 | |
643a7c43 | 871 | return NULL; |
1da177e4 LT |
872 | } |
873 | ||
c2532f68 | 874 | static int aha1542_release(struct Scsi_Host *sh) |
1da177e4 | 875 | { |
c2532f68 OZ |
876 | scsi_remove_host(sh); |
877 | if (sh->dma_channel != 0xff) | |
878 | free_dma(sh->dma_channel); | |
879 | if (sh->irq) | |
880 | free_irq(sh->irq, sh); | |
881 | if (sh->io_port && sh->n_io_port) | |
882 | release_region(sh->io_port, sh->n_io_port); | |
883 | scsi_host_put(sh); | |
1da177e4 LT |
884 | return 0; |
885 | } | |
886 | ||
1da177e4 | 887 | |
1da177e4 LT |
888 | /* |
889 | * This is a device reset. This is handled by sending a special command | |
890 | * to the device. | |
891 | */ | |
55b28f9f | 892 | static int aha1542_dev_reset(struct scsi_cmnd *cmd) |
1da177e4 | 893 | { |
55b28f9f | 894 | struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host); |
1da177e4 | 895 | unsigned long flags; |
e98878f7 | 896 | struct mailbox *mb = aha1542->mb; |
55b28f9f OZ |
897 | u8 target = cmd->device->id; |
898 | u8 lun = cmd->device->lun; | |
1da177e4 | 899 | int mbo; |
e98878f7 | 900 | struct ccb *ccb = aha1542->ccb; |
1da177e4 | 901 | |
1da177e4 | 902 | spin_lock_irqsave(&aha1542_lock, flags); |
e98878f7 | 903 | mbo = aha1542->aha1542_last_mbo_used + 1; |
1da177e4 LT |
904 | if (mbo >= AHA1542_MAILBOXES) |
905 | mbo = 0; | |
906 | ||
907 | do { | |
55b28f9f | 908 | if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL) |
1da177e4 LT |
909 | break; |
910 | mbo++; | |
911 | if (mbo >= AHA1542_MAILBOXES) | |
912 | mbo = 0; | |
e98878f7 | 913 | } while (mbo != aha1542->aha1542_last_mbo_used); |
1da177e4 | 914 | |
55b28f9f | 915 | if (mb[mbo].status || aha1542->int_cmds[mbo]) |
1da177e4 LT |
916 | panic("Unable to find empty mailbox for aha1542.\n"); |
917 | ||
55b28f9f | 918 | aha1542->int_cmds[mbo] = cmd; /* This will effectively |
e98878f7 OZ |
919 | prevent someone else from |
920 | screwing with this cdb. */ | |
1da177e4 | 921 | |
e98878f7 | 922 | aha1542->aha1542_last_mbo_used = mbo; |
1da177e4 LT |
923 | spin_unlock_irqrestore(&aha1542_lock, flags); |
924 | ||
10be6250 | 925 | any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */ |
1da177e4 LT |
926 | |
927 | memset(&ccb[mbo], 0, sizeof(struct ccb)); | |
928 | ||
929 | ccb[mbo].op = 0x81; /* BUS DEVICE RESET */ | |
930 | ||
931 | ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */ | |
932 | ||
933 | ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0; | |
934 | ccb[mbo].commlinkid = 0; | |
935 | ||
936 | /* | |
937 | * Now tell the 1542 to flush all pending commands for this | |
938 | * target | |
939 | */ | |
55b28f9f | 940 | aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI); |
1da177e4 | 941 | |
55b28f9f | 942 | scmd_printk(KERN_WARNING, cmd, |
017560fc | 943 | "Trying device reset for target\n"); |
1da177e4 LT |
944 | |
945 | return SUCCESS; | |
1da177e4 LT |
946 | } |
947 | ||
55b28f9f | 948 | static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd) |
1da177e4 | 949 | { |
55b28f9f | 950 | struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host); |
1da177e4 LT |
951 | int i; |
952 | ||
953 | /* | |
954 | * This does a scsi reset for all devices on the bus. | |
955 | * In principle, we could also reset the 1542 - should | |
956 | * we do this? Try this first, and we can add that later | |
957 | * if it turns out to be useful. | |
958 | */ | |
55b28f9f | 959 | outb(reset_cmd, CONTROL(cmd->device->host->io_port)); |
1da177e4 LT |
960 | |
961 | /* | |
962 | * Wait for the thing to settle down a bit. Unfortunately | |
963 | * this is going to basically lock up the machine while we | |
964 | * wait for this to complete. To be 100% correct, we need to | |
965 | * check for timeout, and if we are doing something like this | |
966 | * we are pretty desperate anyways. | |
967 | */ | |
1da177e4 | 968 | ssleep(4); |
55b28f9f | 969 | spin_lock_irq(cmd->device->host->host_lock); |
1da177e4 | 970 | |
55b28f9f | 971 | if (!wait_mask(STATUS(cmd->device->host->io_port), |
a13b3722 | 972 | STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) { |
55b28f9f | 973 | spin_unlock_irq(cmd->device->host->host_lock); |
a13b3722 OZ |
974 | return FAILED; |
975 | } | |
8537cba8 OZ |
976 | /* |
977 | * We need to do this too before the 1542 can interact with | |
978 | * us again after host reset. | |
979 | */ | |
980 | if (reset_cmd & HRST) | |
55b28f9f | 981 | setup_mailboxes(cmd->device->host->io_port, cmd->device->host); |
1da177e4 LT |
982 | /* |
983 | * Now try to pick up the pieces. For all pending commands, | |
984 | * free any internal data structures, and basically clear things | |
985 | * out. We do not try and restart any commands or anything - | |
986 | * the strategy handler takes care of that crap. | |
987 | */ | |
55b28f9f | 988 | printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no); |
1da177e4 LT |
989 | |
990 | for (i = 0; i < AHA1542_MAILBOXES; i++) { | |
55b28f9f OZ |
991 | if (aha1542->int_cmds[i] != NULL) { |
992 | struct scsi_cmnd *tmp_cmd; | |
993 | tmp_cmd = aha1542->int_cmds[i]; | |
1da177e4 | 994 | |
55b28f9f | 995 | if (tmp_cmd->device->soft_reset) { |
1da177e4 LT |
996 | /* |
997 | * If this device implements the soft reset option, | |
998 | * then it is still holding onto the command, and | |
999 | * may yet complete it. In this case, we don't | |
1000 | * flush the data. | |
1001 | */ | |
1002 | continue; | |
1003 | } | |
55b28f9f OZ |
1004 | kfree(tmp_cmd->host_scribble); |
1005 | tmp_cmd->host_scribble = NULL; | |
1006 | aha1542->int_cmds[i] = NULL; | |
e98878f7 | 1007 | aha1542->mb[i].status = 0; |
1da177e4 LT |
1008 | } |
1009 | } | |
1010 | ||
55b28f9f | 1011 | spin_unlock_irq(cmd->device->host->host_lock); |
1da177e4 | 1012 | return SUCCESS; |
1da177e4 LT |
1013 | } |
1014 | ||
55b28f9f | 1015 | static int aha1542_bus_reset(struct scsi_cmnd *cmd) |
1da177e4 | 1016 | { |
55b28f9f | 1017 | return aha1542_reset(cmd, SCRST); |
8537cba8 | 1018 | } |
1da177e4 | 1019 | |
55b28f9f | 1020 | static int aha1542_host_reset(struct scsi_cmnd *cmd) |
8537cba8 | 1021 | { |
55b28f9f | 1022 | return aha1542_reset(cmd, HRST | SCRST); |
1da177e4 LT |
1023 | } |
1024 | ||
1da177e4 | 1025 | static int aha1542_biosparam(struct scsi_device *sdev, |
17787a09 | 1026 | struct block_device *bdev, sector_t capacity, int geom[]) |
1da177e4 | 1027 | { |
e98878f7 | 1028 | struct aha1542_hostdata *aha1542 = shost_priv(sdev->host); |
1da177e4 | 1029 | |
17787a09 OZ |
1030 | if (capacity >= 0x200000 && |
1031 | aha1542->bios_translation == BIOS_TRANSLATION_25563) { | |
1da177e4 | 1032 | /* Please verify that this is the same as what DOS returns */ |
17787a09 OZ |
1033 | geom[0] = 255; /* heads */ |
1034 | geom[1] = 63; /* sectors */ | |
1da177e4 | 1035 | } else { |
17787a09 OZ |
1036 | geom[0] = 64; /* heads */ |
1037 | geom[1] = 32; /* sectors */ | |
1da177e4 | 1038 | } |
17787a09 | 1039 | geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */ |
1da177e4 LT |
1040 | |
1041 | return 0; | |
1042 | } | |
1043 | MODULE_LICENSE("GPL"); | |
1044 | ||
d0be4a7d | 1045 | static struct scsi_host_template driver_template = { |
643a7c43 | 1046 | .module = THIS_MODULE, |
1da177e4 LT |
1047 | .proc_name = "aha1542", |
1048 | .name = "Adaptec 1542", | |
1da177e4 | 1049 | .queuecommand = aha1542_queuecommand, |
1da177e4 LT |
1050 | .eh_device_reset_handler= aha1542_dev_reset, |
1051 | .eh_bus_reset_handler = aha1542_bus_reset, | |
1052 | .eh_host_reset_handler = aha1542_host_reset, | |
1053 | .bios_param = aha1542_biosparam, | |
1054 | .can_queue = AHA1542_MAILBOXES, | |
1055 | .this_id = 7, | |
10be6250 OZ |
1056 | .sg_tablesize = 16, |
1057 | .cmd_per_lun = 1, | |
1da177e4 LT |
1058 | .unchecked_isa_dma = 1, |
1059 | .use_clustering = ENABLE_CLUSTERING, | |
1060 | }; | |
643a7c43 OZ |
1061 | |
1062 | static int aha1542_isa_match(struct device *pdev, unsigned int ndev) | |
1063 | { | |
1064 | struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev); | |
1065 | ||
1066 | if (!sh) | |
1067 | return 0; | |
1068 | ||
1069 | dev_set_drvdata(pdev, sh); | |
1070 | return 1; | |
1071 | } | |
1072 | ||
1073 | static int aha1542_isa_remove(struct device *pdev, | |
1074 | unsigned int ndev) | |
1075 | { | |
1076 | aha1542_release(dev_get_drvdata(pdev)); | |
1077 | dev_set_drvdata(pdev, NULL); | |
1078 | return 0; | |
1079 | } | |
1080 | ||
1081 | static struct isa_driver aha1542_isa_driver = { | |
1082 | .match = aha1542_isa_match, | |
1083 | .remove = aha1542_isa_remove, | |
1084 | .driver = { | |
1085 | .name = "aha1542" | |
1086 | }, | |
1087 | }; | |
1088 | static int isa_registered; | |
1089 | ||
1090 | #ifdef CONFIG_PNP | |
1091 | static struct pnp_device_id aha1542_pnp_ids[] = { | |
1092 | { .id = "ADP1542" }, | |
1093 | { .id = "" } | |
1094 | }; | |
1095 | MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids); | |
1096 | ||
1097 | static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id) | |
1098 | { | |
1099 | int indx; | |
1100 | struct Scsi_Host *sh; | |
1101 | ||
f71429ab OZ |
1102 | for (indx = 0; indx < ARRAY_SIZE(io); indx++) { |
1103 | if (io[indx]) | |
643a7c43 OZ |
1104 | continue; |
1105 | ||
1106 | if (pnp_activate_dev(pdev) < 0) | |
1107 | continue; | |
1108 | ||
f71429ab | 1109 | io[indx] = pnp_port_start(pdev, 0); |
643a7c43 OZ |
1110 | |
1111 | /* The card can be queried for its DMA, we have | |
1112 | the DMA set up that is enough */ | |
1113 | ||
f71429ab | 1114 | printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", io[indx]); |
643a7c43 OZ |
1115 | } |
1116 | ||
1117 | sh = aha1542_hw_init(&driver_template, &pdev->dev, indx); | |
1118 | if (!sh) | |
1119 | return -ENODEV; | |
1120 | ||
1121 | pnp_set_drvdata(pdev, sh); | |
1122 | return 0; | |
1123 | } | |
1124 | ||
1125 | static void aha1542_pnp_remove(struct pnp_dev *pdev) | |
1126 | { | |
1127 | aha1542_release(pnp_get_drvdata(pdev)); | |
1128 | pnp_set_drvdata(pdev, NULL); | |
1129 | } | |
1130 | ||
1131 | static struct pnp_driver aha1542_pnp_driver = { | |
1132 | .name = "aha1542", | |
1133 | .id_table = aha1542_pnp_ids, | |
1134 | .probe = aha1542_pnp_probe, | |
1135 | .remove = aha1542_pnp_remove, | |
1136 | }; | |
1137 | static int pnp_registered; | |
1138 | #endif /* CONFIG_PNP */ | |
1139 | ||
1140 | static int __init aha1542_init(void) | |
1141 | { | |
1142 | int ret = 0; | |
643a7c43 OZ |
1143 | |
1144 | #ifdef CONFIG_PNP | |
1145 | if (isapnp) { | |
1146 | ret = pnp_register_driver(&aha1542_pnp_driver); | |
1147 | if (!ret) | |
1148 | pnp_registered = 1; | |
1149 | } | |
1150 | #endif | |
1151 | ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS); | |
1152 | if (!ret) | |
1153 | isa_registered = 1; | |
1154 | ||
1155 | #ifdef CONFIG_PNP | |
1156 | if (pnp_registered) | |
1157 | ret = 0; | |
1158 | #endif | |
1159 | if (isa_registered) | |
1160 | ret = 0; | |
1161 | ||
1162 | return ret; | |
1163 | } | |
1164 | ||
1165 | static void __exit aha1542_exit(void) | |
1166 | { | |
1167 | #ifdef CONFIG_PNP | |
1168 | if (pnp_registered) | |
1169 | pnp_unregister_driver(&aha1542_pnp_driver); | |
1170 | #endif | |
1171 | if (isa_registered) | |
1172 | isa_unregister_driver(&aha1542_isa_driver); | |
1173 | } | |
1174 | ||
1175 | module_init(aha1542_init); | |
1176 | module_exit(aha1542_exit); |