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 * Revisions 1.20 Mar-27-2000
33 * - Added support for dynamic DMA
35 ****************************************************************************/
36 #define PCI2000_VERSION "1.20"
38 #include <linux/blkdev.h>
39 #include <linux/interrupt.h>
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/types.h>
43 #include <linux/string.h>
44 #include <linux/pci.h>
45 #include <linux/ioport.h>
46 #include <linux/delay.h>
47 #include <linux/sched.h>
48 #include <linux/proc_fs.h>
49 #include <linux/stat.h>
50 #include <linux/spinlock.h>
53 #include <asm/system.h>
57 #include <scsi/scsi_host.h>
66 #define STOP_HERE {int st;for(st=0;st<100;st++){st=1;}}
76 } SCATGATH, *PSCATGATH;
82 dma_addr_t scatGathDma;
100 DEV2000 dev[MAX_BUS][MAX_UNITS];
101 } ADAPTER2000, *PADAPTER2000;
103 #define HOSTDATA(host) ((PADAPTER2000)&host->hostdata)
104 #define consistentLen (MAX_BUS * MAX_UNITS * (16 * sizeof (SCATGATH) + MAX_COMMAND_SIZE))
107 static struct Scsi_Host *PsiHost[MAXADAPTER] = {NULL,}; // One for each adapter
108 static int NumAdapters = 0;
109 /****************************************************************
110 * Name: WaitReady :LOCAL
112 * Description: Wait for controller ready.
114 * Parameters: padapter - Pointer adapter data structure.
116 * Returns: TRUE on not ready.
118 ****************************************************************/
119 static int WaitReady (PADAPTER2000 padapter)
123 for ( z = 0; z < (TIMEOUT_COMMAND * 4); z++ )
125 if ( !inb_p (padapter->cmd) )
131 /****************************************************************
132 * Name: WaitReadyLong :LOCAL
134 * Description: Wait for controller ready.
136 * Parameters: padapter - Pointer adapter data structure.
138 * Returns: TRUE on not ready.
140 ****************************************************************/
141 static int WaitReadyLong (PADAPTER2000 padapter)
145 for ( z = 0; z < (5000 * 4); z++ )
147 if ( !inb_p (padapter->cmd) )
153 /****************************************************************
154 * Name: OpDone :LOCAL
156 * Description: Clean up operation and issue done to caller.
158 * Parameters: SCpnt - Pointer to SCSI command structure.
159 * status - Caller status.
163 ****************************************************************/
164 static void OpDone (Scsi_Cmnd *SCpnt, ULONG status)
166 SCpnt->result = status;
167 SCpnt->scsi_done (SCpnt);
169 /****************************************************************
170 * Name: Command :LOCAL
172 * Description: Issue queued command to the PCI-2000.
174 * Parameters: padapter - Pointer to adapter information structure.
175 * cmd - PCI-2000 command byte.
177 * Returns: Non-zero command tag if operation is accepted.
179 ****************************************************************/
180 static UCHAR Command (PADAPTER2000 padapter, UCHAR cmd)
182 outb_p (cmd, padapter->cmd);
183 if ( WaitReady (padapter) )
186 if ( inw_p (padapter->mb0) )
189 return inb_p (padapter->mb1);
191 /****************************************************************
192 * Name: BuildSgList :LOCAL
194 * Description: Build the scatter gather list for controller.
196 * Parameters: SCpnt - Pointer to SCSI command structure.
197 * padapter - Pointer to adapter information structure.
198 * pdev - Pointer to adapter device structure.
200 * Returns: Non-zero in not scatter gather.
202 ****************************************************************/
203 static int BuildSgList (Scsi_Cmnd *SCpnt, PADAPTER2000 padapter, PDEV2000 pdev)
207 struct scatterlist *sg;
211 sg = (struct scatterlist *)SCpnt->request_buffer;
212 zc = pci_map_sg (padapter->pdev, sg, SCpnt->use_sg, scsi_to_pci_dma_dir (SCpnt->sc_data_direction));
213 for ( z = 0; z < zc; z++ )
215 pdev->scatGath[z].address = cpu_to_le32 (sg_dma_address (sg));
216 pdev->scatGath[z].length = cpu_to_le32 (sg_dma_len (sg++));
218 outl (pdev->scatGathDma, padapter->mb2);
219 outl ((zc << 24) | SCpnt->request_bufflen, padapter->mb3);
222 if ( !SCpnt->request_bufflen)
224 outl (0, padapter->mb2);
225 outl (0, padapter->mb3);
228 SCpnt->SCp.have_data_in = pci_map_single (padapter->pdev, SCpnt->request_buffer, SCpnt->request_bufflen, scsi_to_pci_dma_dir (SCpnt->sc_data_direction));
229 outl (SCpnt->SCp.have_data_in, padapter->mb2);
230 outl (SCpnt->request_bufflen, padapter->mb3);
233 /*********************************************************************
236 * Description: Execute a simple command.
238 * Parameters: padapter - Pointer to adapter control structure.
239 * cmd - Roy command byte.
241 * Returns: Return error status.
243 ********************************************************************/
244 static int PsiRaidCmd (PADAPTER2000 padapter, char cmd)
246 if ( WaitReady (padapter) ) // test for command register ready
248 outb_p (cmd, padapter->cmd); // issue command
249 if ( WaitReadyLong (padapter) ) // wait for adapter ready
253 /****************************************************************
254 * Name: Irq_Handler :LOCAL
256 * Description: Interrupt handler.
258 * Parameters: irq - Hardware IRQ number.
262 * Returns: TRUE if drive is not ready in time.
264 ****************************************************************/
265 static irqreturn_t Irq_Handler (int irq, void *dev_id, struct pt_regs *regs)
267 struct Scsi_Host *shost = NULL; // Pointer to host data block
268 PADAPTER2000 padapter; // Pointer to adapter control structure
280 DEB(printk ("\npci2000 received interrupt "));
281 for ( z = 0; z < NumAdapters; z++ ) // scan for interrupt to process
283 if ( PsiHost[z]->irq == (UCHAR)(irq & 0xFF) )
285 tag = inb_p (HOSTDATA(PsiHost[z])->tag);
296 DEB (printk ("\npci2000: not my interrupt"));
301 spin_lock_irqsave(shost->host_lock, flags);
302 padapter = HOSTDATA(shost);
304 tag0 = tag & 0x7F; // mask off the error bit
305 for ( bus = 0; bus < MAX_BUS; bus++ ) // scan the busses
307 for ( pun = 0; pun < MAX_UNITS; pun++ ) // scan the targets
309 pdev = &padapter->dev[bus][pun];
312 if ( pdev->tag == tag0 ) // is this it?
321 outb_p (0xFF, padapter->tag); // clear the op interrupt
322 outb_p (CMD_DONE, padapter->cmd); // complete the op
323 goto irq_return; // done, but, with what?
328 switch ( SCpnt->cmnd[0] )
330 case SCSIOP_TEST_UNIT_READY:
331 pci_unmap_single (padapter->pdev, SCpnt->SCp.have_data_in, sizeof (SCpnt->sense_buffer), PCI_DMA_FROMDEVICE);
333 case SCSIOP_READ_CAPACITY:
334 pci_unmap_single (padapter->pdev, SCpnt->SCp.have_data_in, 8, PCI_DMA_FROMDEVICE);
337 case SCSIOP_START_STOP_UNIT:
338 case SCSIOP_MEDIUM_REMOVAL:
342 if ( SCpnt->SCp.have_data_in )
343 pci_unmap_single (padapter->pdev, SCpnt->SCp.have_data_in, SCpnt->request_bufflen, scsi_to_pci_dma_dir(SCpnt->sc_data_direction));
347 pci_unmap_sg (padapter->pdev, (struct scatterlist *)SCpnt->request_buffer, SCpnt->use_sg, scsi_to_pci_dma_dir(SCpnt->sc_data_direction));
351 if ( tag & ERR08_TAGGED ) // is there an error here?
353 if ( WaitReady (padapter) )
355 OpDone (SCpnt, DID_TIME_OUT << 16);
359 outb_p (tag0, padapter->mb0); // get real error code
360 outb_p (CMD_ERROR, padapter->cmd);
361 if ( WaitReady (padapter) ) // wait for controller to suck up the op
363 OpDone (SCpnt, DID_TIME_OUT << 16);
367 error = inl (padapter->mb0); // get error data
368 outb_p (0xFF, padapter->tag); // clear the op interrupt
369 outb_p (CMD_DONE, padapter->cmd); // complete the op
371 DEB (printk ("status: %lX ", error));
372 if ( error == 0x00020002 ) // is this error a check condition?
374 if ( bus ) // are we doint SCSI commands?
376 OpDone (SCpnt, (DID_OK << 16) | 2);
379 if ( *SCpnt->cmnd == SCSIOP_TEST_UNIT_READY )
380 OpDone (SCpnt, (DRIVER_SENSE << 24) | (DID_OK << 16) | 2); // test caller we have sense data too
382 OpDone (SCpnt, DID_ERROR << 16);
385 OpDone (SCpnt, DID_ERROR << 16);
389 outb_p (0xFF, padapter->tag); // clear the op interrupt
390 outb_p (CMD_DONE, padapter->cmd); // complete the op
391 OpDone (SCpnt, DID_OK << 16);
394 spin_unlock_irqrestore(shost->host_lock, flags);
396 return IRQ_RETVAL(handled);
398 /****************************************************************
399 * Name: Pci2000_QueueCommand
401 * Description: Process a queued command from the SCSI manager.
403 * Parameters: SCpnt - Pointer to SCSI command structure.
404 * done - Pointer to done function to call.
406 * Returns: Status code.
408 ****************************************************************/
409 int Pci2000_QueueCommand (Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
411 UCHAR *cdb = (UCHAR *)SCpnt->cmnd; // Pointer to SCSI CDB
412 PADAPTER2000 padapter = HOSTDATA(SCpnt->device->host); // Pointer to adapter control structure
413 int rc = -1; // command return code
414 UCHAR bus = SCpnt->device->channel;
415 UCHAR pun = SCpnt->device->id;
416 UCHAR lun = SCpnt->device->lun;
418 PDEV2000 pdev = &padapter->dev[bus][pun];
422 printk("pci2000_queuecommand: %02X: done can't be NULL\n", *cdb);
426 SCpnt->scsi_done = done;
427 SCpnt->SCp.have_data_in = 0;
428 pdev->SCpnt = SCpnt; // Save this command data
430 if ( WaitReady (padapter) )
436 outw_p (pun | (lun << 8), padapter->mb0);
440 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]));
441 DEB (if(*cdb) printk ("\ntimeout_per_command: %d, timeout_total: %d, timeout: %d", SCpnt->timeout_per_command,
442 SCpnt->timeout_total, SCpnt->timeout));
443 outl (SCpnt->timeout_per_command, padapter->mb1);
444 outb_p (CMD_SCSI_TIMEOUT, padapter->cmd);
445 if ( WaitReady (padapter) )
451 outw_p (pun | (lun << 8), padapter->mb0);
452 outw_p (SCpnt->cmd_len << 8, padapter->mb0 + 2);
453 memcpy (pdev->cdb, cdb, MAX_COMMAND_SIZE);
455 outl (pdev->cdbDma, padapter->mb1);
456 if ( BuildSgList (SCpnt, padapter, pdev) )
459 cmd = CMD_SCSI_THRU_SG;
460 if ( (pdev->tag = Command (padapter, cmd)) == 0 )
475 case SCSIOP_INQUIRY: // inquiry CDB
476 if ( cdb[2] == SC_MY_RAID )
480 case MY_SCSI_REBUILD:
481 OpDone (SCpnt, PsiRaidCmd (padapter, CMD_RAID_REBUILD) << 16);
483 case MY_SCSI_ALARMMUTE:
484 OpDone (SCpnt, PsiRaidCmd (padapter, CMD_RAID_MUTE) << 16);
486 case MY_SCSI_DEMOFAIL:
487 OpDone (SCpnt, PsiRaidCmd (padapter, CMD_RAID_FAIL) << 16);
497 SCpnt->SCp.have_data_in = pci_map_single (padapter->pdev, SCpnt->request_buffer, SCpnt->request_bufflen,
498 scsi_to_pci_dma_dir(SCpnt->sc_data_direction));
499 outl (SCpnt->SCp.have_data_in, padapter->mb2);
501 outl (cdb[5], padapter->mb0);
502 outl (cdb[3], padapter->mb3);
503 cmd = CMD_DASD_RAID_RQ;
511 SCpnt->SCp.have_data_in = pci_map_single (padapter->pdev,
512 ((struct scatterlist *)SCpnt->request_buffer)->address,
513 SCpnt->request_bufflen,
514 scsi_to_pci_dma_dir (SCpnt->sc_data_direction));
518 SCpnt->SCp.have_data_in = pci_map_single (padapter->pdev, SCpnt->request_buffer,
519 SCpnt->request_bufflen,
520 scsi_to_pci_dma_dir (SCpnt->sc_data_direction));
522 outl (SCpnt->SCp.have_data_in, padapter->mb2);
523 outl (SCpnt->request_bufflen, padapter->mb3);
524 cmd = CMD_DASD_SCSI_INQ;
527 case SCSIOP_TEST_UNIT_READY: // test unit ready CDB
528 SCpnt->SCp.have_data_in = pci_map_single (padapter->pdev, SCpnt->sense_buffer, sizeof (SCpnt->sense_buffer), PCI_DMA_FROMDEVICE);
529 outl (SCpnt->SCp.have_data_in, padapter->mb2);
530 outl (sizeof (SCpnt->sense_buffer), padapter->mb3);
531 cmd = CMD_TEST_READY;
534 case SCSIOP_READ_CAPACITY: // read capacity CDB
537 SCpnt->SCp.have_data_in = pci_map_single (padapter->pdev, ((struct scatterlist *)(SCpnt->request_buffer))->address,
538 8, PCI_DMA_FROMDEVICE);
541 SCpnt->SCp.have_data_in = pci_map_single (padapter->pdev, SCpnt->request_buffer, 8, PCI_DMA_FROMDEVICE);
542 outl (SCpnt->SCp.have_data_in, padapter->mb2);
543 outl (8, padapter->mb3);
546 case SCSIOP_VERIFY: // verify CDB
547 outw_p ((USHORT)cdb[8] | ((USHORT)cdb[7] << 8), padapter->mb0 + 2);
548 outl (XSCSI2LONG (&cdb[2]), padapter->mb1);
551 case SCSIOP_READ: // read10 CDB
552 outw_p ((USHORT)cdb[8] | ((USHORT)cdb[7] << 8), padapter->mb0 + 2);
553 outl (XSCSI2LONG (&cdb[2]), padapter->mb1);
554 if ( BuildSgList (SCpnt, padapter, pdev) )
559 case SCSIOP_READ6: // read6 CDB
560 outw_p (cdb[4], padapter->mb0 + 2);
561 outl ((SCSI2LONG (&cdb[1])) & 0x001FFFFF, padapter->mb1);
562 if ( BuildSgList (SCpnt, padapter, pdev) )
567 case SCSIOP_WRITE: // write10 CDB
568 outw_p ((USHORT)cdb[8] | ((USHORT)cdb[7] << 8), padapter->mb0 + 2);
569 outl (XSCSI2LONG (&cdb[2]), padapter->mb1);
570 if ( BuildSgList (SCpnt, padapter, pdev) )
575 case SCSIOP_WRITE6: // write6 CDB
576 outw_p (cdb[4], padapter->mb0 + 2);
577 outl ((SCSI2LONG (&cdb[1])) & 0x001FFFFF, padapter->mb1);
578 if ( BuildSgList (SCpnt, padapter, pdev) )
583 case SCSIOP_START_STOP_UNIT:
584 cmd = CMD_EJECT_MEDIA;
586 case SCSIOP_MEDIUM_REMOVAL:
590 cmd = CMD_UNLOCK_DOOR;
602 DEB (printk ("pci2000_queuecommand: Unsupported command %02X\n", *cdb));
603 OpDone (SCpnt, DID_ERROR << 16);
607 if ( (pdev->tag = Command (padapter, cmd)) == 0 )
611 OpDone (SCpnt, rc << 16);
614 /****************************************************************
615 * Name: Pci2000_Detect
617 * Description: Detect and initialize our boards.
619 * Parameters: tpnt - Pointer to SCSI host template structure.
621 * Returns: Number of adapters installed.
623 ****************************************************************/
624 int Pci2000_Detect (Scsi_Host_Template *tpnt)
628 struct Scsi_Host *pshost;
629 PADAPTER2000 padapter;
632 struct pci_dev *pdev = NULL;
634 dma_addr_t consistentDma;
636 while ( (pdev = pci_find_device (VENDOR_PSI, DEVICE_ROY_1, pdev)) != NULL )
638 if (pci_enable_device(pdev))
640 pshost = scsi_register (tpnt, sizeof(ADAPTER2000));
643 padapter = HOSTDATA(pshost);
645 padapter->basePort = pci_resource_start (pdev, 1);
646 DEB (printk ("\nBase Regs = %#04X", padapter->basePort)); // get the base I/O port address
647 padapter->mb0 = padapter->basePort + RTR_MAILBOX; // get the 32 bit mail boxes
648 padapter->mb1 = padapter->basePort + RTR_MAILBOX + 4;
649 padapter->mb2 = padapter->basePort + RTR_MAILBOX + 8;
650 padapter->mb3 = padapter->basePort + RTR_MAILBOX + 12;
651 padapter->mb4 = padapter->basePort + RTR_MAILBOX + 16;
652 padapter->cmd = padapter->basePort + RTR_LOCAL_DOORBELL; // command register
653 padapter->tag = padapter->basePort + RTR_PCI_DOORBELL; // tag/response register
654 padapter->pdev = pdev;
656 if ( WaitReady (padapter) )
658 outb_p (0x84, padapter->mb0);
659 outb_p (CMD_SPECIFY, padapter->cmd);
660 if ( WaitReady (padapter) )
663 consistent = pci_alloc_consistent (pdev, consistentLen, &consistentDma);
666 printk ("Unable to allocate DMA memory for PCI-2000 controller.\n");
670 scsi_set_device(pshost, &pdev->dev);
671 pshost->irq = pdev->irq;
673 padapter->irqOwned = 0;
674 for ( z = 0; z < installed; z++ ) // scan for shared interrupts
676 if ( PsiHost[z]->irq == pshost->irq ) // if shared then, don't posses
679 if ( setirq ) // if not shared, posses
681 if ( request_irq (pshost->irq, Irq_Handler, SA_SHIRQ, "pci2000", padapter) < 0 )
683 if ( request_irq (pshost->irq, Irq_Handler, SA_INTERRUPT | SA_SHIRQ, "pci2000", padapter) < 0 )
685 printk ("Unable to allocate IRQ for PCI-2000 controller.\n");
686 pci_free_consistent (pdev, consistentLen, consistent, consistentDma);
690 padapter->irqOwned = pshost->irq; // set IRQ as owned
692 PsiHost[installed] = pshost; // save SCSI_HOST pointer
694 pshost->io_port = padapter->basePort;
695 pshost->n_io_port = 0xFF;
696 pshost->unique_id = padapter->basePort;
698 pshost->max_channel = 1;
700 for ( zz = 0; zz < MAX_BUS; zz++ )
701 for ( z = 0; z < MAX_UNITS; z++ )
703 padapter->dev[zz][z].tag = 0;
704 padapter->dev[zz][z].scatGath = (PSCATGATH)consistent;
705 padapter->dev[zz][z].scatGathDma = consistentDma;
706 consistent += 16 * sizeof (SCATGATH);
707 consistentDma += 16 * sizeof (SCATGATH);
708 padapter->dev[zz][z].cdb = (UCHAR *)consistent;
709 padapter->dev[zz][z].cdbDma = consistentDma;
710 consistent += MAX_COMMAND_SIZE;
711 consistentDma += MAX_COMMAND_SIZE;
714 printk("\nPSI-2000 Intelligent Storage SCSI CONTROLLER: at I/O = %lX IRQ = %d\n", padapter->basePort, pshost->irq);
715 printk("Version %s, Compiled %s %s\n\n", PCI2000_VERSION, __DATE__, __TIME__);
717 if ( ++installed < MAXADAPTER )
721 scsi_unregister (pshost);
724 NumAdapters = installed;
727 /****************************************************************
728 * Name: Pci2000_Abort
730 * Description: Process the Abort command from the SCSI manager.
732 * Parameters: SCpnt - Pointer to SCSI command structure.
734 * Returns: Allways snooze.
736 ****************************************************************/
737 int Pci2000_Abort (Scsi_Cmnd *SCpnt)
739 DEB (printk ("pci2000_abort\n"));
740 return SCSI_ABORT_SNOOZE;
742 /****************************************************************
743 * Name: Pci2000_Reset
745 * Description: Process the Reset command from the SCSI manager.
747 * Parameters: SCpnt - Pointer to SCSI command structure.
748 * flags - Flags about the reset command
750 * Returns: No active command at this time, so this means
751 * that each time we got some kind of response the
752 * last time through. Tell the mid-level code to
753 * request sense information in order to decide what
756 ****************************************************************/
757 int Pci2000_Reset (Scsi_Cmnd *SCpnt, unsigned int reset_flags)
759 return SCSI_RESET_PUNT;
761 /****************************************************************
762 * Name: Pci2000_Release
764 * Description: Release resources allocated for a single each adapter.
766 * Parameters: pshost - Pointer to SCSI command structure.
770 ****************************************************************/
771 int Pci2000_Release (struct Scsi_Host *pshost)
773 PADAPTER2000 padapter = HOSTDATA (pshost);
775 if ( padapter->irqOwned )
776 free_irq (pshost->irq, padapter);
777 pci_free_consistent (padapter->pdev, consistentLen, padapter->dev[0][0].scatGath, padapter->dev[0][0].scatGathDma);
778 release_region (pshost->io_port, pshost->n_io_port);
779 scsi_unregister(pshost);
783 /****************************************************************
784 * Name: Pci2000_BiosParam
786 * Description: Process the biosparam request from the SCSI manager to
789 * Parameters: disk - Pointer to SCSI disk structure.
790 * dev - Major/minor number from kernel.
791 * geom - Pointer to integer array to place geometry data.
795 ****************************************************************/
796 int Pci2000_BiosParam (struct scsi_device *sdev, struct block_device *dev,
797 sector_t capacity, int geom[])
799 PADAPTER2000 padapter;
801 padapter = HOSTDATA(sdev->host);
803 if ( WaitReady (padapter) )
805 outb_p (sdev->id, padapter->mb0);
806 outb_p (CMD_GET_PARMS, padapter->cmd);
807 if ( WaitReady (padapter) )
810 geom[0] = inb_p (padapter->mb2 + 3);
811 geom[1] = inb_p (padapter->mb2 + 2);
812 geom[2] = inw_p (padapter->mb2);
817 MODULE_LICENSE("Dual BSD/GPL");
819 static Scsi_Host_Template driver_template = {
820 .proc_name = "pci2000",
821 .name = "PCI-2000 SCSI Intelligent Disk Controller",
822 .detect = Pci2000_Detect,
823 .release = Pci2000_Release,
824 .queuecommand = Pci2000_QueueCommand,
825 .abort = Pci2000_Abort,
826 .reset = Pci2000_Reset,
827 .bios_param = Pci2000_BiosParam,
832 .use_clustering = DISABLE_CLUSTERING,
834 #include "scsi_module.c"