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