Import 2.3.18pre1
[davej-history.git] / drivers / scsi / sym53c416.c
blob84d5706a95b1951c3178ee1a8242fd16996a5e0f
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 *
10 * LILO command line usage: sym53c416=<PORTBASE>[,<IRQ>]
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
15 * later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/ioport.h>
29 #include <linux/sched.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/proc_fs.h>
33 #include <linux/spinlock.h>
34 #include <asm/dma.h>
35 #include <asm/system.h>
36 #include <asm/io.h>
37 #include <linux/blk.h>
38 #include <linux/version.h>
39 #include "scsi.h"
40 #include "hosts.h"
41 #include "sd.h"
42 #include "sym53c416.h"
44 #define VERSION_STRING "Version 1.0.0"
46 #define TC_LOW 0x00 /* Transfer counter low */
47 #define TC_MID 0x01 /* Transfer counter mid */
48 #define SCSI_FIFO 0x02 /* SCSI FIFO register */
49 #define COMMAND_REG 0x03 /* Command Register */
50 #define STATUS_REG 0x04 /* Status Register (READ) */
51 #define DEST_BUS_ID 0x04 /* Destination Bus ID (WRITE) */
52 #define INT_REG 0x05 /* Interrupt Register (READ) */
53 #define TOM 0x05 /* Time out multiplier (WRITE) */
54 #define STP 0x06 /* Synchronous Transfer period */
55 #define SYNC_OFFSET 0x07 /* Synchronous Offset */
56 #define CONF_REG_1 0x08 /* Configuration register 1 */
57 #define CONF_REG_2 0x0B /* Configuration register 2 */
58 #define CONF_REG_3 0x0C /* Configuration register 3 */
59 #define CONF_REG_4 0x0D /* Configuration register 4 */
60 #define TC_HIGH 0x0E /* Transfer counter high */
61 #define PIO_FIFO_1 0x10 /* PIO FIFO register 1 */
62 #define PIO_FIFO_2 0x11 /* PIO FIFO register 2 */
63 #define PIO_FIFO_3 0x12 /* PIO FIFO register 3 */
64 #define PIO_FIFO_4 0x13 /* PIO FIFO register 4 */
65 #define PIO_FIFO_CNT 0x14 /* PIO FIFO count */
66 #define PIO_INT_REG 0x15 /* PIO interrupt register */
67 #define CONF_REG_5 0x16 /* Configuration register 5 */
68 #define FEATURE_EN 0x1D /* Feature Enable register */
70 /* Configuration register 1 entries: */
71 /* Bits 2-0: SCSI ID of host adapter */
72 #define SCM 0x80 /* Slow Cable Mode */
73 #define SRID 0x40 /* SCSI Reset Interrupt Disable */
74 #define PTM 0x20 /* Parity Test Mode */
75 #define EPC 0x10 /* Enable Parity Checking */
76 #define CTME 0x08 /* Special Test Mode */
78 /* Configuration register 2 entries: */
79 #define FE 0x40 /* Features Enable */
80 #define SCSI2 0x08 /* SCSI 2 Enable */
81 #define TBPA 0x04 /* Target Bad Parity Abort */
83 /* Configuration register 3 entries: */
84 #define IDMRC 0x80 /* ID Message Reserved Check */
85 #define QTE 0x40 /* Queue Tag Enable */
86 #define CDB10 0x20 /* Command Descriptor Block 10 */
87 #define FSCSI 0x10 /* FastSCSI */
88 #define FCLK 0x08 /* FastClock */
90 /* Configuration register 4 entries: */
91 #define RBS 0x08 /* Register bank select */
92 #define EAN 0x04 /* Enable Active Negotiation */
94 /* Configuration register 5 entries: */
95 #define LPSR 0x80 /* Lower Power SCSI Reset */
96 #define IE 0x20 /* Interrupt Enable */
97 #define LPM 0x02 /* Low Power Mode */
98 #define WSE0 0x01 /* 0WS Enable */
100 /* Interrupt register entries: */
101 #define SRST 0x80 /* SCSI Reset */
102 #define ILCMD 0x40 /* Illegal Command */
103 #define DIS 0x20 /* Disconnect */
104 #define BS 0x10 /* Bus Service */
105 #define FC 0x08 /* Function Complete */
106 #define RESEL 0x04 /* Reselected */
107 #define SI 0x03 /* Selection Interrupt */
109 /* Status Register Entries: */
110 #define SCI 0x80 /* SCSI Core Int */
111 #define GE 0x40 /* Gross Error */
112 #define PE 0x20 /* Parity Error */
113 #define TC 0x10 /* Terminal Count */
114 #define VGC 0x08 /* Valid Group Code */
115 #define PHBITS 0x07 /* Phase bits */
117 /* PIO Interrupt Register Entries: */
118 #define SCI 0x80 /* SCSI Core Int */
119 #define PFI 0x40 /* PIO FIFO Interrupt */
120 #define FULL 0x20 /* PIO FIFO Full */
121 #define EMPTY 0x10 /* PIO FIFO Empty */
122 #define CE 0x08 /* Collision Error */
123 #define OUE 0x04 /* Overflow / Underflow error */
124 #define FIE 0x02 /* Full Interrupt Enable */
125 #define EIE 0x01 /* Empty Interrupt Enable */
127 /* SYM53C416 SCSI phases (lower 3 bits of SYM53C416_STATUS_REG) */
128 #define PHASE_DATA_OUT 0x00
129 #define PHASE_DATA_IN 0x01
130 #define PHASE_COMMAND 0x02
131 #define PHASE_STATUS 0x03
132 #define PHASE_RESERVED_1 0x04
133 #define PHASE_RESERVED_2 0x05
134 #define PHASE_MESSAGE_OUT 0x06
135 #define PHASE_MESSAGE_IN 0x07
137 /* SYM53C416 core commands */
138 #define NOOP 0x00
139 #define FLUSH_FIFO 0x01
140 #define RESET_CHIP 0x02
141 #define RESET_SCSI_BUS 0x03
142 #define DISABLE_SEL_RESEL 0x45
143 #define RESEL_SEQ 0x40
144 #define SEL_WITHOUT_ATN_SEQ 0x41
145 #define SEL_WITH_ATN_SEQ 0x42
146 #define SEL_WITH_ATN_AND_STOP_SEQ 0x43
147 #define ENABLE_SEL_RESEL 0x44
148 #define SEL_WITH_ATN3_SEQ 0x46
149 #define RESEL3_SEQ 0x47
150 #define SND_MSG 0x20
151 #define SND_STAT 0x21
152 #define SND_DATA 0x22
153 #define DISCONNECT_SEQ 0x23
154 #define TERMINATE_SEQ 0x24
155 #define TARGET_COMM_COMPLETE_SEQ 0x25
156 #define DISCONN 0x27
157 #define RECV_MSG_SEQ 0x28
158 #define RECV_CMD 0x29
159 #define RECV_DATA 0x2A
160 #define RECV_CMD_SEQ 0x2B
161 #define TARGET_ABORT_PIO 0x04
162 #define TRANSFER_INFORMATION 0x10
163 #define INIT_COMM_COMPLETE_SEQ 0x11
164 #define MSG_ACCEPTED 0x12
165 #define TRANSFER_PAD 0x18
166 #define SET_ATN 0x1A
167 #define RESET_ATN 0x1B
168 #define ILLEGAL 0xFF
170 #define PIO_MODE 0x80
172 #define IO_RANGE 0x20 /* 0x00 - 0x1F */
173 #define ID "sym53c416"
174 #define PIO_SIZE 128 /* Size of PIO fifo is 128 bytes */
176 #define READ_TIMEOUT 150
177 #define WRITE_TIMEOUT 150
179 #ifdef MODULE
181 #define sym53c416_base sym53c416
182 #define sym53c416_base_1 sym53c416_1
183 #define sym53c416_base_2 sym53c416_2
184 #define sym53c416_base_3 sym53c416_3
186 static unsigned short sym53c416_base = 0;
187 static unsigned int sym53c416_irq = 0;
188 static unsigned short sym53c416_base_1 = 0;
189 static unsigned int sym53c416_irq_1 = 0;
190 static unsigned short sym53c416_base_2 = 0;
191 static unsigned int sym53c416_irq_2 = 0;
192 static unsigned short sym53c416_base_3 = 0;
193 static unsigned int sym53c416_irq_3 = 0;
195 #endif
197 /* #define DEBUG */
199 /* Macro for debugging purposes */
201 #ifdef DEBUG
202 #define DEB(x) x
203 #else
204 #define DEB(x)
205 #endif
207 #define MAXHOSTS 4
209 enum phases
211 idle,
212 data_out,
213 data_in,
214 command_ph,
215 status_ph,
216 message_out,
217 message_in
220 typedef struct
222 int base;
223 int irq;
224 int scsi_id;
225 } host;
227 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}
234 static int host_index = 0;
236 static char info[120];
238 static Scsi_Cmnd *current_command = NULL;
240 struct proc_dir_entry proc_scsi_sym53c416 = {PROC_SCSI_SYM53C416, 7, ID, S_IFDIR | S_IRUGO | S_IXUGO, 2};
242 int fastpio = 1;
244 int probeaddrs[] = {0x200, 0x220, 0x240, 0};
246 static void sym53c416_set_transfer_counter(int base, unsigned int len)
248 /* Program Transfer Counter */
249 outb(len & 0x0000FF, base + TC_LOW);
250 outb((len & 0x00FF00) >> 8, base + TC_MID);
251 outb((len & 0xFF0000) >> 16, base + TC_HIGH);
254 /* Returns the number of bytes read */
255 static __inline__ unsigned int sym53c416_read(int base, unsigned char *buffer, unsigned int len)
257 unsigned int orig_len = len;
258 unsigned long flags = 0;
259 unsigned int bytes_left;
260 int i;
261 int timeout = READ_TIMEOUT;
263 /* Do transfer */
264 save_flags(flags);
265 cli();
266 while(len && timeout)
268 bytes_left = inb(base + PIO_FIFO_CNT); /* Number of bytes in the PIO FIFO */
269 if(fastpio && bytes_left > 3)
271 insl(base + PIO_FIFO_1, buffer, bytes_left >> 2);
272 buffer += bytes_left & 0xFC;
273 len -= bytes_left & 0xFC;
275 else if(bytes_left > 0)
277 len -= bytes_left;
278 for(; bytes_left > 0; bytes_left--)
279 *(buffer++) = inb(base + PIO_FIFO_1);
281 else
283 i = jiffies + timeout;
284 restore_flags(flags);
285 while(jiffies < i && (inb(base + PIO_INT_REG) & EMPTY) && timeout)
286 if(inb(base + PIO_INT_REG) & SCI)
287 timeout = 0;
288 save_flags(flags);
289 cli();
290 if(inb(base + PIO_INT_REG) & EMPTY)
291 timeout = 0;
294 restore_flags(flags);
295 return orig_len - len;
298 /* Returns the number of bytes written */
299 static __inline__ unsigned int sym53c416_write(int base, unsigned char *buffer, unsigned int len)
301 unsigned int orig_len = len;
302 unsigned long flags = 0;
303 unsigned int bufferfree;
304 unsigned int i;
305 unsigned int timeout = WRITE_TIMEOUT;
307 /* Do transfer */
308 save_flags(flags);
309 cli();
310 while(len && timeout)
312 bufferfree = PIO_SIZE - inb(base + PIO_FIFO_CNT);
313 if(bufferfree > len)
314 bufferfree = len;
315 if(fastpio && bufferfree > 3)
317 outsl(base + PIO_FIFO_1, buffer, bufferfree >> 2);
318 buffer += bufferfree & 0xFC;
319 len -= bufferfree & 0xFC;
321 else if(bufferfree > 0)
323 len -= bufferfree;
324 for(; bufferfree > 0; bufferfree--)
325 outb(*(buffer++), base + PIO_FIFO_1);
327 else
329 i = jiffies + timeout;
330 restore_flags(flags);
331 while(jiffies < i && (inb(base + PIO_INT_REG) & FULL) && timeout)
333 save_flags(flags);
334 cli();
335 if(inb(base + PIO_INT_REG) & FULL)
336 timeout = 0;
339 restore_flags(flags);
340 return orig_len - len;
343 static void sym53c416_intr_handle(int irq, void *dev_id, struct pt_regs *regs)
345 int base = 0;
346 int i;
347 unsigned long flags = 0;
348 unsigned char status_reg, pio_int_reg, int_reg;
349 struct scatterlist *sglist;
350 unsigned int sgcount;
351 unsigned int tot_trans = 0;
353 /* We search the base address of the host adapter which caused the interrupt */
354 for(i = 0; i < host_index && !base; i++)
355 if(irq == hosts[i].irq)
356 base = hosts[i].base;
357 /* If no adapter found, we cannot handle the interrupt. Leave a message */
358 /* and continue. This should never happen... */
359 if(!base)
361 printk("sym53c416: No host adapter defined for interrupt %d\n", irq);
362 return;
364 /* Now we have the base address and we can start handling the interrupt */
365 save_flags(flags);
366 cli();
367 status_reg = inb(base + STATUS_REG);
368 pio_int_reg = inb(base + PIO_INT_REG);
369 int_reg = inb(base + INT_REG);
370 restore_flags(flags);
372 /* First, we handle error conditions */
373 if(int_reg & SCI) /* SCSI Reset */
375 printk("sym53c416: Warning: Reset received\n");
376 current_command->SCp.phase = idle;
377 current_command->result = DID_RESET << 16;
378 spin_lock_irqsave(&io_request_lock, flags);
379 current_command->scsi_done(current_command);
380 spin_unlock_irqrestore(&io_request_lock, flags);
381 return;
383 if(int_reg & ILCMD) /* Illegal Command */
385 printk("sym53c416: Warning: Illegal Command: 0x%02x\n", inb(base + COMMAND_REG));
386 current_command->SCp.phase = idle;
387 current_command->result = DID_ERROR << 16;
388 spin_lock_irqsave(&io_request_lock, flags);
389 current_command->scsi_done(current_command);
390 spin_unlock_irqrestore(&io_request_lock, flags);
391 return;
393 if(status_reg & GE) /* Gross Error */
395 printk("sym53c416: Warning: Gross Error\n");
396 current_command->SCp.phase = idle;
397 current_command->result = DID_ERROR << 16;
398 spin_lock_irqsave(&io_request_lock, flags);
399 current_command->scsi_done(current_command);
400 spin_unlock_irqrestore(&io_request_lock, flags);
401 return;
403 if(status_reg & PE) /* Parity Error */
405 printk("sym53c416: Warning: Parity Error\n");
406 current_command->SCp.phase = idle;
407 current_command->result = DID_PARITY << 16;
408 spin_lock_irqsave(&io_request_lock, flags);
409 current_command->scsi_done(current_command);
410 spin_unlock_irqrestore(&io_request_lock, flags);
411 return;
413 if(pio_int_reg & (CE | OUE))
415 printk("sym53c416: Warning: PIO Interrupt Error\n");
416 current_command->SCp.phase = idle;
417 current_command->result = DID_ERROR << 16;
418 spin_lock_irqsave(&io_request_lock, flags);
419 current_command->scsi_done(current_command);
420 spin_unlock_irqrestore(&io_request_lock, flags);
421 return;
423 if(int_reg & DIS) /* Disconnect */
425 if(current_command->SCp.phase != message_in)
426 current_command->result = DID_NO_CONNECT << 16;
427 else
428 current_command->result = (current_command->SCp.Status & 0xFF) | ((current_command->SCp.Message & 0xFF) << 8) | (DID_OK << 16);
429 current_command->SCp.phase = idle;
431 spin_lock_irqsave(&io_request_lock, flags);
432 current_command->scsi_done(current_command);
433 spin_unlock_irqrestore(&io_request_lock, flags);
434 return;
436 /* Now we handle SCSI phases */
437 switch(status_reg & PHBITS) /* Filter SCSI phase out of status reg */
439 case PHASE_DATA_OUT:
441 if(int_reg & BS)
443 current_command->SCp.phase = data_out;
444 outb(FLUSH_FIFO, base + COMMAND_REG);
445 sym53c416_set_transfer_counter(base, current_command->request_bufflen);
446 outb(TRANSFER_INFORMATION | PIO_MODE, base + COMMAND_REG);
447 if(!current_command->use_sg)
448 tot_trans = sym53c416_write(base, current_command->request_buffer, current_command->request_bufflen);
449 else
451 sgcount = current_command->use_sg;
452 sglist = current_command->request_buffer;
453 while(sgcount--)
455 tot_trans += sym53c416_write(base, sglist->address, sglist->length);
456 sglist++;
459 if(tot_trans < current_command->underflow)
460 printk("sym53c416: Warning: underflow, wrote %d bytes, request for %d bytes\n", tot_trans, current_command->underflow);
462 break;
464 case PHASE_DATA_IN:
466 if(int_reg & BS)
468 current_command->SCp.phase = data_in;
469 outb(FLUSH_FIFO, base + COMMAND_REG);
470 sym53c416_set_transfer_counter(base, current_command->request_bufflen);
471 outb(TRANSFER_INFORMATION | PIO_MODE, base + COMMAND_REG);
472 if(!current_command->use_sg)
473 tot_trans = sym53c416_read(base, current_command->request_buffer, current_command->request_bufflen);
474 else
476 sgcount = current_command->use_sg;
477 sglist = current_command->request_buffer;
478 while(sgcount--)
480 tot_trans += sym53c416_read(base, sglist->address, sglist->length);
481 sglist++;
484 if(tot_trans < current_command->underflow)
485 printk("sym53c416: Warning: underflow, read %d bytes, request for %d bytes\n", tot_trans, current_command->underflow);
487 break;
489 case PHASE_COMMAND:
491 current_command->SCp.phase = command_ph;
492 printk("sym53c416: Warning: Unknown interrupt in command phase\n");
493 break;
495 case PHASE_STATUS:
497 current_command->SCp.phase = status_ph;
498 outb(FLUSH_FIFO, base + COMMAND_REG);
499 outb(INIT_COMM_COMPLETE_SEQ, base + COMMAND_REG);
500 break;
502 case PHASE_RESERVED_1:
503 case PHASE_RESERVED_2:
505 printk("sym53c416: Warning: Reserved phase\n");
506 break;
508 case PHASE_MESSAGE_OUT:
510 current_command->SCp.phase = message_out;
511 outb(SET_ATN, base + COMMAND_REG);
512 outb(MSG_ACCEPTED, base + COMMAND_REG);
513 break;
515 case PHASE_MESSAGE_IN:
517 current_command->SCp.phase = message_in;
518 current_command->SCp.Status = inb(base + SCSI_FIFO);
519 current_command->SCp.Message = inb(base + SCSI_FIFO);
520 if(current_command->SCp.Message == SAVE_POINTERS || current_command->SCp.Message == DISCONNECT)
521 outb(SET_ATN, base + COMMAND_REG);
522 outb(MSG_ACCEPTED, base + COMMAND_REG);
523 break;
528 static void sym53c416_init(int base, int scsi_id)
530 outb(RESET_CHIP, base + COMMAND_REG);
531 outb(NOOP, base + COMMAND_REG);
532 outb(0x99, base + TOM); /* Time out of 250 ms */
533 outb(0x05, base + STP);
534 outb(0x00, base + SYNC_OFFSET);
535 outb(EPC | scsi_id, base + CONF_REG_1);
536 outb(FE | SCSI2 | TBPA, base + CONF_REG_2);
537 outb(IDMRC | QTE | CDB10 | FSCSI | FCLK, base + CONF_REG_3);
538 outb(0x83 | EAN, base + CONF_REG_4);
539 outb(IE | WSE0, base + CONF_REG_5);
540 outb(0, base + FEATURE_EN);
543 static int sym53c416_probeirq(int base, int scsi_id)
545 int irq, irqs, i;
547 /* Clear interrupt register */
548 inb(base + INT_REG);
549 /* Start probing for irq's */
550 irqs = probe_irq_on();
551 /* Reinit chip */
552 sym53c416_init(base, scsi_id);
553 /* Cause interrupt */
554 outb(NOOP, base + COMMAND_REG);
555 outb(ILLEGAL, base + COMMAND_REG);
556 outb(0x07, base + DEST_BUS_ID);
557 outb(0x00, base + DEST_BUS_ID);
558 /* Wait for interrupt to occur */
559 i = jiffies + 20;
560 while(i > jiffies && !(inb(base + STATUS_REG) & SCI))
561 barrier();
562 if(i <= jiffies) /* timed out */
563 return 0;
564 /* Get occurred irq */
565 irq = probe_irq_off(irqs);
566 sym53c416_init(base, scsi_id);
567 return irq;
570 /* Setup: sym53c416=base,irq */
571 void sym53c416_setup(char *str, int *ints)
573 int i;
575 if(host_index >= MAXHOSTS)
577 printk("sym53c416.c: Too many hosts defined\n");
579 else
581 if(ints[0] < 1 || ints[0] > 2)
583 printk("sym53c416.c: Wrong number of parameters:\n");
584 printk("sym53c416.c: usage: sym53c416=<base>[,<irq>]\n");
586 else
588 for(i = 0; i < host_index && i >= 0; i++)
589 if(hosts[i].base == ints[1])
590 i = -2;
591 if(i >= 0)
593 hosts[host_index].base = ints[1];
594 hosts[host_index].irq = (ints[0] == 2)? ints[2] : 0;
595 host_index++;
601 static int sym53c416_test(int base)
603 outb(RESET_CHIP, base + COMMAND_REG);
604 outb(NOOP, base + COMMAND_REG);
605 if(inb(base + COMMAND_REG) != NOOP)
606 return 0;
607 if(!inb(base + TC_HIGH) || inb(base + TC_HIGH) == 0xFF)
608 return 0;
609 if((inb(base + PIO_INT_REG) & (FULL | EMPTY | CE | OUE | FIE | EIE)) != EMPTY)
610 return 0;
611 return 1;
614 void sym53c416_probe(void)
616 int *base = probeaddrs;
617 int ints[2];
619 ints[0] = 1;
620 for(; *base; base++)
621 if(!check_region(*base, IO_RANGE) && sym53c416_test(*base))
623 ints[1] = *base;
624 sym53c416_setup(NULL, ints);
628 int sym53c416_detect(Scsi_Host_Template *tpnt)
630 unsigned long flags;
631 struct Scsi_Host * shpnt = NULL;
632 int i;
633 int count;
635 #ifdef MODULE
636 int ints[3];
638 ints[0] = 2;
639 if(sym53c416_base)
641 ints[1] = sym53c416_base;
642 ints[2] = sym53c416_irq;
643 sym53c416_setup(NULL, ints);
645 if(sym53c416_base_1)
647 ints[1] = sym53c416_base_1;
648 ints[2] = sym53c416_irq_1;
649 sym53c416_setup(NULL, ints);
651 if(sym53c416_base_2)
653 ints[1] = sym53c416_base_2;
654 ints[2] = sym53c416_irq_2;
655 sym53c416_setup(NULL, ints);
657 if(sym53c416_base_3)
659 ints[1] = sym53c416_base_3;
660 ints[2] = sym53c416_irq_3;
661 sym53c416_setup(NULL, ints);
663 #endif
665 printk("sym53c416.c: %s\n", VERSION_STRING);
667 sym53c416_probe();
669 /* Now we register and set up each host adapter found... */
670 for(count = 0, i = 0; i < host_index; i++)
671 if(!sym53c416_test(hosts[i].base))
672 printk("No sym53c416 found at address 0x%03x\n", hosts[i].base);
673 else
675 if(hosts[i].irq == 0)
676 /* We don't have an irq yet, so we should probe for one */
677 if((hosts[i].irq = sym53c416_probeirq(hosts[i].base, hosts[i].scsi_id)) == 0)
678 printk("irq autoprobing failed for sym53c416 at address 0x%03x\n", hosts[i].base);
679 if(hosts[i].irq && !check_region(hosts[i].base, IO_RANGE))
681 shpnt = scsi_register(tpnt, 0);
682 save_flags(flags);
683 cli();
684 /* Request for specified IRQ */
685 if(request_irq(hosts[i].irq, sym53c416_intr_handle, 0, ID, NULL))
687 restore_flags(flags);
688 printk("Unable to assign IRQ %d\n", hosts[i].irq);
689 scsi_unregister(shpnt);
691 else
693 /* Inform the kernel of our IO range */
694 request_region(hosts[i].base, IO_RANGE, ID);
695 shpnt->unique_id = hosts[i].base;
696 shpnt->io_port = hosts[i].base;
697 shpnt->n_io_port = IO_RANGE;
698 shpnt->irq = hosts[i].irq;
699 shpnt->this_id = hosts[i].scsi_id;
700 sym53c416_init(hosts[i].base, hosts[i].scsi_id);
701 count++;
702 restore_flags(flags);
706 return count;
709 const char *sym53c416_info(struct Scsi_Host *SChost)
711 int i;
712 int base = SChost->io_port;
713 int irq = SChost->irq;
714 int scsi_id = 0;
715 int rev = inb(base + TC_HIGH);
717 for(i = 0; i < host_index; i++)
718 if(hosts[i].base == base)
719 scsi_id = hosts[i].scsi_id;
720 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");
721 return info;
724 int sym53c416_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
726 int base;
727 unsigned long flags = 0;
728 int i;
730 /* Store base register as we can have more than one controller in the system */
731 base = SCpnt->host->io_port;
732 current_command = SCpnt; /* set current command */
733 current_command->scsi_done = done; /* set ptr to done function */
734 current_command->SCp.phase = command_ph; /* currect phase is the command phase */
735 current_command->SCp.Status = 0;
736 current_command->SCp.Message = 0;
738 save_flags(flags);
739 cli();
740 outb(SCpnt->target, base + DEST_BUS_ID); /* Set scsi id target */
741 outb(FLUSH_FIFO, base + COMMAND_REG); /* Flush SCSI and PIO FIFO's */
742 /* Write SCSI command into the SCSI fifo */
743 for(i = 0; i < SCpnt->cmd_len; i++)
744 outb(SCpnt->cmnd[i], base + SCSI_FIFO);
745 /* Start selection sequence */
746 outb(SEL_WITHOUT_ATN_SEQ, base + COMMAND_REG);
747 /* Now an interrupt will be generated which we will catch in out interrupt routine */
748 restore_flags(flags);
749 return 0;
752 static void internal_done(Scsi_Cmnd *SCpnt)
754 SCpnt->SCp.Status++;
757 int sym53c416_command(Scsi_Cmnd *SCpnt)
759 sym53c416_queuecommand(SCpnt, internal_done);
760 SCpnt->SCp.Status = 0;
761 while(!SCpnt->SCp.Status)
762 barrier();
763 return SCpnt->result;
766 int sym53c416_abort(Scsi_Cmnd *SCpnt)
768 printk("sym53c416_abort\n");
770 /* We don't know how to abort for the moment */
771 return SCSI_ABORT_SNOOZE;
774 int sym53c416_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
776 int base;
777 int scsi_id = -1;
778 int i;
780 printk("sym53c416_reset\n");
781 base = SCpnt->host->io_port;
782 /* search scsi_id */
783 for(i = 0; i < host_index && scsi_id != -1; i++)
784 if(hosts[i].base == base)
785 scsi_id = hosts[i].scsi_id;
786 outb(RESET_CHIP, base + COMMAND_REG);
787 outb(NOOP | PIO_MODE, base + COMMAND_REG);
788 outb(RESET_SCSI_BUS, base + COMMAND_REG);
789 sym53c416_init(base, scsi_id);
790 return SCSI_RESET_PENDING;
793 int sym53c416_bios_param(Disk *disk, kdev_t dev, int *ip)
795 int size;
797 size = disk->capacity;
798 ip[0] = 64; /* heads */
799 ip[1] = 32; /* sectors */
800 if((ip[2] = size >> 11) > 1024) /* cylinders, test for big disk */
802 ip[0] = 255; /* heads */
803 ip[1] = 63; /* sectors */
804 ip[2] = size / (255 * 63); /* cylinders */
806 return 0;
809 /* Loadable module support */
810 #ifdef MODULE
812 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,26)
813 MODULE_AUTHOR("Lieven Willems");
814 MODULE_PARM(sym53c416, "1-2i");
815 MODULE_PARM(sym53c416_1, "1-2i");
816 MODULE_PARM(sym53c416_2, "1-2i");
817 MODULE_PARM(sym53c416_3, "1-2i");
818 #endif
820 Scsi_Host_Template driver_template = SYM53C416;
822 #include "scsi_module.c"
823 #endif