1 /****************************************************************************
2 * Perceptive Solutions, Inc. PCI-2000 device driver for Linux.
4 * pci2000.c - Linux Host Driver for PCI-2000 IntelliCache SCSI Adapters
6 * Copyright (c) 1997-1999 Perceptive Solutions, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that redistributions of source
11 * code retain the above copyright notice and this comment without
14 * Technical updates and product information at:
15 * http://www.psidisk.com
17 * Please send questions, comments, bug reports to:
18 * tech@psidisk.com Technical Support
21 * Revisions 1.10 Jan-21-1999
22 * - Fixed sign on message to reflect proper controller name.
23 * - Added support for RAID status monitoring and control.
25 * Revisions 1.11 Mar-22-1999
26 * - Fixed control timeout to not lock up the entire system if
27 * controller goes offline completely.
29 * Revisions 1.12 Mar-26-1999
30 * - Fixed spinlock and PCI configuration.
32 ****************************************************************************/
33 #define PCI2000_VERSION "1.12"
35 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/pci.h>
41 #include <linux/ioport.h>
42 #include <linux/delay.h>
43 #include <linux/sched.h>
44 #include <linux/proc_fs.h>
46 #include <asm/system.h>
48 #include <linux/blk.h>
51 #include <linux/stat.h>
56 #if LINUX_VERSION_CODE >= LINUXVERSION(2,1,95)
57 #include <linux/spinlock.h>
59 #if LINUX_VERSION_CODE < LINUXVERSION(2,1,93)
60 #include <linux/bios32.h>
63 struct proc_dir_entry Proc_Scsi_Pci2000
=
64 { PROC_SCSI_PCI2000
, 7, "pci2000", S_IFDIR
| S_IRUGO
| S_IXUGO
, 2 };
70 #define STOP_HERE {int st;for(st=0;st<100;st++){st=1;}}
80 } SCATGATH
, *PSCATGATH
;
85 SCATGATH scatGath
[16];
100 DEV2000 dev
[MAX_BUS
][MAX_UNITS
];
101 } ADAPTER2000
, *PADAPTER2000
;
103 #define HOSTDATA(host) ((PADAPTER2000)&host->hostdata)
106 static struct Scsi_Host
*PsiHost
[MAXADAPTER
] = {NULL
,}; // One for each adapter
107 static int NumAdapters
= 0;
108 /****************************************************************
109 * Name: WaitReady :LOCAL
111 * Description: Wait for controller ready.
113 * Parameters: padapter - Pointer adapter data structure.
115 * Returns: TRUE on not ready.
117 ****************************************************************/
118 static int WaitReady (PADAPTER2000 padapter
)
122 for ( z
= 0; z
< (TIMEOUT_COMMAND
* 4); z
++ )
124 if ( !inb_p (padapter
->cmd
) )
130 /****************************************************************
131 * Name: OpDone :LOCAL
133 * Description: Clean up operation and issue done to caller.
135 * Parameters: SCpnt - Pointer to SCSI command structure.
136 * status - Caller status.
140 ****************************************************************/
141 static void OpDone (Scsi_Cmnd
*SCpnt
, ULONG status
)
143 SCpnt
->result
= status
;
144 SCpnt
->scsi_done (SCpnt
);
146 /****************************************************************
147 * Name: Command :LOCAL
149 * Description: Issue queued command to the PCI-2000.
151 * Parameters: padapter - Pointer to adapter information structure.
152 * cmd - PCI-2000 command byte.
154 * Returns: Non-zero command tag if operation is accepted.
156 ****************************************************************/
157 static UCHAR
Command (PADAPTER2000 padapter
, UCHAR cmd
)
159 outb_p (cmd
, padapter
->cmd
);
160 if ( WaitReady (padapter
) )
163 if ( inw_p (padapter
->mb0
) )
166 return inb_p (padapter
->mb1
);
168 /****************************************************************
169 * Name: BuildSgList :LOCAL
171 * Description: Build the scatter gather list for controller.
173 * Parameters: SCpnt - Pointer to SCSI command structure.
174 * padapter - Pointer to adapter information structure.
175 * pdev - Pointer to adapter device structure.
177 * Returns: Non-zero in not scatter gather.
179 ****************************************************************/
180 static int BuildSgList (Scsi_Cmnd
*SCpnt
, PADAPTER2000 padapter
, PDEV2000 pdev
)
186 for ( z
= 0; z
< SCpnt
->use_sg
; z
++ )
188 pdev
->scatGath
[z
].address
= virt_to_bus (((struct scatterlist
*)SCpnt
->request_buffer
)[z
].address
);
189 pdev
->scatGath
[z
].length
= ((struct scatterlist
*)SCpnt
->request_buffer
)[z
].length
;
191 outl (virt_to_bus (pdev
->scatGath
), padapter
->mb2
);
192 outl ((SCpnt
->use_sg
<< 24) | SCpnt
->request_bufflen
, padapter
->mb3
);
195 outl (virt_to_bus (SCpnt
->request_buffer
), padapter
->mb2
);
196 outl (SCpnt
->request_bufflen
, padapter
->mb3
);
199 /*********************************************************************
202 * Description: Execute a simple command.
204 * Parameters: padapter - Pointer to adapter control structure.
205 * cmd - Roy command byte.
207 * Returns: Return error status.
209 ********************************************************************/
210 static int PsiRaidCmd (PADAPTER2000 padapter
, char cmd
)
212 if ( WaitReady (padapter
) ) // test for command register ready
214 outb_p (cmd
, padapter
->cmd
); // issue command
215 if ( WaitReady (padapter
) ) // wait for adapter ready
219 /****************************************************************
220 * Name: Irq_Handler :LOCAL
222 * Description: Interrupt handler.
224 * Parameters: irq - Hardware IRQ number.
228 * Returns: TRUE if drive is not ready in time.
230 ****************************************************************/
231 static void Irq_Handler (int irq
, void *dev_id
, struct pt_regs
*regs
)
233 struct Scsi_Host
*shost
= NULL
; // Pointer to host data block
234 PADAPTER2000 padapter
; // Pointer to adapter control structure
243 #if LINUX_VERSION_CODE < LINUXVERSION(2,1,95)
245 #else /* version >= v2.1.95 */
247 #endif /* version >= v2.1.95 */
249 #if LINUX_VERSION_CODE < LINUXVERSION(2,1,95)
250 /* Disable interrupts, if they aren't already disabled. */
253 #else /* version >= v2.1.95 */
255 * Disable interrupts, if they aren't already disabled and acquire
258 spin_lock_irqsave (&io_request_lock
, flags
);
259 #endif /* version >= v2.1.95 */
261 DEB(printk ("\npci2000 recieved interrupt "));
262 for ( z
= 0; z
< NumAdapters
; z
++ ) // scan for interrupt to process
264 if ( PsiHost
[z
]->irq
== (UCHAR
)(irq
& 0xFF) )
266 tag
= inb_p (HOSTDATA(PsiHost
[z
])->tag
);
277 DEB (printk ("\npci2000: not my interrupt"));
281 padapter
= HOSTDATA(shost
);
283 tag0
= tag
& 0x7F; // mask off the error bit
284 for ( bus
= 0; bus
< MAX_BUS
; bus
++ ) // scan the busses
286 for ( pun
= 0; pun
< MAX_UNITS
; pun
++ ) // scan the targets
288 pdev
= &padapter
->dev
[bus
][pun
];
291 if ( pdev
->tag
== tag0
) // is this it?
300 outb_p (0xFF, padapter
->tag
); // clear the op interrupt
301 outb_p (CMD_DONE
, padapter
->cmd
); // complete the op
302 goto irq_return
;; // done, but, with what?
305 if ( tag
& ERR08_TAGGED
) // is there an error here?
307 if ( WaitReady (padapter
) )
309 OpDone (SCpnt
, DID_TIME_OUT
<< 16);
313 outb_p (tag0
, padapter
->mb0
); // get real error code
314 outb_p (CMD_ERROR
, padapter
->cmd
);
315 if ( WaitReady (padapter
) ) // wait for controller to suck up the op
317 OpDone (SCpnt
, DID_TIME_OUT
<< 16);
321 error
= inl (padapter
->mb0
); // get error data
322 outb_p (0xFF, padapter
->tag
); // clear the op interrupt
323 outb_p (CMD_DONE
, padapter
->cmd
); // complete the op
325 DEB (printk ("status: %lX ", error
));
326 if ( error
== 0x00020002 ) // is this error a check condition?
328 if ( bus
) // are we doint SCSI commands?
330 OpDone (SCpnt
, (DID_OK
<< 16) | 2);
333 if ( *SCpnt
->cmnd
== SCSIOP_TEST_UNIT_READY
)
334 OpDone (SCpnt
, (DRIVER_SENSE
<< 24) | (DID_OK
<< 16) | 2); // test caller we have sense data too
336 OpDone (SCpnt
, DID_ERROR
<< 16);
339 OpDone (SCpnt
, DID_ERROR
<< 16);
343 outb_p (0xFF, padapter
->tag
); // clear the op interrupt
344 outb_p (CMD_DONE
, padapter
->cmd
); // complete the op
345 OpDone (SCpnt
, DID_OK
<< 16);
348 #if LINUX_VERSION_CODE < LINUXVERSION(2,1,95)
350 * Restore the original flags which will enable interrupts
351 * if and only if they were enabled on entry.
353 restore_flags (flags
);
354 #else /* version >= v2.1.95 */
356 * Release the I/O spinlock and restore the original flags
357 * which will enable interrupts if and only if they were
360 spin_unlock_irqrestore (&io_request_lock
, flags
);
361 #endif /* version >= v2.1.95 */
363 /****************************************************************
364 * Name: Pci2000_QueueCommand
366 * Description: Process a queued command from the SCSI manager.
368 * Parameters: SCpnt - Pointer to SCSI command structure.
369 * done - Pointer to done function to call.
371 * Returns: Status code.
373 ****************************************************************/
374 int Pci2000_QueueCommand (Scsi_Cmnd
*SCpnt
, void (*done
)(Scsi_Cmnd
*))
376 UCHAR
*cdb
= (UCHAR
*)SCpnt
->cmnd
; // Pointer to SCSI CDB
377 PADAPTER2000 padapter
= HOSTDATA(SCpnt
->host
); // Pointer to adapter control structure
378 int rc
= -1; // command return code
379 UCHAR bus
= SCpnt
->channel
;
380 UCHAR pun
= SCpnt
->target
;
381 UCHAR lun
= SCpnt
->lun
;
383 PDEV2000 pdev
= &padapter
->dev
[bus
][pun
];
387 printk("pci2000_queuecommand: %02X: done can't be NULL\n", *cdb
);
391 SCpnt
->scsi_done
= done
;
392 pdev
->SCpnt
= SCpnt
; // Save this command data
394 if ( WaitReady (padapter
) )
400 outw_p (pun
| (lun
<< 8), padapter
->mb0
);
404 DEB (if(*cdb
) printk ("\nCDB: %X- %X %X %X %X %X %X %X %X %X %X ", SCpnt
->cmd_len
, cdb
[0], cdb
[1], cdb
[2], cdb
[3], cdb
[4], cdb
[5], cdb
[6], cdb
[7], cdb
[8], cdb
[9]));
405 DEB (if(*cdb
) printk ("\ntimeout_per_command: %d, timeout_total: %d, timeout: %d, internal_timout: %d", SCpnt
->timeout_per_command
,
406 SCpnt
->timeout_total
, SCpnt
->timeout
, SCpnt
->internal_timeout
));
407 outl (SCpnt
->timeout_per_command
, padapter
->mb1
);
408 outb_p (CMD_SCSI_TIMEOUT
, padapter
->cmd
);
409 if ( WaitReady (padapter
) )
415 outw_p (pun
| (lun
<< 8), padapter
->mb0
);
416 outw_p (SCpnt
->cmd_len
<< 8, padapter
->mb0
+ 2);
417 outl (virt_to_bus (cdb
), padapter
->mb1
);
418 if ( BuildSgList (SCpnt
, padapter
, pdev
) )
421 cmd
= CMD_SCSI_THRU_SG
;
422 if ( (pdev
->tag
= Command (padapter
, cmd
)) == 0 )
437 case SCSIOP_INQUIRY
: // inquiry CDB
438 if ( cdb
[2] == SC_MY_RAID
)
442 case MY_SCSI_REBUILD
:
443 OpDone (SCpnt
, PsiRaidCmd (padapter
, CMD_RAID_REBUILD
) << 16);
445 case MY_SCSI_ALARMMUTE
:
446 OpDone (SCpnt
, PsiRaidCmd (padapter
, CMD_RAID_MUTE
) << 16);
448 case MY_SCSI_DEMOFAIL
:
449 OpDone (SCpnt
, PsiRaidCmd (padapter
, CMD_RAID_FAIL
) << 16);
458 outl (virt_to_bus (SCpnt
->request_buffer
), padapter
->mb2
);
459 outl (cdb
[5], padapter
->mb0
);
460 outl (cdb
[3], padapter
->mb3
);
461 cmd
= CMD_DASD_RAID_RQ
;
469 outl (virt_to_bus (((struct scatterlist
*)(SCpnt
->request_buffer
))->address
), padapter
->mb2
);
473 outl (virt_to_bus (SCpnt
->request_buffer
), padapter
->mb2
);
475 outl (SCpnt
->request_bufflen
, padapter
->mb3
);
476 cmd
= CMD_DASD_SCSI_INQ
;
479 case SCSIOP_TEST_UNIT_READY
: // test unit ready CDB
480 outl (virt_to_bus (SCpnt
->sense_buffer
), padapter
->mb2
);
481 outl (sizeof (SCpnt
->sense_buffer
), padapter
->mb3
);
482 cmd
= CMD_TEST_READY
;
485 case SCSIOP_READ_CAPACITY
: // read capctiy CDB
488 outl (virt_to_bus (((struct scatterlist
*)(SCpnt
->request_buffer
))->address
), padapter
->mb2
);
492 outl (virt_to_bus (SCpnt
->request_buffer
), padapter
->mb2
);
494 outl (8, padapter
->mb3
);
497 case SCSIOP_VERIFY
: // verify CDB
498 outw_p ((USHORT
)cdb
[8] | ((USHORT
)cdb
[7] << 8), padapter
->mb0
+ 2);
499 outl (XSCSI2LONG (&cdb
[2]), padapter
->mb1
);
502 case SCSIOP_READ
: // read10 CDB
503 outw_p ((USHORT
)cdb
[8] | ((USHORT
)cdb
[7] << 8), padapter
->mb0
+ 2);
504 outl (XSCSI2LONG (&cdb
[2]), padapter
->mb1
);
505 if ( BuildSgList (SCpnt
, padapter
, pdev
) )
510 case SCSIOP_READ6
: // read6 CDB
511 outw_p (cdb
[4], padapter
->mb0
+ 2);
512 outl ((SCSI2LONG (&cdb
[1])) & 0x001FFFFF, padapter
->mb1
);
513 if ( BuildSgList (SCpnt
, padapter
, pdev
) )
518 case SCSIOP_WRITE
: // write10 CDB
519 outw_p ((USHORT
)cdb
[8] | ((USHORT
)cdb
[7] << 8), padapter
->mb0
+ 2);
520 outl (XSCSI2LONG (&cdb
[2]), padapter
->mb1
);
521 if ( BuildSgList (SCpnt
, padapter
, pdev
) )
526 case SCSIOP_WRITE6
: // write6 CDB
527 outw_p (cdb
[4], padapter
->mb0
+ 2);
528 outl ((SCSI2LONG (&cdb
[1])) & 0x001FFFFF, padapter
->mb1
);
529 if ( BuildSgList (SCpnt
, padapter
, pdev
) )
534 case SCSIOP_START_STOP_UNIT
:
535 cmd
= CMD_EJECT_MEDIA
;
537 case SCSIOP_MEDIUM_REMOVAL
:
541 cmd
= CMD_UNLOCK_DOOR
;
553 DEB (printk ("pci2000_queuecommand: Unsupported command %02X\n", *cdb
));
554 OpDone (SCpnt
, DID_ERROR
<< 16);
558 if ( (pdev
->tag
= Command (padapter
, cmd
)) == 0 )
562 OpDone (SCpnt
, rc
<< 16);
565 /****************************************************************
566 * Name: internal_done :LOCAL
568 * Description: Done handler for non-queued commands
570 * Parameters: SCpnt - Pointer to SCSI command structure.
574 ****************************************************************/
575 static void internal_done (Scsi_Cmnd
* SCpnt
)
579 /****************************************************************
580 * Name: Pci2000_Command
582 * Description: Process a command from the SCSI manager.
584 * Parameters: SCpnt - Pointer to SCSI command structure.
586 * Returns: Status code.
588 ****************************************************************/
589 int Pci2000_Command (Scsi_Cmnd
*SCpnt
)
591 DEB(printk("pci2000_command: ..calling pci2000_queuecommand\n"));
593 Pci2000_QueueCommand (SCpnt
, internal_done
);
595 SCpnt
->SCp
.Status
= 0;
596 while (!SCpnt
->SCp
.Status
)
598 return SCpnt
->result
;
600 /****************************************************************
601 * Name: Pci2000_Detect
603 * Description: Detect and initialize our boards.
605 * Parameters: tpnt - Pointer to SCSI host template structure.
607 * Returns: Number of adapters installed.
609 ****************************************************************/
610 int Pci2000_Detect (Scsi_Host_Template
*tpnt
)
614 struct Scsi_Host
*pshost
;
615 PADAPTER2000 padapter
;
618 #if LINUX_VERSION_CODE > LINUXVERSION(2,1,92)
619 struct pci_dev
*pdev
= NULL
;
621 UCHAR pci_bus
, pci_device_fn
;
624 #if LINUX_VERSION_CODE > LINUXVERSION(2,1,92)
625 if ( !pci_present () )
627 if ( !pcibios_present () )
630 printk ("pci2000: PCI BIOS not present\n");
634 #if LINUX_VERSION_CODE > LINUXVERSION(2,1,92)
635 while ( (pdev
= pci_find_device (VENDOR_PSI
, DEVICE_ROY_1
, pdev
)) != NULL
)
637 while ( !pcibios_find_device (VENDOR_PSI
, DEVICE_ROY_1
, found
, &pci_bus
, &pci_device_fn
) )
640 pshost
= scsi_register (tpnt
, sizeof(ADAPTER2000
));
641 padapter
= HOSTDATA(pshost
);
643 padapter
->basePort
= pdev
->resource
[1].start
;
645 DEB (printk ("\nBase Regs = %#04X", padapter
->basePort
)); // get the base I/O port address
646 padapter
->mb0
= padapter
->basePort
+ RTR_MAILBOX
; // get the 32 bit mail boxes
647 padapter
->mb1
= padapter
->basePort
+ RTR_MAILBOX
+ 4;
648 padapter
->mb2
= padapter
->basePort
+ RTR_MAILBOX
+ 8;
649 padapter
->mb3
= padapter
->basePort
+ RTR_MAILBOX
+ 12;
650 padapter
->mb4
= padapter
->basePort
+ RTR_MAILBOX
+ 16;
651 padapter
->cmd
= padapter
->basePort
+ RTR_LOCAL_DOORBELL
; // command register
652 padapter
->tag
= padapter
->basePort
+ RTR_PCI_DOORBELL
; // tag/response register
654 if ( WaitReady (padapter
) )
656 outb_p (0x84, padapter
->mb0
);
657 outb_p (CMD_SPECIFY
, padapter
->cmd
);
658 if ( WaitReady (padapter
) )
661 #if LINUX_VERSION_CODE > LINUXVERSION(2,1,92)
662 pshost
->irq
= pdev
->irq
;
664 pcibios_read_config_byte (pci_bus
, pci_device_fn
, PCI_INTERRUPT_LINE
, &pshost
->irq
);
667 padapter
->irqOwned
= 0;
668 for ( z
= 0; z
< installed
; z
++ ) // scan for shared interrupts
670 if ( PsiHost
[z
]->irq
== pshost
->irq
) // if shared then, don't posses
673 if ( setirq
) // if not shared, posses
675 if ( request_irq (pshost
->irq
, Irq_Handler
, SA_SHIRQ
, "pci2000", padapter
) < 0 )
677 if ( request_irq (pshost
->irq
, Irq_Handler
, SA_INTERRUPT
| SA_SHIRQ
, "pci2000", padapter
) < 0 )
679 printk ("Unable to allocate IRQ for PCI-2000 controller.\n");
683 padapter
->irqOwned
= pshost
->irq
; // set IRQ as owned
685 PsiHost
[installed
] = pshost
; // save SCSI_HOST pointer
687 pshost
->io_port
= padapter
->basePort
;
688 pshost
->n_io_port
= 0xFF;
689 pshost
->unique_id
= padapter
->basePort
;
691 pshost
->max_channel
= 1;
693 for ( zz
= 0; zz
< MAX_BUS
; zz
++ )
694 for ( z
= 0; z
< MAX_UNITS
; z
++ )
695 padapter
->dev
[zz
][z
].tag
= 0;
697 printk("\nPSI-2000 Intelligent Storage SCSI CONTROLLER: at I/O = %X IRQ = %d\n", padapter
->basePort
, pshost
->irq
);
698 printk("Version %s, Compiled %s %s\n\n", PCI2000_VERSION
, __DATE__
, __TIME__
);
700 if ( ++installed
< MAXADAPTER
)
704 scsi_unregister (pshost
);
707 NumAdapters
= installed
;
710 /****************************************************************
711 * Name: Pci2000_Abort
713 * Description: Process the Abort command from the SCSI manager.
715 * Parameters: SCpnt - Pointer to SCSI command structure.
717 * Returns: Allways snooze.
719 ****************************************************************/
720 int Pci2000_Abort (Scsi_Cmnd
*SCpnt
)
722 DEB (printk ("pci2000_abort\n"));
723 return SCSI_ABORT_SNOOZE
;
725 /****************************************************************
726 * Name: Pci2000_Reset
728 * Description: Process the Reset command from the SCSI manager.
730 * Parameters: SCpnt - Pointer to SCSI command structure.
731 * flags - Flags about the reset command
733 * Returns: No active command at this time, so this means
734 * that each time we got some kind of response the
735 * last time through. Tell the mid-level code to
736 * request sense information in order to decide what
739 ****************************************************************/
740 int Pci2000_Reset (Scsi_Cmnd
*SCpnt
, unsigned int reset_flags
)
742 return SCSI_RESET_PUNT
;
744 /****************************************************************
745 * Name: Pci2000_Release
747 * Description: Release resources allocated for a single each adapter.
749 * Parameters: pshost - Pointer to SCSI command structure.
753 ****************************************************************/
754 int Pci2000_Release (struct Scsi_Host
*pshost
)
756 PADAPTER2000 padapter
= HOSTDATA (pshost
);
758 if ( padapter
->irqOwned
)
759 #if LINUX_VERSION_CODE < LINUXVERSION(1,3,70)
760 free_irq (pshost
->irq
);
761 #else /* version >= v1.3.70 */
762 free_irq (pshost
->irq
, padapter
);
763 #endif /* version >= v1.3.70 */
764 release_region (pshost
->io_port
, pshost
->n_io_port
);
765 scsi_unregister(pshost
);
770 /****************************************************************
771 * Name: Pci2000_BiosParam
773 * Description: Process the biosparam request from the SCSI manager to
776 * Parameters: disk - Pointer to SCSI disk structure.
777 * dev - Major/minor number from kernel.
778 * geom - Pointer to integer array to place geometry data.
782 ****************************************************************/
783 int Pci2000_BiosParam (Scsi_Disk
*disk
, kdev_t dev
, int geom
[])
785 PADAPTER2000 padapter
;
787 padapter
= HOSTDATA(disk
->device
->host
);
789 if ( WaitReady (padapter
) )
791 outb_p (disk
->device
->id
, padapter
->mb0
);
792 outb_p (CMD_GET_PARMS
, padapter
->cmd
);
793 if ( WaitReady (padapter
) )
796 geom
[0] = inb_p (padapter
->mb2
+ 3);
797 geom
[1] = inb_p (padapter
->mb2
+ 2);
798 geom
[2] = inw_p (padapter
->mb2
);
804 /* Eventually this will go into an include file, but this will be later */
805 Scsi_Host_Template driver_template
= PCI2000
;
807 #include "scsi_module.c"