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/spinlock.h>
53 #include <linux/dma-mapping.h>
55 #include <linux/timer.h>
56 #include <linux/string.h>
57 #include <linux/ioport.h>
58 #include <linux/mutex.h>
60 #include <asm/processor.h> /* for boot_cpu_data */
61 #include <asm/pgtable.h>
62 #include <asm/io.h> /* for virt_to_bus, etc. */
64 #include <scsi/scsi.h>
65 #include <scsi/scsi_cmnd.h>
66 #include <scsi/scsi_device.h>
67 #include <scsi/scsi_host.h>
68 #include <scsi/scsi_tcq.h>
70 #include "dpt/dptsig.h"
73 /*============================================================================
74 * Create a binary signature - this is read by dptsig
75 * Needed for our management apps
76 *============================================================================
78 static DEFINE_MUTEX(adpt_mutex);
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;
117 static long adpt_unlocked_ioctl(struct file *, unsigned int, unsigned long);
119 static long compat_adpt_ioctl(struct file *, unsigned int, unsigned long);
122 static const struct file_operations adpt_fops = {
123 .unlocked_ioctl = adpt_unlocked_ioctl,
125 .release = adpt_close,
127 .compat_ioctl = compat_adpt_ioctl,
129 .llseek = noop_llseek,
132 /* Structures and definitions for synchronous message posting.
133 * See adpt_i2o_post_wait() for description
135 struct adpt_i2o_post_wait_data
139 adpt_wait_queue_head_t *wq;
140 struct adpt_i2o_post_wait_data *next;
143 static struct adpt_i2o_post_wait_data *adpt_post_wait_queue = NULL;
144 static u32 adpt_post_wait_id = 0;
145 static DEFINE_SPINLOCK(adpt_post_wait_lock);
148 /*============================================================================
150 *============================================================================
153 static inline int dpt_dma64(adpt_hba *pHba)
155 return (sizeof(dma_addr_t) > 4 && (pHba)->dma64);
158 static inline u32 dma_high(dma_addr_t addr)
160 return upper_32_bits(addr);
163 static inline u32 dma_low(dma_addr_t addr)
168 static u8 adpt_read_blink_led(adpt_hba* host)
170 if (host->FwDebugBLEDflag_P) {
171 if( readb(host->FwDebugBLEDflag_P) == 0xbc ){
172 return readb(host->FwDebugBLEDvalue_P);
178 /*============================================================================
179 * Scsi host template interface functions
180 *============================================================================
183 static struct pci_device_id dptids[] = {
184 { PCI_DPT_VENDOR_ID, PCI_DPT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
185 { PCI_DPT_VENDOR_ID, PCI_DPT_RAPTOR_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
188 MODULE_DEVICE_TABLE(pci,dptids);
190 static int adpt_detect(struct scsi_host_template* sht)
192 struct pci_dev *pDev = NULL;
196 PINFO("Detecting Adaptec I2O RAID controllers...\n");
198 /* search for all Adatpec I2O RAID cards */
199 while ((pDev = pci_get_device( PCI_DPT_VENDOR_ID, PCI_ANY_ID, pDev))) {
200 if(pDev->device == PCI_DPT_DEVICE_ID ||
201 pDev->device == PCI_DPT_RAPTOR_DEVICE_ID){
202 if(adpt_install_hba(sht, pDev) ){
203 PERROR("Could not Init an I2O RAID device\n");
204 PERROR("Will not try to detect others.\n");
211 /* In INIT state, Activate IOPs */
212 for (pHba = hba_chain; pHba; pHba = next) {
214 // Activate does get status , init outbound, and get hrt
215 if (adpt_i2o_activate_hba(pHba) < 0) {
216 adpt_i2o_delete_hba(pHba);
221 /* Active IOPs in HOLD state */
224 if (hba_chain == NULL)
228 * If build_sys_table fails, we kill everything and bail
229 * as we can't init the IOPs w/o a system table
231 if (adpt_i2o_build_sys_table() < 0) {
232 adpt_i2o_sys_shutdown();
236 PDEBUG("HBA's in HOLD state\n");
238 /* If IOP don't get online, we need to rebuild the System table */
239 for (pHba = hba_chain; pHba; pHba = pHba->next) {
240 if (adpt_i2o_online_hba(pHba) < 0) {
241 adpt_i2o_delete_hba(pHba);
242 goto rebuild_sys_tab;
246 /* Active IOPs now in OPERATIONAL state */
247 PDEBUG("HBA's in OPERATIONAL state\n");
249 printk("dpti: If you have a lot of devices this could take a few minutes.\n");
250 for (pHba = hba_chain; pHba; pHba = next) {
252 printk(KERN_INFO"%s: Reading the hardware resource table.\n", pHba->name);
253 if (adpt_i2o_lct_get(pHba) < 0){
254 adpt_i2o_delete_hba(pHba);
258 if (adpt_i2o_parse_lct(pHba) < 0){
259 adpt_i2o_delete_hba(pHba);
265 adpt_sysfs_class = class_create(THIS_MODULE, "dpt_i2o");
266 if (IS_ERR(adpt_sysfs_class)) {
267 printk(KERN_WARNING"dpti: unable to create dpt_i2o class\n");
268 adpt_sysfs_class = NULL;
271 for (pHba = hba_chain; pHba; pHba = next) {
273 if (adpt_scsi_host_alloc(pHba, sht) < 0){
274 adpt_i2o_delete_hba(pHba);
277 pHba->initialized = TRUE;
278 pHba->state &= ~DPTI_STATE_RESET;
279 if (adpt_sysfs_class) {
280 struct device *dev = device_create(adpt_sysfs_class,
281 NULL, MKDEV(DPTI_I2O_MAJOR, pHba->unit), NULL,
282 "dpti%d", pHba->unit);
284 printk(KERN_WARNING"dpti%d: unable to "
285 "create device in dpt_i2o class\n",
291 // Register our control device node
292 // nodes will need to be created in /dev to access this
293 // the nodes can not be created from within the driver
294 if (hba_count && register_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER, &adpt_fops)) {
295 adpt_i2o_sys_shutdown();
303 * scsi_unregister will be called AFTER we return.
305 static int adpt_release(struct Scsi_Host *host)
307 adpt_hba* pHba = (adpt_hba*) host->hostdata[0];
308 // adpt_i2o_quiesce_hba(pHba);
309 adpt_i2o_delete_hba(pHba);
310 scsi_unregister(host);
315 static void adpt_inquiry(adpt_hba* pHba)
329 memset(msg, 0, sizeof(msg));
330 buf = dma_alloc_coherent(&pHba->pDev->dev, 80, &addr, GFP_KERNEL);
332 printk(KERN_ERR"%s: Could not allocate buffer\n",pHba->name);
335 memset((void*)buf, 0, 36);
338 direction = 0x00000000;
339 scsidir =0x40000000; // DATA IN (iop<--dev)
342 reqlen = 17; // SINGLE SGE, 64 bit
344 reqlen = 14; // SINGLE SGE, 32 bit
345 /* Stick the headers on */
346 msg[0] = reqlen<<16 | SGL_OFFSET_12;
347 msg[1] = (0xff<<24|HOST_TID<<12|ADAPTER_TID);
350 // Adaptec/DPT Private stuff
351 msg[4] = I2O_CMD_SCSI_EXEC|DPT_ORGANIZATION_ID<<16;
352 msg[5] = ADAPTER_TID | 1<<16 /* Interpret*/;
353 /* Direction, disconnect ok | sense data | simple queue , CDBLen */
354 // I2O_SCB_FLAG_ENABLE_DISCONNECT |
355 // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
356 // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
357 msg[6] = scsidir|0x20a00000| 6 /* cmd len*/;
361 memset(scb, 0, sizeof(scb));
362 // Write SCSI command into the message - always 16 byte block
369 // Don't care about the rest of scb
371 memcpy(mptr, scb, sizeof(scb));
373 lenptr=mptr++; /* Remember me - fill in when we know */
375 /* Now fill in the SGList and command */
377 if (dpt_dma64(pHba)) {
378 *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
379 *mptr++ = 1 << PAGE_SHIFT;
380 *mptr++ = 0xD0000000|direction|len;
381 *mptr++ = dma_low(addr);
382 *mptr++ = dma_high(addr);
384 *mptr++ = 0xD0000000|direction|len;
388 // Send it on it's way
389 rcode = adpt_i2o_post_wait(pHba, msg, reqlen<<2, 120);
391 sprintf(pHba->detail, "Adaptec I2O RAID");
392 printk(KERN_INFO "%s: Inquiry Error (%d)\n",pHba->name,rcode);
393 if (rcode != -ETIME && rcode != -EINTR)
394 dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
396 memset(pHba->detail, 0, sizeof(pHba->detail));
397 memcpy(&(pHba->detail), "Vendor: Adaptec ", 16);
398 memcpy(&(pHba->detail[16]), " Model: ", 8);
399 memcpy(&(pHba->detail[24]), (u8*) &buf[16], 16);
400 memcpy(&(pHba->detail[40]), " FW: ", 4);
401 memcpy(&(pHba->detail[44]), (u8*) &buf[32], 4);
402 pHba->detail[48] = '\0'; /* precautionary */
403 dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
405 adpt_i2o_status_get(pHba);
410 static int adpt_slave_configure(struct scsi_device * device)
412 struct Scsi_Host *host = device->host;
415 pHba = (adpt_hba *) host->hostdata[0];
417 if (host->can_queue && device->tagged_supported) {
418 scsi_adjust_queue_depth(device, MSG_SIMPLE_TAG,
419 host->can_queue - 1);
421 scsi_adjust_queue_depth(device, 0, 1);
426 static int adpt_queue(struct scsi_cmnd * cmd, void (*done) (struct scsi_cmnd *))
428 adpt_hba* pHba = NULL;
429 struct adpt_device* pDev = NULL; /* dpt per device information */
431 cmd->scsi_done = done;
433 * SCSI REQUEST_SENSE commands will be executed automatically by the
434 * Host Adapter for any errors, so they should not be executed
435 * explicitly unless the Sense Data is zero indicating that no error
439 if ((cmd->cmnd[0] == REQUEST_SENSE) && (cmd->sense_buffer[0] != 0)) {
440 cmd->result = (DID_OK << 16);
445 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
452 * TODO: I need to block here if I am processing ioctl cmds
453 * but if the outstanding cmds all finish before the ioctl,
454 * the scsi-core will not know to start sending cmds to me again.
455 * I need to a way to restart the scsi-cores queues or should I block
456 * calling scsi_done on the outstanding cmds instead
457 * for now we don't set the IOCTL state
459 if(((pHba->state) & DPTI_STATE_IOCTL) || ((pHba->state) & DPTI_STATE_RESET)) {
460 pHba->host->last_reset = jiffies;
461 pHba->host->resetting = 1;
465 // TODO if the cmd->device if offline then I may need to issue a bus rescan
466 // followed by a get_lct to see if the device is there anymore
467 if((pDev = (struct adpt_device*) (cmd->device->hostdata)) == NULL) {
469 * First command request for this device. Set up a pointer
470 * to the device structure. This should be a TEST_UNIT_READY
471 * command from scan_scsis_single.
473 if ((pDev = adpt_find_device(pHba, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun)) == NULL) {
474 // TODO: if any luns are at this bus, scsi id then fake a TEST_UNIT_READY and INQUIRY response
475 // with type 7F (for all luns less than the max for this bus,id) so the lun scan will continue.
476 cmd->result = (DID_NO_CONNECT << 16);
480 cmd->device->hostdata = pDev;
482 pDev->pScsi_dev = cmd->device;
485 * If we are being called from when the device is being reset,
486 * delay processing of the command until later.
488 if (pDev->state & DPTI_DEV_RESET ) {
491 return adpt_scsi_to_i2o(pHba, cmd, pDev);
494 static int adpt_bios_param(struct scsi_device *sdev, struct block_device *dev,
495 sector_t capacity, int geom[])
501 // *** First lets set the default geometry ****
503 // If the capacity is less than ox2000
504 if (capacity < 0x2000 ) { // floppy
508 // else if between 0x2000 and 0x20000
509 else if (capacity < 0x20000) {
513 // else if between 0x20000 and 0x40000
514 else if (capacity < 0x40000) {
518 // else if between 0x4000 and 0x80000
519 else if (capacity < 0x80000) {
523 // else if greater than 0x80000
528 cylinders = sector_div(capacity, heads * sectors);
530 // Special case if CDROM
531 if(sdev->type == 5) { // CDROM
541 PDEBUG("adpt_bios_param: exit\n");
546 static const char *adpt_info(struct Scsi_Host *host)
550 pHba = (adpt_hba *) host->hostdata[0];
551 return (char *) (pHba->detail);
554 static int adpt_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
555 int length, int inout)
557 struct adpt_device* d;
569 * The user has done a write and wants us to take the
570 * data in the buffer and do something with it.
571 * proc_scsiwrite calls us with inout = 1
573 * Read data from buffer (writing to us) - NOT SUPPORTED
579 * inout = 0 means the user has done a read and wants information
580 * returned, so we write information about the cards into the buffer
581 * proc_scsiread() calls us with inout = 0
584 // Find HBA (host bus adapter) we are looking for
585 mutex_lock(&adpt_configuration_lock);
586 for (pHba = hba_chain; pHba; pHba = pHba->next) {
587 if (pHba->host == host) {
588 break; /* found adapter */
591 mutex_unlock(&adpt_configuration_lock);
597 len = sprintf(buffer , "Adaptec I2O RAID Driver Version: %s\n\n", DPT_I2O_VERSION);
598 len += sprintf(buffer+len, "%s\n", pHba->detail);
599 len += sprintf(buffer+len, "SCSI Host=scsi%d Control Node=/dev/%s irq=%d\n",
600 pHba->host->host_no, pHba->name, host->irq);
601 len += sprintf(buffer+len, "\tpost fifo size = %d\n\treply fifo size = %d\n\tsg table size = %d\n\n",
602 host->can_queue, (int) pHba->reply_fifo_size , host->sg_tablesize);
607 if(pos > offset + length) {
612 * If we haven't even written to where we last left
613 * off (the last time we were called), reset the
619 len += sprintf(buffer+len, "Devices:\n");
620 for(chan = 0; chan < MAX_CHANNEL; chan++) {
621 for(id = 0; id < MAX_ID; id++) {
622 d = pHba->channel[chan].device[id];
624 len += sprintf(buffer+len,"\t%-24.24s", d->pScsi_dev->vendor);
625 len += sprintf(buffer+len," Rev: %-8.8s\n", d->pScsi_dev->rev);
630 if(pos > offset + length) {
638 unit = d->pI2o_dev->lct_data.tid;
639 len += sprintf(buffer+len, "\tTID=%d, (Channel=%d, Target=%d, Lun=%d) (%s)\n\n",
640 unit, (int)d->scsi_channel, (int)d->scsi_id, (int)d->scsi_lun,
641 scsi_device_online(d->pScsi_dev)? "online":"offline");
645 if(pos > offset + length) {
659 * begin is where we last checked our position with regards to offset
660 * begin is always less than offset. len is relative to begin. It
661 * is the number of bytes written past begin
665 /* stop the output and calculate the correct length */
666 *(buffer + len) = '\0';
668 *start = buffer + (offset - begin); /* Start of wanted data */
669 len -= (offset - begin);
680 * Turn a struct scsi_cmnd * into a unique 32 bit 'context'.
682 static u32 adpt_cmd_to_context(struct scsi_cmnd *cmd)
684 return (u32)cmd->serial_number;
688 * Go from a u32 'context' to a struct scsi_cmnd * .
689 * This could probably be made more efficient.
691 static struct scsi_cmnd *
692 adpt_cmd_from_context(adpt_hba * pHba, u32 context)
694 struct scsi_cmnd * cmd;
695 struct scsi_device * d;
700 spin_unlock(pHba->host->host_lock);
701 shost_for_each_device(d, pHba->host) {
703 spin_lock_irqsave(&d->list_lock, flags);
704 list_for_each_entry(cmd, &d->cmd_list, list) {
705 if (((u32)cmd->serial_number == context)) {
706 spin_unlock_irqrestore(&d->list_lock, flags);
708 spin_lock(pHba->host->host_lock);
712 spin_unlock_irqrestore(&d->list_lock, flags);
714 spin_lock(pHba->host->host_lock);
720 * Turn a pointer to ioctl reply data into an u32 'context'
722 static u32 adpt_ioctl_to_context(adpt_hba * pHba, void *reply)
724 #if BITS_PER_LONG == 32
725 return (u32)(unsigned long)reply;
730 spin_lock_irqsave(pHba->host->host_lock, flags);
731 nr = ARRAY_SIZE(pHba->ioctl_reply_context);
732 for (i = 0; i < nr; i++) {
733 if (pHba->ioctl_reply_context[i] == NULL) {
734 pHba->ioctl_reply_context[i] = reply;
738 spin_unlock_irqrestore(pHba->host->host_lock, flags);
741 printk(KERN_WARNING"%s: Too many outstanding "
742 "ioctl commands\n", pHba->name);
751 * Go from an u32 'context' to a pointer to ioctl reply data.
753 static void *adpt_ioctl_from_context(adpt_hba *pHba, u32 context)
755 #if BITS_PER_LONG == 32
756 return (void *)(unsigned long)context;
758 void *p = pHba->ioctl_reply_context[context];
759 pHba->ioctl_reply_context[context] = NULL;
765 /*===========================================================================
766 * Error Handling routines
767 *===========================================================================
770 static int adpt_abort(struct scsi_cmnd * cmd)
772 adpt_hba* pHba = NULL; /* host bus adapter structure */
773 struct adpt_device* dptdevice; /* dpt per device information */
777 if(cmd->serial_number == 0){
780 pHba = (adpt_hba*) cmd->device->host->hostdata[0];
781 printk(KERN_INFO"%s: Trying to Abort cmd=%ld\n",pHba->name, cmd->serial_number);
782 if ((dptdevice = (void*) (cmd->device->hostdata)) == NULL) {
783 printk(KERN_ERR "%s: Unable to abort: No device in cmnd\n",pHba->name);
787 memset(msg, 0, sizeof(msg));
788 msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
789 msg[1] = I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|dptdevice->tid;
792 msg[4] = adpt_cmd_to_context(cmd);
794 spin_lock_irq(pHba->host->host_lock);
795 rcode = adpt_i2o_post_wait(pHba, msg, sizeof(msg), FOREVER);
797 spin_unlock_irq(pHba->host->host_lock);
799 if(rcode == -EOPNOTSUPP ){
800 printk(KERN_INFO"%s: Abort cmd not supported\n",pHba->name);
803 printk(KERN_INFO"%s: Abort cmd=%ld failed.\n",pHba->name, cmd->serial_number);
806 printk(KERN_INFO"%s: Abort cmd=%ld complete.\n",pHba->name, cmd->serial_number);
811 #define I2O_DEVICE_RESET 0x27
812 // This is the same for BLK and SCSI devices
813 // NOTE this is wrong in the i2o.h definitions
814 // This is not currently supported by our adapter but we issue it anyway
815 static int adpt_device_reset(struct scsi_cmnd* cmd)
821 struct adpt_device* d = cmd->device->hostdata;
823 pHba = (void*) cmd->device->host->hostdata[0];
824 printk(KERN_INFO"%s: Trying to reset device\n",pHba->name);
826 printk(KERN_INFO"%s: Reset Device: Device Not found\n",pHba->name);
829 memset(msg, 0, sizeof(msg));
830 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
831 msg[1] = (I2O_DEVICE_RESET<<24|HOST_TID<<12|d->tid);
836 spin_lock_irq(pHba->host->host_lock);
837 old_state = d->state;
838 d->state |= DPTI_DEV_RESET;
839 rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
840 d->state = old_state;
842 spin_unlock_irq(pHba->host->host_lock);
844 if(rcode == -EOPNOTSUPP ){
845 printk(KERN_INFO"%s: Device reset not supported\n",pHba->name);
848 printk(KERN_INFO"%s: Device reset failed\n",pHba->name);
851 printk(KERN_INFO"%s: Device reset successful\n",pHba->name);
857 #define I2O_HBA_BUS_RESET 0x87
858 // This version of bus reset is called by the eh_error handler
859 static int adpt_bus_reset(struct scsi_cmnd* cmd)
865 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
866 memset(msg, 0, sizeof(msg));
867 printk(KERN_WARNING"%s: Bus reset: SCSI Bus %d: tid: %d\n",pHba->name, cmd->device->channel,pHba->channel[cmd->device->channel].tid );
868 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
869 msg[1] = (I2O_HBA_BUS_RESET<<24|HOST_TID<<12|pHba->channel[cmd->device->channel].tid);
873 spin_lock_irq(pHba->host->host_lock);
874 rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
876 spin_unlock_irq(pHba->host->host_lock);
878 printk(KERN_WARNING"%s: Bus reset failed.\n",pHba->name);
881 printk(KERN_WARNING"%s: Bus reset success.\n",pHba->name);
886 // This version of reset is called by the eh_error_handler
887 static int __adpt_reset(struct scsi_cmnd* cmd)
891 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
892 printk(KERN_WARNING"%s: Hba Reset: scsi id %d: tid: %d\n",pHba->name,cmd->device->channel,pHba->channel[cmd->device->channel].tid );
893 rcode = adpt_hba_reset(pHba);
895 printk(KERN_WARNING"%s: HBA reset complete\n",pHba->name);
898 printk(KERN_WARNING"%s: HBA reset failed (%x)\n",pHba->name, rcode);
903 static int adpt_reset(struct scsi_cmnd* cmd)
907 spin_lock_irq(cmd->device->host->host_lock);
908 rc = __adpt_reset(cmd);
909 spin_unlock_irq(cmd->device->host->host_lock);
914 // This version of reset is called by the ioctls and indirectly from eh_error_handler via adpt_reset
915 static int adpt_hba_reset(adpt_hba* pHba)
919 pHba->state |= DPTI_STATE_RESET;
921 // Activate does get status , init outbound, and get hrt
922 if ((rcode=adpt_i2o_activate_hba(pHba)) < 0) {
923 printk(KERN_ERR "%s: Could not activate\n", pHba->name);
924 adpt_i2o_delete_hba(pHba);
928 if ((rcode=adpt_i2o_build_sys_table()) < 0) {
929 adpt_i2o_delete_hba(pHba);
932 PDEBUG("%s: in HOLD state\n",pHba->name);
934 if ((rcode=adpt_i2o_online_hba(pHba)) < 0) {
935 adpt_i2o_delete_hba(pHba);
938 PDEBUG("%s: in OPERATIONAL state\n",pHba->name);
940 if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
941 adpt_i2o_delete_hba(pHba);
945 if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
946 adpt_i2o_delete_hba(pHba);
949 pHba->state &= ~DPTI_STATE_RESET;
951 adpt_fail_posted_scbs(pHba);
952 return 0; /* return success */
955 /*===========================================================================
957 *===========================================================================
961 static void adpt_i2o_sys_shutdown(void)
963 adpt_hba *pHba, *pNext;
964 struct adpt_i2o_post_wait_data *p1, *old;
966 printk(KERN_INFO"Shutting down Adaptec I2O controllers.\n");
967 printk(KERN_INFO" This could take a few minutes if there are many devices attached\n");
968 /* Delete all IOPs from the controller chain */
969 /* They should have already been released by the
972 for (pHba = hba_chain; pHba; pHba = pNext) {
974 adpt_i2o_delete_hba(pHba);
977 /* Remove any timedout entries from the wait queue. */
978 // spin_lock_irqsave(&adpt_post_wait_lock, flags);
979 /* Nothing should be outstanding at this point so just
982 for(p1 = adpt_post_wait_queue; p1;) {
987 // spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
988 adpt_post_wait_queue = NULL;
990 printk(KERN_INFO "Adaptec I2O controllers down.\n");
993 static int adpt_install_hba(struct scsi_host_template* sht, struct pci_dev* pDev)
996 adpt_hba* pHba = NULL;
998 ulong base_addr0_phys = 0;
999 ulong base_addr1_phys = 0;
1000 u32 hba_map0_area_size = 0;
1001 u32 hba_map1_area_size = 0;
1002 void __iomem *base_addr_virt = NULL;
1003 void __iomem *msg_addr_virt = NULL;
1006 int raptorFlag = FALSE;
1008 if(pci_enable_device(pDev)) {
1012 if (pci_request_regions(pDev, "dpt_i2o")) {
1013 PERROR("dpti: adpt_config_hba: pci request region failed\n");
1017 pci_set_master(pDev);
1020 * See if we should enable dma64 mode.
1022 if (sizeof(dma_addr_t) > 4 &&
1023 pci_set_dma_mask(pDev, DMA_BIT_MASK(64)) == 0) {
1024 if (dma_get_required_mask(&pDev->dev) > DMA_BIT_MASK(32))
1027 if (!dma64 && pci_set_dma_mask(pDev, DMA_BIT_MASK(32)) != 0)
1030 /* adapter only supports message blocks below 4GB */
1031 pci_set_consistent_dma_mask(pDev, DMA_BIT_MASK(32));
1033 base_addr0_phys = pci_resource_start(pDev,0);
1034 hba_map0_area_size = pci_resource_len(pDev,0);
1036 // Check if standard PCI card or single BAR Raptor
1037 if(pDev->device == PCI_DPT_DEVICE_ID){
1038 if(pDev->subsystem_device >=0xc032 && pDev->subsystem_device <= 0xc03b){
1039 // Raptor card with this device id needs 4M
1040 hba_map0_area_size = 0x400000;
1041 } else { // Not Raptor - it is a PCI card
1042 if(hba_map0_area_size > 0x100000 ){
1043 hba_map0_area_size = 0x100000;
1046 } else {// Raptor split BAR config
1047 // Use BAR1 in this configuration
1048 base_addr1_phys = pci_resource_start(pDev,1);
1049 hba_map1_area_size = pci_resource_len(pDev,1);
1053 #if BITS_PER_LONG == 64
1055 * The original Adaptec 64 bit driver has this comment here:
1056 * "x86_64 machines need more optimal mappings"
1058 * I assume some HBAs report ridiculously large mappings
1059 * and we need to limit them on platforms with IOMMUs.
1061 if (raptorFlag == TRUE) {
1062 if (hba_map0_area_size > 128)
1063 hba_map0_area_size = 128;
1064 if (hba_map1_area_size > 524288)
1065 hba_map1_area_size = 524288;
1067 if (hba_map0_area_size > 524288)
1068 hba_map0_area_size = 524288;
1072 base_addr_virt = ioremap(base_addr0_phys,hba_map0_area_size);
1073 if (!base_addr_virt) {
1074 pci_release_regions(pDev);
1075 PERROR("dpti: adpt_config_hba: io remap failed\n");
1079 if(raptorFlag == TRUE) {
1080 msg_addr_virt = ioremap(base_addr1_phys, hba_map1_area_size );
1081 if (!msg_addr_virt) {
1082 PERROR("dpti: adpt_config_hba: io remap failed on BAR1\n");
1083 iounmap(base_addr_virt);
1084 pci_release_regions(pDev);
1088 msg_addr_virt = base_addr_virt;
1091 // Allocate and zero the data structure
1092 pHba = kzalloc(sizeof(adpt_hba), GFP_KERNEL);
1094 if (msg_addr_virt != base_addr_virt)
1095 iounmap(msg_addr_virt);
1096 iounmap(base_addr_virt);
1097 pci_release_regions(pDev);
1101 mutex_lock(&adpt_configuration_lock);
1103 if(hba_chain != NULL){
1104 for(p = hba_chain; p->next; p = p->next);
1110 pHba->unit = hba_count;
1111 sprintf(pHba->name, "dpti%d", hba_count);
1114 mutex_unlock(&adpt_configuration_lock);
1117 pHba->base_addr_phys = base_addr0_phys;
1119 // Set up the Virtual Base Address of the I2O Device
1120 pHba->base_addr_virt = base_addr_virt;
1121 pHba->msg_addr_virt = msg_addr_virt;
1122 pHba->irq_mask = base_addr_virt+0x30;
1123 pHba->post_port = base_addr_virt+0x40;
1124 pHba->reply_port = base_addr_virt+0x44;
1129 pHba->status_block = NULL;
1130 pHba->post_count = 0;
1131 pHba->state = DPTI_STATE_RESET;
1133 pHba->devices = NULL;
1134 pHba->dma64 = dma64;
1136 // Initializing the spinlocks
1137 spin_lock_init(&pHba->state_lock);
1138 spin_lock_init(&adpt_post_wait_lock);
1140 if(raptorFlag == 0){
1141 printk(KERN_INFO "Adaptec I2O RAID controller"
1142 " %d at %p size=%x irq=%d%s\n",
1143 hba_count-1, base_addr_virt,
1144 hba_map0_area_size, pDev->irq,
1145 dma64 ? " (64-bit DMA)" : "");
1147 printk(KERN_INFO"Adaptec I2O RAID controller %d irq=%d%s\n",
1148 hba_count-1, pDev->irq,
1149 dma64 ? " (64-bit DMA)" : "");
1150 printk(KERN_INFO" BAR0 %p - size= %x\n",base_addr_virt,hba_map0_area_size);
1151 printk(KERN_INFO" BAR1 %p - size= %x\n",msg_addr_virt,hba_map1_area_size);
1154 if (request_irq (pDev->irq, adpt_isr, IRQF_SHARED, pHba->name, pHba)) {
1155 printk(KERN_ERR"%s: Couldn't register IRQ %d\n", pHba->name, pDev->irq);
1156 adpt_i2o_delete_hba(pHba);
1164 static void adpt_i2o_delete_hba(adpt_hba* pHba)
1168 struct i2o_device* d;
1169 struct i2o_device* next;
1172 struct adpt_device* pDev;
1173 struct adpt_device* pNext;
1176 mutex_lock(&adpt_configuration_lock);
1177 // scsi_unregister calls our adpt_release which
1180 free_irq(pHba->host->irq, pHba);
1183 for( p1 = hba_chain; p1; p2 = p1,p1=p1->next){
1186 p2->next = p1->next;
1188 hba_chain = p1->next;
1195 mutex_unlock(&adpt_configuration_lock);
1197 iounmap(pHba->base_addr_virt);
1198 pci_release_regions(pHba->pDev);
1199 if(pHba->msg_addr_virt != pHba->base_addr_virt){
1200 iounmap(pHba->msg_addr_virt);
1202 if(pHba->FwDebugBuffer_P)
1203 iounmap(pHba->FwDebugBuffer_P);
1205 dma_free_coherent(&pHba->pDev->dev,
1206 pHba->hrt->num_entries * pHba->hrt->entry_len << 2,
1207 pHba->hrt, pHba->hrt_pa);
1210 dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
1211 pHba->lct, pHba->lct_pa);
1213 if(pHba->status_block) {
1214 dma_free_coherent(&pHba->pDev->dev, sizeof(i2o_status_block),
1215 pHba->status_block, pHba->status_block_pa);
1217 if(pHba->reply_pool) {
1218 dma_free_coherent(&pHba->pDev->dev,
1219 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
1220 pHba->reply_pool, pHba->reply_pool_pa);
1223 for(d = pHba->devices; d ; d = next){
1227 for(i = 0 ; i < pHba->top_scsi_channel ; i++){
1228 for(j = 0; j < MAX_ID; j++){
1229 if(pHba->channel[i].device[j] != NULL){
1230 for(pDev = pHba->channel[i].device[j]; pDev; pDev = pNext){
1231 pNext = pDev->next_lun;
1237 pci_dev_put(pHba->pDev);
1238 if (adpt_sysfs_class)
1239 device_destroy(adpt_sysfs_class,
1240 MKDEV(DPTI_I2O_MAJOR, pHba->unit));
1244 unregister_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER);
1245 if (adpt_sysfs_class) {
1246 class_destroy(adpt_sysfs_class);
1247 adpt_sysfs_class = NULL;
1252 static struct adpt_device* adpt_find_device(adpt_hba* pHba, u32 chan, u32 id, u32 lun)
1254 struct adpt_device* d;
1256 if(chan < 0 || chan >= MAX_CHANNEL)
1259 if( pHba->channel[chan].device == NULL){
1260 printk(KERN_DEBUG"Adaptec I2O RAID: Trying to find device before they are allocated\n");
1264 d = pHba->channel[chan].device[id];
1265 if(!d || d->tid == 0) {
1269 /* If it is the only lun at that address then this should match*/
1270 if(d->scsi_lun == lun){
1274 /* else we need to look through all the luns */
1275 for(d=d->next_lun ; d ; d = d->next_lun){
1276 if(d->scsi_lun == lun){
1284 static int adpt_i2o_post_wait(adpt_hba* pHba, u32* msg, int len, int timeout)
1286 // I used my own version of the WAIT_QUEUE_HEAD
1287 // to handle some version differences
1288 // When embedded in the kernel this could go back to the vanilla one
1289 ADPT_DECLARE_WAIT_QUEUE_HEAD(adpt_wq_i2o_post);
1292 struct adpt_i2o_post_wait_data *p1, *p2;
1293 struct adpt_i2o_post_wait_data *wait_data =
1294 kmalloc(sizeof(struct adpt_i2o_post_wait_data), GFP_ATOMIC);
1295 DECLARE_WAITQUEUE(wait, current);
1301 * The spin locking is needed to keep anyone from playing
1302 * with the queue pointers and id while we do the same
1304 spin_lock_irqsave(&adpt_post_wait_lock, flags);
1305 // TODO we need a MORE unique way of getting ids
1306 // to support async LCT get
1307 wait_data->next = adpt_post_wait_queue;
1308 adpt_post_wait_queue = wait_data;
1309 adpt_post_wait_id++;
1310 adpt_post_wait_id &= 0x7fff;
1311 wait_data->id = adpt_post_wait_id;
1312 spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1314 wait_data->wq = &adpt_wq_i2o_post;
1315 wait_data->status = -ETIMEDOUT;
1317 add_wait_queue(&adpt_wq_i2o_post, &wait);
1319 msg[2] |= 0x80000000 | ((u32)wait_data->id);
1321 if((status = adpt_i2o_post_this(pHba, msg, len)) == 0){
1322 set_current_state(TASK_INTERRUPTIBLE);
1324 spin_unlock_irq(pHba->host->host_lock);
1328 timeout = schedule_timeout(timeout);
1330 // I/O issued, but cannot get result in
1331 // specified time. Freeing resorces is
1337 spin_lock_irq(pHba->host->host_lock);
1339 remove_wait_queue(&adpt_wq_i2o_post, &wait);
1341 if(status == -ETIMEDOUT){
1342 printk(KERN_INFO"dpti%d: POST WAIT TIMEOUT\n",pHba->unit);
1343 // We will have to free the wait_data memory during shutdown
1347 /* Remove the entry from the queue. */
1349 spin_lock_irqsave(&adpt_post_wait_lock, flags);
1350 for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p1->next) {
1351 if(p1 == wait_data) {
1352 if(p1->status == I2O_DETAIL_STATUS_UNSUPPORTED_FUNCTION ) {
1353 status = -EOPNOTSUPP;
1356 p2->next = p1->next;
1358 adpt_post_wait_queue = p1->next;
1363 spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1371 static s32 adpt_i2o_post_this(adpt_hba* pHba, u32* data, int len)
1374 u32 m = EMPTY_QUEUE;
1376 ulong timeout = jiffies + 30*HZ;
1379 m = readl(pHba->post_port);
1380 if (m != EMPTY_QUEUE) {
1383 if(time_after(jiffies,timeout)){
1384 printk(KERN_WARNING"dpti%d: Timeout waiting for message frame!\n", pHba->unit);
1387 schedule_timeout_uninterruptible(1);
1388 } while(m == EMPTY_QUEUE);
1390 msg = pHba->msg_addr_virt + m;
1391 memcpy_toio(msg, data, len);
1395 writel(m, pHba->post_port);
1402 static void adpt_i2o_post_wait_complete(u32 context, int status)
1404 struct adpt_i2o_post_wait_data *p1 = NULL;
1406 * We need to search through the adpt_post_wait
1407 * queue to see if the given message is still
1408 * outstanding. If not, it means that the IOP
1409 * took longer to respond to the message than we
1410 * had allowed and timer has already expired.
1411 * Not much we can do about that except log
1412 * it for debug purposes, increase timeout, and recompile
1414 * Lock needed to keep anyone from moving queue pointers
1415 * around while we're looking through them.
1420 spin_lock(&adpt_post_wait_lock);
1421 for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1422 if(p1->id == context) {
1423 p1->status = status;
1424 spin_unlock(&adpt_post_wait_lock);
1425 wake_up_interruptible(p1->wq);
1429 spin_unlock(&adpt_post_wait_lock);
1430 // If this happens we lose commands that probably really completed
1431 printk(KERN_DEBUG"dpti: Could Not find task %d in wait queue\n",context);
1432 printk(KERN_DEBUG" Tasks in wait queue:\n");
1433 for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1434 printk(KERN_DEBUG" %d\n",p1->id);
1439 static s32 adpt_i2o_reset_hba(adpt_hba* pHba)
1444 u32 m = EMPTY_QUEUE ;
1445 ulong timeout = jiffies + (TMOUT_IOPRESET*HZ);
1447 if(pHba->initialized == FALSE) { // First time reset should be quick
1448 timeout = jiffies + (25*HZ);
1450 adpt_i2o_quiesce_hba(pHba);
1455 m = readl(pHba->post_port);
1456 if (m != EMPTY_QUEUE) {
1459 if(time_after(jiffies,timeout)){
1460 printk(KERN_WARNING"Timeout waiting for message!\n");
1463 schedule_timeout_uninterruptible(1);
1464 } while (m == EMPTY_QUEUE);
1466 status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
1467 if(status == NULL) {
1468 adpt_send_nop(pHba, m);
1469 printk(KERN_ERR"IOP reset failed - no free memory.\n");
1474 msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
1475 msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
1480 msg[6]=dma_low(addr);
1481 msg[7]=dma_high(addr);
1483 memcpy_toio(pHba->msg_addr_virt+m, msg, sizeof(msg));
1485 writel(m, pHba->post_port);
1488 while(*status == 0){
1489 if(time_after(jiffies,timeout)){
1490 printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
1491 /* We lose 4 bytes of "status" here, but we cannot
1492 free these because controller may awake and corrupt
1493 those bytes at any time */
1494 /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
1498 schedule_timeout_uninterruptible(1);
1501 if(*status == 0x01 /*I2O_EXEC_IOP_RESET_IN_PROGRESS*/) {
1502 PDEBUG("%s: Reset in progress...\n", pHba->name);
1503 // Here we wait for message frame to become available
1504 // indicated that reset has finished
1507 m = readl(pHba->post_port);
1508 if (m != EMPTY_QUEUE) {
1511 if(time_after(jiffies,timeout)){
1512 printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
1513 /* We lose 4 bytes of "status" here, but we
1514 cannot free these because controller may
1515 awake and corrupt those bytes at any time */
1516 /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
1519 schedule_timeout_uninterruptible(1);
1520 } while (m == EMPTY_QUEUE);
1522 adpt_send_nop(pHba, m);
1524 adpt_i2o_status_get(pHba);
1525 if(*status == 0x02 ||
1526 pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
1527 printk(KERN_WARNING"%s: Reset reject, trying to clear\n",
1530 PDEBUG("%s: Reset completed.\n", pHba->name);
1533 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
1535 // This delay is to allow someone attached to the card through the debug UART to
1536 // set up the dump levels that they want before the rest of the initialization sequence
1543 static int adpt_i2o_parse_lct(adpt_hba* pHba)
1548 struct i2o_device *d;
1549 i2o_lct *lct = pHba->lct;
1553 u32 buf[10]; // larger than 7, or 8 ...
1554 struct adpt_device* pDev;
1557 printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
1561 max = lct->table_size;
1565 for(i=0;i<max;i++) {
1566 if( lct->lct_entry[i].user_tid != 0xfff){
1568 * If we have hidden devices, we need to inform the upper layers about
1569 * the possible maximum id reference to handle device access when
1570 * an array is disassembled. This code has no other purpose but to
1571 * allow us future access to devices that are currently hidden
1572 * behind arrays, hotspares or have not been configured (JBOD mode).
1574 if( lct->lct_entry[i].class_id != I2O_CLASS_RANDOM_BLOCK_STORAGE &&
1575 lct->lct_entry[i].class_id != I2O_CLASS_SCSI_PERIPHERAL &&
1576 lct->lct_entry[i].class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1579 tid = lct->lct_entry[i].tid;
1580 // I2O_DPT_DEVICE_INFO_GROUP_NO;
1581 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
1584 bus_no = buf[0]>>16;
1586 scsi_lun = (buf[2]>>8 )&0xff;
1587 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1588 printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
1591 if (scsi_id >= MAX_ID){
1592 printk(KERN_WARNING"%s: SCSI ID %d out of range \n", pHba->name, bus_no);
1595 if(bus_no > pHba->top_scsi_channel){
1596 pHba->top_scsi_channel = bus_no;
1598 if(scsi_id > pHba->top_scsi_id){
1599 pHba->top_scsi_id = scsi_id;
1601 if(scsi_lun > pHba->top_scsi_lun){
1602 pHba->top_scsi_lun = scsi_lun;
1606 d = kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
1609 printk(KERN_CRIT"%s: Out of memory for I2O device data.\n",pHba->name);
1613 d->controller = pHba;
1616 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
1619 tid = d->lct_data.tid;
1620 adpt_i2o_report_hba_unit(pHba, d);
1621 adpt_i2o_install_device(pHba, d);
1624 for(d = pHba->devices; d ; d = d->next) {
1625 if(d->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT ||
1626 d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PORT){
1627 tid = d->lct_data.tid;
1628 // TODO get the bus_no from hrt-but for now they are in order
1630 if(bus_no > pHba->top_scsi_channel){
1631 pHba->top_scsi_channel = bus_no;
1633 pHba->channel[bus_no].type = d->lct_data.class_id;
1634 pHba->channel[bus_no].tid = tid;
1635 if(adpt_i2o_query_scalar(pHba, tid, 0x0200, -1, buf, 28)>=0)
1637 pHba->channel[bus_no].scsi_id = buf[1];
1638 PDEBUG("Bus %d - SCSI ID %d.\n", bus_no, buf[1]);
1640 // TODO remove - this is just until we get from hrt
1642 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1643 printk(KERN_WARNING"%s: Channel number %d out of range - LCT\n", pHba->name, bus_no);
1649 // Setup adpt_device table
1650 for(d = pHba->devices; d ; d = d->next) {
1651 if(d->lct_data.class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
1652 d->lct_data.class_id == I2O_CLASS_SCSI_PERIPHERAL ||
1653 d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1655 tid = d->lct_data.tid;
1657 // I2O_DPT_DEVICE_INFO_GROUP_NO;
1658 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)>=0) {
1659 bus_no = buf[0]>>16;
1661 scsi_lun = (buf[2]>>8 )&0xff;
1662 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1665 if (scsi_id >= MAX_ID) {
1668 if( pHba->channel[bus_no].device[scsi_id] == NULL){
1669 pDev = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
1673 pHba->channel[bus_no].device[scsi_id] = pDev;
1675 for( pDev = pHba->channel[bus_no].device[scsi_id];
1676 pDev->next_lun; pDev = pDev->next_lun){
1678 pDev->next_lun = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
1679 if(pDev->next_lun == NULL) {
1682 pDev = pDev->next_lun;
1685 pDev->scsi_channel = bus_no;
1686 pDev->scsi_id = scsi_id;
1687 pDev->scsi_lun = scsi_lun;
1690 pDev->type = (buf[0])&0xff;
1691 pDev->flags = (buf[0]>>8)&0xff;
1692 if(scsi_id > pHba->top_scsi_id){
1693 pHba->top_scsi_id = scsi_id;
1695 if(scsi_lun > pHba->top_scsi_lun){
1696 pHba->top_scsi_lun = scsi_lun;
1700 printk(KERN_WARNING"Could not find SCSI ID for %s\n",
1701 d->lct_data.identity_tag);
1710 * Each I2O controller has a chain of devices on it - these match
1711 * the useful parts of the LCT of the board.
1714 static int adpt_i2o_install_device(adpt_hba* pHba, struct i2o_device *d)
1716 mutex_lock(&adpt_configuration_lock);
1719 d->next=pHba->devices;
1721 if (pHba->devices != NULL){
1722 pHba->devices->prev=d;
1727 mutex_unlock(&adpt_configuration_lock);
1731 static int adpt_open(struct inode *inode, struct file *file)
1736 mutex_lock(&adpt_mutex);
1737 //TODO check for root access
1739 minor = iminor(inode);
1740 if (minor >= hba_count) {
1741 mutex_unlock(&adpt_mutex);
1744 mutex_lock(&adpt_configuration_lock);
1745 for (pHba = hba_chain; pHba; pHba = pHba->next) {
1746 if (pHba->unit == minor) {
1747 break; /* found adapter */
1751 mutex_unlock(&adpt_configuration_lock);
1752 mutex_unlock(&adpt_mutex);
1756 // if(pHba->in_use){
1757 // mutex_unlock(&adpt_configuration_lock);
1762 mutex_unlock(&adpt_configuration_lock);
1763 mutex_unlock(&adpt_mutex);
1768 static int adpt_close(struct inode *inode, struct file *file)
1773 minor = iminor(inode);
1774 if (minor >= hba_count) {
1777 mutex_lock(&adpt_configuration_lock);
1778 for (pHba = hba_chain; pHba; pHba = pHba->next) {
1779 if (pHba->unit == minor) {
1780 break; /* found adapter */
1783 mutex_unlock(&adpt_configuration_lock);
1794 static int adpt_i2o_passthru(adpt_hba* pHba, u32 __user *arg)
1796 u32 msg[MAX_MESSAGE_SIZE];
1800 u32 __user *user_msg = arg;
1801 u32 __user * user_reply = NULL;
1802 void *sg_list[pHba->sg_tablesize];
1812 memset(&msg, 0, MAX_MESSAGE_SIZE*4);
1813 // get user msg size in u32s
1814 if(get_user(size, &user_msg[0])){
1819 user_reply = &user_msg[size];
1820 if(size > MAX_MESSAGE_SIZE){
1823 size *= 4; // Convert to bytes
1825 /* Copy in the user's I2O command */
1826 if(copy_from_user(msg, user_msg, size)) {
1829 get_user(reply_size, &user_reply[0]);
1830 reply_size = reply_size>>16;
1831 if(reply_size > REPLY_FRAME_SIZE){
1832 reply_size = REPLY_FRAME_SIZE;
1835 reply = kzalloc(REPLY_FRAME_SIZE*4, GFP_KERNEL);
1837 printk(KERN_WARNING"%s: Could not allocate reply buffer\n",pHba->name);
1840 sg_offset = (msg[0]>>4)&0xf;
1841 msg[2] = 0x40000000; // IOCTL context
1842 msg[3] = adpt_ioctl_to_context(pHba, reply);
1843 if (msg[3] == (u32)-1)
1846 memset(sg_list,0, sizeof(sg_list[0])*pHba->sg_tablesize);
1848 // TODO add 64 bit API
1849 struct sg_simple_element *sg = (struct sg_simple_element*) (msg+sg_offset);
1850 sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
1851 if (sg_count > pHba->sg_tablesize){
1852 printk(KERN_DEBUG"%s:IOCTL SG List too large (%u)\n", pHba->name,sg_count);
1857 for(i = 0; i < sg_count; i++) {
1860 if (!(sg[i].flag_count & 0x10000000 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT*/)) {
1861 printk(KERN_DEBUG"%s:Bad SG element %d - not simple (%x)\n",pHba->name,i, sg[i].flag_count);
1865 sg_size = sg[i].flag_count & 0xffffff;
1866 /* Allocate memory for the transfer */
1867 p = dma_alloc_coherent(&pHba->pDev->dev, sg_size, &addr, GFP_KERNEL);
1869 printk(KERN_DEBUG"%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
1870 pHba->name,sg_size,i,sg_count);
1874 sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
1875 /* Copy in the user's SG buffer if necessary */
1876 if(sg[i].flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR*/) {
1877 // sg_simple_element API is 32 bit
1878 if (copy_from_user(p,(void __user *)(ulong)sg[i].addr_bus, sg_size)) {
1879 printk(KERN_DEBUG"%s: Could not copy SG buf %d FROM user\n",pHba->name,i);
1884 /* sg_simple_element API is 32 bit, but addr < 4GB */
1885 sg[i].addr_bus = addr;
1891 spin_lock_irqsave(pHba->host->host_lock, flags);
1892 // This state stops any new commands from enterring the
1893 // controller while processing the ioctl
1894 // pHba->state |= DPTI_STATE_IOCTL;
1895 // We can't set this now - The scsi subsystem sets host_blocked and
1896 // the queue empties and stops. We need a way to restart the queue
1897 rcode = adpt_i2o_post_wait(pHba, msg, size, FOREVER);
1899 printk("adpt_i2o_passthru: post wait failed %d %p\n",
1901 // pHba->state &= ~DPTI_STATE_IOCTL;
1903 spin_unlock_irqrestore(pHba->host->host_lock, flags);
1904 } while(rcode == -ETIMEDOUT);
1911 /* Copy back the Scatter Gather buffers back to user space */
1913 // TODO add 64 bit API
1914 struct sg_simple_element* sg;
1917 // re-acquire the original message to handle correctly the sg copy operation
1918 memset(&msg, 0, MAX_MESSAGE_SIZE*4);
1919 // get user msg size in u32s
1920 if(get_user(size, &user_msg[0])){
1926 if (size > MAX_MESSAGE_SIZE) {
1930 /* Copy in the user's I2O command */
1931 if (copy_from_user (msg, user_msg, size)) {
1935 sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
1937 // TODO add 64 bit API
1938 sg = (struct sg_simple_element*)(msg + sg_offset);
1939 for (j = 0; j < sg_count; j++) {
1940 /* Copy out the SG list to user's buffer if necessary */
1941 if(! (sg[j].flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR*/)) {
1942 sg_size = sg[j].flag_count & 0xffffff;
1943 // sg_simple_element API is 32 bit
1944 if (copy_to_user((void __user *)(ulong)sg[j].addr_bus,sg_list[j], sg_size)) {
1945 printk(KERN_WARNING"%s: Could not copy %p TO user %x\n",pHba->name, sg_list[j], sg[j].addr_bus);
1953 /* Copy back the reply to user space */
1955 // we wrote our own values for context - now restore the user supplied ones
1956 if(copy_from_user(reply+2, user_msg+2, sizeof(u32)*2)) {
1957 printk(KERN_WARNING"%s: Could not copy message context FROM user\n",pHba->name);
1960 if(copy_to_user(user_reply, reply, reply_size)) {
1961 printk(KERN_WARNING"%s: Could not copy reply TO user\n",pHba->name);
1968 if (rcode != -ETIME && rcode != -EINTR) {
1969 struct sg_simple_element *sg =
1970 (struct sg_simple_element*) (msg +sg_offset);
1973 if(sg_list[--sg_index]) {
1974 dma_free_coherent(&pHba->pDev->dev,
1975 sg[sg_index].flag_count & 0xffffff,
1977 sg[sg_index].addr_bus);
1984 #if defined __ia64__
1985 static void adpt_ia64_info(sysInfo_S* si)
1987 // This is all the info we need for now
1988 // We will add more info as our new
1989 // managmenent utility requires it
1990 si->processorType = PROC_IA64;
1994 #if defined __sparc__
1995 static void adpt_sparc_info(sysInfo_S* si)
1997 // This is all the info we need for now
1998 // We will add more info as our new
1999 // managmenent utility requires it
2000 si->processorType = PROC_ULTRASPARC;
2003 #if defined __alpha__
2004 static void adpt_alpha_info(sysInfo_S* si)
2006 // This is all the info we need for now
2007 // We will add more info as our new
2008 // managmenent utility requires it
2009 si->processorType = PROC_ALPHA;
2013 #if defined __i386__
2014 static void adpt_i386_info(sysInfo_S* si)
2016 // This is all the info we need for now
2017 // We will add more info as our new
2018 // managmenent utility requires it
2019 switch (boot_cpu_data.x86) {
2021 si->processorType = PROC_386;
2024 si->processorType = PROC_486;
2027 si->processorType = PROC_PENTIUM;
2029 default: // Just in case
2030 si->processorType = PROC_PENTIUM;
2037 * This routine returns information about the system. This does not effect
2038 * any logic and if the info is wrong - it doesn't matter.
2041 /* Get all the info we can not get from kernel services */
2042 static int adpt_system_info(void __user *buffer)
2046 memset(&si, 0, sizeof(si));
2048 si.osType = OS_LINUX;
2049 si.osMajorVersion = 0;
2050 si.osMinorVersion = 0;
2052 si.busType = SI_PCI_BUS;
2053 si.processorFamily = DPTI_sig.dsProcessorFamily;
2055 #if defined __i386__
2056 adpt_i386_info(&si);
2057 #elif defined (__ia64__)
2058 adpt_ia64_info(&si);
2059 #elif defined(__sparc__)
2060 adpt_sparc_info(&si);
2061 #elif defined (__alpha__)
2062 adpt_alpha_info(&si);
2064 si.processorType = 0xff ;
2066 if (copy_to_user(buffer, &si, sizeof(si))){
2067 printk(KERN_WARNING"dpti: Could not copy buffer TO user\n");
2074 static int adpt_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
2080 void __user *argp = (void __user *)arg;
2082 minor = iminor(inode);
2083 if (minor >= DPTI_MAX_HBA){
2086 mutex_lock(&adpt_configuration_lock);
2087 for (pHba = hba_chain; pHba; pHba = pHba->next) {
2088 if (pHba->unit == minor) {
2089 break; /* found adapter */
2092 mutex_unlock(&adpt_configuration_lock);
2097 while((volatile u32) pHba->state & DPTI_STATE_RESET )
2098 schedule_timeout_uninterruptible(2);
2101 // TODO: handle 3 cases
2103 if (copy_to_user(argp, &DPTI_sig, sizeof(DPTI_sig))) {
2108 return adpt_i2o_passthru(pHba, argp);
2111 drvrHBAinfo_S HbaInfo;
2113 #define FLG_OSD_PCI_VALID 0x0001
2114 #define FLG_OSD_DMA 0x0002
2115 #define FLG_OSD_I2O 0x0004
2116 memset(&HbaInfo, 0, sizeof(HbaInfo));
2117 HbaInfo.drvrHBAnum = pHba->unit;
2118 HbaInfo.baseAddr = (ulong) pHba->base_addr_phys;
2119 HbaInfo.blinkState = adpt_read_blink_led(pHba);
2120 HbaInfo.pciBusNum = pHba->pDev->bus->number;
2121 HbaInfo.pciDeviceNum=PCI_SLOT(pHba->pDev->devfn);
2122 HbaInfo.Interrupt = pHba->pDev->irq;
2123 HbaInfo.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
2124 if(copy_to_user(argp, &HbaInfo, sizeof(HbaInfo))){
2125 printk(KERN_WARNING"%s: Could not copy HbaInfo TO user\n",pHba->name);
2131 return adpt_system_info(argp);
2134 value = (u32)adpt_read_blink_led(pHba);
2135 if (copy_to_user(argp, &value, sizeof(value))) {
2142 spin_lock_irqsave(pHba->host->host_lock, flags);
2143 adpt_hba_reset(pHba);
2145 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2157 static long adpt_unlocked_ioctl(struct file *file, uint cmd, ulong arg)
2159 struct inode *inode;
2162 inode = file->f_dentry->d_inode;
2164 mutex_lock(&adpt_mutex);
2165 ret = adpt_ioctl(inode, file, cmd, arg);
2166 mutex_unlock(&adpt_mutex);
2171 #ifdef CONFIG_COMPAT
2172 static long compat_adpt_ioctl(struct file *file,
2173 unsigned int cmd, unsigned long arg)
2175 struct inode *inode;
2178 inode = file->f_dentry->d_inode;
2180 mutex_lock(&adpt_mutex);
2190 case (DPT_TARGET_BUSY & 0xFFFF):
2191 case DPT_TARGET_BUSY:
2192 ret = adpt_ioctl(inode, file, cmd, arg);
2198 mutex_unlock(&adpt_mutex);
2204 static irqreturn_t adpt_isr(int irq, void *dev_id)
2206 struct scsi_cmnd* cmd;
2207 adpt_hba* pHba = dev_id;
2209 void __iomem *reply;
2216 printk(KERN_WARNING"adpt_isr: NULL dev_id\n");
2220 spin_lock_irqsave(pHba->host->host_lock, flags);
2222 while( readl(pHba->irq_mask) & I2O_INTERRUPT_PENDING_B) {
2223 m = readl(pHba->reply_port);
2224 if(m == EMPTY_QUEUE){
2225 // Try twice then give up
2227 m = readl(pHba->reply_port);
2228 if(m == EMPTY_QUEUE){
2229 // This really should not happen
2230 printk(KERN_ERR"dpti: Could not get reply frame\n");
2234 if (pHba->reply_pool_pa <= m &&
2235 m < pHba->reply_pool_pa +
2236 (pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4)) {
2237 reply = (u8 *)pHba->reply_pool +
2238 (m - pHba->reply_pool_pa);
2240 /* Ick, we should *never* be here */
2241 printk(KERN_ERR "dpti: reply frame not from pool\n");
2242 reply = (u8 *)bus_to_virt(m);
2245 if (readl(reply) & MSG_FAIL) {
2246 u32 old_m = readl(reply+28);
2249 PDEBUG("%s: Failed message\n",pHba->name);
2250 if(old_m >= 0x100000){
2251 printk(KERN_ERR"%s: Bad preserved MFA (%x)- dropping frame\n",pHba->name,old_m);
2252 writel(m,pHba->reply_port);
2255 // Transaction context is 0 in failed reply frame
2256 msg = pHba->msg_addr_virt + old_m;
2257 old_context = readl(msg+12);
2258 writel(old_context, reply+12);
2259 adpt_send_nop(pHba, old_m);
2261 context = readl(reply+8);
2262 if(context & 0x40000000){ // IOCTL
2263 void *p = adpt_ioctl_from_context(pHba, readl(reply+12));
2265 memcpy_fromio(p, reply, REPLY_FRAME_SIZE * 4);
2267 // All IOCTLs will also be post wait
2269 if(context & 0x80000000){ // Post wait message
2270 status = readl(reply+16);
2272 status &= 0xffff; /* Get detail status */
2274 status = I2O_POST_WAIT_OK;
2276 if(!(context & 0x40000000)) {
2277 cmd = adpt_cmd_from_context(pHba,
2280 printk(KERN_WARNING"%s: Apparent SCSI cmd in Post Wait Context - cmd=%p context=%x\n", pHba->name, cmd, context);
2283 adpt_i2o_post_wait_complete(context, status);
2284 } else { // SCSI message
2285 cmd = adpt_cmd_from_context (pHba, readl(reply+12));
2287 scsi_dma_unmap(cmd);
2288 if(cmd->serial_number != 0) { // If not timedout
2289 adpt_i2o_to_scsi(reply, cmd);
2293 writel(m, pHba->reply_port);
2299 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2300 return IRQ_RETVAL(handled);
2303 static s32 adpt_scsi_to_i2o(adpt_hba* pHba, struct scsi_cmnd* cmd, struct adpt_device* d)
2306 u32 msg[MAX_MESSAGE_SIZE];
2318 memset(msg, 0 , sizeof(msg));
2319 len = scsi_bufflen(cmd);
2320 direction = 0x00000000;
2322 scsidir = 0x00000000; // DATA NO XFER
2325 * Set SCBFlags to indicate if data is being transferred
2326 * in or out, or no data transfer
2327 * Note: Do not have to verify index is less than 0 since
2328 * cmd->cmnd[0] is an unsigned char
2330 switch(cmd->sc_data_direction){
2331 case DMA_FROM_DEVICE:
2332 scsidir =0x40000000; // DATA IN (iop<--dev)
2335 direction=0x04000000; // SGL OUT
2336 scsidir =0x80000000; // DATA OUT (iop-->dev)
2340 case DMA_BIDIRECTIONAL:
2341 scsidir =0x40000000; // DATA IN (iop<--dev)
2342 // Assume In - and continue;
2345 printk(KERN_WARNING"%s: scsi opcode 0x%x not supported.\n",
2346 pHba->name, cmd->cmnd[0]);
2347 cmd->result = (DID_OK <<16) | (INITIATOR_ERROR << 8);
2348 cmd->scsi_done(cmd);
2352 // msg[0] is set later
2353 // I2O_CMD_SCSI_EXEC
2354 msg[1] = ((0xff<<24)|(HOST_TID<<12)|d->tid);
2356 msg[3] = adpt_cmd_to_context(cmd); /* Want SCSI control block back */
2357 // Our cards use the transaction context as the tag for queueing
2358 // Adaptec/DPT Private stuff
2359 msg[4] = I2O_CMD_SCSI_EXEC|(DPT_ORGANIZATION_ID<<16);
2361 /* Direction, disconnect ok | sense data | simple queue , CDBLen */
2362 // I2O_SCB_FLAG_ENABLE_DISCONNECT |
2363 // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
2364 // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
2365 msg[6] = scsidir|0x20a00000|cmd->cmd_len;
2369 // Write SCSI command into the message - always 16 byte block
2370 memset(mptr, 0, 16);
2371 memcpy(mptr, cmd->cmnd, cmd->cmd_len);
2373 lenptr=mptr++; /* Remember me - fill in when we know */
2374 if (dpt_dma64(pHba)) {
2375 reqlen = 16; // SINGLE SGE
2376 *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
2377 *mptr++ = 1 << PAGE_SHIFT;
2379 reqlen = 14; // SINGLE SGE
2381 /* Now fill in the SGList and command */
2383 nseg = scsi_dma_map(cmd);
2386 struct scatterlist *sg;
2389 scsi_for_each_sg(cmd, sg, nseg, i) {
2391 *mptr++ = direction|0x10000000|sg_dma_len(sg);
2392 len+=sg_dma_len(sg);
2393 addr = sg_dma_address(sg);
2394 *mptr++ = dma_low(addr);
2395 if (dpt_dma64(pHba))
2396 *mptr++ = dma_high(addr);
2397 /* Make this an end of list */
2399 *lptr = direction|0xD0000000|sg_dma_len(sg);
2401 reqlen = mptr - msg;
2404 if(cmd->underflow && len != cmd->underflow){
2405 printk(KERN_WARNING"Cmd len %08X Cmd underflow %08X\n",
2406 len, cmd->underflow);
2413 /* Stick the headers on */
2414 msg[0] = reqlen<<16 | ((reqlen > 12) ? SGL_OFFSET_12 : SGL_OFFSET_0);
2416 // Send it on it's way
2417 rcode = adpt_i2o_post_this(pHba, msg, reqlen<<2);
2425 static s32 adpt_scsi_host_alloc(adpt_hba* pHba, struct scsi_host_template *sht)
2427 struct Scsi_Host *host;
2429 host = scsi_host_alloc(sht, sizeof(adpt_hba*));
2431 printk("%s: scsi_host_alloc returned NULL\n", pHba->name);
2434 host->hostdata[0] = (unsigned long)pHba;
2437 host->irq = pHba->pDev->irq;
2438 /* no IO ports, so don't have to set host->io_port and
2442 host->n_io_port = 0;
2443 /* see comments in scsi_host.h */
2445 host->max_lun = 256;
2446 host->max_channel = pHba->top_scsi_channel + 1;
2447 host->cmd_per_lun = 1;
2448 host->unique_id = (u32)sys_tbl_pa + pHba->unit;
2449 host->sg_tablesize = pHba->sg_tablesize;
2450 host->can_queue = pHba->post_fifo_size;
2456 static s32 adpt_i2o_to_scsi(void __iomem *reply, struct scsi_cmnd* cmd)
2461 u32 reply_flags = readl(reply) & 0xff00; // Leave it shifted up 8 bits
2462 // I know this would look cleaner if I just read bytes
2463 // but the model I have been using for all the rest of the
2464 // io is in 4 byte words - so I keep that model
2465 u16 detailed_status = readl(reply+16) &0xffff;
2466 dev_status = (detailed_status & 0xff);
2467 hba_status = detailed_status >> 8;
2469 // calculate resid for sg
2470 scsi_set_resid(cmd, scsi_bufflen(cmd) - readl(reply+20));
2472 pHba = (adpt_hba*) cmd->device->host->hostdata[0];
2474 cmd->sense_buffer[0] = '\0'; // initialize sense valid flag to false
2476 if(!(reply_flags & MSG_FAIL)) {
2477 switch(detailed_status & I2O_SCSI_DSC_MASK) {
2478 case I2O_SCSI_DSC_SUCCESS:
2479 cmd->result = (DID_OK << 16);
2481 if (readl(reply+20) < cmd->underflow) {
2482 cmd->result = (DID_ERROR <<16);
2483 printk(KERN_WARNING"%s: SCSI CMD underflow\n",pHba->name);
2486 case I2O_SCSI_DSC_REQUEST_ABORTED:
2487 cmd->result = (DID_ABORT << 16);
2489 case I2O_SCSI_DSC_PATH_INVALID:
2490 case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
2491 case I2O_SCSI_DSC_SELECTION_TIMEOUT:
2492 case I2O_SCSI_DSC_COMMAND_TIMEOUT:
2493 case I2O_SCSI_DSC_NO_ADAPTER:
2494 case I2O_SCSI_DSC_RESOURCE_UNAVAILABLE:
2495 printk(KERN_WARNING"%s: SCSI Timeout-Device (%d,%d,%d) hba status=0x%x, dev status=0x%x, cmd=0x%x\n",
2496 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun, hba_status, dev_status, cmd->cmnd[0]);
2497 cmd->result = (DID_TIME_OUT << 16);
2499 case I2O_SCSI_DSC_ADAPTER_BUSY:
2500 case I2O_SCSI_DSC_BUS_BUSY:
2501 cmd->result = (DID_BUS_BUSY << 16);
2503 case I2O_SCSI_DSC_SCSI_BUS_RESET:
2504 case I2O_SCSI_DSC_BDR_MESSAGE_SENT:
2505 cmd->result = (DID_RESET << 16);
2507 case I2O_SCSI_DSC_PARITY_ERROR_FAILURE:
2508 printk(KERN_WARNING"%s: SCSI CMD parity error\n",pHba->name);
2509 cmd->result = (DID_PARITY << 16);
2511 case I2O_SCSI_DSC_UNABLE_TO_ABORT:
2512 case I2O_SCSI_DSC_COMPLETE_WITH_ERROR:
2513 case I2O_SCSI_DSC_UNABLE_TO_TERMINATE:
2514 case I2O_SCSI_DSC_MR_MESSAGE_RECEIVED:
2515 case I2O_SCSI_DSC_AUTOSENSE_FAILED:
2516 case I2O_SCSI_DSC_DATA_OVERRUN:
2517 case I2O_SCSI_DSC_UNEXPECTED_BUS_FREE:
2518 case I2O_SCSI_DSC_SEQUENCE_FAILURE:
2519 case I2O_SCSI_DSC_REQUEST_LENGTH_ERROR:
2520 case I2O_SCSI_DSC_PROVIDE_FAILURE:
2521 case I2O_SCSI_DSC_REQUEST_TERMINATED:
2522 case I2O_SCSI_DSC_IDE_MESSAGE_SENT:
2523 case I2O_SCSI_DSC_UNACKNOWLEDGED_EVENT:
2524 case I2O_SCSI_DSC_MESSAGE_RECEIVED:
2525 case I2O_SCSI_DSC_INVALID_CDB:
2526 case I2O_SCSI_DSC_LUN_INVALID:
2527 case I2O_SCSI_DSC_SCSI_TID_INVALID:
2528 case I2O_SCSI_DSC_FUNCTION_UNAVAILABLE:
2529 case I2O_SCSI_DSC_NO_NEXUS:
2530 case I2O_SCSI_DSC_CDB_RECEIVED:
2531 case I2O_SCSI_DSC_LUN_ALREADY_ENABLED:
2532 case I2O_SCSI_DSC_QUEUE_FROZEN:
2533 case I2O_SCSI_DSC_REQUEST_INVALID:
2535 printk(KERN_WARNING"%s: SCSI error %0x-Device(%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2536 pHba->name, detailed_status & I2O_SCSI_DSC_MASK, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
2537 hba_status, dev_status, cmd->cmnd[0]);
2538 cmd->result = (DID_ERROR << 16);
2542 // copy over the request sense data if it was a check
2544 if (dev_status == SAM_STAT_CHECK_CONDITION) {
2545 u32 len = min(SCSI_SENSE_BUFFERSIZE, 40);
2546 // Copy over the sense data
2547 memcpy_fromio(cmd->sense_buffer, (reply+28) , len);
2548 if(cmd->sense_buffer[0] == 0x70 /* class 7 */ &&
2549 cmd->sense_buffer[2] == DATA_PROTECT ){
2550 /* This is to handle an array failed */
2551 cmd->result = (DID_TIME_OUT << 16);
2552 printk(KERN_WARNING"%s: SCSI Data Protect-Device (%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2553 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
2554 hba_status, dev_status, cmd->cmnd[0]);
2559 /* In this condtion we could not talk to the tid
2560 * the card rejected it. We should signal a retry
2561 * for a limitted number of retries.
2563 cmd->result = (DID_TIME_OUT << 16);
2564 printk(KERN_WARNING"%s: I2O MSG_FAIL - Device (%d,%d,%d) tid=%d, cmd=0x%x\n",
2565 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
2566 ((struct adpt_device*)(cmd->device->hostdata))->tid, cmd->cmnd[0]);
2569 cmd->result |= (dev_status);
2571 if(cmd->scsi_done != NULL){
2572 cmd->scsi_done(cmd);
2578 static s32 adpt_rescan(adpt_hba* pHba)
2584 spin_lock_irqsave(pHba->host->host_lock, flags);
2585 if ((rcode=adpt_i2o_lct_get(pHba)) < 0)
2587 if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0)
2591 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2596 static s32 adpt_i2o_reparse_lct(adpt_hba* pHba)
2601 struct i2o_device *d;
2602 i2o_lct *lct = pHba->lct;
2606 u32 buf[10]; // at least 8 u32's
2607 struct adpt_device* pDev = NULL;
2608 struct i2o_device* pI2o_dev = NULL;
2611 printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
2615 max = lct->table_size;
2619 // Mark each drive as unscanned
2620 for (d = pHba->devices; d; d = d->next) {
2621 pDev =(struct adpt_device*) d->owner;
2625 pDev->state |= DPTI_DEV_UNSCANNED;
2628 printk(KERN_INFO "%s: LCT has %d entries.\n", pHba->name,max);
2630 for(i=0;i<max;i++) {
2631 if( lct->lct_entry[i].user_tid != 0xfff){
2635 if( lct->lct_entry[i].class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
2636 lct->lct_entry[i].class_id == I2O_CLASS_SCSI_PERIPHERAL ||
2637 lct->lct_entry[i].class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
2638 tid = lct->lct_entry[i].tid;
2639 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
2640 printk(KERN_ERR"%s: Could not query device\n",pHba->name);
2643 bus_no = buf[0]>>16;
2644 if (bus_no >= MAX_CHANNEL) { /* Something wrong skip it */
2646 "%s: Channel number %d out of range\n",
2647 pHba->name, bus_no);
2652 scsi_lun = (buf[2]>>8 )&0xff;
2653 pDev = pHba->channel[bus_no].device[scsi_id];
2656 if(pDev->scsi_lun == scsi_lun) {
2659 pDev = pDev->next_lun;
2661 if(!pDev ) { // Something new add it
2662 d = kmalloc(sizeof(struct i2o_device),
2666 printk(KERN_CRIT "Out of memory for I2O device data.\n");
2670 d->controller = pHba;
2673 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2676 adpt_i2o_report_hba_unit(pHba, d);
2677 adpt_i2o_install_device(pHba, d);
2679 pDev = pHba->channel[bus_no].device[scsi_id];
2682 kzalloc(sizeof(struct adpt_device),
2687 pHba->channel[bus_no].device[scsi_id] = pDev;
2689 while (pDev->next_lun) {
2690 pDev = pDev->next_lun;
2692 pDev = pDev->next_lun =
2693 kzalloc(sizeof(struct adpt_device),
2699 pDev->tid = d->lct_data.tid;
2700 pDev->scsi_channel = bus_no;
2701 pDev->scsi_id = scsi_id;
2702 pDev->scsi_lun = scsi_lun;
2705 pDev->type = (buf[0])&0xff;
2706 pDev->flags = (buf[0]>>8)&0xff;
2707 // Too late, SCSI system has made up it's mind, but what the hey ...
2708 if(scsi_id > pHba->top_scsi_id){
2709 pHba->top_scsi_id = scsi_id;
2711 if(scsi_lun > pHba->top_scsi_lun){
2712 pHba->top_scsi_lun = scsi_lun;
2715 } // end of new i2o device
2717 // We found an old device - check it
2719 if(pDev->scsi_lun == scsi_lun) {
2720 if(!scsi_device_online(pDev->pScsi_dev)) {
2721 printk(KERN_WARNING"%s: Setting device (%d,%d,%d) back online\n",
2722 pHba->name,bus_no,scsi_id,scsi_lun);
2723 if (pDev->pScsi_dev) {
2724 scsi_device_set_state(pDev->pScsi_dev, SDEV_RUNNING);
2728 if(d->lct_data.tid != tid) { // something changed
2730 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2731 if (pDev->pScsi_dev) {
2732 pDev->pScsi_dev->changed = TRUE;
2733 pDev->pScsi_dev->removable = TRUE;
2736 // Found it - mark it scanned
2737 pDev->state = DPTI_DEV_ONLINE;
2740 pDev = pDev->next_lun;
2744 for (pI2o_dev = pHba->devices; pI2o_dev; pI2o_dev = pI2o_dev->next) {
2745 pDev =(struct adpt_device*) pI2o_dev->owner;
2749 // Drive offline drives that previously existed but could not be found
2751 if (pDev->state & DPTI_DEV_UNSCANNED){
2752 pDev->state = DPTI_DEV_OFFLINE;
2753 printk(KERN_WARNING"%s: Device (%d,%d,%d) offline\n",pHba->name,pDev->scsi_channel,pDev->scsi_id,pDev->scsi_lun);
2754 if (pDev->pScsi_dev) {
2755 scsi_device_set_state(pDev->pScsi_dev, SDEV_OFFLINE);
2762 static void adpt_fail_posted_scbs(adpt_hba* pHba)
2764 struct scsi_cmnd* cmd = NULL;
2765 struct scsi_device* d = NULL;
2767 shost_for_each_device(d, pHba->host) {
2768 unsigned long flags;
2769 spin_lock_irqsave(&d->list_lock, flags);
2770 list_for_each_entry(cmd, &d->cmd_list, list) {
2771 if(cmd->serial_number == 0){
2774 cmd->result = (DID_OK << 16) | (QUEUE_FULL <<1);
2775 cmd->scsi_done(cmd);
2777 spin_unlock_irqrestore(&d->list_lock, flags);
2782 /*============================================================================
2783 * Routines from i2o subsystem
2784 *============================================================================
2790 * Bring an I2O controller into HOLD state. See the spec.
2792 static int adpt_i2o_activate_hba(adpt_hba* pHba)
2796 if(pHba->initialized ) {
2797 if (adpt_i2o_status_get(pHba) < 0) {
2798 if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2799 printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2802 if (adpt_i2o_status_get(pHba) < 0) {
2803 printk(KERN_INFO "HBA not responding.\n");
2808 if(pHba->status_block->iop_state == ADAPTER_STATE_FAULTED) {
2809 printk(KERN_CRIT "%s: hardware fault\n", pHba->name);
2813 if (pHba->status_block->iop_state == ADAPTER_STATE_READY ||
2814 pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
2815 pHba->status_block->iop_state == ADAPTER_STATE_HOLD ||
2816 pHba->status_block->iop_state == ADAPTER_STATE_FAILED) {
2817 adpt_i2o_reset_hba(pHba);
2818 if (adpt_i2o_status_get(pHba) < 0 || pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
2819 printk(KERN_ERR "%s: Failed to initialize.\n", pHba->name);
2824 if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2825 printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2831 if (adpt_i2o_init_outbound_q(pHba) < 0) {
2837 if (adpt_i2o_hrt_get(pHba) < 0) {
2845 * Bring a controller online into OPERATIONAL state.
2848 static int adpt_i2o_online_hba(adpt_hba* pHba)
2850 if (adpt_i2o_systab_send(pHba) < 0) {
2851 adpt_i2o_delete_hba(pHba);
2854 /* In READY state */
2856 if (adpt_i2o_enable_hba(pHba) < 0) {
2857 adpt_i2o_delete_hba(pHba);
2861 /* In OPERATIONAL state */
2865 static s32 adpt_send_nop(adpt_hba*pHba,u32 m)
2868 ulong timeout = jiffies + 5*HZ;
2870 while(m == EMPTY_QUEUE){
2872 m = readl(pHba->post_port);
2873 if(m != EMPTY_QUEUE){
2876 if(time_after(jiffies,timeout)){
2877 printk(KERN_ERR "%s: Timeout waiting for message frame!\n",pHba->name);
2880 schedule_timeout_uninterruptible(1);
2882 msg = (u32 __iomem *)(pHba->msg_addr_virt + m);
2883 writel( THREE_WORD_MSG_SIZE | SGL_OFFSET_0,&msg[0]);
2884 writel( I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0,&msg[1]);
2888 writel(m, pHba->post_port);
2893 static s32 adpt_i2o_init_outbound_q(adpt_hba* pHba)
2897 u32 __iomem *msg = NULL;
2899 ulong timeout = jiffies + TMOUT_INITOUTBOUND*HZ;
2904 m = readl(pHba->post_port);
2905 if (m != EMPTY_QUEUE) {
2909 if(time_after(jiffies,timeout)){
2910 printk(KERN_WARNING"%s: Timeout waiting for message frame\n",pHba->name);
2913 schedule_timeout_uninterruptible(1);
2914 } while(m == EMPTY_QUEUE);
2916 msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
2918 status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
2920 adpt_send_nop(pHba, m);
2921 printk(KERN_WARNING"%s: IOP reset failed - no free memory.\n",
2925 memset(status, 0, 4);
2927 writel(EIGHT_WORD_MSG_SIZE| SGL_OFFSET_6, &msg[0]);
2928 writel(I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID, &msg[1]);
2930 writel(0x0106, &msg[3]); /* Transaction context */
2931 writel(4096, &msg[4]); /* Host page frame size */
2932 writel((REPLY_FRAME_SIZE)<<16|0x80, &msg[5]); /* Outbound msg frame size and Initcode */
2933 writel(0xD0000004, &msg[6]); /* Simple SG LE, EOB */
2934 writel((u32)addr, &msg[7]);
2936 writel(m, pHba->post_port);
2939 // Wait for the reply status to come back
2942 if (*status != 0x01 /*I2O_EXEC_OUTBOUND_INIT_IN_PROGRESS*/) {
2947 if(time_after(jiffies,timeout)){
2948 printk(KERN_WARNING"%s: Timeout Initializing\n",pHba->name);
2949 /* We lose 4 bytes of "status" here, but we
2950 cannot free these because controller may
2951 awake and corrupt those bytes at any time */
2952 /* dma_free_coherent(&pHba->pDev->dev, 4, status, addr); */
2955 schedule_timeout_uninterruptible(1);
2958 // If the command was successful, fill the fifo with our reply
2960 if(*status != 0x04 /*I2O_EXEC_OUTBOUND_INIT_COMPLETE*/) {
2961 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
2964 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
2966 if(pHba->reply_pool != NULL) {
2967 dma_free_coherent(&pHba->pDev->dev,
2968 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
2969 pHba->reply_pool, pHba->reply_pool_pa);
2972 pHba->reply_pool = dma_alloc_coherent(&pHba->pDev->dev,
2973 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
2974 &pHba->reply_pool_pa, GFP_KERNEL);
2975 if (!pHba->reply_pool) {
2976 printk(KERN_ERR "%s: Could not allocate reply pool\n", pHba->name);
2979 memset(pHba->reply_pool, 0 , pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4);
2981 for(i = 0; i < pHba->reply_fifo_size; i++) {
2982 writel(pHba->reply_pool_pa + (i * REPLY_FRAME_SIZE * 4),
2986 adpt_i2o_status_get(pHba);
2992 * I2O System Table. Contains information about
2993 * all the IOPs in the system. Used to inform IOPs
2994 * about each other's existence.
2996 * sys_tbl_ver is the CurrentChangeIndicator that is
2997 * used by IOPs to track changes.
3002 static s32 adpt_i2o_status_get(adpt_hba* pHba)
3007 u8 *status_block=NULL;
3009 if(pHba->status_block == NULL) {
3010 pHba->status_block = dma_alloc_coherent(&pHba->pDev->dev,
3011 sizeof(i2o_status_block),
3012 &pHba->status_block_pa, GFP_KERNEL);
3013 if(pHba->status_block == NULL) {
3015 "dpti%d: Get Status Block failed; Out of memory. \n",
3020 memset(pHba->status_block, 0, sizeof(i2o_status_block));
3021 status_block = (u8*)(pHba->status_block);
3022 timeout = jiffies+TMOUT_GETSTATUS*HZ;
3025 m = readl(pHba->post_port);
3026 if (m != EMPTY_QUEUE) {
3029 if(time_after(jiffies,timeout)){
3030 printk(KERN_ERR "%s: Timeout waiting for message !\n",
3034 schedule_timeout_uninterruptible(1);
3035 } while(m==EMPTY_QUEUE);
3038 msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
3040 writel(NINE_WORD_MSG_SIZE|SGL_OFFSET_0, &msg[0]);
3041 writel(I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID, &msg[1]);
3046 writel( dma_low(pHba->status_block_pa), &msg[6]);
3047 writel( dma_high(pHba->status_block_pa), &msg[7]);
3048 writel(sizeof(i2o_status_block), &msg[8]); // 88 bytes
3051 writel(m, pHba->post_port);
3054 while(status_block[87]!=0xff){
3055 if(time_after(jiffies,timeout)){
3056 printk(KERN_ERR"dpti%d: Get status timeout.\n",
3061 schedule_timeout_uninterruptible(1);
3064 // Set up our number of outbound and inbound messages
3065 pHba->post_fifo_size = pHba->status_block->max_inbound_frames;
3066 if (pHba->post_fifo_size > MAX_TO_IOP_MESSAGES) {
3067 pHba->post_fifo_size = MAX_TO_IOP_MESSAGES;
3070 pHba->reply_fifo_size = pHba->status_block->max_outbound_frames;
3071 if (pHba->reply_fifo_size > MAX_FROM_IOP_MESSAGES) {
3072 pHba->reply_fifo_size = MAX_FROM_IOP_MESSAGES;
3075 // Calculate the Scatter Gather list size
3076 if (dpt_dma64(pHba)) {
3078 = ((pHba->status_block->inbound_frame_size * 4
3080 / (sizeof(struct sg_simple_element) + sizeof(u32)));
3083 = ((pHba->status_block->inbound_frame_size * 4
3085 / sizeof(struct sg_simple_element));
3087 if (pHba->sg_tablesize > SG_LIST_ELEMENTS) {
3088 pHba->sg_tablesize = SG_LIST_ELEMENTS;
3093 printk("dpti%d: State = ",pHba->unit);
3094 switch(pHba->status_block->iop_state) {
3108 printk("OPERATIONAL\n");
3114 printk("FAULTED\n");
3117 printk("%x (unknown!!)\n",pHba->status_block->iop_state);
3124 * Get the IOP's Logical Configuration Table
3126 static int adpt_i2o_lct_get(adpt_hba* pHba)
3132 if ((pHba->lct_size == 0) || (pHba->lct == NULL)){
3133 pHba->lct_size = pHba->status_block->expected_lct_size;
3136 if (pHba->lct == NULL) {
3137 pHba->lct = dma_alloc_coherent(&pHba->pDev->dev,
3138 pHba->lct_size, &pHba->lct_pa,
3140 if(pHba->lct == NULL) {
3141 printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
3146 memset(pHba->lct, 0, pHba->lct_size);
3148 msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
3149 msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
3152 msg[4] = 0xFFFFFFFF; /* All devices */
3153 msg[5] = 0x00000000; /* Report now */
3154 msg[6] = 0xD0000000|pHba->lct_size;
3155 msg[7] = (u32)pHba->lct_pa;
3157 if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 360))) {
3158 printk(KERN_ERR "%s: LCT Get failed (status=%#10x.\n",
3160 printk(KERN_ERR"Adaptec: Error Reading Hardware.\n");
3164 if ((pHba->lct->table_size << 2) > pHba->lct_size) {
3165 pHba->lct_size = pHba->lct->table_size << 2;
3166 dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
3167 pHba->lct, pHba->lct_pa);
3170 } while (pHba->lct == NULL);
3172 PDEBUG("%s: Hardware resource table read.\n", pHba->name);
3175 // I2O_DPT_EXEC_IOP_BUFFERS_GROUP_NO;
3176 if(adpt_i2o_query_scalar(pHba, 0 , 0x8000, -1, buf, sizeof(buf))>=0) {
3177 pHba->FwDebugBufferSize = buf[1];
3178 pHba->FwDebugBuffer_P = ioremap(pHba->base_addr_phys + buf[0],
3179 pHba->FwDebugBufferSize);
3180 if (pHba->FwDebugBuffer_P) {
3181 pHba->FwDebugFlags_P = pHba->FwDebugBuffer_P +
3182 FW_DEBUG_FLAGS_OFFSET;
3183 pHba->FwDebugBLEDvalue_P = pHba->FwDebugBuffer_P +
3184 FW_DEBUG_BLED_OFFSET;
3185 pHba->FwDebugBLEDflag_P = pHba->FwDebugBLEDvalue_P + 1;
3186 pHba->FwDebugStrLength_P = pHba->FwDebugBuffer_P +
3187 FW_DEBUG_STR_LENGTH_OFFSET;
3188 pHba->FwDebugBuffer_P += buf[2];
3189 pHba->FwDebugFlags = 0;
3196 static int adpt_i2o_build_sys_table(void)
3198 adpt_hba* pHba = hba_chain;
3202 dma_free_coherent(&pHba->pDev->dev, sys_tbl_len,
3203 sys_tbl, sys_tbl_pa);
3205 sys_tbl_len = sizeof(struct i2o_sys_tbl) + // Header + IOPs
3206 (hba_count) * sizeof(struct i2o_sys_tbl_entry);
3208 sys_tbl = dma_alloc_coherent(&pHba->pDev->dev,
3209 sys_tbl_len, &sys_tbl_pa, GFP_KERNEL);
3211 printk(KERN_WARNING "SysTab Set failed. Out of memory.\n");
3214 memset(sys_tbl, 0, sys_tbl_len);
3216 sys_tbl->num_entries = hba_count;
3217 sys_tbl->version = I2OVERSION;
3218 sys_tbl->change_ind = sys_tbl_ind++;
3220 for(pHba = hba_chain; pHba; pHba = pHba->next) {
3222 // Get updated Status Block so we have the latest information
3223 if (adpt_i2o_status_get(pHba)) {
3224 sys_tbl->num_entries--;
3225 continue; // try next one
3228 sys_tbl->iops[count].org_id = pHba->status_block->org_id;
3229 sys_tbl->iops[count].iop_id = pHba->unit + 2;
3230 sys_tbl->iops[count].seg_num = 0;
3231 sys_tbl->iops[count].i2o_version = pHba->status_block->i2o_version;
3232 sys_tbl->iops[count].iop_state = pHba->status_block->iop_state;
3233 sys_tbl->iops[count].msg_type = pHba->status_block->msg_type;
3234 sys_tbl->iops[count].frame_size = pHba->status_block->inbound_frame_size;
3235 sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
3236 sys_tbl->iops[count].iop_capabilities = pHba->status_block->iop_capabilities;
3237 addr = pHba->base_addr_phys + 0x40;
3238 sys_tbl->iops[count].inbound_low = dma_low(addr);
3239 sys_tbl->iops[count].inbound_high = dma_high(addr);
3246 u32 *table = (u32*)sys_tbl;
3247 printk(KERN_DEBUG"sys_tbl_len=%d in 32bit words\n",(sys_tbl_len >>2));
3248 for(count = 0; count < (sys_tbl_len >>2); count++) {
3249 printk(KERN_INFO "sys_tbl[%d] = %0#10x\n",
3250 count, table[count]);
3260 * Dump the information block associated with a given unit (TID)
3263 static void adpt_i2o_report_hba_unit(adpt_hba* pHba, struct i2o_device *d)
3266 int unit = d->lct_data.tid;
3268 printk(KERN_INFO "TID %3.3d ", unit);
3270 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 3, buf, 16)>=0)
3273 printk(" Vendor: %-12.12s", buf);
3275 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 4, buf, 16)>=0)
3278 printk(" Device: %-12.12s", buf);
3280 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 6, buf, 8)>=0)
3283 printk(" Rev: %-12.12s\n", buf);
3286 printk(KERN_INFO "\tClass: %.21s\n", adpt_i2o_get_class_name(d->lct_data.class_id));
3287 printk(KERN_INFO "\tSubclass: 0x%04X\n", d->lct_data.sub_class);
3288 printk(KERN_INFO "\tFlags: ");
3290 if(d->lct_data.device_flags&(1<<0))
3291 printk("C"); // ConfigDialog requested
3292 if(d->lct_data.device_flags&(1<<1))
3293 printk("U"); // Multi-user capable
3294 if(!(d->lct_data.device_flags&(1<<4)))
3295 printk("P"); // Peer service enabled!
3296 if(!(d->lct_data.device_flags&(1<<5)))
3297 printk("M"); // Mgmt service enabled!
3304 * Do i2o class name lookup
3306 static const char *adpt_i2o_get_class_name(int class)
3309 static char *i2o_class_name[] = {
3311 "Device Driver Module",
3316 "Fibre Channel Port",
3317 "Fibre Channel Device",
3321 "Floppy Controller",
3323 "Secondary Bus Port",
3324 "Peer Transport Agent",
3329 switch(class&0xFFF) {
3330 case I2O_CLASS_EXECUTIVE:
3334 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
3336 case I2O_CLASS_SEQUENTIAL_STORAGE:
3342 case I2O_CLASS_FIBRE_CHANNEL_PORT:
3344 case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
3346 case I2O_CLASS_SCSI_PERIPHERAL:
3348 case I2O_CLASS_ATE_PORT:
3350 case I2O_CLASS_ATE_PERIPHERAL:
3352 case I2O_CLASS_FLOPPY_CONTROLLER:
3354 case I2O_CLASS_FLOPPY_DEVICE:
3356 case I2O_CLASS_BUS_ADAPTER_PORT:
3358 case I2O_CLASS_PEER_TRANSPORT_AGENT:
3360 case I2O_CLASS_PEER_TRANSPORT:
3363 return i2o_class_name[idx];
3368 static s32 adpt_i2o_hrt_get(adpt_hba* pHba)
3371 int ret, size = sizeof(i2o_hrt);
3374 if (pHba->hrt == NULL) {
3375 pHba->hrt = dma_alloc_coherent(&pHba->pDev->dev,
3376 size, &pHba->hrt_pa, GFP_KERNEL);
3377 if (pHba->hrt == NULL) {
3378 printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", pHba->name);
3383 msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
3384 msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
3387 msg[4]= (0xD0000000 | size); /* Simple transaction */
3388 msg[5]= (u32)pHba->hrt_pa; /* Dump it here */
3390 if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg),20))) {
3391 printk(KERN_ERR "%s: Unable to get HRT (status=%#10x)\n", pHba->name, ret);
3395 if (pHba->hrt->num_entries * pHba->hrt->entry_len << 2 > size) {
3396 int newsize = pHba->hrt->num_entries * pHba->hrt->entry_len << 2;
3397 dma_free_coherent(&pHba->pDev->dev, size,
3398 pHba->hrt, pHba->hrt_pa);
3402 } while(pHba->hrt == NULL);
3407 * Query one scalar group value or a whole scalar group.
3409 static int adpt_i2o_query_scalar(adpt_hba* pHba, int tid,
3410 int group, int field, void *buf, int buflen)
3412 u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
3414 dma_addr_t opblk_pa;
3416 dma_addr_t resblk_pa;
3420 /* 8 bytes for header */
3421 resblk_va = dma_alloc_coherent(&pHba->pDev->dev,
3422 sizeof(u8) * (8 + buflen), &resblk_pa, GFP_KERNEL);
3423 if (resblk_va == NULL) {
3424 printk(KERN_CRIT "%s: query scalar failed; Out of memory.\n", pHba->name);
3428 opblk_va = dma_alloc_coherent(&pHba->pDev->dev,
3429 sizeof(opblk), &opblk_pa, GFP_KERNEL);
3430 if (opblk_va == NULL) {
3431 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3432 resblk_va, resblk_pa);
3433 printk(KERN_CRIT "%s: query operatio failed; Out of memory.\n",
3437 if (field == -1) /* whole group */
3440 memcpy(opblk_va, opblk, sizeof(opblk));
3441 size = adpt_i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, pHba, tid,
3442 opblk_va, opblk_pa, sizeof(opblk),
3443 resblk_va, resblk_pa, sizeof(u8)*(8+buflen));
3444 dma_free_coherent(&pHba->pDev->dev, sizeof(opblk), opblk_va, opblk_pa);
3445 if (size == -ETIME) {
3446 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3447 resblk_va, resblk_pa);
3448 printk(KERN_WARNING "%s: issue params failed; Timed out.\n", pHba->name);
3450 } else if (size == -EINTR) {
3451 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3452 resblk_va, resblk_pa);
3453 printk(KERN_WARNING "%s: issue params failed; Interrupted.\n", pHba->name);
3457 memcpy(buf, resblk_va+8, buflen); /* cut off header */
3459 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3460 resblk_va, resblk_pa);
3468 /* Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
3470 * This function can be used for all UtilParamsGet/Set operations.
3471 * The OperationBlock is given in opblk-buffer,
3472 * and results are returned in resblk-buffer.
3473 * Note that the minimum sized resblk is 8 bytes and contains
3474 * ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
3476 static int adpt_i2o_issue_params(int cmd, adpt_hba* pHba, int tid,
3477 void *opblk_va, dma_addr_t opblk_pa, int oplen,
3478 void *resblk_va, dma_addr_t resblk_pa, int reslen)
3481 u32 *res = (u32 *)resblk_va;
3484 msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
3485 msg[1] = cmd << 24 | HOST_TID << 12 | tid;
3489 msg[5] = 0x54000000 | oplen; /* OperationBlock */
3490 msg[6] = (u32)opblk_pa;
3491 msg[7] = 0xD0000000 | reslen; /* ResultBlock */
3492 msg[8] = (u32)resblk_pa;
3494 if ((wait_status = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 20))) {
3495 printk("adpt_i2o_issue_params: post_wait failed (%p)\n", resblk_va);
3496 return wait_status; /* -DetailedStatus */
3499 if (res[1]&0x00FF0000) { /* BlockStatus != SUCCESS */
3500 printk(KERN_WARNING "%s: %s - Error:\n ErrorInfoSize = 0x%02x, "
3501 "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
3503 (cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
3505 res[1]>>24, (res[1]>>16)&0xFF, res[1]&0xFFFF);
3506 return -((res[1] >> 16) & 0xFF); /* -BlockStatus */
3509 return 4 + ((res[1] & 0x0000FFFF) << 2); /* bytes used in resblk */
3513 static s32 adpt_i2o_quiesce_hba(adpt_hba* pHba)
3518 adpt_i2o_status_get(pHba);
3520 /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
3522 if((pHba->status_block->iop_state != ADAPTER_STATE_READY) &&
3523 (pHba->status_block->iop_state != ADAPTER_STATE_OPERATIONAL)){
3527 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3528 msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
3532 if((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3533 printk(KERN_INFO"dpti%d: Unable to quiesce (status=%#x).\n",
3536 printk(KERN_INFO"dpti%d: Quiesced.\n",pHba->unit);
3539 adpt_i2o_status_get(pHba);
3545 * Enable IOP. Allows the IOP to resume external operations.
3547 static int adpt_i2o_enable_hba(adpt_hba* pHba)
3552 adpt_i2o_status_get(pHba);
3553 if(!pHba->status_block){
3556 /* Enable only allowed on READY state */
3557 if(pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
3560 if(pHba->status_block->iop_state != ADAPTER_STATE_READY)
3563 msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3564 msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
3568 if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3569 printk(KERN_WARNING"%s: Could not enable (status=%#10x).\n",
3572 PDEBUG("%s: Enabled.\n", pHba->name);
3575 adpt_i2o_status_get(pHba);
3580 static int adpt_i2o_systab_send(adpt_hba* pHba)
3585 msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
3586 msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
3589 msg[4] = (0<<16) | ((pHba->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
3590 msg[5] = 0; /* Segment 0 */
3593 * Provide three SGL-elements:
3594 * System table (SysTab), Private memory space declaration and
3595 * Private i/o space declaration
3597 msg[6] = 0x54000000 | sys_tbl_len;
3598 msg[7] = (u32)sys_tbl_pa;
3599 msg[8] = 0x54000000 | 0;
3601 msg[10] = 0xD4000000 | 0;
3604 if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 120))) {
3605 printk(KERN_INFO "%s: Unable to set SysTab (status=%#10x).\n",
3610 PINFO("%s: SysTab set.\n", pHba->name);
3618 /*============================================================================
3620 *============================================================================
3626 static static void adpt_delay(int millisec)
3629 for (i = 0; i < millisec; i++) {
3630 udelay(1000); /* delay for one millisecond */
3636 static struct scsi_host_template driver_template = {
3637 .module = THIS_MODULE,
3639 .proc_name = "dpt_i2o",
3640 .proc_info = adpt_proc_info,
3642 .queuecommand = adpt_queue,
3643 .eh_abort_handler = adpt_abort,
3644 .eh_device_reset_handler = adpt_device_reset,
3645 .eh_bus_reset_handler = adpt_bus_reset,
3646 .eh_host_reset_handler = adpt_reset,
3647 .bios_param = adpt_bios_param,
3648 .slave_configure = adpt_slave_configure,
3649 .can_queue = MAX_TO_IOP_MESSAGES,
3652 .use_clustering = ENABLE_CLUSTERING,
3655 static int __init adpt_init(void)
3658 adpt_hba *pHba, *next;
3660 printk("Loading Adaptec I2O RAID: Version " DPT_I2O_VERSION "\n");
3662 error = adpt_detect(&driver_template);
3665 if (hba_chain == NULL)
3668 for (pHba = hba_chain; pHba; pHba = pHba->next) {
3669 error = scsi_add_host(pHba->host, &pHba->pDev->dev);
3672 scsi_scan_host(pHba->host);
3676 for (pHba = hba_chain; pHba; pHba = next) {
3678 scsi_remove_host(pHba->host);
3683 static void __exit adpt_exit(void)
3685 adpt_hba *pHba, *next;
3687 for (pHba = hba_chain; pHba; pHba = pHba->next)
3688 scsi_remove_host(pHba->host);
3689 for (pHba = hba_chain; pHba; pHba = next) {
3691 adpt_release(pHba->host);
3695 module_init(adpt_init);
3696 module_exit(adpt_exit);
3698 MODULE_LICENSE("GPL");