make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / acorn / scsi / powertec.c
1 /*
2  *  linux/drivers/acorn/scsi/powertec.c
3  *
4  *  Copyright (C) 1997-2000 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This driver is based on experimentation.  Hence, it may have made
11  * assumptions about the particular card that I have available, and
12  * may not be reliable!
13  *
14  * Changelog:
15  *  01-10-1997  RMK     Created, READONLY version.
16  *  15-02-1998  RMK     Added DMA support and hardware definitions.
17  *  15-04-1998  RMK     Only do PIO if FAS216 will allow it.
18  *  02-05-1998  RMK     Moved DMA sg list into per-interface structure.
19  *  27-06-1998  RMK     Changed asm/delay.h to linux/delay.h
20  *  02-04-2000  RMK     Updated for new error handling code.
21  */
22 #include <linux/module.h>
23 #include <linux/blk.h>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/ioport.h>
27 #include <linux/sched.h>
28 #include <linux/proc_fs.h>
29 #include <linux/unistd.h>
30 #include <linux/stat.h>
31 #include <linux/delay.h>
32 #include <linux/pci.h>
33 #include <linux/init.h>
34
35 #include <asm/dma.h>
36 #include <asm/ecard.h>
37 #include <asm/io.h>
38 #include <asm/irq.h>
39 #include <asm/pgtable.h>
40
41 #include "../../scsi/sd.h"
42 #include "../../scsi/hosts.h"
43 #include "fas216.h"
44
45 #include <scsi/scsicam.h>
46
47 /* Configuration */
48 #define POWERTEC_XTALFREQ       40
49 #define POWERTEC_ASYNC_PERIOD   200
50 #define POWERTEC_SYNC_DEPTH     7
51
52 /*
53  * List of devices that the driver will recognise
54  */
55 #define POWERTECSCSI_LIST       { MANU_ALSYSTEMS, PROD_ALSYS_SCSIATAPI }
56
57 #define POWERTEC_FAS216_OFFSET  0xc00
58 #define POWERTEC_FAS216_SHIFT   4
59
60 #define POWERTEC_INTR_STATUS    0x800
61 #define POWERTEC_INTR_BIT       0x80
62
63 #define POWERTEC_RESET_CONTROL  0x406
64 #define POWERTEC_RESET_BIT      1
65
66 #define POWERTEC_TERM_CONTROL   0x806
67 #define POWERTEC_TERM_ENABLE    1
68
69 #define POWERTEC_INTR_CONTROL   0x407
70 #define POWERTEC_INTR_ENABLE    1
71 #define POWERTEC_INTR_DISABLE   0
72
73 /*
74  * Version
75  */
76 #define VER_MAJOR       0
77 #define VER_MINOR       0
78 #define VER_PATCH       5
79
80 static struct expansion_card *ecs[MAX_ECARDS];
81
82 /*
83  * Use term=0,1,0,0,0 to turn terminators on/off
84  */
85 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
86
87 #define NR_SG   256
88
89 typedef struct {
90         FAS216_Info info;
91
92         struct {
93                 unsigned int term_port;
94                 unsigned int terms;
95         } control;
96
97         /* other info... */
98         struct scatterlist sg[NR_SG];   /* Scatter DMA list     */
99 } PowerTecScsi_Info;
100
101 /* Prototype: void powertecscsi_irqenable(ec, irqnr)
102  * Purpose  : Enable interrupts on Powertec SCSI card
103  * Params   : ec    - expansion card structure
104  *          : irqnr - interrupt number
105  */
106 static void
107 powertecscsi_irqenable(struct expansion_card *ec, int irqnr)
108 {
109         unsigned int port = (unsigned int)ec->irq_data;
110         outb(POWERTEC_INTR_ENABLE, port);
111 }
112
113 /* Prototype: void powertecscsi_irqdisable(ec, irqnr)
114  * Purpose  : Disable interrupts on Powertec SCSI card
115  * Params   : ec    - expansion card structure
116  *          : irqnr - interrupt number
117  */
118 static void
119 powertecscsi_irqdisable(struct expansion_card *ec, int irqnr)
120 {
121         unsigned int port = (unsigned int)ec->irq_data;
122         outb(POWERTEC_INTR_DISABLE, port);
123 }
124
125 static const expansioncard_ops_t powertecscsi_ops = {
126         powertecscsi_irqenable,
127         powertecscsi_irqdisable,
128         NULL,
129         NULL,
130         NULL,
131         NULL
132 };
133
134 /* Prototype: void powertecscsi_terminator_ctl(host, on_off)
135  * Purpose  : Turn the Powertec SCSI terminators on or off
136  * Params   : host   - card to turn on/off
137  *          : on_off - !0 to turn on, 0 to turn off
138  */
139 static void
140 powertecscsi_terminator_ctl(struct Scsi_Host *host, int on_off)
141 {
142         PowerTecScsi_Info *info = (PowerTecScsi_Info *)host->hostdata;
143
144         if (on_off)
145                 info->control.terms = POWERTEC_TERM_ENABLE;
146         else
147                 info->control.terms = 0;
148
149         outb(info->control.terms, info->control.term_port);
150 }
151
152 /* Prototype: void powertecscsi_intr(irq, *dev_id, *regs)
153  * Purpose  : handle interrupts from Powertec SCSI card
154  * Params   : irq    - interrupt number
155  *            dev_id - user-defined (Scsi_Host structure)
156  *            regs   - processor registers at interrupt
157  */
158 static void
159 powertecscsi_intr(int irq, void *dev_id, struct pt_regs *regs)
160 {
161         struct Scsi_Host *host = (struct Scsi_Host *)dev_id;
162
163         fas216_intr(host);
164 }
165
166 /* Prototype: fasdmatype_t powertecscsi_dma_setup(host, SCpnt, direction, min_type)
167  * Purpose  : initialises DMA/PIO
168  * Params   : host      - host
169  *            SCpnt     - command
170  *            direction - DMA on to/off of card
171  *            min_type  - minimum DMA support that we must have for this transfer
172  * Returns  : type of transfer to be performed
173  */
174 static fasdmatype_t
175 powertecscsi_dma_setup(struct Scsi_Host *host, Scsi_Pointer *SCp,
176                        fasdmadir_t direction, fasdmatype_t min_type)
177 {
178         PowerTecScsi_Info *info = (PowerTecScsi_Info *)host->hostdata;
179         int dmach = host->dma_channel;
180
181         if (dmach != NO_DMA &&
182             (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
183                 int bufs = SCp->buffers_residual;
184                 int pci_dir, dma_dir;
185
186                 if (bufs)
187                         memcpy(info->sg + 1, SCp->buffer + 1,
188                                 sizeof(struct scatterlist) * bufs);
189                 info->sg[0].address = SCp->ptr;
190                 info->sg[0].page    = NULL;
191                 info->sg[0].length  = SCp->this_residual;
192
193                 if (direction == DMA_OUT)
194                         pci_dir = PCI_DMA_TODEVICE,
195                         dma_dir = DMA_MODE_WRITE;
196                 else
197                         pci_dir = PCI_DMA_FROMDEVICE,
198                         dma_dir = DMA_MODE_READ;
199
200                 pci_map_sg(NULL, info->sg, bufs + 1, pci_dir);
201
202                 disable_dma(dmach);
203                 set_dma_sg(dmach, info->sg, bufs + 1);
204                 set_dma_mode(dmach, dma_dir);
205                 enable_dma(dmach);
206                 return fasdma_real_all;
207         }
208
209         /*
210          * If we're not doing DMA,
211          *  we'll do slow PIO
212          */
213         return fasdma_pio;
214 }
215
216 /* Prototype: int powertecscsi_dma_stop(host, SCpnt)
217  * Purpose  : stops DMA/PIO
218  * Params   : host  - host
219  *            SCpnt - command
220  */
221 static void
222 powertecscsi_dma_stop(struct Scsi_Host *host, Scsi_Pointer *SCp)
223 {
224         if (host->dma_channel != NO_DMA)
225                 disable_dma(host->dma_channel);
226 }
227
228 /* Prototype: int powertecscsi_detect(Scsi_Host_Template * tpnt)
229  * Purpose  : initialises PowerTec SCSI driver
230  * Params   : tpnt - template for this SCSI adapter
231  * Returns  : >0 if host found, 0 otherwise.
232  */
233 int
234 powertecscsi_detect(Scsi_Host_Template *tpnt)
235 {
236         static const card_ids powertecscsi_cids[] =
237                         { POWERTECSCSI_LIST, { 0xffff, 0xffff} };
238         int count = 0;
239         struct Scsi_Host *host;
240   
241         tpnt->proc_name = "powertec";
242         memset(ecs, 0, sizeof (ecs));
243
244         ecard_startfind();
245
246         while (1) {
247                 PowerTecScsi_Info *info;
248
249                 ecs[count] = ecard_find(0, powertecscsi_cids);
250                 if (!ecs[count])
251                         break;
252
253                 ecard_claim(ecs[count]);
254
255                 host = scsi_register(tpnt, sizeof (PowerTecScsi_Info));
256                 if (!host) {
257                         ecard_release(ecs[count]);
258                         break;
259                 }
260
261                 host->io_port = ecard_address(ecs[count], ECARD_IOC, ECARD_FAST);
262                 host->irq = ecs[count]->irq;
263                 host->dma_channel = ecs[count]->dma;
264                 info = (PowerTecScsi_Info *)host->hostdata;
265
266                 if (host->dma_channel != NO_DMA)
267                         set_dma_speed(host->dma_channel, 180);
268
269                 info->control.term_port = host->io_port + POWERTEC_TERM_CONTROL;
270                 info->control.terms = term[count] ? POWERTEC_TERM_ENABLE : 0;
271                 powertecscsi_terminator_ctl(host, info->control.terms);
272
273                 info->info.scsi.io_port =
274                                 host->io_port + POWERTEC_FAS216_OFFSET;
275                 info->info.scsi.io_shift= POWERTEC_FAS216_SHIFT;
276                 info->info.scsi.irq             = host->irq;
277                 info->info.ifcfg.clockrate      = POWERTEC_XTALFREQ;
278                 info->info.ifcfg.select_timeout = 255;
279                 info->info.ifcfg.asyncperiod    = POWERTEC_ASYNC_PERIOD;
280                 info->info.ifcfg.sync_max_depth = POWERTEC_SYNC_DEPTH;
281                 info->info.ifcfg.cntl3          = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
282                 info->info.ifcfg.disconnect_ok  = 1;
283                 info->info.ifcfg.wide_max_size  = 0;
284                 info->info.dma.setup            = powertecscsi_dma_setup;
285                 info->info.dma.pseudo           = NULL;
286                 info->info.dma.stop             = powertecscsi_dma_stop;
287
288                 ecs[count]->irqaddr     = (unsigned char *)
289                             ioaddr(host->io_port + POWERTEC_INTR_STATUS);
290                 ecs[count]->irqmask     = POWERTEC_INTR_BIT;
291                 ecs[count]->irq_data    = (void *)
292                             (host->io_port + POWERTEC_INTR_CONTROL);
293                 ecs[count]->ops         = (expansioncard_ops_t *)&powertecscsi_ops;
294
295                 request_region(host->io_port + POWERTEC_FAS216_OFFSET,
296                                16 << POWERTEC_FAS216_SHIFT, "powertec2-fas");
297
298                 if (host->irq != NO_IRQ &&
299                     request_irq(host->irq, powertecscsi_intr,
300                                 SA_INTERRUPT, "powertec", host)) {
301                         printk("scsi%d: IRQ%d not free, interrupts disabled\n",
302                                host->host_no, host->irq);
303                         host->irq = NO_IRQ;
304                         info->info.scsi.irq = NO_IRQ;
305                 }
306
307                 if (host->dma_channel != NO_DMA &&
308                     request_dma(host->dma_channel, "powertec")) {
309                         printk("scsi%d: DMA%d not free, DMA disabled\n",
310                                host->host_no, host->dma_channel);
311                         host->dma_channel = NO_DMA;
312                 }
313
314                 fas216_init(host);
315                 ++count;
316         }
317         return count;
318 }
319
320 /* Prototype: int powertecscsi_release(struct Scsi_Host * host)
321  * Purpose  : releases all resources used by this adapter
322  * Params   : host - driver host structure to return info for.
323  */
324 int powertecscsi_release(struct Scsi_Host *host)
325 {
326         int i;
327
328         fas216_release(host);
329
330         if (host->irq != NO_IRQ)
331                 free_irq(host->irq, host);
332         if (host->dma_channel != NO_DMA)
333                 free_dma(host->dma_channel);
334         release_region(host->io_port + POWERTEC_FAS216_OFFSET,
335                        16 << POWERTEC_FAS216_SHIFT);
336
337         for (i = 0; i < MAX_ECARDS; i++)
338                 if (ecs[i] &&
339                     host->io_port == ecard_address(ecs[i], ECARD_IOC, ECARD_FAST))
340                         ecard_release(ecs[i]);
341         return 0;
342 }
343
344 /* Prototype: const char *powertecscsi_info(struct Scsi_Host * host)
345  * Purpose  : returns a descriptive string about this interface,
346  * Params   : host - driver host structure to return info for.
347  * Returns  : pointer to a static buffer containing null terminated string.
348  */
349 const char *powertecscsi_info(struct Scsi_Host *host)
350 {
351         PowerTecScsi_Info *info = (PowerTecScsi_Info *)host->hostdata;
352         static char string[100], *p;
353
354         p = string;
355         p += sprintf(p, "%s ", host->hostt->name);
356         p += fas216_info(&info->info, p);
357         p += sprintf(p, "v%d.%d.%d terminators o%s",
358                      VER_MAJOR, VER_MINOR, VER_PATCH,
359                      info->control.terms ? "n" : "ff");
360
361         return string;
362 }
363
364 /* Prototype: int powertecscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
365  * Purpose  : Set a driver specific function
366  * Params   : host   - host to setup
367  *          : buffer - buffer containing string describing operation
368  *          : length - length of string
369  * Returns  : -EINVAL, or 0
370  */
371 static int
372 powertecscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
373 {
374         int ret = length;
375
376         if (length >= 12 && strncmp(buffer, "POWERTECSCSI", 12) == 0) {
377                 buffer += 12;
378                 length -= 12;
379
380                 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
381                         if (buffer[5] == '1')
382                                 powertecscsi_terminator_ctl(host, 1);
383                         else if (buffer[5] == '0')
384                                 powertecscsi_terminator_ctl(host, 0);
385                         else
386                                 ret = -EINVAL;
387                 } else
388                         ret = -EINVAL;
389         } else
390                 ret = -EINVAL;
391
392         return ret;
393 }
394
395 /* Prototype: int powertecscsi_proc_info(char *buffer, char **start, off_t offset,
396  *                                      int length, int host_no, int inout)
397  * Purpose  : Return information about the driver to a user process accessing
398  *            the /proc filesystem.
399  * Params   : buffer  - a buffer to write information to
400  *            start   - a pointer into this buffer set by this routine to the start
401  *                      of the required information.
402  *            offset  - offset into information that we have read upto.
403  *            length  - length of buffer
404  *            host_no - host number to return information for
405  *            inout   - 0 for reading, 1 for writing.
406  * Returns  : length of data written to buffer.
407  */
408 int powertecscsi_proc_info(char *buffer, char **start, off_t offset,
409                             int length, int host_no, int inout)
410 {
411         int pos, begin;
412         struct Scsi_Host *host = scsi_hostlist;
413         PowerTecScsi_Info *info;
414         Scsi_Device *scd;
415
416         while (host) {
417                 if (host->host_no == host_no)
418                         break;
419                 host = host->next;
420         }
421         if (!host)
422                 return 0;
423
424         if (inout == 1)
425                 return powertecscsi_set_proc_info(host, buffer, length);
426
427         info = (PowerTecScsi_Info *)host->hostdata;
428
429         begin = 0;
430         pos = sprintf(buffer,
431                         "PowerTec SCSI driver version %d.%d.%d\n",
432                         VER_MAJOR, VER_MINOR, VER_PATCH);
433
434         pos += fas216_print_host(&info->info, buffer + pos);
435         pos += sprintf(buffer + pos, "Term    : o%s\n",
436                         info->control.terms ? "n" : "ff");
437
438         pos += fas216_print_stats(&info->info, buffer + pos);
439
440         pos += sprintf(buffer+pos, "\nAttached devices:\n");
441
442         for (scd = host->host_queue; scd; scd = scd->next) {
443                 pos += fas216_print_device(&info->info, scd, buffer + pos);
444
445                 if (pos + begin < offset) {
446                         begin += pos;
447                         pos = 0;
448                 }
449                 if (pos + begin > offset + length)
450                         break;
451         }
452
453         *start = buffer + (offset - begin);
454         pos -= offset - begin;
455         if (pos > length)
456                 pos = length;
457
458         return pos;
459 }
460
461 static Scsi_Host_Template powertecscsi_template = {
462         module:                         THIS_MODULE,
463         proc_info:                      powertecscsi_proc_info,
464         name:                           "PowerTec SCSI",
465         detect:                         powertecscsi_detect,
466         release:                        powertecscsi_release,
467         info:                           powertecscsi_info,
468         bios_param:                     scsicam_bios_param,
469         can_queue:                      1,
470         this_id:                        7,
471         sg_tablesize:                   SG_ALL,
472         cmd_per_lun:                    1,
473         use_clustering:                 ENABLE_CLUSTERING,
474         command:                        fas216_command,
475         queuecommand:                   fas216_queue_command,
476         eh_host_reset_handler:          fas216_eh_host_reset,
477         eh_bus_reset_handler:           fas216_eh_bus_reset,
478         eh_device_reset_handler:        fas216_eh_device_reset,
479         eh_abort_handler:               fas216_eh_abort,
480         use_new_eh_code:                1
481 };
482
483 static int __init powertecscsi_init(void)
484 {
485         scsi_register_module(MODULE_SCSI_HA, &powertecscsi_template);
486         if (powertecscsi_template.present)
487                 return 0;
488
489         scsi_unregister_module(MODULE_SCSI_HA, &powertecscsi_template);
490         return -ENODEV;
491 }
492
493 static void __exit powertecscsi_exit(void)
494 {
495         scsi_unregister_module(MODULE_SCSI_HA, &powertecscsi_template);
496 }
497
498 module_init(powertecscsi_init);
499 module_exit(powertecscsi_exit);
500
501 MODULE_AUTHOR("Russell King");
502 MODULE_DESCRIPTION("Powertec SCSI driver");
503 MODULE_PARM(term, "1-8i");
504 MODULE_PARM_DESC(term, "SCSI bus termination");
505 MODULE_LICENSE("GPL");
506 EXPORT_NO_SYMBOLS;