1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /************************************************************************** 31da177e4SLinus Torvalds * Initio 9100 device driver for Linux. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (c) 1994-1998 Initio Corporation 61da177e4SLinus Torvalds * Copyright (c) 1998 Bas Vermeulen <bvermeul@blackstar.xs4all.nl> 772d39feaSAlan Cox * Copyright (c) 2004 Christoph Hellwig <hch@lst.de> 8fa195afeSAlan Cox * Copyright (c) 2007 Red Hat 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds ************************************************************************* 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * DESCRIPTION: 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * This is the Linux low-level SCSI driver for Initio INI-9X00U/UW SCSI host 151da177e4SLinus Torvalds * adapters 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * 08/06/97 hc - v1.01h 181da177e4SLinus Torvalds * - Support inic-940 and inic-935 191da177e4SLinus Torvalds * 09/26/97 hc - v1.01i 201da177e4SLinus Torvalds * - Make correction from J.W. Schultz suggestion 211da177e4SLinus Torvalds * 10/13/97 hc - Support reset function 221da177e4SLinus Torvalds * 10/21/97 hc - v1.01j 231da177e4SLinus Torvalds * - Support 32 LUN (SCSI 3) 241da177e4SLinus Torvalds * 01/14/98 hc - v1.01k 251da177e4SLinus Torvalds * - Fix memory allocation problem 261da177e4SLinus Torvalds * 03/04/98 hc - v1.01l 271da177e4SLinus Torvalds * - Fix tape rewind which will hang the system problem 2872d39feaSAlan Cox * - Set can_queue to initio_num_scb 291da177e4SLinus Torvalds * 06/25/98 hc - v1.01m 301da177e4SLinus Torvalds * - Get it work for kernel version >= 2.1.75 3172d39feaSAlan Cox * - Dynamic assign SCSI bus reset holding time in initio_init() 321da177e4SLinus Torvalds * 07/02/98 hc - v1.01n 331da177e4SLinus Torvalds * - Support 0002134A 341da177e4SLinus Torvalds * 08/07/98 hc - v1.01o 3572d39feaSAlan Cox * - Change the initio_abort_srb routine to use scsi_done. <01> 361da177e4SLinus Torvalds * 09/07/98 hl - v1.02 371da177e4SLinus Torvalds * - Change the INI9100U define and proc_dir_entry to 381da177e4SLinus Torvalds * reflect the newer Kernel 2.1.118, but the v1.o1o 391da177e4SLinus Torvalds * should work with Kernel 2.1.118. 401da177e4SLinus Torvalds * 09/20/98 wh - v1.02a 411da177e4SLinus Torvalds * - Support Abort command. 421da177e4SLinus Torvalds * - Handle reset routine. 431da177e4SLinus Torvalds * 09/21/98 hl - v1.03 441da177e4SLinus Torvalds * - remove comments. 451da177e4SLinus Torvalds * 12/09/98 bv - v1.03a 461da177e4SLinus Torvalds * - Removed unused code 471da177e4SLinus Torvalds * 12/13/98 bv - v1.03b 481da177e4SLinus Torvalds * - Remove cli() locking for kernels >= 2.1.95. This uses 491da177e4SLinus Torvalds * spinlocks to serialize access to the pSRB_head and 501da177e4SLinus Torvalds * pSRB_tail members of the HCS structure. 511da177e4SLinus Torvalds * 09/01/99 bv - v1.03d 521da177e4SLinus Torvalds * - Fixed a deadlock problem in SMP. 531da177e4SLinus Torvalds * 21/01/99 bv - v1.03e 541da177e4SLinus Torvalds * - Add support for the Domex 3192U PCI SCSI 551da177e4SLinus Torvalds * This is a slightly modified patch by 561da177e4SLinus Torvalds * Brian Macy <bmacy@sunshinecomputing.com> 571da177e4SLinus Torvalds * 22/02/99 bv - v1.03f 581da177e4SLinus Torvalds * - Didn't detect the INIC-950 in 2.0.x correctly. 591da177e4SLinus Torvalds * Now fixed. 601da177e4SLinus Torvalds * 05/07/99 bv - v1.03g 611da177e4SLinus Torvalds * - Changed the assumption that HZ = 100 621da177e4SLinus Torvalds * 10/17/03 mc - v1.04 631da177e4SLinus Torvalds * - added new DMA API support 641da177e4SLinus Torvalds * 06/01/04 jmd - v1.04a 651da177e4SLinus Torvalds * - Re-add reset_bus support 661da177e4SLinus Torvalds **************************************************************************/ 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #include <linux/module.h> 691da177e4SLinus Torvalds #include <linux/errno.h> 701da177e4SLinus Torvalds #include <linux/delay.h> 711da177e4SLinus Torvalds #include <linux/pci.h> 721da177e4SLinus Torvalds #include <linux/init.h> 731da177e4SLinus Torvalds #include <linux/blkdev.h> 741da177e4SLinus Torvalds #include <linux/spinlock.h> 751da177e4SLinus Torvalds #include <linux/stat.h> 761da177e4SLinus Torvalds #include <linux/kernel.h> 771da177e4SLinus Torvalds #include <linux/proc_fs.h> 781da177e4SLinus Torvalds #include <linux/string.h> 791da177e4SLinus Torvalds #include <linux/interrupt.h> 801da177e4SLinus Torvalds #include <linux/ioport.h> 811da177e4SLinus Torvalds #include <linux/slab.h> 821da177e4SLinus Torvalds #include <linux/jiffies.h> 83910638aeSMatthias Gehre #include <linux/dma-mapping.h> 841da177e4SLinus Torvalds #include <asm/io.h> 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds #include <scsi/scsi.h> 871da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h> 881da177e4SLinus Torvalds #include <scsi/scsi_device.h> 891da177e4SLinus Torvalds #include <scsi/scsi_host.h> 901da177e4SLinus Torvalds #include <scsi/scsi_tcq.h> 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #include "initio.h" 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds #define SENSE_SIZE 14 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds #define i91u_MAXQUEUE 2 971da177e4SLinus Torvalds #define i91u_REVID "Initio INI-9X00U/UW SCSI device driver; Revision: 1.04a" 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #ifdef DEBUG_i91u 1001da177e4SLinus Torvalds static unsigned int i91u_debug = DEBUG_DEFAULT; 1011da177e4SLinus Torvalds #endif 1021da177e4SLinus Torvalds 10372d39feaSAlan Cox static int initio_tag_enable = 1; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds #ifdef DEBUG_i91u 1061da177e4SLinus Torvalds static int setup_debug = 0; 1071da177e4SLinus Torvalds #endif 1081da177e4SLinus Torvalds 10972d39feaSAlan Cox static void i91uSCBPost(u8 * pHcb, u8 * pScb); 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds #define DEBUG_INTERRUPT 0 1121da177e4SLinus Torvalds #define DEBUG_QUEUE 0 1131da177e4SLinus Torvalds #define DEBUG_STATE 0 1141da177e4SLinus Torvalds #define INT_DISC 0 1151da177e4SLinus Torvalds 11672d39feaSAlan Cox /*--- forward references ---*/ 11772d39feaSAlan Cox static struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun); 11872d39feaSAlan Cox static struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host); 1191da177e4SLinus Torvalds 12072d39feaSAlan Cox static int tulip_main(struct initio_host * host); 1211da177e4SLinus Torvalds 12272d39feaSAlan Cox static int initio_next_state(struct initio_host * host); 12372d39feaSAlan Cox static int initio_state_1(struct initio_host * host); 12472d39feaSAlan Cox static int initio_state_2(struct initio_host * host); 12572d39feaSAlan Cox static int initio_state_3(struct initio_host * host); 12672d39feaSAlan Cox static int initio_state_4(struct initio_host * host); 12772d39feaSAlan Cox static int initio_state_5(struct initio_host * host); 12872d39feaSAlan Cox static int initio_state_6(struct initio_host * host); 12972d39feaSAlan Cox static int initio_state_7(struct initio_host * host); 13072d39feaSAlan Cox static int initio_xfer_data_in(struct initio_host * host); 13172d39feaSAlan Cox static int initio_xfer_data_out(struct initio_host * host); 13272d39feaSAlan Cox static int initio_xpad_in(struct initio_host * host); 13372d39feaSAlan Cox static int initio_xpad_out(struct initio_host * host); 13472d39feaSAlan Cox static int initio_status_msg(struct initio_host * host); 1351da177e4SLinus Torvalds 13672d39feaSAlan Cox static int initio_msgin(struct initio_host * host); 13772d39feaSAlan Cox static int initio_msgin_sync(struct initio_host * host); 13872d39feaSAlan Cox static int initio_msgin_accept(struct initio_host * host); 13972d39feaSAlan Cox static int initio_msgout_reject(struct initio_host * host); 14072d39feaSAlan Cox static int initio_msgin_extend(struct initio_host * host); 1411da177e4SLinus Torvalds 14272d39feaSAlan Cox static int initio_msgout_ide(struct initio_host * host); 14372d39feaSAlan Cox static int initio_msgout_abort_targ(struct initio_host * host); 14472d39feaSAlan Cox static int initio_msgout_abort_tag(struct initio_host * host); 1451da177e4SLinus Torvalds 14672d39feaSAlan Cox static int initio_bus_device_reset(struct initio_host * host); 14772d39feaSAlan Cox static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb); 14872d39feaSAlan Cox static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb); 14972d39feaSAlan Cox static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb); 15072d39feaSAlan Cox static int int_initio_busfree(struct initio_host * host); 15172d39feaSAlan Cox static int int_initio_scsi_rst(struct initio_host * host); 15272d39feaSAlan Cox static int int_initio_bad_seq(struct initio_host * host); 15372d39feaSAlan Cox static int int_initio_resel(struct initio_host * host); 15472d39feaSAlan Cox static int initio_sync_done(struct initio_host * host); 15572d39feaSAlan Cox static int wdtr_done(struct initio_host * host); 15672d39feaSAlan Cox static int wait_tulip(struct initio_host * host); 15772d39feaSAlan Cox static int initio_wait_done_disc(struct initio_host * host); 15872d39feaSAlan Cox static int initio_wait_disc(struct initio_host * host); 15972d39feaSAlan Cox static void tulip_scsi(struct initio_host * host); 16072d39feaSAlan Cox static int initio_post_scsi_rst(struct initio_host * host); 1611da177e4SLinus Torvalds 16272d39feaSAlan Cox static void initio_se2_ew_en(unsigned long base); 16372d39feaSAlan Cox static void initio_se2_ew_ds(unsigned long base); 16472d39feaSAlan Cox static int initio_se2_rd_all(unsigned long base); 16572d39feaSAlan Cox static void initio_se2_update_all(unsigned long base); /* setup default pattern */ 16672d39feaSAlan Cox static void initio_read_eeprom(unsigned long base); 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds /* ---- INTERNAL VARIABLES ---- */ 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static NVRAM i91unvram; 1711da177e4SLinus Torvalds static NVRAM *i91unvramp; 1721da177e4SLinus Torvalds 17372d39feaSAlan Cox static u8 i91udftNvRam[64] = 1741da177e4SLinus Torvalds { 1751da177e4SLinus Torvalds /*----------- header -----------*/ 1761da177e4SLinus Torvalds 0x25, 0xc9, /* Signature */ 1771da177e4SLinus Torvalds 0x40, /* Size */ 1781da177e4SLinus Torvalds 0x01, /* Revision */ 1791da177e4SLinus Torvalds /* -- Host Adapter Structure -- */ 1801da177e4SLinus Torvalds 0x95, /* ModelByte0 */ 1811da177e4SLinus Torvalds 0x00, /* ModelByte1 */ 1821da177e4SLinus Torvalds 0x00, /* ModelInfo */ 1831da177e4SLinus Torvalds 0x01, /* NumOfCh */ 1841da177e4SLinus Torvalds NBC1_DEFAULT, /* BIOSConfig1 */ 1851da177e4SLinus Torvalds 0, /* BIOSConfig2 */ 1861da177e4SLinus Torvalds 0, /* HAConfig1 */ 1871da177e4SLinus Torvalds 0, /* HAConfig2 */ 1881da177e4SLinus Torvalds /* SCSI channel 0 and target Structure */ 1891da177e4SLinus Torvalds 7, /* SCSIid */ 1901da177e4SLinus Torvalds NCC1_DEFAULT, /* SCSIconfig1 */ 1911da177e4SLinus Torvalds 0, /* SCSIconfig2 */ 1921da177e4SLinus Torvalds 0x10, /* NumSCSItarget */ 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, 1951da177e4SLinus Torvalds NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, 1961da177e4SLinus Torvalds NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, 1971da177e4SLinus Torvalds NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds /* SCSI channel 1 and target Structure */ 2001da177e4SLinus Torvalds 7, /* SCSIid */ 2011da177e4SLinus Torvalds NCC1_DEFAULT, /* SCSIconfig1 */ 2021da177e4SLinus Torvalds 0, /* SCSIconfig2 */ 2031da177e4SLinus Torvalds 0x10, /* NumSCSItarget */ 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, 2061da177e4SLinus Torvalds NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, 2071da177e4SLinus Torvalds NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, 2081da177e4SLinus Torvalds NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, 2091da177e4SLinus Torvalds 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2101da177e4SLinus Torvalds 0, 0}; /* - CheckSum - */ 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds 21372d39feaSAlan Cox static u8 initio_rate_tbl[8] = /* fast 20 */ 2141da177e4SLinus Torvalds { 21525985edcSLucas De Marchi /* nanosecond divide by 4 */ 2161da177e4SLinus Torvalds 12, /* 50ns, 20M */ 2171da177e4SLinus Torvalds 18, /* 75ns, 13.3M */ 2181da177e4SLinus Torvalds 25, /* 100ns, 10M */ 2191da177e4SLinus Torvalds 31, /* 125ns, 8M */ 2201da177e4SLinus Torvalds 37, /* 150ns, 6.6M */ 2211da177e4SLinus Torvalds 43, /* 175ns, 5.7M */ 2221da177e4SLinus Torvalds 50, /* 200ns, 5M */ 2231da177e4SLinus Torvalds 62 /* 250ns, 4M */ 2241da177e4SLinus Torvalds }; 2251da177e4SLinus Torvalds 22672d39feaSAlan Cox static void initio_do_pause(unsigned amount) 22772d39feaSAlan Cox { 22872d39feaSAlan Cox /* Pause for amount jiffies */ 2291da177e4SLinus Torvalds unsigned long the_time = jiffies + amount; 2301da177e4SLinus Torvalds 23172d39feaSAlan Cox while (time_before_eq(jiffies, the_time)) 23272d39feaSAlan Cox cpu_relax(); 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds /*-- forward reference --*/ 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds /****************************************************************** 2381da177e4SLinus Torvalds Input: instruction for Serial E2PROM 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds EX: se2_rd(0 call se2_instr() to send address and read command 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds StartBit OP_Code Address Data 2431da177e4SLinus Torvalds --------- -------- ------------------ ------- 2441da177e4SLinus Torvalds 1 1 , 0 A5,A4,A3,A2,A1,A0 D15-D0 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds +----------------------------------------------------- 2471da177e4SLinus Torvalds | 2481da177e4SLinus Torvalds CS -----+ 2491da177e4SLinus Torvalds +--+ +--+ +--+ +--+ +--+ 2501da177e4SLinus Torvalds ^ | ^ | ^ | ^ | ^ | 2511da177e4SLinus Torvalds | | | | | | | | | | 2521da177e4SLinus Torvalds CLK -------+ +--+ +--+ +--+ +--+ +-- 2531da177e4SLinus Torvalds (leading edge trigger) 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds +--1-----1--+ 2561da177e4SLinus Torvalds | SB OP | OP A5 A4 2571da177e4SLinus Torvalds DI ----+ +--0------------------ 2581da177e4SLinus Torvalds (address and cmd sent to nvram) 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds -------------------------------------------+ 2611da177e4SLinus Torvalds | 2621da177e4SLinus Torvalds DO +--- 2631da177e4SLinus Torvalds (data sent from nvram) 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds ******************************************************************/ 26772d39feaSAlan Cox 26872d39feaSAlan Cox /** 26972d39feaSAlan Cox * initio_se2_instr - bitbang an instruction 27072d39feaSAlan Cox * @base: Base of InitIO controller 27172d39feaSAlan Cox * @instr: Instruction for serial E2PROM 27272d39feaSAlan Cox * 27372d39feaSAlan Cox * Bitbang an instruction out to the serial E2Prom 27472d39feaSAlan Cox */ 27572d39feaSAlan Cox 27672d39feaSAlan Cox static void initio_se2_instr(unsigned long base, u8 instr) 2771da177e4SLinus Torvalds { 2781da177e4SLinus Torvalds int i; 27972d39feaSAlan Cox u8 b; 2801da177e4SLinus Torvalds 28172d39feaSAlan Cox outb(SE2CS | SE2DO, base + TUL_NVRAM); /* cs+start bit */ 28272d39feaSAlan Cox udelay(30); 28372d39feaSAlan Cox outb(SE2CS | SE2CLK | SE2DO, base + TUL_NVRAM); /* +CLK */ 28472d39feaSAlan Cox udelay(30); 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds for (i = 0; i < 8; i++) { 2871da177e4SLinus Torvalds if (instr & 0x80) 2881da177e4SLinus Torvalds b = SE2CS | SE2DO; /* -CLK+dataBit */ 2891da177e4SLinus Torvalds else 2901da177e4SLinus Torvalds b = SE2CS; /* -CLK */ 29172d39feaSAlan Cox outb(b, base + TUL_NVRAM); 29272d39feaSAlan Cox udelay(30); 29372d39feaSAlan Cox outb(b | SE2CLK, base + TUL_NVRAM); /* +CLK */ 29472d39feaSAlan Cox udelay(30); 2951da177e4SLinus Torvalds instr <<= 1; 2961da177e4SLinus Torvalds } 29772d39feaSAlan Cox outb(SE2CS, base + TUL_NVRAM); /* -CLK */ 29872d39feaSAlan Cox udelay(30); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds 30272d39feaSAlan Cox /** 30372d39feaSAlan Cox * initio_se2_ew_en - Enable erase/write 30472d39feaSAlan Cox * @base: Base address of InitIO controller 30572d39feaSAlan Cox * 30672d39feaSAlan Cox * Enable erase/write state of serial EEPROM 30772d39feaSAlan Cox */ 30872d39feaSAlan Cox void initio_se2_ew_en(unsigned long base) 3091da177e4SLinus Torvalds { 31072d39feaSAlan Cox initio_se2_instr(base, 0x30); /* EWEN */ 31172d39feaSAlan Cox outb(0, base + TUL_NVRAM); /* -CS */ 31272d39feaSAlan Cox udelay(30); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds 31672d39feaSAlan Cox /** 31772d39feaSAlan Cox * initio_se2_ew_ds - Disable erase/write 31872d39feaSAlan Cox * @base: Base address of InitIO controller 31972d39feaSAlan Cox * 32072d39feaSAlan Cox * Disable erase/write state of serial EEPROM 32172d39feaSAlan Cox */ 32272d39feaSAlan Cox void initio_se2_ew_ds(unsigned long base) 3231da177e4SLinus Torvalds { 32472d39feaSAlan Cox initio_se2_instr(base, 0); /* EWDS */ 32572d39feaSAlan Cox outb(0, base + TUL_NVRAM); /* -CS */ 32672d39feaSAlan Cox udelay(30); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds 33072d39feaSAlan Cox /** 33172d39feaSAlan Cox * initio_se2_rd - read E2PROM word 33272d39feaSAlan Cox * @base: Base of InitIO controller 33372d39feaSAlan Cox * @addr: Address of word in E2PROM 33472d39feaSAlan Cox * 33572d39feaSAlan Cox * Read a word from the NV E2PROM device 33672d39feaSAlan Cox */ 33772d39feaSAlan Cox static u16 initio_se2_rd(unsigned long base, u8 addr) 3381da177e4SLinus Torvalds { 33972d39feaSAlan Cox u8 instr, rb; 34072d39feaSAlan Cox u16 val = 0; 3411da177e4SLinus Torvalds int i; 3421da177e4SLinus Torvalds 34372d39feaSAlan Cox instr = (u8) (addr | 0x80); 34472d39feaSAlan Cox initio_se2_instr(base, instr); /* READ INSTR */ 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds for (i = 15; i >= 0; i--) { 34772d39feaSAlan Cox outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */ 34872d39feaSAlan Cox udelay(30); 34972d39feaSAlan Cox outb(SE2CS, base + TUL_NVRAM); /* -CLK */ 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /* sample data after the following edge of clock */ 35272d39feaSAlan Cox rb = inb(base + TUL_NVRAM); 35372d39feaSAlan Cox rb &= SE2DI; 35472d39feaSAlan Cox val += (rb << i); 35572d39feaSAlan Cox udelay(30); /* 6/20/95 */ 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 35872d39feaSAlan Cox outb(0, base + TUL_NVRAM); /* no chip select */ 35972d39feaSAlan Cox udelay(30); 36072d39feaSAlan Cox return val; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 36372d39feaSAlan Cox /** 36472d39feaSAlan Cox * initio_se2_wr - read E2PROM word 36572d39feaSAlan Cox * @base: Base of InitIO controller 36672d39feaSAlan Cox * @addr: Address of word in E2PROM 36772d39feaSAlan Cox * @val: Value to write 36872d39feaSAlan Cox * 36972d39feaSAlan Cox * Write a word to the NV E2PROM device. Used when recovering from 37072d39feaSAlan Cox * a problem with the NV. 37172d39feaSAlan Cox */ 37272d39feaSAlan Cox static void initio_se2_wr(unsigned long base, u8 addr, u16 val) 3731da177e4SLinus Torvalds { 37472d39feaSAlan Cox u8 rb; 37572d39feaSAlan Cox u8 instr; 3761da177e4SLinus Torvalds int i; 3771da177e4SLinus Torvalds 37872d39feaSAlan Cox instr = (u8) (addr | 0x40); 37972d39feaSAlan Cox initio_se2_instr(base, instr); /* WRITE INSTR */ 3801da177e4SLinus Torvalds for (i = 15; i >= 0; i--) { 38172d39feaSAlan Cox if (val & 0x8000) 38272d39feaSAlan Cox outb(SE2CS | SE2DO, base + TUL_NVRAM); /* -CLK+dataBit 1 */ 3831da177e4SLinus Torvalds else 38472d39feaSAlan Cox outb(SE2CS, base + TUL_NVRAM); /* -CLK+dataBit 0 */ 38572d39feaSAlan Cox udelay(30); 38672d39feaSAlan Cox outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */ 38772d39feaSAlan Cox udelay(30); 38872d39feaSAlan Cox val <<= 1; 3891da177e4SLinus Torvalds } 39072d39feaSAlan Cox outb(SE2CS, base + TUL_NVRAM); /* -CLK */ 39172d39feaSAlan Cox udelay(30); 39272d39feaSAlan Cox outb(0, base + TUL_NVRAM); /* -CS */ 39372d39feaSAlan Cox udelay(30); 3941da177e4SLinus Torvalds 39572d39feaSAlan Cox outb(SE2CS, base + TUL_NVRAM); /* +CS */ 39672d39feaSAlan Cox udelay(30); 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds for (;;) { 39972d39feaSAlan Cox outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */ 40072d39feaSAlan Cox udelay(30); 40172d39feaSAlan Cox outb(SE2CS, base + TUL_NVRAM); /* -CLK */ 40272d39feaSAlan Cox udelay(30); 40372d39feaSAlan Cox if ((rb = inb(base + TUL_NVRAM)) & SE2DI) 4041da177e4SLinus Torvalds break; /* write complete */ 4051da177e4SLinus Torvalds } 40672d39feaSAlan Cox outb(0, base + TUL_NVRAM); /* -CS */ 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 40972d39feaSAlan Cox /** 41072d39feaSAlan Cox * initio_se2_rd_all - read hostadapter NV configuration 41172d39feaSAlan Cox * @base: Base address of InitIO controller 41272d39feaSAlan Cox * 41372d39feaSAlan Cox * Reads the E2PROM data into main memory. Ensures that the checksum 41472d39feaSAlan Cox * and header marker are valid. Returns 1 on success -1 on error. 41572d39feaSAlan Cox */ 4161da177e4SLinus Torvalds 41772d39feaSAlan Cox static int initio_se2_rd_all(unsigned long base) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds int i; 42072d39feaSAlan Cox u16 chksum = 0; 42172d39feaSAlan Cox u16 *np; 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds i91unvramp = &i91unvram; 42472d39feaSAlan Cox np = (u16 *) i91unvramp; 42572d39feaSAlan Cox for (i = 0; i < 32; i++) 42672d39feaSAlan Cox *np++ = initio_se2_rd(base, i); 4271da177e4SLinus Torvalds 42872d39feaSAlan Cox /* Is signature "ini" ok ? */ 4291da177e4SLinus Torvalds if (i91unvramp->NVM_Signature != INI_SIGNATURE) 4301da177e4SLinus Torvalds return -1; 43172d39feaSAlan Cox /* Is ckecksum ok ? */ 43272d39feaSAlan Cox np = (u16 *) i91unvramp; 4331da177e4SLinus Torvalds for (i = 0; i < 31; i++) 4341da177e4SLinus Torvalds chksum += *np++; 43572d39feaSAlan Cox if (i91unvramp->NVM_CheckSum != chksum) 4361da177e4SLinus Torvalds return -1; 4371da177e4SLinus Torvalds return 1; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 44072d39feaSAlan Cox /** 44172d39feaSAlan Cox * initio_se2_update_all - Update E2PROM 44272d39feaSAlan Cox * @base: Base of InitIO controller 44372d39feaSAlan Cox * 44472d39feaSAlan Cox * Update the E2PROM by wrting any changes into the E2PROM 44572d39feaSAlan Cox * chip, rewriting the checksum. 44672d39feaSAlan Cox */ 44772d39feaSAlan Cox static void initio_se2_update_all(unsigned long base) 4481da177e4SLinus Torvalds { /* setup default pattern */ 4491da177e4SLinus Torvalds int i; 45072d39feaSAlan Cox u16 chksum = 0; 45172d39feaSAlan Cox u16 *np, *np1; 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds i91unvramp = &i91unvram; 4541da177e4SLinus Torvalds /* Calculate checksum first */ 45572d39feaSAlan Cox np = (u16 *) i91udftNvRam; 4561da177e4SLinus Torvalds for (i = 0; i < 31; i++) 4571da177e4SLinus Torvalds chksum += *np++; 45872d39feaSAlan Cox *np = chksum; 45972d39feaSAlan Cox initio_se2_ew_en(base); /* Enable write */ 4601da177e4SLinus Torvalds 46172d39feaSAlan Cox np = (u16 *) i91udftNvRam; 46272d39feaSAlan Cox np1 = (u16 *) i91unvramp; 4631da177e4SLinus Torvalds for (i = 0; i < 32; i++, np++, np1++) { 46472d39feaSAlan Cox if (*np != *np1) 46572d39feaSAlan Cox initio_se2_wr(base, i, *np); 4661da177e4SLinus Torvalds } 46772d39feaSAlan Cox initio_se2_ew_ds(base); /* Disable write */ 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 47072d39feaSAlan Cox /** 47172d39feaSAlan Cox * initio_read_eeprom - Retrieve configuration 47272d39feaSAlan Cox * @base: Base of InitIO Host Adapter 47372d39feaSAlan Cox * 47472d39feaSAlan Cox * Retrieve the host adapter configuration data from E2Prom. If the 47572d39feaSAlan Cox * data is invalid then the defaults are used and are also restored 47672d39feaSAlan Cox * into the E2PROM. This forms the access point for the SCSI driver 47772d39feaSAlan Cox * into the E2PROM layer, the other functions for the E2PROM are all 47872d39feaSAlan Cox * internal use. 47972d39feaSAlan Cox * 48072d39feaSAlan Cox * Must be called single threaded, uses a shared global area. 48172d39feaSAlan Cox */ 4821da177e4SLinus Torvalds 48372d39feaSAlan Cox static void initio_read_eeprom(unsigned long base) 4841da177e4SLinus Torvalds { 48572d39feaSAlan Cox u8 gctrl; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds i91unvramp = &i91unvram; 48872d39feaSAlan Cox /* Enable EEProm programming */ 48972d39feaSAlan Cox gctrl = inb(base + TUL_GCTRL); 49072d39feaSAlan Cox outb(gctrl | TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL); 49172d39feaSAlan Cox if (initio_se2_rd_all(base) != 1) { 49272d39feaSAlan Cox initio_se2_update_all(base); /* setup default pattern */ 49372d39feaSAlan Cox initio_se2_rd_all(base); /* load again */ 4941da177e4SLinus Torvalds } 49572d39feaSAlan Cox /* Disable EEProm programming */ 49672d39feaSAlan Cox gctrl = inb(base + TUL_GCTRL); 49772d39feaSAlan Cox outb(gctrl & ~TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL); 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 50072d39feaSAlan Cox /** 50172d39feaSAlan Cox * initio_stop_bm - stop bus master 50272d39feaSAlan Cox * @host: InitIO we are stopping 50372d39feaSAlan Cox * 5043ad2f3fbSDaniel Mack * Stop any pending DMA operation, aborting the DMA if necessary 50572d39feaSAlan Cox */ 5061da177e4SLinus Torvalds 50772d39feaSAlan Cox static void initio_stop_bm(struct initio_host * host) 5081da177e4SLinus Torvalds { 5091da177e4SLinus Torvalds 51072d39feaSAlan Cox if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */ 51172d39feaSAlan Cox outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd); 5121da177e4SLinus Torvalds /* wait Abort DMA xfer done */ 51372d39feaSAlan Cox while ((inb(host->addr + TUL_Int) & XABT) == 0) 51472d39feaSAlan Cox cpu_relax(); 5151da177e4SLinus Torvalds } 51672d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 51972d39feaSAlan Cox /** 52072d39feaSAlan Cox * initio_reset_scsi - Reset SCSI host controller 52172d39feaSAlan Cox * @host: InitIO host to reset 52272d39feaSAlan Cox * @seconds: Recovery time 52372d39feaSAlan Cox * 52472d39feaSAlan Cox * Perform a full reset of the SCSI subsystem. 52572d39feaSAlan Cox */ 52672d39feaSAlan Cox 52772d39feaSAlan Cox static int initio_reset_scsi(struct initio_host * host, int seconds) 5281da177e4SLinus Torvalds { 52972d39feaSAlan Cox outb(TSC_RST_BUS, host->addr + TUL_SCtrl0); 5301da177e4SLinus Torvalds 53172d39feaSAlan Cox while (!((host->jsint = inb(host->addr + TUL_SInt)) & TSS_SCSIRST_INT)) 53272d39feaSAlan Cox cpu_relax(); 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds /* reset tulip chip */ 53572d39feaSAlan Cox outb(0, host->addr + TUL_SSignal); 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds /* Stall for a while, wait for target's firmware ready,make it 2 sec ! */ 5381da177e4SLinus Torvalds /* SONY 5200 tape drive won't work if only stall for 1 sec */ 53972d39feaSAlan Cox /* FIXME: this is a very long busy wait right now */ 54072d39feaSAlan Cox initio_do_pause(seconds * HZ); 5411da177e4SLinus Torvalds 54272d39feaSAlan Cox inb(host->addr + TUL_SInt); 54372d39feaSAlan Cox return SCSI_RESET_SUCCESS; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 54672d39feaSAlan Cox /** 54772d39feaSAlan Cox * initio_init - set up an InitIO host adapter 54872d39feaSAlan Cox * @host: InitIO host adapter 54972d39feaSAlan Cox * @num_scbs: Number of SCBS 55072d39feaSAlan Cox * @bios_addr: BIOS address 55172d39feaSAlan Cox * 55272d39feaSAlan Cox * Set up the host adapter and devices according to the configuration 55372d39feaSAlan Cox * retrieved from the E2PROM. 55472d39feaSAlan Cox * 55572d39feaSAlan Cox * Locking: Calls E2PROM layer code which is not re-enterable so must 55672d39feaSAlan Cox * run single threaded for now. 55772d39feaSAlan Cox */ 55872d39feaSAlan Cox 55972d39feaSAlan Cox static void initio_init(struct initio_host * host, u8 *bios_addr) 5601da177e4SLinus Torvalds { 5611da177e4SLinus Torvalds int i; 56272d39feaSAlan Cox u8 *flags; 56372d39feaSAlan Cox u8 *heads; 5641da177e4SLinus Torvalds 56572d39feaSAlan Cox /* Get E2Prom configuration */ 56672d39feaSAlan Cox initio_read_eeprom(host->addr); 5671da177e4SLinus Torvalds if (i91unvramp->NVM_SCSIInfo[0].NVM_NumOfTarg == 8) 56872d39feaSAlan Cox host->max_tar = 8; 5691da177e4SLinus Torvalds else 57072d39feaSAlan Cox host->max_tar = 16; 5711da177e4SLinus Torvalds 57272d39feaSAlan Cox host->config = i91unvramp->NVM_SCSIInfo[0].NVM_ChConfig1; 5731da177e4SLinus Torvalds 57472d39feaSAlan Cox host->scsi_id = i91unvramp->NVM_SCSIInfo[0].NVM_ChSCSIID; 57572d39feaSAlan Cox host->idmask = ~(1 << host->scsi_id); 5761da177e4SLinus Torvalds 57744456d37SOlaf Hering #ifdef CHK_PARITY 5781da177e4SLinus Torvalds /* Enable parity error response */ 57972d39feaSAlan Cox outb(inb(host->addr + TUL_PCMD) | 0x40, host->addr + TUL_PCMD); 5801da177e4SLinus Torvalds #endif 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* Mask all the interrupt */ 58372d39feaSAlan Cox outb(0x1F, host->addr + TUL_Mask); 5841da177e4SLinus Torvalds 58572d39feaSAlan Cox initio_stop_bm(host); 5861da177e4SLinus Torvalds /* --- Initialize the tulip --- */ 58772d39feaSAlan Cox outb(TSC_RST_CHIP, host->addr + TUL_SCtrl0); 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds /* program HBA's SCSI ID */ 59072d39feaSAlan Cox outb(host->scsi_id << 4, host->addr + TUL_SScsiId); 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds /* Enable Initiator Mode ,phase latch,alternate sync period mode, 5931da177e4SLinus Torvalds disable SCSI reset */ 59472d39feaSAlan Cox if (host->config & HCC_EN_PAR) 59572d39feaSAlan Cox host->sconf1 = (TSC_INITDEFAULT | TSC_EN_SCSI_PAR); 5961da177e4SLinus Torvalds else 59772d39feaSAlan Cox host->sconf1 = (TSC_INITDEFAULT); 59872d39feaSAlan Cox outb(host->sconf1, host->addr + TUL_SConfig); 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* Enable HW reselect */ 60172d39feaSAlan Cox outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); 6021da177e4SLinus Torvalds 60372d39feaSAlan Cox outb(0, host->addr + TUL_SPeriod); 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds /* selection time out = 250 ms */ 60672d39feaSAlan Cox outb(153, host->addr + TUL_STimeOut); 6071da177e4SLinus Torvalds 60872d39feaSAlan Cox /* Enable SCSI terminator */ 60972d39feaSAlan Cox outb((host->config & (HCC_ACT_TERM1 | HCC_ACT_TERM2)), 61072d39feaSAlan Cox host->addr + TUL_XCtrl); 61172d39feaSAlan Cox outb(((host->config & HCC_AUTO_TERM) >> 4) | 61272d39feaSAlan Cox (inb(host->addr + TUL_GCTRL1) & 0xFE), 61372d39feaSAlan Cox host->addr + TUL_GCTRL1); 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds for (i = 0, 61672d39feaSAlan Cox flags = & (i91unvramp->NVM_SCSIInfo[0].NVM_Targ0Config), 61772d39feaSAlan Cox heads = bios_addr + 0x180; 61872d39feaSAlan Cox i < host->max_tar; 61972d39feaSAlan Cox i++, flags++) { 62072d39feaSAlan Cox host->targets[i].flags = *flags & ~(TCF_SYNC_DONE | TCF_WDTR_DONE); 62172d39feaSAlan Cox if (host->targets[i].flags & TCF_EN_255) 62272d39feaSAlan Cox host->targets[i].drv_flags = TCF_DRV_255_63; 6231da177e4SLinus Torvalds else 62472d39feaSAlan Cox host->targets[i].drv_flags = 0; 62572d39feaSAlan Cox host->targets[i].js_period = 0; 62672d39feaSAlan Cox host->targets[i].sconfig0 = host->sconf1; 62772d39feaSAlan Cox host->targets[i].heads = *heads++; 62872d39feaSAlan Cox if (host->targets[i].heads == 255) 62972d39feaSAlan Cox host->targets[i].drv_flags = TCF_DRV_255_63; 6301da177e4SLinus Torvalds else 63172d39feaSAlan Cox host->targets[i].drv_flags = 0; 63272d39feaSAlan Cox host->targets[i].sectors = *heads++; 63372d39feaSAlan Cox host->targets[i].flags &= ~TCF_BUSY; 63472d39feaSAlan Cox host->act_tags[i] = 0; 63572d39feaSAlan Cox host->max_tags[i] = 0xFF; 6361da177e4SLinus Torvalds } /* for */ 6371da177e4SLinus Torvalds printk("i91u: PCI Base=0x%04X, IRQ=%d, BIOS=0x%04X0, SCSI ID=%d\n", 638e9e42fafSAlan Cox host->addr, host->pci_dev->irq, 63972d39feaSAlan Cox host->bios_addr, host->scsi_id); 64072d39feaSAlan Cox /* Reset SCSI Bus */ 64172d39feaSAlan Cox if (host->config & HCC_SCSI_RESET) { 64272d39feaSAlan Cox printk(KERN_INFO "i91u: Reset SCSI Bus ... \n"); 64372d39feaSAlan Cox initio_reset_scsi(host, 10); 6441da177e4SLinus Torvalds } 64572d39feaSAlan Cox outb(0x17, host->addr + TUL_SCFG1); 64672d39feaSAlan Cox outb(0xE9, host->addr + TUL_SIntEnable); 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 64972d39feaSAlan Cox /** 65072d39feaSAlan Cox * initio_alloc_scb - Allocate an SCB 65172d39feaSAlan Cox * @host: InitIO host we are allocating for 65272d39feaSAlan Cox * 65372d39feaSAlan Cox * Walk the SCB list for the controller and allocate a free SCB if 65472d39feaSAlan Cox * one exists. 65572d39feaSAlan Cox */ 65672d39feaSAlan Cox static struct scsi_ctrl_blk *initio_alloc_scb(struct initio_host *host) 6571da177e4SLinus Torvalds { 65872d39feaSAlan Cox struct scsi_ctrl_blk *scb; 65972d39feaSAlan Cox unsigned long flags; 66072d39feaSAlan Cox 66172d39feaSAlan Cox spin_lock_irqsave(&host->avail_lock, flags); 66272d39feaSAlan Cox if ((scb = host->first_avail) != NULL) { 6631da177e4SLinus Torvalds #if DEBUG_QUEUE 66472d39feaSAlan Cox printk("find scb at %p\n", scb); 6651da177e4SLinus Torvalds #endif 66672d39feaSAlan Cox if ((host->first_avail = scb->next) == NULL) 66772d39feaSAlan Cox host->last_avail = NULL; 66872d39feaSAlan Cox scb->next = NULL; 66972d39feaSAlan Cox scb->status = SCB_RENT; 6701da177e4SLinus Torvalds } 67172d39feaSAlan Cox spin_unlock_irqrestore(&host->avail_lock, flags); 67272d39feaSAlan Cox return scb; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 67572d39feaSAlan Cox /** 67672d39feaSAlan Cox * initio_release_scb - Release an SCB 67772d39feaSAlan Cox * @host: InitIO host that owns the SCB 67872d39feaSAlan Cox * @cmnd: SCB command block being returned 67972d39feaSAlan Cox * 68072d39feaSAlan Cox * Return an allocated SCB to the host free list 68172d39feaSAlan Cox */ 68272d39feaSAlan Cox 68372d39feaSAlan Cox static void initio_release_scb(struct initio_host * host, struct scsi_ctrl_blk * cmnd) 6841da177e4SLinus Torvalds { 68572d39feaSAlan Cox unsigned long flags; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds #if DEBUG_QUEUE 68872d39feaSAlan Cox printk("Release SCB %p; ", cmnd); 6891da177e4SLinus Torvalds #endif 69072d39feaSAlan Cox spin_lock_irqsave(&(host->avail_lock), flags); 69172d39feaSAlan Cox cmnd->srb = NULL; 69272d39feaSAlan Cox cmnd->status = 0; 69372d39feaSAlan Cox cmnd->next = NULL; 69472d39feaSAlan Cox if (host->last_avail != NULL) { 69572d39feaSAlan Cox host->last_avail->next = cmnd; 69672d39feaSAlan Cox host->last_avail = cmnd; 6971da177e4SLinus Torvalds } else { 69872d39feaSAlan Cox host->first_avail = cmnd; 69972d39feaSAlan Cox host->last_avail = cmnd; 7001da177e4SLinus Torvalds } 70172d39feaSAlan Cox spin_unlock_irqrestore(&(host->avail_lock), flags); 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds /***************************************************************************/ 70572d39feaSAlan Cox static void initio_append_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp) 7061da177e4SLinus Torvalds { 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds #if DEBUG_QUEUE 70972d39feaSAlan Cox printk("Append pend SCB %p; ", scbp); 7101da177e4SLinus Torvalds #endif 71172d39feaSAlan Cox scbp->status = SCB_PEND; 71272d39feaSAlan Cox scbp->next = NULL; 71372d39feaSAlan Cox if (host->last_pending != NULL) { 71472d39feaSAlan Cox host->last_pending->next = scbp; 71572d39feaSAlan Cox host->last_pending = scbp; 7161da177e4SLinus Torvalds } else { 71772d39feaSAlan Cox host->first_pending = scbp; 71872d39feaSAlan Cox host->last_pending = scbp; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds /***************************************************************************/ 72372d39feaSAlan Cox static void initio_push_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp) 7241da177e4SLinus Torvalds { 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds #if DEBUG_QUEUE 72772d39feaSAlan Cox printk("Push pend SCB %p; ", scbp); 7281da177e4SLinus Torvalds #endif 72972d39feaSAlan Cox scbp->status = SCB_PEND; 73072d39feaSAlan Cox if ((scbp->next = host->first_pending) != NULL) { 73172d39feaSAlan Cox host->first_pending = scbp; 7321da177e4SLinus Torvalds } else { 73372d39feaSAlan Cox host->first_pending = scbp; 73472d39feaSAlan Cox host->last_pending = scbp; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 73872d39feaSAlan Cox static struct scsi_ctrl_blk *initio_find_first_pend_scb(struct initio_host * host) 7391da177e4SLinus Torvalds { 74072d39feaSAlan Cox struct scsi_ctrl_blk *first; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds 74372d39feaSAlan Cox first = host->first_pending; 74472d39feaSAlan Cox while (first != NULL) { 74572d39feaSAlan Cox if (first->opcode != ExecSCSI) 74672d39feaSAlan Cox return first; 74772d39feaSAlan Cox if (first->tagmsg == 0) { 74872d39feaSAlan Cox if ((host->act_tags[first->target] == 0) && 74972d39feaSAlan Cox !(host->targets[first->target].flags & TCF_BUSY)) 75072d39feaSAlan Cox return first; 7511da177e4SLinus Torvalds } else { 75272d39feaSAlan Cox if ((host->act_tags[first->target] >= 75372d39feaSAlan Cox host->max_tags[first->target]) | 75472d39feaSAlan Cox (host->targets[first->target].flags & TCF_BUSY)) { 75572d39feaSAlan Cox first = first->next; 7561da177e4SLinus Torvalds continue; 7571da177e4SLinus Torvalds } 75872d39feaSAlan Cox return first; 7591da177e4SLinus Torvalds } 76072d39feaSAlan Cox first = first->next; 76172d39feaSAlan Cox } 76272d39feaSAlan Cox return first; 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 76572d39feaSAlan Cox static void initio_unlink_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scb) 7661da177e4SLinus Torvalds { 76772d39feaSAlan Cox struct scsi_ctrl_blk *tmp, *prev; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds #if DEBUG_QUEUE 77072d39feaSAlan Cox printk("unlink pend SCB %p; ", scb); 7711da177e4SLinus Torvalds #endif 7721da177e4SLinus Torvalds 77372d39feaSAlan Cox prev = tmp = host->first_pending; 77472d39feaSAlan Cox while (tmp != NULL) { 77572d39feaSAlan Cox if (scb == tmp) { /* Unlink this SCB */ 77672d39feaSAlan Cox if (tmp == host->first_pending) { 77772d39feaSAlan Cox if ((host->first_pending = tmp->next) == NULL) 77872d39feaSAlan Cox host->last_pending = NULL; 7791da177e4SLinus Torvalds } else { 78072d39feaSAlan Cox prev->next = tmp->next; 78172d39feaSAlan Cox if (tmp == host->last_pending) 78272d39feaSAlan Cox host->last_pending = prev; 7831da177e4SLinus Torvalds } 78472d39feaSAlan Cox tmp->next = NULL; 7851da177e4SLinus Torvalds break; 7861da177e4SLinus Torvalds } 78772d39feaSAlan Cox prev = tmp; 78872d39feaSAlan Cox tmp = tmp->next; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds } 79172d39feaSAlan Cox 79272d39feaSAlan Cox static void initio_append_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp) 7931da177e4SLinus Torvalds { 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds #if DEBUG_QUEUE 796e2d435eaSStuart Swales printk("append busy SCB %p; ", scbp); 7971da177e4SLinus Torvalds #endif 79872d39feaSAlan Cox if (scbp->tagmsg) 79972d39feaSAlan Cox host->act_tags[scbp->target]++; 8001da177e4SLinus Torvalds else 80172d39feaSAlan Cox host->targets[scbp->target].flags |= TCF_BUSY; 80272d39feaSAlan Cox scbp->status = SCB_BUSY; 80372d39feaSAlan Cox scbp->next = NULL; 80472d39feaSAlan Cox if (host->last_busy != NULL) { 80572d39feaSAlan Cox host->last_busy->next = scbp; 80672d39feaSAlan Cox host->last_busy = scbp; 8071da177e4SLinus Torvalds } else { 80872d39feaSAlan Cox host->first_busy = scbp; 80972d39feaSAlan Cox host->last_busy = scbp; 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /***************************************************************************/ 81472d39feaSAlan Cox static struct scsi_ctrl_blk *initio_pop_busy_scb(struct initio_host * host) 8151da177e4SLinus Torvalds { 81672d39feaSAlan Cox struct scsi_ctrl_blk *tmp; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds 81972d39feaSAlan Cox if ((tmp = host->first_busy) != NULL) { 82072d39feaSAlan Cox if ((host->first_busy = tmp->next) == NULL) 82172d39feaSAlan Cox host->last_busy = NULL; 82272d39feaSAlan Cox tmp->next = NULL; 82372d39feaSAlan Cox if (tmp->tagmsg) 82472d39feaSAlan Cox host->act_tags[tmp->target]--; 8251da177e4SLinus Torvalds else 82672d39feaSAlan Cox host->targets[tmp->target].flags &= ~TCF_BUSY; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds #if DEBUG_QUEUE 82972d39feaSAlan Cox printk("Pop busy SCB %p; ", tmp); 8301da177e4SLinus Torvalds #endif 83172d39feaSAlan Cox return tmp; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds /***************************************************************************/ 83572d39feaSAlan Cox static void initio_unlink_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scb) 8361da177e4SLinus Torvalds { 83772d39feaSAlan Cox struct scsi_ctrl_blk *tmp, *prev; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds #if DEBUG_QUEUE 84072d39feaSAlan Cox printk("unlink busy SCB %p; ", scb); 8411da177e4SLinus Torvalds #endif 8421da177e4SLinus Torvalds 84372d39feaSAlan Cox prev = tmp = host->first_busy; 84472d39feaSAlan Cox while (tmp != NULL) { 84572d39feaSAlan Cox if (scb == tmp) { /* Unlink this SCB */ 84672d39feaSAlan Cox if (tmp == host->first_busy) { 84772d39feaSAlan Cox if ((host->first_busy = tmp->next) == NULL) 84872d39feaSAlan Cox host->last_busy = NULL; 8491da177e4SLinus Torvalds } else { 85072d39feaSAlan Cox prev->next = tmp->next; 85172d39feaSAlan Cox if (tmp == host->last_busy) 85272d39feaSAlan Cox host->last_busy = prev; 8531da177e4SLinus Torvalds } 85472d39feaSAlan Cox tmp->next = NULL; 85572d39feaSAlan Cox if (tmp->tagmsg) 85672d39feaSAlan Cox host->act_tags[tmp->target]--; 8571da177e4SLinus Torvalds else 85872d39feaSAlan Cox host->targets[tmp->target].flags &= ~TCF_BUSY; 8591da177e4SLinus Torvalds break; 8601da177e4SLinus Torvalds } 86172d39feaSAlan Cox prev = tmp; 86272d39feaSAlan Cox tmp = tmp->next; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds return; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 86772d39feaSAlan Cox struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun) 8681da177e4SLinus Torvalds { 86972d39feaSAlan Cox struct scsi_ctrl_blk *tmp, *prev; 87072d39feaSAlan Cox u16 scbp_tarlun; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds 87372d39feaSAlan Cox prev = tmp = host->first_busy; 87472d39feaSAlan Cox while (tmp != NULL) { 87572d39feaSAlan Cox scbp_tarlun = (tmp->lun << 8) | (tmp->target); 8761da177e4SLinus Torvalds if (scbp_tarlun == tarlun) { /* Unlink this SCB */ 8771da177e4SLinus Torvalds break; 8781da177e4SLinus Torvalds } 87972d39feaSAlan Cox prev = tmp; 88072d39feaSAlan Cox tmp = tmp->next; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds #if DEBUG_QUEUE 88372d39feaSAlan Cox printk("find busy SCB %p; ", tmp); 8841da177e4SLinus Torvalds #endif 88572d39feaSAlan Cox return tmp; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 88872d39feaSAlan Cox static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp) 8891da177e4SLinus Torvalds { 8901da177e4SLinus Torvalds #if DEBUG_QUEUE 89172d39feaSAlan Cox printk("append done SCB %p; ", scbp); 8921da177e4SLinus Torvalds #endif 8931da177e4SLinus Torvalds 89472d39feaSAlan Cox scbp->status = SCB_DONE; 89572d39feaSAlan Cox scbp->next = NULL; 89672d39feaSAlan Cox if (host->last_done != NULL) { 89772d39feaSAlan Cox host->last_done->next = scbp; 89872d39feaSAlan Cox host->last_done = scbp; 8991da177e4SLinus Torvalds } else { 90072d39feaSAlan Cox host->first_done = scbp; 90172d39feaSAlan Cox host->last_done = scbp; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds 90572d39feaSAlan Cox struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host) 9061da177e4SLinus Torvalds { 90772d39feaSAlan Cox struct scsi_ctrl_blk *tmp; 9081da177e4SLinus Torvalds 90972d39feaSAlan Cox if ((tmp = host->first_done) != NULL) { 91072d39feaSAlan Cox if ((host->first_done = tmp->next) == NULL) 91172d39feaSAlan Cox host->last_done = NULL; 91272d39feaSAlan Cox tmp->next = NULL; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds #if DEBUG_QUEUE 91572d39feaSAlan Cox printk("find done SCB %p; ",tmp); 9161da177e4SLinus Torvalds #endif 91772d39feaSAlan Cox return tmp; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 92072d39feaSAlan Cox static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp) 9211da177e4SLinus Torvalds { 92272d39feaSAlan Cox unsigned long flags; 92372d39feaSAlan Cox struct scsi_ctrl_blk *tmp, *prev; 9241da177e4SLinus Torvalds 92572d39feaSAlan Cox spin_lock_irqsave(&host->semaph_lock, flags); 9261da177e4SLinus Torvalds 92772d39feaSAlan Cox if ((host->semaph == 0) && (host->active == NULL)) { 9281da177e4SLinus Torvalds /* disable Jasmin SCSI Int */ 92972d39feaSAlan Cox outb(0x1F, host->addr + TUL_Mask); 93072d39feaSAlan Cox spin_unlock_irqrestore(&host->semaph_lock, flags); 93172d39feaSAlan Cox /* FIXME: synchronize_irq needed ? */ 93272d39feaSAlan Cox tulip_main(host); 93372d39feaSAlan Cox spin_lock_irqsave(&host->semaph_lock, flags); 93472d39feaSAlan Cox host->semaph = 1; 93572d39feaSAlan Cox outb(0x0F, host->addr + TUL_Mask); 93672d39feaSAlan Cox spin_unlock_irqrestore(&host->semaph_lock, flags); 9371da177e4SLinus Torvalds return SCSI_ABORT_SNOOZE; 9381da177e4SLinus Torvalds } 93972d39feaSAlan Cox prev = tmp = host->first_pending; /* Check Pend queue */ 94072d39feaSAlan Cox while (tmp != NULL) { 9411da177e4SLinus Torvalds /* 07/27/98 */ 94272d39feaSAlan Cox if (tmp->srb == srbp) { 94372d39feaSAlan Cox if (tmp == host->active) { 94472d39feaSAlan Cox spin_unlock_irqrestore(&host->semaph_lock, flags); 9451da177e4SLinus Torvalds return SCSI_ABORT_BUSY; 94672d39feaSAlan Cox } else if (tmp == host->first_pending) { 94772d39feaSAlan Cox if ((host->first_pending = tmp->next) == NULL) 94872d39feaSAlan Cox host->last_pending = NULL; 9491da177e4SLinus Torvalds } else { 95072d39feaSAlan Cox prev->next = tmp->next; 95172d39feaSAlan Cox if (tmp == host->last_pending) 95272d39feaSAlan Cox host->last_pending = prev; 9531da177e4SLinus Torvalds } 95472d39feaSAlan Cox tmp->hastat = HOST_ABORTED; 95572d39feaSAlan Cox tmp->flags |= SCF_DONE; 95672d39feaSAlan Cox if (tmp->flags & SCF_POST) 95772d39feaSAlan Cox (*tmp->post) ((u8 *) host, (u8 *) tmp); 95872d39feaSAlan Cox spin_unlock_irqrestore(&host->semaph_lock, flags); 9591da177e4SLinus Torvalds return SCSI_ABORT_SUCCESS; 9601da177e4SLinus Torvalds } 96172d39feaSAlan Cox prev = tmp; 96272d39feaSAlan Cox tmp = tmp->next; 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds 96572d39feaSAlan Cox prev = tmp = host->first_busy; /* Check Busy queue */ 96672d39feaSAlan Cox while (tmp != NULL) { 96772d39feaSAlan Cox if (tmp->srb == srbp) { 96872d39feaSAlan Cox if (tmp == host->active) { 96972d39feaSAlan Cox spin_unlock_irqrestore(&host->semaph_lock, flags); 9701da177e4SLinus Torvalds return SCSI_ABORT_BUSY; 97172d39feaSAlan Cox } else if (tmp->tagmsg == 0) { 97272d39feaSAlan Cox spin_unlock_irqrestore(&host->semaph_lock, flags); 9731da177e4SLinus Torvalds return SCSI_ABORT_BUSY; 9741da177e4SLinus Torvalds } else { 97572d39feaSAlan Cox host->act_tags[tmp->target]--; 97672d39feaSAlan Cox if (tmp == host->first_busy) { 97772d39feaSAlan Cox if ((host->first_busy = tmp->next) == NULL) 97872d39feaSAlan Cox host->last_busy = NULL; 9791da177e4SLinus Torvalds } else { 98072d39feaSAlan Cox prev->next = tmp->next; 98172d39feaSAlan Cox if (tmp == host->last_busy) 98272d39feaSAlan Cox host->last_busy = prev; 9831da177e4SLinus Torvalds } 98472d39feaSAlan Cox tmp->next = NULL; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds 98772d39feaSAlan Cox tmp->hastat = HOST_ABORTED; 98872d39feaSAlan Cox tmp->flags |= SCF_DONE; 98972d39feaSAlan Cox if (tmp->flags & SCF_POST) 99072d39feaSAlan Cox (*tmp->post) ((u8 *) host, (u8 *) tmp); 99172d39feaSAlan Cox spin_unlock_irqrestore(&host->semaph_lock, flags); 9921da177e4SLinus Torvalds return SCSI_ABORT_SUCCESS; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds } 99572d39feaSAlan Cox prev = tmp; 99672d39feaSAlan Cox tmp = tmp->next; 9971da177e4SLinus Torvalds } 99872d39feaSAlan Cox spin_unlock_irqrestore(&host->semaph_lock, flags); 99972d39feaSAlan Cox return SCSI_ABORT_NOT_RUNNING; 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds /***************************************************************************/ 100372d39feaSAlan Cox static int initio_bad_seq(struct initio_host * host) 10041da177e4SLinus Torvalds { 100572d39feaSAlan Cox struct scsi_ctrl_blk *scb; 10061da177e4SLinus Torvalds 100772d39feaSAlan Cox printk("initio_bad_seg c=%d\n", host->index); 10081da177e4SLinus Torvalds 100972d39feaSAlan Cox if ((scb = host->active) != NULL) { 101072d39feaSAlan Cox initio_unlink_busy_scb(host, scb); 101172d39feaSAlan Cox scb->hastat = HOST_BAD_PHAS; 101272d39feaSAlan Cox scb->tastat = 0; 101372d39feaSAlan Cox initio_append_done_scb(host, scb); 10141da177e4SLinus Torvalds } 101572d39feaSAlan Cox initio_stop_bm(host); 101672d39feaSAlan Cox initio_reset_scsi(host, 8); /* 7/29/98 */ 101772d39feaSAlan Cox return initio_post_scsi_rst(host); 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 1020a2ba192cSAdrian Bunk 10211da177e4SLinus Torvalds /************************************************************************/ 102272d39feaSAlan Cox static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb) 10231da177e4SLinus Torvalds { 102472d39feaSAlan Cox unsigned long flags; 10251da177e4SLinus Torvalds 102672d39feaSAlan Cox scb->mode = 0; 10271da177e4SLinus Torvalds 102872d39feaSAlan Cox scb->sgidx = 0; 102972d39feaSAlan Cox scb->sgmax = scb->sglen; 10301da177e4SLinus Torvalds 103172d39feaSAlan Cox spin_lock_irqsave(&host->semaph_lock, flags); 10321da177e4SLinus Torvalds 103372d39feaSAlan Cox initio_append_pend_scb(host, scb); /* Append this SCB to Pending queue */ 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds /* VVVVV 07/21/98 */ 103672d39feaSAlan Cox if (host->semaph == 1) { 103772d39feaSAlan Cox /* Disable Jasmin SCSI Int */ 103872d39feaSAlan Cox outb(0x1F, host->addr + TUL_Mask); 103972d39feaSAlan Cox host->semaph = 0; 104072d39feaSAlan Cox spin_unlock_irqrestore(&host->semaph_lock, flags); 10411da177e4SLinus Torvalds 104272d39feaSAlan Cox tulip_main(host); 10431da177e4SLinus Torvalds 104472d39feaSAlan Cox spin_lock_irqsave(&host->semaph_lock, flags); 104572d39feaSAlan Cox host->semaph = 1; 104672d39feaSAlan Cox outb(0x0F, host->addr + TUL_Mask); 10471da177e4SLinus Torvalds } 104872d39feaSAlan Cox spin_unlock_irqrestore(&host->semaph_lock, flags); 10491da177e4SLinus Torvalds return; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds /***************************************************************************/ 105372d39feaSAlan Cox static int initio_isr(struct initio_host * host) 10541da177e4SLinus Torvalds { 105572d39feaSAlan Cox if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) { 105672d39feaSAlan Cox if (host->semaph == 1) { 105772d39feaSAlan Cox outb(0x1F, host->addr + TUL_Mask); 10581da177e4SLinus Torvalds /* Disable Tulip SCSI Int */ 105972d39feaSAlan Cox host->semaph = 0; 10601da177e4SLinus Torvalds 106172d39feaSAlan Cox tulip_main(host); 10621da177e4SLinus Torvalds 106372d39feaSAlan Cox host->semaph = 1; 106472d39feaSAlan Cox outb(0x0F, host->addr + TUL_Mask); 106572d39feaSAlan Cox return 1; 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds } 106872d39feaSAlan Cox return 0; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 107172d39feaSAlan Cox static int tulip_main(struct initio_host * host) 10721da177e4SLinus Torvalds { 107372d39feaSAlan Cox struct scsi_ctrl_blk *scb; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds for (;;) { 107672d39feaSAlan Cox tulip_scsi(host); /* Call tulip_scsi */ 10771da177e4SLinus Torvalds 107872d39feaSAlan Cox /* Walk the list of completed SCBs */ 107972d39feaSAlan Cox while ((scb = initio_find_done_scb(host)) != NULL) { /* find done entry */ 108072d39feaSAlan Cox if (scb->tastat == INI_QUEUE_FULL) { 108172d39feaSAlan Cox host->max_tags[scb->target] = 108272d39feaSAlan Cox host->act_tags[scb->target] - 1; 108372d39feaSAlan Cox scb->tastat = 0; 108472d39feaSAlan Cox initio_append_pend_scb(host, scb); 10851da177e4SLinus Torvalds continue; 10861da177e4SLinus Torvalds } 108772d39feaSAlan Cox if (!(scb->mode & SCM_RSENS)) { /* not in auto req. sense mode */ 108872d39feaSAlan Cox if (scb->tastat == 2) { 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds /* clr sync. nego flag */ 10911da177e4SLinus Torvalds 109272d39feaSAlan Cox if (scb->flags & SCF_SENSE) { 109372d39feaSAlan Cox u8 len; 109472d39feaSAlan Cox len = scb->senselen; 10951da177e4SLinus Torvalds if (len == 0) 10961da177e4SLinus Torvalds len = 1; 109772d39feaSAlan Cox scb->buflen = scb->senselen; 109872d39feaSAlan Cox scb->bufptr = scb->senseptr; 109972d39feaSAlan Cox scb->flags &= ~(SCF_SG | SCF_DIR); /* for xfer_data_in */ 110072d39feaSAlan Cox /* so, we won't report wrong direction in xfer_data_in, 11011da177e4SLinus Torvalds and won't report HOST_DO_DU in state_6 */ 110272d39feaSAlan Cox scb->mode = SCM_RSENS; 110372d39feaSAlan Cox scb->ident &= 0xBF; /* Disable Disconnect */ 110472d39feaSAlan Cox scb->tagmsg = 0; 110572d39feaSAlan Cox scb->tastat = 0; 110672d39feaSAlan Cox scb->cdblen = 6; 110772d39feaSAlan Cox scb->cdb[0] = SCSICMD_RequestSense; 110872d39feaSAlan Cox scb->cdb[1] = 0; 110972d39feaSAlan Cox scb->cdb[2] = 0; 111072d39feaSAlan Cox scb->cdb[3] = 0; 111172d39feaSAlan Cox scb->cdb[4] = len; 111272d39feaSAlan Cox scb->cdb[5] = 0; 111372d39feaSAlan Cox initio_push_pend_scb(host, scb); 11141da177e4SLinus Torvalds break; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds } else { /* in request sense mode */ 11181da177e4SLinus Torvalds 111972d39feaSAlan Cox if (scb->tastat == 2) { /* check contition status again after sending 11201da177e4SLinus Torvalds requset sense cmd 0x3 */ 112172d39feaSAlan Cox scb->hastat = HOST_BAD_PHAS; 11221da177e4SLinus Torvalds } 112372d39feaSAlan Cox scb->tastat = 2; 11241da177e4SLinus Torvalds } 112572d39feaSAlan Cox scb->flags |= SCF_DONE; 112672d39feaSAlan Cox if (scb->flags & SCF_POST) { 112772d39feaSAlan Cox /* FIXME: only one post method and lose casts */ 112872d39feaSAlan Cox (*scb->post) ((u8 *) host, (u8 *) scb); 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds } /* while */ 11311da177e4SLinus Torvalds /* find_active: */ 113272d39feaSAlan Cox if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING) 11331da177e4SLinus Torvalds continue; 113472d39feaSAlan Cox if (host->active) /* return to OS and wait for xfer_done_ISR/Selected_ISR */ 11351da177e4SLinus Torvalds return 1; /* return to OS, enable interrupt */ 11361da177e4SLinus Torvalds /* Check pending SCB */ 113772d39feaSAlan Cox if (initio_find_first_pend_scb(host) == NULL) 11381da177e4SLinus Torvalds return 1; /* return to OS, enable interrupt */ 11391da177e4SLinus Torvalds } /* End of for loop */ 11401da177e4SLinus Torvalds /* statement won't reach here */ 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 114372d39feaSAlan Cox static void tulip_scsi(struct initio_host * host) 11441da177e4SLinus Torvalds { 114572d39feaSAlan Cox struct scsi_ctrl_blk *scb; 114672d39feaSAlan Cox struct target_control *active_tc; 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds /* make sure to service interrupt asap */ 114972d39feaSAlan Cox if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) { 115072d39feaSAlan Cox host->phase = host->jsstatus0 & TSS_PH_MASK; 115172d39feaSAlan Cox host->jsstatus1 = inb(host->addr + TUL_SStatus1); 115272d39feaSAlan Cox host->jsint = inb(host->addr + TUL_SInt); 115372d39feaSAlan Cox if (host->jsint & TSS_SCSIRST_INT) { /* SCSI bus reset detected */ 115472d39feaSAlan Cox int_initio_scsi_rst(host); 11551da177e4SLinus Torvalds return; 11561da177e4SLinus Torvalds } 115772d39feaSAlan Cox if (host->jsint & TSS_RESEL_INT) { /* if selected/reselected interrupt */ 115872d39feaSAlan Cox if (int_initio_resel(host) == 0) 115972d39feaSAlan Cox initio_next_state(host); 11601da177e4SLinus Torvalds return; 11611da177e4SLinus Torvalds } 116272d39feaSAlan Cox if (host->jsint & TSS_SEL_TIMEOUT) { 116372d39feaSAlan Cox int_initio_busfree(host); 11641da177e4SLinus Torvalds return; 11651da177e4SLinus Torvalds } 116672d39feaSAlan Cox if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */ 116772d39feaSAlan Cox int_initio_busfree(host); /* unexpected bus free or sel timeout */ 11681da177e4SLinus Torvalds return; 11691da177e4SLinus Torvalds } 117072d39feaSAlan Cox if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) { /* func complete or Bus service */ 117172d39feaSAlan Cox if ((scb = host->active) != NULL) 117272d39feaSAlan Cox initio_next_state(host); 11731da177e4SLinus Torvalds return; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds } 117672d39feaSAlan Cox if (host->active != NULL) 11771da177e4SLinus Torvalds return; 11781da177e4SLinus Torvalds 117972d39feaSAlan Cox if ((scb = initio_find_first_pend_scb(host)) == NULL) 11801da177e4SLinus Torvalds return; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds /* program HBA's SCSI ID & target SCSI ID */ 118372d39feaSAlan Cox outb((host->scsi_id << 4) | (scb->target & 0x0F), 118472d39feaSAlan Cox host->addr + TUL_SScsiId); 118572d39feaSAlan Cox if (scb->opcode == ExecSCSI) { 118672d39feaSAlan Cox active_tc = &host->targets[scb->target]; 11871da177e4SLinus Torvalds 118872d39feaSAlan Cox if (scb->tagmsg) 118972d39feaSAlan Cox active_tc->drv_flags |= TCF_DRV_EN_TAG; 11901da177e4SLinus Torvalds else 119172d39feaSAlan Cox active_tc->drv_flags &= ~TCF_DRV_EN_TAG; 11921da177e4SLinus Torvalds 119372d39feaSAlan Cox outb(active_tc->js_period, host->addr + TUL_SPeriod); 119472d39feaSAlan Cox if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) { /* do wdtr negotiation */ 119572d39feaSAlan Cox initio_select_atn_stop(host, scb); 11961da177e4SLinus Torvalds } else { 119772d39feaSAlan Cox if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) { /* do sync negotiation */ 119872d39feaSAlan Cox initio_select_atn_stop(host, scb); 11991da177e4SLinus Torvalds } else { 120072d39feaSAlan Cox if (scb->tagmsg) 120172d39feaSAlan Cox initio_select_atn3(host, scb); 12021da177e4SLinus Torvalds else 120372d39feaSAlan Cox initio_select_atn(host, scb); 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds } 120672d39feaSAlan Cox if (scb->flags & SCF_POLL) { 120772d39feaSAlan Cox while (wait_tulip(host) != -1) { 120872d39feaSAlan Cox if (initio_next_state(host) == -1) 12091da177e4SLinus Torvalds break; 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds } 121272d39feaSAlan Cox } else if (scb->opcode == BusDevRst) { 121372d39feaSAlan Cox initio_select_atn_stop(host, scb); 121472d39feaSAlan Cox scb->next_state = 8; 121572d39feaSAlan Cox if (scb->flags & SCF_POLL) { 121672d39feaSAlan Cox while (wait_tulip(host) != -1) { 121772d39feaSAlan Cox if (initio_next_state(host) == -1) 12181da177e4SLinus Torvalds break; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds } 122172d39feaSAlan Cox } else if (scb->opcode == AbortCmd) { 122272d39feaSAlan Cox if (initio_abort_srb(host, scb->srb) != 0) { 122372d39feaSAlan Cox initio_unlink_pend_scb(host, scb); 122472d39feaSAlan Cox initio_release_scb(host, scb); 12251da177e4SLinus Torvalds } else { 122672d39feaSAlan Cox scb->opcode = BusDevRst; 122772d39feaSAlan Cox initio_select_atn_stop(host, scb); 122872d39feaSAlan Cox scb->next_state = 8; 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds } else { 123172d39feaSAlan Cox initio_unlink_pend_scb(host, scb); 123272d39feaSAlan Cox scb->hastat = 0x16; /* bad command */ 123372d39feaSAlan Cox initio_append_done_scb(host, scb); 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds return; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds 123872d39feaSAlan Cox /** 123972d39feaSAlan Cox * initio_next_state - Next SCSI state 124072d39feaSAlan Cox * @host: InitIO host we are processing 124172d39feaSAlan Cox * 124272d39feaSAlan Cox * Progress the active command block along the state machine 124372d39feaSAlan Cox * until we hit a state which we must wait for activity to occur. 124472d39feaSAlan Cox * 124572d39feaSAlan Cox * Returns zero or a negative code. 124672d39feaSAlan Cox */ 12471da177e4SLinus Torvalds 124872d39feaSAlan Cox static int initio_next_state(struct initio_host * host) 12491da177e4SLinus Torvalds { 12501da177e4SLinus Torvalds int next; 12511da177e4SLinus Torvalds 125272d39feaSAlan Cox next = host->active->next_state; 12531da177e4SLinus Torvalds for (;;) { 12541da177e4SLinus Torvalds switch (next) { 12551da177e4SLinus Torvalds case 1: 125672d39feaSAlan Cox next = initio_state_1(host); 12571da177e4SLinus Torvalds break; 12581da177e4SLinus Torvalds case 2: 125972d39feaSAlan Cox next = initio_state_2(host); 12601da177e4SLinus Torvalds break; 12611da177e4SLinus Torvalds case 3: 126272d39feaSAlan Cox next = initio_state_3(host); 12631da177e4SLinus Torvalds break; 12641da177e4SLinus Torvalds case 4: 126572d39feaSAlan Cox next = initio_state_4(host); 12661da177e4SLinus Torvalds break; 12671da177e4SLinus Torvalds case 5: 126872d39feaSAlan Cox next = initio_state_5(host); 12691da177e4SLinus Torvalds break; 12701da177e4SLinus Torvalds case 6: 127172d39feaSAlan Cox next = initio_state_6(host); 12721da177e4SLinus Torvalds break; 12731da177e4SLinus Torvalds case 7: 127472d39feaSAlan Cox next = initio_state_7(host); 12751da177e4SLinus Torvalds break; 12761da177e4SLinus Torvalds case 8: 127772d39feaSAlan Cox return initio_bus_device_reset(host); 12781da177e4SLinus Torvalds default: 127972d39feaSAlan Cox return initio_bad_seq(host); 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds if (next <= 0) 12821da177e4SLinus Torvalds return next; 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds 128772d39feaSAlan Cox /** 128872d39feaSAlan Cox * initio_state_1 - SCSI state machine 128972d39feaSAlan Cox * @host: InitIO host we are controlling 129072d39feaSAlan Cox * 129172d39feaSAlan Cox * Perform SCSI state processing for Select/Attention/Stop 129272d39feaSAlan Cox */ 129372d39feaSAlan Cox 129472d39feaSAlan Cox static int initio_state_1(struct initio_host * host) 12951da177e4SLinus Torvalds { 129672d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 129772d39feaSAlan Cox struct target_control *active_tc = host->active_tc; 12981da177e4SLinus Torvalds #if DEBUG_STATE 12991da177e4SLinus Torvalds printk("-s1-"); 13001da177e4SLinus Torvalds #endif 13011da177e4SLinus Torvalds 130272d39feaSAlan Cox /* Move the SCB from pending to busy */ 130372d39feaSAlan Cox initio_unlink_pend_scb(host, scb); 130472d39feaSAlan Cox initio_append_busy_scb(host, scb); 13051da177e4SLinus Torvalds 130672d39feaSAlan Cox outb(active_tc->sconfig0, host->addr + TUL_SConfig ); 13071da177e4SLinus Torvalds /* ATN on */ 130872d39feaSAlan Cox if (host->phase == MSG_OUT) { 130972d39feaSAlan Cox outb(TSC_EN_BUS_IN | TSC_HW_RESELECT, host->addr + TUL_SCtrl1); 131072d39feaSAlan Cox outb(scb->ident, host->addr + TUL_SFifo); 13111da177e4SLinus Torvalds 131272d39feaSAlan Cox if (scb->tagmsg) { 131372d39feaSAlan Cox outb(scb->tagmsg, host->addr + TUL_SFifo); 131472d39feaSAlan Cox outb(scb->tagid, host->addr + TUL_SFifo); 13151da177e4SLinus Torvalds } 131672d39feaSAlan Cox if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) { 131772d39feaSAlan Cox active_tc->flags |= TCF_WDTR_DONE; 131872d39feaSAlan Cox outb(MSG_EXTEND, host->addr + TUL_SFifo); 131972d39feaSAlan Cox outb(2, host->addr + TUL_SFifo); /* Extended msg length */ 132072d39feaSAlan Cox outb(3, host->addr + TUL_SFifo); /* Sync request */ 132172d39feaSAlan Cox outb(1, host->addr + TUL_SFifo); /* Start from 16 bits */ 132272d39feaSAlan Cox } else if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) { 132372d39feaSAlan Cox active_tc->flags |= TCF_SYNC_DONE; 132472d39feaSAlan Cox outb(MSG_EXTEND, host->addr + TUL_SFifo); 132572d39feaSAlan Cox outb(3, host->addr + TUL_SFifo); /* extended msg length */ 132672d39feaSAlan Cox outb(1, host->addr + TUL_SFifo); /* sync request */ 132772d39feaSAlan Cox outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo); 132872d39feaSAlan Cox outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */ 13291da177e4SLinus Torvalds } 133072d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 133172d39feaSAlan Cox if (wait_tulip(host) == -1) 133272d39feaSAlan Cox return -1; 13331da177e4SLinus Torvalds } 133472d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 133572d39feaSAlan Cox outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal); 133672d39feaSAlan Cox /* Into before CDB xfer */ 133772d39feaSAlan Cox return 3; 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds 134172d39feaSAlan Cox /** 134272d39feaSAlan Cox * initio_state_2 - SCSI state machine 134372d39feaSAlan Cox * @host: InitIO host we are controlling 134472d39feaSAlan Cox * 134572d39feaSAlan Cox * state after selection with attention 134672d39feaSAlan Cox * state after selection with attention3 134772d39feaSAlan Cox */ 134872d39feaSAlan Cox 134972d39feaSAlan Cox static int initio_state_2(struct initio_host * host) 13501da177e4SLinus Torvalds { 135172d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 135272d39feaSAlan Cox struct target_control *active_tc = host->active_tc; 13531da177e4SLinus Torvalds #if DEBUG_STATE 13541da177e4SLinus Torvalds printk("-s2-"); 13551da177e4SLinus Torvalds #endif 13561da177e4SLinus Torvalds 135772d39feaSAlan Cox initio_unlink_pend_scb(host, scb); 135872d39feaSAlan Cox initio_append_busy_scb(host, scb); 13591da177e4SLinus Torvalds 136072d39feaSAlan Cox outb(active_tc->sconfig0, host->addr + TUL_SConfig); 13611da177e4SLinus Torvalds 136272d39feaSAlan Cox if (host->jsstatus1 & TSS_CMD_PH_CMP) 136372d39feaSAlan Cox return 4; 136472d39feaSAlan Cox 136572d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 136672d39feaSAlan Cox outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal); 136772d39feaSAlan Cox /* Into before CDB xfer */ 136872d39feaSAlan Cox return 3; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 137172d39feaSAlan Cox /** 137272d39feaSAlan Cox * initio_state_3 - SCSI state machine 137372d39feaSAlan Cox * @host: InitIO host we are controlling 137472d39feaSAlan Cox * 137572d39feaSAlan Cox * state before CDB xfer is done 137672d39feaSAlan Cox */ 137772d39feaSAlan Cox 137872d39feaSAlan Cox static int initio_state_3(struct initio_host * host) 13791da177e4SLinus Torvalds { 138072d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 138172d39feaSAlan Cox struct target_control *active_tc = host->active_tc; 13821da177e4SLinus Torvalds int i; 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds #if DEBUG_STATE 13851da177e4SLinus Torvalds printk("-s3-"); 13861da177e4SLinus Torvalds #endif 13871da177e4SLinus Torvalds for (;;) { 138872d39feaSAlan Cox switch (host->phase) { 13891da177e4SLinus Torvalds case CMD_OUT: /* Command out phase */ 139072d39feaSAlan Cox for (i = 0; i < (int) scb->cdblen; i++) 139172d39feaSAlan Cox outb(scb->cdb[i], host->addr + TUL_SFifo); 139272d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 139372d39feaSAlan Cox if (wait_tulip(host) == -1) 139472d39feaSAlan Cox return -1; 139572d39feaSAlan Cox if (host->phase == CMD_OUT) 139672d39feaSAlan Cox return initio_bad_seq(host); 139772d39feaSAlan Cox return 4; 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds case MSG_IN: /* Message in phase */ 140072d39feaSAlan Cox scb->next_state = 3; 140172d39feaSAlan Cox if (initio_msgin(host) == -1) 140272d39feaSAlan Cox return -1; 14031da177e4SLinus Torvalds break; 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds case STATUS_IN: /* Status phase */ 140672d39feaSAlan Cox if (initio_status_msg(host) == -1) 140772d39feaSAlan Cox return -1; 14081da177e4SLinus Torvalds break; 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds case MSG_OUT: /* Message out phase */ 141172d39feaSAlan Cox if (active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) { 141272d39feaSAlan Cox outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */ 141372d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 141472d39feaSAlan Cox if (wait_tulip(host) == -1) 141572d39feaSAlan Cox return -1; 14161da177e4SLinus Torvalds } else { 141772d39feaSAlan Cox active_tc->flags |= TCF_SYNC_DONE; 14181da177e4SLinus Torvalds 141972d39feaSAlan Cox outb(MSG_EXTEND, host->addr + TUL_SFifo); 142072d39feaSAlan Cox outb(3, host->addr + TUL_SFifo); /* ext. msg len */ 142172d39feaSAlan Cox outb(1, host->addr + TUL_SFifo); /* sync request */ 142272d39feaSAlan Cox outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo); 142372d39feaSAlan Cox outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */ 142472d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 142572d39feaSAlan Cox if (wait_tulip(host) == -1) 142672d39feaSAlan Cox return -1; 142772d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 142872d39feaSAlan Cox outb(inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7), host->addr + TUL_SSignal); 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds } 14311da177e4SLinus Torvalds break; 14321da177e4SLinus Torvalds default: 143372d39feaSAlan Cox return initio_bad_seq(host); 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds 143872d39feaSAlan Cox /** 143972d39feaSAlan Cox * initio_state_4 - SCSI state machine 144072d39feaSAlan Cox * @host: InitIO host we are controlling 144172d39feaSAlan Cox * 144272d39feaSAlan Cox * SCSI state machine. State 4 144372d39feaSAlan Cox */ 14441da177e4SLinus Torvalds 144572d39feaSAlan Cox static int initio_state_4(struct initio_host * host) 14461da177e4SLinus Torvalds { 144772d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds #if DEBUG_STATE 14501da177e4SLinus Torvalds printk("-s4-"); 14511da177e4SLinus Torvalds #endif 145272d39feaSAlan Cox if ((scb->flags & SCF_DIR) == SCF_NO_XF) { 145372d39feaSAlan Cox return 6; /* Go to state 6 (After data) */ 14541da177e4SLinus Torvalds } 14551da177e4SLinus Torvalds for (;;) { 145672d39feaSAlan Cox if (scb->buflen == 0) 145772d39feaSAlan Cox return 6; 14581da177e4SLinus Torvalds 145972d39feaSAlan Cox switch (host->phase) { 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds case STATUS_IN: /* Status phase */ 146272d39feaSAlan Cox if ((scb->flags & SCF_DIR) != 0) /* if direction bit set then report data underrun */ 146372d39feaSAlan Cox scb->hastat = HOST_DO_DU; 146472d39feaSAlan Cox if ((initio_status_msg(host)) == -1) 146572d39feaSAlan Cox return -1; 14661da177e4SLinus Torvalds break; 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds case MSG_IN: /* Message in phase */ 146972d39feaSAlan Cox scb->next_state = 0x4; 147072d39feaSAlan Cox if (initio_msgin(host) == -1) 147172d39feaSAlan Cox return -1; 14721da177e4SLinus Torvalds break; 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds case MSG_OUT: /* Message out phase */ 147572d39feaSAlan Cox if (host->jsstatus0 & TSS_PAR_ERROR) { 147672d39feaSAlan Cox scb->buflen = 0; 147772d39feaSAlan Cox scb->hastat = HOST_DO_DU; 147872d39feaSAlan Cox if (initio_msgout_ide(host) == -1) 147972d39feaSAlan Cox return -1; 148072d39feaSAlan Cox return 6; 14811da177e4SLinus Torvalds } else { 148272d39feaSAlan Cox outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */ 148372d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 148472d39feaSAlan Cox if (wait_tulip(host) == -1) 148572d39feaSAlan Cox return -1; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds break; 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds case DATA_IN: /* Data in phase */ 149072d39feaSAlan Cox return initio_xfer_data_in(host); 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds case DATA_OUT: /* Data out phase */ 149372d39feaSAlan Cox return initio_xfer_data_out(host); 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds default: 149672d39feaSAlan Cox return initio_bad_seq(host); 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds 150272d39feaSAlan Cox /** 150372d39feaSAlan Cox * initio_state_5 - SCSI state machine 150472d39feaSAlan Cox * @host: InitIO host we are controlling 150572d39feaSAlan Cox * 150672d39feaSAlan Cox * State after dma xfer done or phase change before xfer done 150772d39feaSAlan Cox */ 150872d39feaSAlan Cox 150972d39feaSAlan Cox static int initio_state_5(struct initio_host * host) 15101da177e4SLinus Torvalds { 151172d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 15121da177e4SLinus Torvalds long cnt, xcnt; /* cannot use unsigned !! code: if (xcnt < 0) */ 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds #if DEBUG_STATE 15151da177e4SLinus Torvalds printk("-s5-"); 15161da177e4SLinus Torvalds #endif 15171da177e4SLinus Torvalds /*------ get remaining count -------*/ 151872d39feaSAlan Cox cnt = inl(host->addr + TUL_SCnt0) & 0x0FFFFFF; 15191da177e4SLinus Torvalds 152072d39feaSAlan Cox if (inb(host->addr + TUL_XCmd) & 0x20) { 15211da177e4SLinus Torvalds /* ----------------------- DATA_IN ----------------------------- */ 15221da177e4SLinus Torvalds /* check scsi parity error */ 152372d39feaSAlan Cox if (host->jsstatus0 & TSS_PAR_ERROR) 152472d39feaSAlan Cox scb->hastat = HOST_DO_DU; 152572d39feaSAlan Cox if (inb(host->addr + TUL_XStatus) & XPEND) { /* DMA xfer pending, Send STOP */ 15261da177e4SLinus Torvalds /* tell Hardware scsi xfer has been terminated */ 152772d39feaSAlan Cox outb(inb(host->addr + TUL_XCtrl) | 0x80, host->addr + TUL_XCtrl); 15281da177e4SLinus Torvalds /* wait until DMA xfer not pending */ 152972d39feaSAlan Cox while (inb(host->addr + TUL_XStatus) & XPEND) 153072d39feaSAlan Cox cpu_relax(); 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds } else { 15331da177e4SLinus Torvalds /*-------- DATA OUT -----------*/ 153472d39feaSAlan Cox if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) { 153572d39feaSAlan Cox if (host->active_tc->js_period & TSC_WIDE_SCSI) 153672d39feaSAlan Cox cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F) << 1; 15371da177e4SLinus Torvalds else 153872d39feaSAlan Cox cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F); 15391da177e4SLinus Torvalds } 154072d39feaSAlan Cox if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */ 154172d39feaSAlan Cox outb(TAX_X_ABT, host->addr + TUL_XCmd); 15421da177e4SLinus Torvalds /* wait Abort DMA xfer done */ 154372d39feaSAlan Cox while ((inb(host->addr + TUL_Int) & XABT) == 0) 154472d39feaSAlan Cox cpu_relax(); 15451da177e4SLinus Torvalds } 154672d39feaSAlan Cox if ((cnt == 1) && (host->phase == DATA_OUT)) { 154772d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 154872d39feaSAlan Cox if (wait_tulip(host) == -1) 154972d39feaSAlan Cox return -1; 15501da177e4SLinus Torvalds cnt = 0; 15511da177e4SLinus Torvalds } else { 155272d39feaSAlan Cox if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) 155372d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 15541da177e4SLinus Torvalds } 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds if (cnt == 0) { 155772d39feaSAlan Cox scb->buflen = 0; 155872d39feaSAlan Cox return 6; /* After Data */ 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds /* Update active data pointer */ 156172d39feaSAlan Cox xcnt = (long) scb->buflen - cnt; /* xcnt== bytes already xferred */ 156272d39feaSAlan Cox scb->buflen = (u32) cnt; /* cnt == bytes left to be xferred */ 156372d39feaSAlan Cox if (scb->flags & SCF_SG) { 156472d39feaSAlan Cox struct sg_entry *sgp; 156572d39feaSAlan Cox unsigned long i; 15661da177e4SLinus Torvalds 156772d39feaSAlan Cox sgp = &scb->sglist[scb->sgidx]; 156872d39feaSAlan Cox for (i = scb->sgidx; i < scb->sgmax; sgp++, i++) { 156972d39feaSAlan Cox xcnt -= (long) sgp->len; 15701da177e4SLinus Torvalds if (xcnt < 0) { /* this sgp xfer half done */ 157172d39feaSAlan Cox xcnt += (long) sgp->len; /* xcnt == bytes xferred in this sgp */ 157272d39feaSAlan Cox sgp->data += (u32) xcnt; /* new ptr to be xfer */ 157372d39feaSAlan Cox sgp->len -= (u32) xcnt; /* new len to be xfer */ 157472d39feaSAlan Cox scb->bufptr += ((u32) (i - scb->sgidx) << 3); 15751da177e4SLinus Torvalds /* new SG table ptr */ 157672d39feaSAlan Cox scb->sglen = (u8) (scb->sgmax - i); 15771da177e4SLinus Torvalds /* new SG table len */ 157872d39feaSAlan Cox scb->sgidx = (u16) i; 15791da177e4SLinus Torvalds /* for next disc and come in this loop */ 158072d39feaSAlan Cox return 4; /* Go to state 4 */ 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds /* else (xcnt >= 0 , i.e. this sgp already xferred */ 15831da177e4SLinus Torvalds } /* for */ 158472d39feaSAlan Cox return 6; /* Go to state 6 */ 15851da177e4SLinus Torvalds } else { 158672d39feaSAlan Cox scb->bufptr += (u32) xcnt; 15871da177e4SLinus Torvalds } 158872d39feaSAlan Cox return 4; /* Go to state 4 */ 15891da177e4SLinus Torvalds } 15901da177e4SLinus Torvalds 159172d39feaSAlan Cox /** 159272d39feaSAlan Cox * initio_state_6 - SCSI state machine 159372d39feaSAlan Cox * @host: InitIO host we are controlling 159472d39feaSAlan Cox * 159572d39feaSAlan Cox * State after Data phase 159672d39feaSAlan Cox */ 159772d39feaSAlan Cox 159872d39feaSAlan Cox static int initio_state_6(struct initio_host * host) 15991da177e4SLinus Torvalds { 160072d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds #if DEBUG_STATE 16031da177e4SLinus Torvalds printk("-s6-"); 16041da177e4SLinus Torvalds #endif 16051da177e4SLinus Torvalds for (;;) { 160672d39feaSAlan Cox switch (host->phase) { 16071da177e4SLinus Torvalds case STATUS_IN: /* Status phase */ 160872d39feaSAlan Cox if ((initio_status_msg(host)) == -1) 160972d39feaSAlan Cox return -1; 16101da177e4SLinus Torvalds break; 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds case MSG_IN: /* Message in phase */ 161372d39feaSAlan Cox scb->next_state = 6; 161472d39feaSAlan Cox if ((initio_msgin(host)) == -1) 161572d39feaSAlan Cox return -1; 16161da177e4SLinus Torvalds break; 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds case MSG_OUT: /* Message out phase */ 161972d39feaSAlan Cox outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */ 162072d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 162172d39feaSAlan Cox if (wait_tulip(host) == -1) 162272d39feaSAlan Cox return -1; 16231da177e4SLinus Torvalds break; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds case DATA_IN: /* Data in phase */ 162672d39feaSAlan Cox return initio_xpad_in(host); 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds case DATA_OUT: /* Data out phase */ 162972d39feaSAlan Cox return initio_xpad_out(host); 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds default: 163272d39feaSAlan Cox return initio_bad_seq(host); 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds } 16361da177e4SLinus Torvalds 163772d39feaSAlan Cox /** 163872d39feaSAlan Cox * initio_state_7 - SCSI state machine 163972d39feaSAlan Cox * @host: InitIO host we are controlling 164072d39feaSAlan Cox * 164172d39feaSAlan Cox */ 164272d39feaSAlan Cox 16430c3dbdebSChen Zhou static int initio_state_7(struct initio_host * host) 16441da177e4SLinus Torvalds { 16451da177e4SLinus Torvalds int cnt, i; 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds #if DEBUG_STATE 16481da177e4SLinus Torvalds printk("-s7-"); 16491da177e4SLinus Torvalds #endif 16501da177e4SLinus Torvalds /* flush SCSI FIFO */ 165172d39feaSAlan Cox cnt = inb(host->addr + TUL_SFifoCnt) & 0x1F; 16521da177e4SLinus Torvalds if (cnt) { 16531da177e4SLinus Torvalds for (i = 0; i < cnt; i++) 165472d39feaSAlan Cox inb(host->addr + TUL_SFifo); 16551da177e4SLinus Torvalds } 165672d39feaSAlan Cox switch (host->phase) { 16571da177e4SLinus Torvalds case DATA_IN: /* Data in phase */ 16581da177e4SLinus Torvalds case DATA_OUT: /* Data out phase */ 165972d39feaSAlan Cox return initio_bad_seq(host); 16601da177e4SLinus Torvalds default: 166172d39feaSAlan Cox return 6; /* Go to state 6 */ 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds } 16641da177e4SLinus Torvalds 166572d39feaSAlan Cox /** 166672d39feaSAlan Cox * initio_xfer_data_in - Commence data input 166772d39feaSAlan Cox * @host: InitIO host in use 166872d39feaSAlan Cox * 166972d39feaSAlan Cox * Commence a block of data transfer. The transfer itself will 167072d39feaSAlan Cox * be managed by the controller and we will get a completion (or 167172d39feaSAlan Cox * failure) interrupt. 167272d39feaSAlan Cox */ 167372d39feaSAlan Cox static int initio_xfer_data_in(struct initio_host * host) 16741da177e4SLinus Torvalds { 167572d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 16761da177e4SLinus Torvalds 167772d39feaSAlan Cox if ((scb->flags & SCF_DIR) == SCF_DOUT) 167872d39feaSAlan Cox return 6; /* wrong direction */ 16791da177e4SLinus Torvalds 168072d39feaSAlan Cox outl(scb->buflen, host->addr + TUL_SCnt0); 168172d39feaSAlan Cox outb(TSC_XF_DMA_IN, host->addr + TUL_SCmd); /* 7/25/95 */ 16821da177e4SLinus Torvalds 168372d39feaSAlan Cox if (scb->flags & SCF_SG) { /* S/G xfer */ 168472d39feaSAlan Cox outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH); 168572d39feaSAlan Cox outl(scb->bufptr, host->addr + TUL_XAddH); 168672d39feaSAlan Cox outb(TAX_SG_IN, host->addr + TUL_XCmd); 16871da177e4SLinus Torvalds } else { 168872d39feaSAlan Cox outl(scb->buflen, host->addr + TUL_XCntH); 168972d39feaSAlan Cox outl(scb->bufptr, host->addr + TUL_XAddH); 169072d39feaSAlan Cox outb(TAX_X_IN, host->addr + TUL_XCmd); 16911da177e4SLinus Torvalds } 169272d39feaSAlan Cox scb->next_state = 0x5; 169372d39feaSAlan Cox return 0; /* return to OS, wait xfer done , let jas_isr come in */ 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 169672d39feaSAlan Cox /** 169772d39feaSAlan Cox * initio_xfer_data_out - Commence data output 169872d39feaSAlan Cox * @host: InitIO host in use 169972d39feaSAlan Cox * 170072d39feaSAlan Cox * Commence a block of data transfer. The transfer itself will 170172d39feaSAlan Cox * be managed by the controller and we will get a completion (or 170272d39feaSAlan Cox * failure) interrupt. 170372d39feaSAlan Cox */ 17041da177e4SLinus Torvalds 170572d39feaSAlan Cox static int initio_xfer_data_out(struct initio_host * host) 17061da177e4SLinus Torvalds { 170772d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 17081da177e4SLinus Torvalds 170972d39feaSAlan Cox if ((scb->flags & SCF_DIR) == SCF_DIN) 171072d39feaSAlan Cox return 6; /* wrong direction */ 17111da177e4SLinus Torvalds 171272d39feaSAlan Cox outl(scb->buflen, host->addr + TUL_SCnt0); 171372d39feaSAlan Cox outb(TSC_XF_DMA_OUT, host->addr + TUL_SCmd); 171472d39feaSAlan Cox 171572d39feaSAlan Cox if (scb->flags & SCF_SG) { /* S/G xfer */ 171672d39feaSAlan Cox outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH); 171772d39feaSAlan Cox outl(scb->bufptr, host->addr + TUL_XAddH); 171872d39feaSAlan Cox outb(TAX_SG_OUT, host->addr + TUL_XCmd); 17191da177e4SLinus Torvalds } else { 172072d39feaSAlan Cox outl(scb->buflen, host->addr + TUL_XCntH); 172172d39feaSAlan Cox outl(scb->bufptr, host->addr + TUL_XAddH); 172272d39feaSAlan Cox outb(TAX_X_OUT, host->addr + TUL_XCmd); 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds 172572d39feaSAlan Cox scb->next_state = 0x5; 172672d39feaSAlan Cox return 0; /* return to OS, wait xfer done , let jas_isr come in */ 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 172972d39feaSAlan Cox int initio_xpad_in(struct initio_host * host) 17301da177e4SLinus Torvalds { 173172d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 173272d39feaSAlan Cox struct target_control *active_tc = host->active_tc; 17331da177e4SLinus Torvalds 173472d39feaSAlan Cox if ((scb->flags & SCF_DIR) != SCF_NO_DCHK) 173572d39feaSAlan Cox scb->hastat = HOST_DO_DU; /* over run */ 17361da177e4SLinus Torvalds for (;;) { 173772d39feaSAlan Cox if (active_tc->js_period & TSC_WIDE_SCSI) 173872d39feaSAlan Cox outl(2, host->addr + TUL_SCnt0); 17391da177e4SLinus Torvalds else 174072d39feaSAlan Cox outl(1, host->addr + TUL_SCnt0); 17411da177e4SLinus Torvalds 174272d39feaSAlan Cox outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd); 174372d39feaSAlan Cox if (wait_tulip(host) == -1) 174472d39feaSAlan Cox return -1; 174572d39feaSAlan Cox if (host->phase != DATA_IN) { 174672d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 174772d39feaSAlan Cox return 6; 17481da177e4SLinus Torvalds } 174972d39feaSAlan Cox inb(host->addr + TUL_SFifo); 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 175372d39feaSAlan Cox int initio_xpad_out(struct initio_host * host) 17541da177e4SLinus Torvalds { 175572d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 175672d39feaSAlan Cox struct target_control *active_tc = host->active_tc; 17571da177e4SLinus Torvalds 175872d39feaSAlan Cox if ((scb->flags & SCF_DIR) != SCF_NO_DCHK) 175972d39feaSAlan Cox scb->hastat = HOST_DO_DU; /* over run */ 17601da177e4SLinus Torvalds for (;;) { 176172d39feaSAlan Cox if (active_tc->js_period & TSC_WIDE_SCSI) 176272d39feaSAlan Cox outl(2, host->addr + TUL_SCnt0); 17631da177e4SLinus Torvalds else 176472d39feaSAlan Cox outl(1, host->addr + TUL_SCnt0); 17651da177e4SLinus Torvalds 176672d39feaSAlan Cox outb(0, host->addr + TUL_SFifo); 176772d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 176872d39feaSAlan Cox if ((wait_tulip(host)) == -1) 176972d39feaSAlan Cox return -1; 177072d39feaSAlan Cox if (host->phase != DATA_OUT) { /* Disable wide CPU to allow read 16 bits */ 177172d39feaSAlan Cox outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); 177272d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 177372d39feaSAlan Cox return 6; 17741da177e4SLinus Torvalds } 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds } 17771da177e4SLinus Torvalds 177872d39feaSAlan Cox int initio_status_msg(struct initio_host * host) 17791da177e4SLinus Torvalds { /* status & MSG_IN */ 178072d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 178172d39feaSAlan Cox u8 msg; 17821da177e4SLinus Torvalds 178372d39feaSAlan Cox outb(TSC_CMD_COMP, host->addr + TUL_SCmd); 178472d39feaSAlan Cox if (wait_tulip(host) == -1) 178572d39feaSAlan Cox return -1; 178672d39feaSAlan Cox 17871da177e4SLinus Torvalds /* get status */ 178872d39feaSAlan Cox scb->tastat = inb(host->addr + TUL_SFifo); 17891da177e4SLinus Torvalds 179072d39feaSAlan Cox if (host->phase == MSG_OUT) { 179172d39feaSAlan Cox if (host->jsstatus0 & TSS_PAR_ERROR) 179272d39feaSAlan Cox outb(MSG_PARITY, host->addr + TUL_SFifo); 179372d39feaSAlan Cox else 179472d39feaSAlan Cox outb(MSG_NOP, host->addr + TUL_SFifo); 179572d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 179672d39feaSAlan Cox return wait_tulip(host); 17971da177e4SLinus Torvalds } 179872d39feaSAlan Cox if (host->phase == MSG_IN) { 179972d39feaSAlan Cox msg = inb(host->addr + TUL_SFifo); 180072d39feaSAlan Cox if (host->jsstatus0 & TSS_PAR_ERROR) { /* Parity error */ 180172d39feaSAlan Cox if ((initio_msgin_accept(host)) == -1) 180272d39feaSAlan Cox return -1; 180372d39feaSAlan Cox if (host->phase != MSG_OUT) 180472d39feaSAlan Cox return initio_bad_seq(host); 180572d39feaSAlan Cox outb(MSG_PARITY, host->addr + TUL_SFifo); 180672d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 180772d39feaSAlan Cox return wait_tulip(host); 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds if (msg == 0) { /* Command complete */ 18101da177e4SLinus Torvalds 181172d39feaSAlan Cox if ((scb->tastat & 0x18) == 0x10) /* No link support */ 181272d39feaSAlan Cox return initio_bad_seq(host); 181372d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 181472d39feaSAlan Cox outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd); 181572d39feaSAlan Cox return initio_wait_done_disc(host); 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds } 181872d39feaSAlan Cox if (msg == MSG_LINK_COMP || msg == MSG_LINK_FLAG) { 181972d39feaSAlan Cox if ((scb->tastat & 0x18) == 0x10) 182072d39feaSAlan Cox return initio_msgin_accept(host); 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds } 182372d39feaSAlan Cox return initio_bad_seq(host); 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds /* scsi bus free */ 182872d39feaSAlan Cox int int_initio_busfree(struct initio_host * host) 18291da177e4SLinus Torvalds { 183072d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 18311da177e4SLinus Torvalds 183272d39feaSAlan Cox if (scb != NULL) { 183372d39feaSAlan Cox if (scb->status & SCB_SELECT) { /* selection timeout */ 183472d39feaSAlan Cox initio_unlink_pend_scb(host, scb); 183572d39feaSAlan Cox scb->hastat = HOST_SEL_TOUT; 183672d39feaSAlan Cox initio_append_done_scb(host, scb); 18371da177e4SLinus Torvalds } else { /* Unexpected bus free */ 183872d39feaSAlan Cox initio_unlink_busy_scb(host, scb); 183972d39feaSAlan Cox scb->hastat = HOST_BUS_FREE; 184072d39feaSAlan Cox initio_append_done_scb(host, scb); 18411da177e4SLinus Torvalds } 184272d39feaSAlan Cox host->active = NULL; 184372d39feaSAlan Cox host->active_tc = NULL; 18441da177e4SLinus Torvalds } 184572d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */ 184672d39feaSAlan Cox outb(TSC_INITDEFAULT, host->addr + TUL_SConfig); 184772d39feaSAlan Cox outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */ 184872d39feaSAlan Cox return -1; 18491da177e4SLinus Torvalds } 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds 185272d39feaSAlan Cox /** 185372d39feaSAlan Cox * int_initio_scsi_rst - SCSI reset occurred 185472d39feaSAlan Cox * @host: Host seeing the reset 185572d39feaSAlan Cox * 185672d39feaSAlan Cox * A SCSI bus reset has occurred. Clean up any pending transfer 185772d39feaSAlan Cox * the hardware is doing by DMA and then abort all active and 185872d39feaSAlan Cox * disconnected commands. The mid layer should sort the rest out 185972d39feaSAlan Cox * for us 186072d39feaSAlan Cox */ 186172d39feaSAlan Cox 186272d39feaSAlan Cox static int int_initio_scsi_rst(struct initio_host * host) 18631da177e4SLinus Torvalds { 186472d39feaSAlan Cox struct scsi_ctrl_blk *scb; 18651da177e4SLinus Torvalds int i; 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /* if DMA xfer is pending, abort DMA xfer */ 186872d39feaSAlan Cox if (inb(host->addr + TUL_XStatus) & 0x01) { 186972d39feaSAlan Cox outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd); 18701da177e4SLinus Torvalds /* wait Abort DMA xfer done */ 187172d39feaSAlan Cox while ((inb(host->addr + TUL_Int) & 0x04) == 0) 187272d39feaSAlan Cox cpu_relax(); 187372d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 18741da177e4SLinus Torvalds } 18751da177e4SLinus Torvalds /* Abort all active & disconnected scb */ 187672d39feaSAlan Cox while ((scb = initio_pop_busy_scb(host)) != NULL) { 187772d39feaSAlan Cox scb->hastat = HOST_BAD_PHAS; 187872d39feaSAlan Cox initio_append_done_scb(host, scb); 18791da177e4SLinus Torvalds } 188072d39feaSAlan Cox host->active = NULL; 188172d39feaSAlan Cox host->active_tc = NULL; 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds /* clr sync nego. done flag */ 188472d39feaSAlan Cox for (i = 0; i < host->max_tar; i++) 188572d39feaSAlan Cox host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE); 188672d39feaSAlan Cox return -1; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds 188972d39feaSAlan Cox /** 189025985edcSLucas De Marchi * int_initio_scsi_resel - Reselection occurred 189172d39feaSAlan Cox * @host: InitIO host adapter 189272d39feaSAlan Cox * 189372d39feaSAlan Cox * A SCSI reselection event has been signalled and the interrupt 189472d39feaSAlan Cox * is now being processed. Work out which command block needs attention 189572d39feaSAlan Cox * and continue processing that command. 189672d39feaSAlan Cox */ 18971da177e4SLinus Torvalds 189872d39feaSAlan Cox int int_initio_resel(struct initio_host * host) 18991da177e4SLinus Torvalds { 190072d39feaSAlan Cox struct scsi_ctrl_blk *scb; 190172d39feaSAlan Cox struct target_control *active_tc; 190272d39feaSAlan Cox u8 tag, msg = 0; 190372d39feaSAlan Cox u8 tar, lun; 19041da177e4SLinus Torvalds 190572d39feaSAlan Cox if ((scb = host->active) != NULL) { 190672d39feaSAlan Cox /* FIXME: Why check and not just clear ? */ 190772d39feaSAlan Cox if (scb->status & SCB_SELECT) /* if waiting for selection complete */ 190872d39feaSAlan Cox scb->status &= ~SCB_SELECT; 190972d39feaSAlan Cox host->active = NULL; 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds /* --------- get target id---------------------- */ 191272d39feaSAlan Cox tar = inb(host->addr + TUL_SBusId); 19131da177e4SLinus Torvalds /* ------ get LUN from Identify message----------- */ 191472d39feaSAlan Cox lun = inb(host->addr + TUL_SIdent) & 0x0F; 19151da177e4SLinus Torvalds /* 07/22/98 from 0x1F -> 0x0F */ 191672d39feaSAlan Cox active_tc = &host->targets[tar]; 191772d39feaSAlan Cox host->active_tc = active_tc; 191872d39feaSAlan Cox outb(active_tc->sconfig0, host->addr + TUL_SConfig); 191972d39feaSAlan Cox outb(active_tc->js_period, host->addr + TUL_SPeriod); 19201da177e4SLinus Torvalds 19211da177e4SLinus Torvalds /* ------------- tag queueing ? ------------------- */ 192272d39feaSAlan Cox if (active_tc->drv_flags & TCF_DRV_EN_TAG) { 192372d39feaSAlan Cox if ((initio_msgin_accept(host)) == -1) 192472d39feaSAlan Cox return -1; 192572d39feaSAlan Cox if (host->phase != MSG_IN) 19261da177e4SLinus Torvalds goto no_tag; 192772d39feaSAlan Cox outl(1, host->addr + TUL_SCnt0); 192872d39feaSAlan Cox outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd); 192972d39feaSAlan Cox if (wait_tulip(host) == -1) 193072d39feaSAlan Cox return -1; 193172d39feaSAlan Cox msg = inb(host->addr + TUL_SFifo); /* Read Tag Message */ 19321da177e4SLinus Torvalds 193372d39feaSAlan Cox if (msg < MSG_STAG || msg > MSG_OTAG) /* Is simple Tag */ 19341da177e4SLinus Torvalds goto no_tag; 19351da177e4SLinus Torvalds 193672d39feaSAlan Cox if (initio_msgin_accept(host) == -1) 193772d39feaSAlan Cox return -1; 19381da177e4SLinus Torvalds 193972d39feaSAlan Cox if (host->phase != MSG_IN) 19401da177e4SLinus Torvalds goto no_tag; 19411da177e4SLinus Torvalds 194272d39feaSAlan Cox outl(1, host->addr + TUL_SCnt0); 194372d39feaSAlan Cox outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd); 194472d39feaSAlan Cox if (wait_tulip(host) == -1) 194572d39feaSAlan Cox return -1; 194672d39feaSAlan Cox tag = inb(host->addr + TUL_SFifo); /* Read Tag ID */ 194772d39feaSAlan Cox scb = host->scb + tag; 194872d39feaSAlan Cox if (scb->target != tar || scb->lun != lun) { 194972d39feaSAlan Cox return initio_msgout_abort_tag(host); 19501da177e4SLinus Torvalds } 195172d39feaSAlan Cox if (scb->status != SCB_BUSY) { /* 03/24/95 */ 195272d39feaSAlan Cox return initio_msgout_abort_tag(host); 19531da177e4SLinus Torvalds } 195472d39feaSAlan Cox host->active = scb; 195572d39feaSAlan Cox if ((initio_msgin_accept(host)) == -1) 195672d39feaSAlan Cox return -1; 19571da177e4SLinus Torvalds } else { /* No tag */ 19581da177e4SLinus Torvalds no_tag: 195972d39feaSAlan Cox if ((scb = initio_find_busy_scb(host, tar | (lun << 8))) == NULL) { 196072d39feaSAlan Cox return initio_msgout_abort_targ(host); 19611da177e4SLinus Torvalds } 196272d39feaSAlan Cox host->active = scb; 196372d39feaSAlan Cox if (!(active_tc->drv_flags & TCF_DRV_EN_TAG)) { 196472d39feaSAlan Cox if ((initio_msgin_accept(host)) == -1) 196572d39feaSAlan Cox return -1; 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds return 0; 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds 197172d39feaSAlan Cox /** 197272d39feaSAlan Cox * int_initio_bad_seq - out of phase 197372d39feaSAlan Cox * @host: InitIO host flagging event 197472d39feaSAlan Cox * 197572d39feaSAlan Cox * We have ended up out of phase somehow. Reset the host controller 197672d39feaSAlan Cox * and throw all our toys out of the pram. Let the midlayer clean up 197772d39feaSAlan Cox */ 19781da177e4SLinus Torvalds 197972d39feaSAlan Cox static int int_initio_bad_seq(struct initio_host * host) 19801da177e4SLinus Torvalds { /* target wrong phase */ 198172d39feaSAlan Cox struct scsi_ctrl_blk *scb; 19821da177e4SLinus Torvalds int i; 19831da177e4SLinus Torvalds 198472d39feaSAlan Cox initio_reset_scsi(host, 10); 19851da177e4SLinus Torvalds 198672d39feaSAlan Cox while ((scb = initio_pop_busy_scb(host)) != NULL) { 198772d39feaSAlan Cox scb->hastat = HOST_BAD_PHAS; 198872d39feaSAlan Cox initio_append_done_scb(host, scb); 19891da177e4SLinus Torvalds } 199072d39feaSAlan Cox for (i = 0; i < host->max_tar; i++) 199172d39feaSAlan Cox host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE); 199272d39feaSAlan Cox return -1; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds 199672d39feaSAlan Cox /** 199772d39feaSAlan Cox * initio_msgout_abort_targ - abort a tag 199872d39feaSAlan Cox * @host: InitIO host 199972d39feaSAlan Cox * 200072d39feaSAlan Cox * Abort when the target/lun does not match or when our SCB is not 200172d39feaSAlan Cox * busy. Used by untagged commands. 200272d39feaSAlan Cox */ 200372d39feaSAlan Cox 200472d39feaSAlan Cox static int initio_msgout_abort_targ(struct initio_host * host) 20051da177e4SLinus Torvalds { 20061da177e4SLinus Torvalds 200772d39feaSAlan Cox outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal); 200872d39feaSAlan Cox if (initio_msgin_accept(host) == -1) 200972d39feaSAlan Cox return -1; 201072d39feaSAlan Cox if (host->phase != MSG_OUT) 201172d39feaSAlan Cox return initio_bad_seq(host); 20121da177e4SLinus Torvalds 201372d39feaSAlan Cox outb(MSG_ABORT, host->addr + TUL_SFifo); 201472d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 20151da177e4SLinus Torvalds 201672d39feaSAlan Cox return initio_wait_disc(host); 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds 201972d39feaSAlan Cox /** 202072d39feaSAlan Cox * initio_msgout_abort_tag - abort a tag 202172d39feaSAlan Cox * @host: InitIO host 202272d39feaSAlan Cox * 202372d39feaSAlan Cox * Abort when the target/lun does not match or when our SCB is not 202472d39feaSAlan Cox * busy. Used for tagged commands. 202572d39feaSAlan Cox */ 202672d39feaSAlan Cox 202772d39feaSAlan Cox static int initio_msgout_abort_tag(struct initio_host * host) 20281da177e4SLinus Torvalds { 20291da177e4SLinus Torvalds 203072d39feaSAlan Cox outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal); 203172d39feaSAlan Cox if (initio_msgin_accept(host) == -1) 203272d39feaSAlan Cox return -1; 203372d39feaSAlan Cox if (host->phase != MSG_OUT) 203472d39feaSAlan Cox return initio_bad_seq(host); 20351da177e4SLinus Torvalds 203672d39feaSAlan Cox outb(MSG_ABORT_TAG, host->addr + TUL_SFifo); 203772d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 20381da177e4SLinus Torvalds 203972d39feaSAlan Cox return initio_wait_disc(host); 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds 204372d39feaSAlan Cox /** 204472d39feaSAlan Cox * initio_msgin - Message in 204572d39feaSAlan Cox * @host: InitIO Host 204672d39feaSAlan Cox * 204772d39feaSAlan Cox * Process incoming message 204872d39feaSAlan Cox */ 204972d39feaSAlan Cox static int initio_msgin(struct initio_host * host) 20501da177e4SLinus Torvalds { 205172d39feaSAlan Cox struct target_control *active_tc; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds for (;;) { 205472d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 20551da177e4SLinus Torvalds 205672d39feaSAlan Cox outl(1, host->addr + TUL_SCnt0); 205772d39feaSAlan Cox outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd); 205872d39feaSAlan Cox if (wait_tulip(host) == -1) 205972d39feaSAlan Cox return -1; 20601da177e4SLinus Torvalds 206172d39feaSAlan Cox switch (inb(host->addr + TUL_SFifo)) { 20621da177e4SLinus Torvalds case MSG_DISC: /* Disconnect msg */ 206372d39feaSAlan Cox outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd); 206472d39feaSAlan Cox return initio_wait_disc(host); 20651da177e4SLinus Torvalds case MSG_SDP: 20661da177e4SLinus Torvalds case MSG_RESTORE: 20671da177e4SLinus Torvalds case MSG_NOP: 206872d39feaSAlan Cox initio_msgin_accept(host); 20691da177e4SLinus Torvalds break; 20701da177e4SLinus Torvalds case MSG_REJ: /* Clear ATN first */ 207172d39feaSAlan Cox outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), 207272d39feaSAlan Cox host->addr + TUL_SSignal); 207372d39feaSAlan Cox active_tc = host->active_tc; 207472d39feaSAlan Cox if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) /* do sync nego */ 207572d39feaSAlan Cox outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), 207672d39feaSAlan Cox host->addr + TUL_SSignal); 207772d39feaSAlan Cox initio_msgin_accept(host); 20781da177e4SLinus Torvalds break; 20791da177e4SLinus Torvalds case MSG_EXTEND: /* extended msg */ 208072d39feaSAlan Cox initio_msgin_extend(host); 20811da177e4SLinus Torvalds break; 20821da177e4SLinus Torvalds case MSG_IGNOREWIDE: 208372d39feaSAlan Cox initio_msgin_accept(host); 20841da177e4SLinus Torvalds break; 20851da177e4SLinus Torvalds case MSG_COMP: 208672d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 208772d39feaSAlan Cox outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd); 208872d39feaSAlan Cox return initio_wait_done_disc(host); 20891da177e4SLinus Torvalds default: 209072d39feaSAlan Cox initio_msgout_reject(host); 20911da177e4SLinus Torvalds break; 20921da177e4SLinus Torvalds } 209372d39feaSAlan Cox if (host->phase != MSG_IN) 209472d39feaSAlan Cox return host->phase; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds /* statement won't reach here */ 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 209972d39feaSAlan Cox static int initio_msgout_reject(struct initio_host * host) 21001da177e4SLinus Torvalds { 210172d39feaSAlan Cox outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal); 21021da177e4SLinus Torvalds 210372d39feaSAlan Cox if (initio_msgin_accept(host) == -1) 210472d39feaSAlan Cox return -1; 21051da177e4SLinus Torvalds 210672d39feaSAlan Cox if (host->phase == MSG_OUT) { 210772d39feaSAlan Cox outb(MSG_REJ, host->addr + TUL_SFifo); /* Msg reject */ 210872d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 210972d39feaSAlan Cox return wait_tulip(host); 21101da177e4SLinus Torvalds } 211172d39feaSAlan Cox return host->phase; 21121da177e4SLinus Torvalds } 21131da177e4SLinus Torvalds 211472d39feaSAlan Cox static int initio_msgout_ide(struct initio_host * host) 21151da177e4SLinus Torvalds { 211672d39feaSAlan Cox outb(MSG_IDE, host->addr + TUL_SFifo); /* Initiator Detected Error */ 211772d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 211872d39feaSAlan Cox return wait_tulip(host); 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds 212172d39feaSAlan Cox static int initio_msgin_extend(struct initio_host * host) 21221da177e4SLinus Torvalds { 212372d39feaSAlan Cox u8 len, idx; 21241da177e4SLinus Torvalds 212572d39feaSAlan Cox if (initio_msgin_accept(host) != MSG_IN) 212672d39feaSAlan Cox return host->phase; 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds /* Get extended msg length */ 212972d39feaSAlan Cox outl(1, host->addr + TUL_SCnt0); 213072d39feaSAlan Cox outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd); 213172d39feaSAlan Cox if (wait_tulip(host) == -1) 213272d39feaSAlan Cox return -1; 21331da177e4SLinus Torvalds 213472d39feaSAlan Cox len = inb(host->addr + TUL_SFifo); 213572d39feaSAlan Cox host->msg[0] = len; 21361da177e4SLinus Torvalds for (idx = 1; len != 0; len--) { 21371da177e4SLinus Torvalds 213872d39feaSAlan Cox if ((initio_msgin_accept(host)) != MSG_IN) 213972d39feaSAlan Cox return host->phase; 214072d39feaSAlan Cox outl(1, host->addr + TUL_SCnt0); 214172d39feaSAlan Cox outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd); 214272d39feaSAlan Cox if (wait_tulip(host) == -1) 214372d39feaSAlan Cox return -1; 214472d39feaSAlan Cox host->msg[idx++] = inb(host->addr + TUL_SFifo); 21451da177e4SLinus Torvalds } 214672d39feaSAlan Cox if (host->msg[1] == 1) { /* if it's synchronous data transfer request */ 214772d39feaSAlan Cox u8 r; 214872d39feaSAlan Cox if (host->msg[0] != 3) /* if length is not right */ 214972d39feaSAlan Cox return initio_msgout_reject(host); 215072d39feaSAlan Cox if (host->active_tc->flags & TCF_NO_SYNC_NEGO) { /* Set OFFSET=0 to do async, nego back */ 215172d39feaSAlan Cox host->msg[3] = 0; 21521da177e4SLinus Torvalds } else { 215372d39feaSAlan Cox if (initio_msgin_sync(host) == 0 && 215472d39feaSAlan Cox (host->active_tc->flags & TCF_SYNC_DONE)) { 215572d39feaSAlan Cox initio_sync_done(host); 215672d39feaSAlan Cox return initio_msgin_accept(host); 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds 216072d39feaSAlan Cox r = inb(host->addr + TUL_SSignal); 216172d39feaSAlan Cox outb((r & (TSC_SET_ACK | 7)) | TSC_SET_ATN, 216272d39feaSAlan Cox host->addr + TUL_SSignal); 216372d39feaSAlan Cox if (initio_msgin_accept(host) != MSG_OUT) 216472d39feaSAlan Cox return host->phase; 21651da177e4SLinus Torvalds /* sync msg out */ 216672d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); 21671da177e4SLinus Torvalds 216872d39feaSAlan Cox initio_sync_done(host); 21691da177e4SLinus Torvalds 217072d39feaSAlan Cox outb(MSG_EXTEND, host->addr + TUL_SFifo); 217172d39feaSAlan Cox outb(3, host->addr + TUL_SFifo); 217272d39feaSAlan Cox outb(1, host->addr + TUL_SFifo); 217372d39feaSAlan Cox outb(host->msg[2], host->addr + TUL_SFifo); 217472d39feaSAlan Cox outb(host->msg[3], host->addr + TUL_SFifo); 217572d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 217672d39feaSAlan Cox return wait_tulip(host); 21771da177e4SLinus Torvalds } 217872d39feaSAlan Cox if (host->msg[0] != 2 || host->msg[1] != 3) 217972d39feaSAlan Cox return initio_msgout_reject(host); 21801da177e4SLinus Torvalds /* if it's WIDE DATA XFER REQ */ 218172d39feaSAlan Cox if (host->active_tc->flags & TCF_NO_WDTR) { 218272d39feaSAlan Cox host->msg[2] = 0; 21831da177e4SLinus Torvalds } else { 218472d39feaSAlan Cox if (host->msg[2] > 2) /* > 32 bits */ 218572d39feaSAlan Cox return initio_msgout_reject(host); 218672d39feaSAlan Cox if (host->msg[2] == 2) { /* == 32 */ 218772d39feaSAlan Cox host->msg[2] = 1; 21881da177e4SLinus Torvalds } else { 218972d39feaSAlan Cox if ((host->active_tc->flags & TCF_NO_WDTR) == 0) { 219072d39feaSAlan Cox wdtr_done(host); 219172d39feaSAlan Cox if ((host->active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) 219272d39feaSAlan Cox outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal); 219372d39feaSAlan Cox return initio_msgin_accept(host); 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds } 21961da177e4SLinus Torvalds } 219772d39feaSAlan Cox outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal); 21981da177e4SLinus Torvalds 219972d39feaSAlan Cox if (initio_msgin_accept(host) != MSG_OUT) 220072d39feaSAlan Cox return host->phase; 22011da177e4SLinus Torvalds /* WDTR msg out */ 220272d39feaSAlan Cox outb(MSG_EXTEND, host->addr + TUL_SFifo); 220372d39feaSAlan Cox outb(2, host->addr + TUL_SFifo); 220472d39feaSAlan Cox outb(3, host->addr + TUL_SFifo); 220572d39feaSAlan Cox outb(host->msg[2], host->addr + TUL_SFifo); 220672d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 220772d39feaSAlan Cox return wait_tulip(host); 22081da177e4SLinus Torvalds } 22091da177e4SLinus Torvalds 221072d39feaSAlan Cox static int initio_msgin_sync(struct initio_host * host) 22111da177e4SLinus Torvalds { 22121da177e4SLinus Torvalds char default_period; 22131da177e4SLinus Torvalds 221472d39feaSAlan Cox default_period = initio_rate_tbl[host->active_tc->flags & TCF_SCSI_RATE]; 221572d39feaSAlan Cox if (host->msg[3] > MAX_OFFSET) { 221672d39feaSAlan Cox host->msg[3] = MAX_OFFSET; 221772d39feaSAlan Cox if (host->msg[2] < default_period) { 221872d39feaSAlan Cox host->msg[2] = default_period; 22191da177e4SLinus Torvalds return 1; 22201da177e4SLinus Torvalds } 222172d39feaSAlan Cox if (host->msg[2] >= 59) /* Change to async */ 222272d39feaSAlan Cox host->msg[3] = 0; 22231da177e4SLinus Torvalds return 1; 22241da177e4SLinus Torvalds } 22251da177e4SLinus Torvalds /* offset requests asynchronous transfers ? */ 222672d39feaSAlan Cox if (host->msg[3] == 0) { 22271da177e4SLinus Torvalds return 0; 22281da177e4SLinus Torvalds } 222972d39feaSAlan Cox if (host->msg[2] < default_period) { 223072d39feaSAlan Cox host->msg[2] = default_period; 22311da177e4SLinus Torvalds return 1; 22321da177e4SLinus Torvalds } 223372d39feaSAlan Cox if (host->msg[2] >= 59) { 223472d39feaSAlan Cox host->msg[3] = 0; 22351da177e4SLinus Torvalds return 1; 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds return 0; 22381da177e4SLinus Torvalds } 22391da177e4SLinus Torvalds 224072d39feaSAlan Cox static int wdtr_done(struct initio_host * host) 22411da177e4SLinus Torvalds { 224272d39feaSAlan Cox host->active_tc->flags &= ~TCF_SYNC_DONE; 224372d39feaSAlan Cox host->active_tc->flags |= TCF_WDTR_DONE; 22441da177e4SLinus Torvalds 224572d39feaSAlan Cox host->active_tc->js_period = 0; 224672d39feaSAlan Cox if (host->msg[2]) /* if 16 bit */ 224772d39feaSAlan Cox host->active_tc->js_period |= TSC_WIDE_SCSI; 224872d39feaSAlan Cox host->active_tc->sconfig0 &= ~TSC_ALT_PERIOD; 224972d39feaSAlan Cox outb(host->active_tc->sconfig0, host->addr + TUL_SConfig); 225072d39feaSAlan Cox outb(host->active_tc->js_period, host->addr + TUL_SPeriod); 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds return 1; 22531da177e4SLinus Torvalds } 22541da177e4SLinus Torvalds 225572d39feaSAlan Cox static int initio_sync_done(struct initio_host * host) 22561da177e4SLinus Torvalds { 22571da177e4SLinus Torvalds int i; 22581da177e4SLinus Torvalds 225972d39feaSAlan Cox host->active_tc->flags |= TCF_SYNC_DONE; 22601da177e4SLinus Torvalds 226172d39feaSAlan Cox if (host->msg[3]) { 226272d39feaSAlan Cox host->active_tc->js_period |= host->msg[3]; 22631da177e4SLinus Torvalds for (i = 0; i < 8; i++) { 226472d39feaSAlan Cox if (initio_rate_tbl[i] >= host->msg[2]) /* pick the big one */ 22651da177e4SLinus Torvalds break; 22661da177e4SLinus Torvalds } 226772d39feaSAlan Cox host->active_tc->js_period |= (i << 4); 226872d39feaSAlan Cox host->active_tc->sconfig0 |= TSC_ALT_PERIOD; 22691da177e4SLinus Torvalds } 227072d39feaSAlan Cox outb(host->active_tc->sconfig0, host->addr + TUL_SConfig); 227172d39feaSAlan Cox outb(host->active_tc->js_period, host->addr + TUL_SPeriod); 22721da177e4SLinus Torvalds 227372d39feaSAlan Cox return -1; 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds 227772d39feaSAlan Cox static int initio_post_scsi_rst(struct initio_host * host) 22781da177e4SLinus Torvalds { 227972d39feaSAlan Cox struct scsi_ctrl_blk *scb; 228072d39feaSAlan Cox struct target_control *active_tc; 22811da177e4SLinus Torvalds int i; 22821da177e4SLinus Torvalds 228372d39feaSAlan Cox host->active = NULL; 228472d39feaSAlan Cox host->active_tc = NULL; 228572d39feaSAlan Cox host->flags = 0; 22861da177e4SLinus Torvalds 228772d39feaSAlan Cox while ((scb = initio_pop_busy_scb(host)) != NULL) { 228872d39feaSAlan Cox scb->hastat = HOST_BAD_PHAS; 228972d39feaSAlan Cox initio_append_done_scb(host, scb); 22901da177e4SLinus Torvalds } 22911da177e4SLinus Torvalds /* clear sync done flag */ 229272d39feaSAlan Cox active_tc = &host->targets[0]; 229372d39feaSAlan Cox for (i = 0; i < host->max_tar; active_tc++, i++) { 229472d39feaSAlan Cox active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE); 22951da177e4SLinus Torvalds /* Initialize the sync. xfer register values to an asyn xfer */ 229672d39feaSAlan Cox active_tc->js_period = 0; 229772d39feaSAlan Cox active_tc->sconfig0 = host->sconf1; 229872d39feaSAlan Cox host->act_tags[0] = 0; /* 07/22/98 */ 229972d39feaSAlan Cox host->targets[i].flags &= ~TCF_BUSY; /* 07/22/98 */ 23001da177e4SLinus Torvalds } /* for */ 23011da177e4SLinus Torvalds 230272d39feaSAlan Cox return -1; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 230572d39feaSAlan Cox static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb) 23061da177e4SLinus Torvalds { 230772d39feaSAlan Cox scb->status |= SCB_SELECT; 230872d39feaSAlan Cox scb->next_state = 0x1; 230972d39feaSAlan Cox host->active = scb; 231072d39feaSAlan Cox host->active_tc = &host->targets[scb->target]; 231172d39feaSAlan Cox outb(TSC_SELATNSTOP, host->addr + TUL_SCmd); 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds 231572d39feaSAlan Cox static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb) 23161da177e4SLinus Torvalds { 23171da177e4SLinus Torvalds int i; 23181da177e4SLinus Torvalds 231972d39feaSAlan Cox scb->status |= SCB_SELECT; 232072d39feaSAlan Cox scb->next_state = 0x2; 23211da177e4SLinus Torvalds 232272d39feaSAlan Cox outb(scb->ident, host->addr + TUL_SFifo); 232372d39feaSAlan Cox for (i = 0; i < (int) scb->cdblen; i++) 232472d39feaSAlan Cox outb(scb->cdb[i], host->addr + TUL_SFifo); 232572d39feaSAlan Cox host->active_tc = &host->targets[scb->target]; 232672d39feaSAlan Cox host->active = scb; 232772d39feaSAlan Cox outb(TSC_SEL_ATN, host->addr + TUL_SCmd); 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 233072d39feaSAlan Cox static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb) 23311da177e4SLinus Torvalds { 23321da177e4SLinus Torvalds int i; 23331da177e4SLinus Torvalds 233472d39feaSAlan Cox scb->status |= SCB_SELECT; 233572d39feaSAlan Cox scb->next_state = 0x2; 23361da177e4SLinus Torvalds 233772d39feaSAlan Cox outb(scb->ident, host->addr + TUL_SFifo); 233872d39feaSAlan Cox outb(scb->tagmsg, host->addr + TUL_SFifo); 233972d39feaSAlan Cox outb(scb->tagid, host->addr + TUL_SFifo); 234072d39feaSAlan Cox for (i = 0; i < scb->cdblen; i++) 234172d39feaSAlan Cox outb(scb->cdb[i], host->addr + TUL_SFifo); 234272d39feaSAlan Cox host->active_tc = &host->targets[scb->target]; 234372d39feaSAlan Cox host->active = scb; 234472d39feaSAlan Cox outb(TSC_SEL_ATN3, host->addr + TUL_SCmd); 23451da177e4SLinus Torvalds } 23461da177e4SLinus Torvalds 234772d39feaSAlan Cox /** 234872d39feaSAlan Cox * initio_bus_device_reset - SCSI Bus Device Reset 234972d39feaSAlan Cox * @host: InitIO host to reset 235072d39feaSAlan Cox * 235172d39feaSAlan Cox * Perform a device reset and abort all pending SCBs for the 235272d39feaSAlan Cox * victim device 235372d39feaSAlan Cox */ 235472d39feaSAlan Cox int initio_bus_device_reset(struct initio_host * host) 23551da177e4SLinus Torvalds { 235672d39feaSAlan Cox struct scsi_ctrl_blk *scb = host->active; 235772d39feaSAlan Cox struct target_control *active_tc = host->active_tc; 235872d39feaSAlan Cox struct scsi_ctrl_blk *tmp, *prev; 235972d39feaSAlan Cox u8 tar; 23601da177e4SLinus Torvalds 236172d39feaSAlan Cox if (host->phase != MSG_OUT) 236272d39feaSAlan Cox return int_initio_bad_seq(host); /* Unexpected phase */ 236372d39feaSAlan Cox 236472d39feaSAlan Cox initio_unlink_pend_scb(host, scb); 236572d39feaSAlan Cox initio_release_scb(host, scb); 23661da177e4SLinus Torvalds 23671da177e4SLinus Torvalds 236872d39feaSAlan Cox tar = scb->target; /* target */ 236972d39feaSAlan Cox active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE | TCF_BUSY); 23701da177e4SLinus Torvalds /* clr sync. nego & WDTR flags 07/22/98 */ 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds /* abort all SCB with same target */ 237372d39feaSAlan Cox prev = tmp = host->first_busy; /* Check Busy queue */ 237472d39feaSAlan Cox while (tmp != NULL) { 237572d39feaSAlan Cox if (tmp->target == tar) { 23761da177e4SLinus Torvalds /* unlink it */ 237772d39feaSAlan Cox if (tmp == host->first_busy) { 237872d39feaSAlan Cox if ((host->first_busy = tmp->next) == NULL) 237972d39feaSAlan Cox host->last_busy = NULL; 23801da177e4SLinus Torvalds } else { 238172d39feaSAlan Cox prev->next = tmp->next; 238272d39feaSAlan Cox if (tmp == host->last_busy) 238372d39feaSAlan Cox host->last_busy = prev; 23841da177e4SLinus Torvalds } 238572d39feaSAlan Cox tmp->hastat = HOST_ABORTED; 238672d39feaSAlan Cox initio_append_done_scb(host, tmp); 23871da177e4SLinus Torvalds } 23881da177e4SLinus Torvalds /* Previous haven't change */ 23891da177e4SLinus Torvalds else { 239072d39feaSAlan Cox prev = tmp; 23911da177e4SLinus Torvalds } 239272d39feaSAlan Cox tmp = tmp->next; 23931da177e4SLinus Torvalds } 239472d39feaSAlan Cox outb(MSG_DEVRST, host->addr + TUL_SFifo); 239572d39feaSAlan Cox outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd); 239672d39feaSAlan Cox return initio_wait_disc(host); 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds } 23991da177e4SLinus Torvalds 240072d39feaSAlan Cox static int initio_msgin_accept(struct initio_host * host) 24011da177e4SLinus Torvalds { 240272d39feaSAlan Cox outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd); 240372d39feaSAlan Cox return wait_tulip(host); 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds 240672d39feaSAlan Cox static int wait_tulip(struct initio_host * host) 24071da177e4SLinus Torvalds { 24081da177e4SLinus Torvalds 240972d39feaSAlan Cox while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) 241072d39feaSAlan Cox & TSS_INT_PENDING)) 241172d39feaSAlan Cox cpu_relax(); 24121da177e4SLinus Torvalds 241372d39feaSAlan Cox host->jsint = inb(host->addr + TUL_SInt); 241472d39feaSAlan Cox host->phase = host->jsstatus0 & TSS_PH_MASK; 241572d39feaSAlan Cox host->jsstatus1 = inb(host->addr + TUL_SStatus1); 24161da177e4SLinus Torvalds 241772d39feaSAlan Cox if (host->jsint & TSS_RESEL_INT) /* if SCSI bus reset detected */ 241872d39feaSAlan Cox return int_initio_resel(host); 241972d39feaSAlan Cox if (host->jsint & TSS_SEL_TIMEOUT) /* if selected/reselected timeout interrupt */ 242072d39feaSAlan Cox return int_initio_busfree(host); 242172d39feaSAlan Cox if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */ 242272d39feaSAlan Cox return int_initio_scsi_rst(host); 242372d39feaSAlan Cox 242472d39feaSAlan Cox if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */ 242572d39feaSAlan Cox if (host->flags & HCF_EXPECT_DONE_DISC) { 242672d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */ 242772d39feaSAlan Cox initio_unlink_busy_scb(host, host->active); 242872d39feaSAlan Cox host->active->hastat = 0; 242972d39feaSAlan Cox initio_append_done_scb(host, host->active); 243072d39feaSAlan Cox host->active = NULL; 243172d39feaSAlan Cox host->active_tc = NULL; 243272d39feaSAlan Cox host->flags &= ~HCF_EXPECT_DONE_DISC; 243372d39feaSAlan Cox outb(TSC_INITDEFAULT, host->addr + TUL_SConfig); 243472d39feaSAlan Cox outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */ 243572d39feaSAlan Cox return -1; 24361da177e4SLinus Torvalds } 243772d39feaSAlan Cox if (host->flags & HCF_EXPECT_DISC) { 243872d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */ 243972d39feaSAlan Cox host->active = NULL; 244072d39feaSAlan Cox host->active_tc = NULL; 244172d39feaSAlan Cox host->flags &= ~HCF_EXPECT_DISC; 244272d39feaSAlan Cox outb(TSC_INITDEFAULT, host->addr + TUL_SConfig); 244372d39feaSAlan Cox outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */ 244472d39feaSAlan Cox return -1; 24451da177e4SLinus Torvalds } 244672d39feaSAlan Cox return int_initio_busfree(host); 24471da177e4SLinus Torvalds } 244872d39feaSAlan Cox /* The old code really does the below. Can probably be removed */ 244972d39feaSAlan Cox if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) 245072d39feaSAlan Cox return host->phase; 245172d39feaSAlan Cox return host->phase; 24521da177e4SLinus Torvalds } 245372d39feaSAlan Cox 245472d39feaSAlan Cox static int initio_wait_disc(struct initio_host * host) 24551da177e4SLinus Torvalds { 245672d39feaSAlan Cox while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING)) 245772d39feaSAlan Cox cpu_relax(); 24581da177e4SLinus Torvalds 245972d39feaSAlan Cox host->jsint = inb(host->addr + TUL_SInt); 24601da177e4SLinus Torvalds 246172d39feaSAlan Cox if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */ 246272d39feaSAlan Cox return int_initio_scsi_rst(host); 246372d39feaSAlan Cox if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */ 246472d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */ 246572d39feaSAlan Cox outb(TSC_INITDEFAULT, host->addr + TUL_SConfig); 246672d39feaSAlan Cox outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */ 246772d39feaSAlan Cox host->active = NULL; 246872d39feaSAlan Cox return -1; 24691da177e4SLinus Torvalds } 247072d39feaSAlan Cox return initio_bad_seq(host); 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds 247372d39feaSAlan Cox static int initio_wait_done_disc(struct initio_host * host) 24741da177e4SLinus Torvalds { 247572d39feaSAlan Cox while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) 247672d39feaSAlan Cox & TSS_INT_PENDING)) 247772d39feaSAlan Cox cpu_relax(); 24781da177e4SLinus Torvalds 247972d39feaSAlan Cox host->jsint = inb(host->addr + TUL_SInt); 24801da177e4SLinus Torvalds 248172d39feaSAlan Cox if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */ 248272d39feaSAlan Cox return int_initio_scsi_rst(host); 248372d39feaSAlan Cox if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */ 248472d39feaSAlan Cox outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */ 248572d39feaSAlan Cox outb(TSC_INITDEFAULT, host->addr + TUL_SConfig); 248672d39feaSAlan Cox outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */ 248772d39feaSAlan Cox initio_unlink_busy_scb(host, host->active); 24881da177e4SLinus Torvalds 248972d39feaSAlan Cox initio_append_done_scb(host, host->active); 249072d39feaSAlan Cox host->active = NULL; 249172d39feaSAlan Cox return -1; 24921da177e4SLinus Torvalds } 249372d39feaSAlan Cox return initio_bad_seq(host); 249472d39feaSAlan Cox } 24951da177e4SLinus Torvalds 249672d39feaSAlan Cox /** 249772d39feaSAlan Cox * i91u_intr - IRQ handler 249872d39feaSAlan Cox * @irqno: IRQ number 249972d39feaSAlan Cox * @dev_id: IRQ identifier 250072d39feaSAlan Cox * 250172d39feaSAlan Cox * Take the relevant locks and then invoke the actual isr processing 250272d39feaSAlan Cox * code under the lock. 250372d39feaSAlan Cox */ 25041da177e4SLinus Torvalds 25057d12e780SDavid Howells static irqreturn_t i91u_intr(int irqno, void *dev_id) 25061da177e4SLinus Torvalds { 25071da177e4SLinus Torvalds struct Scsi_Host *dev = dev_id; 25081da177e4SLinus Torvalds unsigned long flags; 250972d39feaSAlan Cox int r; 25101da177e4SLinus Torvalds 25111da177e4SLinus Torvalds spin_lock_irqsave(dev->host_lock, flags); 251272d39feaSAlan Cox r = initio_isr((struct initio_host *)dev->hostdata); 25131da177e4SLinus Torvalds spin_unlock_irqrestore(dev->host_lock, flags); 251472d39feaSAlan Cox if (r) 25151da177e4SLinus Torvalds return IRQ_HANDLED; 251672d39feaSAlan Cox else 251772d39feaSAlan Cox return IRQ_NONE; 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds 25201da177e4SLinus Torvalds 252172d39feaSAlan Cox /** 252272d39feaSAlan Cox * initio_build_scb - Build the mappings and SCB 252372d39feaSAlan Cox * @host: InitIO host taking the command 252472d39feaSAlan Cox * @cblk: Firmware command block 252572d39feaSAlan Cox * @cmnd: SCSI midlayer command block 252672d39feaSAlan Cox * 252772d39feaSAlan Cox * Translate the abstract SCSI command into a firmware command block 252872d39feaSAlan Cox * suitable for feeding to the InitIO host controller. This also requires 252972d39feaSAlan Cox * we build the scatter gather lists and ensure they are mapped properly. 253072d39feaSAlan Cox */ 25311da177e4SLinus Torvalds 253272d39feaSAlan Cox static void initio_build_scb(struct initio_host * host, struct scsi_ctrl_blk * cblk, struct scsi_cmnd * cmnd) 25331da177e4SLinus Torvalds { /* Create corresponding SCB */ 253472d39feaSAlan Cox struct scatterlist *sglist; 253572d39feaSAlan Cox struct sg_entry *sg; /* Pointer to SG list */ 2536a258c85dSFUJITA Tomonori int i, nseg; 253772d39feaSAlan Cox long total_len; 25381da177e4SLinus Torvalds dma_addr_t dma_addr; 25391da177e4SLinus Torvalds 254072d39feaSAlan Cox /* Fill in the command headers */ 254172d39feaSAlan Cox cblk->post = i91uSCBPost; /* i91u's callback routine */ 254272d39feaSAlan Cox cblk->srb = cmnd; 254372d39feaSAlan Cox cblk->opcode = ExecSCSI; 254472d39feaSAlan Cox cblk->flags = SCF_POST; /* After SCSI done, call post routine */ 254572d39feaSAlan Cox cblk->target = cmnd->device->id; 254672d39feaSAlan Cox cblk->lun = cmnd->device->lun; 254772d39feaSAlan Cox cblk->ident = cmnd->device->lun | DISC_ALLOW; 25481da177e4SLinus Torvalds 254972d39feaSAlan Cox cblk->flags |= SCF_SENSE; /* Turn on auto request sense */ 255072d39feaSAlan Cox 255172d39feaSAlan Cox /* Map the sense buffer into bus memory */ 255272d39feaSAlan Cox dma_addr = dma_map_single(&host->pci_dev->dev, cmnd->sense_buffer, 25531da177e4SLinus Torvalds SENSE_SIZE, DMA_FROM_DEVICE); 2554423eef6fSGrant Grundler cblk->senseptr = (u32)dma_addr; 2555423eef6fSGrant Grundler cblk->senselen = SENSE_SIZE; 255672d39feaSAlan Cox cmnd->SCp.ptr = (char *)(unsigned long)dma_addr; 255772d39feaSAlan Cox cblk->cdblen = cmnd->cmd_len; 25581da177e4SLinus Torvalds 255972d39feaSAlan Cox /* Clear the returned status */ 256072d39feaSAlan Cox cblk->hastat = 0; 256172d39feaSAlan Cox cblk->tastat = 0; 256272d39feaSAlan Cox /* Command the command */ 256364a87b24SBoaz Harrosh memcpy(cblk->cdb, cmnd->cmnd, cmnd->cmd_len); 25641da177e4SLinus Torvalds 256572d39feaSAlan Cox /* Set up tags */ 256672d39feaSAlan Cox if (cmnd->device->tagged_supported) { /* Tag Support */ 256772d39feaSAlan Cox cblk->tagmsg = SIMPLE_QUEUE_TAG; /* Do simple tag only */ 25681da177e4SLinus Torvalds } else { 256972d39feaSAlan Cox cblk->tagmsg = 0; /* No tag support */ 25701da177e4SLinus Torvalds } 257172d39feaSAlan Cox 25721da177e4SLinus Torvalds /* todo handle map_sg error */ 2573a258c85dSFUJITA Tomonori nseg = scsi_dma_map(cmnd); 2574a258c85dSFUJITA Tomonori BUG_ON(nseg < 0); 2575a258c85dSFUJITA Tomonori if (nseg) { 257672d39feaSAlan Cox dma_addr = dma_map_single(&host->pci_dev->dev, &cblk->sglist[0], 257772d39feaSAlan Cox sizeof(struct sg_entry) * TOTAL_SG_ENTRY, 25781da177e4SLinus Torvalds DMA_BIDIRECTIONAL); 2579423eef6fSGrant Grundler cblk->bufptr = (u32)dma_addr; 258072d39feaSAlan Cox cmnd->SCp.dma_handle = dma_addr; 25811da177e4SLinus Torvalds 2582e2d435eaSStuart Swales cblk->sglen = nseg; 25831da177e4SLinus Torvalds 258472d39feaSAlan Cox cblk->flags |= SCF_SG; /* Turn on SG list flag */ 2585a258c85dSFUJITA Tomonori total_len = 0; 2586a258c85dSFUJITA Tomonori sg = &cblk->sglist[0]; 2587a258c85dSFUJITA Tomonori scsi_for_each_sg(cmnd, sglist, cblk->sglen, i) { 258872d39feaSAlan Cox sg->data = cpu_to_le32((u32)sg_dma_address(sglist)); 2589423eef6fSGrant Grundler sg->len = cpu_to_le32((u32)sg_dma_len(sglist)); 2590423eef6fSGrant Grundler total_len += sg_dma_len(sglist); 2591a169e637SBoaz Harrosh ++sg; 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds 2594a258c85dSFUJITA Tomonori cblk->buflen = (scsi_bufflen(cmnd) > total_len) ? 2595a258c85dSFUJITA Tomonori total_len : scsi_bufflen(cmnd); 259672d39feaSAlan Cox } else { /* No data transfer required */ 259772d39feaSAlan Cox cblk->buflen = 0; 259872d39feaSAlan Cox cblk->sglen = 0; 25991da177e4SLinus Torvalds } 26001da177e4SLinus Torvalds } 26011da177e4SLinus Torvalds 260272d39feaSAlan Cox /** 260372d39feaSAlan Cox * i91u_queuecommand - Queue a new command if possible 260472d39feaSAlan Cox * @cmd: SCSI command block from the mid layer 260572d39feaSAlan Cox * @done: Completion handler 260672d39feaSAlan Cox * 260772d39feaSAlan Cox * Attempts to queue a new command with the host adapter. Will return 260872d39feaSAlan Cox * zero if successful or indicate a host busy condition if not (which 260972d39feaSAlan Cox * will cause the mid layer to call us again later with the command) 261072d39feaSAlan Cox */ 261172d39feaSAlan Cox 2612f281233dSJeff Garzik static int i91u_queuecommand_lck(struct scsi_cmnd *cmd, 26131da177e4SLinus Torvalds void (*done)(struct scsi_cmnd *)) 26141da177e4SLinus Torvalds { 261572d39feaSAlan Cox struct initio_host *host = (struct initio_host *) cmd->device->host->hostdata; 261672d39feaSAlan Cox struct scsi_ctrl_blk *cmnd; 26171da177e4SLinus Torvalds 26181da177e4SLinus Torvalds cmd->scsi_done = done; 26191da177e4SLinus Torvalds 262072d39feaSAlan Cox cmnd = initio_alloc_scb(host); 262172d39feaSAlan Cox if (!cmnd) 26221da177e4SLinus Torvalds return SCSI_MLQUEUE_HOST_BUSY; 26231da177e4SLinus Torvalds 262472d39feaSAlan Cox initio_build_scb(host, cmnd, cmd); 262572d39feaSAlan Cox initio_exec_scb(host, cmnd); 26261da177e4SLinus Torvalds return 0; 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds 2629f281233dSJeff Garzik static DEF_SCSI_QCMD(i91u_queuecommand) 2630f281233dSJeff Garzik 263172d39feaSAlan Cox /** 263272d39feaSAlan Cox * i91u_bus_reset - reset the SCSI bus 263372d39feaSAlan Cox * @cmnd: Command block we want to trigger the reset for 263472d39feaSAlan Cox * 263572d39feaSAlan Cox * Initiate a SCSI bus reset sequence 26361da177e4SLinus Torvalds */ 263772d39feaSAlan Cox 263872d39feaSAlan Cox static int i91u_bus_reset(struct scsi_cmnd * cmnd) 26391da177e4SLinus Torvalds { 264072d39feaSAlan Cox struct initio_host *host; 26411da177e4SLinus Torvalds 264272d39feaSAlan Cox host = (struct initio_host *) cmnd->device->host->hostdata; 26431da177e4SLinus Torvalds 264472d39feaSAlan Cox spin_lock_irq(cmnd->device->host->host_lock); 264572d39feaSAlan Cox initio_reset_scsi(host, 0); 264672d39feaSAlan Cox spin_unlock_irq(cmnd->device->host->host_lock); 264768b3aa7cSJeff Garzik 26481da177e4SLinus Torvalds return SUCCESS; 26491da177e4SLinus Torvalds } 26501da177e4SLinus Torvalds 265172d39feaSAlan Cox /** 265272d39feaSAlan Cox * i91u_biospararm - return the "logical geometry 265372d39feaSAlan Cox * @sdev: SCSI device 265472d39feaSAlan Cox * @dev; Matching block device 265572d39feaSAlan Cox * @capacity: Sector size of drive 265672d39feaSAlan Cox * @info_array: Return space for BIOS geometry 265772d39feaSAlan Cox * 265872d39feaSAlan Cox * Map the device geometry in a manner compatible with the host 265972d39feaSAlan Cox * controller BIOS behaviour. 266072d39feaSAlan Cox * 266172d39feaSAlan Cox * FIXME: limited to 2^32 sector devices. 26621da177e4SLinus Torvalds */ 266372d39feaSAlan Cox 26641da177e4SLinus Torvalds static int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev, 26651da177e4SLinus Torvalds sector_t capacity, int *info_array) 26661da177e4SLinus Torvalds { 266772d39feaSAlan Cox struct initio_host *host; /* Point to Host adapter control block */ 266872d39feaSAlan Cox struct target_control *tc; 26691da177e4SLinus Torvalds 267072d39feaSAlan Cox host = (struct initio_host *) sdev->host->hostdata; 267172d39feaSAlan Cox tc = &host->targets[sdev->id]; 26721da177e4SLinus Torvalds 267372d39feaSAlan Cox if (tc->heads) { 267472d39feaSAlan Cox info_array[0] = tc->heads; 267572d39feaSAlan Cox info_array[1] = tc->sectors; 267672d39feaSAlan Cox info_array[2] = (unsigned long)capacity / tc->heads / tc->sectors; 26771da177e4SLinus Torvalds } else { 267872d39feaSAlan Cox if (tc->drv_flags & TCF_DRV_255_63) { 26791da177e4SLinus Torvalds info_array[0] = 255; 26801da177e4SLinus Torvalds info_array[1] = 63; 26811da177e4SLinus Torvalds info_array[2] = (unsigned long)capacity / 255 / 63; 26821da177e4SLinus Torvalds } else { 26831da177e4SLinus Torvalds info_array[0] = 64; 26841da177e4SLinus Torvalds info_array[1] = 32; 26851da177e4SLinus Torvalds info_array[2] = (unsigned long)capacity >> 11; 26861da177e4SLinus Torvalds } 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds 26891da177e4SLinus Torvalds #if defined(DEBUG_BIOSPARAM) 26901da177e4SLinus Torvalds if (i91u_debug & debug_biosparam) { 26911da177e4SLinus Torvalds printk("bios geometry: head=%d, sec=%d, cyl=%d\n", 26921da177e4SLinus Torvalds info_array[0], info_array[1], info_array[2]); 26931da177e4SLinus Torvalds printk("WARNING: check, if the bios geometry is correct.\n"); 26941da177e4SLinus Torvalds } 26951da177e4SLinus Torvalds #endif 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds return 0; 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds 270072d39feaSAlan Cox /** 270172d39feaSAlan Cox * i91u_unmap_scb - Unmap a command 270272d39feaSAlan Cox * @pci_dev: PCI device the command is for 270372d39feaSAlan Cox * @cmnd: The command itself 270472d39feaSAlan Cox * 270572d39feaSAlan Cox * Unmap any PCI mapping/IOMMU resources allocated when the command 270672d39feaSAlan Cox * was mapped originally as part of initio_build_scb 270772d39feaSAlan Cox */ 270872d39feaSAlan Cox 270972d39feaSAlan Cox static void i91u_unmap_scb(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd) 27101da177e4SLinus Torvalds { 27111da177e4SLinus Torvalds /* auto sense buffer */ 27121da177e4SLinus Torvalds if (cmnd->SCp.ptr) { 27131da177e4SLinus Torvalds dma_unmap_single(&pci_dev->dev, 27141da177e4SLinus Torvalds (dma_addr_t)((unsigned long)cmnd->SCp.ptr), 27151da177e4SLinus Torvalds SENSE_SIZE, DMA_FROM_DEVICE); 27161da177e4SLinus Torvalds cmnd->SCp.ptr = NULL; 27171da177e4SLinus Torvalds } 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds /* request buffer */ 2720a258c85dSFUJITA Tomonori if (scsi_sg_count(cmnd)) { 27211da177e4SLinus Torvalds dma_unmap_single(&pci_dev->dev, cmnd->SCp.dma_handle, 272272d39feaSAlan Cox sizeof(struct sg_entry) * TOTAL_SG_ENTRY, 27231da177e4SLinus Torvalds DMA_BIDIRECTIONAL); 27241da177e4SLinus Torvalds 2725a258c85dSFUJITA Tomonori scsi_dma_unmap(cmnd); 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds } 27281da177e4SLinus Torvalds 272972d39feaSAlan Cox /** 273072d39feaSAlan Cox * i91uSCBPost - SCSI callback 273172d39feaSAlan Cox * @host: Pointer to host adapter control block. 273272d39feaSAlan Cox * @cmnd: Pointer to SCSI control block. 273372d39feaSAlan Cox * 273472d39feaSAlan Cox * This is callback routine be called when tulip finish one 273572d39feaSAlan Cox * SCSI command. 273672d39feaSAlan Cox */ 273772d39feaSAlan Cox 273872d39feaSAlan Cox static void i91uSCBPost(u8 * host_mem, u8 * cblk_mem) 27391da177e4SLinus Torvalds { 274072d39feaSAlan Cox struct scsi_cmnd *cmnd; /* Pointer to SCSI request block */ 274172d39feaSAlan Cox struct initio_host *host; 274272d39feaSAlan Cox struct scsi_ctrl_blk *cblk; 27431da177e4SLinus Torvalds 274472d39feaSAlan Cox host = (struct initio_host *) host_mem; 274572d39feaSAlan Cox cblk = (struct scsi_ctrl_blk *) cblk_mem; 274672d39feaSAlan Cox if ((cmnd = cblk->srb) == NULL) { 274772d39feaSAlan Cox printk(KERN_ERR "i91uSCBPost: SRB pointer is empty\n"); 274872d39feaSAlan Cox WARN_ON(1); 274972d39feaSAlan Cox initio_release_scb(host, cblk); /* Release SCB for current channel */ 27501da177e4SLinus Torvalds return; 27511da177e4SLinus Torvalds } 275272d39feaSAlan Cox 275372d39feaSAlan Cox /* 275472d39feaSAlan Cox * Remap the firmware error status into a mid layer one 275572d39feaSAlan Cox */ 275672d39feaSAlan Cox switch (cblk->hastat) { 27571da177e4SLinus Torvalds case 0x0: 27581da177e4SLinus Torvalds case 0xa: /* Linked command complete without error and linked normally */ 27591da177e4SLinus Torvalds case 0xb: /* Linked command complete without error interrupt generated */ 276072d39feaSAlan Cox cblk->hastat = 0; 27611da177e4SLinus Torvalds break; 27621da177e4SLinus Torvalds 27631da177e4SLinus Torvalds case 0x11: /* Selection time out-The initiator selection or target 27641da177e4SLinus Torvalds reselection was not complete within the SCSI Time out period */ 276572d39feaSAlan Cox cblk->hastat = DID_TIME_OUT; 27661da177e4SLinus Torvalds break; 27671da177e4SLinus Torvalds 27681da177e4SLinus Torvalds case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus 27691da177e4SLinus Torvalds phase sequence was requested by the target. The host adapter 27701da177e4SLinus Torvalds will generate a SCSI Reset Condition, notifying the host with 27711da177e4SLinus Torvalds a SCRD interrupt */ 277272d39feaSAlan Cox cblk->hastat = DID_RESET; 27731da177e4SLinus Torvalds break; 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds case 0x1a: /* SCB Aborted. 07/21/98 */ 277672d39feaSAlan Cox cblk->hastat = DID_ABORT; 27771da177e4SLinus Torvalds break; 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds case 0x12: /* Data overrun/underrun-The target attempted to transfer more data 27801da177e4SLinus Torvalds than was allocated by the Data Length field or the sum of the 27811da177e4SLinus Torvalds Scatter / Gather Data Length fields. */ 27821da177e4SLinus Torvalds case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */ 27831da177e4SLinus Torvalds case 0x16: /* Invalid SCB Operation Code. */ 27841da177e4SLinus Torvalds 27851da177e4SLinus Torvalds default: 278672d39feaSAlan Cox printk("ini9100u: %x %x\n", cblk->hastat, cblk->tastat); 278772d39feaSAlan Cox cblk->hastat = DID_ERROR; /* Couldn't find any better */ 27881da177e4SLinus Torvalds break; 27891da177e4SLinus Torvalds } 27901da177e4SLinus Torvalds 279172d39feaSAlan Cox cmnd->result = cblk->tastat | (cblk->hastat << 16); 279272d39feaSAlan Cox i91u_unmap_scb(host->pci_dev, cmnd); 279372d39feaSAlan Cox cmnd->scsi_done(cmnd); /* Notify system DONE */ 279472d39feaSAlan Cox initio_release_scb(host, cblk); /* Release SCB for current channel */ 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 279772d39feaSAlan Cox static struct scsi_host_template initio_template = { 27981da177e4SLinus Torvalds .proc_name = "INI9100U", 279972d39feaSAlan Cox .name = "Initio INI-9X00U/UW SCSI device driver", 28001da177e4SLinus Torvalds .queuecommand = i91u_queuecommand, 28011da177e4SLinus Torvalds .eh_bus_reset_handler = i91u_bus_reset, 28021da177e4SLinus Torvalds .bios_param = i91u_biosparam, 280372d39feaSAlan Cox .can_queue = MAX_TARGETS * i91u_MAXQUEUE, 28041da177e4SLinus Torvalds .this_id = 1, 28051da177e4SLinus Torvalds .sg_tablesize = SG_ALL, 28061da177e4SLinus Torvalds }; 28071da177e4SLinus Torvalds 280872d39feaSAlan Cox static int initio_probe_one(struct pci_dev *pdev, 280972d39feaSAlan Cox const struct pci_device_id *id) 281072d39feaSAlan Cox { 281172d39feaSAlan Cox struct Scsi_Host *shost; 281272d39feaSAlan Cox struct initio_host *host; 281372d39feaSAlan Cox u32 reg; 281472d39feaSAlan Cox u16 bios_seg; 281572d39feaSAlan Cox struct scsi_ctrl_blk *scb, *tmp, *prev = NULL /* silence gcc */; 281672d39feaSAlan Cox int num_scb, i, error; 281772d39feaSAlan Cox 281872d39feaSAlan Cox error = pci_enable_device(pdev); 281972d39feaSAlan Cox if (error) 282072d39feaSAlan Cox return error; 282172d39feaSAlan Cox 282272d39feaSAlan Cox pci_read_config_dword(pdev, 0x44, (u32 *) & reg); 282372d39feaSAlan Cox bios_seg = (u16) (reg & 0xFF); 282472d39feaSAlan Cox if (((reg & 0xFF00) >> 8) == 0xFF) 282572d39feaSAlan Cox reg = 0; 282672d39feaSAlan Cox bios_seg = (bios_seg << 8) + ((u16) ((reg & 0xFF00) >> 8)); 282772d39feaSAlan Cox 2828663b4117SChristoph Hellwig if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) { 282972d39feaSAlan Cox printk(KERN_WARNING "i91u: Could not set 32 bit DMA mask\n"); 283072d39feaSAlan Cox error = -ENODEV; 283172d39feaSAlan Cox goto out_disable_device; 283272d39feaSAlan Cox } 283372d39feaSAlan Cox shost = scsi_host_alloc(&initio_template, sizeof(struct initio_host)); 283472d39feaSAlan Cox if (!shost) { 283572d39feaSAlan Cox printk(KERN_WARNING "initio: Could not allocate host structure.\n"); 283672d39feaSAlan Cox error = -ENOMEM; 283772d39feaSAlan Cox goto out_disable_device; 283872d39feaSAlan Cox } 283972d39feaSAlan Cox host = (struct initio_host *)shost->hostdata; 284072d39feaSAlan Cox memset(host, 0, sizeof(struct initio_host)); 284199f1f534SAlan Cox host->addr = pci_resource_start(pdev, 0); 2842e2d435eaSStuart Swales host->bios_addr = bios_seg; 284372d39feaSAlan Cox 284472d39feaSAlan Cox if (!request_region(host->addr, 256, "i91u")) { 284572d39feaSAlan Cox printk(KERN_WARNING "initio: I/O port range 0x%x is busy.\n", host->addr); 284672d39feaSAlan Cox error = -ENODEV; 284772d39feaSAlan Cox goto out_host_put; 284872d39feaSAlan Cox } 284972d39feaSAlan Cox 285072d39feaSAlan Cox if (initio_tag_enable) /* 1.01i */ 285172d39feaSAlan Cox num_scb = MAX_TARGETS * i91u_MAXQUEUE; 285272d39feaSAlan Cox else 285372d39feaSAlan Cox num_scb = MAX_TARGETS + 3; /* 1-tape, 1-CD_ROM, 1- extra */ 285472d39feaSAlan Cox 285572d39feaSAlan Cox for (; num_scb >= MAX_TARGETS + 3; num_scb--) { 285672d39feaSAlan Cox i = num_scb * sizeof(struct scsi_ctrl_blk); 285772d39feaSAlan Cox if ((scb = kzalloc(i, GFP_DMA)) != NULL) 285872d39feaSAlan Cox break; 285972d39feaSAlan Cox } 286072d39feaSAlan Cox 286172d39feaSAlan Cox if (!scb) { 286272d39feaSAlan Cox printk(KERN_WARNING "initio: Cannot allocate SCB array.\n"); 286372d39feaSAlan Cox error = -ENOMEM; 286472d39feaSAlan Cox goto out_release_region; 286572d39feaSAlan Cox } 286672d39feaSAlan Cox 2867e9e42fafSAlan Cox host->pci_dev = pdev; 2868e9e42fafSAlan Cox 2869e2d435eaSStuart Swales host->semaph = 1; 2870e2d435eaSStuart Swales spin_lock_init(&host->semaph_lock); 287172d39feaSAlan Cox host->num_scbs = num_scb; 287272d39feaSAlan Cox host->scb = scb; 287372d39feaSAlan Cox host->next_pending = scb; 287472d39feaSAlan Cox host->next_avail = scb; 287572d39feaSAlan Cox for (i = 0, tmp = scb; i < num_scb; i++, tmp++) { 287672d39feaSAlan Cox tmp->tagid = i; 287772d39feaSAlan Cox if (i != 0) 287872d39feaSAlan Cox prev->next = tmp; 287972d39feaSAlan Cox prev = tmp; 288072d39feaSAlan Cox } 288172d39feaSAlan Cox prev->next = NULL; 288272d39feaSAlan Cox host->scb_end = tmp; 288372d39feaSAlan Cox host->first_avail = scb; 288472d39feaSAlan Cox host->last_avail = prev; 2885e9e42fafSAlan Cox spin_lock_init(&host->avail_lock); 288672d39feaSAlan Cox 2887e2d435eaSStuart Swales initio_init(host, phys_to_virt(((u32)bios_seg << 4))); 288872d39feaSAlan Cox 288972d39feaSAlan Cox host->jsstatus0 = 0; 289072d39feaSAlan Cox 289172d39feaSAlan Cox shost->io_port = host->addr; 289272d39feaSAlan Cox shost->n_io_port = 0xff; 289372d39feaSAlan Cox shost->can_queue = num_scb; /* 03/05/98 */ 289472d39feaSAlan Cox shost->unique_id = host->addr; 289572d39feaSAlan Cox shost->max_id = host->max_tar; 289672d39feaSAlan Cox shost->max_lun = 32; /* 10/21/97 */ 289772d39feaSAlan Cox shost->irq = pdev->irq; 289872d39feaSAlan Cox shost->this_id = host->scsi_id; /* Assign HCS index */ 289972d39feaSAlan Cox shost->base = host->addr; 290072d39feaSAlan Cox shost->sg_tablesize = TOTAL_SG_ENTRY; 290172d39feaSAlan Cox 29024909cc2bSMichael Opdenacker error = request_irq(pdev->irq, i91u_intr, IRQF_SHARED, "i91u", shost); 290372d39feaSAlan Cox if (error < 0) { 290472d39feaSAlan Cox printk(KERN_WARNING "initio: Unable to request IRQ %d\n", pdev->irq); 290572d39feaSAlan Cox goto out_free_scbs; 290672d39feaSAlan Cox } 290772d39feaSAlan Cox 290872d39feaSAlan Cox pci_set_drvdata(pdev, shost); 290972d39feaSAlan Cox 291072d39feaSAlan Cox error = scsi_add_host(shost, &pdev->dev); 291172d39feaSAlan Cox if (error) 291272d39feaSAlan Cox goto out_free_irq; 291372d39feaSAlan Cox scsi_scan_host(shost); 291472d39feaSAlan Cox return 0; 291572d39feaSAlan Cox out_free_irq: 291672d39feaSAlan Cox free_irq(pdev->irq, shost); 291772d39feaSAlan Cox out_free_scbs: 291872d39feaSAlan Cox kfree(host->scb); 291972d39feaSAlan Cox out_release_region: 292072d39feaSAlan Cox release_region(host->addr, 256); 292172d39feaSAlan Cox out_host_put: 292272d39feaSAlan Cox scsi_host_put(shost); 292372d39feaSAlan Cox out_disable_device: 292472d39feaSAlan Cox pci_disable_device(pdev); 292572d39feaSAlan Cox return error; 292672d39feaSAlan Cox } 292772d39feaSAlan Cox 292872d39feaSAlan Cox /** 292972d39feaSAlan Cox * initio_remove_one - control shutdown 293072d39feaSAlan Cox * @pdev: PCI device being released 293172d39feaSAlan Cox * 293272d39feaSAlan Cox * Release the resources assigned to this adapter after it has 293372d39feaSAlan Cox * finished being used. 293472d39feaSAlan Cox */ 293572d39feaSAlan Cox 293672d39feaSAlan Cox static void initio_remove_one(struct pci_dev *pdev) 293772d39feaSAlan Cox { 293872d39feaSAlan Cox struct Scsi_Host *host = pci_get_drvdata(pdev); 293972d39feaSAlan Cox struct initio_host *s = (struct initio_host *)host->hostdata; 294072d39feaSAlan Cox scsi_remove_host(host); 294172d39feaSAlan Cox free_irq(pdev->irq, host); 294272d39feaSAlan Cox release_region(s->addr, 256); 294372d39feaSAlan Cox scsi_host_put(host); 294472d39feaSAlan Cox pci_disable_device(pdev); 294572d39feaSAlan Cox } 294672d39feaSAlan Cox 294772d39feaSAlan Cox MODULE_LICENSE("GPL"); 294872d39feaSAlan Cox 294972d39feaSAlan Cox static struct pci_device_id initio_pci_tbl[] = { 295072d39feaSAlan Cox {PCI_VENDOR_ID_INIT, 0x9500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, 295172d39feaSAlan Cox {PCI_VENDOR_ID_INIT, 0x9400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, 295272d39feaSAlan Cox {PCI_VENDOR_ID_INIT, 0x9401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, 295372d39feaSAlan Cox {PCI_VENDOR_ID_INIT, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, 295472d39feaSAlan Cox {PCI_VENDOR_ID_DOMEX, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, 295572d39feaSAlan Cox {0,} 295672d39feaSAlan Cox }; 295772d39feaSAlan Cox MODULE_DEVICE_TABLE(pci, initio_pci_tbl); 295872d39feaSAlan Cox 295972d39feaSAlan Cox static struct pci_driver initio_pci_driver = { 296072d39feaSAlan Cox .name = "initio", 296172d39feaSAlan Cox .id_table = initio_pci_tbl, 296272d39feaSAlan Cox .probe = initio_probe_one, 29636f039790SGreg Kroah-Hartman .remove = initio_remove_one, 296472d39feaSAlan Cox }; 2965ca57b069SLiu Shixin module_pci_driver(initio_pci_driver); 296672d39feaSAlan Cox 296772d39feaSAlan Cox MODULE_DESCRIPTION("Initio INI-9X00U/UW SCSI device driver"); 296872d39feaSAlan Cox MODULE_AUTHOR("Initio Corporation"); 296972d39feaSAlan Cox MODULE_LICENSE("GPL"); 2970