1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**************************************************************************
3 * Initio 9100 device driver for Linux.
5 * Copyright (c) 1994-1998 Initio Corporation
6 * Copyright (c) 1998 Bas Vermeulen <bvermeul@blackstar.xs4all.nl>
7 * Copyright (c) 2004 Christoph Hellwig <hch@lst.de>
8 * Copyright (c) 2007 Red Hat
10 *************************************************************************
14 * This is the Linux low-level SCSI driver for Initio INI-9X00U/UW SCSI host
17 * 08/06/97 hc - v1.01h
18 * - Support inic-940 and inic-935
19 * 09/26/97 hc - v1.01i
20 * - Make correction from J.W. Schultz suggestion
21 * 10/13/97 hc - Support reset function
22 * 10/21/97 hc - v1.01j
23 * - Support 32 LUN (SCSI 3)
24 * 01/14/98 hc - v1.01k
25 * - Fix memory allocation problem
26 * 03/04/98 hc - v1.01l
27 * - Fix tape rewind which will hang the system problem
28 * - Set can_queue to initio_num_scb
29 * 06/25/98 hc - v1.01m
30 * - Get it work for kernel version >= 2.1.75
31 * - Dynamic assign SCSI bus reset holding time in initio_init()
32 * 07/02/98 hc - v1.01n
34 * 08/07/98 hc - v1.01o
35 * - Change the initio_abort_srb routine to use scsi_done. <01>
37 * - Change the INI9100U define and proc_dir_entry to
38 * reflect the newer Kernel 2.1.118, but the v1.o1o
39 * should work with Kernel 2.1.118.
40 * 09/20/98 wh - v1.02a
41 * - Support Abort command.
42 * - Handle reset routine.
45 * 12/09/98 bv - v1.03a
46 * - Removed unused code
47 * 12/13/98 bv - v1.03b
48 * - Remove cli() locking for kernels >= 2.1.95. This uses
49 * spinlocks to serialize access to the pSRB_head and
50 * pSRB_tail members of the HCS structure.
51 * 09/01/99 bv - v1.03d
52 * - Fixed a deadlock problem in SMP.
53 * 21/01/99 bv - v1.03e
54 * - Add support for the Domex 3192U PCI SCSI
55 * This is a slightly modified patch by
56 * Brian Macy <bmacy@sunshinecomputing.com>
57 * 22/02/99 bv - v1.03f
58 * - Didn't detect the INIC-950 in 2.0.x correctly.
60 * 05/07/99 bv - v1.03g
61 * - Changed the assumption that HZ = 100
63 * - added new DMA API support
64 * 06/01/04 jmd - v1.04a
65 * - Re-add reset_bus support
66 **************************************************************************/
68 #include <linux/module.h>
69 #include <linux/errno.h>
70 #include <linux/delay.h>
71 #include <linux/pci.h>
72 #include <linux/init.h>
73 #include <linux/blkdev.h>
74 #include <linux/spinlock.h>
75 #include <linux/stat.h>
76 #include <linux/kernel.h>
77 #include <linux/proc_fs.h>
78 #include <linux/string.h>
79 #include <linux/interrupt.h>
80 #include <linux/ioport.h>
81 #include <linux/slab.h>
82 #include <linux/jiffies.h>
83 #include <linux/dma-mapping.h>
86 #include <scsi/scsi.h>
87 #include <scsi/scsi_cmnd.h>
88 #include <scsi/scsi_device.h>
89 #include <scsi/scsi_host.h>
90 #include <scsi/scsi_tcq.h>
96 #define i91u_MAXQUEUE 2
97 #define i91u_REVID "Initio INI-9X00U/UW SCSI device driver; Revision: 1.04a"
100 static unsigned int i91u_debug = DEBUG_DEFAULT;
103 static int initio_tag_enable = 1;
106 static int setup_debug = 0;
109 static void i91uSCBPost(u8 * pHcb, u8 * pScb);
111 #define DEBUG_INTERRUPT 0
112 #define DEBUG_QUEUE 0
113 #define DEBUG_STATE 0
116 /*--- forward references ---*/
117 static struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun);
118 static struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host);
120 static int tulip_main(struct initio_host * host);
122 static int initio_next_state(struct initio_host * host);
123 static int initio_state_1(struct initio_host * host);
124 static int initio_state_2(struct initio_host * host);
125 static int initio_state_3(struct initio_host * host);
126 static int initio_state_4(struct initio_host * host);
127 static int initio_state_5(struct initio_host * host);
128 static int initio_state_6(struct initio_host * host);
129 static int initio_state_7(struct initio_host * host);
130 static int initio_xfer_data_in(struct initio_host * host);
131 static int initio_xfer_data_out(struct initio_host * host);
132 static int initio_xpad_in(struct initio_host * host);
133 static int initio_xpad_out(struct initio_host * host);
134 static int initio_status_msg(struct initio_host * host);
136 static int initio_msgin(struct initio_host * host);
137 static int initio_msgin_sync(struct initio_host * host);
138 static int initio_msgin_accept(struct initio_host * host);
139 static int initio_msgout_reject(struct initio_host * host);
140 static int initio_msgin_extend(struct initio_host * host);
142 static int initio_msgout_ide(struct initio_host * host);
143 static int initio_msgout_abort_targ(struct initio_host * host);
144 static int initio_msgout_abort_tag(struct initio_host * host);
146 static int initio_bus_device_reset(struct initio_host * host);
147 static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb);
148 static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb);
149 static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb);
150 static int int_initio_busfree(struct initio_host * host);
151 static int int_initio_scsi_rst(struct initio_host * host);
152 static int int_initio_bad_seq(struct initio_host * host);
153 static int int_initio_resel(struct initio_host * host);
154 static int initio_sync_done(struct initio_host * host);
155 static int wdtr_done(struct initio_host * host);
156 static int wait_tulip(struct initio_host * host);
157 static int initio_wait_done_disc(struct initio_host * host);
158 static int initio_wait_disc(struct initio_host * host);
159 static void tulip_scsi(struct initio_host * host);
160 static int initio_post_scsi_rst(struct initio_host * host);
162 static void initio_se2_ew_en(unsigned long base);
163 static void initio_se2_ew_ds(unsigned long base);
164 static int initio_se2_rd_all(unsigned long base);
165 static void initio_se2_update_all(unsigned long base); /* setup default pattern */
166 static void initio_read_eeprom(unsigned long base);
168 /* ---- INTERNAL VARIABLES ---- */
170 static NVRAM i91unvram;
171 static NVRAM *i91unvramp;
173 static u8 i91udftNvRam[64] =
175 /*----------- header -----------*/
176 0x25, 0xc9, /* Signature */
179 /* -- Host Adapter Structure -- */
180 0x95, /* ModelByte0 */
181 0x00, /* ModelByte1 */
182 0x00, /* ModelInfo */
184 NBC1_DEFAULT, /* BIOSConfig1 */
188 /* SCSI channel 0 and target Structure */
190 NCC1_DEFAULT, /* SCSIconfig1 */
192 0x10, /* NumSCSItarget */
194 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
195 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
196 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
197 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
199 /* SCSI channel 1 and target Structure */
201 NCC1_DEFAULT, /* SCSIconfig1 */
203 0x10, /* NumSCSItarget */
205 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
206 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
207 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
208 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
209 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
210 0, 0}; /* - CheckSum - */
213 static u8 initio_rate_tbl[8] = /* fast 20 */
215 /* nanosecond divide by 4 */
217 18, /* 75ns, 13.3M */
220 37, /* 150ns, 6.6M */
221 43, /* 175ns, 5.7M */
226 static void initio_do_pause(unsigned amount)
228 /* Pause for amount jiffies */
229 unsigned long the_time = jiffies + amount;
231 while (time_before_eq(jiffies, the_time))
235 /*-- forward reference --*/
237 /******************************************************************
238 Input: instruction for Serial E2PROM
240 EX: se2_rd(0 call se2_instr() to send address and read command
242 StartBit OP_Code Address Data
243 --------- -------- ------------------ -------
244 1 1 , 0 A5,A4,A3,A2,A1,A0 D15-D0
246 +-----------------------------------------------------
249 +--+ +--+ +--+ +--+ +--+
252 CLK -------+ +--+ +--+ +--+ +--+ +--
253 (leading edge trigger)
257 DI ----+ +--0------------------
258 (address and cmd sent to nvram)
260 -------------------------------------------+
263 (data sent from nvram)
266 ******************************************************************/
269 * initio_se2_instr - bitbang an instruction
270 * @base: Base of InitIO controller
271 * @instr: Instruction for serial E2PROM
273 * Bitbang an instruction out to the serial E2Prom
276 static void initio_se2_instr(unsigned long base, u8 instr)
281 outb(SE2CS | SE2DO, base + TUL_NVRAM); /* cs+start bit */
283 outb(SE2CS | SE2CLK | SE2DO, base + TUL_NVRAM); /* +CLK */
286 for (i = 0; i < 8; i++) {
288 b = SE2CS | SE2DO; /* -CLK+dataBit */
290 b = SE2CS; /* -CLK */
291 outb(b, base + TUL_NVRAM);
293 outb(b | SE2CLK, base + TUL_NVRAM); /* +CLK */
297 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
303 * initio_se2_ew_en - Enable erase/write
304 * @base: Base address of InitIO controller
306 * Enable erase/write state of serial EEPROM
308 void initio_se2_ew_en(unsigned long base)
310 initio_se2_instr(base, 0x30); /* EWEN */
311 outb(0, base + TUL_NVRAM); /* -CS */
317 * initio_se2_ew_ds - Disable erase/write
318 * @base: Base address of InitIO controller
320 * Disable erase/write state of serial EEPROM
322 void initio_se2_ew_ds(unsigned long base)
324 initio_se2_instr(base, 0); /* EWDS */
325 outb(0, base + TUL_NVRAM); /* -CS */
331 * initio_se2_rd - read E2PROM word
332 * @base: Base of InitIO controller
333 * @addr: Address of word in E2PROM
335 * Read a word from the NV E2PROM device
337 static u16 initio_se2_rd(unsigned long base, u8 addr)
343 instr = (u8) (addr | 0x80);
344 initio_se2_instr(base, instr); /* READ INSTR */
346 for (i = 15; i >= 0; i--) {
347 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
349 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
351 /* sample data after the following edge of clock */
352 rb = inb(base + TUL_NVRAM);
355 udelay(30); /* 6/20/95 */
358 outb(0, base + TUL_NVRAM); /* no chip select */
364 * initio_se2_wr - read E2PROM word
365 * @base: Base of InitIO controller
366 * @addr: Address of word in E2PROM
367 * @val: Value to write
369 * Write a word to the NV E2PROM device. Used when recovering from
370 * a problem with the NV.
372 static void initio_se2_wr(unsigned long base, u8 addr, u16 val)
377 instr = (u8) (addr | 0x40);
378 initio_se2_instr(base, instr); /* WRITE INSTR */
379 for (i = 15; i >= 0; i--) {
381 outb(SE2CS | SE2DO, base + TUL_NVRAM); /* -CLK+dataBit 1 */
383 outb(SE2CS, base + TUL_NVRAM); /* -CLK+dataBit 0 */
385 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
389 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
391 outb(0, base + TUL_NVRAM); /* -CS */
394 outb(SE2CS, base + TUL_NVRAM); /* +CS */
398 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
400 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
402 if (inb(base + TUL_NVRAM) & SE2DI)
403 break; /* write complete */
405 outb(0, base + TUL_NVRAM); /* -CS */
409 * initio_se2_rd_all - read hostadapter NV configuration
410 * @base: Base address of InitIO controller
412 * Reads the E2PROM data into main memory. Ensures that the checksum
413 * and header marker are valid. Returns 1 on success -1 on error.
416 static int initio_se2_rd_all(unsigned long base)
422 i91unvramp = &i91unvram;
423 np = (u16 *) i91unvramp;
424 for (i = 0; i < 32; i++)
425 *np++ = initio_se2_rd(base, i);
427 /* Is signature "ini" ok ? */
428 if (i91unvramp->NVM_Signature != INI_SIGNATURE)
430 /* Is ckecksum ok ? */
431 np = (u16 *) i91unvramp;
432 for (i = 0; i < 31; i++)
434 if (i91unvramp->NVM_CheckSum != chksum)
440 * initio_se2_update_all - Update E2PROM
441 * @base: Base of InitIO controller
443 * Update the E2PROM by wrting any changes into the E2PROM
444 * chip, rewriting the checksum.
446 static void initio_se2_update_all(unsigned long base)
447 { /* setup default pattern */
452 i91unvramp = &i91unvram;
453 /* Calculate checksum first */
454 np = (u16 *) i91udftNvRam;
455 for (i = 0; i < 31; i++)
458 initio_se2_ew_en(base); /* Enable write */
460 np = (u16 *) i91udftNvRam;
461 np1 = (u16 *) i91unvramp;
462 for (i = 0; i < 32; i++, np++, np1++) {
464 initio_se2_wr(base, i, *np);
466 initio_se2_ew_ds(base); /* Disable write */
470 * initio_read_eeprom - Retrieve configuration
471 * @base: Base of InitIO Host Adapter
473 * Retrieve the host adapter configuration data from E2Prom. If the
474 * data is invalid then the defaults are used and are also restored
475 * into the E2PROM. This forms the access point for the SCSI driver
476 * into the E2PROM layer, the other functions for the E2PROM are all
479 * Must be called single threaded, uses a shared global area.
482 static void initio_read_eeprom(unsigned long base)
486 i91unvramp = &i91unvram;
487 /* Enable EEProm programming */
488 gctrl = inb(base + TUL_GCTRL);
489 outb(gctrl | TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
490 if (initio_se2_rd_all(base) != 1) {
491 initio_se2_update_all(base); /* setup default pattern */
492 initio_se2_rd_all(base); /* load again */
494 /* Disable EEProm programming */
495 gctrl = inb(base + TUL_GCTRL);
496 outb(gctrl & ~TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
500 * initio_stop_bm - stop bus master
501 * @host: InitIO we are stopping
503 * Stop any pending DMA operation, aborting the DMA if necessary
506 static void initio_stop_bm(struct initio_host * host)
509 if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */
510 outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
511 /* wait Abort DMA xfer done */
512 while ((inb(host->addr + TUL_Int) & XABT) == 0)
515 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
519 * initio_reset_scsi - Reset SCSI host controller
520 * @host: InitIO host to reset
521 * @seconds: Recovery time
523 * Perform a full reset of the SCSI subsystem.
526 static int initio_reset_scsi(struct initio_host * host, int seconds)
528 outb(TSC_RST_BUS, host->addr + TUL_SCtrl0);
530 while (!((host->jsint = inb(host->addr + TUL_SInt)) & TSS_SCSIRST_INT))
533 /* reset tulip chip */
534 outb(0, host->addr + TUL_SSignal);
536 /* Stall for a while, wait for target's firmware ready,make it 2 sec ! */
537 /* SONY 5200 tape drive won't work if only stall for 1 sec */
538 /* FIXME: this is a very long busy wait right now */
539 initio_do_pause(seconds * HZ);
541 inb(host->addr + TUL_SInt);
542 return SCSI_RESET_SUCCESS;
546 * initio_init - set up an InitIO host adapter
547 * @host: InitIO host adapter
548 * @bios_addr: BIOS address
550 * Set up the host adapter and devices according to the configuration
551 * retrieved from the E2PROM.
553 * Locking: Calls E2PROM layer code which is not re-enterable so must
554 * run single threaded for now.
557 static void initio_init(struct initio_host * host, u8 *bios_addr)
563 /* Get E2Prom configuration */
564 initio_read_eeprom(host->addr);
565 if (i91unvramp->NVM_SCSIInfo[0].NVM_NumOfTarg == 8)
570 host->config = i91unvramp->NVM_SCSIInfo[0].NVM_ChConfig1;
572 host->scsi_id = i91unvramp->NVM_SCSIInfo[0].NVM_ChSCSIID;
573 host->idmask = ~(1 << host->scsi_id);
576 /* Enable parity error response */
577 outb(inb(host->addr + TUL_PCMD) | 0x40, host->addr + TUL_PCMD);
580 /* Mask all the interrupt */
581 outb(0x1F, host->addr + TUL_Mask);
583 initio_stop_bm(host);
584 /* --- Initialize the tulip --- */
585 outb(TSC_RST_CHIP, host->addr + TUL_SCtrl0);
587 /* program HBA's SCSI ID */
588 outb(host->scsi_id << 4, host->addr + TUL_SScsiId);
590 /* Enable Initiator Mode ,phase latch,alternate sync period mode,
591 disable SCSI reset */
592 if (host->config & HCC_EN_PAR)
593 host->sconf1 = (TSC_INITDEFAULT | TSC_EN_SCSI_PAR);
595 host->sconf1 = (TSC_INITDEFAULT);
596 outb(host->sconf1, host->addr + TUL_SConfig);
598 /* Enable HW reselect */
599 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
601 outb(0, host->addr + TUL_SPeriod);
603 /* selection time out = 250 ms */
604 outb(153, host->addr + TUL_STimeOut);
606 /* Enable SCSI terminator */
607 outb((host->config & (HCC_ACT_TERM1 | HCC_ACT_TERM2)),
608 host->addr + TUL_XCtrl);
609 outb(((host->config & HCC_AUTO_TERM) >> 4) |
610 (inb(host->addr + TUL_GCTRL1) & 0xFE),
611 host->addr + TUL_GCTRL1);
614 flags = & (i91unvramp->NVM_SCSIInfo[0].NVM_Targ0Config),
615 heads = bios_addr + 0x180;
618 host->targets[i].flags = *flags & ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
619 if (host->targets[i].flags & TCF_EN_255)
620 host->targets[i].drv_flags = TCF_DRV_255_63;
622 host->targets[i].drv_flags = 0;
623 host->targets[i].js_period = 0;
624 host->targets[i].sconfig0 = host->sconf1;
625 host->targets[i].heads = *heads++;
626 if (host->targets[i].heads == 255)
627 host->targets[i].drv_flags = TCF_DRV_255_63;
629 host->targets[i].drv_flags = 0;
630 host->targets[i].sectors = *heads++;
631 host->targets[i].flags &= ~TCF_BUSY;
632 host->act_tags[i] = 0;
633 host->max_tags[i] = 0xFF;
635 printk("i91u: PCI Base=0x%04X, IRQ=%d, BIOS=0x%04X0, SCSI ID=%d\n",
636 host->addr, host->pci_dev->irq,
637 host->bios_addr, host->scsi_id);
639 if (host->config & HCC_SCSI_RESET) {
640 printk(KERN_INFO "i91u: Reset SCSI Bus ... \n");
641 initio_reset_scsi(host, 10);
643 outb(0x17, host->addr + TUL_SCFG1);
644 outb(0xE9, host->addr + TUL_SIntEnable);
648 * initio_alloc_scb - Allocate an SCB
649 * @host: InitIO host we are allocating for
651 * Walk the SCB list for the controller and allocate a free SCB if
654 static struct scsi_ctrl_blk *initio_alloc_scb(struct initio_host *host)
656 struct scsi_ctrl_blk *scb;
659 spin_lock_irqsave(&host->avail_lock, flags);
660 if ((scb = host->first_avail) != NULL) {
662 printk("find scb at %p\n", scb);
664 if ((host->first_avail = scb->next) == NULL)
665 host->last_avail = NULL;
667 scb->status = SCB_RENT;
669 spin_unlock_irqrestore(&host->avail_lock, flags);
674 * initio_release_scb - Release an SCB
675 * @host: InitIO host that owns the SCB
676 * @cmnd: SCB command block being returned
678 * Return an allocated SCB to the host free list
681 static void initio_release_scb(struct initio_host * host, struct scsi_ctrl_blk * cmnd)
686 printk("Release SCB %p; ", cmnd);
688 spin_lock_irqsave(&(host->avail_lock), flags);
692 if (host->last_avail != NULL) {
693 host->last_avail->next = cmnd;
694 host->last_avail = cmnd;
696 host->first_avail = cmnd;
697 host->last_avail = cmnd;
699 spin_unlock_irqrestore(&(host->avail_lock), flags);
702 /***************************************************************************/
703 static void initio_append_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
707 printk("Append pend SCB %p; ", scbp);
709 scbp->status = SCB_PEND;
711 if (host->last_pending != NULL) {
712 host->last_pending->next = scbp;
713 host->last_pending = scbp;
715 host->first_pending = scbp;
716 host->last_pending = scbp;
720 /***************************************************************************/
721 static void initio_push_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
725 printk("Push pend SCB %p; ", scbp);
727 scbp->status = SCB_PEND;
728 if ((scbp->next = host->first_pending) != NULL) {
729 host->first_pending = scbp;
731 host->first_pending = scbp;
732 host->last_pending = scbp;
736 static struct scsi_ctrl_blk *initio_find_first_pend_scb(struct initio_host * host)
738 struct scsi_ctrl_blk *first;
741 first = host->first_pending;
742 while (first != NULL) {
743 if (first->opcode != ExecSCSI)
745 if (first->tagmsg == 0) {
746 if ((host->act_tags[first->target] == 0) &&
747 !(host->targets[first->target].flags & TCF_BUSY))
750 if ((host->act_tags[first->target] >=
751 host->max_tags[first->target]) |
752 (host->targets[first->target].flags & TCF_BUSY)) {
763 static void initio_unlink_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
765 struct scsi_ctrl_blk *tmp, *prev;
768 printk("unlink pend SCB %p; ", scb);
771 prev = tmp = host->first_pending;
772 while (tmp != NULL) {
773 if (scb == tmp) { /* Unlink this SCB */
774 if (tmp == host->first_pending) {
775 if ((host->first_pending = tmp->next) == NULL)
776 host->last_pending = NULL;
778 prev->next = tmp->next;
779 if (tmp == host->last_pending)
780 host->last_pending = prev;
790 static void initio_append_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
794 printk("append busy SCB %p; ", scbp);
797 host->act_tags[scbp->target]++;
799 host->targets[scbp->target].flags |= TCF_BUSY;
800 scbp->status = SCB_BUSY;
802 if (host->last_busy != NULL) {
803 host->last_busy->next = scbp;
804 host->last_busy = scbp;
806 host->first_busy = scbp;
807 host->last_busy = scbp;
811 /***************************************************************************/
812 static struct scsi_ctrl_blk *initio_pop_busy_scb(struct initio_host * host)
814 struct scsi_ctrl_blk *tmp;
817 if ((tmp = host->first_busy) != NULL) {
818 if ((host->first_busy = tmp->next) == NULL)
819 host->last_busy = NULL;
822 host->act_tags[tmp->target]--;
824 host->targets[tmp->target].flags &= ~TCF_BUSY;
827 printk("Pop busy SCB %p; ", tmp);
832 /***************************************************************************/
833 static void initio_unlink_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
835 struct scsi_ctrl_blk *tmp, *prev;
838 printk("unlink busy SCB %p; ", scb);
841 prev = tmp = host->first_busy;
842 while (tmp != NULL) {
843 if (scb == tmp) { /* Unlink this SCB */
844 if (tmp == host->first_busy) {
845 if ((host->first_busy = tmp->next) == NULL)
846 host->last_busy = NULL;
848 prev->next = tmp->next;
849 if (tmp == host->last_busy)
850 host->last_busy = prev;
854 host->act_tags[tmp->target]--;
856 host->targets[tmp->target].flags &= ~TCF_BUSY;
865 struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun)
867 struct scsi_ctrl_blk *tmp;
871 tmp = host->first_busy;
872 while (tmp != NULL) {
873 scbp_tarlun = (tmp->lun << 8) | (tmp->target);
874 if (scbp_tarlun == tarlun) { /* Unlink this SCB */
880 printk("find busy SCB %p; ", tmp);
885 static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
888 printk("append done SCB %p; ", scbp);
891 scbp->status = SCB_DONE;
893 if (host->last_done != NULL) {
894 host->last_done->next = scbp;
895 host->last_done = scbp;
897 host->first_done = scbp;
898 host->last_done = scbp;
902 struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host)
904 struct scsi_ctrl_blk *tmp;
906 if ((tmp = host->first_done) != NULL) {
907 if ((host->first_done = tmp->next) == NULL)
908 host->last_done = NULL;
912 printk("find done SCB %p; ",tmp);
917 static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp)
920 struct scsi_ctrl_blk *tmp, *prev;
922 spin_lock_irqsave(&host->semaph_lock, flags);
924 if ((host->semaph == 0) && (host->active == NULL)) {
925 /* disable Jasmin SCSI Int */
926 outb(0x1F, host->addr + TUL_Mask);
927 spin_unlock_irqrestore(&host->semaph_lock, flags);
928 /* FIXME: synchronize_irq needed ? */
930 spin_lock_irqsave(&host->semaph_lock, flags);
932 outb(0x0F, host->addr + TUL_Mask);
933 spin_unlock_irqrestore(&host->semaph_lock, flags);
934 return SCSI_ABORT_SNOOZE;
936 prev = tmp = host->first_pending; /* Check Pend queue */
937 while (tmp != NULL) {
939 if (tmp->srb == srbp) {
940 if (tmp == host->active) {
941 spin_unlock_irqrestore(&host->semaph_lock, flags);
942 return SCSI_ABORT_BUSY;
943 } else if (tmp == host->first_pending) {
944 if ((host->first_pending = tmp->next) == NULL)
945 host->last_pending = NULL;
947 prev->next = tmp->next;
948 if (tmp == host->last_pending)
949 host->last_pending = prev;
951 tmp->hastat = HOST_ABORTED;
952 tmp->flags |= SCF_DONE;
953 if (tmp->flags & SCF_POST)
954 (*tmp->post) ((u8 *) host, (u8 *) tmp);
955 spin_unlock_irqrestore(&host->semaph_lock, flags);
956 return SCSI_ABORT_SUCCESS;
962 prev = tmp = host->first_busy; /* Check Busy queue */
963 while (tmp != NULL) {
964 if (tmp->srb == srbp) {
965 if (tmp == host->active) {
966 spin_unlock_irqrestore(&host->semaph_lock, flags);
967 return SCSI_ABORT_BUSY;
968 } else if (tmp->tagmsg == 0) {
969 spin_unlock_irqrestore(&host->semaph_lock, flags);
970 return SCSI_ABORT_BUSY;
972 host->act_tags[tmp->target]--;
973 if (tmp == host->first_busy) {
974 if ((host->first_busy = tmp->next) == NULL)
975 host->last_busy = NULL;
977 prev->next = tmp->next;
978 if (tmp == host->last_busy)
979 host->last_busy = prev;
984 tmp->hastat = HOST_ABORTED;
985 tmp->flags |= SCF_DONE;
986 if (tmp->flags & SCF_POST)
987 (*tmp->post) ((u8 *) host, (u8 *) tmp);
988 spin_unlock_irqrestore(&host->semaph_lock, flags);
989 return SCSI_ABORT_SUCCESS;
995 spin_unlock_irqrestore(&host->semaph_lock, flags);
996 return SCSI_ABORT_NOT_RUNNING;
999 /***************************************************************************/
1000 static int initio_bad_seq(struct initio_host * host)
1002 struct scsi_ctrl_blk *scb;
1004 printk("initio_bad_seg c=%d\n", host->index);
1006 if ((scb = host->active) != NULL) {
1007 initio_unlink_busy_scb(host, scb);
1008 scb->hastat = HOST_BAD_PHAS;
1010 initio_append_done_scb(host, scb);
1012 initio_stop_bm(host);
1013 initio_reset_scsi(host, 8); /* 7/29/98 */
1014 return initio_post_scsi_rst(host);
1018 /************************************************************************/
1019 static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
1021 unsigned long flags;
1026 scb->sgmax = scb->sglen;
1028 spin_lock_irqsave(&host->semaph_lock, flags);
1030 initio_append_pend_scb(host, scb); /* Append this SCB to Pending queue */
1032 /* VVVVV 07/21/98 */
1033 if (host->semaph == 1) {
1034 /* Disable Jasmin SCSI Int */
1035 outb(0x1F, host->addr + TUL_Mask);
1037 spin_unlock_irqrestore(&host->semaph_lock, flags);
1041 spin_lock_irqsave(&host->semaph_lock, flags);
1043 outb(0x0F, host->addr + TUL_Mask);
1045 spin_unlock_irqrestore(&host->semaph_lock, flags);
1049 /***************************************************************************/
1050 static int initio_isr(struct initio_host * host)
1052 if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) {
1053 if (host->semaph == 1) {
1054 outb(0x1F, host->addr + TUL_Mask);
1055 /* Disable Tulip SCSI Int */
1061 outb(0x0F, host->addr + TUL_Mask);
1068 static int tulip_main(struct initio_host * host)
1070 struct scsi_ctrl_blk *scb;
1073 tulip_scsi(host); /* Call tulip_scsi */
1075 /* Walk the list of completed SCBs */
1076 while ((scb = initio_find_done_scb(host)) != NULL) { /* find done entry */
1077 if (scb->tastat == INI_QUEUE_FULL) {
1078 host->max_tags[scb->target] =
1079 host->act_tags[scb->target] - 1;
1081 initio_append_pend_scb(host, scb);
1084 if (!(scb->mode & SCM_RSENS)) { /* not in auto req. sense mode */
1085 if (scb->tastat == 2) {
1087 /* clr sync. nego flag */
1089 if (scb->flags & SCF_SENSE) {
1091 len = scb->senselen;
1094 scb->buflen = scb->senselen;
1095 scb->bufptr = scb->senseptr;
1096 scb->flags &= ~(SCF_SG | SCF_DIR); /* for xfer_data_in */
1097 /* so, we won't report wrong direction in xfer_data_in,
1098 and won't report HOST_DO_DU in state_6 */
1099 scb->mode = SCM_RSENS;
1100 scb->ident &= 0xBF; /* Disable Disconnect */
1104 scb->cdb[0] = SCSICMD_RequestSense;
1110 initio_push_pend_scb(host, scb);
1114 } else { /* in request sense mode */
1116 if (scb->tastat == 2) { /* check contition status again after sending
1117 requset sense cmd 0x3 */
1118 scb->hastat = HOST_BAD_PHAS;
1122 scb->flags |= SCF_DONE;
1123 if (scb->flags & SCF_POST) {
1124 /* FIXME: only one post method and lose casts */
1125 (*scb->post) ((u8 *) host, (u8 *) scb);
1129 if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING)
1131 if (host->active) /* return to OS and wait for xfer_done_ISR/Selected_ISR */
1132 return 1; /* return to OS, enable interrupt */
1133 /* Check pending SCB */
1134 if (initio_find_first_pend_scb(host) == NULL)
1135 return 1; /* return to OS, enable interrupt */
1136 } /* End of for loop */
1137 /* statement won't reach here */
1140 static void tulip_scsi(struct initio_host * host)
1142 struct scsi_ctrl_blk *scb;
1143 struct target_control *active_tc;
1145 /* make sure to service interrupt asap */
1146 if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) {
1147 host->phase = host->jsstatus0 & TSS_PH_MASK;
1148 host->jsstatus1 = inb(host->addr + TUL_SStatus1);
1149 host->jsint = inb(host->addr + TUL_SInt);
1150 if (host->jsint & TSS_SCSIRST_INT) { /* SCSI bus reset detected */
1151 int_initio_scsi_rst(host);
1154 if (host->jsint & TSS_RESEL_INT) { /* if selected/reselected interrupt */
1155 if (int_initio_resel(host) == 0)
1156 initio_next_state(host);
1159 if (host->jsint & TSS_SEL_TIMEOUT) {
1160 int_initio_busfree(host);
1163 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
1164 int_initio_busfree(host); /* unexpected bus free or sel timeout */
1167 if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) { /* func complete or Bus service */
1169 initio_next_state(host);
1173 if (host->active != NULL)
1176 if ((scb = initio_find_first_pend_scb(host)) == NULL)
1179 /* program HBA's SCSI ID & target SCSI ID */
1180 outb((host->scsi_id << 4) | (scb->target & 0x0F),
1181 host->addr + TUL_SScsiId);
1182 if (scb->opcode == ExecSCSI) {
1183 active_tc = &host->targets[scb->target];
1186 active_tc->drv_flags |= TCF_DRV_EN_TAG;
1188 active_tc->drv_flags &= ~TCF_DRV_EN_TAG;
1190 outb(active_tc->js_period, host->addr + TUL_SPeriod);
1191 if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) { /* do wdtr negotiation */
1192 initio_select_atn_stop(host, scb);
1194 if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) { /* do sync negotiation */
1195 initio_select_atn_stop(host, scb);
1198 initio_select_atn3(host, scb);
1200 initio_select_atn(host, scb);
1203 if (scb->flags & SCF_POLL) {
1204 while (wait_tulip(host) != -1) {
1205 if (initio_next_state(host) == -1)
1209 } else if (scb->opcode == BusDevRst) {
1210 initio_select_atn_stop(host, scb);
1211 scb->next_state = 8;
1212 if (scb->flags & SCF_POLL) {
1213 while (wait_tulip(host) != -1) {
1214 if (initio_next_state(host) == -1)
1218 } else if (scb->opcode == AbortCmd) {
1219 if (initio_abort_srb(host, scb->srb) != 0) {
1220 initio_unlink_pend_scb(host, scb);
1221 initio_release_scb(host, scb);
1223 scb->opcode = BusDevRst;
1224 initio_select_atn_stop(host, scb);
1225 scb->next_state = 8;
1228 initio_unlink_pend_scb(host, scb);
1229 scb->hastat = 0x16; /* bad command */
1230 initio_append_done_scb(host, scb);
1236 * initio_next_state - Next SCSI state
1237 * @host: InitIO host we are processing
1239 * Progress the active command block along the state machine
1240 * until we hit a state which we must wait for activity to occur.
1242 * Returns zero or a negative code.
1245 static int initio_next_state(struct initio_host * host)
1249 next = host->active->next_state;
1253 next = initio_state_1(host);
1256 next = initio_state_2(host);
1259 next = initio_state_3(host);
1262 next = initio_state_4(host);
1265 next = initio_state_5(host);
1268 next = initio_state_6(host);
1271 next = initio_state_7(host);
1274 return initio_bus_device_reset(host);
1276 return initio_bad_seq(host);
1285 * initio_state_1 - SCSI state machine
1286 * @host: InitIO host we are controlling
1288 * Perform SCSI state processing for Select/Attention/Stop
1291 static int initio_state_1(struct initio_host * host)
1293 struct scsi_ctrl_blk *scb = host->active;
1294 struct target_control *active_tc = host->active_tc;
1299 /* Move the SCB from pending to busy */
1300 initio_unlink_pend_scb(host, scb);
1301 initio_append_busy_scb(host, scb);
1303 outb(active_tc->sconfig0, host->addr + TUL_SConfig );
1305 if (host->phase == MSG_OUT) {
1306 outb(TSC_EN_BUS_IN | TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1307 outb(scb->ident, host->addr + TUL_SFifo);
1310 outb(scb->tagmsg, host->addr + TUL_SFifo);
1311 outb(scb->tagid, host->addr + TUL_SFifo);
1313 if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {
1314 active_tc->flags |= TCF_WDTR_DONE;
1315 outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1316 outb(2, host->addr + TUL_SFifo); /* Extended msg length */
1317 outb(EXTENDED_SDTR, host->addr + TUL_SFifo); /* Sync request */
1318 outb(1, host->addr + TUL_SFifo); /* Start from 16 bits */
1319 } else if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {
1320 active_tc->flags |= TCF_SYNC_DONE;
1321 outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1322 outb(3, host->addr + TUL_SFifo); /* extended msg length */
1323 outb(EXTENDED_SDTR, host->addr + TUL_SFifo); /* sync request */
1324 outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1325 outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */
1327 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1328 if (wait_tulip(host) == -1)
1331 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1332 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1333 /* Into before CDB xfer */
1339 * initio_state_2 - SCSI state machine
1340 * @host: InitIO host we are controlling
1342 * state after selection with attention
1343 * state after selection with attention3
1346 static int initio_state_2(struct initio_host * host)
1348 struct scsi_ctrl_blk *scb = host->active;
1349 struct target_control *active_tc = host->active_tc;
1354 initio_unlink_pend_scb(host, scb);
1355 initio_append_busy_scb(host, scb);
1357 outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1359 if (host->jsstatus1 & TSS_CMD_PH_CMP)
1362 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1363 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1364 /* Into before CDB xfer */
1369 * initio_state_3 - SCSI state machine
1370 * @host: InitIO host we are controlling
1372 * state before CDB xfer is done
1375 static int initio_state_3(struct initio_host * host)
1377 struct scsi_ctrl_blk *scb = host->active;
1378 struct target_control *active_tc = host->active_tc;
1385 switch (host->phase) {
1386 case CMD_OUT: /* Command out phase */
1387 for (i = 0; i < (int) scb->cdblen; i++)
1388 outb(scb->cdb[i], host->addr + TUL_SFifo);
1389 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1390 if (wait_tulip(host) == -1)
1392 if (host->phase == CMD_OUT)
1393 return initio_bad_seq(host);
1396 case MSG_IN: /* Message in phase */
1397 scb->next_state = 3;
1398 if (initio_msgin(host) == -1)
1402 case STATUS_IN: /* Status phase */
1403 if (initio_status_msg(host) == -1)
1407 case MSG_OUT: /* Message out phase */
1408 if (active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) {
1409 outb(NOP, host->addr + TUL_SFifo); /* msg nop */
1410 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1411 if (wait_tulip(host) == -1)
1414 active_tc->flags |= TCF_SYNC_DONE;
1416 outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1417 outb(3, host->addr + TUL_SFifo); /* ext. msg len */
1418 outb(EXTENDED_SDTR, host->addr + TUL_SFifo); /* sync request */
1419 outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1420 outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */
1421 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1422 if (wait_tulip(host) == -1)
1424 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1425 outb(inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7), host->addr + TUL_SSignal);
1430 return initio_bad_seq(host);
1436 * initio_state_4 - SCSI state machine
1437 * @host: InitIO host we are controlling
1439 * SCSI state machine. State 4
1442 static int initio_state_4(struct initio_host * host)
1444 struct scsi_ctrl_blk *scb = host->active;
1449 if ((scb->flags & SCF_DIR) == SCF_NO_XF) {
1450 return 6; /* Go to state 6 (After data) */
1453 if (scb->buflen == 0)
1456 switch (host->phase) {
1458 case STATUS_IN: /* Status phase */
1459 if ((scb->flags & SCF_DIR) != 0) /* if direction bit set then report data underrun */
1460 scb->hastat = HOST_DO_DU;
1461 if ((initio_status_msg(host)) == -1)
1465 case MSG_IN: /* Message in phase */
1466 scb->next_state = 0x4;
1467 if (initio_msgin(host) == -1)
1471 case MSG_OUT: /* Message out phase */
1472 if (host->jsstatus0 & TSS_PAR_ERROR) {
1474 scb->hastat = HOST_DO_DU;
1475 if (initio_msgout_ide(host) == -1)
1479 outb(NOP, host->addr + TUL_SFifo); /* msg nop */
1480 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1481 if (wait_tulip(host) == -1)
1486 case DATA_IN: /* Data in phase */
1487 return initio_xfer_data_in(host);
1489 case DATA_OUT: /* Data out phase */
1490 return initio_xfer_data_out(host);
1493 return initio_bad_seq(host);
1500 * initio_state_5 - SCSI state machine
1501 * @host: InitIO host we are controlling
1503 * State after dma xfer done or phase change before xfer done
1506 static int initio_state_5(struct initio_host * host)
1508 struct scsi_ctrl_blk *scb = host->active;
1509 long cnt, xcnt; /* cannot use unsigned !! code: if (xcnt < 0) */
1514 /*------ get remaining count -------*/
1515 cnt = inl(host->addr + TUL_SCnt0) & 0x0FFFFFF;
1517 if (inb(host->addr + TUL_XCmd) & 0x20) {
1518 /* ----------------------- DATA_IN ----------------------------- */
1519 /* check scsi parity error */
1520 if (host->jsstatus0 & TSS_PAR_ERROR)
1521 scb->hastat = HOST_DO_DU;
1522 if (inb(host->addr + TUL_XStatus) & XPEND) { /* DMA xfer pending, Send STOP */
1523 /* tell Hardware scsi xfer has been terminated */
1524 outb(inb(host->addr + TUL_XCtrl) | 0x80, host->addr + TUL_XCtrl);
1525 /* wait until DMA xfer not pending */
1526 while (inb(host->addr + TUL_XStatus) & XPEND)
1530 /*-------- DATA OUT -----------*/
1531 if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) {
1532 if (host->active_tc->js_period & TSC_WIDE_SCSI)
1533 cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F) << 1;
1535 cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F);
1537 if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */
1538 outb(TAX_X_ABT, host->addr + TUL_XCmd);
1539 /* wait Abort DMA xfer done */
1540 while ((inb(host->addr + TUL_Int) & XABT) == 0)
1543 if ((cnt == 1) && (host->phase == DATA_OUT)) {
1544 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1545 if (wait_tulip(host) == -1)
1549 if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0)
1550 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1555 return 6; /* After Data */
1557 /* Update active data pointer */
1558 xcnt = (long) scb->buflen - cnt; /* xcnt== bytes already xferred */
1559 scb->buflen = (u32) cnt; /* cnt == bytes left to be xferred */
1560 if (scb->flags & SCF_SG) {
1561 struct sg_entry *sgp;
1564 sgp = &scb->sglist[scb->sgidx];
1565 for (i = scb->sgidx; i < scb->sgmax; sgp++, i++) {
1566 xcnt -= (long) sgp->len;
1567 if (xcnt < 0) { /* this sgp xfer half done */
1568 xcnt += (long) sgp->len; /* xcnt == bytes xferred in this sgp */
1569 sgp->data += (u32) xcnt; /* new ptr to be xfer */
1570 sgp->len -= (u32) xcnt; /* new len to be xfer */
1571 scb->bufptr += ((u32) (i - scb->sgidx) << 3);
1572 /* new SG table ptr */
1573 scb->sglen = (u8) (scb->sgmax - i);
1574 /* new SG table len */
1575 scb->sgidx = (u16) i;
1576 /* for next disc and come in this loop */
1577 return 4; /* Go to state 4 */
1579 /* else (xcnt >= 0 , i.e. this sgp already xferred */
1581 return 6; /* Go to state 6 */
1583 scb->bufptr += (u32) xcnt;
1585 return 4; /* Go to state 4 */
1589 * initio_state_6 - SCSI state machine
1590 * @host: InitIO host we are controlling
1592 * State after Data phase
1595 static int initio_state_6(struct initio_host * host)
1597 struct scsi_ctrl_blk *scb = host->active;
1603 switch (host->phase) {
1604 case STATUS_IN: /* Status phase */
1605 if ((initio_status_msg(host)) == -1)
1609 case MSG_IN: /* Message in phase */
1610 scb->next_state = 6;
1611 if ((initio_msgin(host)) == -1)
1615 case MSG_OUT: /* Message out phase */
1616 outb(NOP, host->addr + TUL_SFifo); /* msg nop */
1617 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1618 if (wait_tulip(host) == -1)
1622 case DATA_IN: /* Data in phase */
1623 return initio_xpad_in(host);
1625 case DATA_OUT: /* Data out phase */
1626 return initio_xpad_out(host);
1629 return initio_bad_seq(host);
1635 * initio_state_7 - SCSI state machine
1636 * @host: InitIO host we are controlling
1640 static int initio_state_7(struct initio_host * host)
1647 /* flush SCSI FIFO */
1648 cnt = inb(host->addr + TUL_SFifoCnt) & 0x1F;
1650 for (i = 0; i < cnt; i++)
1651 inb(host->addr + TUL_SFifo);
1653 switch (host->phase) {
1654 case DATA_IN: /* Data in phase */
1655 case DATA_OUT: /* Data out phase */
1656 return initio_bad_seq(host);
1658 return 6; /* Go to state 6 */
1663 * initio_xfer_data_in - Commence data input
1664 * @host: InitIO host in use
1666 * Commence a block of data transfer. The transfer itself will
1667 * be managed by the controller and we will get a completion (or
1668 * failure) interrupt.
1670 static int initio_xfer_data_in(struct initio_host * host)
1672 struct scsi_ctrl_blk *scb = host->active;
1674 if ((scb->flags & SCF_DIR) == SCF_DOUT)
1675 return 6; /* wrong direction */
1677 outl(scb->buflen, host->addr + TUL_SCnt0);
1678 outb(TSC_XF_DMA_IN, host->addr + TUL_SCmd); /* 7/25/95 */
1680 if (scb->flags & SCF_SG) { /* S/G xfer */
1681 outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1682 outl(scb->bufptr, host->addr + TUL_XAddH);
1683 outb(TAX_SG_IN, host->addr + TUL_XCmd);
1685 outl(scb->buflen, host->addr + TUL_XCntH);
1686 outl(scb->bufptr, host->addr + TUL_XAddH);
1687 outb(TAX_X_IN, host->addr + TUL_XCmd);
1689 scb->next_state = 0x5;
1690 return 0; /* return to OS, wait xfer done , let jas_isr come in */
1694 * initio_xfer_data_out - Commence data output
1695 * @host: InitIO host in use
1697 * Commence a block of data transfer. The transfer itself will
1698 * be managed by the controller and we will get a completion (or
1699 * failure) interrupt.
1702 static int initio_xfer_data_out(struct initio_host * host)
1704 struct scsi_ctrl_blk *scb = host->active;
1706 if ((scb->flags & SCF_DIR) == SCF_DIN)
1707 return 6; /* wrong direction */
1709 outl(scb->buflen, host->addr + TUL_SCnt0);
1710 outb(TSC_XF_DMA_OUT, host->addr + TUL_SCmd);
1712 if (scb->flags & SCF_SG) { /* S/G xfer */
1713 outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1714 outl(scb->bufptr, host->addr + TUL_XAddH);
1715 outb(TAX_SG_OUT, host->addr + TUL_XCmd);
1717 outl(scb->buflen, host->addr + TUL_XCntH);
1718 outl(scb->bufptr, host->addr + TUL_XAddH);
1719 outb(TAX_X_OUT, host->addr + TUL_XCmd);
1722 scb->next_state = 0x5;
1723 return 0; /* return to OS, wait xfer done , let jas_isr come in */
1726 int initio_xpad_in(struct initio_host * host)
1728 struct scsi_ctrl_blk *scb = host->active;
1729 struct target_control *active_tc = host->active_tc;
1731 if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1732 scb->hastat = HOST_DO_DU; /* over run */
1734 if (active_tc->js_period & TSC_WIDE_SCSI)
1735 outl(2, host->addr + TUL_SCnt0);
1737 outl(1, host->addr + TUL_SCnt0);
1739 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1740 if (wait_tulip(host) == -1)
1742 if (host->phase != DATA_IN) {
1743 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1746 inb(host->addr + TUL_SFifo);
1750 int initio_xpad_out(struct initio_host * host)
1752 struct scsi_ctrl_blk *scb = host->active;
1753 struct target_control *active_tc = host->active_tc;
1755 if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1756 scb->hastat = HOST_DO_DU; /* over run */
1758 if (active_tc->js_period & TSC_WIDE_SCSI)
1759 outl(2, host->addr + TUL_SCnt0);
1761 outl(1, host->addr + TUL_SCnt0);
1763 outb(0, host->addr + TUL_SFifo);
1764 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1765 if ((wait_tulip(host)) == -1)
1767 if (host->phase != DATA_OUT) { /* Disable wide CPU to allow read 16 bits */
1768 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1769 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1775 int initio_status_msg(struct initio_host * host)
1776 { /* status & MSG_IN */
1777 struct scsi_ctrl_blk *scb = host->active;
1780 outb(TSC_CMD_COMP, host->addr + TUL_SCmd);
1781 if (wait_tulip(host) == -1)
1785 scb->tastat = inb(host->addr + TUL_SFifo);
1787 if (host->phase == MSG_OUT) {
1788 if (host->jsstatus0 & TSS_PAR_ERROR)
1789 outb(MSG_PARITY_ERROR, host->addr + TUL_SFifo);
1791 outb(NOP, host->addr + TUL_SFifo);
1792 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1793 return wait_tulip(host);
1795 if (host->phase == MSG_IN) {
1796 msg = inb(host->addr + TUL_SFifo);
1797 if (host->jsstatus0 & TSS_PAR_ERROR) { /* Parity error */
1798 if ((initio_msgin_accept(host)) == -1)
1800 if (host->phase != MSG_OUT)
1801 return initio_bad_seq(host);
1802 outb(MSG_PARITY_ERROR, host->addr + TUL_SFifo);
1803 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1804 return wait_tulip(host);
1806 if (msg == 0) { /* Command complete */
1808 if ((scb->tastat & 0x18) == 0x10) /* No link support */
1809 return initio_bad_seq(host);
1810 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1811 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
1812 return initio_wait_done_disc(host);
1815 if (msg == LINKED_CMD_COMPLETE ||
1816 msg == LINKED_FLG_CMD_COMPLETE) {
1817 if ((scb->tastat & 0x18) == 0x10)
1818 return initio_msgin_accept(host);
1821 return initio_bad_seq(host);
1826 int int_initio_busfree(struct initio_host * host)
1828 struct scsi_ctrl_blk *scb = host->active;
1831 if (scb->status & SCB_SELECT) { /* selection timeout */
1832 initio_unlink_pend_scb(host, scb);
1833 scb->hastat = HOST_SEL_TOUT;
1834 initio_append_done_scb(host, scb);
1835 } else { /* Unexpected bus free */
1836 initio_unlink_busy_scb(host, scb);
1837 scb->hastat = HOST_BUS_FREE;
1838 initio_append_done_scb(host, scb);
1840 host->active = NULL;
1841 host->active_tc = NULL;
1843 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
1844 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
1845 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
1851 * int_initio_scsi_rst - SCSI reset occurred
1852 * @host: Host seeing the reset
1854 * A SCSI bus reset has occurred. Clean up any pending transfer
1855 * the hardware is doing by DMA and then abort all active and
1856 * disconnected commands. The mid layer should sort the rest out
1860 static int int_initio_scsi_rst(struct initio_host * host)
1862 struct scsi_ctrl_blk *scb;
1865 /* if DMA xfer is pending, abort DMA xfer */
1866 if (inb(host->addr + TUL_XStatus) & 0x01) {
1867 outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
1868 /* wait Abort DMA xfer done */
1869 while ((inb(host->addr + TUL_Int) & 0x04) == 0)
1871 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1873 /* Abort all active & disconnected scb */
1874 while ((scb = initio_pop_busy_scb(host)) != NULL) {
1875 scb->hastat = HOST_BAD_PHAS;
1876 initio_append_done_scb(host, scb);
1878 host->active = NULL;
1879 host->active_tc = NULL;
1881 /* clr sync nego. done flag */
1882 for (i = 0; i < host->max_tar; i++)
1883 host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1888 * int_initio_resel - Reselection occurred
1889 * @host: InitIO host adapter
1891 * A SCSI reselection event has been signalled and the interrupt
1892 * is now being processed. Work out which command block needs attention
1893 * and continue processing that command.
1896 int int_initio_resel(struct initio_host * host)
1898 struct scsi_ctrl_blk *scb;
1899 struct target_control *active_tc;
1903 if ((scb = host->active) != NULL) {
1904 /* FIXME: Why check and not just clear ? */
1905 if (scb->status & SCB_SELECT) /* if waiting for selection complete */
1906 scb->status &= ~SCB_SELECT;
1907 host->active = NULL;
1909 /* --------- get target id---------------------- */
1910 tar = inb(host->addr + TUL_SBusId);
1911 /* ------ get LUN from Identify message----------- */
1912 lun = inb(host->addr + TUL_SIdent) & 0x0F;
1913 /* 07/22/98 from 0x1F -> 0x0F */
1914 active_tc = &host->targets[tar];
1915 host->active_tc = active_tc;
1916 outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1917 outb(active_tc->js_period, host->addr + TUL_SPeriod);
1919 /* ------------- tag queueing ? ------------------- */
1920 if (active_tc->drv_flags & TCF_DRV_EN_TAG) {
1921 if ((initio_msgin_accept(host)) == -1)
1923 if (host->phase != MSG_IN)
1925 outl(1, host->addr + TUL_SCnt0);
1926 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1927 if (wait_tulip(host) == -1)
1929 msg = inb(host->addr + TUL_SFifo); /* Read Tag Message */
1931 if (msg < SIMPLE_QUEUE_TAG || msg > ORDERED_QUEUE_TAG)
1935 if (initio_msgin_accept(host) == -1)
1938 if (host->phase != MSG_IN)
1941 outl(1, host->addr + TUL_SCnt0);
1942 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1943 if (wait_tulip(host) == -1)
1945 tag = inb(host->addr + TUL_SFifo); /* Read Tag ID */
1946 scb = host->scb + tag;
1947 if (scb->target != tar || scb->lun != lun) {
1948 return initio_msgout_abort_tag(host);
1950 if (scb->status != SCB_BUSY) { /* 03/24/95 */
1951 return initio_msgout_abort_tag(host);
1954 if ((initio_msgin_accept(host)) == -1)
1956 } else { /* No tag */
1958 if ((scb = initio_find_busy_scb(host, tar | (lun << 8))) == NULL) {
1959 return initio_msgout_abort_targ(host);
1962 if (!(active_tc->drv_flags & TCF_DRV_EN_TAG)) {
1963 if ((initio_msgin_accept(host)) == -1)
1971 * int_initio_bad_seq - out of phase
1972 * @host: InitIO host flagging event
1974 * We have ended up out of phase somehow. Reset the host controller
1975 * and throw all our toys out of the pram. Let the midlayer clean up
1978 static int int_initio_bad_seq(struct initio_host * host)
1979 { /* target wrong phase */
1980 struct scsi_ctrl_blk *scb;
1983 initio_reset_scsi(host, 10);
1985 while ((scb = initio_pop_busy_scb(host)) != NULL) {
1986 scb->hastat = HOST_BAD_PHAS;
1987 initio_append_done_scb(host, scb);
1989 for (i = 0; i < host->max_tar; i++)
1990 host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1996 * initio_msgout_abort_targ - abort a tag
1997 * @host: InitIO host
1999 * Abort when the target/lun does not match or when our SCB is not
2000 * busy. Used by untagged commands.
2003 static int initio_msgout_abort_targ(struct initio_host * host)
2006 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2007 if (initio_msgin_accept(host) == -1)
2009 if (host->phase != MSG_OUT)
2010 return initio_bad_seq(host);
2012 outb(ABORT_TASK_SET, host->addr + TUL_SFifo);
2013 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2015 return initio_wait_disc(host);
2019 * initio_msgout_abort_tag - abort a tag
2020 * @host: InitIO host
2022 * Abort when the target/lun does not match or when our SCB is not
2023 * busy. Used for tagged commands.
2026 static int initio_msgout_abort_tag(struct initio_host * host)
2029 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2030 if (initio_msgin_accept(host) == -1)
2032 if (host->phase != MSG_OUT)
2033 return initio_bad_seq(host);
2035 outb(ABORT_TASK, host->addr + TUL_SFifo);
2036 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2038 return initio_wait_disc(host);
2043 * initio_msgin - Message in
2044 * @host: InitIO Host
2046 * Process incoming message
2048 static int initio_msgin(struct initio_host * host)
2050 struct target_control *active_tc;
2053 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2055 outl(1, host->addr + TUL_SCnt0);
2056 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2057 if (wait_tulip(host) == -1)
2060 switch (inb(host->addr + TUL_SFifo)) {
2061 case DISCONNECT: /* Disconnect msg */
2062 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2063 return initio_wait_disc(host);
2065 case RESTORE_POINTERS:
2067 initio_msgin_accept(host);
2069 case MESSAGE_REJECT: /* Clear ATN first */
2070 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)),
2071 host->addr + TUL_SSignal);
2072 active_tc = host->active_tc;
2073 if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) /* do sync nego */
2074 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN),
2075 host->addr + TUL_SSignal);
2076 initio_msgin_accept(host);
2078 case EXTENDED_MESSAGE: /* extended msg */
2079 initio_msgin_extend(host);
2081 case IGNORE_WIDE_RESIDUE:
2082 initio_msgin_accept(host);
2084 case COMMAND_COMPLETE:
2085 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2086 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2087 return initio_wait_done_disc(host);
2089 initio_msgout_reject(host);
2092 if (host->phase != MSG_IN)
2095 /* statement won't reach here */
2098 static int initio_msgout_reject(struct initio_host * host)
2100 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2102 if (initio_msgin_accept(host) == -1)
2105 if (host->phase == MSG_OUT) {
2106 outb(MESSAGE_REJECT, host->addr + TUL_SFifo); /* Msg reject */
2107 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2108 return wait_tulip(host);
2113 static int initio_msgout_ide(struct initio_host * host)
2115 outb(INITIATOR_ERROR, host->addr + TUL_SFifo); /* Initiator Detected Error */
2116 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2117 return wait_tulip(host);
2120 static int initio_msgin_extend(struct initio_host * host)
2124 if (initio_msgin_accept(host) != MSG_IN)
2127 /* Get extended msg length */
2128 outl(1, host->addr + TUL_SCnt0);
2129 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2130 if (wait_tulip(host) == -1)
2133 len = inb(host->addr + TUL_SFifo);
2135 for (idx = 1; len != 0; len--) {
2137 if ((initio_msgin_accept(host)) != MSG_IN)
2139 outl(1, host->addr + TUL_SCnt0);
2140 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2141 if (wait_tulip(host) == -1)
2143 host->msg[idx++] = inb(host->addr + TUL_SFifo);
2145 if (host->msg[1] == 1) { /* if it's synchronous data transfer request */
2147 if (host->msg[0] != 3) /* if length is not right */
2148 return initio_msgout_reject(host);
2149 if (host->active_tc->flags & TCF_NO_SYNC_NEGO) { /* Set OFFSET=0 to do async, nego back */
2152 if (initio_msgin_sync(host) == 0 &&
2153 (host->active_tc->flags & TCF_SYNC_DONE)) {
2154 initio_sync_done(host);
2155 return initio_msgin_accept(host);
2159 r = inb(host->addr + TUL_SSignal);
2160 outb((r & (TSC_SET_ACK | 7)) | TSC_SET_ATN,
2161 host->addr + TUL_SSignal);
2162 if (initio_msgin_accept(host) != MSG_OUT)
2165 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2167 initio_sync_done(host);
2169 outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
2170 outb(3, host->addr + TUL_SFifo);
2171 outb(EXTENDED_SDTR, host->addr + TUL_SFifo);
2172 outb(host->msg[2], host->addr + TUL_SFifo);
2173 outb(host->msg[3], host->addr + TUL_SFifo);
2174 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2175 return wait_tulip(host);
2177 if (host->msg[0] != 2 || host->msg[1] != 3)
2178 return initio_msgout_reject(host);
2179 /* if it's WIDE DATA XFER REQ */
2180 if (host->active_tc->flags & TCF_NO_WDTR) {
2183 if (host->msg[2] > 2) /* > 32 bits */
2184 return initio_msgout_reject(host);
2185 if (host->msg[2] == 2) { /* == 32 */
2188 if ((host->active_tc->flags & TCF_NO_WDTR) == 0) {
2190 if ((host->active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)
2191 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2192 return initio_msgin_accept(host);
2196 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2198 if (initio_msgin_accept(host) != MSG_OUT)
2201 outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
2202 outb(2, host->addr + TUL_SFifo);
2203 outb(EXTENDED_WDTR, host->addr + TUL_SFifo);
2204 outb(host->msg[2], host->addr + TUL_SFifo);
2205 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2206 return wait_tulip(host);
2209 static int initio_msgin_sync(struct initio_host * host)
2211 char default_period;
2213 default_period = initio_rate_tbl[host->active_tc->flags & TCF_SCSI_RATE];
2214 if (host->msg[3] > MAX_OFFSET) {
2215 host->msg[3] = MAX_OFFSET;
2216 if (host->msg[2] < default_period) {
2217 host->msg[2] = default_period;
2220 if (host->msg[2] >= 59) /* Change to async */
2224 /* offset requests asynchronous transfers ? */
2225 if (host->msg[3] == 0) {
2228 if (host->msg[2] < default_period) {
2229 host->msg[2] = default_period;
2232 if (host->msg[2] >= 59) {
2239 static int wdtr_done(struct initio_host * host)
2241 host->active_tc->flags &= ~TCF_SYNC_DONE;
2242 host->active_tc->flags |= TCF_WDTR_DONE;
2244 host->active_tc->js_period = 0;
2245 if (host->msg[2]) /* if 16 bit */
2246 host->active_tc->js_period |= TSC_WIDE_SCSI;
2247 host->active_tc->sconfig0 &= ~TSC_ALT_PERIOD;
2248 outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2249 outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
2254 static int initio_sync_done(struct initio_host * host)
2258 host->active_tc->flags |= TCF_SYNC_DONE;
2261 host->active_tc->js_period |= host->msg[3];
2262 for (i = 0; i < 8; i++) {
2263 if (initio_rate_tbl[i] >= host->msg[2]) /* pick the big one */
2266 host->active_tc->js_period |= (i << 4);
2267 host->active_tc->sconfig0 |= TSC_ALT_PERIOD;
2269 outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2270 outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
2276 static int initio_post_scsi_rst(struct initio_host * host)
2278 struct scsi_ctrl_blk *scb;
2279 struct target_control *active_tc;
2282 host->active = NULL;
2283 host->active_tc = NULL;
2286 while ((scb = initio_pop_busy_scb(host)) != NULL) {
2287 scb->hastat = HOST_BAD_PHAS;
2288 initio_append_done_scb(host, scb);
2290 /* clear sync done flag */
2291 active_tc = &host->targets[0];
2292 for (i = 0; i < host->max_tar; active_tc++, i++) {
2293 active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
2294 /* Initialize the sync. xfer register values to an asyn xfer */
2295 active_tc->js_period = 0;
2296 active_tc->sconfig0 = host->sconf1;
2297 host->act_tags[0] = 0; /* 07/22/98 */
2298 host->targets[i].flags &= ~TCF_BUSY; /* 07/22/98 */
2304 static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb)
2306 scb->status |= SCB_SELECT;
2307 scb->next_state = 0x1;
2309 host->active_tc = &host->targets[scb->target];
2310 outb(TSC_SELATNSTOP, host->addr + TUL_SCmd);
2314 static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb)
2318 scb->status |= SCB_SELECT;
2319 scb->next_state = 0x2;
2321 outb(scb->ident, host->addr + TUL_SFifo);
2322 for (i = 0; i < (int) scb->cdblen; i++)
2323 outb(scb->cdb[i], host->addr + TUL_SFifo);
2324 host->active_tc = &host->targets[scb->target];
2326 outb(TSC_SEL_ATN, host->addr + TUL_SCmd);
2329 static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb)
2333 scb->status |= SCB_SELECT;
2334 scb->next_state = 0x2;
2336 outb(scb->ident, host->addr + TUL_SFifo);
2337 outb(scb->tagmsg, host->addr + TUL_SFifo);
2338 outb(scb->tagid, host->addr + TUL_SFifo);
2339 for (i = 0; i < scb->cdblen; i++)
2340 outb(scb->cdb[i], host->addr + TUL_SFifo);
2341 host->active_tc = &host->targets[scb->target];
2343 outb(TSC_SEL_ATN3, host->addr + TUL_SCmd);
2347 * initio_bus_device_reset - SCSI Bus Device Reset
2348 * @host: InitIO host to reset
2350 * Perform a device reset and abort all pending SCBs for the
2353 int initio_bus_device_reset(struct initio_host * host)
2355 struct scsi_ctrl_blk *scb = host->active;
2356 struct target_control *active_tc = host->active_tc;
2357 struct scsi_ctrl_blk *tmp, *prev;
2360 if (host->phase != MSG_OUT)
2361 return int_initio_bad_seq(host); /* Unexpected phase */
2363 initio_unlink_pend_scb(host, scb);
2364 initio_release_scb(host, scb);
2367 tar = scb->target; /* target */
2368 active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE | TCF_BUSY);
2369 /* clr sync. nego & WDTR flags 07/22/98 */
2371 /* abort all SCB with same target */
2372 prev = tmp = host->first_busy; /* Check Busy queue */
2373 while (tmp != NULL) {
2374 if (tmp->target == tar) {
2376 if (tmp == host->first_busy) {
2377 if ((host->first_busy = tmp->next) == NULL)
2378 host->last_busy = NULL;
2380 prev->next = tmp->next;
2381 if (tmp == host->last_busy)
2382 host->last_busy = prev;
2384 tmp->hastat = HOST_ABORTED;
2385 initio_append_done_scb(host, tmp);
2387 /* Previous haven't change */
2393 outb(TARGET_RESET, host->addr + TUL_SFifo);
2394 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2395 return initio_wait_disc(host);
2399 static int initio_msgin_accept(struct initio_host * host)
2401 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2402 return wait_tulip(host);
2405 static int wait_tulip(struct initio_host * host)
2408 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2412 host->jsint = inb(host->addr + TUL_SInt);
2413 host->phase = host->jsstatus0 & TSS_PH_MASK;
2414 host->jsstatus1 = inb(host->addr + TUL_SStatus1);
2416 if (host->jsint & TSS_RESEL_INT) /* if SCSI bus reset detected */
2417 return int_initio_resel(host);
2418 if (host->jsint & TSS_SEL_TIMEOUT) /* if selected/reselected timeout interrupt */
2419 return int_initio_busfree(host);
2420 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2421 return int_initio_scsi_rst(host);
2423 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2424 if (host->flags & HCF_EXPECT_DONE_DISC) {
2425 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2426 initio_unlink_busy_scb(host, host->active);
2427 host->active->hastat = 0;
2428 initio_append_done_scb(host, host->active);
2429 host->active = NULL;
2430 host->active_tc = NULL;
2431 host->flags &= ~HCF_EXPECT_DONE_DISC;
2432 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2433 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2436 if (host->flags & HCF_EXPECT_DISC) {
2437 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2438 host->active = NULL;
2439 host->active_tc = NULL;
2440 host->flags &= ~HCF_EXPECT_DISC;
2441 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2442 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2445 return int_initio_busfree(host);
2447 /* The old code really does the below. Can probably be removed */
2448 if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV))
2453 static int initio_wait_disc(struct initio_host * host)
2455 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING))
2458 host->jsint = inb(host->addr + TUL_SInt);
2460 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2461 return int_initio_scsi_rst(host);
2462 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2463 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2464 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2465 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2466 host->active = NULL;
2469 return initio_bad_seq(host);
2472 static int initio_wait_done_disc(struct initio_host * host)
2474 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2478 host->jsint = inb(host->addr + TUL_SInt);
2480 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2481 return int_initio_scsi_rst(host);
2482 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2483 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2484 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2485 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2486 initio_unlink_busy_scb(host, host->active);
2488 initio_append_done_scb(host, host->active);
2489 host->active = NULL;
2492 return initio_bad_seq(host);
2496 * i91u_intr - IRQ handler
2497 * @irqno: IRQ number
2498 * @dev_id: IRQ identifier
2500 * Take the relevant locks and then invoke the actual isr processing
2501 * code under the lock.
2504 static irqreturn_t i91u_intr(int irqno, void *dev_id)
2506 struct Scsi_Host *dev = dev_id;
2507 unsigned long flags;
2510 spin_lock_irqsave(dev->host_lock, flags);
2511 r = initio_isr((struct initio_host *)dev->hostdata);
2512 spin_unlock_irqrestore(dev->host_lock, flags);
2521 * initio_build_scb - Build the mappings and SCB
2522 * @host: InitIO host taking the command
2523 * @cblk: Firmware command block
2524 * @cmnd: SCSI midlayer command block
2526 * Translate the abstract SCSI command into a firmware command block
2527 * suitable for feeding to the InitIO host controller. This also requires
2528 * we build the scatter gather lists and ensure they are mapped properly.
2531 static void initio_build_scb(struct initio_host * host, struct scsi_ctrl_blk * cblk, struct scsi_cmnd * cmnd)
2532 { /* Create corresponding SCB */
2533 struct scatterlist *sglist;
2534 struct sg_entry *sg; /* Pointer to SG list */
2537 dma_addr_t dma_addr;
2539 /* Fill in the command headers */
2540 cblk->post = i91uSCBPost; /* i91u's callback routine */
2542 cblk->opcode = ExecSCSI;
2543 cblk->flags = SCF_POST; /* After SCSI done, call post routine */
2544 cblk->target = cmnd->device->id;
2545 cblk->lun = cmnd->device->lun;
2546 cblk->ident = cmnd->device->lun | DISC_ALLOW;
2548 cblk->flags |= SCF_SENSE; /* Turn on auto request sense */
2550 /* Map the sense buffer into bus memory */
2551 dma_addr = dma_map_single(&host->pci_dev->dev, cmnd->sense_buffer,
2552 SENSE_SIZE, DMA_FROM_DEVICE);
2553 cblk->senseptr = (u32)dma_addr;
2554 cblk->senselen = SENSE_SIZE;
2555 initio_priv(cmnd)->sense_dma_addr = dma_addr;
2556 cblk->cdblen = cmnd->cmd_len;
2558 /* Clear the returned status */
2561 /* Command the command */
2562 memcpy(cblk->cdb, cmnd->cmnd, cmnd->cmd_len);
2565 if (cmnd->device->tagged_supported) { /* Tag Support */
2566 cblk->tagmsg = SIMPLE_QUEUE_TAG; /* Do simple tag only */
2568 cblk->tagmsg = 0; /* No tag support */
2571 /* todo handle map_sg error */
2572 nseg = scsi_dma_map(cmnd);
2575 dma_addr = dma_map_single(&host->pci_dev->dev, &cblk->sglist[0],
2576 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
2578 cblk->bufptr = (u32)dma_addr;
2579 initio_priv(cmnd)->sglist_dma_addr = dma_addr;
2583 cblk->flags |= SCF_SG; /* Turn on SG list flag */
2585 sg = &cblk->sglist[0];
2586 scsi_for_each_sg(cmnd, sglist, cblk->sglen, i) {
2587 sg->data = cpu_to_le32((u32)sg_dma_address(sglist));
2588 sg->len = cpu_to_le32((u32)sg_dma_len(sglist));
2589 total_len += sg_dma_len(sglist);
2593 cblk->buflen = (scsi_bufflen(cmnd) > total_len) ?
2594 total_len : scsi_bufflen(cmnd);
2595 } else { /* No data transfer required */
2602 * i91u_queuecommand_lck - Queue a new command if possible
2603 * @cmd: SCSI command block from the mid layer
2605 * Attempts to queue a new command with the host adapter. Will return
2606 * zero if successful or indicate a host busy condition if not (which
2607 * will cause the mid layer to call us again later with the command)
2609 static int i91u_queuecommand_lck(struct scsi_cmnd *cmd)
2611 struct initio_host *host = (struct initio_host *) cmd->device->host->hostdata;
2612 struct scsi_ctrl_blk *cmnd;
2614 cmnd = initio_alloc_scb(host);
2616 return SCSI_MLQUEUE_HOST_BUSY;
2618 initio_build_scb(host, cmnd, cmd);
2619 initio_exec_scb(host, cmnd);
2623 static DEF_SCSI_QCMD(i91u_queuecommand)
2626 * i91u_bus_reset - reset the SCSI bus
2627 * @cmnd: Command block we want to trigger the reset for
2629 * Initiate a SCSI bus reset sequence
2632 static int i91u_bus_reset(struct scsi_cmnd * cmnd)
2634 struct initio_host *host;
2636 host = (struct initio_host *) cmnd->device->host->hostdata;
2638 spin_lock_irq(cmnd->device->host->host_lock);
2639 initio_reset_scsi(host, 0);
2640 spin_unlock_irq(cmnd->device->host->host_lock);
2646 * i91u_biosparam - return the "logical geometry
2647 * @sdev: SCSI device
2648 * @dev: Matching block device
2649 * @capacity: Sector size of drive
2650 * @info_array: Return space for BIOS geometry
2652 * Map the device geometry in a manner compatible with the host
2653 * controller BIOS behaviour.
2655 * FIXME: limited to 2^32 sector devices.
2658 static int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev,
2659 sector_t capacity, int *info_array)
2661 struct initio_host *host; /* Point to Host adapter control block */
2662 struct target_control *tc;
2664 host = (struct initio_host *) sdev->host->hostdata;
2665 tc = &host->targets[sdev->id];
2668 info_array[0] = tc->heads;
2669 info_array[1] = tc->sectors;
2670 info_array[2] = (unsigned long)capacity / tc->heads / tc->sectors;
2672 if (tc->drv_flags & TCF_DRV_255_63) {
2673 info_array[0] = 255;
2675 info_array[2] = (unsigned long)capacity / 255 / 63;
2679 info_array[2] = (unsigned long)capacity >> 11;
2683 #if defined(DEBUG_BIOSPARAM)
2684 if (i91u_debug & debug_biosparam) {
2685 printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
2686 info_array[0], info_array[1], info_array[2]);
2687 printk("WARNING: check, if the bios geometry is correct.\n");
2695 * i91u_unmap_scb - Unmap a command
2696 * @pci_dev: PCI device the command is for
2697 * @cmnd: The command itself
2699 * Unmap any PCI mapping/IOMMU resources allocated when the command
2700 * was mapped originally as part of initio_build_scb
2703 static void i91u_unmap_scb(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd)
2705 /* auto sense buffer */
2706 if (initio_priv(cmnd)->sense_dma_addr) {
2707 dma_unmap_single(&pci_dev->dev,
2708 initio_priv(cmnd)->sense_dma_addr,
2709 SENSE_SIZE, DMA_FROM_DEVICE);
2710 initio_priv(cmnd)->sense_dma_addr = 0;
2713 /* request buffer */
2714 if (scsi_sg_count(cmnd)) {
2715 dma_unmap_single(&pci_dev->dev,
2716 initio_priv(cmnd)->sglist_dma_addr,
2717 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
2720 scsi_dma_unmap(cmnd);
2725 * i91uSCBPost - SCSI callback
2727 * This is callback routine be called when tulip finish one
2731 static void i91uSCBPost(u8 * host_mem, u8 * cblk_mem)
2733 struct scsi_cmnd *cmnd; /* Pointer to SCSI request block */
2734 struct initio_host *host;
2735 struct scsi_ctrl_blk *cblk;
2737 host = (struct initio_host *) host_mem;
2738 cblk = (struct scsi_ctrl_blk *) cblk_mem;
2739 if ((cmnd = cblk->srb) == NULL) {
2740 printk(KERN_ERR "i91uSCBPost: SRB pointer is empty\n");
2742 initio_release_scb(host, cblk); /* Release SCB for current channel */
2747 * Remap the firmware error status into a mid layer one
2749 switch (cblk->hastat) {
2751 case 0xa: /* Linked command complete without error and linked normally */
2752 case 0xb: /* Linked command complete without error interrupt generated */
2756 case 0x11: /* Selection time out-The initiator selection or target
2757 reselection was not complete within the SCSI Time out period */
2758 cblk->hastat = DID_TIME_OUT;
2761 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
2762 phase sequence was requested by the target. The host adapter
2763 will generate a SCSI Reset Condition, notifying the host with
2765 cblk->hastat = DID_RESET;
2768 case 0x1a: /* SCB Aborted. 07/21/98 */
2769 cblk->hastat = DID_ABORT;
2772 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
2773 than was allocated by the Data Length field or the sum of the
2774 Scatter / Gather Data Length fields. */
2775 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
2776 case 0x16: /* Invalid SCB Operation Code. */
2779 printk("ini9100u: %x %x\n", cblk->hastat, cblk->tastat);
2780 cblk->hastat = DID_ERROR; /* Couldn't find any better */
2784 cmnd->result = cblk->tastat | (cblk->hastat << 16);
2785 i91u_unmap_scb(host->pci_dev, cmnd);
2786 scsi_done(cmnd); /* Notify system DONE */
2787 initio_release_scb(host, cblk); /* Release SCB for current channel */
2790 static const struct scsi_host_template initio_template = {
2791 .proc_name = "INI9100U",
2792 .name = "Initio INI-9X00U/UW SCSI device driver",
2793 .queuecommand = i91u_queuecommand,
2794 .eh_bus_reset_handler = i91u_bus_reset,
2795 .bios_param = i91u_biosparam,
2796 .can_queue = MAX_TARGETS * i91u_MAXQUEUE,
2798 .sg_tablesize = SG_ALL,
2799 .cmd_size = sizeof(struct initio_cmd_priv),
2802 static int initio_probe_one(struct pci_dev *pdev,
2803 const struct pci_device_id *id)
2805 struct Scsi_Host *shost;
2806 struct initio_host *host;
2809 struct scsi_ctrl_blk *scb, *tmp, *prev = NULL /* silence gcc */;
2810 int num_scb, i, error;
2812 error = pci_enable_device(pdev);
2816 pci_read_config_dword(pdev, 0x44, (u32 *) & reg);
2817 bios_seg = (u16) (reg & 0xFF);
2818 if (((reg & 0xFF00) >> 8) == 0xFF)
2820 bios_seg = (bios_seg << 8) + ((u16) ((reg & 0xFF00) >> 8));
2822 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
2823 printk(KERN_WARNING "i91u: Could not set 32 bit DMA mask\n");
2825 goto out_disable_device;
2827 shost = scsi_host_alloc(&initio_template, sizeof(struct initio_host));
2829 printk(KERN_WARNING "initio: Could not allocate host structure.\n");
2831 goto out_disable_device;
2833 host = (struct initio_host *)shost->hostdata;
2834 memset(host, 0, sizeof(struct initio_host));
2835 host->addr = pci_resource_start(pdev, 0);
2836 host->bios_addr = bios_seg;
2838 if (!request_region(host->addr, 256, "i91u")) {
2839 printk(KERN_WARNING "initio: I/O port range 0x%x is busy.\n", host->addr);
2844 if (initio_tag_enable) /* 1.01i */
2845 num_scb = MAX_TARGETS * i91u_MAXQUEUE;
2847 num_scb = MAX_TARGETS + 3; /* 1-tape, 1-CD_ROM, 1- extra */
2849 for (; num_scb >= MAX_TARGETS + 3; num_scb--) {
2850 i = num_scb * sizeof(struct scsi_ctrl_blk);
2851 scb = kzalloc(i, GFP_KERNEL);
2857 printk(KERN_WARNING "initio: Cannot allocate SCB array.\n");
2859 goto out_release_region;
2862 host->pci_dev = pdev;
2865 spin_lock_init(&host->semaph_lock);
2866 host->num_scbs = num_scb;
2868 host->next_pending = scb;
2869 host->next_avail = scb;
2870 for (i = 0, tmp = scb; i < num_scb; i++, tmp++) {
2877 host->scb_end = tmp;
2878 host->first_avail = scb;
2879 host->last_avail = prev;
2880 spin_lock_init(&host->avail_lock);
2882 initio_init(host, phys_to_virt(((u32)bios_seg << 4)));
2884 host->jsstatus0 = 0;
2886 shost->io_port = host->addr;
2887 shost->n_io_port = 0xff;
2888 shost->can_queue = num_scb; /* 03/05/98 */
2889 shost->unique_id = host->addr;
2890 shost->max_id = host->max_tar;
2891 shost->max_lun = 32; /* 10/21/97 */
2892 shost->irq = pdev->irq;
2893 shost->this_id = host->scsi_id; /* Assign HCS index */
2894 shost->base = host->addr;
2895 shost->sg_tablesize = TOTAL_SG_ENTRY;
2897 error = request_irq(pdev->irq, i91u_intr, IRQF_SHARED, "i91u", shost);
2899 printk(KERN_WARNING "initio: Unable to request IRQ %d\n", pdev->irq);
2903 pci_set_drvdata(pdev, shost);
2905 error = scsi_add_host(shost, &pdev->dev);
2908 scsi_scan_host(shost);
2911 free_irq(pdev->irq, shost);
2915 release_region(host->addr, 256);
2917 scsi_host_put(shost);
2919 pci_disable_device(pdev);
2924 * initio_remove_one - control shutdown
2925 * @pdev: PCI device being released
2927 * Release the resources assigned to this adapter after it has
2928 * finished being used.
2931 static void initio_remove_one(struct pci_dev *pdev)
2933 struct Scsi_Host *host = pci_get_drvdata(pdev);
2934 struct initio_host *s = (struct initio_host *)host->hostdata;
2935 scsi_remove_host(host);
2936 free_irq(pdev->irq, host);
2937 release_region(s->addr, 256);
2938 scsi_host_put(host);
2939 pci_disable_device(pdev);
2942 MODULE_LICENSE("GPL");
2944 static struct pci_device_id initio_pci_tbl[] = {
2945 {PCI_VENDOR_ID_INIT, 0x9500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2946 {PCI_VENDOR_ID_INIT, 0x9400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2947 {PCI_VENDOR_ID_INIT, 0x9401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2948 {PCI_VENDOR_ID_INIT, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2949 {PCI_VENDOR_ID_DOMEX, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2952 MODULE_DEVICE_TABLE(pci, initio_pci_tbl);
2954 static struct pci_driver initio_pci_driver = {
2956 .id_table = initio_pci_tbl,
2957 .probe = initio_probe_one,
2958 .remove = initio_remove_one,
2960 module_pci_driver(initio_pci_driver);
2962 MODULE_DESCRIPTION("Initio INI-9X00U/UW SCSI device driver");
2963 MODULE_AUTHOR("Initio Corporation");
2964 MODULE_LICENSE("GPL");