blob: 41fd64c9c8e96cc852c65c08453a5ac63532412e [file] [log] [blame]
Thomas Gleixnerc82ee6d2019-05-19 15:51:48 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/**************************************************************************
3 * Initio 9100 device driver for Linux.
4 *
5 * Copyright (c) 1994-1998 Initio Corporation
6 * Copyright (c) 1998 Bas Vermeulen <bvermeul@blackstar.xs4all.nl>
Alan Cox72d39fe2007-05-21 15:06:43 +01007 * Copyright (c) 2004 Christoph Hellwig <hch@lst.de>
Alan Coxfa195af2008-10-27 15:16:36 +00008 * Copyright (c) 2007 Red Hat
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *************************************************************************
11 *
12 * DESCRIPTION:
13 *
14 * This is the Linux low-level SCSI driver for Initio INI-9X00U/UW SCSI host
15 * adapters
16 *
17 * 08/06/97 hc - v1.01h
18 * - Support inic-940 and inic-935
19 * 09/26/97 hc - v1.01i
20 * - Make correction from J.W. Schultz suggestion
21 * 10/13/97 hc - Support reset function
22 * 10/21/97 hc - v1.01j
23 * - Support 32 LUN (SCSI 3)
24 * 01/14/98 hc - v1.01k
25 * - Fix memory allocation problem
26 * 03/04/98 hc - v1.01l
27 * - Fix tape rewind which will hang the system problem
Alan Cox72d39fe2007-05-21 15:06:43 +010028 * - Set can_queue to initio_num_scb
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * 06/25/98 hc - v1.01m
30 * - Get it work for kernel version >= 2.1.75
Alan Cox72d39fe2007-05-21 15:06:43 +010031 * - Dynamic assign SCSI bus reset holding time in initio_init()
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * 07/02/98 hc - v1.01n
33 * - Support 0002134A
34 * 08/07/98 hc - v1.01o
Alan Cox72d39fe2007-05-21 15:06:43 +010035 * - Change the initio_abort_srb routine to use scsi_done. <01>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * 09/07/98 hl - v1.02
37 * - Change the INI9100U define and proc_dir_entry to
38 * reflect the newer Kernel 2.1.118, but the v1.o1o
39 * should work with Kernel 2.1.118.
40 * 09/20/98 wh - v1.02a
41 * - Support Abort command.
42 * - Handle reset routine.
43 * 09/21/98 hl - v1.03
44 * - remove comments.
45 * 12/09/98 bv - v1.03a
46 * - Removed unused code
47 * 12/13/98 bv - v1.03b
48 * - Remove cli() locking for kernels >= 2.1.95. This uses
49 * spinlocks to serialize access to the pSRB_head and
50 * pSRB_tail members of the HCS structure.
51 * 09/01/99 bv - v1.03d
52 * - Fixed a deadlock problem in SMP.
53 * 21/01/99 bv - v1.03e
54 * - Add support for the Domex 3192U PCI SCSI
55 * This is a slightly modified patch by
56 * Brian Macy <bmacy@sunshinecomputing.com>
57 * 22/02/99 bv - v1.03f
58 * - Didn't detect the INIC-950 in 2.0.x correctly.
59 * Now fixed.
60 * 05/07/99 bv - v1.03g
61 * - Changed the assumption that HZ = 100
62 * 10/17/03 mc - v1.04
63 * - added new DMA API support
64 * 06/01/04 jmd - v1.04a
65 * - Re-add reset_bus support
66 **************************************************************************/
67
68#include <linux/module.h>
69#include <linux/errno.h>
70#include <linux/delay.h>
71#include <linux/pci.h>
72#include <linux/init.h>
73#include <linux/blkdev.h>
74#include <linux/spinlock.h>
75#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/kernel.h>
77#include <linux/proc_fs.h>
78#include <linux/string.h>
79#include <linux/interrupt.h>
80#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/slab.h>
82#include <linux/jiffies.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080083#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <asm/io.h>
85
86#include <scsi/scsi.h>
87#include <scsi/scsi_cmnd.h>
88#include <scsi/scsi_device.h>
89#include <scsi/scsi_host.h>
90#include <scsi/scsi_tcq.h>
91
92#include "initio.h"
93
94#define SENSE_SIZE 14
95
96#define i91u_MAXQUEUE 2
97#define i91u_REVID "Initio INI-9X00U/UW SCSI device driver; Revision: 1.04a"
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#ifdef DEBUG_i91u
100static unsigned int i91u_debug = DEBUG_DEFAULT;
101#endif
102
Alan Cox72d39fe2007-05-21 15:06:43 +0100103static int initio_tag_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105#ifdef DEBUG_i91u
106static int setup_debug = 0;
107#endif
108
Alan Cox72d39fe2007-05-21 15:06:43 +0100109static void i91uSCBPost(u8 * pHcb, u8 * pScb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define DEBUG_INTERRUPT 0
112#define DEBUG_QUEUE 0
113#define DEBUG_STATE 0
114#define INT_DISC 0
115
Alan Cox72d39fe2007-05-21 15:06:43 +0100116/*--- forward references ---*/
117static struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun);
118static struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Alan Cox72d39fe2007-05-21 15:06:43 +0100120static int tulip_main(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Alan Cox72d39fe2007-05-21 15:06:43 +0100122static int initio_next_state(struct initio_host * host);
123static int initio_state_1(struct initio_host * host);
124static int initio_state_2(struct initio_host * host);
125static int initio_state_3(struct initio_host * host);
126static int initio_state_4(struct initio_host * host);
127static int initio_state_5(struct initio_host * host);
128static int initio_state_6(struct initio_host * host);
129static int initio_state_7(struct initio_host * host);
130static int initio_xfer_data_in(struct initio_host * host);
131static int initio_xfer_data_out(struct initio_host * host);
132static int initio_xpad_in(struct initio_host * host);
133static int initio_xpad_out(struct initio_host * host);
134static int initio_status_msg(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Alan Cox72d39fe2007-05-21 15:06:43 +0100136static int initio_msgin(struct initio_host * host);
137static int initio_msgin_sync(struct initio_host * host);
138static int initio_msgin_accept(struct initio_host * host);
139static int initio_msgout_reject(struct initio_host * host);
140static int initio_msgin_extend(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Alan Cox72d39fe2007-05-21 15:06:43 +0100142static int initio_msgout_ide(struct initio_host * host);
143static int initio_msgout_abort_targ(struct initio_host * host);
144static int initio_msgout_abort_tag(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Alan Cox72d39fe2007-05-21 15:06:43 +0100146static int initio_bus_device_reset(struct initio_host * host);
147static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb);
148static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb);
149static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb);
150static int int_initio_busfree(struct initio_host * host);
151static int int_initio_scsi_rst(struct initio_host * host);
152static int int_initio_bad_seq(struct initio_host * host);
153static int int_initio_resel(struct initio_host * host);
154static int initio_sync_done(struct initio_host * host);
155static int wdtr_done(struct initio_host * host);
156static int wait_tulip(struct initio_host * host);
157static int initio_wait_done_disc(struct initio_host * host);
158static int initio_wait_disc(struct initio_host * host);
159static void tulip_scsi(struct initio_host * host);
160static int initio_post_scsi_rst(struct initio_host * host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Alan Cox72d39fe2007-05-21 15:06:43 +0100162static void initio_se2_ew_en(unsigned long base);
163static void initio_se2_ew_ds(unsigned long base);
164static int initio_se2_rd_all(unsigned long base);
165static void initio_se2_update_all(unsigned long base); /* setup default pattern */
166static void initio_read_eeprom(unsigned long base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Alan Cox72d39fe2007-05-21 15:06:43 +0100168/* ---- INTERNAL VARIABLES ---- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static NVRAM i91unvram;
171static NVRAM *i91unvramp;
172
Alan Cox72d39fe2007-05-21 15:06:43 +0100173static u8 i91udftNvRam[64] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Alan Cox72d39fe2007-05-21 15:06:43 +0100175 /*----------- header -----------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 0x25, 0xc9, /* Signature */
177 0x40, /* Size */
178 0x01, /* Revision */
179 /* -- Host Adapter Structure -- */
180 0x95, /* ModelByte0 */
181 0x00, /* ModelByte1 */
182 0x00, /* ModelInfo */
183 0x01, /* NumOfCh */
184 NBC1_DEFAULT, /* BIOSConfig1 */
185 0, /* BIOSConfig2 */
186 0, /* HAConfig1 */
187 0, /* HAConfig2 */
188 /* SCSI channel 0 and target Structure */
189 7, /* SCSIid */
190 NCC1_DEFAULT, /* SCSIconfig1 */
191 0, /* SCSIconfig2 */
192 0x10, /* NumSCSItarget */
193
194 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
195 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
196 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
197 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
198
199 /* SCSI channel 1 and target Structure */
200 7, /* SCSIid */
201 NCC1_DEFAULT, /* SCSIconfig1 */
202 0, /* SCSIconfig2 */
203 0x10, /* NumSCSItarget */
204
205 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
206 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
207 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
208 NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
209 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
210 0, 0}; /* - CheckSum - */
211
212
Alan Cox72d39fe2007-05-21 15:06:43 +0100213static u8 initio_rate_tbl[8] = /* fast 20 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300215 /* nanosecond divide by 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 12, /* 50ns, 20M */
217 18, /* 75ns, 13.3M */
218 25, /* 100ns, 10M */
219 31, /* 125ns, 8M */
220 37, /* 150ns, 6.6M */
221 43, /* 175ns, 5.7M */
222 50, /* 200ns, 5M */
223 62 /* 250ns, 4M */
224};
225
Alan Cox72d39fe2007-05-21 15:06:43 +0100226static void initio_do_pause(unsigned amount)
227{
228 /* Pause for amount jiffies */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 unsigned long the_time = jiffies + amount;
230
Alan Cox72d39fe2007-05-21 15:06:43 +0100231 while (time_before_eq(jiffies, the_time))
232 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/*-- forward reference --*/
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/******************************************************************
238 Input: instruction for Serial E2PROM
239
240 EX: se2_rd(0 call se2_instr() to send address and read command
241
242 StartBit OP_Code Address Data
243 --------- -------- ------------------ -------
244 1 1 , 0 A5,A4,A3,A2,A1,A0 D15-D0
245
246 +-----------------------------------------------------
247 |
248 CS -----+
249 +--+ +--+ +--+ +--+ +--+
250 ^ | ^ | ^ | ^ | ^ |
251 | | | | | | | | | |
252 CLK -------+ +--+ +--+ +--+ +--+ +--
253 (leading edge trigger)
254
255 +--1-----1--+
256 | SB OP | OP A5 A4
257 DI ----+ +--0------------------
258 (address and cmd sent to nvram)
259
260 -------------------------------------------+
261 |
262 DO +---
263 (data sent from nvram)
264
265
266******************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100267
268/**
269 * initio_se2_instr - bitbang an instruction
270 * @base: Base of InitIO controller
271 * @instr: Instruction for serial E2PROM
272 *
273 * Bitbang an instruction out to the serial E2Prom
274 */
275
276static void initio_se2_instr(unsigned long base, u8 instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100279 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Alan Cox72d39fe2007-05-21 15:06:43 +0100281 outb(SE2CS | SE2DO, base + TUL_NVRAM); /* cs+start bit */
282 udelay(30);
283 outb(SE2CS | SE2CLK | SE2DO, base + TUL_NVRAM); /* +CLK */
284 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 for (i = 0; i < 8; i++) {
287 if (instr & 0x80)
Alan Cox72d39fe2007-05-21 15:06:43 +0100288 b = SE2CS | SE2DO; /* -CLK+dataBit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100290 b = SE2CS; /* -CLK */
291 outb(b, base + TUL_NVRAM);
292 udelay(30);
293 outb(b | SE2CLK, base + TUL_NVRAM); /* +CLK */
294 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 instr <<= 1;
296 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100297 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
298 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301
Alan Cox72d39fe2007-05-21 15:06:43 +0100302/**
303 * initio_se2_ew_en - Enable erase/write
304 * @base: Base address of InitIO controller
305 *
306 * Enable erase/write state of serial EEPROM
307 */
308void initio_se2_ew_en(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Alan Cox72d39fe2007-05-21 15:06:43 +0100310 initio_se2_instr(base, 0x30); /* EWEN */
311 outb(0, base + TUL_NVRAM); /* -CS */
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_ds - Disable erase/write
318 * @base: Base address of InitIO controller
319 *
320 * Disable erase/write state of serial EEPROM
321 */
322void initio_se2_ew_ds(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Alan Cox72d39fe2007-05-21 15:06:43 +0100324 initio_se2_instr(base, 0); /* EWDS */
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_rd - read E2PROM word
332 * @base: Base of InitIO controller
333 * @addr: Address of word in E2PROM
334 *
335 * Read a word from the NV E2PROM device
336 */
337static u16 initio_se2_rd(unsigned long base, u8 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Alan Cox72d39fe2007-05-21 15:06:43 +0100339 u8 instr, rb;
340 u16 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int i;
342
Alan Cox72d39fe2007-05-21 15:06:43 +0100343 instr = (u8) (addr | 0x80);
344 initio_se2_instr(base, instr); /* READ INSTR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 for (i = 15; i >= 0; i--) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100347 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
348 udelay(30);
349 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* sample data after the following edge of clock */
Alan Cox72d39fe2007-05-21 15:06:43 +0100352 rb = inb(base + TUL_NVRAM);
353 rb &= SE2DI;
354 val += (rb << i);
355 udelay(30); /* 6/20/95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
Alan Cox72d39fe2007-05-21 15:06:43 +0100358 outb(0, base + TUL_NVRAM); /* no chip select */
359 udelay(30);
360 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Alan Cox72d39fe2007-05-21 15:06:43 +0100363/**
364 * initio_se2_wr - read E2PROM word
365 * @base: Base of InitIO controller
366 * @addr: Address of word in E2PROM
367 * @val: Value to write
368 *
369 * Write a word to the NV E2PROM device. Used when recovering from
370 * a problem with the NV.
371 */
372static void initio_se2_wr(unsigned long base, u8 addr, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Alan Cox72d39fe2007-05-21 15:06:43 +0100374 u8 rb;
375 u8 instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int i;
377
Alan Cox72d39fe2007-05-21 15:06:43 +0100378 instr = (u8) (addr | 0x40);
379 initio_se2_instr(base, instr); /* WRITE INSTR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 for (i = 15; i >= 0; i--) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100381 if (val & 0x8000)
382 outb(SE2CS | SE2DO, base + TUL_NVRAM); /* -CLK+dataBit 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100384 outb(SE2CS, base + TUL_NVRAM); /* -CLK+dataBit 0 */
385 udelay(30);
386 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
387 udelay(30);
388 val <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100390 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
391 udelay(30);
392 outb(0, base + TUL_NVRAM); /* -CS */
393 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Alan Cox72d39fe2007-05-21 15:06:43 +0100395 outb(SE2CS, base + TUL_NVRAM); /* +CS */
396 udelay(30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100399 outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
400 udelay(30);
401 outb(SE2CS, base + TUL_NVRAM); /* -CLK */
402 udelay(30);
403 if ((rb = inb(base + TUL_NVRAM)) & SE2DI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break; /* write complete */
405 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100406 outb(0, base + TUL_NVRAM); /* -CS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Alan Cox72d39fe2007-05-21 15:06:43 +0100409/**
410 * initio_se2_rd_all - read hostadapter NV configuration
411 * @base: Base address of InitIO controller
412 *
413 * Reads the E2PROM data into main memory. Ensures that the checksum
414 * and header marker are valid. Returns 1 on success -1 on error.
415 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Alan Cox72d39fe2007-05-21 15:06:43 +0100417static int initio_se2_rd_all(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100420 u16 chksum = 0;
421 u16 *np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 i91unvramp = &i91unvram;
Alan Cox72d39fe2007-05-21 15:06:43 +0100424 np = (u16 *) i91unvramp;
425 for (i = 0; i < 32; i++)
426 *np++ = initio_se2_rd(base, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Alan Cox72d39fe2007-05-21 15:06:43 +0100428 /* Is signature "ini" ok ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (i91unvramp->NVM_Signature != INI_SIGNATURE)
430 return -1;
Alan Cox72d39fe2007-05-21 15:06:43 +0100431 /* Is ckecksum ok ? */
432 np = (u16 *) i91unvramp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 for (i = 0; i < 31; i++)
434 chksum += *np++;
Alan Cox72d39fe2007-05-21 15:06:43 +0100435 if (i91unvramp->NVM_CheckSum != chksum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -1;
437 return 1;
438}
439
Alan Cox72d39fe2007-05-21 15:06:43 +0100440/**
441 * initio_se2_update_all - Update E2PROM
442 * @base: Base of InitIO controller
443 *
444 * Update the E2PROM by wrting any changes into the E2PROM
445 * chip, rewriting the checksum.
446 */
447static void initio_se2_update_all(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{ /* setup default pattern */
449 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100450 u16 chksum = 0;
451 u16 *np, *np1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 i91unvramp = &i91unvram;
454 /* Calculate checksum first */
Alan Cox72d39fe2007-05-21 15:06:43 +0100455 np = (u16 *) i91udftNvRam;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 for (i = 0; i < 31; i++)
457 chksum += *np++;
Alan Cox72d39fe2007-05-21 15:06:43 +0100458 *np = chksum;
459 initio_se2_ew_en(base); /* Enable write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Alan Cox72d39fe2007-05-21 15:06:43 +0100461 np = (u16 *) i91udftNvRam;
462 np1 = (u16 *) i91unvramp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 for (i = 0; i < 32; i++, np++, np1++) {
Alan Cox72d39fe2007-05-21 15:06:43 +0100464 if (*np != *np1)
465 initio_se2_wr(base, i, *np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100467 initio_se2_ew_ds(base); /* Disable write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Alan Cox72d39fe2007-05-21 15:06:43 +0100470/**
471 * initio_read_eeprom - Retrieve configuration
472 * @base: Base of InitIO Host Adapter
473 *
474 * Retrieve the host adapter configuration data from E2Prom. If the
475 * data is invalid then the defaults are used and are also restored
476 * into the E2PROM. This forms the access point for the SCSI driver
477 * into the E2PROM layer, the other functions for the E2PROM are all
478 * internal use.
479 *
480 * Must be called single threaded, uses a shared global area.
481 */
482
483static void initio_read_eeprom(unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Alan Cox72d39fe2007-05-21 15:06:43 +0100485 u8 gctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 i91unvramp = &i91unvram;
Alan Cox72d39fe2007-05-21 15:06:43 +0100488 /* Enable EEProm programming */
489 gctrl = inb(base + TUL_GCTRL);
490 outb(gctrl | TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
491 if (initio_se2_rd_all(base) != 1) {
492 initio_se2_update_all(base); /* setup default pattern */
493 initio_se2_rd_all(base); /* load again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100495 /* Disable EEProm programming */
496 gctrl = inb(base + TUL_GCTRL);
497 outb(gctrl & ~TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Alan Cox72d39fe2007-05-21 15:06:43 +0100500/**
501 * initio_stop_bm - stop bus master
502 * @host: InitIO we are stopping
503 *
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800504 * Stop any pending DMA operation, aborting the DMA if necessary
Alan Cox72d39fe2007-05-21 15:06:43 +0100505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Alan Cox72d39fe2007-05-21 15:06:43 +0100507static void initio_stop_bm(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509
Alan Cox72d39fe2007-05-21 15:06:43 +0100510 if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */
511 outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* wait Abort DMA xfer done */
Alan Cox72d39fe2007-05-21 15:06:43 +0100513 while ((inb(host->addr + TUL_Int) & XABT) == 0)
514 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100516 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Alan Cox72d39fe2007-05-21 15:06:43 +0100519/**
520 * initio_reset_scsi - Reset SCSI host controller
521 * @host: InitIO host to reset
522 * @seconds: Recovery time
523 *
524 * Perform a full reset of the SCSI subsystem.
525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Alan Cox72d39fe2007-05-21 15:06:43 +0100527static int initio_reset_scsi(struct initio_host * host, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Alan Cox72d39fe2007-05-21 15:06:43 +0100529 outb(TSC_RST_BUS, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Alan Cox72d39fe2007-05-21 15:06:43 +0100531 while (!((host->jsint = inb(host->addr + TUL_SInt)) & TSS_SCSIRST_INT))
532 cpu_relax();
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* reset tulip chip */
Alan Cox72d39fe2007-05-21 15:06:43 +0100535 outb(0, host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* Stall for a while, wait for target's firmware ready,make it 2 sec ! */
538 /* SONY 5200 tape drive won't work if only stall for 1 sec */
Alan Cox72d39fe2007-05-21 15:06:43 +0100539 /* FIXME: this is a very long busy wait right now */
540 initio_do_pause(seconds * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Alan Cox72d39fe2007-05-21 15:06:43 +0100542 inb(host->addr + TUL_SInt);
543 return SCSI_RESET_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Alan Cox72d39fe2007-05-21 15:06:43 +0100546/**
547 * initio_init - set up an InitIO host adapter
548 * @host: InitIO host adapter
549 * @num_scbs: Number of SCBS
550 * @bios_addr: BIOS address
551 *
552 * Set up the host adapter and devices according to the configuration
553 * retrieved from the E2PROM.
554 *
555 * Locking: Calls E2PROM layer code which is not re-enterable so must
556 * run single threaded for now.
557 */
558
559static void initio_init(struct initio_host * host, u8 *bios_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 int i;
Alan Cox72d39fe2007-05-21 15:06:43 +0100562 u8 *flags;
563 u8 *heads;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Alan Cox72d39fe2007-05-21 15:06:43 +0100565 /* Get E2Prom configuration */
566 initio_read_eeprom(host->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (i91unvramp->NVM_SCSIInfo[0].NVM_NumOfTarg == 8)
Alan Cox72d39fe2007-05-21 15:06:43 +0100568 host->max_tar = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100570 host->max_tar = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Alan Cox72d39fe2007-05-21 15:06:43 +0100572 host->config = i91unvramp->NVM_SCSIInfo[0].NVM_ChConfig1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Alan Cox72d39fe2007-05-21 15:06:43 +0100574 host->scsi_id = i91unvramp->NVM_SCSIInfo[0].NVM_ChSCSIID;
575 host->idmask = ~(1 << host->scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Olaf Hering44456d32005-07-27 11:45:17 -0700577#ifdef CHK_PARITY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* Enable parity error response */
Alan Cox72d39fe2007-05-21 15:06:43 +0100579 outb(inb(host->addr + TUL_PCMD) | 0x40, host->addr + TUL_PCMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580#endif
581
582 /* Mask all the interrupt */
Alan Cox72d39fe2007-05-21 15:06:43 +0100583 outb(0x1F, host->addr + TUL_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Alan Cox72d39fe2007-05-21 15:06:43 +0100585 initio_stop_bm(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* --- Initialize the tulip --- */
Alan Cox72d39fe2007-05-21 15:06:43 +0100587 outb(TSC_RST_CHIP, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* program HBA's SCSI ID */
Alan Cox72d39fe2007-05-21 15:06:43 +0100590 outb(host->scsi_id << 4, host->addr + TUL_SScsiId);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* Enable Initiator Mode ,phase latch,alternate sync period mode,
593 disable SCSI reset */
Alan Cox72d39fe2007-05-21 15:06:43 +0100594 if (host->config & HCC_EN_PAR)
595 host->sconf1 = (TSC_INITDEFAULT | TSC_EN_SCSI_PAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100597 host->sconf1 = (TSC_INITDEFAULT);
598 outb(host->sconf1, host->addr + TUL_SConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Alan Cox72d39fe2007-05-21 15:06:43 +0100600 /* Enable HW reselect */
601 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Alan Cox72d39fe2007-05-21 15:06:43 +0100603 outb(0, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* selection time out = 250 ms */
Alan Cox72d39fe2007-05-21 15:06:43 +0100606 outb(153, host->addr + TUL_STimeOut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Alan Cox72d39fe2007-05-21 15:06:43 +0100608 /* Enable SCSI terminator */
609 outb((host->config & (HCC_ACT_TERM1 | HCC_ACT_TERM2)),
610 host->addr + TUL_XCtrl);
611 outb(((host->config & HCC_AUTO_TERM) >> 4) |
612 (inb(host->addr + TUL_GCTRL1) & 0xFE),
613 host->addr + TUL_GCTRL1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 for (i = 0,
Alan Cox72d39fe2007-05-21 15:06:43 +0100616 flags = & (i91unvramp->NVM_SCSIInfo[0].NVM_Targ0Config),
617 heads = bios_addr + 0x180;
618 i < host->max_tar;
619 i++, flags++) {
620 host->targets[i].flags = *flags & ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
621 if (host->targets[i].flags & TCF_EN_255)
622 host->targets[i].drv_flags = TCF_DRV_255_63;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100624 host->targets[i].drv_flags = 0;
625 host->targets[i].js_period = 0;
626 host->targets[i].sconfig0 = host->sconf1;
627 host->targets[i].heads = *heads++;
628 if (host->targets[i].heads == 255)
629 host->targets[i].drv_flags = TCF_DRV_255_63;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100631 host->targets[i].drv_flags = 0;
632 host->targets[i].sectors = *heads++;
633 host->targets[i].flags &= ~TCF_BUSY;
634 host->act_tags[i] = 0;
635 host->max_tags[i] = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 } /* for */
637 printk("i91u: PCI Base=0x%04X, IRQ=%d, BIOS=0x%04X0, SCSI ID=%d\n",
Alan Coxe9e42fa2007-10-15 20:51:32 +0100638 host->addr, host->pci_dev->irq,
Alan Cox72d39fe2007-05-21 15:06:43 +0100639 host->bios_addr, host->scsi_id);
640 /* Reset SCSI Bus */
641 if (host->config & HCC_SCSI_RESET) {
642 printk(KERN_INFO "i91u: Reset SCSI Bus ... \n");
643 initio_reset_scsi(host, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100645 outb(0x17, host->addr + TUL_SCFG1);
646 outb(0xE9, host->addr + TUL_SIntEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Alan Cox72d39fe2007-05-21 15:06:43 +0100649/**
650 * initio_alloc_scb - Allocate an SCB
651 * @host: InitIO host we are allocating for
652 *
653 * Walk the SCB list for the controller and allocate a free SCB if
654 * one exists.
655 */
656static struct scsi_ctrl_blk *initio_alloc_scb(struct initio_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Alan Cox72d39fe2007-05-21 15:06:43 +0100658 struct scsi_ctrl_blk *scb;
659 unsigned long flags;
660
661 spin_lock_irqsave(&host->avail_lock, flags);
662 if ((scb = host->first_avail) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100664 printk("find scb at %p\n", scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100666 if ((host->first_avail = scb->next) == NULL)
667 host->last_avail = NULL;
668 scb->next = NULL;
669 scb->status = SCB_RENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100671 spin_unlock_irqrestore(&host->avail_lock, flags);
672 return scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Alan Cox72d39fe2007-05-21 15:06:43 +0100675/**
676 * initio_release_scb - Release an SCB
677 * @host: InitIO host that owns the SCB
678 * @cmnd: SCB command block being returned
679 *
680 * Return an allocated SCB to the host free list
681 */
682
683static void initio_release_scb(struct initio_host * host, struct scsi_ctrl_blk * cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Alan Cox72d39fe2007-05-21 15:06:43 +0100685 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100688 printk("Release SCB %p; ", cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100690 spin_lock_irqsave(&(host->avail_lock), flags);
691 cmnd->srb = NULL;
692 cmnd->status = 0;
693 cmnd->next = NULL;
694 if (host->last_avail != NULL) {
695 host->last_avail->next = cmnd;
696 host->last_avail = cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100698 host->first_avail = cmnd;
699 host->last_avail = cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100701 spin_unlock_irqrestore(&(host->avail_lock), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100705static void initio_append_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707
708#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100709 printk("Append pend SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100711 scbp->status = SCB_PEND;
712 scbp->next = NULL;
713 if (host->last_pending != NULL) {
714 host->last_pending->next = scbp;
715 host->last_pending = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100717 host->first_pending = scbp;
718 host->last_pending = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720}
721
722/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100723static void initio_push_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725
726#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100727 printk("Push pend SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100729 scbp->status = SCB_PEND;
730 if ((scbp->next = host->first_pending) != NULL) {
731 host->first_pending = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100733 host->first_pending = scbp;
734 host->last_pending = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736}
737
Alan Cox72d39fe2007-05-21 15:06:43 +0100738static struct scsi_ctrl_blk *initio_find_first_pend_scb(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Alan Cox72d39fe2007-05-21 15:06:43 +0100740 struct scsi_ctrl_blk *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742
Alan Cox72d39fe2007-05-21 15:06:43 +0100743 first = host->first_pending;
744 while (first != NULL) {
745 if (first->opcode != ExecSCSI)
746 return first;
747 if (first->tagmsg == 0) {
748 if ((host->act_tags[first->target] == 0) &&
749 !(host->targets[first->target].flags & TCF_BUSY))
750 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100752 if ((host->act_tags[first->target] >=
753 host->max_tags[first->target]) |
754 (host->targets[first->target].flags & TCF_BUSY)) {
755 first = first->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 continue;
757 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100758 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100760 first = first->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100762 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
Alan Cox72d39fe2007-05-21 15:06:43 +0100764
765static void initio_unlink_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Alan Cox72d39fe2007-05-21 15:06:43 +0100767 struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100770 printk("unlink pend SCB %p; ", scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771#endif
772
Alan Cox72d39fe2007-05-21 15:06:43 +0100773 prev = tmp = host->first_pending;
774 while (tmp != NULL) {
775 if (scb == tmp) { /* Unlink this SCB */
776 if (tmp == host->first_pending) {
777 if ((host->first_pending = tmp->next) == NULL)
778 host->last_pending = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100780 prev->next = tmp->next;
781 if (tmp == host->last_pending)
782 host->last_pending = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100784 tmp->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 break;
786 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100787 prev = tmp;
788 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
Alan Cox72d39fe2007-05-21 15:06:43 +0100791
792static void initio_append_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794
795#if DEBUG_QUEUE
Stuart Swalese2d435e2008-01-23 20:00:48 +0000796 printk("append busy SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100798 if (scbp->tagmsg)
799 host->act_tags[scbp->target]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100801 host->targets[scbp->target].flags |= TCF_BUSY;
802 scbp->status = SCB_BUSY;
803 scbp->next = NULL;
804 if (host->last_busy != NULL) {
805 host->last_busy->next = scbp;
806 host->last_busy = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100808 host->first_busy = scbp;
809 host->last_busy = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811}
812
813/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100814static struct scsi_ctrl_blk *initio_pop_busy_scb(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Alan Cox72d39fe2007-05-21 15:06:43 +0100816 struct scsi_ctrl_blk *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818
Alan Cox72d39fe2007-05-21 15:06:43 +0100819 if ((tmp = host->first_busy) != NULL) {
820 if ((host->first_busy = tmp->next) == NULL)
821 host->last_busy = NULL;
822 tmp->next = NULL;
823 if (tmp->tagmsg)
824 host->act_tags[tmp->target]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100826 host->targets[tmp->target].flags &= ~TCF_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100829 printk("Pop busy SCB %p; ", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100831 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +0100835static void initio_unlink_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Alan Cox72d39fe2007-05-21 15:06:43 +0100837 struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100840 printk("unlink busy SCB %p; ", scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841#endif
842
Alan Cox72d39fe2007-05-21 15:06:43 +0100843 prev = tmp = host->first_busy;
844 while (tmp != NULL) {
845 if (scb == tmp) { /* Unlink this SCB */
846 if (tmp == host->first_busy) {
847 if ((host->first_busy = tmp->next) == NULL)
848 host->last_busy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100850 prev->next = tmp->next;
851 if (tmp == host->last_busy)
852 host->last_busy = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100854 tmp->next = NULL;
855 if (tmp->tagmsg)
856 host->act_tags[tmp->target]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 else
Alan Cox72d39fe2007-05-21 15:06:43 +0100858 host->targets[tmp->target].flags &= ~TCF_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 break;
860 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100861 prev = tmp;
862 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 return;
865}
866
Alan Cox72d39fe2007-05-21 15:06:43 +0100867struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Alan Cox72d39fe2007-05-21 15:06:43 +0100869 struct scsi_ctrl_blk *tmp, *prev;
870 u16 scbp_tarlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872
Alan Cox72d39fe2007-05-21 15:06:43 +0100873 prev = tmp = host->first_busy;
874 while (tmp != NULL) {
875 scbp_tarlun = (tmp->lun << 8) | (tmp->target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (scbp_tarlun == tarlun) { /* Unlink this SCB */
877 break;
878 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100879 prev = tmp;
880 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100883 printk("find busy SCB %p; ", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100885 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Alan Cox72d39fe2007-05-21 15:06:43 +0100888static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100891 printk("append done SCB %p; ", scbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892#endif
893
Alan Cox72d39fe2007-05-21 15:06:43 +0100894 scbp->status = SCB_DONE;
895 scbp->next = NULL;
896 if (host->last_done != NULL) {
897 host->last_done->next = scbp;
898 host->last_done = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100900 host->first_done = scbp;
901 host->last_done = scbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903}
904
Alan Cox72d39fe2007-05-21 15:06:43 +0100905struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Alan Cox72d39fe2007-05-21 15:06:43 +0100907 struct scsi_ctrl_blk *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Alan Cox72d39fe2007-05-21 15:06:43 +0100909 if ((tmp = host->first_done) != NULL) {
910 if ((host->first_done = tmp->next) == NULL)
911 host->last_done = NULL;
912 tmp->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914#if DEBUG_QUEUE
Alan Cox72d39fe2007-05-21 15:06:43 +0100915 printk("find done SCB %p; ",tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916#endif
Alan Cox72d39fe2007-05-21 15:06:43 +0100917 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Alan Cox72d39fe2007-05-21 15:06:43 +0100920static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Alan Cox72d39fe2007-05-21 15:06:43 +0100922 unsigned long flags;
923 struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Alan Cox72d39fe2007-05-21 15:06:43 +0100925 spin_lock_irqsave(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Alan Cox72d39fe2007-05-21 15:06:43 +0100927 if ((host->semaph == 0) && (host->active == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* disable Jasmin SCSI Int */
Alan Cox72d39fe2007-05-21 15:06:43 +0100929 outb(0x1F, host->addr + TUL_Mask);
930 spin_unlock_irqrestore(&host->semaph_lock, flags);
931 /* FIXME: synchronize_irq needed ? */
932 tulip_main(host);
933 spin_lock_irqsave(&host->semaph_lock, flags);
934 host->semaph = 1;
935 outb(0x0F, host->addr + TUL_Mask);
936 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return SCSI_ABORT_SNOOZE;
938 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100939 prev = tmp = host->first_pending; /* Check Pend queue */
940 while (tmp != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* 07/27/98 */
Alan Cox72d39fe2007-05-21 15:06:43 +0100942 if (tmp->srb == srbp) {
943 if (tmp == host->active) {
944 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return SCSI_ABORT_BUSY;
Alan Cox72d39fe2007-05-21 15:06:43 +0100946 } else if (tmp == host->first_pending) {
947 if ((host->first_pending = tmp->next) == NULL)
948 host->last_pending = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100950 prev->next = tmp->next;
951 if (tmp == host->last_pending)
952 host->last_pending = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100954 tmp->hastat = HOST_ABORTED;
955 tmp->flags |= SCF_DONE;
956 if (tmp->flags & SCF_POST)
957 (*tmp->post) ((u8 *) host, (u8 *) tmp);
958 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return SCSI_ABORT_SUCCESS;
960 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100961 prev = tmp;
962 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
Alan Cox72d39fe2007-05-21 15:06:43 +0100965 prev = tmp = host->first_busy; /* Check Busy queue */
966 while (tmp != NULL) {
967 if (tmp->srb == srbp) {
968 if (tmp == host->active) {
969 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return SCSI_ABORT_BUSY;
Alan Cox72d39fe2007-05-21 15:06:43 +0100971 } else if (tmp->tagmsg == 0) {
972 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return SCSI_ABORT_BUSY;
974 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100975 host->act_tags[tmp->target]--;
976 if (tmp == host->first_busy) {
977 if ((host->first_busy = tmp->next) == NULL)
978 host->last_busy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +0100980 prev->next = tmp->next;
981 if (tmp == host->last_busy)
982 host->last_busy = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100984 tmp->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986
Alan Cox72d39fe2007-05-21 15:06:43 +0100987 tmp->hastat = HOST_ABORTED;
988 tmp->flags |= SCF_DONE;
989 if (tmp->flags & SCF_POST)
990 (*tmp->post) ((u8 *) host, (u8 *) tmp);
991 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return SCSI_ABORT_SUCCESS;
993 }
994 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100995 prev = tmp;
996 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Alan Cox72d39fe2007-05-21 15:06:43 +0100998 spin_unlock_irqrestore(&host->semaph_lock, flags);
999 return SCSI_ABORT_NOT_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +01001003static int initio_bad_seq(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Alan Cox72d39fe2007-05-21 15:06:43 +01001005 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Alan Cox72d39fe2007-05-21 15:06:43 +01001007 printk("initio_bad_seg c=%d\n", host->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Alan Cox72d39fe2007-05-21 15:06:43 +01001009 if ((scb = host->active) != NULL) {
1010 initio_unlink_busy_scb(host, scb);
1011 scb->hastat = HOST_BAD_PHAS;
1012 scb->tastat = 0;
1013 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001015 initio_stop_bm(host);
1016 initio_reset_scsi(host, 8); /* 7/29/98 */
1017 return initio_post_scsi_rst(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Adrian Bunka2ba1922005-06-25 14:58:50 -07001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021/************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +01001022static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Alan Cox72d39fe2007-05-21 15:06:43 +01001024 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Alan Cox72d39fe2007-05-21 15:06:43 +01001026 scb->mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Alan Cox72d39fe2007-05-21 15:06:43 +01001028 scb->sgidx = 0;
1029 scb->sgmax = scb->sglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Alan Cox72d39fe2007-05-21 15:06:43 +01001031 spin_lock_irqsave(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Alan Cox72d39fe2007-05-21 15:06:43 +01001033 initio_append_pend_scb(host, scb); /* Append this SCB to Pending queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035/* VVVVV 07/21/98 */
Alan Cox72d39fe2007-05-21 15:06:43 +01001036 if (host->semaph == 1) {
1037 /* Disable Jasmin SCSI Int */
1038 outb(0x1F, host->addr + TUL_Mask);
1039 host->semaph = 0;
1040 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Alan Cox72d39fe2007-05-21 15:06:43 +01001042 tulip_main(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Alan Cox72d39fe2007-05-21 15:06:43 +01001044 spin_lock_irqsave(&host->semaph_lock, flags);
1045 host->semaph = 1;
1046 outb(0x0F, host->addr + TUL_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001048 spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return;
1050}
1051
1052/***************************************************************************/
Alan Cox72d39fe2007-05-21 15:06:43 +01001053static int initio_isr(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Alan Cox72d39fe2007-05-21 15:06:43 +01001055 if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) {
1056 if (host->semaph == 1) {
1057 outb(0x1F, host->addr + TUL_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* Disable Tulip SCSI Int */
Alan Cox72d39fe2007-05-21 15:06:43 +01001059 host->semaph = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Alan Cox72d39fe2007-05-21 15:06:43 +01001061 tulip_main(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Alan Cox72d39fe2007-05-21 15:06:43 +01001063 host->semaph = 1;
1064 outb(0x0F, host->addr + TUL_Mask);
1065 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001068 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Alan Cox72d39fe2007-05-21 15:06:43 +01001071static int tulip_main(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Alan Cox72d39fe2007-05-21 15:06:43 +01001073 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001076 tulip_scsi(host); /* Call tulip_scsi */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Alan Cox72d39fe2007-05-21 15:06:43 +01001078 /* Walk the list of completed SCBs */
1079 while ((scb = initio_find_done_scb(host)) != NULL) { /* find done entry */
1080 if (scb->tastat == INI_QUEUE_FULL) {
1081 host->max_tags[scb->target] =
1082 host->act_tags[scb->target] - 1;
1083 scb->tastat = 0;
1084 initio_append_pend_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 continue;
1086 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001087 if (!(scb->mode & SCM_RSENS)) { /* not in auto req. sense mode */
1088 if (scb->tastat == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 /* clr sync. nego flag */
1091
Alan Cox72d39fe2007-05-21 15:06:43 +01001092 if (scb->flags & SCF_SENSE) {
1093 u8 len;
1094 len = scb->senselen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (len == 0)
1096 len = 1;
Alan Cox72d39fe2007-05-21 15:06:43 +01001097 scb->buflen = scb->senselen;
1098 scb->bufptr = scb->senseptr;
1099 scb->flags &= ~(SCF_SG | SCF_DIR); /* for xfer_data_in */
1100 /* so, we won't report wrong direction in xfer_data_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 and won't report HOST_DO_DU in state_6 */
Alan Cox72d39fe2007-05-21 15:06:43 +01001102 scb->mode = SCM_RSENS;
1103 scb->ident &= 0xBF; /* Disable Disconnect */
1104 scb->tagmsg = 0;
1105 scb->tastat = 0;
1106 scb->cdblen = 6;
1107 scb->cdb[0] = SCSICMD_RequestSense;
1108 scb->cdb[1] = 0;
1109 scb->cdb[2] = 0;
1110 scb->cdb[3] = 0;
1111 scb->cdb[4] = len;
1112 scb->cdb[5] = 0;
1113 initio_push_pend_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
1115 }
1116 }
1117 } else { /* in request sense mode */
1118
Alan Cox72d39fe2007-05-21 15:06:43 +01001119 if (scb->tastat == 2) { /* check contition status again after sending
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 requset sense cmd 0x3 */
Alan Cox72d39fe2007-05-21 15:06:43 +01001121 scb->hastat = HOST_BAD_PHAS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001123 scb->tastat = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001125 scb->flags |= SCF_DONE;
1126 if (scb->flags & SCF_POST) {
1127 /* FIXME: only one post method and lose casts */
1128 (*scb->post) ((u8 *) host, (u8 *) scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /* find_active: */
Alan Cox72d39fe2007-05-21 15:06:43 +01001132 if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 continue;
Alan Cox72d39fe2007-05-21 15:06:43 +01001134 if (host->active) /* return to OS and wait for xfer_done_ISR/Selected_ISR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return 1; /* return to OS, enable interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /* Check pending SCB */
Alan Cox72d39fe2007-05-21 15:06:43 +01001137 if (initio_find_first_pend_scb(host) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return 1; /* return to OS, enable interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 } /* End of for loop */
1140 /* statement won't reach here */
1141}
1142
Alan Cox72d39fe2007-05-21 15:06:43 +01001143static void tulip_scsi(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Alan Cox72d39fe2007-05-21 15:06:43 +01001145 struct scsi_ctrl_blk *scb;
1146 struct target_control *active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 /* make sure to service interrupt asap */
Alan Cox72d39fe2007-05-21 15:06:43 +01001149 if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) {
1150 host->phase = host->jsstatus0 & TSS_PH_MASK;
1151 host->jsstatus1 = inb(host->addr + TUL_SStatus1);
1152 host->jsint = inb(host->addr + TUL_SInt);
1153 if (host->jsint & TSS_SCSIRST_INT) { /* SCSI bus reset detected */
1154 int_initio_scsi_rst(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return;
1156 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001157 if (host->jsint & TSS_RESEL_INT) { /* if selected/reselected interrupt */
1158 if (int_initio_resel(host) == 0)
1159 initio_next_state(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return;
1161 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001162 if (host->jsint & TSS_SEL_TIMEOUT) {
1163 int_initio_busfree(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return;
1165 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001166 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
1167 int_initio_busfree(host); /* unexpected bus free or sel timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return;
1169 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001170 if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) { /* func complete or Bus service */
1171 if ((scb = host->active) != NULL)
1172 initio_next_state(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return;
1174 }
1175 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001176 if (host->active != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return;
1178
Alan Cox72d39fe2007-05-21 15:06:43 +01001179 if ((scb = initio_find_first_pend_scb(host)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return;
1181
1182 /* program HBA's SCSI ID & target SCSI ID */
Alan Cox72d39fe2007-05-21 15:06:43 +01001183 outb((host->scsi_id << 4) | (scb->target & 0x0F),
1184 host->addr + TUL_SScsiId);
1185 if (scb->opcode == ExecSCSI) {
1186 active_tc = &host->targets[scb->target];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Alan Cox72d39fe2007-05-21 15:06:43 +01001188 if (scb->tagmsg)
1189 active_tc->drv_flags |= TCF_DRV_EN_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001191 active_tc->drv_flags &= ~TCF_DRV_EN_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Alan Cox72d39fe2007-05-21 15:06:43 +01001193 outb(active_tc->js_period, host->addr + TUL_SPeriod);
1194 if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) { /* do wdtr negotiation */
1195 initio_select_atn_stop(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001197 if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) { /* do sync negotiation */
1198 initio_select_atn_stop(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001200 if (scb->tagmsg)
1201 initio_select_atn3(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001203 initio_select_atn(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001206 if (scb->flags & SCF_POLL) {
1207 while (wait_tulip(host) != -1) {
1208 if (initio_next_state(host) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 break;
1210 }
1211 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001212 } else if (scb->opcode == BusDevRst) {
1213 initio_select_atn_stop(host, scb);
1214 scb->next_state = 8;
1215 if (scb->flags & SCF_POLL) {
1216 while (wait_tulip(host) != -1) {
1217 if (initio_next_state(host) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 break;
1219 }
1220 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001221 } else if (scb->opcode == AbortCmd) {
1222 if (initio_abort_srb(host, scb->srb) != 0) {
1223 initio_unlink_pend_scb(host, scb);
1224 initio_release_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001226 scb->opcode = BusDevRst;
1227 initio_select_atn_stop(host, scb);
1228 scb->next_state = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001231 initio_unlink_pend_scb(host, scb);
1232 scb->hastat = 0x16; /* bad command */
1233 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 return;
1236}
1237
Alan Cox72d39fe2007-05-21 15:06:43 +01001238/**
1239 * initio_next_state - Next SCSI state
1240 * @host: InitIO host we are processing
1241 *
1242 * Progress the active command block along the state machine
1243 * until we hit a state which we must wait for activity to occur.
1244 *
1245 * Returns zero or a negative code.
1246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Alan Cox72d39fe2007-05-21 15:06:43 +01001248static int initio_next_state(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 int next;
1251
Alan Cox72d39fe2007-05-21 15:06:43 +01001252 next = host->active->next_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 for (;;) {
1254 switch (next) {
1255 case 1:
Alan Cox72d39fe2007-05-21 15:06:43 +01001256 next = initio_state_1(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 break;
1258 case 2:
Alan Cox72d39fe2007-05-21 15:06:43 +01001259 next = initio_state_2(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 break;
1261 case 3:
Alan Cox72d39fe2007-05-21 15:06:43 +01001262 next = initio_state_3(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 break;
1264 case 4:
Alan Cox72d39fe2007-05-21 15:06:43 +01001265 next = initio_state_4(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 break;
1267 case 5:
Alan Cox72d39fe2007-05-21 15:06:43 +01001268 next = initio_state_5(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 break;
1270 case 6:
Alan Cox72d39fe2007-05-21 15:06:43 +01001271 next = initio_state_6(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
1273 case 7:
Alan Cox72d39fe2007-05-21 15:06:43 +01001274 next = initio_state_7(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 break;
1276 case 8:
Alan Cox72d39fe2007-05-21 15:06:43 +01001277 return initio_bus_device_reset(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001279 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 if (next <= 0)
1282 return next;
1283 }
1284}
1285
1286
Alan Cox72d39fe2007-05-21 15:06:43 +01001287/**
1288 * initio_state_1 - SCSI state machine
1289 * @host: InitIO host we are controlling
1290 *
1291 * Perform SCSI state processing for Select/Attention/Stop
1292 */
1293
1294static int initio_state_1(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Alan Cox72d39fe2007-05-21 15:06:43 +01001296 struct scsi_ctrl_blk *scb = host->active;
1297 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298#if DEBUG_STATE
1299 printk("-s1-");
1300#endif
1301
Alan Cox72d39fe2007-05-21 15:06:43 +01001302 /* Move the SCB from pending to busy */
1303 initio_unlink_pend_scb(host, scb);
1304 initio_append_busy_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Alan Cox72d39fe2007-05-21 15:06:43 +01001306 outb(active_tc->sconfig0, host->addr + TUL_SConfig );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /* ATN on */
Alan Cox72d39fe2007-05-21 15:06:43 +01001308 if (host->phase == MSG_OUT) {
1309 outb(TSC_EN_BUS_IN | TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1310 outb(scb->ident, host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Alan Cox72d39fe2007-05-21 15:06:43 +01001312 if (scb->tagmsg) {
1313 outb(scb->tagmsg, host->addr + TUL_SFifo);
1314 outb(scb->tagid, host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001316 if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {
1317 active_tc->flags |= TCF_WDTR_DONE;
1318 outb(MSG_EXTEND, host->addr + TUL_SFifo);
1319 outb(2, host->addr + TUL_SFifo); /* Extended msg length */
1320 outb(3, host->addr + TUL_SFifo); /* Sync request */
1321 outb(1, host->addr + TUL_SFifo); /* Start from 16 bits */
1322 } else if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {
1323 active_tc->flags |= TCF_SYNC_DONE;
1324 outb(MSG_EXTEND, host->addr + TUL_SFifo);
1325 outb(3, host->addr + TUL_SFifo); /* extended msg length */
1326 outb(1, host->addr + TUL_SFifo); /* sync request */
1327 outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1328 outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001330 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1331 if (wait_tulip(host) == -1)
1332 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001334 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1335 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1336 /* Into before CDB xfer */
1337 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
1340
Alan Cox72d39fe2007-05-21 15:06:43 +01001341/**
1342 * initio_state_2 - SCSI state machine
1343 * @host: InitIO host we are controlling
1344 *
1345 * state after selection with attention
1346 * state after selection with attention3
1347 */
1348
1349static int initio_state_2(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Alan Cox72d39fe2007-05-21 15:06:43 +01001351 struct scsi_ctrl_blk *scb = host->active;
1352 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353#if DEBUG_STATE
1354 printk("-s2-");
1355#endif
1356
Alan Cox72d39fe2007-05-21 15:06:43 +01001357 initio_unlink_pend_scb(host, scb);
1358 initio_append_busy_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Alan Cox72d39fe2007-05-21 15:06:43 +01001360 outb(active_tc->sconfig0, host->addr + TUL_SConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Alan Cox72d39fe2007-05-21 15:06:43 +01001362 if (host->jsstatus1 & TSS_CMD_PH_CMP)
1363 return 4;
1364
1365 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1366 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1367 /* Into before CDB xfer */
1368 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
Alan Cox72d39fe2007-05-21 15:06:43 +01001371/**
1372 * initio_state_3 - SCSI state machine
1373 * @host: InitIO host we are controlling
1374 *
1375 * state before CDB xfer is done
1376 */
1377
1378static int initio_state_3(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Alan Cox72d39fe2007-05-21 15:06:43 +01001380 struct scsi_ctrl_blk *scb = host->active;
1381 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 int i;
1383
1384#if DEBUG_STATE
1385 printk("-s3-");
1386#endif
1387 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001388 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 case CMD_OUT: /* Command out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001390 for (i = 0; i < (int) scb->cdblen; i++)
1391 outb(scb->cdb[i], host->addr + TUL_SFifo);
1392 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1393 if (wait_tulip(host) == -1)
1394 return -1;
1395 if (host->phase == CMD_OUT)
1396 return initio_bad_seq(host);
1397 return 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 case MSG_IN: /* Message in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001400 scb->next_state = 3;
1401 if (initio_msgin(host) == -1)
1402 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 break;
1404
1405 case STATUS_IN: /* Status phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001406 if (initio_status_msg(host) == -1)
1407 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 break;
1409
1410 case MSG_OUT: /* Message out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001411 if (active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) {
1412 outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
1413 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1414 if (wait_tulip(host) == -1)
1415 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001417 active_tc->flags |= TCF_SYNC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Alan Cox72d39fe2007-05-21 15:06:43 +01001419 outb(MSG_EXTEND, host->addr + TUL_SFifo);
1420 outb(3, host->addr + TUL_SFifo); /* ext. msg len */
1421 outb(1, host->addr + TUL_SFifo); /* sync request */
1422 outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1423 outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */
1424 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1425 if (wait_tulip(host) == -1)
1426 return -1;
1427 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1428 outb(inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7), host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 }
1431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001433 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435 }
1436}
1437
Alan Cox72d39fe2007-05-21 15:06:43 +01001438/**
1439 * initio_state_4 - SCSI state machine
1440 * @host: InitIO host we are controlling
1441 *
1442 * SCSI state machine. State 4
1443 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Alan Cox72d39fe2007-05-21 15:06:43 +01001445static int initio_state_4(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Alan Cox72d39fe2007-05-21 15:06:43 +01001447 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449#if DEBUG_STATE
1450 printk("-s4-");
1451#endif
Alan Cox72d39fe2007-05-21 15:06:43 +01001452 if ((scb->flags & SCF_DIR) == SCF_NO_XF) {
1453 return 6; /* Go to state 6 (After data) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001456 if (scb->buflen == 0)
1457 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Alan Cox72d39fe2007-05-21 15:06:43 +01001459 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 case STATUS_IN: /* Status phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001462 if ((scb->flags & SCF_DIR) != 0) /* if direction bit set then report data underrun */
1463 scb->hastat = HOST_DO_DU;
1464 if ((initio_status_msg(host)) == -1)
1465 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467
1468 case MSG_IN: /* Message in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001469 scb->next_state = 0x4;
1470 if (initio_msgin(host) == -1)
1471 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473
1474 case MSG_OUT: /* Message out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001475 if (host->jsstatus0 & TSS_PAR_ERROR) {
1476 scb->buflen = 0;
1477 scb->hastat = HOST_DO_DU;
1478 if (initio_msgout_ide(host) == -1)
1479 return -1;
1480 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001482 outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
1483 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1484 if (wait_tulip(host) == -1)
1485 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 break;
1488
1489 case DATA_IN: /* Data in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001490 return initio_xfer_data_in(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 case DATA_OUT: /* Data out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001493 return initio_xfer_data_out(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001496 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498 }
1499}
1500
1501
Alan Cox72d39fe2007-05-21 15:06:43 +01001502/**
1503 * initio_state_5 - SCSI state machine
1504 * @host: InitIO host we are controlling
1505 *
1506 * State after dma xfer done or phase change before xfer done
1507 */
1508
1509static int initio_state_5(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Alan Cox72d39fe2007-05-21 15:06:43 +01001511 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 long cnt, xcnt; /* cannot use unsigned !! code: if (xcnt < 0) */
1513
1514#if DEBUG_STATE
1515 printk("-s5-");
1516#endif
Alan Cox72d39fe2007-05-21 15:06:43 +01001517 /*------ get remaining count -------*/
1518 cnt = inl(host->addr + TUL_SCnt0) & 0x0FFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Alan Cox72d39fe2007-05-21 15:06:43 +01001520 if (inb(host->addr + TUL_XCmd) & 0x20) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /* ----------------------- DATA_IN ----------------------------- */
1522 /* check scsi parity error */
Alan Cox72d39fe2007-05-21 15:06:43 +01001523 if (host->jsstatus0 & TSS_PAR_ERROR)
1524 scb->hastat = HOST_DO_DU;
1525 if (inb(host->addr + TUL_XStatus) & XPEND) { /* DMA xfer pending, Send STOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 /* tell Hardware scsi xfer has been terminated */
Alan Cox72d39fe2007-05-21 15:06:43 +01001527 outb(inb(host->addr + TUL_XCtrl) | 0x80, host->addr + TUL_XCtrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 /* wait until DMA xfer not pending */
Alan Cox72d39fe2007-05-21 15:06:43 +01001529 while (inb(host->addr + TUL_XStatus) & XPEND)
1530 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001533 /*-------- DATA OUT -----------*/
1534 if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) {
1535 if (host->active_tc->js_period & TSC_WIDE_SCSI)
1536 cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001538 cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001540 if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */
1541 outb(TAX_X_ABT, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /* wait Abort DMA xfer done */
Alan Cox72d39fe2007-05-21 15:06:43 +01001543 while ((inb(host->addr + TUL_Int) & XABT) == 0)
1544 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001546 if ((cnt == 1) && (host->phase == DATA_OUT)) {
1547 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1548 if (wait_tulip(host) == -1)
1549 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 cnt = 0;
1551 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001552 if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0)
1553 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (cnt == 0) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001557 scb->buflen = 0;
1558 return 6; /* After Data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560 /* Update active data pointer */
Alan Cox72d39fe2007-05-21 15:06:43 +01001561 xcnt = (long) scb->buflen - cnt; /* xcnt== bytes already xferred */
1562 scb->buflen = (u32) cnt; /* cnt == bytes left to be xferred */
1563 if (scb->flags & SCF_SG) {
1564 struct sg_entry *sgp;
1565 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Alan Cox72d39fe2007-05-21 15:06:43 +01001567 sgp = &scb->sglist[scb->sgidx];
1568 for (i = scb->sgidx; i < scb->sgmax; sgp++, i++) {
1569 xcnt -= (long) sgp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (xcnt < 0) { /* this sgp xfer half done */
Alan Cox72d39fe2007-05-21 15:06:43 +01001571 xcnt += (long) sgp->len; /* xcnt == bytes xferred in this sgp */
1572 sgp->data += (u32) xcnt; /* new ptr to be xfer */
1573 sgp->len -= (u32) xcnt; /* new len to be xfer */
1574 scb->bufptr += ((u32) (i - scb->sgidx) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /* new SG table ptr */
Alan Cox72d39fe2007-05-21 15:06:43 +01001576 scb->sglen = (u8) (scb->sgmax - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /* new SG table len */
Alan Cox72d39fe2007-05-21 15:06:43 +01001578 scb->sgidx = (u16) i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* for next disc and come in this loop */
Alan Cox72d39fe2007-05-21 15:06:43 +01001580 return 4; /* Go to state 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582 /* else (xcnt >= 0 , i.e. this sgp already xferred */
1583 } /* for */
Alan Cox72d39fe2007-05-21 15:06:43 +01001584 return 6; /* Go to state 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001586 scb->bufptr += (u32) xcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001588 return 4; /* Go to state 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Alan Cox72d39fe2007-05-21 15:06:43 +01001591/**
1592 * initio_state_6 - SCSI state machine
1593 * @host: InitIO host we are controlling
1594 *
1595 * State after Data phase
1596 */
1597
1598static int initio_state_6(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Alan Cox72d39fe2007-05-21 15:06:43 +01001600 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602#if DEBUG_STATE
1603 printk("-s6-");
1604#endif
1605 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001606 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 case STATUS_IN: /* Status phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001608 if ((initio_status_msg(host)) == -1)
1609 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 break;
1611
1612 case MSG_IN: /* Message in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001613 scb->next_state = 6;
1614 if ((initio_msgin(host)) == -1)
1615 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 break;
1617
1618 case MSG_OUT: /* Message out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001619 outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
1620 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1621 if (wait_tulip(host) == -1)
1622 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 break;
1624
1625 case DATA_IN: /* Data in phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001626 return initio_xpad_in(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 case DATA_OUT: /* Data out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001629 return initio_xpad_out(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001632 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634 }
1635}
1636
Alan Cox72d39fe2007-05-21 15:06:43 +01001637/**
1638 * initio_state_7 - SCSI state machine
1639 * @host: InitIO host we are controlling
1640 *
1641 */
1642
1643int initio_state_7(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 int cnt, i;
1646
1647#if DEBUG_STATE
1648 printk("-s7-");
1649#endif
1650 /* flush SCSI FIFO */
Alan Cox72d39fe2007-05-21 15:06:43 +01001651 cnt = inb(host->addr + TUL_SFifoCnt) & 0x1F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (cnt) {
1653 for (i = 0; i < cnt; i++)
Alan Cox72d39fe2007-05-21 15:06:43 +01001654 inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001656 switch (host->phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 case DATA_IN: /* Data in phase */
1658 case DATA_OUT: /* Data out phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001659 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01001661 return 6; /* Go to state 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663}
1664
Alan Cox72d39fe2007-05-21 15:06:43 +01001665/**
1666 * initio_xfer_data_in - Commence data input
1667 * @host: InitIO host in use
1668 *
1669 * Commence a block of data transfer. The transfer itself will
1670 * be managed by the controller and we will get a completion (or
1671 * failure) interrupt.
1672 */
1673static int initio_xfer_data_in(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Alan Cox72d39fe2007-05-21 15:06:43 +01001675 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Alan Cox72d39fe2007-05-21 15:06:43 +01001677 if ((scb->flags & SCF_DIR) == SCF_DOUT)
1678 return 6; /* wrong direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Alan Cox72d39fe2007-05-21 15:06:43 +01001680 outl(scb->buflen, host->addr + TUL_SCnt0);
1681 outb(TSC_XF_DMA_IN, host->addr + TUL_SCmd); /* 7/25/95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Alan Cox72d39fe2007-05-21 15:06:43 +01001683 if (scb->flags & SCF_SG) { /* S/G xfer */
1684 outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1685 outl(scb->bufptr, host->addr + TUL_XAddH);
1686 outb(TAX_SG_IN, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001688 outl(scb->buflen, host->addr + TUL_XCntH);
1689 outl(scb->bufptr, host->addr + TUL_XAddH);
1690 outb(TAX_X_IN, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001692 scb->next_state = 0x5;
1693 return 0; /* return to OS, wait xfer done , let jas_isr come in */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Alan Cox72d39fe2007-05-21 15:06:43 +01001696/**
1697 * initio_xfer_data_out - Commence data output
1698 * @host: InitIO host in use
1699 *
1700 * Commence a block of data transfer. The transfer itself will
1701 * be managed by the controller and we will get a completion (or
1702 * failure) interrupt.
1703 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Alan Cox72d39fe2007-05-21 15:06:43 +01001705static int initio_xfer_data_out(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Alan Cox72d39fe2007-05-21 15:06:43 +01001707 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Alan Cox72d39fe2007-05-21 15:06:43 +01001709 if ((scb->flags & SCF_DIR) == SCF_DIN)
1710 return 6; /* wrong direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Alan Cox72d39fe2007-05-21 15:06:43 +01001712 outl(scb->buflen, host->addr + TUL_SCnt0);
1713 outb(TSC_XF_DMA_OUT, host->addr + TUL_SCmd);
1714
1715 if (scb->flags & SCF_SG) { /* S/G xfer */
1716 outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1717 outl(scb->bufptr, host->addr + TUL_XAddH);
1718 outb(TAX_SG_OUT, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01001720 outl(scb->buflen, host->addr + TUL_XCntH);
1721 outl(scb->bufptr, host->addr + TUL_XAddH);
1722 outb(TAX_X_OUT, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724
Alan Cox72d39fe2007-05-21 15:06:43 +01001725 scb->next_state = 0x5;
1726 return 0; /* return to OS, wait xfer done , let jas_isr come in */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Alan Cox72d39fe2007-05-21 15:06:43 +01001729int initio_xpad_in(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Alan Cox72d39fe2007-05-21 15:06:43 +01001731 struct scsi_ctrl_blk *scb = host->active;
1732 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Alan Cox72d39fe2007-05-21 15:06:43 +01001734 if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1735 scb->hastat = HOST_DO_DU; /* over run */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001737 if (active_tc->js_period & TSC_WIDE_SCSI)
1738 outl(2, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001740 outl(1, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Alan Cox72d39fe2007-05-21 15:06:43 +01001742 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1743 if (wait_tulip(host) == -1)
1744 return -1;
1745 if (host->phase != DATA_IN) {
1746 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1747 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001749 inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751}
1752
Alan Cox72d39fe2007-05-21 15:06:43 +01001753int initio_xpad_out(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Alan Cox72d39fe2007-05-21 15:06:43 +01001755 struct scsi_ctrl_blk *scb = host->active;
1756 struct target_control *active_tc = host->active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Alan Cox72d39fe2007-05-21 15:06:43 +01001758 if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1759 scb->hastat = HOST_DO_DU; /* over run */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01001761 if (active_tc->js_period & TSC_WIDE_SCSI)
1762 outl(2, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 else
Alan Cox72d39fe2007-05-21 15:06:43 +01001764 outl(1, host->addr + TUL_SCnt0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Alan Cox72d39fe2007-05-21 15:06:43 +01001766 outb(0, host->addr + TUL_SFifo);
1767 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1768 if ((wait_tulip(host)) == -1)
1769 return -1;
1770 if (host->phase != DATA_OUT) { /* Disable wide CPU to allow read 16 bits */
1771 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1772 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1773 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775 }
1776}
1777
Alan Cox72d39fe2007-05-21 15:06:43 +01001778int initio_status_msg(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{ /* status & MSG_IN */
Alan Cox72d39fe2007-05-21 15:06:43 +01001780 struct scsi_ctrl_blk *scb = host->active;
1781 u8 msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Alan Cox72d39fe2007-05-21 15:06:43 +01001783 outb(TSC_CMD_COMP, host->addr + TUL_SCmd);
1784 if (wait_tulip(host) == -1)
1785 return -1;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /* get status */
Alan Cox72d39fe2007-05-21 15:06:43 +01001788 scb->tastat = inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Alan Cox72d39fe2007-05-21 15:06:43 +01001790 if (host->phase == MSG_OUT) {
1791 if (host->jsstatus0 & TSS_PAR_ERROR)
1792 outb(MSG_PARITY, host->addr + TUL_SFifo);
1793 else
1794 outb(MSG_NOP, host->addr + TUL_SFifo);
1795 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1796 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001798 if (host->phase == MSG_IN) {
1799 msg = inb(host->addr + TUL_SFifo);
1800 if (host->jsstatus0 & TSS_PAR_ERROR) { /* Parity error */
1801 if ((initio_msgin_accept(host)) == -1)
1802 return -1;
1803 if (host->phase != MSG_OUT)
1804 return initio_bad_seq(host);
1805 outb(MSG_PARITY, host->addr + TUL_SFifo);
1806 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1807 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
1809 if (msg == 0) { /* Command complete */
1810
Alan Cox72d39fe2007-05-21 15:06:43 +01001811 if ((scb->tastat & 0x18) == 0x10) /* No link support */
1812 return initio_bad_seq(host);
1813 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1814 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
1815 return initio_wait_done_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001818 if (msg == MSG_LINK_COMP || msg == MSG_LINK_FLAG) {
1819 if ((scb->tastat & 0x18) == 0x10)
1820 return initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001823 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827/* scsi bus free */
Alan Cox72d39fe2007-05-21 15:06:43 +01001828int int_initio_busfree(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Alan Cox72d39fe2007-05-21 15:06:43 +01001830 struct scsi_ctrl_blk *scb = host->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Alan Cox72d39fe2007-05-21 15:06:43 +01001832 if (scb != NULL) {
1833 if (scb->status & SCB_SELECT) { /* selection timeout */
1834 initio_unlink_pend_scb(host, scb);
1835 scb->hastat = HOST_SEL_TOUT;
1836 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 } else { /* Unexpected bus free */
Alan Cox72d39fe2007-05-21 15:06:43 +01001838 initio_unlink_busy_scb(host, scb);
1839 scb->hastat = HOST_BUS_FREE;
1840 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001842 host->active = NULL;
1843 host->active_tc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001845 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
1846 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
1847 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
1848 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
1851
Alan Cox72d39fe2007-05-21 15:06:43 +01001852/**
1853 * int_initio_scsi_rst - SCSI reset occurred
1854 * @host: Host seeing the reset
1855 *
1856 * A SCSI bus reset has occurred. Clean up any pending transfer
1857 * the hardware is doing by DMA and then abort all active and
1858 * disconnected commands. The mid layer should sort the rest out
1859 * for us
1860 */
1861
1862static int int_initio_scsi_rst(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Alan Cox72d39fe2007-05-21 15:06:43 +01001864 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 int i;
1866
1867 /* if DMA xfer is pending, abort DMA xfer */
Alan Cox72d39fe2007-05-21 15:06:43 +01001868 if (inb(host->addr + TUL_XStatus) & 0x01) {
1869 outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /* wait Abort DMA xfer done */
Alan Cox72d39fe2007-05-21 15:06:43 +01001871 while ((inb(host->addr + TUL_Int) & 0x04) == 0)
1872 cpu_relax();
1873 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875 /* Abort all active & disconnected scb */
Alan Cox72d39fe2007-05-21 15:06:43 +01001876 while ((scb = initio_pop_busy_scb(host)) != NULL) {
1877 scb->hastat = HOST_BAD_PHAS;
1878 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001880 host->active = NULL;
1881 host->active_tc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 /* clr sync nego. done flag */
Alan Cox72d39fe2007-05-21 15:06:43 +01001884 for (i = 0; i < host->max_tar; i++)
1885 host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1886 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
Alan Cox72d39fe2007-05-21 15:06:43 +01001889/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001890 * int_initio_scsi_resel - Reselection occurred
Alan Cox72d39fe2007-05-21 15:06:43 +01001891 * @host: InitIO host adapter
1892 *
1893 * A SCSI reselection event has been signalled and the interrupt
1894 * is now being processed. Work out which command block needs attention
1895 * and continue processing that command.
1896 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Alan Cox72d39fe2007-05-21 15:06:43 +01001898int int_initio_resel(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Alan Cox72d39fe2007-05-21 15:06:43 +01001900 struct scsi_ctrl_blk *scb;
1901 struct target_control *active_tc;
1902 u8 tag, msg = 0;
1903 u8 tar, lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Alan Cox72d39fe2007-05-21 15:06:43 +01001905 if ((scb = host->active) != NULL) {
1906 /* FIXME: Why check and not just clear ? */
1907 if (scb->status & SCB_SELECT) /* if waiting for selection complete */
1908 scb->status &= ~SCB_SELECT;
1909 host->active = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911 /* --------- get target id---------------------- */
Alan Cox72d39fe2007-05-21 15:06:43 +01001912 tar = inb(host->addr + TUL_SBusId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 /* ------ get LUN from Identify message----------- */
Alan Cox72d39fe2007-05-21 15:06:43 +01001914 lun = inb(host->addr + TUL_SIdent) & 0x0F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 /* 07/22/98 from 0x1F -> 0x0F */
Alan Cox72d39fe2007-05-21 15:06:43 +01001916 active_tc = &host->targets[tar];
1917 host->active_tc = active_tc;
1918 outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1919 outb(active_tc->js_period, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /* ------------- tag queueing ? ------------------- */
Alan Cox72d39fe2007-05-21 15:06:43 +01001922 if (active_tc->drv_flags & TCF_DRV_EN_TAG) {
1923 if ((initio_msgin_accept(host)) == -1)
1924 return -1;
1925 if (host->phase != MSG_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 goto no_tag;
Alan Cox72d39fe2007-05-21 15:06:43 +01001927 outl(1, host->addr + TUL_SCnt0);
1928 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1929 if (wait_tulip(host) == -1)
1930 return -1;
1931 msg = inb(host->addr + TUL_SFifo); /* Read Tag Message */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Alan Cox72d39fe2007-05-21 15:06:43 +01001933 if (msg < MSG_STAG || msg > MSG_OTAG) /* Is simple Tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 goto no_tag;
1935
Alan Cox72d39fe2007-05-21 15:06:43 +01001936 if (initio_msgin_accept(host) == -1)
1937 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Alan Cox72d39fe2007-05-21 15:06:43 +01001939 if (host->phase != MSG_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto no_tag;
1941
Alan Cox72d39fe2007-05-21 15:06:43 +01001942 outl(1, host->addr + TUL_SCnt0);
1943 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1944 if (wait_tulip(host) == -1)
1945 return -1;
1946 tag = inb(host->addr + TUL_SFifo); /* Read Tag ID */
1947 scb = host->scb + tag;
1948 if (scb->target != tar || scb->lun != lun) {
1949 return initio_msgout_abort_tag(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001951 if (scb->status != SCB_BUSY) { /* 03/24/95 */
1952 return initio_msgout_abort_tag(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001954 host->active = scb;
1955 if ((initio_msgin_accept(host)) == -1)
1956 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 } else { /* No tag */
1958 no_tag:
Alan Cox72d39fe2007-05-21 15:06:43 +01001959 if ((scb = initio_find_busy_scb(host, tar | (lun << 8))) == NULL) {
1960 return initio_msgout_abort_targ(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001962 host->active = scb;
1963 if (!(active_tc->drv_flags & TCF_DRV_EN_TAG)) {
1964 if ((initio_msgin_accept(host)) == -1)
1965 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967 }
1968 return 0;
1969}
1970
Alan Cox72d39fe2007-05-21 15:06:43 +01001971/**
1972 * int_initio_bad_seq - out of phase
1973 * @host: InitIO host flagging event
1974 *
1975 * We have ended up out of phase somehow. Reset the host controller
1976 * and throw all our toys out of the pram. Let the midlayer clean up
1977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Alan Cox72d39fe2007-05-21 15:06:43 +01001979static int int_initio_bad_seq(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{ /* target wrong phase */
Alan Cox72d39fe2007-05-21 15:06:43 +01001981 struct scsi_ctrl_blk *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 int i;
1983
Alan Cox72d39fe2007-05-21 15:06:43 +01001984 initio_reset_scsi(host, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Alan Cox72d39fe2007-05-21 15:06:43 +01001986 while ((scb = initio_pop_busy_scb(host)) != NULL) {
1987 scb->hastat = HOST_BAD_PHAS;
1988 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
Alan Cox72d39fe2007-05-21 15:06:43 +01001990 for (i = 0; i < host->max_tar; i++)
1991 host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1992 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995
Alan Cox72d39fe2007-05-21 15:06:43 +01001996/**
1997 * initio_msgout_abort_targ - abort a tag
1998 * @host: InitIO host
1999 *
2000 * Abort when the target/lun does not match or when our SCB is not
2001 * busy. Used by untagged commands.
2002 */
2003
2004static int initio_msgout_abort_targ(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006
Alan Cox72d39fe2007-05-21 15:06:43 +01002007 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2008 if (initio_msgin_accept(host) == -1)
2009 return -1;
2010 if (host->phase != MSG_OUT)
2011 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Alan Cox72d39fe2007-05-21 15:06:43 +01002013 outb(MSG_ABORT, host->addr + TUL_SFifo);
2014 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Alan Cox72d39fe2007-05-21 15:06:43 +01002016 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Alan Cox72d39fe2007-05-21 15:06:43 +01002019/**
2020 * initio_msgout_abort_tag - abort a tag
2021 * @host: InitIO host
2022 *
2023 * Abort when the target/lun does not match or when our SCB is not
2024 * busy. Used for tagged commands.
2025 */
2026
2027static int initio_msgout_abort_tag(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029
Alan Cox72d39fe2007-05-21 15:06:43 +01002030 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2031 if (initio_msgin_accept(host) == -1)
2032 return -1;
2033 if (host->phase != MSG_OUT)
2034 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Alan Cox72d39fe2007-05-21 15:06:43 +01002036 outb(MSG_ABORT_TAG, host->addr + TUL_SFifo);
2037 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Alan Cox72d39fe2007-05-21 15:06:43 +01002039 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041}
2042
Alan Cox72d39fe2007-05-21 15:06:43 +01002043/**
2044 * initio_msgin - Message in
2045 * @host: InitIO Host
2046 *
2047 * Process incoming message
2048 */
2049static int initio_msgin(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Alan Cox72d39fe2007-05-21 15:06:43 +01002051 struct target_control *active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 for (;;) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002054 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Alan Cox72d39fe2007-05-21 15:06:43 +01002056 outl(1, host->addr + TUL_SCnt0);
2057 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2058 if (wait_tulip(host) == -1)
2059 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Alan Cox72d39fe2007-05-21 15:06:43 +01002061 switch (inb(host->addr + TUL_SFifo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 case MSG_DISC: /* Disconnect msg */
Alan Cox72d39fe2007-05-21 15:06:43 +01002063 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2064 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 case MSG_SDP:
2066 case MSG_RESTORE:
2067 case MSG_NOP:
Alan Cox72d39fe2007-05-21 15:06:43 +01002068 initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 case MSG_REJ: /* Clear ATN first */
Alan Cox72d39fe2007-05-21 15:06:43 +01002071 outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)),
2072 host->addr + TUL_SSignal);
2073 active_tc = host->active_tc;
2074 if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) /* do sync nego */
2075 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN),
2076 host->addr + TUL_SSignal);
2077 initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 case MSG_EXTEND: /* extended msg */
Alan Cox72d39fe2007-05-21 15:06:43 +01002080 initio_msgin_extend(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 case MSG_IGNOREWIDE:
Alan Cox72d39fe2007-05-21 15:06:43 +01002083 initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 case MSG_COMP:
Alan Cox72d39fe2007-05-21 15:06:43 +01002086 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2087 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2088 return initio_wait_done_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01002090 initio_msgout_reject(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 break;
2092 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002093 if (host->phase != MSG_IN)
2094 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096 /* statement won't reach here */
2097}
2098
Alan Cox72d39fe2007-05-21 15:06:43 +01002099static int initio_msgout_reject(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Alan Cox72d39fe2007-05-21 15:06:43 +01002101 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Alan Cox72d39fe2007-05-21 15:06:43 +01002103 if (initio_msgin_accept(host) == -1)
2104 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Alan Cox72d39fe2007-05-21 15:06:43 +01002106 if (host->phase == MSG_OUT) {
2107 outb(MSG_REJ, host->addr + TUL_SFifo); /* Msg reject */
2108 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2109 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002111 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Alan Cox72d39fe2007-05-21 15:06:43 +01002114static int initio_msgout_ide(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Alan Cox72d39fe2007-05-21 15:06:43 +01002116 outb(MSG_IDE, host->addr + TUL_SFifo); /* Initiator Detected Error */
2117 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2118 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
Alan Cox72d39fe2007-05-21 15:06:43 +01002121static int initio_msgin_extend(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Alan Cox72d39fe2007-05-21 15:06:43 +01002123 u8 len, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Alan Cox72d39fe2007-05-21 15:06:43 +01002125 if (initio_msgin_accept(host) != MSG_IN)
2126 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /* Get extended msg length */
Alan Cox72d39fe2007-05-21 15:06:43 +01002129 outl(1, host->addr + TUL_SCnt0);
2130 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2131 if (wait_tulip(host) == -1)
2132 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Alan Cox72d39fe2007-05-21 15:06:43 +01002134 len = inb(host->addr + TUL_SFifo);
2135 host->msg[0] = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 for (idx = 1; len != 0; len--) {
2137
Alan Cox72d39fe2007-05-21 15:06:43 +01002138 if ((initio_msgin_accept(host)) != MSG_IN)
2139 return host->phase;
2140 outl(1, host->addr + TUL_SCnt0);
2141 outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2142 if (wait_tulip(host) == -1)
2143 return -1;
2144 host->msg[idx++] = inb(host->addr + TUL_SFifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002146 if (host->msg[1] == 1) { /* if it's synchronous data transfer request */
2147 u8 r;
2148 if (host->msg[0] != 3) /* if length is not right */
2149 return initio_msgout_reject(host);
2150 if (host->active_tc->flags & TCF_NO_SYNC_NEGO) { /* Set OFFSET=0 to do async, nego back */
2151 host->msg[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002153 if (initio_msgin_sync(host) == 0 &&
2154 (host->active_tc->flags & TCF_SYNC_DONE)) {
2155 initio_sync_done(host);
2156 return initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158 }
2159
Alan Cox72d39fe2007-05-21 15:06:43 +01002160 r = inb(host->addr + TUL_SSignal);
2161 outb((r & (TSC_SET_ACK | 7)) | TSC_SET_ATN,
2162 host->addr + TUL_SSignal);
2163 if (initio_msgin_accept(host) != MSG_OUT)
2164 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /* sync msg out */
Alan Cox72d39fe2007-05-21 15:06:43 +01002166 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Alan Cox72d39fe2007-05-21 15:06:43 +01002168 initio_sync_done(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Alan Cox72d39fe2007-05-21 15:06:43 +01002170 outb(MSG_EXTEND, host->addr + TUL_SFifo);
2171 outb(3, host->addr + TUL_SFifo);
2172 outb(1, host->addr + TUL_SFifo);
2173 outb(host->msg[2], host->addr + TUL_SFifo);
2174 outb(host->msg[3], host->addr + TUL_SFifo);
2175 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2176 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002178 if (host->msg[0] != 2 || host->msg[1] != 3)
2179 return initio_msgout_reject(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /* if it's WIDE DATA XFER REQ */
Alan Cox72d39fe2007-05-21 15:06:43 +01002181 if (host->active_tc->flags & TCF_NO_WDTR) {
2182 host->msg[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002184 if (host->msg[2] > 2) /* > 32 bits */
2185 return initio_msgout_reject(host);
2186 if (host->msg[2] == 2) { /* == 32 */
2187 host->msg[2] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002189 if ((host->active_tc->flags & TCF_NO_WDTR) == 0) {
2190 wdtr_done(host);
2191 if ((host->active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)
2192 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2193 return initio_msgin_accept(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195 }
2196 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002197 outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Alan Cox72d39fe2007-05-21 15:06:43 +01002199 if (initio_msgin_accept(host) != MSG_OUT)
2200 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 /* WDTR msg out */
Alan Cox72d39fe2007-05-21 15:06:43 +01002202 outb(MSG_EXTEND, host->addr + TUL_SFifo);
2203 outb(2, host->addr + TUL_SFifo);
2204 outb(3, host->addr + TUL_SFifo);
2205 outb(host->msg[2], host->addr + TUL_SFifo);
2206 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2207 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Alan Cox72d39fe2007-05-21 15:06:43 +01002210static int initio_msgin_sync(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 char default_period;
2213
Alan Cox72d39fe2007-05-21 15:06:43 +01002214 default_period = initio_rate_tbl[host->active_tc->flags & TCF_SCSI_RATE];
2215 if (host->msg[3] > MAX_OFFSET) {
2216 host->msg[3] = MAX_OFFSET;
2217 if (host->msg[2] < default_period) {
2218 host->msg[2] = default_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return 1;
2220 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002221 if (host->msg[2] >= 59) /* Change to async */
2222 host->msg[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return 1;
2224 }
2225 /* offset requests asynchronous transfers ? */
Alan Cox72d39fe2007-05-21 15:06:43 +01002226 if (host->msg[3] == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return 0;
2228 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002229 if (host->msg[2] < default_period) {
2230 host->msg[2] = default_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return 1;
2232 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002233 if (host->msg[2] >= 59) {
2234 host->msg[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return 1;
2236 }
2237 return 0;
2238}
2239
Alan Cox72d39fe2007-05-21 15:06:43 +01002240static int wdtr_done(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Alan Cox72d39fe2007-05-21 15:06:43 +01002242 host->active_tc->flags &= ~TCF_SYNC_DONE;
2243 host->active_tc->flags |= TCF_WDTR_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Alan Cox72d39fe2007-05-21 15:06:43 +01002245 host->active_tc->js_period = 0;
2246 if (host->msg[2]) /* if 16 bit */
2247 host->active_tc->js_period |= TSC_WIDE_SCSI;
2248 host->active_tc->sconfig0 &= ~TSC_ALT_PERIOD;
2249 outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2250 outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 return 1;
2253}
2254
Alan Cox72d39fe2007-05-21 15:06:43 +01002255static int initio_sync_done(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 int i;
2258
Alan Cox72d39fe2007-05-21 15:06:43 +01002259 host->active_tc->flags |= TCF_SYNC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Alan Cox72d39fe2007-05-21 15:06:43 +01002261 if (host->msg[3]) {
2262 host->active_tc->js_period |= host->msg[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 for (i = 0; i < 8; i++) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002264 if (initio_rate_tbl[i] >= host->msg[2]) /* pick the big one */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 break;
2266 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002267 host->active_tc->js_period |= (i << 4);
2268 host->active_tc->sconfig0 |= TSC_ALT_PERIOD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002270 outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2271 outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Alan Cox72d39fe2007-05-21 15:06:43 +01002273 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
2276
Alan Cox72d39fe2007-05-21 15:06:43 +01002277static int initio_post_scsi_rst(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Alan Cox72d39fe2007-05-21 15:06:43 +01002279 struct scsi_ctrl_blk *scb;
2280 struct target_control *active_tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 int i;
2282
Alan Cox72d39fe2007-05-21 15:06:43 +01002283 host->active = NULL;
2284 host->active_tc = NULL;
2285 host->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Alan Cox72d39fe2007-05-21 15:06:43 +01002287 while ((scb = initio_pop_busy_scb(host)) != NULL) {
2288 scb->hastat = HOST_BAD_PHAS;
2289 initio_append_done_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291 /* clear sync done flag */
Alan Cox72d39fe2007-05-21 15:06:43 +01002292 active_tc = &host->targets[0];
2293 for (i = 0; i < host->max_tar; active_tc++, i++) {
2294 active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /* Initialize the sync. xfer register values to an asyn xfer */
Alan Cox72d39fe2007-05-21 15:06:43 +01002296 active_tc->js_period = 0;
2297 active_tc->sconfig0 = host->sconf1;
2298 host->act_tags[0] = 0; /* 07/22/98 */
2299 host->targets[i].flags &= ~TCF_BUSY; /* 07/22/98 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 } /* for */
2301
Alan Cox72d39fe2007-05-21 15:06:43 +01002302 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Alan Cox72d39fe2007-05-21 15:06:43 +01002305static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Alan Cox72d39fe2007-05-21 15:06:43 +01002307 scb->status |= SCB_SELECT;
2308 scb->next_state = 0x1;
2309 host->active = scb;
2310 host->active_tc = &host->targets[scb->target];
2311 outb(TSC_SELATNSTOP, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
2314
Alan Cox72d39fe2007-05-21 15:06:43 +01002315static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
2317 int i;
2318
Alan Cox72d39fe2007-05-21 15:06:43 +01002319 scb->status |= SCB_SELECT;
2320 scb->next_state = 0x2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Alan Cox72d39fe2007-05-21 15:06:43 +01002322 outb(scb->ident, host->addr + TUL_SFifo);
2323 for (i = 0; i < (int) scb->cdblen; i++)
2324 outb(scb->cdb[i], host->addr + TUL_SFifo);
2325 host->active_tc = &host->targets[scb->target];
2326 host->active = scb;
2327 outb(TSC_SEL_ATN, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
Alan Cox72d39fe2007-05-21 15:06:43 +01002330static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
2332 int i;
2333
Alan Cox72d39fe2007-05-21 15:06:43 +01002334 scb->status |= SCB_SELECT;
2335 scb->next_state = 0x2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Alan Cox72d39fe2007-05-21 15:06:43 +01002337 outb(scb->ident, host->addr + TUL_SFifo);
2338 outb(scb->tagmsg, host->addr + TUL_SFifo);
2339 outb(scb->tagid, host->addr + TUL_SFifo);
2340 for (i = 0; i < scb->cdblen; i++)
2341 outb(scb->cdb[i], host->addr + TUL_SFifo);
2342 host->active_tc = &host->targets[scb->target];
2343 host->active = scb;
2344 outb(TSC_SEL_ATN3, host->addr + TUL_SCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346
Alan Cox72d39fe2007-05-21 15:06:43 +01002347/**
2348 * initio_bus_device_reset - SCSI Bus Device Reset
2349 * @host: InitIO host to reset
2350 *
2351 * Perform a device reset and abort all pending SCBs for the
2352 * victim device
2353 */
2354int initio_bus_device_reset(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Alan Cox72d39fe2007-05-21 15:06:43 +01002356 struct scsi_ctrl_blk *scb = host->active;
2357 struct target_control *active_tc = host->active_tc;
2358 struct scsi_ctrl_blk *tmp, *prev;
2359 u8 tar;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Alan Cox72d39fe2007-05-21 15:06:43 +01002361 if (host->phase != MSG_OUT)
2362 return int_initio_bad_seq(host); /* Unexpected phase */
2363
2364 initio_unlink_pend_scb(host, scb);
2365 initio_release_scb(host, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367
Alan Cox72d39fe2007-05-21 15:06:43 +01002368 tar = scb->target; /* target */
2369 active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE | TCF_BUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 /* clr sync. nego & WDTR flags 07/22/98 */
2371
2372 /* abort all SCB with same target */
Alan Cox72d39fe2007-05-21 15:06:43 +01002373 prev = tmp = host->first_busy; /* Check Busy queue */
2374 while (tmp != NULL) {
2375 if (tmp->target == tar) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 /* unlink it */
Alan Cox72d39fe2007-05-21 15:06:43 +01002377 if (tmp == host->first_busy) {
2378 if ((host->first_busy = tmp->next) == NULL)
2379 host->last_busy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002381 prev->next = tmp->next;
2382 if (tmp == host->last_busy)
2383 host->last_busy = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002385 tmp->hastat = HOST_ABORTED;
2386 initio_append_done_scb(host, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
2388 /* Previous haven't change */
2389 else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002390 prev = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002392 tmp = tmp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002394 outb(MSG_DEVRST, host->addr + TUL_SFifo);
2395 outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2396 return initio_wait_disc(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398}
2399
Alan Cox72d39fe2007-05-21 15:06:43 +01002400static int initio_msgin_accept(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
Alan Cox72d39fe2007-05-21 15:06:43 +01002402 outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2403 return wait_tulip(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
Alan Cox72d39fe2007-05-21 15:06:43 +01002406static int wait_tulip(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
2408
Alan Cox72d39fe2007-05-21 15:06:43 +01002409 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2410 & TSS_INT_PENDING))
2411 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Alan Cox72d39fe2007-05-21 15:06:43 +01002413 host->jsint = inb(host->addr + TUL_SInt);
2414 host->phase = host->jsstatus0 & TSS_PH_MASK;
2415 host->jsstatus1 = inb(host->addr + TUL_SStatus1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Alan Cox72d39fe2007-05-21 15:06:43 +01002417 if (host->jsint & TSS_RESEL_INT) /* if SCSI bus reset detected */
2418 return int_initio_resel(host);
2419 if (host->jsint & TSS_SEL_TIMEOUT) /* if selected/reselected timeout interrupt */
2420 return int_initio_busfree(host);
2421 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2422 return int_initio_scsi_rst(host);
2423
2424 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2425 if (host->flags & HCF_EXPECT_DONE_DISC) {
2426 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2427 initio_unlink_busy_scb(host, host->active);
2428 host->active->hastat = 0;
2429 initio_append_done_scb(host, host->active);
2430 host->active = NULL;
2431 host->active_tc = NULL;
2432 host->flags &= ~HCF_EXPECT_DONE_DISC;
2433 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2434 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2435 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002437 if (host->flags & HCF_EXPECT_DISC) {
2438 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2439 host->active = NULL;
2440 host->active_tc = NULL;
2441 host->flags &= ~HCF_EXPECT_DISC;
2442 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2443 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2444 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002446 return int_initio_busfree(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002448 /* The old code really does the below. Can probably be removed */
2449 if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV))
2450 return host->phase;
2451 return host->phase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
Alan Cox72d39fe2007-05-21 15:06:43 +01002453
2454static int initio_wait_disc(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Alan Cox72d39fe2007-05-21 15:06:43 +01002456 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING))
2457 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Alan Cox72d39fe2007-05-21 15:06:43 +01002459 host->jsint = inb(host->addr + TUL_SInt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Alan Cox72d39fe2007-05-21 15:06:43 +01002461 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2462 return int_initio_scsi_rst(host);
2463 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2464 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2465 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2466 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2467 host->active = NULL;
2468 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002470 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Alan Cox72d39fe2007-05-21 15:06:43 +01002473static int initio_wait_done_disc(struct initio_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
Alan Cox72d39fe2007-05-21 15:06:43 +01002475 while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2476 & TSS_INT_PENDING))
2477 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Alan Cox72d39fe2007-05-21 15:06:43 +01002479 host->jsint = inb(host->addr + TUL_SInt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Alan Cox72d39fe2007-05-21 15:06:43 +01002481 if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
2482 return int_initio_scsi_rst(host);
2483 if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
2484 outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2485 outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2486 outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
2487 initio_unlink_busy_scb(host, host->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Alan Cox72d39fe2007-05-21 15:06:43 +01002489 initio_append_done_scb(host, host->active);
2490 host->active = NULL;
2491 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002493 return initio_bad_seq(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Alan Cox72d39fe2007-05-21 15:06:43 +01002496/**
2497 * i91u_intr - IRQ handler
2498 * @irqno: IRQ number
2499 * @dev_id: IRQ identifier
2500 *
2501 * Take the relevant locks and then invoke the actual isr processing
2502 * code under the lock.
2503 */
2504
David Howells7d12e782006-10-05 14:55:46 +01002505static irqreturn_t i91u_intr(int irqno, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
2507 struct Scsi_Host *dev = dev_id;
2508 unsigned long flags;
Alan Cox72d39fe2007-05-21 15:06:43 +01002509 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 spin_lock_irqsave(dev->host_lock, flags);
Alan Cox72d39fe2007-05-21 15:06:43 +01002512 r = initio_isr((struct initio_host *)dev->hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 spin_unlock_irqrestore(dev->host_lock, flags);
Alan Cox72d39fe2007-05-21 15:06:43 +01002514 if (r)
2515 return IRQ_HANDLED;
2516 else
2517 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Alan Cox72d39fe2007-05-21 15:06:43 +01002521/**
2522 * initio_build_scb - Build the mappings and SCB
2523 * @host: InitIO host taking the command
2524 * @cblk: Firmware command block
2525 * @cmnd: SCSI midlayer command block
2526 *
2527 * Translate the abstract SCSI command into a firmware command block
2528 * suitable for feeding to the InitIO host controller. This also requires
2529 * we build the scatter gather lists and ensure they are mapped properly.
2530 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Alan Cox72d39fe2007-05-21 15:06:43 +01002532static void initio_build_scb(struct initio_host * host, struct scsi_ctrl_blk * cblk, struct scsi_cmnd * cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{ /* Create corresponding SCB */
Alan Cox72d39fe2007-05-21 15:06:43 +01002534 struct scatterlist *sglist;
2535 struct sg_entry *sg; /* Pointer to SG list */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002536 int i, nseg;
Alan Cox72d39fe2007-05-21 15:06:43 +01002537 long total_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 dma_addr_t dma_addr;
2539
Alan Cox72d39fe2007-05-21 15:06:43 +01002540 /* Fill in the command headers */
2541 cblk->post = i91uSCBPost; /* i91u's callback routine */
2542 cblk->srb = cmnd;
2543 cblk->opcode = ExecSCSI;
2544 cblk->flags = SCF_POST; /* After SCSI done, call post routine */
2545 cblk->target = cmnd->device->id;
2546 cblk->lun = cmnd->device->lun;
2547 cblk->ident = cmnd->device->lun | DISC_ALLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Alan Cox72d39fe2007-05-21 15:06:43 +01002549 cblk->flags |= SCF_SENSE; /* Turn on auto request sense */
2550
2551 /* Map the sense buffer into bus memory */
2552 dma_addr = dma_map_single(&host->pci_dev->dev, cmnd->sense_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 SENSE_SIZE, DMA_FROM_DEVICE);
Grant Grundler423eef62008-04-05 10:14:22 -06002554 cblk->senseptr = (u32)dma_addr;
2555 cblk->senselen = SENSE_SIZE;
Alan Cox72d39fe2007-05-21 15:06:43 +01002556 cmnd->SCp.ptr = (char *)(unsigned long)dma_addr;
2557 cblk->cdblen = cmnd->cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Alan Cox72d39fe2007-05-21 15:06:43 +01002559 /* Clear the returned status */
2560 cblk->hastat = 0;
2561 cblk->tastat = 0;
2562 /* Command the command */
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002563 memcpy(cblk->cdb, cmnd->cmnd, cmnd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Alan Cox72d39fe2007-05-21 15:06:43 +01002565 /* Set up tags */
2566 if (cmnd->device->tagged_supported) { /* Tag Support */
2567 cblk->tagmsg = SIMPLE_QUEUE_TAG; /* Do simple tag only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002569 cblk->tagmsg = 0; /* No tag support */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /* todo handle map_sg error */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002573 nseg = scsi_dma_map(cmnd);
2574 BUG_ON(nseg < 0);
2575 if (nseg) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002576 dma_addr = dma_map_single(&host->pci_dev->dev, &cblk->sglist[0],
2577 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 DMA_BIDIRECTIONAL);
Grant Grundler423eef62008-04-05 10:14:22 -06002579 cblk->bufptr = (u32)dma_addr;
Alan Cox72d39fe2007-05-21 15:06:43 +01002580 cmnd->SCp.dma_handle = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Stuart Swalese2d435e2008-01-23 20:00:48 +00002582 cblk->sglen = nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Alan Cox72d39fe2007-05-21 15:06:43 +01002584 cblk->flags |= SCF_SG; /* Turn on SG list flag */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002585 total_len = 0;
2586 sg = &cblk->sglist[0];
2587 scsi_for_each_sg(cmnd, sglist, cblk->sglen, i) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002588 sg->data = cpu_to_le32((u32)sg_dma_address(sglist));
Grant Grundler423eef62008-04-05 10:14:22 -06002589 sg->len = cpu_to_le32((u32)sg_dma_len(sglist));
2590 total_len += sg_dma_len(sglist);
Boaz Harrosha169e632007-12-17 18:08:59 +02002591 ++sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
2593
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002594 cblk->buflen = (scsi_bufflen(cmnd) > total_len) ?
2595 total_len : scsi_bufflen(cmnd);
Alan Cox72d39fe2007-05-21 15:06:43 +01002596 } else { /* No data transfer required */
2597 cblk->buflen = 0;
2598 cblk->sglen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 }
2600}
2601
Alan Cox72d39fe2007-05-21 15:06:43 +01002602/**
2603 * i91u_queuecommand - Queue a new command if possible
2604 * @cmd: SCSI command block from the mid layer
2605 * @done: Completion handler
2606 *
2607 * Attempts to queue a new command with the host adapter. Will return
2608 * zero if successful or indicate a host busy condition if not (which
2609 * will cause the mid layer to call us again later with the command)
2610 */
2611
Jeff Garzikf2812332010-11-16 02:10:29 -05002612static int i91u_queuecommand_lck(struct scsi_cmnd *cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 void (*done)(struct scsi_cmnd *))
2614{
Alan Cox72d39fe2007-05-21 15:06:43 +01002615 struct initio_host *host = (struct initio_host *) cmd->device->host->hostdata;
2616 struct scsi_ctrl_blk *cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 cmd->scsi_done = done;
2619
Alan Cox72d39fe2007-05-21 15:06:43 +01002620 cmnd = initio_alloc_scb(host);
2621 if (!cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return SCSI_MLQUEUE_HOST_BUSY;
2623
Alan Cox72d39fe2007-05-21 15:06:43 +01002624 initio_build_scb(host, cmnd, cmd);
2625 initio_exec_scb(host, cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 return 0;
2627}
2628
Jeff Garzikf2812332010-11-16 02:10:29 -05002629static DEF_SCSI_QCMD(i91u_queuecommand)
2630
Alan Cox72d39fe2007-05-21 15:06:43 +01002631/**
2632 * i91u_bus_reset - reset the SCSI bus
2633 * @cmnd: Command block we want to trigger the reset for
2634 *
2635 * Initiate a SCSI bus reset sequence
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 */
Alan Cox72d39fe2007-05-21 15:06:43 +01002637
2638static int i91u_bus_reset(struct scsi_cmnd * cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Alan Cox72d39fe2007-05-21 15:06:43 +01002640 struct initio_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Alan Cox72d39fe2007-05-21 15:06:43 +01002642 host = (struct initio_host *) cmnd->device->host->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Alan Cox72d39fe2007-05-21 15:06:43 +01002644 spin_lock_irq(cmnd->device->host->host_lock);
2645 initio_reset_scsi(host, 0);
2646 spin_unlock_irq(cmnd->device->host->host_lock);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return SUCCESS;
2649}
2650
Alan Cox72d39fe2007-05-21 15:06:43 +01002651/**
2652 * i91u_biospararm - return the "logical geometry
2653 * @sdev: SCSI device
2654 * @dev; Matching block device
2655 * @capacity: Sector size of drive
2656 * @info_array: Return space for BIOS geometry
2657 *
2658 * Map the device geometry in a manner compatible with the host
2659 * controller BIOS behaviour.
2660 *
2661 * FIXME: limited to 2^32 sector devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 */
Alan Cox72d39fe2007-05-21 15:06:43 +01002663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664static int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev,
2665 sector_t capacity, int *info_array)
2666{
Alan Cox72d39fe2007-05-21 15:06:43 +01002667 struct initio_host *host; /* Point to Host adapter control block */
2668 struct target_control *tc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Alan Cox72d39fe2007-05-21 15:06:43 +01002670 host = (struct initio_host *) sdev->host->hostdata;
2671 tc = &host->targets[sdev->id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Alan Cox72d39fe2007-05-21 15:06:43 +01002673 if (tc->heads) {
2674 info_array[0] = tc->heads;
2675 info_array[1] = tc->sectors;
2676 info_array[2] = (unsigned long)capacity / tc->heads / tc->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 } else {
Alan Cox72d39fe2007-05-21 15:06:43 +01002678 if (tc->drv_flags & TCF_DRV_255_63) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 info_array[0] = 255;
2680 info_array[1] = 63;
2681 info_array[2] = (unsigned long)capacity / 255 / 63;
2682 } else {
2683 info_array[0] = 64;
2684 info_array[1] = 32;
2685 info_array[2] = (unsigned long)capacity >> 11;
2686 }
2687 }
2688
2689#if defined(DEBUG_BIOSPARAM)
2690 if (i91u_debug & debug_biosparam) {
2691 printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
2692 info_array[0], info_array[1], info_array[2]);
2693 printk("WARNING: check, if the bios geometry is correct.\n");
2694 }
2695#endif
2696
2697 return 0;
2698}
2699
Alan Cox72d39fe2007-05-21 15:06:43 +01002700/**
2701 * i91u_unmap_scb - Unmap a command
2702 * @pci_dev: PCI device the command is for
2703 * @cmnd: The command itself
2704 *
2705 * Unmap any PCI mapping/IOMMU resources allocated when the command
2706 * was mapped originally as part of initio_build_scb
2707 */
2708
2709static void i91u_unmap_scb(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 /* auto sense buffer */
2712 if (cmnd->SCp.ptr) {
2713 dma_unmap_single(&pci_dev->dev,
2714 (dma_addr_t)((unsigned long)cmnd->SCp.ptr),
2715 SENSE_SIZE, DMA_FROM_DEVICE);
2716 cmnd->SCp.ptr = NULL;
2717 }
2718
2719 /* request buffer */
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002720 if (scsi_sg_count(cmnd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 dma_unmap_single(&pci_dev->dev, cmnd->SCp.dma_handle,
Alan Cox72d39fe2007-05-21 15:06:43 +01002722 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 DMA_BIDIRECTIONAL);
2724
FUJITA Tomonoria258c852007-05-21 14:58:30 +09002725 scsi_dma_unmap(cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 }
2727}
2728
Alan Cox72d39fe2007-05-21 15:06:43 +01002729/**
2730 * i91uSCBPost - SCSI callback
2731 * @host: Pointer to host adapter control block.
2732 * @cmnd: Pointer to SCSI control block.
2733 *
2734 * This is callback routine be called when tulip finish one
2735 * SCSI command.
2736 */
2737
2738static void i91uSCBPost(u8 * host_mem, u8 * cblk_mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739{
Alan Cox72d39fe2007-05-21 15:06:43 +01002740 struct scsi_cmnd *cmnd; /* Pointer to SCSI request block */
2741 struct initio_host *host;
2742 struct scsi_ctrl_blk *cblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Alan Cox72d39fe2007-05-21 15:06:43 +01002744 host = (struct initio_host *) host_mem;
2745 cblk = (struct scsi_ctrl_blk *) cblk_mem;
2746 if ((cmnd = cblk->srb) == NULL) {
2747 printk(KERN_ERR "i91uSCBPost: SRB pointer is empty\n");
2748 WARN_ON(1);
2749 initio_release_scb(host, cblk); /* Release SCB for current channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return;
2751 }
Alan Cox72d39fe2007-05-21 15:06:43 +01002752
2753 /*
2754 * Remap the firmware error status into a mid layer one
2755 */
2756 switch (cblk->hastat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 case 0x0:
2758 case 0xa: /* Linked command complete without error and linked normally */
2759 case 0xb: /* Linked command complete without error interrupt generated */
Alan Cox72d39fe2007-05-21 15:06:43 +01002760 cblk->hastat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 break;
2762
2763 case 0x11: /* Selection time out-The initiator selection or target
2764 reselection was not complete within the SCSI Time out period */
Alan Cox72d39fe2007-05-21 15:06:43 +01002765 cblk->hastat = DID_TIME_OUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 break;
2767
2768 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
2769 phase sequence was requested by the target. The host adapter
2770 will generate a SCSI Reset Condition, notifying the host with
2771 a SCRD interrupt */
Alan Cox72d39fe2007-05-21 15:06:43 +01002772 cblk->hastat = DID_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 break;
2774
2775 case 0x1a: /* SCB Aborted. 07/21/98 */
Alan Cox72d39fe2007-05-21 15:06:43 +01002776 cblk->hastat = DID_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 break;
2778
2779 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
2780 than was allocated by the Data Length field or the sum of the
2781 Scatter / Gather Data Length fields. */
2782 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
2783 case 0x16: /* Invalid SCB Operation Code. */
2784
2785 default:
Alan Cox72d39fe2007-05-21 15:06:43 +01002786 printk("ini9100u: %x %x\n", cblk->hastat, cblk->tastat);
2787 cblk->hastat = DID_ERROR; /* Couldn't find any better */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 break;
2789 }
2790
Alan Cox72d39fe2007-05-21 15:06:43 +01002791 cmnd->result = cblk->tastat | (cblk->hastat << 16);
Alan Cox72d39fe2007-05-21 15:06:43 +01002792 i91u_unmap_scb(host->pci_dev, cmnd);
2793 cmnd->scsi_done(cmnd); /* Notify system DONE */
2794 initio_release_scb(host, cblk); /* Release SCB for current channel */
2795}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Alan Cox72d39fe2007-05-21 15:06:43 +01002797static struct scsi_host_template initio_template = {
2798 .proc_name = "INI9100U",
2799 .name = "Initio INI-9X00U/UW SCSI device driver",
2800 .queuecommand = i91u_queuecommand,
2801 .eh_bus_reset_handler = i91u_bus_reset,
2802 .bios_param = i91u_biosparam,
2803 .can_queue = MAX_TARGETS * i91u_MAXQUEUE,
2804 .this_id = 1,
2805 .sg_tablesize = SG_ALL,
Alan Cox72d39fe2007-05-21 15:06:43 +01002806};
2807
2808static int initio_probe_one(struct pci_dev *pdev,
2809 const struct pci_device_id *id)
2810{
2811 struct Scsi_Host *shost;
2812 struct initio_host *host;
2813 u32 reg;
2814 u16 bios_seg;
2815 struct scsi_ctrl_blk *scb, *tmp, *prev = NULL /* silence gcc */;
2816 int num_scb, i, error;
2817
2818 error = pci_enable_device(pdev);
2819 if (error)
2820 return error;
2821
2822 pci_read_config_dword(pdev, 0x44, (u32 *) & reg);
2823 bios_seg = (u16) (reg & 0xFF);
2824 if (((reg & 0xFF00) >> 8) == 0xFF)
2825 reg = 0;
2826 bios_seg = (bios_seg << 8) + ((u16) ((reg & 0xFF00) >> 8));
2827
Christoph Hellwig663b4112018-10-18 15:10:19 +02002828 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
Alan Cox72d39fe2007-05-21 15:06:43 +01002829 printk(KERN_WARNING "i91u: Could not set 32 bit DMA mask\n");
2830 error = -ENODEV;
2831 goto out_disable_device;
2832 }
2833 shost = scsi_host_alloc(&initio_template, sizeof(struct initio_host));
2834 if (!shost) {
2835 printk(KERN_WARNING "initio: Could not allocate host structure.\n");
2836 error = -ENOMEM;
2837 goto out_disable_device;
2838 }
2839 host = (struct initio_host *)shost->hostdata;
2840 memset(host, 0, sizeof(struct initio_host));
Alan Cox99f1f532007-12-13 16:14:05 -08002841 host->addr = pci_resource_start(pdev, 0);
Stuart Swalese2d435e2008-01-23 20:00:48 +00002842 host->bios_addr = bios_seg;
Alan Cox72d39fe2007-05-21 15:06:43 +01002843
2844 if (!request_region(host->addr, 256, "i91u")) {
2845 printk(KERN_WARNING "initio: I/O port range 0x%x is busy.\n", host->addr);
2846 error = -ENODEV;
2847 goto out_host_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
2849
Alan Cox72d39fe2007-05-21 15:06:43 +01002850 if (initio_tag_enable) /* 1.01i */
2851 num_scb = MAX_TARGETS * i91u_MAXQUEUE;
2852 else
2853 num_scb = MAX_TARGETS + 3; /* 1-tape, 1-CD_ROM, 1- extra */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Alan Cox72d39fe2007-05-21 15:06:43 +01002855 for (; num_scb >= MAX_TARGETS + 3; num_scb--) {
2856 i = num_scb * sizeof(struct scsi_ctrl_blk);
2857 if ((scb = kzalloc(i, GFP_DMA)) != NULL)
2858 break;
2859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Alan Cox72d39fe2007-05-21 15:06:43 +01002861 if (!scb) {
2862 printk(KERN_WARNING "initio: Cannot allocate SCB array.\n");
2863 error = -ENOMEM;
2864 goto out_release_region;
2865 }
2866
Alan Coxe9e42fa2007-10-15 20:51:32 +01002867 host->pci_dev = pdev;
2868
Stuart Swalese2d435e2008-01-23 20:00:48 +00002869 host->semaph = 1;
2870 spin_lock_init(&host->semaph_lock);
Alan Cox72d39fe2007-05-21 15:06:43 +01002871 host->num_scbs = num_scb;
2872 host->scb = scb;
2873 host->next_pending = scb;
2874 host->next_avail = scb;
2875 for (i = 0, tmp = scb; i < num_scb; i++, tmp++) {
2876 tmp->tagid = i;
2877 if (i != 0)
2878 prev->next = tmp;
2879 prev = tmp;
2880 }
2881 prev->next = NULL;
2882 host->scb_end = tmp;
2883 host->first_avail = scb;
2884 host->last_avail = prev;
Alan Coxe9e42fa2007-10-15 20:51:32 +01002885 spin_lock_init(&host->avail_lock);
Alan Cox72d39fe2007-05-21 15:06:43 +01002886
Stuart Swalese2d435e2008-01-23 20:00:48 +00002887 initio_init(host, phys_to_virt(((u32)bios_seg << 4)));
Alan Cox72d39fe2007-05-21 15:06:43 +01002888
2889 host->jsstatus0 = 0;
2890
2891 shost->io_port = host->addr;
2892 shost->n_io_port = 0xff;
2893 shost->can_queue = num_scb; /* 03/05/98 */
2894 shost->unique_id = host->addr;
2895 shost->max_id = host->max_tar;
2896 shost->max_lun = 32; /* 10/21/97 */
2897 shost->irq = pdev->irq;
2898 shost->this_id = host->scsi_id; /* Assign HCS index */
2899 shost->base = host->addr;
2900 shost->sg_tablesize = TOTAL_SG_ENTRY;
2901
Michael Opdenacker4909cc22014-03-05 06:09:41 +01002902 error = request_irq(pdev->irq, i91u_intr, IRQF_SHARED, "i91u", shost);
Alan Cox72d39fe2007-05-21 15:06:43 +01002903 if (error < 0) {
2904 printk(KERN_WARNING "initio: Unable to request IRQ %d\n", pdev->irq);
2905 goto out_free_scbs;
2906 }
2907
2908 pci_set_drvdata(pdev, shost);
Alan Cox72d39fe2007-05-21 15:06:43 +01002909
2910 error = scsi_add_host(shost, &pdev->dev);
2911 if (error)
2912 goto out_free_irq;
2913 scsi_scan_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return 0;
Alan Cox72d39fe2007-05-21 15:06:43 +01002915out_free_irq:
2916 free_irq(pdev->irq, shost);
2917out_free_scbs:
2918 kfree(host->scb);
2919out_release_region:
2920 release_region(host->addr, 256);
2921out_host_put:
2922 scsi_host_put(shost);
2923out_disable_device:
2924 pci_disable_device(pdev);
2925 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Alan Cox72d39fe2007-05-21 15:06:43 +01002928/**
2929 * initio_remove_one - control shutdown
2930 * @pdev: PCI device being released
2931 *
2932 * Release the resources assigned to this adapter after it has
2933 * finished being used.
2934 */
2935
2936static void initio_remove_one(struct pci_dev *pdev)
2937{
2938 struct Scsi_Host *host = pci_get_drvdata(pdev);
2939 struct initio_host *s = (struct initio_host *)host->hostdata;
2940 scsi_remove_host(host);
2941 free_irq(pdev->irq, host);
2942 release_region(s->addr, 256);
2943 scsi_host_put(host);
2944 pci_disable_device(pdev);
2945}
2946
2947MODULE_LICENSE("GPL");
2948
2949static struct pci_device_id initio_pci_tbl[] = {
2950 {PCI_VENDOR_ID_INIT, 0x9500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2951 {PCI_VENDOR_ID_INIT, 0x9400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2952 {PCI_VENDOR_ID_INIT, 0x9401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2953 {PCI_VENDOR_ID_INIT, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2954 {PCI_VENDOR_ID_DOMEX, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2955 {0,}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956};
Alan Cox72d39fe2007-05-21 15:06:43 +01002957MODULE_DEVICE_TABLE(pci, initio_pci_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Alan Cox72d39fe2007-05-21 15:06:43 +01002959static struct pci_driver initio_pci_driver = {
2960 .name = "initio",
2961 .id_table = initio_pci_tbl,
2962 .probe = initio_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08002963 .remove = initio_remove_one,
Alan Cox72d39fe2007-05-21 15:06:43 +01002964};
2965
2966static int __init initio_init_driver(void)
2967{
2968 return pci_register_driver(&initio_pci_driver);
2969}
2970
2971static void __exit initio_exit_driver(void)
2972{
2973 pci_unregister_driver(&initio_pci_driver);
2974}
2975
2976MODULE_DESCRIPTION("Initio INI-9X00U/UW SCSI device driver");
2977MODULE_AUTHOR("Initio Corporation");
2978MODULE_LICENSE("GPL");
2979
2980module_init(initio_init_driver);
2981module_exit(initio_exit_driver);