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 *============================================================================
184 static struct pci_device_id dptids[] = {
185 { PCI_DPT_VENDOR_ID, PCI_DPT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
186 { PCI_DPT_VENDOR_ID, PCI_DPT_RAPTOR_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
191 MODULE_DEVICE_TABLE(pci,dptids);
193 static int adpt_detect(struct scsi_host_template* sht)
195 struct pci_dev *pDev = NULL;
199 PINFO("Detecting Adaptec I2O RAID controllers...\n");
201 /* search for all Adatpec I2O RAID cards */
202 while ((pDev = pci_get_device( PCI_DPT_VENDOR_ID, PCI_ANY_ID, pDev))) {
203 if(pDev->device == PCI_DPT_DEVICE_ID ||
204 pDev->device == PCI_DPT_RAPTOR_DEVICE_ID){
205 if(adpt_install_hba(sht, pDev) ){
206 PERROR("Could not Init an I2O RAID device\n");
207 PERROR("Will not try to detect others.\n");
214 /* In INIT state, Activate IOPs */
215 for (pHba = hba_chain; pHba; pHba = next) {
217 // Activate does get status , init outbound, and get hrt
218 if (adpt_i2o_activate_hba(pHba) < 0) {
219 adpt_i2o_delete_hba(pHba);
224 /* Active IOPs in HOLD state */
227 if (hba_chain == NULL)
231 * If build_sys_table fails, we kill everything and bail
232 * as we can't init the IOPs w/o a system table
234 if (adpt_i2o_build_sys_table() < 0) {
235 adpt_i2o_sys_shutdown();
239 PDEBUG("HBA's in HOLD state\n");
241 /* If IOP don't get online, we need to rebuild the System table */
242 for (pHba = hba_chain; pHba; pHba = pHba->next) {
243 if (adpt_i2o_online_hba(pHba) < 0) {
244 adpt_i2o_delete_hba(pHba);
245 goto rebuild_sys_tab;
249 /* Active IOPs now in OPERATIONAL state */
250 PDEBUG("HBA's in OPERATIONAL state\n");
252 printk("dpti: If you have a lot of devices this could take a few minutes.\n");
253 for (pHba = hba_chain; pHba; pHba = next) {
255 printk(KERN_INFO"%s: Reading the hardware resource table.\n", pHba->name);
256 if (adpt_i2o_lct_get(pHba) < 0){
257 adpt_i2o_delete_hba(pHba);
261 if (adpt_i2o_parse_lct(pHba) < 0){
262 adpt_i2o_delete_hba(pHba);
268 adpt_sysfs_class = class_create(THIS_MODULE, "dpt_i2o");
269 if (IS_ERR(adpt_sysfs_class)) {
270 printk(KERN_WARNING"dpti: unable to create dpt_i2o class\n");
271 adpt_sysfs_class = NULL;
274 for (pHba = hba_chain; pHba; pHba = next) {
276 if (adpt_scsi_host_alloc(pHba, sht) < 0){
277 adpt_i2o_delete_hba(pHba);
280 pHba->initialized = TRUE;
281 pHba->state &= ~DPTI_STATE_RESET;
282 if (adpt_sysfs_class) {
283 struct device *dev = device_create(adpt_sysfs_class,
284 NULL, MKDEV(DPTI_I2O_MAJOR, pHba->unit), NULL,
285 "dpti%d", pHba->unit);
287 printk(KERN_WARNING"dpti%d: unable to "
288 "create device in dpt_i2o class\n",
294 // Register our control device node
295 // nodes will need to be created in /dev to access this
296 // the nodes can not be created from within the driver
297 if (hba_count && register_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER, &adpt_fops)) {
298 adpt_i2o_sys_shutdown();
306 * scsi_unregister will be called AFTER we return.
308 static int adpt_release(struct Scsi_Host *host)
310 adpt_hba* pHba = (adpt_hba*) host->hostdata[0];
311 // adpt_i2o_quiesce_hba(pHba);
312 adpt_i2o_delete_hba(pHba);
313 scsi_unregister(host);
318 static void adpt_inquiry(adpt_hba* pHba)
332 memset(msg, 0, sizeof(msg));
333 buf = dma_alloc_coherent(&pHba->pDev->dev, 80, &addr, GFP_KERNEL);
335 printk(KERN_ERR"%s: Could not allocate buffer\n",pHba->name);
338 memset((void*)buf, 0, 36);
341 direction = 0x00000000;
342 scsidir =0x40000000; // DATA IN (iop<--dev)
345 reqlen = 17; // SINGLE SGE, 64 bit
347 reqlen = 14; // SINGLE SGE, 32 bit
348 /* Stick the headers on */
349 msg[0] = reqlen<<16 | SGL_OFFSET_12;
350 msg[1] = (0xff<<24|HOST_TID<<12|ADAPTER_TID);
353 // Adaptec/DPT Private stuff
354 msg[4] = I2O_CMD_SCSI_EXEC|DPT_ORGANIZATION_ID<<16;
355 msg[5] = ADAPTER_TID | 1<<16 /* Interpret*/;
356 /* Direction, disconnect ok | sense data | simple queue , CDBLen */
357 // I2O_SCB_FLAG_ENABLE_DISCONNECT |
358 // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
359 // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
360 msg[6] = scsidir|0x20a00000| 6 /* cmd len*/;
364 memset(scb, 0, sizeof(scb));
365 // Write SCSI command into the message - always 16 byte block
372 // Don't care about the rest of scb
374 memcpy(mptr, scb, sizeof(scb));
376 lenptr=mptr++; /* Remember me - fill in when we know */
378 /* Now fill in the SGList and command */
380 if (dpt_dma64(pHba)) {
381 *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
382 *mptr++ = 1 << PAGE_SHIFT;
383 *mptr++ = 0xD0000000|direction|len;
384 *mptr++ = dma_low(addr);
385 *mptr++ = dma_high(addr);
387 *mptr++ = 0xD0000000|direction|len;
391 // Send it on it's way
392 rcode = adpt_i2o_post_wait(pHba, msg, reqlen<<2, 120);
394 sprintf(pHba->detail, "Adaptec I2O RAID");
395 printk(KERN_INFO "%s: Inquiry Error (%d)\n",pHba->name,rcode);
396 if (rcode != -ETIME && rcode != -EINTR)
397 dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
399 memset(pHba->detail, 0, sizeof(pHba->detail));
400 memcpy(&(pHba->detail), "Vendor: Adaptec ", 16);
401 memcpy(&(pHba->detail[16]), " Model: ", 8);
402 memcpy(&(pHba->detail[24]), (u8*) &buf[16], 16);
403 memcpy(&(pHba->detail[40]), " FW: ", 4);
404 memcpy(&(pHba->detail[44]), (u8*) &buf[32], 4);
405 pHba->detail[48] = '\0'; /* precautionary */
406 dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
408 adpt_i2o_status_get(pHba);
413 static int adpt_slave_configure(struct scsi_device * device)
415 struct Scsi_Host *host = device->host;
418 pHba = (adpt_hba *) host->hostdata[0];
420 if (host->can_queue && device->tagged_supported) {
421 scsi_change_queue_depth(device,
422 host->can_queue - 1);
427 static int adpt_queue_lck(struct scsi_cmnd * cmd, void (*done) (struct scsi_cmnd *))
429 adpt_hba* pHba = NULL;
430 struct adpt_device* pDev = NULL; /* dpt per device information */
432 cmd->scsi_done = done;
434 * SCSI REQUEST_SENSE commands will be executed automatically by the
435 * Host Adapter for any errors, so they should not be executed
436 * explicitly unless the Sense Data is zero indicating that no error
440 if ((cmd->cmnd[0] == REQUEST_SENSE) && (cmd->sense_buffer[0] != 0)) {
441 cmd->result = (DID_OK << 16);
446 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
452 if ((pHba->state) & DPTI_STATE_RESET)
453 return SCSI_MLQUEUE_HOST_BUSY;
455 // TODO if the cmd->device if offline then I may need to issue a bus rescan
456 // followed by a get_lct to see if the device is there anymore
457 if((pDev = (struct adpt_device*) (cmd->device->hostdata)) == NULL) {
459 * First command request for this device. Set up a pointer
460 * to the device structure. This should be a TEST_UNIT_READY
461 * command from scan_scsis_single.
463 if ((pDev = adpt_find_device(pHba, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun)) == NULL) {
464 // TODO: if any luns are at this bus, scsi id then fake a TEST_UNIT_READY and INQUIRY response
465 // with type 7F (for all luns less than the max for this bus,id) so the lun scan will continue.
466 cmd->result = (DID_NO_CONNECT << 16);
470 cmd->device->hostdata = pDev;
472 pDev->pScsi_dev = cmd->device;
475 * If we are being called from when the device is being reset,
476 * delay processing of the command until later.
478 if (pDev->state & DPTI_DEV_RESET ) {
481 return adpt_scsi_to_i2o(pHba, cmd, pDev);
484 static DEF_SCSI_QCMD(adpt_queue)
486 static int adpt_bios_param(struct scsi_device *sdev, struct block_device *dev,
487 sector_t capacity, int geom[])
493 // *** First lets set the default geometry ****
495 // If the capacity is less than ox2000
496 if (capacity < 0x2000 ) { // floppy
500 // else if between 0x2000 and 0x20000
501 else if (capacity < 0x20000) {
505 // else if between 0x20000 and 0x40000
506 else if (capacity < 0x40000) {
510 // else if between 0x4000 and 0x80000
511 else if (capacity < 0x80000) {
515 // else if greater than 0x80000
520 cylinders = sector_div(capacity, heads * sectors);
522 // Special case if CDROM
523 if(sdev->type == 5) { // CDROM
533 PDEBUG("adpt_bios_param: exit\n");
538 static const char *adpt_info(struct Scsi_Host *host)
542 pHba = (adpt_hba *) host->hostdata[0];
543 return (char *) (pHba->detail);
546 static int adpt_show_info(struct seq_file *m, struct Scsi_Host *host)
548 struct adpt_device* d;
554 // Find HBA (host bus adapter) we are looking for
555 mutex_lock(&adpt_configuration_lock);
556 for (pHba = hba_chain; pHba; pHba = pHba->next) {
557 if (pHba->host == host) {
558 break; /* found adapter */
561 mutex_unlock(&adpt_configuration_lock);
567 seq_printf(m, "Adaptec I2O RAID Driver Version: %s\n\n", DPT_I2O_VERSION);
568 seq_printf(m, "%s\n", pHba->detail);
569 seq_printf(m, "SCSI Host=scsi%d Control Node=/dev/%s irq=%d\n",
570 pHba->host->host_no, pHba->name, host->irq);
571 seq_printf(m, "\tpost fifo size = %d\n\treply fifo size = %d\n\tsg table size = %d\n\n",
572 host->can_queue, (int) pHba->reply_fifo_size , host->sg_tablesize);
574 seq_puts(m, "Devices:\n");
575 for(chan = 0; chan < MAX_CHANNEL; chan++) {
576 for(id = 0; id < MAX_ID; id++) {
577 d = pHba->channel[chan].device[id];
579 seq_printf(m,"\t%-24.24s", d->pScsi_dev->vendor);
580 seq_printf(m," Rev: %-8.8s\n", d->pScsi_dev->rev);
582 unit = d->pI2o_dev->lct_data.tid;
583 seq_printf(m, "\tTID=%d, (Channel=%d, Target=%d, Lun=%llu) (%s)\n\n",
584 unit, (int)d->scsi_channel, (int)d->scsi_id, d->scsi_lun,
585 scsi_device_online(d->pScsi_dev)? "online":"offline");
594 * Turn a struct scsi_cmnd * into a unique 32 bit 'context'.
596 static u32 adpt_cmd_to_context(struct scsi_cmnd *cmd)
598 return (u32)cmd->serial_number;
602 * Go from a u32 'context' to a struct scsi_cmnd * .
603 * This could probably be made more efficient.
605 static struct scsi_cmnd *
606 adpt_cmd_from_context(adpt_hba * pHba, u32 context)
608 struct scsi_cmnd * cmd;
609 struct scsi_device * d;
614 spin_unlock(pHba->host->host_lock);
615 shost_for_each_device(d, pHba->host) {
617 spin_lock_irqsave(&d->list_lock, flags);
618 list_for_each_entry(cmd, &d->cmd_list, list) {
619 if (((u32)cmd->serial_number == context)) {
620 spin_unlock_irqrestore(&d->list_lock, flags);
622 spin_lock(pHba->host->host_lock);
626 spin_unlock_irqrestore(&d->list_lock, flags);
628 spin_lock(pHba->host->host_lock);
634 * Turn a pointer to ioctl reply data into an u32 'context'
636 static u32 adpt_ioctl_to_context(adpt_hba * pHba, void *reply)
638 #if BITS_PER_LONG == 32
639 return (u32)(unsigned long)reply;
644 spin_lock_irqsave(pHba->host->host_lock, flags);
645 nr = ARRAY_SIZE(pHba->ioctl_reply_context);
646 for (i = 0; i < nr; i++) {
647 if (pHba->ioctl_reply_context[i] == NULL) {
648 pHba->ioctl_reply_context[i] = reply;
652 spin_unlock_irqrestore(pHba->host->host_lock, flags);
655 printk(KERN_WARNING"%s: Too many outstanding "
656 "ioctl commands\n", pHba->name);
665 * Go from an u32 'context' to a pointer to ioctl reply data.
667 static void *adpt_ioctl_from_context(adpt_hba *pHba, u32 context)
669 #if BITS_PER_LONG == 32
670 return (void *)(unsigned long)context;
672 void *p = pHba->ioctl_reply_context[context];
673 pHba->ioctl_reply_context[context] = NULL;
679 /*===========================================================================
680 * Error Handling routines
681 *===========================================================================
684 static int adpt_abort(struct scsi_cmnd * cmd)
686 adpt_hba* pHba = NULL; /* host bus adapter structure */
687 struct adpt_device* dptdevice; /* dpt per device information */
691 if(cmd->serial_number == 0){
694 pHba = (adpt_hba*) cmd->device->host->hostdata[0];
695 printk(KERN_INFO"%s: Trying to Abort\n",pHba->name);
696 if ((dptdevice = (void*) (cmd->device->hostdata)) == NULL) {
697 printk(KERN_ERR "%s: Unable to abort: No device in cmnd\n",pHba->name);
701 memset(msg, 0, sizeof(msg));
702 msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
703 msg[1] = I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|dptdevice->tid;
706 msg[4] = adpt_cmd_to_context(cmd);
708 spin_lock_irq(pHba->host->host_lock);
709 rcode = adpt_i2o_post_wait(pHba, msg, sizeof(msg), FOREVER);
711 spin_unlock_irq(pHba->host->host_lock);
713 if(rcode == -EOPNOTSUPP ){
714 printk(KERN_INFO"%s: Abort cmd not supported\n",pHba->name);
717 printk(KERN_INFO"%s: Abort failed.\n",pHba->name);
720 printk(KERN_INFO"%s: Abort complete.\n",pHba->name);
725 #define I2O_DEVICE_RESET 0x27
726 // This is the same for BLK and SCSI devices
727 // NOTE this is wrong in the i2o.h definitions
728 // This is not currently supported by our adapter but we issue it anyway
729 static int adpt_device_reset(struct scsi_cmnd* cmd)
735 struct adpt_device* d = cmd->device->hostdata;
737 pHba = (void*) cmd->device->host->hostdata[0];
738 printk(KERN_INFO"%s: Trying to reset device\n",pHba->name);
740 printk(KERN_INFO"%s: Reset Device: Device Not found\n",pHba->name);
743 memset(msg, 0, sizeof(msg));
744 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
745 msg[1] = (I2O_DEVICE_RESET<<24|HOST_TID<<12|d->tid);
750 spin_lock_irq(pHba->host->host_lock);
751 old_state = d->state;
752 d->state |= DPTI_DEV_RESET;
753 rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
754 d->state = old_state;
756 spin_unlock_irq(pHba->host->host_lock);
758 if(rcode == -EOPNOTSUPP ){
759 printk(KERN_INFO"%s: Device reset not supported\n",pHba->name);
762 printk(KERN_INFO"%s: Device reset failed\n",pHba->name);
765 printk(KERN_INFO"%s: Device reset successful\n",pHba->name);
771 #define I2O_HBA_BUS_RESET 0x87
772 // This version of bus reset is called by the eh_error handler
773 static int adpt_bus_reset(struct scsi_cmnd* cmd)
779 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
780 memset(msg, 0, sizeof(msg));
781 printk(KERN_WARNING"%s: Bus reset: SCSI Bus %d: tid: %d\n",pHba->name, cmd->device->channel,pHba->channel[cmd->device->channel].tid );
782 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
783 msg[1] = (I2O_HBA_BUS_RESET<<24|HOST_TID<<12|pHba->channel[cmd->device->channel].tid);
787 spin_lock_irq(pHba->host->host_lock);
788 rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
790 spin_unlock_irq(pHba->host->host_lock);
792 printk(KERN_WARNING"%s: Bus reset failed.\n",pHba->name);
795 printk(KERN_WARNING"%s: Bus reset success.\n",pHba->name);
800 // This version of reset is called by the eh_error_handler
801 static int __adpt_reset(struct scsi_cmnd* cmd)
805 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
806 printk(KERN_WARNING"%s: Hba Reset: scsi id %d: tid: %d\n",pHba->name,cmd->device->channel,pHba->channel[cmd->device->channel].tid );
807 rcode = adpt_hba_reset(pHba);
809 printk(KERN_WARNING"%s: HBA reset complete\n",pHba->name);
812 printk(KERN_WARNING"%s: HBA reset failed (%x)\n",pHba->name, rcode);
817 static int adpt_reset(struct scsi_cmnd* cmd)
821 spin_lock_irq(cmd->device->host->host_lock);
822 rc = __adpt_reset(cmd);
823 spin_unlock_irq(cmd->device->host->host_lock);
828 // This version of reset is called by the ioctls and indirectly from eh_error_handler via adpt_reset
829 static int adpt_hba_reset(adpt_hba* pHba)
833 pHba->state |= DPTI_STATE_RESET;
835 // Activate does get status , init outbound, and get hrt
836 if ((rcode=adpt_i2o_activate_hba(pHba)) < 0) {
837 printk(KERN_ERR "%s: Could not activate\n", pHba->name);
838 adpt_i2o_delete_hba(pHba);
842 if ((rcode=adpt_i2o_build_sys_table()) < 0) {
843 adpt_i2o_delete_hba(pHba);
846 PDEBUG("%s: in HOLD state\n",pHba->name);
848 if ((rcode=adpt_i2o_online_hba(pHba)) < 0) {
849 adpt_i2o_delete_hba(pHba);
852 PDEBUG("%s: in OPERATIONAL state\n",pHba->name);
854 if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
855 adpt_i2o_delete_hba(pHba);
859 if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
860 adpt_i2o_delete_hba(pHba);
863 pHba->state &= ~DPTI_STATE_RESET;
865 adpt_fail_posted_scbs(pHba);
866 return 0; /* return success */
869 /*===========================================================================
871 *===========================================================================
875 static void adpt_i2o_sys_shutdown(void)
877 adpt_hba *pHba, *pNext;
878 struct adpt_i2o_post_wait_data *p1, *old;
880 printk(KERN_INFO"Shutting down Adaptec I2O controllers.\n");
881 printk(KERN_INFO" This could take a few minutes if there are many devices attached\n");
882 /* Delete all IOPs from the controller chain */
883 /* They should have already been released by the
886 for (pHba = hba_chain; pHba; pHba = pNext) {
888 adpt_i2o_delete_hba(pHba);
891 /* Remove any timedout entries from the wait queue. */
892 // spin_lock_irqsave(&adpt_post_wait_lock, flags);
893 /* Nothing should be outstanding at this point so just
896 for(p1 = adpt_post_wait_queue; p1;) {
901 // spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
902 adpt_post_wait_queue = NULL;
904 printk(KERN_INFO "Adaptec I2O controllers down.\n");
907 static int adpt_install_hba(struct scsi_host_template* sht, struct pci_dev* pDev)
910 adpt_hba* pHba = NULL;
912 ulong base_addr0_phys = 0;
913 ulong base_addr1_phys = 0;
914 u32 hba_map0_area_size = 0;
915 u32 hba_map1_area_size = 0;
916 void __iomem *base_addr_virt = NULL;
917 void __iomem *msg_addr_virt = NULL;
920 int raptorFlag = FALSE;
922 if(pci_enable_device(pDev)) {
926 if (pci_request_regions(pDev, "dpt_i2o")) {
927 PERROR("dpti: adpt_config_hba: pci request region failed\n");
931 pci_set_master(pDev);
934 * See if we should enable dma64 mode.
936 if (sizeof(dma_addr_t) > 4 &&
937 pci_set_dma_mask(pDev, DMA_BIT_MASK(64)) == 0) {
938 if (dma_get_required_mask(&pDev->dev) > DMA_BIT_MASK(32))
941 if (!dma64 && pci_set_dma_mask(pDev, DMA_BIT_MASK(32)) != 0)
944 /* adapter only supports message blocks below 4GB */
945 pci_set_consistent_dma_mask(pDev, DMA_BIT_MASK(32));
947 base_addr0_phys = pci_resource_start(pDev,0);
948 hba_map0_area_size = pci_resource_len(pDev,0);
950 // Check if standard PCI card or single BAR Raptor
951 if(pDev->device == PCI_DPT_DEVICE_ID){
952 if(pDev->subsystem_device >=0xc032 && pDev->subsystem_device <= 0xc03b){
953 // Raptor card with this device id needs 4M
954 hba_map0_area_size = 0x400000;
955 } else { // Not Raptor - it is a PCI card
956 if(hba_map0_area_size > 0x100000 ){
957 hba_map0_area_size = 0x100000;
960 } else {// Raptor split BAR config
961 // Use BAR1 in this configuration
962 base_addr1_phys = pci_resource_start(pDev,1);
963 hba_map1_area_size = pci_resource_len(pDev,1);
967 #if BITS_PER_LONG == 64
969 * The original Adaptec 64 bit driver has this comment here:
970 * "x86_64 machines need more optimal mappings"
972 * I assume some HBAs report ridiculously large mappings
973 * and we need to limit them on platforms with IOMMUs.
975 if (raptorFlag == TRUE) {
976 if (hba_map0_area_size > 128)
977 hba_map0_area_size = 128;
978 if (hba_map1_area_size > 524288)
979 hba_map1_area_size = 524288;
981 if (hba_map0_area_size > 524288)
982 hba_map0_area_size = 524288;
986 base_addr_virt = ioremap(base_addr0_phys,hba_map0_area_size);
987 if (!base_addr_virt) {
988 pci_release_regions(pDev);
989 PERROR("dpti: adpt_config_hba: io remap failed\n");
993 if(raptorFlag == TRUE) {
994 msg_addr_virt = ioremap(base_addr1_phys, hba_map1_area_size );
995 if (!msg_addr_virt) {
996 PERROR("dpti: adpt_config_hba: io remap failed on BAR1\n");
997 iounmap(base_addr_virt);
998 pci_release_regions(pDev);
1002 msg_addr_virt = base_addr_virt;
1005 // Allocate and zero the data structure
1006 pHba = kzalloc(sizeof(adpt_hba), GFP_KERNEL);
1008 if (msg_addr_virt != base_addr_virt)
1009 iounmap(msg_addr_virt);
1010 iounmap(base_addr_virt);
1011 pci_release_regions(pDev);
1015 mutex_lock(&adpt_configuration_lock);
1017 if(hba_chain != NULL){
1018 for(p = hba_chain; p->next; p = p->next);
1024 pHba->unit = hba_count;
1025 sprintf(pHba->name, "dpti%d", hba_count);
1028 mutex_unlock(&adpt_configuration_lock);
1031 pHba->base_addr_phys = base_addr0_phys;
1033 // Set up the Virtual Base Address of the I2O Device
1034 pHba->base_addr_virt = base_addr_virt;
1035 pHba->msg_addr_virt = msg_addr_virt;
1036 pHba->irq_mask = base_addr_virt+0x30;
1037 pHba->post_port = base_addr_virt+0x40;
1038 pHba->reply_port = base_addr_virt+0x44;
1043 pHba->status_block = NULL;
1044 pHba->post_count = 0;
1045 pHba->state = DPTI_STATE_RESET;
1047 pHba->devices = NULL;
1048 pHba->dma64 = dma64;
1050 // Initializing the spinlocks
1051 spin_lock_init(&pHba->state_lock);
1052 spin_lock_init(&adpt_post_wait_lock);
1054 if(raptorFlag == 0){
1055 printk(KERN_INFO "Adaptec I2O RAID controller"
1056 " %d at %p size=%x irq=%d%s\n",
1057 hba_count-1, base_addr_virt,
1058 hba_map0_area_size, pDev->irq,
1059 dma64 ? " (64-bit DMA)" : "");
1061 printk(KERN_INFO"Adaptec I2O RAID controller %d irq=%d%s\n",
1062 hba_count-1, pDev->irq,
1063 dma64 ? " (64-bit DMA)" : "");
1064 printk(KERN_INFO" BAR0 %p - size= %x\n",base_addr_virt,hba_map0_area_size);
1065 printk(KERN_INFO" BAR1 %p - size= %x\n",msg_addr_virt,hba_map1_area_size);
1068 if (request_irq (pDev->irq, adpt_isr, IRQF_SHARED, pHba->name, pHba)) {
1069 printk(KERN_ERR"%s: Couldn't register IRQ %d\n", pHba->name, pDev->irq);
1070 adpt_i2o_delete_hba(pHba);
1078 static void adpt_i2o_delete_hba(adpt_hba* pHba)
1082 struct i2o_device* d;
1083 struct i2o_device* next;
1086 struct adpt_device* pDev;
1087 struct adpt_device* pNext;
1090 mutex_lock(&adpt_configuration_lock);
1091 // scsi_unregister calls our adpt_release which
1094 free_irq(pHba->host->irq, pHba);
1097 for( p1 = hba_chain; p1; p2 = p1,p1=p1->next){
1100 p2->next = p1->next;
1102 hba_chain = p1->next;
1109 mutex_unlock(&adpt_configuration_lock);
1111 iounmap(pHba->base_addr_virt);
1112 pci_release_regions(pHba->pDev);
1113 if(pHba->msg_addr_virt != pHba->base_addr_virt){
1114 iounmap(pHba->msg_addr_virt);
1116 if(pHba->FwDebugBuffer_P)
1117 iounmap(pHba->FwDebugBuffer_P);
1119 dma_free_coherent(&pHba->pDev->dev,
1120 pHba->hrt->num_entries * pHba->hrt->entry_len << 2,
1121 pHba->hrt, pHba->hrt_pa);
1124 dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
1125 pHba->lct, pHba->lct_pa);
1127 if(pHba->status_block) {
1128 dma_free_coherent(&pHba->pDev->dev, sizeof(i2o_status_block),
1129 pHba->status_block, pHba->status_block_pa);
1131 if(pHba->reply_pool) {
1132 dma_free_coherent(&pHba->pDev->dev,
1133 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
1134 pHba->reply_pool, pHba->reply_pool_pa);
1137 for(d = pHba->devices; d ; d = next){
1141 for(i = 0 ; i < pHba->top_scsi_channel ; i++){
1142 for(j = 0; j < MAX_ID; j++){
1143 if(pHba->channel[i].device[j] != NULL){
1144 for(pDev = pHba->channel[i].device[j]; pDev; pDev = pNext){
1145 pNext = pDev->next_lun;
1151 pci_dev_put(pHba->pDev);
1152 if (adpt_sysfs_class)
1153 device_destroy(adpt_sysfs_class,
1154 MKDEV(DPTI_I2O_MAJOR, pHba->unit));
1158 unregister_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER);
1159 if (adpt_sysfs_class) {
1160 class_destroy(adpt_sysfs_class);
1161 adpt_sysfs_class = NULL;
1166 static struct adpt_device* adpt_find_device(adpt_hba* pHba, u32 chan, u32 id, u64 lun)
1168 struct adpt_device* d;
1170 if(chan < 0 || chan >= MAX_CHANNEL)
1173 if( pHba->channel[chan].device == NULL){
1174 printk(KERN_DEBUG"Adaptec I2O RAID: Trying to find device before they are allocated\n");
1178 d = pHba->channel[chan].device[id];
1179 if(!d || d->tid == 0) {
1183 /* If it is the only lun at that address then this should match*/
1184 if(d->scsi_lun == lun){
1188 /* else we need to look through all the luns */
1189 for(d=d->next_lun ; d ; d = d->next_lun){
1190 if(d->scsi_lun == lun){
1198 static int adpt_i2o_post_wait(adpt_hba* pHba, u32* msg, int len, int timeout)
1200 // I used my own version of the WAIT_QUEUE_HEAD
1201 // to handle some version differences
1202 // When embedded in the kernel this could go back to the vanilla one
1203 ADPT_DECLARE_WAIT_QUEUE_HEAD(adpt_wq_i2o_post);
1206 struct adpt_i2o_post_wait_data *p1, *p2;
1207 struct adpt_i2o_post_wait_data *wait_data =
1208 kmalloc(sizeof(struct adpt_i2o_post_wait_data), GFP_ATOMIC);
1209 DECLARE_WAITQUEUE(wait, current);
1215 * The spin locking is needed to keep anyone from playing
1216 * with the queue pointers and id while we do the same
1218 spin_lock_irqsave(&adpt_post_wait_lock, flags);
1219 // TODO we need a MORE unique way of getting ids
1220 // to support async LCT get
1221 wait_data->next = adpt_post_wait_queue;
1222 adpt_post_wait_queue = wait_data;
1223 adpt_post_wait_id++;
1224 adpt_post_wait_id &= 0x7fff;
1225 wait_data->id = adpt_post_wait_id;
1226 spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1228 wait_data->wq = &adpt_wq_i2o_post;
1229 wait_data->status = -ETIMEDOUT;
1231 add_wait_queue(&adpt_wq_i2o_post, &wait);
1233 msg[2] |= 0x80000000 | ((u32)wait_data->id);
1235 if((status = adpt_i2o_post_this(pHba, msg, len)) == 0){
1236 set_current_state(TASK_INTERRUPTIBLE);
1238 spin_unlock_irq(pHba->host->host_lock);
1242 timeout = schedule_timeout(timeout);
1244 // I/O issued, but cannot get result in
1245 // specified time. Freeing resorces is
1251 spin_lock_irq(pHba->host->host_lock);
1253 remove_wait_queue(&adpt_wq_i2o_post, &wait);
1255 if(status == -ETIMEDOUT){
1256 printk(KERN_INFO"dpti%d: POST WAIT TIMEOUT\n",pHba->unit);
1257 // We will have to free the wait_data memory during shutdown
1261 /* Remove the entry from the queue. */
1263 spin_lock_irqsave(&adpt_post_wait_lock, flags);
1264 for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p1->next) {
1265 if(p1 == wait_data) {
1266 if(p1->status == I2O_DETAIL_STATUS_UNSUPPORTED_FUNCTION ) {
1267 status = -EOPNOTSUPP;
1270 p2->next = p1->next;
1272 adpt_post_wait_queue = p1->next;
1277 spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1285 static s32 adpt_i2o_post_this(adpt_hba* pHba, u32* data, int len)
1288 u32 m = EMPTY_QUEUE;
1290 ulong timeout = jiffies + 30*HZ;
1293 m = readl(pHba->post_port);
1294 if (m != EMPTY_QUEUE) {
1297 if(time_after(jiffies,timeout)){
1298 printk(KERN_WARNING"dpti%d: Timeout waiting for message frame!\n", pHba->unit);
1301 schedule_timeout_uninterruptible(1);
1302 } while(m == EMPTY_QUEUE);
1304 msg = pHba->msg_addr_virt + m;
1305 memcpy_toio(msg, data, len);
1309 writel(m, pHba->post_port);
1316 static void adpt_i2o_post_wait_complete(u32 context, int status)
1318 struct adpt_i2o_post_wait_data *p1 = NULL;
1320 * We need to search through the adpt_post_wait
1321 * queue to see if the given message is still
1322 * outstanding. If not, it means that the IOP
1323 * took longer to respond to the message than we
1324 * had allowed and timer has already expired.
1325 * Not much we can do about that except log
1326 * it for debug purposes, increase timeout, and recompile
1328 * Lock needed to keep anyone from moving queue pointers
1329 * around while we're looking through them.
1334 spin_lock(&adpt_post_wait_lock);
1335 for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1336 if(p1->id == context) {
1337 p1->status = status;
1338 spin_unlock(&adpt_post_wait_lock);
1339 wake_up_interruptible(p1->wq);
1343 spin_unlock(&adpt_post_wait_lock);
1344 // If this happens we lose commands that probably really completed
1345 printk(KERN_DEBUG"dpti: Could Not find task %d in wait queue\n",context);
1346 printk(KERN_DEBUG" Tasks in wait queue:\n");
1347 for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1348 printk(KERN_DEBUG" %d\n",p1->id);
1353 static s32 adpt_i2o_reset_hba(adpt_hba* pHba)
1358 u32 m = EMPTY_QUEUE ;
1359 ulong timeout = jiffies + (TMOUT_IOPRESET*HZ);
1361 if(pHba->initialized == FALSE) { // First time reset should be quick
1362 timeout = jiffies + (25*HZ);
1364 adpt_i2o_quiesce_hba(pHba);
1369 m = readl(pHba->post_port);
1370 if (m != EMPTY_QUEUE) {
1373 if(time_after(jiffies,timeout)){
1374 printk(KERN_WARNING"Timeout waiting for message!\n");
1377 schedule_timeout_uninterruptible(1);
1378 } while (m == EMPTY_QUEUE);
1380 status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
1381 if(status == NULL) {
1382 adpt_send_nop(pHba, m);
1383 printk(KERN_ERR"IOP reset failed - no free memory.\n");
1388 msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
1389 msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
1394 msg[6]=dma_low(addr);
1395 msg[7]=dma_high(addr);
1397 memcpy_toio(pHba->msg_addr_virt+m, msg, sizeof(msg));
1399 writel(m, pHba->post_port);
1402 while(*status == 0){
1403 if(time_after(jiffies,timeout)){
1404 printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
1405 /* We lose 4 bytes of "status" here, but we cannot
1406 free these because controller may awake and corrupt
1407 those bytes at any time */
1408 /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
1412 schedule_timeout_uninterruptible(1);
1415 if(*status == 0x01 /*I2O_EXEC_IOP_RESET_IN_PROGRESS*/) {
1416 PDEBUG("%s: Reset in progress...\n", pHba->name);
1417 // Here we wait for message frame to become available
1418 // indicated that reset has finished
1421 m = readl(pHba->post_port);
1422 if (m != EMPTY_QUEUE) {
1425 if(time_after(jiffies,timeout)){
1426 printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
1427 /* We lose 4 bytes of "status" here, but we
1428 cannot free these because controller may
1429 awake and corrupt those bytes at any time */
1430 /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
1433 schedule_timeout_uninterruptible(1);
1434 } while (m == EMPTY_QUEUE);
1436 adpt_send_nop(pHba, m);
1438 adpt_i2o_status_get(pHba);
1439 if(*status == 0x02 ||
1440 pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
1441 printk(KERN_WARNING"%s: Reset reject, trying to clear\n",
1444 PDEBUG("%s: Reset completed.\n", pHba->name);
1447 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
1449 // This delay is to allow someone attached to the card through the debug UART to
1450 // set up the dump levels that they want before the rest of the initialization sequence
1457 static int adpt_i2o_parse_lct(adpt_hba* pHba)
1462 struct i2o_device *d;
1463 i2o_lct *lct = pHba->lct;
1467 u32 buf[10]; // larger than 7, or 8 ...
1468 struct adpt_device* pDev;
1471 printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
1475 max = lct->table_size;
1479 for(i=0;i<max;i++) {
1480 if( lct->lct_entry[i].user_tid != 0xfff){
1482 * If we have hidden devices, we need to inform the upper layers about
1483 * the possible maximum id reference to handle device access when
1484 * an array is disassembled. This code has no other purpose but to
1485 * allow us future access to devices that are currently hidden
1486 * behind arrays, hotspares or have not been configured (JBOD mode).
1488 if( lct->lct_entry[i].class_id != I2O_CLASS_RANDOM_BLOCK_STORAGE &&
1489 lct->lct_entry[i].class_id != I2O_CLASS_SCSI_PERIPHERAL &&
1490 lct->lct_entry[i].class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1493 tid = lct->lct_entry[i].tid;
1494 // I2O_DPT_DEVICE_INFO_GROUP_NO;
1495 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
1498 bus_no = buf[0]>>16;
1500 scsi_lun = scsilun_to_int((struct scsi_lun *)&buf[2]);
1501 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1502 printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
1505 if (scsi_id >= MAX_ID){
1506 printk(KERN_WARNING"%s: SCSI ID %d out of range \n", pHba->name, bus_no);
1509 if(bus_no > pHba->top_scsi_channel){
1510 pHba->top_scsi_channel = bus_no;
1512 if(scsi_id > pHba->top_scsi_id){
1513 pHba->top_scsi_id = scsi_id;
1515 if(scsi_lun > pHba->top_scsi_lun){
1516 pHba->top_scsi_lun = scsi_lun;
1520 d = kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
1523 printk(KERN_CRIT"%s: Out of memory for I2O device data.\n",pHba->name);
1527 d->controller = pHba;
1530 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
1533 tid = d->lct_data.tid;
1534 adpt_i2o_report_hba_unit(pHba, d);
1535 adpt_i2o_install_device(pHba, d);
1538 for(d = pHba->devices; d ; d = d->next) {
1539 if(d->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT ||
1540 d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PORT){
1541 tid = d->lct_data.tid;
1542 // TODO get the bus_no from hrt-but for now they are in order
1544 if(bus_no > pHba->top_scsi_channel){
1545 pHba->top_scsi_channel = bus_no;
1547 pHba->channel[bus_no].type = d->lct_data.class_id;
1548 pHba->channel[bus_no].tid = tid;
1549 if(adpt_i2o_query_scalar(pHba, tid, 0x0200, -1, buf, 28)>=0)
1551 pHba->channel[bus_no].scsi_id = buf[1];
1552 PDEBUG("Bus %d - SCSI ID %d.\n", bus_no, buf[1]);
1554 // TODO remove - this is just until we get from hrt
1556 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1557 printk(KERN_WARNING"%s: Channel number %d out of range - LCT\n", pHba->name, bus_no);
1563 // Setup adpt_device table
1564 for(d = pHba->devices; d ; d = d->next) {
1565 if(d->lct_data.class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
1566 d->lct_data.class_id == I2O_CLASS_SCSI_PERIPHERAL ||
1567 d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1569 tid = d->lct_data.tid;
1571 // I2O_DPT_DEVICE_INFO_GROUP_NO;
1572 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)>=0) {
1573 bus_no = buf[0]>>16;
1575 scsi_lun = scsilun_to_int((struct scsi_lun *)&buf[2]);
1576 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1579 if (scsi_id >= MAX_ID) {
1582 if( pHba->channel[bus_no].device[scsi_id] == NULL){
1583 pDev = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
1587 pHba->channel[bus_no].device[scsi_id] = pDev;
1589 for( pDev = pHba->channel[bus_no].device[scsi_id];
1590 pDev->next_lun; pDev = pDev->next_lun){
1592 pDev->next_lun = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
1593 if(pDev->next_lun == NULL) {
1596 pDev = pDev->next_lun;
1599 pDev->scsi_channel = bus_no;
1600 pDev->scsi_id = scsi_id;
1601 pDev->scsi_lun = scsi_lun;
1604 pDev->type = (buf[0])&0xff;
1605 pDev->flags = (buf[0]>>8)&0xff;
1606 if(scsi_id > pHba->top_scsi_id){
1607 pHba->top_scsi_id = scsi_id;
1609 if(scsi_lun > pHba->top_scsi_lun){
1610 pHba->top_scsi_lun = scsi_lun;
1614 printk(KERN_WARNING"Could not find SCSI ID for %s\n",
1615 d->lct_data.identity_tag);
1624 * Each I2O controller has a chain of devices on it - these match
1625 * the useful parts of the LCT of the board.
1628 static int adpt_i2o_install_device(adpt_hba* pHba, struct i2o_device *d)
1630 mutex_lock(&adpt_configuration_lock);
1633 d->next=pHba->devices;
1635 if (pHba->devices != NULL){
1636 pHba->devices->prev=d;
1641 mutex_unlock(&adpt_configuration_lock);
1645 static int adpt_open(struct inode *inode, struct file *file)
1650 mutex_lock(&adpt_mutex);
1651 //TODO check for root access
1653 minor = iminor(inode);
1654 if (minor >= hba_count) {
1655 mutex_unlock(&adpt_mutex);
1658 mutex_lock(&adpt_configuration_lock);
1659 for (pHba = hba_chain; pHba; pHba = pHba->next) {
1660 if (pHba->unit == minor) {
1661 break; /* found adapter */
1665 mutex_unlock(&adpt_configuration_lock);
1666 mutex_unlock(&adpt_mutex);
1670 // if(pHba->in_use){
1671 // mutex_unlock(&adpt_configuration_lock);
1676 mutex_unlock(&adpt_configuration_lock);
1677 mutex_unlock(&adpt_mutex);
1682 static int adpt_close(struct inode *inode, struct file *file)
1687 minor = iminor(inode);
1688 if (minor >= hba_count) {
1691 mutex_lock(&adpt_configuration_lock);
1692 for (pHba = hba_chain; pHba; pHba = pHba->next) {
1693 if (pHba->unit == minor) {
1694 break; /* found adapter */
1697 mutex_unlock(&adpt_configuration_lock);
1708 static int adpt_i2o_passthru(adpt_hba* pHba, u32 __user *arg)
1710 u32 msg[MAX_MESSAGE_SIZE];
1714 u32 __user *user_msg = arg;
1715 u32 __user * user_reply = NULL;
1716 void *sg_list[pHba->sg_tablesize];
1726 memset(&msg, 0, MAX_MESSAGE_SIZE*4);
1727 // get user msg size in u32s
1728 if(get_user(size, &user_msg[0])){
1733 user_reply = &user_msg[size];
1734 if(size > MAX_MESSAGE_SIZE){
1737 size *= 4; // Convert to bytes
1739 /* Copy in the user's I2O command */
1740 if(copy_from_user(msg, user_msg, size)) {
1743 get_user(reply_size, &user_reply[0]);
1744 reply_size = reply_size>>16;
1745 if(reply_size > REPLY_FRAME_SIZE){
1746 reply_size = REPLY_FRAME_SIZE;
1749 reply = kzalloc(REPLY_FRAME_SIZE*4, GFP_KERNEL);
1751 printk(KERN_WARNING"%s: Could not allocate reply buffer\n",pHba->name);
1754 sg_offset = (msg[0]>>4)&0xf;
1755 msg[2] = 0x40000000; // IOCTL context
1756 msg[3] = adpt_ioctl_to_context(pHba, reply);
1757 if (msg[3] == (u32)-1)
1760 memset(sg_list,0, sizeof(sg_list[0])*pHba->sg_tablesize);
1762 // TODO add 64 bit API
1763 struct sg_simple_element *sg = (struct sg_simple_element*) (msg+sg_offset);
1764 sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
1765 if (sg_count > pHba->sg_tablesize){
1766 printk(KERN_DEBUG"%s:IOCTL SG List too large (%u)\n", pHba->name,sg_count);
1771 for(i = 0; i < sg_count; i++) {
1774 if (!(sg[i].flag_count & 0x10000000 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT*/)) {
1775 printk(KERN_DEBUG"%s:Bad SG element %d - not simple (%x)\n",pHba->name,i, sg[i].flag_count);
1779 sg_size = sg[i].flag_count & 0xffffff;
1780 /* Allocate memory for the transfer */
1781 p = dma_alloc_coherent(&pHba->pDev->dev, sg_size, &addr, GFP_KERNEL);
1783 printk(KERN_DEBUG"%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
1784 pHba->name,sg_size,i,sg_count);
1788 sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
1789 /* Copy in the user's SG buffer if necessary */
1790 if(sg[i].flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR*/) {
1791 // sg_simple_element API is 32 bit
1792 if (copy_from_user(p,(void __user *)(ulong)sg[i].addr_bus, sg_size)) {
1793 printk(KERN_DEBUG"%s: Could not copy SG buf %d FROM user\n",pHba->name,i);
1798 /* sg_simple_element API is 32 bit, but addr < 4GB */
1799 sg[i].addr_bus = addr;
1805 * Stop any new commands from enterring the
1806 * controller while processing the ioctl
1809 scsi_block_requests(pHba->host);
1810 spin_lock_irqsave(pHba->host->host_lock, flags);
1812 rcode = adpt_i2o_post_wait(pHba, msg, size, FOREVER);
1814 printk("adpt_i2o_passthru: post wait failed %d %p\n",
1817 spin_unlock_irqrestore(pHba->host->host_lock, flags);
1818 scsi_unblock_requests(pHba->host);
1820 } while (rcode == -ETIMEDOUT);
1827 /* Copy back the Scatter Gather buffers back to user space */
1829 // TODO add 64 bit API
1830 struct sg_simple_element* sg;
1833 // re-acquire the original message to handle correctly the sg copy operation
1834 memset(&msg, 0, MAX_MESSAGE_SIZE*4);
1835 // get user msg size in u32s
1836 if(get_user(size, &user_msg[0])){
1842 if (size > MAX_MESSAGE_SIZE) {
1846 /* Copy in the user's I2O command */
1847 if (copy_from_user (msg, user_msg, size)) {
1851 sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
1853 // TODO add 64 bit API
1854 sg = (struct sg_simple_element*)(msg + sg_offset);
1855 for (j = 0; j < sg_count; j++) {
1856 /* Copy out the SG list to user's buffer if necessary */
1857 if(! (sg[j].flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR*/)) {
1858 sg_size = sg[j].flag_count & 0xffffff;
1859 // sg_simple_element API is 32 bit
1860 if (copy_to_user((void __user *)(ulong)sg[j].addr_bus,sg_list[j], sg_size)) {
1861 printk(KERN_WARNING"%s: Could not copy %p TO user %x\n",pHba->name, sg_list[j], sg[j].addr_bus);
1869 /* Copy back the reply to user space */
1871 // we wrote our own values for context - now restore the user supplied ones
1872 if(copy_from_user(reply+2, user_msg+2, sizeof(u32)*2)) {
1873 printk(KERN_WARNING"%s: Could not copy message context FROM user\n",pHba->name);
1876 if(copy_to_user(user_reply, reply, reply_size)) {
1877 printk(KERN_WARNING"%s: Could not copy reply TO user\n",pHba->name);
1884 if (rcode != -ETIME && rcode != -EINTR) {
1885 struct sg_simple_element *sg =
1886 (struct sg_simple_element*) (msg +sg_offset);
1889 if(sg_list[--sg_index]) {
1890 dma_free_coherent(&pHba->pDev->dev,
1891 sg[sg_index].flag_count & 0xffffff,
1893 sg[sg_index].addr_bus);
1900 #if defined __ia64__
1901 static void adpt_ia64_info(sysInfo_S* si)
1903 // This is all the info we need for now
1904 // We will add more info as our new
1905 // managmenent utility requires it
1906 si->processorType = PROC_IA64;
1910 #if defined __sparc__
1911 static void adpt_sparc_info(sysInfo_S* si)
1913 // This is all the info we need for now
1914 // We will add more info as our new
1915 // managmenent utility requires it
1916 si->processorType = PROC_ULTRASPARC;
1919 #if defined __alpha__
1920 static void adpt_alpha_info(sysInfo_S* si)
1922 // This is all the info we need for now
1923 // We will add more info as our new
1924 // managmenent utility requires it
1925 si->processorType = PROC_ALPHA;
1929 #if defined __i386__
1931 #include <uapi/asm/vm86.h>
1933 static void adpt_i386_info(sysInfo_S* si)
1935 // This is all the info we need for now
1936 // We will add more info as our new
1937 // managmenent utility requires it
1938 switch (boot_cpu_data.x86) {
1940 si->processorType = PROC_386;
1943 si->processorType = PROC_486;
1946 si->processorType = PROC_PENTIUM;
1948 default: // Just in case
1949 si->processorType = PROC_PENTIUM;
1956 * This routine returns information about the system. This does not effect
1957 * any logic and if the info is wrong - it doesn't matter.
1960 /* Get all the info we can not get from kernel services */
1961 static int adpt_system_info(void __user *buffer)
1965 memset(&si, 0, sizeof(si));
1967 si.osType = OS_LINUX;
1968 si.osMajorVersion = 0;
1969 si.osMinorVersion = 0;
1971 si.busType = SI_PCI_BUS;
1972 si.processorFamily = DPTI_sig.dsProcessorFamily;
1974 #if defined __i386__
1975 adpt_i386_info(&si);
1976 #elif defined (__ia64__)
1977 adpt_ia64_info(&si);
1978 #elif defined(__sparc__)
1979 adpt_sparc_info(&si);
1980 #elif defined (__alpha__)
1981 adpt_alpha_info(&si);
1983 si.processorType = 0xff ;
1985 if (copy_to_user(buffer, &si, sizeof(si))){
1986 printk(KERN_WARNING"dpti: Could not copy buffer TO user\n");
1993 static int adpt_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
1999 void __user *argp = (void __user *)arg;
2001 minor = iminor(inode);
2002 if (minor >= DPTI_MAX_HBA){
2005 mutex_lock(&adpt_configuration_lock);
2006 for (pHba = hba_chain; pHba; pHba = pHba->next) {
2007 if (pHba->unit == minor) {
2008 break; /* found adapter */
2011 mutex_unlock(&adpt_configuration_lock);
2016 while((volatile u32) pHba->state & DPTI_STATE_RESET )
2017 schedule_timeout_uninterruptible(2);
2020 // TODO: handle 3 cases
2022 if (copy_to_user(argp, &DPTI_sig, sizeof(DPTI_sig))) {
2027 return adpt_i2o_passthru(pHba, argp);
2030 drvrHBAinfo_S HbaInfo;
2032 #define FLG_OSD_PCI_VALID 0x0001
2033 #define FLG_OSD_DMA 0x0002
2034 #define FLG_OSD_I2O 0x0004
2035 memset(&HbaInfo, 0, sizeof(HbaInfo));
2036 HbaInfo.drvrHBAnum = pHba->unit;
2037 HbaInfo.baseAddr = (ulong) pHba->base_addr_phys;
2038 HbaInfo.blinkState = adpt_read_blink_led(pHba);
2039 HbaInfo.pciBusNum = pHba->pDev->bus->number;
2040 HbaInfo.pciDeviceNum=PCI_SLOT(pHba->pDev->devfn);
2041 HbaInfo.Interrupt = pHba->pDev->irq;
2042 HbaInfo.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
2043 if(copy_to_user(argp, &HbaInfo, sizeof(HbaInfo))){
2044 printk(KERN_WARNING"%s: Could not copy HbaInfo TO user\n",pHba->name);
2050 return adpt_system_info(argp);
2053 value = (u32)adpt_read_blink_led(pHba);
2054 if (copy_to_user(argp, &value, sizeof(value))) {
2061 spin_lock_irqsave(pHba->host->host_lock, flags);
2062 adpt_hba_reset(pHba);
2064 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2076 static long adpt_unlocked_ioctl(struct file *file, uint cmd, ulong arg)
2078 struct inode *inode;
2081 inode = file_inode(file);
2083 mutex_lock(&adpt_mutex);
2084 ret = adpt_ioctl(inode, file, cmd, arg);
2085 mutex_unlock(&adpt_mutex);
2090 #ifdef CONFIG_COMPAT
2091 static long compat_adpt_ioctl(struct file *file,
2092 unsigned int cmd, unsigned long arg)
2094 struct inode *inode;
2097 inode = file_inode(file);
2099 mutex_lock(&adpt_mutex);
2109 case (DPT_TARGET_BUSY & 0xFFFF):
2110 case DPT_TARGET_BUSY:
2111 ret = adpt_ioctl(inode, file, cmd, arg);
2117 mutex_unlock(&adpt_mutex);
2123 static irqreturn_t adpt_isr(int irq, void *dev_id)
2125 struct scsi_cmnd* cmd;
2126 adpt_hba* pHba = dev_id;
2128 void __iomem *reply;
2135 printk(KERN_WARNING"adpt_isr: NULL dev_id\n");
2139 spin_lock_irqsave(pHba->host->host_lock, flags);
2141 while( readl(pHba->irq_mask) & I2O_INTERRUPT_PENDING_B) {
2142 m = readl(pHba->reply_port);
2143 if(m == EMPTY_QUEUE){
2144 // Try twice then give up
2146 m = readl(pHba->reply_port);
2147 if(m == EMPTY_QUEUE){
2148 // This really should not happen
2149 printk(KERN_ERR"dpti: Could not get reply frame\n");
2153 if (pHba->reply_pool_pa <= m &&
2154 m < pHba->reply_pool_pa +
2155 (pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4)) {
2156 reply = (u8 *)pHba->reply_pool +
2157 (m - pHba->reply_pool_pa);
2159 /* Ick, we should *never* be here */
2160 printk(KERN_ERR "dpti: reply frame not from pool\n");
2161 reply = (u8 *)bus_to_virt(m);
2164 if (readl(reply) & MSG_FAIL) {
2165 u32 old_m = readl(reply+28);
2168 PDEBUG("%s: Failed message\n",pHba->name);
2169 if(old_m >= 0x100000){
2170 printk(KERN_ERR"%s: Bad preserved MFA (%x)- dropping frame\n",pHba->name,old_m);
2171 writel(m,pHba->reply_port);
2174 // Transaction context is 0 in failed reply frame
2175 msg = pHba->msg_addr_virt + old_m;
2176 old_context = readl(msg+12);
2177 writel(old_context, reply+12);
2178 adpt_send_nop(pHba, old_m);
2180 context = readl(reply+8);
2181 if(context & 0x40000000){ // IOCTL
2182 void *p = adpt_ioctl_from_context(pHba, readl(reply+12));
2184 memcpy_fromio(p, reply, REPLY_FRAME_SIZE * 4);
2186 // All IOCTLs will also be post wait
2188 if(context & 0x80000000){ // Post wait message
2189 status = readl(reply+16);
2191 status &= 0xffff; /* Get detail status */
2193 status = I2O_POST_WAIT_OK;
2195 if(!(context & 0x40000000)) {
2196 cmd = adpt_cmd_from_context(pHba,
2199 printk(KERN_WARNING"%s: Apparent SCSI cmd in Post Wait Context - cmd=%p context=%x\n", pHba->name, cmd, context);
2202 adpt_i2o_post_wait_complete(context, status);
2203 } else { // SCSI message
2204 cmd = adpt_cmd_from_context (pHba, readl(reply+12));
2206 scsi_dma_unmap(cmd);
2207 if(cmd->serial_number != 0) { // If not timedout
2208 adpt_i2o_to_scsi(reply, cmd);
2212 writel(m, pHba->reply_port);
2218 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2219 return IRQ_RETVAL(handled);
2222 static s32 adpt_scsi_to_i2o(adpt_hba* pHba, struct scsi_cmnd* cmd, struct adpt_device* d)
2225 u32 msg[MAX_MESSAGE_SIZE];
2237 memset(msg, 0 , sizeof(msg));
2238 len = scsi_bufflen(cmd);
2239 direction = 0x00000000;
2241 scsidir = 0x00000000; // DATA NO XFER
2244 * Set SCBFlags to indicate if data is being transferred
2245 * in or out, or no data transfer
2246 * Note: Do not have to verify index is less than 0 since
2247 * cmd->cmnd[0] is an unsigned char
2249 switch(cmd->sc_data_direction){
2250 case DMA_FROM_DEVICE:
2251 scsidir =0x40000000; // DATA IN (iop<--dev)
2254 direction=0x04000000; // SGL OUT
2255 scsidir =0x80000000; // DATA OUT (iop-->dev)
2259 case DMA_BIDIRECTIONAL:
2260 scsidir =0x40000000; // DATA IN (iop<--dev)
2261 // Assume In - and continue;
2264 printk(KERN_WARNING"%s: scsi opcode 0x%x not supported.\n",
2265 pHba->name, cmd->cmnd[0]);
2266 cmd->result = (DID_OK <<16) | (INITIATOR_ERROR << 8);
2267 cmd->scsi_done(cmd);
2271 // msg[0] is set later
2272 // I2O_CMD_SCSI_EXEC
2273 msg[1] = ((0xff<<24)|(HOST_TID<<12)|d->tid);
2275 msg[3] = adpt_cmd_to_context(cmd); /* Want SCSI control block back */
2276 // Our cards use the transaction context as the tag for queueing
2277 // Adaptec/DPT Private stuff
2278 msg[4] = I2O_CMD_SCSI_EXEC|(DPT_ORGANIZATION_ID<<16);
2280 /* Direction, disconnect ok | sense data | simple queue , CDBLen */
2281 // I2O_SCB_FLAG_ENABLE_DISCONNECT |
2282 // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
2283 // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
2284 msg[6] = scsidir|0x20a00000|cmd->cmd_len;
2288 // Write SCSI command into the message - always 16 byte block
2289 memset(mptr, 0, 16);
2290 memcpy(mptr, cmd->cmnd, cmd->cmd_len);
2292 lenptr=mptr++; /* Remember me - fill in when we know */
2293 if (dpt_dma64(pHba)) {
2294 reqlen = 16; // SINGLE SGE
2295 *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
2296 *mptr++ = 1 << PAGE_SHIFT;
2298 reqlen = 14; // SINGLE SGE
2300 /* Now fill in the SGList and command */
2302 nseg = scsi_dma_map(cmd);
2305 struct scatterlist *sg;
2308 scsi_for_each_sg(cmd, sg, nseg, i) {
2310 *mptr++ = direction|0x10000000|sg_dma_len(sg);
2311 len+=sg_dma_len(sg);
2312 addr = sg_dma_address(sg);
2313 *mptr++ = dma_low(addr);
2314 if (dpt_dma64(pHba))
2315 *mptr++ = dma_high(addr);
2316 /* Make this an end of list */
2318 *lptr = direction|0xD0000000|sg_dma_len(sg);
2320 reqlen = mptr - msg;
2323 if(cmd->underflow && len != cmd->underflow){
2324 printk(KERN_WARNING"Cmd len %08X Cmd underflow %08X\n",
2325 len, cmd->underflow);
2332 /* Stick the headers on */
2333 msg[0] = reqlen<<16 | ((reqlen > 12) ? SGL_OFFSET_12 : SGL_OFFSET_0);
2335 // Send it on it's way
2336 rcode = adpt_i2o_post_this(pHba, msg, reqlen<<2);
2344 static s32 adpt_scsi_host_alloc(adpt_hba* pHba, struct scsi_host_template *sht)
2346 struct Scsi_Host *host;
2348 host = scsi_host_alloc(sht, sizeof(adpt_hba*));
2350 printk("%s: scsi_host_alloc returned NULL\n", pHba->name);
2353 host->hostdata[0] = (unsigned long)pHba;
2356 host->irq = pHba->pDev->irq;
2357 /* no IO ports, so don't have to set host->io_port and
2361 host->n_io_port = 0;
2362 /* see comments in scsi_host.h */
2364 host->max_lun = 256;
2365 host->max_channel = pHba->top_scsi_channel + 1;
2366 host->cmd_per_lun = 1;
2367 host->unique_id = (u32)sys_tbl_pa + pHba->unit;
2368 host->sg_tablesize = pHba->sg_tablesize;
2369 host->can_queue = pHba->post_fifo_size;
2370 host->use_cmd_list = 1;
2376 static s32 adpt_i2o_to_scsi(void __iomem *reply, struct scsi_cmnd* cmd)
2381 u32 reply_flags = readl(reply) & 0xff00; // Leave it shifted up 8 bits
2382 // I know this would look cleaner if I just read bytes
2383 // but the model I have been using for all the rest of the
2384 // io is in 4 byte words - so I keep that model
2385 u16 detailed_status = readl(reply+16) &0xffff;
2386 dev_status = (detailed_status & 0xff);
2387 hba_status = detailed_status >> 8;
2389 // calculate resid for sg
2390 scsi_set_resid(cmd, scsi_bufflen(cmd) - readl(reply+20));
2392 pHba = (adpt_hba*) cmd->device->host->hostdata[0];
2394 cmd->sense_buffer[0] = '\0'; // initialize sense valid flag to false
2396 if(!(reply_flags & MSG_FAIL)) {
2397 switch(detailed_status & I2O_SCSI_DSC_MASK) {
2398 case I2O_SCSI_DSC_SUCCESS:
2399 cmd->result = (DID_OK << 16);
2401 if (readl(reply+20) < cmd->underflow) {
2402 cmd->result = (DID_ERROR <<16);
2403 printk(KERN_WARNING"%s: SCSI CMD underflow\n",pHba->name);
2406 case I2O_SCSI_DSC_REQUEST_ABORTED:
2407 cmd->result = (DID_ABORT << 16);
2409 case I2O_SCSI_DSC_PATH_INVALID:
2410 case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
2411 case I2O_SCSI_DSC_SELECTION_TIMEOUT:
2412 case I2O_SCSI_DSC_COMMAND_TIMEOUT:
2413 case I2O_SCSI_DSC_NO_ADAPTER:
2414 case I2O_SCSI_DSC_RESOURCE_UNAVAILABLE:
2415 printk(KERN_WARNING"%s: SCSI Timeout-Device (%d,%d,%llu) hba status=0x%x, dev status=0x%x, cmd=0x%x\n",
2416 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun, hba_status, dev_status, cmd->cmnd[0]);
2417 cmd->result = (DID_TIME_OUT << 16);
2419 case I2O_SCSI_DSC_ADAPTER_BUSY:
2420 case I2O_SCSI_DSC_BUS_BUSY:
2421 cmd->result = (DID_BUS_BUSY << 16);
2423 case I2O_SCSI_DSC_SCSI_BUS_RESET:
2424 case I2O_SCSI_DSC_BDR_MESSAGE_SENT:
2425 cmd->result = (DID_RESET << 16);
2427 case I2O_SCSI_DSC_PARITY_ERROR_FAILURE:
2428 printk(KERN_WARNING"%s: SCSI CMD parity error\n",pHba->name);
2429 cmd->result = (DID_PARITY << 16);
2431 case I2O_SCSI_DSC_UNABLE_TO_ABORT:
2432 case I2O_SCSI_DSC_COMPLETE_WITH_ERROR:
2433 case I2O_SCSI_DSC_UNABLE_TO_TERMINATE:
2434 case I2O_SCSI_DSC_MR_MESSAGE_RECEIVED:
2435 case I2O_SCSI_DSC_AUTOSENSE_FAILED:
2436 case I2O_SCSI_DSC_DATA_OVERRUN:
2437 case I2O_SCSI_DSC_UNEXPECTED_BUS_FREE:
2438 case I2O_SCSI_DSC_SEQUENCE_FAILURE:
2439 case I2O_SCSI_DSC_REQUEST_LENGTH_ERROR:
2440 case I2O_SCSI_DSC_PROVIDE_FAILURE:
2441 case I2O_SCSI_DSC_REQUEST_TERMINATED:
2442 case I2O_SCSI_DSC_IDE_MESSAGE_SENT:
2443 case I2O_SCSI_DSC_UNACKNOWLEDGED_EVENT:
2444 case I2O_SCSI_DSC_MESSAGE_RECEIVED:
2445 case I2O_SCSI_DSC_INVALID_CDB:
2446 case I2O_SCSI_DSC_LUN_INVALID:
2447 case I2O_SCSI_DSC_SCSI_TID_INVALID:
2448 case I2O_SCSI_DSC_FUNCTION_UNAVAILABLE:
2449 case I2O_SCSI_DSC_NO_NEXUS:
2450 case I2O_SCSI_DSC_CDB_RECEIVED:
2451 case I2O_SCSI_DSC_LUN_ALREADY_ENABLED:
2452 case I2O_SCSI_DSC_QUEUE_FROZEN:
2453 case I2O_SCSI_DSC_REQUEST_INVALID:
2455 printk(KERN_WARNING"%s: SCSI error %0x-Device(%d,%d,%llu) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2456 pHba->name, detailed_status & I2O_SCSI_DSC_MASK, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun,
2457 hba_status, dev_status, cmd->cmnd[0]);
2458 cmd->result = (DID_ERROR << 16);
2462 // copy over the request sense data if it was a check
2464 if (dev_status == SAM_STAT_CHECK_CONDITION) {
2465 u32 len = min(SCSI_SENSE_BUFFERSIZE, 40);
2466 // Copy over the sense data
2467 memcpy_fromio(cmd->sense_buffer, (reply+28) , len);
2468 if(cmd->sense_buffer[0] == 0x70 /* class 7 */ &&
2469 cmd->sense_buffer[2] == DATA_PROTECT ){
2470 /* This is to handle an array failed */
2471 cmd->result = (DID_TIME_OUT << 16);
2472 printk(KERN_WARNING"%s: SCSI Data Protect-Device (%d,%d,%llu) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2473 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun,
2474 hba_status, dev_status, cmd->cmnd[0]);
2479 /* In this condtion we could not talk to the tid
2480 * the card rejected it. We should signal a retry
2481 * for a limitted number of retries.
2483 cmd->result = (DID_TIME_OUT << 16);
2484 printk(KERN_WARNING"%s: I2O MSG_FAIL - Device (%d,%d,%llu) tid=%d, cmd=0x%x\n",
2485 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun,
2486 ((struct adpt_device*)(cmd->device->hostdata))->tid, cmd->cmnd[0]);
2489 cmd->result |= (dev_status);
2491 if(cmd->scsi_done != NULL){
2492 cmd->scsi_done(cmd);
2498 static s32 adpt_rescan(adpt_hba* pHba)
2504 spin_lock_irqsave(pHba->host->host_lock, flags);
2505 if ((rcode=adpt_i2o_lct_get(pHba)) < 0)
2507 if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0)
2511 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2516 static s32 adpt_i2o_reparse_lct(adpt_hba* pHba)
2521 struct i2o_device *d;
2522 i2o_lct *lct = pHba->lct;
2526 u32 buf[10]; // at least 8 u32's
2527 struct adpt_device* pDev = NULL;
2528 struct i2o_device* pI2o_dev = NULL;
2531 printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
2535 max = lct->table_size;
2539 // Mark each drive as unscanned
2540 for (d = pHba->devices; d; d = d->next) {
2541 pDev =(struct adpt_device*) d->owner;
2545 pDev->state |= DPTI_DEV_UNSCANNED;
2548 printk(KERN_INFO "%s: LCT has %d entries.\n", pHba->name,max);
2550 for(i=0;i<max;i++) {
2551 if( lct->lct_entry[i].user_tid != 0xfff){
2555 if( lct->lct_entry[i].class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
2556 lct->lct_entry[i].class_id == I2O_CLASS_SCSI_PERIPHERAL ||
2557 lct->lct_entry[i].class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
2558 tid = lct->lct_entry[i].tid;
2559 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
2560 printk(KERN_ERR"%s: Could not query device\n",pHba->name);
2563 bus_no = buf[0]>>16;
2564 if (bus_no >= MAX_CHANNEL) { /* Something wrong skip it */
2566 "%s: Channel number %d out of range\n",
2567 pHba->name, bus_no);
2572 scsi_lun = scsilun_to_int((struct scsi_lun *)&buf[2]);
2573 pDev = pHba->channel[bus_no].device[scsi_id];
2576 if(pDev->scsi_lun == scsi_lun) {
2579 pDev = pDev->next_lun;
2581 if(!pDev ) { // Something new add it
2582 d = kmalloc(sizeof(struct i2o_device),
2586 printk(KERN_CRIT "Out of memory for I2O device data.\n");
2590 d->controller = pHba;
2593 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2596 adpt_i2o_report_hba_unit(pHba, d);
2597 adpt_i2o_install_device(pHba, d);
2599 pDev = pHba->channel[bus_no].device[scsi_id];
2602 kzalloc(sizeof(struct adpt_device),
2607 pHba->channel[bus_no].device[scsi_id] = pDev;
2609 while (pDev->next_lun) {
2610 pDev = pDev->next_lun;
2612 pDev = pDev->next_lun =
2613 kzalloc(sizeof(struct adpt_device),
2619 pDev->tid = d->lct_data.tid;
2620 pDev->scsi_channel = bus_no;
2621 pDev->scsi_id = scsi_id;
2622 pDev->scsi_lun = scsi_lun;
2625 pDev->type = (buf[0])&0xff;
2626 pDev->flags = (buf[0]>>8)&0xff;
2627 // Too late, SCSI system has made up it's mind, but what the hey ...
2628 if(scsi_id > pHba->top_scsi_id){
2629 pHba->top_scsi_id = scsi_id;
2631 if(scsi_lun > pHba->top_scsi_lun){
2632 pHba->top_scsi_lun = scsi_lun;
2635 } // end of new i2o device
2637 // We found an old device - check it
2639 if(pDev->scsi_lun == scsi_lun) {
2640 if(!scsi_device_online(pDev->pScsi_dev)) {
2641 printk(KERN_WARNING"%s: Setting device (%d,%d,%llu) back online\n",
2642 pHba->name,bus_no,scsi_id,scsi_lun);
2643 if (pDev->pScsi_dev) {
2644 scsi_device_set_state(pDev->pScsi_dev, SDEV_RUNNING);
2648 if(d->lct_data.tid != tid) { // something changed
2650 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2651 if (pDev->pScsi_dev) {
2652 pDev->pScsi_dev->changed = TRUE;
2653 pDev->pScsi_dev->removable = TRUE;
2656 // Found it - mark it scanned
2657 pDev->state = DPTI_DEV_ONLINE;
2660 pDev = pDev->next_lun;
2664 for (pI2o_dev = pHba->devices; pI2o_dev; pI2o_dev = pI2o_dev->next) {
2665 pDev =(struct adpt_device*) pI2o_dev->owner;
2669 // Drive offline drives that previously existed but could not be found
2671 if (pDev->state & DPTI_DEV_UNSCANNED){
2672 pDev->state = DPTI_DEV_OFFLINE;
2673 printk(KERN_WARNING"%s: Device (%d,%d,%llu) offline\n",pHba->name,pDev->scsi_channel,pDev->scsi_id,pDev->scsi_lun);
2674 if (pDev->pScsi_dev) {
2675 scsi_device_set_state(pDev->pScsi_dev, SDEV_OFFLINE);
2682 static void adpt_fail_posted_scbs(adpt_hba* pHba)
2684 struct scsi_cmnd* cmd = NULL;
2685 struct scsi_device* d = NULL;
2687 shost_for_each_device(d, pHba->host) {
2688 unsigned long flags;
2689 spin_lock_irqsave(&d->list_lock, flags);
2690 list_for_each_entry(cmd, &d->cmd_list, list) {
2691 if(cmd->serial_number == 0){
2694 cmd->result = (DID_OK << 16) | (QUEUE_FULL <<1);
2695 cmd->scsi_done(cmd);
2697 spin_unlock_irqrestore(&d->list_lock, flags);
2702 /*============================================================================
2703 * Routines from i2o subsystem
2704 *============================================================================
2710 * Bring an I2O controller into HOLD state. See the spec.
2712 static int adpt_i2o_activate_hba(adpt_hba* pHba)
2716 if(pHba->initialized ) {
2717 if (adpt_i2o_status_get(pHba) < 0) {
2718 if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2719 printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2722 if (adpt_i2o_status_get(pHba) < 0) {
2723 printk(KERN_INFO "HBA not responding.\n");
2728 if(pHba->status_block->iop_state == ADAPTER_STATE_FAULTED) {
2729 printk(KERN_CRIT "%s: hardware fault\n", pHba->name);
2733 if (pHba->status_block->iop_state == ADAPTER_STATE_READY ||
2734 pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
2735 pHba->status_block->iop_state == ADAPTER_STATE_HOLD ||
2736 pHba->status_block->iop_state == ADAPTER_STATE_FAILED) {
2737 adpt_i2o_reset_hba(pHba);
2738 if (adpt_i2o_status_get(pHba) < 0 || pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
2739 printk(KERN_ERR "%s: Failed to initialize.\n", pHba->name);
2744 if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2745 printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2751 if (adpt_i2o_init_outbound_q(pHba) < 0) {
2757 if (adpt_i2o_hrt_get(pHba) < 0) {
2765 * Bring a controller online into OPERATIONAL state.
2768 static int adpt_i2o_online_hba(adpt_hba* pHba)
2770 if (adpt_i2o_systab_send(pHba) < 0) {
2771 adpt_i2o_delete_hba(pHba);
2774 /* In READY state */
2776 if (adpt_i2o_enable_hba(pHba) < 0) {
2777 adpt_i2o_delete_hba(pHba);
2781 /* In OPERATIONAL state */
2785 static s32 adpt_send_nop(adpt_hba*pHba,u32 m)
2788 ulong timeout = jiffies + 5*HZ;
2790 while(m == EMPTY_QUEUE){
2792 m = readl(pHba->post_port);
2793 if(m != EMPTY_QUEUE){
2796 if(time_after(jiffies,timeout)){
2797 printk(KERN_ERR "%s: Timeout waiting for message frame!\n",pHba->name);
2800 schedule_timeout_uninterruptible(1);
2802 msg = (u32 __iomem *)(pHba->msg_addr_virt + m);
2803 writel( THREE_WORD_MSG_SIZE | SGL_OFFSET_0,&msg[0]);
2804 writel( I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0,&msg[1]);
2808 writel(m, pHba->post_port);
2813 static s32 adpt_i2o_init_outbound_q(adpt_hba* pHba)
2817 u32 __iomem *msg = NULL;
2819 ulong timeout = jiffies + TMOUT_INITOUTBOUND*HZ;
2824 m = readl(pHba->post_port);
2825 if (m != EMPTY_QUEUE) {
2829 if(time_after(jiffies,timeout)){
2830 printk(KERN_WARNING"%s: Timeout waiting for message frame\n",pHba->name);
2833 schedule_timeout_uninterruptible(1);
2834 } while(m == EMPTY_QUEUE);
2836 msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
2838 status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
2840 adpt_send_nop(pHba, m);
2841 printk(KERN_WARNING"%s: IOP reset failed - no free memory.\n",
2845 memset(status, 0, 4);
2847 writel(EIGHT_WORD_MSG_SIZE| SGL_OFFSET_6, &msg[0]);
2848 writel(I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID, &msg[1]);
2850 writel(0x0106, &msg[3]); /* Transaction context */
2851 writel(4096, &msg[4]); /* Host page frame size */
2852 writel((REPLY_FRAME_SIZE)<<16|0x80, &msg[5]); /* Outbound msg frame size and Initcode */
2853 writel(0xD0000004, &msg[6]); /* Simple SG LE, EOB */
2854 writel((u32)addr, &msg[7]);
2856 writel(m, pHba->post_port);
2859 // Wait for the reply status to come back
2862 if (*status != 0x01 /*I2O_EXEC_OUTBOUND_INIT_IN_PROGRESS*/) {
2867 if(time_after(jiffies,timeout)){
2868 printk(KERN_WARNING"%s: Timeout Initializing\n",pHba->name);
2869 /* We lose 4 bytes of "status" here, but we
2870 cannot free these because controller may
2871 awake and corrupt those bytes at any time */
2872 /* dma_free_coherent(&pHba->pDev->dev, 4, status, addr); */
2875 schedule_timeout_uninterruptible(1);
2878 // If the command was successful, fill the fifo with our reply
2880 if(*status != 0x04 /*I2O_EXEC_OUTBOUND_INIT_COMPLETE*/) {
2881 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
2884 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
2886 if(pHba->reply_pool != NULL) {
2887 dma_free_coherent(&pHba->pDev->dev,
2888 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
2889 pHba->reply_pool, pHba->reply_pool_pa);
2892 pHba->reply_pool = dma_alloc_coherent(&pHba->pDev->dev,
2893 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
2894 &pHba->reply_pool_pa, GFP_KERNEL);
2895 if (!pHba->reply_pool) {
2896 printk(KERN_ERR "%s: Could not allocate reply pool\n", pHba->name);
2899 memset(pHba->reply_pool, 0 , pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4);
2901 for(i = 0; i < pHba->reply_fifo_size; i++) {
2902 writel(pHba->reply_pool_pa + (i * REPLY_FRAME_SIZE * 4),
2906 adpt_i2o_status_get(pHba);
2912 * I2O System Table. Contains information about
2913 * all the IOPs in the system. Used to inform IOPs
2914 * about each other's existence.
2916 * sys_tbl_ver is the CurrentChangeIndicator that is
2917 * used by IOPs to track changes.
2922 static s32 adpt_i2o_status_get(adpt_hba* pHba)
2927 u8 *status_block=NULL;
2929 if(pHba->status_block == NULL) {
2930 pHba->status_block = dma_alloc_coherent(&pHba->pDev->dev,
2931 sizeof(i2o_status_block),
2932 &pHba->status_block_pa, GFP_KERNEL);
2933 if(pHba->status_block == NULL) {
2935 "dpti%d: Get Status Block failed; Out of memory. \n",
2940 memset(pHba->status_block, 0, sizeof(i2o_status_block));
2941 status_block = (u8*)(pHba->status_block);
2942 timeout = jiffies+TMOUT_GETSTATUS*HZ;
2945 m = readl(pHba->post_port);
2946 if (m != EMPTY_QUEUE) {
2949 if(time_after(jiffies,timeout)){
2950 printk(KERN_ERR "%s: Timeout waiting for message !\n",
2954 schedule_timeout_uninterruptible(1);
2955 } while(m==EMPTY_QUEUE);
2958 msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
2960 writel(NINE_WORD_MSG_SIZE|SGL_OFFSET_0, &msg[0]);
2961 writel(I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID, &msg[1]);
2966 writel( dma_low(pHba->status_block_pa), &msg[6]);
2967 writel( dma_high(pHba->status_block_pa), &msg[7]);
2968 writel(sizeof(i2o_status_block), &msg[8]); // 88 bytes
2971 writel(m, pHba->post_port);
2974 while(status_block[87]!=0xff){
2975 if(time_after(jiffies,timeout)){
2976 printk(KERN_ERR"dpti%d: Get status timeout.\n",
2981 schedule_timeout_uninterruptible(1);
2984 // Set up our number of outbound and inbound messages
2985 pHba->post_fifo_size = pHba->status_block->max_inbound_frames;
2986 if (pHba->post_fifo_size > MAX_TO_IOP_MESSAGES) {
2987 pHba->post_fifo_size = MAX_TO_IOP_MESSAGES;
2990 pHba->reply_fifo_size = pHba->status_block->max_outbound_frames;
2991 if (pHba->reply_fifo_size > MAX_FROM_IOP_MESSAGES) {
2992 pHba->reply_fifo_size = MAX_FROM_IOP_MESSAGES;
2995 // Calculate the Scatter Gather list size
2996 if (dpt_dma64(pHba)) {
2998 = ((pHba->status_block->inbound_frame_size * 4
3000 / (sizeof(struct sg_simple_element) + sizeof(u32)));
3003 = ((pHba->status_block->inbound_frame_size * 4
3005 / sizeof(struct sg_simple_element));
3007 if (pHba->sg_tablesize > SG_LIST_ELEMENTS) {
3008 pHba->sg_tablesize = SG_LIST_ELEMENTS;
3013 printk("dpti%d: State = ",pHba->unit);
3014 switch(pHba->status_block->iop_state) {
3028 printk("OPERATIONAL\n");
3034 printk("FAULTED\n");
3037 printk("%x (unknown!!)\n",pHba->status_block->iop_state);
3044 * Get the IOP's Logical Configuration Table
3046 static int adpt_i2o_lct_get(adpt_hba* pHba)
3052 if ((pHba->lct_size == 0) || (pHba->lct == NULL)){
3053 pHba->lct_size = pHba->status_block->expected_lct_size;
3056 if (pHba->lct == NULL) {
3057 pHba->lct = dma_alloc_coherent(&pHba->pDev->dev,
3058 pHba->lct_size, &pHba->lct_pa,
3060 if(pHba->lct == NULL) {
3061 printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
3066 memset(pHba->lct, 0, pHba->lct_size);
3068 msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
3069 msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
3072 msg[4] = 0xFFFFFFFF; /* All devices */
3073 msg[5] = 0x00000000; /* Report now */
3074 msg[6] = 0xD0000000|pHba->lct_size;
3075 msg[7] = (u32)pHba->lct_pa;
3077 if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 360))) {
3078 printk(KERN_ERR "%s: LCT Get failed (status=%#10x.\n",
3080 printk(KERN_ERR"Adaptec: Error Reading Hardware.\n");
3084 if ((pHba->lct->table_size << 2) > pHba->lct_size) {
3085 pHba->lct_size = pHba->lct->table_size << 2;
3086 dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
3087 pHba->lct, pHba->lct_pa);
3090 } while (pHba->lct == NULL);
3092 PDEBUG("%s: Hardware resource table read.\n", pHba->name);
3095 // I2O_DPT_EXEC_IOP_BUFFERS_GROUP_NO;
3096 if(adpt_i2o_query_scalar(pHba, 0 , 0x8000, -1, buf, sizeof(buf))>=0) {
3097 pHba->FwDebugBufferSize = buf[1];
3098 pHba->FwDebugBuffer_P = ioremap(pHba->base_addr_phys + buf[0],
3099 pHba->FwDebugBufferSize);
3100 if (pHba->FwDebugBuffer_P) {
3101 pHba->FwDebugFlags_P = pHba->FwDebugBuffer_P +
3102 FW_DEBUG_FLAGS_OFFSET;
3103 pHba->FwDebugBLEDvalue_P = pHba->FwDebugBuffer_P +
3104 FW_DEBUG_BLED_OFFSET;
3105 pHba->FwDebugBLEDflag_P = pHba->FwDebugBLEDvalue_P + 1;
3106 pHba->FwDebugStrLength_P = pHba->FwDebugBuffer_P +
3107 FW_DEBUG_STR_LENGTH_OFFSET;
3108 pHba->FwDebugBuffer_P += buf[2];
3109 pHba->FwDebugFlags = 0;
3116 static int adpt_i2o_build_sys_table(void)
3118 adpt_hba* pHba = hba_chain;
3122 dma_free_coherent(&pHba->pDev->dev, sys_tbl_len,
3123 sys_tbl, sys_tbl_pa);
3125 sys_tbl_len = sizeof(struct i2o_sys_tbl) + // Header + IOPs
3126 (hba_count) * sizeof(struct i2o_sys_tbl_entry);
3128 sys_tbl = dma_alloc_coherent(&pHba->pDev->dev,
3129 sys_tbl_len, &sys_tbl_pa, GFP_KERNEL);
3131 printk(KERN_WARNING "SysTab Set failed. Out of memory.\n");
3134 memset(sys_tbl, 0, sys_tbl_len);
3136 sys_tbl->num_entries = hba_count;
3137 sys_tbl->version = I2OVERSION;
3138 sys_tbl->change_ind = sys_tbl_ind++;
3140 for(pHba = hba_chain; pHba; pHba = pHba->next) {
3142 // Get updated Status Block so we have the latest information
3143 if (adpt_i2o_status_get(pHba)) {
3144 sys_tbl->num_entries--;
3145 continue; // try next one
3148 sys_tbl->iops[count].org_id = pHba->status_block->org_id;
3149 sys_tbl->iops[count].iop_id = pHba->unit + 2;
3150 sys_tbl->iops[count].seg_num = 0;
3151 sys_tbl->iops[count].i2o_version = pHba->status_block->i2o_version;
3152 sys_tbl->iops[count].iop_state = pHba->status_block->iop_state;
3153 sys_tbl->iops[count].msg_type = pHba->status_block->msg_type;
3154 sys_tbl->iops[count].frame_size = pHba->status_block->inbound_frame_size;
3155 sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
3156 sys_tbl->iops[count].iop_capabilities = pHba->status_block->iop_capabilities;
3157 addr = pHba->base_addr_phys + 0x40;
3158 sys_tbl->iops[count].inbound_low = dma_low(addr);
3159 sys_tbl->iops[count].inbound_high = dma_high(addr);
3166 u32 *table = (u32*)sys_tbl;
3167 printk(KERN_DEBUG"sys_tbl_len=%d in 32bit words\n",(sys_tbl_len >>2));
3168 for(count = 0; count < (sys_tbl_len >>2); count++) {
3169 printk(KERN_INFO "sys_tbl[%d] = %0#10x\n",
3170 count, table[count]);
3180 * Dump the information block associated with a given unit (TID)
3183 static void adpt_i2o_report_hba_unit(adpt_hba* pHba, struct i2o_device *d)
3186 int unit = d->lct_data.tid;
3188 printk(KERN_INFO "TID %3.3d ", unit);
3190 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 3, buf, 16)>=0)
3193 printk(" Vendor: %-12.12s", buf);
3195 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 4, buf, 16)>=0)
3198 printk(" Device: %-12.12s", buf);
3200 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 6, buf, 8)>=0)
3203 printk(" Rev: %-12.12s\n", buf);
3206 printk(KERN_INFO "\tClass: %.21s\n", adpt_i2o_get_class_name(d->lct_data.class_id));
3207 printk(KERN_INFO "\tSubclass: 0x%04X\n", d->lct_data.sub_class);
3208 printk(KERN_INFO "\tFlags: ");
3210 if(d->lct_data.device_flags&(1<<0))
3211 printk("C"); // ConfigDialog requested
3212 if(d->lct_data.device_flags&(1<<1))
3213 printk("U"); // Multi-user capable
3214 if(!(d->lct_data.device_flags&(1<<4)))
3215 printk("P"); // Peer service enabled!
3216 if(!(d->lct_data.device_flags&(1<<5)))
3217 printk("M"); // Mgmt service enabled!
3224 * Do i2o class name lookup
3226 static const char *adpt_i2o_get_class_name(int class)
3229 static char *i2o_class_name[] = {
3231 "Device Driver Module",
3236 "Fibre Channel Port",
3237 "Fibre Channel Device",
3241 "Floppy Controller",
3243 "Secondary Bus Port",
3244 "Peer Transport Agent",
3249 switch(class&0xFFF) {
3250 case I2O_CLASS_EXECUTIVE:
3254 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
3256 case I2O_CLASS_SEQUENTIAL_STORAGE:
3262 case I2O_CLASS_FIBRE_CHANNEL_PORT:
3264 case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
3266 case I2O_CLASS_SCSI_PERIPHERAL:
3268 case I2O_CLASS_ATE_PORT:
3270 case I2O_CLASS_ATE_PERIPHERAL:
3272 case I2O_CLASS_FLOPPY_CONTROLLER:
3274 case I2O_CLASS_FLOPPY_DEVICE:
3276 case I2O_CLASS_BUS_ADAPTER_PORT:
3278 case I2O_CLASS_PEER_TRANSPORT_AGENT:
3280 case I2O_CLASS_PEER_TRANSPORT:
3283 return i2o_class_name[idx];
3288 static s32 adpt_i2o_hrt_get(adpt_hba* pHba)
3291 int ret, size = sizeof(i2o_hrt);
3294 if (pHba->hrt == NULL) {
3295 pHba->hrt = dma_alloc_coherent(&pHba->pDev->dev,
3296 size, &pHba->hrt_pa, GFP_KERNEL);
3297 if (pHba->hrt == NULL) {
3298 printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", pHba->name);
3303 msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
3304 msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
3307 msg[4]= (0xD0000000 | size); /* Simple transaction */
3308 msg[5]= (u32)pHba->hrt_pa; /* Dump it here */
3310 if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg),20))) {
3311 printk(KERN_ERR "%s: Unable to get HRT (status=%#10x)\n", pHba->name, ret);
3315 if (pHba->hrt->num_entries * pHba->hrt->entry_len << 2 > size) {
3316 int newsize = pHba->hrt->num_entries * pHba->hrt->entry_len << 2;
3317 dma_free_coherent(&pHba->pDev->dev, size,
3318 pHba->hrt, pHba->hrt_pa);
3322 } while(pHba->hrt == NULL);
3327 * Query one scalar group value or a whole scalar group.
3329 static int adpt_i2o_query_scalar(adpt_hba* pHba, int tid,
3330 int group, int field, void *buf, int buflen)
3332 u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
3334 dma_addr_t opblk_pa;
3336 dma_addr_t resblk_pa;
3340 /* 8 bytes for header */
3341 resblk_va = dma_alloc_coherent(&pHba->pDev->dev,
3342 sizeof(u8) * (8 + buflen), &resblk_pa, GFP_KERNEL);
3343 if (resblk_va == NULL) {
3344 printk(KERN_CRIT "%s: query scalar failed; Out of memory.\n", pHba->name);
3348 opblk_va = dma_alloc_coherent(&pHba->pDev->dev,
3349 sizeof(opblk), &opblk_pa, GFP_KERNEL);
3350 if (opblk_va == NULL) {
3351 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3352 resblk_va, resblk_pa);
3353 printk(KERN_CRIT "%s: query operatio failed; Out of memory.\n",
3357 if (field == -1) /* whole group */
3360 memcpy(opblk_va, opblk, sizeof(opblk));
3361 size = adpt_i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, pHba, tid,
3362 opblk_va, opblk_pa, sizeof(opblk),
3363 resblk_va, resblk_pa, sizeof(u8)*(8+buflen));
3364 dma_free_coherent(&pHba->pDev->dev, sizeof(opblk), opblk_va, opblk_pa);
3365 if (size == -ETIME) {
3366 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3367 resblk_va, resblk_pa);
3368 printk(KERN_WARNING "%s: issue params failed; Timed out.\n", pHba->name);
3370 } else if (size == -EINTR) {
3371 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3372 resblk_va, resblk_pa);
3373 printk(KERN_WARNING "%s: issue params failed; Interrupted.\n", pHba->name);
3377 memcpy(buf, resblk_va+8, buflen); /* cut off header */
3379 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3380 resblk_va, resblk_pa);
3388 /* Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
3390 * This function can be used for all UtilParamsGet/Set operations.
3391 * The OperationBlock is given in opblk-buffer,
3392 * and results are returned in resblk-buffer.
3393 * Note that the minimum sized resblk is 8 bytes and contains
3394 * ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
3396 static int adpt_i2o_issue_params(int cmd, adpt_hba* pHba, int tid,
3397 void *opblk_va, dma_addr_t opblk_pa, int oplen,
3398 void *resblk_va, dma_addr_t resblk_pa, int reslen)
3401 u32 *res = (u32 *)resblk_va;
3404 msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
3405 msg[1] = cmd << 24 | HOST_TID << 12 | tid;
3409 msg[5] = 0x54000000 | oplen; /* OperationBlock */
3410 msg[6] = (u32)opblk_pa;
3411 msg[7] = 0xD0000000 | reslen; /* ResultBlock */
3412 msg[8] = (u32)resblk_pa;
3414 if ((wait_status = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 20))) {
3415 printk("adpt_i2o_issue_params: post_wait failed (%p)\n", resblk_va);
3416 return wait_status; /* -DetailedStatus */
3419 if (res[1]&0x00FF0000) { /* BlockStatus != SUCCESS */
3420 printk(KERN_WARNING "%s: %s - Error:\n ErrorInfoSize = 0x%02x, "
3421 "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
3423 (cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
3425 res[1]>>24, (res[1]>>16)&0xFF, res[1]&0xFFFF);
3426 return -((res[1] >> 16) & 0xFF); /* -BlockStatus */
3429 return 4 + ((res[1] & 0x0000FFFF) << 2); /* bytes used in resblk */
3433 static s32 adpt_i2o_quiesce_hba(adpt_hba* pHba)
3438 adpt_i2o_status_get(pHba);
3440 /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
3442 if((pHba->status_block->iop_state != ADAPTER_STATE_READY) &&
3443 (pHba->status_block->iop_state != ADAPTER_STATE_OPERATIONAL)){
3447 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3448 msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
3452 if((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3453 printk(KERN_INFO"dpti%d: Unable to quiesce (status=%#x).\n",
3456 printk(KERN_INFO"dpti%d: Quiesced.\n",pHba->unit);
3459 adpt_i2o_status_get(pHba);
3465 * Enable IOP. Allows the IOP to resume external operations.
3467 static int adpt_i2o_enable_hba(adpt_hba* pHba)
3472 adpt_i2o_status_get(pHba);
3473 if(!pHba->status_block){
3476 /* Enable only allowed on READY state */
3477 if(pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
3480 if(pHba->status_block->iop_state != ADAPTER_STATE_READY)
3483 msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3484 msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
3488 if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3489 printk(KERN_WARNING"%s: Could not enable (status=%#10x).\n",
3492 PDEBUG("%s: Enabled.\n", pHba->name);
3495 adpt_i2o_status_get(pHba);
3500 static int adpt_i2o_systab_send(adpt_hba* pHba)
3505 msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
3506 msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
3509 msg[4] = (0<<16) | ((pHba->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
3510 msg[5] = 0; /* Segment 0 */
3513 * Provide three SGL-elements:
3514 * System table (SysTab), Private memory space declaration and
3515 * Private i/o space declaration
3517 msg[6] = 0x54000000 | sys_tbl_len;
3518 msg[7] = (u32)sys_tbl_pa;
3519 msg[8] = 0x54000000 | 0;
3521 msg[10] = 0xD4000000 | 0;
3524 if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 120))) {
3525 printk(KERN_INFO "%s: Unable to set SysTab (status=%#10x).\n",
3530 PINFO("%s: SysTab set.\n", pHba->name);
3538 /*============================================================================
3540 *============================================================================
3546 static static void adpt_delay(int millisec)
3549 for (i = 0; i < millisec; i++) {
3550 udelay(1000); /* delay for one millisecond */
3556 static struct scsi_host_template driver_template = {
3557 .module = THIS_MODULE,
3559 .proc_name = "dpt_i2o",
3560 .show_info = adpt_show_info,
3562 .queuecommand = adpt_queue,
3563 .eh_abort_handler = adpt_abort,
3564 .eh_device_reset_handler = adpt_device_reset,
3565 .eh_bus_reset_handler = adpt_bus_reset,
3566 .eh_host_reset_handler = adpt_reset,
3567 .bios_param = adpt_bios_param,
3568 .slave_configure = adpt_slave_configure,
3569 .can_queue = MAX_TO_IOP_MESSAGES,
3571 .use_clustering = ENABLE_CLUSTERING,
3574 static int __init adpt_init(void)
3577 adpt_hba *pHba, *next;
3579 printk("Loading Adaptec I2O RAID: Version " DPT_I2O_VERSION "\n");
3581 error = adpt_detect(&driver_template);
3584 if (hba_chain == NULL)
3587 for (pHba = hba_chain; pHba; pHba = pHba->next) {
3588 error = scsi_add_host(pHba->host, &pHba->pDev->dev);
3591 scsi_scan_host(pHba->host);
3595 for (pHba = hba_chain; pHba; pHba = next) {
3597 scsi_remove_host(pHba->host);
3602 static void __exit adpt_exit(void)
3604 adpt_hba *pHba, *next;
3606 for (pHba = hba_chain; pHba; pHba = pHba->next)
3607 scsi_remove_host(pHba->host);
3608 for (pHba = hba_chain; pHba; pHba = next) {
3610 adpt_release(pHba->host);
3614 module_init(adpt_init);
3615 module_exit(adpt_exit);
3617 MODULE_LICENSE("GPL");