0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/kernel.h>
0013 #include <linux/types.h>
0014 #include <linux/string.h>
0015 #include <linux/delay.h>
0016 #include <linux/init.h>
0017 #include <linux/spinlock.h>
0018 #include <linux/isa.h>
0019 #include <linux/pnp.h>
0020 #include <linux/slab.h>
0021 #include <linux/io.h>
0022 #include <asm/dma.h>
0023 #include <scsi/scsi_cmnd.h>
0024 #include <scsi/scsi_device.h>
0025 #include <scsi/scsi_host.h>
0026 #include "aha1542.h"
0027
0028 #define MAXBOARDS 4
0029
0030 static bool isapnp = 1;
0031 module_param(isapnp, bool, 0);
0032 MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
0033
0034 static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
0035 module_param_hw_array(io, int, ioport, NULL, 0);
0036 MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
0037
0038
0039 static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 };
0040 module_param_array(bus_on, int, NULL, 0);
0041 MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
0042
0043
0044 static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 };
0045 module_param_array(bus_off, int, NULL, 0);
0046 MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
0047
0048
0049 static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
0050 module_param_array(dma_speed, int, NULL, 0);
0051 MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
0052
0053 #define BIOS_TRANSLATION_6432 1
0054 #define BIOS_TRANSLATION_25563 2
0055
0056 struct aha1542_hostdata {
0057
0058 int bios_translation;
0059 int aha1542_last_mbi_used;
0060 int aha1542_last_mbo_used;
0061 struct scsi_cmnd *int_cmds[AHA1542_MAILBOXES];
0062 struct mailbox *mb;
0063 dma_addr_t mb_handle;
0064 struct ccb *ccb;
0065 dma_addr_t ccb_handle;
0066 };
0067
0068 #define AHA1542_MAX_SECTORS 16
0069
0070 struct aha1542_cmd {
0071
0072 void *data_buffer;
0073 dma_addr_t data_buffer_handle;
0074 };
0075
0076 static inline void aha1542_intr_reset(u16 base)
0077 {
0078 outb(IRST, CONTROL(base));
0079 }
0080
0081 static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
0082 {
0083 bool delayed = true;
0084
0085 if (timeout == 0) {
0086 timeout = 3000000;
0087 delayed = false;
0088 }
0089
0090 while (1) {
0091 u8 bits = inb(port) & mask;
0092 if ((bits & allof) == allof && ((bits & noneof) == 0))
0093 break;
0094 if (delayed)
0095 mdelay(1);
0096 if (--timeout == 0)
0097 return false;
0098 }
0099
0100 return true;
0101 }
0102
0103 static int aha1542_outb(unsigned int base, u8 val)
0104 {
0105 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
0106 return 1;
0107 outb(val, DATA(base));
0108
0109 return 0;
0110 }
0111
0112 static int aha1542_out(unsigned int base, u8 *buf, int len)
0113 {
0114 while (len--) {
0115 if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
0116 return 1;
0117 outb(*buf++, DATA(base));
0118 }
0119 if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
0120 return 1;
0121
0122 return 0;
0123 }
0124
0125
0126
0127
0128
0129
0130 static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
0131 {
0132 while (len--) {
0133 if (!wait_mask(STATUS(base), DF, DF, 0, timeout))
0134 return 1;
0135 *buf++ = inb(DATA(base));
0136 }
0137 return 0;
0138 }
0139
0140 static int makecode(unsigned hosterr, unsigned scsierr)
0141 {
0142 switch (hosterr) {
0143 case 0x0:
0144 case 0xa:
0145 case 0xb:
0146 hosterr = 0;
0147 break;
0148
0149 case 0x11:
0150
0151
0152 hosterr = DID_TIME_OUT;
0153 break;
0154
0155 case 0x12:
0156
0157
0158
0159
0160 case 0x13:
0161
0162 case 0x15:
0163
0164
0165
0166 case 0x16:
0167
0168
0169
0170 case 0x17:
0171
0172
0173
0174 case 0x18:
0175
0176
0177
0178 case 0x19:
0179
0180
0181
0182
0183 case 0x1a:
0184
0185
0186
0187 #ifdef DEBUG
0188 printk("Aha1542: %x %x\n", hosterr, scsierr);
0189 #endif
0190 hosterr = DID_ERROR;
0191 break;
0192
0193 case 0x14:
0194
0195
0196
0197
0198 hosterr = DID_RESET;
0199 break;
0200 default:
0201 printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
0202 break;
0203 }
0204 return scsierr | (hosterr << 16);
0205 }
0206
0207 static int aha1542_test_port(struct Scsi_Host *sh)
0208 {
0209 int i;
0210
0211
0212 if (inb(STATUS(sh->io_port)) == 0xff)
0213 return 0;
0214
0215
0216
0217
0218 aha1542_intr_reset(sh->io_port);
0219
0220 outb(SRST | IRST , CONTROL(sh->io_port));
0221
0222 mdelay(20);
0223
0224
0225 if (!wait_mask(STATUS(sh->io_port), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
0226 return 0;
0227
0228
0229 if (inb(INTRFLAGS(sh->io_port)) & INTRMASK)
0230 return 0;
0231
0232
0233
0234
0235
0236
0237 aha1542_outb(sh->io_port, CMD_INQUIRY);
0238
0239 for (i = 0; i < 4; i++) {
0240 if (!wait_mask(STATUS(sh->io_port), DF, DF, 0, 0))
0241 return 0;
0242 (void)inb(DATA(sh->io_port));
0243 }
0244
0245
0246 if (inb(STATUS(sh->io_port)) & DF)
0247 return 0;
0248
0249
0250 if (!wait_mask(INTRFLAGS(sh->io_port), HACC, HACC, 0, 0))
0251 return 0;
0252
0253
0254 outb(IRST, CONTROL(sh->io_port));
0255
0256 return 1;
0257 }
0258
0259 static void aha1542_free_cmd(struct scsi_cmnd *cmd)
0260 {
0261 struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
0262
0263 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
0264 struct request *rq = scsi_cmd_to_rq(cmd);
0265 void *buf = acmd->data_buffer;
0266 struct req_iterator iter;
0267 struct bio_vec bv;
0268
0269 rq_for_each_segment(bv, rq, iter) {
0270 memcpy_to_bvec(&bv, buf);
0271 buf += bv.bv_len;
0272 }
0273 }
0274
0275 scsi_dma_unmap(cmd);
0276 }
0277
0278 static irqreturn_t aha1542_interrupt(int irq, void *dev_id)
0279 {
0280 struct Scsi_Host *sh = dev_id;
0281 struct aha1542_hostdata *aha1542 = shost_priv(sh);
0282 int errstatus, mbi, mbo, mbistatus;
0283 int number_serviced;
0284 unsigned long flags;
0285 struct scsi_cmnd *tmp_cmd;
0286 int flag;
0287 struct mailbox *mb = aha1542->mb;
0288 struct ccb *ccb = aha1542->ccb;
0289
0290 #ifdef DEBUG
0291 {
0292 flag = inb(INTRFLAGS(sh->io_port));
0293 shost_printk(KERN_DEBUG, sh, "aha1542_intr_handle: ");
0294 if (!(flag & ANYINTR))
0295 printk("no interrupt?");
0296 if (flag & MBIF)
0297 printk("MBIF ");
0298 if (flag & MBOA)
0299 printk("MBOF ");
0300 if (flag & HACC)
0301 printk("HACC ");
0302 if (flag & SCRD)
0303 printk("SCRD ");
0304 printk("status %02x\n", inb(STATUS(sh->io_port)));
0305 }
0306 #endif
0307 number_serviced = 0;
0308
0309 spin_lock_irqsave(sh->host_lock, flags);
0310 while (1) {
0311 flag = inb(INTRFLAGS(sh->io_port));
0312
0313
0314
0315
0316
0317
0318
0319 if (flag & ~MBIF) {
0320 if (flag & MBOA)
0321 printk("MBOF ");
0322 if (flag & HACC)
0323 printk("HACC ");
0324 if (flag & SCRD)
0325 printk("SCRD ");
0326 }
0327 aha1542_intr_reset(sh->io_port);
0328
0329 mbi = aha1542->aha1542_last_mbi_used + 1;
0330 if (mbi >= 2 * AHA1542_MAILBOXES)
0331 mbi = AHA1542_MAILBOXES;
0332
0333 do {
0334 if (mb[mbi].status != 0)
0335 break;
0336 mbi++;
0337 if (mbi >= 2 * AHA1542_MAILBOXES)
0338 mbi = AHA1542_MAILBOXES;
0339 } while (mbi != aha1542->aha1542_last_mbi_used);
0340
0341 if (mb[mbi].status == 0) {
0342 spin_unlock_irqrestore(sh->host_lock, flags);
0343
0344 if (!number_serviced)
0345 shost_printk(KERN_WARNING, sh, "interrupt received, but no mail.\n");
0346 return IRQ_HANDLED;
0347 }
0348
0349 mbo = (scsi2int(mb[mbi].ccbptr) - (unsigned long)aha1542->ccb_handle) / sizeof(struct ccb);
0350 mbistatus = mb[mbi].status;
0351 mb[mbi].status = 0;
0352 aha1542->aha1542_last_mbi_used = mbi;
0353
0354 #ifdef DEBUG
0355 if (ccb[mbo].tarstat | ccb[mbo].hastat)
0356 shost_printk(KERN_DEBUG, sh, "aha1542_command: returning %x (status %d)\n",
0357 ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
0358 #endif
0359
0360 if (mbistatus == 3)
0361 continue;
0362
0363 #ifdef DEBUG
0364 shost_printk(KERN_DEBUG, sh, "...done %d %d\n", mbo, mbi);
0365 #endif
0366
0367 tmp_cmd = aha1542->int_cmds[mbo];
0368
0369 if (!tmp_cmd) {
0370 spin_unlock_irqrestore(sh->host_lock, flags);
0371 shost_printk(KERN_WARNING, sh, "Unexpected interrupt\n");
0372 shost_printk(KERN_WARNING, sh, "tarstat=%x, hastat=%x idlun=%x ccb#=%d\n", ccb[mbo].tarstat,
0373 ccb[mbo].hastat, ccb[mbo].idlun, mbo);
0374 return IRQ_HANDLED;
0375 }
0376 aha1542_free_cmd(tmp_cmd);
0377
0378
0379
0380
0381
0382 if (ccb[mbo].tarstat == 2)
0383 memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
0384 SCSI_SENSE_BUFFERSIZE);
0385
0386
0387
0388
0389
0390 if (mbistatus != 1)
0391
0392 errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
0393 else
0394 errstatus = 0;
0395
0396 #ifdef DEBUG
0397 if (errstatus)
0398 shost_printk(KERN_DEBUG, sh, "(aha1542 error:%x %x %x) ", errstatus,
0399 ccb[mbo].hastat, ccb[mbo].tarstat);
0400 if (ccb[mbo].tarstat == 2)
0401 print_hex_dump_bytes("sense: ", DUMP_PREFIX_NONE, &ccb[mbo].cdb[ccb[mbo].cdblen], 12);
0402 if (errstatus)
0403 printk("aha1542_intr_handle: returning %6x\n", errstatus);
0404 #endif
0405 tmp_cmd->result = errstatus;
0406 aha1542->int_cmds[mbo] = NULL;
0407
0408
0409 scsi_done(tmp_cmd);
0410 number_serviced++;
0411 }
0412 }
0413
0414 static int aha1542_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
0415 {
0416 struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
0417 struct aha1542_hostdata *aha1542 = shost_priv(sh);
0418 u8 direction;
0419 u8 target = cmd->device->id;
0420 u8 lun = cmd->device->lun;
0421 unsigned long flags;
0422 int bufflen = scsi_bufflen(cmd);
0423 int mbo;
0424 struct mailbox *mb = aha1542->mb;
0425 struct ccb *ccb = aha1542->ccb;
0426
0427 if (*cmd->cmnd == REQUEST_SENSE) {
0428
0429 cmd->result = 0;
0430 scsi_done(cmd);
0431 return 0;
0432 }
0433 #ifdef DEBUG
0434 {
0435 int i = -1;
0436 if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
0437 i = xscsi2int(cmd->cmnd + 2);
0438 else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
0439 i = scsi2int(cmd->cmnd + 2);
0440 shost_printk(KERN_DEBUG, sh, "aha1542_queuecommand: dev %d cmd %02x pos %d len %d",
0441 target, *cmd->cmnd, i, bufflen);
0442 print_hex_dump_bytes("command: ", DUMP_PREFIX_NONE, cmd->cmnd, cmd->cmd_len);
0443 }
0444 #endif
0445
0446 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
0447 struct request *rq = scsi_cmd_to_rq(cmd);
0448 void *buf = acmd->data_buffer;
0449 struct req_iterator iter;
0450 struct bio_vec bv;
0451
0452 rq_for_each_segment(bv, rq, iter) {
0453 memcpy_from_bvec(buf, &bv);
0454 buf += bv.bv_len;
0455 }
0456 }
0457
0458
0459
0460
0461
0462
0463 spin_lock_irqsave(sh->host_lock, flags);
0464 mbo = aha1542->aha1542_last_mbo_used + 1;
0465 if (mbo >= AHA1542_MAILBOXES)
0466 mbo = 0;
0467
0468 do {
0469 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
0470 break;
0471 mbo++;
0472 if (mbo >= AHA1542_MAILBOXES)
0473 mbo = 0;
0474 } while (mbo != aha1542->aha1542_last_mbo_used);
0475
0476 if (mb[mbo].status || aha1542->int_cmds[mbo])
0477 panic("Unable to find empty mailbox for aha1542.\n");
0478
0479 aha1542->int_cmds[mbo] = cmd;
0480
0481
0482
0483 aha1542->aha1542_last_mbo_used = mbo;
0484
0485 #ifdef DEBUG
0486 shost_printk(KERN_DEBUG, sh, "Sending command (%d)...", mbo);
0487 #endif
0488
0489
0490 any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
0491
0492 memset(&ccb[mbo], 0, sizeof(struct ccb));
0493
0494 ccb[mbo].cdblen = cmd->cmd_len;
0495
0496 direction = 0;
0497 if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
0498 direction = 8;
0499 else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
0500 direction = 16;
0501
0502 memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
0503 ccb[mbo].op = 0;
0504 any2scsi(ccb[mbo].datalen, bufflen);
0505 if (bufflen)
0506 any2scsi(ccb[mbo].dataptr, acmd->data_buffer_handle);
0507 else
0508 any2scsi(ccb[mbo].dataptr, 0);
0509 ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7);
0510 ccb[mbo].rsalen = 16;
0511 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
0512 ccb[mbo].commlinkid = 0;
0513
0514 #ifdef DEBUG
0515 print_hex_dump_bytes("sending: ", DUMP_PREFIX_NONE, &ccb[mbo], sizeof(ccb[mbo]) - 10);
0516 printk("aha1542_queuecommand: now waiting for interrupt ");
0517 #endif
0518 mb[mbo].status = 1;
0519 aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
0520 spin_unlock_irqrestore(sh->host_lock, flags);
0521
0522 return 0;
0523 }
0524
0525
0526 static void setup_mailboxes(struct Scsi_Host *sh)
0527 {
0528 struct aha1542_hostdata *aha1542 = shost_priv(sh);
0529 u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
0530 int i;
0531
0532 for (i = 0; i < AHA1542_MAILBOXES; i++) {
0533 aha1542->mb[i].status = 0;
0534 any2scsi(aha1542->mb[i].ccbptr,
0535 aha1542->ccb_handle + i * sizeof(struct ccb));
0536 aha1542->mb[AHA1542_MAILBOXES + i].status = 0;
0537 }
0538 aha1542_intr_reset(sh->io_port);
0539 any2scsi(mb_cmd + 2, aha1542->mb_handle);
0540 if (aha1542_out(sh->io_port, mb_cmd, 5))
0541 shost_printk(KERN_ERR, sh, "failed setting up mailboxes\n");
0542 aha1542_intr_reset(sh->io_port);
0543 }
0544
0545 static int aha1542_getconfig(struct Scsi_Host *sh)
0546 {
0547 u8 inquiry_result[3];
0548 int i;
0549 i = inb(STATUS(sh->io_port));
0550 if (i & DF) {
0551 i = inb(DATA(sh->io_port));
0552 }
0553 aha1542_outb(sh->io_port, CMD_RETCONF);
0554 aha1542_in(sh->io_port, inquiry_result, 3, 0);
0555 if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
0556 shost_printk(KERN_ERR, sh, "error querying board settings\n");
0557 aha1542_intr_reset(sh->io_port);
0558 switch (inquiry_result[0]) {
0559 case 0x80:
0560 sh->dma_channel = 7;
0561 break;
0562 case 0x40:
0563 sh->dma_channel = 6;
0564 break;
0565 case 0x20:
0566 sh->dma_channel = 5;
0567 break;
0568 case 0x01:
0569 sh->dma_channel = 0;
0570 break;
0571 case 0:
0572
0573
0574
0575
0576 sh->dma_channel = 0xFF;
0577 break;
0578 default:
0579 shost_printk(KERN_ERR, sh, "Unable to determine DMA channel.\n");
0580 return -1;
0581 }
0582 switch (inquiry_result[1]) {
0583 case 0x40:
0584 sh->irq = 15;
0585 break;
0586 case 0x20:
0587 sh->irq = 14;
0588 break;
0589 case 0x8:
0590 sh->irq = 12;
0591 break;
0592 case 0x4:
0593 sh->irq = 11;
0594 break;
0595 case 0x2:
0596 sh->irq = 10;
0597 break;
0598 case 0x1:
0599 sh->irq = 9;
0600 break;
0601 default:
0602 shost_printk(KERN_ERR, sh, "Unable to determine IRQ level.\n");
0603 return -1;
0604 }
0605 sh->this_id = inquiry_result[2] & 7;
0606 return 0;
0607 }
0608
0609
0610
0611
0612
0613
0614 static int aha1542_mbenable(struct Scsi_Host *sh)
0615 {
0616 static u8 mbenable_cmd[3];
0617 static u8 mbenable_result[2];
0618 int retval;
0619
0620 retval = BIOS_TRANSLATION_6432;
0621
0622 aha1542_outb(sh->io_port, CMD_EXTBIOS);
0623 if (aha1542_in(sh->io_port, mbenable_result, 2, 100))
0624 return retval;
0625 if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 100))
0626 goto fail;
0627 aha1542_intr_reset(sh->io_port);
0628
0629 if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
0630 mbenable_cmd[0] = CMD_MBENABLE;
0631 mbenable_cmd[1] = 0;
0632 mbenable_cmd[2] = mbenable_result[1];
0633
0634 if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
0635 retval = BIOS_TRANSLATION_25563;
0636
0637 if (aha1542_out(sh->io_port, mbenable_cmd, 3))
0638 goto fail;
0639 }
0640 while (0) {
0641 fail:
0642 shost_printk(KERN_ERR, sh, "Mailbox init failed\n");
0643 }
0644 aha1542_intr_reset(sh->io_port);
0645 return retval;
0646 }
0647
0648
0649 static int aha1542_query(struct Scsi_Host *sh)
0650 {
0651 struct aha1542_hostdata *aha1542 = shost_priv(sh);
0652 u8 inquiry_result[4];
0653 int i;
0654 i = inb(STATUS(sh->io_port));
0655 if (i & DF) {
0656 i = inb(DATA(sh->io_port));
0657 }
0658 aha1542_outb(sh->io_port, CMD_INQUIRY);
0659 aha1542_in(sh->io_port, inquiry_result, 4, 0);
0660 if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
0661 shost_printk(KERN_ERR, sh, "error querying card type\n");
0662 aha1542_intr_reset(sh->io_port);
0663
0664 aha1542->bios_translation = BIOS_TRANSLATION_6432;
0665
0666
0667
0668
0669
0670
0671
0672
0673 if (inquiry_result[0] == 0x43) {
0674 shost_printk(KERN_INFO, sh, "Emulation mode not supported for AHA-1740 hardware, use aha1740 driver instead.\n");
0675 return 1;
0676 }
0677
0678
0679
0680
0681
0682
0683 aha1542->bios_translation = aha1542_mbenable(sh);
0684
0685 return 0;
0686 }
0687
0688 static u8 dma_speed_hw(int dma_speed)
0689 {
0690 switch (dma_speed) {
0691 case 5:
0692 return 0x00;
0693 case 6:
0694 return 0x04;
0695 case 7:
0696 return 0x01;
0697 case 8:
0698 return 0x02;
0699 case 10:
0700 return 0x03;
0701 }
0702
0703 return 0xff;
0704 }
0705
0706
0707 static void aha1542_set_bus_times(struct Scsi_Host *sh, int bus_on, int bus_off, int dma_speed)
0708 {
0709 if (bus_on > 0) {
0710 u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on, 2, 15) };
0711
0712 aha1542_intr_reset(sh->io_port);
0713 if (aha1542_out(sh->io_port, oncmd, 2))
0714 goto fail;
0715 }
0716
0717 if (bus_off > 0) {
0718 u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off, 1, 64) };
0719
0720 aha1542_intr_reset(sh->io_port);
0721 if (aha1542_out(sh->io_port, offcmd, 2))
0722 goto fail;
0723 }
0724
0725 if (dma_speed_hw(dma_speed) != 0xff) {
0726 u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed) };
0727
0728 aha1542_intr_reset(sh->io_port);
0729 if (aha1542_out(sh->io_port, dmacmd, 2))
0730 goto fail;
0731 }
0732 aha1542_intr_reset(sh->io_port);
0733 return;
0734 fail:
0735 shost_printk(KERN_ERR, sh, "setting bus on/off-time failed\n");
0736 aha1542_intr_reset(sh->io_port);
0737 }
0738
0739
0740 static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
0741 {
0742 unsigned int base_io = io[indx];
0743 struct Scsi_Host *sh;
0744 struct aha1542_hostdata *aha1542;
0745 char dma_info[] = "no DMA";
0746
0747 if (base_io == 0)
0748 return NULL;
0749
0750 if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
0751 return NULL;
0752
0753 sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
0754 if (!sh)
0755 goto release;
0756 aha1542 = shost_priv(sh);
0757
0758 sh->unique_id = base_io;
0759 sh->io_port = base_io;
0760 sh->n_io_port = AHA1542_REGION_SIZE;
0761 aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
0762 aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
0763
0764 if (!aha1542_test_port(sh))
0765 goto unregister;
0766
0767 aha1542_set_bus_times(sh, bus_on[indx], bus_off[indx], dma_speed[indx]);
0768 if (aha1542_query(sh))
0769 goto unregister;
0770 if (aha1542_getconfig(sh) == -1)
0771 goto unregister;
0772
0773 if (sh->dma_channel != 0xFF)
0774 snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel);
0775 shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n",
0776 sh->this_id, base_io, sh->irq, dma_info);
0777 if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
0778 shost_printk(KERN_INFO, sh, "Using extended bios translation\n");
0779
0780 if (dma_set_mask_and_coherent(pdev, DMA_BIT_MASK(24)) < 0)
0781 goto unregister;
0782
0783 aha1542->mb = dma_alloc_coherent(pdev,
0784 AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
0785 &aha1542->mb_handle, GFP_KERNEL);
0786 if (!aha1542->mb)
0787 goto unregister;
0788
0789 aha1542->ccb = dma_alloc_coherent(pdev,
0790 AHA1542_MAILBOXES * sizeof(struct ccb),
0791 &aha1542->ccb_handle, GFP_KERNEL);
0792 if (!aha1542->ccb)
0793 goto free_mb;
0794
0795 setup_mailboxes(sh);
0796
0797 if (request_irq(sh->irq, aha1542_interrupt, 0, "aha1542", sh)) {
0798 shost_printk(KERN_ERR, sh, "Unable to allocate IRQ.\n");
0799 goto free_ccb;
0800 }
0801 if (sh->dma_channel != 0xFF) {
0802 if (request_dma(sh->dma_channel, "aha1542")) {
0803 shost_printk(KERN_ERR, sh, "Unable to allocate DMA channel.\n");
0804 goto free_irq;
0805 }
0806 if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
0807 set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
0808 enable_dma(sh->dma_channel);
0809 }
0810 }
0811
0812 if (scsi_add_host(sh, pdev))
0813 goto free_dma;
0814
0815 scsi_scan_host(sh);
0816
0817 return sh;
0818
0819 free_dma:
0820 if (sh->dma_channel != 0xff)
0821 free_dma(sh->dma_channel);
0822 free_irq:
0823 free_irq(sh->irq, sh);
0824 free_ccb:
0825 dma_free_coherent(pdev, AHA1542_MAILBOXES * sizeof(struct ccb),
0826 aha1542->ccb, aha1542->ccb_handle);
0827 free_mb:
0828 dma_free_coherent(pdev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
0829 aha1542->mb, aha1542->mb_handle);
0830 unregister:
0831 scsi_host_put(sh);
0832 release:
0833 release_region(base_io, AHA1542_REGION_SIZE);
0834
0835 return NULL;
0836 }
0837
0838 static int aha1542_release(struct Scsi_Host *sh)
0839 {
0840 struct aha1542_hostdata *aha1542 = shost_priv(sh);
0841 struct device *dev = sh->dma_dev;
0842
0843 scsi_remove_host(sh);
0844 if (sh->dma_channel != 0xff)
0845 free_dma(sh->dma_channel);
0846 dma_free_coherent(dev, AHA1542_MAILBOXES * sizeof(struct ccb),
0847 aha1542->ccb, aha1542->ccb_handle);
0848 dma_free_coherent(dev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
0849 aha1542->mb, aha1542->mb_handle);
0850 if (sh->irq)
0851 free_irq(sh->irq, sh);
0852 if (sh->io_port && sh->n_io_port)
0853 release_region(sh->io_port, sh->n_io_port);
0854 scsi_host_put(sh);
0855 return 0;
0856 }
0857
0858
0859
0860
0861
0862
0863 static int aha1542_dev_reset(struct scsi_cmnd *cmd)
0864 {
0865 struct Scsi_Host *sh = cmd->device->host;
0866 struct aha1542_hostdata *aha1542 = shost_priv(sh);
0867 unsigned long flags;
0868 struct mailbox *mb = aha1542->mb;
0869 u8 target = cmd->device->id;
0870 u8 lun = cmd->device->lun;
0871 int mbo;
0872 struct ccb *ccb = aha1542->ccb;
0873
0874 spin_lock_irqsave(sh->host_lock, flags);
0875 mbo = aha1542->aha1542_last_mbo_used + 1;
0876 if (mbo >= AHA1542_MAILBOXES)
0877 mbo = 0;
0878
0879 do {
0880 if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
0881 break;
0882 mbo++;
0883 if (mbo >= AHA1542_MAILBOXES)
0884 mbo = 0;
0885 } while (mbo != aha1542->aha1542_last_mbo_used);
0886
0887 if (mb[mbo].status || aha1542->int_cmds[mbo])
0888 panic("Unable to find empty mailbox for aha1542.\n");
0889
0890 aha1542->int_cmds[mbo] = cmd;
0891
0892
0893
0894
0895 aha1542->aha1542_last_mbo_used = mbo;
0896
0897
0898 any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
0899
0900 memset(&ccb[mbo], 0, sizeof(struct ccb));
0901
0902 ccb[mbo].op = 0x81;
0903
0904 ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);
0905
0906 ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
0907 ccb[mbo].commlinkid = 0;
0908
0909
0910
0911
0912
0913 aha1542_outb(sh->io_port, CMD_START_SCSI);
0914 spin_unlock_irqrestore(sh->host_lock, flags);
0915
0916 scmd_printk(KERN_WARNING, cmd,
0917 "Trying device reset for target\n");
0918
0919 return SUCCESS;
0920 }
0921
0922 static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
0923 {
0924 struct Scsi_Host *sh = cmd->device->host;
0925 struct aha1542_hostdata *aha1542 = shost_priv(sh);
0926 unsigned long flags;
0927 int i;
0928
0929 spin_lock_irqsave(sh->host_lock, flags);
0930
0931
0932
0933
0934
0935
0936 outb(reset_cmd, CONTROL(cmd->device->host->io_port));
0937
0938 if (!wait_mask(STATUS(cmd->device->host->io_port),
0939 STATMASK, IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
0940 spin_unlock_irqrestore(sh->host_lock, flags);
0941 return FAILED;
0942 }
0943
0944
0945
0946
0947
0948 if (reset_cmd & HRST)
0949 setup_mailboxes(cmd->device->host);
0950
0951
0952
0953
0954
0955
0956
0957 shost_printk(KERN_WARNING, cmd->device->host, "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
0958
0959 for (i = 0; i < AHA1542_MAILBOXES; i++) {
0960 if (aha1542->int_cmds[i] != NULL) {
0961 struct scsi_cmnd *tmp_cmd;
0962 tmp_cmd = aha1542->int_cmds[i];
0963
0964 if (tmp_cmd->device->soft_reset) {
0965
0966
0967
0968
0969
0970
0971 continue;
0972 }
0973 aha1542_free_cmd(tmp_cmd);
0974 aha1542->int_cmds[i] = NULL;
0975 aha1542->mb[i].status = 0;
0976 }
0977 }
0978
0979 spin_unlock_irqrestore(sh->host_lock, flags);
0980 return SUCCESS;
0981 }
0982
0983 static int aha1542_bus_reset(struct scsi_cmnd *cmd)
0984 {
0985 return aha1542_reset(cmd, SCRST);
0986 }
0987
0988 static int aha1542_host_reset(struct scsi_cmnd *cmd)
0989 {
0990 return aha1542_reset(cmd, HRST | SCRST);
0991 }
0992
0993 static int aha1542_biosparam(struct scsi_device *sdev,
0994 struct block_device *bdev, sector_t capacity, int geom[])
0995 {
0996 struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
0997
0998 if (capacity >= 0x200000 &&
0999 aha1542->bios_translation == BIOS_TRANSLATION_25563) {
1000
1001 geom[0] = 255;
1002 geom[1] = 63;
1003 } else {
1004 geom[0] = 64;
1005 geom[1] = 32;
1006 }
1007 geom[2] = sector_div(capacity, geom[0] * geom[1]);
1008
1009 return 0;
1010 }
1011 MODULE_LICENSE("GPL");
1012
1013 static int aha1542_init_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
1014 {
1015 struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
1016
1017 acmd->data_buffer = dma_alloc_coherent(shost->dma_dev,
1018 SECTOR_SIZE * AHA1542_MAX_SECTORS,
1019 &acmd->data_buffer_handle, GFP_KERNEL);
1020 if (!acmd->data_buffer)
1021 return -ENOMEM;
1022 return 0;
1023 }
1024
1025 static int aha1542_exit_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
1026 {
1027 struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
1028
1029 dma_free_coherent(shost->dma_dev, SECTOR_SIZE * AHA1542_MAX_SECTORS,
1030 acmd->data_buffer, acmd->data_buffer_handle);
1031 return 0;
1032 }
1033
1034 static struct scsi_host_template driver_template = {
1035 .module = THIS_MODULE,
1036 .proc_name = "aha1542",
1037 .name = "Adaptec 1542",
1038 .cmd_size = sizeof(struct aha1542_cmd),
1039 .queuecommand = aha1542_queuecommand,
1040 .eh_device_reset_handler= aha1542_dev_reset,
1041 .eh_bus_reset_handler = aha1542_bus_reset,
1042 .eh_host_reset_handler = aha1542_host_reset,
1043 .bios_param = aha1542_biosparam,
1044 .init_cmd_priv = aha1542_init_cmd_priv,
1045 .exit_cmd_priv = aha1542_exit_cmd_priv,
1046 .can_queue = AHA1542_MAILBOXES,
1047 .this_id = 7,
1048 .max_sectors = AHA1542_MAX_SECTORS,
1049 .sg_tablesize = SG_ALL,
1050 };
1051
1052 static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1053 {
1054 struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1055
1056 if (!sh)
1057 return 0;
1058
1059 dev_set_drvdata(pdev, sh);
1060 return 1;
1061 }
1062
1063 static void aha1542_isa_remove(struct device *pdev,
1064 unsigned int ndev)
1065 {
1066 aha1542_release(dev_get_drvdata(pdev));
1067 dev_set_drvdata(pdev, NULL);
1068 }
1069
1070 static struct isa_driver aha1542_isa_driver = {
1071 .match = aha1542_isa_match,
1072 .remove = aha1542_isa_remove,
1073 .driver = {
1074 .name = "aha1542"
1075 },
1076 };
1077 static int isa_registered;
1078
1079 #ifdef CONFIG_PNP
1080 static const struct pnp_device_id aha1542_pnp_ids[] = {
1081 { .id = "ADP1542" },
1082 { .id = "" }
1083 };
1084 MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1085
1086 static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1087 {
1088 int indx;
1089 struct Scsi_Host *sh;
1090
1091 for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1092 if (io[indx])
1093 continue;
1094
1095 if (pnp_activate_dev(pdev) < 0)
1096 continue;
1097
1098 io[indx] = pnp_port_start(pdev, 0);
1099
1100
1101
1102
1103
1104
1105 dev_info(&pdev->dev, "ISAPnP found an AHA1535 at I/O 0x%03X", io[indx]);
1106 }
1107
1108 sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1109 if (!sh)
1110 return -ENODEV;
1111
1112 pnp_set_drvdata(pdev, sh);
1113 return 0;
1114 }
1115
1116 static void aha1542_pnp_remove(struct pnp_dev *pdev)
1117 {
1118 aha1542_release(pnp_get_drvdata(pdev));
1119 pnp_set_drvdata(pdev, NULL);
1120 }
1121
1122 static struct pnp_driver aha1542_pnp_driver = {
1123 .name = "aha1542",
1124 .id_table = aha1542_pnp_ids,
1125 .probe = aha1542_pnp_probe,
1126 .remove = aha1542_pnp_remove,
1127 };
1128 static int pnp_registered;
1129 #endif
1130
1131 static int __init aha1542_init(void)
1132 {
1133 int ret = 0;
1134
1135 #ifdef CONFIG_PNP
1136 if (isapnp) {
1137 ret = pnp_register_driver(&aha1542_pnp_driver);
1138 if (!ret)
1139 pnp_registered = 1;
1140 }
1141 #endif
1142 ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1143 if (!ret)
1144 isa_registered = 1;
1145
1146 #ifdef CONFIG_PNP
1147 if (pnp_registered)
1148 ret = 0;
1149 #endif
1150 if (isa_registered)
1151 ret = 0;
1152
1153 return ret;
1154 }
1155
1156 static void __exit aha1542_exit(void)
1157 {
1158 #ifdef CONFIG_PNP
1159 if (pnp_registered)
1160 pnp_unregister_driver(&aha1542_pnp_driver);
1161 #endif
1162 if (isa_registered)
1163 isa_unregister_driver(&aha1542_isa_driver);
1164 }
1165
1166 module_init(aha1542_init);
1167 module_exit(aha1542_exit);