import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / acorn / scsi / cumana_2.c
1 /*
2  *  linux/drivers/acorn/scsi/cumana_2.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  *  Changelog:
11  *   30-08-1997 RMK     0.0.0   Created, READONLY version.
12  *   22-01-1998 RMK     0.0.1   Updated to 2.1.80.
13  *   15-04-1998 RMK     0.0.1   Only do PIO if FAS216 will allow it.
14  *   02-05-1998 RMK     0.0.2   Updated & added DMA support.
15  *   27-06-1998 RMK             Changed asm/delay.h to linux/delay.h
16  *   18-08-1998 RMK     0.0.3   Fixed synchronous transfer depth.
17  *   02-04-2000 RMK     0.0.4   Updated for new error handling code.
18  */
19 #include <linux/module.h>
20 #include <linux/blk.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/ioport.h>
24 #include <linux/sched.h>
25 #include <linux/proc_fs.h>
26 #include <linux/unistd.h>
27 #include <linux/stat.h>
28 #include <linux/delay.h>
29 #include <linux/pci.h>
30 #include <linux/init.h>
31
32 #include <asm/dma.h>
33 #include <asm/ecard.h>
34 #include <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/pgtable.h>
37
38 #include "../../scsi/sd.h"
39 #include "../../scsi/hosts.h"
40 #include "fas216.h"
41
42 #include <scsi/scsicam.h>
43
44 /* Configuration */
45 #define CUMANASCSI2_XTALFREQ            40
46 #define CUMANASCSI2_ASYNC_PERIOD        200
47 #define CUMANASCSI2_SYNC_DEPTH          7
48
49 /*
50  * List of devices that the driver will recognise
51  */
52 #define CUMANASCSI2_LIST                { MANU_CUMANA, PROD_CUMANA_SCSI_2 }
53
54 #define CUMANASCSI2_STATUS              (0)
55 #define STATUS_INT                      (1 << 0)
56 #define STATUS_DRQ                      (1 << 1)
57 #define STATUS_LATCHED                  (1 << 3)
58
59 #define CUMANASCSI2_ALATCH              (5)
60 #define ALATCH_ENA_INT                  (3)
61 #define ALATCH_DIS_INT                  (2)
62 #define ALATCH_ENA_TERM                 (5)
63 #define ALATCH_DIS_TERM                 (4)
64 #define ALATCH_ENA_BIT32                (11)
65 #define ALATCH_DIS_BIT32                (10)
66 #define ALATCH_ENA_DMA                  (13)
67 #define ALATCH_DIS_DMA                  (12)
68 #define ALATCH_DMA_OUT                  (15)
69 #define ALATCH_DMA_IN                   (14)
70
71 #define CUMANASCSI2_PSEUDODMA           (0x80)
72
73 #define CUMANASCSI2_FAS216_OFFSET       (0xc0)
74 #define CUMANASCSI2_FAS216_SHIFT        0
75
76 /*
77  * Version
78  */
79 #define VER_MAJOR       0
80 #define VER_MINOR       0
81 #define VER_PATCH       4
82
83 static struct expansion_card *ecs[MAX_ECARDS];
84
85 /*
86  * Use term=0,1,0,0,0 to turn terminators on/off
87  */
88 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
89
90 #define NR_SG   256
91
92 typedef struct {
93         FAS216_Info info;
94
95         /* other info... */
96         unsigned int    status;         /* card status register */
97         unsigned int    alatch;         /* Control register     */
98         unsigned int    terms;          /* Terminator state     */
99         unsigned int    dmaarea;        /* Pseudo DMA area      */
100         struct scatterlist sg[NR_SG];   /* Scatter DMA list     */
101 } CumanaScsi2_Info;
102
103 #define CSTATUS_IRQ     (1 << 0)
104 #define CSTATUS_DRQ     (1 << 1)
105
106 /* Prototype: void cumanascsi_2_irqenable(ec, irqnr)
107  * Purpose  : Enable interrupts on Cumana SCSI 2 card
108  * Params   : ec    - expansion card structure
109  *          : irqnr - interrupt number
110  */
111 static void
112 cumanascsi_2_irqenable(struct expansion_card *ec, int irqnr)
113 {
114         unsigned int port = (unsigned int)ec->irq_data;
115         outb(ALATCH_ENA_INT, port);
116 }
117
118 /* Prototype: void cumanascsi_2_irqdisable(ec, irqnr)
119  * Purpose  : Disable interrupts on Cumana SCSI 2 card
120  * Params   : ec    - expansion card structure
121  *          : irqnr - interrupt number
122  */
123 static void
124 cumanascsi_2_irqdisable(struct expansion_card *ec, int irqnr)
125 {
126         unsigned int port = (unsigned int)ec->irq_data;
127         outb(ALATCH_DIS_INT, port);
128 }
129
130 static const expansioncard_ops_t cumanascsi_2_ops = {
131         cumanascsi_2_irqenable,
132         cumanascsi_2_irqdisable,
133         NULL,
134         NULL,
135         NULL,
136         NULL
137 };
138
139 /* Prototype: void cumanascsi_2_terminator_ctl(host, on_off)
140  * Purpose  : Turn the Cumana SCSI 2 terminators on or off
141  * Params   : host   - card to turn on/off
142  *          : on_off - !0 to turn on, 0 to turn off
143  */
144 static void
145 cumanascsi_2_terminator_ctl(struct Scsi_Host *host, int on_off)
146 {
147         CumanaScsi2_Info *info = (CumanaScsi2_Info *)host->hostdata;
148
149         if (on_off) {
150                 info->terms = 1;
151                 outb (ALATCH_ENA_TERM, info->alatch);
152         } else {
153                 info->terms = 0;
154                 outb (ALATCH_DIS_TERM, info->alatch);
155         }
156 }
157
158 /* Prototype: void cumanascsi_2_intr(irq, *dev_id, *regs)
159  * Purpose  : handle interrupts from Cumana SCSI 2 card
160  * Params   : irq    - interrupt number
161  *            dev_id - user-defined (Scsi_Host structure)
162  *            regs   - processor registers at interrupt
163  */
164 static void
165 cumanascsi_2_intr(int irq, void *dev_id, struct pt_regs *regs)
166 {
167         struct Scsi_Host *host = (struct Scsi_Host *)dev_id;
168
169         fas216_intr(host);
170 }
171
172 /* Prototype: fasdmatype_t cumanascsi_2_dma_setup(host, SCpnt, direction, min_type)
173  * Purpose  : initialises DMA/PIO
174  * Params   : host      - host
175  *            SCpnt     - command
176  *            direction - DMA on to/off of card
177  *            min_type  - minimum DMA support that we must have for this transfer
178  * Returns  : type of transfer to be performed
179  */
180 static fasdmatype_t
181 cumanascsi_2_dma_setup(struct Scsi_Host *host, Scsi_Pointer *SCp,
182                        fasdmadir_t direction, fasdmatype_t min_type)
183 {
184         CumanaScsi2_Info *info = (CumanaScsi2_Info *)host->hostdata;
185         int dmach = host->dma_channel;
186
187         outb(ALATCH_DIS_DMA, info->alatch);
188
189         if (dmach != NO_DMA &&
190             (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
191                 int bufs = SCp->buffers_residual;
192                 int pci_dir, dma_dir, alatch_dir;
193
194                 if (bufs)
195                         memcpy(info->sg + 1, SCp->buffer + 1,
196                                 sizeof(struct scatterlist) * bufs);
197                 info->sg[0].address = SCp->ptr;
198                 info->sg[0].page    = NULL;
199                 info->sg[0].length  = SCp->this_residual;
200
201                 if (direction == DMA_OUT)
202                         pci_dir = PCI_DMA_TODEVICE,
203                         dma_dir = DMA_MODE_WRITE,
204                         alatch_dir = ALATCH_DMA_OUT;
205                 else
206                         pci_dir = PCI_DMA_FROMDEVICE,
207                         dma_dir = DMA_MODE_READ,
208                         alatch_dir = ALATCH_DMA_IN;
209
210                 pci_map_sg(NULL, info->sg, bufs + 1, pci_dir);
211
212                 disable_dma(dmach);
213                 set_dma_sg(dmach, info->sg, bufs + 1);
214                 outb(alatch_dir, info->alatch);
215                 set_dma_mode(dmach, dma_dir);
216                 enable_dma(dmach);
217                 outb(ALATCH_ENA_DMA, info->alatch);
218                 outb(ALATCH_DIS_BIT32, info->alatch);
219                 return fasdma_real_all;
220         }
221
222         /*
223          * If we're not doing DMA,
224          *  we'll do pseudo DMA
225          */
226         return fasdma_pio;
227 }
228
229 /*
230  * Prototype: void cumanascsi_2_dma_pseudo(host, SCpnt, direction, transfer)
231  * Purpose  : handles pseudo DMA
232  * Params   : host      - host
233  *            SCpnt     - command
234  *            direction - DMA on to/off of card
235  *            transfer  - minimum number of bytes we expect to transfer
236  */
237 static void
238 cumanascsi_2_dma_pseudo(struct Scsi_Host *host, Scsi_Pointer *SCp,
239                         fasdmadir_t direction, int transfer)
240 {
241         CumanaScsi2_Info *info = (CumanaScsi2_Info *)host->hostdata;
242         unsigned int length;
243         unsigned char *addr;
244
245         length = SCp->this_residual;
246         addr = SCp->ptr;
247
248         if (direction == DMA_OUT)
249 #if 0
250                 while (length > 1) {
251                         unsigned long word;
252                         unsigned int status = inb(info->status);
253
254                         if (status & STATUS_INT)
255                                 goto end;
256
257                         if (!(status & STATUS_DRQ))
258                                 continue;
259
260                         word = *addr | *(addr + 1) << 8;
261                         outw (info->dmaarea);
262                         addr += 2;
263                         length -= 2;
264                 }
265 #else
266                 printk ("PSEUDO_OUT???\n");
267 #endif
268         else {
269                 if (transfer && (transfer & 255)) {
270                         while (length >= 256) {
271                                 unsigned int status = inb(info->status);
272
273                                 if (status & STATUS_INT)
274                                         goto end;
275             
276                                 if (!(status & STATUS_DRQ))
277                                         continue;
278
279                                 insw(info->dmaarea, addr, 256 >> 1);
280                                 addr += 256;
281                                 length -= 256;
282                         }
283                 }
284
285                 while (length > 0) {
286                         unsigned long word;
287                         unsigned int status = inb(info->status);
288
289                         if (status & STATUS_INT)
290                                 goto end;
291
292                         if (!(status & STATUS_DRQ))
293                                 continue;
294
295                         word = inw (info->dmaarea);
296                         *addr++ = word;
297                         if (--length > 0) {
298                                 *addr++ = word >> 8;
299                                 length --;
300                         }
301                 }
302         }
303
304 end:
305 }
306
307 /* Prototype: int cumanascsi_2_dma_stop(host, SCpnt)
308  * Purpose  : stops DMA/PIO
309  * Params   : host  - host
310  *            SCpnt - command
311  */
312 static void
313 cumanascsi_2_dma_stop(struct Scsi_Host *host, Scsi_Pointer *SCp)
314 {
315         CumanaScsi2_Info *info = (CumanaScsi2_Info *)host->hostdata;
316         if (host->dma_channel != NO_DMA) {
317                 outb(ALATCH_DIS_DMA, info->alatch);
318                 disable_dma(host->dma_channel);
319         }
320 }
321
322 /* Prototype: int cumanascsi_2_detect(Scsi_Host_Template * tpnt)
323  * Purpose  : initialises Cumana SCSI 2 driver
324  * Params   : tpnt - template for this SCSI adapter
325  * Returns  : >0 if host found, 0 otherwise.
326  */
327 int
328 cumanascsi_2_detect(Scsi_Host_Template *tpnt)
329 {
330         static const card_ids cumanascsi_2_cids[] =
331                         { CUMANASCSI2_LIST, { 0xffff, 0xffff} };
332         int count = 0;
333         struct Scsi_Host *host;
334   
335         tpnt->proc_name = "cumanascs2";
336         memset(ecs, 0, sizeof (ecs));
337
338         ecard_startfind();
339
340         while (1) {
341                 CumanaScsi2_Info *info;
342
343                 ecs[count] = ecard_find(0, cumanascsi_2_cids);
344                 if (!ecs[count])
345                         break;
346
347                 ecard_claim(ecs[count]);
348
349                 host = scsi_register(tpnt, sizeof (CumanaScsi2_Info));
350                 if (!host) {
351                         ecard_release(ecs[count]);
352                         break;
353                 }
354
355                 host->io_port = ecard_address(ecs[count], ECARD_MEMC, 0);
356                 host->irq = ecs[count]->irq;
357                 host->dma_channel = ecs[count]->dma;
358                 info = (CumanaScsi2_Info *)host->hostdata;
359
360                 info->terms                     = term[count] ? 1 : 0;
361                 cumanascsi_2_terminator_ctl(host, info->terms);
362
363                 info->info.scsi.io_port         = host->io_port + CUMANASCSI2_FAS216_OFFSET;
364                 info->info.scsi.io_shift        = CUMANASCSI2_FAS216_SHIFT;
365                 info->info.scsi.irq             = host->irq;
366                 info->info.ifcfg.clockrate      = CUMANASCSI2_XTALFREQ;
367                 info->info.ifcfg.select_timeout = 255;
368                 info->info.ifcfg.asyncperiod    = CUMANASCSI2_ASYNC_PERIOD;
369                 info->info.ifcfg.sync_max_depth = CUMANASCSI2_SYNC_DEPTH;
370                 info->info.ifcfg.cntl3          = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
371                 info->info.ifcfg.disconnect_ok  = 1;
372                 info->info.ifcfg.wide_max_size  = 0;
373                 info->info.dma.setup            = cumanascsi_2_dma_setup;
374                 info->info.dma.pseudo           = cumanascsi_2_dma_pseudo;
375                 info->info.dma.stop             = cumanascsi_2_dma_stop;
376                 info->dmaarea                   = host->io_port + CUMANASCSI2_PSEUDODMA;
377                 info->status                    = host->io_port + CUMANASCSI2_STATUS;
378                 info->alatch                    = host->io_port + CUMANASCSI2_ALATCH;
379
380                 ecs[count]->irqaddr     = (unsigned char *)ioaddr(info->status);
381                 ecs[count]->irqmask     = STATUS_INT;
382                 ecs[count]->irq_data    = (void *)info->alatch;
383                 ecs[count]->ops         = (expansioncard_ops_t *)&cumanascsi_2_ops;
384
385                 request_region(host->io_port + CUMANASCSI2_FAS216_OFFSET,
386                                16 << CUMANASCSI2_FAS216_SHIFT, "cumanascsi2-fas");
387
388                 if (host->irq != NO_IRQ &&
389                     request_irq(host->irq, cumanascsi_2_intr,
390                                 SA_INTERRUPT, "cumanascsi2", host)) {
391                         printk("scsi%d: IRQ%d not free, interrupts disabled\n",
392                                host->host_no, host->irq);
393                         host->irq = NO_IRQ;
394                         info->info.scsi.irq = NO_IRQ;
395                 }
396
397                 if (host->dma_channel != NO_DMA &&
398                     request_dma(host->dma_channel, "cumanascsi2")) {
399                         printk("scsi%d: DMA%d not free, DMA disabled\n",
400                                host->host_no, host->dma_channel);
401                         host->dma_channel = NO_DMA;
402                 }
403
404                 fas216_init(host);
405                 ++count;
406         }
407         return count;
408 }
409
410 /* Prototype: int cumanascsi_2_release(struct Scsi_Host * host)
411  * Purpose  : releases all resources used by this adapter
412  * Params   : host - driver host structure to return info for.
413  */
414 int cumanascsi_2_release(struct Scsi_Host *host)
415 {
416         int i;
417
418         fas216_release(host);
419
420         if (host->irq != NO_IRQ)
421                 free_irq(host->irq, host);
422         if (host->dma_channel != NO_DMA)
423                 free_dma(host->dma_channel);
424         release_region(host->io_port + CUMANASCSI2_FAS216_OFFSET,
425                        16 << CUMANASCSI2_FAS216_SHIFT);
426
427         for (i = 0; i < MAX_ECARDS; i++)
428                 if (ecs[i] && host->io_port == ecard_address (ecs[i], ECARD_MEMC, 0))
429                         ecard_release (ecs[i]);
430         return 0;
431 }
432
433 /* Prototype: const char *cumanascsi_2_info(struct Scsi_Host * host)
434  * Purpose  : returns a descriptive string about this interface,
435  * Params   : host - driver host structure to return info for.
436  * Returns  : pointer to a static buffer containing null terminated string.
437  */
438 const char *cumanascsi_2_info(struct Scsi_Host *host)
439 {
440         CumanaScsi2_Info *info = (CumanaScsi2_Info *)host->hostdata;
441         static char string[100], *p;
442
443         p = string;
444         p += sprintf(p, "%s ", host->hostt->name);
445         p += fas216_info(&info->info, p);
446         p += sprintf(p, "v%d.%d.%d terminators o%s",
447                      VER_MAJOR, VER_MINOR, VER_PATCH,
448                      info->terms ? "n" : "ff");
449
450         return string;
451 }
452
453 /* Prototype: int cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
454  * Purpose  : Set a driver specific function
455  * Params   : host   - host to setup
456  *          : buffer - buffer containing string describing operation
457  *          : length - length of string
458  * Returns  : -EINVAL, or 0
459  */
460 static int
461 cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
462 {
463         int ret = length;
464
465         if (length >= 11 && strcmp(buffer, "CUMANASCSI2") == 0) {
466                 buffer += 11;
467                 length -= 11;
468
469                 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
470                         if (buffer[5] == '1')
471                                 cumanascsi_2_terminator_ctl(host, 1);
472                         else if (buffer[5] == '0')
473                                 cumanascsi_2_terminator_ctl(host, 0);
474                         else
475                                 ret = -EINVAL;
476                 } else
477                         ret = -EINVAL;
478         } else
479                 ret = -EINVAL;
480
481         return ret;
482 }
483
484 /* Prototype: int cumanascsi_2_proc_info(char *buffer, char **start, off_t offset,
485  *                                       int length, int host_no, int inout)
486  * Purpose  : Return information about the driver to a user process accessing
487  *            the /proc filesystem.
488  * Params   : buffer - a buffer to write information to
489  *            start  - a pointer into this buffer set by this routine to the start
490  *                     of the required information.
491  *            offset - offset into information that we have read upto.
492  *            length - length of buffer
493  *            host_no - host number to return information for
494  *            inout  - 0 for reading, 1 for writing.
495  * Returns  : length of data written to buffer.
496  */
497 int cumanascsi_2_proc_info (char *buffer, char **start, off_t offset,
498                             int length, int host_no, int inout)
499 {
500         int pos, begin;
501         struct Scsi_Host *host = scsi_hostlist;
502         CumanaScsi2_Info *info;
503         Scsi_Device *scd;
504
505         while (host) {
506                 if (host->host_no == host_no)
507                         break;
508                 host = host->next;
509         }
510         if (!host)
511                 return 0;
512
513         if (inout == 1)
514                 return cumanascsi_2_set_proc_info(host, buffer, length);
515
516         info = (CumanaScsi2_Info *)host->hostdata;
517
518         begin = 0;
519         pos = sprintf(buffer,
520                         "Cumana SCSI II driver version %d.%d.%d\n",
521                         VER_MAJOR, VER_MINOR, VER_PATCH);
522
523         pos += fas216_print_host(&info->info, buffer + pos);
524         pos += sprintf(buffer + pos, "Term    : o%s\n",
525                         info->terms ? "n" : "ff");
526
527         pos += fas216_print_stats(&info->info, buffer + pos);
528
529         pos += sprintf(buffer+pos, "\nAttached devices:\n");
530
531         for (scd = host->host_queue; scd; scd = scd->next) {
532                 int len;
533
534                 proc_print_scsidevice(scd, buffer, &len, pos);
535                 pos += len;
536                 pos += sprintf(buffer+pos, "Extensions: ");
537                 if (scd->tagged_supported)
538                         pos += sprintf(buffer+pos, "TAG %sabled [%d] ",
539                                        scd->tagged_queue ? "en" : "dis",
540                                        scd->current_tag);
541                 pos += sprintf(buffer+pos, "\n");
542
543                 if (pos + begin < offset) {
544                         begin += pos;
545                         pos = 0;
546                 }
547                 if (pos + begin > offset + length)
548                         break;
549         }
550
551         *start = buffer + (offset - begin);
552         pos -= offset - begin;
553         if (pos > length)
554                 pos = length;
555
556         return pos;
557 }
558
559 static Scsi_Host_Template cumanascsi2_template = {
560         module:                         THIS_MODULE,
561         proc_info:                      cumanascsi_2_proc_info,
562         name:                           "Cumana SCSI II",
563         detect:                         cumanascsi_2_detect,
564         release:                        cumanascsi_2_release,
565         info:                           cumanascsi_2_info,
566         bios_param:                     scsicam_bios_param,
567         can_queue:                      1,
568         this_id:                        7,
569         sg_tablesize:                   SG_ALL,
570         cmd_per_lun:                    1,
571         use_clustering:                 DISABLE_CLUSTERING,
572         command:                        fas216_command,
573         queuecommand:                   fas216_queue_command,
574         eh_host_reset_handler:          fas216_eh_host_reset,
575         eh_bus_reset_handler:           fas216_eh_bus_reset,
576         eh_device_reset_handler:        fas216_eh_device_reset,
577         eh_abort_handler:               fas216_eh_abort,
578         use_new_eh_code:                1
579 };
580
581 static int __init cumanascsi2_init(void)
582 {
583         scsi_register_module(MODULE_SCSI_HA, &cumanascsi2_template);
584         if (cumanascsi2_template.present)
585                 return 0;
586
587         scsi_unregister_module(MODULE_SCSI_HA, &cumanascsi2_template);
588         return -ENODEV;
589 }
590
591 static void __exit cumanascsi2_exit(void)
592 {
593         scsi_unregister_module(MODULE_SCSI_HA, &cumanascsi2_template);
594 }
595
596 module_init(cumanascsi2_init);
597 module_exit(cumanascsi2_exit);
598
599 MODULE_AUTHOR("Russell King");
600 MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
601 MODULE_PARM(term, "1-8i");
602 MODULE_PARM_DESC(term, "SCSI bus termination");
603 MODULE_LICENSE("GPL");
604 EXPORT_NO_SYMBOLS;