1/*
2 *  sym53c416.c
3 *  Low-level SCSI driver for sym53c416 chip.
4 *  Copyright (C) 1998 Lieven Willems (lw_linux@hotmail.com)
5 *
6 *  Changes :
7 *
8 *  Marcelo Tosatti <marcelo@conectiva.com.br> : Added io_request_lock locking
9 *  Alan Cox <alan@redhat.com> : Cleaned up code formatting
10 *				 Fixed an irq locking bug
11 *				 Added ISAPnP support
12 *
13 *  LILO command line usage: sym53c416=<PORTBASE>[,<IRQ>]
14 *
15 *  This program is free software; you can redistribute it and/or modify it
16 *  under the terms of the GNU General Public License as published by the
17 *  Free Software Foundation; either version 2, or (at your option) any
18 *  later version.
19 *
20 *  This program is distributed in the hope that it will be useful, but
21 *  WITHOUT ANY WARRANTY; without even the implied warranty of
22 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 *  General Public License for more details.
24 *
25 */
26
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/types.h>
30#include <linux/string.h>
31#include <linux/ioport.h>
32#include <linux/sched.h>
33#include <linux/interrupt.h>
34#include <linux/delay.h>
35#include <linux/proc_fs.h>
36#include <linux/spinlock.h>
37#include <asm/dma.h>
38#include <asm/system.h>
39#include <asm/io.h>
40#include <linux/blk.h>
41#include <linux/version.h>
42#include <linux/isapnp.h>
43#include "scsi.h"
44#include "hosts.h"
45#include "sd.h"
46#include "sym53c416.h"
47
48#define VERSION_STRING        "Version 1.0.0-ac"
49
50#define TC_LOW       0x00     /* Transfer counter low        */
51#define TC_MID       0x01     /* Transfer counter mid        */
52#define SCSI_FIFO    0x02     /* SCSI FIFO register          */
53#define COMMAND_REG  0x03     /* Command Register            */
54#define STATUS_REG   0x04     /* Status Register (READ)      */
55#define DEST_BUS_ID  0x04     /* Destination Bus ID (WRITE)  */
56#define INT_REG      0x05     /* Interrupt Register (READ)   */
57#define TOM          0x05     /* Time out multiplier (WRITE) */
58#define STP          0x06     /* Synchronous Transfer period */
59#define SYNC_OFFSET  0x07     /* Synchronous Offset          */
60#define CONF_REG_1   0x08     /* Configuration register 1    */
61#define CONF_REG_2   0x0B     /* Configuration register 2    */
62#define CONF_REG_3   0x0C     /* Configuration register 3    */
63#define CONF_REG_4   0x0D     /* Configuration register 4    */
64#define TC_HIGH      0x0E     /* Transfer counter high       */
65#define PIO_FIFO_1   0x10     /* PIO FIFO register 1         */
66#define PIO_FIFO_2   0x11     /* PIO FIFO register 2         */
67#define PIO_FIFO_3   0x12     /* PIO FIFO register 3         */
68#define PIO_FIFO_4   0x13     /* PIO FIFO register 4         */
69#define PIO_FIFO_CNT 0x14     /* PIO FIFO count              */
70#define PIO_INT_REG  0x15     /* PIO interrupt register      */
71#define CONF_REG_5   0x16     /* Configuration register 5    */
72#define FEATURE_EN   0x1D     /* Feature Enable register     */
73
74/* Configuration register 1 entries: */
75/* Bits 2-0: SCSI ID of host adapter */
76#define SCM    0x80                     /* Slow Cable Mode              */
77#define SRID   0x40                     /* SCSI Reset Interrupt Disable */
78#define PTM    0x20                     /* Parity Test Mode             */
79#define EPC    0x10                     /* Enable Parity Checking       */
80#define CTME   0x08                     /* Special Test Mode            */
81
82/* Configuration register 2 entries: */
83#define FE     0x40                     /* Features Enable              */
84#define SCSI2  0x08                     /* SCSI 2 Enable                */
85#define TBPA   0x04                     /* Target Bad Parity Abort      */
86
87/* Configuration register 3 entries: */
88#define IDMRC  0x80                     /* ID Message Reserved Check    */
89#define QTE    0x40                     /* Queue Tag Enable             */
90#define CDB10  0x20                     /* Command Descriptor Block 10  */
91#define FSCSI  0x10                     /* FastSCSI                     */
92#define FCLK   0x08                     /* FastClock                    */
93
94/* Configuration register 4 entries: */
95#define RBS    0x08                     /* Register bank select         */
96#define EAN    0x04                     /* Enable Active Negotiation    */
97
98/* Configuration register 5 entries: */
99#define LPSR   0x80                     /* Lower Power SCSI Reset       */
100#define IE     0x20                     /* Interrupt Enable             */
101#define LPM    0x02                     /* Low Power Mode               */
102#define WSE0   0x01                     /* 0WS Enable                   */
103
104/* Interrupt register entries: */
105#define SRST   0x80                     /* SCSI Reset                   */
106#define ILCMD  0x40                     /* Illegal Command              */
107#define DIS    0x20                     /* Disconnect                   */
108#define BS     0x10                     /* Bus Service                  */
109#define FC     0x08                     /* Function Complete            */
110#define RESEL  0x04                     /* Reselected                   */
111#define SI     0x03                     /* Selection Interrupt          */
112
113/* Status Register Entries: */
114#define SCI    0x80                     /* SCSI Core Int                */
115#define GE     0x40                     /* Gross Error                  */
116#define PE     0x20                     /* Parity Error                 */
117#define TC     0x10                     /* Terminal Count               */
118#define VGC    0x08                     /* Valid Group Code             */
119#define PHBITS 0x07                     /* Phase bits                   */
120
121/* PIO Interrupt Register Entries: */
122#define SCI    0x80                     /* SCSI Core Int                */
123#define PFI    0x40                     /* PIO FIFO Interrupt           */
124#define FULL   0x20                     /* PIO FIFO Full                */
125#define EMPTY  0x10                     /* PIO FIFO Empty               */
126#define CE     0x08                     /* Collision Error              */
127#define OUE    0x04                     /* Overflow / Underflow error   */
128#define FIE    0x02                     /* Full Interrupt Enable        */
129#define EIE    0x01                     /* Empty Interrupt Enable       */
130
131/* SYM53C416 SCSI phases (lower 3 bits of SYM53C416_STATUS_REG) */
132#define PHASE_DATA_OUT    0x00
133#define PHASE_DATA_IN     0x01
134#define PHASE_COMMAND     0x02
135#define PHASE_STATUS      0x03
136#define PHASE_RESERVED_1  0x04
137#define PHASE_RESERVED_2  0x05
138#define PHASE_MESSAGE_OUT 0x06
139#define PHASE_MESSAGE_IN  0x07
140
141/* SYM53C416 core commands */
142#define NOOP                      0x00
143#define FLUSH_FIFO                0x01
144#define RESET_CHIP                0x02
145#define RESET_SCSI_BUS            0x03
146#define DISABLE_SEL_RESEL         0x45
147#define RESEL_SEQ                 0x40
148#define SEL_WITHOUT_ATN_SEQ       0x41
149#define SEL_WITH_ATN_SEQ          0x42
150#define SEL_WITH_ATN_AND_STOP_SEQ 0x43
151#define ENABLE_SEL_RESEL          0x44
152#define SEL_WITH_ATN3_SEQ         0x46
153#define RESEL3_SEQ                0x47
154#define SND_MSG                   0x20
155#define SND_STAT                  0x21
156#define SND_DATA                  0x22
157#define DISCONNECT_SEQ            0x23
158#define TERMINATE_SEQ             0x24
159#define TARGET_COMM_COMPLETE_SEQ  0x25
160#define DISCONN                   0x27
161#define RECV_MSG_SEQ              0x28
162#define RECV_CMD                  0x29
163#define RECV_DATA                 0x2A
164#define RECV_CMD_SEQ              0x2B
165#define TARGET_ABORT_PIO          0x04
166#define TRANSFER_INFORMATION      0x10
167#define INIT_COMM_COMPLETE_SEQ    0x11
168#define MSG_ACCEPTED              0x12
169#define TRANSFER_PAD              0x18
170#define SET_ATN                   0x1A
171#define RESET_ATN                 0x1B
172#define ILLEGAL                   0xFF
173
174#define PIO_MODE                  0x80
175
176#define IO_RANGE 0x20         /* 0x00 - 0x1F                   */
177#define ID       "sym53c416"	/* Attention: copied to the sym53c416.h */
178#define PIO_SIZE 128          /* Size of PIO fifo is 128 bytes */
179
180#define READ_TIMEOUT              150
181#define WRITE_TIMEOUT             150
182
183#ifdef MODULE
184
185#define sym53c416_base sym53c416
186#define sym53c416_base_1 sym53c416_1
187#define sym53c416_base_2 sym53c416_2
188#define sym53c416_base_3 sym53c416_3
189
190static unsigned int sym53c416_base[2] = {0,0};
191static unsigned int sym53c416_base_1[2] = {0,0};
192static unsigned int sym53c416_base_2[2] = {0,0};
193static unsigned int sym53c416_base_3[2] = {0,0};
194
195#endif
196
197/* #define DEBUG */
198
199/* Macro for debugging purposes */
200
201#ifdef DEBUG
202#define DEB(x) x
203#else
204#define DEB(x)
205#endif
206
207#define MAXHOSTS 4
208
209enum phases
210{
211	idle,
212	data_out,
213	data_in,
214	command_ph,
215	status_ph,
216	message_out,
217	message_in
218};
219
220typedef struct
221{
222	int base;
223	int irq;
224	int scsi_id;
225} host;
226
227static host hosts[MAXHOSTS] = {
228                       {0, 0, SYM53C416_SCSI_ID},
229                       {0, 0, SYM53C416_SCSI_ID},
230                       {0, 0, SYM53C416_SCSI_ID},
231                       {0, 0, SYM53C416_SCSI_ID}
232                       };
233
234static int host_index = 0;
235static char info[120];
236static Scsi_Cmnd *current_command = NULL;
237static int fastpio = 1;
238
239static int probeaddrs[] = {0x200, 0x220, 0x240, 0};
240
241static void sym53c416_set_transfer_counter(int base, unsigned int len)
242{
243	/* Program Transfer Counter */
244	outb(len & 0x0000FF, base + TC_LOW);
245	outb((len & 0x00FF00) >> 8, base + TC_MID);
246	outb((len & 0xFF0000) >> 16, base + TC_HIGH);
247}
248
249/* Returns the number of bytes read */
250static __inline__ unsigned int sym53c416_read(int base, unsigned char *buffer, unsigned int len)
251{
252	unsigned int orig_len = len;
253	unsigned long flags = 0;
254	unsigned int bytes_left;
255	int i;
256	int timeout = READ_TIMEOUT;
257
258	/* Do transfer */
259	save_flags(flags);
260	cli();
261	while(len && timeout)
262	{
263		bytes_left = inb(base + PIO_FIFO_CNT); /* Number of bytes in the PIO FIFO */
264		if(fastpio && bytes_left > 3)
265		{
266			insl(base + PIO_FIFO_1, buffer, bytes_left >> 2);
267			buffer += bytes_left & 0xFC;
268			len -= bytes_left & 0xFC;
269		}
270		else if(bytes_left > 0)
271		{
272			len -= bytes_left;
273			for(; bytes_left > 0; bytes_left--)
274				*(buffer++) = inb(base + PIO_FIFO_1);
275		}
276		else
277		{
278			i = jiffies + timeout;
279			restore_flags(flags);
280			while(jiffies < i && (inb(base + PIO_INT_REG) & EMPTY) && timeout)
281				if(inb(base + PIO_INT_REG) & SCI)
282					timeout = 0;
283			save_flags(flags);
284			cli();
285			if(inb(base + PIO_INT_REG) & EMPTY)
286				timeout = 0;
287		}
288	}
289	restore_flags(flags);
290	return orig_len - len;
291}
292
293/* Returns the number of bytes written */
294static __inline__ unsigned int sym53c416_write(int base, unsigned char *buffer, unsigned int len)
295{
296	unsigned int orig_len = len;
297	unsigned long flags = 0;
298	unsigned int bufferfree;
299	unsigned int i;
300	unsigned int timeout = WRITE_TIMEOUT;
301
302	/* Do transfer */
303	save_flags(flags);
304	cli();
305	while(len && timeout)
306	{
307		bufferfree = PIO_SIZE - inb(base + PIO_FIFO_CNT);
308		if(bufferfree > len)
309			bufferfree = len;
310		if(fastpio && bufferfree > 3)
311		{
312			outsl(base + PIO_FIFO_1, buffer, bufferfree >> 2);
313			buffer += bufferfree & 0xFC;
314			len -= bufferfree & 0xFC;
315		}
316		else if(bufferfree > 0)
317		{
318			len -= bufferfree;
319			for(; bufferfree > 0; bufferfree--)
320				outb(*(buffer++), base + PIO_FIFO_1);
321		}
322		else
323		{
324			i = jiffies + timeout;
325			restore_flags(flags);
326			while(jiffies < i && (inb(base + PIO_INT_REG) & FULL) && timeout)
327				;
328			save_flags(flags);
329			cli();
330			if(inb(base + PIO_INT_REG) & FULL)
331				timeout = 0;
332		}
333	}
334	restore_flags(flags);
335	return orig_len - len;
336}
337
338static void sym53c416_intr_handle(int irq, void *dev_id, struct pt_regs *regs)
339{
340	int base = 0;
341	int i;
342	unsigned long flags = 0;
343	unsigned char status_reg, pio_int_reg, int_reg;
344	struct scatterlist *sglist;
345	unsigned int sgcount;
346	unsigned int tot_trans = 0;
347
348	/* We search the base address of the host adapter which caused the interrupt */
349	for(i = 0; i < host_index && !base; i++)
350		if(irq == hosts[i].irq)
351			base = hosts[i].base;
352	/* If no adapter found, we cannot handle the interrupt. Leave a message */
353	/* and continue. This should never happen...                            */
354	if(!base)
355	{
356		printk(KERN_ERR "sym53c416: No host adapter defined for interrupt %d\n", irq);
357		return;
358	}
359	/* Now we have the base address and we can start handling the interrupt */
360
361	spin_lock_irqsave(&io_request_lock,flags);
362	status_reg = inb(base + STATUS_REG);
363	pio_int_reg = inb(base + PIO_INT_REG);
364	int_reg = inb(base + INT_REG);
365	spin_unlock_irqrestore(&io_request_lock, flags);
366
367	/* First, we handle error conditions */
368	if(int_reg & SCI)         /* SCSI Reset */
369	{
370		printk(KERN_DEBUG "sym53c416: Reset received\n");
371		current_command->SCp.phase = idle;
372		current_command->result = DID_RESET << 16;
373		spin_lock_irqsave(&io_request_lock, flags);
374		current_command->scsi_done(current_command);
375		spin_unlock_irqrestore(&io_request_lock, flags);
376		return;
377	}
378	if(int_reg & ILCMD)       /* Illegal Command */
379	{
380		printk(KERN_WARNING "sym53c416: Illegal Command: 0x%02x.\n", inb(base + COMMAND_REG));
381		current_command->SCp.phase = idle;
382		current_command->result = DID_ERROR << 16;
383		spin_lock_irqsave(&io_request_lock, flags);
384		current_command->scsi_done(current_command);
385		spin_unlock_irqrestore(&io_request_lock, flags);
386		return;
387	}
388	if(status_reg & GE)         /* Gross Error */
389	{
390		printk(KERN_WARNING "sym53c416: Controller reports gross error.\n");
391		current_command->SCp.phase = idle;
392		current_command->result = DID_ERROR << 16;
393		spin_lock_irqsave(&io_request_lock, flags);
394		current_command->scsi_done(current_command);
395		spin_unlock_irqrestore(&io_request_lock, flags);
396		return;
397	}
398	if(status_reg & PE)         /* Parity Error */
399	{
400		printk(KERN_WARNING "sym53c416:SCSI parity error.\n");
401		current_command->SCp.phase = idle;
402		current_command->result = DID_PARITY << 16;
403		spin_lock_irqsave(&io_request_lock, flags);
404		current_command->scsi_done(current_command);
405		spin_unlock_irqrestore(&io_request_lock, flags);
406		return;
407	}
408	if(pio_int_reg & (CE | OUE))
409	{
410		printk(KERN_WARNING "sym53c416: PIO interrupt error.\n");
411		current_command->SCp.phase = idle;
412		current_command->result = DID_ERROR << 16;
413		spin_lock_irqsave(&io_request_lock, flags);
414		current_command->scsi_done(current_command);
415		spin_unlock_irqrestore(&io_request_lock, flags);
416		return;
417	}
418	if(int_reg & DIS)           /* Disconnect */
419	{
420		if(current_command->SCp.phase != message_in)
421			current_command->result = DID_NO_CONNECT << 16;
422		else
423			current_command->result = (current_command->SCp.Status & 0xFF) | ((current_command->SCp.Message & 0xFF) << 8) | (DID_OK << 16);
424		current_command->SCp.phase = idle;
425		spin_lock_irqsave(&io_request_lock, flags);
426		current_command->scsi_done(current_command);
427		spin_unlock_irqrestore(&io_request_lock, flags);
428		return;
429	}
430	/* Now we handle SCSI phases         */
431
432	switch(status_reg & PHBITS)       /* Filter SCSI phase out of status reg */
433	{
434		case PHASE_DATA_OUT:
435		{
436			if(int_reg & BS)
437			{
438				current_command->SCp.phase = data_out;
439				outb(FLUSH_FIFO, base + COMMAND_REG);
440				sym53c416_set_transfer_counter(base, current_command->request_bufflen);
441				outb(TRANSFER_INFORMATION | PIO_MODE, base + COMMAND_REG);
442				if(!current_command->use_sg)
443					tot_trans = sym53c416_write(base, current_command->request_buffer, current_command->request_bufflen);
444				else
445				{
446					sgcount = current_command->use_sg;
447					sglist = current_command->request_buffer;
448					while(sgcount--)
449					{
450						tot_trans += sym53c416_write(base, sglist->address, sglist->length);
451						sglist++;
452					}
453				}
454				if(tot_trans < current_command->underflow)
455					printk(KERN_WARNING "sym53c416: Underflow, wrote %d bytes, request for %d bytes.\n", tot_trans, current_command->underflow);
456			}
457			break;
458		}
459
460		case PHASE_DATA_IN:
461		{
462			if(int_reg & BS)
463			{
464				current_command->SCp.phase = data_in;
465				outb(FLUSH_FIFO, base + COMMAND_REG);
466				sym53c416_set_transfer_counter(base, current_command->request_bufflen);
467				outb(TRANSFER_INFORMATION | PIO_MODE, base + COMMAND_REG);
468				if(!current_command->use_sg)
469					tot_trans = sym53c416_read(base, current_command->request_buffer, current_command->request_bufflen);
470				else
471				{
472					sgcount = current_command->use_sg;
473					sglist = current_command->request_buffer;
474					while(sgcount--)
475					{
476						tot_trans += sym53c416_read(base, sglist->address, sglist->length);
477						sglist++;
478					}
479				}
480				if(tot_trans < current_command->underflow)
481					printk(KERN_WARNING "sym53c416: Underflow, read %d bytes, request for %d bytes.\n", tot_trans, current_command->underflow);
482			}
483			break;
484		}
485
486		case PHASE_COMMAND:
487		{
488			current_command->SCp.phase = command_ph;
489			printk(KERN_ERR "sym53c416: Unknown interrupt in command phase.\n");
490			break;
491		}
492
493		case PHASE_STATUS:
494		{
495			current_command->SCp.phase = status_ph;
496			outb(FLUSH_FIFO, base + COMMAND_REG);
497			outb(INIT_COMM_COMPLETE_SEQ, base + COMMAND_REG);
498			break;
499		}
500
501		case PHASE_RESERVED_1:
502		case PHASE_RESERVED_2:
503		{
504			printk(KERN_ERR "sym53c416: Reserved phase occurred.\n");
505			break;
506		}
507
508		case PHASE_MESSAGE_OUT:
509		{
510			current_command->SCp.phase = message_out;
511			outb(SET_ATN, base + COMMAND_REG);
512			outb(MSG_ACCEPTED, base + COMMAND_REG);
513			break;
514		}
515
516		case PHASE_MESSAGE_IN:
517		{
518			current_command->SCp.phase = message_in;
519			current_command->SCp.Status = inb(base + SCSI_FIFO);
520			current_command->SCp.Message = inb(base + SCSI_FIFO);
521			if(current_command->SCp.Message == SAVE_POINTERS || current_command->SCp.Message == DISCONNECT)
522				outb(SET_ATN, base + COMMAND_REG);
523			outb(MSG_ACCEPTED, base + COMMAND_REG);
524			break;
525		}
526	}
527}
528
529static void sym53c416_init(int base, int scsi_id)
530{
531	outb(RESET_CHIP, base + COMMAND_REG);
532	outb(NOOP, base + COMMAND_REG);
533	outb(0x99, base + TOM); /* Time out of 250 ms */
534	outb(0x05, base + STP);
535	outb(0x00, base + SYNC_OFFSET);
536	outb(EPC | scsi_id, base + CONF_REG_1);
537	outb(FE | SCSI2 | TBPA, base + CONF_REG_2);
538	outb(IDMRC | QTE | CDB10 | FSCSI | FCLK, base + CONF_REG_3);
539	outb(0x83 | EAN, base + CONF_REG_4);
540	outb(IE | WSE0, base + CONF_REG_5);
541	outb(0, base + FEATURE_EN);
542}
543
544static int sym53c416_probeirq(int base, int scsi_id)
545{
546	int irq, irqs, i;
547
548	/* Clear interrupt register */
549	inb(base + INT_REG);
550	/* Start probing for irq's */
551	irqs = probe_irq_on();
552	/* Reinit chip */
553	sym53c416_init(base, scsi_id);
554	/* Cause interrupt */
555	outb(NOOP, base + COMMAND_REG);
556	outb(ILLEGAL, base + COMMAND_REG);
557	outb(0x07, base + DEST_BUS_ID);
558	outb(0x00, base + DEST_BUS_ID);
559	/* Wait for interrupt to occur */
560	i = jiffies + 20;
561	while(i > jiffies && !(inb(base + STATUS_REG) & SCI))
562		barrier();
563	if(i <= jiffies) /* timed out */
564		return 0;
565	/* Get occurred irq */
566	irq = probe_irq_off(irqs);
567	sym53c416_init(base, scsi_id);
568	return irq;
569}
570
571/* Setup: sym53c416=base,irq */
572void sym53c416_setup(char *str, int *ints)
573{
574	int i;
575
576	if(host_index >= MAXHOSTS)
577	{
578		printk(KERN_WARNING "sym53c416: Too many hosts defined\n");
579		return;
580	}
581	if(ints[0] < 1 || ints[0] > 2)
582	{
583		printk(KERN_ERR "sym53c416: Wrong number of parameters:\n");
584		printk(KERN_ERR "sym53c416: usage: sym53c416=<base>[,<irq>]\n");
585		return;
586	}
587	for(i = 0; i < host_index && i >= 0; i++)
588	        if(hosts[i].base == ints[1])
589        		i = -2;
590	if(i >= 0)
591	{
592        	hosts[host_index].base = ints[1];
593        	hosts[host_index].irq = (ints[0] == 2)? ints[2] : 0;
594        	host_index++;
595	}
596}
597
598static int sym53c416_test(int base)
599{
600	outb(RESET_CHIP, base + COMMAND_REG);
601	outb(NOOP, base + COMMAND_REG);
602	if(inb(base + COMMAND_REG) != NOOP)
603		return 0;
604	if(!inb(base + TC_HIGH) || inb(base + TC_HIGH) == 0xFF)
605		return 0;
606	if((inb(base + PIO_INT_REG) & (FULL | EMPTY | CE | OUE | FIE | EIE)) != EMPTY)
607		return 0;
608	return 1;
609}
610
611
612static struct isapnp_device_id id_table[] = {
613	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
614		ISAPNP_VENDOR('S','L','I'), ISAPNP_FUNCTION(0x4163), 0 },
615	{0}
616};
617
618MODULE_DEVICE_TABLE(isapnp, id_table);
619
620void sym53c416_probe(void)
621{
622	int *base = probeaddrs;
623	int ints[2];
624
625	ints[0] = 1;
626	for(; *base; base++)
627	{
628		if(!check_region(*base, IO_RANGE) && sym53c416_test(*base))
629		{
630			ints[1] = *base;
631			sym53c416_setup(NULL, ints);
632		}
633	}
634}
635
636int sym53c416_detect(Scsi_Host_Template *tpnt)
637{
638	unsigned long flags;
639	struct Scsi_Host * shpnt = NULL;
640	int i;
641	int count;
642	struct pci_dev *idev = NULL;
643
644#ifdef MODULE
645	int ints[3];
646
647	ints[0] = 2;
648	if(sym53c416_base)
649	{
650		ints[1] = sym53c416_base[0];
651		ints[2] = sym53c416_base[1];
652		sym53c416_setup(NULL, ints);
653	}
654	if(sym53c416_base_1)
655	{
656		ints[1] = sym53c416_base_1[0];
657		ints[2] = sym53c416_base_1[1];
658		sym53c416_setup(NULL, ints);
659	}
660	if(sym53c416_base_2)
661	{
662		ints[1] = sym53c416_base_2[0];
663		ints[2] = sym53c416_base_2[1];
664		sym53c416_setup(NULL, ints);
665	}
666	if(sym53c416_base_3)
667	{
668		ints[1] = sym53c416_base_3[0];
669		ints[2] = sym53c416_base_3[1];
670		sym53c416_setup(NULL, ints);
671	}
672#endif
673	printk(KERN_INFO "sym53c416.c: %s\n", VERSION_STRING);
674
675	while((idev=isapnp_find_dev(NULL, ISAPNP_VENDOR('S','L','I'),
676				ISAPNP_FUNCTION(0x4163), idev))!=NULL)
677	{
678		int i[3];
679
680		if(idev->prepare(idev)<0)
681		{
682			printk(KERN_WARNING "sym53c416: unable to prepare PnP card.\n");
683			continue;
684		}
685		if(idev->activate(idev)<0)
686		{
687			printk(KERN_WARNING "sym53c416: unable to activate PnP card.\n");
688			continue;
689		}
690
691		i[0] = 2;
692		i[1] = idev->resource[0].start;
693		i[2] = idev->irq_resource[0].start;
694
695		printk(KERN_INFO "sym53c416: ISAPnP card found and configured at 0x%X, IRQ %d.\n",
696			i[1], i[2]);
697		sym53c416_setup(NULL, i);
698	}
699	sym53c416_probe();
700
701	/* Now we register and set up each host adapter found... */
702	for(count = 0, i = 0; i < host_index; i++)
703	{
704		if(!sym53c416_test(hosts[i].base))
705			printk(KERN_WARNING "No sym53c416 found at address 0x%03x\n", hosts[i].base);
706		else
707		{
708			if(hosts[i].irq == 0)
709			/* We don't have an irq yet, so we should probe for one */
710				if((hosts[i].irq = sym53c416_probeirq(hosts[i].base, hosts[i].scsi_id)) == 0)
711					printk(KERN_WARNING "IRQ autoprobing failed for sym53c416 at address 0x%03x\n", hosts[i].base);
712			if(hosts[i].irq && !check_region(hosts[i].base, IO_RANGE))
713			{
714				shpnt = scsi_register(tpnt, 0);
715				if(shpnt==NULL)
716					continue;
717				save_flags(flags);
718				cli();
719				/* Request for specified IRQ */
720				if(request_irq(hosts[i].irq, sym53c416_intr_handle, 0, ID, NULL))
721				{
722					restore_flags(flags);
723					printk(KERN_ERR "sym53c416: Unable to assign IRQ %d\n", hosts[i].irq);
724					scsi_unregister(shpnt);
725				}
726				else
727				{
728					/* Inform the kernel of our IO range */
729					request_region(hosts[i].base, IO_RANGE, ID);
730					shpnt->unique_id = hosts[i].base;
731					shpnt->io_port = hosts[i].base;
732					shpnt->n_io_port = IO_RANGE;
733					shpnt->irq = hosts[i].irq;
734					shpnt->this_id = hosts[i].scsi_id;
735					sym53c416_init(hosts[i].base, hosts[i].scsi_id);
736					count++;
737					restore_flags(flags);
738				}
739			}
740		}
741	}
742	return count;
743}
744
745const char *sym53c416_info(struct Scsi_Host *SChost)
746{
747	int i;
748	int base = SChost->io_port;
749	int irq = SChost->irq;
750	int scsi_id = 0;
751	int rev = inb(base + TC_HIGH);
752
753	for(i = 0; i < host_index; i++)
754		if(hosts[i].base == base)
755			scsi_id = hosts[i].scsi_id;
756	sprintf(info, "Symbios Logic 53c416 (rev. %d) at 0x%03x, irq %d, SCSI-ID %d, %s pio", rev, base, irq, scsi_id, (fastpio)? "fast" : "slow");
757	return info;
758}
759
760int sym53c416_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
761{
762	int base;
763	unsigned long flags = 0;
764	int i;
765
766	/* Store base register as we can have more than one controller in the system */
767	base = SCpnt->host->io_port;
768	current_command = SCpnt;                  /* set current command                */
769	current_command->scsi_done = done;        /* set ptr to done function           */
770	current_command->SCp.phase = command_ph;  /* currect phase is the command phase */
771	current_command->SCp.Status = 0;
772	current_command->SCp.Message = 0;
773
774	save_flags(flags);
775	cli();
776	outb(SCpnt->target, base + DEST_BUS_ID); /* Set scsi id target        */
777	outb(FLUSH_FIFO, base + COMMAND_REG);    /* Flush SCSI and PIO FIFO's */
778	/* Write SCSI command into the SCSI fifo */
779	for(i = 0; i < SCpnt->cmd_len; i++)
780		outb(SCpnt->cmnd[i], base + SCSI_FIFO);
781	/* Start selection sequence */
782	outb(SEL_WITHOUT_ATN_SEQ, base + COMMAND_REG);
783	/* Now an interrupt will be generated which we will catch in out interrupt routine */
784	restore_flags(flags);
785	return 0;
786}
787
788static void internal_done(Scsi_Cmnd *SCpnt)
789{
790	SCpnt->SCp.Status++;
791}
792
793static int sym53c416_command(Scsi_Cmnd *SCpnt)
794{
795	sym53c416_queuecommand(SCpnt, internal_done);
796	SCpnt->SCp.Status = 0;
797	while(!SCpnt->SCp.Status)
798		barrier();
799	return SCpnt->result;
800}
801
802static int sym53c416_abort(Scsi_Cmnd *SCpnt)
803{
804	//printk("sym53c416_abort\n");
805	/* We don't know how to abort for the moment */
806	return SCSI_ABORT_SNOOZE;
807}
808
809static int sym53c416_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
810{
811	int base;
812	int scsi_id = -1;
813	int i;
814
815	//printk("sym53c416_reset\n");
816	base = SCpnt->host->io_port;
817	/* search scsi_id */
818	for(i = 0; i < host_index && scsi_id != -1; i++)
819		if(hosts[i].base == base)
820			scsi_id = hosts[i].scsi_id;
821	outb(RESET_CHIP, base + COMMAND_REG);
822	outb(NOOP | PIO_MODE, base + COMMAND_REG);
823	outb(RESET_SCSI_BUS, base + COMMAND_REG);
824	sym53c416_init(base, scsi_id);
825	return SCSI_RESET_PENDING;
826}
827
828static int sym53c416_bios_param(Disk *disk, kdev_t dev, int *ip)
829{
830	int size;
831
832	size = disk->capacity;
833	ip[0] = 64;				/* heads                        */
834	ip[1] = 32;				/* sectors                      */
835	if((ip[2] = size >> 11) > 1024)		/* cylinders, test for big disk */
836	{
837		ip[0] = 255;			/* heads                        */
838		ip[1] = 63;			/* sectors                      */
839		ip[2] = size / (255 * 63);	/* cylinders                    */
840	}
841	return 0;
842}
843
844/* Loadable module support */
845#ifdef MODULE
846
847MODULE_AUTHOR("Lieven Willems");
848MODULE_LICENSE("GPL");
849
850MODULE_PARM(sym53c416, "1-2i");
851MODULE_PARM(sym53c416_1, "1-2i");
852MODULE_PARM(sym53c416_2, "1-2i");
853MODULE_PARM(sym53c416_3, "1-2i");
854
855#endif
856
857static Scsi_Host_Template driver_template = SYM53C416;
858
859#include "scsi_module.c"
860