1 /***************************************************************************
5 copyright : (C) 2000 by Adaptec
7 July 30, 2001 First version being submitted
8 for inclusion in the kernel. V2.4
10 See Documentation/scsi/dpti.txt for history, notes, license info
12 ***************************************************************************/
14 /***************************************************************************
16 * This program is free software; you can redistribute it and/or modify *
17 * it under the terms of the GNU General Public License as published by *
18 * the Free Software Foundation; either version 2 of the License, or *
19 * (at your option) any later version. *
21 ***************************************************************************/
22 /***************************************************************************
23 * Sat Dec 20 2003 Go Taniguchi <go@turbolinux.co.jp>
24 - Support 2.6 kernel and DMA-mapping
25 - ioctl fix for raid tools
26 - use schedule_timeout in long long loop
27 **************************************************************************/
30 /*#define UARTDELAY 1 */
32 #include <linux/module.h>
34 MODULE_AUTHOR("Deanna Bonds, with _lots_ of help from Mark Salyzyn");
35 MODULE_DESCRIPTION("Adaptec I2O RAID Driver");
37 ////////////////////////////////////////////////////////////////
39 #include <linux/ioctl.h> /* For SCSI-Passthrough */
40 #include <asm/uaccess.h>
42 #include <linux/stat.h>
43 #include <linux/slab.h> /* for kmalloc() */
44 #include <linux/pci.h> /* for PCI support */
45 #include <linux/proc_fs.h>
46 #include <linux/blkdev.h>
47 #include <linux/delay.h> /* for udelay */
48 #include <linux/interrupt.h>
49 #include <linux/kernel.h> /* for printk */
50 #include <linux/sched.h>
51 #include <linux/reboot.h>
52 #include <linux/smp_lock.h>
53 #include <linux/spinlock.h>
54 #include <linux/dma-mapping.h>
56 #include <linux/timer.h>
57 #include <linux/string.h>
58 #include <linux/ioport.h>
59 #include <linux/mutex.h>
61 #include <asm/processor.h> /* for boot_cpu_data */
62 #include <asm/pgtable.h>
63 #include <asm/io.h> /* for virt_to_bus, etc. */
65 #include <scsi/scsi.h>
66 #include <scsi/scsi_cmnd.h>
67 #include <scsi/scsi_device.h>
68 #include <scsi/scsi_host.h>
69 #include <scsi/scsi_tcq.h>
71 #include "dpt/dptsig.h"
74 /*============================================================================
75 * Create a binary signature - this is read by dptsig
76 * Needed for our management apps
77 *============================================================================
79 static dpt_sig_S DPTI_sig = {
80 {'d', 'P', 't', 'S', 'i', 'G'}, SIG_VERSION,
82 PROC_INTEL, PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
83 #elif defined(__ia64__)
84 PROC_INTEL, PROC_IA64,
85 #elif defined(__sparc__)
86 PROC_ULTRASPARC, PROC_ULTRASPARC,
87 #elif defined(__alpha__)
88 PROC_ALPHA, PROC_ALPHA,
92 FT_HBADRVR, 0, OEM_DPT, OS_LINUX, CAP_OVERLAP, DEV_ALL,
93 ADF_ALL_SC5, 0, 0, DPT_VERSION, DPT_REVISION, DPT_SUBREVISION,
94 DPT_MONTH, DPT_DAY, DPT_YEAR, "Adaptec Linux I2O RAID Driver"
100 /*============================================================================
102 *============================================================================
105 static DEFINE_MUTEX(adpt_configuration_lock);
107 static struct i2o_sys_tbl *sys_tbl;
108 static dma_addr_t sys_tbl_pa;
109 static int sys_tbl_ind;
110 static int sys_tbl_len;
112 static adpt_hba* hba_chain = NULL;
113 static int hba_count = 0;
115 static struct class *adpt_sysfs_class;
118 static long compat_adpt_ioctl(struct file *, unsigned int, unsigned long);
121 static const struct file_operations adpt_fops = {
124 .release = adpt_close,
126 .compat_ioctl = compat_adpt_ioctl,
130 /* Structures and definitions for synchronous message posting.
131 * See adpt_i2o_post_wait() for description
133 struct adpt_i2o_post_wait_data
137 adpt_wait_queue_head_t *wq;
138 struct adpt_i2o_post_wait_data *next;
141 static struct adpt_i2o_post_wait_data *adpt_post_wait_queue = NULL;
142 static u32 adpt_post_wait_id = 0;
143 static DEFINE_SPINLOCK(adpt_post_wait_lock);
146 /*============================================================================
148 *============================================================================
151 static inline int dpt_dma64(adpt_hba *pHba)
153 return (sizeof(dma_addr_t) > 4 && (pHba)->dma64);
156 static inline u32 dma_high(dma_addr_t addr)
158 return upper_32_bits(addr);
161 static inline u32 dma_low(dma_addr_t addr)
166 static u8 adpt_read_blink_led(adpt_hba* host)
168 if (host->FwDebugBLEDflag_P) {
169 if( readb(host->FwDebugBLEDflag_P) == 0xbc ){
170 return readb(host->FwDebugBLEDvalue_P);
176 /*============================================================================
177 * Scsi host template interface functions
178 *============================================================================
181 static struct pci_device_id dptids[] = {
182 { PCI_DPT_VENDOR_ID, PCI_DPT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
183 { PCI_DPT_VENDOR_ID, PCI_DPT_RAPTOR_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
186 MODULE_DEVICE_TABLE(pci,dptids);
188 static int adpt_detect(struct scsi_host_template* sht)
190 struct pci_dev *pDev = NULL;
194 PINFO("Detecting Adaptec I2O RAID controllers...\n");
196 /* search for all Adatpec I2O RAID cards */
197 while ((pDev = pci_get_device( PCI_DPT_VENDOR_ID, PCI_ANY_ID, pDev))) {
198 if(pDev->device == PCI_DPT_DEVICE_ID ||
199 pDev->device == PCI_DPT_RAPTOR_DEVICE_ID){
200 if(adpt_install_hba(sht, pDev) ){
201 PERROR("Could not Init an I2O RAID device\n");
202 PERROR("Will not try to detect others.\n");
209 /* In INIT state, Activate IOPs */
210 for (pHba = hba_chain; pHba; pHba = next) {
212 // Activate does get status , init outbound, and get hrt
213 if (adpt_i2o_activate_hba(pHba) < 0) {
214 adpt_i2o_delete_hba(pHba);
219 /* Active IOPs in HOLD state */
222 if (hba_chain == NULL)
226 * If build_sys_table fails, we kill everything and bail
227 * as we can't init the IOPs w/o a system table
229 if (adpt_i2o_build_sys_table() < 0) {
230 adpt_i2o_sys_shutdown();
234 PDEBUG("HBA's in HOLD state\n");
236 /* If IOP don't get online, we need to rebuild the System table */
237 for (pHba = hba_chain; pHba; pHba = pHba->next) {
238 if (adpt_i2o_online_hba(pHba) < 0) {
239 adpt_i2o_delete_hba(pHba);
240 goto rebuild_sys_tab;
244 /* Active IOPs now in OPERATIONAL state */
245 PDEBUG("HBA's in OPERATIONAL state\n");
247 printk("dpti: If you have a lot of devices this could take a few minutes.\n");
248 for (pHba = hba_chain; pHba; pHba = next) {
250 printk(KERN_INFO"%s: Reading the hardware resource table.\n", pHba->name);
251 if (adpt_i2o_lct_get(pHba) < 0){
252 adpt_i2o_delete_hba(pHba);
256 if (adpt_i2o_parse_lct(pHba) < 0){
257 adpt_i2o_delete_hba(pHba);
263 adpt_sysfs_class = class_create(THIS_MODULE, "dpt_i2o");
264 if (IS_ERR(adpt_sysfs_class)) {
265 printk(KERN_WARNING"dpti: unable to create dpt_i2o class\n");
266 adpt_sysfs_class = NULL;
269 for (pHba = hba_chain; pHba; pHba = next) {
271 if (adpt_scsi_host_alloc(pHba, sht) < 0){
272 adpt_i2o_delete_hba(pHba);
275 pHba->initialized = TRUE;
276 pHba->state &= ~DPTI_STATE_RESET;
277 if (adpt_sysfs_class) {
278 struct device *dev = device_create(adpt_sysfs_class,
279 NULL, MKDEV(DPTI_I2O_MAJOR, pHba->unit), NULL,
280 "dpti%d", pHba->unit);
282 printk(KERN_WARNING"dpti%d: unable to "
283 "create device in dpt_i2o class\n",
289 // Register our control device node
290 // nodes will need to be created in /dev to access this
291 // the nodes can not be created from within the driver
292 if (hba_count && register_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER, &adpt_fops)) {
293 adpt_i2o_sys_shutdown();
301 * scsi_unregister will be called AFTER we return.
303 static int adpt_release(struct Scsi_Host *host)
305 adpt_hba* pHba = (adpt_hba*) host->hostdata[0];
306 // adpt_i2o_quiesce_hba(pHba);
307 adpt_i2o_delete_hba(pHba);
308 scsi_unregister(host);
313 static void adpt_inquiry(adpt_hba* pHba)
327 memset(msg, 0, sizeof(msg));
328 buf = dma_alloc_coherent(&pHba->pDev->dev, 80, &addr, GFP_KERNEL);
330 printk(KERN_ERR"%s: Could not allocate buffer\n",pHba->name);
333 memset((void*)buf, 0, 36);
336 direction = 0x00000000;
337 scsidir =0x40000000; // DATA IN (iop<--dev)
340 reqlen = 17; // SINGLE SGE, 64 bit
342 reqlen = 14; // SINGLE SGE, 32 bit
343 /* Stick the headers on */
344 msg[0] = reqlen<<16 | SGL_OFFSET_12;
345 msg[1] = (0xff<<24|HOST_TID<<12|ADAPTER_TID);
348 // Adaptec/DPT Private stuff
349 msg[4] = I2O_CMD_SCSI_EXEC|DPT_ORGANIZATION_ID<<16;
350 msg[5] = ADAPTER_TID | 1<<16 /* Interpret*/;
351 /* Direction, disconnect ok | sense data | simple queue , CDBLen */
352 // I2O_SCB_FLAG_ENABLE_DISCONNECT |
353 // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
354 // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
355 msg[6] = scsidir|0x20a00000| 6 /* cmd len*/;
359 memset(scb, 0, sizeof(scb));
360 // Write SCSI command into the message - always 16 byte block
367 // Don't care about the rest of scb
369 memcpy(mptr, scb, sizeof(scb));
371 lenptr=mptr++; /* Remember me - fill in when we know */
373 /* Now fill in the SGList and command */
375 if (dpt_dma64(pHba)) {
376 *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
377 *mptr++ = 1 << PAGE_SHIFT;
378 *mptr++ = 0xD0000000|direction|len;
379 *mptr++ = dma_low(addr);
380 *mptr++ = dma_high(addr);
382 *mptr++ = 0xD0000000|direction|len;
386 // Send it on it's way
387 rcode = adpt_i2o_post_wait(pHba, msg, reqlen<<2, 120);
389 sprintf(pHba->detail, "Adaptec I2O RAID");
390 printk(KERN_INFO "%s: Inquiry Error (%d)\n",pHba->name,rcode);
391 if (rcode != -ETIME && rcode != -EINTR)
392 dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
394 memset(pHba->detail, 0, sizeof(pHba->detail));
395 memcpy(&(pHba->detail), "Vendor: Adaptec ", 16);
396 memcpy(&(pHba->detail[16]), " Model: ", 8);
397 memcpy(&(pHba->detail[24]), (u8*) &buf[16], 16);
398 memcpy(&(pHba->detail[40]), " FW: ", 4);
399 memcpy(&(pHba->detail[44]), (u8*) &buf[32], 4);
400 pHba->detail[48] = '\0'; /* precautionary */
401 dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
403 adpt_i2o_status_get(pHba);
408 static int adpt_slave_configure(struct scsi_device * device)
410 struct Scsi_Host *host = device->host;
413 pHba = (adpt_hba *) host->hostdata[0];
415 if (host->can_queue && device->tagged_supported) {
416 scsi_adjust_queue_depth(device, MSG_SIMPLE_TAG,
417 host->can_queue - 1);
419 scsi_adjust_queue_depth(device, 0, 1);
424 static int adpt_queue(struct scsi_cmnd * cmd, void (*done) (struct scsi_cmnd *))
426 adpt_hba* pHba = NULL;
427 struct adpt_device* pDev = NULL; /* dpt per device information */
429 cmd->scsi_done = done;
431 * SCSI REQUEST_SENSE commands will be executed automatically by the
432 * Host Adapter for any errors, so they should not be executed
433 * explicitly unless the Sense Data is zero indicating that no error
437 if ((cmd->cmnd[0] == REQUEST_SENSE) && (cmd->sense_buffer[0] != 0)) {
438 cmd->result = (DID_OK << 16);
443 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
450 * TODO: I need to block here if I am processing ioctl cmds
451 * but if the outstanding cmds all finish before the ioctl,
452 * the scsi-core will not know to start sending cmds to me again.
453 * I need to a way to restart the scsi-cores queues or should I block
454 * calling scsi_done on the outstanding cmds instead
455 * for now we don't set the IOCTL state
457 if(((pHba->state) & DPTI_STATE_IOCTL) || ((pHba->state) & DPTI_STATE_RESET)) {
458 pHba->host->last_reset = jiffies;
459 pHba->host->resetting = 1;
463 // TODO if the cmd->device if offline then I may need to issue a bus rescan
464 // followed by a get_lct to see if the device is there anymore
465 if((pDev = (struct adpt_device*) (cmd->device->hostdata)) == NULL) {
467 * First command request for this device. Set up a pointer
468 * to the device structure. This should be a TEST_UNIT_READY
469 * command from scan_scsis_single.
471 if ((pDev = adpt_find_device(pHba, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun)) == NULL) {
472 // TODO: if any luns are at this bus, scsi id then fake a TEST_UNIT_READY and INQUIRY response
473 // with type 7F (for all luns less than the max for this bus,id) so the lun scan will continue.
474 cmd->result = (DID_NO_CONNECT << 16);
478 cmd->device->hostdata = pDev;
480 pDev->pScsi_dev = cmd->device;
483 * If we are being called from when the device is being reset,
484 * delay processing of the command until later.
486 if (pDev->state & DPTI_DEV_RESET ) {
489 return adpt_scsi_to_i2o(pHba, cmd, pDev);
492 static int adpt_bios_param(struct scsi_device *sdev, struct block_device *dev,
493 sector_t capacity, int geom[])
499 // *** First lets set the default geometry ****
501 // If the capacity is less than ox2000
502 if (capacity < 0x2000 ) { // floppy
506 // else if between 0x2000 and 0x20000
507 else if (capacity < 0x20000) {
511 // else if between 0x20000 and 0x40000
512 else if (capacity < 0x40000) {
516 // else if between 0x4000 and 0x80000
517 else if (capacity < 0x80000) {
521 // else if greater than 0x80000
526 cylinders = sector_div(capacity, heads * sectors);
528 // Special case if CDROM
529 if(sdev->type == 5) { // CDROM
539 PDEBUG("adpt_bios_param: exit\n");
544 static const char *adpt_info(struct Scsi_Host *host)
548 pHba = (adpt_hba *) host->hostdata[0];
549 return (char *) (pHba->detail);
552 static int adpt_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
553 int length, int inout)
555 struct adpt_device* d;
567 * The user has done a write and wants us to take the
568 * data in the buffer and do something with it.
569 * proc_scsiwrite calls us with inout = 1
571 * Read data from buffer (writing to us) - NOT SUPPORTED
577 * inout = 0 means the user has done a read and wants information
578 * returned, so we write information about the cards into the buffer
579 * proc_scsiread() calls us with inout = 0
582 // Find HBA (host bus adapter) we are looking for
583 mutex_lock(&adpt_configuration_lock);
584 for (pHba = hba_chain; pHba; pHba = pHba->next) {
585 if (pHba->host == host) {
586 break; /* found adapter */
589 mutex_unlock(&adpt_configuration_lock);
595 len = sprintf(buffer , "Adaptec I2O RAID Driver Version: %s\n\n", DPT_I2O_VERSION);
596 len += sprintf(buffer+len, "%s\n", pHba->detail);
597 len += sprintf(buffer+len, "SCSI Host=scsi%d Control Node=/dev/%s irq=%d\n",
598 pHba->host->host_no, pHba->name, host->irq);
599 len += sprintf(buffer+len, "\tpost fifo size = %d\n\treply fifo size = %d\n\tsg table size = %d\n\n",
600 host->can_queue, (int) pHba->reply_fifo_size , host->sg_tablesize);
605 if(pos > offset + length) {
610 * If we haven't even written to where we last left
611 * off (the last time we were called), reset the
617 len += sprintf(buffer+len, "Devices:\n");
618 for(chan = 0; chan < MAX_CHANNEL; chan++) {
619 for(id = 0; id < MAX_ID; id++) {
620 d = pHba->channel[chan].device[id];
622 len += sprintf(buffer+len,"\t%-24.24s", d->pScsi_dev->vendor);
623 len += sprintf(buffer+len," Rev: %-8.8s\n", d->pScsi_dev->rev);
628 if(pos > offset + length) {
636 unit = d->pI2o_dev->lct_data.tid;
637 len += sprintf(buffer+len, "\tTID=%d, (Channel=%d, Target=%d, Lun=%d) (%s)\n\n",
638 unit, (int)d->scsi_channel, (int)d->scsi_id, (int)d->scsi_lun,
639 scsi_device_online(d->pScsi_dev)? "online":"offline");
643 if(pos > offset + length) {
657 * begin is where we last checked our position with regards to offset
658 * begin is always less than offset. len is relative to begin. It
659 * is the number of bytes written past begin
663 /* stop the output and calculate the correct length */
664 *(buffer + len) = '\0';
666 *start = buffer + (offset - begin); /* Start of wanted data */
667 len -= (offset - begin);
678 * Turn a struct scsi_cmnd * into a unique 32 bit 'context'.
680 static u32 adpt_cmd_to_context(struct scsi_cmnd *cmd)
682 return (u32)cmd->serial_number;
686 * Go from a u32 'context' to a struct scsi_cmnd * .
687 * This could probably be made more efficient.
689 static struct scsi_cmnd *
690 adpt_cmd_from_context(adpt_hba * pHba, u32 context)
692 struct scsi_cmnd * cmd;
693 struct scsi_device * d;
698 spin_unlock(pHba->host->host_lock);
699 shost_for_each_device(d, pHba->host) {
701 spin_lock_irqsave(&d->list_lock, flags);
702 list_for_each_entry(cmd, &d->cmd_list, list) {
703 if (((u32)cmd->serial_number == context)) {
704 spin_unlock_irqrestore(&d->list_lock, flags);
706 spin_lock(pHba->host->host_lock);
710 spin_unlock_irqrestore(&d->list_lock, flags);
712 spin_lock(pHba->host->host_lock);
718 * Turn a pointer to ioctl reply data into an u32 'context'
720 static u32 adpt_ioctl_to_context(adpt_hba * pHba, void *reply)
722 #if BITS_PER_LONG == 32
723 return (u32)(unsigned long)reply;
728 spin_lock_irqsave(pHba->host->host_lock, flags);
729 nr = ARRAY_SIZE(pHba->ioctl_reply_context);
730 for (i = 0; i < nr; i++) {
731 if (pHba->ioctl_reply_context[i] == NULL) {
732 pHba->ioctl_reply_context[i] = reply;
736 spin_unlock_irqrestore(pHba->host->host_lock, flags);
739 printk(KERN_WARNING"%s: Too many outstanding "
740 "ioctl commands\n", pHba->name);
749 * Go from an u32 'context' to a pointer to ioctl reply data.
751 static void *adpt_ioctl_from_context(adpt_hba *pHba, u32 context)
753 #if BITS_PER_LONG == 32
754 return (void *)(unsigned long)context;
756 void *p = pHba->ioctl_reply_context[context];
757 pHba->ioctl_reply_context[context] = NULL;
763 /*===========================================================================
764 * Error Handling routines
765 *===========================================================================
768 static int adpt_abort(struct scsi_cmnd * cmd)
770 adpt_hba* pHba = NULL; /* host bus adapter structure */
771 struct adpt_device* dptdevice; /* dpt per device information */
775 if(cmd->serial_number == 0){
778 pHba = (adpt_hba*) cmd->device->host->hostdata[0];
779 printk(KERN_INFO"%s: Trying to Abort cmd=%ld\n",pHba->name, cmd->serial_number);
780 if ((dptdevice = (void*) (cmd->device->hostdata)) == NULL) {
781 printk(KERN_ERR "%s: Unable to abort: No device in cmnd\n",pHba->name);
785 memset(msg, 0, sizeof(msg));
786 msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
787 msg[1] = I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|dptdevice->tid;
790 msg[4] = adpt_cmd_to_context(cmd);
792 spin_lock_irq(pHba->host->host_lock);
793 rcode = adpt_i2o_post_wait(pHba, msg, sizeof(msg), FOREVER);
795 spin_unlock_irq(pHba->host->host_lock);
797 if(rcode == -EOPNOTSUPP ){
798 printk(KERN_INFO"%s: Abort cmd not supported\n",pHba->name);
801 printk(KERN_INFO"%s: Abort cmd=%ld failed.\n",pHba->name, cmd->serial_number);
804 printk(KERN_INFO"%s: Abort cmd=%ld complete.\n",pHba->name, cmd->serial_number);
809 #define I2O_DEVICE_RESET 0x27
810 // This is the same for BLK and SCSI devices
811 // NOTE this is wrong in the i2o.h definitions
812 // This is not currently supported by our adapter but we issue it anyway
813 static int adpt_device_reset(struct scsi_cmnd* cmd)
819 struct adpt_device* d = cmd->device->hostdata;
821 pHba = (void*) cmd->device->host->hostdata[0];
822 printk(KERN_INFO"%s: Trying to reset device\n",pHba->name);
824 printk(KERN_INFO"%s: Reset Device: Device Not found\n",pHba->name);
827 memset(msg, 0, sizeof(msg));
828 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
829 msg[1] = (I2O_DEVICE_RESET<<24|HOST_TID<<12|d->tid);
834 spin_lock_irq(pHba->host->host_lock);
835 old_state = d->state;
836 d->state |= DPTI_DEV_RESET;
837 rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
838 d->state = old_state;
840 spin_unlock_irq(pHba->host->host_lock);
842 if(rcode == -EOPNOTSUPP ){
843 printk(KERN_INFO"%s: Device reset not supported\n",pHba->name);
846 printk(KERN_INFO"%s: Device reset failed\n",pHba->name);
849 printk(KERN_INFO"%s: Device reset successful\n",pHba->name);
855 #define I2O_HBA_BUS_RESET 0x87
856 // This version of bus reset is called by the eh_error handler
857 static int adpt_bus_reset(struct scsi_cmnd* cmd)
863 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
864 memset(msg, 0, sizeof(msg));
865 printk(KERN_WARNING"%s: Bus reset: SCSI Bus %d: tid: %d\n",pHba->name, cmd->device->channel,pHba->channel[cmd->device->channel].tid );
866 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
867 msg[1] = (I2O_HBA_BUS_RESET<<24|HOST_TID<<12|pHba->channel[cmd->device->channel].tid);
871 spin_lock_irq(pHba->host->host_lock);
872 rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
874 spin_unlock_irq(pHba->host->host_lock);
876 printk(KERN_WARNING"%s: Bus reset failed.\n",pHba->name);
879 printk(KERN_WARNING"%s: Bus reset success.\n",pHba->name);
884 // This version of reset is called by the eh_error_handler
885 static int __adpt_reset(struct scsi_cmnd* cmd)
889 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
890 printk(KERN_WARNING"%s: Hba Reset: scsi id %d: tid: %d\n",pHba->name,cmd->device->channel,pHba->channel[cmd->device->channel].tid );
891 rcode = adpt_hba_reset(pHba);
893 printk(KERN_WARNING"%s: HBA reset complete\n",pHba->name);
896 printk(KERN_WARNING"%s: HBA reset failed (%x)\n",pHba->name, rcode);
901 static int adpt_reset(struct scsi_cmnd* cmd)
905 spin_lock_irq(cmd->device->host->host_lock);
906 rc = __adpt_reset(cmd);
907 spin_unlock_irq(cmd->device->host->host_lock);
912 // This version of reset is called by the ioctls and indirectly from eh_error_handler via adpt_reset
913 static int adpt_hba_reset(adpt_hba* pHba)
917 pHba->state |= DPTI_STATE_RESET;
919 // Activate does get status , init outbound, and get hrt
920 if ((rcode=adpt_i2o_activate_hba(pHba)) < 0) {
921 printk(KERN_ERR "%s: Could not activate\n", pHba->name);
922 adpt_i2o_delete_hba(pHba);
926 if ((rcode=adpt_i2o_build_sys_table()) < 0) {
927 adpt_i2o_delete_hba(pHba);
930 PDEBUG("%s: in HOLD state\n",pHba->name);
932 if ((rcode=adpt_i2o_online_hba(pHba)) < 0) {
933 adpt_i2o_delete_hba(pHba);
936 PDEBUG("%s: in OPERATIONAL state\n",pHba->name);
938 if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
939 adpt_i2o_delete_hba(pHba);
943 if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
944 adpt_i2o_delete_hba(pHba);
947 pHba->state &= ~DPTI_STATE_RESET;
949 adpt_fail_posted_scbs(pHba);
950 return 0; /* return success */
953 /*===========================================================================
955 *===========================================================================
959 static void adpt_i2o_sys_shutdown(void)
961 adpt_hba *pHba, *pNext;
962 struct adpt_i2o_post_wait_data *p1, *old;
964 printk(KERN_INFO"Shutting down Adaptec I2O controllers.\n");
965 printk(KERN_INFO" This could take a few minutes if there are many devices attached\n");
966 /* Delete all IOPs from the controller chain */
967 /* They should have already been released by the
970 for (pHba = hba_chain; pHba; pHba = pNext) {
972 adpt_i2o_delete_hba(pHba);
975 /* Remove any timedout entries from the wait queue. */
976 // spin_lock_irqsave(&adpt_post_wait_lock, flags);
977 /* Nothing should be outstanding at this point so just
980 for(p1 = adpt_post_wait_queue; p1;) {
985 // spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
986 adpt_post_wait_queue = NULL;
988 printk(KERN_INFO "Adaptec I2O controllers down.\n");
991 static int adpt_install_hba(struct scsi_host_template* sht, struct pci_dev* pDev)
994 adpt_hba* pHba = NULL;
996 ulong base_addr0_phys = 0;
997 ulong base_addr1_phys = 0;
998 u32 hba_map0_area_size = 0;
999 u32 hba_map1_area_size = 0;
1000 void __iomem *base_addr_virt = NULL;
1001 void __iomem *msg_addr_virt = NULL;
1004 int raptorFlag = FALSE;
1006 if(pci_enable_device(pDev)) {
1010 if (pci_request_regions(pDev, "dpt_i2o")) {
1011 PERROR("dpti: adpt_config_hba: pci request region failed\n");
1015 pci_set_master(pDev);
1018 * See if we should enable dma64 mode.
1020 if (sizeof(dma_addr_t) > 4 &&
1021 pci_set_dma_mask(pDev, DMA_BIT_MASK(64)) == 0) {
1022 if (dma_get_required_mask(&pDev->dev) > DMA_BIT_MASK(32))
1025 if (!dma64 && pci_set_dma_mask(pDev, DMA_BIT_MASK(32)) != 0)
1028 /* adapter only supports message blocks below 4GB */
1029 pci_set_consistent_dma_mask(pDev, DMA_BIT_MASK(32));
1031 base_addr0_phys = pci_resource_start(pDev,0);
1032 hba_map0_area_size = pci_resource_len(pDev,0);
1034 // Check if standard PCI card or single BAR Raptor
1035 if(pDev->device == PCI_DPT_DEVICE_ID){
1036 if(pDev->subsystem_device >=0xc032 && pDev->subsystem_device <= 0xc03b){
1037 // Raptor card with this device id needs 4M
1038 hba_map0_area_size = 0x400000;
1039 } else { // Not Raptor - it is a PCI card
1040 if(hba_map0_area_size > 0x100000 ){
1041 hba_map0_area_size = 0x100000;
1044 } else {// Raptor split BAR config
1045 // Use BAR1 in this configuration
1046 base_addr1_phys = pci_resource_start(pDev,1);
1047 hba_map1_area_size = pci_resource_len(pDev,1);
1051 #if BITS_PER_LONG == 64
1053 * The original Adaptec 64 bit driver has this comment here:
1054 * "x86_64 machines need more optimal mappings"
1056 * I assume some HBAs report ridiculously large mappings
1057 * and we need to limit them on platforms with IOMMUs.
1059 if (raptorFlag == TRUE) {
1060 if (hba_map0_area_size > 128)
1061 hba_map0_area_size = 128;
1062 if (hba_map1_area_size > 524288)
1063 hba_map1_area_size = 524288;
1065 if (hba_map0_area_size > 524288)
1066 hba_map0_area_size = 524288;
1070 base_addr_virt = ioremap(base_addr0_phys,hba_map0_area_size);
1071 if (!base_addr_virt) {
1072 pci_release_regions(pDev);
1073 PERROR("dpti: adpt_config_hba: io remap failed\n");
1077 if(raptorFlag == TRUE) {
1078 msg_addr_virt = ioremap(base_addr1_phys, hba_map1_area_size );
1079 if (!msg_addr_virt) {
1080 PERROR("dpti: adpt_config_hba: io remap failed on BAR1\n");
1081 iounmap(base_addr_virt);
1082 pci_release_regions(pDev);
1086 msg_addr_virt = base_addr_virt;
1089 // Allocate and zero the data structure
1090 pHba = kzalloc(sizeof(adpt_hba), GFP_KERNEL);
1092 if (msg_addr_virt != base_addr_virt)
1093 iounmap(msg_addr_virt);
1094 iounmap(base_addr_virt);
1095 pci_release_regions(pDev);
1099 mutex_lock(&adpt_configuration_lock);
1101 if(hba_chain != NULL){
1102 for(p = hba_chain; p->next; p = p->next);
1108 pHba->unit = hba_count;
1109 sprintf(pHba->name, "dpti%d", hba_count);
1112 mutex_unlock(&adpt_configuration_lock);
1115 pHba->base_addr_phys = base_addr0_phys;
1117 // Set up the Virtual Base Address of the I2O Device
1118 pHba->base_addr_virt = base_addr_virt;
1119 pHba->msg_addr_virt = msg_addr_virt;
1120 pHba->irq_mask = base_addr_virt+0x30;
1121 pHba->post_port = base_addr_virt+0x40;
1122 pHba->reply_port = base_addr_virt+0x44;
1127 pHba->status_block = NULL;
1128 pHba->post_count = 0;
1129 pHba->state = DPTI_STATE_RESET;
1131 pHba->devices = NULL;
1132 pHba->dma64 = dma64;
1134 // Initializing the spinlocks
1135 spin_lock_init(&pHba->state_lock);
1136 spin_lock_init(&adpt_post_wait_lock);
1138 if(raptorFlag == 0){
1139 printk(KERN_INFO "Adaptec I2O RAID controller"
1140 " %d at %p size=%x irq=%d%s\n",
1141 hba_count-1, base_addr_virt,
1142 hba_map0_area_size, pDev->irq,
1143 dma64 ? " (64-bit DMA)" : "");
1145 printk(KERN_INFO"Adaptec I2O RAID controller %d irq=%d%s\n",
1146 hba_count-1, pDev->irq,
1147 dma64 ? " (64-bit DMA)" : "");
1148 printk(KERN_INFO" BAR0 %p - size= %x\n",base_addr_virt,hba_map0_area_size);
1149 printk(KERN_INFO" BAR1 %p - size= %x\n",msg_addr_virt,hba_map1_area_size);
1152 if (request_irq (pDev->irq, adpt_isr, IRQF_SHARED, pHba->name, pHba)) {
1153 printk(KERN_ERR"%s: Couldn't register IRQ %d\n", pHba->name, pDev->irq);
1154 adpt_i2o_delete_hba(pHba);
1162 static void adpt_i2o_delete_hba(adpt_hba* pHba)
1166 struct i2o_device* d;
1167 struct i2o_device* next;
1170 struct adpt_device* pDev;
1171 struct adpt_device* pNext;
1174 mutex_lock(&adpt_configuration_lock);
1175 // scsi_unregister calls our adpt_release which
1178 free_irq(pHba->host->irq, pHba);
1181 for( p1 = hba_chain; p1; p2 = p1,p1=p1->next){
1184 p2->next = p1->next;
1186 hba_chain = p1->next;
1193 mutex_unlock(&adpt_configuration_lock);
1195 iounmap(pHba->base_addr_virt);
1196 pci_release_regions(pHba->pDev);
1197 if(pHba->msg_addr_virt != pHba->base_addr_virt){
1198 iounmap(pHba->msg_addr_virt);
1200 if(pHba->FwDebugBuffer_P)
1201 iounmap(pHba->FwDebugBuffer_P);
1203 dma_free_coherent(&pHba->pDev->dev,
1204 pHba->hrt->num_entries * pHba->hrt->entry_len << 2,
1205 pHba->hrt, pHba->hrt_pa);
1208 dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
1209 pHba->lct, pHba->lct_pa);
1211 if(pHba->status_block) {
1212 dma_free_coherent(&pHba->pDev->dev, sizeof(i2o_status_block),
1213 pHba->status_block, pHba->status_block_pa);
1215 if(pHba->reply_pool) {
1216 dma_free_coherent(&pHba->pDev->dev,
1217 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
1218 pHba->reply_pool, pHba->reply_pool_pa);
1221 for(d = pHba->devices; d ; d = next){
1225 for(i = 0 ; i < pHba->top_scsi_channel ; i++){
1226 for(j = 0; j < MAX_ID; j++){
1227 if(pHba->channel[i].device[j] != NULL){
1228 for(pDev = pHba->channel[i].device[j]; pDev; pDev = pNext){
1229 pNext = pDev->next_lun;
1235 pci_dev_put(pHba->pDev);
1236 if (adpt_sysfs_class)
1237 device_destroy(adpt_sysfs_class,
1238 MKDEV(DPTI_I2O_MAJOR, pHba->unit));
1242 unregister_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER);
1243 if (adpt_sysfs_class) {
1244 class_destroy(adpt_sysfs_class);
1245 adpt_sysfs_class = NULL;
1250 static struct adpt_device* adpt_find_device(adpt_hba* pHba, u32 chan, u32 id, u32 lun)
1252 struct adpt_device* d;
1254 if(chan < 0 || chan >= MAX_CHANNEL)
1257 if( pHba->channel[chan].device == NULL){
1258 printk(KERN_DEBUG"Adaptec I2O RAID: Trying to find device before they are allocated\n");
1262 d = pHba->channel[chan].device[id];
1263 if(!d || d->tid == 0) {
1267 /* If it is the only lun at that address then this should match*/
1268 if(d->scsi_lun == lun){
1272 /* else we need to look through all the luns */
1273 for(d=d->next_lun ; d ; d = d->next_lun){
1274 if(d->scsi_lun == lun){
1282 static int adpt_i2o_post_wait(adpt_hba* pHba, u32* msg, int len, int timeout)
1284 // I used my own version of the WAIT_QUEUE_HEAD
1285 // to handle some version differences
1286 // When embedded in the kernel this could go back to the vanilla one
1287 ADPT_DECLARE_WAIT_QUEUE_HEAD(adpt_wq_i2o_post);
1290 struct adpt_i2o_post_wait_data *p1, *p2;
1291 struct adpt_i2o_post_wait_data *wait_data =
1292 kmalloc(sizeof(struct adpt_i2o_post_wait_data),GFP_KERNEL);
1293 DECLARE_WAITQUEUE(wait, current);
1299 * The spin locking is needed to keep anyone from playing
1300 * with the queue pointers and id while we do the same
1302 spin_lock_irqsave(&adpt_post_wait_lock, flags);
1303 // TODO we need a MORE unique way of getting ids
1304 // to support async LCT get
1305 wait_data->next = adpt_post_wait_queue;
1306 adpt_post_wait_queue = wait_data;
1307 adpt_post_wait_id++;
1308 adpt_post_wait_id &= 0x7fff;
1309 wait_data->id = adpt_post_wait_id;
1310 spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1312 wait_data->wq = &adpt_wq_i2o_post;
1313 wait_data->status = -ETIMEDOUT;
1315 add_wait_queue(&adpt_wq_i2o_post, &wait);
1317 msg[2] |= 0x80000000 | ((u32)wait_data->id);
1319 if((status = adpt_i2o_post_this(pHba, msg, len)) == 0){
1320 set_current_state(TASK_INTERRUPTIBLE);
1322 spin_unlock_irq(pHba->host->host_lock);
1326 timeout = schedule_timeout(timeout);
1328 // I/O issued, but cannot get result in
1329 // specified time. Freeing resorces is
1335 spin_lock_irq(pHba->host->host_lock);
1337 remove_wait_queue(&adpt_wq_i2o_post, &wait);
1339 if(status == -ETIMEDOUT){
1340 printk(KERN_INFO"dpti%d: POST WAIT TIMEOUT\n",pHba->unit);
1341 // We will have to free the wait_data memory during shutdown
1345 /* Remove the entry from the queue. */
1347 spin_lock_irqsave(&adpt_post_wait_lock, flags);
1348 for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p1->next) {
1349 if(p1 == wait_data) {
1350 if(p1->status == I2O_DETAIL_STATUS_UNSUPPORTED_FUNCTION ) {
1351 status = -EOPNOTSUPP;
1354 p2->next = p1->next;
1356 adpt_post_wait_queue = p1->next;
1361 spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1369 static s32 adpt_i2o_post_this(adpt_hba* pHba, u32* data, int len)
1372 u32 m = EMPTY_QUEUE;
1374 ulong timeout = jiffies + 30*HZ;
1377 m = readl(pHba->post_port);
1378 if (m != EMPTY_QUEUE) {
1381 if(time_after(jiffies,timeout)){
1382 printk(KERN_WARNING"dpti%d: Timeout waiting for message frame!\n", pHba->unit);
1385 schedule_timeout_uninterruptible(1);
1386 } while(m == EMPTY_QUEUE);
1388 msg = pHba->msg_addr_virt + m;
1389 memcpy_toio(msg, data, len);
1393 writel(m, pHba->post_port);
1400 static void adpt_i2o_post_wait_complete(u32 context, int status)
1402 struct adpt_i2o_post_wait_data *p1 = NULL;
1404 * We need to search through the adpt_post_wait
1405 * queue to see if the given message is still
1406 * outstanding. If not, it means that the IOP
1407 * took longer to respond to the message than we
1408 * had allowed and timer has already expired.
1409 * Not much we can do about that except log
1410 * it for debug purposes, increase timeout, and recompile
1412 * Lock needed to keep anyone from moving queue pointers
1413 * around while we're looking through them.
1418 spin_lock(&adpt_post_wait_lock);
1419 for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1420 if(p1->id == context) {
1421 p1->status = status;
1422 spin_unlock(&adpt_post_wait_lock);
1423 wake_up_interruptible(p1->wq);
1427 spin_unlock(&adpt_post_wait_lock);
1428 // If this happens we lose commands that probably really completed
1429 printk(KERN_DEBUG"dpti: Could Not find task %d in wait queue\n",context);
1430 printk(KERN_DEBUG" Tasks in wait queue:\n");
1431 for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1432 printk(KERN_DEBUG" %d\n",p1->id);
1437 static s32 adpt_i2o_reset_hba(adpt_hba* pHba)
1442 u32 m = EMPTY_QUEUE ;
1443 ulong timeout = jiffies + (TMOUT_IOPRESET*HZ);
1445 if(pHba->initialized == FALSE) { // First time reset should be quick
1446 timeout = jiffies + (25*HZ);
1448 adpt_i2o_quiesce_hba(pHba);
1453 m = readl(pHba->post_port);
1454 if (m != EMPTY_QUEUE) {
1457 if(time_after(jiffies,timeout)){
1458 printk(KERN_WARNING"Timeout waiting for message!\n");
1461 schedule_timeout_uninterruptible(1);
1462 } while (m == EMPTY_QUEUE);
1464 status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
1465 if(status == NULL) {
1466 adpt_send_nop(pHba, m);
1467 printk(KERN_ERR"IOP reset failed - no free memory.\n");
1472 msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
1473 msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
1478 msg[6]=dma_low(addr);
1479 msg[7]=dma_high(addr);
1481 memcpy_toio(pHba->msg_addr_virt+m, msg, sizeof(msg));
1483 writel(m, pHba->post_port);
1486 while(*status == 0){
1487 if(time_after(jiffies,timeout)){
1488 printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
1489 /* We lose 4 bytes of "status" here, but we cannot
1490 free these because controller may awake and corrupt
1491 those bytes at any time */
1492 /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
1496 schedule_timeout_uninterruptible(1);
1499 if(*status == 0x01 /*I2O_EXEC_IOP_RESET_IN_PROGRESS*/) {
1500 PDEBUG("%s: Reset in progress...\n", pHba->name);
1501 // Here we wait for message frame to become available
1502 // indicated that reset has finished
1505 m = readl(pHba->post_port);
1506 if (m != EMPTY_QUEUE) {
1509 if(time_after(jiffies,timeout)){
1510 printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
1511 /* We lose 4 bytes of "status" here, but we
1512 cannot free these because controller may
1513 awake and corrupt those bytes at any time */
1514 /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
1517 schedule_timeout_uninterruptible(1);
1518 } while (m == EMPTY_QUEUE);
1520 adpt_send_nop(pHba, m);
1522 adpt_i2o_status_get(pHba);
1523 if(*status == 0x02 ||
1524 pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
1525 printk(KERN_WARNING"%s: Reset reject, trying to clear\n",
1528 PDEBUG("%s: Reset completed.\n", pHba->name);
1531 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
1533 // This delay is to allow someone attached to the card through the debug UART to
1534 // set up the dump levels that they want before the rest of the initialization sequence
1541 static int adpt_i2o_parse_lct(adpt_hba* pHba)
1546 struct i2o_device *d;
1547 i2o_lct *lct = pHba->lct;
1551 u32 buf[10]; // larger than 7, or 8 ...
1552 struct adpt_device* pDev;
1555 printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
1559 max = lct->table_size;
1563 for(i=0;i<max;i++) {
1564 if( lct->lct_entry[i].user_tid != 0xfff){
1566 * If we have hidden devices, we need to inform the upper layers about
1567 * the possible maximum id reference to handle device access when
1568 * an array is disassembled. This code has no other purpose but to
1569 * allow us future access to devices that are currently hidden
1570 * behind arrays, hotspares or have not been configured (JBOD mode).
1572 if( lct->lct_entry[i].class_id != I2O_CLASS_RANDOM_BLOCK_STORAGE &&
1573 lct->lct_entry[i].class_id != I2O_CLASS_SCSI_PERIPHERAL &&
1574 lct->lct_entry[i].class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1577 tid = lct->lct_entry[i].tid;
1578 // I2O_DPT_DEVICE_INFO_GROUP_NO;
1579 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
1582 bus_no = buf[0]>>16;
1584 scsi_lun = (buf[2]>>8 )&0xff;
1585 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1586 printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
1589 if (scsi_id >= MAX_ID){
1590 printk(KERN_WARNING"%s: SCSI ID %d out of range \n", pHba->name, bus_no);
1593 if(bus_no > pHba->top_scsi_channel){
1594 pHba->top_scsi_channel = bus_no;
1596 if(scsi_id > pHba->top_scsi_id){
1597 pHba->top_scsi_id = scsi_id;
1599 if(scsi_lun > pHba->top_scsi_lun){
1600 pHba->top_scsi_lun = scsi_lun;
1604 d = kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
1607 printk(KERN_CRIT"%s: Out of memory for I2O device data.\n",pHba->name);
1611 d->controller = pHba;
1614 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
1617 tid = d->lct_data.tid;
1618 adpt_i2o_report_hba_unit(pHba, d);
1619 adpt_i2o_install_device(pHba, d);
1622 for(d = pHba->devices; d ; d = d->next) {
1623 if(d->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT ||
1624 d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PORT){
1625 tid = d->lct_data.tid;
1626 // TODO get the bus_no from hrt-but for now they are in order
1628 if(bus_no > pHba->top_scsi_channel){
1629 pHba->top_scsi_channel = bus_no;
1631 pHba->channel[bus_no].type = d->lct_data.class_id;
1632 pHba->channel[bus_no].tid = tid;
1633 if(adpt_i2o_query_scalar(pHba, tid, 0x0200, -1, buf, 28)>=0)
1635 pHba->channel[bus_no].scsi_id = buf[1];
1636 PDEBUG("Bus %d - SCSI ID %d.\n", bus_no, buf[1]);
1638 // TODO remove - this is just until we get from hrt
1640 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1641 printk(KERN_WARNING"%s: Channel number %d out of range - LCT\n", pHba->name, bus_no);
1647 // Setup adpt_device table
1648 for(d = pHba->devices; d ; d = d->next) {
1649 if(d->lct_data.class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
1650 d->lct_data.class_id == I2O_CLASS_SCSI_PERIPHERAL ||
1651 d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1653 tid = d->lct_data.tid;
1655 // I2O_DPT_DEVICE_INFO_GROUP_NO;
1656 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)>=0) {
1657 bus_no = buf[0]>>16;
1659 scsi_lun = (buf[2]>>8 )&0xff;
1660 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1663 if (scsi_id >= MAX_ID) {
1666 if( pHba->channel[bus_no].device[scsi_id] == NULL){
1667 pDev = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
1671 pHba->channel[bus_no].device[scsi_id] = pDev;
1673 for( pDev = pHba->channel[bus_no].device[scsi_id];
1674 pDev->next_lun; pDev = pDev->next_lun){
1676 pDev->next_lun = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
1677 if(pDev->next_lun == NULL) {
1680 pDev = pDev->next_lun;
1683 pDev->scsi_channel = bus_no;
1684 pDev->scsi_id = scsi_id;
1685 pDev->scsi_lun = scsi_lun;
1688 pDev->type = (buf[0])&0xff;
1689 pDev->flags = (buf[0]>>8)&0xff;
1690 if(scsi_id > pHba->top_scsi_id){
1691 pHba->top_scsi_id = scsi_id;
1693 if(scsi_lun > pHba->top_scsi_lun){
1694 pHba->top_scsi_lun = scsi_lun;
1698 printk(KERN_WARNING"Could not find SCSI ID for %s\n",
1699 d->lct_data.identity_tag);
1708 * Each I2O controller has a chain of devices on it - these match
1709 * the useful parts of the LCT of the board.
1712 static int adpt_i2o_install_device(adpt_hba* pHba, struct i2o_device *d)
1714 mutex_lock(&adpt_configuration_lock);
1717 d->next=pHba->devices;
1719 if (pHba->devices != NULL){
1720 pHba->devices->prev=d;
1725 mutex_unlock(&adpt_configuration_lock);
1729 static int adpt_open(struct inode *inode, struct file *file)
1735 //TODO check for root access
1737 minor = iminor(inode);
1738 if (minor >= hba_count) {
1742 mutex_lock(&adpt_configuration_lock);
1743 for (pHba = hba_chain; pHba; pHba = pHba->next) {
1744 if (pHba->unit == minor) {
1745 break; /* found adapter */
1749 mutex_unlock(&adpt_configuration_lock);
1754 // if(pHba->in_use){
1755 // mutex_unlock(&adpt_configuration_lock);
1760 mutex_unlock(&adpt_configuration_lock);
1766 static int adpt_close(struct inode *inode, struct file *file)
1771 minor = iminor(inode);
1772 if (minor >= hba_count) {
1775 mutex_lock(&adpt_configuration_lock);
1776 for (pHba = hba_chain; pHba; pHba = pHba->next) {
1777 if (pHba->unit == minor) {
1778 break; /* found adapter */
1781 mutex_unlock(&adpt_configuration_lock);
1792 static int adpt_i2o_passthru(adpt_hba* pHba, u32 __user *arg)
1794 u32 msg[MAX_MESSAGE_SIZE];
1798 u32 __user *user_msg = arg;
1799 u32 __user * user_reply = NULL;
1800 void *sg_list[pHba->sg_tablesize];
1810 memset(&msg, 0, MAX_MESSAGE_SIZE*4);
1811 // get user msg size in u32s
1812 if(get_user(size, &user_msg[0])){
1817 user_reply = &user_msg[size];
1818 if(size > MAX_MESSAGE_SIZE){
1821 size *= 4; // Convert to bytes
1823 /* Copy in the user's I2O command */
1824 if(copy_from_user(msg, user_msg, size)) {
1827 get_user(reply_size, &user_reply[0]);
1828 reply_size = reply_size>>16;
1829 if(reply_size > REPLY_FRAME_SIZE){
1830 reply_size = REPLY_FRAME_SIZE;
1833 reply = kzalloc(REPLY_FRAME_SIZE*4, GFP_KERNEL);
1835 printk(KERN_WARNING"%s: Could not allocate reply buffer\n",pHba->name);
1838 sg_offset = (msg[0]>>4)&0xf;
1839 msg[2] = 0x40000000; // IOCTL context
1840 msg[3] = adpt_ioctl_to_context(pHba, reply);
1841 if (msg[3] == (u32)-1)
1844 memset(sg_list,0, sizeof(sg_list[0])*pHba->sg_tablesize);
1846 // TODO add 64 bit API
1847 struct sg_simple_element *sg = (struct sg_simple_element*) (msg+sg_offset);
1848 sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
1849 if (sg_count > pHba->sg_tablesize){
1850 printk(KERN_DEBUG"%s:IOCTL SG List too large (%u)\n", pHba->name,sg_count);
1855 for(i = 0; i < sg_count; i++) {
1858 if (!(sg[i].flag_count & 0x10000000 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT*/)) {
1859 printk(KERN_DEBUG"%s:Bad SG element %d - not simple (%x)\n",pHba->name,i, sg[i].flag_count);
1863 sg_size = sg[i].flag_count & 0xffffff;
1864 /* Allocate memory for the transfer */
1865 p = dma_alloc_coherent(&pHba->pDev->dev, sg_size, &addr, GFP_KERNEL);
1867 printk(KERN_DEBUG"%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
1868 pHba->name,sg_size,i,sg_count);
1872 sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
1873 /* Copy in the user's SG buffer if necessary */
1874 if(sg[i].flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR*/) {
1875 // sg_simple_element API is 32 bit
1876 if (copy_from_user(p,(void __user *)(ulong)sg[i].addr_bus, sg_size)) {
1877 printk(KERN_DEBUG"%s: Could not copy SG buf %d FROM user\n",pHba->name,i);
1882 /* sg_simple_element API is 32 bit, but addr < 4GB */
1883 sg[i].addr_bus = addr;
1889 spin_lock_irqsave(pHba->host->host_lock, flags);
1890 // This state stops any new commands from enterring the
1891 // controller while processing the ioctl
1892 // pHba->state |= DPTI_STATE_IOCTL;
1893 // We can't set this now - The scsi subsystem sets host_blocked and
1894 // the queue empties and stops. We need a way to restart the queue
1895 rcode = adpt_i2o_post_wait(pHba, msg, size, FOREVER);
1897 printk("adpt_i2o_passthru: post wait failed %d %p\n",
1899 // pHba->state &= ~DPTI_STATE_IOCTL;
1901 spin_unlock_irqrestore(pHba->host->host_lock, flags);
1902 } while(rcode == -ETIMEDOUT);
1909 /* Copy back the Scatter Gather buffers back to user space */
1911 // TODO add 64 bit API
1912 struct sg_simple_element* sg;
1915 // re-acquire the original message to handle correctly the sg copy operation
1916 memset(&msg, 0, MAX_MESSAGE_SIZE*4);
1917 // get user msg size in u32s
1918 if(get_user(size, &user_msg[0])){
1924 if (size > MAX_MESSAGE_SIZE) {
1928 /* Copy in the user's I2O command */
1929 if (copy_from_user (msg, user_msg, size)) {
1933 sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
1935 // TODO add 64 bit API
1936 sg = (struct sg_simple_element*)(msg + sg_offset);
1937 for (j = 0; j < sg_count; j++) {
1938 /* Copy out the SG list to user's buffer if necessary */
1939 if(! (sg[j].flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR*/)) {
1940 sg_size = sg[j].flag_count & 0xffffff;
1941 // sg_simple_element API is 32 bit
1942 if (copy_to_user((void __user *)(ulong)sg[j].addr_bus,sg_list[j], sg_size)) {
1943 printk(KERN_WARNING"%s: Could not copy %p TO user %x\n",pHba->name, sg_list[j], sg[j].addr_bus);
1951 /* Copy back the reply to user space */
1953 // we wrote our own values for context - now restore the user supplied ones
1954 if(copy_from_user(reply+2, user_msg+2, sizeof(u32)*2)) {
1955 printk(KERN_WARNING"%s: Could not copy message context FROM user\n",pHba->name);
1958 if(copy_to_user(user_reply, reply, reply_size)) {
1959 printk(KERN_WARNING"%s: Could not copy reply TO user\n",pHba->name);
1966 if (rcode != -ETIME && rcode != -EINTR) {
1967 struct sg_simple_element *sg =
1968 (struct sg_simple_element*) (msg +sg_offset);
1971 if(sg_list[--sg_index]) {
1972 dma_free_coherent(&pHba->pDev->dev,
1973 sg[sg_index].flag_count & 0xffffff,
1975 sg[sg_index].addr_bus);
1982 #if defined __ia64__
1983 static void adpt_ia64_info(sysInfo_S* si)
1985 // This is all the info we need for now
1986 // We will add more info as our new
1987 // managmenent utility requires it
1988 si->processorType = PROC_IA64;
1992 #if defined __sparc__
1993 static void adpt_sparc_info(sysInfo_S* si)
1995 // This is all the info we need for now
1996 // We will add more info as our new
1997 // managmenent utility requires it
1998 si->processorType = PROC_ULTRASPARC;
2001 #if defined __alpha__
2002 static void adpt_alpha_info(sysInfo_S* si)
2004 // This is all the info we need for now
2005 // We will add more info as our new
2006 // managmenent utility requires it
2007 si->processorType = PROC_ALPHA;
2011 #if defined __i386__
2012 static void adpt_i386_info(sysInfo_S* si)
2014 // This is all the info we need for now
2015 // We will add more info as our new
2016 // managmenent utility requires it
2017 switch (boot_cpu_data.x86) {
2019 si->processorType = PROC_386;
2022 si->processorType = PROC_486;
2025 si->processorType = PROC_PENTIUM;
2027 default: // Just in case
2028 si->processorType = PROC_PENTIUM;
2035 * This routine returns information about the system. This does not effect
2036 * any logic and if the info is wrong - it doesn't matter.
2039 /* Get all the info we can not get from kernel services */
2040 static int adpt_system_info(void __user *buffer)
2044 memset(&si, 0, sizeof(si));
2046 si.osType = OS_LINUX;
2047 si.osMajorVersion = 0;
2048 si.osMinorVersion = 0;
2050 si.busType = SI_PCI_BUS;
2051 si.processorFamily = DPTI_sig.dsProcessorFamily;
2053 #if defined __i386__
2054 adpt_i386_info(&si);
2055 #elif defined (__ia64__)
2056 adpt_ia64_info(&si);
2057 #elif defined(__sparc__)
2058 adpt_sparc_info(&si);
2059 #elif defined (__alpha__)
2060 adpt_alpha_info(&si);
2062 si.processorType = 0xff ;
2064 if (copy_to_user(buffer, &si, sizeof(si))){
2065 printk(KERN_WARNING"dpti: Could not copy buffer TO user\n");
2072 static int adpt_ioctl(struct inode *inode, struct file *file, uint cmd,
2079 void __user *argp = (void __user *)arg;
2081 minor = iminor(inode);
2082 if (minor >= DPTI_MAX_HBA){
2085 mutex_lock(&adpt_configuration_lock);
2086 for (pHba = hba_chain; pHba; pHba = pHba->next) {
2087 if (pHba->unit == minor) {
2088 break; /* found adapter */
2091 mutex_unlock(&adpt_configuration_lock);
2096 while((volatile u32) pHba->state & DPTI_STATE_RESET )
2097 schedule_timeout_uninterruptible(2);
2100 // TODO: handle 3 cases
2102 if (copy_to_user(argp, &DPTI_sig, sizeof(DPTI_sig))) {
2107 return adpt_i2o_passthru(pHba, argp);
2110 drvrHBAinfo_S HbaInfo;
2112 #define FLG_OSD_PCI_VALID 0x0001
2113 #define FLG_OSD_DMA 0x0002
2114 #define FLG_OSD_I2O 0x0004
2115 memset(&HbaInfo, 0, sizeof(HbaInfo));
2116 HbaInfo.drvrHBAnum = pHba->unit;
2117 HbaInfo.baseAddr = (ulong) pHba->base_addr_phys;
2118 HbaInfo.blinkState = adpt_read_blink_led(pHba);
2119 HbaInfo.pciBusNum = pHba->pDev->bus->number;
2120 HbaInfo.pciDeviceNum=PCI_SLOT(pHba->pDev->devfn);
2121 HbaInfo.Interrupt = pHba->pDev->irq;
2122 HbaInfo.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
2123 if(copy_to_user(argp, &HbaInfo, sizeof(HbaInfo))){
2124 printk(KERN_WARNING"%s: Could not copy HbaInfo TO user\n",pHba->name);
2130 return adpt_system_info(argp);
2133 value = (u32)adpt_read_blink_led(pHba);
2134 if (copy_to_user(argp, &value, sizeof(value))) {
2141 spin_lock_irqsave(pHba->host->host_lock, flags);
2142 adpt_hba_reset(pHba);
2144 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2156 #ifdef CONFIG_COMPAT
2157 static long compat_adpt_ioctl(struct file *file,
2158 unsigned int cmd, unsigned long arg)
2160 struct inode *inode;
2163 inode = file->f_dentry->d_inode;
2175 case (DPT_TARGET_BUSY & 0xFFFF):
2176 case DPT_TARGET_BUSY:
2177 ret = adpt_ioctl(inode, file, cmd, arg);
2189 static irqreturn_t adpt_isr(int irq, void *dev_id)
2191 struct scsi_cmnd* cmd;
2192 adpt_hba* pHba = dev_id;
2194 void __iomem *reply;
2201 printk(KERN_WARNING"adpt_isr: NULL dev_id\n");
2205 spin_lock_irqsave(pHba->host->host_lock, flags);
2207 while( readl(pHba->irq_mask) & I2O_INTERRUPT_PENDING_B) {
2208 m = readl(pHba->reply_port);
2209 if(m == EMPTY_QUEUE){
2210 // Try twice then give up
2212 m = readl(pHba->reply_port);
2213 if(m == EMPTY_QUEUE){
2214 // This really should not happen
2215 printk(KERN_ERR"dpti: Could not get reply frame\n");
2219 if (pHba->reply_pool_pa <= m &&
2220 m < pHba->reply_pool_pa +
2221 (pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4)) {
2222 reply = (u8 *)pHba->reply_pool +
2223 (m - pHba->reply_pool_pa);
2225 /* Ick, we should *never* be here */
2226 printk(KERN_ERR "dpti: reply frame not from pool\n");
2227 reply = (u8 *)bus_to_virt(m);
2230 if (readl(reply) & MSG_FAIL) {
2231 u32 old_m = readl(reply+28);
2234 PDEBUG("%s: Failed message\n",pHba->name);
2235 if(old_m >= 0x100000){
2236 printk(KERN_ERR"%s: Bad preserved MFA (%x)- dropping frame\n",pHba->name,old_m);
2237 writel(m,pHba->reply_port);
2240 // Transaction context is 0 in failed reply frame
2241 msg = pHba->msg_addr_virt + old_m;
2242 old_context = readl(msg+12);
2243 writel(old_context, reply+12);
2244 adpt_send_nop(pHba, old_m);
2246 context = readl(reply+8);
2247 if(context & 0x40000000){ // IOCTL
2248 void *p = adpt_ioctl_from_context(pHba, readl(reply+12));
2250 memcpy_fromio(p, reply, REPLY_FRAME_SIZE * 4);
2252 // All IOCTLs will also be post wait
2254 if(context & 0x80000000){ // Post wait message
2255 status = readl(reply+16);
2257 status &= 0xffff; /* Get detail status */
2259 status = I2O_POST_WAIT_OK;
2261 if(!(context & 0x40000000)) {
2262 cmd = adpt_cmd_from_context(pHba,
2265 printk(KERN_WARNING"%s: Apparent SCSI cmd in Post Wait Context - cmd=%p context=%x\n", pHba->name, cmd, context);
2268 adpt_i2o_post_wait_complete(context, status);
2269 } else { // SCSI message
2270 cmd = adpt_cmd_from_context (pHba, readl(reply+12));
2272 scsi_dma_unmap(cmd);
2273 if(cmd->serial_number != 0) { // If not timedout
2274 adpt_i2o_to_scsi(reply, cmd);
2278 writel(m, pHba->reply_port);
2284 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2285 return IRQ_RETVAL(handled);
2288 static s32 adpt_scsi_to_i2o(adpt_hba* pHba, struct scsi_cmnd* cmd, struct adpt_device* d)
2291 u32 msg[MAX_MESSAGE_SIZE];
2303 memset(msg, 0 , sizeof(msg));
2304 len = scsi_bufflen(cmd);
2305 direction = 0x00000000;
2307 scsidir = 0x00000000; // DATA NO XFER
2310 * Set SCBFlags to indicate if data is being transferred
2311 * in or out, or no data transfer
2312 * Note: Do not have to verify index is less than 0 since
2313 * cmd->cmnd[0] is an unsigned char
2315 switch(cmd->sc_data_direction){
2316 case DMA_FROM_DEVICE:
2317 scsidir =0x40000000; // DATA IN (iop<--dev)
2320 direction=0x04000000; // SGL OUT
2321 scsidir =0x80000000; // DATA OUT (iop-->dev)
2325 case DMA_BIDIRECTIONAL:
2326 scsidir =0x40000000; // DATA IN (iop<--dev)
2327 // Assume In - and continue;
2330 printk(KERN_WARNING"%s: scsi opcode 0x%x not supported.\n",
2331 pHba->name, cmd->cmnd[0]);
2332 cmd->result = (DID_OK <<16) | (INITIATOR_ERROR << 8);
2333 cmd->scsi_done(cmd);
2337 // msg[0] is set later
2338 // I2O_CMD_SCSI_EXEC
2339 msg[1] = ((0xff<<24)|(HOST_TID<<12)|d->tid);
2341 msg[3] = adpt_cmd_to_context(cmd); /* Want SCSI control block back */
2342 // Our cards use the transaction context as the tag for queueing
2343 // Adaptec/DPT Private stuff
2344 msg[4] = I2O_CMD_SCSI_EXEC|(DPT_ORGANIZATION_ID<<16);
2346 /* Direction, disconnect ok | sense data | simple queue , CDBLen */
2347 // I2O_SCB_FLAG_ENABLE_DISCONNECT |
2348 // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
2349 // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
2350 msg[6] = scsidir|0x20a00000|cmd->cmd_len;
2354 // Write SCSI command into the message - always 16 byte block
2355 memset(mptr, 0, 16);
2356 memcpy(mptr, cmd->cmnd, cmd->cmd_len);
2358 lenptr=mptr++; /* Remember me - fill in when we know */
2359 if (dpt_dma64(pHba)) {
2360 reqlen = 16; // SINGLE SGE
2361 *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
2362 *mptr++ = 1 << PAGE_SHIFT;
2364 reqlen = 14; // SINGLE SGE
2366 /* Now fill in the SGList and command */
2368 nseg = scsi_dma_map(cmd);
2371 struct scatterlist *sg;
2374 scsi_for_each_sg(cmd, sg, nseg, i) {
2376 *mptr++ = direction|0x10000000|sg_dma_len(sg);
2377 len+=sg_dma_len(sg);
2378 addr = sg_dma_address(sg);
2379 *mptr++ = dma_low(addr);
2380 if (dpt_dma64(pHba))
2381 *mptr++ = dma_high(addr);
2382 /* Make this an end of list */
2384 *lptr = direction|0xD0000000|sg_dma_len(sg);
2386 reqlen = mptr - msg;
2389 if(cmd->underflow && len != cmd->underflow){
2390 printk(KERN_WARNING"Cmd len %08X Cmd underflow %08X\n",
2391 len, cmd->underflow);
2398 /* Stick the headers on */
2399 msg[0] = reqlen<<16 | ((reqlen > 12) ? SGL_OFFSET_12 : SGL_OFFSET_0);
2401 // Send it on it's way
2402 rcode = adpt_i2o_post_this(pHba, msg, reqlen<<2);
2410 static s32 adpt_scsi_host_alloc(adpt_hba* pHba, struct scsi_host_template *sht)
2412 struct Scsi_Host *host;
2414 host = scsi_host_alloc(sht, sizeof(adpt_hba*));
2416 printk("%s: scsi_host_alloc returned NULL\n", pHba->name);
2419 host->hostdata[0] = (unsigned long)pHba;
2422 host->irq = pHba->pDev->irq;
2423 /* no IO ports, so don't have to set host->io_port and
2427 host->n_io_port = 0;
2428 /* see comments in scsi_host.h */
2430 host->max_lun = 256;
2431 host->max_channel = pHba->top_scsi_channel + 1;
2432 host->cmd_per_lun = 1;
2433 host->unique_id = (u32)sys_tbl_pa + pHba->unit;
2434 host->sg_tablesize = pHba->sg_tablesize;
2435 host->can_queue = pHba->post_fifo_size;
2441 static s32 adpt_i2o_to_scsi(void __iomem *reply, struct scsi_cmnd* cmd)
2446 u32 reply_flags = readl(reply) & 0xff00; // Leave it shifted up 8 bits
2447 // I know this would look cleaner if I just read bytes
2448 // but the model I have been using for all the rest of the
2449 // io is in 4 byte words - so I keep that model
2450 u16 detailed_status = readl(reply+16) &0xffff;
2451 dev_status = (detailed_status & 0xff);
2452 hba_status = detailed_status >> 8;
2454 // calculate resid for sg
2455 scsi_set_resid(cmd, scsi_bufflen(cmd) - readl(reply+20));
2457 pHba = (adpt_hba*) cmd->device->host->hostdata[0];
2459 cmd->sense_buffer[0] = '\0'; // initialize sense valid flag to false
2461 if(!(reply_flags & MSG_FAIL)) {
2462 switch(detailed_status & I2O_SCSI_DSC_MASK) {
2463 case I2O_SCSI_DSC_SUCCESS:
2464 cmd->result = (DID_OK << 16);
2466 if (readl(reply+20) < cmd->underflow) {
2467 cmd->result = (DID_ERROR <<16);
2468 printk(KERN_WARNING"%s: SCSI CMD underflow\n",pHba->name);
2471 case I2O_SCSI_DSC_REQUEST_ABORTED:
2472 cmd->result = (DID_ABORT << 16);
2474 case I2O_SCSI_DSC_PATH_INVALID:
2475 case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
2476 case I2O_SCSI_DSC_SELECTION_TIMEOUT:
2477 case I2O_SCSI_DSC_COMMAND_TIMEOUT:
2478 case I2O_SCSI_DSC_NO_ADAPTER:
2479 case I2O_SCSI_DSC_RESOURCE_UNAVAILABLE:
2480 printk(KERN_WARNING"%s: SCSI Timeout-Device (%d,%d,%d) hba status=0x%x, dev status=0x%x, cmd=0x%x\n",
2481 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun, hba_status, dev_status, cmd->cmnd[0]);
2482 cmd->result = (DID_TIME_OUT << 16);
2484 case I2O_SCSI_DSC_ADAPTER_BUSY:
2485 case I2O_SCSI_DSC_BUS_BUSY:
2486 cmd->result = (DID_BUS_BUSY << 16);
2488 case I2O_SCSI_DSC_SCSI_BUS_RESET:
2489 case I2O_SCSI_DSC_BDR_MESSAGE_SENT:
2490 cmd->result = (DID_RESET << 16);
2492 case I2O_SCSI_DSC_PARITY_ERROR_FAILURE:
2493 printk(KERN_WARNING"%s: SCSI CMD parity error\n",pHba->name);
2494 cmd->result = (DID_PARITY << 16);
2496 case I2O_SCSI_DSC_UNABLE_TO_ABORT:
2497 case I2O_SCSI_DSC_COMPLETE_WITH_ERROR:
2498 case I2O_SCSI_DSC_UNABLE_TO_TERMINATE:
2499 case I2O_SCSI_DSC_MR_MESSAGE_RECEIVED:
2500 case I2O_SCSI_DSC_AUTOSENSE_FAILED:
2501 case I2O_SCSI_DSC_DATA_OVERRUN:
2502 case I2O_SCSI_DSC_UNEXPECTED_BUS_FREE:
2503 case I2O_SCSI_DSC_SEQUENCE_FAILURE:
2504 case I2O_SCSI_DSC_REQUEST_LENGTH_ERROR:
2505 case I2O_SCSI_DSC_PROVIDE_FAILURE:
2506 case I2O_SCSI_DSC_REQUEST_TERMINATED:
2507 case I2O_SCSI_DSC_IDE_MESSAGE_SENT:
2508 case I2O_SCSI_DSC_UNACKNOWLEDGED_EVENT:
2509 case I2O_SCSI_DSC_MESSAGE_RECEIVED:
2510 case I2O_SCSI_DSC_INVALID_CDB:
2511 case I2O_SCSI_DSC_LUN_INVALID:
2512 case I2O_SCSI_DSC_SCSI_TID_INVALID:
2513 case I2O_SCSI_DSC_FUNCTION_UNAVAILABLE:
2514 case I2O_SCSI_DSC_NO_NEXUS:
2515 case I2O_SCSI_DSC_CDB_RECEIVED:
2516 case I2O_SCSI_DSC_LUN_ALREADY_ENABLED:
2517 case I2O_SCSI_DSC_QUEUE_FROZEN:
2518 case I2O_SCSI_DSC_REQUEST_INVALID:
2520 printk(KERN_WARNING"%s: SCSI error %0x-Device(%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2521 pHba->name, detailed_status & I2O_SCSI_DSC_MASK, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
2522 hba_status, dev_status, cmd->cmnd[0]);
2523 cmd->result = (DID_ERROR << 16);
2527 // copy over the request sense data if it was a check
2529 if (dev_status == SAM_STAT_CHECK_CONDITION) {
2530 u32 len = min(SCSI_SENSE_BUFFERSIZE, 40);
2531 // Copy over the sense data
2532 memcpy_fromio(cmd->sense_buffer, (reply+28) , len);
2533 if(cmd->sense_buffer[0] == 0x70 /* class 7 */ &&
2534 cmd->sense_buffer[2] == DATA_PROTECT ){
2535 /* This is to handle an array failed */
2536 cmd->result = (DID_TIME_OUT << 16);
2537 printk(KERN_WARNING"%s: SCSI Data Protect-Device (%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2538 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
2539 hba_status, dev_status, cmd->cmnd[0]);
2544 /* In this condtion we could not talk to the tid
2545 * the card rejected it. We should signal a retry
2546 * for a limitted number of retries.
2548 cmd->result = (DID_TIME_OUT << 16);
2549 printk(KERN_WARNING"%s: I2O MSG_FAIL - Device (%d,%d,%d) tid=%d, cmd=0x%x\n",
2550 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
2551 ((struct adpt_device*)(cmd->device->hostdata))->tid, cmd->cmnd[0]);
2554 cmd->result |= (dev_status);
2556 if(cmd->scsi_done != NULL){
2557 cmd->scsi_done(cmd);
2563 static s32 adpt_rescan(adpt_hba* pHba)
2569 spin_lock_irqsave(pHba->host->host_lock, flags);
2570 if ((rcode=adpt_i2o_lct_get(pHba)) < 0)
2572 if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0)
2576 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2581 static s32 adpt_i2o_reparse_lct(adpt_hba* pHba)
2586 struct i2o_device *d;
2587 i2o_lct *lct = pHba->lct;
2591 u32 buf[10]; // at least 8 u32's
2592 struct adpt_device* pDev = NULL;
2593 struct i2o_device* pI2o_dev = NULL;
2596 printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
2600 max = lct->table_size;
2604 // Mark each drive as unscanned
2605 for (d = pHba->devices; d; d = d->next) {
2606 pDev =(struct adpt_device*) d->owner;
2610 pDev->state |= DPTI_DEV_UNSCANNED;
2613 printk(KERN_INFO "%s: LCT has %d entries.\n", pHba->name,max);
2615 for(i=0;i<max;i++) {
2616 if( lct->lct_entry[i].user_tid != 0xfff){
2620 if( lct->lct_entry[i].class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
2621 lct->lct_entry[i].class_id == I2O_CLASS_SCSI_PERIPHERAL ||
2622 lct->lct_entry[i].class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
2623 tid = lct->lct_entry[i].tid;
2624 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
2625 printk(KERN_ERR"%s: Could not query device\n",pHba->name);
2628 bus_no = buf[0]>>16;
2630 scsi_lun = (buf[2]>>8 )&0xff;
2631 pDev = pHba->channel[bus_no].device[scsi_id];
2634 if(pDev->scsi_lun == scsi_lun) {
2637 pDev = pDev->next_lun;
2639 if(!pDev ) { // Something new add it
2640 d = kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
2643 printk(KERN_CRIT "Out of memory for I2O device data.\n");
2647 d->controller = pHba;
2650 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2653 adpt_i2o_report_hba_unit(pHba, d);
2654 adpt_i2o_install_device(pHba, d);
2656 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
2657 printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
2660 pDev = pHba->channel[bus_no].device[scsi_id];
2662 pDev = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
2666 pHba->channel[bus_no].device[scsi_id] = pDev;
2668 while (pDev->next_lun) {
2669 pDev = pDev->next_lun;
2671 pDev = pDev->next_lun = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
2676 pDev->tid = d->lct_data.tid;
2677 pDev->scsi_channel = bus_no;
2678 pDev->scsi_id = scsi_id;
2679 pDev->scsi_lun = scsi_lun;
2682 pDev->type = (buf[0])&0xff;
2683 pDev->flags = (buf[0]>>8)&0xff;
2684 // Too late, SCSI system has made up it's mind, but what the hey ...
2685 if(scsi_id > pHba->top_scsi_id){
2686 pHba->top_scsi_id = scsi_id;
2688 if(scsi_lun > pHba->top_scsi_lun){
2689 pHba->top_scsi_lun = scsi_lun;
2692 } // end of new i2o device
2694 // We found an old device - check it
2696 if(pDev->scsi_lun == scsi_lun) {
2697 if(!scsi_device_online(pDev->pScsi_dev)) {
2698 printk(KERN_WARNING"%s: Setting device (%d,%d,%d) back online\n",
2699 pHba->name,bus_no,scsi_id,scsi_lun);
2700 if (pDev->pScsi_dev) {
2701 scsi_device_set_state(pDev->pScsi_dev, SDEV_RUNNING);
2705 if(d->lct_data.tid != tid) { // something changed
2707 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2708 if (pDev->pScsi_dev) {
2709 pDev->pScsi_dev->changed = TRUE;
2710 pDev->pScsi_dev->removable = TRUE;
2713 // Found it - mark it scanned
2714 pDev->state = DPTI_DEV_ONLINE;
2717 pDev = pDev->next_lun;
2721 for (pI2o_dev = pHba->devices; pI2o_dev; pI2o_dev = pI2o_dev->next) {
2722 pDev =(struct adpt_device*) pI2o_dev->owner;
2726 // Drive offline drives that previously existed but could not be found
2728 if (pDev->state & DPTI_DEV_UNSCANNED){
2729 pDev->state = DPTI_DEV_OFFLINE;
2730 printk(KERN_WARNING"%s: Device (%d,%d,%d) offline\n",pHba->name,pDev->scsi_channel,pDev->scsi_id,pDev->scsi_lun);
2731 if (pDev->pScsi_dev) {
2732 scsi_device_set_state(pDev->pScsi_dev, SDEV_OFFLINE);
2739 static void adpt_fail_posted_scbs(adpt_hba* pHba)
2741 struct scsi_cmnd* cmd = NULL;
2742 struct scsi_device* d = NULL;
2744 shost_for_each_device(d, pHba->host) {
2745 unsigned long flags;
2746 spin_lock_irqsave(&d->list_lock, flags);
2747 list_for_each_entry(cmd, &d->cmd_list, list) {
2748 if(cmd->serial_number == 0){
2751 cmd->result = (DID_OK << 16) | (QUEUE_FULL <<1);
2752 cmd->scsi_done(cmd);
2754 spin_unlock_irqrestore(&d->list_lock, flags);
2759 /*============================================================================
2760 * Routines from i2o subsystem
2761 *============================================================================
2767 * Bring an I2O controller into HOLD state. See the spec.
2769 static int adpt_i2o_activate_hba(adpt_hba* pHba)
2773 if(pHba->initialized ) {
2774 if (adpt_i2o_status_get(pHba) < 0) {
2775 if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2776 printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2779 if (adpt_i2o_status_get(pHba) < 0) {
2780 printk(KERN_INFO "HBA not responding.\n");
2785 if(pHba->status_block->iop_state == ADAPTER_STATE_FAULTED) {
2786 printk(KERN_CRIT "%s: hardware fault\n", pHba->name);
2790 if (pHba->status_block->iop_state == ADAPTER_STATE_READY ||
2791 pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
2792 pHba->status_block->iop_state == ADAPTER_STATE_HOLD ||
2793 pHba->status_block->iop_state == ADAPTER_STATE_FAILED) {
2794 adpt_i2o_reset_hba(pHba);
2795 if (adpt_i2o_status_get(pHba) < 0 || pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
2796 printk(KERN_ERR "%s: Failed to initialize.\n", pHba->name);
2801 if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2802 printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2808 if (adpt_i2o_init_outbound_q(pHba) < 0) {
2814 if (adpt_i2o_hrt_get(pHba) < 0) {
2822 * Bring a controller online into OPERATIONAL state.
2825 static int adpt_i2o_online_hba(adpt_hba* pHba)
2827 if (adpt_i2o_systab_send(pHba) < 0) {
2828 adpt_i2o_delete_hba(pHba);
2831 /* In READY state */
2833 if (adpt_i2o_enable_hba(pHba) < 0) {
2834 adpt_i2o_delete_hba(pHba);
2838 /* In OPERATIONAL state */
2842 static s32 adpt_send_nop(adpt_hba*pHba,u32 m)
2845 ulong timeout = jiffies + 5*HZ;
2847 while(m == EMPTY_QUEUE){
2849 m = readl(pHba->post_port);
2850 if(m != EMPTY_QUEUE){
2853 if(time_after(jiffies,timeout)){
2854 printk(KERN_ERR "%s: Timeout waiting for message frame!\n",pHba->name);
2857 schedule_timeout_uninterruptible(1);
2859 msg = (u32 __iomem *)(pHba->msg_addr_virt + m);
2860 writel( THREE_WORD_MSG_SIZE | SGL_OFFSET_0,&msg[0]);
2861 writel( I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0,&msg[1]);
2865 writel(m, pHba->post_port);
2870 static s32 adpt_i2o_init_outbound_q(adpt_hba* pHba)
2874 u32 __iomem *msg = NULL;
2876 ulong timeout = jiffies + TMOUT_INITOUTBOUND*HZ;
2881 m = readl(pHba->post_port);
2882 if (m != EMPTY_QUEUE) {
2886 if(time_after(jiffies,timeout)){
2887 printk(KERN_WARNING"%s: Timeout waiting for message frame\n",pHba->name);
2890 schedule_timeout_uninterruptible(1);
2891 } while(m == EMPTY_QUEUE);
2893 msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
2895 status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
2897 adpt_send_nop(pHba, m);
2898 printk(KERN_WARNING"%s: IOP reset failed - no free memory.\n",
2902 memset(status, 0, 4);
2904 writel(EIGHT_WORD_MSG_SIZE| SGL_OFFSET_6, &msg[0]);
2905 writel(I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID, &msg[1]);
2907 writel(0x0106, &msg[3]); /* Transaction context */
2908 writel(4096, &msg[4]); /* Host page frame size */
2909 writel((REPLY_FRAME_SIZE)<<16|0x80, &msg[5]); /* Outbound msg frame size and Initcode */
2910 writel(0xD0000004, &msg[6]); /* Simple SG LE, EOB */
2911 writel((u32)addr, &msg[7]);
2913 writel(m, pHba->post_port);
2916 // Wait for the reply status to come back
2919 if (*status != 0x01 /*I2O_EXEC_OUTBOUND_INIT_IN_PROGRESS*/) {
2924 if(time_after(jiffies,timeout)){
2925 printk(KERN_WARNING"%s: Timeout Initializing\n",pHba->name);
2926 /* We lose 4 bytes of "status" here, but we
2927 cannot free these because controller may
2928 awake and corrupt those bytes at any time */
2929 /* dma_free_coherent(&pHba->pDev->dev, 4, status, addr); */
2932 schedule_timeout_uninterruptible(1);
2935 // If the command was successful, fill the fifo with our reply
2937 if(*status != 0x04 /*I2O_EXEC_OUTBOUND_INIT_COMPLETE*/) {
2938 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
2941 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
2943 if(pHba->reply_pool != NULL) {
2944 dma_free_coherent(&pHba->pDev->dev,
2945 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
2946 pHba->reply_pool, pHba->reply_pool_pa);
2949 pHba->reply_pool = dma_alloc_coherent(&pHba->pDev->dev,
2950 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
2951 &pHba->reply_pool_pa, GFP_KERNEL);
2952 if (!pHba->reply_pool) {
2953 printk(KERN_ERR "%s: Could not allocate reply pool\n", pHba->name);
2956 memset(pHba->reply_pool, 0 , pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4);
2958 for(i = 0; i < pHba->reply_fifo_size; i++) {
2959 writel(pHba->reply_pool_pa + (i * REPLY_FRAME_SIZE * 4),
2963 adpt_i2o_status_get(pHba);
2969 * I2O System Table. Contains information about
2970 * all the IOPs in the system. Used to inform IOPs
2971 * about each other's existence.
2973 * sys_tbl_ver is the CurrentChangeIndicator that is
2974 * used by IOPs to track changes.
2979 static s32 adpt_i2o_status_get(adpt_hba* pHba)
2984 u8 *status_block=NULL;
2986 if(pHba->status_block == NULL) {
2987 pHba->status_block = dma_alloc_coherent(&pHba->pDev->dev,
2988 sizeof(i2o_status_block),
2989 &pHba->status_block_pa, GFP_KERNEL);
2990 if(pHba->status_block == NULL) {
2992 "dpti%d: Get Status Block failed; Out of memory. \n",
2997 memset(pHba->status_block, 0, sizeof(i2o_status_block));
2998 status_block = (u8*)(pHba->status_block);
2999 timeout = jiffies+TMOUT_GETSTATUS*HZ;
3002 m = readl(pHba->post_port);
3003 if (m != EMPTY_QUEUE) {
3006 if(time_after(jiffies,timeout)){
3007 printk(KERN_ERR "%s: Timeout waiting for message !\n",
3011 schedule_timeout_uninterruptible(1);
3012 } while(m==EMPTY_QUEUE);
3015 msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
3017 writel(NINE_WORD_MSG_SIZE|SGL_OFFSET_0, &msg[0]);
3018 writel(I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID, &msg[1]);
3023 writel( dma_low(pHba->status_block_pa), &msg[6]);
3024 writel( dma_high(pHba->status_block_pa), &msg[7]);
3025 writel(sizeof(i2o_status_block), &msg[8]); // 88 bytes
3028 writel(m, pHba->post_port);
3031 while(status_block[87]!=0xff){
3032 if(time_after(jiffies,timeout)){
3033 printk(KERN_ERR"dpti%d: Get status timeout.\n",
3038 schedule_timeout_uninterruptible(1);
3041 // Set up our number of outbound and inbound messages
3042 pHba->post_fifo_size = pHba->status_block->max_inbound_frames;
3043 if (pHba->post_fifo_size > MAX_TO_IOP_MESSAGES) {
3044 pHba->post_fifo_size = MAX_TO_IOP_MESSAGES;
3047 pHba->reply_fifo_size = pHba->status_block->max_outbound_frames;
3048 if (pHba->reply_fifo_size > MAX_FROM_IOP_MESSAGES) {
3049 pHba->reply_fifo_size = MAX_FROM_IOP_MESSAGES;
3052 // Calculate the Scatter Gather list size
3053 if (dpt_dma64(pHba)) {
3055 = ((pHba->status_block->inbound_frame_size * 4
3057 / (sizeof(struct sg_simple_element) + sizeof(u32)));
3060 = ((pHba->status_block->inbound_frame_size * 4
3062 / sizeof(struct sg_simple_element));
3064 if (pHba->sg_tablesize > SG_LIST_ELEMENTS) {
3065 pHba->sg_tablesize = SG_LIST_ELEMENTS;
3070 printk("dpti%d: State = ",pHba->unit);
3071 switch(pHba->status_block->iop_state) {
3085 printk("OPERATIONAL\n");
3091 printk("FAULTED\n");
3094 printk("%x (unknown!!)\n",pHba->status_block->iop_state);
3101 * Get the IOP's Logical Configuration Table
3103 static int adpt_i2o_lct_get(adpt_hba* pHba)
3109 if ((pHba->lct_size == 0) || (pHba->lct == NULL)){
3110 pHba->lct_size = pHba->status_block->expected_lct_size;
3113 if (pHba->lct == NULL) {
3114 pHba->lct = dma_alloc_coherent(&pHba->pDev->dev,
3115 pHba->lct_size, &pHba->lct_pa,
3117 if(pHba->lct == NULL) {
3118 printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
3123 memset(pHba->lct, 0, pHba->lct_size);
3125 msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
3126 msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
3129 msg[4] = 0xFFFFFFFF; /* All devices */
3130 msg[5] = 0x00000000; /* Report now */
3131 msg[6] = 0xD0000000|pHba->lct_size;
3132 msg[7] = (u32)pHba->lct_pa;
3134 if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 360))) {
3135 printk(KERN_ERR "%s: LCT Get failed (status=%#10x.\n",
3137 printk(KERN_ERR"Adaptec: Error Reading Hardware.\n");
3141 if ((pHba->lct->table_size << 2) > pHba->lct_size) {
3142 pHba->lct_size = pHba->lct->table_size << 2;
3143 dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
3144 pHba->lct, pHba->lct_pa);
3147 } while (pHba->lct == NULL);
3149 PDEBUG("%s: Hardware resource table read.\n", pHba->name);
3152 // I2O_DPT_EXEC_IOP_BUFFERS_GROUP_NO;
3153 if(adpt_i2o_query_scalar(pHba, 0 , 0x8000, -1, buf, sizeof(buf))>=0) {
3154 pHba->FwDebugBufferSize = buf[1];
3155 pHba->FwDebugBuffer_P = ioremap(pHba->base_addr_phys + buf[0],
3156 pHba->FwDebugBufferSize);
3157 if (pHba->FwDebugBuffer_P) {
3158 pHba->FwDebugFlags_P = pHba->FwDebugBuffer_P +
3159 FW_DEBUG_FLAGS_OFFSET;
3160 pHba->FwDebugBLEDvalue_P = pHba->FwDebugBuffer_P +
3161 FW_DEBUG_BLED_OFFSET;
3162 pHba->FwDebugBLEDflag_P = pHba->FwDebugBLEDvalue_P + 1;
3163 pHba->FwDebugStrLength_P = pHba->FwDebugBuffer_P +
3164 FW_DEBUG_STR_LENGTH_OFFSET;
3165 pHba->FwDebugBuffer_P += buf[2];
3166 pHba->FwDebugFlags = 0;
3173 static int adpt_i2o_build_sys_table(void)
3175 adpt_hba* pHba = hba_chain;
3179 dma_free_coherent(&pHba->pDev->dev, sys_tbl_len,
3180 sys_tbl, sys_tbl_pa);
3182 sys_tbl_len = sizeof(struct i2o_sys_tbl) + // Header + IOPs
3183 (hba_count) * sizeof(struct i2o_sys_tbl_entry);
3185 sys_tbl = dma_alloc_coherent(&pHba->pDev->dev,
3186 sys_tbl_len, &sys_tbl_pa, GFP_KERNEL);
3188 printk(KERN_WARNING "SysTab Set failed. Out of memory.\n");
3191 memset(sys_tbl, 0, sys_tbl_len);
3193 sys_tbl->num_entries = hba_count;
3194 sys_tbl->version = I2OVERSION;
3195 sys_tbl->change_ind = sys_tbl_ind++;
3197 for(pHba = hba_chain; pHba; pHba = pHba->next) {
3199 // Get updated Status Block so we have the latest information
3200 if (adpt_i2o_status_get(pHba)) {
3201 sys_tbl->num_entries--;
3202 continue; // try next one
3205 sys_tbl->iops[count].org_id = pHba->status_block->org_id;
3206 sys_tbl->iops[count].iop_id = pHba->unit + 2;
3207 sys_tbl->iops[count].seg_num = 0;
3208 sys_tbl->iops[count].i2o_version = pHba->status_block->i2o_version;
3209 sys_tbl->iops[count].iop_state = pHba->status_block->iop_state;
3210 sys_tbl->iops[count].msg_type = pHba->status_block->msg_type;
3211 sys_tbl->iops[count].frame_size = pHba->status_block->inbound_frame_size;
3212 sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
3213 sys_tbl->iops[count].iop_capabilities = pHba->status_block->iop_capabilities;
3214 addr = pHba->base_addr_phys + 0x40;
3215 sys_tbl->iops[count].inbound_low = dma_low(addr);
3216 sys_tbl->iops[count].inbound_high = dma_high(addr);
3223 u32 *table = (u32*)sys_tbl;
3224 printk(KERN_DEBUG"sys_tbl_len=%d in 32bit words\n",(sys_tbl_len >>2));
3225 for(count = 0; count < (sys_tbl_len >>2); count++) {
3226 printk(KERN_INFO "sys_tbl[%d] = %0#10x\n",
3227 count, table[count]);
3237 * Dump the information block associated with a given unit (TID)
3240 static void adpt_i2o_report_hba_unit(adpt_hba* pHba, struct i2o_device *d)
3243 int unit = d->lct_data.tid;
3245 printk(KERN_INFO "TID %3.3d ", unit);
3247 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 3, buf, 16)>=0)
3250 printk(" Vendor: %-12.12s", buf);
3252 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 4, buf, 16)>=0)
3255 printk(" Device: %-12.12s", buf);
3257 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 6, buf, 8)>=0)
3260 printk(" Rev: %-12.12s\n", buf);
3263 printk(KERN_INFO "\tClass: %.21s\n", adpt_i2o_get_class_name(d->lct_data.class_id));
3264 printk(KERN_INFO "\tSubclass: 0x%04X\n", d->lct_data.sub_class);
3265 printk(KERN_INFO "\tFlags: ");
3267 if(d->lct_data.device_flags&(1<<0))
3268 printk("C"); // ConfigDialog requested
3269 if(d->lct_data.device_flags&(1<<1))
3270 printk("U"); // Multi-user capable
3271 if(!(d->lct_data.device_flags&(1<<4)))
3272 printk("P"); // Peer service enabled!
3273 if(!(d->lct_data.device_flags&(1<<5)))
3274 printk("M"); // Mgmt service enabled!
3281 * Do i2o class name lookup
3283 static const char *adpt_i2o_get_class_name(int class)
3286 static char *i2o_class_name[] = {
3288 "Device Driver Module",
3293 "Fibre Channel Port",
3294 "Fibre Channel Device",
3298 "Floppy Controller",
3300 "Secondary Bus Port",
3301 "Peer Transport Agent",
3306 switch(class&0xFFF) {
3307 case I2O_CLASS_EXECUTIVE:
3311 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
3313 case I2O_CLASS_SEQUENTIAL_STORAGE:
3319 case I2O_CLASS_FIBRE_CHANNEL_PORT:
3321 case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
3323 case I2O_CLASS_SCSI_PERIPHERAL:
3325 case I2O_CLASS_ATE_PORT:
3327 case I2O_CLASS_ATE_PERIPHERAL:
3329 case I2O_CLASS_FLOPPY_CONTROLLER:
3331 case I2O_CLASS_FLOPPY_DEVICE:
3333 case I2O_CLASS_BUS_ADAPTER_PORT:
3335 case I2O_CLASS_PEER_TRANSPORT_AGENT:
3337 case I2O_CLASS_PEER_TRANSPORT:
3340 return i2o_class_name[idx];
3345 static s32 adpt_i2o_hrt_get(adpt_hba* pHba)
3348 int ret, size = sizeof(i2o_hrt);
3351 if (pHba->hrt == NULL) {
3352 pHba->hrt = dma_alloc_coherent(&pHba->pDev->dev,
3353 size, &pHba->hrt_pa, GFP_KERNEL);
3354 if (pHba->hrt == NULL) {
3355 printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", pHba->name);
3360 msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
3361 msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
3364 msg[4]= (0xD0000000 | size); /* Simple transaction */
3365 msg[5]= (u32)pHba->hrt_pa; /* Dump it here */
3367 if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg),20))) {
3368 printk(KERN_ERR "%s: Unable to get HRT (status=%#10x)\n", pHba->name, ret);
3372 if (pHba->hrt->num_entries * pHba->hrt->entry_len << 2 > size) {
3373 int newsize = pHba->hrt->num_entries * pHba->hrt->entry_len << 2;
3374 dma_free_coherent(&pHba->pDev->dev, size,
3375 pHba->hrt, pHba->hrt_pa);
3379 } while(pHba->hrt == NULL);
3384 * Query one scalar group value or a whole scalar group.
3386 static int adpt_i2o_query_scalar(adpt_hba* pHba, int tid,
3387 int group, int field, void *buf, int buflen)
3389 u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
3391 dma_addr_t opblk_pa;
3393 dma_addr_t resblk_pa;
3397 /* 8 bytes for header */
3398 resblk_va = dma_alloc_coherent(&pHba->pDev->dev,
3399 sizeof(u8) * (8 + buflen), &resblk_pa, GFP_KERNEL);
3400 if (resblk_va == NULL) {
3401 printk(KERN_CRIT "%s: query scalar failed; Out of memory.\n", pHba->name);
3405 opblk_va = dma_alloc_coherent(&pHba->pDev->dev,
3406 sizeof(opblk), &opblk_pa, GFP_KERNEL);
3407 if (opblk_va == NULL) {
3408 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3409 resblk_va, resblk_pa);
3410 printk(KERN_CRIT "%s: query operatio failed; Out of memory.\n",
3414 if (field == -1) /* whole group */
3417 memcpy(opblk_va, opblk, sizeof(opblk));
3418 size = adpt_i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, pHba, tid,
3419 opblk_va, opblk_pa, sizeof(opblk),
3420 resblk_va, resblk_pa, sizeof(u8)*(8+buflen));
3421 dma_free_coherent(&pHba->pDev->dev, sizeof(opblk), opblk_va, opblk_pa);
3422 if (size == -ETIME) {
3423 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3424 resblk_va, resblk_pa);
3425 printk(KERN_WARNING "%s: issue params failed; Timed out.\n", pHba->name);
3427 } else if (size == -EINTR) {
3428 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3429 resblk_va, resblk_pa);
3430 printk(KERN_WARNING "%s: issue params failed; Interrupted.\n", pHba->name);
3434 memcpy(buf, resblk_va+8, buflen); /* cut off header */
3436 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3437 resblk_va, resblk_pa);
3445 /* Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
3447 * This function can be used for all UtilParamsGet/Set operations.
3448 * The OperationBlock is given in opblk-buffer,
3449 * and results are returned in resblk-buffer.
3450 * Note that the minimum sized resblk is 8 bytes and contains
3451 * ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
3453 static int adpt_i2o_issue_params(int cmd, adpt_hba* pHba, int tid,
3454 void *opblk_va, dma_addr_t opblk_pa, int oplen,
3455 void *resblk_va, dma_addr_t resblk_pa, int reslen)
3458 u32 *res = (u32 *)resblk_va;
3461 msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
3462 msg[1] = cmd << 24 | HOST_TID << 12 | tid;
3466 msg[5] = 0x54000000 | oplen; /* OperationBlock */
3467 msg[6] = (u32)opblk_pa;
3468 msg[7] = 0xD0000000 | reslen; /* ResultBlock */
3469 msg[8] = (u32)resblk_pa;
3471 if ((wait_status = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 20))) {
3472 printk("adpt_i2o_issue_params: post_wait failed (%p)\n", resblk_va);
3473 return wait_status; /* -DetailedStatus */
3476 if (res[1]&0x00FF0000) { /* BlockStatus != SUCCESS */
3477 printk(KERN_WARNING "%s: %s - Error:\n ErrorInfoSize = 0x%02x, "
3478 "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
3480 (cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
3482 res[1]>>24, (res[1]>>16)&0xFF, res[1]&0xFFFF);
3483 return -((res[1] >> 16) & 0xFF); /* -BlockStatus */
3486 return 4 + ((res[1] & 0x0000FFFF) << 2); /* bytes used in resblk */
3490 static s32 adpt_i2o_quiesce_hba(adpt_hba* pHba)
3495 adpt_i2o_status_get(pHba);
3497 /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
3499 if((pHba->status_block->iop_state != ADAPTER_STATE_READY) &&
3500 (pHba->status_block->iop_state != ADAPTER_STATE_OPERATIONAL)){
3504 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3505 msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
3509 if((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3510 printk(KERN_INFO"dpti%d: Unable to quiesce (status=%#x).\n",
3513 printk(KERN_INFO"dpti%d: Quiesced.\n",pHba->unit);
3516 adpt_i2o_status_get(pHba);
3522 * Enable IOP. Allows the IOP to resume external operations.
3524 static int adpt_i2o_enable_hba(adpt_hba* pHba)
3529 adpt_i2o_status_get(pHba);
3530 if(!pHba->status_block){
3533 /* Enable only allowed on READY state */
3534 if(pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
3537 if(pHba->status_block->iop_state != ADAPTER_STATE_READY)
3540 msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3541 msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
3545 if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3546 printk(KERN_WARNING"%s: Could not enable (status=%#10x).\n",
3549 PDEBUG("%s: Enabled.\n", pHba->name);
3552 adpt_i2o_status_get(pHba);
3557 static int adpt_i2o_systab_send(adpt_hba* pHba)
3562 msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
3563 msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
3566 msg[4] = (0<<16) | ((pHba->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
3567 msg[5] = 0; /* Segment 0 */
3570 * Provide three SGL-elements:
3571 * System table (SysTab), Private memory space declaration and
3572 * Private i/o space declaration
3574 msg[6] = 0x54000000 | sys_tbl_len;
3575 msg[7] = (u32)sys_tbl_pa;
3576 msg[8] = 0x54000000 | 0;
3578 msg[10] = 0xD4000000 | 0;
3581 if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 120))) {
3582 printk(KERN_INFO "%s: Unable to set SysTab (status=%#10x).\n",
3587 PINFO("%s: SysTab set.\n", pHba->name);
3595 /*============================================================================
3597 *============================================================================
3603 static static void adpt_delay(int millisec)
3606 for (i = 0; i < millisec; i++) {
3607 udelay(1000); /* delay for one millisecond */
3613 static struct scsi_host_template driver_template = {
3614 .module = THIS_MODULE,
3616 .proc_name = "dpt_i2o",
3617 .proc_info = adpt_proc_info,
3619 .queuecommand = adpt_queue,
3620 .eh_abort_handler = adpt_abort,
3621 .eh_device_reset_handler = adpt_device_reset,
3622 .eh_bus_reset_handler = adpt_bus_reset,
3623 .eh_host_reset_handler = adpt_reset,
3624 .bios_param = adpt_bios_param,
3625 .slave_configure = adpt_slave_configure,
3626 .can_queue = MAX_TO_IOP_MESSAGES,
3629 .use_clustering = ENABLE_CLUSTERING,
3632 static int __init adpt_init(void)
3635 adpt_hba *pHba, *next;
3637 printk("Loading Adaptec I2O RAID: Version " DPT_I2O_VERSION "\n");
3639 error = adpt_detect(&driver_template);
3642 if (hba_chain == NULL)
3645 for (pHba = hba_chain; pHba; pHba = pHba->next) {
3646 error = scsi_add_host(pHba->host, &pHba->pDev->dev);
3649 scsi_scan_host(pHba->host);
3653 for (pHba = hba_chain; pHba; pHba = next) {
3655 scsi_remove_host(pHba->host);
3660 static void __exit adpt_exit(void)
3662 adpt_hba *pHba, *next;
3664 for (pHba = hba_chain; pHba; pHba = pHba->next)
3665 scsi_remove_host(pHba->host);
3666 for (pHba = hba_chain; pHba; pHba = next) {
3668 adpt_release(pHba->host);
3672 module_init(adpt_init);
3673 module_exit(adpt_exit);
3675 MODULE_LICENSE("GPL");