1 /*----------------------------------------------------------------*/ 3 Qlogic linux driver - work in progress. No Warranty express or implied. 4 Use at your own risk. Support Tort Reform so you won't have to read all 5 these silly disclaimers. 7 Copyright 1994, Tom Zerucha. 10 Additional Code, and much appreciated help by 14 Thanks to Eric Youngdale and Dave Hinds for loadable module and PCMCIA 15 help respectively, and for suffering through my foolishness during the 18 Reference Qlogic FAS408 Technical Manual, 53408-510-00A, May 10, 1994 19 (you can reference it, but it is incomplete and inaccurate in places) 21 Version 0.46 1/30/97 - kernel 1.2.0+ 23 Functions as standalone, loadable, and PCMCIA driver, the latter from 24 Dave Hind's PCMCIA package. 26 Redistributable under terms of the GNU Public License 29 /*----------------------------------------------------------------*/ 32 /* Set the following to 2 to use normal interrupt (active high/totempole- 33 tristate), otherwise use 0 (REQUIRED FOR PCMCIA) for active low, open 35 #define QL_INT_ACTIVE_HIGH 2 37 /* Set the following to 1 to enable the use of interrupts. Note that 0 tends 38 to be more stable, but slower (or ties up the system more) */ 41 /* Set the following to max out the speed of the PIO PseudoDMA transfers, 42 again, 0 tends to be slower, but more stable. */ 43 #define QL_TURBO_PDMA 1 45 /* This should be 1 to enable parity detection */ 46 #define QL_ENABLE_PARITY 1 48 /* This will reset all devices when the driver is initialized (during bootup). 49 The other linux drivers don't do this, but the DOS drivers do, and after 50 using DOS or some kind of crash or lockup this will bring things back 51 without requiring a cold boot. It does take some time to recover from a 52 reset, so it is slower, and I have seen timeouts so that devices weren't 53 recognized when this was set. */ 54 #define QL_RESET_AT_START 0 56 /* crystal frequency in megahertz (for offset 5 and 9) 57 Please set this for your card. Most Qlogic cards are 40 Mhz. The 58 Control Concepts ISA (not VLB) is 24 Mhz */ 62 /* DANGER! modify these at your own risk */ 63 /* SLOWCABLE can usually be reset to zero if you have a clean setup and 64 proper termination. The rest are for synchronous transfers and other 65 advanced features if your device can transfer faster than 5Mb/sec. 66 If you are really curious, email me for a quick howto until I have 71 /* config register 1 (offset 8) options */ 72 /* This needs to be set to 1 if your cabling is long or noisy */ 77 /* This will set fast (10Mhz) synchronous timing when set to 1 78 For this to have an effect, FASTCLK must also be 1 */ 81 /* This when set to 1 will set a faster sync transfer rate */ 87 /* This is the sync transfer divisor, XTALFREQ/X will be the maximum 88 achievable data rate (assuming the rest of the system is capable 95 /* This is the count of how many synchronous transfers can take place 96 i.e. how many reqs can occur before an ack is given. 97 The maximum value for this is 15, the upper bits can modify 98 REQ/ACK assertion and deassertion during synchronous transfers 99 If this is 0, the bus will only transfer asynchronously */ 101 /* for the curious, bits 7&6 control the deassertion delay in 1/2 cycles 102 of the 40Mhz clock. If FASTCLK is 1, specifying 01 (1/2) will 103 cause the deassertion to be early by 1/2 clock. Bits 5&4 control 104 the assertion delay, also in 1/2 clocks (FASTCLK is ignored here). */ 106 /*----------------------------------------------------------------*/ 108 #undef QL_INT_ACTIVE_HIGH 109 #define QL_INT_ACTIVE_HIGH 0 113 #include <linux/module.h> 119 #include <linux/blk.h>/* to get disk capacity */ 120 #include <linux/kernel.h> 121 #include <linux/string.h> 122 #include <linux/ioport.h> 123 #include <linux/sched.h> 124 #include <linux/proc_fs.h> 125 #include <linux/unistd.h> 126 #include <linux/spinlock.h> 131 #include"qlogicfas.h" 132 #include<linux/stat.h> 134 static struct proc_dir_entry proc_scsi_qlogicfas
= { 135 PROC_SCSI_QLOGICFAS
,6,"qlogicfas", 136 S_IFDIR
| S_IRUGO
| S_IXUGO
,2 139 /*----------------------------------------------------------------*/ 140 /* driver state info, local to driver */ 141 static int qbase
=0;/* Port */ 142 static int qinitid
;/* initiator ID */ 143 static int qabort
;/* Flag to cause an abort */ 144 static int qlirq
= -1;/* IRQ being used */ 145 static char qinfo
[80];/* description */ 146 static Scsi_Cmnd
*qlcmd
;/* current command being processed */ 148 static int qlcfg5
= ( XTALFREQ
<<5);/* 15625/512 */ 149 static int qlcfg6
= SYNCXFRPD
; 150 static int qlcfg7
= SYNCOFFST
; 151 static int qlcfg8
= ( SLOWCABLE
<<7) | ( QL_ENABLE_PARITY
<<4); 152 static int qlcfg9
= ( ( XTALFREQ
+4) /5); 153 static int qlcfgc
= ( FASTCLK
<<3) | ( FASTSCSI
<<4); 155 /*----------------------------------------------------------------*/ 156 /* The qlogic card uses two register maps - These macros select which one */ 157 #define REG0 ( outb( inb( qbase + 0xd ) & 0x7f , qbase + 0xd ), outb( 4 , qbase + 0xd )) 158 #define REG1 ( outb( inb( qbase + 0xd ) | 0x80 , qbase + 0xd ), outb( 0xb4 | QL_INT_ACTIVE_HIGH , qbase + 0xd )) 160 /* following is watchdog timeout in microseconds */ 161 #define WATCHDOG 5000000 163 /*----------------------------------------------------------------*/ 164 /* the following will set the monitor border color (useful to find 165 where something crashed or gets stuck at and as a simple profiler) */ 168 #define rtrc(i) {inb(0x3da);outb(0x31,0x3c0);outb((i),0x3c0);} 173 /*----------------------------------------------------------------*/ 174 /* local functions */ 175 /*----------------------------------------------------------------*/ 176 static voidql_zap(void); 177 /* error recovery - reset everything */ 186 outb(3, qbase
+3);/* reset SCSI */ 187 outb(2, qbase
+3);/* reset chip */ 190 restore_flags( flags
); 193 /*----------------------------------------------------------------*/ 195 static intql_pdma(int phase
,char*request
,int reqlen
) 199 if(phase
&1) {/* in */ 202 /* empty fifo in large chunks */ 203 if( reqlen
>=128&& (inb( qbase
+8) &2) ) {/* full */ 204 insl( qbase
+4, request
,32); 208 while( reqlen
>=84&& !( j
&0xc0) )/* 2/3 */ 209 if( (j
=inb( qbase
+8)) &4) { 210 insl( qbase
+4, request
,21); 214 if( reqlen
>=44&& (inb( qbase
+8) &8) ) {/* 1/3 */ 215 insl( qbase
+4, request
,11); 220 /* until both empty and int (or until reclen is 0) */ 223 while( reqlen
&& !( (j
&0x10) && (j
&0xc0) ) ) { 224 /* while bytes to receive and not empty */ 226 while( reqlen
&& !( (j
=inb(qbase
+8)) &0x10) ) { 227 *request
++ =inb(qbase
+4); 238 if( reqlen
>=128&&inb( qbase
+8) &0x10) {/* empty */ 239 outsl(qbase
+4, request
,32); 243 while( reqlen
>=84&& !( j
&0xc0) )/* 1/3 */ 244 if( !((j
=inb( qbase
+8)) &8) ) { 245 outsl( qbase
+4, request
,21); 249 if( reqlen
>=40&& !(inb( qbase
+8) &4) ) {/* 2/3 */ 250 outsl( qbase
+4, request
,10); 255 /* until full and int (or until reclen is 0) */ 258 while( reqlen
&& !( (j
&2) && (j
&0xc0) ) ) { 259 /* while bytes to send and not full */ 260 while( reqlen
&& !( (j
=inb(qbase
+8)) &2) ) { 261 outb(*request
++, qbase
+4); 268 /* maybe return reqlen */ 269 returninb( qbase
+8) &0xc0; 272 /*----------------------------------------------------------------*/ 273 /* wait for interrupt flag (polled - not real hardware interrupt) */ 274 static intql_wai(void) 278 i
= jiffies
+ WATCHDOG
; 279 while( i
> jiffies
&& !qabort
&& !((k
=inb(qbase
+4)) &0xe0)) 282 return(DID_TIME_OUT
); 284 return(qabort
==1? DID_ABORT
: DID_RESET
); 294 /*----------------------------------------------------------------*/ 295 /* initiate scsi command - queueing handler */ 296 static voidql_icmd(Scsi_Cmnd
* cmd
) 306 /* clearing of interrupts and the fifo is needed */ 307 inb(qbase
+5);/* clear interrupts */ 308 if(inb(qbase
+5))/* if still interrupting */ 309 outb(2, qbase
+3);/* reset chip */ 310 else if(inb(qbase
+7) &0x1f) 311 outb(1, qbase
+3);/* clear fifo */ 312 while(inb(qbase
+5));/* clear ints */ 314 outb(1, qbase
+8);/* set for PIO pseudo DMA */ 315 outb(0, qbase
+0xb);/* disable ints */ 316 inb(qbase
+8);/* clear int bits */ 318 outb(0x40, qbase
+0xb);/* enable features */ 321 outb( qlcfgc
, qbase
+0xc); 322 /* config: no reset interrupt, (initiator) bus id */ 323 outb(0x40| qlcfg8
| qinitid
, qbase
+8); 324 outb( qlcfg7
, qbase
+7); 325 outb( qlcfg6
, qbase
+6); 327 outb(qlcfg5
, qbase
+5);/* select timer */ 328 outb(qlcfg9
&7, qbase
+9);/* prescaler */ 329 /* outb(0x99, qbase + 5); */ 330 outb(cmd
->target
, qbase
+4); 332 for(i
=0; i
< cmd
->cmd_len
; i
++) 333 outb(cmd
->cmnd
[i
], qbase
+2); 335 outb(0x41, qbase
+3);/* select and send command */ 336 restore_flags( flags
); 338 /*----------------------------------------------------------------*/ 339 /* process scsi command - usually after interrupt */ 340 static unsigned intql_pcmd(Scsi_Cmnd
* cmd
) 342 unsigned int i
, j
, k
; 343 unsigned int result
;/* ultimate return result */ 344 unsigned int status
;/* scsi returned status */ 345 unsigned int message
;/* scsi returned message */ 346 unsigned int phase
;/* recorded scsi phase */ 347 unsigned int reqlen
;/* total length of transfer */ 348 struct scatterlist
*sglist
;/* scatter-gather list pointer */ 349 unsigned int sgcount
;/* sg counter */ 355 return(DID_NO_CONNECT
<<16); 357 i
|=inb(qbase
+5);/* the 0x10 bit can be set after the 0x08 */ 359 printk("Ql:Bad Interrupt status:%02x\n", i
); 361 return(DID_BAD_INTR
<<16); 363 j
&=7;/* j = inb( qbase + 7 ) >> 5; */ 364 /* correct status is supposed to be step 4 */ 365 /* it sometimes returns step 3 but with 0 bytes left to send */ 366 /* We can try stuffing the FIFO with the max each time, but we will get a 367 sequence of 3 if any bytes are left (but we do flush the FIFO anyway */ 369 printk("Ql:Bad sequence for command %d, int %02X, cmdleft = %d\n", j
, i
,inb( qbase
+7) &0x1f); 371 return(DID_ERROR
<<16); 374 if(inb(qbase
+7) &0x1f)/* if some bytes in fifo */ 375 outb(1, qbase
+3);/* clear fifo */ 376 /* note that request_bufflen is the total xfer size when sg is used */ 377 reqlen
= cmd
->request_bufflen
; 378 /* note that it won't work if transfers > 16M are requested */ 379 if(reqlen
&& !((phase
=inb(qbase
+4)) &6)) {/* data phase */ 381 outb(reqlen
, qbase
);/* low-mid xfer cnt */ 382 outb(reqlen
>>8, qbase
+1);/* low-mid xfer cnt */ 383 outb(reqlen
>>16, qbase
+0xe);/* high xfer cnt */ 384 outb(0x90, qbase
+3);/* command do xfer */ 385 /* PIO pseudo DMA to buffer or sglist */ 388 ql_pdma(phase
, cmd
->request_buffer
, cmd
->request_bufflen
); 390 sgcount
= cmd
->use_sg
; 391 sglist
= cmd
->request_buffer
; 395 return((qabort
==1? DID_ABORT
: DID_RESET
) <<16); 397 if(ql_pdma(phase
, sglist
->address
, sglist
->length
)) 404 /* wait for irq (split into second state of irq handler if this can take time) */ 407 k
=inb(qbase
+5);/* should be 0x10, bus service */ 409 /*** Enter Status (and Message In) Phase ***/ 410 k
= jiffies
+ WATCHDOG
; 411 while( k
> jiffies
&& !qabort
&& !(inb(qbase
+4) &6));/* wait for status phase */ 414 return(DID_TIME_OUT
<<16); 416 while(inb(qbase
+5));/* clear pending ints */ 418 return((qabort
==1? DID_ABORT
: DID_RESET
) <<16); 419 outb(0x11, qbase
+3);/* get status and message */ 422 i
=inb(qbase
+5);/* get chip irq stat */ 423 j
=inb(qbase
+7) &0x1f;/* and bytes rec'd */ 424 status
=inb(qbase
+2); 425 message
=inb(qbase
+2); 426 /* should get function complete int if Status and message, else bus serv if only status */ 427 if(!((i
==8&& j
==2) || (i
==0x10&& j
==1))) { 428 printk("Ql:Error during status phase, int=%02X, %d bytes recd\n", i
, j
); 431 outb(0x12, qbase
+3);/* done, disconnect */ 435 /* should get bus service interrupt and disconnect interrupt */ 436 i
=inb(qbase
+5);/* should be bus service */ 437 while(!qabort
&& ((i
&0x20) !=0x20)) { 443 return((qabort
==1? DID_ABORT
: DID_RESET
) <<16); 444 return(result
<<16) | (message
<<8) | (status
& STATUS_MASK
); 448 /*----------------------------------------------------------------*/ 449 /* interrupt handler */ 450 static voidql_ihandl(int irq
,void*dev_id
,struct pt_regs
* regs
) 454 if(!(inb(qbase
+4) &0x80))/* false alarm? */ 456 if(qlcmd
== NULL
) {/* no command to process? */ 459 while(i
-- &&inb(qbase
+5));/* maybe also ql_zap() */ 463 icmd
->result
=ql_pcmd(icmd
); 465 /* if result is CHECK CONDITION done calls qcommand to request sense */ 466 (icmd
->scsi_done
) (icmd
); 469 static voiddo_ql_ihandl(int irq
,void*dev_id
,struct pt_regs
* regs
) 473 spin_lock_irqsave(&io_request_lock
, flags
); 474 ql_ihandl(irq
, dev_id
, regs
); 475 spin_unlock_irqrestore(&io_request_lock
, flags
); 479 /*----------------------------------------------------------------*/ 480 /* global functions */ 481 /*----------------------------------------------------------------*/ 482 /* non queued command */ 484 static voidqlidone(Scsi_Cmnd
* cmd
) {};/* null function */ 487 /* command process */ 488 intqlogicfas_command(Scsi_Cmnd
* cmd
) 493 qlogicfas_queuecommand(cmd
, qlidone
); 494 while(qlcmd
!= NULL
); 498 /* non-irq version */ 499 if(cmd
->target
== qinitid
) 500 return(DID_BAD_TARGET
<<16); 509 /*----------------------------------------------------------------*/ 511 intqlogicfas_queuecommand(Scsi_Cmnd
* cmd
,void(*done
) (Scsi_Cmnd
*)) 513 if(cmd
->target
== qinitid
) { 514 cmd
->result
= DID_BAD_TARGET
<<16; 519 cmd
->scsi_done
= done
; 520 /* wait for the last command's interrupt to finish */ 527 intqlogicfas_queuecommand(Scsi_Cmnd
* cmd
,void(*done
) (Scsi_Cmnd
*)) 534 /*----------------------------------------------------------------*/ 535 /* allow PCMCIA code to preset the port */ 536 /* port should be 0 and irq to -1 respectively for autoprobing */ 537 voidqlogicfas_preset(int port
,int irq
) 544 /*----------------------------------------------------------------*/ 545 /* look for qlogic card and init if found */ 546 intqlogicfas_detect(Scsi_Host_Template
* host
) 548 int i
, j
;/* these are only used by IRQ detect */ 549 int qltyp
;/* type of chip */ 550 struct Scsi_Host
*hreg
;/* registered host structure */ 553 host
->proc_dir
= &proc_scsi_qlogicfas
; 555 /* Qlogic Cards only exist at 0x230 or 0x330 (the chip itself decodes the 556 address - I check 230 first since MIDI cards are typically at 330 558 Theoretically, two Qlogic cards can coexist in the same system. This 559 should work by simply using this as a loadable module for the second 560 card, but I haven't tested this. 564 for(qbase
=0x230; qbase
<0x430; qbase
+=0x100) { 565 if(check_region( qbase
,0x10) ) 568 if( ( (inb(qbase
+0xe) ^inb(qbase
+0xe)) ==7) 569 && ( (inb(qbase
+0xe) ^inb(qbase
+0xe)) ==7) ) 576 printk("Ql: Using preset base address of %03x\n", qbase
); 578 qltyp
=inb(qbase
+0xe) &0xf8; 579 qinitid
= host
->this_id
; 581 qinitid
=7;/* if no ID, use 7 */ 582 outb(1, qbase
+8);/* set for PIO pseudo DMA */ 584 outb(0x40| qlcfg8
| qinitid
, qbase
+8);/* (ini) bus id, disable scsi rst */ 585 outb(qlcfg5
, qbase
+5);/* select timer */ 586 outb(qlcfg9
, qbase
+9);/* prescaler */ 587 #if QL_RESET_AT_START 590 while(inb( qbase
+0xf) &4); 594 /* IRQ probe - toggle pin and check request pending */ 601 outb(0x90, qbase
+3);/* illegal command - cause interrupt */ 603 outb(10,0x20);/* access pending interrupt map */ 606 outb(0xb0| QL_INT_ACTIVE_HIGH
, qbase
+0xd);/* int pin off */ 607 i
&= ~(inb(0x20) | (inb(0xa0) <<8));/* find IRQ off */ 608 outb(0xb4| QL_INT_ACTIVE_HIGH
, qbase
+0xd);/* int pin on */ 609 i
&=inb(0x20) | (inb(0xa0) <<8);/* find IRQ on */ 612 while(inb(qbase
+5));/* purge int */ 614 while(i
)/* find on bit */ 615 i
>>=1, j
++;/* should check for exactly 1 on */ 617 restore_flags( flags
); 620 printk("Ql: Using preset IRQ %d\n", qlirq
); 622 if(qlirq
>=0&& !request_irq(qlirq
, do_ql_ihandl
,0,"qlogicfas", NULL
)) 625 request_region( qbase
,0x10,"qlogicfas"); 626 hreg
=scsi_register( host
,0);/* no host data */ 627 hreg
->io_port
= qbase
; 629 hreg
->dma_channel
= -1; 633 sprintf(qinfo
,"Qlogicfas Driver version 0.46, chip %02X at %03X, IRQ %d, TPdma:%d", 634 qltyp
, qbase
, qlirq
, QL_TURBO_PDMA
); 640 /*----------------------------------------------------------------*/ 641 /* return bios parameters */ 642 intqlogicfas_biosparam(Disk
* disk
, kdev_t dev
,int ip
[]) 644 /* This should mimic the DOS Qlogic driver's behavior exactly */ 647 ip
[2] = disk
->capacity
/ (ip
[0] * ip
[1]); 651 ip
[2] = disk
->capacity
/ (ip
[0] * ip
[1]); 658 /*----------------------------------------------------------------*/ 659 /* abort command in progress */ 660 intqlogicfas_abort(Scsi_Cmnd
* cmd
) 667 /*----------------------------------------------------------------*/ 669 intqlogicfas_reset(Scsi_Cmnd
* cmd
,unsigned int ignored
) 676 /*----------------------------------------------------------------*/ 677 /* return info string */ 678 const char*qlogicfas_info(struct Scsi_Host
* host
) 684 /* Eventually this will go into an include file, but this will be later */ 685 Scsi_Host_Template driver_template
= QLOGICFAS
; 687 #include"scsi_module.c"