1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Qlogic FAS408 ISA card driver
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright 1994, Tom Zerucha.
5*4882a593Smuzhiyun * tz@execpc.com
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Redistributable under terms of the GNU General Public License
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * For the avoidance of doubt the "preferred form" of this code is one which
10*4882a593Smuzhiyun * is in an open non patent encumbered format. Where cryptographic key signing
11*4882a593Smuzhiyun * forms part of the process of creating an executable the information
12*4882a593Smuzhiyun * including keys needed to generate an equivalently functional executable
13*4882a593Smuzhiyun * are deemed to be part of the source code.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Check qlogicfas408.c for more credits and info.
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/blkdev.h> /* to get disk capacity */
20*4882a593Smuzhiyun #include <linux/kernel.h>
21*4882a593Smuzhiyun #include <linux/string.h>
22*4882a593Smuzhiyun #include <linux/init.h>
23*4882a593Smuzhiyun #include <linux/interrupt.h>
24*4882a593Smuzhiyun #include <linux/ioport.h>
25*4882a593Smuzhiyun #include <linux/proc_fs.h>
26*4882a593Smuzhiyun #include <linux/unistd.h>
27*4882a593Smuzhiyun #include <linux/spinlock.h>
28*4882a593Smuzhiyun #include <linux/stat.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #include <asm/io.h>
31*4882a593Smuzhiyun #include <asm/irq.h>
32*4882a593Smuzhiyun #include <asm/dma.h>
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #include "scsi.h"
35*4882a593Smuzhiyun #include <scsi/scsi_host.h>
36*4882a593Smuzhiyun #include "qlogicfas408.h"
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /* Set the following to 2 to use normal interrupt (active high/totempole-
39*4882a593Smuzhiyun * tristate), otherwise use 0 (REQUIRED FOR PCMCIA) for active low, open
40*4882a593Smuzhiyun * drain
41*4882a593Smuzhiyun */
42*4882a593Smuzhiyun #define INT_TYPE 2
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun static char qlogicfas_name[] = "qlogicfas";
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * Look for qlogic card and init if found
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun
__qlogicfas_detect(struct scsi_host_template * host,int qbase,int qlirq)50*4882a593Smuzhiyun static struct Scsi_Host *__qlogicfas_detect(struct scsi_host_template *host,
51*4882a593Smuzhiyun int qbase,
52*4882a593Smuzhiyun int qlirq)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun int qltyp; /* type of chip */
55*4882a593Smuzhiyun int qinitid;
56*4882a593Smuzhiyun struct Scsi_Host *hreg; /* registered host structure */
57*4882a593Smuzhiyun struct qlogicfas408_priv *priv;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* Qlogic Cards only exist at 0x230 or 0x330 (the chip itself
60*4882a593Smuzhiyun * decodes the address - I check 230 first since MIDI cards are
61*4882a593Smuzhiyun * typically at 0x330
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * Theoretically, two Qlogic cards can coexist in the same system.
64*4882a593Smuzhiyun * This should work by simply using this as a loadable module for
65*4882a593Smuzhiyun * the second card, but I haven't tested this.
66*4882a593Smuzhiyun */
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun if (!qbase || qlirq == -1)
69*4882a593Smuzhiyun goto err;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun if (!request_region(qbase, 0x10, qlogicfas_name)) {
72*4882a593Smuzhiyun printk(KERN_INFO "%s: address %#x is busy\n", qlogicfas_name,
73*4882a593Smuzhiyun qbase);
74*4882a593Smuzhiyun goto err;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun if (!qlogicfas408_detect(qbase, INT_TYPE)) {
78*4882a593Smuzhiyun printk(KERN_WARNING "%s: probe failed for %#x\n",
79*4882a593Smuzhiyun qlogicfas_name,
80*4882a593Smuzhiyun qbase);
81*4882a593Smuzhiyun goto err_release_mem;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun printk(KERN_INFO "%s: Using preset base address of %03x,"
85*4882a593Smuzhiyun " IRQ %d\n", qlogicfas_name, qbase, qlirq);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun qltyp = qlogicfas408_get_chip_type(qbase, INT_TYPE);
88*4882a593Smuzhiyun qinitid = host->this_id;
89*4882a593Smuzhiyun if (qinitid < 0)
90*4882a593Smuzhiyun qinitid = 7; /* if no ID, use 7 */
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun qlogicfas408_setup(qbase, qinitid, INT_TYPE);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun hreg = scsi_host_alloc(host, sizeof(struct qlogicfas408_priv));
95*4882a593Smuzhiyun if (!hreg)
96*4882a593Smuzhiyun goto err_release_mem;
97*4882a593Smuzhiyun priv = get_priv_by_host(hreg);
98*4882a593Smuzhiyun hreg->io_port = qbase;
99*4882a593Smuzhiyun hreg->n_io_port = 16;
100*4882a593Smuzhiyun hreg->dma_channel = -1;
101*4882a593Smuzhiyun if (qlirq != -1)
102*4882a593Smuzhiyun hreg->irq = qlirq;
103*4882a593Smuzhiyun priv->qbase = qbase;
104*4882a593Smuzhiyun priv->qlirq = qlirq;
105*4882a593Smuzhiyun priv->qinitid = qinitid;
106*4882a593Smuzhiyun priv->shost = hreg;
107*4882a593Smuzhiyun priv->int_type = INT_TYPE;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun sprintf(priv->qinfo,
110*4882a593Smuzhiyun "Qlogicfas Driver version 0.46, chip %02X at %03X, IRQ %d, TPdma:%d",
111*4882a593Smuzhiyun qltyp, qbase, qlirq, QL_TURBO_PDMA);
112*4882a593Smuzhiyun host->name = qlogicfas_name;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun if (request_irq(qlirq, qlogicfas408_ihandl, 0, qlogicfas_name, hreg))
115*4882a593Smuzhiyun goto free_scsi_host;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun if (scsi_add_host(hreg, NULL))
118*4882a593Smuzhiyun goto free_interrupt;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun scsi_scan_host(hreg);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun return hreg;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun free_interrupt:
125*4882a593Smuzhiyun free_irq(qlirq, hreg);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun free_scsi_host:
128*4882a593Smuzhiyun scsi_host_put(hreg);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun err_release_mem:
131*4882a593Smuzhiyun release_region(qbase, 0x10);
132*4882a593Smuzhiyun err:
133*4882a593Smuzhiyun return NULL;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun #define MAX_QLOGICFAS 8
137*4882a593Smuzhiyun static struct qlogicfas408_priv *cards;
138*4882a593Smuzhiyun static int iobase[MAX_QLOGICFAS];
139*4882a593Smuzhiyun static int irq[MAX_QLOGICFAS] = { [0 ... MAX_QLOGICFAS-1] = -1 };
140*4882a593Smuzhiyun module_param_hw_array(iobase, int, ioport, NULL, 0);
141*4882a593Smuzhiyun module_param_hw_array(irq, int, irq, NULL, 0);
142*4882a593Smuzhiyun MODULE_PARM_DESC(iobase, "I/O address");
143*4882a593Smuzhiyun MODULE_PARM_DESC(irq, "IRQ");
144*4882a593Smuzhiyun
qlogicfas_detect(struct scsi_host_template * sht)145*4882a593Smuzhiyun static int qlogicfas_detect(struct scsi_host_template *sht)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun struct Scsi_Host *shost;
148*4882a593Smuzhiyun struct qlogicfas408_priv *priv;
149*4882a593Smuzhiyun int num;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun for (num = 0; num < MAX_QLOGICFAS; num++) {
152*4882a593Smuzhiyun shost = __qlogicfas_detect(sht, iobase[num], irq[num]);
153*4882a593Smuzhiyun if (shost == NULL) {
154*4882a593Smuzhiyun /* no more devices */
155*4882a593Smuzhiyun break;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun priv = get_priv_by_host(shost);
158*4882a593Smuzhiyun priv->next = cards;
159*4882a593Smuzhiyun cards = priv;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun return num;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
qlogicfas_release(struct Scsi_Host * shost)165*4882a593Smuzhiyun static int qlogicfas_release(struct Scsi_Host *shost)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun struct qlogicfas408_priv *priv = get_priv_by_host(shost);
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun scsi_remove_host(shost);
170*4882a593Smuzhiyun if (shost->irq) {
171*4882a593Smuzhiyun qlogicfas408_disable_ints(priv);
172*4882a593Smuzhiyun free_irq(shost->irq, shost);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun if (shost->io_port && shost->n_io_port)
175*4882a593Smuzhiyun release_region(shost->io_port, shost->n_io_port);
176*4882a593Smuzhiyun scsi_host_put(shost);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun return 0;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /*
182*4882a593Smuzhiyun * The driver template is also needed for PCMCIA
183*4882a593Smuzhiyun */
184*4882a593Smuzhiyun static struct scsi_host_template qlogicfas_driver_template = {
185*4882a593Smuzhiyun .module = THIS_MODULE,
186*4882a593Smuzhiyun .name = qlogicfas_name,
187*4882a593Smuzhiyun .proc_name = qlogicfas_name,
188*4882a593Smuzhiyun .info = qlogicfas408_info,
189*4882a593Smuzhiyun .queuecommand = qlogicfas408_queuecommand,
190*4882a593Smuzhiyun .eh_abort_handler = qlogicfas408_abort,
191*4882a593Smuzhiyun .eh_host_reset_handler = qlogicfas408_host_reset,
192*4882a593Smuzhiyun .bios_param = qlogicfas408_biosparam,
193*4882a593Smuzhiyun .can_queue = 1,
194*4882a593Smuzhiyun .this_id = -1,
195*4882a593Smuzhiyun .sg_tablesize = SG_ALL,
196*4882a593Smuzhiyun .dma_boundary = PAGE_SIZE - 1,
197*4882a593Smuzhiyun };
198*4882a593Smuzhiyun
qlogicfas_init(void)199*4882a593Smuzhiyun static __init int qlogicfas_init(void)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun if (!qlogicfas_detect(&qlogicfas_driver_template)) {
202*4882a593Smuzhiyun /* no cards found */
203*4882a593Smuzhiyun printk(KERN_INFO "%s: no cards were found, please specify "
204*4882a593Smuzhiyun "I/O address and IRQ using iobase= and irq= "
205*4882a593Smuzhiyun "options", qlogicfas_name);
206*4882a593Smuzhiyun return -ENODEV;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun return 0;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
qlogicfas_exit(void)212*4882a593Smuzhiyun static __exit void qlogicfas_exit(void)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun struct qlogicfas408_priv *priv;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun for (priv = cards; priv != NULL; priv = priv->next)
217*4882a593Smuzhiyun qlogicfas_release(priv->shost);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun MODULE_AUTHOR("Tom Zerucha, Michael Griffith");
221*4882a593Smuzhiyun MODULE_DESCRIPTION("Driver for the Qlogic FAS408 based ISA card");
222*4882a593Smuzhiyun MODULE_LICENSE("GPL");
223*4882a593Smuzhiyun module_init(qlogicfas_init);
224*4882a593Smuzhiyun module_exit(qlogicfas_exit);
225*4882a593Smuzhiyun
226