ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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  * 10/17/03 mc  - v1.04
108  *              - added new DMA API support
109  **************************************************************************/
110
111 #define CVT_LINUX_VERSION(V,P,S)        (V * 65536 + P * 256 + S)
112
113 #ifndef LINUX_VERSION_CODE
114 #include <linux/version.h>
115 #endif
116
117 #include <linux/module.h>
118 #include <linux/errno.h>
119 #include <linux/delay.h>
120 #include <linux/pci.h>
121 #include <linux/init.h>
122 #include <linux/blkdev.h>
123 #include <linux/spinlock.h>
124 #include <linux/stat.h>
125 #include <linux/config.h>
126 #include <linux/kernel.h>
127 #include <linux/proc_fs.h>
128 #include <linux/string.h>
129 #include <linux/interrupt.h>
130 #include <linux/ioport.h>
131 #include <linux/sched.h>
132 #include <linux/slab.h>
133
134 #include <asm/io.h>
135
136 #include "scsi.h"
137 #include "hosts.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 = {
145         .proc_name      = "INI9100U",
146         .name           = i91u_REVID,
147         .detect         = i91u_detect,
148         .release        = i91u_release,
149         .queuecommand   = i91u_queue,
150 //      .abort          = i91u_abort,
151 //      .reset          = i91u_reset,
152         .bios_param     = i91u_biosparam,
153         .can_queue      = 1,
154         .this_id        = 1,
155         .sg_tablesize   = SG_ALL,
156         .cmd_per_lun    = 1,
157         .use_clustering = ENABLE_CLUSTERING,
158 };
159 #include "scsi_module.c"
160
161 char *i91uCopyright = "Copyright (C) 1996-98";
162 char *i91uInitioName = "by Initio Corporation";
163 char *i91uProductName = "INI-9X00U/UW";
164 char *i91uVersion = "v1.04";
165
166 #define TULSZ(sz)     (sizeof(sz) / sizeof(sz[0]))
167 #define TUL_RDWORD(x,y)         (short)(inl((int)((ULONG)((ULONG)x+(UCHAR)y)) ))
168
169 /* set by i91_setup according to the command line */
170 static int setup_called = 0;
171
172 static int tul_num_ch = 4;      /* Maximum 4 adapters           */
173 static int tul_num_scb;
174 static int tul_tag_enable = 1;
175 static SCB *tul_scb;
176
177 #ifdef DEBUG_i91u
178 static int setup_debug = 0;
179 #endif
180
181 static char *setup_str = (char *) NULL;
182
183 static void i91u_panic(char *msg);
184
185 static void i91uSCBPost(BYTE * pHcb, BYTE * pScb);
186
187                                 /* ---- EXTERNAL FUNCTIONS ---- */
188                                         /* Get total number of adapters */
189 extern void init_i91uAdapter_table(void);
190 extern int Addi91u_into_Adapter_table(WORD, WORD, BYTE, BYTE, BYTE);
191 extern int tul_ReturnNumberOfAdapters(void);
192 extern void get_tulipPCIConfig(HCS * pHCB, int iChannel_index);
193 extern int init_tulip(HCS * pHCB, SCB * pSCB, int tul_num_scb, BYTE * pbBiosAdr, int reset_time);
194 extern SCB *tul_alloc_scb(HCS * pHCB);
195 extern int tul_abort_srb(HCS * pHCB, Scsi_Cmnd * pSRB);
196 extern void tul_exec_scb(HCS * pHCB, SCB * pSCB);
197 extern void tul_release_scb(HCS * pHCB, SCB * pSCB);
198 extern void tul_stop_bm(HCS * pHCB);
199 extern int tul_reset_scsi(HCS * pCurHcb, int seconds);
200 extern int tul_isr(HCS * pHCB);
201 extern int tul_reset(HCS * pHCB, Scsi_Cmnd * pSRB, unsigned char target);
202 extern int tul_reset_scsi_bus(HCS * pCurHcb);
203 extern int tul_device_reset(HCS * pCurHcb, ULONG pSrb, unsigned int target, unsigned int ResetFlags);
204                                 /* ---- EXTERNAL VARIABLES ---- */
205 extern HCS tul_hcs[];
206
207 const PCI_ID i91u_pci_devices[] = {
208         { INI_VENDOR_ID, I950_DEVICE_ID },
209         { INI_VENDOR_ID, I940_DEVICE_ID },
210         { INI_VENDOR_ID, I935_DEVICE_ID },
211         { INI_VENDOR_ID, I920_DEVICE_ID },
212         { DMX_VENDOR_ID, I920_DEVICE_ID },
213 };
214
215 /*
216  *  queue services:
217  */
218 /*****************************************************************************
219  Function name  : i91uAppendSRBToQueue
220  Description    : This function will push current request into save list
221  Input          : pSRB  -       Pointer to SCSI request block.
222                   pHCB  -       Pointer to host adapter structure
223  Output         : None.
224  Return         : None.
225 *****************************************************************************/
226 static void i91uAppendSRBToQueue(HCS * pHCB, Scsi_Cmnd * pSRB)
227 {
228         ULONG flags;
229         spin_lock_irqsave(&(pHCB->pSRB_lock), flags);
230
231         pSRB->host_scribble = NULL;     /* Pointer to next */
232
233         if (pHCB->pSRB_head == NULL)
234                 pHCB->pSRB_head = pSRB;
235         else
236                 pHCB->pSRB_tail->host_scribble = (char *)pSRB;  /* Pointer to next */
237         pHCB->pSRB_tail = pSRB;
238
239         spin_unlock_irqrestore(&(pHCB->pSRB_lock), flags);
240         return;
241 }
242
243 /*****************************************************************************
244  Function name  : i91uPopSRBFromQueue
245  Description    : This function will pop current request from save list
246  Input          : pHCB  -       Pointer to host adapter structure
247  Output         : None.
248  Return         : pSRB  -       Pointer to SCSI request block.
249 *****************************************************************************/
250 static Scsi_Cmnd *i91uPopSRBFromQueue(HCS * pHCB)
251 {
252         Scsi_Cmnd *pSRB;
253         ULONG flags;
254
255         spin_lock_irqsave(&(pHCB->pSRB_lock), flags);
256
257         if ((pSRB = pHCB->pSRB_head) != NULL) {
258                 pHCB->pSRB_head = (struct scsi_cmnd *)pHCB->pSRB_head->host_scribble;
259                 pSRB->host_scribble = NULL;
260         }
261         spin_unlock_irqrestore(&(pHCB->pSRB_lock), flags);
262
263         return (pSRB);
264 }
265
266 static irqreturn_t i91u_intr(int irqno, void *dev_id, struct pt_regs *regs)
267 {
268         struct Scsi_Host *dev = dev_id;
269         unsigned long flags;
270         
271         spin_lock_irqsave(dev->host_lock, flags);
272         tul_isr((HCS *)dev->base);
273         spin_unlock_irqrestore(dev->host_lock, flags);
274         return IRQ_HANDLED;
275 }
276
277 /* called from init/main.c */
278
279 void i91u_setup(char *str, int *ints)
280 {
281         if (setup_called)
282                 i91u_panic("i91u: i91u_setup called twice.\n");
283
284         setup_called = ints[0];
285         setup_str = str;
286
287 #ifdef DEBUG_i91u
288         setup_debug = ints[0] >= 1 ? ints[1] : DEBUG_DEFAULT;
289 #endif
290 }
291
292 int tul_NewReturnNumberOfAdapters(void)
293 {
294         struct pci_dev *pDev = NULL;    /* Start from none              */
295         int iAdapters = 0;
296         long dRegValue;
297         WORD wBIOS;
298         int i = 0;
299
300         init_i91uAdapter_table();
301
302         for (i = 0; i < TULSZ(i91u_pci_devices); i++)
303         {
304                 while ((pDev = pci_find_device(i91u_pci_devices[i].vendor_id, i91u_pci_devices[i].device_id, pDev)) != NULL) {
305                         if (pci_enable_device(pDev))
306                                 continue;
307                         pci_read_config_dword(pDev, 0x44, (u32 *) & dRegValue);
308                         wBIOS = (UWORD) (dRegValue & 0xFF);
309                         if (((dRegValue & 0xFF00) >> 8) == 0xFF)
310                                 dRegValue = 0;
311                         wBIOS = (wBIOS << 8) + ((UWORD) ((dRegValue & 0xFF00) >> 8));
312                         if (pci_set_dma_mask(pDev, 0xffffffff)) {
313                                 printk(KERN_WARNING 
314                                        "i91u: Could not set 32 bit DMA mask\n");
315                                 continue;
316                         }
317
318                         if (Addi91u_into_Adapter_table(wBIOS,
319                                                         (pDev->resource[0].start),
320                                                         pDev->irq,
321                                                         pDev->bus->number,
322                                                         (pDev->devfn >> 3)
323                                 ) == 0)
324                                 iAdapters++;
325                 }
326         }
327
328         return (iAdapters);
329 }
330
331 int i91u_detect(Scsi_Host_Template * tpnt)
332 {
333         HCS *pHCB;
334         struct Scsi_Host *hreg;
335         unsigned long i;        /* 01/14/98                     */
336         int ok = 0, iAdapters;
337         ULONG dBiosAdr;
338         BYTE *pbBiosAdr;
339
340         tpnt->proc_name = "INI9100U";
341
342         if (setup_called) {     /* Setup by i91u_setup          */
343                 printk("i91u: processing commandline: ");
344
345 #ifdef DEBUG_i91u
346                 if (setup_called > 1) {
347                         printk("\ni91u: %s\n", setup_str);
348                         printk("i91u: usage: i91u[=<DEBUG>]\n");
349                         i91u_panic("i91u panics in line %d", __LINE__);
350                 }
351                 i91u_debug = setup_debug;
352 #endif
353         }
354         /* Get total number of adapters in the motherboard */
355         iAdapters = tul_NewReturnNumberOfAdapters();
356         if (iAdapters == 0)     /* If no tulip founded, return */
357                 return (0);
358
359         tul_num_ch = (iAdapters > tul_num_ch) ? tul_num_ch : iAdapters;
360         /* Update actually channel number */
361         if (tul_tag_enable) {   /* 1.01i                  */
362                 tul_num_scb = MAX_TARGETS * i91u_MAXQUEUE;
363         } else {
364                 tul_num_scb = MAX_TARGETS + 3;  /* 1-tape, 1-CD_ROM, 1- extra */
365         }                       /* Update actually SCBs per adapter */
366
367         /* Get total memory needed for HCS */
368         i = tul_num_ch * sizeof(HCS);
369         memset((unsigned char *) &tul_hcs[0], 0, i);    /* Initialize tul_hcs 0 */
370         /* Get total memory needed for SCB */
371
372         for (; tul_num_scb >= MAX_TARGETS + 3; tul_num_scb--) {
373                 i = tul_num_ch * tul_num_scb * sizeof(SCB);
374                 if ((tul_scb = (SCB *) kmalloc(i, GFP_ATOMIC | GFP_DMA)) != NULL)
375                         break;
376         }
377         if (tul_scb == NULL) {
378                 printk("i91u: SCB memory allocation error\n");
379                 return (0);
380         }
381         memset((unsigned char *) tul_scb, 0, i);
382
383         for (i = 0, pHCB = &tul_hcs[0];         /* Get pointer for control block */
384              i < tul_num_ch;
385              i++, pHCB++) {
386                 pHCB->pSRB_head = NULL;         /* Initial SRB save queue       */
387                 pHCB->pSRB_tail = NULL;         /* Initial SRB save queue       */
388                 pHCB->pSRB_lock = SPIN_LOCK_UNLOCKED;   /* SRB save queue lock */
389                 get_tulipPCIConfig(pHCB, i);
390
391                 dBiosAdr = pHCB->HCS_BIOS;
392                 dBiosAdr = (dBiosAdr << 4);
393
394                 pbBiosAdr = phys_to_virt(dBiosAdr);
395
396                 init_tulip(pHCB, tul_scb + (i * tul_num_scb), tul_num_scb, pbBiosAdr, 10);
397                 request_region(pHCB->HCS_Base, 256, "i91u"); /* Register */ 
398
399                 pHCB->HCS_Index = i;    /* 7/29/98 */
400                 hreg = scsi_register(tpnt, sizeof(HCS));
401                 if(hreg == NULL) {
402                         release_region(pHCB->HCS_Base, 256);
403                         return 0;
404                 }
405                 hreg->io_port = pHCB->HCS_Base;
406                 hreg->n_io_port = 0xff;
407                 hreg->can_queue = tul_num_scb;  /* 03/05/98                      */
408                 hreg->unique_id = pHCB->HCS_Base;
409                 hreg->max_id = pHCB->HCS_MaxTar;
410                 hreg->max_lun = 32;     /* 10/21/97                     */
411                 hreg->irq = pHCB->HCS_Intr;
412                 hreg->this_id = pHCB->HCS_SCSI_ID;      /* Assign HCS index           */
413                 hreg->base = (unsigned long)pHCB;
414                 hreg->sg_tablesize = TOTAL_SG_ENTRY;    /* Maximun support is 32 */
415
416                 /* Initial tulip chip           */
417                 ok = request_irq(pHCB->HCS_Intr, i91u_intr, SA_INTERRUPT | SA_SHIRQ, "i91u", hreg);
418                 if (ok < 0) {
419                         printk(KERN_WARNING "i91u: unable to request IRQ %d\n\n", pHCB->HCS_Intr);
420                         return 0;
421                 }
422         }
423
424         tpnt->this_id = -1;
425         tpnt->can_queue = 1;
426
427         return 1;
428 }
429
430 static void i91uBuildSCB(HCS * pHCB, SCB * pSCB, Scsi_Cmnd * SCpnt)
431 {                               /* Create corresponding SCB     */
432         struct scatterlist *pSrbSG;
433         SG *pSG;                /* Pointer to SG list           */
434         int i;
435         long TotalLen;
436         dma_addr_t dma_addr;
437
438         pSCB->SCB_Post = i91uSCBPost;   /* i91u's callback routine      */
439         pSCB->SCB_Srb = SCpnt;
440         pSCB->SCB_Opcode = ExecSCSI;
441         pSCB->SCB_Flags = SCF_POST;     /* After SCSI done, call post routine */
442         pSCB->SCB_Target = SCpnt->device->id;
443         pSCB->SCB_Lun = SCpnt->device->lun;
444         pSCB->SCB_Ident = SCpnt->device->lun | DISC_ALLOW;
445
446         pSCB->SCB_Flags |= SCF_SENSE;   /* Turn on auto request sense   */
447         dma_addr = dma_map_single(&pHCB->pci_dev->dev, SCpnt->sense_buffer,
448                                   SENSE_SIZE, DMA_FROM_DEVICE);
449         pSCB->SCB_SensePtr = cpu_to_le32((u32)dma_addr);
450         pSCB->SCB_SenseLen = cpu_to_le32(SENSE_SIZE);
451         SCpnt->SCp.ptr = (char *)(unsigned long)dma_addr;
452
453         pSCB->SCB_CDBLen = SCpnt->cmd_len;
454         pSCB->SCB_HaStat = 0;
455         pSCB->SCB_TaStat = 0;
456         memcpy(&pSCB->SCB_CDB[0], &SCpnt->cmnd, SCpnt->cmd_len);
457
458         if (SCpnt->device->tagged_supported) {  /* Tag Support                  */
459                 pSCB->SCB_TagMsg = SIMPLE_QUEUE_TAG;    /* Do simple tag only   */
460         } else {
461                 pSCB->SCB_TagMsg = 0;   /* No tag support               */
462         }
463         /* todo handle map_sg error */
464         if (SCpnt->use_sg) {
465                 dma_addr = dma_map_single(&pHCB->pci_dev->dev, &pSCB->SCB_SGList[0],
466                                           sizeof(struct SG_Struc) * TOTAL_SG_ENTRY,
467                                           DMA_BIDIRECTIONAL);
468                 pSCB->SCB_BufPtr = cpu_to_le32((u32)dma_addr);
469                 SCpnt->SCp.dma_handle = dma_addr;
470
471                 pSrbSG = (struct scatterlist *) SCpnt->request_buffer;
472                 pSCB->SCB_SGLen = dma_map_sg(&pHCB->pci_dev->dev, pSrbSG,
473                                              SCpnt->use_sg, SCpnt->sc_data_direction);
474
475                 pSCB->SCB_Flags |= SCF_SG;      /* Turn on SG list flag       */
476                 for (i = 0, TotalLen = 0, pSG = &pSCB->SCB_SGList[0];   /* 1.01g */
477                      i < pSCB->SCB_SGLen; i++, pSG++, pSrbSG++) {
478                         pSG->SG_Ptr = cpu_to_le32((u32)sg_dma_address(pSrbSG));
479                         TotalLen += pSG->SG_Len = cpu_to_le32((u32)sg_dma_len(pSrbSG));
480                 }
481
482                 pSCB->SCB_BufLen = (SCpnt->request_bufflen > TotalLen) ?
483                     TotalLen : SCpnt->request_bufflen;
484         } else if (SCpnt->request_bufflen) {            /* Non SG */
485                 dma_addr = dma_map_single(&pHCB->pci_dev->dev, SCpnt->request_buffer,
486                                           SCpnt->request_bufflen,
487                                           SCpnt->sc_data_direction);
488                 SCpnt->SCp.dma_handle = dma_addr;
489                 pSCB->SCB_BufPtr = cpu_to_le32((u32)dma_addr);
490                 pSCB->SCB_BufLen = cpu_to_le32((u32)SCpnt->request_bufflen);
491                 pSCB->SCB_SGLen = 0;
492         } else {
493                 pSCB->SCB_BufLen = 0;
494                 pSCB->SCB_SGLen = 0;
495         }
496 }
497
498 /* 
499  *  Queue a command and setup interrupts for a free bus.
500  */
501 int i91u_queue(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
502 {
503         register SCB *pSCB;
504         HCS *pHCB;              /* Point to Host adapter control block */
505
506         if (SCpnt->device->lun > 16) {  /* 07/22/98 */
507
508                 SCpnt->result = (DID_TIME_OUT << 16);
509                 done(SCpnt);    /* Notify system DONE           */
510                 return (0);
511         }
512         pHCB = (HCS *) SCpnt->device->host->base;
513
514         SCpnt->scsi_done = done;
515         /* Get free SCSI control block  */
516         if ((pSCB = tul_alloc_scb(pHCB)) == NULL) {
517                 i91uAppendSRBToQueue(pHCB, SCpnt);      /* Buffer this request  */
518                 return (0);
519         }
520         i91uBuildSCB(pHCB, pSCB, SCpnt);
521         tul_exec_scb(pHCB, pSCB);       /* Start execute SCB            */
522         return (0);
523 }
524
525 /*
526  *  Abort a queued command
527  *  (commands that are on the bus can't be aborted easily)
528  */
529 int i91u_abort(Scsi_Cmnd * SCpnt)
530 {
531         HCS *pHCB;
532
533         pHCB = (HCS *) SCpnt->device->host->base;
534         return tul_abort_srb(pHCB, SCpnt);
535 }
536
537 /*
538  *  Reset registers, reset a hanging bus and
539  *  kill active and disconnected commands for target w/o soft reset
540  */
541 int i91u_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
542 {                               /* I need Host Control Block Information */
543         HCS *pHCB;
544
545         pHCB = (HCS *) SCpnt->device->host->base;
546
547         if (reset_flags & (SCSI_RESET_SUGGEST_BUS_RESET | SCSI_RESET_SUGGEST_HOST_RESET))
548                 return tul_reset_scsi_bus(pHCB);
549         else
550                 return tul_device_reset(pHCB, (ULONG) SCpnt, SCpnt->device->id, reset_flags);
551 }
552
553 /*
554  * Return the "logical geometry"
555  */
556 int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev,
557                 sector_t capacity, int *info_array)
558 {
559         HCS *pHcb;              /* Point to Host adapter control block */
560         TCS *pTcb;
561
562         pHcb = (HCS *) sdev->host->base;
563         pTcb = &pHcb->HCS_Tcs[sdev->id];
564
565         if (pTcb->TCS_DrvHead) {
566                 info_array[0] = pTcb->TCS_DrvHead;
567                 info_array[1] = pTcb->TCS_DrvSector;
568                 info_array[2] = (unsigned long)capacity / pTcb->TCS_DrvHead / pTcb->TCS_DrvSector;
569         } else {
570                 if (pTcb->TCS_DrvFlags & TCF_DRV_255_63) {
571                         info_array[0] = 255;
572                         info_array[1] = 63;
573                         info_array[2] = (unsigned long)capacity / 255 / 63;
574                 } else {
575                         info_array[0] = 64;
576                         info_array[1] = 32;
577                         info_array[2] = (unsigned long)capacity >> 11;
578                 }
579         }
580
581 #if defined(DEBUG_BIOSPARAM)
582         if (i91u_debug & debug_biosparam) {
583                 printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
584                        info_array[0], info_array[1], info_array[2]);
585                 printk("WARNING: check, if the bios geometry is correct.\n");
586         }
587 #endif
588
589         return 0;
590 }
591
592 static void i91u_unmap_cmnd(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd)
593 {
594         /* auto sense buffer */
595         if (cmnd->SCp.ptr) {
596                 dma_unmap_single(&pci_dev->dev,
597                                  (dma_addr_t)((unsigned long)cmnd->SCp.ptr),
598                                  SENSE_SIZE, SCSI_DATA_READ);
599                 cmnd->SCp.ptr = NULL;
600         }
601
602         /* request buffer */
603         if (cmnd->use_sg) {
604                 dma_unmap_single(&pci_dev->dev, cmnd->SCp.dma_handle,
605                                  sizeof(struct SG_Struc) * TOTAL_SG_ENTRY,
606                                  DMA_BIDIRECTIONAL);
607
608                 dma_unmap_sg(&pci_dev->dev, cmnd->request_buffer,
609                              cmnd->use_sg,
610                              cmnd->sc_data_direction);
611         } else if (cmnd->request_bufflen) {
612                 dma_unmap_single(&pci_dev->dev, cmnd->SCp.dma_handle,
613                                  cmnd->request_bufflen,
614                                  cmnd->sc_data_direction);
615         }
616 }
617
618 /*****************************************************************************
619  Function name  : i91uSCBPost
620  Description    : This is callback routine be called when tulip finish one
621                         SCSI command.
622  Input          : pHCB  -       Pointer to host adapter control block.
623                   pSCB  -       Pointer to SCSI control block.
624  Output         : None.
625  Return         : None.
626 *****************************************************************************/
627 static void i91uSCBPost(BYTE * pHcb, BYTE * pScb)
628 {
629         Scsi_Cmnd *pSRB;        /* Pointer to SCSI request block */
630         HCS *pHCB;
631         SCB *pSCB;
632
633         pHCB = (HCS *) pHcb;
634         pSCB = (SCB *) pScb;
635         if ((pSRB = pSCB->SCB_Srb) == 0) {
636                 printk("i91uSCBPost: SRB pointer is empty\n");
637
638                 tul_release_scb(pHCB, pSCB);    /* Release SCB for current channel */
639                 return;
640         }
641         switch (pSCB->SCB_HaStat) {
642         case 0x0:
643         case 0xa:               /* Linked command complete without error and linked normally */
644         case 0xb:               /* Linked command complete without error interrupt generated */
645                 pSCB->SCB_HaStat = 0;
646                 break;
647
648         case 0x11:              /* Selection time out-The initiator selection or target
649                                    reselection was not complete within the SCSI Time out period */
650                 pSCB->SCB_HaStat = DID_TIME_OUT;
651                 break;
652
653         case 0x14:              /* Target bus phase sequence failure-An invalid bus phase or bus
654                                    phase sequence was requested by the target. The host adapter
655                                    will generate a SCSI Reset Condition, notifying the host with
656                                    a SCRD interrupt */
657                 pSCB->SCB_HaStat = DID_RESET;
658                 break;
659
660         case 0x1a:              /* SCB Aborted. 07/21/98 */
661                 pSCB->SCB_HaStat = DID_ABORT;
662                 break;
663
664         case 0x12:              /* Data overrun/underrun-The target attempted to transfer more data
665                                    than was allocated by the Data Length field or the sum of the
666                                    Scatter / Gather Data Length fields. */
667         case 0x13:              /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
668         case 0x16:              /* Invalid SCB Operation Code. */
669
670         default:
671                 printk("ini9100u: %x %x\n", pSCB->SCB_HaStat, pSCB->SCB_TaStat);
672                 pSCB->SCB_HaStat = DID_ERROR;   /* Couldn't find any better */
673                 break;
674         }
675
676         pSRB->result = pSCB->SCB_TaStat | (pSCB->SCB_HaStat << 16);
677
678         if (pSRB == NULL) {
679                 printk("pSRB is NULL\n");
680         }
681
682         i91u_unmap_cmnd(pHCB->pci_dev, pSRB);
683         pSRB->scsi_done(pSRB);  /* Notify system DONE           */
684         if ((pSRB = i91uPopSRBFromQueue(pHCB)) != NULL)
685                 /* Find the next pending SRB    */
686         {                       /* Assume resend will success   */
687                 /* Reuse old SCB                */
688                 i91uBuildSCB(pHCB, pSCB, pSRB);         /* Create corresponding SCB     */
689
690                 tul_exec_scb(pHCB, pSCB);       /* Start execute SCB            */
691         } else {                /* No Pending SRB               */
692                 tul_release_scb(pHCB, pSCB);    /* Release SCB for current channel */
693         }
694         return;
695 }
696
697 /* 
698  * Dump the current driver status and panic...
699  */
700 static void i91u_panic(char *msg)
701 {
702         printk("\ni91u_panic: %s\n", msg);
703         panic("i91u panic");
704 }
705
706 /*
707  * Release ressources
708  */
709 int i91u_release(struct Scsi_Host *hreg)
710 {
711         free_irq(hreg->irq, hreg);
712         release_region(hreg->io_port, 256);
713         return 0;
714 }
715 MODULE_LICENSE("Dual BSD/GPL");