xref: /OK3568_Linux_fs/kernel/drivers/scsi/a100u2w.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Initio A100 device driver for Linux.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (c) 1994-1998 Initio Corporation
5*4882a593Smuzhiyun  * Copyright (c) 2003-2004 Christoph Hellwig
6*4882a593Smuzhiyun  * All rights reserved.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
9*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
10*4882a593Smuzhiyun  * the Free Software Foundation; either version 2, or (at your option)
11*4882a593Smuzhiyun  * any later version.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful,
14*4882a593Smuzhiyun  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*4882a593Smuzhiyun  * GNU General Public License for more details.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
19*4882a593Smuzhiyun  * along with this program; see the file COPYING.  If not, write to
20*4882a593Smuzhiyun  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23*4882a593Smuzhiyun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*4882a593Smuzhiyun  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
26*4882a593Smuzhiyun  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*4882a593Smuzhiyun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*4882a593Smuzhiyun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*4882a593Smuzhiyun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*4882a593Smuzhiyun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*4882a593Smuzhiyun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*4882a593Smuzhiyun  * SUCH DAMAGE.
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * Revision History:
37*4882a593Smuzhiyun  * 07/02/98 hl	- v.91n Initial drivers.
38*4882a593Smuzhiyun  * 09/14/98 hl - v1.01 Support new Kernel.
39*4882a593Smuzhiyun  * 09/22/98 hl - v1.01a Support reset.
40*4882a593Smuzhiyun  * 09/24/98 hl - v1.01b Fixed reset.
41*4882a593Smuzhiyun  * 10/05/98 hl - v1.02 split the source code and release.
42*4882a593Smuzhiyun  * 12/19/98 bv - v1.02a Use spinlocks for 2.1.95 and up
43*4882a593Smuzhiyun  * 01/31/99 bv - v1.02b Use mdelay instead of waitForPause
44*4882a593Smuzhiyun  * 08/08/99 bv - v1.02c Use waitForPause again.
45*4882a593Smuzhiyun  * 06/25/02 Doug Ledford <dledford@redhat.com> - v1.02d
46*4882a593Smuzhiyun  *          - Remove limit on number of controllers
47*4882a593Smuzhiyun  *          - Port to DMA mapping API
48*4882a593Smuzhiyun  *          - Clean up interrupt handler registration
49*4882a593Smuzhiyun  *          - Fix memory leaks
50*4882a593Smuzhiyun  *          - Fix allocation of scsi host structs and private data
51*4882a593Smuzhiyun  * 11/18/03 Christoph Hellwig <hch@lst.de>
52*4882a593Smuzhiyun  *	    - Port to new probing API
53*4882a593Smuzhiyun  *	    - Fix some more leaks in init failure cases
54*4882a593Smuzhiyun  * 9/28/04 Christoph Hellwig <hch@lst.de>
55*4882a593Smuzhiyun  *	    - merge the two source files
56*4882a593Smuzhiyun  *	    - remove internal queueing code
57*4882a593Smuzhiyun  * 14/06/07 Alan Cox <alan@lxorguk.ukuu.org.uk>
58*4882a593Smuzhiyun  *	 - Grand cleanup and Linuxisation
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #include <linux/module.h>
62*4882a593Smuzhiyun #include <linux/errno.h>
63*4882a593Smuzhiyun #include <linux/delay.h>
64*4882a593Smuzhiyun #include <linux/interrupt.h>
65*4882a593Smuzhiyun #include <linux/pci.h>
66*4882a593Smuzhiyun #include <linux/init.h>
67*4882a593Smuzhiyun #include <linux/blkdev.h>
68*4882a593Smuzhiyun #include <linux/spinlock.h>
69*4882a593Smuzhiyun #include <linux/kernel.h>
70*4882a593Smuzhiyun #include <linux/string.h>
71*4882a593Smuzhiyun #include <linux/ioport.h>
72*4882a593Smuzhiyun #include <linux/dma-mapping.h>
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #include <asm/io.h>
75*4882a593Smuzhiyun #include <asm/irq.h>
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #include <scsi/scsi.h>
78*4882a593Smuzhiyun #include <scsi/scsi_cmnd.h>
79*4882a593Smuzhiyun #include <scsi/scsi_device.h>
80*4882a593Smuzhiyun #include <scsi/scsi_host.h>
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #include "a100u2w.h"
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun static struct orc_scb *__orc_alloc_scb(struct orc_host * host);
86*4882a593Smuzhiyun static void inia100_scb_handler(struct orc_host *host, struct orc_scb *scb);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun static struct orc_nvram nvram, *nvramp = &nvram;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun static u8 default_nvram[64] =
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun /*----------header -------------*/
93*4882a593Smuzhiyun 	0x01,			/* 0x00: Sub System Vendor ID 0 */
94*4882a593Smuzhiyun 	0x11,			/* 0x01: Sub System Vendor ID 1 */
95*4882a593Smuzhiyun 	0x60,			/* 0x02: Sub System ID 0        */
96*4882a593Smuzhiyun 	0x10,			/* 0x03: Sub System ID 1        */
97*4882a593Smuzhiyun 	0x00,			/* 0x04: SubClass               */
98*4882a593Smuzhiyun 	0x01,			/* 0x05: Vendor ID 0            */
99*4882a593Smuzhiyun 	0x11,			/* 0x06: Vendor ID 1            */
100*4882a593Smuzhiyun 	0x60,			/* 0x07: Device ID 0            */
101*4882a593Smuzhiyun 	0x10,			/* 0x08: Device ID 1            */
102*4882a593Smuzhiyun 	0x00,			/* 0x09: Reserved               */
103*4882a593Smuzhiyun 	0x00,			/* 0x0A: Reserved               */
104*4882a593Smuzhiyun 	0x01,			/* 0x0B: Revision of Data Structure     */
105*4882a593Smuzhiyun 				/* -- Host Adapter Structure --- */
106*4882a593Smuzhiyun 	0x01,			/* 0x0C: Number Of SCSI Channel */
107*4882a593Smuzhiyun 	0x01,			/* 0x0D: BIOS Configuration 1   */
108*4882a593Smuzhiyun 	0x00,			/* 0x0E: BIOS Configuration 2   */
109*4882a593Smuzhiyun 	0x00,			/* 0x0F: BIOS Configuration 3   */
110*4882a593Smuzhiyun 				/* --- SCSI Channel 0 Configuration --- */
111*4882a593Smuzhiyun 	0x07,			/* 0x10: H/A ID                 */
112*4882a593Smuzhiyun 	0x83,			/* 0x11: Channel Configuration  */
113*4882a593Smuzhiyun 	0x20,			/* 0x12: MAX TAG per target     */
114*4882a593Smuzhiyun 	0x0A,			/* 0x13: SCSI Reset Recovering time     */
115*4882a593Smuzhiyun 	0x00,			/* 0x14: Channel Configuration4 */
116*4882a593Smuzhiyun 	0x00,			/* 0x15: Channel Configuration5 */
117*4882a593Smuzhiyun 				/* SCSI Channel 0 Target Configuration  */
118*4882a593Smuzhiyun 				/* 0x16-0x25                    */
119*4882a593Smuzhiyun 	0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8,
120*4882a593Smuzhiyun 	0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8,
121*4882a593Smuzhiyun 				/* --- SCSI Channel 1 Configuration --- */
122*4882a593Smuzhiyun 	0x07,			/* 0x26: H/A ID                 */
123*4882a593Smuzhiyun 	0x83,			/* 0x27: Channel Configuration  */
124*4882a593Smuzhiyun 	0x20,			/* 0x28: MAX TAG per target     */
125*4882a593Smuzhiyun 	0x0A,			/* 0x29: SCSI Reset Recovering time     */
126*4882a593Smuzhiyun 	0x00,			/* 0x2A: Channel Configuration4 */
127*4882a593Smuzhiyun 	0x00,			/* 0x2B: Channel Configuration5 */
128*4882a593Smuzhiyun 				/* SCSI Channel 1 Target Configuration  */
129*4882a593Smuzhiyun 				/* 0x2C-0x3B                    */
130*4882a593Smuzhiyun 	0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8,
131*4882a593Smuzhiyun 	0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8,
132*4882a593Smuzhiyun 	0x00,			/* 0x3C: Reserved               */
133*4882a593Smuzhiyun 	0x00,			/* 0x3D: Reserved               */
134*4882a593Smuzhiyun 	0x00,			/* 0x3E: Reserved               */
135*4882a593Smuzhiyun 	0x00			/* 0x3F: Checksum               */
136*4882a593Smuzhiyun };
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 
wait_chip_ready(struct orc_host * host)139*4882a593Smuzhiyun static u8 wait_chip_ready(struct orc_host * host)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	int i;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	for (i = 0; i < 10; i++) {	/* Wait 1 second for report timeout     */
144*4882a593Smuzhiyun 		if (inb(host->base + ORC_HCTRL) & HOSTSTOP)	/* Wait HOSTSTOP set */
145*4882a593Smuzhiyun 			return 1;
146*4882a593Smuzhiyun 		msleep(100);
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun 	return 0;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
wait_firmware_ready(struct orc_host * host)151*4882a593Smuzhiyun static u8 wait_firmware_ready(struct orc_host * host)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	int i;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	for (i = 0; i < 10; i++) {	/* Wait 1 second for report timeout     */
156*4882a593Smuzhiyun 		if (inb(host->base + ORC_HSTUS) & RREADY)		/* Wait READY set */
157*4882a593Smuzhiyun 			return 1;
158*4882a593Smuzhiyun 		msleep(100);	/* wait 100ms before try again  */
159*4882a593Smuzhiyun 	}
160*4882a593Smuzhiyun 	return 0;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /***************************************************************************/
wait_scsi_reset_done(struct orc_host * host)164*4882a593Smuzhiyun static u8 wait_scsi_reset_done(struct orc_host * host)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	int i;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	for (i = 0; i < 10; i++) {	/* Wait 1 second for report timeout     */
169*4882a593Smuzhiyun 		if (!(inb(host->base + ORC_HCTRL) & SCSIRST))	/* Wait SCSIRST done */
170*4882a593Smuzhiyun 			return 1;
171*4882a593Smuzhiyun 		mdelay(100);	/* wait 100ms before try again  */
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun 	return 0;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /***************************************************************************/
wait_HDO_off(struct orc_host * host)177*4882a593Smuzhiyun static u8 wait_HDO_off(struct orc_host * host)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	int i;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	for (i = 0; i < 10; i++) {	/* Wait 1 second for report timeout     */
182*4882a593Smuzhiyun 		if (!(inb(host->base + ORC_HCTRL) & HDO))		/* Wait HDO off */
183*4882a593Smuzhiyun 			return 1;
184*4882a593Smuzhiyun 		mdelay(100);	/* wait 100ms before try again  */
185*4882a593Smuzhiyun 	}
186*4882a593Smuzhiyun 	return 0;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /***************************************************************************/
wait_hdi_set(struct orc_host * host,u8 * data)190*4882a593Smuzhiyun static u8 wait_hdi_set(struct orc_host * host, u8 * data)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	int i;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	for (i = 0; i < 10; i++) {	/* Wait 1 second for report timeout     */
195*4882a593Smuzhiyun 		if ((*data = inb(host->base + ORC_HSTUS)) & HDI)
196*4882a593Smuzhiyun 			return 1;	/* Wait HDI set */
197*4882a593Smuzhiyun 		mdelay(100);	/* wait 100ms before try again  */
198*4882a593Smuzhiyun 	}
199*4882a593Smuzhiyun 	return 0;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun /***************************************************************************/
orc_read_fwrev(struct orc_host * host)203*4882a593Smuzhiyun static unsigned short orc_read_fwrev(struct orc_host * host)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	u16 version;
206*4882a593Smuzhiyun 	u8 data;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	outb(ORC_CMD_VERSION, host->base + ORC_HDATA);
209*4882a593Smuzhiyun 	outb(HDO, host->base + ORC_HCTRL);
210*4882a593Smuzhiyun 	if (wait_HDO_off(host) == 0)	/* Wait HDO off   */
211*4882a593Smuzhiyun 		return 0;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (wait_hdi_set(host, &data) == 0)	/* Wait HDI set   */
214*4882a593Smuzhiyun 		return 0;
215*4882a593Smuzhiyun 	version = inb(host->base + ORC_HDATA);
216*4882a593Smuzhiyun 	outb(data, host->base + ORC_HSTUS);	/* Clear HDI            */
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	if (wait_hdi_set(host, &data) == 0)	/* Wait HDI set   */
219*4882a593Smuzhiyun 		return 0;
220*4882a593Smuzhiyun 	version |= inb(host->base + ORC_HDATA) << 8;
221*4882a593Smuzhiyun 	outb(data, host->base + ORC_HSTUS);	/* Clear HDI            */
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	return version;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /***************************************************************************/
orc_nv_write(struct orc_host * host,unsigned char address,unsigned char value)227*4882a593Smuzhiyun static u8 orc_nv_write(struct orc_host * host, unsigned char address, unsigned char value)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	outb(ORC_CMD_SET_NVM, host->base + ORC_HDATA);	/* Write command */
230*4882a593Smuzhiyun 	outb(HDO, host->base + ORC_HCTRL);
231*4882a593Smuzhiyun 	if (wait_HDO_off(host) == 0)	/* Wait HDO off   */
232*4882a593Smuzhiyun 		return 0;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	outb(address, host->base + ORC_HDATA);	/* Write address */
235*4882a593Smuzhiyun 	outb(HDO, host->base + ORC_HCTRL);
236*4882a593Smuzhiyun 	if (wait_HDO_off(host) == 0)	/* Wait HDO off   */
237*4882a593Smuzhiyun 		return 0;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	outb(value, host->base + ORC_HDATA);	/* Write value  */
240*4882a593Smuzhiyun 	outb(HDO, host->base + ORC_HCTRL);
241*4882a593Smuzhiyun 	if (wait_HDO_off(host) == 0)	/* Wait HDO off   */
242*4882a593Smuzhiyun 		return 0;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	return 1;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun /***************************************************************************/
orc_nv_read(struct orc_host * host,u8 address,u8 * ptr)248*4882a593Smuzhiyun static u8 orc_nv_read(struct orc_host * host, u8 address, u8 *ptr)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun 	unsigned char data;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	outb(ORC_CMD_GET_NVM, host->base + ORC_HDATA);	/* Write command */
253*4882a593Smuzhiyun 	outb(HDO, host->base + ORC_HCTRL);
254*4882a593Smuzhiyun 	if (wait_HDO_off(host) == 0)	/* Wait HDO off   */
255*4882a593Smuzhiyun 		return 0;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	outb(address, host->base + ORC_HDATA);	/* Write address */
258*4882a593Smuzhiyun 	outb(HDO, host->base + ORC_HCTRL);
259*4882a593Smuzhiyun 	if (wait_HDO_off(host) == 0)	/* Wait HDO off   */
260*4882a593Smuzhiyun 		return 0;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	if (wait_hdi_set(host, &data) == 0)	/* Wait HDI set   */
263*4882a593Smuzhiyun 		return 0;
264*4882a593Smuzhiyun 	*ptr = inb(host->base + ORC_HDATA);
265*4882a593Smuzhiyun 	outb(data, host->base + ORC_HSTUS);	/* Clear HDI    */
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	return 1;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun /**
272*4882a593Smuzhiyun  *	orc_exec_sb		-	Queue an SCB with the HA
273*4882a593Smuzhiyun  *	@host: host adapter the SCB belongs to
274*4882a593Smuzhiyun  *	@scb: SCB to queue for execution
275*4882a593Smuzhiyun  */
276*4882a593Smuzhiyun 
orc_exec_scb(struct orc_host * host,struct orc_scb * scb)277*4882a593Smuzhiyun static void orc_exec_scb(struct orc_host * host, struct orc_scb * scb)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	scb->status = ORCSCB_POST;
280*4882a593Smuzhiyun 	outb(scb->scbidx, host->base + ORC_PQUEUE);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun /**
285*4882a593Smuzhiyun  *	se2_rd_all	-	read SCSI parameters from EEPROM
286*4882a593Smuzhiyun  *	@host: Host whose EEPROM is being loaded
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  *	Read SCSI H/A configuration parameters from serial EEPROM
289*4882a593Smuzhiyun  */
290*4882a593Smuzhiyun 
se2_rd_all(struct orc_host * host)291*4882a593Smuzhiyun static int se2_rd_all(struct orc_host * host)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	int i;
294*4882a593Smuzhiyun 	u8 *np, chksum = 0;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	np = (u8 *) nvramp;
297*4882a593Smuzhiyun 	for (i = 0; i < 64; i++, np++) {	/* <01> */
298*4882a593Smuzhiyun 		if (orc_nv_read(host, (u8) i, np) == 0)
299*4882a593Smuzhiyun 			return -1;
300*4882a593Smuzhiyun 	}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	/*------ Is ckecksum ok ? ------*/
303*4882a593Smuzhiyun 	np = (u8 *) nvramp;
304*4882a593Smuzhiyun 	for (i = 0; i < 63; i++)
305*4882a593Smuzhiyun 		chksum += *np++;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	if (nvramp->CheckSum != (u8) chksum)
308*4882a593Smuzhiyun 		return -1;
309*4882a593Smuzhiyun 	return 1;
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun /**
313*4882a593Smuzhiyun  *	se2_update_all		-	update the EEPROM
314*4882a593Smuzhiyun  *	@host: Host whose EEPROM is being updated
315*4882a593Smuzhiyun  *
316*4882a593Smuzhiyun  *	Update changed bytes in the EEPROM image.
317*4882a593Smuzhiyun  */
318*4882a593Smuzhiyun 
se2_update_all(struct orc_host * host)319*4882a593Smuzhiyun static void se2_update_all(struct orc_host * host)
320*4882a593Smuzhiyun {				/* setup default pattern  */
321*4882a593Smuzhiyun 	int i;
322*4882a593Smuzhiyun 	u8 *np, *np1, chksum = 0;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	/* Calculate checksum first   */
325*4882a593Smuzhiyun 	np = (u8 *) default_nvram;
326*4882a593Smuzhiyun 	for (i = 0; i < 63; i++)
327*4882a593Smuzhiyun 		chksum += *np++;
328*4882a593Smuzhiyun 	*np = chksum;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	np = (u8 *) default_nvram;
331*4882a593Smuzhiyun 	np1 = (u8 *) nvramp;
332*4882a593Smuzhiyun 	for (i = 0; i < 64; i++, np++, np1++) {
333*4882a593Smuzhiyun 		if (*np != *np1)
334*4882a593Smuzhiyun 			orc_nv_write(host, (u8) i, *np);
335*4882a593Smuzhiyun 	}
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /**
339*4882a593Smuzhiyun  *	read_eeprom		-	load EEPROM
340*4882a593Smuzhiyun  *	@host: Host EEPROM to read
341*4882a593Smuzhiyun  *
342*4882a593Smuzhiyun  *	Read the EEPROM for a given host. If it is invalid or fails
343*4882a593Smuzhiyun  *	the restore the defaults and use them.
344*4882a593Smuzhiyun  */
345*4882a593Smuzhiyun 
read_eeprom(struct orc_host * host)346*4882a593Smuzhiyun static void read_eeprom(struct orc_host * host)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	if (se2_rd_all(host) != 1) {
349*4882a593Smuzhiyun 		se2_update_all(host);	/* setup default pattern        */
350*4882a593Smuzhiyun 		se2_rd_all(host);	/* load again                   */
351*4882a593Smuzhiyun 	}
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun /**
356*4882a593Smuzhiyun  *	orc_load_firmware	-	initialise firmware
357*4882a593Smuzhiyun  *	@host: Host to set up
358*4882a593Smuzhiyun  *
359*4882a593Smuzhiyun  *	Load the firmware from the EEPROM into controller SRAM. This
360*4882a593Smuzhiyun  *	is basically a 4K block copy and then a 4K block read to check
361*4882a593Smuzhiyun  *	correctness. The rest is convulted by the indirect interfaces
362*4882a593Smuzhiyun  *	in the hardware
363*4882a593Smuzhiyun  */
364*4882a593Smuzhiyun 
orc_load_firmware(struct orc_host * host)365*4882a593Smuzhiyun static u8 orc_load_firmware(struct orc_host * host)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun 	u32 data32;
368*4882a593Smuzhiyun 	u16 bios_addr;
369*4882a593Smuzhiyun 	u16 i;
370*4882a593Smuzhiyun 	u8 *data32_ptr, data;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	/* Set up the EEPROM for access */
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	data = inb(host->base + ORC_GCFG);
376*4882a593Smuzhiyun 	outb(data | EEPRG, host->base + ORC_GCFG);	/* Enable EEPROM programming */
377*4882a593Smuzhiyun 	outb(0x00, host->base + ORC_EBIOSADR2);
378*4882a593Smuzhiyun 	outw(0x0000, host->base + ORC_EBIOSADR0);
379*4882a593Smuzhiyun 	if (inb(host->base + ORC_EBIOSDATA) != 0x55) {
380*4882a593Smuzhiyun 		outb(data, host->base + ORC_GCFG);	/* Disable EEPROM programming */
381*4882a593Smuzhiyun 		return 0;
382*4882a593Smuzhiyun 	}
383*4882a593Smuzhiyun 	outw(0x0001, host->base + ORC_EBIOSADR0);
384*4882a593Smuzhiyun 	if (inb(host->base + ORC_EBIOSDATA) != 0xAA) {
385*4882a593Smuzhiyun 		outb(data, host->base + ORC_GCFG);	/* Disable EEPROM programming */
386*4882a593Smuzhiyun 		return 0;
387*4882a593Smuzhiyun 	}
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	outb(PRGMRST | DOWNLOAD, host->base + ORC_RISCCTL);	/* Enable SRAM programming */
390*4882a593Smuzhiyun 	data32_ptr = (u8 *) & data32;
391*4882a593Smuzhiyun 	data32 = cpu_to_le32(0);		/* Initial FW address to 0 */
392*4882a593Smuzhiyun 	outw(0x0010, host->base + ORC_EBIOSADR0);
393*4882a593Smuzhiyun 	*data32_ptr = inb(host->base + ORC_EBIOSDATA);		/* Read from BIOS */
394*4882a593Smuzhiyun 	outw(0x0011, host->base + ORC_EBIOSADR0);
395*4882a593Smuzhiyun 	*(data32_ptr + 1) = inb(host->base + ORC_EBIOSDATA);	/* Read from BIOS */
396*4882a593Smuzhiyun 	outw(0x0012, host->base + ORC_EBIOSADR0);
397*4882a593Smuzhiyun 	*(data32_ptr + 2) = inb(host->base + ORC_EBIOSDATA);	/* Read from BIOS */
398*4882a593Smuzhiyun 	outw(*(data32_ptr + 2), host->base + ORC_EBIOSADR2);
399*4882a593Smuzhiyun 	outl(le32_to_cpu(data32), host->base + ORC_FWBASEADR);		/* Write FW address */
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	/* Copy the code from the BIOS to the SRAM */
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	udelay(500);	/* Required on Sun Ultra 5 ... 350 -> failures */
404*4882a593Smuzhiyun 	bios_addr = (u16) le32_to_cpu(data32);	/* FW code locate at BIOS address + ? */
405*4882a593Smuzhiyun 	for (i = 0, data32_ptr = (u8 *) & data32;	/* Download the code    */
406*4882a593Smuzhiyun 	     i < 0x1000;	/* Firmware code size = 4K      */
407*4882a593Smuzhiyun 	     i++, bios_addr++) {
408*4882a593Smuzhiyun 		outw(bios_addr, host->base + ORC_EBIOSADR0);
409*4882a593Smuzhiyun 		*data32_ptr++ = inb(host->base + ORC_EBIOSDATA);	/* Read from BIOS */
410*4882a593Smuzhiyun 		if ((i % 4) == 3) {
411*4882a593Smuzhiyun 			outl(le32_to_cpu(data32), host->base + ORC_RISCRAM);	/* Write every 4 bytes */
412*4882a593Smuzhiyun 			data32_ptr = (u8 *) & data32;
413*4882a593Smuzhiyun 		}
414*4882a593Smuzhiyun 	}
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	/* Go back and check they match */
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	outb(PRGMRST | DOWNLOAD, host->base + ORC_RISCCTL);	/* Reset program count 0 */
419*4882a593Smuzhiyun 	bios_addr -= 0x1000;	/* Reset the BIOS address */
420*4882a593Smuzhiyun 	for (i = 0, data32_ptr = (u8 *) & data32;	/* Check the code       */
421*4882a593Smuzhiyun 	     i < 0x1000;	/* Firmware code size = 4K      */
422*4882a593Smuzhiyun 	     i++, bios_addr++) {
423*4882a593Smuzhiyun 		outw(bios_addr, host->base + ORC_EBIOSADR0);
424*4882a593Smuzhiyun 		*data32_ptr++ = inb(host->base + ORC_EBIOSDATA);	/* Read from BIOS */
425*4882a593Smuzhiyun 		if ((i % 4) == 3) {
426*4882a593Smuzhiyun 			if (inl(host->base + ORC_RISCRAM) != le32_to_cpu(data32)) {
427*4882a593Smuzhiyun 				outb(PRGMRST, host->base + ORC_RISCCTL);	/* Reset program to 0 */
428*4882a593Smuzhiyun 				outb(data, host->base + ORC_GCFG);	/*Disable EEPROM programming */
429*4882a593Smuzhiyun 				return 0;
430*4882a593Smuzhiyun 			}
431*4882a593Smuzhiyun 			data32_ptr = (u8 *) & data32;
432*4882a593Smuzhiyun 		}
433*4882a593Smuzhiyun 	}
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	/* Success */
436*4882a593Smuzhiyun 	outb(PRGMRST, host->base + ORC_RISCCTL);	/* Reset program to 0   */
437*4882a593Smuzhiyun 	outb(data, host->base + ORC_GCFG);	/* Disable EEPROM programming */
438*4882a593Smuzhiyun 	return 1;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun /***************************************************************************/
setup_SCBs(struct orc_host * host)442*4882a593Smuzhiyun static void setup_SCBs(struct orc_host * host)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun 	struct orc_scb *scb;
445*4882a593Smuzhiyun 	int i;
446*4882a593Smuzhiyun 	struct orc_extended_scb *escb;
447*4882a593Smuzhiyun 	dma_addr_t escb_phys;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	/* Setup SCB base and SCB Size registers */
450*4882a593Smuzhiyun 	outb(ORC_MAXQUEUE, host->base + ORC_SCBSIZE);	/* Total number of SCBs */
451*4882a593Smuzhiyun 	/* SCB base address 0      */
452*4882a593Smuzhiyun 	outl(host->scb_phys, host->base + ORC_SCBBASE0);
453*4882a593Smuzhiyun 	/* SCB base address 1      */
454*4882a593Smuzhiyun 	outl(host->scb_phys, host->base + ORC_SCBBASE1);
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	/* setup scatter list address with one buffer */
457*4882a593Smuzhiyun 	scb = host->scb_virt;
458*4882a593Smuzhiyun 	escb = host->escb_virt;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	for (i = 0; i < ORC_MAXQUEUE; i++) {
461*4882a593Smuzhiyun 		escb_phys = (host->escb_phys + (sizeof(struct orc_extended_scb) * i));
462*4882a593Smuzhiyun 		scb->sg_addr = cpu_to_le32((u32) escb_phys);
463*4882a593Smuzhiyun 		scb->sense_addr = cpu_to_le32((u32) escb_phys);
464*4882a593Smuzhiyun 		scb->escb = escb;
465*4882a593Smuzhiyun 		scb->scbidx = i;
466*4882a593Smuzhiyun 		scb++;
467*4882a593Smuzhiyun 		escb++;
468*4882a593Smuzhiyun 	}
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun /**
472*4882a593Smuzhiyun  *	init_alloc_map		-	initialise allocation map
473*4882a593Smuzhiyun  *	@host: host map to configure
474*4882a593Smuzhiyun  *
475*4882a593Smuzhiyun  *	Initialise the allocation maps for this device. If the device
476*4882a593Smuzhiyun  *	is not quiescent the caller must hold the allocation lock
477*4882a593Smuzhiyun  */
478*4882a593Smuzhiyun 
init_alloc_map(struct orc_host * host)479*4882a593Smuzhiyun static void init_alloc_map(struct orc_host * host)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 	u8 i, j;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	for (i = 0; i < MAX_CHANNELS; i++) {
484*4882a593Smuzhiyun 		for (j = 0; j < 8; j++) {
485*4882a593Smuzhiyun 			host->allocation_map[i][j] = 0xffffffff;
486*4882a593Smuzhiyun 		}
487*4882a593Smuzhiyun 	}
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun /**
491*4882a593Smuzhiyun  *	init_orchid		-	initialise the host adapter
492*4882a593Smuzhiyun  *	@host:host adapter to initialise
493*4882a593Smuzhiyun  *
494*4882a593Smuzhiyun  *	Initialise the controller and if necessary load the firmware.
495*4882a593Smuzhiyun  *
496*4882a593Smuzhiyun  *	Returns -1 if the initialisation fails.
497*4882a593Smuzhiyun  */
498*4882a593Smuzhiyun 
init_orchid(struct orc_host * host)499*4882a593Smuzhiyun static int init_orchid(struct orc_host * host)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	u8 *ptr;
502*4882a593Smuzhiyun 	u16 revision;
503*4882a593Smuzhiyun 	u8 i;
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	init_alloc_map(host);
506*4882a593Smuzhiyun 	outb(0xFF, host->base + ORC_GIMSK);	/* Disable all interrupts */
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	if (inb(host->base + ORC_HSTUS) & RREADY) {	/* Orchid is ready */
509*4882a593Smuzhiyun 		revision = orc_read_fwrev(host);
510*4882a593Smuzhiyun 		if (revision == 0xFFFF) {
511*4882a593Smuzhiyun 			outb(DEVRST, host->base + ORC_HCTRL);	/* Reset Host Adapter   */
512*4882a593Smuzhiyun 			if (wait_chip_ready(host) == 0)
513*4882a593Smuzhiyun 				return -1;
514*4882a593Smuzhiyun 			orc_load_firmware(host);	/* Download FW                  */
515*4882a593Smuzhiyun 			setup_SCBs(host);	/* Setup SCB base and SCB Size registers */
516*4882a593Smuzhiyun 			outb(0x00, host->base + ORC_HCTRL);	/* clear HOSTSTOP       */
517*4882a593Smuzhiyun 			if (wait_firmware_ready(host) == 0)
518*4882a593Smuzhiyun 				return -1;
519*4882a593Smuzhiyun 			/* Wait for firmware ready     */
520*4882a593Smuzhiyun 		} else {
521*4882a593Smuzhiyun 			setup_SCBs(host);	/* Setup SCB base and SCB Size registers */
522*4882a593Smuzhiyun 		}
523*4882a593Smuzhiyun 	} else {		/* Orchid is not Ready          */
524*4882a593Smuzhiyun 		outb(DEVRST, host->base + ORC_HCTRL);	/* Reset Host Adapter   */
525*4882a593Smuzhiyun 		if (wait_chip_ready(host) == 0)
526*4882a593Smuzhiyun 			return -1;
527*4882a593Smuzhiyun 		orc_load_firmware(host);	/* Download FW                  */
528*4882a593Smuzhiyun 		setup_SCBs(host);	/* Setup SCB base and SCB Size registers */
529*4882a593Smuzhiyun 		outb(HDO, host->base + ORC_HCTRL);	/* Do Hardware Reset &  */
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 		/*     clear HOSTSTOP  */
532*4882a593Smuzhiyun 		if (wait_firmware_ready(host) == 0)		/* Wait for firmware ready      */
533*4882a593Smuzhiyun 			return -1;
534*4882a593Smuzhiyun 	}
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	/* Load an EEProm copy into RAM */
537*4882a593Smuzhiyun 	/* Assumes single threaded at this point */
538*4882a593Smuzhiyun 	read_eeprom(host);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	if (nvramp->revision != 1)
541*4882a593Smuzhiyun 		return -1;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	host->scsi_id = nvramp->scsi_id;
544*4882a593Smuzhiyun 	host->BIOScfg = nvramp->BIOSConfig1;
545*4882a593Smuzhiyun 	host->max_targets = MAX_TARGETS;
546*4882a593Smuzhiyun 	ptr = (u8 *) & (nvramp->Target00Config);
547*4882a593Smuzhiyun 	for (i = 0; i < 16; ptr++, i++) {
548*4882a593Smuzhiyun 		host->target_flag[i] = *ptr;
549*4882a593Smuzhiyun 		host->max_tags[i] = ORC_MAXTAGS;
550*4882a593Smuzhiyun 	}
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	if (nvramp->SCSI0Config & NCC_BUSRESET)
553*4882a593Smuzhiyun 		host->flags |= HCF_SCSI_RESET;
554*4882a593Smuzhiyun 	outb(0xFB, host->base + ORC_GIMSK);	/* enable RP FIFO interrupt     */
555*4882a593Smuzhiyun 	return 0;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun /**
559*4882a593Smuzhiyun  *	orc_reset_scsi_bus		-	perform bus reset
560*4882a593Smuzhiyun  *	@host: host being reset
561*4882a593Smuzhiyun  *
562*4882a593Smuzhiyun  *	Perform a full bus reset on the adapter.
563*4882a593Smuzhiyun  */
564*4882a593Smuzhiyun 
orc_reset_scsi_bus(struct orc_host * host)565*4882a593Smuzhiyun static int orc_reset_scsi_bus(struct orc_host * host)
566*4882a593Smuzhiyun {				/* I need Host Control Block Information */
567*4882a593Smuzhiyun 	unsigned long flags;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	spin_lock_irqsave(&host->allocation_lock, flags);
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	init_alloc_map(host);
572*4882a593Smuzhiyun 	/* reset scsi bus */
573*4882a593Smuzhiyun 	outb(SCSIRST, host->base + ORC_HCTRL);
574*4882a593Smuzhiyun 	/* FIXME: We can spend up to a second with the lock held and
575*4882a593Smuzhiyun 	   interrupts off here */
576*4882a593Smuzhiyun 	if (wait_scsi_reset_done(host) == 0) {
577*4882a593Smuzhiyun 		spin_unlock_irqrestore(&host->allocation_lock, flags);
578*4882a593Smuzhiyun 		return FAILED;
579*4882a593Smuzhiyun 	} else {
580*4882a593Smuzhiyun 		spin_unlock_irqrestore(&host->allocation_lock, flags);
581*4882a593Smuzhiyun 		return SUCCESS;
582*4882a593Smuzhiyun 	}
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun /**
586*4882a593Smuzhiyun  *	orc_device_reset	-	device reset handler
587*4882a593Smuzhiyun  *	@host: host to reset
588*4882a593Smuzhiyun  *	@cmd: command causing the reset
589*4882a593Smuzhiyun  *	@target; target device
590*4882a593Smuzhiyun  *
591*4882a593Smuzhiyun  *	Reset registers, reset a hanging bus and kill active and disconnected
592*4882a593Smuzhiyun  *	commands for target w/o soft reset
593*4882a593Smuzhiyun  */
594*4882a593Smuzhiyun 
orc_device_reset(struct orc_host * host,struct scsi_cmnd * cmd,unsigned int target)595*4882a593Smuzhiyun static int orc_device_reset(struct orc_host * host, struct scsi_cmnd *cmd, unsigned int target)
596*4882a593Smuzhiyun {				/* I need Host Control Block Information */
597*4882a593Smuzhiyun 	struct orc_scb *scb;
598*4882a593Smuzhiyun 	struct orc_extended_scb *escb;
599*4882a593Smuzhiyun 	struct orc_scb *host_scb;
600*4882a593Smuzhiyun 	u8 i;
601*4882a593Smuzhiyun 	unsigned long flags;
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	spin_lock_irqsave(&(host->allocation_lock), flags);
604*4882a593Smuzhiyun 	scb = (struct orc_scb *) NULL;
605*4882a593Smuzhiyun 	escb = (struct orc_extended_scb *) NULL;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	/* setup scatter list address with one buffer */
608*4882a593Smuzhiyun 	host_scb = host->scb_virt;
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	/* FIXME: is this safe if we then fail to issue the reset or race
611*4882a593Smuzhiyun 	   a completion ? */
612*4882a593Smuzhiyun 	init_alloc_map(host);
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	/* Find the scb corresponding to the command */
615*4882a593Smuzhiyun 	for (i = 0; i < ORC_MAXQUEUE; i++) {
616*4882a593Smuzhiyun 		escb = host_scb->escb;
617*4882a593Smuzhiyun 		if (host_scb->status && escb->srb == cmd)
618*4882a593Smuzhiyun 			break;
619*4882a593Smuzhiyun 		host_scb++;
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	if (i == ORC_MAXQUEUE) {
623*4882a593Smuzhiyun 		printk(KERN_ERR "Unable to Reset - No SCB Found\n");
624*4882a593Smuzhiyun 		spin_unlock_irqrestore(&(host->allocation_lock), flags);
625*4882a593Smuzhiyun 		return FAILED;
626*4882a593Smuzhiyun 	}
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	/* Allocate a new SCB for the reset command to the firmware */
629*4882a593Smuzhiyun 	if ((scb = __orc_alloc_scb(host)) == NULL) {
630*4882a593Smuzhiyun 		/* Can't happen.. */
631*4882a593Smuzhiyun 		spin_unlock_irqrestore(&(host->allocation_lock), flags);
632*4882a593Smuzhiyun 		return FAILED;
633*4882a593Smuzhiyun 	}
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	/* Reset device is handled by the firmware, we fill in an SCB and
636*4882a593Smuzhiyun 	   fire it at the controller, it does the rest */
637*4882a593Smuzhiyun 	scb->opcode = ORC_BUSDEVRST;
638*4882a593Smuzhiyun 	scb->target = target;
639*4882a593Smuzhiyun 	scb->hastat = 0;
640*4882a593Smuzhiyun 	scb->tastat = 0;
641*4882a593Smuzhiyun 	scb->status = 0x0;
642*4882a593Smuzhiyun 	scb->link = 0xFF;
643*4882a593Smuzhiyun 	scb->reserved0 = 0;
644*4882a593Smuzhiyun 	scb->reserved1 = 0;
645*4882a593Smuzhiyun 	scb->xferlen = cpu_to_le32(0);
646*4882a593Smuzhiyun 	scb->sg_len = cpu_to_le32(0);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	escb->srb = NULL;
649*4882a593Smuzhiyun 	escb->srb = cmd;
650*4882a593Smuzhiyun 	orc_exec_scb(host, scb);	/* Start execute SCB            */
651*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->allocation_lock, flags);
652*4882a593Smuzhiyun 	return SUCCESS;
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun /**
656*4882a593Smuzhiyun  *	__orc_alloc_scb		-		allocate an SCB
657*4882a593Smuzhiyun  *	@host: host to allocate from
658*4882a593Smuzhiyun  *
659*4882a593Smuzhiyun  *	Allocate an SCB and return a pointer to the SCB object. NULL
660*4882a593Smuzhiyun  *	is returned if no SCB is free. The caller must already hold
661*4882a593Smuzhiyun  *	the allocator lock at this point.
662*4882a593Smuzhiyun  */
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 
__orc_alloc_scb(struct orc_host * host)665*4882a593Smuzhiyun static struct orc_scb *__orc_alloc_scb(struct orc_host * host)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun 	u8 channel;
668*4882a593Smuzhiyun 	unsigned long idx;
669*4882a593Smuzhiyun 	u8 index;
670*4882a593Smuzhiyun 	u8 i;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	channel = host->index;
673*4882a593Smuzhiyun 	for (i = 0; i < 8; i++) {
674*4882a593Smuzhiyun 		for (index = 0; index < 32; index++) {
675*4882a593Smuzhiyun 			if ((host->allocation_map[channel][i] >> index) & 0x01) {
676*4882a593Smuzhiyun 				host->allocation_map[channel][i] &= ~(1 << index);
677*4882a593Smuzhiyun 				idx = index + 32 * i;
678*4882a593Smuzhiyun 				/*
679*4882a593Smuzhiyun 				 * Translate the index to a structure instance
680*4882a593Smuzhiyun 				 */
681*4882a593Smuzhiyun 				return host->scb_virt + idx;
682*4882a593Smuzhiyun 			}
683*4882a593Smuzhiyun 		}
684*4882a593Smuzhiyun 	}
685*4882a593Smuzhiyun 	return NULL;
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun /**
689*4882a593Smuzhiyun  *	orc_alloc_scb		-		allocate an SCB
690*4882a593Smuzhiyun  *	@host: host to allocate from
691*4882a593Smuzhiyun  *
692*4882a593Smuzhiyun  *	Allocate an SCB and return a pointer to the SCB object. NULL
693*4882a593Smuzhiyun  *	is returned if no SCB is free.
694*4882a593Smuzhiyun  */
695*4882a593Smuzhiyun 
orc_alloc_scb(struct orc_host * host)696*4882a593Smuzhiyun static struct orc_scb *orc_alloc_scb(struct orc_host * host)
697*4882a593Smuzhiyun {
698*4882a593Smuzhiyun 	struct orc_scb *scb;
699*4882a593Smuzhiyun 	unsigned long flags;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	spin_lock_irqsave(&host->allocation_lock, flags);
702*4882a593Smuzhiyun 	scb = __orc_alloc_scb(host);
703*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->allocation_lock, flags);
704*4882a593Smuzhiyun 	return scb;
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun /**
708*4882a593Smuzhiyun  *	orc_release_scb			-	release an SCB
709*4882a593Smuzhiyun  *	@host: host owning the SCB
710*4882a593Smuzhiyun  *	@scb: SCB that is now free
711*4882a593Smuzhiyun  *
712*4882a593Smuzhiyun  *	Called to return a completed SCB to the allocation pool. Before
713*4882a593Smuzhiyun  *	calling the SCB must be out of use on both the host and the HA.
714*4882a593Smuzhiyun  */
715*4882a593Smuzhiyun 
orc_release_scb(struct orc_host * host,struct orc_scb * scb)716*4882a593Smuzhiyun static void orc_release_scb(struct orc_host *host, struct orc_scb *scb)
717*4882a593Smuzhiyun {
718*4882a593Smuzhiyun 	unsigned long flags;
719*4882a593Smuzhiyun 	u8 index, i, channel;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	spin_lock_irqsave(&(host->allocation_lock), flags);
722*4882a593Smuzhiyun 	channel = host->index;	/* Channel */
723*4882a593Smuzhiyun 	index = scb->scbidx;
724*4882a593Smuzhiyun 	i = index / 32;
725*4882a593Smuzhiyun 	index %= 32;
726*4882a593Smuzhiyun 	host->allocation_map[channel][i] |= (1 << index);
727*4882a593Smuzhiyun 	spin_unlock_irqrestore(&(host->allocation_lock), flags);
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun /**
731*4882a593Smuzhiyun  *	orchid_abort_scb	-	abort a command
732*4882a593Smuzhiyun  *
733*4882a593Smuzhiyun  *	Abort a queued command that has been passed to the firmware layer
734*4882a593Smuzhiyun  *	if possible. This is all handled by the firmware. We aks the firmware
735*4882a593Smuzhiyun  *	and it either aborts the command or fails
736*4882a593Smuzhiyun  */
737*4882a593Smuzhiyun 
orchid_abort_scb(struct orc_host * host,struct orc_scb * scb)738*4882a593Smuzhiyun static int orchid_abort_scb(struct orc_host * host, struct orc_scb * scb)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun 	unsigned char data, status;
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	outb(ORC_CMD_ABORT_SCB, host->base + ORC_HDATA);	/* Write command */
743*4882a593Smuzhiyun 	outb(HDO, host->base + ORC_HCTRL);
744*4882a593Smuzhiyun 	if (wait_HDO_off(host) == 0)	/* Wait HDO off   */
745*4882a593Smuzhiyun 		return 0;
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	outb(scb->scbidx, host->base + ORC_HDATA);	/* Write address */
748*4882a593Smuzhiyun 	outb(HDO, host->base + ORC_HCTRL);
749*4882a593Smuzhiyun 	if (wait_HDO_off(host) == 0)	/* Wait HDO off   */
750*4882a593Smuzhiyun 		return 0;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	if (wait_hdi_set(host, &data) == 0)	/* Wait HDI set   */
753*4882a593Smuzhiyun 		return 0;
754*4882a593Smuzhiyun 	status = inb(host->base + ORC_HDATA);
755*4882a593Smuzhiyun 	outb(data, host->base + ORC_HSTUS);	/* Clear HDI    */
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	if (status == 1)	/* 0 - Successfully               */
758*4882a593Smuzhiyun 		return 0;	/* 1 - Fail                     */
759*4882a593Smuzhiyun 	return 1;
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun 
inia100_abort_cmd(struct orc_host * host,struct scsi_cmnd * cmd)762*4882a593Smuzhiyun static int inia100_abort_cmd(struct orc_host * host, struct scsi_cmnd *cmd)
763*4882a593Smuzhiyun {
764*4882a593Smuzhiyun 	struct orc_extended_scb *escb;
765*4882a593Smuzhiyun 	struct orc_scb *scb;
766*4882a593Smuzhiyun 	u8 i;
767*4882a593Smuzhiyun 	unsigned long flags;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	spin_lock_irqsave(&(host->allocation_lock), flags);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	scb = host->scb_virt;
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	/* Walk the queue until we find the SCB that belongs to the command
774*4882a593Smuzhiyun 	   block. This isn't a performance critical path so a walk in the park
775*4882a593Smuzhiyun 	   here does no harm */
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	for (i = 0; i < ORC_MAXQUEUE; i++, scb++) {
778*4882a593Smuzhiyun 		escb = scb->escb;
779*4882a593Smuzhiyun 		if (scb->status && escb->srb == cmd) {
780*4882a593Smuzhiyun 			if (scb->tag_msg == 0) {
781*4882a593Smuzhiyun 				goto out;
782*4882a593Smuzhiyun 			} else {
783*4882a593Smuzhiyun 				/* Issue an ABORT to the firmware */
784*4882a593Smuzhiyun 				if (orchid_abort_scb(host, scb)) {
785*4882a593Smuzhiyun 					escb->srb = NULL;
786*4882a593Smuzhiyun 					spin_unlock_irqrestore(&host->allocation_lock, flags);
787*4882a593Smuzhiyun 					return SUCCESS;
788*4882a593Smuzhiyun 				} else
789*4882a593Smuzhiyun 					goto out;
790*4882a593Smuzhiyun 			}
791*4882a593Smuzhiyun 		}
792*4882a593Smuzhiyun 	}
793*4882a593Smuzhiyun out:
794*4882a593Smuzhiyun 	spin_unlock_irqrestore(&host->allocation_lock, flags);
795*4882a593Smuzhiyun 	return FAILED;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun /**
799*4882a593Smuzhiyun  *	orc_interrupt		-	IRQ processing
800*4882a593Smuzhiyun  *	@host: Host causing the interrupt
801*4882a593Smuzhiyun  *
802*4882a593Smuzhiyun  *	This function is called from the IRQ handler and protected
803*4882a593Smuzhiyun  *	by the host lock. While the controller reports that there are
804*4882a593Smuzhiyun  *	scb's for processing we pull them off the controller, turn the
805*4882a593Smuzhiyun  *	index into a host address pointer to the scb and call the scb
806*4882a593Smuzhiyun  *	handler.
807*4882a593Smuzhiyun  *
808*4882a593Smuzhiyun  *	Returns IRQ_HANDLED if any SCBs were processed, IRQ_NONE otherwise
809*4882a593Smuzhiyun  */
810*4882a593Smuzhiyun 
orc_interrupt(struct orc_host * host)811*4882a593Smuzhiyun static irqreturn_t orc_interrupt(struct orc_host * host)
812*4882a593Smuzhiyun {
813*4882a593Smuzhiyun 	u8 scb_index;
814*4882a593Smuzhiyun 	struct orc_scb *scb;
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	/* Check if we have an SCB queued for servicing */
817*4882a593Smuzhiyun 	if (inb(host->base + ORC_RQUEUECNT) == 0)
818*4882a593Smuzhiyun 		return IRQ_NONE;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	do {
821*4882a593Smuzhiyun 		/* Get the SCB index of the SCB to service */
822*4882a593Smuzhiyun 		scb_index = inb(host->base + ORC_RQUEUE);
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 		/* Translate it back to a host pointer */
825*4882a593Smuzhiyun 		scb = (struct orc_scb *) ((unsigned long) host->scb_virt + (unsigned long) (sizeof(struct orc_scb) * scb_index));
826*4882a593Smuzhiyun 		scb->status = 0x0;
827*4882a593Smuzhiyun 		/* Process the SCB */
828*4882a593Smuzhiyun 		inia100_scb_handler(host, scb);
829*4882a593Smuzhiyun 	} while (inb(host->base + ORC_RQUEUECNT));
830*4882a593Smuzhiyun 	return IRQ_HANDLED;
831*4882a593Smuzhiyun }				/* End of I1060Interrupt() */
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun /**
834*4882a593Smuzhiyun  *	inia100_build_scb	-	build SCB
835*4882a593Smuzhiyun  *	@host: host owing the control block
836*4882a593Smuzhiyun  *	@scb: control block to use
837*4882a593Smuzhiyun  *	@cmd: Mid layer command
838*4882a593Smuzhiyun  *
839*4882a593Smuzhiyun  *	Build a host adapter control block from the SCSI mid layer command
840*4882a593Smuzhiyun  */
841*4882a593Smuzhiyun 
inia100_build_scb(struct orc_host * host,struct orc_scb * scb,struct scsi_cmnd * cmd)842*4882a593Smuzhiyun static int inia100_build_scb(struct orc_host * host, struct orc_scb * scb, struct scsi_cmnd * cmd)
843*4882a593Smuzhiyun {				/* Create corresponding SCB     */
844*4882a593Smuzhiyun 	struct scatterlist *sg;
845*4882a593Smuzhiyun 	struct orc_sgent *sgent;		/* Pointer to SG list           */
846*4882a593Smuzhiyun 	int i, count_sg;
847*4882a593Smuzhiyun 	struct orc_extended_scb *escb;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	/* Links between the escb, scb and Linux scsi midlayer cmd */
850*4882a593Smuzhiyun 	escb = scb->escb;
851*4882a593Smuzhiyun 	escb->srb = cmd;
852*4882a593Smuzhiyun 	sgent = NULL;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	/* Set up the SCB to do a SCSI command block */
855*4882a593Smuzhiyun 	scb->opcode = ORC_EXECSCSI;
856*4882a593Smuzhiyun 	scb->flags = SCF_NO_DCHK;	/* Clear done bit               */
857*4882a593Smuzhiyun 	scb->target = cmd->device->id;
858*4882a593Smuzhiyun 	scb->lun = cmd->device->lun;
859*4882a593Smuzhiyun 	scb->reserved0 = 0;
860*4882a593Smuzhiyun 	scb->reserved1 = 0;
861*4882a593Smuzhiyun 	scb->sg_len = cpu_to_le32(0);
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	scb->xferlen = cpu_to_le32((u32) scsi_bufflen(cmd));
864*4882a593Smuzhiyun 	sgent = (struct orc_sgent *) & escb->sglist[0];
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	count_sg = scsi_dma_map(cmd);
867*4882a593Smuzhiyun 	if (count_sg < 0)
868*4882a593Smuzhiyun 		return count_sg;
869*4882a593Smuzhiyun 	BUG_ON(count_sg > TOTAL_SG_ENTRY);
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	/* Build the scatter gather lists */
872*4882a593Smuzhiyun 	if (count_sg) {
873*4882a593Smuzhiyun 		scb->sg_len = cpu_to_le32((u32) (count_sg * 8));
874*4882a593Smuzhiyun 		scsi_for_each_sg(cmd, sg, count_sg, i) {
875*4882a593Smuzhiyun 			sgent->base = cpu_to_le32((u32) sg_dma_address(sg));
876*4882a593Smuzhiyun 			sgent->length = cpu_to_le32((u32) sg_dma_len(sg));
877*4882a593Smuzhiyun 			sgent++;
878*4882a593Smuzhiyun 		}
879*4882a593Smuzhiyun 	} else {
880*4882a593Smuzhiyun 		scb->sg_len = cpu_to_le32(0);
881*4882a593Smuzhiyun 		sgent->base = cpu_to_le32(0);
882*4882a593Smuzhiyun 		sgent->length = cpu_to_le32(0);
883*4882a593Smuzhiyun 	}
884*4882a593Smuzhiyun 	scb->sg_addr = (u32) scb->sense_addr;	/* sense_addr is already little endian */
885*4882a593Smuzhiyun 	scb->hastat = 0;
886*4882a593Smuzhiyun 	scb->tastat = 0;
887*4882a593Smuzhiyun 	scb->link = 0xFF;
888*4882a593Smuzhiyun 	scb->sense_len = SENSE_SIZE;
889*4882a593Smuzhiyun 	scb->cdb_len = cmd->cmd_len;
890*4882a593Smuzhiyun 	if (scb->cdb_len >= IMAX_CDB) {
891*4882a593Smuzhiyun 		printk("max cdb length= %x\n", cmd->cmd_len);
892*4882a593Smuzhiyun 		scb->cdb_len = IMAX_CDB;
893*4882a593Smuzhiyun 	}
894*4882a593Smuzhiyun 	scb->ident = (u8)(cmd->device->lun & 0xff) | DISC_ALLOW;
895*4882a593Smuzhiyun 	if (cmd->device->tagged_supported) {	/* Tag Support                  */
896*4882a593Smuzhiyun 		scb->tag_msg = SIMPLE_QUEUE_TAG;	/* Do simple tag only   */
897*4882a593Smuzhiyun 	} else {
898*4882a593Smuzhiyun 		scb->tag_msg = 0;	/* No tag support               */
899*4882a593Smuzhiyun 	}
900*4882a593Smuzhiyun 	memcpy(scb->cdb, cmd->cmnd, scb->cdb_len);
901*4882a593Smuzhiyun 	return 0;
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun /**
905*4882a593Smuzhiyun  *	inia100_queue		-	queue command with host
906*4882a593Smuzhiyun  *	@cmd: Command block
907*4882a593Smuzhiyun  *	@done: Completion function
908*4882a593Smuzhiyun  *
909*4882a593Smuzhiyun  *	Called by the mid layer to queue a command. Process the command
910*4882a593Smuzhiyun  *	block, build the host specific scb structures and if there is room
911*4882a593Smuzhiyun  *	queue the command down to the controller
912*4882a593Smuzhiyun  */
913*4882a593Smuzhiyun 
inia100_queue_lck(struct scsi_cmnd * cmd,void (* done)(struct scsi_cmnd *))914*4882a593Smuzhiyun static int inia100_queue_lck(struct scsi_cmnd * cmd, void (*done) (struct scsi_cmnd *))
915*4882a593Smuzhiyun {
916*4882a593Smuzhiyun 	struct orc_scb *scb;
917*4882a593Smuzhiyun 	struct orc_host *host;		/* Point to Host adapter control block */
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	host = (struct orc_host *) cmd->device->host->hostdata;
920*4882a593Smuzhiyun 	cmd->scsi_done = done;
921*4882a593Smuzhiyun 	/* Get free SCSI control block  */
922*4882a593Smuzhiyun 	if ((scb = orc_alloc_scb(host)) == NULL)
923*4882a593Smuzhiyun 		return SCSI_MLQUEUE_HOST_BUSY;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	if (inia100_build_scb(host, scb, cmd)) {
926*4882a593Smuzhiyun 		orc_release_scb(host, scb);
927*4882a593Smuzhiyun 		return SCSI_MLQUEUE_HOST_BUSY;
928*4882a593Smuzhiyun 	}
929*4882a593Smuzhiyun 	orc_exec_scb(host, scb);	/* Start execute SCB            */
930*4882a593Smuzhiyun 	return 0;
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun 
DEF_SCSI_QCMD(inia100_queue)933*4882a593Smuzhiyun static DEF_SCSI_QCMD(inia100_queue)
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun /*****************************************************************************
936*4882a593Smuzhiyun  Function name  : inia100_abort
937*4882a593Smuzhiyun  Description    : Abort a queued command.
938*4882a593Smuzhiyun 	                 (commands that are on the bus can't be aborted easily)
939*4882a593Smuzhiyun  Input          : host  -       Pointer to host adapter structure
940*4882a593Smuzhiyun  Output         : None.
941*4882a593Smuzhiyun  Return         : pSRB  -       Pointer to SCSI request block.
942*4882a593Smuzhiyun *****************************************************************************/
943*4882a593Smuzhiyun static int inia100_abort(struct scsi_cmnd * cmd)
944*4882a593Smuzhiyun {
945*4882a593Smuzhiyun 	struct orc_host *host;
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	host = (struct orc_host *) cmd->device->host->hostdata;
948*4882a593Smuzhiyun 	return inia100_abort_cmd(host, cmd);
949*4882a593Smuzhiyun }
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun /*****************************************************************************
952*4882a593Smuzhiyun  Function name  : inia100_reset
953*4882a593Smuzhiyun  Description    : Reset registers, reset a hanging bus and
954*4882a593Smuzhiyun                   kill active and disconnected commands for target w/o soft reset
955*4882a593Smuzhiyun  Input          : host  -       Pointer to host adapter structure
956*4882a593Smuzhiyun  Output         : None.
957*4882a593Smuzhiyun  Return         : pSRB  -       Pointer to SCSI request block.
958*4882a593Smuzhiyun *****************************************************************************/
inia100_bus_reset(struct scsi_cmnd * cmd)959*4882a593Smuzhiyun static int inia100_bus_reset(struct scsi_cmnd * cmd)
960*4882a593Smuzhiyun {				/* I need Host Control Block Information */
961*4882a593Smuzhiyun 	struct orc_host *host;
962*4882a593Smuzhiyun 	host = (struct orc_host *) cmd->device->host->hostdata;
963*4882a593Smuzhiyun 	return orc_reset_scsi_bus(host);
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun /*****************************************************************************
967*4882a593Smuzhiyun  Function name  : inia100_device_reset
968*4882a593Smuzhiyun  Description    : Reset the device
969*4882a593Smuzhiyun  Input          : host  -       Pointer to host adapter structure
970*4882a593Smuzhiyun  Output         : None.
971*4882a593Smuzhiyun  Return         : pSRB  -       Pointer to SCSI request block.
972*4882a593Smuzhiyun *****************************************************************************/
inia100_device_reset(struct scsi_cmnd * cmd)973*4882a593Smuzhiyun static int inia100_device_reset(struct scsi_cmnd * cmd)
974*4882a593Smuzhiyun {				/* I need Host Control Block Information */
975*4882a593Smuzhiyun 	struct orc_host *host;
976*4882a593Smuzhiyun 	host = (struct orc_host *) cmd->device->host->hostdata;
977*4882a593Smuzhiyun 	return orc_device_reset(host, cmd, scmd_id(cmd));
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun /**
982*4882a593Smuzhiyun  *	inia100_scb_handler	-	interrupt callback
983*4882a593Smuzhiyun  *	@host: Host causing the interrupt
984*4882a593Smuzhiyun  *	@scb: SCB the controller returned as needing processing
985*4882a593Smuzhiyun  *
986*4882a593Smuzhiyun  *	Perform completion processing on a control block. Do the conversions
987*4882a593Smuzhiyun  *	from host to SCSI midlayer error coding, save any sense data and
988*4882a593Smuzhiyun  *	the complete with the midlayer and recycle the scb.
989*4882a593Smuzhiyun  */
990*4882a593Smuzhiyun 
inia100_scb_handler(struct orc_host * host,struct orc_scb * scb)991*4882a593Smuzhiyun static void inia100_scb_handler(struct orc_host *host, struct orc_scb *scb)
992*4882a593Smuzhiyun {
993*4882a593Smuzhiyun 	struct scsi_cmnd *cmd;	/* Pointer to SCSI request block */
994*4882a593Smuzhiyun 	struct orc_extended_scb *escb;
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	escb = scb->escb;
997*4882a593Smuzhiyun 	if ((cmd = (struct scsi_cmnd *) escb->srb) == NULL) {
998*4882a593Smuzhiyun 		printk(KERN_ERR "inia100_scb_handler: SRB pointer is empty\n");
999*4882a593Smuzhiyun 		orc_release_scb(host, scb);	/* Release SCB for current channel */
1000*4882a593Smuzhiyun 		return;
1001*4882a593Smuzhiyun 	}
1002*4882a593Smuzhiyun 	escb->srb = NULL;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	switch (scb->hastat) {
1005*4882a593Smuzhiyun 	case 0x0:
1006*4882a593Smuzhiyun 	case 0xa:		/* Linked command complete without error and linked normally */
1007*4882a593Smuzhiyun 	case 0xb:		/* Linked command complete without error interrupt generated */
1008*4882a593Smuzhiyun 		scb->hastat = 0;
1009*4882a593Smuzhiyun 		break;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	case 0x11:		/* Selection time out-The initiator selection or target
1012*4882a593Smuzhiyun 				   reselection was not complete within the SCSI Time out period */
1013*4882a593Smuzhiyun 		scb->hastat = DID_TIME_OUT;
1014*4882a593Smuzhiyun 		break;
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 	case 0x14:		/* Target bus phase sequence failure-An invalid bus phase or bus
1017*4882a593Smuzhiyun 				   phase sequence was requested by the target. The host adapter
1018*4882a593Smuzhiyun 				   will generate a SCSI Reset Condition, notifying the host with
1019*4882a593Smuzhiyun 				   a SCRD interrupt */
1020*4882a593Smuzhiyun 		scb->hastat = DID_RESET;
1021*4882a593Smuzhiyun 		break;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	case 0x1a:		/* SCB Aborted. 07/21/98 */
1024*4882a593Smuzhiyun 		scb->hastat = DID_ABORT;
1025*4882a593Smuzhiyun 		break;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	case 0x12:		/* Data overrun/underrun-The target attempted to transfer more data
1028*4882a593Smuzhiyun 				   than was allocated by the Data Length field or the sum of the
1029*4882a593Smuzhiyun 				   Scatter / Gather Data Length fields. */
1030*4882a593Smuzhiyun 	case 0x13:		/* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
1031*4882a593Smuzhiyun 	case 0x16:		/* Invalid CCB Operation Code-The first byte of the CCB was invalid. */
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	default:
1034*4882a593Smuzhiyun 		printk(KERN_DEBUG "inia100: %x %x\n", scb->hastat, scb->tastat);
1035*4882a593Smuzhiyun 		scb->hastat = DID_ERROR;	/* Couldn't find any better */
1036*4882a593Smuzhiyun 		break;
1037*4882a593Smuzhiyun 	}
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	if (scb->tastat == 2) {	/* Check condition              */
1040*4882a593Smuzhiyun 		memcpy((unsigned char *) &cmd->sense_buffer[0],
1041*4882a593Smuzhiyun 		   (unsigned char *) &escb->sglist[0], SENSE_SIZE);
1042*4882a593Smuzhiyun 	}
1043*4882a593Smuzhiyun 	cmd->result = scb->tastat | (scb->hastat << 16);
1044*4882a593Smuzhiyun 	scsi_dma_unmap(cmd);
1045*4882a593Smuzhiyun 	cmd->scsi_done(cmd);	/* Notify system DONE           */
1046*4882a593Smuzhiyun 	orc_release_scb(host, scb);	/* Release SCB for current channel */
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun /**
1050*4882a593Smuzhiyun  *	inia100_intr		-	interrupt handler
1051*4882a593Smuzhiyun  *	@irqno: Interrupt value
1052*4882a593Smuzhiyun  *	@devid: Host adapter
1053*4882a593Smuzhiyun  *
1054*4882a593Smuzhiyun  *	Entry point for IRQ handling. All the real work is performed
1055*4882a593Smuzhiyun  *	by orc_interrupt.
1056*4882a593Smuzhiyun  */
inia100_intr(int irqno,void * devid)1057*4882a593Smuzhiyun static irqreturn_t inia100_intr(int irqno, void *devid)
1058*4882a593Smuzhiyun {
1059*4882a593Smuzhiyun 	struct Scsi_Host *shost = (struct Scsi_Host *)devid;
1060*4882a593Smuzhiyun 	struct orc_host *host = (struct orc_host *)shost->hostdata;
1061*4882a593Smuzhiyun 	unsigned long flags;
1062*4882a593Smuzhiyun 	irqreturn_t res;
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun 	spin_lock_irqsave(shost->host_lock, flags);
1065*4882a593Smuzhiyun 	res = orc_interrupt(host);
1066*4882a593Smuzhiyun 	spin_unlock_irqrestore(shost->host_lock, flags);
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 	return res;
1069*4882a593Smuzhiyun }
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun static struct scsi_host_template inia100_template = {
1072*4882a593Smuzhiyun 	.proc_name		= "inia100",
1073*4882a593Smuzhiyun 	.name			= inia100_REVID,
1074*4882a593Smuzhiyun 	.queuecommand		= inia100_queue,
1075*4882a593Smuzhiyun 	.eh_abort_handler	= inia100_abort,
1076*4882a593Smuzhiyun 	.eh_bus_reset_handler	= inia100_bus_reset,
1077*4882a593Smuzhiyun 	.eh_device_reset_handler = inia100_device_reset,
1078*4882a593Smuzhiyun 	.can_queue		= 1,
1079*4882a593Smuzhiyun 	.this_id		= 1,
1080*4882a593Smuzhiyun 	.sg_tablesize		= SG_ALL,
1081*4882a593Smuzhiyun };
1082*4882a593Smuzhiyun 
inia100_probe_one(struct pci_dev * pdev,const struct pci_device_id * id)1083*4882a593Smuzhiyun static int inia100_probe_one(struct pci_dev *pdev,
1084*4882a593Smuzhiyun 			     const struct pci_device_id *id)
1085*4882a593Smuzhiyun {
1086*4882a593Smuzhiyun 	struct Scsi_Host *shost;
1087*4882a593Smuzhiyun 	struct orc_host *host;
1088*4882a593Smuzhiyun 	unsigned long port, bios;
1089*4882a593Smuzhiyun 	int error = -ENODEV;
1090*4882a593Smuzhiyun 	u32 sz;
1091*4882a593Smuzhiyun 	unsigned long biosaddr;
1092*4882a593Smuzhiyun 	char *bios_phys;
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	if (pci_enable_device(pdev))
1095*4882a593Smuzhiyun 		goto out;
1096*4882a593Smuzhiyun 	if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
1097*4882a593Smuzhiyun 		printk(KERN_WARNING "Unable to set 32bit DMA "
1098*4882a593Smuzhiyun 				    "on inia100 adapter, ignoring.\n");
1099*4882a593Smuzhiyun 		goto out_disable_device;
1100*4882a593Smuzhiyun 	}
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 	pci_set_master(pdev);
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	port = pci_resource_start(pdev, 0);
1105*4882a593Smuzhiyun 	if (!request_region(port, 256, "inia100")) {
1106*4882a593Smuzhiyun 		printk(KERN_WARNING "inia100: io port 0x%lx, is busy.\n", port);
1107*4882a593Smuzhiyun 		goto out_disable_device;
1108*4882a593Smuzhiyun 	}
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	/* <02> read from base address + 0x50 offset to get the bios value. */
1111*4882a593Smuzhiyun 	bios = inw(port + 0x50);
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun 	shost = scsi_host_alloc(&inia100_template, sizeof(struct orc_host));
1115*4882a593Smuzhiyun 	if (!shost)
1116*4882a593Smuzhiyun 		goto out_release_region;
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	host = (struct orc_host *)shost->hostdata;
1119*4882a593Smuzhiyun 	host->pdev = pdev;
1120*4882a593Smuzhiyun 	host->base = port;
1121*4882a593Smuzhiyun 	host->BIOScfg = bios;
1122*4882a593Smuzhiyun 	spin_lock_init(&host->allocation_lock);
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	/* Get total memory needed for SCB */
1125*4882a593Smuzhiyun 	sz = ORC_MAXQUEUE * sizeof(struct orc_scb);
1126*4882a593Smuzhiyun 	host->scb_virt = dma_alloc_coherent(&pdev->dev, sz, &host->scb_phys,
1127*4882a593Smuzhiyun 					    GFP_KERNEL);
1128*4882a593Smuzhiyun 	if (!host->scb_virt) {
1129*4882a593Smuzhiyun 		printk("inia100: SCB memory allocation error\n");
1130*4882a593Smuzhiyun 		goto out_host_put;
1131*4882a593Smuzhiyun 	}
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	/* Get total memory needed for ESCB */
1134*4882a593Smuzhiyun 	sz = ORC_MAXQUEUE * sizeof(struct orc_extended_scb);
1135*4882a593Smuzhiyun 	host->escb_virt = dma_alloc_coherent(&pdev->dev, sz, &host->escb_phys,
1136*4882a593Smuzhiyun 					     GFP_KERNEL);
1137*4882a593Smuzhiyun 	if (!host->escb_virt) {
1138*4882a593Smuzhiyun 		printk("inia100: ESCB memory allocation error\n");
1139*4882a593Smuzhiyun 		goto out_free_scb_array;
1140*4882a593Smuzhiyun 	}
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	biosaddr = host->BIOScfg;
1143*4882a593Smuzhiyun 	biosaddr = (biosaddr << 4);
1144*4882a593Smuzhiyun 	bios_phys = phys_to_virt(biosaddr);
1145*4882a593Smuzhiyun 	if (init_orchid(host)) {	/* Initialize orchid chip */
1146*4882a593Smuzhiyun 		printk("inia100: initial orchid fail!!\n");
1147*4882a593Smuzhiyun 		goto out_free_escb_array;
1148*4882a593Smuzhiyun 	}
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	shost->io_port = host->base;
1151*4882a593Smuzhiyun 	shost->n_io_port = 0xff;
1152*4882a593Smuzhiyun 	shost->can_queue = ORC_MAXQUEUE;
1153*4882a593Smuzhiyun 	shost->unique_id = shost->io_port;
1154*4882a593Smuzhiyun 	shost->max_id = host->max_targets;
1155*4882a593Smuzhiyun 	shost->max_lun = 16;
1156*4882a593Smuzhiyun 	shost->irq = pdev->irq;
1157*4882a593Smuzhiyun 	shost->this_id = host->scsi_id;	/* Assign HCS index */
1158*4882a593Smuzhiyun 	shost->sg_tablesize = TOTAL_SG_ENTRY;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	/* Initial orc chip           */
1161*4882a593Smuzhiyun 	error = request_irq(pdev->irq, inia100_intr, IRQF_SHARED,
1162*4882a593Smuzhiyun 			"inia100", shost);
1163*4882a593Smuzhiyun 	if (error < 0) {
1164*4882a593Smuzhiyun 		printk(KERN_WARNING "inia100: unable to get irq %d\n",
1165*4882a593Smuzhiyun 				pdev->irq);
1166*4882a593Smuzhiyun 		goto out_free_escb_array;
1167*4882a593Smuzhiyun 	}
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	pci_set_drvdata(pdev, shost);
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	error = scsi_add_host(shost, &pdev->dev);
1172*4882a593Smuzhiyun 	if (error)
1173*4882a593Smuzhiyun 		goto out_free_irq;
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 	scsi_scan_host(shost);
1176*4882a593Smuzhiyun 	return 0;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun out_free_irq:
1179*4882a593Smuzhiyun         free_irq(shost->irq, shost);
1180*4882a593Smuzhiyun out_free_escb_array:
1181*4882a593Smuzhiyun 	dma_free_coherent(&pdev->dev,
1182*4882a593Smuzhiyun 			ORC_MAXQUEUE * sizeof(struct orc_extended_scb),
1183*4882a593Smuzhiyun 			host->escb_virt, host->escb_phys);
1184*4882a593Smuzhiyun out_free_scb_array:
1185*4882a593Smuzhiyun 	dma_free_coherent(&pdev->dev,
1186*4882a593Smuzhiyun 			ORC_MAXQUEUE * sizeof(struct orc_scb),
1187*4882a593Smuzhiyun 			host->scb_virt, host->scb_phys);
1188*4882a593Smuzhiyun out_host_put:
1189*4882a593Smuzhiyun 	scsi_host_put(shost);
1190*4882a593Smuzhiyun out_release_region:
1191*4882a593Smuzhiyun         release_region(port, 256);
1192*4882a593Smuzhiyun out_disable_device:
1193*4882a593Smuzhiyun 	pci_disable_device(pdev);
1194*4882a593Smuzhiyun out:
1195*4882a593Smuzhiyun 	return error;
1196*4882a593Smuzhiyun }
1197*4882a593Smuzhiyun 
inia100_remove_one(struct pci_dev * pdev)1198*4882a593Smuzhiyun static void inia100_remove_one(struct pci_dev *pdev)
1199*4882a593Smuzhiyun {
1200*4882a593Smuzhiyun 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
1201*4882a593Smuzhiyun 	struct orc_host *host = (struct orc_host *)shost->hostdata;
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	scsi_remove_host(shost);
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun         free_irq(shost->irq, shost);
1206*4882a593Smuzhiyun 	dma_free_coherent(&pdev->dev,
1207*4882a593Smuzhiyun 			ORC_MAXQUEUE * sizeof(struct orc_extended_scb),
1208*4882a593Smuzhiyun 			host->escb_virt, host->escb_phys);
1209*4882a593Smuzhiyun 	dma_free_coherent(&pdev->dev,
1210*4882a593Smuzhiyun 			ORC_MAXQUEUE * sizeof(struct orc_scb),
1211*4882a593Smuzhiyun 			host->scb_virt, host->scb_phys);
1212*4882a593Smuzhiyun         release_region(shost->io_port, 256);
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	scsi_host_put(shost);
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun static struct pci_device_id inia100_pci_tbl[] = {
1218*4882a593Smuzhiyun 	{PCI_VENDOR_ID_INIT, 0x1060, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1219*4882a593Smuzhiyun 	{0,}
1220*4882a593Smuzhiyun };
1221*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, inia100_pci_tbl);
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun static struct pci_driver inia100_pci_driver = {
1224*4882a593Smuzhiyun 	.name		= "inia100",
1225*4882a593Smuzhiyun 	.id_table	= inia100_pci_tbl,
1226*4882a593Smuzhiyun 	.probe		= inia100_probe_one,
1227*4882a593Smuzhiyun 	.remove		= inia100_remove_one,
1228*4882a593Smuzhiyun };
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun module_pci_driver(inia100_pci_driver);
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun MODULE_DESCRIPTION("Initio A100U2W SCSI driver");
1233*4882a593Smuzhiyun MODULE_AUTHOR("Initio Corporation");
1234*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
1235