scsi/NCR5380: reduce depth of sun3_scsi nested includes
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / sun3_scsi_vme.c
1  /*
2  * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
3  *
4  * Sun3 DMA routines added by Sam Creasey (sammy@sammy.net)
5  *
6  * VME support added by Sam Creasey
7  *
8  * Adapted from sun3_scsi.c -- see there for other headers
9  *
10  * TODO: modify this driver to support multiple Sun3 SCSI VME boards
11  *
12  */
13
14 #define AUTOSENSE
15
16 #include <linux/types.h>
17 #include <linux/stddef.h>
18 #include <linux/ctype.h>
19 #include <linux/delay.h>
20
21 #include <linux/module.h>
22 #include <linux/signal.h>
23 #include <linux/ioport.h>
24 #include <linux/init.h>
25 #include <linux/blkdev.h>
26
27 #include <asm/io.h>
28
29 #include <asm/sun3ints.h>
30 #include <asm/dvma.h>
31 #include <asm/idprom.h>
32 #include <asm/machines.h>
33
34 #define SUN3_SCSI_VME
35
36 #undef SUN3_SCSI_DEBUG
37
38 /* dma on! */
39 #define REAL_DMA
40
41 #include "scsi.h"
42 #include "initio.h"
43 #include <scsi/scsi_host.h>
44 #include "sun3_scsi.h"
45 #include "NCR5380.h"
46
47 extern int sun3_map_test(unsigned long, char *);
48
49 #define USE_WRAPPER
50 /*#define RESET_BOOT */
51 #define DRIVER_SETUP
52
53 /*
54  * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
55  */
56 #ifdef BUG
57 #undef RESET_BOOT
58 #undef DRIVER_SETUP
59 #endif
60
61 /* #define SUPPORT_TAGS */
62
63 //#define       ENABLE_IRQ()    enable_irq( SUN3_VEC_VMESCSI0 ); 
64 #define ENABLE_IRQ()
65
66
67 static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
68 static inline unsigned char sun3scsi_read(int reg);
69 static inline void sun3scsi_write(int reg, int value);
70
71 static int setup_can_queue = -1;
72 module_param(setup_can_queue, int, 0);
73 static int setup_cmd_per_lun = -1;
74 module_param(setup_cmd_per_lun, int, 0);
75 static int setup_sg_tablesize = -1;
76 module_param(setup_sg_tablesize, int, 0);
77 #ifdef SUPPORT_TAGS
78 static int setup_use_tagged_queuing = -1;
79 module_param(setup_use_tagged_queuing, int, 0);
80 #endif
81 static int setup_hostid = -1;
82 module_param(setup_hostid, int, 0);
83
84 static struct scsi_cmnd *sun3_dma_setup_done = NULL;
85
86 #define AFTER_RESET_DELAY       (HZ/2)
87
88 /* ms to wait after hitting dma regs */
89 #define SUN3_DMA_DELAY 10
90
91 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
92 #define SUN3_DVMA_BUFSIZE 0xe000
93
94 /* minimum number of bytes to do dma on */
95 #define SUN3_DMA_MINSIZE 128
96
97 static volatile unsigned char *sun3_scsi_regp;
98 static volatile struct sun3_dma_regs *dregs;
99 #ifdef OLDDMA
100 static unsigned char *dmabuf = NULL; /* dma memory buffer */
101 #endif
102 static unsigned char *sun3_dma_orig_addr = NULL;
103 static unsigned long sun3_dma_orig_count = 0;
104 static int sun3_dma_active = 0;
105 static unsigned long last_residual = 0;
106
107 /*
108  * NCR 5380 register access functions
109  */
110
111 static inline unsigned char sun3scsi_read(int reg)
112 {
113         return( sun3_scsi_regp[reg] );
114 }
115
116 static inline void sun3scsi_write(int reg, int value)
117 {
118         sun3_scsi_regp[reg] = value;
119 }
120
121 /*
122  * XXX: status debug
123  */
124 static struct Scsi_Host *default_instance;
125
126 /*
127  * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
128  *
129  * Purpose : initializes mac NCR5380 driver based on the
130  *      command line / compile time port and irq definitions.
131  *
132  * Inputs : tpnt - template for this SCSI adapter.
133  *
134  * Returns : 1 if a host adapter was found, 0 if not.
135  *
136  */
137  
138 static int __init sun3scsi_detect(struct scsi_host_template * tpnt)
139 {
140         unsigned long ioaddr, irq = 0;
141         static int called = 0;
142         struct Scsi_Host *instance;
143         int i;
144         unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI, 
145                                    IOBASE_SUN3_VMESCSI + 0x4000,
146                                    0 };
147         unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
148                                   SUN3_VEC_VMESCSI1,
149                                   0 };
150         /* check that this machine has an onboard 5380 */
151         switch(idprom->id_machtype) {
152         case SM_SUN3|SM_3_160:
153         case SM_SUN3|SM_3_260:
154                 break;
155
156         default:
157                 return 0;
158         }
159
160         if(called)
161                 return 0;
162
163         tpnt->proc_name = "Sun3 5380 VME SCSI";
164
165         /* setup variables */
166         tpnt->can_queue =
167                 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
168         tpnt->cmd_per_lun =
169                 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
170         tpnt->sg_tablesize = 
171                 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
172         
173         if (setup_hostid >= 0)
174                 tpnt->this_id = setup_hostid;
175         else {
176                 /* use 7 as default */
177                 tpnt->this_id = 7;
178         }
179         
180         ioaddr = 0;
181         for(i = 0; addrs[i] != 0; i++) {
182                 unsigned char x;
183                 
184                 ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
185                                                      SUN3_PAGE_TYPE_VME16);
186                 irq = vecs[i];
187                 sun3_scsi_regp = (unsigned char *)ioaddr;
188                 
189                 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
190                 
191                 if(sun3_map_test((unsigned long)dregs, &x)) {
192                         unsigned short oldcsr;
193
194                         oldcsr = dregs->csr;
195                         dregs->csr = 0;
196                         udelay(SUN3_DMA_DELAY);
197                         if(dregs->csr == 0x1400)
198                                 break;
199                         
200                         dregs->csr = oldcsr;
201                 }
202
203                 iounmap((void *)ioaddr);
204                 ioaddr = 0;
205         }
206
207         if(!ioaddr)
208                 return 0;
209         
210 #ifdef SUPPORT_TAGS
211         if (setup_use_tagged_queuing < 0)
212                 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
213 #endif
214
215         instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
216         if(instance == NULL)
217                 return 0;
218                 
219         default_instance = instance;
220
221         instance->io_port = (unsigned long) ioaddr;
222         instance->irq = irq;
223
224         NCR5380_init(instance, 0);
225
226         instance->n_io_port = 32;
227
228         ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
229
230         if (request_irq(instance->irq, scsi_sun3_intr,
231                         0, "Sun3SCSI-5380VME", instance)) {
232 #ifndef REAL_DMA
233                 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
234                        instance->host_no, instance->irq);
235                 instance->irq = SCSI_IRQ_NONE;
236 #else
237                 printk("scsi%d: IRQ%d not free, bailing out\n",
238                        instance->host_no, instance->irq);
239                 return 0;
240 #endif
241         }
242
243         printk("scsi%d: Sun3 5380 VME at port %lX irq", instance->host_no, instance->io_port);
244         if (instance->irq == SCSI_IRQ_NONE)
245                 printk ("s disabled");
246         else
247                 printk (" %d", instance->irq);
248         printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
249                instance->can_queue, instance->cmd_per_lun,
250                SUN3SCSI_PUBLIC_RELEASE);
251         printk("\nscsi%d:", instance->host_no);
252         NCR5380_print_options(instance);
253         printk("\n");
254
255         dregs->csr = 0;
256         udelay(SUN3_DMA_DELAY);
257         dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
258         udelay(SUN3_DMA_DELAY);
259         dregs->fifo_count = 0;
260         dregs->fifo_count_hi = 0;
261         dregs->dma_addr_hi = 0;
262         dregs->dma_addr_lo = 0;
263         dregs->dma_count_hi = 0;
264         dregs->dma_count_lo = 0;
265
266         dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
267
268         called = 1;
269
270 #ifdef RESET_BOOT
271         sun3_scsi_reset_boot(instance);
272 #endif
273
274         return 1;
275 }
276
277 int sun3scsi_release (struct Scsi_Host *shpnt)
278 {
279         if (shpnt->irq != SCSI_IRQ_NONE)
280                 free_irq(shpnt->irq, shpnt);
281
282         iounmap((void *)sun3_scsi_regp);
283
284         NCR5380_exit(shpnt);
285         return 0;
286 }
287
288 #ifdef RESET_BOOT
289 /*
290  * Our 'bus reset on boot' function
291  */
292
293 static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
294 {
295         unsigned long end;
296
297         NCR5380_local_declare();
298         NCR5380_setup(instance);
299         
300         /*
301          * Do a SCSI reset to clean up the bus during initialization. No
302          * messing with the queues, interrupts, or locks necessary here.
303          */
304
305         printk( "Sun3 SCSI: resetting the SCSI bus..." );
306
307         /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
308 //              sun3_disable_irq( IRQ_SUN3_SCSI );
309
310         /* get in phase */
311         NCR5380_write( TARGET_COMMAND_REG,
312                       PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
313
314         /* assert RST */
315         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
316
317         /* The min. reset hold time is 25us, so 40us should be enough */
318         udelay( 50 );
319
320         /* reset RST and interrupt */
321         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
322         NCR5380_read( RESET_PARITY_INTERRUPT_REG );
323
324         for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
325                 barrier();
326
327         /* switch on SCSI IRQ again */
328 //              sun3_enable_irq( IRQ_SUN3_SCSI );
329
330         printk( " done\n" );
331 }
332 #endif
333
334 static const char * sun3scsi_info (struct Scsi_Host *spnt) {
335     return "";
336 }
337
338 // safe bits for the CSR
339 #define CSR_GOOD 0x060f
340
341 static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
342 {
343         unsigned short csr = dregs->csr;
344         int handled = 0;
345
346         dregs->csr &= ~CSR_DMA_ENABLE;
347
348
349 #ifdef SUN3_SCSI_DEBUG
350         printk("scsi_intr csr %x\n", csr);
351 #endif
352
353         if(csr & ~CSR_GOOD) {
354                 if(csr & CSR_DMA_BUSERR) {
355                         printk("scsi%d: bus error in dma\n", default_instance->host_no);
356 #ifdef SUN3_SCSI_DEBUG
357                         printk("scsi: residual %x count %x addr %p dmaaddr %x\n", 
358                                dregs->fifo_count,
359                                dregs->dma_count_lo | (dregs->dma_count_hi << 16),
360                                sun3_dma_orig_addr,
361                                dregs->dma_addr_lo | (dregs->dma_addr_hi << 16));
362 #endif
363                 }
364
365                 if(csr & CSR_DMA_CONFLICT) {
366                         printk("scsi%d: dma conflict\n", default_instance->host_no);
367                 }
368                 handled = 1;
369         }
370
371         if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
372                 NCR5380_intr(irq, dummy);
373                 handled = 1;
374         }
375
376         return IRQ_RETVAL(handled);
377 }
378
379 /*
380  * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk; 
381  * reentering NCR5380_print_status seems to have ugly side effects
382  */
383
384 /* this doesn't seem to get used at all -- sam */
385 #if 0
386 void sun3_sun3_debug (void)
387 {
388         unsigned long flags;
389         NCR5380_local_declare();
390
391         if (default_instance) {
392                         local_irq_save(flags);
393                         NCR5380_print_status(default_instance);
394                         local_irq_restore(flags);
395         }
396 }
397 #endif
398
399
400 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
401 static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
402 {
403         void *addr;
404
405         if(sun3_dma_orig_addr != NULL)
406                 dvma_unmap(sun3_dma_orig_addr);
407
408 //      addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
409         addr = (void *)dvma_map_vme((unsigned long) data, count);
410                 
411         sun3_dma_orig_addr = addr;
412         sun3_dma_orig_count = count;
413         
414 #ifdef SUN3_SCSI_DEBUG
415         printk("scsi: dma_setup addr %p count %x\n", addr, count);
416 #endif
417
418 //      dregs->fifo_count = 0;
419 #if 0   
420         /* reset fifo */
421         dregs->csr &= ~CSR_FIFO;
422         dregs->csr |= CSR_FIFO;
423 #endif  
424         /* set direction */
425         if(write_flag)
426                 dregs->csr |= CSR_SEND;
427         else
428                 dregs->csr &= ~CSR_SEND;
429         
430         /* reset fifo */
431 //      dregs->csr &= ~CSR_FIFO;
432 //      dregs->csr |= CSR_FIFO;
433
434         dregs->csr |= CSR_PACK_ENABLE;
435
436         dregs->dma_addr_hi = ((unsigned long)addr >> 16);
437         dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
438         
439         dregs->dma_count_hi = 0;
440         dregs->dma_count_lo = 0;
441         dregs->fifo_count_hi = 0;
442         dregs->fifo_count = 0;
443                 
444 #ifdef SUN3_SCSI_DEBUG
445         printk("scsi: dma_setup done csr %x\n", dregs->csr);
446 #endif
447         return count;
448
449 }
450
451 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
452 {
453         return last_residual;
454 }
455
456 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
457                                                   struct scsi_cmnd *cmd,
458                                                   int write_flag)
459 {
460         if (cmd->request->cmd_type == REQ_TYPE_FS)
461                 return wanted;
462         else
463                 return 0;
464 }
465
466 static int sun3scsi_dma_start(unsigned long count, char *data)
467 {
468         
469         unsigned short csr;
470
471         csr = dregs->csr;
472 #ifdef SUN3_SCSI_DEBUG
473         printk("scsi: dma_start data %p count %x csr %x fifo %x\n", data, count, csr, dregs->fifo_count);
474 #endif
475         
476         dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
477         dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
478
479         dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
480         dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
481
482 //      if(!(csr & CSR_DMA_ENABLE))
483 //              dregs->csr |= CSR_DMA_ENABLE;
484
485         return 0;
486 }
487
488 /* clean up after our dma is done */
489 static int sun3scsi_dma_finish(int write_flag)
490 {
491         unsigned short fifo;
492         int ret = 0;
493         
494         sun3_dma_active = 0;
495
496         dregs->csr &= ~CSR_DMA_ENABLE;
497         
498         fifo = dregs->fifo_count;
499         if(write_flag) {
500                 if((fifo > 0) && (fifo < sun3_dma_orig_count))
501                         fifo++;
502         }
503
504         last_residual = fifo;
505 #ifdef SUN3_SCSI_DEBUG
506         printk("scsi: residual %x total %x\n", fifo, sun3_dma_orig_count);
507 #endif
508         /* empty bytes from the fifo which didn't make it */
509         if((!write_flag) && (dregs->csr & CSR_LEFT)) {
510                 unsigned char *vaddr;
511
512 #ifdef SUN3_SCSI_DEBUG
513                 printk("scsi: got left over bytes\n");
514 #endif
515
516                 vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
517                 
518                 vaddr += (sun3_dma_orig_count - fifo);
519                 vaddr--;
520                 
521                 switch(dregs->csr & CSR_LEFT) {
522                 case CSR_LEFT_3:
523                         *vaddr = (dregs->bpack_lo & 0xff00) >> 8;
524                         vaddr--;
525                         
526                 case CSR_LEFT_2:
527                         *vaddr = (dregs->bpack_hi & 0x00ff);
528                         vaddr--;
529                         
530                 case CSR_LEFT_1:
531                         *vaddr = (dregs->bpack_hi & 0xff00) >> 8;
532                         break;
533                 }
534                 
535                 
536         }
537
538         dvma_unmap(sun3_dma_orig_addr);
539         sun3_dma_orig_addr = NULL;
540
541         dregs->dma_addr_hi = 0;
542         dregs->dma_addr_lo = 0;
543         dregs->dma_count_hi = 0;
544         dregs->dma_count_lo = 0;
545
546         dregs->fifo_count = 0;
547         dregs->fifo_count_hi = 0;
548
549         dregs->csr &= ~CSR_SEND;
550         
551 //      dregs->csr |= CSR_DMA_ENABLE;
552         
553 #if 0
554         /* reset fifo */
555         dregs->csr &= ~CSR_FIFO;
556         dregs->csr |= CSR_FIFO;
557 #endif  
558         sun3_dma_setup_done = NULL;
559
560         return ret;
561
562 }
563
564 #include "sun3_NCR5380.c"
565
566 static struct scsi_host_template driver_template = {
567         .name                   = SUN3_SCSI_NAME,
568         .detect                 = sun3scsi_detect,
569         .release                = sun3scsi_release,
570         .info                   = sun3scsi_info,
571         .queuecommand           = sun3scsi_queue_command,
572         .eh_abort_handler       = sun3scsi_abort,
573         .eh_bus_reset_handler   = sun3scsi_bus_reset,
574         .can_queue              = CAN_QUEUE,
575         .this_id                = 7,
576         .sg_tablesize           = SG_TABLESIZE,
577         .cmd_per_lun            = CMD_PER_LUN,
578         .use_clustering         = DISABLE_CLUSTERING
579 };
580
581
582 #include "scsi_module.c"
583
584 MODULE_LICENSE("GPL");