0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 #include <linux/module.h>
0069 #include <linux/errno.h>
0070 #include <linux/delay.h>
0071 #include <linux/pci.h>
0072 #include <linux/init.h>
0073 #include <linux/blkdev.h>
0074 #include <linux/spinlock.h>
0075 #include <linux/stat.h>
0076 #include <linux/kernel.h>
0077 #include <linux/proc_fs.h>
0078 #include <linux/string.h>
0079 #include <linux/interrupt.h>
0080 #include <linux/ioport.h>
0081 #include <linux/slab.h>
0082 #include <linux/jiffies.h>
0083 #include <linux/dma-mapping.h>
0084 #include <asm/io.h>
0085
0086 #include <scsi/scsi.h>
0087 #include <scsi/scsi_cmnd.h>
0088 #include <scsi/scsi_device.h>
0089 #include <scsi/scsi_host.h>
0090 #include <scsi/scsi_tcq.h>
0091
0092 #include "initio.h"
0093
0094 #define SENSE_SIZE 14
0095
0096 #define i91u_MAXQUEUE 2
0097 #define i91u_REVID "Initio INI-9X00U/UW SCSI device driver; Revision: 1.04a"
0098
0099 #ifdef DEBUG_i91u
0100 static unsigned int i91u_debug = DEBUG_DEFAULT;
0101 #endif
0102
0103 static int initio_tag_enable = 1;
0104
0105 #ifdef DEBUG_i91u
0106 static int setup_debug = 0;
0107 #endif
0108
0109 static void i91uSCBPost(u8 * pHcb, u8 * pScb);
0110
0111 #define DEBUG_INTERRUPT 0
0112 #define DEBUG_QUEUE 0
0113 #define DEBUG_STATE 0
0114 #define INT_DISC 0
0115
0116
0117 static struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun);
0118 static struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host);
0119
0120 static int tulip_main(struct initio_host * host);
0121
0122 static int initio_next_state(struct initio_host * host);
0123 static int initio_state_1(struct initio_host * host);
0124 static int initio_state_2(struct initio_host * host);
0125 static int initio_state_3(struct initio_host * host);
0126 static int initio_state_4(struct initio_host * host);
0127 static int initio_state_5(struct initio_host * host);
0128 static int initio_state_6(struct initio_host * host);
0129 static int initio_state_7(struct initio_host * host);
0130 static int initio_xfer_data_in(struct initio_host * host);
0131 static int initio_xfer_data_out(struct initio_host * host);
0132 static int initio_xpad_in(struct initio_host * host);
0133 static int initio_xpad_out(struct initio_host * host);
0134 static int initio_status_msg(struct initio_host * host);
0135
0136 static int initio_msgin(struct initio_host * host);
0137 static int initio_msgin_sync(struct initio_host * host);
0138 static int initio_msgin_accept(struct initio_host * host);
0139 static int initio_msgout_reject(struct initio_host * host);
0140 static int initio_msgin_extend(struct initio_host * host);
0141
0142 static int initio_msgout_ide(struct initio_host * host);
0143 static int initio_msgout_abort_targ(struct initio_host * host);
0144 static int initio_msgout_abort_tag(struct initio_host * host);
0145
0146 static int initio_bus_device_reset(struct initio_host * host);
0147 static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb);
0148 static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb);
0149 static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb);
0150 static int int_initio_busfree(struct initio_host * host);
0151 static int int_initio_scsi_rst(struct initio_host * host);
0152 static int int_initio_bad_seq(struct initio_host * host);
0153 static int int_initio_resel(struct initio_host * host);
0154 static int initio_sync_done(struct initio_host * host);
0155 static int wdtr_done(struct initio_host * host);
0156 static int wait_tulip(struct initio_host * host);
0157 static int initio_wait_done_disc(struct initio_host * host);
0158 static int initio_wait_disc(struct initio_host * host);
0159 static void tulip_scsi(struct initio_host * host);
0160 static int initio_post_scsi_rst(struct initio_host * host);
0161
0162 static void initio_se2_ew_en(unsigned long base);
0163 static void initio_se2_ew_ds(unsigned long base);
0164 static int initio_se2_rd_all(unsigned long base);
0165 static void initio_se2_update_all(unsigned long base);
0166 static void initio_read_eeprom(unsigned long base);
0167
0168
0169
0170 static NVRAM i91unvram;
0171 static NVRAM *i91unvramp;
0172
0173 static u8 i91udftNvRam[64] =
0174 {
0175
0176 0x25, 0xc9,
0177 0x40,
0178 0x01,
0179
0180 0x95,
0181 0x00,
0182 0x00,
0183 0x01,
0184 NBC1_DEFAULT,
0185 0,
0186 0,
0187 0,
0188
0189 7,
0190 NCC1_DEFAULT,
0191 0,
0192 0x10,
0193
0194 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
0195 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
0196 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
0197 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
0198
0199
0200 7,
0201 NCC1_DEFAULT,
0202 0,
0203 0x10,
0204
0205 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
0206 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
0207 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
0208 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
0209 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0210 0, 0};
0211
0212
0213 static u8 initio_rate_tbl[8] =
0214 {
0215
0216 12,
0217 18,
0218 25,
0219 31,
0220 37,
0221 43,
0222 50,
0223 62
0224 };
0225
0226 static void initio_do_pause(unsigned amount)
0227 {
0228
0229 unsigned long the_time = jiffies + amount;
0230
0231 while (time_before_eq(jiffies, the_time))
0232 cpu_relax();
0233 }
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 static void initio_se2_instr(unsigned long base, u8 instr)
0277 {
0278 int i;
0279 u8 b;
0280
0281 outb(SE2CS | SE2DO, base + TUL_NVRAM);
0282 udelay(30);
0283 outb(SE2CS | SE2CLK | SE2DO, base + TUL_NVRAM);
0284 udelay(30);
0285
0286 for (i = 0; i < 8; i++) {
0287 if (instr & 0x80)
0288 b = SE2CS | SE2DO;
0289 else
0290 b = SE2CS;
0291 outb(b, base + TUL_NVRAM);
0292 udelay(30);
0293 outb(b | SE2CLK, base + TUL_NVRAM);
0294 udelay(30);
0295 instr <<= 1;
0296 }
0297 outb(SE2CS, base + TUL_NVRAM);
0298 udelay(30);
0299 }
0300
0301
0302
0303
0304
0305
0306
0307
0308 void initio_se2_ew_en(unsigned long base)
0309 {
0310 initio_se2_instr(base, 0x30);
0311 outb(0, base + TUL_NVRAM);
0312 udelay(30);
0313 }
0314
0315
0316
0317
0318
0319
0320
0321
0322 void initio_se2_ew_ds(unsigned long base)
0323 {
0324 initio_se2_instr(base, 0);
0325 outb(0, base + TUL_NVRAM);
0326 udelay(30);
0327 }
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337 static u16 initio_se2_rd(unsigned long base, u8 addr)
0338 {
0339 u8 instr, rb;
0340 u16 val = 0;
0341 int i;
0342
0343 instr = (u8) (addr | 0x80);
0344 initio_se2_instr(base, instr);
0345
0346 for (i = 15; i >= 0; i--) {
0347 outb(SE2CS | SE2CLK, base + TUL_NVRAM);
0348 udelay(30);
0349 outb(SE2CS, base + TUL_NVRAM);
0350
0351
0352 rb = inb(base + TUL_NVRAM);
0353 rb &= SE2DI;
0354 val += (rb << i);
0355 udelay(30);
0356 }
0357
0358 outb(0, base + TUL_NVRAM);
0359 udelay(30);
0360 return val;
0361 }
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372 static void initio_se2_wr(unsigned long base, u8 addr, u16 val)
0373 {
0374 u8 rb;
0375 u8 instr;
0376 int i;
0377
0378 instr = (u8) (addr | 0x40);
0379 initio_se2_instr(base, instr);
0380 for (i = 15; i >= 0; i--) {
0381 if (val & 0x8000)
0382 outb(SE2CS | SE2DO, base + TUL_NVRAM);
0383 else
0384 outb(SE2CS, base + TUL_NVRAM);
0385 udelay(30);
0386 outb(SE2CS | SE2CLK, base + TUL_NVRAM);
0387 udelay(30);
0388 val <<= 1;
0389 }
0390 outb(SE2CS, base + TUL_NVRAM);
0391 udelay(30);
0392 outb(0, base + TUL_NVRAM);
0393 udelay(30);
0394
0395 outb(SE2CS, base + TUL_NVRAM);
0396 udelay(30);
0397
0398 for (;;) {
0399 outb(SE2CS | SE2CLK, base + TUL_NVRAM);
0400 udelay(30);
0401 outb(SE2CS, base + TUL_NVRAM);
0402 udelay(30);
0403 if ((rb = inb(base + TUL_NVRAM)) & SE2DI)
0404 break;
0405 }
0406 outb(0, base + TUL_NVRAM);
0407 }
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417 static int initio_se2_rd_all(unsigned long base)
0418 {
0419 int i;
0420 u16 chksum = 0;
0421 u16 *np;
0422
0423 i91unvramp = &i91unvram;
0424 np = (u16 *) i91unvramp;
0425 for (i = 0; i < 32; i++)
0426 *np++ = initio_se2_rd(base, i);
0427
0428
0429 if (i91unvramp->NVM_Signature != INI_SIGNATURE)
0430 return -1;
0431
0432 np = (u16 *) i91unvramp;
0433 for (i = 0; i < 31; i++)
0434 chksum += *np++;
0435 if (i91unvramp->NVM_CheckSum != chksum)
0436 return -1;
0437 return 1;
0438 }
0439
0440
0441
0442
0443
0444
0445
0446
0447 static void initio_se2_update_all(unsigned long base)
0448 {
0449 int i;
0450 u16 chksum = 0;
0451 u16 *np, *np1;
0452
0453 i91unvramp = &i91unvram;
0454
0455 np = (u16 *) i91udftNvRam;
0456 for (i = 0; i < 31; i++)
0457 chksum += *np++;
0458 *np = chksum;
0459 initio_se2_ew_en(base);
0460
0461 np = (u16 *) i91udftNvRam;
0462 np1 = (u16 *) i91unvramp;
0463 for (i = 0; i < 32; i++, np++, np1++) {
0464 if (*np != *np1)
0465 initio_se2_wr(base, i, *np);
0466 }
0467 initio_se2_ew_ds(base);
0468 }
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483 static void initio_read_eeprom(unsigned long base)
0484 {
0485 u8 gctrl;
0486
0487 i91unvramp = &i91unvram;
0488
0489 gctrl = inb(base + TUL_GCTRL);
0490 outb(gctrl | TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
0491 if (initio_se2_rd_all(base) != 1) {
0492 initio_se2_update_all(base);
0493 initio_se2_rd_all(base);
0494 }
0495
0496 gctrl = inb(base + TUL_GCTRL);
0497 outb(gctrl & ~TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
0498 }
0499
0500
0501
0502
0503
0504
0505
0506
0507 static void initio_stop_bm(struct initio_host * host)
0508 {
0509
0510 if (inb(host->addr + TUL_XStatus) & XPEND) {
0511 outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
0512
0513 while ((inb(host->addr + TUL_Int) & XABT) == 0)
0514 cpu_relax();
0515 }
0516 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
0517 }
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527 static int initio_reset_scsi(struct initio_host * host, int seconds)
0528 {
0529 outb(TSC_RST_BUS, host->addr + TUL_SCtrl0);
0530
0531 while (!((host->jsint = inb(host->addr + TUL_SInt)) & TSS_SCSIRST_INT))
0532 cpu_relax();
0533
0534
0535 outb(0, host->addr + TUL_SSignal);
0536
0537
0538
0539
0540 initio_do_pause(seconds * HZ);
0541
0542 inb(host->addr + TUL_SInt);
0543 return SCSI_RESET_SUCCESS;
0544 }
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558 static void initio_init(struct initio_host * host, u8 *bios_addr)
0559 {
0560 int i;
0561 u8 *flags;
0562 u8 *heads;
0563
0564
0565 initio_read_eeprom(host->addr);
0566 if (i91unvramp->NVM_SCSIInfo[0].NVM_NumOfTarg == 8)
0567 host->max_tar = 8;
0568 else
0569 host->max_tar = 16;
0570
0571 host->config = i91unvramp->NVM_SCSIInfo[0].NVM_ChConfig1;
0572
0573 host->scsi_id = i91unvramp->NVM_SCSIInfo[0].NVM_ChSCSIID;
0574 host->idmask = ~(1 << host->scsi_id);
0575
0576 #ifdef CHK_PARITY
0577
0578 outb(inb(host->addr + TUL_PCMD) | 0x40, host->addr + TUL_PCMD);
0579 #endif
0580
0581
0582 outb(0x1F, host->addr + TUL_Mask);
0583
0584 initio_stop_bm(host);
0585
0586 outb(TSC_RST_CHIP, host->addr + TUL_SCtrl0);
0587
0588
0589 outb(host->scsi_id << 4, host->addr + TUL_SScsiId);
0590
0591
0592
0593 if (host->config & HCC_EN_PAR)
0594 host->sconf1 = (TSC_INITDEFAULT | TSC_EN_SCSI_PAR);
0595 else
0596 host->sconf1 = (TSC_INITDEFAULT);
0597 outb(host->sconf1, host->addr + TUL_SConfig);
0598
0599
0600 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
0601
0602 outb(0, host->addr + TUL_SPeriod);
0603
0604
0605 outb(153, host->addr + TUL_STimeOut);
0606
0607
0608 outb((host->config & (HCC_ACT_TERM1 | HCC_ACT_TERM2)),
0609 host->addr + TUL_XCtrl);
0610 outb(((host->config & HCC_AUTO_TERM) >> 4) |
0611 (inb(host->addr + TUL_GCTRL1) & 0xFE),
0612 host->addr + TUL_GCTRL1);
0613
0614 for (i = 0,
0615 flags = & (i91unvramp->NVM_SCSIInfo[0].NVM_Targ0Config),
0616 heads = bios_addr + 0x180;
0617 i < host->max_tar;
0618 i++, flags++) {
0619 host->targets[i].flags = *flags & ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
0620 if (host->targets[i].flags & TCF_EN_255)
0621 host->targets[i].drv_flags = TCF_DRV_255_63;
0622 else
0623 host->targets[i].drv_flags = 0;
0624 host->targets[i].js_period = 0;
0625 host->targets[i].sconfig0 = host->sconf1;
0626 host->targets[i].heads = *heads++;
0627 if (host->targets[i].heads == 255)
0628 host->targets[i].drv_flags = TCF_DRV_255_63;
0629 else
0630 host->targets[i].drv_flags = 0;
0631 host->targets[i].sectors = *heads++;
0632 host->targets[i].flags &= ~TCF_BUSY;
0633 host->act_tags[i] = 0;
0634 host->max_tags[i] = 0xFF;
0635 }
0636 printk("i91u: PCI Base=0x%04X, IRQ=%d, BIOS=0x%04X0, SCSI ID=%d\n",
0637 host->addr, host->pci_dev->irq,
0638 host->bios_addr, host->scsi_id);
0639
0640 if (host->config & HCC_SCSI_RESET) {
0641 printk(KERN_INFO "i91u: Reset SCSI Bus ... \n");
0642 initio_reset_scsi(host, 10);
0643 }
0644 outb(0x17, host->addr + TUL_SCFG1);
0645 outb(0xE9, host->addr + TUL_SIntEnable);
0646 }
0647
0648
0649
0650
0651
0652
0653
0654
0655 static struct scsi_ctrl_blk *initio_alloc_scb(struct initio_host *host)
0656 {
0657 struct scsi_ctrl_blk *scb;
0658 unsigned long flags;
0659
0660 spin_lock_irqsave(&host->avail_lock, flags);
0661 if ((scb = host->first_avail) != NULL) {
0662 #if DEBUG_QUEUE
0663 printk("find scb at %p\n", scb);
0664 #endif
0665 if ((host->first_avail = scb->next) == NULL)
0666 host->last_avail = NULL;
0667 scb->next = NULL;
0668 scb->status = SCB_RENT;
0669 }
0670 spin_unlock_irqrestore(&host->avail_lock, flags);
0671 return scb;
0672 }
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682 static void initio_release_scb(struct initio_host * host, struct scsi_ctrl_blk * cmnd)
0683 {
0684 unsigned long flags;
0685
0686 #if DEBUG_QUEUE
0687 printk("Release SCB %p; ", cmnd);
0688 #endif
0689 spin_lock_irqsave(&(host->avail_lock), flags);
0690 cmnd->srb = NULL;
0691 cmnd->status = 0;
0692 cmnd->next = NULL;
0693 if (host->last_avail != NULL) {
0694 host->last_avail->next = cmnd;
0695 host->last_avail = cmnd;
0696 } else {
0697 host->first_avail = cmnd;
0698 host->last_avail = cmnd;
0699 }
0700 spin_unlock_irqrestore(&(host->avail_lock), flags);
0701 }
0702
0703
0704 static void initio_append_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
0705 {
0706
0707 #if DEBUG_QUEUE
0708 printk("Append pend SCB %p; ", scbp);
0709 #endif
0710 scbp->status = SCB_PEND;
0711 scbp->next = NULL;
0712 if (host->last_pending != NULL) {
0713 host->last_pending->next = scbp;
0714 host->last_pending = scbp;
0715 } else {
0716 host->first_pending = scbp;
0717 host->last_pending = scbp;
0718 }
0719 }
0720
0721
0722 static void initio_push_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
0723 {
0724
0725 #if DEBUG_QUEUE
0726 printk("Push pend SCB %p; ", scbp);
0727 #endif
0728 scbp->status = SCB_PEND;
0729 if ((scbp->next = host->first_pending) != NULL) {
0730 host->first_pending = scbp;
0731 } else {
0732 host->first_pending = scbp;
0733 host->last_pending = scbp;
0734 }
0735 }
0736
0737 static struct scsi_ctrl_blk *initio_find_first_pend_scb(struct initio_host * host)
0738 {
0739 struct scsi_ctrl_blk *first;
0740
0741
0742 first = host->first_pending;
0743 while (first != NULL) {
0744 if (first->opcode != ExecSCSI)
0745 return first;
0746 if (first->tagmsg == 0) {
0747 if ((host->act_tags[first->target] == 0) &&
0748 !(host->targets[first->target].flags & TCF_BUSY))
0749 return first;
0750 } else {
0751 if ((host->act_tags[first->target] >=
0752 host->max_tags[first->target]) |
0753 (host->targets[first->target].flags & TCF_BUSY)) {
0754 first = first->next;
0755 continue;
0756 }
0757 return first;
0758 }
0759 first = first->next;
0760 }
0761 return first;
0762 }
0763
0764 static void initio_unlink_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
0765 {
0766 struct scsi_ctrl_blk *tmp, *prev;
0767
0768 #if DEBUG_QUEUE
0769 printk("unlink pend SCB %p; ", scb);
0770 #endif
0771
0772 prev = tmp = host->first_pending;
0773 while (tmp != NULL) {
0774 if (scb == tmp) {
0775 if (tmp == host->first_pending) {
0776 if ((host->first_pending = tmp->next) == NULL)
0777 host->last_pending = NULL;
0778 } else {
0779 prev->next = tmp->next;
0780 if (tmp == host->last_pending)
0781 host->last_pending = prev;
0782 }
0783 tmp->next = NULL;
0784 break;
0785 }
0786 prev = tmp;
0787 tmp = tmp->next;
0788 }
0789 }
0790
0791 static void initio_append_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
0792 {
0793
0794 #if DEBUG_QUEUE
0795 printk("append busy SCB %p; ", scbp);
0796 #endif
0797 if (scbp->tagmsg)
0798 host->act_tags[scbp->target]++;
0799 else
0800 host->targets[scbp->target].flags |= TCF_BUSY;
0801 scbp->status = SCB_BUSY;
0802 scbp->next = NULL;
0803 if (host->last_busy != NULL) {
0804 host->last_busy->next = scbp;
0805 host->last_busy = scbp;
0806 } else {
0807 host->first_busy = scbp;
0808 host->last_busy = scbp;
0809 }
0810 }
0811
0812
0813 static struct scsi_ctrl_blk *initio_pop_busy_scb(struct initio_host * host)
0814 {
0815 struct scsi_ctrl_blk *tmp;
0816
0817
0818 if ((tmp = host->first_busy) != NULL) {
0819 if ((host->first_busy = tmp->next) == NULL)
0820 host->last_busy = NULL;
0821 tmp->next = NULL;
0822 if (tmp->tagmsg)
0823 host->act_tags[tmp->target]--;
0824 else
0825 host->targets[tmp->target].flags &= ~TCF_BUSY;
0826 }
0827 #if DEBUG_QUEUE
0828 printk("Pop busy SCB %p; ", tmp);
0829 #endif
0830 return tmp;
0831 }
0832
0833
0834 static void initio_unlink_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
0835 {
0836 struct scsi_ctrl_blk *tmp, *prev;
0837
0838 #if DEBUG_QUEUE
0839 printk("unlink busy SCB %p; ", scb);
0840 #endif
0841
0842 prev = tmp = host->first_busy;
0843 while (tmp != NULL) {
0844 if (scb == tmp) {
0845 if (tmp == host->first_busy) {
0846 if ((host->first_busy = tmp->next) == NULL)
0847 host->last_busy = NULL;
0848 } else {
0849 prev->next = tmp->next;
0850 if (tmp == host->last_busy)
0851 host->last_busy = prev;
0852 }
0853 tmp->next = NULL;
0854 if (tmp->tagmsg)
0855 host->act_tags[tmp->target]--;
0856 else
0857 host->targets[tmp->target].flags &= ~TCF_BUSY;
0858 break;
0859 }
0860 prev = tmp;
0861 tmp = tmp->next;
0862 }
0863 return;
0864 }
0865
0866 struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun)
0867 {
0868 struct scsi_ctrl_blk *tmp;
0869 u16 scbp_tarlun;
0870
0871
0872 tmp = host->first_busy;
0873 while (tmp != NULL) {
0874 scbp_tarlun = (tmp->lun << 8) | (tmp->target);
0875 if (scbp_tarlun == tarlun) {
0876 break;
0877 }
0878 tmp = tmp->next;
0879 }
0880 #if DEBUG_QUEUE
0881 printk("find busy SCB %p; ", tmp);
0882 #endif
0883 return tmp;
0884 }
0885
0886 static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
0887 {
0888 #if DEBUG_QUEUE
0889 printk("append done SCB %p; ", scbp);
0890 #endif
0891
0892 scbp->status = SCB_DONE;
0893 scbp->next = NULL;
0894 if (host->last_done != NULL) {
0895 host->last_done->next = scbp;
0896 host->last_done = scbp;
0897 } else {
0898 host->first_done = scbp;
0899 host->last_done = scbp;
0900 }
0901 }
0902
0903 struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host)
0904 {
0905 struct scsi_ctrl_blk *tmp;
0906
0907 if ((tmp = host->first_done) != NULL) {
0908 if ((host->first_done = tmp->next) == NULL)
0909 host->last_done = NULL;
0910 tmp->next = NULL;
0911 }
0912 #if DEBUG_QUEUE
0913 printk("find done SCB %p; ",tmp);
0914 #endif
0915 return tmp;
0916 }
0917
0918 static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp)
0919 {
0920 unsigned long flags;
0921 struct scsi_ctrl_blk *tmp, *prev;
0922
0923 spin_lock_irqsave(&host->semaph_lock, flags);
0924
0925 if ((host->semaph == 0) && (host->active == NULL)) {
0926
0927 outb(0x1F, host->addr + TUL_Mask);
0928 spin_unlock_irqrestore(&host->semaph_lock, flags);
0929
0930 tulip_main(host);
0931 spin_lock_irqsave(&host->semaph_lock, flags);
0932 host->semaph = 1;
0933 outb(0x0F, host->addr + TUL_Mask);
0934 spin_unlock_irqrestore(&host->semaph_lock, flags);
0935 return SCSI_ABORT_SNOOZE;
0936 }
0937 prev = tmp = host->first_pending;
0938 while (tmp != NULL) {
0939
0940 if (tmp->srb == srbp) {
0941 if (tmp == host->active) {
0942 spin_unlock_irqrestore(&host->semaph_lock, flags);
0943 return SCSI_ABORT_BUSY;
0944 } else if (tmp == host->first_pending) {
0945 if ((host->first_pending = tmp->next) == NULL)
0946 host->last_pending = NULL;
0947 } else {
0948 prev->next = tmp->next;
0949 if (tmp == host->last_pending)
0950 host->last_pending = prev;
0951 }
0952 tmp->hastat = HOST_ABORTED;
0953 tmp->flags |= SCF_DONE;
0954 if (tmp->flags & SCF_POST)
0955 (*tmp->post) ((u8 *) host, (u8 *) tmp);
0956 spin_unlock_irqrestore(&host->semaph_lock, flags);
0957 return SCSI_ABORT_SUCCESS;
0958 }
0959 prev = tmp;
0960 tmp = tmp->next;
0961 }
0962
0963 prev = tmp = host->first_busy;
0964 while (tmp != NULL) {
0965 if (tmp->srb == srbp) {
0966 if (tmp == host->active) {
0967 spin_unlock_irqrestore(&host->semaph_lock, flags);
0968 return SCSI_ABORT_BUSY;
0969 } else if (tmp->tagmsg == 0) {
0970 spin_unlock_irqrestore(&host->semaph_lock, flags);
0971 return SCSI_ABORT_BUSY;
0972 } else {
0973 host->act_tags[tmp->target]--;
0974 if (tmp == host->first_busy) {
0975 if ((host->first_busy = tmp->next) == NULL)
0976 host->last_busy = NULL;
0977 } else {
0978 prev->next = tmp->next;
0979 if (tmp == host->last_busy)
0980 host->last_busy = prev;
0981 }
0982 tmp->next = NULL;
0983
0984
0985 tmp->hastat = HOST_ABORTED;
0986 tmp->flags |= SCF_DONE;
0987 if (tmp->flags & SCF_POST)
0988 (*tmp->post) ((u8 *) host, (u8 *) tmp);
0989 spin_unlock_irqrestore(&host->semaph_lock, flags);
0990 return SCSI_ABORT_SUCCESS;
0991 }
0992 }
0993 prev = tmp;
0994 tmp = tmp->next;
0995 }
0996 spin_unlock_irqrestore(&host->semaph_lock, flags);
0997 return SCSI_ABORT_NOT_RUNNING;
0998 }
0999
1000
1001 static int initio_bad_seq(struct initio_host * host)
1002 {
1003 struct scsi_ctrl_blk *scb;
1004
1005 printk("initio_bad_seg c=%d\n", host->index);
1006
1007 if ((scb = host->active) != NULL) {
1008 initio_unlink_busy_scb(host, scb);
1009 scb->hastat = HOST_BAD_PHAS;
1010 scb->tastat = 0;
1011 initio_append_done_scb(host, scb);
1012 }
1013 initio_stop_bm(host);
1014 initio_reset_scsi(host, 8);
1015 return initio_post_scsi_rst(host);
1016 }
1017
1018
1019
1020 static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
1021 {
1022 unsigned long flags;
1023
1024 scb->mode = 0;
1025
1026 scb->sgidx = 0;
1027 scb->sgmax = scb->sglen;
1028
1029 spin_lock_irqsave(&host->semaph_lock, flags);
1030
1031 initio_append_pend_scb(host, scb);
1032
1033
1034 if (host->semaph == 1) {
1035
1036 outb(0x1F, host->addr + TUL_Mask);
1037 host->semaph = 0;
1038 spin_unlock_irqrestore(&host->semaph_lock, flags);
1039
1040 tulip_main(host);
1041
1042 spin_lock_irqsave(&host->semaph_lock, flags);
1043 host->semaph = 1;
1044 outb(0x0F, host->addr + TUL_Mask);
1045 }
1046 spin_unlock_irqrestore(&host->semaph_lock, flags);
1047 return;
1048 }
1049
1050
1051 static int initio_isr(struct initio_host * host)
1052 {
1053 if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) {
1054 if (host->semaph == 1) {
1055 outb(0x1F, host->addr + TUL_Mask);
1056
1057 host->semaph = 0;
1058
1059 tulip_main(host);
1060
1061 host->semaph = 1;
1062 outb(0x0F, host->addr + TUL_Mask);
1063 return 1;
1064 }
1065 }
1066 return 0;
1067 }
1068
1069 static int tulip_main(struct initio_host * host)
1070 {
1071 struct scsi_ctrl_blk *scb;
1072
1073 for (;;) {
1074 tulip_scsi(host);
1075
1076
1077 while ((scb = initio_find_done_scb(host)) != NULL) {
1078 if (scb->tastat == INI_QUEUE_FULL) {
1079 host->max_tags[scb->target] =
1080 host->act_tags[scb->target] - 1;
1081 scb->tastat = 0;
1082 initio_append_pend_scb(host, scb);
1083 continue;
1084 }
1085 if (!(scb->mode & SCM_RSENS)) {
1086 if (scb->tastat == 2) {
1087
1088
1089
1090 if (scb->flags & SCF_SENSE) {
1091 u8 len;
1092 len = scb->senselen;
1093 if (len == 0)
1094 len = 1;
1095 scb->buflen = scb->senselen;
1096 scb->bufptr = scb->senseptr;
1097 scb->flags &= ~(SCF_SG | SCF_DIR);
1098
1099
1100 scb->mode = SCM_RSENS;
1101 scb->ident &= 0xBF;
1102 scb->tagmsg = 0;
1103 scb->tastat = 0;
1104 scb->cdblen = 6;
1105 scb->cdb[0] = SCSICMD_RequestSense;
1106 scb->cdb[1] = 0;
1107 scb->cdb[2] = 0;
1108 scb->cdb[3] = 0;
1109 scb->cdb[4] = len;
1110 scb->cdb[5] = 0;
1111 initio_push_pend_scb(host, scb);
1112 break;
1113 }
1114 }
1115 } else {
1116
1117 if (scb->tastat == 2) {
1118
1119 scb->hastat = HOST_BAD_PHAS;
1120 }
1121 scb->tastat = 2;
1122 }
1123 scb->flags |= SCF_DONE;
1124 if (scb->flags & SCF_POST) {
1125
1126 (*scb->post) ((u8 *) host, (u8 *) scb);
1127 }
1128 }
1129
1130 if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING)
1131 continue;
1132 if (host->active)
1133 return 1;
1134
1135 if (initio_find_first_pend_scb(host) == NULL)
1136 return 1;
1137 }
1138
1139 }
1140
1141 static void tulip_scsi(struct initio_host * host)
1142 {
1143 struct scsi_ctrl_blk *scb;
1144 struct target_control *active_tc;
1145
1146
1147 if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) {
1148 host->phase = host->jsstatus0 & TSS_PH_MASK;
1149 host->jsstatus1 = inb(host->addr + TUL_SStatus1);
1150 host->jsint = inb(host->addr + TUL_SInt);
1151 if (host->jsint & TSS_SCSIRST_INT) {
1152 int_initio_scsi_rst(host);
1153 return;
1154 }
1155 if (host->jsint & TSS_RESEL_INT) {
1156 if (int_initio_resel(host) == 0)
1157 initio_next_state(host);
1158 return;
1159 }
1160 if (host->jsint & TSS_SEL_TIMEOUT) {
1161 int_initio_busfree(host);
1162 return;
1163 }
1164 if (host->jsint & TSS_DISC_INT) {
1165 int_initio_busfree(host);
1166 return;
1167 }
1168 if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) {
1169 if ((scb = host->active) != NULL)
1170 initio_next_state(host);
1171 return;
1172 }
1173 }
1174 if (host->active != NULL)
1175 return;
1176
1177 if ((scb = initio_find_first_pend_scb(host)) == NULL)
1178 return;
1179
1180
1181 outb((host->scsi_id << 4) | (scb->target & 0x0F),
1182 host->addr + TUL_SScsiId);
1183 if (scb->opcode == ExecSCSI) {
1184 active_tc = &host->targets[scb->target];
1185
1186 if (scb->tagmsg)
1187 active_tc->drv_flags |= TCF_DRV_EN_TAG;
1188 else
1189 active_tc->drv_flags &= ~TCF_DRV_EN_TAG;
1190
1191 outb(active_tc->js_period, host->addr + TUL_SPeriod);
1192 if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {
1193 initio_select_atn_stop(host, scb);
1194 } else {
1195 if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {
1196 initio_select_atn_stop(host, scb);
1197 } else {
1198 if (scb->tagmsg)
1199 initio_select_atn3(host, scb);
1200 else
1201 initio_select_atn(host, scb);
1202 }
1203 }
1204 if (scb->flags & SCF_POLL) {
1205 while (wait_tulip(host) != -1) {
1206 if (initio_next_state(host) == -1)
1207 break;
1208 }
1209 }
1210 } else if (scb->opcode == BusDevRst) {
1211 initio_select_atn_stop(host, scb);
1212 scb->next_state = 8;
1213 if (scb->flags & SCF_POLL) {
1214 while (wait_tulip(host) != -1) {
1215 if (initio_next_state(host) == -1)
1216 break;
1217 }
1218 }
1219 } else if (scb->opcode == AbortCmd) {
1220 if (initio_abort_srb(host, scb->srb) != 0) {
1221 initio_unlink_pend_scb(host, scb);
1222 initio_release_scb(host, scb);
1223 } else {
1224 scb->opcode = BusDevRst;
1225 initio_select_atn_stop(host, scb);
1226 scb->next_state = 8;
1227 }
1228 } else {
1229 initio_unlink_pend_scb(host, scb);
1230 scb->hastat = 0x16;
1231 initio_append_done_scb(host, scb);
1232 }
1233 return;
1234 }
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246 static int initio_next_state(struct initio_host * host)
1247 {
1248 int next;
1249
1250 next = host->active->next_state;
1251 for (;;) {
1252 switch (next) {
1253 case 1:
1254 next = initio_state_1(host);
1255 break;
1256 case 2:
1257 next = initio_state_2(host);
1258 break;
1259 case 3:
1260 next = initio_state_3(host);
1261 break;
1262 case 4:
1263 next = initio_state_4(host);
1264 break;
1265 case 5:
1266 next = initio_state_5(host);
1267 break;
1268 case 6:
1269 next = initio_state_6(host);
1270 break;
1271 case 7:
1272 next = initio_state_7(host);
1273 break;
1274 case 8:
1275 return initio_bus_device_reset(host);
1276 default:
1277 return initio_bad_seq(host);
1278 }
1279 if (next <= 0)
1280 return next;
1281 }
1282 }
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292 static int initio_state_1(struct initio_host * host)
1293 {
1294 struct scsi_ctrl_blk *scb = host->active;
1295 struct target_control *active_tc = host->active_tc;
1296 #if DEBUG_STATE
1297 printk("-s1-");
1298 #endif
1299
1300
1301 initio_unlink_pend_scb(host, scb);
1302 initio_append_busy_scb(host, scb);
1303
1304 outb(active_tc->sconfig0, host->addr + TUL_SConfig );
1305
1306 if (host->phase == MSG_OUT) {
1307 outb(TSC_EN_BUS_IN | TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1308 outb(scb->ident, host->addr + TUL_SFifo);
1309
1310 if (scb->tagmsg) {
1311 outb(scb->tagmsg, host->addr + TUL_SFifo);
1312 outb(scb->tagid, host->addr + TUL_SFifo);
1313 }
1314 if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {
1315 active_tc->flags |= TCF_WDTR_DONE;
1316 outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1317 outb(2, host->addr + TUL_SFifo);
1318 outb(EXTENDED_SDTR, host->addr + TUL_SFifo);
1319 outb(1, host->addr + TUL_SFifo);
1320 } else if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {
1321 active_tc->flags |= TCF_SYNC_DONE;
1322 outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1323 outb(3, host->addr + TUL_SFifo);
1324 outb(EXTENDED_SDTR, host->addr + TUL_SFifo);
1325 outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1326 outb(MAX_OFFSET, host->addr + TUL_SFifo);
1327 }
1328 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1329 if (wait_tulip(host) == -1)
1330 return -1;
1331 }
1332 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1333 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1334
1335 return 3;
1336 }
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347 static int initio_state_2(struct initio_host * host)
1348 {
1349 struct scsi_ctrl_blk *scb = host->active;
1350 struct target_control *active_tc = host->active_tc;
1351 #if DEBUG_STATE
1352 printk("-s2-");
1353 #endif
1354
1355 initio_unlink_pend_scb(host, scb);
1356 initio_append_busy_scb(host, scb);
1357
1358 outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1359
1360 if (host->jsstatus1 & TSS_CMD_PH_CMP)
1361 return 4;
1362
1363 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1364 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1365
1366 return 3;
1367 }
1368
1369
1370
1371
1372
1373
1374
1375
1376 static int initio_state_3(struct initio_host * host)
1377 {
1378 struct scsi_ctrl_blk *scb = host->active;
1379 struct target_control *active_tc = host->active_tc;
1380 int i;
1381
1382 #if DEBUG_STATE
1383 printk("-s3-");
1384 #endif
1385 for (;;) {
1386 switch (host->phase) {
1387 case CMD_OUT:
1388 for (i = 0; i < (int) scb->cdblen; i++)
1389 outb(scb->cdb[i], host->addr + TUL_SFifo);
1390 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1391 if (wait_tulip(host) == -1)
1392 return -1;
1393 if (host->phase == CMD_OUT)
1394 return initio_bad_seq(host);
1395 return 4;
1396
1397 case MSG_IN:
1398 scb->next_state = 3;
1399 if (initio_msgin(host) == -1)
1400 return -1;
1401 break;
1402
1403 case STATUS_IN:
1404 if (initio_status_msg(host) == -1)
1405 return -1;
1406 break;
1407
1408 case MSG_OUT:
1409 if (active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) {
1410 outb(NOP, host->addr + TUL_SFifo);
1411 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1412 if (wait_tulip(host) == -1)
1413 return -1;
1414 } else {
1415 active_tc->flags |= TCF_SYNC_DONE;
1416
1417 outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1418 outb(3, host->addr + TUL_SFifo);
1419 outb(EXTENDED_SDTR, host->addr + TUL_SFifo);
1420 outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1421 outb(MAX_OFFSET, host->addr + TUL_SFifo);
1422 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1423 if (wait_tulip(host) == -1)
1424 return -1;
1425 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1426 outb(inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7), host->addr + TUL_SSignal);
1427
1428 }
1429 break;
1430 default:
1431 return initio_bad_seq(host);
1432 }
1433 }
1434 }
1435
1436
1437
1438
1439
1440
1441
1442
1443 static int initio_state_4(struct initio_host * host)
1444 {
1445 struct scsi_ctrl_blk *scb = host->active;
1446
1447 #if DEBUG_STATE
1448 printk("-s4-");
1449 #endif
1450 if ((scb->flags & SCF_DIR) == SCF_NO_XF) {
1451 return 6;
1452 }
1453 for (;;) {
1454 if (scb->buflen == 0)
1455 return 6;
1456
1457 switch (host->phase) {
1458
1459 case STATUS_IN:
1460 if ((scb->flags & SCF_DIR) != 0)
1461 scb->hastat = HOST_DO_DU;
1462 if ((initio_status_msg(host)) == -1)
1463 return -1;
1464 break;
1465
1466 case MSG_IN:
1467 scb->next_state = 0x4;
1468 if (initio_msgin(host) == -1)
1469 return -1;
1470 break;
1471
1472 case MSG_OUT:
1473 if (host->jsstatus0 & TSS_PAR_ERROR) {
1474 scb->buflen = 0;
1475 scb->hastat = HOST_DO_DU;
1476 if (initio_msgout_ide(host) == -1)
1477 return -1;
1478 return 6;
1479 } else {
1480 outb(NOP, host->addr + TUL_SFifo);
1481 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1482 if (wait_tulip(host) == -1)
1483 return -1;
1484 }
1485 break;
1486
1487 case DATA_IN:
1488 return initio_xfer_data_in(host);
1489
1490 case DATA_OUT:
1491 return initio_xfer_data_out(host);
1492
1493 default:
1494 return initio_bad_seq(host);
1495 }
1496 }
1497 }
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507 static int initio_state_5(struct initio_host * host)
1508 {
1509 struct scsi_ctrl_blk *scb = host->active;
1510 long cnt, xcnt;
1511
1512 #if DEBUG_STATE
1513 printk("-s5-");
1514 #endif
1515
1516 cnt = inl(host->addr + TUL_SCnt0) & 0x0FFFFFF;
1517
1518 if (inb(host->addr + TUL_XCmd) & 0x20) {
1519
1520
1521 if (host->jsstatus0 & TSS_PAR_ERROR)
1522 scb->hastat = HOST_DO_DU;
1523 if (inb(host->addr + TUL_XStatus) & XPEND) {
1524
1525 outb(inb(host->addr + TUL_XCtrl) | 0x80, host->addr + TUL_XCtrl);
1526
1527 while (inb(host->addr + TUL_XStatus) & XPEND)
1528 cpu_relax();
1529 }
1530 } else {
1531
1532 if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) {
1533 if (host->active_tc->js_period & TSC_WIDE_SCSI)
1534 cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F) << 1;
1535 else
1536 cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F);
1537 }
1538 if (inb(host->addr + TUL_XStatus) & XPEND) {
1539 outb(TAX_X_ABT, host->addr + TUL_XCmd);
1540
1541 while ((inb(host->addr + TUL_Int) & XABT) == 0)
1542 cpu_relax();
1543 }
1544 if ((cnt == 1) && (host->phase == DATA_OUT)) {
1545 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1546 if (wait_tulip(host) == -1)
1547 return -1;
1548 cnt = 0;
1549 } else {
1550 if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0)
1551 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1552 }
1553 }
1554 if (cnt == 0) {
1555 scb->buflen = 0;
1556 return 6;
1557 }
1558
1559 xcnt = (long) scb->buflen - cnt;
1560 scb->buflen = (u32) cnt;
1561 if (scb->flags & SCF_SG) {
1562 struct sg_entry *sgp;
1563 unsigned long i;
1564
1565 sgp = &scb->sglist[scb->sgidx];
1566 for (i = scb->sgidx; i < scb->sgmax; sgp++, i++) {
1567 xcnt -= (long) sgp->len;
1568 if (xcnt < 0) {
1569 xcnt += (long) sgp->len;
1570 sgp->data += (u32) xcnt;
1571 sgp->len -= (u32) xcnt;
1572 scb->bufptr += ((u32) (i - scb->sgidx) << 3);
1573
1574 scb->sglen = (u8) (scb->sgmax - i);
1575
1576 scb->sgidx = (u16) i;
1577
1578 return 4;
1579 }
1580
1581 }
1582 return 6;
1583 } else {
1584 scb->bufptr += (u32) xcnt;
1585 }
1586 return 4;
1587 }
1588
1589
1590
1591
1592
1593
1594
1595
1596 static int initio_state_6(struct initio_host * host)
1597 {
1598 struct scsi_ctrl_blk *scb = host->active;
1599
1600 #if DEBUG_STATE
1601 printk("-s6-");
1602 #endif
1603 for (;;) {
1604 switch (host->phase) {
1605 case STATUS_IN:
1606 if ((initio_status_msg(host)) == -1)
1607 return -1;
1608 break;
1609
1610 case MSG_IN:
1611 scb->next_state = 6;
1612 if ((initio_msgin(host)) == -1)
1613 return -1;
1614 break;
1615
1616 case MSG_OUT:
1617 outb(NOP, host->addr + TUL_SFifo);
1618 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1619 if (wait_tulip(host) == -1)
1620 return -1;
1621 break;
1622
1623 case DATA_IN:
1624 return initio_xpad_in(host);
1625
1626 case DATA_OUT:
1627 return initio_xpad_out(host);
1628
1629 default:
1630 return initio_bad_seq(host);
1631 }
1632 }
1633 }
1634
1635
1636
1637
1638
1639
1640
1641 static int initio_state_7(struct initio_host * host)
1642 {
1643 int cnt, i;
1644
1645 #if DEBUG_STATE
1646 printk("-s7-");
1647 #endif
1648
1649 cnt = inb(host->addr + TUL_SFifoCnt) & 0x1F;
1650 if (cnt) {
1651 for (i = 0; i < cnt; i++)
1652 inb(host->addr + TUL_SFifo);
1653 }
1654 switch (host->phase) {
1655 case DATA_IN:
1656 case DATA_OUT:
1657 return initio_bad_seq(host);
1658 default:
1659 return 6;
1660 }
1661 }
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671 static int initio_xfer_data_in(struct initio_host * host)
1672 {
1673 struct scsi_ctrl_blk *scb = host->active;
1674
1675 if ((scb->flags & SCF_DIR) == SCF_DOUT)
1676 return 6;
1677
1678 outl(scb->buflen, host->addr + TUL_SCnt0);
1679 outb(TSC_XF_DMA_IN, host->addr + TUL_SCmd);
1680
1681 if (scb->flags & SCF_SG) {
1682 outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1683 outl(scb->bufptr, host->addr + TUL_XAddH);
1684 outb(TAX_SG_IN, host->addr + TUL_XCmd);
1685 } else {
1686 outl(scb->buflen, host->addr + TUL_XCntH);
1687 outl(scb->bufptr, host->addr + TUL_XAddH);
1688 outb(TAX_X_IN, host->addr + TUL_XCmd);
1689 }
1690 scb->next_state = 0x5;
1691 return 0;
1692 }
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703 static int initio_xfer_data_out(struct initio_host * host)
1704 {
1705 struct scsi_ctrl_blk *scb = host->active;
1706
1707 if ((scb->flags & SCF_DIR) == SCF_DIN)
1708 return 6;
1709
1710 outl(scb->buflen, host->addr + TUL_SCnt0);
1711 outb(TSC_XF_DMA_OUT, host->addr + TUL_SCmd);
1712
1713 if (scb->flags & SCF_SG) {
1714 outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1715 outl(scb->bufptr, host->addr + TUL_XAddH);
1716 outb(TAX_SG_OUT, host->addr + TUL_XCmd);
1717 } else {
1718 outl(scb->buflen, host->addr + TUL_XCntH);
1719 outl(scb->bufptr, host->addr + TUL_XAddH);
1720 outb(TAX_X_OUT, host->addr + TUL_XCmd);
1721 }
1722
1723 scb->next_state = 0x5;
1724 return 0;
1725 }
1726
1727 int initio_xpad_in(struct initio_host * host)
1728 {
1729 struct scsi_ctrl_blk *scb = host->active;
1730 struct target_control *active_tc = host->active_tc;
1731
1732 if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1733 scb->hastat = HOST_DO_DU;
1734 for (;;) {
1735 if (active_tc->js_period & TSC_WIDE_SCSI)
1736 outl(2, host->addr + TUL_SCnt0);
1737 else
1738 outl(1, host->addr + TUL_SCnt0);
1739
1740 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1741 if (wait_tulip(host) == -1)
1742 return -1;
1743 if (host->phase != DATA_IN) {
1744 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1745 return 6;
1746 }
1747 inb(host->addr + TUL_SFifo);
1748 }
1749 }
1750
1751 int initio_xpad_out(struct initio_host * host)
1752 {
1753 struct scsi_ctrl_blk *scb = host->active;
1754 struct target_control *active_tc = host->active_tc;
1755
1756 if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1757 scb->hastat = HOST_DO_DU;
1758 for (;;) {
1759 if (active_tc->js_period & TSC_WIDE_SCSI)
1760 outl(2, host->addr + TUL_SCnt0);
1761 else
1762 outl(1, host->addr + TUL_SCnt0);
1763
1764 outb(0, host->addr + TUL_SFifo);
1765 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1766 if ((wait_tulip(host)) == -1)
1767 return -1;
1768 if (host->phase != DATA_OUT) {
1769 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1770 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1771 return 6;
1772 }
1773 }
1774 }
1775
1776 int initio_status_msg(struct initio_host * host)
1777 {
1778 struct scsi_ctrl_blk *scb = host->active;
1779 u8 msg;
1780
1781 outb(TSC_CMD_COMP, host->addr + TUL_SCmd);
1782 if (wait_tulip(host) == -1)
1783 return -1;
1784
1785
1786 scb->tastat = inb(host->addr + TUL_SFifo);
1787
1788 if (host->phase == MSG_OUT) {
1789 if (host->jsstatus0 & TSS_PAR_ERROR)
1790 outb(MSG_PARITY_ERROR, host->addr + TUL_SFifo);
1791 else
1792 outb(NOP, host->addr + TUL_SFifo);
1793 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1794 return wait_tulip(host);
1795 }
1796 if (host->phase == MSG_IN) {
1797 msg = inb(host->addr + TUL_SFifo);
1798 if (host->jsstatus0 & TSS_PAR_ERROR) {
1799 if ((initio_msgin_accept(host)) == -1)
1800 return -1;
1801 if (host->phase != MSG_OUT)
1802 return initio_bad_seq(host);
1803 outb(MSG_PARITY_ERROR, host->addr + TUL_SFifo);
1804 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1805 return wait_tulip(host);
1806 }
1807 if (msg == 0) {
1808
1809 if ((scb->tastat & 0x18) == 0x10)
1810 return initio_bad_seq(host);
1811 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1812 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
1813 return initio_wait_done_disc(host);
1814
1815 }
1816 if (msg == LINKED_CMD_COMPLETE ||
1817 msg == LINKED_FLG_CMD_COMPLETE) {
1818 if ((scb->tastat & 0x18) == 0x10)
1819 return initio_msgin_accept(host);
1820 }
1821 }
1822 return initio_bad_seq(host);
1823 }
1824
1825
1826
1827 int int_initio_busfree(struct initio_host * host)
1828 {
1829 struct scsi_ctrl_blk *scb = host->active;
1830
1831 if (scb != NULL) {
1832 if (scb->status & SCB_SELECT) {
1833 initio_unlink_pend_scb(host, scb);
1834 scb->hastat = HOST_SEL_TOUT;
1835 initio_append_done_scb(host, scb);
1836 } else {
1837 initio_unlink_busy_scb(host, scb);
1838 scb->hastat = HOST_BUS_FREE;
1839 initio_append_done_scb(host, scb);
1840 }
1841 host->active = NULL;
1842 host->active_tc = NULL;
1843 }
1844 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1845 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
1846 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1847 return -1;
1848 }
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861 static int int_initio_scsi_rst(struct initio_host * host)
1862 {
1863 struct scsi_ctrl_blk *scb;
1864 int i;
1865
1866
1867 if (inb(host->addr + TUL_XStatus) & 0x01) {
1868 outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
1869
1870 while ((inb(host->addr + TUL_Int) & 0x04) == 0)
1871 cpu_relax();
1872 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1873 }
1874
1875 while ((scb = initio_pop_busy_scb(host)) != NULL) {
1876 scb->hastat = HOST_BAD_PHAS;
1877 initio_append_done_scb(host, scb);
1878 }
1879 host->active = NULL;
1880 host->active_tc = NULL;
1881
1882
1883 for (i = 0; i < host->max_tar; i++)
1884 host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1885 return -1;
1886 }
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897 int int_initio_resel(struct initio_host * host)
1898 {
1899 struct scsi_ctrl_blk *scb;
1900 struct target_control *active_tc;
1901 u8 tag, msg = 0;
1902 u8 tar, lun;
1903
1904 if ((scb = host->active) != NULL) {
1905
1906 if (scb->status & SCB_SELECT)
1907 scb->status &= ~SCB_SELECT;
1908 host->active = NULL;
1909 }
1910
1911 tar = inb(host->addr + TUL_SBusId);
1912
1913 lun = inb(host->addr + TUL_SIdent) & 0x0F;
1914
1915 active_tc = &host->targets[tar];
1916 host->active_tc = active_tc;
1917 outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1918 outb(active_tc->js_period, host->addr + TUL_SPeriod);
1919
1920
1921 if (active_tc->drv_flags & TCF_DRV_EN_TAG) {
1922 if ((initio_msgin_accept(host)) == -1)
1923 return -1;
1924 if (host->phase != MSG_IN)
1925 goto no_tag;
1926 outl(1, host->addr + TUL_SCnt0);
1927 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1928 if (wait_tulip(host) == -1)
1929 return -1;
1930 msg = inb(host->addr + TUL_SFifo);
1931
1932 if (msg < SIMPLE_QUEUE_TAG || msg > ORDERED_QUEUE_TAG)
1933
1934 goto no_tag;
1935
1936 if (initio_msgin_accept(host) == -1)
1937 return -1;
1938
1939 if (host->phase != MSG_IN)
1940 goto no_tag;
1941
1942 outl(1, host->addr + TUL_SCnt0);
1943 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1944 if (wait_tulip(host) == -1)
1945 return -1;
1946 tag = inb(host->addr + TUL_SFifo);
1947 scb = host->scb + tag;
1948 if (scb->target != tar || scb->lun != lun) {
1949 return initio_msgout_abort_tag(host);
1950 }
1951 if (scb->status != SCB_BUSY) {
1952 return initio_msgout_abort_tag(host);
1953 }
1954 host->active = scb;
1955 if ((initio_msgin_accept(host)) == -1)
1956 return -1;
1957 } else {
1958 no_tag:
1959 if ((scb = initio_find_busy_scb(host, tar | (lun << 8))) == NULL) {
1960 return initio_msgout_abort_targ(host);
1961 }
1962 host->active = scb;
1963 if (!(active_tc->drv_flags & TCF_DRV_EN_TAG)) {
1964 if ((initio_msgin_accept(host)) == -1)
1965 return -1;
1966 }
1967 }
1968 return 0;
1969 }
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979 static int int_initio_bad_seq(struct initio_host * host)
1980 {
1981 struct scsi_ctrl_blk *scb;
1982 int i;
1983
1984 initio_reset_scsi(host, 10);
1985
1986 while ((scb = initio_pop_busy_scb(host)) != NULL) {
1987 scb->hastat = HOST_BAD_PHAS;
1988 initio_append_done_scb(host, scb);
1989 }
1990 for (i = 0; i < host->max_tar; i++)
1991 host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1992 return -1;
1993 }
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004 static int initio_msgout_abort_targ(struct initio_host * host)
2005 {
2006
2007 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2008 if (initio_msgin_accept(host) == -1)
2009 return -1;
2010 if (host->phase != MSG_OUT)
2011 return initio_bad_seq(host);
2012
2013 outb(ABORT_TASK_SET, host->addr + TUL_SFifo);
2014 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2015
2016 return initio_wait_disc(host);
2017 }
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027 static int initio_msgout_abort_tag(struct initio_host * host)
2028 {
2029
2030 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2031 if (initio_msgin_accept(host) == -1)
2032 return -1;
2033 if (host->phase != MSG_OUT)
2034 return initio_bad_seq(host);
2035
2036 outb(ABORT_TASK, host->addr + TUL_SFifo);
2037 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2038
2039 return initio_wait_disc(host);
2040
2041 }
2042
2043
2044
2045
2046
2047
2048
2049 static int initio_msgin(struct initio_host * host)
2050 {
2051 struct target_control *active_tc;
2052
2053 for (;;) {
2054 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2055
2056 outl(1, host->addr + TUL_SCnt0);
2057 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2058 if (wait_tulip(host) == -1)
2059 return -1;
2060
2061 switch (inb(host->addr + TUL_SFifo)) {
2062 case DISCONNECT:
2063 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2064 return initio_wait_disc(host);
2065 case SAVE_POINTERS:
2066 case RESTORE_POINTERS:
2067 case NOP:
2068 initio_msgin_accept(host);
2069 break;
2070 case MESSAGE_REJECT:
2071 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)),
2072 host->addr + TUL_SSignal);
2073 active_tc = host->active_tc;
2074 if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)
2075 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN),
2076 host->addr + TUL_SSignal);
2077 initio_msgin_accept(host);
2078 break;
2079 case EXTENDED_MESSAGE:
2080 initio_msgin_extend(host);
2081 break;
2082 case IGNORE_WIDE_RESIDUE:
2083 initio_msgin_accept(host);
2084 break;
2085 case COMMAND_COMPLETE:
2086 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2087 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2088 return initio_wait_done_disc(host);
2089 default:
2090 initio_msgout_reject(host);
2091 break;
2092 }
2093 if (host->phase != MSG_IN)
2094 return host->phase;
2095 }
2096
2097 }
2098
2099 static int initio_msgout_reject(struct initio_host * host)
2100 {
2101 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2102
2103 if (initio_msgin_accept(host) == -1)
2104 return -1;
2105
2106 if (host->phase == MSG_OUT) {
2107 outb(MESSAGE_REJECT, host->addr + TUL_SFifo);
2108 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2109 return wait_tulip(host);
2110 }
2111 return host->phase;
2112 }
2113
2114 static int initio_msgout_ide(struct initio_host * host)
2115 {
2116 outb(INITIATOR_ERROR, host->addr + TUL_SFifo);
2117 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2118 return wait_tulip(host);
2119 }
2120
2121 static int initio_msgin_extend(struct initio_host * host)
2122 {
2123 u8 len, idx;
2124
2125 if (initio_msgin_accept(host) != MSG_IN)
2126 return host->phase;
2127
2128
2129 outl(1, host->addr + TUL_SCnt0);
2130 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2131 if (wait_tulip(host) == -1)
2132 return -1;
2133
2134 len = inb(host->addr + TUL_SFifo);
2135 host->msg[0] = len;
2136 for (idx = 1; len != 0; len--) {
2137
2138 if ((initio_msgin_accept(host)) != MSG_IN)
2139 return host->phase;
2140 outl(1, host->addr + TUL_SCnt0);
2141 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2142 if (wait_tulip(host) == -1)
2143 return -1;
2144 host->msg[idx++] = inb(host->addr + TUL_SFifo);
2145 }
2146 if (host->msg[1] == 1) {
2147 u8 r;
2148 if (host->msg[0] != 3)
2149 return initio_msgout_reject(host);
2150 if (host->active_tc->flags & TCF_NO_SYNC_NEGO) {
2151 host->msg[3] = 0;
2152 } else {
2153 if (initio_msgin_sync(host) == 0 &&
2154 (host->active_tc->flags & TCF_SYNC_DONE)) {
2155 initio_sync_done(host);
2156 return initio_msgin_accept(host);
2157 }
2158 }
2159
2160 r = inb(host->addr + TUL_SSignal);
2161 outb((r & (TSC_SET_ACK | 7)) | TSC_SET_ATN,
2162 host->addr + TUL_SSignal);
2163 if (initio_msgin_accept(host) != MSG_OUT)
2164 return host->phase;
2165
2166 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2167
2168 initio_sync_done(host);
2169
2170 outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
2171 outb(3, host->addr + TUL_SFifo);
2172 outb(EXTENDED_SDTR, host->addr + TUL_SFifo);
2173 outb(host->msg[2], host->addr + TUL_SFifo);
2174 outb(host->msg[3], host->addr + TUL_SFifo);
2175 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2176 return wait_tulip(host);
2177 }
2178 if (host->msg[0] != 2 || host->msg[1] != 3)
2179 return initio_msgout_reject(host);
2180
2181 if (host->active_tc->flags & TCF_NO_WDTR) {
2182 host->msg[2] = 0;
2183 } else {
2184 if (host->msg[2] > 2)
2185 return initio_msgout_reject(host);
2186 if (host->msg[2] == 2) {
2187 host->msg[2] = 1;
2188 } else {
2189 if ((host->active_tc->flags & TCF_NO_WDTR) == 0) {
2190 wdtr_done(host);
2191 if ((host->active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)
2192 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2193 return initio_msgin_accept(host);
2194 }
2195 }
2196 }
2197 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2198
2199 if (initio_msgin_accept(host) != MSG_OUT)
2200 return host->phase;
2201
2202 outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
2203 outb(2, host->addr + TUL_SFifo);
2204 outb(EXTENDED_WDTR, host->addr + TUL_SFifo);
2205 outb(host->msg[2], host->addr + TUL_SFifo);
2206 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2207 return wait_tulip(host);
2208 }
2209
2210 static int initio_msgin_sync(struct initio_host * host)
2211 {
2212 char default_period;
2213
2214 default_period = initio_rate_tbl[host->active_tc->flags & TCF_SCSI_RATE];
2215 if (host->msg[3] > MAX_OFFSET) {
2216 host->msg[3] = MAX_OFFSET;
2217 if (host->msg[2] < default_period) {
2218 host->msg[2] = default_period;
2219 return 1;
2220 }
2221 if (host->msg[2] >= 59)
2222 host->msg[3] = 0;
2223 return 1;
2224 }
2225
2226 if (host->msg[3] == 0) {
2227 return 0;
2228 }
2229 if (host->msg[2] < default_period) {
2230 host->msg[2] = default_period;
2231 return 1;
2232 }
2233 if (host->msg[2] >= 59) {
2234 host->msg[3] = 0;
2235 return 1;
2236 }
2237 return 0;
2238 }
2239
2240 static int wdtr_done(struct initio_host * host)
2241 {
2242 host->active_tc->flags &= ~TCF_SYNC_DONE;
2243 host->active_tc->flags |= TCF_WDTR_DONE;
2244
2245 host->active_tc->js_period = 0;
2246 if (host->msg[2])
2247 host->active_tc->js_period |= TSC_WIDE_SCSI;
2248 host->active_tc->sconfig0 &= ~TSC_ALT_PERIOD;
2249 outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2250 outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
2251
2252 return 1;
2253 }
2254
2255 static int initio_sync_done(struct initio_host * host)
2256 {
2257 int i;
2258
2259 host->active_tc->flags |= TCF_SYNC_DONE;
2260
2261 if (host->msg[3]) {
2262 host->active_tc->js_period |= host->msg[3];
2263 for (i = 0; i < 8; i++) {
2264 if (initio_rate_tbl[i] >= host->msg[2])
2265 break;
2266 }
2267 host->active_tc->js_period |= (i << 4);
2268 host->active_tc->sconfig0 |= TSC_ALT_PERIOD;
2269 }
2270 outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2271 outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
2272
2273 return -1;
2274 }
2275
2276
2277 static int initio_post_scsi_rst(struct initio_host * host)
2278 {
2279 struct scsi_ctrl_blk *scb;
2280 struct target_control *active_tc;
2281 int i;
2282
2283 host->active = NULL;
2284 host->active_tc = NULL;
2285 host->flags = 0;
2286
2287 while ((scb = initio_pop_busy_scb(host)) != NULL) {
2288 scb->hastat = HOST_BAD_PHAS;
2289 initio_append_done_scb(host, scb);
2290 }
2291
2292 active_tc = &host->targets[0];
2293 for (i = 0; i < host->max_tar; active_tc++, i++) {
2294 active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
2295
2296 active_tc->js_period = 0;
2297 active_tc->sconfig0 = host->sconf1;
2298 host->act_tags[0] = 0;
2299 host->targets[i].flags &= ~TCF_BUSY;
2300 }
2301
2302 return -1;
2303 }
2304
2305 static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb)
2306 {
2307 scb->status |= SCB_SELECT;
2308 scb->next_state = 0x1;
2309 host->active = scb;
2310 host->active_tc = &host->targets[scb->target];
2311 outb(TSC_SELATNSTOP, host->addr + TUL_SCmd);
2312 }
2313
2314
2315 static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb)
2316 {
2317 int i;
2318
2319 scb->status |= SCB_SELECT;
2320 scb->next_state = 0x2;
2321
2322 outb(scb->ident, host->addr + TUL_SFifo);
2323 for (i = 0; i < (int) scb->cdblen; i++)
2324 outb(scb->cdb[i], host->addr + TUL_SFifo);
2325 host->active_tc = &host->targets[scb->target];
2326 host->active = scb;
2327 outb(TSC_SEL_ATN, host->addr + TUL_SCmd);
2328 }
2329
2330 static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb)
2331 {
2332 int i;
2333
2334 scb->status |= SCB_SELECT;
2335 scb->next_state = 0x2;
2336
2337 outb(scb->ident, host->addr + TUL_SFifo);
2338 outb(scb->tagmsg, host->addr + TUL_SFifo);
2339 outb(scb->tagid, host->addr + TUL_SFifo);
2340 for (i = 0; i < scb->cdblen; i++)
2341 outb(scb->cdb[i], host->addr + TUL_SFifo);
2342 host->active_tc = &host->targets[scb->target];
2343 host->active = scb;
2344 outb(TSC_SEL_ATN3, host->addr + TUL_SCmd);
2345 }
2346
2347
2348
2349
2350
2351
2352
2353
2354 int initio_bus_device_reset(struct initio_host * host)
2355 {
2356 struct scsi_ctrl_blk *scb = host->active;
2357 struct target_control *active_tc = host->active_tc;
2358 struct scsi_ctrl_blk *tmp, *prev;
2359 u8 tar;
2360
2361 if (host->phase != MSG_OUT)
2362 return int_initio_bad_seq(host);
2363
2364 initio_unlink_pend_scb(host, scb);
2365 initio_release_scb(host, scb);
2366
2367
2368 tar = scb->target;
2369 active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE | TCF_BUSY);
2370
2371
2372
2373 prev = tmp = host->first_busy;
2374 while (tmp != NULL) {
2375 if (tmp->target == tar) {
2376
2377 if (tmp == host->first_busy) {
2378 if ((host->first_busy = tmp->next) == NULL)
2379 host->last_busy = NULL;
2380 } else {
2381 prev->next = tmp->next;
2382 if (tmp == host->last_busy)
2383 host->last_busy = prev;
2384 }
2385 tmp->hastat = HOST_ABORTED;
2386 initio_append_done_scb(host, tmp);
2387 }
2388
2389 else {
2390 prev = tmp;
2391 }
2392 tmp = tmp->next;
2393 }
2394 outb(TARGET_RESET, host->addr + TUL_SFifo);
2395 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2396 return initio_wait_disc(host);
2397
2398 }
2399
2400 static int initio_msgin_accept(struct initio_host * host)
2401 {
2402 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2403 return wait_tulip(host);
2404 }
2405
2406 static int wait_tulip(struct initio_host * host)
2407 {
2408
2409 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2410 & TSS_INT_PENDING))
2411 cpu_relax();
2412
2413 host->jsint = inb(host->addr + TUL_SInt);
2414 host->phase = host->jsstatus0 & TSS_PH_MASK;
2415 host->jsstatus1 = inb(host->addr + TUL_SStatus1);
2416
2417 if (host->jsint & TSS_RESEL_INT)
2418 return int_initio_resel(host);
2419 if (host->jsint & TSS_SEL_TIMEOUT)
2420 return int_initio_busfree(host);
2421 if (host->jsint & TSS_SCSIRST_INT)
2422 return int_initio_scsi_rst(host);
2423
2424 if (host->jsint & TSS_DISC_INT) {
2425 if (host->flags & HCF_EXPECT_DONE_DISC) {
2426 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2427 initio_unlink_busy_scb(host, host->active);
2428 host->active->hastat = 0;
2429 initio_append_done_scb(host, host->active);
2430 host->active = NULL;
2431 host->active_tc = NULL;
2432 host->flags &= ~HCF_EXPECT_DONE_DISC;
2433 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2434 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
2435 return -1;
2436 }
2437 if (host->flags & HCF_EXPECT_DISC) {
2438 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2439 host->active = NULL;
2440 host->active_tc = NULL;
2441 host->flags &= ~HCF_EXPECT_DISC;
2442 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2443 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
2444 return -1;
2445 }
2446 return int_initio_busfree(host);
2447 }
2448
2449 if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV))
2450 return host->phase;
2451 return host->phase;
2452 }
2453
2454 static int initio_wait_disc(struct initio_host * host)
2455 {
2456 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING))
2457 cpu_relax();
2458
2459 host->jsint = inb(host->addr + TUL_SInt);
2460
2461 if (host->jsint & TSS_SCSIRST_INT)
2462 return int_initio_scsi_rst(host);
2463 if (host->jsint & TSS_DISC_INT) {
2464 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2465 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2466 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
2467 host->active = NULL;
2468 return -1;
2469 }
2470 return initio_bad_seq(host);
2471 }
2472
2473 static int initio_wait_done_disc(struct initio_host * host)
2474 {
2475 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2476 & TSS_INT_PENDING))
2477 cpu_relax();
2478
2479 host->jsint = inb(host->addr + TUL_SInt);
2480
2481 if (host->jsint & TSS_SCSIRST_INT)
2482 return int_initio_scsi_rst(host);
2483 if (host->jsint & TSS_DISC_INT) {
2484 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2485 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2486 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
2487 initio_unlink_busy_scb(host, host->active);
2488
2489 initio_append_done_scb(host, host->active);
2490 host->active = NULL;
2491 return -1;
2492 }
2493 return initio_bad_seq(host);
2494 }
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505 static irqreturn_t i91u_intr(int irqno, void *dev_id)
2506 {
2507 struct Scsi_Host *dev = dev_id;
2508 unsigned long flags;
2509 int r;
2510
2511 spin_lock_irqsave(dev->host_lock, flags);
2512 r = initio_isr((struct initio_host *)dev->hostdata);
2513 spin_unlock_irqrestore(dev->host_lock, flags);
2514 if (r)
2515 return IRQ_HANDLED;
2516 else
2517 return IRQ_NONE;
2518 }
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532 static void initio_build_scb(struct initio_host * host, struct scsi_ctrl_blk * cblk, struct scsi_cmnd * cmnd)
2533 {
2534 struct scatterlist *sglist;
2535 struct sg_entry *sg;
2536 int i, nseg;
2537 long total_len;
2538 dma_addr_t dma_addr;
2539
2540
2541 cblk->post = i91uSCBPost;
2542 cblk->srb = cmnd;
2543 cblk->opcode = ExecSCSI;
2544 cblk->flags = SCF_POST;
2545 cblk->target = cmnd->device->id;
2546 cblk->lun = cmnd->device->lun;
2547 cblk->ident = cmnd->device->lun | DISC_ALLOW;
2548
2549 cblk->flags |= SCF_SENSE;
2550
2551
2552 dma_addr = dma_map_single(&host->pci_dev->dev, cmnd->sense_buffer,
2553 SENSE_SIZE, DMA_FROM_DEVICE);
2554 cblk->senseptr = (u32)dma_addr;
2555 cblk->senselen = SENSE_SIZE;
2556 initio_priv(cmnd)->sense_dma_addr = dma_addr;
2557 cblk->cdblen = cmnd->cmd_len;
2558
2559
2560 cblk->hastat = 0;
2561 cblk->tastat = 0;
2562
2563 memcpy(cblk->cdb, cmnd->cmnd, cmnd->cmd_len);
2564
2565
2566 if (cmnd->device->tagged_supported) {
2567 cblk->tagmsg = SIMPLE_QUEUE_TAG;
2568 } else {
2569 cblk->tagmsg = 0;
2570 }
2571
2572
2573 nseg = scsi_dma_map(cmnd);
2574 BUG_ON(nseg < 0);
2575 if (nseg) {
2576 dma_addr = dma_map_single(&host->pci_dev->dev, &cblk->sglist[0],
2577 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
2578 DMA_BIDIRECTIONAL);
2579 cblk->bufptr = (u32)dma_addr;
2580 initio_priv(cmnd)->sglist_dma_addr = dma_addr;
2581
2582 cblk->sglen = nseg;
2583
2584 cblk->flags |= SCF_SG;
2585 total_len = 0;
2586 sg = &cblk->sglist[0];
2587 scsi_for_each_sg(cmnd, sglist, cblk->sglen, i) {
2588 sg->data = cpu_to_le32((u32)sg_dma_address(sglist));
2589 sg->len = cpu_to_le32((u32)sg_dma_len(sglist));
2590 total_len += sg_dma_len(sglist);
2591 ++sg;
2592 }
2593
2594 cblk->buflen = (scsi_bufflen(cmnd) > total_len) ?
2595 total_len : scsi_bufflen(cmnd);
2596 } else {
2597 cblk->buflen = 0;
2598 cblk->sglen = 0;
2599 }
2600 }
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610 static int i91u_queuecommand_lck(struct scsi_cmnd *cmd)
2611 {
2612 struct initio_host *host = (struct initio_host *) cmd->device->host->hostdata;
2613 struct scsi_ctrl_blk *cmnd;
2614
2615 cmnd = initio_alloc_scb(host);
2616 if (!cmnd)
2617 return SCSI_MLQUEUE_HOST_BUSY;
2618
2619 initio_build_scb(host, cmnd, cmd);
2620 initio_exec_scb(host, cmnd);
2621 return 0;
2622 }
2623
2624 static DEF_SCSI_QCMD(i91u_queuecommand)
2625
2626
2627
2628
2629
2630
2631
2632
2633 static int i91u_bus_reset(struct scsi_cmnd * cmnd)
2634 {
2635 struct initio_host *host;
2636
2637 host = (struct initio_host *) cmnd->device->host->hostdata;
2638
2639 spin_lock_irq(cmnd->device->host->host_lock);
2640 initio_reset_scsi(host, 0);
2641 spin_unlock_irq(cmnd->device->host->host_lock);
2642
2643 return SUCCESS;
2644 }
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659 static int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev,
2660 sector_t capacity, int *info_array)
2661 {
2662 struct initio_host *host;
2663 struct target_control *tc;
2664
2665 host = (struct initio_host *) sdev->host->hostdata;
2666 tc = &host->targets[sdev->id];
2667
2668 if (tc->heads) {
2669 info_array[0] = tc->heads;
2670 info_array[1] = tc->sectors;
2671 info_array[2] = (unsigned long)capacity / tc->heads / tc->sectors;
2672 } else {
2673 if (tc->drv_flags & TCF_DRV_255_63) {
2674 info_array[0] = 255;
2675 info_array[1] = 63;
2676 info_array[2] = (unsigned long)capacity / 255 / 63;
2677 } else {
2678 info_array[0] = 64;
2679 info_array[1] = 32;
2680 info_array[2] = (unsigned long)capacity >> 11;
2681 }
2682 }
2683
2684 #if defined(DEBUG_BIOSPARAM)
2685 if (i91u_debug & debug_biosparam) {
2686 printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
2687 info_array[0], info_array[1], info_array[2]);
2688 printk("WARNING: check, if the bios geometry is correct.\n");
2689 }
2690 #endif
2691
2692 return 0;
2693 }
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704 static void i91u_unmap_scb(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd)
2705 {
2706
2707 if (initio_priv(cmnd)->sense_dma_addr) {
2708 dma_unmap_single(&pci_dev->dev,
2709 initio_priv(cmnd)->sense_dma_addr,
2710 SENSE_SIZE, DMA_FROM_DEVICE);
2711 initio_priv(cmnd)->sense_dma_addr = 0;
2712 }
2713
2714
2715 if (scsi_sg_count(cmnd)) {
2716 dma_unmap_single(&pci_dev->dev,
2717 initio_priv(cmnd)->sglist_dma_addr,
2718 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
2719 DMA_BIDIRECTIONAL);
2720
2721 scsi_dma_unmap(cmnd);
2722 }
2723 }
2724
2725
2726
2727
2728
2729
2730
2731
2732 static void i91uSCBPost(u8 * host_mem, u8 * cblk_mem)
2733 {
2734 struct scsi_cmnd *cmnd;
2735 struct initio_host *host;
2736 struct scsi_ctrl_blk *cblk;
2737
2738 host = (struct initio_host *) host_mem;
2739 cblk = (struct scsi_ctrl_blk *) cblk_mem;
2740 if ((cmnd = cblk->srb) == NULL) {
2741 printk(KERN_ERR "i91uSCBPost: SRB pointer is empty\n");
2742 WARN_ON(1);
2743 initio_release_scb(host, cblk);
2744 return;
2745 }
2746
2747
2748
2749
2750 switch (cblk->hastat) {
2751 case 0x0:
2752 case 0xa:
2753 case 0xb:
2754 cblk->hastat = 0;
2755 break;
2756
2757 case 0x11:
2758
2759 cblk->hastat = DID_TIME_OUT;
2760 break;
2761
2762 case 0x14:
2763
2764
2765
2766 cblk->hastat = DID_RESET;
2767 break;
2768
2769 case 0x1a:
2770 cblk->hastat = DID_ABORT;
2771 break;
2772
2773 case 0x12:
2774
2775
2776 case 0x13:
2777 case 0x16:
2778
2779 default:
2780 printk("ini9100u: %x %x\n", cblk->hastat, cblk->tastat);
2781 cblk->hastat = DID_ERROR;
2782 break;
2783 }
2784
2785 cmnd->result = cblk->tastat | (cblk->hastat << 16);
2786 i91u_unmap_scb(host->pci_dev, cmnd);
2787 scsi_done(cmnd);
2788 initio_release_scb(host, cblk);
2789 }
2790
2791 static struct scsi_host_template initio_template = {
2792 .proc_name = "INI9100U",
2793 .name = "Initio INI-9X00U/UW SCSI device driver",
2794 .queuecommand = i91u_queuecommand,
2795 .eh_bus_reset_handler = i91u_bus_reset,
2796 .bios_param = i91u_biosparam,
2797 .can_queue = MAX_TARGETS * i91u_MAXQUEUE,
2798 .this_id = 1,
2799 .sg_tablesize = SG_ALL,
2800 .cmd_size = sizeof(struct initio_cmd_priv),
2801 };
2802
2803 static int initio_probe_one(struct pci_dev *pdev,
2804 const struct pci_device_id *id)
2805 {
2806 struct Scsi_Host *shost;
2807 struct initio_host *host;
2808 u32 reg;
2809 u16 bios_seg;
2810 struct scsi_ctrl_blk *scb, *tmp, *prev = NULL ;
2811 int num_scb, i, error;
2812
2813 error = pci_enable_device(pdev);
2814 if (error)
2815 return error;
2816
2817 pci_read_config_dword(pdev, 0x44, (u32 *) & reg);
2818 bios_seg = (u16) (reg & 0xFF);
2819 if (((reg & 0xFF00) >> 8) == 0xFF)
2820 reg = 0;
2821 bios_seg = (bios_seg << 8) + ((u16) ((reg & 0xFF00) >> 8));
2822
2823 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
2824 printk(KERN_WARNING "i91u: Could not set 32 bit DMA mask\n");
2825 error = -ENODEV;
2826 goto out_disable_device;
2827 }
2828 shost = scsi_host_alloc(&initio_template, sizeof(struct initio_host));
2829 if (!shost) {
2830 printk(KERN_WARNING "initio: Could not allocate host structure.\n");
2831 error = -ENOMEM;
2832 goto out_disable_device;
2833 }
2834 host = (struct initio_host *)shost->hostdata;
2835 memset(host, 0, sizeof(struct initio_host));
2836 host->addr = pci_resource_start(pdev, 0);
2837 host->bios_addr = bios_seg;
2838
2839 if (!request_region(host->addr, 256, "i91u")) {
2840 printk(KERN_WARNING "initio: I/O port range 0x%x is busy.\n", host->addr);
2841 error = -ENODEV;
2842 goto out_host_put;
2843 }
2844
2845 if (initio_tag_enable)
2846 num_scb = MAX_TARGETS * i91u_MAXQUEUE;
2847 else
2848 num_scb = MAX_TARGETS + 3;
2849
2850 for (; num_scb >= MAX_TARGETS + 3; num_scb--) {
2851 i = num_scb * sizeof(struct scsi_ctrl_blk);
2852 scb = kzalloc(i, GFP_KERNEL);
2853 if (scb)
2854 break;
2855 }
2856
2857 if (!scb) {
2858 printk(KERN_WARNING "initio: Cannot allocate SCB array.\n");
2859 error = -ENOMEM;
2860 goto out_release_region;
2861 }
2862
2863 host->pci_dev = pdev;
2864
2865 host->semaph = 1;
2866 spin_lock_init(&host->semaph_lock);
2867 host->num_scbs = num_scb;
2868 host->scb = scb;
2869 host->next_pending = scb;
2870 host->next_avail = scb;
2871 for (i = 0, tmp = scb; i < num_scb; i++, tmp++) {
2872 tmp->tagid = i;
2873 if (i != 0)
2874 prev->next = tmp;
2875 prev = tmp;
2876 }
2877 prev->next = NULL;
2878 host->scb_end = tmp;
2879 host->first_avail = scb;
2880 host->last_avail = prev;
2881 spin_lock_init(&host->avail_lock);
2882
2883 initio_init(host, phys_to_virt(((u32)bios_seg << 4)));
2884
2885 host->jsstatus0 = 0;
2886
2887 shost->io_port = host->addr;
2888 shost->n_io_port = 0xff;
2889 shost->can_queue = num_scb;
2890 shost->unique_id = host->addr;
2891 shost->max_id = host->max_tar;
2892 shost->max_lun = 32;
2893 shost->irq = pdev->irq;
2894 shost->this_id = host->scsi_id;
2895 shost->base = host->addr;
2896 shost->sg_tablesize = TOTAL_SG_ENTRY;
2897
2898 error = request_irq(pdev->irq, i91u_intr, IRQF_SHARED, "i91u", shost);
2899 if (error < 0) {
2900 printk(KERN_WARNING "initio: Unable to request IRQ %d\n", pdev->irq);
2901 goto out_free_scbs;
2902 }
2903
2904 pci_set_drvdata(pdev, shost);
2905
2906 error = scsi_add_host(shost, &pdev->dev);
2907 if (error)
2908 goto out_free_irq;
2909 scsi_scan_host(shost);
2910 return 0;
2911 out_free_irq:
2912 free_irq(pdev->irq, shost);
2913 out_free_scbs:
2914 kfree(host->scb);
2915 out_release_region:
2916 release_region(host->addr, 256);
2917 out_host_put:
2918 scsi_host_put(shost);
2919 out_disable_device:
2920 pci_disable_device(pdev);
2921 return error;
2922 }
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932 static void initio_remove_one(struct pci_dev *pdev)
2933 {
2934 struct Scsi_Host *host = pci_get_drvdata(pdev);
2935 struct initio_host *s = (struct initio_host *)host->hostdata;
2936 scsi_remove_host(host);
2937 free_irq(pdev->irq, host);
2938 release_region(s->addr, 256);
2939 scsi_host_put(host);
2940 pci_disable_device(pdev);
2941 }
2942
2943 MODULE_LICENSE("GPL");
2944
2945 static struct pci_device_id initio_pci_tbl[] = {
2946 {PCI_VENDOR_ID_INIT, 0x9500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2947 {PCI_VENDOR_ID_INIT, 0x9400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2948 {PCI_VENDOR_ID_INIT, 0x9401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2949 {PCI_VENDOR_ID_INIT, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2950 {PCI_VENDOR_ID_DOMEX, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2951 {0,}
2952 };
2953 MODULE_DEVICE_TABLE(pci, initio_pci_tbl);
2954
2955 static struct pci_driver initio_pci_driver = {
2956 .name = "initio",
2957 .id_table = initio_pci_tbl,
2958 .probe = initio_probe_one,
2959 .remove = initio_remove_one,
2960 };
2961 module_pci_driver(initio_pci_driver);
2962
2963 MODULE_DESCRIPTION("Initio INI-9X00U/UW SCSI device driver");
2964 MODULE_AUTHOR("Initio Corporation");
2965 MODULE_LICENSE("GPL");