This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / scsi / NCR_D700.c
1 /* -*- mode: c; c-basic-offset: 8 -*- */
2
3 /* NCR Dual 700 MCA SCSI Driver
4  *
5  * Copyright (C) 2001 by James.Bottomley@HansenPartnership.com
6 **-----------------------------------------------------------------------------
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 of the License, or
11 **  (at your option) 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; if not, write to the Free Software
20 **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 **
22 **-----------------------------------------------------------------------------
23  */
24
25 /* Notes:
26  *
27  * Most of the work is done in the chip specific module, 53c700.o
28  *
29  * TODO List:
30  *
31  * 1. Extract the SCSI ID from the voyager CMOS table (necessary to
32  *    support multi-host environments.
33  *
34  * */
35
36
37 /* CHANGELOG 
38  *
39  * Version 2.2
40  *
41  * Added mca_set_adapter_name().
42  *
43  * Version 2.1
44  *
45  * Modularise the driver into a Board piece (this file) and a chip
46  * piece 53c700.[ch] and 53c700.scr, added module options.  You can
47  * now specify the scsi id by the parameters
48  *
49  * NCR_D700=slot:<n> [siop:<n>] id:<n> ....
50  *
51  * They need to be comma separated if compiled into the kernel
52  *
53  * Version 2.0
54  *
55  * Initial implementation of TCQ (Tag Command Queueing).  TCQ is full
56  * featured and uses the clock algorithm to keep track of outstanding
57  * tags and guard against individual tag starvation.  Also fixed a bug
58  * in all of the 1.x versions where the D700_data_residue() function
59  * was returning results off by 32 bytes (and thus causing the same 32
60  * bytes to be written twice corrupting the data block).  It turns out
61  * the 53c700 only has a 6 bit DBC and DFIFO registers not 7 bit ones
62  * like the 53c710 (The 710 is the only data manual still available,
63  * which I'd been using to program the 700).
64  *
65  * Version 1.2
66  *
67  * Much improved message handling engine
68  *
69  * Version 1.1
70  *
71  * Add code to handle selection reasonably correctly.  By the time we
72  * get the selection interrupt, we've already responded, but drop off the
73  * bus and hope the selector will go away.
74  *
75  * Version 1.0:
76  *
77  *   Initial release.  Fully functional except for procfs and tag
78  * command queueing.  Has only been tested on cards with 53c700-66
79  * chips and only single ended. Features are
80  *
81  * 1. Synchronous data transfers to offset 8 (limit of 700-66) and
82  *    100ns (10MHz) limit of SCSI-2
83  *
84  * 2. Disconnection and reselection
85  *
86  * Testing:
87  * 
88  *  I've only really tested this with the 700-66 chip, but have done
89  * soak tests in multi-device environments to verify that
90  * disconnections and reselections are being processed correctly.
91  * */
92
93 #define NCR_D700_VERSION "2.2"
94
95 #include <linux/blkdev.h>
96 #include <linux/interrupt.h>
97 #include <linux/kernel.h>
98 #include <linux/module.h>
99 #include <linux/mca.h>
100 #include <asm/io.h>
101 #include <scsi/scsi_host.h>
102 #include <scsi/scsi_device.h>
103 #include <scsi/scsi_transport.h>
104 #include <scsi/scsi_transport_spi.h>
105
106 #include "53c700.h"
107 #include "NCR_D700.h"
108
109 char *NCR_D700;                 /* command line from insmod */
110
111 MODULE_AUTHOR("James Bottomley");
112 MODULE_DESCRIPTION("NCR Dual700 SCSI Driver");
113 MODULE_LICENSE("GPL");
114 MODULE_PARM(NCR_D700, "s");
115
116 static __u8 __initdata id_array[2*(MCA_MAX_SLOT_NR + 1)] =
117         { [0 ... 2*(MCA_MAX_SLOT_NR + 1)-1] = 7 };
118
119 #ifdef MODULE
120 #define ARG_SEP ' '
121 #else
122 #define ARG_SEP ','
123 #endif
124
125 static int __init
126 param_setup(char *string)
127 {
128         char *pos = string, *next;
129         int slot = -1, siop = -1;
130
131         while(pos != NULL && (next = strchr(pos, ':')) != NULL) {
132                 int val = (int)simple_strtoul(++next, NULL, 0);
133
134                 if(!strncmp(pos, "slot:", 5))
135                         slot = val;
136                 else if(!strncmp(pos, "siop:", 5))
137                         siop = val;
138                 else if(!strncmp(pos, "id:", 3)) {
139                         if(slot == -1) {
140                                 printk(KERN_WARNING "NCR D700: Must specify slot for id parameter\n");
141                         } else if(slot > MCA_MAX_SLOT_NR) {
142                                 printk(KERN_WARNING "NCR D700: Illegal slot %d for id %d\n", slot, val);
143                         } else {
144                                 if(siop != 0 && siop != 1) {
145                                         id_array[slot*2] = val;
146                                         id_array[slot*2 + 1] =val;
147                                 } else {
148                                         id_array[slot*2 + siop] = val;
149                                 }
150                         }
151                 }
152                 if((pos = strchr(pos, ARG_SEP)) != NULL)
153                         pos++;
154         }
155         return 1;
156 }
157
158 /* Host template.  The 53c700 routine NCR_700_detect will
159  * fill in all of the missing routines */
160 static struct scsi_host_template NCR_D700_driver_template = {
161         .module                 = THIS_MODULE,
162         .name                   = "NCR Dual 700 MCA",
163         .proc_name              = "NCR_D700",
164         .this_id                = 7,
165 };
166
167 /* We needs this helper because we have two hosts per struct device */
168 struct NCR_D700_private {
169         struct device           *dev;
170         struct Scsi_Host        *hosts[2];
171 };
172
173 static int 
174 NCR_D700_probe_one(struct NCR_D700_private *p, int siop,
175                 int irq, int slot, u32 region, int differential)
176 {
177         struct NCR_700_Host_Parameters *hostdata;
178         struct Scsi_Host *host;
179         int ret;
180
181         hostdata = kmalloc(sizeof(*hostdata), GFP_KERNEL);
182         if (!hostdata) {
183                 printk(KERN_ERR "NCR D700: SIOP%d: Failed to allocate host"
184                        "data, detatching\n", siop);
185                 return -ENOMEM;
186         }
187         memset(hostdata, 0, sizeof(*hostdata));
188
189         if (!request_region(region, 64, "NCR_D700")) {
190                 printk(KERN_ERR "NCR D700: Failed to reserve IO region 0x%x\n",
191                                 region);
192                 ret = -ENODEV;
193                 goto region_failed;
194         }
195                 
196         /* Fill in the three required pieces of hostdata */
197         hostdata->base = region;
198         hostdata->differential = (((1<<siop) & differential) != 0);
199         hostdata->clock = NCR_D700_CLOCK_MHZ;
200
201         NCR_700_set_io_mapped(hostdata);
202
203         /* and register the siop */
204         host = NCR_700_detect(&NCR_D700_driver_template, hostdata);
205         if (!host) {
206                 ret = -ENOMEM;
207                 goto detect_failed;
208         }
209
210         host->irq = irq;
211         /* FIXME: Read this from SUS */
212         host->this_id = id_array[slot * 2 + siop];
213         printk(KERN_NOTICE "NCR D700: SIOP%d, SCSI id is %d\n",
214                         siop, host->this_id);
215         if (request_irq(irq, NCR_700_intr, SA_SHIRQ, "NCR_D700", host)) {
216                 printk(KERN_ERR "NCR D700: SIOP%d: irq problem, "
217                                 "detatching\n", siop);
218                 ret = -ENODEV;
219                 goto irq_failed;
220         }
221
222         scsi_add_host(host, p->dev); /* XXX handle failure */
223         scsi_scan_host(host);
224
225         p->hosts[siop] = host;
226         hostdata->dev = p->dev;
227         return 0;
228
229  irq_failed:
230         scsi_host_put(host);
231         NCR_700_release(host);
232  detect_failed:
233         release_region(host->base, 64);
234  region_failed:
235         kfree(hostdata);
236
237         return ret;
238 }
239
240 /* Detect a D700 card.  Note, because of the setup --- the chips are
241  * essentially connectecd to the MCA bus independently, it is easier
242  * to set them up as two separate host adapters, rather than one
243  * adapter with two channels */
244 static int
245 NCR_D700_probe(struct device *dev)
246 {
247         struct NCR_D700_private *p;
248         int differential;
249         static int banner = 1;
250         struct mca_device *mca_dev = to_mca_device(dev);
251         int slot = mca_dev->slot;
252         int found = 0;
253         int irq, i;
254         int pos3j, pos3k, pos3a, pos3b, pos4;
255         __u32 base_addr, offset_addr;
256
257         /* enable board interrupt */
258         pos4 = mca_device_read_pos(mca_dev, 4);
259         pos4 |= 0x4;
260         mca_device_write_pos(mca_dev, 4, pos4);
261
262         mca_device_write_pos(mca_dev, 6, 9);
263         pos3j = mca_device_read_pos(mca_dev, 3);
264         mca_device_write_pos(mca_dev, 6, 10);
265         pos3k = mca_device_read_pos(mca_dev, 3);
266         mca_device_write_pos(mca_dev, 6, 0);
267         pos3a = mca_device_read_pos(mca_dev, 3);
268         mca_device_write_pos(mca_dev, 6, 1);
269         pos3b = mca_device_read_pos(mca_dev, 3);
270
271         base_addr = ((pos3j << 8) | pos3k) & 0xfffffff0;
272         offset_addr = ((pos3a << 8) | pos3b) & 0xffffff70;
273
274         irq = (pos4 & 0x3) + 11;
275         if(irq >= 13)
276                 irq++;
277         if(banner) {
278                 printk(KERN_NOTICE "NCR D700: Driver Version " NCR_D700_VERSION "\n"
279                        "NCR D700:  Copyright (c) 2001 by James.Bottomley@HansenPartnership.com\n"
280                        "NCR D700:\n");
281                 banner = 0;
282         }
283         /* now do the bus related transforms */
284         irq = mca_device_transform_irq(mca_dev, irq);
285         base_addr = mca_device_transform_ioport(mca_dev, base_addr);
286         offset_addr = mca_device_transform_ioport(mca_dev, offset_addr);
287
288         printk(KERN_NOTICE "NCR D700: found in slot %d  irq = %d  I/O base = 0x%x\n", slot, irq, offset_addr);
289
290         /*outb(BOARD_RESET, base_addr);*/
291
292         /* clear any pending interrupts */
293         (void)inb(base_addr + 0x08);
294         /* get modctl, used later for setting diff bits */
295         switch(differential = (inb(base_addr + 0x08) >> 6)) {
296         case 0x00:
297                 /* only SIOP1 differential */
298                 differential = 0x02;
299                 break;
300         case 0x01:
301                 /* Both SIOPs differential */
302                 differential = 0x03;
303                 break;
304         case 0x03:
305                 /* No SIOPs differential */
306                 differential = 0x00;
307                 break;
308         default:
309                 printk(KERN_ERR "D700: UNEXPECTED DIFFERENTIAL RESULT 0x%02x\n",
310                        differential);
311                 differential = 0x00;
312                 break;
313         }
314
315         p = kmalloc(sizeof(*p), GFP_KERNEL);
316         if (!p)
317                 return -ENOMEM;
318         p->dev = dev;
319
320         /* plumb in both 700 chips */
321         for (i = 0; i < 2; i++) {
322                 int err;
323
324                 if ((err = NCR_D700_probe_one(p, i, irq, slot,
325                                               offset_addr + (0x80 * i),
326                                               differential)) != 0)
327                         printk("D700: SIOP%d: probe failed, error = %d\n",
328                                i, err);
329                 else
330                         found++;
331         }
332
333         if (!found) {
334                 kfree(p);
335                 return -ENODEV;
336         }
337
338         mca_device_set_claim(mca_dev, 1);
339         mca_device_set_name(mca_dev, "NCR_D700");
340         dev_set_drvdata(dev, p);
341         return 0;
342 }
343
344 static void
345 NCR_D700_remove_one(struct Scsi_Host *host)
346 {
347         scsi_remove_host(host);
348         NCR_700_release(host);
349         kfree((struct NCR_700_Host_Parameters *)host->hostdata[0]);
350         free_irq(host->irq, host);
351         release_region(host->base, 64);
352 }
353
354 static int
355 NCR_D700_remove(struct device *dev)
356 {
357         struct NCR_D700_private *p = dev_get_drvdata(dev);
358         int i;
359
360         for (i = 0; i < 2; i++)
361                 NCR_D700_remove_one(p->hosts[i]);
362
363         kfree(p);
364         return 0;
365 }
366
367 static short NCR_D700_id_table[] = { NCR_D700_MCA_ID, 0 };
368
369 struct mca_driver NCR_D700_driver = {
370         .id_table = NCR_D700_id_table,
371         .driver = {
372                 .name           = "NCR_D700",
373                 .bus            = &mca_bus_type,
374                 .probe          = NCR_D700_probe,
375                 .remove         = NCR_D700_remove,
376         },
377 };
378
379 static int __init NCR_D700_init(void)
380 {
381 #ifdef MODULE
382         if (NCR_D700)
383                 param_setup(NCR_D700);
384 #endif
385
386         return mca_register_driver(&NCR_D700_driver);
387 }
388
389 static void __exit NCR_D700_exit(void)
390 {
391         mca_unregister_driver(&NCR_D700_driver);
392 }
393
394 module_init(NCR_D700_init);
395 module_exit(NCR_D700_exit);
396
397 __setup("NCR_D700=", param_setup);