blob: a7714160fbc33d8ff24330327680016117c113d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**************************************************************************
2 * Initio 9100 device driver for Linux.
3 *
4 * Copyright (c) 1994-1998 Initio Corporation
5 * Copyright (c) 1998 Bas Vermeulen <bvermeul@blackstar.xs4all.nl>
Alan Cox72d39fe2007-05-21 15:06:43 +01006 * Copyright (c) 2004 Christoph Hellwig <hch@lst.de>
Alan Coxfa195af2008-10-27 15:16:36 +00007 * Copyright (c) 2007 Red Hat
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
24 *************************************************************************
25 *
26 * DESCRIPTION:
27 *
28 * This is the Linux low-level SCSI driver for Initio INI-9X00U/UW SCSI host
29 * adapters
30 *
31 * 08/06/97 hc - v1.01h
32 * - Support inic-940 and inic-935
33 * 09/26/97 hc - v1.01i
34 * - Make correction from J.W. Schultz suggestion
35 * 10/13/97 hc - Support reset function
36 * 10/21/97 hc - v1.01j
37 * - Support 32 LUN (SCSI 3)
38 * 01/14/98 hc - v1.01k
39 * - Fix memory allocation problem
40 * 03/04/98 hc - v1.01l
41 * - Fix tape rewind which will hang the system problem
Alan Cox72d39fe2007-05-21 15:06:43 +010042 * - Set can_queue to initio_num_scb
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * 06/25/98 hc - v1.01m
44 * - Get it work for kernel version >= 2.1.75
Alan Cox72d39fe2007-05-21 15:06:43 +010045 * - Dynamic assign SCSI bus reset holding time in initio_init()
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * 07/02/98 hc - v1.01n
47 * - Support 0002134A
48 * 08/07/98 hc - v1.01o
Alan Cox72d39fe2007-05-21 15:06:43 +010049 * - Change the initio_abort_srb routine to use scsi_done. <01>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * 09/07/98 hl - v1.02
51 * - Change the INI9100U define and proc_dir_entry to
52 * reflect the newer Kernel 2.1.118, but the v1.o1o
53 * should work with Kernel 2.1.118.
54 * 09/20/98 wh - v1.02a
55 * - Support Abort command.
56 * - Handle reset routine.
57 * 09/21/98 hl - v1.03
58 * - remove comments.
59 * 12/09/98 bv - v1.03a
60 * - Removed unused code
61 * 12/13/98 bv - v1.03b
62 * - Remove cli() locking for kernels >= 2.1.95. This uses
63 * spinlocks to serialize access to the pSRB_head and
64 * pSRB_tail members of the HCS structure.
65 * 09/01/99 bv - v1.03d
66 * - Fixed a deadlock problem in SMP.
67 * 21/01/99 bv - v1.03e
68 * - Add support for the Domex 3192U PCI SCSI
69 * This is a slightly modified patch by
70 * Brian Macy <bmacy@sunshinecomputing.com>
71 * 22/02/99 bv - v1.03f
72 * - Didn't detect the INIC-950 in 2.0.x correctly.
73 * Now fixed.
74 * 05/07/99 bv - v1.03g
75 * - Changed the assumption that HZ = 100
76 * 10/17/03 mc - v1.04
77 * - added new DMA API support
78 * 06/01/04 jmd - v1.04a
79 * - Re-add reset_bus support
80 **************************************************************************/
81
82#include <linux/module.h>
83#include <linux/errno.h>
84#include <linux/delay.h>
85#include <linux/pci.h>
86#include <linux/init.h>
87#include <linux/blkdev.h>
88#include <linux/spinlock.h>
89#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <linux/kernel.h>
91#include <linux/proc_fs.h>
92#include <linux/string.h>
93#include <linux/interrupt.h>
94#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/slab.h>
96#include <linux/jiffies.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080097#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <asm/io.h>
99
100#include <scsi/scsi.h>
101#include <scsi/scsi_cmnd.h>
102#include <scsi/scsi_device.h>
103#include <scsi/scsi_host.h>
104#include <scsi/scsi_tcq.h>
105
106#include "initio.h"
107
108#define SENSE_SIZE 14
109
110#define i91u_MAXQUEUE 2
111#define i91u_REVID "Initio INI-9X00U/UW SCSI device driver; Revision: 1.04a"
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define I950_DEVICE_ID 0x9500 /* Initio's inic-950 product ID */
114#define I940_DEVICE_ID 0x9400 /* Initio's inic-940 product ID */
115#define I935_DEVICE_ID 0x9401 /* Initio's inic-935 product ID */
116#define I920_DEVICE_ID 0x0002 /* Initio's other product ID */
117
118#ifdef DEBUG_i91u
119static unsigned int i91u_debug = DEBUG_DEFAULT;
120#endif
121
Alan Cox72d39fe2007-05-21 15:06:43 +0100122static int initio_tag_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124#ifdef DEBUG_i91u
125static int setup_debug = 0;
126#endif
127
Alan Cox72d39fe2007-05-21 15:06:43 +0100128static void i91uSCBPost(u8 * pHcb, u8 * pScb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Ben Collins01288732006-10-18 08:40:57 -0400130/* PCI Devices supported by this driver */
Randy Dunlap84ad58e2006-11-19 18:53:43 -0800131static struct pci_device_id i91u_pci_devices[] = {
Ben Collins01288732006-10-18 08:40:57 -0400132 { PCI_VENDOR_ID_INIT, I950_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
133 { PCI_VENDOR_ID_INIT, I940_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
134 { PCI_VENDOR_ID_INIT, I935_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
135 { PCI_VENDOR_ID_INIT, I920_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
136 { PCI_VENDOR_ID_DOMEX, I920_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
137 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
Ben Collins01288732006-10-18 08:40:57 -0400139MODULE_DEVICE_TABLE(pci, i91u_pci_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141#define DEBUG_INTERRUPT 0
142#define DEBUG_QUEUE 0
143#define DEBUG_STATE 0
144#define INT_DISC 0
145
Alan Cox72d39fe2007-05-21 15:06:43 +0100146/*--- forward references ---*/
147static struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun);
148static struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Alan Cox72d39fe2007-05-21 15:06:43 +0100150static int tulip_main(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Alan Cox72d39fe2007-05-21 15:06:43 +0100152static int initio_next_state(struct initio_host * host);
153static int initio_state_1(struct initio_host * host);
154static int initio_state_2(struct initio_host * host);
155static int initio_state_3(struct initio_host * host);
156static int initio_state_4(struct initio_host * host);
157static int initio_state_5(struct initio_host * host);
158static int initio_state_6(struct initio_host * host);
159static int initio_state_7(struct initio_host * host);
160static int initio_xfer_data_in(struct initio_host * host);
161static int initio_xfer_data_out(struct initio_host * host);
162static int initio_xpad_in(struct initio_host * host);
163static int initio_xpad_out(struct initio_host * host);
164static int initio_status_msg(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Alan Cox72d39fe2007-05-21 15:06:43 +0100166static int initio_msgin(struct initio_host * host);
167static int initio_msgin_sync(struct initio_host * host);
168static int initio_msgin_accept(struct initio_host * host);
169static int initio_msgout_reject(struct initio_host * host);
170static int initio_msgin_extend(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Alan Cox72d39fe2007-05-21 15:06:43 +0100172static int initio_msgout_ide(struct initio_host * host);
173static int initio_msgout_abort_targ(struct initio_host * host);
174static int initio_msgout_abort_tag(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Alan Cox72d39fe2007-05-21 15:06:43 +0100176static int initio_bus_device_reset(struct initio_host * host);
177static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb);
178static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb);
179static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb);
180static int int_initio_busfree(struct initio_host * host);
181static int int_initio_scsi_rst(struct initio_host * host);
182static int int_initio_bad_seq(struct initio_host * host);
183static int int_initio_resel(struct initio_host * host);
184static int initio_sync_done(struct initio_host * host);
185static int wdtr_done(struct initio_host * host);
186static int wait_tulip(struct initio_host * host);
187static int initio_wait_done_disc(struct initio_host * host);
188static int initio_wait_disc(struct initio_host * host);
189static void tulip_scsi(struct initio_host * host);
190static int initio_post_scsi_rst(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Alan Cox72d39fe2007-05-21 15:06:43 +0100192static void initio_se2_ew_en(unsigned long base);
193static void initio_se2_ew_ds(unsigned long base);
194static int initio_se2_rd_all(unsigned long base);
195static void initio_se2_update_all(unsigned long base); /* setup default pattern */
196static void initio_read_eeprom(unsigned long base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Alan Cox72d39fe2007-05-21 15:06:43 +0100198/* ---- INTERNAL VARIABLES ---- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static NVRAM i91unvram;
201static NVRAM *i91unvramp;
202
Alan Cox72d39fe2007-05-21 15:06:43 +0100203static u8 i91udftNvRam[64] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Alan Cox72d39fe2007-05-21 15:06:43 +0100205 /*----------- header -----------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 0x25, 0xc9, /* Signature */
207 0x40, /* Size */
208 0x01, /* Revision */
209 /* -- Host Adapter Structure -- */
210 0x95, /* ModelByte0 */
211 0x00, /* ModelByte1 */
212 0x00, /* ModelInfo */
213 0x01, /* NumOfCh */
214 NBC1_DEFAULT, /* BIOSConfig1 */
215 0, /* BIOSConfig2 */
216 0, /* HAConfig1 */
217 0, /* HAConfig2 */
218 /* SCSI channel 0 and target Structure */
219 7, /* SCSIid */
220 NCC1_DEFAULT, /* SCSIconfig1 */
221 0, /* SCSIconfig2 */
222 0x10, /* NumSCSItarget */
223
224 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
225 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
226 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
227 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
228
229 /* SCSI channel 1 and target Structure */
230 7, /* SCSIid */
231 NCC1_DEFAULT, /* SCSIconfig1 */
232 0, /* SCSIconfig2 */
233 0x10, /* NumSCSItarget */
234
235 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
236 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
237 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
238 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
239 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
240 0, 0}; /* - CheckSum - */
241
242
Alan Cox72d39fe2007-05-21 15:06:43 +0100243static u8 initio_rate_tbl[8] = /* fast 20 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 /* nanosecond devide by 4 */
246 12, /* 50ns, 20M */
247 18, /* 75ns, 13.3M */
248 25, /* 100ns, 10M */
249 31, /* 125ns, 8M */
250 37, /* 150ns, 6.6M */
251 43, /* 175ns, 5.7M */
252 50, /* 200ns, 5M */
253 62 /* 250ns, 4M */
254};
255
Alan Cox72d39fe2007-05-21 15:06:43 +0100256static void initio_do_pause(unsigned amount)
257{
258 /* Pause for amount jiffies */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned long the_time = jiffies + amount;
260
Alan Cox72d39fe2007-05-21 15:06:43 +0100261 while (time_before_eq(jiffies, the_time))
262 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265/*-- forward reference --*/
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/******************************************************************
268 Input: instruction for Serial E2PROM
269
270 EX: se2_rd(0 call se2_instr() to send address and read command
271
272 StartBit OP_Code Address Data
273 --------- -------- ------------------ -------
274 1 1 , 0 A5,A4,A3,A2,A1,A0 D15-D0
275
276 +-----------------------------------------------------
277 |
278 CS -----+
279 +--+ +--+ +--+ +--+ +--+
280 ^ | ^ | ^ | ^ | ^ |
281 | | | | | | | | | |
282 CLK -------+ +--+ +--+ +--+ +--+ +--
283 (leading edge trigger)
284
285 +--1-----1--+
286 | SB OP | OP A5 A4
287 DI ----+ +--0------------------
288 (address and cmd sent to nvram)
289
290 -------------------------------------------+
291 |
292 DO +---
293 (data sent from nvram)
294
295
296******************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100297
298/**
299 * initio_se2_instr - bitbang an instruction
300 * @base: Base of InitIO controller
301 * @instr: Instruction for serial E2PROM
302 *
303 * Bitbang an instruction out to the serial E2Prom
304 */
305
306static void initio_se2_instr(unsigned long base, u8 instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100309 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Alan Cox72d39fe2007-05-21 15:06:43 +0100311 outb(SE2CS | SE2DO, base + TUL_NVRAM); /* cs+start bit */
312 udelay(30);
313 outb(SE2CS | SE2CLK | SE2DO, base + TUL_NVRAM); /* +CLK */
314 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 for (i = 0; i < 8; i++) {
317 if (instr & 0x80)
Alan Cox72d39fe2007-05-21 15:06:43 +0100318 b = SE2CS | SE2DO; /* -CLK+dataBit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100320 b = SE2CS; /* -CLK */
321 outb(b, base + TUL_NVRAM);
322 udelay(30);
323 outb(b | SE2CLK, base + TUL_NVRAM); /* +CLK */
324 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 instr <<= 1;
326 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100327 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
328 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331
Alan Cox72d39fe2007-05-21 15:06:43 +0100332/**
333 * initio_se2_ew_en - Enable erase/write
334 * @base: Base address of InitIO controller
335 *
336 * Enable erase/write state of serial EEPROM
337 */
338void initio_se2_ew_en(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Alan Cox72d39fe2007-05-21 15:06:43 +0100340 initio_se2_instr(base, 0x30); /* EWEN */
341 outb(0, base + TUL_NVRAM); /* -CS */
342 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345
Alan Cox72d39fe2007-05-21 15:06:43 +0100346/**
347 * initio_se2_ew_ds - Disable erase/write
348 * @base: Base address of InitIO controller
349 *
350 * Disable erase/write state of serial EEPROM
351 */
352void initio_se2_ew_ds(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Alan Cox72d39fe2007-05-21 15:06:43 +0100354 initio_se2_instr(base, 0); /* EWDS */
355 outb(0, base + TUL_NVRAM); /* -CS */
356 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359
Alan Cox72d39fe2007-05-21 15:06:43 +0100360/**
361 * initio_se2_rd - read E2PROM word
362 * @base: Base of InitIO controller
363 * @addr: Address of word in E2PROM
364 *
365 * Read a word from the NV E2PROM device
366 */
367static u16 initio_se2_rd(unsigned long base, u8 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Alan Cox72d39fe2007-05-21 15:06:43 +0100369 u8 instr, rb;
370 u16 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int i;
372
Alan Cox72d39fe2007-05-21 15:06:43 +0100373 instr = (u8) (addr | 0x80);
374 initio_se2_instr(base, instr); /* READ INSTR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 for (i = 15; i >= 0; i--) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100377 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
378 udelay(30);
379 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /* sample data after the following edge of clock */
Alan Cox72d39fe2007-05-21 15:06:43 +0100382 rb = inb(base + TUL_NVRAM);
383 rb &= SE2DI;
384 val += (rb << i);
385 udelay(30); /* 6/20/95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Alan Cox72d39fe2007-05-21 15:06:43 +0100388 outb(0, base + TUL_NVRAM); /* no chip select */
389 udelay(30);
390 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Alan Cox72d39fe2007-05-21 15:06:43 +0100393/**
394 * initio_se2_wr - read E2PROM word
395 * @base: Base of InitIO controller
396 * @addr: Address of word in E2PROM
397 * @val: Value to write
398 *
399 * Write a word to the NV E2PROM device. Used when recovering from
400 * a problem with the NV.
401 */
402static void initio_se2_wr(unsigned long base, u8 addr, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Alan Cox72d39fe2007-05-21 15:06:43 +0100404 u8 rb;
405 u8 instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int i;
407
Alan Cox72d39fe2007-05-21 15:06:43 +0100408 instr = (u8) (addr | 0x40);
409 initio_se2_instr(base, instr); /* WRITE INSTR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 for (i = 15; i >= 0; i--) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100411 if (val & 0x8000)
412 outb(SE2CS | SE2DO, base + TUL_NVRAM); /* -CLK+dataBit 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100414 outb(SE2CS, base + TUL_NVRAM); /* -CLK+dataBit 0 */
415 udelay(30);
416 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
417 udelay(30);
418 val <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100420 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
421 udelay(30);
422 outb(0, base + TUL_NVRAM); /* -CS */
423 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Alan Cox72d39fe2007-05-21 15:06:43 +0100425 outb(SE2CS, base + TUL_NVRAM); /* +CS */
426 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100429 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
430 udelay(30);
431 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
432 udelay(30);
433 if ((rb = inb(base + TUL_NVRAM)) & SE2DI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 break; /* write complete */
435 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100436 outb(0, base + TUL_NVRAM); /* -CS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Alan Cox72d39fe2007-05-21 15:06:43 +0100439/**
440 * initio_se2_rd_all - read hostadapter NV configuration
441 * @base: Base address of InitIO controller
442 *
443 * Reads the E2PROM data into main memory. Ensures that the checksum
444 * and header marker are valid. Returns 1 on success -1 on error.
445 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Alan Cox72d39fe2007-05-21 15:06:43 +0100447static int initio_se2_rd_all(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100450 u16 chksum = 0;
451 u16 *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 i91unvramp = &i91unvram;
Alan Cox72d39fe2007-05-21 15:06:43 +0100454 np = (u16 *) i91unvramp;
455 for (i = 0; i < 32; i++)
456 *np++ = initio_se2_rd(base, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Alan Cox72d39fe2007-05-21 15:06:43 +0100458 /* Is signature "ini" ok ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (i91unvramp->NVM_Signature != INI_SIGNATURE)
460 return -1;
Alan Cox72d39fe2007-05-21 15:06:43 +0100461 /* Is ckecksum ok ? */
462 np = (u16 *) i91unvramp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 for (i = 0; i < 31; i++)
464 chksum += *np++;
Alan Cox72d39fe2007-05-21 15:06:43 +0100465 if (i91unvramp->NVM_CheckSum != chksum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return -1;
467 return 1;
468}
469
Alan Cox72d39fe2007-05-21 15:06:43 +0100470/**
471 * initio_se2_update_all - Update E2PROM
472 * @base: Base of InitIO controller
473 *
474 * Update the E2PROM by wrting any changes into the E2PROM
475 * chip, rewriting the checksum.
476 */
477static void initio_se2_update_all(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{ /* setup default pattern */
479 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100480 u16 chksum = 0;
481 u16 *np, *np1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 i91unvramp = &i91unvram;
484 /* Calculate checksum first */
Alan Cox72d39fe2007-05-21 15:06:43 +0100485 np = (u16 *) i91udftNvRam;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 for (i = 0; i < 31; i++)
487 chksum += *np++;
Alan Cox72d39fe2007-05-21 15:06:43 +0100488 *np = chksum;
489 initio_se2_ew_en(base); /* Enable write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Alan Cox72d39fe2007-05-21 15:06:43 +0100491 np = (u16 *) i91udftNvRam;
492 np1 = (u16 *) i91unvramp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 for (i = 0; i < 32; i++, np++, np1++) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100494 if (*np != *np1)
495 initio_se2_wr(base, i, *np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100497 initio_se2_ew_ds(base); /* Disable write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Alan Cox72d39fe2007-05-21 15:06:43 +0100500/**
501 * initio_read_eeprom - Retrieve configuration
502 * @base: Base of InitIO Host Adapter
503 *
504 * Retrieve the host adapter configuration data from E2Prom. If the
505 * data is invalid then the defaults are used and are also restored
506 * into the E2PROM. This forms the access point for the SCSI driver
507 * into the E2PROM layer, the other functions for the E2PROM are all
508 * internal use.
509 *
510 * Must be called single threaded, uses a shared global area.
511 */
512
513static void initio_read_eeprom(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Alan Cox72d39fe2007-05-21 15:06:43 +0100515 u8 gctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 i91unvramp = &i91unvram;
Alan Cox72d39fe2007-05-21 15:06:43 +0100518 /* Enable EEProm programming */
519 gctrl = inb(base + TUL_GCTRL);
520 outb(gctrl | TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
521 if (initio_se2_rd_all(base) != 1) {
522 initio_se2_update_all(base); /* setup default pattern */
523 initio_se2_rd_all(base); /* load again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100525 /* Disable EEProm programming */
526 gctrl = inb(base + TUL_GCTRL);
527 outb(gctrl & ~TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Alan Cox72d39fe2007-05-21 15:06:43 +0100530/**
531 * initio_stop_bm - stop bus master
532 * @host: InitIO we are stopping
533 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800534 * Stop any pending DMA operation, aborting the DMA if necessary
Alan Cox72d39fe2007-05-21 15:06:43 +0100535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Alan Cox72d39fe2007-05-21 15:06:43 +0100537static void initio_stop_bm(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539
Alan Cox72d39fe2007-05-21 15:06:43 +0100540 if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */
541 outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* wait Abort DMA xfer done */
Alan Cox72d39fe2007-05-21 15:06:43 +0100543 while ((inb(host->addr + TUL_Int) & XABT) == 0)
544 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100546 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Alan Cox72d39fe2007-05-21 15:06:43 +0100549/**
550 * initio_reset_scsi - Reset SCSI host controller
551 * @host: InitIO host to reset
552 * @seconds: Recovery time
553 *
554 * Perform a full reset of the SCSI subsystem.
555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Alan Cox72d39fe2007-05-21 15:06:43 +0100557static int initio_reset_scsi(struct initio_host * host, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Alan Cox72d39fe2007-05-21 15:06:43 +0100559 outb(TSC_RST_BUS, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Alan Cox72d39fe2007-05-21 15:06:43 +0100561 while (!((host->jsint = inb(host->addr + TUL_SInt)) & TSS_SCSIRST_INT))
562 cpu_relax();
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* reset tulip chip */
Alan Cox72d39fe2007-05-21 15:06:43 +0100565 outb(0, host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* Stall for a while, wait for target's firmware ready,make it 2 sec ! */
568 /* SONY 5200 tape drive won't work if only stall for 1 sec */
Alan Cox72d39fe2007-05-21 15:06:43 +0100569 /* FIXME: this is a very long busy wait right now */
570 initio_do_pause(seconds * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Alan Cox72d39fe2007-05-21 15:06:43 +0100572 inb(host->addr + TUL_SInt);
573 return SCSI_RESET_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Alan Cox72d39fe2007-05-21 15:06:43 +0100576/**
577 * initio_init - set up an InitIO host adapter
578 * @host: InitIO host adapter
579 * @num_scbs: Number of SCBS
580 * @bios_addr: BIOS address
581 *
582 * Set up the host adapter and devices according to the configuration
583 * retrieved from the E2PROM.
584 *
585 * Locking: Calls E2PROM layer code which is not re-enterable so must
586 * run single threaded for now.
587 */
588
589static void initio_init(struct initio_host * host, u8 *bios_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100592 u8 *flags;
593 u8 *heads;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Alan Cox72d39fe2007-05-21 15:06:43 +0100595 /* Get E2Prom configuration */
596 initio_read_eeprom(host->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (i91unvramp->NVM_SCSIInfo[0].NVM_NumOfTarg == 8)
Alan Cox72d39fe2007-05-21 15:06:43 +0100598 host->max_tar = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100600 host->max_tar = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Alan Cox72d39fe2007-05-21 15:06:43 +0100602 host->config = i91unvramp->NVM_SCSIInfo[0].NVM_ChConfig1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Alan Cox72d39fe2007-05-21 15:06:43 +0100604 host->scsi_id = i91unvramp->NVM_SCSIInfo[0].NVM_ChSCSIID;
605 host->idmask = ~(1 << host->scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Olaf Hering44456d32005-07-27 11:45:17 -0700607#ifdef CHK_PARITY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Enable parity error response */
Alan Cox72d39fe2007-05-21 15:06:43 +0100609 outb(inb(host->addr + TUL_PCMD) | 0x40, host->addr + TUL_PCMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610#endif
611
612 /* Mask all the interrupt */
Alan Cox72d39fe2007-05-21 15:06:43 +0100613 outb(0x1F, host->addr + TUL_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Alan Cox72d39fe2007-05-21 15:06:43 +0100615 initio_stop_bm(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* --- Initialize the tulip --- */
Alan Cox72d39fe2007-05-21 15:06:43 +0100617 outb(TSC_RST_CHIP, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* program HBA's SCSI ID */
Alan Cox72d39fe2007-05-21 15:06:43 +0100620 outb(host->scsi_id << 4, host->addr + TUL_SScsiId);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /* Enable Initiator Mode ,phase latch,alternate sync period mode,
623 disable SCSI reset */
Alan Cox72d39fe2007-05-21 15:06:43 +0100624 if (host->config & HCC_EN_PAR)
625 host->sconf1 = (TSC_INITDEFAULT | TSC_EN_SCSI_PAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100627 host->sconf1 = (TSC_INITDEFAULT);
628 outb(host->sconf1, host->addr + TUL_SConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Alan Cox72d39fe2007-05-21 15:06:43 +0100630 /* Enable HW reselect */
631 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Alan Cox72d39fe2007-05-21 15:06:43 +0100633 outb(0, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* selection time out = 250 ms */
Alan Cox72d39fe2007-05-21 15:06:43 +0100636 outb(153, host->addr + TUL_STimeOut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Alan Cox72d39fe2007-05-21 15:06:43 +0100638 /* Enable SCSI terminator */
639 outb((host->config & (HCC_ACT_TERM1 | HCC_ACT_TERM2)),
640 host->addr + TUL_XCtrl);
641 outb(((host->config & HCC_AUTO_TERM) >> 4) |
642 (inb(host->addr + TUL_GCTRL1) & 0xFE),
643 host->addr + TUL_GCTRL1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 for (i = 0,
Alan Cox72d39fe2007-05-21 15:06:43 +0100646 flags = & (i91unvramp->NVM_SCSIInfo[0].NVM_Targ0Config),
647 heads = bios_addr + 0x180;
648 i < host->max_tar;
649 i++, flags++) {
650 host->targets[i].flags = *flags & ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
651 if (host->targets[i].flags & TCF_EN_255)
652 host->targets[i].drv_flags = TCF_DRV_255_63;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100654 host->targets[i].drv_flags = 0;
655 host->targets[i].js_period = 0;
656 host->targets[i].sconfig0 = host->sconf1;
657 host->targets[i].heads = *heads++;
658 if (host->targets[i].heads == 255)
659 host->targets[i].drv_flags = TCF_DRV_255_63;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100661 host->targets[i].drv_flags = 0;
662 host->targets[i].sectors = *heads++;
663 host->targets[i].flags &= ~TCF_BUSY;
664 host->act_tags[i] = 0;
665 host->max_tags[i] = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 } /* for */
667 printk("i91u: PCI Base=0x%04X, IRQ=%d, BIOS=0x%04X0, SCSI ID=%d\n",
Alan Coxe9e42fa2007-10-15 20:51:32 +0100668 host->addr, host->pci_dev->irq,
Alan Cox72d39fe2007-05-21 15:06:43 +0100669 host->bios_addr, host->scsi_id);
670 /* Reset SCSI Bus */
671 if (host->config & HCC_SCSI_RESET) {
672 printk(KERN_INFO "i91u: Reset SCSI Bus ... \n");
673 initio_reset_scsi(host, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100675 outb(0x17, host->addr + TUL_SCFG1);
676 outb(0xE9, host->addr + TUL_SIntEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Alan Cox72d39fe2007-05-21 15:06:43 +0100679/**
680 * initio_alloc_scb - Allocate an SCB
681 * @host: InitIO host we are allocating for
682 *
683 * Walk the SCB list for the controller and allocate a free SCB if
684 * one exists.
685 */
686static struct scsi_ctrl_blk *initio_alloc_scb(struct initio_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Alan Cox72d39fe2007-05-21 15:06:43 +0100688 struct scsi_ctrl_blk *scb;
689 unsigned long flags;
690
691 spin_lock_irqsave(&host->avail_lock, flags);
692 if ((scb = host->first_avail) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100694 printk("find scb at %p\n", scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100696 if ((host->first_avail = scb->next) == NULL)
697 host->last_avail = NULL;
698 scb->next = NULL;
699 scb->status = SCB_RENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100701 spin_unlock_irqrestore(&host->avail_lock, flags);
702 return scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Alan Cox72d39fe2007-05-21 15:06:43 +0100705/**
706 * initio_release_scb - Release an SCB
707 * @host: InitIO host that owns the SCB
708 * @cmnd: SCB command block being returned
709 *
710 * Return an allocated SCB to the host free list
711 */
712
713static void initio_release_scb(struct initio_host * host, struct scsi_ctrl_blk * cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Alan Cox72d39fe2007-05-21 15:06:43 +0100715 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100718 printk("Release SCB %p; ", cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100720 spin_lock_irqsave(&(host->avail_lock), flags);
721 cmnd->srb = NULL;
722 cmnd->status = 0;
723 cmnd->next = NULL;
724 if (host->last_avail != NULL) {
725 host->last_avail->next = cmnd;
726 host->last_avail = cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100728 host->first_avail = cmnd;
729 host->last_avail = cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100731 spin_unlock_irqrestore(&(host->avail_lock), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100735static void initio_append_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737
738#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100739 printk("Append pend SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100741 scbp->status = SCB_PEND;
742 scbp->next = NULL;
743 if (host->last_pending != NULL) {
744 host->last_pending->next = scbp;
745 host->last_pending = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100747 host->first_pending = scbp;
748 host->last_pending = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750}
751
752/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100753static void initio_push_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755
756#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100757 printk("Push pend SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100759 scbp->status = SCB_PEND;
760 if ((scbp->next = host->first_pending) != NULL) {
761 host->first_pending = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100763 host->first_pending = scbp;
764 host->last_pending = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766}
767
Alan Cox72d39fe2007-05-21 15:06:43 +0100768static struct scsi_ctrl_blk *initio_find_first_pend_scb(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Alan Cox72d39fe2007-05-21 15:06:43 +0100770 struct scsi_ctrl_blk *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772
Alan Cox72d39fe2007-05-21 15:06:43 +0100773 first = host->first_pending;
774 while (first != NULL) {
775 if (first->opcode != ExecSCSI)
776 return first;
777 if (first->tagmsg == 0) {
778 if ((host->act_tags[first->target] == 0) &&
779 !(host->targets[first->target].flags & TCF_BUSY))
780 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100782 if ((host->act_tags[first->target] >=
783 host->max_tags[first->target]) |
784 (host->targets[first->target].flags & TCF_BUSY)) {
785 first = first->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 continue;
787 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100788 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100790 first = first->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100792 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
Alan Cox72d39fe2007-05-21 15:06:43 +0100794
795static void initio_unlink_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Alan Cox72d39fe2007-05-21 15:06:43 +0100797 struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100800 printk("unlink pend SCB %p; ", scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801#endif
802
Alan Cox72d39fe2007-05-21 15:06:43 +0100803 prev = tmp = host->first_pending;
804 while (tmp != NULL) {
805 if (scb == tmp) { /* Unlink this SCB */
806 if (tmp == host->first_pending) {
807 if ((host->first_pending = tmp->next) == NULL)
808 host->last_pending = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100810 prev->next = tmp->next;
811 if (tmp == host->last_pending)
812 host->last_pending = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100814 tmp->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 break;
816 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100817 prev = tmp;
818 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
Alan Cox72d39fe2007-05-21 15:06:43 +0100821
822static void initio_append_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824
825#if DEBUG_QUEUE
Stuart Swalese2d435e2008-01-23 20:00:48 +0000826 printk("append busy SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100828 if (scbp->tagmsg)
829 host->act_tags[scbp->target]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100831 host->targets[scbp->target].flags |= TCF_BUSY;
832 scbp->status = SCB_BUSY;
833 scbp->next = NULL;
834 if (host->last_busy != NULL) {
835 host->last_busy->next = scbp;
836 host->last_busy = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100838 host->first_busy = scbp;
839 host->last_busy = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841}
842
843/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100844static struct scsi_ctrl_blk *initio_pop_busy_scb(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Alan Cox72d39fe2007-05-21 15:06:43 +0100846 struct scsi_ctrl_blk *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848
Alan Cox72d39fe2007-05-21 15:06:43 +0100849 if ((tmp = host->first_busy) != NULL) {
850 if ((host->first_busy = tmp->next) == NULL)
851 host->last_busy = NULL;
852 tmp->next = NULL;
853 if (tmp->tagmsg)
854 host->act_tags[tmp->target]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100856 host->targets[tmp->target].flags &= ~TCF_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100859 printk("Pop busy SCB %p; ", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100861 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100865static void initio_unlink_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Alan Cox72d39fe2007-05-21 15:06:43 +0100867 struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100870 printk("unlink busy SCB %p; ", scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#endif
872
Alan Cox72d39fe2007-05-21 15:06:43 +0100873 prev = tmp = host->first_busy;
874 while (tmp != NULL) {
875 if (scb == tmp) { /* Unlink this SCB */
876 if (tmp == host->first_busy) {
877 if ((host->first_busy = tmp->next) == NULL)
878 host->last_busy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100880 prev->next = tmp->next;
881 if (tmp == host->last_busy)
882 host->last_busy = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100884 tmp->next = NULL;
885 if (tmp->tagmsg)
886 host->act_tags[tmp->target]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100888 host->targets[tmp->target].flags &= ~TCF_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 break;
890 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100891 prev = tmp;
892 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 return;
895}
896
Alan Cox72d39fe2007-05-21 15:06:43 +0100897struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Alan Cox72d39fe2007-05-21 15:06:43 +0100899 struct scsi_ctrl_blk *tmp, *prev;
900 u16 scbp_tarlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902
Alan Cox72d39fe2007-05-21 15:06:43 +0100903 prev = tmp = host->first_busy;
904 while (tmp != NULL) {
905 scbp_tarlun = (tmp->lun << 8) | (tmp->target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (scbp_tarlun == tarlun) { /* Unlink this SCB */
907 break;
908 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100909 prev = tmp;
910 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100913 printk("find busy SCB %p; ", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100915 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Alan Cox72d39fe2007-05-21 15:06:43 +0100918static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100921 printk("append done SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922#endif
923
Alan Cox72d39fe2007-05-21 15:06:43 +0100924 scbp->status = SCB_DONE;
925 scbp->next = NULL;
926 if (host->last_done != NULL) {
927 host->last_done->next = scbp;
928 host->last_done = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100930 host->first_done = scbp;
931 host->last_done = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933}
934
Alan Cox72d39fe2007-05-21 15:06:43 +0100935struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Alan Cox72d39fe2007-05-21 15:06:43 +0100937 struct scsi_ctrl_blk *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Alan Cox72d39fe2007-05-21 15:06:43 +0100939 if ((tmp = host->first_done) != NULL) {
940 if ((host->first_done = tmp->next) == NULL)
941 host->last_done = NULL;
942 tmp->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100945 printk("find done SCB %p; ",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100947 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Alan Cox72d39fe2007-05-21 15:06:43 +0100950static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Alan Cox72d39fe2007-05-21 15:06:43 +0100952 unsigned long flags;
953 struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Alan Cox72d39fe2007-05-21 15:06:43 +0100955 spin_lock_irqsave(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Alan Cox72d39fe2007-05-21 15:06:43 +0100957 if ((host->semaph == 0) && (host->active == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* disable Jasmin SCSI Int */
Alan Cox72d39fe2007-05-21 15:06:43 +0100959 outb(0x1F, host->addr + TUL_Mask);
960 spin_unlock_irqrestore(&host->semaph_lock, flags);
961 /* FIXME: synchronize_irq needed ? */
962 tulip_main(host);
963 spin_lock_irqsave(&host->semaph_lock, flags);
964 host->semaph = 1;
965 outb(0x0F, host->addr + TUL_Mask);
966 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return SCSI_ABORT_SNOOZE;
968 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100969 prev = tmp = host->first_pending; /* Check Pend queue */
970 while (tmp != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* 07/27/98 */
Alan Cox72d39fe2007-05-21 15:06:43 +0100972 if (tmp->srb == srbp) {
973 if (tmp == host->active) {
974 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return SCSI_ABORT_BUSY;
Alan Cox72d39fe2007-05-21 15:06:43 +0100976 } else if (tmp == host->first_pending) {
977 if ((host->first_pending = tmp->next) == NULL)
978 host->last_pending = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100980 prev->next = tmp->next;
981 if (tmp == host->last_pending)
982 host->last_pending = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100984 tmp->hastat = HOST_ABORTED;
985 tmp->flags |= SCF_DONE;
986 if (tmp->flags & SCF_POST)
987 (*tmp->post) ((u8 *) host, (u8 *) tmp);
988 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return SCSI_ABORT_SUCCESS;
990 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100991 prev = tmp;
992 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
Alan Cox72d39fe2007-05-21 15:06:43 +0100995 prev = tmp = host->first_busy; /* Check Busy queue */
996 while (tmp != NULL) {
997 if (tmp->srb == srbp) {
998 if (tmp == host->active) {
999 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return SCSI_ABORT_BUSY;
Alan Cox72d39fe2007-05-21 15:06:43 +01001001 } else if (tmp->tagmsg == 0) {
1002 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return SCSI_ABORT_BUSY;
1004 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001005 host->act_tags[tmp->target]--;
1006 if (tmp == host->first_busy) {
1007 if ((host->first_busy = tmp->next) == NULL)
1008 host->last_busy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001010 prev->next = tmp->next;
1011 if (tmp == host->last_busy)
1012 host->last_busy = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001014 tmp->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016
Alan Cox72d39fe2007-05-21 15:06:43 +01001017 tmp->hastat = HOST_ABORTED;
1018 tmp->flags |= SCF_DONE;
1019 if (tmp->flags & SCF_POST)
1020 (*tmp->post) ((u8 *) host, (u8 *) tmp);
1021 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return SCSI_ABORT_SUCCESS;
1023 }
1024 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001025 prev = tmp;
1026 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001028 spin_unlock_irqrestore(&host->semaph_lock, flags);
1029 return SCSI_ABORT_NOT_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +01001033static int initio_bad_seq(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Alan Cox72d39fe2007-05-21 15:06:43 +01001035 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Alan Cox72d39fe2007-05-21 15:06:43 +01001037 printk("initio_bad_seg c=%d\n", host->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Alan Cox72d39fe2007-05-21 15:06:43 +01001039 if ((scb = host->active) != NULL) {
1040 initio_unlink_busy_scb(host, scb);
1041 scb->hastat = HOST_BAD_PHAS;
1042 scb->tastat = 0;
1043 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001045 initio_stop_bm(host);
1046 initio_reset_scsi(host, 8); /* 7/29/98 */
1047 return initio_post_scsi_rst(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Adrian Bunka2ba1922005-06-25 14:58:50 -07001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +01001052static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Alan Cox72d39fe2007-05-21 15:06:43 +01001054 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Alan Cox72d39fe2007-05-21 15:06:43 +01001056 scb->mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Alan Cox72d39fe2007-05-21 15:06:43 +01001058 scb->sgidx = 0;
1059 scb->sgmax = scb->sglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Alan Cox72d39fe2007-05-21 15:06:43 +01001061 spin_lock_irqsave(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Alan Cox72d39fe2007-05-21 15:06:43 +01001063 initio_append_pend_scb(host, scb); /* Append this SCB to Pending queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065/* VVVVV 07/21/98 */
Alan Cox72d39fe2007-05-21 15:06:43 +01001066 if (host->semaph == 1) {
1067 /* Disable Jasmin SCSI Int */
1068 outb(0x1F, host->addr + TUL_Mask);
1069 host->semaph = 0;
1070 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Alan Cox72d39fe2007-05-21 15:06:43 +01001072 tulip_main(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Alan Cox72d39fe2007-05-21 15:06:43 +01001074 spin_lock_irqsave(&host->semaph_lock, flags);
1075 host->semaph = 1;
1076 outb(0x0F, host->addr + TUL_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001078 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return;
1080}
1081
1082/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +01001083static int initio_isr(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Alan Cox72d39fe2007-05-21 15:06:43 +01001085 if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) {
1086 if (host->semaph == 1) {
1087 outb(0x1F, host->addr + TUL_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /* Disable Tulip SCSI Int */
Alan Cox72d39fe2007-05-21 15:06:43 +01001089 host->semaph = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Alan Cox72d39fe2007-05-21 15:06:43 +01001091 tulip_main(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Alan Cox72d39fe2007-05-21 15:06:43 +01001093 host->semaph = 1;
1094 outb(0x0F, host->addr + TUL_Mask);
1095 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Alan Cox72d39fe2007-05-21 15:06:43 +01001101static int tulip_main(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Alan Cox72d39fe2007-05-21 15:06:43 +01001103 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001106 tulip_scsi(host); /* Call tulip_scsi */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Alan Cox72d39fe2007-05-21 15:06:43 +01001108 /* Walk the list of completed SCBs */
1109 while ((scb = initio_find_done_scb(host)) != NULL) { /* find done entry */
1110 if (scb->tastat == INI_QUEUE_FULL) {
1111 host->max_tags[scb->target] =
1112 host->act_tags[scb->target] - 1;
1113 scb->tastat = 0;
1114 initio_append_pend_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 continue;
1116 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001117 if (!(scb->mode & SCM_RSENS)) { /* not in auto req. sense mode */
1118 if (scb->tastat == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /* clr sync. nego flag */
1121
Alan Cox72d39fe2007-05-21 15:06:43 +01001122 if (scb->flags & SCF_SENSE) {
1123 u8 len;
1124 len = scb->senselen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (len == 0)
1126 len = 1;
Alan Cox72d39fe2007-05-21 15:06:43 +01001127 scb->buflen = scb->senselen;
1128 scb->bufptr = scb->senseptr;
1129 scb->flags &= ~(SCF_SG | SCF_DIR); /* for xfer_data_in */
1130 /* so, we won't report wrong direction in xfer_data_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 and won't report HOST_DO_DU in state_6 */
Alan Cox72d39fe2007-05-21 15:06:43 +01001132 scb->mode = SCM_RSENS;
1133 scb->ident &= 0xBF; /* Disable Disconnect */
1134 scb->tagmsg = 0;
1135 scb->tastat = 0;
1136 scb->cdblen = 6;
1137 scb->cdb[0] = SCSICMD_RequestSense;
1138 scb->cdb[1] = 0;
1139 scb->cdb[2] = 0;
1140 scb->cdb[3] = 0;
1141 scb->cdb[4] = len;
1142 scb->cdb[5] = 0;
1143 initio_push_pend_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 break;
1145 }
1146 }
1147 } else { /* in request sense mode */
1148
Alan Cox72d39fe2007-05-21 15:06:43 +01001149 if (scb->tastat == 2) { /* check contition status again after sending
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 requset sense cmd 0x3 */
Alan Cox72d39fe2007-05-21 15:06:43 +01001151 scb->hastat = HOST_BAD_PHAS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001153 scb->tastat = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001155 scb->flags |= SCF_DONE;
1156 if (scb->flags & SCF_POST) {
1157 /* FIXME: only one post method and lose casts */
1158 (*scb->post) ((u8 *) host, (u8 *) scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /* find_active: */
Alan Cox72d39fe2007-05-21 15:06:43 +01001162 if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 continue;
Alan Cox72d39fe2007-05-21 15:06:43 +01001164 if (host->active) /* return to OS and wait for xfer_done_ISR/Selected_ISR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return 1; /* return to OS, enable interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* Check pending SCB */
Alan Cox72d39fe2007-05-21 15:06:43 +01001167 if (initio_find_first_pend_scb(host) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return 1; /* return to OS, enable interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 } /* End of for loop */
1170 /* statement won't reach here */
1171}
1172
Alan Cox72d39fe2007-05-21 15:06:43 +01001173static void tulip_scsi(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Alan Cox72d39fe2007-05-21 15:06:43 +01001175 struct scsi_ctrl_blk *scb;
1176 struct target_control *active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /* make sure to service interrupt asap */
Alan Cox72d39fe2007-05-21 15:06:43 +01001179 if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) {
1180 host->phase = host->jsstatus0 & TSS_PH_MASK;
1181 host->jsstatus1 = inb(host->addr + TUL_SStatus1);
1182 host->jsint = inb(host->addr + TUL_SInt);
1183 if (host->jsint & TSS_SCSIRST_INT) { /* SCSI bus reset detected */
1184 int_initio_scsi_rst(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return;
1186 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001187 if (host->jsint & TSS_RESEL_INT) { /* if selected/reselected interrupt */
1188 if (int_initio_resel(host) == 0)
1189 initio_next_state(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return;
1191 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001192 if (host->jsint & TSS_SEL_TIMEOUT) {
1193 int_initio_busfree(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return;
1195 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001196 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
1197 int_initio_busfree(host); /* unexpected bus free or sel timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return;
1199 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001200 if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) { /* func complete or Bus service */
1201 if ((scb = host->active) != NULL)
1202 initio_next_state(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return;
1204 }
1205 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001206 if (host->active != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return;
1208
Alan Cox72d39fe2007-05-21 15:06:43 +01001209 if ((scb = initio_find_first_pend_scb(host)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return;
1211
1212 /* program HBA's SCSI ID & target SCSI ID */
Alan Cox72d39fe2007-05-21 15:06:43 +01001213 outb((host->scsi_id << 4) | (scb->target & 0x0F),
1214 host->addr + TUL_SScsiId);
1215 if (scb->opcode == ExecSCSI) {
1216 active_tc = &host->targets[scb->target];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Alan Cox72d39fe2007-05-21 15:06:43 +01001218 if (scb->tagmsg)
1219 active_tc->drv_flags |= TCF_DRV_EN_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001221 active_tc->drv_flags &= ~TCF_DRV_EN_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Alan Cox72d39fe2007-05-21 15:06:43 +01001223 outb(active_tc->js_period, host->addr + TUL_SPeriod);
1224 if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) { /* do wdtr negotiation */
1225 initio_select_atn_stop(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001227 if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) { /* do sync negotiation */
1228 initio_select_atn_stop(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001230 if (scb->tagmsg)
1231 initio_select_atn3(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001233 initio_select_atn(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001236 if (scb->flags & SCF_POLL) {
1237 while (wait_tulip(host) != -1) {
1238 if (initio_next_state(host) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 break;
1240 }
1241 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001242 } else if (scb->opcode == BusDevRst) {
1243 initio_select_atn_stop(host, scb);
1244 scb->next_state = 8;
1245 if (scb->flags & SCF_POLL) {
1246 while (wait_tulip(host) != -1) {
1247 if (initio_next_state(host) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 break;
1249 }
1250 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001251 } else if (scb->opcode == AbortCmd) {
1252 if (initio_abort_srb(host, scb->srb) != 0) {
1253 initio_unlink_pend_scb(host, scb);
1254 initio_release_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001256 scb->opcode = BusDevRst;
1257 initio_select_atn_stop(host, scb);
1258 scb->next_state = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001261 initio_unlink_pend_scb(host, scb);
1262 scb->hastat = 0x16; /* bad command */
1263 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265 return;
1266}
1267
Alan Cox72d39fe2007-05-21 15:06:43 +01001268/**
1269 * initio_next_state - Next SCSI state
1270 * @host: InitIO host we are processing
1271 *
1272 * Progress the active command block along the state machine
1273 * until we hit a state which we must wait for activity to occur.
1274 *
1275 * Returns zero or a negative code.
1276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Alan Cox72d39fe2007-05-21 15:06:43 +01001278static int initio_next_state(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 int next;
1281
Alan Cox72d39fe2007-05-21 15:06:43 +01001282 next = host->active->next_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 for (;;) {
1284 switch (next) {
1285 case 1:
Alan Cox72d39fe2007-05-21 15:06:43 +01001286 next = initio_state_1(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
1288 case 2:
Alan Cox72d39fe2007-05-21 15:06:43 +01001289 next = initio_state_2(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 break;
1291 case 3:
Alan Cox72d39fe2007-05-21 15:06:43 +01001292 next = initio_state_3(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
1294 case 4:
Alan Cox72d39fe2007-05-21 15:06:43 +01001295 next = initio_state_4(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 break;
1297 case 5:
Alan Cox72d39fe2007-05-21 15:06:43 +01001298 next = initio_state_5(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 break;
1300 case 6:
Alan Cox72d39fe2007-05-21 15:06:43 +01001301 next = initio_state_6(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 break;
1303 case 7:
Alan Cox72d39fe2007-05-21 15:06:43 +01001304 next = initio_state_7(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 break;
1306 case 8:
Alan Cox72d39fe2007-05-21 15:06:43 +01001307 return initio_bus_device_reset(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001309 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311 if (next <= 0)
1312 return next;
1313 }
1314}
1315
1316
Alan Cox72d39fe2007-05-21 15:06:43 +01001317/**
1318 * initio_state_1 - SCSI state machine
1319 * @host: InitIO host we are controlling
1320 *
1321 * Perform SCSI state processing for Select/Attention/Stop
1322 */
1323
1324static int initio_state_1(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Alan Cox72d39fe2007-05-21 15:06:43 +01001326 struct scsi_ctrl_blk *scb = host->active;
1327 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328#if DEBUG_STATE
1329 printk("-s1-");
1330#endif
1331
Alan Cox72d39fe2007-05-21 15:06:43 +01001332 /* Move the SCB from pending to busy */
1333 initio_unlink_pend_scb(host, scb);
1334 initio_append_busy_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Alan Cox72d39fe2007-05-21 15:06:43 +01001336 outb(active_tc->sconfig0, host->addr + TUL_SConfig );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* ATN on */
Alan Cox72d39fe2007-05-21 15:06:43 +01001338 if (host->phase == MSG_OUT) {
1339 outb(TSC_EN_BUS_IN | TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1340 outb(scb->ident, host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Alan Cox72d39fe2007-05-21 15:06:43 +01001342 if (scb->tagmsg) {
1343 outb(scb->tagmsg, host->addr + TUL_SFifo);
1344 outb(scb->tagid, host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001346 if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {
1347 active_tc->flags |= TCF_WDTR_DONE;
1348 outb(MSG_EXTEND, host->addr + TUL_SFifo);
1349 outb(2, host->addr + TUL_SFifo); /* Extended msg length */
1350 outb(3, host->addr + TUL_SFifo); /* Sync request */
1351 outb(1, host->addr + TUL_SFifo); /* Start from 16 bits */
1352 } else if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {
1353 active_tc->flags |= TCF_SYNC_DONE;
1354 outb(MSG_EXTEND, host->addr + TUL_SFifo);
1355 outb(3, host->addr + TUL_SFifo); /* extended msg length */
1356 outb(1, host->addr + TUL_SFifo); /* sync request */
1357 outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1358 outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001360 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1361 if (wait_tulip(host) == -1)
1362 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001364 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1365 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1366 /* Into before CDB xfer */
1367 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
1370
Alan Cox72d39fe2007-05-21 15:06:43 +01001371/**
1372 * initio_state_2 - SCSI state machine
1373 * @host: InitIO host we are controlling
1374 *
1375 * state after selection with attention
1376 * state after selection with attention3
1377 */
1378
1379static int initio_state_2(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Alan Cox72d39fe2007-05-21 15:06:43 +01001381 struct scsi_ctrl_blk *scb = host->active;
1382 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383#if DEBUG_STATE
1384 printk("-s2-");
1385#endif
1386
Alan Cox72d39fe2007-05-21 15:06:43 +01001387 initio_unlink_pend_scb(host, scb);
1388 initio_append_busy_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Alan Cox72d39fe2007-05-21 15:06:43 +01001390 outb(active_tc->sconfig0, host->addr + TUL_SConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Alan Cox72d39fe2007-05-21 15:06:43 +01001392 if (host->jsstatus1 & TSS_CMD_PH_CMP)
1393 return 4;
1394
1395 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1396 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1397 /* Into before CDB xfer */
1398 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
Alan Cox72d39fe2007-05-21 15:06:43 +01001401/**
1402 * initio_state_3 - SCSI state machine
1403 * @host: InitIO host we are controlling
1404 *
1405 * state before CDB xfer is done
1406 */
1407
1408static int initio_state_3(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Alan Cox72d39fe2007-05-21 15:06:43 +01001410 struct scsi_ctrl_blk *scb = host->active;
1411 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 int i;
1413
1414#if DEBUG_STATE
1415 printk("-s3-");
1416#endif
1417 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001418 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 case CMD_OUT: /* Command out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001420 for (i = 0; i < (int) scb->cdblen; i++)
1421 outb(scb->cdb[i], host->addr + TUL_SFifo);
1422 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1423 if (wait_tulip(host) == -1)
1424 return -1;
1425 if (host->phase == CMD_OUT)
1426 return initio_bad_seq(host);
1427 return 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 case MSG_IN: /* Message in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001430 scb->next_state = 3;
1431 if (initio_msgin(host) == -1)
1432 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 break;
1434
1435 case STATUS_IN: /* Status phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001436 if (initio_status_msg(host) == -1)
1437 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 break;
1439
1440 case MSG_OUT: /* Message out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001441 if (active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) {
1442 outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
1443 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1444 if (wait_tulip(host) == -1)
1445 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001447 active_tc->flags |= TCF_SYNC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Alan Cox72d39fe2007-05-21 15:06:43 +01001449 outb(MSG_EXTEND, host->addr + TUL_SFifo);
1450 outb(3, host->addr + TUL_SFifo); /* ext. msg len */
1451 outb(1, host->addr + TUL_SFifo); /* sync request */
1452 outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1453 outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */
1454 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1455 if (wait_tulip(host) == -1)
1456 return -1;
1457 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1458 outb(inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7), host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 }
1461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001463 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465 }
1466}
1467
Alan Cox72d39fe2007-05-21 15:06:43 +01001468/**
1469 * initio_state_4 - SCSI state machine
1470 * @host: InitIO host we are controlling
1471 *
1472 * SCSI state machine. State 4
1473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Alan Cox72d39fe2007-05-21 15:06:43 +01001475static int initio_state_4(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Alan Cox72d39fe2007-05-21 15:06:43 +01001477 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479#if DEBUG_STATE
1480 printk("-s4-");
1481#endif
Alan Cox72d39fe2007-05-21 15:06:43 +01001482 if ((scb->flags & SCF_DIR) == SCF_NO_XF) {
1483 return 6; /* Go to state 6 (After data) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001486 if (scb->buflen == 0)
1487 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Alan Cox72d39fe2007-05-21 15:06:43 +01001489 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 case STATUS_IN: /* Status phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001492 if ((scb->flags & SCF_DIR) != 0) /* if direction bit set then report data underrun */
1493 scb->hastat = HOST_DO_DU;
1494 if ((initio_status_msg(host)) == -1)
1495 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 break;
1497
1498 case MSG_IN: /* Message in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001499 scb->next_state = 0x4;
1500 if (initio_msgin(host) == -1)
1501 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 break;
1503
1504 case MSG_OUT: /* Message out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001505 if (host->jsstatus0 & TSS_PAR_ERROR) {
1506 scb->buflen = 0;
1507 scb->hastat = HOST_DO_DU;
1508 if (initio_msgout_ide(host) == -1)
1509 return -1;
1510 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001512 outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
1513 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1514 if (wait_tulip(host) == -1)
1515 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
1517 break;
1518
1519 case DATA_IN: /* Data in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001520 return initio_xfer_data_in(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 case DATA_OUT: /* Data out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001523 return initio_xfer_data_out(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001526 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 }
1529}
1530
1531
Alan Cox72d39fe2007-05-21 15:06:43 +01001532/**
1533 * initio_state_5 - SCSI state machine
1534 * @host: InitIO host we are controlling
1535 *
1536 * State after dma xfer done or phase change before xfer done
1537 */
1538
1539static int initio_state_5(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Alan Cox72d39fe2007-05-21 15:06:43 +01001541 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 long cnt, xcnt; /* cannot use unsigned !! code: if (xcnt < 0) */
1543
1544#if DEBUG_STATE
1545 printk("-s5-");
1546#endif
Alan Cox72d39fe2007-05-21 15:06:43 +01001547 /*------ get remaining count -------*/
1548 cnt = inl(host->addr + TUL_SCnt0) & 0x0FFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Alan Cox72d39fe2007-05-21 15:06:43 +01001550 if (inb(host->addr + TUL_XCmd) & 0x20) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /* ----------------------- DATA_IN ----------------------------- */
1552 /* check scsi parity error */
Alan Cox72d39fe2007-05-21 15:06:43 +01001553 if (host->jsstatus0 & TSS_PAR_ERROR)
1554 scb->hastat = HOST_DO_DU;
1555 if (inb(host->addr + TUL_XStatus) & XPEND) { /* DMA xfer pending, Send STOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /* tell Hardware scsi xfer has been terminated */
Alan Cox72d39fe2007-05-21 15:06:43 +01001557 outb(inb(host->addr + TUL_XCtrl) | 0x80, host->addr + TUL_XCtrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /* wait until DMA xfer not pending */
Alan Cox72d39fe2007-05-21 15:06:43 +01001559 while (inb(host->addr + TUL_XStatus) & XPEND)
1560 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001563 /*-------- DATA OUT -----------*/
1564 if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) {
1565 if (host->active_tc->js_period & TSC_WIDE_SCSI)
1566 cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001568 cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001570 if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */
1571 outb(TAX_X_ABT, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* wait Abort DMA xfer done */
Alan Cox72d39fe2007-05-21 15:06:43 +01001573 while ((inb(host->addr + TUL_Int) & XABT) == 0)
1574 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001576 if ((cnt == 1) && (host->phase == DATA_OUT)) {
1577 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1578 if (wait_tulip(host) == -1)
1579 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 cnt = 0;
1581 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001582 if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0)
1583 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (cnt == 0) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001587 scb->buflen = 0;
1588 return 6; /* After Data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590 /* Update active data pointer */
Alan Cox72d39fe2007-05-21 15:06:43 +01001591 xcnt = (long) scb->buflen - cnt; /* xcnt== bytes already xferred */
1592 scb->buflen = (u32) cnt; /* cnt == bytes left to be xferred */
1593 if (scb->flags & SCF_SG) {
1594 struct sg_entry *sgp;
1595 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Alan Cox72d39fe2007-05-21 15:06:43 +01001597 sgp = &scb->sglist[scb->sgidx];
1598 for (i = scb->sgidx; i < scb->sgmax; sgp++, i++) {
1599 xcnt -= (long) sgp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (xcnt < 0) { /* this sgp xfer half done */
Alan Cox72d39fe2007-05-21 15:06:43 +01001601 xcnt += (long) sgp->len; /* xcnt == bytes xferred in this sgp */
1602 sgp->data += (u32) xcnt; /* new ptr to be xfer */
1603 sgp->len -= (u32) xcnt; /* new len to be xfer */
1604 scb->bufptr += ((u32) (i - scb->sgidx) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /* new SG table ptr */
Alan Cox72d39fe2007-05-21 15:06:43 +01001606 scb->sglen = (u8) (scb->sgmax - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /* new SG table len */
Alan Cox72d39fe2007-05-21 15:06:43 +01001608 scb->sgidx = (u16) i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /* for next disc and come in this loop */
Alan Cox72d39fe2007-05-21 15:06:43 +01001610 return 4; /* Go to state 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 /* else (xcnt >= 0 , i.e. this sgp already xferred */
1613 } /* for */
Alan Cox72d39fe2007-05-21 15:06:43 +01001614 return 6; /* Go to state 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001616 scb->bufptr += (u32) xcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001618 return 4; /* Go to state 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Alan Cox72d39fe2007-05-21 15:06:43 +01001621/**
1622 * initio_state_6 - SCSI state machine
1623 * @host: InitIO host we are controlling
1624 *
1625 * State after Data phase
1626 */
1627
1628static int initio_state_6(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Alan Cox72d39fe2007-05-21 15:06:43 +01001630 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632#if DEBUG_STATE
1633 printk("-s6-");
1634#endif
1635 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001636 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 case STATUS_IN: /* Status phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001638 if ((initio_status_msg(host)) == -1)
1639 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641
1642 case MSG_IN: /* Message in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001643 scb->next_state = 6;
1644 if ((initio_msgin(host)) == -1)
1645 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 break;
1647
1648 case MSG_OUT: /* Message out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001649 outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
1650 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1651 if (wait_tulip(host) == -1)
1652 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654
1655 case DATA_IN: /* Data in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001656 return initio_xpad_in(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 case DATA_OUT: /* Data out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001659 return initio_xpad_out(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001662 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664 }
1665}
1666
Alan Cox72d39fe2007-05-21 15:06:43 +01001667/**
1668 * initio_state_7 - SCSI state machine
1669 * @host: InitIO host we are controlling
1670 *
1671 */
1672
1673int initio_state_7(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 int cnt, i;
1676
1677#if DEBUG_STATE
1678 printk("-s7-");
1679#endif
1680 /* flush SCSI FIFO */
Alan Cox72d39fe2007-05-21 15:06:43 +01001681 cnt = inb(host->addr + TUL_SFifoCnt) & 0x1F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (cnt) {
1683 for (i = 0; i < cnt; i++)
Alan Cox72d39fe2007-05-21 15:06:43 +01001684 inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001686 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 case DATA_IN: /* Data in phase */
1688 case DATA_OUT: /* Data out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001689 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001691 return 6; /* Go to state 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693}
1694
Alan Cox72d39fe2007-05-21 15:06:43 +01001695/**
1696 * initio_xfer_data_in - Commence data input
1697 * @host: InitIO host in use
1698 *
1699 * Commence a block of data transfer. The transfer itself will
1700 * be managed by the controller and we will get a completion (or
1701 * failure) interrupt.
1702 */
1703static int initio_xfer_data_in(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Alan Cox72d39fe2007-05-21 15:06:43 +01001705 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Alan Cox72d39fe2007-05-21 15:06:43 +01001707 if ((scb->flags & SCF_DIR) == SCF_DOUT)
1708 return 6; /* wrong direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Alan Cox72d39fe2007-05-21 15:06:43 +01001710 outl(scb->buflen, host->addr + TUL_SCnt0);
1711 outb(TSC_XF_DMA_IN, host->addr + TUL_SCmd); /* 7/25/95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Alan Cox72d39fe2007-05-21 15:06:43 +01001713 if (scb->flags & SCF_SG) { /* S/G xfer */
1714 outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1715 outl(scb->bufptr, host->addr + TUL_XAddH);
1716 outb(TAX_SG_IN, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001718 outl(scb->buflen, host->addr + TUL_XCntH);
1719 outl(scb->bufptr, host->addr + TUL_XAddH);
1720 outb(TAX_X_IN, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001722 scb->next_state = 0x5;
1723 return 0; /* return to OS, wait xfer done , let jas_isr come in */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Alan Cox72d39fe2007-05-21 15:06:43 +01001726/**
1727 * initio_xfer_data_out - Commence data output
1728 * @host: InitIO host in use
1729 *
1730 * Commence a block of data transfer. The transfer itself will
1731 * be managed by the controller and we will get a completion (or
1732 * failure) interrupt.
1733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Alan Cox72d39fe2007-05-21 15:06:43 +01001735static int initio_xfer_data_out(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Alan Cox72d39fe2007-05-21 15:06:43 +01001737 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Alan Cox72d39fe2007-05-21 15:06:43 +01001739 if ((scb->flags & SCF_DIR) == SCF_DIN)
1740 return 6; /* wrong direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Alan Cox72d39fe2007-05-21 15:06:43 +01001742 outl(scb->buflen, host->addr + TUL_SCnt0);
1743 outb(TSC_XF_DMA_OUT, host->addr + TUL_SCmd);
1744
1745 if (scb->flags & SCF_SG) { /* S/G xfer */
1746 outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1747 outl(scb->bufptr, host->addr + TUL_XAddH);
1748 outb(TAX_SG_OUT, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001750 outl(scb->buflen, host->addr + TUL_XCntH);
1751 outl(scb->bufptr, host->addr + TUL_XAddH);
1752 outb(TAX_X_OUT, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
Alan Cox72d39fe2007-05-21 15:06:43 +01001755 scb->next_state = 0x5;
1756 return 0; /* return to OS, wait xfer done , let jas_isr come in */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Alan Cox72d39fe2007-05-21 15:06:43 +01001759int initio_xpad_in(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Alan Cox72d39fe2007-05-21 15:06:43 +01001761 struct scsi_ctrl_blk *scb = host->active;
1762 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Alan Cox72d39fe2007-05-21 15:06:43 +01001764 if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1765 scb->hastat = HOST_DO_DU; /* over run */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001767 if (active_tc->js_period & TSC_WIDE_SCSI)
1768 outl(2, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001770 outl(1, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Alan Cox72d39fe2007-05-21 15:06:43 +01001772 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1773 if (wait_tulip(host) == -1)
1774 return -1;
1775 if (host->phase != DATA_IN) {
1776 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1777 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001779 inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781}
1782
Alan Cox72d39fe2007-05-21 15:06:43 +01001783int initio_xpad_out(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Alan Cox72d39fe2007-05-21 15:06:43 +01001785 struct scsi_ctrl_blk *scb = host->active;
1786 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Alan Cox72d39fe2007-05-21 15:06:43 +01001788 if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1789 scb->hastat = HOST_DO_DU; /* over run */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001791 if (active_tc->js_period & TSC_WIDE_SCSI)
1792 outl(2, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001794 outl(1, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Alan Cox72d39fe2007-05-21 15:06:43 +01001796 outb(0, host->addr + TUL_SFifo);
1797 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1798 if ((wait_tulip(host)) == -1)
1799 return -1;
1800 if (host->phase != DATA_OUT) { /* Disable wide CPU to allow read 16 bits */
1801 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1802 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1803 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 }
1806}
1807
Alan Cox72d39fe2007-05-21 15:06:43 +01001808int initio_status_msg(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{ /* status & MSG_IN */
Alan Cox72d39fe2007-05-21 15:06:43 +01001810 struct scsi_ctrl_blk *scb = host->active;
1811 u8 msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Alan Cox72d39fe2007-05-21 15:06:43 +01001813 outb(TSC_CMD_COMP, host->addr + TUL_SCmd);
1814 if (wait_tulip(host) == -1)
1815 return -1;
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /* get status */
Alan Cox72d39fe2007-05-21 15:06:43 +01001818 scb->tastat = inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Alan Cox72d39fe2007-05-21 15:06:43 +01001820 if (host->phase == MSG_OUT) {
1821 if (host->jsstatus0 & TSS_PAR_ERROR)
1822 outb(MSG_PARITY, host->addr + TUL_SFifo);
1823 else
1824 outb(MSG_NOP, host->addr + TUL_SFifo);
1825 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1826 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001828 if (host->phase == MSG_IN) {
1829 msg = inb(host->addr + TUL_SFifo);
1830 if (host->jsstatus0 & TSS_PAR_ERROR) { /* Parity error */
1831 if ((initio_msgin_accept(host)) == -1)
1832 return -1;
1833 if (host->phase != MSG_OUT)
1834 return initio_bad_seq(host);
1835 outb(MSG_PARITY, host->addr + TUL_SFifo);
1836 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1837 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839 if (msg == 0) { /* Command complete */
1840
Alan Cox72d39fe2007-05-21 15:06:43 +01001841 if ((scb->tastat & 0x18) == 0x10) /* No link support */
1842 return initio_bad_seq(host);
1843 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1844 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
1845 return initio_wait_done_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001848 if (msg == MSG_LINK_COMP || msg == MSG_LINK_FLAG) {
1849 if ((scb->tastat & 0x18) == 0x10)
1850 return initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001853 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857/* scsi bus free */
Alan Cox72d39fe2007-05-21 15:06:43 +01001858int int_initio_busfree(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Alan Cox72d39fe2007-05-21 15:06:43 +01001860 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Alan Cox72d39fe2007-05-21 15:06:43 +01001862 if (scb != NULL) {
1863 if (scb->status & SCB_SELECT) { /* selection timeout */
1864 initio_unlink_pend_scb(host, scb);
1865 scb->hastat = HOST_SEL_TOUT;
1866 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 } else { /* Unexpected bus free */
Alan Cox72d39fe2007-05-21 15:06:43 +01001868 initio_unlink_busy_scb(host, scb);
1869 scb->hastat = HOST_BUS_FREE;
1870 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001872 host->active = NULL;
1873 host->active_tc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001875 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
1876 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
1877 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
1878 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
1881
Alan Cox72d39fe2007-05-21 15:06:43 +01001882/**
1883 * int_initio_scsi_rst - SCSI reset occurred
1884 * @host: Host seeing the reset
1885 *
1886 * A SCSI bus reset has occurred. Clean up any pending transfer
1887 * the hardware is doing by DMA and then abort all active and
1888 * disconnected commands. The mid layer should sort the rest out
1889 * for us
1890 */
1891
1892static int int_initio_scsi_rst(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Alan Cox72d39fe2007-05-21 15:06:43 +01001894 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 int i;
1896
1897 /* if DMA xfer is pending, abort DMA xfer */
Alan Cox72d39fe2007-05-21 15:06:43 +01001898 if (inb(host->addr + TUL_XStatus) & 0x01) {
1899 outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /* wait Abort DMA xfer done */
Alan Cox72d39fe2007-05-21 15:06:43 +01001901 while ((inb(host->addr + TUL_Int) & 0x04) == 0)
1902 cpu_relax();
1903 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905 /* Abort all active & disconnected scb */
Alan Cox72d39fe2007-05-21 15:06:43 +01001906 while ((scb = initio_pop_busy_scb(host)) != NULL) {
1907 scb->hastat = HOST_BAD_PHAS;
1908 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001910 host->active = NULL;
1911 host->active_tc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 /* clr sync nego. done flag */
Alan Cox72d39fe2007-05-21 15:06:43 +01001914 for (i = 0; i < host->max_tar; i++)
1915 host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1916 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
Alan Cox72d39fe2007-05-21 15:06:43 +01001919/**
1920 * int_initio_scsi_resel - Reselection occured
1921 * @host: InitIO host adapter
1922 *
1923 * A SCSI reselection event has been signalled and the interrupt
1924 * is now being processed. Work out which command block needs attention
1925 * and continue processing that command.
1926 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Alan Cox72d39fe2007-05-21 15:06:43 +01001928int int_initio_resel(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Alan Cox72d39fe2007-05-21 15:06:43 +01001930 struct scsi_ctrl_blk *scb;
1931 struct target_control *active_tc;
1932 u8 tag, msg = 0;
1933 u8 tar, lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Alan Cox72d39fe2007-05-21 15:06:43 +01001935 if ((scb = host->active) != NULL) {
1936 /* FIXME: Why check and not just clear ? */
1937 if (scb->status & SCB_SELECT) /* if waiting for selection complete */
1938 scb->status &= ~SCB_SELECT;
1939 host->active = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941 /* --------- get target id---------------------- */
Alan Cox72d39fe2007-05-21 15:06:43 +01001942 tar = inb(host->addr + TUL_SBusId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /* ------ get LUN from Identify message----------- */
Alan Cox72d39fe2007-05-21 15:06:43 +01001944 lun = inb(host->addr + TUL_SIdent) & 0x0F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /* 07/22/98 from 0x1F -> 0x0F */
Alan Cox72d39fe2007-05-21 15:06:43 +01001946 active_tc = &host->targets[tar];
1947 host->active_tc = active_tc;
1948 outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1949 outb(active_tc->js_period, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /* ------------- tag queueing ? ------------------- */
Alan Cox72d39fe2007-05-21 15:06:43 +01001952 if (active_tc->drv_flags & TCF_DRV_EN_TAG) {
1953 if ((initio_msgin_accept(host)) == -1)
1954 return -1;
1955 if (host->phase != MSG_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 goto no_tag;
Alan Cox72d39fe2007-05-21 15:06:43 +01001957 outl(1, host->addr + TUL_SCnt0);
1958 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1959 if (wait_tulip(host) == -1)
1960 return -1;
1961 msg = inb(host->addr + TUL_SFifo); /* Read Tag Message */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Alan Cox72d39fe2007-05-21 15:06:43 +01001963 if (msg < MSG_STAG || msg > MSG_OTAG) /* Is simple Tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 goto no_tag;
1965
Alan Cox72d39fe2007-05-21 15:06:43 +01001966 if (initio_msgin_accept(host) == -1)
1967 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Alan Cox72d39fe2007-05-21 15:06:43 +01001969 if (host->phase != MSG_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 goto no_tag;
1971
Alan Cox72d39fe2007-05-21 15:06:43 +01001972 outl(1, host->addr + TUL_SCnt0);
1973 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1974 if (wait_tulip(host) == -1)
1975 return -1;
1976 tag = inb(host->addr + TUL_SFifo); /* Read Tag ID */
1977 scb = host->scb + tag;
1978 if (scb->target != tar || scb->lun != lun) {
1979 return initio_msgout_abort_tag(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001981 if (scb->status != SCB_BUSY) { /* 03/24/95 */
1982 return initio_msgout_abort_tag(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001984 host->active = scb;
1985 if ((initio_msgin_accept(host)) == -1)
1986 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 } else { /* No tag */
1988 no_tag:
Alan Cox72d39fe2007-05-21 15:06:43 +01001989 if ((scb = initio_find_busy_scb(host, tar | (lun << 8))) == NULL) {
1990 return initio_msgout_abort_targ(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001992 host->active = scb;
1993 if (!(active_tc->drv_flags & TCF_DRV_EN_TAG)) {
1994 if ((initio_msgin_accept(host)) == -1)
1995 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 }
1998 return 0;
1999}
2000
Alan Cox72d39fe2007-05-21 15:06:43 +01002001/**
2002 * int_initio_bad_seq - out of phase
2003 * @host: InitIO host flagging event
2004 *
2005 * We have ended up out of phase somehow. Reset the host controller
2006 * and throw all our toys out of the pram. Let the midlayer clean up
2007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Alan Cox72d39fe2007-05-21 15:06:43 +01002009static int int_initio_bad_seq(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{ /* target wrong phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01002011 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 int i;
2013
Alan Cox72d39fe2007-05-21 15:06:43 +01002014 initio_reset_scsi(host, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Alan Cox72d39fe2007-05-21 15:06:43 +01002016 while ((scb = initio_pop_busy_scb(host)) != NULL) {
2017 scb->hastat = HOST_BAD_PHAS;
2018 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002020 for (i = 0; i < host->max_tar; i++)
2021 host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
2022 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
2025
Alan Cox72d39fe2007-05-21 15:06:43 +01002026/**
2027 * initio_msgout_abort_targ - abort a tag
2028 * @host: InitIO host
2029 *
2030 * Abort when the target/lun does not match or when our SCB is not
2031 * busy. Used by untagged commands.
2032 */
2033
2034static int initio_msgout_abort_targ(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036
Alan Cox72d39fe2007-05-21 15:06:43 +01002037 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2038 if (initio_msgin_accept(host) == -1)
2039 return -1;
2040 if (host->phase != MSG_OUT)
2041 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Alan Cox72d39fe2007-05-21 15:06:43 +01002043 outb(MSG_ABORT, host->addr + TUL_SFifo);
2044 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Alan Cox72d39fe2007-05-21 15:06:43 +01002046 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Alan Cox72d39fe2007-05-21 15:06:43 +01002049/**
2050 * initio_msgout_abort_tag - abort a tag
2051 * @host: InitIO host
2052 *
2053 * Abort when the target/lun does not match or when our SCB is not
2054 * busy. Used for tagged commands.
2055 */
2056
2057static int initio_msgout_abort_tag(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059
Alan Cox72d39fe2007-05-21 15:06:43 +01002060 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2061 if (initio_msgin_accept(host) == -1)
2062 return -1;
2063 if (host->phase != MSG_OUT)
2064 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Alan Cox72d39fe2007-05-21 15:06:43 +01002066 outb(MSG_ABORT_TAG, host->addr + TUL_SFifo);
2067 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Alan Cox72d39fe2007-05-21 15:06:43 +01002069 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071}
2072
Alan Cox72d39fe2007-05-21 15:06:43 +01002073/**
2074 * initio_msgin - Message in
2075 * @host: InitIO Host
2076 *
2077 * Process incoming message
2078 */
2079static int initio_msgin(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Alan Cox72d39fe2007-05-21 15:06:43 +01002081 struct target_control *active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002084 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Alan Cox72d39fe2007-05-21 15:06:43 +01002086 outl(1, host->addr + TUL_SCnt0);
2087 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2088 if (wait_tulip(host) == -1)
2089 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Alan Cox72d39fe2007-05-21 15:06:43 +01002091 switch (inb(host->addr + TUL_SFifo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 case MSG_DISC: /* Disconnect msg */
Alan Cox72d39fe2007-05-21 15:06:43 +01002093 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2094 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 case MSG_SDP:
2096 case MSG_RESTORE:
2097 case MSG_NOP:
Alan Cox72d39fe2007-05-21 15:06:43 +01002098 initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 case MSG_REJ: /* Clear ATN first */
Alan Cox72d39fe2007-05-21 15:06:43 +01002101 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)),
2102 host->addr + TUL_SSignal);
2103 active_tc = host->active_tc;
2104 if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) /* do sync nego */
2105 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN),
2106 host->addr + TUL_SSignal);
2107 initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 case MSG_EXTEND: /* extended msg */
Alan Cox72d39fe2007-05-21 15:06:43 +01002110 initio_msgin_extend(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 case MSG_IGNOREWIDE:
Alan Cox72d39fe2007-05-21 15:06:43 +01002113 initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 case MSG_COMP:
Alan Cox72d39fe2007-05-21 15:06:43 +01002116 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2117 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2118 return initio_wait_done_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01002120 initio_msgout_reject(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 break;
2122 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002123 if (host->phase != MSG_IN)
2124 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126 /* statement won't reach here */
2127}
2128
Alan Cox72d39fe2007-05-21 15:06:43 +01002129static int initio_msgout_reject(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Alan Cox72d39fe2007-05-21 15:06:43 +01002131 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Alan Cox72d39fe2007-05-21 15:06:43 +01002133 if (initio_msgin_accept(host) == -1)
2134 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Alan Cox72d39fe2007-05-21 15:06:43 +01002136 if (host->phase == MSG_OUT) {
2137 outb(MSG_REJ, host->addr + TUL_SFifo); /* Msg reject */
2138 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2139 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002141 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Alan Cox72d39fe2007-05-21 15:06:43 +01002144static int initio_msgout_ide(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Alan Cox72d39fe2007-05-21 15:06:43 +01002146 outb(MSG_IDE, host->addr + TUL_SFifo); /* Initiator Detected Error */
2147 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2148 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Alan Cox72d39fe2007-05-21 15:06:43 +01002151static int initio_msgin_extend(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Alan Cox72d39fe2007-05-21 15:06:43 +01002153 u8 len, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Alan Cox72d39fe2007-05-21 15:06:43 +01002155 if (initio_msgin_accept(host) != MSG_IN)
2156 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 /* Get extended msg length */
Alan Cox72d39fe2007-05-21 15:06:43 +01002159 outl(1, host->addr + TUL_SCnt0);
2160 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2161 if (wait_tulip(host) == -1)
2162 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Alan Cox72d39fe2007-05-21 15:06:43 +01002164 len = inb(host->addr + TUL_SFifo);
2165 host->msg[0] = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 for (idx = 1; len != 0; len--) {
2167
Alan Cox72d39fe2007-05-21 15:06:43 +01002168 if ((initio_msgin_accept(host)) != MSG_IN)
2169 return host->phase;
2170 outl(1, host->addr + TUL_SCnt0);
2171 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2172 if (wait_tulip(host) == -1)
2173 return -1;
2174 host->msg[idx++] = inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002176 if (host->msg[1] == 1) { /* if it's synchronous data transfer request */
2177 u8 r;
2178 if (host->msg[0] != 3) /* if length is not right */
2179 return initio_msgout_reject(host);
2180 if (host->active_tc->flags & TCF_NO_SYNC_NEGO) { /* Set OFFSET=0 to do async, nego back */
2181 host->msg[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002183 if (initio_msgin_sync(host) == 0 &&
2184 (host->active_tc->flags & TCF_SYNC_DONE)) {
2185 initio_sync_done(host);
2186 return initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188 }
2189
Alan Cox72d39fe2007-05-21 15:06:43 +01002190 r = inb(host->addr + TUL_SSignal);
2191 outb((r & (TSC_SET_ACK | 7)) | TSC_SET_ATN,
2192 host->addr + TUL_SSignal);
2193 if (initio_msgin_accept(host) != MSG_OUT)
2194 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /* sync msg out */
Alan Cox72d39fe2007-05-21 15:06:43 +01002196 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Alan Cox72d39fe2007-05-21 15:06:43 +01002198 initio_sync_done(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Alan Cox72d39fe2007-05-21 15:06:43 +01002200 outb(MSG_EXTEND, host->addr + TUL_SFifo);
2201 outb(3, host->addr + TUL_SFifo);
2202 outb(1, host->addr + TUL_SFifo);
2203 outb(host->msg[2], host->addr + TUL_SFifo);
2204 outb(host->msg[3], host->addr + TUL_SFifo);
2205 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2206 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002208 if (host->msg[0] != 2 || host->msg[1] != 3)
2209 return initio_msgout_reject(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /* if it's WIDE DATA XFER REQ */
Alan Cox72d39fe2007-05-21 15:06:43 +01002211 if (host->active_tc->flags & TCF_NO_WDTR) {
2212 host->msg[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002214 if (host->msg[2] > 2) /* > 32 bits */
2215 return initio_msgout_reject(host);
2216 if (host->msg[2] == 2) { /* == 32 */
2217 host->msg[2] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002219 if ((host->active_tc->flags & TCF_NO_WDTR) == 0) {
2220 wdtr_done(host);
2221 if ((host->active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)
2222 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2223 return initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
2225 }
2226 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002227 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Alan Cox72d39fe2007-05-21 15:06:43 +01002229 if (initio_msgin_accept(host) != MSG_OUT)
2230 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /* WDTR msg out */
Alan Cox72d39fe2007-05-21 15:06:43 +01002232 outb(MSG_EXTEND, host->addr + TUL_SFifo);
2233 outb(2, host->addr + TUL_SFifo);
2234 outb(3, host->addr + TUL_SFifo);
2235 outb(host->msg[2], host->addr + TUL_SFifo);
2236 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2237 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Alan Cox72d39fe2007-05-21 15:06:43 +01002240static int initio_msgin_sync(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
2242 char default_period;
2243
Alan Cox72d39fe2007-05-21 15:06:43 +01002244 default_period = initio_rate_tbl[host->active_tc->flags & TCF_SCSI_RATE];
2245 if (host->msg[3] > MAX_OFFSET) {
2246 host->msg[3] = MAX_OFFSET;
2247 if (host->msg[2] < default_period) {
2248 host->msg[2] = default_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return 1;
2250 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002251 if (host->msg[2] >= 59) /* Change to async */
2252 host->msg[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return 1;
2254 }
2255 /* offset requests asynchronous transfers ? */
Alan Cox72d39fe2007-05-21 15:06:43 +01002256 if (host->msg[3] == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return 0;
2258 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002259 if (host->msg[2] < default_period) {
2260 host->msg[2] = default_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return 1;
2262 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002263 if (host->msg[2] >= 59) {
2264 host->msg[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return 1;
2266 }
2267 return 0;
2268}
2269
Alan Cox72d39fe2007-05-21 15:06:43 +01002270static int wdtr_done(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Alan Cox72d39fe2007-05-21 15:06:43 +01002272 host->active_tc->flags &= ~TCF_SYNC_DONE;
2273 host->active_tc->flags |= TCF_WDTR_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Alan Cox72d39fe2007-05-21 15:06:43 +01002275 host->active_tc->js_period = 0;
2276 if (host->msg[2]) /* if 16 bit */
2277 host->active_tc->js_period |= TSC_WIDE_SCSI;
2278 host->active_tc->sconfig0 &= ~TSC_ALT_PERIOD;
2279 outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2280 outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 return 1;
2283}
2284
Alan Cox72d39fe2007-05-21 15:06:43 +01002285static int initio_sync_done(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 int i;
2288
Alan Cox72d39fe2007-05-21 15:06:43 +01002289 host->active_tc->flags |= TCF_SYNC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Alan Cox72d39fe2007-05-21 15:06:43 +01002291 if (host->msg[3]) {
2292 host->active_tc->js_period |= host->msg[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 for (i = 0; i < 8; i++) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002294 if (initio_rate_tbl[i] >= host->msg[2]) /* pick the big one */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 break;
2296 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002297 host->active_tc->js_period |= (i << 4);
2298 host->active_tc->sconfig0 |= TSC_ALT_PERIOD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002300 outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2301 outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Alan Cox72d39fe2007-05-21 15:06:43 +01002303 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306
Alan Cox72d39fe2007-05-21 15:06:43 +01002307static int initio_post_scsi_rst(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Alan Cox72d39fe2007-05-21 15:06:43 +01002309 struct scsi_ctrl_blk *scb;
2310 struct target_control *active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 int i;
2312
Alan Cox72d39fe2007-05-21 15:06:43 +01002313 host->active = NULL;
2314 host->active_tc = NULL;
2315 host->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Alan Cox72d39fe2007-05-21 15:06:43 +01002317 while ((scb = initio_pop_busy_scb(host)) != NULL) {
2318 scb->hastat = HOST_BAD_PHAS;
2319 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321 /* clear sync done flag */
Alan Cox72d39fe2007-05-21 15:06:43 +01002322 active_tc = &host->targets[0];
2323 for (i = 0; i < host->max_tar; active_tc++, i++) {
2324 active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 /* Initialize the sync. xfer register values to an asyn xfer */
Alan Cox72d39fe2007-05-21 15:06:43 +01002326 active_tc->js_period = 0;
2327 active_tc->sconfig0 = host->sconf1;
2328 host->act_tags[0] = 0; /* 07/22/98 */
2329 host->targets[i].flags &= ~TCF_BUSY; /* 07/22/98 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 } /* for */
2331
Alan Cox72d39fe2007-05-21 15:06:43 +01002332 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Alan Cox72d39fe2007-05-21 15:06:43 +01002335static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Alan Cox72d39fe2007-05-21 15:06:43 +01002337 scb->status |= SCB_SELECT;
2338 scb->next_state = 0x1;
2339 host->active = scb;
2340 host->active_tc = &host->targets[scb->target];
2341 outb(TSC_SELATNSTOP, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
2344
Alan Cox72d39fe2007-05-21 15:06:43 +01002345static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 int i;
2348
Alan Cox72d39fe2007-05-21 15:06:43 +01002349 scb->status |= SCB_SELECT;
2350 scb->next_state = 0x2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Alan Cox72d39fe2007-05-21 15:06:43 +01002352 outb(scb->ident, host->addr + TUL_SFifo);
2353 for (i = 0; i < (int) scb->cdblen; i++)
2354 outb(scb->cdb[i], host->addr + TUL_SFifo);
2355 host->active_tc = &host->targets[scb->target];
2356 host->active = scb;
2357 outb(TSC_SEL_ATN, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
Alan Cox72d39fe2007-05-21 15:06:43 +01002360static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
2362 int i;
2363
Alan Cox72d39fe2007-05-21 15:06:43 +01002364 scb->status |= SCB_SELECT;
2365 scb->next_state = 0x2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Alan Cox72d39fe2007-05-21 15:06:43 +01002367 outb(scb->ident, host->addr + TUL_SFifo);
2368 outb(scb->tagmsg, host->addr + TUL_SFifo);
2369 outb(scb->tagid, host->addr + TUL_SFifo);
2370 for (i = 0; i < scb->cdblen; i++)
2371 outb(scb->cdb[i], host->addr + TUL_SFifo);
2372 host->active_tc = &host->targets[scb->target];
2373 host->active = scb;
2374 outb(TSC_SEL_ATN3, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
Alan Cox72d39fe2007-05-21 15:06:43 +01002377/**
2378 * initio_bus_device_reset - SCSI Bus Device Reset
2379 * @host: InitIO host to reset
2380 *
2381 * Perform a device reset and abort all pending SCBs for the
2382 * victim device
2383 */
2384int initio_bus_device_reset(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Alan Cox72d39fe2007-05-21 15:06:43 +01002386 struct scsi_ctrl_blk *scb = host->active;
2387 struct target_control *active_tc = host->active_tc;
2388 struct scsi_ctrl_blk *tmp, *prev;
2389 u8 tar;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Alan Cox72d39fe2007-05-21 15:06:43 +01002391 if (host->phase != MSG_OUT)
2392 return int_initio_bad_seq(host); /* Unexpected phase */
2393
2394 initio_unlink_pend_scb(host, scb);
2395 initio_release_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397
Alan Cox72d39fe2007-05-21 15:06:43 +01002398 tar = scb->target; /* target */
2399 active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE | TCF_BUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /* clr sync. nego & WDTR flags 07/22/98 */
2401
2402 /* abort all SCB with same target */
Alan Cox72d39fe2007-05-21 15:06:43 +01002403 prev = tmp = host->first_busy; /* Check Busy queue */
2404 while (tmp != NULL) {
2405 if (tmp->target == tar) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* unlink it */
Alan Cox72d39fe2007-05-21 15:06:43 +01002407 if (tmp == host->first_busy) {
2408 if ((host->first_busy = tmp->next) == NULL)
2409 host->last_busy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002411 prev->next = tmp->next;
2412 if (tmp == host->last_busy)
2413 host->last_busy = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002415 tmp->hastat = HOST_ABORTED;
2416 initio_append_done_scb(host, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
2418 /* Previous haven't change */
2419 else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002420 prev = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002422 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002424 outb(MSG_DEVRST, host->addr + TUL_SFifo);
2425 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2426 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428}
2429
Alan Cox72d39fe2007-05-21 15:06:43 +01002430static int initio_msgin_accept(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Alan Cox72d39fe2007-05-21 15:06:43 +01002432 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2433 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Alan Cox72d39fe2007-05-21 15:06:43 +01002436static int wait_tulip(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438
Alan Cox72d39fe2007-05-21 15:06:43 +01002439 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2440 & TSS_INT_PENDING))
2441 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Alan Cox72d39fe2007-05-21 15:06:43 +01002443 host->jsint = inb(host->addr + TUL_SInt);
2444 host->phase = host->jsstatus0 & TSS_PH_MASK;
2445 host->jsstatus1 = inb(host->addr + TUL_SStatus1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Alan Cox72d39fe2007-05-21 15:06:43 +01002447 if (host->jsint & TSS_RESEL_INT) /* if SCSI bus reset detected */
2448 return int_initio_resel(host);
2449 if (host->jsint & TSS_SEL_TIMEOUT) /* if selected/reselected timeout interrupt */
2450 return int_initio_busfree(host);
2451 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2452 return int_initio_scsi_rst(host);
2453
2454 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2455 if (host->flags & HCF_EXPECT_DONE_DISC) {
2456 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2457 initio_unlink_busy_scb(host, host->active);
2458 host->active->hastat = 0;
2459 initio_append_done_scb(host, host->active);
2460 host->active = NULL;
2461 host->active_tc = NULL;
2462 host->flags &= ~HCF_EXPECT_DONE_DISC;
2463 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2464 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2465 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002467 if (host->flags & HCF_EXPECT_DISC) {
2468 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2469 host->active = NULL;
2470 host->active_tc = NULL;
2471 host->flags &= ~HCF_EXPECT_DISC;
2472 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2473 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2474 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002476 return int_initio_busfree(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002478 /* The old code really does the below. Can probably be removed */
2479 if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV))
2480 return host->phase;
2481 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
Alan Cox72d39fe2007-05-21 15:06:43 +01002483
2484static int initio_wait_disc(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Alan Cox72d39fe2007-05-21 15:06:43 +01002486 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING))
2487 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Alan Cox72d39fe2007-05-21 15:06:43 +01002489 host->jsint = inb(host->addr + TUL_SInt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Alan Cox72d39fe2007-05-21 15:06:43 +01002491 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2492 return int_initio_scsi_rst(host);
2493 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2494 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2495 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2496 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2497 host->active = NULL;
2498 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002500 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Alan Cox72d39fe2007-05-21 15:06:43 +01002503static int initio_wait_done_disc(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Alan Cox72d39fe2007-05-21 15:06:43 +01002505 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2506 & TSS_INT_PENDING))
2507 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Alan Cox72d39fe2007-05-21 15:06:43 +01002509 host->jsint = inb(host->addr + TUL_SInt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Alan Cox72d39fe2007-05-21 15:06:43 +01002511 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2512 return int_initio_scsi_rst(host);
2513 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2514 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2515 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2516 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2517 initio_unlink_busy_scb(host, host->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Alan Cox72d39fe2007-05-21 15:06:43 +01002519 initio_append_done_scb(host, host->active);
2520 host->active = NULL;
2521 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002523 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
Alan Cox72d39fe2007-05-21 15:06:43 +01002526/**
2527 * i91u_intr - IRQ handler
2528 * @irqno: IRQ number
2529 * @dev_id: IRQ identifier
2530 *
2531 * Take the relevant locks and then invoke the actual isr processing
2532 * code under the lock.
2533 */
2534
David Howells7d12e782006-10-05 14:55:46 +01002535static irqreturn_t i91u_intr(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
2537 struct Scsi_Host *dev = dev_id;
2538 unsigned long flags;
Alan Cox72d39fe2007-05-21 15:06:43 +01002539 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 spin_lock_irqsave(dev->host_lock, flags);
Alan Cox72d39fe2007-05-21 15:06:43 +01002542 r = initio_isr((struct initio_host *)dev->hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 spin_unlock_irqrestore(dev->host_lock, flags);
Alan Cox72d39fe2007-05-21 15:06:43 +01002544 if (r)
2545 return IRQ_HANDLED;
2546 else
2547 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Alan Cox72d39fe2007-05-21 15:06:43 +01002551/**
2552 * initio_build_scb - Build the mappings and SCB
2553 * @host: InitIO host taking the command
2554 * @cblk: Firmware command block
2555 * @cmnd: SCSI midlayer command block
2556 *
2557 * Translate the abstract SCSI command into a firmware command block
2558 * suitable for feeding to the InitIO host controller. This also requires
2559 * we build the scatter gather lists and ensure they are mapped properly.
2560 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Alan Cox72d39fe2007-05-21 15:06:43 +01002562static void initio_build_scb(struct initio_host * host, struct scsi_ctrl_blk * cblk, struct scsi_cmnd * cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{ /* Create corresponding SCB */
Alan Cox72d39fe2007-05-21 15:06:43 +01002564 struct scatterlist *sglist;
2565 struct sg_entry *sg; /* Pointer to SG list */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002566 int i, nseg;
Alan Cox72d39fe2007-05-21 15:06:43 +01002567 long total_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 dma_addr_t dma_addr;
2569
Alan Cox72d39fe2007-05-21 15:06:43 +01002570 /* Fill in the command headers */
2571 cblk->post = i91uSCBPost; /* i91u's callback routine */
2572 cblk->srb = cmnd;
2573 cblk->opcode = ExecSCSI;
2574 cblk->flags = SCF_POST; /* After SCSI done, call post routine */
2575 cblk->target = cmnd->device->id;
2576 cblk->lun = cmnd->device->lun;
2577 cblk->ident = cmnd->device->lun | DISC_ALLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Alan Cox72d39fe2007-05-21 15:06:43 +01002579 cblk->flags |= SCF_SENSE; /* Turn on auto request sense */
2580
2581 /* Map the sense buffer into bus memory */
2582 dma_addr = dma_map_single(&host->pci_dev->dev, cmnd->sense_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 SENSE_SIZE, DMA_FROM_DEVICE);
Grant Grundler423eef62008-04-05 10:14:22 -06002584 cblk->senseptr = (u32)dma_addr;
2585 cblk->senselen = SENSE_SIZE;
Alan Cox72d39fe2007-05-21 15:06:43 +01002586 cmnd->SCp.ptr = (char *)(unsigned long)dma_addr;
2587 cblk->cdblen = cmnd->cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Alan Cox72d39fe2007-05-21 15:06:43 +01002589 /* Clear the returned status */
2590 cblk->hastat = 0;
2591 cblk->tastat = 0;
2592 /* Command the command */
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002593 memcpy(cblk->cdb, cmnd->cmnd, cmnd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Alan Cox72d39fe2007-05-21 15:06:43 +01002595 /* Set up tags */
2596 if (cmnd->device->tagged_supported) { /* Tag Support */
2597 cblk->tagmsg = SIMPLE_QUEUE_TAG; /* Do simple tag only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002599 cblk->tagmsg = 0; /* No tag support */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /* todo handle map_sg error */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002603 nseg = scsi_dma_map(cmnd);
2604 BUG_ON(nseg < 0);
2605 if (nseg) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002606 dma_addr = dma_map_single(&host->pci_dev->dev, &cblk->sglist[0],
2607 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 DMA_BIDIRECTIONAL);
Grant Grundler423eef62008-04-05 10:14:22 -06002609 cblk->bufptr = (u32)dma_addr;
Alan Cox72d39fe2007-05-21 15:06:43 +01002610 cmnd->SCp.dma_handle = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Stuart Swalese2d435e2008-01-23 20:00:48 +00002612 cblk->sglen = nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Alan Cox72d39fe2007-05-21 15:06:43 +01002614 cblk->flags |= SCF_SG; /* Turn on SG list flag */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002615 total_len = 0;
2616 sg = &cblk->sglist[0];
2617 scsi_for_each_sg(cmnd, sglist, cblk->sglen, i) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002618 sg->data = cpu_to_le32((u32)sg_dma_address(sglist));
Grant Grundler423eef62008-04-05 10:14:22 -06002619 sg->len = cpu_to_le32((u32)sg_dma_len(sglist));
2620 total_len += sg_dma_len(sglist);
Boaz Harrosha169e632007-12-17 18:08:59 +02002621 ++sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002624 cblk->buflen = (scsi_bufflen(cmnd) > total_len) ?
2625 total_len : scsi_bufflen(cmnd);
Alan Cox72d39fe2007-05-21 15:06:43 +01002626 } else { /* No data transfer required */
2627 cblk->buflen = 0;
2628 cblk->sglen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
2630}
2631
Alan Cox72d39fe2007-05-21 15:06:43 +01002632/**
2633 * i91u_queuecommand - Queue a new command if possible
2634 * @cmd: SCSI command block from the mid layer
2635 * @done: Completion handler
2636 *
2637 * Attempts to queue a new command with the host adapter. Will return
2638 * zero if successful or indicate a host busy condition if not (which
2639 * will cause the mid layer to call us again later with the command)
2640 */
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642static int i91u_queuecommand(struct scsi_cmnd *cmd,
2643 void (*done)(struct scsi_cmnd *))
2644{
Alan Cox72d39fe2007-05-21 15:06:43 +01002645 struct initio_host *host = (struct initio_host *) cmd->device->host->hostdata;
2646 struct scsi_ctrl_blk *cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 cmd->scsi_done = done;
2649
Alan Cox72d39fe2007-05-21 15:06:43 +01002650 cmnd = initio_alloc_scb(host);
2651 if (!cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 return SCSI_MLQUEUE_HOST_BUSY;
2653
Alan Cox72d39fe2007-05-21 15:06:43 +01002654 initio_build_scb(host, cmnd, cmd);
2655 initio_exec_scb(host, cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return 0;
2657}
2658
Alan Cox72d39fe2007-05-21 15:06:43 +01002659/**
2660 * i91u_bus_reset - reset the SCSI bus
2661 * @cmnd: Command block we want to trigger the reset for
2662 *
2663 * Initiate a SCSI bus reset sequence
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 */
Alan Cox72d39fe2007-05-21 15:06:43 +01002665
2666static int i91u_bus_reset(struct scsi_cmnd * cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Alan Cox72d39fe2007-05-21 15:06:43 +01002668 struct initio_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Alan Cox72d39fe2007-05-21 15:06:43 +01002670 host = (struct initio_host *) cmnd->device->host->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Alan Cox72d39fe2007-05-21 15:06:43 +01002672 spin_lock_irq(cmnd->device->host->host_lock);
2673 initio_reset_scsi(host, 0);
2674 spin_unlock_irq(cmnd->device->host->host_lock);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 return SUCCESS;
2677}
2678
Alan Cox72d39fe2007-05-21 15:06:43 +01002679/**
2680 * i91u_biospararm - return the "logical geometry
2681 * @sdev: SCSI device
2682 * @dev; Matching block device
2683 * @capacity: Sector size of drive
2684 * @info_array: Return space for BIOS geometry
2685 *
2686 * Map the device geometry in a manner compatible with the host
2687 * controller BIOS behaviour.
2688 *
2689 * FIXME: limited to 2^32 sector devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 */
Alan Cox72d39fe2007-05-21 15:06:43 +01002691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692static int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev,
2693 sector_t capacity, int *info_array)
2694{
Alan Cox72d39fe2007-05-21 15:06:43 +01002695 struct initio_host *host; /* Point to Host adapter control block */
2696 struct target_control *tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Alan Cox72d39fe2007-05-21 15:06:43 +01002698 host = (struct initio_host *) sdev->host->hostdata;
2699 tc = &host->targets[sdev->id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Alan Cox72d39fe2007-05-21 15:06:43 +01002701 if (tc->heads) {
2702 info_array[0] = tc->heads;
2703 info_array[1] = tc->sectors;
2704 info_array[2] = (unsigned long)capacity / tc->heads / tc->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002706 if (tc->drv_flags & TCF_DRV_255_63) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 info_array[0] = 255;
2708 info_array[1] = 63;
2709 info_array[2] = (unsigned long)capacity / 255 / 63;
2710 } else {
2711 info_array[0] = 64;
2712 info_array[1] = 32;
2713 info_array[2] = (unsigned long)capacity >> 11;
2714 }
2715 }
2716
2717#if defined(DEBUG_BIOSPARAM)
2718 if (i91u_debug & debug_biosparam) {
2719 printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
2720 info_array[0], info_array[1], info_array[2]);
2721 printk("WARNING: check, if the bios geometry is correct.\n");
2722 }
2723#endif
2724
2725 return 0;
2726}
2727
Alan Cox72d39fe2007-05-21 15:06:43 +01002728/**
2729 * i91u_unmap_scb - Unmap a command
2730 * @pci_dev: PCI device the command is for
2731 * @cmnd: The command itself
2732 *
2733 * Unmap any PCI mapping/IOMMU resources allocated when the command
2734 * was mapped originally as part of initio_build_scb
2735 */
2736
2737static void i91u_unmap_scb(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 /* auto sense buffer */
2740 if (cmnd->SCp.ptr) {
2741 dma_unmap_single(&pci_dev->dev,
2742 (dma_addr_t)((unsigned long)cmnd->SCp.ptr),
2743 SENSE_SIZE, DMA_FROM_DEVICE);
2744 cmnd->SCp.ptr = NULL;
2745 }
2746
2747 /* request buffer */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002748 if (scsi_sg_count(cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 dma_unmap_single(&pci_dev->dev, cmnd->SCp.dma_handle,
Alan Cox72d39fe2007-05-21 15:06:43 +01002750 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 DMA_BIDIRECTIONAL);
2752
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002753 scsi_dma_unmap(cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 }
2755}
2756
Alan Cox72d39fe2007-05-21 15:06:43 +01002757/**
2758 * i91uSCBPost - SCSI callback
2759 * @host: Pointer to host adapter control block.
2760 * @cmnd: Pointer to SCSI control block.
2761 *
2762 * This is callback routine be called when tulip finish one
2763 * SCSI command.
2764 */
2765
2766static void i91uSCBPost(u8 * host_mem, u8 * cblk_mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
Alan Cox72d39fe2007-05-21 15:06:43 +01002768 struct scsi_cmnd *cmnd; /* Pointer to SCSI request block */
2769 struct initio_host *host;
2770 struct scsi_ctrl_blk *cblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Alan Cox72d39fe2007-05-21 15:06:43 +01002772 host = (struct initio_host *) host_mem;
2773 cblk = (struct scsi_ctrl_blk *) cblk_mem;
2774 if ((cmnd = cblk->srb) == NULL) {
2775 printk(KERN_ERR "i91uSCBPost: SRB pointer is empty\n");
2776 WARN_ON(1);
2777 initio_release_scb(host, cblk); /* Release SCB for current channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return;
2779 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002780
2781 /*
2782 * Remap the firmware error status into a mid layer one
2783 */
2784 switch (cblk->hastat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 case 0x0:
2786 case 0xa: /* Linked command complete without error and linked normally */
2787 case 0xb: /* Linked command complete without error interrupt generated */
Alan Cox72d39fe2007-05-21 15:06:43 +01002788 cblk->hastat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 break;
2790
2791 case 0x11: /* Selection time out-The initiator selection or target
2792 reselection was not complete within the SCSI Time out period */
Alan Cox72d39fe2007-05-21 15:06:43 +01002793 cblk->hastat = DID_TIME_OUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 break;
2795
2796 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
2797 phase sequence was requested by the target. The host adapter
2798 will generate a SCSI Reset Condition, notifying the host with
2799 a SCRD interrupt */
Alan Cox72d39fe2007-05-21 15:06:43 +01002800 cblk->hastat = DID_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 break;
2802
2803 case 0x1a: /* SCB Aborted. 07/21/98 */
Alan Cox72d39fe2007-05-21 15:06:43 +01002804 cblk->hastat = DID_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 break;
2806
2807 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
2808 than was allocated by the Data Length field or the sum of the
2809 Scatter / Gather Data Length fields. */
2810 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
2811 case 0x16: /* Invalid SCB Operation Code. */
2812
2813 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01002814 printk("ini9100u: %x %x\n", cblk->hastat, cblk->tastat);
2815 cblk->hastat = DID_ERROR; /* Couldn't find any better */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 break;
2817 }
2818
Alan Cox72d39fe2007-05-21 15:06:43 +01002819 cmnd->result = cblk->tastat | (cblk->hastat << 16);
2820 WARN_ON(cmnd == NULL);
2821 i91u_unmap_scb(host->pci_dev, cmnd);
2822 cmnd->scsi_done(cmnd); /* Notify system DONE */
2823 initio_release_scb(host, cblk); /* Release SCB for current channel */
2824}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Alan Cox72d39fe2007-05-21 15:06:43 +01002826static struct scsi_host_template initio_template = {
2827 .proc_name = "INI9100U",
2828 .name = "Initio INI-9X00U/UW SCSI device driver",
2829 .queuecommand = i91u_queuecommand,
2830 .eh_bus_reset_handler = i91u_bus_reset,
2831 .bios_param = i91u_biosparam,
2832 .can_queue = MAX_TARGETS * i91u_MAXQUEUE,
2833 .this_id = 1,
2834 .sg_tablesize = SG_ALL,
2835 .cmd_per_lun = 1,
2836 .use_clustering = ENABLE_CLUSTERING,
2837};
2838
2839static int initio_probe_one(struct pci_dev *pdev,
2840 const struct pci_device_id *id)
2841{
2842 struct Scsi_Host *shost;
2843 struct initio_host *host;
2844 u32 reg;
2845 u16 bios_seg;
2846 struct scsi_ctrl_blk *scb, *tmp, *prev = NULL /* silence gcc */;
2847 int num_scb, i, error;
2848
2849 error = pci_enable_device(pdev);
2850 if (error)
2851 return error;
2852
2853 pci_read_config_dword(pdev, 0x44, (u32 *) & reg);
2854 bios_seg = (u16) (reg & 0xFF);
2855 if (((reg & 0xFF00) >> 8) == 0xFF)
2856 reg = 0;
2857 bios_seg = (bios_seg << 8) + ((u16) ((reg & 0xFF00) >> 8));
2858
Yang Hongyang284901a2009-04-06 19:01:15 -07002859 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002860 printk(KERN_WARNING "i91u: Could not set 32 bit DMA mask\n");
2861 error = -ENODEV;
2862 goto out_disable_device;
2863 }
2864 shost = scsi_host_alloc(&initio_template, sizeof(struct initio_host));
2865 if (!shost) {
2866 printk(KERN_WARNING "initio: Could not allocate host structure.\n");
2867 error = -ENOMEM;
2868 goto out_disable_device;
2869 }
2870 host = (struct initio_host *)shost->hostdata;
2871 memset(host, 0, sizeof(struct initio_host));
Alan Cox99f1f532007-12-13 16:14:05 -08002872 host->addr = pci_resource_start(pdev, 0);
Stuart Swalese2d435e2008-01-23 20:00:48 +00002873 host->bios_addr = bios_seg;
Alan Cox72d39fe2007-05-21 15:06:43 +01002874
2875 if (!request_region(host->addr, 256, "i91u")) {
2876 printk(KERN_WARNING "initio: I/O port range 0x%x is busy.\n", host->addr);
2877 error = -ENODEV;
2878 goto out_host_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
2880
Alan Cox72d39fe2007-05-21 15:06:43 +01002881 if (initio_tag_enable) /* 1.01i */
2882 num_scb = MAX_TARGETS * i91u_MAXQUEUE;
2883 else
2884 num_scb = MAX_TARGETS + 3; /* 1-tape, 1-CD_ROM, 1- extra */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Alan Cox72d39fe2007-05-21 15:06:43 +01002886 for (; num_scb >= MAX_TARGETS + 3; num_scb--) {
2887 i = num_scb * sizeof(struct scsi_ctrl_blk);
2888 if ((scb = kzalloc(i, GFP_DMA)) != NULL)
2889 break;
2890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Alan Cox72d39fe2007-05-21 15:06:43 +01002892 if (!scb) {
2893 printk(KERN_WARNING "initio: Cannot allocate SCB array.\n");
2894 error = -ENOMEM;
2895 goto out_release_region;
2896 }
2897
Alan Coxe9e42fa2007-10-15 20:51:32 +01002898 host->pci_dev = pdev;
2899
Stuart Swalese2d435e2008-01-23 20:00:48 +00002900 host->semaph = 1;
2901 spin_lock_init(&host->semaph_lock);
Alan Cox72d39fe2007-05-21 15:06:43 +01002902 host->num_scbs = num_scb;
2903 host->scb = scb;
2904 host->next_pending = scb;
2905 host->next_avail = scb;
2906 for (i = 0, tmp = scb; i < num_scb; i++, tmp++) {
2907 tmp->tagid = i;
2908 if (i != 0)
2909 prev->next = tmp;
2910 prev = tmp;
2911 }
2912 prev->next = NULL;
2913 host->scb_end = tmp;
2914 host->first_avail = scb;
2915 host->last_avail = prev;
Alan Coxe9e42fa2007-10-15 20:51:32 +01002916 spin_lock_init(&host->avail_lock);
Alan Cox72d39fe2007-05-21 15:06:43 +01002917
Stuart Swalese2d435e2008-01-23 20:00:48 +00002918 initio_init(host, phys_to_virt(((u32)bios_seg << 4)));
Alan Cox72d39fe2007-05-21 15:06:43 +01002919
2920 host->jsstatus0 = 0;
2921
2922 shost->io_port = host->addr;
2923 shost->n_io_port = 0xff;
2924 shost->can_queue = num_scb; /* 03/05/98 */
2925 shost->unique_id = host->addr;
2926 shost->max_id = host->max_tar;
2927 shost->max_lun = 32; /* 10/21/97 */
2928 shost->irq = pdev->irq;
2929 shost->this_id = host->scsi_id; /* Assign HCS index */
2930 shost->base = host->addr;
2931 shost->sg_tablesize = TOTAL_SG_ENTRY;
2932
2933 error = request_irq(pdev->irq, i91u_intr, IRQF_DISABLED|IRQF_SHARED, "i91u", shost);
2934 if (error < 0) {
2935 printk(KERN_WARNING "initio: Unable to request IRQ %d\n", pdev->irq);
2936 goto out_free_scbs;
2937 }
2938
2939 pci_set_drvdata(pdev, shost);
Alan Cox72d39fe2007-05-21 15:06:43 +01002940
2941 error = scsi_add_host(shost, &pdev->dev);
2942 if (error)
2943 goto out_free_irq;
2944 scsi_scan_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return 0;
Alan Cox72d39fe2007-05-21 15:06:43 +01002946out_free_irq:
2947 free_irq(pdev->irq, shost);
2948out_free_scbs:
2949 kfree(host->scb);
2950out_release_region:
2951 release_region(host->addr, 256);
2952out_host_put:
2953 scsi_host_put(shost);
2954out_disable_device:
2955 pci_disable_device(pdev);
2956 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Alan Cox72d39fe2007-05-21 15:06:43 +01002959/**
2960 * initio_remove_one - control shutdown
2961 * @pdev: PCI device being released
2962 *
2963 * Release the resources assigned to this adapter after it has
2964 * finished being used.
2965 */
2966
2967static void initio_remove_one(struct pci_dev *pdev)
2968{
2969 struct Scsi_Host *host = pci_get_drvdata(pdev);
2970 struct initio_host *s = (struct initio_host *)host->hostdata;
2971 scsi_remove_host(host);
2972 free_irq(pdev->irq, host);
2973 release_region(s->addr, 256);
2974 scsi_host_put(host);
2975 pci_disable_device(pdev);
2976}
2977
2978MODULE_LICENSE("GPL");
2979
2980static struct pci_device_id initio_pci_tbl[] = {
2981 {PCI_VENDOR_ID_INIT, 0x9500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2982 {PCI_VENDOR_ID_INIT, 0x9400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2983 {PCI_VENDOR_ID_INIT, 0x9401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2984 {PCI_VENDOR_ID_INIT, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2985 {PCI_VENDOR_ID_DOMEX, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2986 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987};
Alan Cox72d39fe2007-05-21 15:06:43 +01002988MODULE_DEVICE_TABLE(pci, initio_pci_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Alan Cox72d39fe2007-05-21 15:06:43 +01002990static struct pci_driver initio_pci_driver = {
2991 .name = "initio",
2992 .id_table = initio_pci_tbl,
2993 .probe = initio_probe_one,
2994 .remove = __devexit_p(initio_remove_one),
2995};
2996
2997static int __init initio_init_driver(void)
2998{
2999 return pci_register_driver(&initio_pci_driver);
3000}
3001
3002static void __exit initio_exit_driver(void)
3003{
3004 pci_unregister_driver(&initio_pci_driver);
3005}
3006
3007MODULE_DESCRIPTION("Initio INI-9X00U/UW SCSI device driver");
3008MODULE_AUTHOR("Initio Corporation");
3009MODULE_LICENSE("GPL");
3010
3011module_init(initio_init_driver);
3012module_exit(initio_exit_driver);