blob: 7a91cf3ff17326bd4973735bee79c82e15bd50ee [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#ifdef DEBUG_i91u
114static unsigned int i91u_debug = DEBUG_DEFAULT;
115#endif
116
Alan Cox72d39fe2007-05-21 15:06:43 +0100117static int initio_tag_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119#ifdef DEBUG_i91u
120static int setup_debug = 0;
121#endif
122
Alan Cox72d39fe2007-05-21 15:06:43 +0100123static void i91uSCBPost(u8 * pHcb, u8 * pScb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define DEBUG_INTERRUPT 0
126#define DEBUG_QUEUE 0
127#define DEBUG_STATE 0
128#define INT_DISC 0
129
Alan Cox72d39fe2007-05-21 15:06:43 +0100130/*--- forward references ---*/
131static struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun);
132static struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Alan Cox72d39fe2007-05-21 15:06:43 +0100134static int tulip_main(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Alan Cox72d39fe2007-05-21 15:06:43 +0100136static int initio_next_state(struct initio_host * host);
137static int initio_state_1(struct initio_host * host);
138static int initio_state_2(struct initio_host * host);
139static int initio_state_3(struct initio_host * host);
140static int initio_state_4(struct initio_host * host);
141static int initio_state_5(struct initio_host * host);
142static int initio_state_6(struct initio_host * host);
143static int initio_state_7(struct initio_host * host);
144static int initio_xfer_data_in(struct initio_host * host);
145static int initio_xfer_data_out(struct initio_host * host);
146static int initio_xpad_in(struct initio_host * host);
147static int initio_xpad_out(struct initio_host * host);
148static int initio_status_msg(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Alan Cox72d39fe2007-05-21 15:06:43 +0100150static int initio_msgin(struct initio_host * host);
151static int initio_msgin_sync(struct initio_host * host);
152static int initio_msgin_accept(struct initio_host * host);
153static int initio_msgout_reject(struct initio_host * host);
154static int initio_msgin_extend(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Alan Cox72d39fe2007-05-21 15:06:43 +0100156static int initio_msgout_ide(struct initio_host * host);
157static int initio_msgout_abort_targ(struct initio_host * host);
158static int initio_msgout_abort_tag(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Alan Cox72d39fe2007-05-21 15:06:43 +0100160static int initio_bus_device_reset(struct initio_host * host);
161static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb);
162static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb);
163static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb);
164static int int_initio_busfree(struct initio_host * host);
165static int int_initio_scsi_rst(struct initio_host * host);
166static int int_initio_bad_seq(struct initio_host * host);
167static int int_initio_resel(struct initio_host * host);
168static int initio_sync_done(struct initio_host * host);
169static int wdtr_done(struct initio_host * host);
170static int wait_tulip(struct initio_host * host);
171static int initio_wait_done_disc(struct initio_host * host);
172static int initio_wait_disc(struct initio_host * host);
173static void tulip_scsi(struct initio_host * host);
174static int initio_post_scsi_rst(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Alan Cox72d39fe2007-05-21 15:06:43 +0100176static void initio_se2_ew_en(unsigned long base);
177static void initio_se2_ew_ds(unsigned long base);
178static int initio_se2_rd_all(unsigned long base);
179static void initio_se2_update_all(unsigned long base); /* setup default pattern */
180static void initio_read_eeprom(unsigned long base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Alan Cox72d39fe2007-05-21 15:06:43 +0100182/* ---- INTERNAL VARIABLES ---- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static NVRAM i91unvram;
185static NVRAM *i91unvramp;
186
Alan Cox72d39fe2007-05-21 15:06:43 +0100187static u8 i91udftNvRam[64] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Alan Cox72d39fe2007-05-21 15:06:43 +0100189 /*----------- header -----------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 0x25, 0xc9, /* Signature */
191 0x40, /* Size */
192 0x01, /* Revision */
193 /* -- Host Adapter Structure -- */
194 0x95, /* ModelByte0 */
195 0x00, /* ModelByte1 */
196 0x00, /* ModelInfo */
197 0x01, /* NumOfCh */
198 NBC1_DEFAULT, /* BIOSConfig1 */
199 0, /* BIOSConfig2 */
200 0, /* HAConfig1 */
201 0, /* HAConfig2 */
202 /* SCSI channel 0 and target Structure */
203 7, /* SCSIid */
204 NCC1_DEFAULT, /* SCSIconfig1 */
205 0, /* SCSIconfig2 */
206 0x10, /* NumSCSItarget */
207
208 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
209 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
210 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
211 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
212
213 /* SCSI channel 1 and target Structure */
214 7, /* SCSIid */
215 NCC1_DEFAULT, /* SCSIconfig1 */
216 0, /* SCSIconfig2 */
217 0x10, /* NumSCSItarget */
218
219 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
220 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
221 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
222 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
223 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
224 0, 0}; /* - CheckSum - */
225
226
Alan Cox72d39fe2007-05-21 15:06:43 +0100227static u8 initio_rate_tbl[8] = /* fast 20 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300229 /* nanosecond divide by 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 12, /* 50ns, 20M */
231 18, /* 75ns, 13.3M */
232 25, /* 100ns, 10M */
233 31, /* 125ns, 8M */
234 37, /* 150ns, 6.6M */
235 43, /* 175ns, 5.7M */
236 50, /* 200ns, 5M */
237 62 /* 250ns, 4M */
238};
239
Alan Cox72d39fe2007-05-21 15:06:43 +0100240static void initio_do_pause(unsigned amount)
241{
242 /* Pause for amount jiffies */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long the_time = jiffies + amount;
244
Alan Cox72d39fe2007-05-21 15:06:43 +0100245 while (time_before_eq(jiffies, the_time))
246 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/*-- forward reference --*/
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/******************************************************************
252 Input: instruction for Serial E2PROM
253
254 EX: se2_rd(0 call se2_instr() to send address and read command
255
256 StartBit OP_Code Address Data
257 --------- -------- ------------------ -------
258 1 1 , 0 A5,A4,A3,A2,A1,A0 D15-D0
259
260 +-----------------------------------------------------
261 |
262 CS -----+
263 +--+ +--+ +--+ +--+ +--+
264 ^ | ^ | ^ | ^ | ^ |
265 | | | | | | | | | |
266 CLK -------+ +--+ +--+ +--+ +--+ +--
267 (leading edge trigger)
268
269 +--1-----1--+
270 | SB OP | OP A5 A4
271 DI ----+ +--0------------------
272 (address and cmd sent to nvram)
273
274 -------------------------------------------+
275 |
276 DO +---
277 (data sent from nvram)
278
279
280******************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100281
282/**
283 * initio_se2_instr - bitbang an instruction
284 * @base: Base of InitIO controller
285 * @instr: Instruction for serial E2PROM
286 *
287 * Bitbang an instruction out to the serial E2Prom
288 */
289
290static void initio_se2_instr(unsigned long base, u8 instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100293 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Alan Cox72d39fe2007-05-21 15:06:43 +0100295 outb(SE2CS | SE2DO, base + TUL_NVRAM); /* cs+start bit */
296 udelay(30);
297 outb(SE2CS | SE2CLK | SE2DO, base + TUL_NVRAM); /* +CLK */
298 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 for (i = 0; i < 8; i++) {
301 if (instr & 0x80)
Alan Cox72d39fe2007-05-21 15:06:43 +0100302 b = SE2CS | SE2DO; /* -CLK+dataBit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100304 b = SE2CS; /* -CLK */
305 outb(b, base + TUL_NVRAM);
306 udelay(30);
307 outb(b | SE2CLK, base + TUL_NVRAM); /* +CLK */
308 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 instr <<= 1;
310 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100311 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
312 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315
Alan Cox72d39fe2007-05-21 15:06:43 +0100316/**
317 * initio_se2_ew_en - Enable erase/write
318 * @base: Base address of InitIO controller
319 *
320 * Enable erase/write state of serial EEPROM
321 */
322void initio_se2_ew_en(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Alan Cox72d39fe2007-05-21 15:06:43 +0100324 initio_se2_instr(base, 0x30); /* EWEN */
325 outb(0, base + TUL_NVRAM); /* -CS */
326 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329
Alan Cox72d39fe2007-05-21 15:06:43 +0100330/**
331 * initio_se2_ew_ds - Disable erase/write
332 * @base: Base address of InitIO controller
333 *
334 * Disable erase/write state of serial EEPROM
335 */
336void initio_se2_ew_ds(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Alan Cox72d39fe2007-05-21 15:06:43 +0100338 initio_se2_instr(base, 0); /* EWDS */
339 outb(0, base + TUL_NVRAM); /* -CS */
340 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343
Alan Cox72d39fe2007-05-21 15:06:43 +0100344/**
345 * initio_se2_rd - read E2PROM word
346 * @base: Base of InitIO controller
347 * @addr: Address of word in E2PROM
348 *
349 * Read a word from the NV E2PROM device
350 */
351static u16 initio_se2_rd(unsigned long base, u8 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Alan Cox72d39fe2007-05-21 15:06:43 +0100353 u8 instr, rb;
354 u16 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int i;
356
Alan Cox72d39fe2007-05-21 15:06:43 +0100357 instr = (u8) (addr | 0x80);
358 initio_se2_instr(base, instr); /* READ INSTR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 for (i = 15; i >= 0; i--) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100361 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
362 udelay(30);
363 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* sample data after the following edge of clock */
Alan Cox72d39fe2007-05-21 15:06:43 +0100366 rb = inb(base + TUL_NVRAM);
367 rb &= SE2DI;
368 val += (rb << i);
369 udelay(30); /* 6/20/95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371
Alan Cox72d39fe2007-05-21 15:06:43 +0100372 outb(0, base + TUL_NVRAM); /* no chip select */
373 udelay(30);
374 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Alan Cox72d39fe2007-05-21 15:06:43 +0100377/**
378 * initio_se2_wr - read E2PROM word
379 * @base: Base of InitIO controller
380 * @addr: Address of word in E2PROM
381 * @val: Value to write
382 *
383 * Write a word to the NV E2PROM device. Used when recovering from
384 * a problem with the NV.
385 */
386static void initio_se2_wr(unsigned long base, u8 addr, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Alan Cox72d39fe2007-05-21 15:06:43 +0100388 u8 rb;
389 u8 instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int i;
391
Alan Cox72d39fe2007-05-21 15:06:43 +0100392 instr = (u8) (addr | 0x40);
393 initio_se2_instr(base, instr); /* WRITE INSTR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 for (i = 15; i >= 0; i--) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100395 if (val & 0x8000)
396 outb(SE2CS | SE2DO, base + TUL_NVRAM); /* -CLK+dataBit 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100398 outb(SE2CS, base + TUL_NVRAM); /* -CLK+dataBit 0 */
399 udelay(30);
400 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
401 udelay(30);
402 val <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100404 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
405 udelay(30);
406 outb(0, base + TUL_NVRAM); /* -CS */
407 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Alan Cox72d39fe2007-05-21 15:06:43 +0100409 outb(SE2CS, base + TUL_NVRAM); /* +CS */
410 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100413 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
414 udelay(30);
415 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
416 udelay(30);
417 if ((rb = inb(base + TUL_NVRAM)) & SE2DI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 break; /* write complete */
419 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100420 outb(0, base + TUL_NVRAM); /* -CS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Alan Cox72d39fe2007-05-21 15:06:43 +0100423/**
424 * initio_se2_rd_all - read hostadapter NV configuration
425 * @base: Base address of InitIO controller
426 *
427 * Reads the E2PROM data into main memory. Ensures that the checksum
428 * and header marker are valid. Returns 1 on success -1 on error.
429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Alan Cox72d39fe2007-05-21 15:06:43 +0100431static int initio_se2_rd_all(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100434 u16 chksum = 0;
435 u16 *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 i91unvramp = &i91unvram;
Alan Cox72d39fe2007-05-21 15:06:43 +0100438 np = (u16 *) i91unvramp;
439 for (i = 0; i < 32; i++)
440 *np++ = initio_se2_rd(base, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Alan Cox72d39fe2007-05-21 15:06:43 +0100442 /* Is signature "ini" ok ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (i91unvramp->NVM_Signature != INI_SIGNATURE)
444 return -1;
Alan Cox72d39fe2007-05-21 15:06:43 +0100445 /* Is ckecksum ok ? */
446 np = (u16 *) i91unvramp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 for (i = 0; i < 31; i++)
448 chksum += *np++;
Alan Cox72d39fe2007-05-21 15:06:43 +0100449 if (i91unvramp->NVM_CheckSum != chksum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return -1;
451 return 1;
452}
453
Alan Cox72d39fe2007-05-21 15:06:43 +0100454/**
455 * initio_se2_update_all - Update E2PROM
456 * @base: Base of InitIO controller
457 *
458 * Update the E2PROM by wrting any changes into the E2PROM
459 * chip, rewriting the checksum.
460 */
461static void initio_se2_update_all(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{ /* setup default pattern */
463 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100464 u16 chksum = 0;
465 u16 *np, *np1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 i91unvramp = &i91unvram;
468 /* Calculate checksum first */
Alan Cox72d39fe2007-05-21 15:06:43 +0100469 np = (u16 *) i91udftNvRam;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 for (i = 0; i < 31; i++)
471 chksum += *np++;
Alan Cox72d39fe2007-05-21 15:06:43 +0100472 *np = chksum;
473 initio_se2_ew_en(base); /* Enable write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Alan Cox72d39fe2007-05-21 15:06:43 +0100475 np = (u16 *) i91udftNvRam;
476 np1 = (u16 *) i91unvramp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 for (i = 0; i < 32; i++, np++, np1++) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100478 if (*np != *np1)
479 initio_se2_wr(base, i, *np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100481 initio_se2_ew_ds(base); /* Disable write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Alan Cox72d39fe2007-05-21 15:06:43 +0100484/**
485 * initio_read_eeprom - Retrieve configuration
486 * @base: Base of InitIO Host Adapter
487 *
488 * Retrieve the host adapter configuration data from E2Prom. If the
489 * data is invalid then the defaults are used and are also restored
490 * into the E2PROM. This forms the access point for the SCSI driver
491 * into the E2PROM layer, the other functions for the E2PROM are all
492 * internal use.
493 *
494 * Must be called single threaded, uses a shared global area.
495 */
496
497static void initio_read_eeprom(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Alan Cox72d39fe2007-05-21 15:06:43 +0100499 u8 gctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 i91unvramp = &i91unvram;
Alan Cox72d39fe2007-05-21 15:06:43 +0100502 /* Enable EEProm programming */
503 gctrl = inb(base + TUL_GCTRL);
504 outb(gctrl | TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
505 if (initio_se2_rd_all(base) != 1) {
506 initio_se2_update_all(base); /* setup default pattern */
507 initio_se2_rd_all(base); /* load again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100509 /* Disable EEProm programming */
510 gctrl = inb(base + TUL_GCTRL);
511 outb(gctrl & ~TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Alan Cox72d39fe2007-05-21 15:06:43 +0100514/**
515 * initio_stop_bm - stop bus master
516 * @host: InitIO we are stopping
517 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800518 * Stop any pending DMA operation, aborting the DMA if necessary
Alan Cox72d39fe2007-05-21 15:06:43 +0100519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Alan Cox72d39fe2007-05-21 15:06:43 +0100521static void initio_stop_bm(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523
Alan Cox72d39fe2007-05-21 15:06:43 +0100524 if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */
525 outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* wait Abort DMA xfer done */
Alan Cox72d39fe2007-05-21 15:06:43 +0100527 while ((inb(host->addr + TUL_Int) & XABT) == 0)
528 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100530 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Alan Cox72d39fe2007-05-21 15:06:43 +0100533/**
534 * initio_reset_scsi - Reset SCSI host controller
535 * @host: InitIO host to reset
536 * @seconds: Recovery time
537 *
538 * Perform a full reset of the SCSI subsystem.
539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Alan Cox72d39fe2007-05-21 15:06:43 +0100541static int initio_reset_scsi(struct initio_host * host, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Alan Cox72d39fe2007-05-21 15:06:43 +0100543 outb(TSC_RST_BUS, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Alan Cox72d39fe2007-05-21 15:06:43 +0100545 while (!((host->jsint = inb(host->addr + TUL_SInt)) & TSS_SCSIRST_INT))
546 cpu_relax();
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* reset tulip chip */
Alan Cox72d39fe2007-05-21 15:06:43 +0100549 outb(0, host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* Stall for a while, wait for target's firmware ready,make it 2 sec ! */
552 /* SONY 5200 tape drive won't work if only stall for 1 sec */
Alan Cox72d39fe2007-05-21 15:06:43 +0100553 /* FIXME: this is a very long busy wait right now */
554 initio_do_pause(seconds * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Alan Cox72d39fe2007-05-21 15:06:43 +0100556 inb(host->addr + TUL_SInt);
557 return SCSI_RESET_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Alan Cox72d39fe2007-05-21 15:06:43 +0100560/**
561 * initio_init - set up an InitIO host adapter
562 * @host: InitIO host adapter
563 * @num_scbs: Number of SCBS
564 * @bios_addr: BIOS address
565 *
566 * Set up the host adapter and devices according to the configuration
567 * retrieved from the E2PROM.
568 *
569 * Locking: Calls E2PROM layer code which is not re-enterable so must
570 * run single threaded for now.
571 */
572
573static void initio_init(struct initio_host * host, u8 *bios_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100576 u8 *flags;
577 u8 *heads;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Alan Cox72d39fe2007-05-21 15:06:43 +0100579 /* Get E2Prom configuration */
580 initio_read_eeprom(host->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (i91unvramp->NVM_SCSIInfo[0].NVM_NumOfTarg == 8)
Alan Cox72d39fe2007-05-21 15:06:43 +0100582 host->max_tar = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100584 host->max_tar = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Alan Cox72d39fe2007-05-21 15:06:43 +0100586 host->config = i91unvramp->NVM_SCSIInfo[0].NVM_ChConfig1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Alan Cox72d39fe2007-05-21 15:06:43 +0100588 host->scsi_id = i91unvramp->NVM_SCSIInfo[0].NVM_ChSCSIID;
589 host->idmask = ~(1 << host->scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Olaf Hering44456d32005-07-27 11:45:17 -0700591#ifdef CHK_PARITY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* Enable parity error response */
Alan Cox72d39fe2007-05-21 15:06:43 +0100593 outb(inb(host->addr + TUL_PCMD) | 0x40, host->addr + TUL_PCMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#endif
595
596 /* Mask all the interrupt */
Alan Cox72d39fe2007-05-21 15:06:43 +0100597 outb(0x1F, host->addr + TUL_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Alan Cox72d39fe2007-05-21 15:06:43 +0100599 initio_stop_bm(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /* --- Initialize the tulip --- */
Alan Cox72d39fe2007-05-21 15:06:43 +0100601 outb(TSC_RST_CHIP, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* program HBA's SCSI ID */
Alan Cox72d39fe2007-05-21 15:06:43 +0100604 outb(host->scsi_id << 4, host->addr + TUL_SScsiId);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* Enable Initiator Mode ,phase latch,alternate sync period mode,
607 disable SCSI reset */
Alan Cox72d39fe2007-05-21 15:06:43 +0100608 if (host->config & HCC_EN_PAR)
609 host->sconf1 = (TSC_INITDEFAULT | TSC_EN_SCSI_PAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100611 host->sconf1 = (TSC_INITDEFAULT);
612 outb(host->sconf1, host->addr + TUL_SConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Alan Cox72d39fe2007-05-21 15:06:43 +0100614 /* Enable HW reselect */
615 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Alan Cox72d39fe2007-05-21 15:06:43 +0100617 outb(0, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* selection time out = 250 ms */
Alan Cox72d39fe2007-05-21 15:06:43 +0100620 outb(153, host->addr + TUL_STimeOut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Alan Cox72d39fe2007-05-21 15:06:43 +0100622 /* Enable SCSI terminator */
623 outb((host->config & (HCC_ACT_TERM1 | HCC_ACT_TERM2)),
624 host->addr + TUL_XCtrl);
625 outb(((host->config & HCC_AUTO_TERM) >> 4) |
626 (inb(host->addr + TUL_GCTRL1) & 0xFE),
627 host->addr + TUL_GCTRL1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 for (i = 0,
Alan Cox72d39fe2007-05-21 15:06:43 +0100630 flags = & (i91unvramp->NVM_SCSIInfo[0].NVM_Targ0Config),
631 heads = bios_addr + 0x180;
632 i < host->max_tar;
633 i++, flags++) {
634 host->targets[i].flags = *flags & ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
635 if (host->targets[i].flags & TCF_EN_255)
636 host->targets[i].drv_flags = TCF_DRV_255_63;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100638 host->targets[i].drv_flags = 0;
639 host->targets[i].js_period = 0;
640 host->targets[i].sconfig0 = host->sconf1;
641 host->targets[i].heads = *heads++;
642 if (host->targets[i].heads == 255)
643 host->targets[i].drv_flags = TCF_DRV_255_63;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100645 host->targets[i].drv_flags = 0;
646 host->targets[i].sectors = *heads++;
647 host->targets[i].flags &= ~TCF_BUSY;
648 host->act_tags[i] = 0;
649 host->max_tags[i] = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 } /* for */
651 printk("i91u: PCI Base=0x%04X, IRQ=%d, BIOS=0x%04X0, SCSI ID=%d\n",
Alan Coxe9e42fa2007-10-15 20:51:32 +0100652 host->addr, host->pci_dev->irq,
Alan Cox72d39fe2007-05-21 15:06:43 +0100653 host->bios_addr, host->scsi_id);
654 /* Reset SCSI Bus */
655 if (host->config & HCC_SCSI_RESET) {
656 printk(KERN_INFO "i91u: Reset SCSI Bus ... \n");
657 initio_reset_scsi(host, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100659 outb(0x17, host->addr + TUL_SCFG1);
660 outb(0xE9, host->addr + TUL_SIntEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Alan Cox72d39fe2007-05-21 15:06:43 +0100663/**
664 * initio_alloc_scb - Allocate an SCB
665 * @host: InitIO host we are allocating for
666 *
667 * Walk the SCB list for the controller and allocate a free SCB if
668 * one exists.
669 */
670static struct scsi_ctrl_blk *initio_alloc_scb(struct initio_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Alan Cox72d39fe2007-05-21 15:06:43 +0100672 struct scsi_ctrl_blk *scb;
673 unsigned long flags;
674
675 spin_lock_irqsave(&host->avail_lock, flags);
676 if ((scb = host->first_avail) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100678 printk("find scb at %p\n", scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100680 if ((host->first_avail = scb->next) == NULL)
681 host->last_avail = NULL;
682 scb->next = NULL;
683 scb->status = SCB_RENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100685 spin_unlock_irqrestore(&host->avail_lock, flags);
686 return scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Alan Cox72d39fe2007-05-21 15:06:43 +0100689/**
690 * initio_release_scb - Release an SCB
691 * @host: InitIO host that owns the SCB
692 * @cmnd: SCB command block being returned
693 *
694 * Return an allocated SCB to the host free list
695 */
696
697static void initio_release_scb(struct initio_host * host, struct scsi_ctrl_blk * cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Alan Cox72d39fe2007-05-21 15:06:43 +0100699 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100702 printk("Release SCB %p; ", cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100704 spin_lock_irqsave(&(host->avail_lock), flags);
705 cmnd->srb = NULL;
706 cmnd->status = 0;
707 cmnd->next = NULL;
708 if (host->last_avail != NULL) {
709 host->last_avail->next = cmnd;
710 host->last_avail = cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100712 host->first_avail = cmnd;
713 host->last_avail = cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100715 spin_unlock_irqrestore(&(host->avail_lock), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100719static void initio_append_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721
722#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100723 printk("Append pend SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100725 scbp->status = SCB_PEND;
726 scbp->next = NULL;
727 if (host->last_pending != NULL) {
728 host->last_pending->next = scbp;
729 host->last_pending = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100731 host->first_pending = scbp;
732 host->last_pending = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734}
735
736/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100737static void initio_push_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739
740#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100741 printk("Push pend SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100743 scbp->status = SCB_PEND;
744 if ((scbp->next = host->first_pending) != NULL) {
745 host->first_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
Alan Cox72d39fe2007-05-21 15:06:43 +0100752static struct scsi_ctrl_blk *initio_find_first_pend_scb(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Alan Cox72d39fe2007-05-21 15:06:43 +0100754 struct scsi_ctrl_blk *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756
Alan Cox72d39fe2007-05-21 15:06:43 +0100757 first = host->first_pending;
758 while (first != NULL) {
759 if (first->opcode != ExecSCSI)
760 return first;
761 if (first->tagmsg == 0) {
762 if ((host->act_tags[first->target] == 0) &&
763 !(host->targets[first->target].flags & TCF_BUSY))
764 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100766 if ((host->act_tags[first->target] >=
767 host->max_tags[first->target]) |
768 (host->targets[first->target].flags & TCF_BUSY)) {
769 first = first->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 continue;
771 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100772 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100774 first = first->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100776 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
Alan Cox72d39fe2007-05-21 15:06:43 +0100778
779static void initio_unlink_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Alan Cox72d39fe2007-05-21 15:06:43 +0100781 struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100784 printk("unlink pend SCB %p; ", scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#endif
786
Alan Cox72d39fe2007-05-21 15:06:43 +0100787 prev = tmp = host->first_pending;
788 while (tmp != NULL) {
789 if (scb == tmp) { /* Unlink this SCB */
790 if (tmp == host->first_pending) {
791 if ((host->first_pending = tmp->next) == NULL)
792 host->last_pending = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100794 prev->next = tmp->next;
795 if (tmp == host->last_pending)
796 host->last_pending = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100798 tmp->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
800 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100801 prev = tmp;
802 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
Alan Cox72d39fe2007-05-21 15:06:43 +0100805
806static void initio_append_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808
809#if DEBUG_QUEUE
Stuart Swalese2d435e2008-01-23 20:00:48 +0000810 printk("append busy SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100812 if (scbp->tagmsg)
813 host->act_tags[scbp->target]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100815 host->targets[scbp->target].flags |= TCF_BUSY;
816 scbp->status = SCB_BUSY;
817 scbp->next = NULL;
818 if (host->last_busy != NULL) {
819 host->last_busy->next = scbp;
820 host->last_busy = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100822 host->first_busy = scbp;
823 host->last_busy = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825}
826
827/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100828static struct scsi_ctrl_blk *initio_pop_busy_scb(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Alan Cox72d39fe2007-05-21 15:06:43 +0100830 struct scsi_ctrl_blk *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832
Alan Cox72d39fe2007-05-21 15:06:43 +0100833 if ((tmp = host->first_busy) != NULL) {
834 if ((host->first_busy = tmp->next) == NULL)
835 host->last_busy = NULL;
836 tmp->next = NULL;
837 if (tmp->tagmsg)
838 host->act_tags[tmp->target]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100840 host->targets[tmp->target].flags &= ~TCF_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100843 printk("Pop busy SCB %p; ", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100845 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100849static void initio_unlink_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Alan Cox72d39fe2007-05-21 15:06:43 +0100851 struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100854 printk("unlink busy SCB %p; ", scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855#endif
856
Alan Cox72d39fe2007-05-21 15:06:43 +0100857 prev = tmp = host->first_busy;
858 while (tmp != NULL) {
859 if (scb == tmp) { /* Unlink this SCB */
860 if (tmp == host->first_busy) {
861 if ((host->first_busy = tmp->next) == NULL)
862 host->last_busy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100864 prev->next = tmp->next;
865 if (tmp == host->last_busy)
866 host->last_busy = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100868 tmp->next = NULL;
869 if (tmp->tagmsg)
870 host->act_tags[tmp->target]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100872 host->targets[tmp->target].flags &= ~TCF_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 break;
874 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100875 prev = tmp;
876 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 return;
879}
880
Alan Cox72d39fe2007-05-21 15:06:43 +0100881struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Alan Cox72d39fe2007-05-21 15:06:43 +0100883 struct scsi_ctrl_blk *tmp, *prev;
884 u16 scbp_tarlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886
Alan Cox72d39fe2007-05-21 15:06:43 +0100887 prev = tmp = host->first_busy;
888 while (tmp != NULL) {
889 scbp_tarlun = (tmp->lun << 8) | (tmp->target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (scbp_tarlun == tarlun) { /* Unlink this SCB */
891 break;
892 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100893 prev = tmp;
894 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100897 printk("find busy SCB %p; ", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100899 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Alan Cox72d39fe2007-05-21 15:06:43 +0100902static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100905 printk("append done SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#endif
907
Alan Cox72d39fe2007-05-21 15:06:43 +0100908 scbp->status = SCB_DONE;
909 scbp->next = NULL;
910 if (host->last_done != NULL) {
911 host->last_done->next = scbp;
912 host->last_done = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100914 host->first_done = scbp;
915 host->last_done = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917}
918
Alan Cox72d39fe2007-05-21 15:06:43 +0100919struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Alan Cox72d39fe2007-05-21 15:06:43 +0100921 struct scsi_ctrl_blk *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Alan Cox72d39fe2007-05-21 15:06:43 +0100923 if ((tmp = host->first_done) != NULL) {
924 if ((host->first_done = tmp->next) == NULL)
925 host->last_done = NULL;
926 tmp->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100929 printk("find done SCB %p; ",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100931 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Alan Cox72d39fe2007-05-21 15:06:43 +0100934static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Alan Cox72d39fe2007-05-21 15:06:43 +0100936 unsigned long flags;
937 struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Alan Cox72d39fe2007-05-21 15:06:43 +0100939 spin_lock_irqsave(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Alan Cox72d39fe2007-05-21 15:06:43 +0100941 if ((host->semaph == 0) && (host->active == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* disable Jasmin SCSI Int */
Alan Cox72d39fe2007-05-21 15:06:43 +0100943 outb(0x1F, host->addr + TUL_Mask);
944 spin_unlock_irqrestore(&host->semaph_lock, flags);
945 /* FIXME: synchronize_irq needed ? */
946 tulip_main(host);
947 spin_lock_irqsave(&host->semaph_lock, flags);
948 host->semaph = 1;
949 outb(0x0F, host->addr + TUL_Mask);
950 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return SCSI_ABORT_SNOOZE;
952 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100953 prev = tmp = host->first_pending; /* Check Pend queue */
954 while (tmp != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* 07/27/98 */
Alan Cox72d39fe2007-05-21 15:06:43 +0100956 if (tmp->srb == srbp) {
957 if (tmp == host->active) {
958 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return SCSI_ABORT_BUSY;
Alan Cox72d39fe2007-05-21 15:06:43 +0100960 } else if (tmp == host->first_pending) {
961 if ((host->first_pending = tmp->next) == NULL)
962 host->last_pending = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100964 prev->next = tmp->next;
965 if (tmp == host->last_pending)
966 host->last_pending = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100968 tmp->hastat = HOST_ABORTED;
969 tmp->flags |= SCF_DONE;
970 if (tmp->flags & SCF_POST)
971 (*tmp->post) ((u8 *) host, (u8 *) tmp);
972 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return SCSI_ABORT_SUCCESS;
974 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100975 prev = tmp;
976 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978
Alan Cox72d39fe2007-05-21 15:06:43 +0100979 prev = tmp = host->first_busy; /* Check Busy queue */
980 while (tmp != NULL) {
981 if (tmp->srb == srbp) {
982 if (tmp == host->active) {
983 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return SCSI_ABORT_BUSY;
Alan Cox72d39fe2007-05-21 15:06:43 +0100985 } else if (tmp->tagmsg == 0) {
986 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return SCSI_ABORT_BUSY;
988 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100989 host->act_tags[tmp->target]--;
990 if (tmp == host->first_busy) {
991 if ((host->first_busy = tmp->next) == NULL)
992 host->last_busy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100994 prev->next = tmp->next;
995 if (tmp == host->last_busy)
996 host->last_busy = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100998 tmp->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000
Alan Cox72d39fe2007-05-21 15:06:43 +01001001 tmp->hastat = HOST_ABORTED;
1002 tmp->flags |= SCF_DONE;
1003 if (tmp->flags & SCF_POST)
1004 (*tmp->post) ((u8 *) host, (u8 *) tmp);
1005 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return SCSI_ABORT_SUCCESS;
1007 }
1008 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001009 prev = tmp;
1010 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001012 spin_unlock_irqrestore(&host->semaph_lock, flags);
1013 return SCSI_ABORT_NOT_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +01001017static int initio_bad_seq(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Alan Cox72d39fe2007-05-21 15:06:43 +01001019 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Alan Cox72d39fe2007-05-21 15:06:43 +01001021 printk("initio_bad_seg c=%d\n", host->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Alan Cox72d39fe2007-05-21 15:06:43 +01001023 if ((scb = host->active) != NULL) {
1024 initio_unlink_busy_scb(host, scb);
1025 scb->hastat = HOST_BAD_PHAS;
1026 scb->tastat = 0;
1027 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001029 initio_stop_bm(host);
1030 initio_reset_scsi(host, 8); /* 7/29/98 */
1031 return initio_post_scsi_rst(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Adrian Bunka2ba1922005-06-25 14:58:50 -07001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +01001036static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Alan Cox72d39fe2007-05-21 15:06:43 +01001038 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Alan Cox72d39fe2007-05-21 15:06:43 +01001040 scb->mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Alan Cox72d39fe2007-05-21 15:06:43 +01001042 scb->sgidx = 0;
1043 scb->sgmax = scb->sglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Alan Cox72d39fe2007-05-21 15:06:43 +01001045 spin_lock_irqsave(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Alan Cox72d39fe2007-05-21 15:06:43 +01001047 initio_append_pend_scb(host, scb); /* Append this SCB to Pending queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049/* VVVVV 07/21/98 */
Alan Cox72d39fe2007-05-21 15:06:43 +01001050 if (host->semaph == 1) {
1051 /* Disable Jasmin SCSI Int */
1052 outb(0x1F, host->addr + TUL_Mask);
1053 host->semaph = 0;
1054 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Alan Cox72d39fe2007-05-21 15:06:43 +01001056 tulip_main(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Alan Cox72d39fe2007-05-21 15:06:43 +01001058 spin_lock_irqsave(&host->semaph_lock, flags);
1059 host->semaph = 1;
1060 outb(0x0F, host->addr + TUL_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001062 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return;
1064}
1065
1066/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +01001067static int initio_isr(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Alan Cox72d39fe2007-05-21 15:06:43 +01001069 if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) {
1070 if (host->semaph == 1) {
1071 outb(0x1F, host->addr + TUL_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /* Disable Tulip SCSI Int */
Alan Cox72d39fe2007-05-21 15:06:43 +01001073 host->semaph = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Alan Cox72d39fe2007-05-21 15:06:43 +01001075 tulip_main(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Alan Cox72d39fe2007-05-21 15:06:43 +01001077 host->semaph = 1;
1078 outb(0x0F, host->addr + TUL_Mask);
1079 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Alan Cox72d39fe2007-05-21 15:06:43 +01001085static int tulip_main(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Alan Cox72d39fe2007-05-21 15:06:43 +01001087 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001090 tulip_scsi(host); /* Call tulip_scsi */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Alan Cox72d39fe2007-05-21 15:06:43 +01001092 /* Walk the list of completed SCBs */
1093 while ((scb = initio_find_done_scb(host)) != NULL) { /* find done entry */
1094 if (scb->tastat == INI_QUEUE_FULL) {
1095 host->max_tags[scb->target] =
1096 host->act_tags[scb->target] - 1;
1097 scb->tastat = 0;
1098 initio_append_pend_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 continue;
1100 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001101 if (!(scb->mode & SCM_RSENS)) { /* not in auto req. sense mode */
1102 if (scb->tastat == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /* clr sync. nego flag */
1105
Alan Cox72d39fe2007-05-21 15:06:43 +01001106 if (scb->flags & SCF_SENSE) {
1107 u8 len;
1108 len = scb->senselen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (len == 0)
1110 len = 1;
Alan Cox72d39fe2007-05-21 15:06:43 +01001111 scb->buflen = scb->senselen;
1112 scb->bufptr = scb->senseptr;
1113 scb->flags &= ~(SCF_SG | SCF_DIR); /* for xfer_data_in */
1114 /* so, we won't report wrong direction in xfer_data_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 and won't report HOST_DO_DU in state_6 */
Alan Cox72d39fe2007-05-21 15:06:43 +01001116 scb->mode = SCM_RSENS;
1117 scb->ident &= 0xBF; /* Disable Disconnect */
1118 scb->tagmsg = 0;
1119 scb->tastat = 0;
1120 scb->cdblen = 6;
1121 scb->cdb[0] = SCSICMD_RequestSense;
1122 scb->cdb[1] = 0;
1123 scb->cdb[2] = 0;
1124 scb->cdb[3] = 0;
1125 scb->cdb[4] = len;
1126 scb->cdb[5] = 0;
1127 initio_push_pend_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 break;
1129 }
1130 }
1131 } else { /* in request sense mode */
1132
Alan Cox72d39fe2007-05-21 15:06:43 +01001133 if (scb->tastat == 2) { /* check contition status again after sending
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 requset sense cmd 0x3 */
Alan Cox72d39fe2007-05-21 15:06:43 +01001135 scb->hastat = HOST_BAD_PHAS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001137 scb->tastat = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001139 scb->flags |= SCF_DONE;
1140 if (scb->flags & SCF_POST) {
1141 /* FIXME: only one post method and lose casts */
1142 (*scb->post) ((u8 *) host, (u8 *) scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /* find_active: */
Alan Cox72d39fe2007-05-21 15:06:43 +01001146 if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 continue;
Alan Cox72d39fe2007-05-21 15:06:43 +01001148 if (host->active) /* return to OS and wait for xfer_done_ISR/Selected_ISR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return 1; /* return to OS, enable interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* Check pending SCB */
Alan Cox72d39fe2007-05-21 15:06:43 +01001151 if (initio_find_first_pend_scb(host) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return 1; /* return to OS, enable interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 } /* End of for loop */
1154 /* statement won't reach here */
1155}
1156
Alan Cox72d39fe2007-05-21 15:06:43 +01001157static void tulip_scsi(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Alan Cox72d39fe2007-05-21 15:06:43 +01001159 struct scsi_ctrl_blk *scb;
1160 struct target_control *active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* make sure to service interrupt asap */
Alan Cox72d39fe2007-05-21 15:06:43 +01001163 if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) {
1164 host->phase = host->jsstatus0 & TSS_PH_MASK;
1165 host->jsstatus1 = inb(host->addr + TUL_SStatus1);
1166 host->jsint = inb(host->addr + TUL_SInt);
1167 if (host->jsint & TSS_SCSIRST_INT) { /* SCSI bus reset detected */
1168 int_initio_scsi_rst(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return;
1170 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001171 if (host->jsint & TSS_RESEL_INT) { /* if selected/reselected interrupt */
1172 if (int_initio_resel(host) == 0)
1173 initio_next_state(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return;
1175 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001176 if (host->jsint & TSS_SEL_TIMEOUT) {
1177 int_initio_busfree(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return;
1179 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001180 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
1181 int_initio_busfree(host); /* unexpected bus free or sel timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return;
1183 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001184 if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) { /* func complete or Bus service */
1185 if ((scb = host->active) != NULL)
1186 initio_next_state(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return;
1188 }
1189 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001190 if (host->active != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return;
1192
Alan Cox72d39fe2007-05-21 15:06:43 +01001193 if ((scb = initio_find_first_pend_scb(host)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return;
1195
1196 /* program HBA's SCSI ID & target SCSI ID */
Alan Cox72d39fe2007-05-21 15:06:43 +01001197 outb((host->scsi_id << 4) | (scb->target & 0x0F),
1198 host->addr + TUL_SScsiId);
1199 if (scb->opcode == ExecSCSI) {
1200 active_tc = &host->targets[scb->target];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Alan Cox72d39fe2007-05-21 15:06:43 +01001202 if (scb->tagmsg)
1203 active_tc->drv_flags |= TCF_DRV_EN_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001205 active_tc->drv_flags &= ~TCF_DRV_EN_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Alan Cox72d39fe2007-05-21 15:06:43 +01001207 outb(active_tc->js_period, host->addr + TUL_SPeriod);
1208 if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) { /* do wdtr negotiation */
1209 initio_select_atn_stop(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001211 if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) { /* do sync negotiation */
1212 initio_select_atn_stop(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001214 if (scb->tagmsg)
1215 initio_select_atn3(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001217 initio_select_atn(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001220 if (scb->flags & SCF_POLL) {
1221 while (wait_tulip(host) != -1) {
1222 if (initio_next_state(host) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 break;
1224 }
1225 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001226 } else if (scb->opcode == BusDevRst) {
1227 initio_select_atn_stop(host, scb);
1228 scb->next_state = 8;
1229 if (scb->flags & SCF_POLL) {
1230 while (wait_tulip(host) != -1) {
1231 if (initio_next_state(host) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 break;
1233 }
1234 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001235 } else if (scb->opcode == AbortCmd) {
1236 if (initio_abort_srb(host, scb->srb) != 0) {
1237 initio_unlink_pend_scb(host, scb);
1238 initio_release_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001240 scb->opcode = BusDevRst;
1241 initio_select_atn_stop(host, scb);
1242 scb->next_state = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001245 initio_unlink_pend_scb(host, scb);
1246 scb->hastat = 0x16; /* bad command */
1247 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 return;
1250}
1251
Alan Cox72d39fe2007-05-21 15:06:43 +01001252/**
1253 * initio_next_state - Next SCSI state
1254 * @host: InitIO host we are processing
1255 *
1256 * Progress the active command block along the state machine
1257 * until we hit a state which we must wait for activity to occur.
1258 *
1259 * Returns zero or a negative code.
1260 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Alan Cox72d39fe2007-05-21 15:06:43 +01001262static int initio_next_state(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 int next;
1265
Alan Cox72d39fe2007-05-21 15:06:43 +01001266 next = host->active->next_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 for (;;) {
1268 switch (next) {
1269 case 1:
Alan Cox72d39fe2007-05-21 15:06:43 +01001270 next = initio_state_1(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272 case 2:
Alan Cox72d39fe2007-05-21 15:06:43 +01001273 next = initio_state_2(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
1275 case 3:
Alan Cox72d39fe2007-05-21 15:06:43 +01001276 next = initio_state_3(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 break;
1278 case 4:
Alan Cox72d39fe2007-05-21 15:06:43 +01001279 next = initio_state_4(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 break;
1281 case 5:
Alan Cox72d39fe2007-05-21 15:06:43 +01001282 next = initio_state_5(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
1284 case 6:
Alan Cox72d39fe2007-05-21 15:06:43 +01001285 next = initio_state_6(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 break;
1287 case 7:
Alan Cox72d39fe2007-05-21 15:06:43 +01001288 next = initio_state_7(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 break;
1290 case 8:
Alan Cox72d39fe2007-05-21 15:06:43 +01001291 return initio_bus_device_reset(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001293 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295 if (next <= 0)
1296 return next;
1297 }
1298}
1299
1300
Alan Cox72d39fe2007-05-21 15:06:43 +01001301/**
1302 * initio_state_1 - SCSI state machine
1303 * @host: InitIO host we are controlling
1304 *
1305 * Perform SCSI state processing for Select/Attention/Stop
1306 */
1307
1308static int initio_state_1(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Alan Cox72d39fe2007-05-21 15:06:43 +01001310 struct scsi_ctrl_blk *scb = host->active;
1311 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312#if DEBUG_STATE
1313 printk("-s1-");
1314#endif
1315
Alan Cox72d39fe2007-05-21 15:06:43 +01001316 /* Move the SCB from pending to busy */
1317 initio_unlink_pend_scb(host, scb);
1318 initio_append_busy_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Alan Cox72d39fe2007-05-21 15:06:43 +01001320 outb(active_tc->sconfig0, host->addr + TUL_SConfig );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* ATN on */
Alan Cox72d39fe2007-05-21 15:06:43 +01001322 if (host->phase == MSG_OUT) {
1323 outb(TSC_EN_BUS_IN | TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1324 outb(scb->ident, host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Alan Cox72d39fe2007-05-21 15:06:43 +01001326 if (scb->tagmsg) {
1327 outb(scb->tagmsg, host->addr + TUL_SFifo);
1328 outb(scb->tagid, host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001330 if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {
1331 active_tc->flags |= TCF_WDTR_DONE;
1332 outb(MSG_EXTEND, host->addr + TUL_SFifo);
1333 outb(2, host->addr + TUL_SFifo); /* Extended msg length */
1334 outb(3, host->addr + TUL_SFifo); /* Sync request */
1335 outb(1, host->addr + TUL_SFifo); /* Start from 16 bits */
1336 } else if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {
1337 active_tc->flags |= TCF_SYNC_DONE;
1338 outb(MSG_EXTEND, host->addr + TUL_SFifo);
1339 outb(3, host->addr + TUL_SFifo); /* extended msg length */
1340 outb(1, host->addr + TUL_SFifo); /* sync request */
1341 outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1342 outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001344 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1345 if (wait_tulip(host) == -1)
1346 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001348 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1349 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1350 /* Into before CDB xfer */
1351 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
1354
Alan Cox72d39fe2007-05-21 15:06:43 +01001355/**
1356 * initio_state_2 - SCSI state machine
1357 * @host: InitIO host we are controlling
1358 *
1359 * state after selection with attention
1360 * state after selection with attention3
1361 */
1362
1363static int initio_state_2(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Alan Cox72d39fe2007-05-21 15:06:43 +01001365 struct scsi_ctrl_blk *scb = host->active;
1366 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367#if DEBUG_STATE
1368 printk("-s2-");
1369#endif
1370
Alan Cox72d39fe2007-05-21 15:06:43 +01001371 initio_unlink_pend_scb(host, scb);
1372 initio_append_busy_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Alan Cox72d39fe2007-05-21 15:06:43 +01001374 outb(active_tc->sconfig0, host->addr + TUL_SConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Alan Cox72d39fe2007-05-21 15:06:43 +01001376 if (host->jsstatus1 & TSS_CMD_PH_CMP)
1377 return 4;
1378
1379 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1380 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1381 /* Into before CDB xfer */
1382 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Alan Cox72d39fe2007-05-21 15:06:43 +01001385/**
1386 * initio_state_3 - SCSI state machine
1387 * @host: InitIO host we are controlling
1388 *
1389 * state before CDB xfer is done
1390 */
1391
1392static int initio_state_3(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Alan Cox72d39fe2007-05-21 15:06:43 +01001394 struct scsi_ctrl_blk *scb = host->active;
1395 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 int i;
1397
1398#if DEBUG_STATE
1399 printk("-s3-");
1400#endif
1401 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001402 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 case CMD_OUT: /* Command out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001404 for (i = 0; i < (int) scb->cdblen; i++)
1405 outb(scb->cdb[i], host->addr + TUL_SFifo);
1406 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1407 if (wait_tulip(host) == -1)
1408 return -1;
1409 if (host->phase == CMD_OUT)
1410 return initio_bad_seq(host);
1411 return 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 case MSG_IN: /* Message in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001414 scb->next_state = 3;
1415 if (initio_msgin(host) == -1)
1416 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 break;
1418
1419 case STATUS_IN: /* Status phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001420 if (initio_status_msg(host) == -1)
1421 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423
1424 case MSG_OUT: /* Message out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001425 if (active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) {
1426 outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
1427 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1428 if (wait_tulip(host) == -1)
1429 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001431 active_tc->flags |= TCF_SYNC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Alan Cox72d39fe2007-05-21 15:06:43 +01001433 outb(MSG_EXTEND, host->addr + TUL_SFifo);
1434 outb(3, host->addr + TUL_SFifo); /* ext. msg len */
1435 outb(1, host->addr + TUL_SFifo); /* sync request */
1436 outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1437 outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */
1438 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1439 if (wait_tulip(host) == -1)
1440 return -1;
1441 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1442 outb(inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7), host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 }
1445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001447 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449 }
1450}
1451
Alan Cox72d39fe2007-05-21 15:06:43 +01001452/**
1453 * initio_state_4 - SCSI state machine
1454 * @host: InitIO host we are controlling
1455 *
1456 * SCSI state machine. State 4
1457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Alan Cox72d39fe2007-05-21 15:06:43 +01001459static int initio_state_4(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Alan Cox72d39fe2007-05-21 15:06:43 +01001461 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463#if DEBUG_STATE
1464 printk("-s4-");
1465#endif
Alan Cox72d39fe2007-05-21 15:06:43 +01001466 if ((scb->flags & SCF_DIR) == SCF_NO_XF) {
1467 return 6; /* Go to state 6 (After data) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001470 if (scb->buflen == 0)
1471 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Alan Cox72d39fe2007-05-21 15:06:43 +01001473 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 case STATUS_IN: /* Status phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001476 if ((scb->flags & SCF_DIR) != 0) /* if direction bit set then report data underrun */
1477 scb->hastat = HOST_DO_DU;
1478 if ((initio_status_msg(host)) == -1)
1479 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 break;
1481
1482 case MSG_IN: /* Message in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001483 scb->next_state = 0x4;
1484 if (initio_msgin(host) == -1)
1485 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
1487
1488 case MSG_OUT: /* Message out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001489 if (host->jsstatus0 & TSS_PAR_ERROR) {
1490 scb->buflen = 0;
1491 scb->hastat = HOST_DO_DU;
1492 if (initio_msgout_ide(host) == -1)
1493 return -1;
1494 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001496 outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
1497 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1498 if (wait_tulip(host) == -1)
1499 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 break;
1502
1503 case DATA_IN: /* Data in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001504 return initio_xfer_data_in(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 case DATA_OUT: /* Data out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001507 return initio_xfer_data_out(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001510 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 }
1513}
1514
1515
Alan Cox72d39fe2007-05-21 15:06:43 +01001516/**
1517 * initio_state_5 - SCSI state machine
1518 * @host: InitIO host we are controlling
1519 *
1520 * State after dma xfer done or phase change before xfer done
1521 */
1522
1523static int initio_state_5(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Alan Cox72d39fe2007-05-21 15:06:43 +01001525 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 long cnt, xcnt; /* cannot use unsigned !! code: if (xcnt < 0) */
1527
1528#if DEBUG_STATE
1529 printk("-s5-");
1530#endif
Alan Cox72d39fe2007-05-21 15:06:43 +01001531 /*------ get remaining count -------*/
1532 cnt = inl(host->addr + TUL_SCnt0) & 0x0FFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Alan Cox72d39fe2007-05-21 15:06:43 +01001534 if (inb(host->addr + TUL_XCmd) & 0x20) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /* ----------------------- DATA_IN ----------------------------- */
1536 /* check scsi parity error */
Alan Cox72d39fe2007-05-21 15:06:43 +01001537 if (host->jsstatus0 & TSS_PAR_ERROR)
1538 scb->hastat = HOST_DO_DU;
1539 if (inb(host->addr + TUL_XStatus) & XPEND) { /* DMA xfer pending, Send STOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /* tell Hardware scsi xfer has been terminated */
Alan Cox72d39fe2007-05-21 15:06:43 +01001541 outb(inb(host->addr + TUL_XCtrl) | 0x80, host->addr + TUL_XCtrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /* wait until DMA xfer not pending */
Alan Cox72d39fe2007-05-21 15:06:43 +01001543 while (inb(host->addr + TUL_XStatus) & XPEND)
1544 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001547 /*-------- DATA OUT -----------*/
1548 if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) {
1549 if (host->active_tc->js_period & TSC_WIDE_SCSI)
1550 cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001552 cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001554 if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */
1555 outb(TAX_X_ABT, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /* wait Abort DMA xfer done */
Alan Cox72d39fe2007-05-21 15:06:43 +01001557 while ((inb(host->addr + TUL_Int) & XABT) == 0)
1558 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001560 if ((cnt == 1) && (host->phase == DATA_OUT)) {
1561 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1562 if (wait_tulip(host) == -1)
1563 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 cnt = 0;
1565 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001566 if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0)
1567 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (cnt == 0) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001571 scb->buflen = 0;
1572 return 6; /* After Data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 /* Update active data pointer */
Alan Cox72d39fe2007-05-21 15:06:43 +01001575 xcnt = (long) scb->buflen - cnt; /* xcnt== bytes already xferred */
1576 scb->buflen = (u32) cnt; /* cnt == bytes left to be xferred */
1577 if (scb->flags & SCF_SG) {
1578 struct sg_entry *sgp;
1579 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Alan Cox72d39fe2007-05-21 15:06:43 +01001581 sgp = &scb->sglist[scb->sgidx];
1582 for (i = scb->sgidx; i < scb->sgmax; sgp++, i++) {
1583 xcnt -= (long) sgp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (xcnt < 0) { /* this sgp xfer half done */
Alan Cox72d39fe2007-05-21 15:06:43 +01001585 xcnt += (long) sgp->len; /* xcnt == bytes xferred in this sgp */
1586 sgp->data += (u32) xcnt; /* new ptr to be xfer */
1587 sgp->len -= (u32) xcnt; /* new len to be xfer */
1588 scb->bufptr += ((u32) (i - scb->sgidx) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* new SG table ptr */
Alan Cox72d39fe2007-05-21 15:06:43 +01001590 scb->sglen = (u8) (scb->sgmax - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /* new SG table len */
Alan Cox72d39fe2007-05-21 15:06:43 +01001592 scb->sgidx = (u16) i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /* for next disc and come in this loop */
Alan Cox72d39fe2007-05-21 15:06:43 +01001594 return 4; /* Go to state 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596 /* else (xcnt >= 0 , i.e. this sgp already xferred */
1597 } /* for */
Alan Cox72d39fe2007-05-21 15:06:43 +01001598 return 6; /* Go to state 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001600 scb->bufptr += (u32) xcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001602 return 4; /* Go to state 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Alan Cox72d39fe2007-05-21 15:06:43 +01001605/**
1606 * initio_state_6 - SCSI state machine
1607 * @host: InitIO host we are controlling
1608 *
1609 * State after Data phase
1610 */
1611
1612static int initio_state_6(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Alan Cox72d39fe2007-05-21 15:06:43 +01001614 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616#if DEBUG_STATE
1617 printk("-s6-");
1618#endif
1619 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001620 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 case STATUS_IN: /* Status phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001622 if ((initio_status_msg(host)) == -1)
1623 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 break;
1625
1626 case MSG_IN: /* Message in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001627 scb->next_state = 6;
1628 if ((initio_msgin(host)) == -1)
1629 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 break;
1631
1632 case MSG_OUT: /* Message out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001633 outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
1634 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1635 if (wait_tulip(host) == -1)
1636 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 break;
1638
1639 case DATA_IN: /* Data in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001640 return initio_xpad_in(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 case DATA_OUT: /* Data out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001643 return initio_xpad_out(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001646 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648 }
1649}
1650
Alan Cox72d39fe2007-05-21 15:06:43 +01001651/**
1652 * initio_state_7 - SCSI state machine
1653 * @host: InitIO host we are controlling
1654 *
1655 */
1656
1657int initio_state_7(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 int cnt, i;
1660
1661#if DEBUG_STATE
1662 printk("-s7-");
1663#endif
1664 /* flush SCSI FIFO */
Alan Cox72d39fe2007-05-21 15:06:43 +01001665 cnt = inb(host->addr + TUL_SFifoCnt) & 0x1F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (cnt) {
1667 for (i = 0; i < cnt; i++)
Alan Cox72d39fe2007-05-21 15:06:43 +01001668 inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001670 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 case DATA_IN: /* Data in phase */
1672 case DATA_OUT: /* Data out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001673 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001675 return 6; /* Go to state 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677}
1678
Alan Cox72d39fe2007-05-21 15:06:43 +01001679/**
1680 * initio_xfer_data_in - Commence data input
1681 * @host: InitIO host in use
1682 *
1683 * Commence a block of data transfer. The transfer itself will
1684 * be managed by the controller and we will get a completion (or
1685 * failure) interrupt.
1686 */
1687static int initio_xfer_data_in(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Alan Cox72d39fe2007-05-21 15:06:43 +01001689 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Alan Cox72d39fe2007-05-21 15:06:43 +01001691 if ((scb->flags & SCF_DIR) == SCF_DOUT)
1692 return 6; /* wrong direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Alan Cox72d39fe2007-05-21 15:06:43 +01001694 outl(scb->buflen, host->addr + TUL_SCnt0);
1695 outb(TSC_XF_DMA_IN, host->addr + TUL_SCmd); /* 7/25/95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Alan Cox72d39fe2007-05-21 15:06:43 +01001697 if (scb->flags & SCF_SG) { /* S/G xfer */
1698 outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1699 outl(scb->bufptr, host->addr + TUL_XAddH);
1700 outb(TAX_SG_IN, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001702 outl(scb->buflen, host->addr + TUL_XCntH);
1703 outl(scb->bufptr, host->addr + TUL_XAddH);
1704 outb(TAX_X_IN, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001706 scb->next_state = 0x5;
1707 return 0; /* return to OS, wait xfer done , let jas_isr come in */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Alan Cox72d39fe2007-05-21 15:06:43 +01001710/**
1711 * initio_xfer_data_out - Commence data output
1712 * @host: InitIO host in use
1713 *
1714 * Commence a block of data transfer. The transfer itself will
1715 * be managed by the controller and we will get a completion (or
1716 * failure) interrupt.
1717 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Alan Cox72d39fe2007-05-21 15:06:43 +01001719static int initio_xfer_data_out(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Alan Cox72d39fe2007-05-21 15:06:43 +01001721 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Alan Cox72d39fe2007-05-21 15:06:43 +01001723 if ((scb->flags & SCF_DIR) == SCF_DIN)
1724 return 6; /* wrong direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Alan Cox72d39fe2007-05-21 15:06:43 +01001726 outl(scb->buflen, host->addr + TUL_SCnt0);
1727 outb(TSC_XF_DMA_OUT, host->addr + TUL_SCmd);
1728
1729 if (scb->flags & SCF_SG) { /* S/G xfer */
1730 outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1731 outl(scb->bufptr, host->addr + TUL_XAddH);
1732 outb(TAX_SG_OUT, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001734 outl(scb->buflen, host->addr + TUL_XCntH);
1735 outl(scb->bufptr, host->addr + TUL_XAddH);
1736 outb(TAX_X_OUT, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738
Alan Cox72d39fe2007-05-21 15:06:43 +01001739 scb->next_state = 0x5;
1740 return 0; /* return to OS, wait xfer done , let jas_isr come in */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Alan Cox72d39fe2007-05-21 15:06:43 +01001743int initio_xpad_in(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Alan Cox72d39fe2007-05-21 15:06:43 +01001745 struct scsi_ctrl_blk *scb = host->active;
1746 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Alan Cox72d39fe2007-05-21 15:06:43 +01001748 if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1749 scb->hastat = HOST_DO_DU; /* over run */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001751 if (active_tc->js_period & TSC_WIDE_SCSI)
1752 outl(2, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001754 outl(1, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Alan Cox72d39fe2007-05-21 15:06:43 +01001756 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1757 if (wait_tulip(host) == -1)
1758 return -1;
1759 if (host->phase != DATA_IN) {
1760 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1761 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001763 inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765}
1766
Alan Cox72d39fe2007-05-21 15:06:43 +01001767int initio_xpad_out(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Alan Cox72d39fe2007-05-21 15:06:43 +01001769 struct scsi_ctrl_blk *scb = host->active;
1770 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Alan Cox72d39fe2007-05-21 15:06:43 +01001772 if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1773 scb->hastat = HOST_DO_DU; /* over run */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001775 if (active_tc->js_period & TSC_WIDE_SCSI)
1776 outl(2, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001778 outl(1, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Alan Cox72d39fe2007-05-21 15:06:43 +01001780 outb(0, host->addr + TUL_SFifo);
1781 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1782 if ((wait_tulip(host)) == -1)
1783 return -1;
1784 if (host->phase != DATA_OUT) { /* Disable wide CPU to allow read 16 bits */
1785 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1786 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1787 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789 }
1790}
1791
Alan Cox72d39fe2007-05-21 15:06:43 +01001792int initio_status_msg(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{ /* status & MSG_IN */
Alan Cox72d39fe2007-05-21 15:06:43 +01001794 struct scsi_ctrl_blk *scb = host->active;
1795 u8 msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Alan Cox72d39fe2007-05-21 15:06:43 +01001797 outb(TSC_CMD_COMP, host->addr + TUL_SCmd);
1798 if (wait_tulip(host) == -1)
1799 return -1;
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /* get status */
Alan Cox72d39fe2007-05-21 15:06:43 +01001802 scb->tastat = inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Alan Cox72d39fe2007-05-21 15:06:43 +01001804 if (host->phase == MSG_OUT) {
1805 if (host->jsstatus0 & TSS_PAR_ERROR)
1806 outb(MSG_PARITY, host->addr + TUL_SFifo);
1807 else
1808 outb(MSG_NOP, host->addr + TUL_SFifo);
1809 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1810 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001812 if (host->phase == MSG_IN) {
1813 msg = inb(host->addr + TUL_SFifo);
1814 if (host->jsstatus0 & TSS_PAR_ERROR) { /* Parity error */
1815 if ((initio_msgin_accept(host)) == -1)
1816 return -1;
1817 if (host->phase != MSG_OUT)
1818 return initio_bad_seq(host);
1819 outb(MSG_PARITY, host->addr + TUL_SFifo);
1820 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1821 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 if (msg == 0) { /* Command complete */
1824
Alan Cox72d39fe2007-05-21 15:06:43 +01001825 if ((scb->tastat & 0x18) == 0x10) /* No link support */
1826 return initio_bad_seq(host);
1827 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1828 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
1829 return initio_wait_done_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001832 if (msg == MSG_LINK_COMP || msg == MSG_LINK_FLAG) {
1833 if ((scb->tastat & 0x18) == 0x10)
1834 return initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001837 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841/* scsi bus free */
Alan Cox72d39fe2007-05-21 15:06:43 +01001842int int_initio_busfree(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Alan Cox72d39fe2007-05-21 15:06:43 +01001844 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Alan Cox72d39fe2007-05-21 15:06:43 +01001846 if (scb != NULL) {
1847 if (scb->status & SCB_SELECT) { /* selection timeout */
1848 initio_unlink_pend_scb(host, scb);
1849 scb->hastat = HOST_SEL_TOUT;
1850 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 } else { /* Unexpected bus free */
Alan Cox72d39fe2007-05-21 15:06:43 +01001852 initio_unlink_busy_scb(host, scb);
1853 scb->hastat = HOST_BUS_FREE;
1854 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001856 host->active = NULL;
1857 host->active_tc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001859 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
1860 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
1861 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
1862 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865
Alan Cox72d39fe2007-05-21 15:06:43 +01001866/**
1867 * int_initio_scsi_rst - SCSI reset occurred
1868 * @host: Host seeing the reset
1869 *
1870 * A SCSI bus reset has occurred. Clean up any pending transfer
1871 * the hardware is doing by DMA and then abort all active and
1872 * disconnected commands. The mid layer should sort the rest out
1873 * for us
1874 */
1875
1876static int int_initio_scsi_rst(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Alan Cox72d39fe2007-05-21 15:06:43 +01001878 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 int i;
1880
1881 /* if DMA xfer is pending, abort DMA xfer */
Alan Cox72d39fe2007-05-21 15:06:43 +01001882 if (inb(host->addr + TUL_XStatus) & 0x01) {
1883 outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /* wait Abort DMA xfer done */
Alan Cox72d39fe2007-05-21 15:06:43 +01001885 while ((inb(host->addr + TUL_Int) & 0x04) == 0)
1886 cpu_relax();
1887 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889 /* Abort all active & disconnected scb */
Alan Cox72d39fe2007-05-21 15:06:43 +01001890 while ((scb = initio_pop_busy_scb(host)) != NULL) {
1891 scb->hastat = HOST_BAD_PHAS;
1892 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001894 host->active = NULL;
1895 host->active_tc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 /* clr sync nego. done flag */
Alan Cox72d39fe2007-05-21 15:06:43 +01001898 for (i = 0; i < host->max_tar; i++)
1899 host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1900 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
1902
Alan Cox72d39fe2007-05-21 15:06:43 +01001903/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001904 * int_initio_scsi_resel - Reselection occurred
Alan Cox72d39fe2007-05-21 15:06:43 +01001905 * @host: InitIO host adapter
1906 *
1907 * A SCSI reselection event has been signalled and the interrupt
1908 * is now being processed. Work out which command block needs attention
1909 * and continue processing that command.
1910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Alan Cox72d39fe2007-05-21 15:06:43 +01001912int int_initio_resel(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Alan Cox72d39fe2007-05-21 15:06:43 +01001914 struct scsi_ctrl_blk *scb;
1915 struct target_control *active_tc;
1916 u8 tag, msg = 0;
1917 u8 tar, lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Alan Cox72d39fe2007-05-21 15:06:43 +01001919 if ((scb = host->active) != NULL) {
1920 /* FIXME: Why check and not just clear ? */
1921 if (scb->status & SCB_SELECT) /* if waiting for selection complete */
1922 scb->status &= ~SCB_SELECT;
1923 host->active = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925 /* --------- get target id---------------------- */
Alan Cox72d39fe2007-05-21 15:06:43 +01001926 tar = inb(host->addr + TUL_SBusId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 /* ------ get LUN from Identify message----------- */
Alan Cox72d39fe2007-05-21 15:06:43 +01001928 lun = inb(host->addr + TUL_SIdent) & 0x0F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 /* 07/22/98 from 0x1F -> 0x0F */
Alan Cox72d39fe2007-05-21 15:06:43 +01001930 active_tc = &host->targets[tar];
1931 host->active_tc = active_tc;
1932 outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1933 outb(active_tc->js_period, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* ------------- tag queueing ? ------------------- */
Alan Cox72d39fe2007-05-21 15:06:43 +01001936 if (active_tc->drv_flags & TCF_DRV_EN_TAG) {
1937 if ((initio_msgin_accept(host)) == -1)
1938 return -1;
1939 if (host->phase != MSG_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto no_tag;
Alan Cox72d39fe2007-05-21 15:06:43 +01001941 outl(1, host->addr + TUL_SCnt0);
1942 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1943 if (wait_tulip(host) == -1)
1944 return -1;
1945 msg = inb(host->addr + TUL_SFifo); /* Read Tag Message */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Alan Cox72d39fe2007-05-21 15:06:43 +01001947 if (msg < MSG_STAG || msg > MSG_OTAG) /* Is simple Tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto no_tag;
1949
Alan Cox72d39fe2007-05-21 15:06:43 +01001950 if (initio_msgin_accept(host) == -1)
1951 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Alan Cox72d39fe2007-05-21 15:06:43 +01001953 if (host->phase != MSG_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto no_tag;
1955
Alan Cox72d39fe2007-05-21 15:06:43 +01001956 outl(1, host->addr + TUL_SCnt0);
1957 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1958 if (wait_tulip(host) == -1)
1959 return -1;
1960 tag = inb(host->addr + TUL_SFifo); /* Read Tag ID */
1961 scb = host->scb + tag;
1962 if (scb->target != tar || scb->lun != lun) {
1963 return initio_msgout_abort_tag(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001965 if (scb->status != SCB_BUSY) { /* 03/24/95 */
1966 return initio_msgout_abort_tag(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001968 host->active = scb;
1969 if ((initio_msgin_accept(host)) == -1)
1970 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 } else { /* No tag */
1972 no_tag:
Alan Cox72d39fe2007-05-21 15:06:43 +01001973 if ((scb = initio_find_busy_scb(host, tar | (lun << 8))) == NULL) {
1974 return initio_msgout_abort_targ(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001976 host->active = scb;
1977 if (!(active_tc->drv_flags & TCF_DRV_EN_TAG)) {
1978 if ((initio_msgin_accept(host)) == -1)
1979 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981 }
1982 return 0;
1983}
1984
Alan Cox72d39fe2007-05-21 15:06:43 +01001985/**
1986 * int_initio_bad_seq - out of phase
1987 * @host: InitIO host flagging event
1988 *
1989 * We have ended up out of phase somehow. Reset the host controller
1990 * and throw all our toys out of the pram. Let the midlayer clean up
1991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Alan Cox72d39fe2007-05-21 15:06:43 +01001993static int int_initio_bad_seq(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{ /* target wrong phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001995 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 int i;
1997
Alan Cox72d39fe2007-05-21 15:06:43 +01001998 initio_reset_scsi(host, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Alan Cox72d39fe2007-05-21 15:06:43 +01002000 while ((scb = initio_pop_busy_scb(host)) != NULL) {
2001 scb->hastat = HOST_BAD_PHAS;
2002 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002004 for (i = 0; i < host->max_tar; i++)
2005 host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
2006 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
2009
Alan Cox72d39fe2007-05-21 15:06:43 +01002010/**
2011 * initio_msgout_abort_targ - abort a tag
2012 * @host: InitIO host
2013 *
2014 * Abort when the target/lun does not match or when our SCB is not
2015 * busy. Used by untagged commands.
2016 */
2017
2018static int initio_msgout_abort_targ(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020
Alan Cox72d39fe2007-05-21 15:06:43 +01002021 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2022 if (initio_msgin_accept(host) == -1)
2023 return -1;
2024 if (host->phase != MSG_OUT)
2025 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Alan Cox72d39fe2007-05-21 15:06:43 +01002027 outb(MSG_ABORT, host->addr + TUL_SFifo);
2028 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Alan Cox72d39fe2007-05-21 15:06:43 +01002030 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Alan Cox72d39fe2007-05-21 15:06:43 +01002033/**
2034 * initio_msgout_abort_tag - abort a tag
2035 * @host: InitIO host
2036 *
2037 * Abort when the target/lun does not match or when our SCB is not
2038 * busy. Used for tagged commands.
2039 */
2040
2041static int initio_msgout_abort_tag(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043
Alan Cox72d39fe2007-05-21 15:06:43 +01002044 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2045 if (initio_msgin_accept(host) == -1)
2046 return -1;
2047 if (host->phase != MSG_OUT)
2048 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Alan Cox72d39fe2007-05-21 15:06:43 +01002050 outb(MSG_ABORT_TAG, host->addr + TUL_SFifo);
2051 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Alan Cox72d39fe2007-05-21 15:06:43 +01002053 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055}
2056
Alan Cox72d39fe2007-05-21 15:06:43 +01002057/**
2058 * initio_msgin - Message in
2059 * @host: InitIO Host
2060 *
2061 * Process incoming message
2062 */
2063static int initio_msgin(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Alan Cox72d39fe2007-05-21 15:06:43 +01002065 struct target_control *active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002068 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Alan Cox72d39fe2007-05-21 15:06:43 +01002070 outl(1, host->addr + TUL_SCnt0);
2071 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2072 if (wait_tulip(host) == -1)
2073 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Alan Cox72d39fe2007-05-21 15:06:43 +01002075 switch (inb(host->addr + TUL_SFifo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 case MSG_DISC: /* Disconnect msg */
Alan Cox72d39fe2007-05-21 15:06:43 +01002077 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2078 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 case MSG_SDP:
2080 case MSG_RESTORE:
2081 case MSG_NOP:
Alan Cox72d39fe2007-05-21 15:06:43 +01002082 initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 case MSG_REJ: /* Clear ATN first */
Alan Cox72d39fe2007-05-21 15:06:43 +01002085 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)),
2086 host->addr + TUL_SSignal);
2087 active_tc = host->active_tc;
2088 if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) /* do sync nego */
2089 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN),
2090 host->addr + TUL_SSignal);
2091 initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 case MSG_EXTEND: /* extended msg */
Alan Cox72d39fe2007-05-21 15:06:43 +01002094 initio_msgin_extend(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 case MSG_IGNOREWIDE:
Alan Cox72d39fe2007-05-21 15:06:43 +01002097 initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 case MSG_COMP:
Alan Cox72d39fe2007-05-21 15:06:43 +01002100 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2101 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2102 return initio_wait_done_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01002104 initio_msgout_reject(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 break;
2106 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002107 if (host->phase != MSG_IN)
2108 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110 /* statement won't reach here */
2111}
2112
Alan Cox72d39fe2007-05-21 15:06:43 +01002113static int initio_msgout_reject(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Alan Cox72d39fe2007-05-21 15:06:43 +01002115 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Alan Cox72d39fe2007-05-21 15:06:43 +01002117 if (initio_msgin_accept(host) == -1)
2118 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Alan Cox72d39fe2007-05-21 15:06:43 +01002120 if (host->phase == MSG_OUT) {
2121 outb(MSG_REJ, host->addr + TUL_SFifo); /* Msg reject */
2122 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2123 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002125 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Alan Cox72d39fe2007-05-21 15:06:43 +01002128static int initio_msgout_ide(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Alan Cox72d39fe2007-05-21 15:06:43 +01002130 outb(MSG_IDE, host->addr + TUL_SFifo); /* Initiator Detected Error */
2131 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2132 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
Alan Cox72d39fe2007-05-21 15:06:43 +01002135static int initio_msgin_extend(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Alan Cox72d39fe2007-05-21 15:06:43 +01002137 u8 len, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Alan Cox72d39fe2007-05-21 15:06:43 +01002139 if (initio_msgin_accept(host) != MSG_IN)
2140 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 /* Get extended msg length */
Alan Cox72d39fe2007-05-21 15:06:43 +01002143 outl(1, host->addr + TUL_SCnt0);
2144 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2145 if (wait_tulip(host) == -1)
2146 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Alan Cox72d39fe2007-05-21 15:06:43 +01002148 len = inb(host->addr + TUL_SFifo);
2149 host->msg[0] = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 for (idx = 1; len != 0; len--) {
2151
Alan Cox72d39fe2007-05-21 15:06:43 +01002152 if ((initio_msgin_accept(host)) != MSG_IN)
2153 return host->phase;
2154 outl(1, host->addr + TUL_SCnt0);
2155 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2156 if (wait_tulip(host) == -1)
2157 return -1;
2158 host->msg[idx++] = inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002160 if (host->msg[1] == 1) { /* if it's synchronous data transfer request */
2161 u8 r;
2162 if (host->msg[0] != 3) /* if length is not right */
2163 return initio_msgout_reject(host);
2164 if (host->active_tc->flags & TCF_NO_SYNC_NEGO) { /* Set OFFSET=0 to do async, nego back */
2165 host->msg[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002167 if (initio_msgin_sync(host) == 0 &&
2168 (host->active_tc->flags & TCF_SYNC_DONE)) {
2169 initio_sync_done(host);
2170 return initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172 }
2173
Alan Cox72d39fe2007-05-21 15:06:43 +01002174 r = inb(host->addr + TUL_SSignal);
2175 outb((r & (TSC_SET_ACK | 7)) | TSC_SET_ATN,
2176 host->addr + TUL_SSignal);
2177 if (initio_msgin_accept(host) != MSG_OUT)
2178 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /* sync msg out */
Alan Cox72d39fe2007-05-21 15:06:43 +01002180 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Alan Cox72d39fe2007-05-21 15:06:43 +01002182 initio_sync_done(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Alan Cox72d39fe2007-05-21 15:06:43 +01002184 outb(MSG_EXTEND, host->addr + TUL_SFifo);
2185 outb(3, host->addr + TUL_SFifo);
2186 outb(1, host->addr + TUL_SFifo);
2187 outb(host->msg[2], host->addr + TUL_SFifo);
2188 outb(host->msg[3], host->addr + TUL_SFifo);
2189 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2190 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002192 if (host->msg[0] != 2 || host->msg[1] != 3)
2193 return initio_msgout_reject(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /* if it's WIDE DATA XFER REQ */
Alan Cox72d39fe2007-05-21 15:06:43 +01002195 if (host->active_tc->flags & TCF_NO_WDTR) {
2196 host->msg[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002198 if (host->msg[2] > 2) /* > 32 bits */
2199 return initio_msgout_reject(host);
2200 if (host->msg[2] == 2) { /* == 32 */
2201 host->msg[2] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002203 if ((host->active_tc->flags & TCF_NO_WDTR) == 0) {
2204 wdtr_done(host);
2205 if ((host->active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)
2206 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2207 return initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209 }
2210 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002211 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Alan Cox72d39fe2007-05-21 15:06:43 +01002213 if (initio_msgin_accept(host) != MSG_OUT)
2214 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /* WDTR msg out */
Alan Cox72d39fe2007-05-21 15:06:43 +01002216 outb(MSG_EXTEND, host->addr + TUL_SFifo);
2217 outb(2, host->addr + TUL_SFifo);
2218 outb(3, host->addr + TUL_SFifo);
2219 outb(host->msg[2], host->addr + TUL_SFifo);
2220 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2221 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
Alan Cox72d39fe2007-05-21 15:06:43 +01002224static int initio_msgin_sync(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 char default_period;
2227
Alan Cox72d39fe2007-05-21 15:06:43 +01002228 default_period = initio_rate_tbl[host->active_tc->flags & TCF_SCSI_RATE];
2229 if (host->msg[3] > MAX_OFFSET) {
2230 host->msg[3] = MAX_OFFSET;
2231 if (host->msg[2] < default_period) {
2232 host->msg[2] = default_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return 1;
2234 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002235 if (host->msg[2] >= 59) /* Change to async */
2236 host->msg[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 1;
2238 }
2239 /* offset requests asynchronous transfers ? */
Alan Cox72d39fe2007-05-21 15:06:43 +01002240 if (host->msg[3] == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return 0;
2242 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002243 if (host->msg[2] < default_period) {
2244 host->msg[2] = default_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return 1;
2246 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002247 if (host->msg[2] >= 59) {
2248 host->msg[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return 1;
2250 }
2251 return 0;
2252}
2253
Alan Cox72d39fe2007-05-21 15:06:43 +01002254static int wdtr_done(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Alan Cox72d39fe2007-05-21 15:06:43 +01002256 host->active_tc->flags &= ~TCF_SYNC_DONE;
2257 host->active_tc->flags |= TCF_WDTR_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Alan Cox72d39fe2007-05-21 15:06:43 +01002259 host->active_tc->js_period = 0;
2260 if (host->msg[2]) /* if 16 bit */
2261 host->active_tc->js_period |= TSC_WIDE_SCSI;
2262 host->active_tc->sconfig0 &= ~TSC_ALT_PERIOD;
2263 outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2264 outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 return 1;
2267}
2268
Alan Cox72d39fe2007-05-21 15:06:43 +01002269static int initio_sync_done(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 int i;
2272
Alan Cox72d39fe2007-05-21 15:06:43 +01002273 host->active_tc->flags |= TCF_SYNC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Alan Cox72d39fe2007-05-21 15:06:43 +01002275 if (host->msg[3]) {
2276 host->active_tc->js_period |= host->msg[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 for (i = 0; i < 8; i++) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002278 if (initio_rate_tbl[i] >= host->msg[2]) /* pick the big one */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 break;
2280 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002281 host->active_tc->js_period |= (i << 4);
2282 host->active_tc->sconfig0 |= TSC_ALT_PERIOD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002284 outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2285 outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Alan Cox72d39fe2007-05-21 15:06:43 +01002287 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
2290
Alan Cox72d39fe2007-05-21 15:06:43 +01002291static int initio_post_scsi_rst(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Alan Cox72d39fe2007-05-21 15:06:43 +01002293 struct scsi_ctrl_blk *scb;
2294 struct target_control *active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 int i;
2296
Alan Cox72d39fe2007-05-21 15:06:43 +01002297 host->active = NULL;
2298 host->active_tc = NULL;
2299 host->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Alan Cox72d39fe2007-05-21 15:06:43 +01002301 while ((scb = initio_pop_busy_scb(host)) != NULL) {
2302 scb->hastat = HOST_BAD_PHAS;
2303 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305 /* clear sync done flag */
Alan Cox72d39fe2007-05-21 15:06:43 +01002306 active_tc = &host->targets[0];
2307 for (i = 0; i < host->max_tar; active_tc++, i++) {
2308 active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /* Initialize the sync. xfer register values to an asyn xfer */
Alan Cox72d39fe2007-05-21 15:06:43 +01002310 active_tc->js_period = 0;
2311 active_tc->sconfig0 = host->sconf1;
2312 host->act_tags[0] = 0; /* 07/22/98 */
2313 host->targets[i].flags &= ~TCF_BUSY; /* 07/22/98 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 } /* for */
2315
Alan Cox72d39fe2007-05-21 15:06:43 +01002316 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
Alan Cox72d39fe2007-05-21 15:06:43 +01002319static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Alan Cox72d39fe2007-05-21 15:06:43 +01002321 scb->status |= SCB_SELECT;
2322 scb->next_state = 0x1;
2323 host->active = scb;
2324 host->active_tc = &host->targets[scb->target];
2325 outb(TSC_SELATNSTOP, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
2328
Alan Cox72d39fe2007-05-21 15:06:43 +01002329static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
2331 int i;
2332
Alan Cox72d39fe2007-05-21 15:06:43 +01002333 scb->status |= SCB_SELECT;
2334 scb->next_state = 0x2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Alan Cox72d39fe2007-05-21 15:06:43 +01002336 outb(scb->ident, host->addr + TUL_SFifo);
2337 for (i = 0; i < (int) scb->cdblen; i++)
2338 outb(scb->cdb[i], host->addr + TUL_SFifo);
2339 host->active_tc = &host->targets[scb->target];
2340 host->active = scb;
2341 outb(TSC_SEL_ATN, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Alan Cox72d39fe2007-05-21 15:06:43 +01002344static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 int i;
2347
Alan Cox72d39fe2007-05-21 15:06:43 +01002348 scb->status |= SCB_SELECT;
2349 scb->next_state = 0x2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Alan Cox72d39fe2007-05-21 15:06:43 +01002351 outb(scb->ident, host->addr + TUL_SFifo);
2352 outb(scb->tagmsg, host->addr + TUL_SFifo);
2353 outb(scb->tagid, host->addr + TUL_SFifo);
2354 for (i = 0; i < scb->cdblen; i++)
2355 outb(scb->cdb[i], host->addr + TUL_SFifo);
2356 host->active_tc = &host->targets[scb->target];
2357 host->active = scb;
2358 outb(TSC_SEL_ATN3, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
Alan Cox72d39fe2007-05-21 15:06:43 +01002361/**
2362 * initio_bus_device_reset - SCSI Bus Device Reset
2363 * @host: InitIO host to reset
2364 *
2365 * Perform a device reset and abort all pending SCBs for the
2366 * victim device
2367 */
2368int initio_bus_device_reset(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Alan Cox72d39fe2007-05-21 15:06:43 +01002370 struct scsi_ctrl_blk *scb = host->active;
2371 struct target_control *active_tc = host->active_tc;
2372 struct scsi_ctrl_blk *tmp, *prev;
2373 u8 tar;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Alan Cox72d39fe2007-05-21 15:06:43 +01002375 if (host->phase != MSG_OUT)
2376 return int_initio_bad_seq(host); /* Unexpected phase */
2377
2378 initio_unlink_pend_scb(host, scb);
2379 initio_release_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381
Alan Cox72d39fe2007-05-21 15:06:43 +01002382 tar = scb->target; /* target */
2383 active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE | TCF_BUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 /* clr sync. nego & WDTR flags 07/22/98 */
2385
2386 /* abort all SCB with same target */
Alan Cox72d39fe2007-05-21 15:06:43 +01002387 prev = tmp = host->first_busy; /* Check Busy queue */
2388 while (tmp != NULL) {
2389 if (tmp->target == tar) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 /* unlink it */
Alan Cox72d39fe2007-05-21 15:06:43 +01002391 if (tmp == host->first_busy) {
2392 if ((host->first_busy = tmp->next) == NULL)
2393 host->last_busy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002395 prev->next = tmp->next;
2396 if (tmp == host->last_busy)
2397 host->last_busy = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002399 tmp->hastat = HOST_ABORTED;
2400 initio_append_done_scb(host, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 /* Previous haven't change */
2403 else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002404 prev = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002406 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002408 outb(MSG_DEVRST, host->addr + TUL_SFifo);
2409 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2410 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412}
2413
Alan Cox72d39fe2007-05-21 15:06:43 +01002414static int initio_msgin_accept(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Alan Cox72d39fe2007-05-21 15:06:43 +01002416 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2417 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Alan Cox72d39fe2007-05-21 15:06:43 +01002420static int wait_tulip(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
2422
Alan Cox72d39fe2007-05-21 15:06:43 +01002423 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2424 & TSS_INT_PENDING))
2425 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Alan Cox72d39fe2007-05-21 15:06:43 +01002427 host->jsint = inb(host->addr + TUL_SInt);
2428 host->phase = host->jsstatus0 & TSS_PH_MASK;
2429 host->jsstatus1 = inb(host->addr + TUL_SStatus1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Alan Cox72d39fe2007-05-21 15:06:43 +01002431 if (host->jsint & TSS_RESEL_INT) /* if SCSI bus reset detected */
2432 return int_initio_resel(host);
2433 if (host->jsint & TSS_SEL_TIMEOUT) /* if selected/reselected timeout interrupt */
2434 return int_initio_busfree(host);
2435 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2436 return int_initio_scsi_rst(host);
2437
2438 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2439 if (host->flags & HCF_EXPECT_DONE_DISC) {
2440 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2441 initio_unlink_busy_scb(host, host->active);
2442 host->active->hastat = 0;
2443 initio_append_done_scb(host, host->active);
2444 host->active = NULL;
2445 host->active_tc = NULL;
2446 host->flags &= ~HCF_EXPECT_DONE_DISC;
2447 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2448 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2449 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002451 if (host->flags & HCF_EXPECT_DISC) {
2452 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2453 host->active = NULL;
2454 host->active_tc = NULL;
2455 host->flags &= ~HCF_EXPECT_DISC;
2456 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2457 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2458 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002460 return int_initio_busfree(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002462 /* The old code really does the below. Can probably be removed */
2463 if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV))
2464 return host->phase;
2465 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
Alan Cox72d39fe2007-05-21 15:06:43 +01002467
2468static int initio_wait_disc(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
Alan Cox72d39fe2007-05-21 15:06:43 +01002470 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING))
2471 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Alan Cox72d39fe2007-05-21 15:06:43 +01002473 host->jsint = inb(host->addr + TUL_SInt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Alan Cox72d39fe2007-05-21 15:06:43 +01002475 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2476 return int_initio_scsi_rst(host);
2477 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2478 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2479 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2480 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2481 host->active = NULL;
2482 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002484 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
Alan Cox72d39fe2007-05-21 15:06:43 +01002487static int initio_wait_done_disc(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Alan Cox72d39fe2007-05-21 15:06:43 +01002489 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2490 & TSS_INT_PENDING))
2491 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Alan Cox72d39fe2007-05-21 15:06:43 +01002493 host->jsint = inb(host->addr + TUL_SInt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Alan Cox72d39fe2007-05-21 15:06:43 +01002495 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2496 return int_initio_scsi_rst(host);
2497 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2498 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2499 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2500 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2501 initio_unlink_busy_scb(host, host->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Alan Cox72d39fe2007-05-21 15:06:43 +01002503 initio_append_done_scb(host, host->active);
2504 host->active = NULL;
2505 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002507 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Alan Cox72d39fe2007-05-21 15:06:43 +01002510/**
2511 * i91u_intr - IRQ handler
2512 * @irqno: IRQ number
2513 * @dev_id: IRQ identifier
2514 *
2515 * Take the relevant locks and then invoke the actual isr processing
2516 * code under the lock.
2517 */
2518
David Howells7d12e782006-10-05 14:55:46 +01002519static irqreturn_t i91u_intr(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
2521 struct Scsi_Host *dev = dev_id;
2522 unsigned long flags;
Alan Cox72d39fe2007-05-21 15:06:43 +01002523 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 spin_lock_irqsave(dev->host_lock, flags);
Alan Cox72d39fe2007-05-21 15:06:43 +01002526 r = initio_isr((struct initio_host *)dev->hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 spin_unlock_irqrestore(dev->host_lock, flags);
Alan Cox72d39fe2007-05-21 15:06:43 +01002528 if (r)
2529 return IRQ_HANDLED;
2530 else
2531 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Alan Cox72d39fe2007-05-21 15:06:43 +01002535/**
2536 * initio_build_scb - Build the mappings and SCB
2537 * @host: InitIO host taking the command
2538 * @cblk: Firmware command block
2539 * @cmnd: SCSI midlayer command block
2540 *
2541 * Translate the abstract SCSI command into a firmware command block
2542 * suitable for feeding to the InitIO host controller. This also requires
2543 * we build the scatter gather lists and ensure they are mapped properly.
2544 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Alan Cox72d39fe2007-05-21 15:06:43 +01002546static void initio_build_scb(struct initio_host * host, struct scsi_ctrl_blk * cblk, struct scsi_cmnd * cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{ /* Create corresponding SCB */
Alan Cox72d39fe2007-05-21 15:06:43 +01002548 struct scatterlist *sglist;
2549 struct sg_entry *sg; /* Pointer to SG list */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002550 int i, nseg;
Alan Cox72d39fe2007-05-21 15:06:43 +01002551 long total_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 dma_addr_t dma_addr;
2553
Alan Cox72d39fe2007-05-21 15:06:43 +01002554 /* Fill in the command headers */
2555 cblk->post = i91uSCBPost; /* i91u's callback routine */
2556 cblk->srb = cmnd;
2557 cblk->opcode = ExecSCSI;
2558 cblk->flags = SCF_POST; /* After SCSI done, call post routine */
2559 cblk->target = cmnd->device->id;
2560 cblk->lun = cmnd->device->lun;
2561 cblk->ident = cmnd->device->lun | DISC_ALLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Alan Cox72d39fe2007-05-21 15:06:43 +01002563 cblk->flags |= SCF_SENSE; /* Turn on auto request sense */
2564
2565 /* Map the sense buffer into bus memory */
2566 dma_addr = dma_map_single(&host->pci_dev->dev, cmnd->sense_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 SENSE_SIZE, DMA_FROM_DEVICE);
Grant Grundler423eef62008-04-05 10:14:22 -06002568 cblk->senseptr = (u32)dma_addr;
2569 cblk->senselen = SENSE_SIZE;
Alan Cox72d39fe2007-05-21 15:06:43 +01002570 cmnd->SCp.ptr = (char *)(unsigned long)dma_addr;
2571 cblk->cdblen = cmnd->cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Alan Cox72d39fe2007-05-21 15:06:43 +01002573 /* Clear the returned status */
2574 cblk->hastat = 0;
2575 cblk->tastat = 0;
2576 /* Command the command */
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002577 memcpy(cblk->cdb, cmnd->cmnd, cmnd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Alan Cox72d39fe2007-05-21 15:06:43 +01002579 /* Set up tags */
2580 if (cmnd->device->tagged_supported) { /* Tag Support */
2581 cblk->tagmsg = SIMPLE_QUEUE_TAG; /* Do simple tag only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002583 cblk->tagmsg = 0; /* No tag support */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /* todo handle map_sg error */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002587 nseg = scsi_dma_map(cmnd);
2588 BUG_ON(nseg < 0);
2589 if (nseg) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002590 dma_addr = dma_map_single(&host->pci_dev->dev, &cblk->sglist[0],
2591 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 DMA_BIDIRECTIONAL);
Grant Grundler423eef62008-04-05 10:14:22 -06002593 cblk->bufptr = (u32)dma_addr;
Alan Cox72d39fe2007-05-21 15:06:43 +01002594 cmnd->SCp.dma_handle = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Stuart Swalese2d435e2008-01-23 20:00:48 +00002596 cblk->sglen = nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Alan Cox72d39fe2007-05-21 15:06:43 +01002598 cblk->flags |= SCF_SG; /* Turn on SG list flag */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002599 total_len = 0;
2600 sg = &cblk->sglist[0];
2601 scsi_for_each_sg(cmnd, sglist, cblk->sglen, i) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002602 sg->data = cpu_to_le32((u32)sg_dma_address(sglist));
Grant Grundler423eef62008-04-05 10:14:22 -06002603 sg->len = cpu_to_le32((u32)sg_dma_len(sglist));
2604 total_len += sg_dma_len(sglist);
Boaz Harrosha169e632007-12-17 18:08:59 +02002605 ++sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002608 cblk->buflen = (scsi_bufflen(cmnd) > total_len) ?
2609 total_len : scsi_bufflen(cmnd);
Alan Cox72d39fe2007-05-21 15:06:43 +01002610 } else { /* No data transfer required */
2611 cblk->buflen = 0;
2612 cblk->sglen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614}
2615
Alan Cox72d39fe2007-05-21 15:06:43 +01002616/**
2617 * i91u_queuecommand - Queue a new command if possible
2618 * @cmd: SCSI command block from the mid layer
2619 * @done: Completion handler
2620 *
2621 * Attempts to queue a new command with the host adapter. Will return
2622 * zero if successful or indicate a host busy condition if not (which
2623 * will cause the mid layer to call us again later with the command)
2624 */
2625
Jeff Garzikf2812332010-11-16 02:10:29 -05002626static int i91u_queuecommand_lck(struct scsi_cmnd *cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 void (*done)(struct scsi_cmnd *))
2628{
Alan Cox72d39fe2007-05-21 15:06:43 +01002629 struct initio_host *host = (struct initio_host *) cmd->device->host->hostdata;
2630 struct scsi_ctrl_blk *cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 cmd->scsi_done = done;
2633
Alan Cox72d39fe2007-05-21 15:06:43 +01002634 cmnd = initio_alloc_scb(host);
2635 if (!cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return SCSI_MLQUEUE_HOST_BUSY;
2637
Alan Cox72d39fe2007-05-21 15:06:43 +01002638 initio_build_scb(host, cmnd, cmd);
2639 initio_exec_scb(host, cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return 0;
2641}
2642
Jeff Garzikf2812332010-11-16 02:10:29 -05002643static DEF_SCSI_QCMD(i91u_queuecommand)
2644
Alan Cox72d39fe2007-05-21 15:06:43 +01002645/**
2646 * i91u_bus_reset - reset the SCSI bus
2647 * @cmnd: Command block we want to trigger the reset for
2648 *
2649 * Initiate a SCSI bus reset sequence
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 */
Alan Cox72d39fe2007-05-21 15:06:43 +01002651
2652static int i91u_bus_reset(struct scsi_cmnd * cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
Alan Cox72d39fe2007-05-21 15:06:43 +01002654 struct initio_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Alan Cox72d39fe2007-05-21 15:06:43 +01002656 host = (struct initio_host *) cmnd->device->host->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Alan Cox72d39fe2007-05-21 15:06:43 +01002658 spin_lock_irq(cmnd->device->host->host_lock);
2659 initio_reset_scsi(host, 0);
2660 spin_unlock_irq(cmnd->device->host->host_lock);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return SUCCESS;
2663}
2664
Alan Cox72d39fe2007-05-21 15:06:43 +01002665/**
2666 * i91u_biospararm - return the "logical geometry
2667 * @sdev: SCSI device
2668 * @dev; Matching block device
2669 * @capacity: Sector size of drive
2670 * @info_array: Return space for BIOS geometry
2671 *
2672 * Map the device geometry in a manner compatible with the host
2673 * controller BIOS behaviour.
2674 *
2675 * FIXME: limited to 2^32 sector devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 */
Alan Cox72d39fe2007-05-21 15:06:43 +01002677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678static int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev,
2679 sector_t capacity, int *info_array)
2680{
Alan Cox72d39fe2007-05-21 15:06:43 +01002681 struct initio_host *host; /* Point to Host adapter control block */
2682 struct target_control *tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Alan Cox72d39fe2007-05-21 15:06:43 +01002684 host = (struct initio_host *) sdev->host->hostdata;
2685 tc = &host->targets[sdev->id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Alan Cox72d39fe2007-05-21 15:06:43 +01002687 if (tc->heads) {
2688 info_array[0] = tc->heads;
2689 info_array[1] = tc->sectors;
2690 info_array[2] = (unsigned long)capacity / tc->heads / tc->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002692 if (tc->drv_flags & TCF_DRV_255_63) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 info_array[0] = 255;
2694 info_array[1] = 63;
2695 info_array[2] = (unsigned long)capacity / 255 / 63;
2696 } else {
2697 info_array[0] = 64;
2698 info_array[1] = 32;
2699 info_array[2] = (unsigned long)capacity >> 11;
2700 }
2701 }
2702
2703#if defined(DEBUG_BIOSPARAM)
2704 if (i91u_debug & debug_biosparam) {
2705 printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
2706 info_array[0], info_array[1], info_array[2]);
2707 printk("WARNING: check, if the bios geometry is correct.\n");
2708 }
2709#endif
2710
2711 return 0;
2712}
2713
Alan Cox72d39fe2007-05-21 15:06:43 +01002714/**
2715 * i91u_unmap_scb - Unmap a command
2716 * @pci_dev: PCI device the command is for
2717 * @cmnd: The command itself
2718 *
2719 * Unmap any PCI mapping/IOMMU resources allocated when the command
2720 * was mapped originally as part of initio_build_scb
2721 */
2722
2723static void i91u_unmap_scb(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
2725 /* auto sense buffer */
2726 if (cmnd->SCp.ptr) {
2727 dma_unmap_single(&pci_dev->dev,
2728 (dma_addr_t)((unsigned long)cmnd->SCp.ptr),
2729 SENSE_SIZE, DMA_FROM_DEVICE);
2730 cmnd->SCp.ptr = NULL;
2731 }
2732
2733 /* request buffer */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002734 if (scsi_sg_count(cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 dma_unmap_single(&pci_dev->dev, cmnd->SCp.dma_handle,
Alan Cox72d39fe2007-05-21 15:06:43 +01002736 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 DMA_BIDIRECTIONAL);
2738
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002739 scsi_dma_unmap(cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
2741}
2742
Alan Cox72d39fe2007-05-21 15:06:43 +01002743/**
2744 * i91uSCBPost - SCSI callback
2745 * @host: Pointer to host adapter control block.
2746 * @cmnd: Pointer to SCSI control block.
2747 *
2748 * This is callback routine be called when tulip finish one
2749 * SCSI command.
2750 */
2751
2752static void i91uSCBPost(u8 * host_mem, u8 * cblk_mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Alan Cox72d39fe2007-05-21 15:06:43 +01002754 struct scsi_cmnd *cmnd; /* Pointer to SCSI request block */
2755 struct initio_host *host;
2756 struct scsi_ctrl_blk *cblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Alan Cox72d39fe2007-05-21 15:06:43 +01002758 host = (struct initio_host *) host_mem;
2759 cblk = (struct scsi_ctrl_blk *) cblk_mem;
2760 if ((cmnd = cblk->srb) == NULL) {
2761 printk(KERN_ERR "i91uSCBPost: SRB pointer is empty\n");
2762 WARN_ON(1);
2763 initio_release_scb(host, cblk); /* Release SCB for current channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return;
2765 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002766
2767 /*
2768 * Remap the firmware error status into a mid layer one
2769 */
2770 switch (cblk->hastat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 case 0x0:
2772 case 0xa: /* Linked command complete without error and linked normally */
2773 case 0xb: /* Linked command complete without error interrupt generated */
Alan Cox72d39fe2007-05-21 15:06:43 +01002774 cblk->hastat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 break;
2776
2777 case 0x11: /* Selection time out-The initiator selection or target
2778 reselection was not complete within the SCSI Time out period */
Alan Cox72d39fe2007-05-21 15:06:43 +01002779 cblk->hastat = DID_TIME_OUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 break;
2781
2782 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
2783 phase sequence was requested by the target. The host adapter
2784 will generate a SCSI Reset Condition, notifying the host with
2785 a SCRD interrupt */
Alan Cox72d39fe2007-05-21 15:06:43 +01002786 cblk->hastat = DID_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 break;
2788
2789 case 0x1a: /* SCB Aborted. 07/21/98 */
Alan Cox72d39fe2007-05-21 15:06:43 +01002790 cblk->hastat = DID_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 break;
2792
2793 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
2794 than was allocated by the Data Length field or the sum of the
2795 Scatter / Gather Data Length fields. */
2796 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
2797 case 0x16: /* Invalid SCB Operation Code. */
2798
2799 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01002800 printk("ini9100u: %x %x\n", cblk->hastat, cblk->tastat);
2801 cblk->hastat = DID_ERROR; /* Couldn't find any better */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 break;
2803 }
2804
Alan Cox72d39fe2007-05-21 15:06:43 +01002805 cmnd->result = cblk->tastat | (cblk->hastat << 16);
Alan Cox72d39fe2007-05-21 15:06:43 +01002806 i91u_unmap_scb(host->pci_dev, cmnd);
2807 cmnd->scsi_done(cmnd); /* Notify system DONE */
2808 initio_release_scb(host, cblk); /* Release SCB for current channel */
2809}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Alan Cox72d39fe2007-05-21 15:06:43 +01002811static struct scsi_host_template initio_template = {
2812 .proc_name = "INI9100U",
2813 .name = "Initio INI-9X00U/UW SCSI device driver",
2814 .queuecommand = i91u_queuecommand,
2815 .eh_bus_reset_handler = i91u_bus_reset,
2816 .bios_param = i91u_biosparam,
2817 .can_queue = MAX_TARGETS * i91u_MAXQUEUE,
2818 .this_id = 1,
2819 .sg_tablesize = SG_ALL,
Alan Cox72d39fe2007-05-21 15:06:43 +01002820 .use_clustering = ENABLE_CLUSTERING,
2821};
2822
2823static int initio_probe_one(struct pci_dev *pdev,
2824 const struct pci_device_id *id)
2825{
2826 struct Scsi_Host *shost;
2827 struct initio_host *host;
2828 u32 reg;
2829 u16 bios_seg;
2830 struct scsi_ctrl_blk *scb, *tmp, *prev = NULL /* silence gcc */;
2831 int num_scb, i, error;
2832
2833 error = pci_enable_device(pdev);
2834 if (error)
2835 return error;
2836
2837 pci_read_config_dword(pdev, 0x44, (u32 *) & reg);
2838 bios_seg = (u16) (reg & 0xFF);
2839 if (((reg & 0xFF00) >> 8) == 0xFF)
2840 reg = 0;
2841 bios_seg = (bios_seg << 8) + ((u16) ((reg & 0xFF00) >> 8));
2842
Yang Hongyang284901a2009-04-06 19:01:15 -07002843 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002844 printk(KERN_WARNING "i91u: Could not set 32 bit DMA mask\n");
2845 error = -ENODEV;
2846 goto out_disable_device;
2847 }
2848 shost = scsi_host_alloc(&initio_template, sizeof(struct initio_host));
2849 if (!shost) {
2850 printk(KERN_WARNING "initio: Could not allocate host structure.\n");
2851 error = -ENOMEM;
2852 goto out_disable_device;
2853 }
2854 host = (struct initio_host *)shost->hostdata;
2855 memset(host, 0, sizeof(struct initio_host));
Alan Cox99f1f532007-12-13 16:14:05 -08002856 host->addr = pci_resource_start(pdev, 0);
Stuart Swalese2d435e2008-01-23 20:00:48 +00002857 host->bios_addr = bios_seg;
Alan Cox72d39fe2007-05-21 15:06:43 +01002858
2859 if (!request_region(host->addr, 256, "i91u")) {
2860 printk(KERN_WARNING "initio: I/O port range 0x%x is busy.\n", host->addr);
2861 error = -ENODEV;
2862 goto out_host_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
2864
Alan Cox72d39fe2007-05-21 15:06:43 +01002865 if (initio_tag_enable) /* 1.01i */
2866 num_scb = MAX_TARGETS * i91u_MAXQUEUE;
2867 else
2868 num_scb = MAX_TARGETS + 3; /* 1-tape, 1-CD_ROM, 1- extra */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Alan Cox72d39fe2007-05-21 15:06:43 +01002870 for (; num_scb >= MAX_TARGETS + 3; num_scb--) {
2871 i = num_scb * sizeof(struct scsi_ctrl_blk);
2872 if ((scb = kzalloc(i, GFP_DMA)) != NULL)
2873 break;
2874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Alan Cox72d39fe2007-05-21 15:06:43 +01002876 if (!scb) {
2877 printk(KERN_WARNING "initio: Cannot allocate SCB array.\n");
2878 error = -ENOMEM;
2879 goto out_release_region;
2880 }
2881
Alan Coxe9e42fa2007-10-15 20:51:32 +01002882 host->pci_dev = pdev;
2883
Stuart Swalese2d435e2008-01-23 20:00:48 +00002884 host->semaph = 1;
2885 spin_lock_init(&host->semaph_lock);
Alan Cox72d39fe2007-05-21 15:06:43 +01002886 host->num_scbs = num_scb;
2887 host->scb = scb;
2888 host->next_pending = scb;
2889 host->next_avail = scb;
2890 for (i = 0, tmp = scb; i < num_scb; i++, tmp++) {
2891 tmp->tagid = i;
2892 if (i != 0)
2893 prev->next = tmp;
2894 prev = tmp;
2895 }
2896 prev->next = NULL;
2897 host->scb_end = tmp;
2898 host->first_avail = scb;
2899 host->last_avail = prev;
Alan Coxe9e42fa2007-10-15 20:51:32 +01002900 spin_lock_init(&host->avail_lock);
Alan Cox72d39fe2007-05-21 15:06:43 +01002901
Stuart Swalese2d435e2008-01-23 20:00:48 +00002902 initio_init(host, phys_to_virt(((u32)bios_seg << 4)));
Alan Cox72d39fe2007-05-21 15:06:43 +01002903
2904 host->jsstatus0 = 0;
2905
2906 shost->io_port = host->addr;
2907 shost->n_io_port = 0xff;
2908 shost->can_queue = num_scb; /* 03/05/98 */
2909 shost->unique_id = host->addr;
2910 shost->max_id = host->max_tar;
2911 shost->max_lun = 32; /* 10/21/97 */
2912 shost->irq = pdev->irq;
2913 shost->this_id = host->scsi_id; /* Assign HCS index */
2914 shost->base = host->addr;
2915 shost->sg_tablesize = TOTAL_SG_ENTRY;
2916
Michael Opdenacker4909cc22014-03-05 06:09:41 +01002917 error = request_irq(pdev->irq, i91u_intr, IRQF_SHARED, "i91u", shost);
Alan Cox72d39fe2007-05-21 15:06:43 +01002918 if (error < 0) {
2919 printk(KERN_WARNING "initio: Unable to request IRQ %d\n", pdev->irq);
2920 goto out_free_scbs;
2921 }
2922
2923 pci_set_drvdata(pdev, shost);
Alan Cox72d39fe2007-05-21 15:06:43 +01002924
2925 error = scsi_add_host(shost, &pdev->dev);
2926 if (error)
2927 goto out_free_irq;
2928 scsi_scan_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return 0;
Alan Cox72d39fe2007-05-21 15:06:43 +01002930out_free_irq:
2931 free_irq(pdev->irq, shost);
2932out_free_scbs:
2933 kfree(host->scb);
2934out_release_region:
2935 release_region(host->addr, 256);
2936out_host_put:
2937 scsi_host_put(shost);
2938out_disable_device:
2939 pci_disable_device(pdev);
2940 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Alan Cox72d39fe2007-05-21 15:06:43 +01002943/**
2944 * initio_remove_one - control shutdown
2945 * @pdev: PCI device being released
2946 *
2947 * Release the resources assigned to this adapter after it has
2948 * finished being used.
2949 */
2950
2951static void initio_remove_one(struct pci_dev *pdev)
2952{
2953 struct Scsi_Host *host = pci_get_drvdata(pdev);
2954 struct initio_host *s = (struct initio_host *)host->hostdata;
2955 scsi_remove_host(host);
2956 free_irq(pdev->irq, host);
2957 release_region(s->addr, 256);
2958 scsi_host_put(host);
2959 pci_disable_device(pdev);
2960}
2961
2962MODULE_LICENSE("GPL");
2963
2964static struct pci_device_id initio_pci_tbl[] = {
2965 {PCI_VENDOR_ID_INIT, 0x9500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2966 {PCI_VENDOR_ID_INIT, 0x9400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2967 {PCI_VENDOR_ID_INIT, 0x9401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2968 {PCI_VENDOR_ID_INIT, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2969 {PCI_VENDOR_ID_DOMEX, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2970 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971};
Alan Cox72d39fe2007-05-21 15:06:43 +01002972MODULE_DEVICE_TABLE(pci, initio_pci_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Alan Cox72d39fe2007-05-21 15:06:43 +01002974static struct pci_driver initio_pci_driver = {
2975 .name = "initio",
2976 .id_table = initio_pci_tbl,
2977 .probe = initio_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08002978 .remove = initio_remove_one,
Alan Cox72d39fe2007-05-21 15:06:43 +01002979};
2980
2981static int __init initio_init_driver(void)
2982{
2983 return pci_register_driver(&initio_pci_driver);
2984}
2985
2986static void __exit initio_exit_driver(void)
2987{
2988 pci_unregister_driver(&initio_pci_driver);
2989}
2990
2991MODULE_DESCRIPTION("Initio INI-9X00U/UW SCSI device driver");
2992MODULE_AUTHOR("Initio Corporation");
2993MODULE_LICENSE("GPL");
2994
2995module_init(initio_init_driver);
2996module_exit(initio_exit_driver);