blob: c10466939a5228da1774a7068469ad5ad7651dc1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic Generic NCR5380 driver
3 *
4 * Copyright 1993, Drew Eckhardt
5 * Visionary Computing
6 * (Unix and Linux consulting and custom programming)
7 * drew@colorado.edu
8 * +1 (303) 440-4894
9 *
10 * NCR53C400 extensions (c) 1994,1995,1996, Kevin Lentin
11 * K.Lentin@cs.monash.edu.au
12 *
13 * NCR53C400A extensions (c) 1996, Ingmar Baumgart
14 * ingmar@gonzo.schwaben.de
15 *
16 * DTC3181E extensions (c) 1997, Ronald van Cuijlenborg
17 * ronald.van.cuijlenborg@tip.nl or nutty@dds.nl
18 *
19 * Added ISAPNP support for DTC436 adapters,
20 * Thomas Sailer, sailer@ife.ee.ethz.ch
21 *
22 * ALPHA RELEASE 1.
23 *
24 * For more information, please consult
25 *
26 * NCR 5380 Family
27 * SCSI Protocol Controller
28 * Databook
29 *
30 * NCR Microelectronics
31 * 1635 Aeroplaza Drive
32 * Colorado Springs, CO 80916
33 * 1+ (719) 578-3400
34 * 1+ (800) 334-5454
35 */
36
37/*
38 * TODO : flesh out DMA support, find some one actually using this (I have
39 * a memory mapped Trantor board that works fine)
40 */
41
42/*
43 * Options :
44 *
45 * PARITY - enable parity checking. Not supported.
46 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * The card is detected and initialized in one of several ways :
48 * 1. With command line overrides - NCR5380=port,irq may be
49 * used on the LILO command line to override the defaults.
50 *
51 * 2. With the GENERIC_NCR5380_OVERRIDE compile time define. This is
52 * specified as an array of address, irq, dma, board tuples. Ie, for
53 * one board at 0x350, IRQ5, no dma, I could say
54 * -DGENERIC_NCR5380_OVERRIDE={{0xcc000, 5, DMA_NONE, BOARD_NCR5380}}
55 *
56 * -1 should be specified for no or DMA interrupt, -2 to autoprobe for an
57 * IRQ line if overridden on the command line.
58 *
59 * 3. When included as a module, with arguments passed on the command line:
60 * ncr_irq=xx the interrupt
61 * ncr_addr=xx the port or base address (for port or memory
62 * mapped, resp.)
63 * ncr_dma=xx the DMA
64 * ncr_5380=1 to set up for a NCR5380 board
65 * ncr_53c400=1 to set up for a NCR53C400 board
66 * e.g.
67 * modprobe g_NCR5380 ncr_irq=5 ncr_addr=0x350 ncr_5380=1
68 * for a port mapped NCR5380 board or
69 * modprobe g_NCR5380 ncr_irq=255 ncr_addr=0xc8000 ncr_53c400=1
70 * for a memory mapped NCR53C400 board with interrupts disabled.
71 *
72 * 255 should be specified for no or DMA interrupt, 254 to autoprobe for an
73 * IRQ line if overridden on the command line.
74 *
75 */
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* settings for DTC3181E card with only Mustek scanner attached */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define USLEEP_POLL 1
79#define USLEEP_SLEEP 20
80#define USLEEP_WAITLONG 500
81
82#define AUTOPROBE_IRQ
83#define AUTOSENSE
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#ifdef CONFIG_SCSI_GENERIC_NCR53C400
87#define NCR53C400_PSEUDO_DMA 1
88#define PSEUDO_DMA
89#define NCR53C400
90#define NCR5380_STATS
91#undef NCR5380_STAT_LIMIT
92#endif
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include <asm/io.h>
95#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/blkdev.h>
97#include "scsi.h"
98#include <scsi/scsi_host.h>
99#include "g_NCR5380.h"
100#include "NCR5380.h"
101#include <linux/stat.h>
102#include <linux/init.h>
103#include <linux/ioport.h>
104#include <linux/isapnp.h>
105#include <linux/delay.h>
106#include <linux/interrupt.h>
107
108#define NCR_NOT_SET 0
109static int ncr_irq = NCR_NOT_SET;
110static int ncr_dma = NCR_NOT_SET;
111static int ncr_addr = NCR_NOT_SET;
112static int ncr_5380 = NCR_NOT_SET;
113static int ncr_53c400 = NCR_NOT_SET;
114static int ncr_53c400a = NCR_NOT_SET;
115static int dtc_3181e = NCR_NOT_SET;
116
117static struct override {
Al Viroc818cb62006-03-24 03:15:37 -0800118 NCR5380_map_type NCR5380_map_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 int irq;
120 int dma;
121 int board; /* Use NCR53c400, Ricoh, etc. extensions ? */
122} overrides
123#ifdef GENERIC_NCR5380_OVERRIDE
124[] __initdata = GENERIC_NCR5380_OVERRIDE;
125#else
126[1] __initdata = { { 0,},};
127#endif
128
Tobias Klauser6391a112006-06-08 22:23:48 -0700129#define NO_OVERRIDES ARRAY_SIZE(overrides)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Tobias Klauser6391a112006-06-08 22:23:48 -0700131#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/**
134 * internal_setup - handle lilo command string override
135 * @board: BOARD_* identifier for the board
136 * @str: unused
137 * @ints: numeric parameters
138 *
139 * Do LILO command line initialization of the overrides array. Display
140 * errors when needed
141 *
142 * Locks: none
143 */
144
145static void __init internal_setup(int board, char *str, int *ints)
146{
147 static int commandline_current = 0;
148 switch (board) {
149 case BOARD_NCR5380:
150 if (ints[0] != 2 && ints[0] != 3) {
151 printk(KERN_ERR "generic_NCR5380_setup : usage ncr5380=" STRVAL(NCR5380_map_name) ",irq,dma\n");
152 return;
153 }
154 break;
155 case BOARD_NCR53C400:
156 if (ints[0] != 2) {
157 printk(KERN_ERR "generic_NCR53C400_setup : usage ncr53c400=" STRVAL(NCR5380_map_name) ",irq\n");
158 return;
159 }
160 break;
161 case BOARD_NCR53C400A:
162 if (ints[0] != 2) {
163 printk(KERN_ERR "generic_NCR53C400A_setup : usage ncr53c400a=" STRVAL(NCR5380_map_name) ",irq\n");
164 return;
165 }
166 break;
167 case BOARD_DTC3181E:
168 if (ints[0] != 2) {
169 printk("generic_DTC3181E_setup : usage dtc3181e=" STRVAL(NCR5380_map_name) ",irq\n");
170 return;
171 }
172 break;
173 }
174
175 if (commandline_current < NO_OVERRIDES) {
176 overrides[commandline_current].NCR5380_map_name = (NCR5380_map_type) ints[1];
177 overrides[commandline_current].irq = ints[2];
178 if (ints[0] == 3)
179 overrides[commandline_current].dma = ints[3];
180 else
181 overrides[commandline_current].dma = DMA_NONE;
182 overrides[commandline_current].board = board;
183 ++commandline_current;
184 }
185}
186
187
188/**
189 * do_NCR53C80_setup - set up entry point
190 * @str: unused
191 *
192 * Setup function invoked at boot to parse the ncr5380= command
193 * line.
194 */
195
196static int __init do_NCR5380_setup(char *str)
197{
198 int ints[10];
199
Tobias Klauser6391a112006-06-08 22:23:48 -0700200 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 internal_setup(BOARD_NCR5380, str, ints);
202 return 1;
203}
204
205/**
206 * do_NCR53C400_setup - set up entry point
207 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700208 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *
210 * Setup function invoked at boot to parse the ncr53c400= command
211 * line.
212 */
213
214static int __init do_NCR53C400_setup(char *str)
215{
216 int ints[10];
217
Tobias Klauser6391a112006-06-08 22:23:48 -0700218 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 internal_setup(BOARD_NCR53C400, str, ints);
220 return 1;
221}
222
223/**
224 * do_NCR53C400A_setup - set up entry point
225 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700226 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
228 * Setup function invoked at boot to parse the ncr53c400a= command
229 * line.
230 */
231
232static int __init do_NCR53C400A_setup(char *str)
233{
234 int ints[10];
235
Tobias Klauser6391a112006-06-08 22:23:48 -0700236 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 internal_setup(BOARD_NCR53C400A, str, ints);
238 return 1;
239}
240
241/**
242 * do_DTC3181E_setup - set up entry point
243 * @str: unused
Tobias Klauser6391a112006-06-08 22:23:48 -0700244 * @ints: integer parameters from kernel setup code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
246 * Setup function invoked at boot to parse the dtc3181e= command
247 * line.
248 */
249
250static int __init do_DTC3181E_setup(char *str)
251{
252 int ints[10];
253
Tobias Klauser6391a112006-06-08 22:23:48 -0700254 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 internal_setup(BOARD_DTC3181E, str, ints);
256 return 1;
257}
258
259#endif
260
261/**
262 * generic_NCR5380_detect - look for NCR5380 controllers
263 * @tpnt: the scsi template
264 *
265 * Scan for the present of NCR5380, NCR53C400, NCR53C400A, DTC3181E
266 * and DTC436(ISAPnP) controllers. If overrides have been set we use
267 * them.
268 *
269 * The caller supplied NCR5380_init function is invoked from here, before
270 * the interrupt line is taken.
271 *
272 * Locks: none
273 */
274
Finn Thained8b9e72014-11-12 16:11:51 +1100275static int __init generic_NCR5380_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 static int current_override = 0;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700278 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 unsigned int *ports;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700280#ifndef SCSI_G_NCR5380_MEM
281 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 unsigned long region_size = 16;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700283#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 static unsigned int __initdata ncr_53c400a_ports[] = {
285 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
286 };
287 static unsigned int __initdata dtc_3181e_ports[] = {
288 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
289 };
290 int flags = 0;
291 struct Scsi_Host *instance;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700292#ifdef SCSI_G_NCR5380_MEM
Al Viroc818cb62006-03-24 03:15:37 -0800293 unsigned long base;
294 void __iomem *iomem;
295#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (ncr_irq != NCR_NOT_SET)
298 overrides[0].irq = ncr_irq;
299 if (ncr_dma != NCR_NOT_SET)
300 overrides[0].dma = ncr_dma;
301 if (ncr_addr != NCR_NOT_SET)
302 overrides[0].NCR5380_map_name = (NCR5380_map_type) ncr_addr;
303 if (ncr_5380 != NCR_NOT_SET)
304 overrides[0].board = BOARD_NCR5380;
305 else if (ncr_53c400 != NCR_NOT_SET)
306 overrides[0].board = BOARD_NCR53C400;
307 else if (ncr_53c400a != NCR_NOT_SET)
308 overrides[0].board = BOARD_NCR53C400A;
309 else if (dtc_3181e != NCR_NOT_SET)
310 overrides[0].board = BOARD_DTC3181E;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700311#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!current_override && isapnp_present()) {
313 struct pnp_dev *dev = NULL;
314 count = 0;
315 while ((dev = pnp_find_dev(NULL, ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e), dev))) {
316 if (count >= NO_OVERRIDES)
317 break;
Ondrej Zaryc94babb2010-08-10 18:01:15 -0700318 if (pnp_device_attach(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (pnp_activate_dev(dev) < 0) {
321 printk(KERN_ERR "dtc436e probe: activate failed\n");
322 pnp_device_detach(dev);
323 continue;
324 }
325 if (!pnp_port_valid(dev, 0)) {
326 printk(KERN_ERR "dtc436e probe: no valid port\n");
327 pnp_device_detach(dev);
328 continue;
329 }
330 if (pnp_irq_valid(dev, 0))
331 overrides[count].irq = pnp_irq(dev, 0);
332 else
333 overrides[count].irq = SCSI_IRQ_NONE;
334 if (pnp_dma_valid(dev, 0))
335 overrides[count].dma = pnp_dma(dev, 0);
336 else
337 overrides[count].dma = DMA_NONE;
338 overrides[count].NCR5380_map_name = (NCR5380_map_type) pnp_port_start(dev, 0);
339 overrides[count].board = BOARD_DTC3181E;
340 count++;
341 }
342 }
Ondrej Zary702a98c2010-08-10 18:01:16 -0700343#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 tpnt->proc_name = "g_NCR5380";
345
346 for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
347 if (!(overrides[current_override].NCR5380_map_name))
348 continue;
349
350 ports = NULL;
351 switch (overrides[current_override].board) {
352 case BOARD_NCR5380:
353 flags = FLAG_NO_PSEUDO_DMA;
354 break;
355 case BOARD_NCR53C400:
356 flags = FLAG_NCR53C400;
357 break;
358 case BOARD_NCR53C400A:
359 flags = FLAG_NO_PSEUDO_DMA;
360 ports = ncr_53c400a_ports;
361 break;
362 case BOARD_DTC3181E:
363 flags = FLAG_NO_PSEUDO_DMA | FLAG_DTC3181E;
364 ports = dtc_3181e_ports;
365 break;
366 }
367
Ondrej Zary702a98c2010-08-10 18:01:16 -0700368#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (ports) {
370 /* wakeup sequence for the NCR53C400A and DTC3181E */
371
372 /* Disable the adapter and look for a free io port */
373 outb(0x59, 0x779);
374 outb(0xb9, 0x379);
375 outb(0xc5, 0x379);
376 outb(0xae, 0x379);
377 outb(0xa6, 0x379);
378 outb(0x00, 0x379);
379
380 if (overrides[current_override].NCR5380_map_name != PORT_AUTO)
381 for (i = 0; ports[i]; i++) {
382 if (!request_region(ports[i], 16, "ncr53c80"))
383 continue;
384 if (overrides[current_override].NCR5380_map_name == ports[i])
385 break;
386 release_region(ports[i], 16);
387 } else
388 for (i = 0; ports[i]; i++) {
389 if (!request_region(ports[i], 16, "ncr53c80"))
390 continue;
391 if (inb(ports[i]) == 0xff)
392 break;
393 release_region(ports[i], 16);
394 }
395 if (ports[i]) {
396 /* At this point we have our region reserved */
397 outb(0x59, 0x779);
398 outb(0xb9, 0x379);
399 outb(0xc5, 0x379);
400 outb(0xae, 0x379);
401 outb(0xa6, 0x379);
402 outb(0x80 | i, 0x379); /* set io port to be used */
403 outb(0xc0, ports[i] + 9);
404 if (inb(ports[i] + 9) != 0x80)
405 continue;
406 else
407 overrides[current_override].NCR5380_map_name = ports[i];
408 } else
409 continue;
410 }
411 else
412 {
413 /* Not a 53C400A style setup - just grab */
414 if(!(request_region(overrides[current_override].NCR5380_map_name, NCR5380_region_size, "ncr5380")))
415 continue;
416 region_size = NCR5380_region_size;
417 }
418#else
Al Viroc818cb62006-03-24 03:15:37 -0800419 base = overrides[current_override].NCR5380_map_name;
420 if (!request_mem_region(base, NCR5380_region_size, "ncr5380"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 continue;
Al Viroc818cb62006-03-24 03:15:37 -0800422 iomem = ioremap(base, NCR5380_region_size);
423 if (!iomem) {
424 release_mem_region(base, NCR5380_region_size);
425 continue;
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427#endif
428 instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
429 if (instance == NULL) {
Ondrej Zary702a98c2010-08-10 18:01:16 -0700430#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 release_region(overrides[current_override].NCR5380_map_name, region_size);
432#else
Al Viroc818cb62006-03-24 03:15:37 -0800433 iounmap(iomem);
434 release_mem_region(base, NCR5380_region_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435#endif
436 continue;
437 }
438
439 instance->NCR5380_instance_name = overrides[current_override].NCR5380_map_name;
Ondrej Zary702a98c2010-08-10 18:01:16 -0700440#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 instance->n_io_port = region_size;
Al Viroc818cb62006-03-24 03:15:37 -0800442#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700443 ((struct NCR5380_hostdata *)instance->hostdata)->iomem = iomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#endif
445
446 NCR5380_init(instance, flags);
447
448 if (overrides[current_override].irq != IRQ_AUTO)
449 instance->irq = overrides[current_override].irq;
450 else
451 instance->irq = NCR5380_probe_irq(instance, 0xffff);
452
453 if (instance->irq != SCSI_IRQ_NONE)
Jeff Garzik1e641662007-11-11 19:52:05 -0500454 if (request_irq(instance->irq, generic_NCR5380_intr,
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100455 0, "NCR5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 printk(KERN_WARNING "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
457 instance->irq = SCSI_IRQ_NONE;
458 }
459
460 if (instance->irq == SCSI_IRQ_NONE) {
461 printk(KERN_INFO "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
462 printk(KERN_INFO "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
463 }
464
465 printk(KERN_INFO "scsi%d : at " STRVAL(NCR5380_map_name) " 0x%x", instance->host_no, (unsigned int) instance->NCR5380_instance_name);
466 if (instance->irq == SCSI_IRQ_NONE)
467 printk(" interrupts disabled");
468 else
469 printk(" irq %d", instance->irq);
470 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d", CAN_QUEUE, CMD_PER_LUN, GENERIC_NCR5380_PUBLIC_RELEASE);
471 NCR5380_print_options(instance);
472 printk("\n");
473
474 ++current_override;
475 ++count;
476 }
477 return count;
478}
479
480/**
481 * generic_NCR5380_info - reporting string
482 * @host: NCR5380 to report on
483 *
484 * Report driver information for the NCR5380
485 */
486
Finn Thained8b9e72014-11-12 16:11:51 +1100487static const char *generic_NCR5380_info(struct Scsi_Host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 static const char string[] = "Generic NCR5380/53C400 Driver";
490 return string;
491}
492
493/**
494 * generic_NCR5380_release_resources - free resources
495 * @instance: host adapter to clean up
496 *
497 * Free the generic interface resources from this adapter.
498 *
499 * Locks: none
500 */
501
Finn Thained8b9e72014-11-12 16:11:51 +1100502static int generic_NCR5380_release_resources(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 NCR5380_local_declare();
505 NCR5380_setup(instance);
506
507 if (instance->irq != SCSI_IRQ_NONE)
Jeff Garzik1e641662007-11-11 19:52:05 -0500508 free_irq(instance->irq, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 NCR5380_exit(instance);
510
Ondrej Zary702a98c2010-08-10 18:01:16 -0700511#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 release_region(instance->NCR5380_instance_name, instance->n_io_port);
513#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700514 iounmap(((struct NCR5380_hostdata *)instance->hostdata)->iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 release_mem_region(instance->NCR5380_instance_name, NCR5380_region_size);
516#endif
517
518
519 return 0;
520}
521
522#ifdef BIOSPARAM
523/**
524 * generic_NCR5380_biosparam
525 * @disk: disk to compute geometry for
526 * @dev: device identifier for this disk
527 * @ip: sizes to fill in
528 *
529 * Generates a BIOS / DOS compatible H-C-S mapping for the specified
530 * device / size.
531 *
532 * XXX Most SCSI boards use this mapping, I could be incorrect. Someone
533 * using hard disks on a trantor should verify that this mapping
534 * corresponds to that used by the BIOS / ASPI driver by running the linux
535 * fdisk program and matching the H_C_S coordinates to what DOS uses.
536 *
537 * Locks: none
538 */
539
540static int
541generic_NCR5380_biosparam(struct scsi_device *sdev, struct block_device *bdev,
542 sector_t capacity, int *ip)
543{
544 ip[0] = 64;
545 ip[1] = 32;
546 ip[2] = capacity >> 11;
547 return 0;
548}
549#endif
550
Gabriel C8d9e0f42007-08-10 14:50:39 -0700551#ifdef NCR53C400_PSEUDO_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553/**
554 * NCR5380_pread - pseudo DMA read
555 * @instance: adapter to read from
556 * @dst: buffer to read into
557 * @len: buffer length
558 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300559 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * controller
561 */
562
563static inline int NCR5380_pread(struct Scsi_Host *instance, unsigned char *dst, int len)
564{
565 int blocks = len / 128;
566 int start = 0;
567 int bl;
568
569 NCR5380_local_declare();
570 NCR5380_setup(instance);
571
572 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE | CSR_TRANS_DIR);
573 NCR5380_write(C400_BLOCK_COUNTER_REG, blocks);
574 while (1) {
575 if ((bl = NCR5380_read(C400_BLOCK_COUNTER_REG)) == 0) {
576 break;
577 }
578 if (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ) {
579 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
580 return -1;
581 }
582 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY);
583
Ondrej Zary702a98c2010-08-10 18:01:16 -0700584#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 {
586 int i;
587 for (i = 0; i < 128; i++)
588 dst[start + i] = NCR5380_read(C400_HOST_BUFFER);
589 }
590#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700591 /* implies SCSI_G_NCR5380_MEM */
Al Viroc818cb62006-03-24 03:15:37 -0800592 memcpy_fromio(dst + start, iomem + NCR53C400_host_buffer, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593#endif
594 start += 128;
595 blocks--;
596 }
597
598 if (blocks) {
599 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY)
600 {
601 // FIXME - no timeout
602 }
603
Ondrej Zary702a98c2010-08-10 18:01:16 -0700604#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 {
606 int i;
607 for (i = 0; i < 128; i++)
608 dst[start + i] = NCR5380_read(C400_HOST_BUFFER);
609 }
610#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700611 /* implies SCSI_G_NCR5380_MEM */
Al Viroc818cb62006-03-24 03:15:37 -0800612 memcpy_fromio(dst + start, iomem + NCR53C400_host_buffer, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#endif
614 start += 128;
615 blocks--;
616 }
617
618 if (!(NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ))
619 printk("53C400r: no 53C80 gated irq after transfer");
620
621#if 0
622 /*
623 * DON'T DO THIS - THEY NEVER ARRIVE!
624 */
625 printk("53C400r: Waiting for 53C80 registers\n");
626 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_53C80_REG)
627 ;
628#endif
629 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
630 printk(KERN_ERR "53C400r: no end dma signal\n");
631
632 NCR5380_write(MODE_REG, MR_BASE);
633 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
634 return 0;
635}
636
637/**
638 * NCR5380_write - pseudo DMA write
639 * @instance: adapter to read from
640 * @dst: buffer to read into
641 * @len: buffer length
642 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300643 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * controller
645 */
646
647static inline int NCR5380_pwrite(struct Scsi_Host *instance, unsigned char *src, int len)
648{
649 int blocks = len / 128;
650 int start = 0;
651 int bl;
652 int i;
653
654 NCR5380_local_declare();
655 NCR5380_setup(instance);
656
657 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
658 NCR5380_write(C400_BLOCK_COUNTER_REG, blocks);
659 while (1) {
660 if (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ) {
661 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
662 return -1;
663 }
664
665 if ((bl = NCR5380_read(C400_BLOCK_COUNTER_REG)) == 0) {
666 break;
667 }
668 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY)
669 ; // FIXME - timeout
Ondrej Zary702a98c2010-08-10 18:01:16 -0700670#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 {
672 for (i = 0; i < 128; i++)
673 NCR5380_write(C400_HOST_BUFFER, src[start + i]);
674 }
675#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700676 /* implies SCSI_G_NCR5380_MEM */
Al Viroc818cb62006-03-24 03:15:37 -0800677 memcpy_toio(iomem + NCR53C400_host_buffer, src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678#endif
679 start += 128;
680 blocks--;
681 }
682 if (blocks) {
683 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY)
684 ; // FIXME - no timeout
685
Ondrej Zary702a98c2010-08-10 18:01:16 -0700686#ifndef SCSI_G_NCR5380_MEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 {
688 for (i = 0; i < 128; i++)
689 NCR5380_write(C400_HOST_BUFFER, src[start + i]);
690 }
691#else
Ondrej Zary702a98c2010-08-10 18:01:16 -0700692 /* implies SCSI_G_NCR5380_MEM */
Al Viroc818cb62006-03-24 03:15:37 -0800693 memcpy_toio(iomem + NCR53C400_host_buffer, src + start, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#endif
695 start += 128;
696 blocks--;
697 }
698
699#if 0
700 printk("53C400w: waiting for registers to be available\n");
701 THEY NEVER DO ! while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_53C80_REG);
702 printk("53C400w: Got em\n");
703#endif
704
705 /* Let's wait for this instead - could be ugly */
706 /* All documentation says to check for this. Maybe my hardware is too
707 * fast. Waiting for it seems to work fine! KLL
708 */
709 while (!(i = NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ))
710 ; // FIXME - no timeout
711
712 /*
713 * I know. i is certainly != 0 here but the loop is new. See previous
714 * comment.
715 */
716 if (i) {
717 if (!((i = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_END_DMA_TRANSFER))
718 printk(KERN_ERR "53C400w: No END OF DMA bit - WHOOPS! BASR=%0x\n", i);
719 } else
720 printk(KERN_ERR "53C400w: no 53C80 gated irq after transfer (last block)\n");
721
722#if 0
723 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
724 printk(KERN_ERR "53C400w: no end dma signal\n");
725 }
726#endif
727 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
728 ; // TIMEOUT
729 return 0;
730}
731#endif /* PSEUDO_DMA */
732
733/*
734 * Include the NCR5380 core code that we build our driver around
735 */
736
737#include "NCR5380.c"
738
Al Virodd7ab712013-03-31 01:15:54 -0400739#define PRINTP(x) seq_printf(m, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740#define ANDP ,
741
Al Virodd7ab712013-03-31 01:15:54 -0400742static void sprint_opcode(struct seq_file *m, int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 PRINTP("0x%02x " ANDP opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Al Virodd7ab712013-03-31 01:15:54 -0400747static void sprint_command(struct seq_file *m, unsigned char *command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Al Virodd7ab712013-03-31 01:15:54 -0400749 int i, s;
750 sprint_opcode(m, command[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
752 PRINTP("%02x " ANDP command[i]);
753 PRINTP("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/**
757 * sprintf_Scsi_Cmnd - print a scsi command
Al Virodd7ab712013-03-31 01:15:54 -0400758 * @m: seq_fil to print into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * @cmd: SCSI command block
760 *
761 * Print out the target and command data in hex
762 */
763
Al Virodd7ab712013-03-31 01:15:54 -0400764static void sprint_Scsi_Cmnd(struct seq_file *m, Scsi_Cmnd * cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200766 PRINTP("host number %d destination target %d, lun %llu\n" ANDP cmd->device->host->host_no ANDP cmd->device->id ANDP cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 PRINTP(" command = ");
Al Virodd7ab712013-03-31 01:15:54 -0400768 sprint_command(m, cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/**
772 * generic_NCR5380_proc_info - /proc for NCR5380 driver
773 * @buffer: buffer to print into
774 * @start: start position
775 * @offset: offset into buffer
776 * @len: length
777 * @hostno: instance to affect
778 * @inout: read/write
779 *
780 * Provide the procfs information for the 5380 controller. We fill
781 * this with useful debugging information including the commands
782 * being executed, disconnected command queue and the statistical
783 * data
784 *
785 * Locks: global cli/lock for queue walk
786 */
787
Al Virodd7ab712013-03-31 01:15:54 -0400788static int generic_NCR5380_show_info(struct seq_file *m, struct Scsi_Host *scsi_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 NCR5380_local_declare();
791 unsigned long flags;
792 unsigned char status;
793 int i;
794 Scsi_Cmnd *ptr;
795 struct NCR5380_hostdata *hostdata;
796#ifdef NCR5380_STATS
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100797 struct scsi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798#endif
799
800 NCR5380_setup(scsi_ptr);
801 hostdata = (struct NCR5380_hostdata *) scsi_ptr->hostdata;
802
803 spin_lock_irqsave(scsi_ptr->host_lock, flags);
804 PRINTP("SCSI host number %d : %s\n" ANDP scsi_ptr->host_no ANDP scsi_ptr->hostt->name);
805 PRINTP("Generic NCR5380 driver version %d\n" ANDP GENERIC_NCR5380_PUBLIC_RELEASE);
806 PRINTP("NCR5380 core version %d\n" ANDP NCR5380_PUBLIC_RELEASE);
807#ifdef NCR53C400
808 PRINTP("NCR53C400 extension version %d\n" ANDP NCR53C400_PUBLIC_RELEASE);
809 PRINTP("NCR53C400 card%s detected\n" ANDP(((struct NCR5380_hostdata *) scsi_ptr->hostdata)->flags & FLAG_NCR53C400) ? "" : " not");
810# if NCR53C400_PSEUDO_DMA
811 PRINTP("NCR53C400 pseudo DMA used\n");
812# endif
813#else
814 PRINTP("NO NCR53C400 driver extensions\n");
815#endif
816 PRINTP("Using %s mapping at %s 0x%lx, " ANDP STRVAL(NCR5380_map_config) ANDP STRVAL(NCR5380_map_name) ANDP scsi_ptr->NCR5380_instance_name);
817 if (scsi_ptr->irq == SCSI_IRQ_NONE)
818 PRINTP("no interrupt\n");
819 else
820 PRINTP("on interrupt %d\n" ANDP scsi_ptr->irq);
821
822#ifdef NCR5380_STATS
823 if (hostdata->connected || hostdata->issue_queue || hostdata->disconnected_queue)
824 PRINTP("There are commands pending, transfer rates may be crud\n");
825 if (hostdata->pendingr)
826 PRINTP(" %d pending reads" ANDP hostdata->pendingr);
827 if (hostdata->pendingw)
828 PRINTP(" %d pending writes" ANDP hostdata->pendingw);
829 if (hostdata->pendingr || hostdata->pendingw)
830 PRINTP("\n");
831 shost_for_each_device(dev, scsi_ptr) {
832 unsigned long br = hostdata->bytes_read[dev->id];
833 unsigned long bw = hostdata->bytes_write[dev->id];
834 long tr = hostdata->time_read[dev->id] / HZ;
835 long tw = hostdata->time_write[dev->id] / HZ;
836
Matthew Wilcox4ff36712006-07-04 12:15:20 -0600837 PRINTP(" T:%d %s " ANDP dev->id ANDP scsi_device_type(dev->type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 for (i = 0; i < 8; i++)
839 if (dev->vendor[i] >= 0x20)
Al Virodd7ab712013-03-31 01:15:54 -0400840 seq_putc(m, dev->vendor[i]);
841 seq_putc(m, ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 for (i = 0; i < 16; i++)
843 if (dev->model[i] >= 0x20)
Al Virodd7ab712013-03-31 01:15:54 -0400844 seq_putc(m, dev->model[i]);
845 seq_putc(m, ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 for (i = 0; i < 4; i++)
847 if (dev->rev[i] >= 0x20)
Al Virodd7ab712013-03-31 01:15:54 -0400848 seq_putc(m, dev->rev[i]);
849 seq_putc(m, ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 PRINTP("\n%10ld kb read in %5ld secs" ANDP br / 1024 ANDP tr);
852 if (tr)
853 PRINTP(" @ %5ld bps" ANDP br / tr);
854
855 PRINTP("\n%10ld kb written in %5ld secs" ANDP bw / 1024 ANDP tw);
856 if (tw)
857 PRINTP(" @ %5ld bps" ANDP bw / tw);
858 PRINTP("\n");
859 }
860#endif
861
862 status = NCR5380_read(STATUS_REG);
863 if (!(status & SR_REQ))
864 PRINTP("REQ not asserted, phase unknown.\n");
865 else {
866 for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
867 PRINTP("Phase %s\n" ANDP phases[i].name);
868 }
869
870 if (!hostdata->connected) {
871 PRINTP("No currently connected command\n");
872 } else {
Al Virodd7ab712013-03-31 01:15:54 -0400873 sprint_Scsi_Cmnd(m, (Scsi_Cmnd *) hostdata->connected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
876 PRINTP("issue_queue\n");
877
878 for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400879 sprint_Scsi_Cmnd(m, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 PRINTP("disconnected_queue\n");
882
883 for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
Al Virodd7ab712013-03-31 01:15:54 -0400884 sprint_Scsi_Cmnd(m, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 spin_unlock_irqrestore(scsi_ptr->host_lock, flags);
Al Virodd7ab712013-03-31 01:15:54 -0400887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890#undef PRINTP
891#undef ANDP
892
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100893static struct scsi_host_template driver_template = {
Al Virodd7ab712013-03-31 01:15:54 -0400894 .show_info = generic_NCR5380_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 .name = "Generic NCR5380/NCR53C400 Scsi Driver",
896 .detect = generic_NCR5380_detect,
897 .release = generic_NCR5380_release_resources,
898 .info = generic_NCR5380_info,
899 .queuecommand = generic_NCR5380_queue_command,
900 .eh_abort_handler = generic_NCR5380_abort,
901 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 .bios_param = NCR5380_BIOSPARAM,
903 .can_queue = CAN_QUEUE,
904 .this_id = 7,
905 .sg_tablesize = SG_ALL,
906 .cmd_per_lun = CMD_PER_LUN,
907 .use_clustering = DISABLE_CLUSTERING,
908};
909#include <linux/module.h>
910#include "scsi_module.c"
911
912module_param(ncr_irq, int, 0);
913module_param(ncr_dma, int, 0);
914module_param(ncr_addr, int, 0);
915module_param(ncr_5380, int, 0);
916module_param(ncr_53c400, int, 0);
917module_param(ncr_53c400a, int, 0);
918module_param(dtc_3181e, int, 0);
919MODULE_LICENSE("GPL");
920
Ondrej Zary702a98c2010-08-10 18:01:16 -0700921#ifndef SCSI_G_NCR5380_MEM
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800922static struct isapnp_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 {
924 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
925 ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e),
926 0},
927 {0}
928};
929
930MODULE_DEVICE_TABLE(isapnp, id_table);
Ondrej Zary702a98c2010-08-10 18:01:16 -0700931#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933__setup("ncr5380=", do_NCR5380_setup);
934__setup("ncr53c400=", do_NCR53C400_setup);
935__setup("ncr53c400a=", do_NCR53C400A_setup);
936__setup("dtc3181e=", do_DTC3181E_setup);