blob: d3a6d15fb77af90ead7ee78bfd1deed94e5bef8f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Initio A100 device driver for Linux.
3 *
4 * Copyright (c) 1994-1998 Initio Corporation
5 * Copyright (c) 2003-2004 Christoph Hellwig
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
26 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35/*
36 * Revision History:
37 * 07/02/98 hl - v.91n Initial drivers.
38 * 09/14/98 hl - v1.01 Support new Kernel.
39 * 09/22/98 hl - v1.01a Support reset.
40 * 09/24/98 hl - v1.01b Fixed reset.
41 * 10/05/98 hl - v1.02 split the source code and release.
42 * 12/19/98 bv - v1.02a Use spinlocks for 2.1.95 and up
43 * 01/31/99 bv - v1.02b Use mdelay instead of waitForPause
44 * 08/08/99 bv - v1.02c Use waitForPause again.
45 * 06/25/02 Doug Ledford <dledford@redhat.com> - v1.02d
46 * - Remove limit on number of controllers
47 * - Port to DMA mapping API
48 * - Clean up interrupt handler registration
49 * - Fix memory leaks
50 * - Fix allocation of scsi host structs and private data
51 * 11/18/03 Christoph Hellwig <hch@lst.de>
52 * - Port to new probing API
53 * - Fix some more leaks in init failure cases
54 * 9/28/04 Christoph Hellwig <hch@lst.de>
55 * - merge the two source files
56 * - remove internal queueing code
Alan Cox4023c4742007-06-15 14:45:30 +010057 * 14/06/07 Alan Cox <alan@redhat.com>
58 * - Grand cleanup and Linuxisation
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
60
61#include <linux/module.h>
62#include <linux/errno.h>
63#include <linux/delay.h>
64#include <linux/interrupt.h>
65#include <linux/pci.h>
66#include <linux/init.h>
67#include <linux/blkdev.h>
68#include <linux/spinlock.h>
69#include <linux/kernel.h>
70#include <linux/string.h>
71#include <linux/ioport.h>
72#include <linux/slab.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080073#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75#include <asm/io.h>
76#include <asm/irq.h>
77
78#include <scsi/scsi.h>
79#include <scsi/scsi_cmnd.h>
80#include <scsi/scsi_device.h>
81#include <scsi/scsi_host.h>
82
83#include "a100u2w.h"
84
85
Alan Cox4023c4742007-06-15 14:45:30 +010086static struct orc_scb *__orc_alloc_scb(struct orc_host * host);
87static void inia100_scb_handler(struct orc_host *host, struct orc_scb *scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Alan Cox4023c4742007-06-15 14:45:30 +010089static struct orc_nvram nvram, *nvramp = &nvram;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Alan Cox4023c4742007-06-15 14:45:30 +010091static u8 default_nvram[64] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93/*----------header -------------*/
94 0x01, /* 0x00: Sub System Vendor ID 0 */
95 0x11, /* 0x01: Sub System Vendor ID 1 */
96 0x60, /* 0x02: Sub System ID 0 */
97 0x10, /* 0x03: Sub System ID 1 */
98 0x00, /* 0x04: SubClass */
99 0x01, /* 0x05: Vendor ID 0 */
100 0x11, /* 0x06: Vendor ID 1 */
101 0x60, /* 0x07: Device ID 0 */
102 0x10, /* 0x08: Device ID 1 */
103 0x00, /* 0x09: Reserved */
104 0x00, /* 0x0A: Reserved */
105 0x01, /* 0x0B: Revision of Data Structure */
106 /* -- Host Adapter Structure --- */
107 0x01, /* 0x0C: Number Of SCSI Channel */
108 0x01, /* 0x0D: BIOS Configuration 1 */
109 0x00, /* 0x0E: BIOS Configuration 2 */
110 0x00, /* 0x0F: BIOS Configuration 3 */
111 /* --- SCSI Channel 0 Configuration --- */
112 0x07, /* 0x10: H/A ID */
113 0x83, /* 0x11: Channel Configuration */
114 0x20, /* 0x12: MAX TAG per target */
115 0x0A, /* 0x13: SCSI Reset Recovering time */
116 0x00, /* 0x14: Channel Configuration4 */
117 0x00, /* 0x15: Channel Configuration5 */
118 /* SCSI Channel 0 Target Configuration */
119 /* 0x16-0x25 */
120 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8,
121 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8,
122 /* --- SCSI Channel 1 Configuration --- */
123 0x07, /* 0x26: H/A ID */
124 0x83, /* 0x27: Channel Configuration */
125 0x20, /* 0x28: MAX TAG per target */
126 0x0A, /* 0x29: SCSI Reset Recovering time */
127 0x00, /* 0x2A: Channel Configuration4 */
128 0x00, /* 0x2B: Channel Configuration5 */
129 /* SCSI Channel 1 Target Configuration */
130 /* 0x2C-0x3B */
131 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8,
132 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8,
133 0x00, /* 0x3C: Reserved */
134 0x00, /* 0x3D: Reserved */
135 0x00, /* 0x3E: Reserved */
136 0x00 /* 0x3F: Checksum */
137};
138
139
Alan Cox4023c4742007-06-15 14:45:30 +0100140static u8 wait_chip_ready(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 int i;
143
144 for (i = 0; i < 10; i++) { /* Wait 1 second for report timeout */
Alan Cox4023c4742007-06-15 14:45:30 +0100145 if (inb(host->base + ORC_HCTRL) & HOSTSTOP) /* Wait HOSTSTOP set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return 1;
Alan Cox4023c4742007-06-15 14:45:30 +0100147 mdelay(100);
148 }
149 return 0;
150}
151
152static u8 wait_firmware_ready(struct orc_host * host)
153{
154 int i;
155
156 for (i = 0; i < 10; i++) { /* Wait 1 second for report timeout */
157 if (inb(host->base + ORC_HSTUS) & RREADY) /* Wait READY set */
158 return 1;
159 mdelay(100); /* wait 100ms before try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161 return 0;
162}
163
164/***************************************************************************/
Alan Cox4023c4742007-06-15 14:45:30 +0100165static u8 wait_scsi_reset_done(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 int i;
168
169 for (i = 0; i < 10; i++) { /* Wait 1 second for report timeout */
Alan Cox4023c4742007-06-15 14:45:30 +0100170 if (!(inb(host->base + ORC_HCTRL) & SCSIRST)) /* Wait SCSIRST done */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 1;
Alan Cox4023c4742007-06-15 14:45:30 +0100172 mdelay(100); /* wait 100ms before try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 return 0;
175}
176
177/***************************************************************************/
Alan Cox4023c4742007-06-15 14:45:30 +0100178static u8 wait_HDO_off(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 int i;
181
182 for (i = 0; i < 10; i++) { /* Wait 1 second for report timeout */
Alan Cox4023c4742007-06-15 14:45:30 +0100183 if (!(inb(host->base + ORC_HCTRL) & HDO)) /* Wait HDO off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return 1;
Alan Cox4023c4742007-06-15 14:45:30 +0100185 mdelay(100); /* wait 100ms before try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187 return 0;
188}
189
190/***************************************************************************/
Alan Cox4023c4742007-06-15 14:45:30 +0100191static u8 wait_hdi_set(struct orc_host * host, u8 * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int i;
194
195 for (i = 0; i < 10; i++) { /* Wait 1 second for report timeout */
Alan Cox4023c4742007-06-15 14:45:30 +0100196 if ((*data = inb(host->base + ORC_HSTUS)) & HDI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 1; /* Wait HDI set */
Alan Cox4023c4742007-06-15 14:45:30 +0100198 mdelay(100); /* wait 100ms before try again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 return 0;
201}
202
203/***************************************************************************/
Alan Cox4023c4742007-06-15 14:45:30 +0100204static unsigned short orc_read_fwrev(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Alan Cox4023c4742007-06-15 14:45:30 +0100206 u16 version;
207 u8 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Alan Cox4023c4742007-06-15 14:45:30 +0100209 outb(ORC_CMD_VERSION, host->base + ORC_HDATA);
210 outb(HDO, host->base + ORC_HCTRL);
211 if (wait_HDO_off(host) == 0) /* Wait HDO off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 0;
213
Alan Cox4023c4742007-06-15 14:45:30 +0100214 if (wait_hdi_set(host, &data) == 0) /* Wait HDI set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 0;
Alan Cox4023c4742007-06-15 14:45:30 +0100216 version = inb(host->base + ORC_HDATA);
217 outb(data, host->base + ORC_HSTUS); /* Clear HDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Alan Cox4023c4742007-06-15 14:45:30 +0100219 if (wait_hdi_set(host, &data) == 0) /* Wait HDI set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
Alan Cox4023c4742007-06-15 14:45:30 +0100221 version |= inb(host->base + ORC_HDATA) << 8;
222 outb(data, host->base + ORC_HSTUS); /* Clear HDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Alan Cox4023c4742007-06-15 14:45:30 +0100224 return version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/***************************************************************************/
Alan Cox4023c4742007-06-15 14:45:30 +0100228static u8 orc_nv_write(struct orc_host * host, unsigned char address, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Alan Cox4023c4742007-06-15 14:45:30 +0100230 outb(ORC_CMD_SET_NVM, host->base + ORC_HDATA); /* Write command */
231 outb(HDO, host->base + ORC_HCTRL);
232 if (wait_HDO_off(host) == 0) /* Wait HDO off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
234
Alan Cox4023c4742007-06-15 14:45:30 +0100235 outb(address, host->base + ORC_HDATA); /* Write address */
236 outb(HDO, host->base + ORC_HCTRL);
237 if (wait_HDO_off(host) == 0) /* Wait HDO off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 0;
239
Alan Cox4023c4742007-06-15 14:45:30 +0100240 outb(value, host->base + ORC_HDATA); /* Write value */
241 outb(HDO, host->base + ORC_HCTRL);
242 if (wait_HDO_off(host) == 0) /* Wait HDO off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244
245 return 1;
246}
247
248/***************************************************************************/
Alan Cox4023c4742007-06-15 14:45:30 +0100249static u8 orc_nv_read(struct orc_host * host, u8 address, u8 *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Alan Cox4023c4742007-06-15 14:45:30 +0100251 unsigned char data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Alan Cox4023c4742007-06-15 14:45:30 +0100253 outb(ORC_CMD_GET_NVM, host->base + ORC_HDATA); /* Write command */
254 outb(HDO, host->base + ORC_HCTRL);
255 if (wait_HDO_off(host) == 0) /* Wait HDO off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257
Alan Cox4023c4742007-06-15 14:45:30 +0100258 outb(address, host->base + ORC_HDATA); /* Write address */
259 outb(HDO, host->base + ORC_HCTRL);
260 if (wait_HDO_off(host) == 0) /* Wait HDO off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262
Alan Cox4023c4742007-06-15 14:45:30 +0100263 if (wait_hdi_set(host, &data) == 0) /* Wait HDI set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return 0;
Alan Cox4023c4742007-06-15 14:45:30 +0100265 *ptr = inb(host->base + ORC_HDATA);
266 outb(data, host->base + ORC_HSTUS); /* Clear HDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 return 1;
Alan Cox4023c4742007-06-15 14:45:30 +0100269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Alan Cox4023c4742007-06-15 14:45:30 +0100272/**
273 * orc_exec_sb - Queue an SCB with the HA
274 * @host: host adapter the SCB belongs to
275 * @scb: SCB to queue for execution
276 */
277
278static void orc_exec_scb(struct orc_host * host, struct orc_scb * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Alan Cox4023c4742007-06-15 14:45:30 +0100280 scb->status = ORCSCB_POST;
281 outb(scb->scbidx, host->base + ORC_PQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284
Alan Cox4023c4742007-06-15 14:45:30 +0100285/**
286 * se2_rd_all - read SCSI parameters from EEPROM
287 * @host: Host whose EEPROM is being loaded
288 *
289 * Read SCSI H/A configuration parameters from serial EEPROM
290 */
291
292static int se2_rd_all(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 int i;
Alan Cox4023c4742007-06-15 14:45:30 +0100295 u8 *np, chksum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Alan Cox4023c4742007-06-15 14:45:30 +0100297 np = (u8 *) nvramp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 for (i = 0; i < 64; i++, np++) { /* <01> */
Alan Cox4023c4742007-06-15 14:45:30 +0100299 if (orc_nv_read(host, (u8) i, np) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
Alan Cox4023c4742007-06-15 14:45:30 +0100303 /*------ Is ckecksum ok ? ------*/
304 np = (u8 *) nvramp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 for (i = 0; i < 63; i++)
306 chksum += *np++;
307
Alan Cox4023c4742007-06-15 14:45:30 +0100308 if (nvramp->CheckSum != (u8) chksum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return -1;
310 return 1;
311}
312
Alan Cox4023c4742007-06-15 14:45:30 +0100313/**
314 * se2_update_all - update the EEPROM
315 * @host: Host whose EEPROM is being updated
316 *
317 * Update changed bytes in the EEPROM image.
318 */
319
320static void se2_update_all(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{ /* setup default pattern */
322 int i;
Alan Cox4023c4742007-06-15 14:45:30 +0100323 u8 *np, *np1, chksum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* Calculate checksum first */
Alan Cox4023c4742007-06-15 14:45:30 +0100326 np = (u8 *) default_nvram;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 for (i = 0; i < 63; i++)
328 chksum += *np++;
329 *np = chksum;
330
Alan Cox4023c4742007-06-15 14:45:30 +0100331 np = (u8 *) default_nvram;
332 np1 = (u8 *) nvramp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 for (i = 0; i < 64; i++, np++, np1++) {
Alan Cox4023c4742007-06-15 14:45:30 +0100334 if (*np != *np1)
335 orc_nv_write(host, (u8) i, *np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Alan Cox4023c4742007-06-15 14:45:30 +0100339/**
340 * read_eeprom - load EEPROM
341 * @host: Host EEPROM to read
342 *
343 * Read the EEPROM for a given host. If it is invalid or fails
344 * the restore the defaults and use them.
345 */
346
347static void read_eeprom(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Alan Cox4023c4742007-06-15 14:45:30 +0100349 if (se2_rd_all(host) != 1) {
350 se2_update_all(host); /* setup default pattern */
351 se2_rd_all(host); /* load again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353}
354
355
Alan Cox4023c4742007-06-15 14:45:30 +0100356/**
357 * orc_load_firmware - initialise firmware
358 * @host: Host to set up
359 *
360 * Load the firmware from the EEPROM into controller SRAM. This
361 * is basically a 4K block copy and then a 4K block read to check
362 * correctness. The rest is convulted by the indirect interfaces
363 * in the hardware
364 */
365
366static u8 orc_load_firmware(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Alan Cox4023c4742007-06-15 14:45:30 +0100368 u32 data32;
369 u16 bios_addr;
370 u16 i;
371 u8 *data32_ptr, data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373
Alan Cox4023c4742007-06-15 14:45:30 +0100374 /* Set up the EEPROM for access */
375
376 data = inb(host->base + ORC_GCFG);
377 outb(data | EEPRG, host->base + ORC_GCFG); /* Enable EEPROM programming */
378 outb(0x00, host->base + ORC_EBIOSADR2);
379 outw(0x0000, host->base + ORC_EBIOSADR0);
380 if (inb(host->base + ORC_EBIOSDATA) != 0x55) {
381 outb(data, host->base + ORC_GCFG); /* Disable EEPROM programming */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383 }
Alan Cox4023c4742007-06-15 14:45:30 +0100384 outw(0x0001, host->base + ORC_EBIOSADR0);
385 if (inb(host->base + ORC_EBIOSDATA) != 0xAA) {
386 outb(data, host->base + ORC_GCFG); /* Disable EEPROM programming */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Alan Cox4023c4742007-06-15 14:45:30 +0100390 outb(PRGMRST | DOWNLOAD, host->base + ORC_RISCCTL); /* Enable SRAM programming */
391 data32_ptr = (u8 *) & data32;
392 data32 = 0; /* Initial FW address to 0 */
393 outw(0x0010, host->base + ORC_EBIOSADR0);
394 *data32_ptr = inb(host->base + ORC_EBIOSDATA); /* Read from BIOS */
395 outw(0x0011, host->base + ORC_EBIOSADR0);
396 *(data32_ptr + 1) = inb(host->base + ORC_EBIOSDATA); /* Read from BIOS */
397 outw(0x0012, host->base + ORC_EBIOSADR0);
398 *(data32_ptr + 2) = inb(host->base + ORC_EBIOSDATA); /* Read from BIOS */
399 outw(*(data32_ptr + 2), host->base + ORC_EBIOSADR2);
400 outl(data32, host->base + ORC_FWBASEADR); /* Write FW address */
401
402 /* Copy the code from the BIOS to the SRAM */
403
404 bios_addr = (u16) data32; /* FW code locate at BIOS address + ? */
405 for (i = 0, data32_ptr = (u8 *) & data32; /* Download the code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 i < 0x1000; /* Firmware code size = 4K */
Alan Cox4023c4742007-06-15 14:45:30 +0100407 i++, bios_addr++) {
408 outw(bios_addr, host->base + ORC_EBIOSADR0);
409 *data32_ptr++ = inb(host->base + ORC_EBIOSDATA); /* Read from BIOS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if ((i % 4) == 3) {
Alan Cox4023c4742007-06-15 14:45:30 +0100411 outl(data32, host->base + ORC_RISCRAM); /* Write every 4 bytes */
412 data32_ptr = (u8 *) & data32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415
Alan Cox4023c4742007-06-15 14:45:30 +0100416 /* Go back and check they match */
417
418 outb(PRGMRST | DOWNLOAD, host->base + ORC_RISCCTL); /* Reset program count 0 */
419 bios_addr -= 0x1000; /* Reset the BIOS adddress */
420 for (i = 0, data32_ptr = (u8 *) & data32; /* Check the code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 i < 0x1000; /* Firmware code size = 4K */
Alan Cox4023c4742007-06-15 14:45:30 +0100422 i++, bios_addr++) {
423 outw(bios_addr, host->base + ORC_EBIOSADR0);
424 *data32_ptr++ = inb(host->base + ORC_EBIOSDATA); /* Read from BIOS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if ((i % 4) == 3) {
Alan Cox4023c4742007-06-15 14:45:30 +0100426 if (inl(host->base + ORC_RISCRAM) != data32) {
427 outb(PRGMRST, host->base + ORC_RISCCTL); /* Reset program to 0 */
428 outb(data, host->base + ORC_GCFG); /*Disable EEPROM programming */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430 }
Alan Cox4023c4742007-06-15 14:45:30 +0100431 data32_ptr = (u8 *) & data32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 }
Alan Cox4023c4742007-06-15 14:45:30 +0100434
435 /* Success */
436 outb(PRGMRST, host->base + ORC_RISCCTL); /* Reset program to 0 */
437 outb(data, host->base + ORC_GCFG); /* Disable EEPROM programming */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 1;
439}
440
441/***************************************************************************/
Alan Cox4023c4742007-06-15 14:45:30 +0100442static void setup_SCBs(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Alan Cox4023c4742007-06-15 14:45:30 +0100444 struct orc_scb *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int i;
Alan Cox4023c4742007-06-15 14:45:30 +0100446 struct orc_extended_scb *escb;
447 dma_addr_t escb_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Alan Cox4023c4742007-06-15 14:45:30 +0100449 /* Setup SCB base and SCB Size registers */
450 outb(ORC_MAXQUEUE, host->base + ORC_SCBSIZE); /* Total number of SCBs */
451 /* SCB base address 0 */
452 outl(host->scb_phys, host->base + ORC_SCBBASE0);
453 /* SCB base address 1 */
454 outl(host->scb_phys, host->base + ORC_SCBBASE1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /* setup scatter list address with one buffer */
Alan Cox4023c4742007-06-15 14:45:30 +0100457 scb = host->scb_virt;
458 escb = host->escb_virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 for (i = 0; i < ORC_MAXQUEUE; i++) {
Alan Cox4023c4742007-06-15 14:45:30 +0100461 escb_phys = (host->escb_phys + (sizeof(struct orc_extended_scb) * i));
462 scb->sg_addr = (u32) escb_phys;
463 scb->sense_addr = (u32) escb_phys;
464 scb->escb = escb;
465 scb->scbidx = i;
466 scb++;
467 escb++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Alan Cox4023c4742007-06-15 14:45:30 +0100471/**
472 * init_alloc_map - initialise allocation map
473 * @host: host map to configure
474 *
475 * Initialise the allocation maps for this device. If the device
476 * is not quiescent the caller must hold the allocation lock
477 */
478
479static void init_alloc_map(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Alan Cox4023c4742007-06-15 14:45:30 +0100481 u8 i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 for (i = 0; i < MAX_CHANNELS; i++) {
484 for (j = 0; j < 8; j++) {
Alan Cox4023c4742007-06-15 14:45:30 +0100485 host->allocation_map[i][j] = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 }
488}
489
Alan Cox4023c4742007-06-15 14:45:30 +0100490/**
491 * init_orchid - initialise the host adapter
492 * @host:host adapter to initialise
493 *
494 * Initialise the controller and if neccessary load the firmware.
495 *
496 * Returns -1 if the initialisation fails.
497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Alan Cox4023c4742007-06-15 14:45:30 +0100499static int init_orchid(struct orc_host * host)
500{
501 u8 *ptr;
502 u16 revision;
503 u8 i;
504
505 init_alloc_map(host);
506 outb(0xFF, host->base + ORC_GIMSK); /* Disable all interrupts */
507
508 if (inb(host->base + ORC_HSTUS) & RREADY) { /* Orchid is ready */
509 revision = orc_read_fwrev(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (revision == 0xFFFF) {
Alan Cox4023c4742007-06-15 14:45:30 +0100511 outb(DEVRST, host->base + ORC_HCTRL); /* Reset Host Adapter */
512 if (wait_chip_ready(host) == 0)
513 return -1;
514 orc_load_firmware(host); /* Download FW */
515 setup_SCBs(host); /* Setup SCB base and SCB Size registers */
516 outb(0x00, host->base + ORC_HCTRL); /* clear HOSTSTOP */
517 if (wait_firmware_ready(host) == 0)
518 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* Wait for firmware ready */
520 } else {
Alan Cox4023c4742007-06-15 14:45:30 +0100521 setup_SCBs(host); /* Setup SCB base and SCB Size registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 } else { /* Orchid is not Ready */
Alan Cox4023c4742007-06-15 14:45:30 +0100524 outb(DEVRST, host->base + ORC_HCTRL); /* Reset Host Adapter */
525 if (wait_chip_ready(host) == 0)
526 return -1;
527 orc_load_firmware(host); /* Download FW */
528 setup_SCBs(host); /* Setup SCB base and SCB Size registers */
529 outb(HDO, host->base + ORC_HCTRL); /* Do Hardware Reset & */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /* clear HOSTSTOP */
Alan Cox4023c4742007-06-15 14:45:30 +0100532 if (wait_firmware_ready(host) == 0) /* Wait for firmware ready */
533 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
Alan Cox4023c4742007-06-15 14:45:30 +0100536 /* Load an EEProm copy into RAM */
537 /* Assumes single threaded at this point */
538 read_eeprom(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Alan Cox4023c4742007-06-15 14:45:30 +0100540 if (nvramp->revision != 1)
541 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Alan Cox4023c4742007-06-15 14:45:30 +0100543 host->scsi_id = nvramp->scsi_id;
544 host->BIOScfg = nvramp->BIOSConfig1;
545 host->max_targets = MAX_TARGETS;
546 ptr = (u8 *) & (nvramp->Target00Config);
547 for (i = 0; i < 16; ptr++, i++) {
548 host->target_flag[i] = *ptr;
549 host->max_tags[i] = ORC_MAXTAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Alan Cox4023c4742007-06-15 14:45:30 +0100551
552 if (nvramp->SCSI0Config & NCC_BUSRESET)
553 host->flags |= HCF_SCSI_RESET;
554 outb(0xFB, host->base + ORC_GIMSK); /* enable RP FIFO interrupt */
555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Alan Cox4023c4742007-06-15 14:45:30 +0100558/**
559 * orc_reset_scsi_bus - perform bus reset
560 * @host: host being reset
561 *
562 * Perform a full bus reset on the adapter.
563 */
564
565static int orc_reset_scsi_bus(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{ /* I need Host Control Block Information */
Alan Cox4023c4742007-06-15 14:45:30 +0100567 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Alan Cox4023c4742007-06-15 14:45:30 +0100569 spin_lock_irqsave(&host->allocation_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Alan Cox4023c4742007-06-15 14:45:30 +0100571 init_alloc_map(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* reset scsi bus */
Alan Cox4023c4742007-06-15 14:45:30 +0100573 outb(SCSIRST, host->base + ORC_HCTRL);
574 /* FIXME: We can spend up to a second with the lock held and
575 interrupts off here */
576 if (wait_scsi_reset_done(host) == 0) {
577 spin_unlock_irqrestore(&host->allocation_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return FAILED;
579 } else {
Alan Cox4023c4742007-06-15 14:45:30 +0100580 spin_unlock_irqrestore(&host->allocation_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return SUCCESS;
582 }
583}
584
Alan Cox4023c4742007-06-15 14:45:30 +0100585/**
586 * orc_device_reset - device reset handler
587 * @host: host to reset
588 * @cmd: command causing the reset
589 * @target; target device
590 *
591 * Reset registers, reset a hanging bus and kill active and disconnected
592 * commands for target w/o soft reset
593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Alan Cox4023c4742007-06-15 14:45:30 +0100595static int orc_device_reset(struct orc_host * host, struct scsi_cmnd *cmd, unsigned int target)
596{ /* I need Host Control Block Information */
597 struct orc_scb *scb;
598 struct orc_extended_scb *escb;
599 struct orc_scb *host_scb;
600 u8 i;
601 unsigned long flags;
602
603 spin_lock_irqsave(&(host->allocation_lock), flags);
604 scb = (struct orc_scb *) NULL;
605 escb = (struct orc_extended_scb *) NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* setup scatter list address with one buffer */
Alan Cox4023c4742007-06-15 14:45:30 +0100608 host_scb = host->scb_virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Alan Cox4023c4742007-06-15 14:45:30 +0100610 /* FIXME: is this safe if we then fail to issue the reset or race
611 a completion ? */
612 init_alloc_map(host);
613
614 /* Find the scb corresponding to the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 for (i = 0; i < ORC_MAXQUEUE; i++) {
Alan Cox4023c4742007-06-15 14:45:30 +0100616 escb = host_scb->escb;
617 if (host_scb->status && escb->srb == cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
Alan Cox4023c4742007-06-15 14:45:30 +0100619 host_scb++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
622 if (i == ORC_MAXQUEUE) {
Alan Cox4023c4742007-06-15 14:45:30 +0100623 printk(KERN_ERR "Unable to Reset - No SCB Found\n");
624 spin_unlock_irqrestore(&(host->allocation_lock), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return FAILED;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Alan Cox4023c4742007-06-15 14:45:30 +0100628 /* Allocate a new SCB for the reset command to the firmware */
629 if ((scb = __orc_alloc_scb(host)) == NULL) {
630 /* Can't happen.. */
631 spin_unlock_irqrestore(&(host->allocation_lock), flags);
632 return FAILED;
633 }
634
635 /* Reset device is handled by the firmare, we fill in an SCB and
636 fire it at the controller, it does the rest */
637 scb->opcode = ORC_BUSDEVRST;
638 scb->target = target;
639 scb->hastat = 0;
640 scb->tastat = 0;
641 scb->status = 0x0;
642 scb->link = 0xFF;
643 scb->reserved0 = 0;
644 scb->reserved1 = 0;
645 scb->xferlen = 0;
646 scb->sg_len = 0;
647
648 escb->srb = NULL;
649 escb->srb = cmd;
650 orc_exec_scb(host, scb); /* Start execute SCB */
651 spin_unlock_irqrestore(&host->allocation_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return SUCCESS;
653}
654
Alan Cox4023c4742007-06-15 14:45:30 +0100655/**
656 * __orc_alloc_scb - allocate an SCB
657 * @host: host to allocate from
658 *
659 * Allocate an SCB and return a pointer to the SCB object. NULL
660 * is returned if no SCB is free. The caller must already hold
661 * the allocator lock at this point.
662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Alan Cox4023c4742007-06-15 14:45:30 +0100664
665static struct orc_scb *__orc_alloc_scb(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Alan Cox4023c4742007-06-15 14:45:30 +0100667 u8 channel;
668 unsigned long idx;
669 u8 index;
670 u8 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Alan Cox4023c4742007-06-15 14:45:30 +0100672 channel = host->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 for (i = 0; i < 8; i++) {
674 for (index = 0; index < 32; index++) {
Alan Cox4023c4742007-06-15 14:45:30 +0100675 if ((host->allocation_map[channel][i] >> index) & 0x01) {
676 host->allocation_map[channel][i] &= ~(1 << index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678 }
679 }
680 idx = index + 32 * i;
Alan Cox4023c4742007-06-15 14:45:30 +0100681 /* Translate the index to a structure instance */
682 return (struct orc_scb *) ((unsigned long) host->scb_virt + (idx * sizeof(struct orc_scb)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Alan Cox4023c4742007-06-15 14:45:30 +0100684 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Alan Cox4023c4742007-06-15 14:45:30 +0100687/**
688 * orc_alloc_scb - allocate an SCB
689 * @host: host to allocate from
690 *
691 * Allocate an SCB and return a pointer to the SCB object. NULL
692 * is returned if no SCB is free.
693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Alan Cox4023c4742007-06-15 14:45:30 +0100695static struct orc_scb *orc_alloc_scb(struct orc_host * host)
696{
697 struct orc_scb *scb;
698 unsigned long flags;
699
700 spin_lock_irqsave(&host->allocation_lock, flags);
701 scb = __orc_alloc_scb(host);
702 spin_unlock_irqrestore(&host->allocation_lock, flags);
703 return scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Alan Cox4023c4742007-06-15 14:45:30 +0100706/**
707 * orc_release_scb - release an SCB
708 * @host: host owning the SCB
709 * @scb: SCB that is now free
710 *
711 * Called to return a completed SCB to the allocation pool. Before
712 * calling the SCB must be out of use on both the host and the HA.
713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Alan Cox4023c4742007-06-15 14:45:30 +0100715static void orc_release_scb(struct orc_host *host, struct orc_scb *scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Alan Cox4023c4742007-06-15 14:45:30 +0100717 unsigned long flags;
718 u8 index, i, channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Alan Cox4023c4742007-06-15 14:45:30 +0100720 spin_lock_irqsave(&(host->allocation_lock), flags);
721 channel = host->index; /* Channel */
722 index = scb->scbidx;
723 i = index / 32;
724 index %= 32;
725 host->allocation_map[channel][i] |= (1 << index);
726 spin_unlock_irqrestore(&(host->allocation_lock), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Alan Cox4023c4742007-06-15 14:45:30 +0100729/**
730 * orchid_abort_scb - abort a command
731 *
732 * Abort a queued command that has been passed to the firmware layer
733 * if possible. This is all handled by the firmware. We aks the firmware
734 * and it either aborts the command or fails
735 */
736
737static int orchid_abort_scb(struct orc_host * host, struct orc_scb * scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Alan Cox4023c4742007-06-15 14:45:30 +0100739 unsigned char data, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Alan Cox4023c4742007-06-15 14:45:30 +0100741 outb(ORC_CMD_ABORT_SCB, host->base + ORC_HDATA); /* Write command */
742 outb(HDO, host->base + ORC_HCTRL);
743 if (wait_HDO_off(host) == 0) /* Wait HDO off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return 0;
745
Alan Cox4023c4742007-06-15 14:45:30 +0100746 outb(scb->scbidx, host->base + ORC_HDATA); /* Write address */
747 outb(HDO, host->base + ORC_HCTRL);
748 if (wait_HDO_off(host) == 0) /* Wait HDO off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return 0;
750
Alan Cox4023c4742007-06-15 14:45:30 +0100751 if (wait_hdi_set(host, &data) == 0) /* Wait HDI set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
Alan Cox4023c4742007-06-15 14:45:30 +0100753 status = inb(host->base + ORC_HDATA);
754 outb(data, host->base + ORC_HSTUS); /* Clear HDI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Alan Cox4023c4742007-06-15 14:45:30 +0100756 if (status == 1) /* 0 - Successfully */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0; /* 1 - Fail */
758 return 1;
759}
760
Alan Cox4023c4742007-06-15 14:45:30 +0100761static int inia100_abort_cmd(struct orc_host * host, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Alan Cox4023c4742007-06-15 14:45:30 +0100763 struct orc_extended_scb *escb;
764 struct orc_scb *scb;
765 u8 i;
766 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Alan Cox4023c4742007-06-15 14:45:30 +0100768 spin_lock_irqsave(&(host->allocation_lock), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Alan Cox4023c4742007-06-15 14:45:30 +0100770 scb = host->scb_virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Alan Cox4023c4742007-06-15 14:45:30 +0100772 /* Walk the queue until we find the SCB that belongs to the command
773 block. This isn't a performance critical path so a walk in the park
774 here does no harm */
775
776 for (i = 0; i < ORC_MAXQUEUE; i++, scb++) {
777 escb = scb->escb;
778 if (scb->status && escb->srb == cmd) {
779 if (scb->tag_msg == 0) {
780 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 } else {
Alan Cox4023c4742007-06-15 14:45:30 +0100782 /* Issue an ABORT to the firmware */
783 if (orchid_abort_scb(host, scb)) {
784 escb->srb = NULL;
785 spin_unlock_irqrestore(&host->allocation_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return SUCCESS;
Alan Cox4023c4742007-06-15 14:45:30 +0100787 } else
788 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 }
791 }
Alan Cox4023c4742007-06-15 14:45:30 +0100792out:
793 spin_unlock_irqrestore(&host->allocation_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return FAILED;
795}
796
Alan Cox4023c4742007-06-15 14:45:30 +0100797/**
798 * orc_interrupt - IRQ processing
799 * @host: Host causing the interrupt
800 *
801 * This function is called from the IRQ handler and protected
802 * by the host lock. While the controller reports that there are
803 * scb's for processing we pull them off the controller, turn the
804 * index into a host address pointer to the scb and call the scb
805 * handler.
806 *
807 * Returns IRQ_HANDLED if any SCBs were processed, IRQ_NONE otherwise
808 */
809
810static irqreturn_t orc_interrupt(struct orc_host * host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Alan Cox4023c4742007-06-15 14:45:30 +0100812 u8 scb_index;
813 struct orc_scb *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Alan Cox4023c4742007-06-15 14:45:30 +0100815 /* Check if we have an SCB queued for servicing */
816 if (inb(host->base + ORC_RQUEUECNT) == 0)
817 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 do {
Alan Cox4023c4742007-06-15 14:45:30 +0100820 /* Get the SCB index of the SCB to service */
821 scb_index = inb(host->base + ORC_RQUEUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Alan Cox4023c4742007-06-15 14:45:30 +0100823 /* Translate it back to a host pointer */
824 scb = (struct orc_scb *) ((unsigned long) host->scb_virt + (unsigned long) (sizeof(struct orc_scb) * scb_index));
825 scb->status = 0x0;
826 /* Process the SCB */
827 inia100_scb_handler(host, scb);
828 } while (inb(host->base + ORC_RQUEUECNT));
829 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830} /* End of I1060Interrupt() */
831
Alan Cox4023c4742007-06-15 14:45:30 +0100832/**
833 * inia100_build_scb - build SCB
834 * @host: host owing the control block
835 * @scb: control block to use
836 * @cmd: Mid layer command
837 *
838 * Build a host adapter control block from the SCSI mid layer command
839 */
840
841static void inia100_build_scb(struct orc_host * host, struct orc_scb * scb, struct scsi_cmnd * cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{ /* Create corresponding SCB */
FUJITA Tomonori985c0a72007-05-14 20:26:37 +0900843 struct scatterlist *sg;
Alan Cox4023c4742007-06-15 14:45:30 +0100844 struct orc_sgent *sgent; /* Pointer to SG list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 int i, count_sg;
Alan Cox4023c4742007-06-15 14:45:30 +0100846 struct orc_extended_scb *escb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Alan Cox4023c4742007-06-15 14:45:30 +0100848 /* Links between the escb, scb and Linux scsi midlayer cmd */
849 escb = scb->escb;
850 escb->srb = cmd;
851 sgent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Alan Cox4023c4742007-06-15 14:45:30 +0100853 /* Set up the SCB to do a SCSI command block */
854 scb->opcode = ORC_EXECSCSI;
855 scb->flags = SCF_NO_DCHK; /* Clear done bit */
856 scb->target = cmd->device->id;
857 scb->lun = cmd->device->lun;
858 scb->reserved0 = 0;
859 scb->reserved1 = 0;
860 scb->sg_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Alan Cox4023c4742007-06-15 14:45:30 +0100862 scb->xferlen = (u32) scsi_bufflen(cmd);
863 sgent = (struct orc_sgent *) & escb->sglist[0];
FUJITA Tomonori985c0a72007-05-14 20:26:37 +0900864
Alan Cox4023c4742007-06-15 14:45:30 +0100865 count_sg = scsi_dma_map(cmd);
FUJITA Tomonori985c0a72007-05-14 20:26:37 +0900866 BUG_ON(count_sg < 0);
Alan Cox4023c4742007-06-15 14:45:30 +0100867
868 /* Build the scatter gather lists */
FUJITA Tomonori985c0a72007-05-14 20:26:37 +0900869 if (count_sg) {
Alan Cox4023c4742007-06-15 14:45:30 +0100870 scb->sg_len = (u32) (count_sg * 8);
871 scsi_for_each_sg(cmd, sg, count_sg, i) {
872 sgent->base = (u32) sg_dma_address(sg);
873 sgent->length = (u32) sg_dma_len(sg);
874 sgent++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
FUJITA Tomonori985c0a72007-05-14 20:26:37 +0900876 } else {
Alan Cox4023c4742007-06-15 14:45:30 +0100877 scb->sg_len = 0;
878 sgent->base = 0;
879 sgent->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Alan Cox4023c4742007-06-15 14:45:30 +0100881 scb->sg_addr = (u32) scb->sense_addr;
882 scb->hastat = 0;
883 scb->tastat = 0;
884 scb->link = 0xFF;
885 scb->sense_len = SENSE_SIZE;
886 scb->cdb_len = cmd->cmd_len;
887 if (scb->cdb_len >= IMAX_CDB) {
888 printk("max cdb length= %x\b", cmd->cmd_len);
889 scb->cdb_len = IMAX_CDB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Alan Cox4023c4742007-06-15 14:45:30 +0100891 scb->ident = cmd->device->lun | DISC_ALLOW;
892 if (cmd->device->tagged_supported) { /* Tag Support */
893 scb->tag_msg = SIMPLE_QUEUE_TAG; /* Do simple tag only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 } else {
Alan Cox4023c4742007-06-15 14:45:30 +0100895 scb->tag_msg = 0; /* No tag support */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
Alan Cox4023c4742007-06-15 14:45:30 +0100897 memcpy(&scb->cdb[0], &cmd->cmnd, scb->cdb_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Alan Cox4023c4742007-06-15 14:45:30 +0100900/**
901 * inia100_queue - queue command with host
902 * @cmd: Command block
903 * @done: Completion function
904 *
905 * Called by the mid layer to queue a command. Process the command
906 * block, build the host specific scb structures and if there is room
907 * queue the command down to the controller
908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Alan Cox4023c4742007-06-15 14:45:30 +0100910static int inia100_queue(struct scsi_cmnd * cmd, void (*done) (struct scsi_cmnd *))
911{
912 struct orc_scb *scb;
913 struct orc_host *host; /* Point to Host adapter control block */
914
915 host = (struct orc_host *) cmd->device->host->hostdata;
916 cmd->scsi_done = done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* Get free SCSI control block */
Alan Cox4023c4742007-06-15 14:45:30 +0100918 if ((scb = orc_alloc_scb(host)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return SCSI_MLQUEUE_HOST_BUSY;
920
Alan Cox4023c4742007-06-15 14:45:30 +0100921 inia100_build_scb(host, scb, cmd);
922 orc_exec_scb(host, scb); /* Start execute SCB */
923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926/*****************************************************************************
927 Function name : inia100_abort
928 Description : Abort a queued command.
929 (commands that are on the bus can't be aborted easily)
Alan Cox4023c4742007-06-15 14:45:30 +0100930 Input : host - Pointer to host adapter structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 Output : None.
932 Return : pSRB - Pointer to SCSI request block.
933*****************************************************************************/
Alan Cox4023c4742007-06-15 14:45:30 +0100934static int inia100_abort(struct scsi_cmnd * cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Alan Cox4023c4742007-06-15 14:45:30 +0100936 struct orc_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Alan Cox4023c4742007-06-15 14:45:30 +0100938 host = (struct orc_host *) cmd->device->host->hostdata;
939 return inia100_abort_cmd(host, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942/*****************************************************************************
943 Function name : inia100_reset
944 Description : Reset registers, reset a hanging bus and
945 kill active and disconnected commands for target w/o soft reset
Alan Cox4023c4742007-06-15 14:45:30 +0100946 Input : host - Pointer to host adapter structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 Output : None.
948 Return : pSRB - Pointer to SCSI request block.
949*****************************************************************************/
Alan Cox4023c4742007-06-15 14:45:30 +0100950static int inia100_bus_reset(struct scsi_cmnd * cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{ /* I need Host Control Block Information */
Alan Cox4023c4742007-06-15 14:45:30 +0100952 struct orc_host *host;
953 host = (struct orc_host *) cmd->device->host->hostdata;
954 return orc_reset_scsi_bus(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/*****************************************************************************
958 Function name : inia100_device_reset
959 Description : Reset the device
Alan Cox4023c4742007-06-15 14:45:30 +0100960 Input : host - Pointer to host adapter structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 Output : None.
962 Return : pSRB - Pointer to SCSI request block.
963*****************************************************************************/
Alan Cox4023c4742007-06-15 14:45:30 +0100964static int inia100_device_reset(struct scsi_cmnd * cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{ /* I need Host Control Block Information */
Alan Cox4023c4742007-06-15 14:45:30 +0100966 struct orc_host *host;
967 host = (struct orc_host *) cmd->device->host->hostdata;
968 return orc_device_reset(host, cmd, scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970}
971
Alan Cox4023c4742007-06-15 14:45:30 +0100972/**
973 * inia100_scb_handler - interrupt callback
974 * @host: Host causing the interrupt
975 * @scb: SCB the controller returned as needing processing
976 *
977 * Perform completion processing on a control block. Do the conversions
978 * from host to SCSI midlayer error coding, save any sense data and
979 * the complete with the midlayer and recycle the scb.
980 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Alan Cox4023c4742007-06-15 14:45:30 +0100982static void inia100_scb_handler(struct orc_host *host, struct orc_scb *scb)
983{
984 struct scsi_cmnd *cmd; /* Pointer to SCSI request block */
985 struct orc_extended_scb *escb;
986
987 escb = scb->escb;
988 if ((cmd = (struct scsi_cmnd *) escb->srb) == NULL) {
989 printk(KERN_ERR "inia100_scb_handler: SRB pointer is empty\n");
990 orc_release_scb(host, scb); /* Release SCB for current channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return;
992 }
Alan Cox4023c4742007-06-15 14:45:30 +0100993 escb->srb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Alan Cox4023c4742007-06-15 14:45:30 +0100995 switch (scb->hastat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 case 0x0:
997 case 0xa: /* Linked command complete without error and linked normally */
998 case 0xb: /* Linked command complete without error interrupt generated */
Alan Cox4023c4742007-06-15 14:45:30 +0100999 scb->hastat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 break;
1001
1002 case 0x11: /* Selection time out-The initiator selection or target
1003 reselection was not complete within the SCSI Time out period */
Alan Cox4023c4742007-06-15 14:45:30 +01001004 scb->hastat = DID_TIME_OUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 break;
1006
1007 case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
1008 phase sequence was requested by the target. The host adapter
1009 will generate a SCSI Reset Condition, notifying the host with
1010 a SCRD interrupt */
Alan Cox4023c4742007-06-15 14:45:30 +01001011 scb->hastat = DID_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 break;
1013
1014 case 0x1a: /* SCB Aborted. 07/21/98 */
Alan Cox4023c4742007-06-15 14:45:30 +01001015 scb->hastat = DID_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017
1018 case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
1019 than was allocated by the Data Length field or the sum of the
1020 Scatter / Gather Data Length fields. */
1021 case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
1022 case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid. */
1023
1024 default:
Alan Cox4023c4742007-06-15 14:45:30 +01001025 printk(KERN_DEBUG "inia100: %x %x\n", scb->hastat, scb->tastat);
1026 scb->hastat = DID_ERROR; /* Couldn't find any better */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 break;
1028 }
1029
Alan Cox4023c4742007-06-15 14:45:30 +01001030 if (scb->tastat == 2) { /* Check condition */
1031 memcpy((unsigned char *) &cmd->sense_buffer[0],
1032 (unsigned char *) &escb->sglist[0], SENSE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Alan Cox4023c4742007-06-15 14:45:30 +01001034 cmd->result = scb->tastat | (scb->hastat << 16);
1035 scsi_dma_unmap(cmd);
1036 cmd->scsi_done(cmd); /* Notify system DONE */
1037 orc_release_scb(host, scb); /* Release SCB for current channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Alan Cox4023c4742007-06-15 14:45:30 +01001040/**
1041 * inia100_intr - interrupt handler
1042 * @irqno: Interrupt value
1043 * @devid: Host adapter
1044 *
1045 * Entry point for IRQ handling. All the real work is performed
1046 * by orc_interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 */
David Howells7d12e782006-10-05 14:55:46 +01001048static irqreturn_t inia100_intr(int irqno, void *devid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Alan Cox4023c4742007-06-15 14:45:30 +01001050 struct Scsi_Host *shost = (struct Scsi_Host *)devid;
1051 struct orc_host *host = (struct orc_host *)shost->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 unsigned long flags;
Alan Cox4023c4742007-06-15 14:45:30 +01001053 irqreturn_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Alan Cox4023c4742007-06-15 14:45:30 +01001055 spin_lock_irqsave(shost->host_lock, flags);
1056 res = orc_interrupt(host);
1057 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Alan Cox4023c4742007-06-15 14:45:30 +01001059 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062static struct scsi_host_template inia100_template = {
1063 .proc_name = "inia100",
1064 .name = inia100_REVID,
1065 .queuecommand = inia100_queue,
1066 .eh_abort_handler = inia100_abort,
1067 .eh_bus_reset_handler = inia100_bus_reset,
1068 .eh_device_reset_handler = inia100_device_reset,
1069 .can_queue = 1,
1070 .this_id = 1,
1071 .sg_tablesize = SG_ALL,
1072 .cmd_per_lun = 1,
1073 .use_clustering = ENABLE_CLUSTERING,
FUJITA Tomonori9cb83c72007-10-16 11:24:32 +02001074 .use_sg_chaining = ENABLE_SG_CHAINING,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075};
1076
1077static int __devinit inia100_probe_one(struct pci_dev *pdev,
1078 const struct pci_device_id *id)
1079{
1080 struct Scsi_Host *shost;
Alan Cox4023c4742007-06-15 14:45:30 +01001081 struct orc_host *host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 unsigned long port, bios;
1083 int error = -ENODEV;
1084 u32 sz;
Alan Cox4023c4742007-06-15 14:45:30 +01001085 unsigned long biosaddr;
1086 char *bios_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (pci_enable_device(pdev))
1089 goto out;
Matthias Gehre910638a2006-03-28 01:56:48 -08001090 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 printk(KERN_WARNING "Unable to set 32bit DMA "
1092 "on inia100 adapter, ignoring.\n");
1093 goto out_disable_device;
1094 }
1095
1096 pci_set_master(pdev);
1097
1098 port = pci_resource_start(pdev, 0);
1099 if (!request_region(port, 256, "inia100")) {
1100 printk(KERN_WARNING "inia100: io port 0x%lx, is busy.\n", port);
1101 goto out_disable_device;
1102 }
1103
Robert P. J. Day4c3ee822007-02-17 19:18:52 +01001104 /* <02> read from base address + 0x50 offset to get the bios value. */
Alan Cox4023c4742007-06-15 14:45:30 +01001105 bios = inw(port + 0x50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107
Alan Cox4023c4742007-06-15 14:45:30 +01001108 shost = scsi_host_alloc(&inia100_template, sizeof(struct orc_host));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (!shost)
1110 goto out_release_region;
1111
Alan Cox4023c4742007-06-15 14:45:30 +01001112 host = (struct orc_host *)shost->hostdata;
1113 host->pdev = pdev;
1114 host->base = port;
1115 host->BIOScfg = bios;
1116 spin_lock_init(&host->allocation_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /* Get total memory needed for SCB */
Alan Cox4023c4742007-06-15 14:45:30 +01001119 sz = ORC_MAXQUEUE * sizeof(struct orc_scb);
1120 host->scb_virt = pci_alloc_consistent(pdev, sz,
1121 &host->scb_phys);
1122 if (!host->scb_virt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 printk("inia100: SCB memory allocation error\n");
1124 goto out_host_put;
1125 }
Alan Cox4023c4742007-06-15 14:45:30 +01001126 memset(host->scb_virt, 0, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /* Get total memory needed for ESCB */
Alan Cox4023c4742007-06-15 14:45:30 +01001129 sz = ORC_MAXQUEUE * sizeof(struct orc_extended_scb);
1130 host->escb_virt = pci_alloc_consistent(pdev, sz,
1131 &host->escb_phys);
1132 if (!host->escb_virt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 printk("inia100: ESCB memory allocation error\n");
1134 goto out_free_scb_array;
1135 }
Alan Cox4023c4742007-06-15 14:45:30 +01001136 memset(host->escb_virt, 0, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Alan Cox4023c4742007-06-15 14:45:30 +01001138 biosaddr = host->BIOScfg;
1139 biosaddr = (biosaddr << 4);
1140 bios_phys = phys_to_virt(biosaddr);
1141 if (init_orchid(host)) { /* Initialize orchid chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 printk("inia100: initial orchid fail!!\n");
1143 goto out_free_escb_array;
1144 }
1145
Alan Cox4023c4742007-06-15 14:45:30 +01001146 shost->io_port = host->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 shost->n_io_port = 0xff;
1148 shost->can_queue = ORC_MAXQUEUE;
1149 shost->unique_id = shost->io_port;
Alan Cox4023c4742007-06-15 14:45:30 +01001150 shost->max_id = host->max_targets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 shost->max_lun = 16;
Alan Cox4023c4742007-06-15 14:45:30 +01001152 shost->irq = pdev->irq;
1153 shost->this_id = host->scsi_id; /* Assign HCS index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 shost->sg_tablesize = TOTAL_SG_ENTRY;
1155
1156 /* Initial orc chip */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001157 error = request_irq(pdev->irq, inia100_intr, IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 "inia100", shost);
1159 if (error < 0) {
1160 printk(KERN_WARNING "inia100: unable to get irq %d\n",
1161 pdev->irq);
1162 goto out_free_escb_array;
1163 }
1164
1165 pci_set_drvdata(pdev, shost);
1166
1167 error = scsi_add_host(shost, &pdev->dev);
1168 if (error)
1169 goto out_free_irq;
1170
1171 scsi_scan_host(shost);
1172 return 0;
1173
Alan Cox4023c4742007-06-15 14:45:30 +01001174out_free_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 free_irq(shost->irq, shost);
Alan Cox4023c4742007-06-15 14:45:30 +01001176out_free_escb_array:
1177 pci_free_consistent(pdev, ORC_MAXQUEUE * sizeof(struct orc_extended_scb),
1178 host->escb_virt, host->escb_phys);
1179out_free_scb_array:
1180 pci_free_consistent(pdev, ORC_MAXQUEUE * sizeof(struct orc_scb),
1181 host->scb_virt, host->scb_phys);
1182out_host_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 scsi_host_put(shost);
Alan Cox4023c4742007-06-15 14:45:30 +01001184out_release_region:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 release_region(port, 256);
Alan Cox4023c4742007-06-15 14:45:30 +01001186out_disable_device:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 pci_disable_device(pdev);
Alan Cox4023c4742007-06-15 14:45:30 +01001188out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return error;
1190}
1191
1192static void __devexit inia100_remove_one(struct pci_dev *pdev)
1193{
1194 struct Scsi_Host *shost = pci_get_drvdata(pdev);
Alan Cox4023c4742007-06-15 14:45:30 +01001195 struct orc_host *host = (struct orc_host *)shost->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 scsi_remove_host(shost);
1198
1199 free_irq(shost->irq, shost);
Alan Cox4023c4742007-06-15 14:45:30 +01001200 pci_free_consistent(pdev, ORC_MAXQUEUE * sizeof(struct orc_extended_scb),
1201 host->escb_virt, host->escb_phys);
1202 pci_free_consistent(pdev, ORC_MAXQUEUE * sizeof(struct orc_scb),
1203 host->scb_virt, host->scb_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 release_region(shost->io_port, 256);
1205
1206 scsi_host_put(shost);
1207}
1208
1209static struct pci_device_id inia100_pci_tbl[] = {
1210 {PCI_VENDOR_ID_INIT, 0x1060, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1211 {0,}
1212};
1213MODULE_DEVICE_TABLE(pci, inia100_pci_tbl);
1214
1215static struct pci_driver inia100_pci_driver = {
1216 .name = "inia100",
1217 .id_table = inia100_pci_tbl,
1218 .probe = inia100_probe_one,
1219 .remove = __devexit_p(inia100_remove_one),
1220};
1221
1222static int __init inia100_init(void)
1223{
Henrik Kretzschmardcbccbde2006-09-25 16:58:58 -07001224 return pci_register_driver(&inia100_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227static void __exit inia100_exit(void)
1228{
1229 pci_unregister_driver(&inia100_pci_driver);
1230}
1231
1232MODULE_DESCRIPTION("Initio A100U2W SCSI driver");
1233MODULE_AUTHOR("Initio Corporation");
1234MODULE_LICENSE("Dual BSD/GPL");
1235
1236module_init(inia100_init);
1237module_exit(inia100_exit);