[SCSI] lpfc 8.1.12 : Misc bug fixes and code cleanup
[linux-2.6] / drivers / scsi / mac_scsi.c
1 /*
2  * Generic Macintosh NCR5380 driver
3  *
4  * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
5  *
6  * derived in part from:
7  */
8 /*
9  * Generic Generic NCR5380 driver
10  *
11  * Copyright 1995, Russell King
12  *
13  * ALPHA RELEASE 1.
14  *
15  * For more information, please consult
16  *
17  * NCR 5380 Family
18  * SCSI Protocol Controller
19  * Databook
20  *
21  * NCR Microelectronics
22  * 1635 Aeroplaza Drive
23  * Colorado Springs, CO 80916
24  * 1+ (719) 578-3400
25  * 1+ (800) 334-5454
26  */
27
28 /*
29  * $Log: mac_NCR5380.c,v $
30  */
31
32 #include <linux/types.h>
33 #include <linux/stddef.h>
34 #include <linux/ctype.h>
35 #include <linux/delay.h>
36
37 #include <linux/module.h>
38 #include <linux/signal.h>
39 #include <linux/ioport.h>
40 #include <linux/init.h>
41 #include <linux/blkdev.h>
42 #include <linux/interrupt.h>
43
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/system.h>
47
48 #include <asm/macintosh.h>
49 #include <asm/macints.h>
50 #include <asm/machw.h>
51 #include <asm/mac_via.h>
52
53 #include "scsi.h"
54 #include <scsi/scsi_host.h>
55 #include "mac_scsi.h"
56 #include "NCR5380.h"
57
58 #if 0
59 #define NDEBUG (NDEBUG_INTR | NDEBUG_PSEUDO_DMA | NDEBUG_ARBITRATION | NDEBUG_SELECTION | NDEBUG_RESELECTION)
60 #else
61 #define NDEBUG (NDEBUG_ABORT)
62 #endif
63
64 #define RESET_BOOT
65 #define DRIVER_SETUP
66
67 extern void via_scsi_clear(void);
68
69 #ifdef RESET_BOOT
70 static void mac_scsi_reset_boot(struct Scsi_Host *instance);
71 #endif
72
73 static int setup_called = 0;
74 static int setup_can_queue = -1;
75 static int setup_cmd_per_lun = -1;
76 static int setup_sg_tablesize = -1;
77 static int setup_use_pdma = -1;
78 #ifdef SUPPORT_TAGS
79 static int setup_use_tagged_queuing = -1;
80 #endif
81 static int setup_hostid = -1;
82
83 /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
84  * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
85  * need ten times the standard value... */
86 #define TOSHIBA_DELAY
87
88 #ifdef TOSHIBA_DELAY
89 #define AFTER_RESET_DELAY       (5*HZ/2)
90 #else
91 #define AFTER_RESET_DELAY       (HZ/2)
92 #endif
93
94 static volatile unsigned char *mac_scsi_regp = NULL;
95 static volatile unsigned char *mac_scsi_drq  = NULL;
96 static volatile unsigned char *mac_scsi_nodrq = NULL;
97
98
99 /*
100  * NCR 5380 register access functions
101  */
102
103 #if 0
104 /* Debug versions */
105 #define CTRL(p,v) (*ctrl = (v))
106
107 static char macscsi_read(struct Scsi_Host *instance, int reg)
108 {
109   int iobase = instance->io_port;
110   int i;
111   int *ctrl = &((struct NCR5380_hostdata *)instance->hostdata)->ctrl;
112
113   CTRL(iobase, 0);
114   i = in_8(iobase + (reg<<4));
115   CTRL(iobase, 0x40);
116
117   return i;
118 }
119
120 static void macscsi_write(struct Scsi_Host *instance, int reg, int value)
121 {
122   int iobase = instance->io_port;
123   int *ctrl = &((struct NCR5380_hostdata *)instance->hostdata)->ctrl;
124
125   CTRL(iobase, 0);
126   out_8(iobase + (reg<<4), value);
127   CTRL(iobase, 0x40);
128 }
129 #else
130
131 /* Fast versions */
132 static __inline__ char macscsi_read(struct Scsi_Host *instance, int reg)
133 {
134   return in_8(instance->io_port + (reg<<4));
135 }
136
137 static __inline__ void macscsi_write(struct Scsi_Host *instance, int reg, int value)
138 {
139   out_8(instance->io_port + (reg<<4), value);
140 }
141 #endif
142
143
144 /*
145  * Function : mac_scsi_setup(char *str)
146  *
147  * Purpose : booter command line initialization of the overrides array,
148  *
149  * Inputs : str - comma delimited list of options
150  *
151  */
152
153 static int __init mac_scsi_setup(char *str) {
154 #ifdef DRIVER_SETUP     
155         int ints[7];
156         
157         (void)get_options( str, ARRAY_SIZE(ints), ints);
158         
159         if (setup_called++ || ints[0] < 1 || ints[0] > 6) {
160             printk(KERN_WARNING "scsi: <mac5380>"
161                 " Usage: mac5380=<can_queue>[,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>,<use_pdma>]\n");
162             printk(KERN_ALERT "scsi: <mac5380> Bad Penguin parameters?\n");
163             return 0;
164         }
165             
166         if (ints[0] >= 1) {
167                 if (ints[1] > 0)
168                         /* no limits on this, just > 0 */
169                         setup_can_queue = ints[1];
170         }
171         if (ints[0] >= 2) {
172                 if (ints[2] > 0)
173                         setup_cmd_per_lun = ints[2];
174         }
175         if (ints[0] >= 3) {
176                 if (ints[3] >= 0) {
177                         setup_sg_tablesize = ints[3];
178                         /* Must be <= SG_ALL (255) */
179                         if (setup_sg_tablesize > SG_ALL)
180                                 setup_sg_tablesize = SG_ALL;
181                 }
182         }
183         if (ints[0] >= 4) {
184                 /* Must be between 0 and 7 */
185                 if (ints[4] >= 0 && ints[4] <= 7)
186                         setup_hostid = ints[4];
187                 else if (ints[4] > 7)
188                         printk(KERN_WARNING "mac_scsi_setup: invalid host ID %d !\n", ints[4] );
189         }
190 #ifdef SUPPORT_TAGS     
191         if (ints[0] >= 5) {
192                 if (ints[5] >= 0)
193                         setup_use_tagged_queuing = !!ints[5];
194         }
195         
196         if (ints[0] == 6) {
197             if (ints[6] >= 0)
198                 setup_use_pdma = ints[6];
199         }
200 #else
201         if (ints[0] == 5) {
202             if (ints[5] >= 0)
203                 setup_use_pdma = ints[5];
204         }
205 #endif /* SUPPORT_TAGS */
206         
207 #endif /* DRIVER_SETUP */
208         return 1;
209 }
210
211 __setup("mac5380=", mac_scsi_setup);
212
213 /*
214  * If you want to find the instance with (k)gdb ...
215  */
216 #if NDEBUG
217 static struct Scsi_Host *default_instance;
218 #endif
219
220 /*
221  * Function : int macscsi_detect(struct scsi_host_template * tpnt)
222  *
223  * Purpose : initializes mac NCR5380 driver based on the
224  *      command line / compile time port and irq definitions.
225  *
226  * Inputs : tpnt - template for this SCSI adapter.
227  *
228  * Returns : 1 if a host adapter was found, 0 if not.
229  *
230  */
231  
232 int macscsi_detect(struct scsi_host_template * tpnt)
233 {
234     static int called = 0;
235     int flags = 0;
236     struct Scsi_Host *instance;
237
238     if (!MACH_IS_MAC || called)
239         return( 0 );
240
241     if (macintosh_config->scsi_type != MAC_SCSI_OLD)
242         return( 0 );
243
244     /* setup variables */
245     tpnt->can_queue =
246         (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
247     tpnt->cmd_per_lun =
248         (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
249     tpnt->sg_tablesize = 
250         (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
251
252     if (setup_hostid >= 0)
253         tpnt->this_id = setup_hostid;
254     else {
255         /* use 7 as default */
256         tpnt->this_id = 7;
257     }
258
259 #ifdef SUPPORT_TAGS
260     if (setup_use_tagged_queuing < 0)
261         setup_use_tagged_queuing = USE_TAGGED_QUEUING;
262 #endif
263
264     /* Once we support multiple 5380s (e.g. DuoDock) we'll do
265        something different here */
266     instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
267 #if NDEBUG
268     default_instance = instance;
269 #endif
270     
271     if (macintosh_config->ident == MAC_MODEL_IIFX) {
272         mac_scsi_regp  = via1+0x8000;
273         mac_scsi_drq   = via1+0xE000;
274         mac_scsi_nodrq = via1+0xC000;
275         /* The IIFX should be able to do true DMA, but pseudo-dma doesn't work */
276         flags = FLAG_NO_PSEUDO_DMA;
277     } else {
278         mac_scsi_regp  = via1+0x10000;
279         mac_scsi_drq   = via1+0x6000;
280         mac_scsi_nodrq = via1+0x12000;
281     }
282
283     if (! setup_use_pdma)
284         flags = FLAG_NO_PSEUDO_DMA;
285         
286     instance->io_port = (unsigned long) mac_scsi_regp;
287     instance->irq = IRQ_MAC_SCSI;
288
289 #ifdef RESET_BOOT   
290     mac_scsi_reset_boot(instance);
291 #endif
292     
293     NCR5380_init(instance, flags);
294
295     instance->n_io_port = 255;
296
297     ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
298
299     if (instance->irq != SCSI_IRQ_NONE)
300         if (request_irq(instance->irq, NCR5380_intr, IRQ_FLG_SLOW, 
301                 "ncr5380", instance)) {
302             printk(KERN_WARNING "scsi%d: IRQ%d not free, interrupts disabled\n",
303                    instance->host_no, instance->irq);
304             instance->irq = SCSI_IRQ_NONE;
305         }
306
307     printk(KERN_INFO "scsi%d: generic 5380 at port %lX irq", instance->host_no, instance->io_port);
308     if (instance->irq == SCSI_IRQ_NONE)
309         printk (KERN_INFO "s disabled");
310     else
311         printk (KERN_INFO " %d", instance->irq);
312     printk(KERN_INFO " options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
313            instance->can_queue, instance->cmd_per_lun, MACSCSI_PUBLIC_RELEASE);
314     printk(KERN_INFO "\nscsi%d:", instance->host_no);
315     NCR5380_print_options(instance);
316     printk("\n");
317     called = 1;
318     return 1;
319 }
320
321 int macscsi_release (struct Scsi_Host *shpnt)
322 {
323         if (shpnt->irq != SCSI_IRQ_NONE)
324                 free_irq (shpnt->irq, NCR5380_intr);
325         NCR5380_exit(shpnt);
326
327         return 0;
328 }
329
330 #ifdef RESET_BOOT
331 /*
332  * Our 'bus reset on boot' function
333  */
334
335 static void mac_scsi_reset_boot(struct Scsi_Host *instance)
336 {
337         unsigned long end;
338
339         NCR5380_local_declare();
340         NCR5380_setup(instance);
341         
342         /*
343          * Do a SCSI reset to clean up the bus during initialization. No messing
344          * with the queues, interrupts, or locks necessary here.
345          */
346
347         printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." );
348
349         /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
350         disable_irq(IRQ_MAC_SCSI);
351
352         /* get in phase */
353         NCR5380_write( TARGET_COMMAND_REG,
354                       PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
355
356         /* assert RST */
357         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
358         /* The min. reset hold time is 25us, so 40us should be enough */
359         udelay( 50 );
360         /* reset RST and interrupt */
361         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
362         NCR5380_read( RESET_PARITY_INTERRUPT_REG );
363
364         for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
365                 barrier();
366
367         /* switch on SCSI IRQ again */
368         enable_irq(IRQ_MAC_SCSI);
369
370         printk(KERN_INFO " done\n" );
371 }
372 #endif
373
374 const char * macscsi_info (struct Scsi_Host *spnt) {
375         return "";
376 }
377
378 /* 
379    Pseudo-DMA: (Ove Edlund)
380    The code attempts to catch bus errors that occur if one for example
381    "trips over the cable".
382    XXX: Since bus errors in the PDMA routines never happen on my 
383    computer, the bus error code is untested. 
384    If the code works as intended, a bus error results in Pseudo-DMA 
385    beeing disabled, meaning that the driver switches to slow handshake. 
386    If bus errors are NOT extremely rare, this has to be changed. 
387 */
388
389 #define CP_IO_TO_MEM(s,d,len)                           \
390 __asm__ __volatile__                                    \
391     ("    cmp.w  #4,%2\n"                               \
392      "    bls    8f\n"                                  \
393      "    move.w %1,%%d0\n"                             \
394      "    neg.b  %%d0\n"                                \
395      "    and.w  #3,%%d0\n"                             \
396      "    sub.w  %%d0,%2\n"                             \
397      "    bra    2f\n"                                  \
398      " 1: move.b (%0),(%1)+\n"                          \
399      " 2: dbf    %%d0,1b\n"                             \
400      "    move.w %2,%%d0\n"                             \
401      "    lsr.w  #5,%%d0\n"                             \
402      "    bra    4f\n"                                  \
403      " 3: move.l (%0),(%1)+\n"                          \
404      "31: move.l (%0),(%1)+\n"                          \
405      "32: move.l (%0),(%1)+\n"                          \
406      "33: move.l (%0),(%1)+\n"                          \
407      "34: move.l (%0),(%1)+\n"                          \
408      "35: move.l (%0),(%1)+\n"                          \
409      "36: move.l (%0),(%1)+\n"                          \
410      "37: move.l (%0),(%1)+\n"                          \
411      " 4: dbf    %%d0,3b\n"                             \
412      "    move.w %2,%%d0\n"                             \
413      "    lsr.w  #2,%%d0\n"                             \
414      "    and.w  #7,%%d0\n"                             \
415      "    bra    6f\n"                                  \
416      " 5: move.l (%0),(%1)+\n"                          \
417      " 6: dbf    %%d0,5b\n"                             \
418      "    and.w  #3,%2\n"                               \
419      "    bra    8f\n"                                  \
420      " 7: move.b (%0),(%1)+\n"                          \
421      " 8: dbf    %2,7b\n"                               \
422      "    moveq.l #0, %2\n"                             \
423      " 9: \n"                                           \
424      ".section .fixup,\"ax\"\n"                         \
425      "    .even\n"                                      \
426      "90: moveq.l #1, %2\n"                             \
427      "    jra 9b\n"                                     \
428      ".previous\n"                                      \
429      ".section __ex_table,\"a\"\n"                      \
430      "   .align 4\n"                                    \
431      "   .long  1b,90b\n"                               \
432      "   .long  3b,90b\n"                               \
433      "   .long 31b,90b\n"                               \
434      "   .long 32b,90b\n"                               \
435      "   .long 33b,90b\n"                               \
436      "   .long 34b,90b\n"                               \
437      "   .long 35b,90b\n"                               \
438      "   .long 36b,90b\n"                               \
439      "   .long 37b,90b\n"                               \
440      "   .long  5b,90b\n"                               \
441      "   .long  7b,90b\n"                               \
442      ".previous"                                        \
443      : "=a"(s), "=a"(d), "=d"(len)                      \
444      : "0"(s), "1"(d), "2"(len)                         \
445      : "d0")
446
447
448 static int macscsi_pread (struct Scsi_Host *instance,
449                           unsigned char *dst, int len)
450 {
451    unsigned char *d;
452    volatile unsigned char *s;
453
454    NCR5380_local_declare();
455    NCR5380_setup(instance);
456
457    s = mac_scsi_drq+0x60;
458    d = dst;
459
460 /* These conditions are derived from MacOS */
461
462    while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) 
463          && !(NCR5380_read(STATUS_REG) & SR_REQ))
464       ;
465    if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) 
466          && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
467       printk(KERN_ERR "Error in macscsi_pread\n");
468       return -1;
469    }
470
471    CP_IO_TO_MEM(s, d, len);
472    
473    if (len != 0) {
474       printk(KERN_NOTICE "Bus error in macscsi_pread\n");
475       return -1;
476    }
477    
478    return 0;
479 }
480
481
482 #define CP_MEM_TO_IO(s,d,len)                           \
483 __asm__ __volatile__                                    \
484     ("    cmp.w  #4,%2\n"                               \
485      "    bls    8f\n"                                  \
486      "    move.w %0,%%d0\n"                             \
487      "    neg.b  %%d0\n"                                \
488      "    and.w  #3,%%d0\n"                             \
489      "    sub.w  %%d0,%2\n"                             \
490      "    bra    2f\n"                                  \
491      " 1: move.b (%0)+,(%1)\n"                          \
492      " 2: dbf    %%d0,1b\n"                             \
493      "    move.w %2,%%d0\n"                             \
494      "    lsr.w  #5,%%d0\n"                             \
495      "    bra    4f\n"                                  \
496      " 3: move.l (%0)+,(%1)\n"                          \
497      "31: move.l (%0)+,(%1)\n"                          \
498      "32: move.l (%0)+,(%1)\n"                          \
499      "33: move.l (%0)+,(%1)\n"                          \
500      "34: move.l (%0)+,(%1)\n"                          \
501      "35: move.l (%0)+,(%1)\n"                          \
502      "36: move.l (%0)+,(%1)\n"                          \
503      "37: move.l (%0)+,(%1)\n"                          \
504      " 4: dbf    %%d0,3b\n"                             \
505      "    move.w %2,%%d0\n"                             \
506      "    lsr.w  #2,%%d0\n"                             \
507      "    and.w  #7,%%d0\n"                             \
508      "    bra    6f\n"                                  \
509      " 5: move.l (%0)+,(%1)\n"                          \
510      " 6: dbf    %%d0,5b\n"                             \
511      "    and.w  #3,%2\n"                               \
512      "    bra    8f\n"                                  \
513      " 7: move.b (%0)+,(%1)\n"                          \
514      " 8: dbf    %2,7b\n"                               \
515      "    moveq.l #0, %2\n"                             \
516      " 9: \n"                                           \
517      ".section .fixup,\"ax\"\n"                         \
518      "    .even\n"                                      \
519      "90: moveq.l #1, %2\n"                             \
520      "    jra 9b\n"                                     \
521      ".previous\n"                                      \
522      ".section __ex_table,\"a\"\n"                      \
523      "   .align 4\n"                                    \
524      "   .long  1b,90b\n"                               \
525      "   .long  3b,90b\n"                               \
526      "   .long 31b,90b\n"                               \
527      "   .long 32b,90b\n"                               \
528      "   .long 33b,90b\n"                               \
529      "   .long 34b,90b\n"                               \
530      "   .long 35b,90b\n"                               \
531      "   .long 36b,90b\n"                               \
532      "   .long 37b,90b\n"                               \
533      "   .long  5b,90b\n"                               \
534      "   .long  7b,90b\n"                               \
535      ".previous"                                        \
536      : "=a"(s), "=a"(d), "=d"(len)                      \
537      : "0"(s), "1"(d), "2"(len)                         \
538      : "d0")
539
540 static int macscsi_pwrite (struct Scsi_Host *instance,
541                                   unsigned char *src, int len)
542 {
543    unsigned char *s;
544    volatile unsigned char *d;
545
546    NCR5380_local_declare();
547    NCR5380_setup(instance);
548
549    s = src;
550    d = mac_scsi_drq;
551    
552 /* These conditions are derived from MacOS */
553
554    while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) 
555          && (!(NCR5380_read(STATUS_REG) & SR_REQ) 
556             || (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))) 
557       ;
558    if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
559       printk(KERN_ERR "Error in macscsi_pwrite\n");
560       return -1;
561    }
562
563    CP_MEM_TO_IO(s, d, len);   
564
565    if (len != 0) {
566       printk(KERN_NOTICE "Bus error in macscsi_pwrite\n");
567       return -1;
568    }
569    
570    return 0;
571 }
572
573
574 /* These control the behaviour of the generic 5380 core */
575 #define AUTOSENSE
576 #define PSEUDO_DMA
577
578 #include "NCR5380.c"
579
580 static struct scsi_host_template driver_template = {
581         .proc_name                      = "Mac5380",
582         .proc_info                      = macscsi_proc_info,
583         .name                           = "Macintosh NCR5380 SCSI",
584         .detect                         = macscsi_detect,
585         .release                        = macscsi_release,
586         .info                           = macscsi_info,
587         .queuecommand                   = macscsi_queue_command,
588         .eh_abort_handler               = macscsi_abort,
589         .eh_bus_reset_handler           = macscsi_bus_reset,
590         .can_queue                      = CAN_QUEUE,
591         .this_id                        = 7,
592         .sg_tablesize                   = SG_ALL,
593         .cmd_per_lun                    = CMD_PER_LUN,
594         .unchecked_isa_dma              = 0,
595         .use_clustering                 = DISABLE_CLUSTERING
596 };
597
598
599 #include "scsi_module.c"