make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / scsi / ini9100u.c
1 /**************************************************************************
2  * Initio 9100 device driver for Linux.
3  *
4  * Copyright (c) 1994-1998 Initio Corporation
5  * Copyright (c) 1998 Bas Vermeulen <bvermeul@blackstar.xs4all.nl>
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  *
22  * --------------------------------------------------------------------------
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  * 1. Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions, and the following disclaimer,
29  *    without modification, immediately at the beginning of the file.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  *    notice, this list of conditions and the following disclaimer in the
32  *    documentation and/or other materials provided with the distribution.
33  * 3. The name of the author may not be used to endorse or promote products
34  *    derived from this software without specific prior written permission.
35  *
36  * Where this Software is combined with software released under the terms of 
37  * the GNU General Public License ("GPL") and the terms of the GPL would require the 
38  * combined work to also be released under the terms of the GPL, the terms
39  * and conditions of this License will apply in addition to those of the
40  * GPL with the exception of any terms or conditions of this License that
41  * conflict with, or are expressly prohibited by, the GPL.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
47  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  *
55  *************************************************************************
56  *
57  * DESCRIPTION:
58  *
59  * This is the Linux low-level SCSI driver for Initio INI-9X00U/UW SCSI host
60  * adapters
61  *
62  * 08/06/97 hc  - v1.01h
63  *              - Support inic-940 and inic-935
64  * 09/26/97 hc  - v1.01i
65  *              - Make correction from J.W. Schultz suggestion
66  * 10/13/97 hc  - Support reset function
67  * 10/21/97 hc  - v1.01j
68  *              - Support 32 LUN (SCSI 3)
69  * 01/14/98 hc  - v1.01k
70  *              - Fix memory allocation problem
71  * 03/04/98 hc  - v1.01l
72  *              - Fix tape rewind which will hang the system problem
73  *              - Set can_queue to tul_num_scb
74  * 06/25/98 hc  - v1.01m
75  *              - Get it work for kernel version >= 2.1.75
76  *              - Dynamic assign SCSI bus reset holding time in init_tulip()
77  * 07/02/98 hc  - v1.01n
78  *              - Support 0002134A
79  * 08/07/98 hc  - v1.01o
80  *              - Change the tul_abort_srb routine to use scsi_done. <01>
81  * 09/07/98 hl  - v1.02
82  *              - Change the INI9100U define and proc_dir_entry to
83  *                reflect the newer Kernel 2.1.118, but the v1.o1o
84  *                should work with Kernel 2.1.118.
85  * 09/20/98 wh  - v1.02a
86  *              - Support Abort command.
87  *              - Handle reset routine.
88  * 09/21/98 hl  - v1.03
89  *              - remove comments.
90  * 12/09/98 bv  - v1.03a
91  *              - Removed unused code
92  * 12/13/98 bv  - v1.03b
93  *              - Remove cli() locking for kernels >= 2.1.95. This uses
94  *                spinlocks to serialize access to the pSRB_head and
95  *                pSRB_tail members of the HCS structure.
96  * 09/01/99 bv  - v1.03d
97  *              - Fixed a deadlock problem in SMP.
98  * 21/01/99 bv  - v1.03e
99  *              - Add support for the Domex 3192U PCI SCSI
100  *                This is a slightly modified patch by
101  *                Brian Macy <bmacy@sunshinecomputing.com>
102  * 22/02/99 bv  - v1.03f
103  *              - Didn't detect the INIC-950 in 2.0.x correctly.
104  *                Now fixed.
105  * 05/07/99 bv  - v1.03g
106  *              - Changed the assumption that HZ = 100
107  **************************************************************************/
108
109 #define CVT_LINUX_VERSION(V,P,S)        (V * 65536 + P * 256 + S)
110
111 #ifndef LINUX_VERSION_CODE
112 #include <linux/version.h>
113 #endif
114
115 #include <linux/module.h>
116
117 #include <stdarg.h>
118 #include <asm/irq.h>
119 #include <linux/errno.h>
120 #include <linux/delay.h>
121 #include <linux/pci.h>
122 #include <linux/init.h>
123 #include <linux/blk.h>
124 #include <linux/spinlock.h>
125 #include <linux/stat.h>
126 #include <linux/config.h>
127
128 #include <linux/kernel.h>
129 #include <linux/string.h>
130 #include <linux/ioport.h>
131 #include <linux/sched.h>
132 #include <linux/proc_fs.h>
133 #include <asm/io.h>
134 #include "scsi.h"
135 #include "sd.h"
136 #include "hosts.h"
137 #include <linux/slab.h>
138 #include "ini9100u.h"
139
140 #ifdef DEBUG_i91u
141 unsigned int i91u_debug = DEBUG_DEFAULT;
142 #endif
143
144 static Scsi_Host_Template driver_template = INI9100U;
145 #include "scsi_module.c"
146
147 char *i91uCopyright = "Copyright (C) 1996-98";
148 char *i91uInitioName = "by Initio Corporation";
149 char *i91uProductName = "INI-9X00U/UW";
150 char *i91uVersion = "v1.03g";
151
152 #define TULSZ(sz)     (sizeof(sz) / sizeof(sz[0]))
153 #define TUL_RDWORD(x,y)         (short)(inl((int)((ULONG)((ULONG)x+(UCHAR)y)) ))
154
155 /* set by i91_setup according to the command line */
156 static int setup_called = 0;
157
158 static int tul_num_ch = 4;      /* Maximum 4 adapters           */
159 static int tul_num_scb;
160 static int tul_tag_enable = 1;
161 static SCB *tul_scb;
162
163 #ifdef DEBUG_i91u
164 static int setup_debug = 0;
165 #endif
166
167 static char *setup_str = (char *) NULL;
168
169 static void i91u_intr0(int irq, void *dev_id, struct pt_regs *);
170 static void i91u_intr1(int irq, void *dev_id, struct pt_regs *);
171 static void i91u_intr2(int irq, void *dev_id, struct pt_regs *);
172 static void i91u_intr3(int irq, void *dev_id, struct pt_regs *);
173 static void i91u_intr4(int irq, void *dev_id, struct pt_regs *);
174 static void i91u_intr5(int irq, void *dev_id, struct pt_regs *);
175 static void i91u_intr6(int irq, void *dev_id, struct pt_regs *);
176 static void i91u_intr7(int irq, void *dev_id, struct pt_regs *);
177
178 static void i91u_panic(char *msg);
179
180 static void i91uSCBPost(BYTE * pHcb, BYTE * pScb);
181
182                                 /* ---- EXTERNAL FUNCTIONS ---- */
183                                         /* Get total number of adapters */
184 extern void init_i91uAdapter_table(void);
185 extern int Addi91u_into_Adapter_table(WORD, WORD, BYTE, BYTE, BYTE);
186 extern int tul_ReturnNumberOfAdapters(void);
187 extern void get_tulipPCIConfig(HCS * pHCB, int iChannel_index);
188 extern int init_tulip(HCS * pHCB, SCB * pSCB, int tul_num_scb, BYTE * pbBiosAdr, int reset_time);
189 extern SCB *tul_alloc_scb(HCS * pHCB);
190 extern int tul_abort_srb(HCS * pHCB, Scsi_Cmnd * pSRB);
191 extern void tul_exec_scb(HCS * pHCB, SCB * pSCB);
192 extern void tul_release_scb(HCS * pHCB, SCB * pSCB);
193 extern void tul_stop_bm(HCS * pHCB);
194 extern int tul_reset_scsi(HCS * pCurHcb, int seconds);
195 extern int tul_isr(HCS * pHCB);
196 extern int tul_reset(HCS * pHCB, Scsi_Cmnd * pSRB, unsigned char target);
197 extern int tul_reset_scsi_bus(HCS * pCurHcb);
198 extern int tul_device_reset(HCS * pCurHcb, ULONG pSrb, unsigned int target, unsigned int ResetFlags);
199                                 /* ---- EXTERNAL VARIABLES ---- */
200 extern HCS tul_hcs[];
201
202 const PCI_ID i91u_pci_devices[] = {
203         { INI_VENDOR_ID, I950_DEVICE_ID },
204         { INI_VENDOR_ID, I940_DEVICE_ID },
205         { INI_VENDOR_ID, I935_DEVICE_ID },
206         { INI_VENDOR_ID, I920_DEVICE_ID },
207         { DMX_VENDOR_ID, I920_DEVICE_ID },
208 };
209
210 /*
211  *  queue services:
212  */
213 /*****************************************************************************
214  Function name  : i91uAppendSRBToQueue
215  Description    : This function will push current request into save list
216  Input          : pSRB  -       Pointer to SCSI request block.
217                   pHCB  -       Pointer to host adapter structure
218  Output         : None.
219  Return         : None.
220 *****************************************************************************/
221 static void i91uAppendSRBToQueue(HCS * pHCB, Scsi_Cmnd * pSRB)
222 {
223         ULONG flags;
224         spin_lock_irqsave(&(pHCB->pSRB_lock), flags);
225
226         pSRB->next = NULL;      /* Pointer to next              */
227
228         if (pHCB->pSRB_head == NULL)
229                 pHCB->pSRB_head = pSRB;
230         else
231                 pHCB->pSRB_tail->next = pSRB;   /* Pointer to next              */
232         pHCB->pSRB_tail = pSRB;
233
234         spin_unlock_irqrestore(&(pHCB->pSRB_lock), flags);
235         return;
236 }
237
238 /*****************************************************************************
239  Function name  : i91uPopSRBFromQueue
240  Description    : This function will pop current request from save list
241  Input          : pHCB  -       Pointer to host adapter structure
242  Output         : None.
243  Return         : pSRB  -       Pointer to SCSI request block.
244 *****************************************************************************/
245 static Scsi_Cmnd *i91uPopSRBFromQueue(HCS * pHCB)
246 {
247         Scsi_Cmnd *pSRB;
248         ULONG flags;
249
250         spin_lock_irqsave(&(pHCB->pSRB_lock), flags);
251
252         if ((pSRB = pHCB->pSRB_head) != NULL) {
253                 pHCB->pSRB_head = pHCB->pSRB_head->next;
254                 pSRB->next = NULL;
255         }
256         spin_unlock_irqrestore(&(pHCB->pSRB_lock), flags);
257
258         return (pSRB);
259 }
260
261 /* called from init/main.c */
262
263 void i91u_setup(char *str, int *ints)
264 {
265         if (setup_called)
266                 i91u_panic("i91u: i91u_setup called twice.\n");
267
268         setup_called = ints[0];
269         setup_str = str;
270
271 #ifdef DEBUG_i91u
272         setup_debug = ints[0] >= 1 ? ints[1] : DEBUG_DEFAULT;
273 #endif
274 }
275
276 int tul_NewReturnNumberOfAdapters(void)
277 {
278         struct pci_dev *pDev = NULL;    /* Start from none              */
279         int iAdapters = 0;
280         long dRegValue;
281         WORD wBIOS;
282         int i = 0;
283
284         init_i91uAdapter_table();
285
286         for (i = 0; i < TULSZ(i91u_pci_devices); i++)
287         {
288                 while ((pDev = pci_find_device(i91u_pci_devices[i].vendor_id, i91u_pci_devices[i].device_id, pDev)) != NULL) {
289                         if (pci_enable_device(pDev))
290                                 continue;
291                         pci_read_config_dword(pDev, 0x44, (u32 *) & dRegValue);
292                         wBIOS = (UWORD) (dRegValue & 0xFF);
293                         if (((dRegValue & 0xFF00) >> 8) == 0xFF)
294                                 dRegValue = 0;
295                         wBIOS = (wBIOS << 8) + ((UWORD) ((dRegValue & 0xFF00) >> 8));
296                         if (Addi91u_into_Adapter_table(wBIOS,
297                                                         (pDev->resource[0].start),
298                                                         pDev->irq,
299                                                         pDev->bus->number,
300                                                         (pDev->devfn >> 3)
301                                 ) == 0)
302                                 iAdapters++;
303                 }
304         }
305
306         return (iAdapters);
307 }
308
309 int i91u_detect(Scsi_Host_Template * tpnt)
310 {
311         SCB *pSCB;
312         HCS *pHCB;
313         struct Scsi_Host *hreg;
314         unsigned long i;        /* 01/14/98                     */
315         int ok = 0, iAdapters;
316         ULONG dBiosAdr;
317         BYTE *pbBiosAdr;
318
319         tpnt->proc_name = "INI9100U";
320
321         if (setup_called) {     /* Setup by i91u_setup          */
322                 printk("i91u: processing commandline: ");
323
324 #ifdef DEBUG_i91u
325                 if (setup_called > 1) {
326                         printk("\ni91u: %s\n", setup_str);
327                         printk("i91u: usage: i91u[=<DEBUG>]\n");
328                         i91u_panic("i91u panics in line %d", __LINE__);
329                 }
330                 i91u_debug = setup_debug;
331 #endif
332         }
333         /* Get total number of adapters in the motherboard */
334 #ifdef CONFIG_PCI
335         iAdapters = tul_NewReturnNumberOfAdapters();
336 #else
337         iAdapters = tul_ReturnNumberOfAdapters();
338 #endif
339
340         if (iAdapters == 0)     /* If no tulip founded, return */
341                 return (0);
342
343         tul_num_ch = (iAdapters > tul_num_ch) ? tul_num_ch : iAdapters;
344         /* Update actually channel number */
345         if (tul_tag_enable) {   /* 1.01i                  */
346                 tul_num_scb = MAX_TARGETS * i91u_MAXQUEUE;
347         } else {
348                 tul_num_scb = MAX_TARGETS + 3;  /* 1-tape, 1-CD_ROM, 1- extra */
349         }                       /* Update actually SCBs per adapter */
350
351         /* Get total memory needed for HCS */
352         i = tul_num_ch * sizeof(HCS);
353         memset((unsigned char *) &tul_hcs[0], 0, i);    /* Initialize tul_hcs 0 */
354         /* Get total memory needed for SCB */
355
356         for (; tul_num_scb >= MAX_TARGETS + 3; tul_num_scb--) {
357                 i = tul_num_ch * tul_num_scb * sizeof(SCB);
358                 if ((tul_scb = (SCB *) kmalloc(i, GFP_ATOMIC | GFP_DMA)) != NULL)
359                         break;
360         }
361         if (tul_scb == NULL) {
362                 printk("i91u: SCB memory allocation error\n");
363                 return (0);
364         }
365         memset((unsigned char *) tul_scb, 0, i);
366
367         pSCB = tul_scb;
368         for (i = 0; i < tul_num_ch * tul_num_scb; i++, pSCB++) {
369                 pSCB->SCB_SGPAddr = (U32) VIRT_TO_BUS(&pSCB->SCB_SGList[0]);
370         }
371
372         for (i = 0, pHCB = &tul_hcs[0];         /* Get pointer for control block */
373              i < tul_num_ch;
374              i++, pHCB++) {
375                 pHCB->pSRB_head = NULL;         /* Initial SRB save queue       */
376                 pHCB->pSRB_tail = NULL;         /* Initial SRB save queue       */
377                 pHCB->pSRB_lock = SPIN_LOCK_UNLOCKED;   /* SRB save queue lock */
378                 get_tulipPCIConfig(pHCB, i);
379
380                 dBiosAdr = pHCB->HCS_BIOS;
381                 dBiosAdr = (dBiosAdr << 4);
382
383                 pbBiosAdr = phys_to_virt(dBiosAdr);
384
385                 init_tulip(pHCB, tul_scb + (i * tul_num_scb), tul_num_scb, pbBiosAdr, 10);
386                 request_region(pHCB->HCS_Base, 256, "i91u"); /* Register */ 
387
388                 pHCB->HCS_Index = i;    /* 7/29/98 */
389                 hreg = scsi_register(tpnt, sizeof(HCS));
390                 if(hreg == NULL)
391                 {
392                         release_region(pHCB->HCS_Base, 256);
393                         return 0;
394                 }
395                 hreg->io_port = pHCB->HCS_Base;
396                 hreg->n_io_port = 0xff;
397                 hreg->can_queue = tul_num_scb;  /* 03/05/98                      */
398                 hreg->unique_id = pHCB->HCS_Base;
399                 hreg->max_id = pHCB->HCS_MaxTar;
400                 hreg->max_lun = 32;     /* 10/21/97                     */
401                 hreg->irq = pHCB->HCS_Intr;
402                 hreg->this_id = pHCB->HCS_SCSI_ID;      /* Assign HCS index           */
403                 hreg->base = (unsigned long)pHCB;
404                 hreg->sg_tablesize = TOTAL_SG_ENTRY;    /* Maximun support is 32 */
405
406                 /* Initial tulip chip           */
407                 switch (i) {
408                 case 0:
409                         ok = request_irq(pHCB->HCS_Intr, i91u_intr0, SA_INTERRUPT | SA_SHIRQ, "i91u", hreg);
410                         break;
411                 case 1:
412                         ok = request_irq(pHCB->HCS_Intr, i91u_intr1, SA_INTERRUPT | SA_SHIRQ, "i91u", hreg);
413                         break;
414                 case 2:
415                         ok = request_irq(pHCB->HCS_Intr, i91u_intr2, SA_INTERRUPT | SA_SHIRQ, "i91u", hreg);
416                         break;
417                 case 3:
418                         ok = request_irq(pHCB->HCS_Intr, i91u_intr3, SA_INTERRUPT | SA_SHIRQ, "i91u", hreg);
419                         break;
420                 case 4:
421                         ok = request_irq(pHCB->HCS_Intr, i91u_intr4, SA_INTERRUPT | SA_SHIRQ, "i91u", hreg);
422                         break;
423                 case 5:
424                         ok = request_irq(pHCB->HCS_Intr, i91u_intr5, SA_INTERRUPT | SA_SHIRQ, "i91u", hreg);
425                         break;
426                 case 6:
427                         ok = request_irq(pHCB->HCS_Intr, i91u_intr6, SA_INTERRUPT | SA_SHIRQ, "i91u", hreg);
428                         break;
429                 case 7:
430                         ok = request_irq(pHCB->HCS_Intr, i91u_intr7, SA_INTERRUPT | SA_SHIRQ, "i91u", hreg);
431                         break;
432                 default:
433                         i91u_panic("i91u: Too many host adapters\n");
434                         break;
435                 }
436                 if (ok < 0) {
437                         if (ok == -EINVAL) {
438                                 printk("i91u: bad IRQ %d.\n", pHCB->HCS_Intr);
439                                 printk("         Contact author.\n");
440                         } else if (ok == -EBUSY)
441                                 printk("i91u: IRQ %d already in use. Configure another.\n",
442                                        pHCB->HCS_Intr);
443                         else {
444                                 printk("\ni91u: Unexpected error code on requesting IRQ %d.\n",
445                                        pHCB->HCS_Intr);
446                                 printk("         Contact author.\n");
447                         }
448                         i91u_panic("i91u: driver needs an IRQ.\n");
449                 }
450         }
451
452         tpnt->this_id = -1;
453         tpnt->can_queue = 1;
454
455         return 1;
456 }
457
458 static void i91uBuildSCB(HCS * pHCB, SCB * pSCB, Scsi_Cmnd * SCpnt)
459 {                               /* Create corresponding SCB     */
460         struct scatterlist *pSrbSG;
461         SG *pSG;                /* Pointer to SG list           */
462         int i;
463         long TotalLen;
464
465         pSCB->SCB_Post = i91uSCBPost;   /* i91u's callback routine      */
466         pSCB->SCB_Srb = SCpnt;
467         pSCB->SCB_Opcode = ExecSCSI;
468         pSCB->SCB_Flags = SCF_POST;     /* After SCSI done, call post routine */
469         pSCB->SCB_Target = SCpnt->target;
470         pSCB->SCB_Lun = SCpnt->lun;
471         pSCB->SCB_Ident = SCpnt->lun | DISC_ALLOW;
472         pSCB->SCB_Flags |= SCF_SENSE;   /* Turn on auto request sense   */
473
474         pSCB->SCB_SensePtr = (U32) VIRT_TO_BUS(SCpnt->sense_buffer);
475
476         pSCB->SCB_SenseLen = SENSE_SIZE;
477
478         pSCB->SCB_CDBLen = SCpnt->cmd_len;
479         pSCB->SCB_HaStat = 0;
480         pSCB->SCB_TaStat = 0;
481         memcpy(&pSCB->SCB_CDB[0], &SCpnt->cmnd, SCpnt->cmd_len);
482
483         if (SCpnt->device->tagged_supported) {  /* Tag Support                  */
484                 pSCB->SCB_TagMsg = SIMPLE_QUEUE_TAG;    /* Do simple tag only   */
485         } else {
486                 pSCB->SCB_TagMsg = 0;   /* No tag support               */
487         }
488
489         if (SCpnt->use_sg) {
490                 pSrbSG = (struct scatterlist *) SCpnt->request_buffer;
491                 if (SCpnt->use_sg == 1) {       /* If only one entry in the list *//*      treat it as regular I/O */
492                         pSCB->SCB_BufPtr = (U32) VIRT_TO_BUS(pSrbSG->address);
493                         TotalLen = pSrbSG->length;
494                         pSCB->SCB_SGLen = 0;
495                 } else {        /* Assign SG physical address   */
496                         pSCB->SCB_BufPtr = pSCB->SCB_SGPAddr;
497                         pSCB->SCB_Flags |= SCF_SG;      /* Turn on SG list flag       */
498                         for (i = 0, TotalLen = 0, pSG = &pSCB->SCB_SGList[0];   /* 1.01g */
499                              i < SCpnt->use_sg;
500                              i++, pSG++, pSrbSG++) {
501                                 pSG->SG_Ptr = (U32) VIRT_TO_BUS(pSrbSG->address);
502                                 TotalLen += pSG->SG_Len = pSrbSG->length;
503                         }
504                         pSCB->SCB_SGLen = i;
505                 }
506                 pSCB->SCB_BufLen = (SCpnt->request_bufflen > TotalLen) ?
507                     TotalLen : SCpnt->request_bufflen;
508         } else {                /* Non SG                       */
509                 pSCB->SCB_BufPtr = (U32) VIRT_TO_BUS(SCpnt->request_buffer);
510                 pSCB->SCB_BufLen = SCpnt->request_bufflen;
511                 pSCB->SCB_SGLen = 0;
512         }
513
514         return;
515 }
516
517 /* 
518  *  Queue a command and setup interrupts for a free bus.
519  */
520 int i91u_queue(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
521 {
522         register SCB *pSCB;
523         HCS *pHCB;              /* Point to Host adapter control block */
524
525         if (SCpnt->lun > 16) {  /* 07/22/98 */
526
527                 SCpnt->result = (DID_TIME_OUT << 16);
528                 done(SCpnt);    /* Notify system DONE           */
529                 return (0);
530         }
531         pHCB = (HCS *) SCpnt->host->base;
532
533         SCpnt->scsi_done = done;
534         /* Get free SCSI control block  */
535         if ((pSCB = tul_alloc_scb(pHCB)) == NULL) {
536                 i91uAppendSRBToQueue(pHCB, SCpnt);      /* Buffer this request  */
537                 return (0);
538         }
539         i91uBuildSCB(pHCB, pSCB, SCpnt);
540         tul_exec_scb(pHCB, pSCB);       /* Start execute SCB            */
541         return (0);
542 }
543
544 /*
545  *  We only support command in interrupt-driven fashion
546  */
547 int i91u_command(Scsi_Cmnd * SCpnt)
548 {
549         printk("i91u: interrupt driven driver; use i91u_queue()\n");
550         return -1;
551 }
552
553 /*
554  *  Abort a queued command
555  *  (commands that are on the bus can't be aborted easily)
556  */
557 int i91u_abort(Scsi_Cmnd * SCpnt)
558 {
559         HCS *pHCB;
560
561         pHCB = (HCS *) SCpnt->host->base;
562         return tul_abort_srb(pHCB, SCpnt);
563 }
564
565 /*
566  *  Reset registers, reset a hanging bus and
567  *  kill active and disconnected commands for target w/o soft reset
568  */
569 int i91u_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
570 {                               /* I need Host Control Block Information */
571         HCS *pHCB;
572
573         pHCB = (HCS *) SCpnt->host->base;
574
575         if (reset_flags & (SCSI_RESET_SUGGEST_BUS_RESET | SCSI_RESET_SUGGEST_HOST_RESET))
576                 return tul_reset_scsi_bus(pHCB);
577         else
578                 return tul_device_reset(pHCB, (ULONG) SCpnt, SCpnt->target, reset_flags);
579 }
580
581 /*
582  * Return the "logical geometry"
583  */
584 int i91u_biosparam(Scsi_Disk * disk, kdev_t dev, int *info_array)
585 {
586         HCS *pHcb;              /* Point to Host adapter control block */
587         TCS *pTcb;
588
589         pHcb = (HCS *) disk->device->host->base;
590         pTcb = &pHcb->HCS_Tcs[disk->device->id];
591
592         if (pTcb->TCS_DrvHead) {
593                 info_array[0] = pTcb->TCS_DrvHead;
594                 info_array[1] = pTcb->TCS_DrvSector;
595                 info_array[2] = disk->capacity / pTcb->TCS_DrvHead / pTcb->TCS_DrvSector;
596         } else {
597                 if (pTcb->TCS_DrvFlags & TCF_DRV_255_63) {
598                         info_array[0] = 255;
599                         info_array[1] = 63;
600                         info_array[2] = disk->capacity / 255 / 63;
601                 } else {
602                         info_array[0] = 64;
603                         info_array[1] = 32;
604                         info_array[2] = disk->capacity >> 11;
605                 }
606         }
607
608 #if defined(DEBUG_BIOSPARAM)
609         if (i91u_debug & debug_biosparam) {
610                 printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
611                        info_array[0], info_array[1], info_array[2]);
612                 printk("WARNING: check, if the bios geometry is correct.\n");
613         }
614 #endif
615
616         return 0;
617 }
618
619 /*****************************************************************************
620  Function name  : i91uSCBPost
621  Description    : This is callback routine be called when tulip finish one
622                         SCSI command.
623  Input          : pHCB  -       Pointer to host adapter control block.
624                   pSCB  -       Pointer to SCSI control block.
625  Output         : None.
626  Return         : None.
627 *****************************************************************************/
628 static void i91uSCBPost(BYTE * pHcb, BYTE * pScb)
629 {
630         Scsi_Cmnd *pSRB;        /* Pointer to SCSI request block */
631         HCS *pHCB;
632         SCB *pSCB;
633
634         pHCB = (HCS *) pHcb;
635         pSCB = (SCB *) pScb;
636         if ((pSRB = pSCB->SCB_Srb) == 0) {
637                 printk("i91uSCBPost: SRB pointer is empty\n");
638
639                 tul_release_scb(pHCB, pSCB);    /* Release SCB for current channel */
640                 return;
641         }
642         switch (pSCB->SCB_HaStat) {
643         case 0x0:
644         case 0xa:               /* Linked command complete without error and linked normally */
645         case 0xb:               /* Linked command complete without error interrupt generated */
646                 pSCB->SCB_HaStat = 0;
647                 break;
648
649         case 0x11:              /* Selection time out-The initiator selection or target
650                                    reselection was not complete within the SCSI Time out period */
651                 pSCB->SCB_HaStat = DID_TIME_OUT;
652                 break;
653
654         case 0x14:              /* Target bus phase sequence failure-An invalid bus phase or bus
655                                    phase sequence was requested by the target. The host adapter
656                                    will generate a SCSI Reset Condition, notifying the host with
657                                    a SCRD interrupt */
658                 pSCB->SCB_HaStat = DID_RESET;
659                 break;
660
661         case 0x1a:              /* SCB Aborted. 07/21/98 */
662                 pSCB->SCB_HaStat = DID_ABORT;
663                 break;
664
665         case 0x12:              /* Data overrun/underrun-The target attempted to transfer more data
666                                    than was allocated by the Data Length field or the sum of the
667                                    Scatter / Gather Data Length fields. */
668         case 0x13:              /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
669         case 0x16:              /* Invalid SCB Operation Code. */
670
671         default:
672                 printk("ini9100u: %x %x\n", pSCB->SCB_HaStat, pSCB->SCB_TaStat);
673                 pSCB->SCB_HaStat = DID_ERROR;   /* Couldn't find any better */
674                 break;
675         }
676
677         pSRB->result = pSCB->SCB_TaStat | (pSCB->SCB_HaStat << 16);
678
679         if (pSRB == NULL) {
680                 printk("pSRB is NULL\n");
681         }
682         pSRB->scsi_done(pSRB);  /* Notify system DONE           */
683         if ((pSRB = i91uPopSRBFromQueue(pHCB)) != NULL)
684                 /* Find the next pending SRB    */
685         {                       /* Assume resend will success   */
686                 /* Reuse old SCB                */
687                 i91uBuildSCB(pHCB, pSCB, pSRB);         /* Create corresponding SCB     */
688
689                 tul_exec_scb(pHCB, pSCB);       /* Start execute SCB            */
690         } else {                /* No Pending SRB               */
691                 tul_release_scb(pHCB, pSCB);    /* Release SCB for current channel */
692         }
693         return;
694 }
695
696 /*
697  * Interrupts handler (main routine of the driver)
698  */
699 static void i91u_intr0(int irqno, void *dev_id, struct pt_regs *regs)
700 {
701         unsigned long flags;
702
703         if (tul_hcs[0].HCS_Intr != irqno)
704                 return;
705
706         spin_lock_irqsave(&io_request_lock, flags);
707
708         tul_isr(&tul_hcs[0]);
709
710         spin_unlock_irqrestore(&io_request_lock, flags);
711 }
712
713 static void i91u_intr1(int irqno, void *dev_id, struct pt_regs *regs)
714 {
715         unsigned long flags;
716
717         if (tul_hcs[1].HCS_Intr != irqno)
718                 return;
719
720         spin_lock_irqsave(&io_request_lock, flags);
721
722         tul_isr(&tul_hcs[1]);
723
724         spin_unlock_irqrestore(&io_request_lock, flags);
725 }
726
727 static void i91u_intr2(int irqno, void *dev_id, struct pt_regs *regs)
728 {
729         unsigned long flags;
730
731         if (tul_hcs[2].HCS_Intr != irqno)
732                 return;
733
734         spin_lock_irqsave(&io_request_lock, flags);
735
736         tul_isr(&tul_hcs[2]);
737
738         spin_unlock_irqrestore(&io_request_lock, flags);
739 }
740
741 static void i91u_intr3(int irqno, void *dev_id, struct pt_regs *regs)
742 {
743         unsigned long flags;
744
745         if (tul_hcs[3].HCS_Intr != irqno)
746                 return;
747
748         spin_lock_irqsave(&io_request_lock, flags);
749
750         tul_isr(&tul_hcs[3]);
751
752         spin_unlock_irqrestore(&io_request_lock, flags);
753 }
754
755 static void i91u_intr4(int irqno, void *dev_id, struct pt_regs *regs)
756 {
757         unsigned long flags;
758
759         if (tul_hcs[4].HCS_Intr != irqno)
760                 return;
761
762         spin_lock_irqsave(&io_request_lock, flags);
763
764         tul_isr(&tul_hcs[4]);
765
766         spin_unlock_irqrestore(&io_request_lock, flags);
767 }
768
769 static void i91u_intr5(int irqno, void *dev_id, struct pt_regs *regs)
770 {
771         unsigned long flags;
772
773         if (tul_hcs[5].HCS_Intr != irqno)
774                 return;
775
776         spin_lock_irqsave(&io_request_lock, flags);
777
778         tul_isr(&tul_hcs[5]);
779
780         spin_unlock_irqrestore(&io_request_lock, flags);
781 }
782
783 static void i91u_intr6(int irqno, void *dev_id, struct pt_regs *regs)
784 {
785         unsigned long flags;
786
787         if (tul_hcs[6].HCS_Intr != irqno)
788                 return;
789
790         spin_lock_irqsave(&io_request_lock, flags);
791
792         tul_isr(&tul_hcs[6]);
793
794         spin_unlock_irqrestore(&io_request_lock, flags);
795 }
796
797 static void i91u_intr7(int irqno, void *dev_id, struct pt_regs *regs)
798 {
799         unsigned long flags;
800
801         if (tul_hcs[7].HCS_Intr != irqno)
802                 return;
803
804         spin_lock_irqsave(&io_request_lock, flags);
805
806         tul_isr(&tul_hcs[7]);
807
808         spin_unlock_irqrestore(&io_request_lock, flags);
809 }
810
811 /* 
812  * Dump the current driver status and panic...
813  */
814 static void i91u_panic(char *msg)
815 {
816         printk("\ni91u_panic: %s\n", msg);
817         panic("i91u panic");
818 }
819
820 /*
821  * Release ressources
822  */
823 int i91u_release(struct Scsi_Host *hreg)
824 {
825         free_irq(hreg->irq, hreg);
826         release_region(hreg->io_port, 256);
827         return 0;
828 }
829 MODULE_LICENSE("Dual BSD/GPL");