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