ASoC: ep93xx i2s audio driver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / dpt_i2o.c
blobb0c576f84b2863a0e523f51f743ee402222e8396
1 /***************************************************************************
2 dpti.c - description
3 -------------------
4 begin : Thu Sep 7 2000
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
11 and credits
12 ***************************************************************************/
14 /***************************************************************************
15 * *
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. *
20 * *
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 **************************************************************************/
29 /*#define DEBUG 1 */
30 /*#define UARTDELAY 1 */
32 #include <linux/module.h>
34 MODULE_AUTHOR("Deanna Bonds, with _lots_ of help from Mark Salyzyn");
35 MODULE_DESCRIPTION("Adaptec I2O RAID Driver");
37 ////////////////////////////////////////////////////////////////
39 #include <linux/ioctl.h> /* For SCSI-Passthrough */
40 #include <asm/uaccess.h>
42 #include <linux/stat.h>
43 #include <linux/slab.h> /* for kmalloc() */
44 #include <linux/pci.h> /* for PCI support */
45 #include <linux/proc_fs.h>
46 #include <linux/blkdev.h>
47 #include <linux/delay.h> /* for udelay */
48 #include <linux/interrupt.h>
49 #include <linux/kernel.h> /* for printk */
50 #include <linux/sched.h>
51 #include <linux/reboot.h>
52 #include <linux/smp_lock.h>
53 #include <linux/spinlock.h>
54 #include <linux/dma-mapping.h>
56 #include <linux/timer.h>
57 #include <linux/string.h>
58 #include <linux/ioport.h>
59 #include <linux/mutex.h>
61 #include <asm/processor.h> /* for boot_cpu_data */
62 #include <asm/pgtable.h>
63 #include <asm/io.h> /* for virt_to_bus, etc. */
65 #include <scsi/scsi.h>
66 #include <scsi/scsi_cmnd.h>
67 #include <scsi/scsi_device.h>
68 #include <scsi/scsi_host.h>
69 #include <scsi/scsi_tcq.h>
71 #include "dpt/dptsig.h"
72 #include "dpti.h"
74 /*============================================================================
75 * Create a binary signature - this is read by dptsig
76 * Needed for our management apps
77 *============================================================================
79 static dpt_sig_S DPTI_sig = {
80 {'d', 'P', 't', 'S', 'i', 'G'}, SIG_VERSION,
81 #ifdef __i386__
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,
89 #else
90 (-1),(-1),
91 #endif
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 /*============================================================================
101 * Globals
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);
118 #ifdef CONFIG_COMPAT
119 static long compat_adpt_ioctl(struct file *, unsigned int, unsigned long);
120 #endif
122 static const struct file_operations adpt_fops = {
123 .unlocked_ioctl = adpt_unlocked_ioctl,
124 .open = adpt_open,
125 .release = adpt_close,
126 #ifdef CONFIG_COMPAT
127 .compat_ioctl = compat_adpt_ioctl,
128 #endif
131 /* Structures and definitions for synchronous message posting.
132 * See adpt_i2o_post_wait() for description
133 * */
134 struct adpt_i2o_post_wait_data
136 int status;
137 u32 id;
138 adpt_wait_queue_head_t *wq;
139 struct adpt_i2o_post_wait_data *next;
142 static struct adpt_i2o_post_wait_data *adpt_post_wait_queue = NULL;
143 static u32 adpt_post_wait_id = 0;
144 static DEFINE_SPINLOCK(adpt_post_wait_lock);
147 /*============================================================================
148 * Functions
149 *============================================================================
152 static inline int dpt_dma64(adpt_hba *pHba)
154 return (sizeof(dma_addr_t) > 4 && (pHba)->dma64);
157 static inline u32 dma_high(dma_addr_t addr)
159 return upper_32_bits(addr);
162 static inline u32 dma_low(dma_addr_t addr)
164 return (u32)addr;
167 static u8 adpt_read_blink_led(adpt_hba* host)
169 if (host->FwDebugBLEDflag_P) {
170 if( readb(host->FwDebugBLEDflag_P) == 0xbc ){
171 return readb(host->FwDebugBLEDvalue_P);
174 return 0;
177 /*============================================================================
178 * Scsi host template interface functions
179 *============================================================================
182 static struct pci_device_id dptids[] = {
183 { PCI_DPT_VENDOR_ID, PCI_DPT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
184 { PCI_DPT_VENDOR_ID, PCI_DPT_RAPTOR_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
185 { 0, }
187 MODULE_DEVICE_TABLE(pci,dptids);
189 static int adpt_detect(struct scsi_host_template* sht)
191 struct pci_dev *pDev = NULL;
192 adpt_hba *pHba;
193 adpt_hba *next;
195 PINFO("Detecting Adaptec I2O RAID controllers...\n");
197 /* search for all Adatpec I2O RAID cards */
198 while ((pDev = pci_get_device( PCI_DPT_VENDOR_ID, PCI_ANY_ID, pDev))) {
199 if(pDev->device == PCI_DPT_DEVICE_ID ||
200 pDev->device == PCI_DPT_RAPTOR_DEVICE_ID){
201 if(adpt_install_hba(sht, pDev) ){
202 PERROR("Could not Init an I2O RAID device\n");
203 PERROR("Will not try to detect others.\n");
204 return hba_count-1;
206 pci_dev_get(pDev);
210 /* In INIT state, Activate IOPs */
211 for (pHba = hba_chain; pHba; pHba = next) {
212 next = pHba->next;
213 // Activate does get status , init outbound, and get hrt
214 if (adpt_i2o_activate_hba(pHba) < 0) {
215 adpt_i2o_delete_hba(pHba);
220 /* Active IOPs in HOLD state */
222 rebuild_sys_tab:
223 if (hba_chain == NULL)
224 return 0;
227 * If build_sys_table fails, we kill everything and bail
228 * as we can't init the IOPs w/o a system table
230 if (adpt_i2o_build_sys_table() < 0) {
231 adpt_i2o_sys_shutdown();
232 return 0;
235 PDEBUG("HBA's in HOLD state\n");
237 /* If IOP don't get online, we need to rebuild the System table */
238 for (pHba = hba_chain; pHba; pHba = pHba->next) {
239 if (adpt_i2o_online_hba(pHba) < 0) {
240 adpt_i2o_delete_hba(pHba);
241 goto rebuild_sys_tab;
245 /* Active IOPs now in OPERATIONAL state */
246 PDEBUG("HBA's in OPERATIONAL state\n");
248 printk("dpti: If you have a lot of devices this could take a few minutes.\n");
249 for (pHba = hba_chain; pHba; pHba = next) {
250 next = pHba->next;
251 printk(KERN_INFO"%s: Reading the hardware resource table.\n", pHba->name);
252 if (adpt_i2o_lct_get(pHba) < 0){
253 adpt_i2o_delete_hba(pHba);
254 continue;
257 if (adpt_i2o_parse_lct(pHba) < 0){
258 adpt_i2o_delete_hba(pHba);
259 continue;
261 adpt_inquiry(pHba);
264 adpt_sysfs_class = class_create(THIS_MODULE, "dpt_i2o");
265 if (IS_ERR(adpt_sysfs_class)) {
266 printk(KERN_WARNING"dpti: unable to create dpt_i2o class\n");
267 adpt_sysfs_class = NULL;
270 for (pHba = hba_chain; pHba; pHba = next) {
271 next = pHba->next;
272 if (adpt_scsi_host_alloc(pHba, sht) < 0){
273 adpt_i2o_delete_hba(pHba);
274 continue;
276 pHba->initialized = TRUE;
277 pHba->state &= ~DPTI_STATE_RESET;
278 if (adpt_sysfs_class) {
279 struct device *dev = device_create(adpt_sysfs_class,
280 NULL, MKDEV(DPTI_I2O_MAJOR, pHba->unit), NULL,
281 "dpti%d", pHba->unit);
282 if (IS_ERR(dev)) {
283 printk(KERN_WARNING"dpti%d: unable to "
284 "create device in dpt_i2o class\n",
285 pHba->unit);
290 // Register our control device node
291 // nodes will need to be created in /dev to access this
292 // the nodes can not be created from within the driver
293 if (hba_count && register_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER, &adpt_fops)) {
294 adpt_i2o_sys_shutdown();
295 return 0;
297 return hba_count;
302 * scsi_unregister will be called AFTER we return.
304 static int adpt_release(struct Scsi_Host *host)
306 adpt_hba* pHba = (adpt_hba*) host->hostdata[0];
307 // adpt_i2o_quiesce_hba(pHba);
308 adpt_i2o_delete_hba(pHba);
309 scsi_unregister(host);
310 return 0;
314 static void adpt_inquiry(adpt_hba* pHba)
316 u32 msg[17];
317 u32 *mptr;
318 u32 *lenptr;
319 int direction;
320 int scsidir;
321 u32 len;
322 u32 reqlen;
323 u8* buf;
324 dma_addr_t addr;
325 u8 scb[16];
326 s32 rcode;
328 memset(msg, 0, sizeof(msg));
329 buf = dma_alloc_coherent(&pHba->pDev->dev, 80, &addr, GFP_KERNEL);
330 if(!buf){
331 printk(KERN_ERR"%s: Could not allocate buffer\n",pHba->name);
332 return;
334 memset((void*)buf, 0, 36);
336 len = 36;
337 direction = 0x00000000;
338 scsidir =0x40000000; // DATA IN (iop<--dev)
340 if (dpt_dma64(pHba))
341 reqlen = 17; // SINGLE SGE, 64 bit
342 else
343 reqlen = 14; // SINGLE SGE, 32 bit
344 /* Stick the headers on */
345 msg[0] = reqlen<<16 | SGL_OFFSET_12;
346 msg[1] = (0xff<<24|HOST_TID<<12|ADAPTER_TID);
347 msg[2] = 0;
348 msg[3] = 0;
349 // Adaptec/DPT Private stuff
350 msg[4] = I2O_CMD_SCSI_EXEC|DPT_ORGANIZATION_ID<<16;
351 msg[5] = ADAPTER_TID | 1<<16 /* Interpret*/;
352 /* Direction, disconnect ok | sense data | simple queue , CDBLen */
353 // I2O_SCB_FLAG_ENABLE_DISCONNECT |
354 // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
355 // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
356 msg[6] = scsidir|0x20a00000| 6 /* cmd len*/;
358 mptr=msg+7;
360 memset(scb, 0, sizeof(scb));
361 // Write SCSI command into the message - always 16 byte block
362 scb[0] = INQUIRY;
363 scb[1] = 0;
364 scb[2] = 0;
365 scb[3] = 0;
366 scb[4] = 36;
367 scb[5] = 0;
368 // Don't care about the rest of scb
370 memcpy(mptr, scb, sizeof(scb));
371 mptr+=4;
372 lenptr=mptr++; /* Remember me - fill in when we know */
374 /* Now fill in the SGList and command */
375 *lenptr = len;
376 if (dpt_dma64(pHba)) {
377 *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
378 *mptr++ = 1 << PAGE_SHIFT;
379 *mptr++ = 0xD0000000|direction|len;
380 *mptr++ = dma_low(addr);
381 *mptr++ = dma_high(addr);
382 } else {
383 *mptr++ = 0xD0000000|direction|len;
384 *mptr++ = addr;
387 // Send it on it's way
388 rcode = adpt_i2o_post_wait(pHba, msg, reqlen<<2, 120);
389 if (rcode != 0) {
390 sprintf(pHba->detail, "Adaptec I2O RAID");
391 printk(KERN_INFO "%s: Inquiry Error (%d)\n",pHba->name,rcode);
392 if (rcode != -ETIME && rcode != -EINTR)
393 dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
394 } else {
395 memset(pHba->detail, 0, sizeof(pHba->detail));
396 memcpy(&(pHba->detail), "Vendor: Adaptec ", 16);
397 memcpy(&(pHba->detail[16]), " Model: ", 8);
398 memcpy(&(pHba->detail[24]), (u8*) &buf[16], 16);
399 memcpy(&(pHba->detail[40]), " FW: ", 4);
400 memcpy(&(pHba->detail[44]), (u8*) &buf[32], 4);
401 pHba->detail[48] = '\0'; /* precautionary */
402 dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
404 adpt_i2o_status_get(pHba);
405 return ;
409 static int adpt_slave_configure(struct scsi_device * device)
411 struct Scsi_Host *host = device->host;
412 adpt_hba* pHba;
414 pHba = (adpt_hba *) host->hostdata[0];
416 if (host->can_queue && device->tagged_supported) {
417 scsi_adjust_queue_depth(device, MSG_SIMPLE_TAG,
418 host->can_queue - 1);
419 } else {
420 scsi_adjust_queue_depth(device, 0, 1);
422 return 0;
425 static int adpt_queue(struct scsi_cmnd * cmd, void (*done) (struct scsi_cmnd *))
427 adpt_hba* pHba = NULL;
428 struct adpt_device* pDev = NULL; /* dpt per device information */
430 cmd->scsi_done = done;
432 * SCSI REQUEST_SENSE commands will be executed automatically by the
433 * Host Adapter for any errors, so they should not be executed
434 * explicitly unless the Sense Data is zero indicating that no error
435 * occurred.
438 if ((cmd->cmnd[0] == REQUEST_SENSE) && (cmd->sense_buffer[0] != 0)) {
439 cmd->result = (DID_OK << 16);
440 cmd->scsi_done(cmd);
441 return 0;
444 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
445 if (!pHba) {
446 return FAILED;
449 rmb();
451 * TODO: I need to block here if I am processing ioctl cmds
452 * but if the outstanding cmds all finish before the ioctl,
453 * the scsi-core will not know to start sending cmds to me again.
454 * I need to a way to restart the scsi-cores queues or should I block
455 * calling scsi_done on the outstanding cmds instead
456 * for now we don't set the IOCTL state
458 if(((pHba->state) & DPTI_STATE_IOCTL) || ((pHba->state) & DPTI_STATE_RESET)) {
459 pHba->host->last_reset = jiffies;
460 pHba->host->resetting = 1;
461 return 1;
464 // TODO if the cmd->device if offline then I may need to issue a bus rescan
465 // followed by a get_lct to see if the device is there anymore
466 if((pDev = (struct adpt_device*) (cmd->device->hostdata)) == NULL) {
468 * First command request for this device. Set up a pointer
469 * to the device structure. This should be a TEST_UNIT_READY
470 * command from scan_scsis_single.
472 if ((pDev = adpt_find_device(pHba, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun)) == NULL) {
473 // TODO: if any luns are at this bus, scsi id then fake a TEST_UNIT_READY and INQUIRY response
474 // with type 7F (for all luns less than the max for this bus,id) so the lun scan will continue.
475 cmd->result = (DID_NO_CONNECT << 16);
476 cmd->scsi_done(cmd);
477 return 0;
479 cmd->device->hostdata = pDev;
481 pDev->pScsi_dev = cmd->device;
484 * If we are being called from when the device is being reset,
485 * delay processing of the command until later.
487 if (pDev->state & DPTI_DEV_RESET ) {
488 return FAILED;
490 return adpt_scsi_to_i2o(pHba, cmd, pDev);
493 static int adpt_bios_param(struct scsi_device *sdev, struct block_device *dev,
494 sector_t capacity, int geom[])
496 int heads=-1;
497 int sectors=-1;
498 int cylinders=-1;
500 // *** First lets set the default geometry ****
502 // If the capacity is less than ox2000
503 if (capacity < 0x2000 ) { // floppy
504 heads = 18;
505 sectors = 2;
507 // else if between 0x2000 and 0x20000
508 else if (capacity < 0x20000) {
509 heads = 64;
510 sectors = 32;
512 // else if between 0x20000 and 0x40000
513 else if (capacity < 0x40000) {
514 heads = 65;
515 sectors = 63;
517 // else if between 0x4000 and 0x80000
518 else if (capacity < 0x80000) {
519 heads = 128;
520 sectors = 63;
522 // else if greater than 0x80000
523 else {
524 heads = 255;
525 sectors = 63;
527 cylinders = sector_div(capacity, heads * sectors);
529 // Special case if CDROM
530 if(sdev->type == 5) { // CDROM
531 heads = 252;
532 sectors = 63;
533 cylinders = 1111;
536 geom[0] = heads;
537 geom[1] = sectors;
538 geom[2] = cylinders;
540 PDEBUG("adpt_bios_param: exit\n");
541 return 0;
545 static const char *adpt_info(struct Scsi_Host *host)
547 adpt_hba* pHba;
549 pHba = (adpt_hba *) host->hostdata[0];
550 return (char *) (pHba->detail);
553 static int adpt_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
554 int length, int inout)
556 struct adpt_device* d;
557 int id;
558 int chan;
559 int len = 0;
560 int begin = 0;
561 int pos = 0;
562 adpt_hba* pHba;
563 int unit;
565 *start = buffer;
566 if (inout == TRUE) {
568 * The user has done a write and wants us to take the
569 * data in the buffer and do something with it.
570 * proc_scsiwrite calls us with inout = 1
572 * Read data from buffer (writing to us) - NOT SUPPORTED
574 return -EINVAL;
578 * inout = 0 means the user has done a read and wants information
579 * returned, so we write information about the cards into the buffer
580 * proc_scsiread() calls us with inout = 0
583 // Find HBA (host bus adapter) we are looking for
584 mutex_lock(&adpt_configuration_lock);
585 for (pHba = hba_chain; pHba; pHba = pHba->next) {
586 if (pHba->host == host) {
587 break; /* found adapter */
590 mutex_unlock(&adpt_configuration_lock);
591 if (pHba == NULL) {
592 return 0;
594 host = pHba->host;
596 len = sprintf(buffer , "Adaptec I2O RAID Driver Version: %s\n\n", DPT_I2O_VERSION);
597 len += sprintf(buffer+len, "%s\n", pHba->detail);
598 len += sprintf(buffer+len, "SCSI Host=scsi%d Control Node=/dev/%s irq=%d\n",
599 pHba->host->host_no, pHba->name, host->irq);
600 len += sprintf(buffer+len, "\tpost fifo size = %d\n\treply fifo size = %d\n\tsg table size = %d\n\n",
601 host->can_queue, (int) pHba->reply_fifo_size , host->sg_tablesize);
603 pos = begin + len;
605 /* CHECKPOINT */
606 if(pos > offset + length) {
607 goto stop_output;
609 if(pos <= offset) {
611 * If we haven't even written to where we last left
612 * off (the last time we were called), reset the
613 * beginning pointer.
615 len = 0;
616 begin = pos;
618 len += sprintf(buffer+len, "Devices:\n");
619 for(chan = 0; chan < MAX_CHANNEL; chan++) {
620 for(id = 0; id < MAX_ID; id++) {
621 d = pHba->channel[chan].device[id];
622 while(d){
623 len += sprintf(buffer+len,"\t%-24.24s", d->pScsi_dev->vendor);
624 len += sprintf(buffer+len," Rev: %-8.8s\n", d->pScsi_dev->rev);
625 pos = begin + len;
628 /* CHECKPOINT */
629 if(pos > offset + length) {
630 goto stop_output;
632 if(pos <= offset) {
633 len = 0;
634 begin = pos;
637 unit = d->pI2o_dev->lct_data.tid;
638 len += sprintf(buffer+len, "\tTID=%d, (Channel=%d, Target=%d, Lun=%d) (%s)\n\n",
639 unit, (int)d->scsi_channel, (int)d->scsi_id, (int)d->scsi_lun,
640 scsi_device_online(d->pScsi_dev)? "online":"offline");
641 pos = begin + len;
643 /* CHECKPOINT */
644 if(pos > offset + length) {
645 goto stop_output;
647 if(pos <= offset) {
648 len = 0;
649 begin = pos;
652 d = d->next_lun;
658 * begin is where we last checked our position with regards to offset
659 * begin is always less than offset. len is relative to begin. It
660 * is the number of bytes written past begin
663 stop_output:
664 /* stop the output and calculate the correct length */
665 *(buffer + len) = '\0';
667 *start = buffer + (offset - begin); /* Start of wanted data */
668 len -= (offset - begin);
669 if(len > length) {
670 len = length;
671 } else if(len < 0){
672 len = 0;
673 **start = '\0';
675 return len;
679 * Turn a struct scsi_cmnd * into a unique 32 bit 'context'.
681 static u32 adpt_cmd_to_context(struct scsi_cmnd *cmd)
683 return (u32)cmd->serial_number;
687 * Go from a u32 'context' to a struct scsi_cmnd * .
688 * This could probably be made more efficient.
690 static struct scsi_cmnd *
691 adpt_cmd_from_context(adpt_hba * pHba, u32 context)
693 struct scsi_cmnd * cmd;
694 struct scsi_device * d;
696 if (context == 0)
697 return NULL;
699 spin_unlock(pHba->host->host_lock);
700 shost_for_each_device(d, pHba->host) {
701 unsigned long flags;
702 spin_lock_irqsave(&d->list_lock, flags);
703 list_for_each_entry(cmd, &d->cmd_list, list) {
704 if (((u32)cmd->serial_number == context)) {
705 spin_unlock_irqrestore(&d->list_lock, flags);
706 scsi_device_put(d);
707 spin_lock(pHba->host->host_lock);
708 return cmd;
711 spin_unlock_irqrestore(&d->list_lock, flags);
713 spin_lock(pHba->host->host_lock);
715 return NULL;
719 * Turn a pointer to ioctl reply data into an u32 'context'
721 static u32 adpt_ioctl_to_context(adpt_hba * pHba, void *reply)
723 #if BITS_PER_LONG == 32
724 return (u32)(unsigned long)reply;
725 #else
726 ulong flags = 0;
727 u32 nr, i;
729 spin_lock_irqsave(pHba->host->host_lock, flags);
730 nr = ARRAY_SIZE(pHba->ioctl_reply_context);
731 for (i = 0; i < nr; i++) {
732 if (pHba->ioctl_reply_context[i] == NULL) {
733 pHba->ioctl_reply_context[i] = reply;
734 break;
737 spin_unlock_irqrestore(pHba->host->host_lock, flags);
738 if (i >= nr) {
739 kfree (reply);
740 printk(KERN_WARNING"%s: Too many outstanding "
741 "ioctl commands\n", pHba->name);
742 return (u32)-1;
745 return i;
746 #endif
750 * Go from an u32 'context' to a pointer to ioctl reply data.
752 static void *adpt_ioctl_from_context(adpt_hba *pHba, u32 context)
754 #if BITS_PER_LONG == 32
755 return (void *)(unsigned long)context;
756 #else
757 void *p = pHba->ioctl_reply_context[context];
758 pHba->ioctl_reply_context[context] = NULL;
760 return p;
761 #endif
764 /*===========================================================================
765 * Error Handling routines
766 *===========================================================================
769 static int adpt_abort(struct scsi_cmnd * cmd)
771 adpt_hba* pHba = NULL; /* host bus adapter structure */
772 struct adpt_device* dptdevice; /* dpt per device information */
773 u32 msg[5];
774 int rcode;
776 if(cmd->serial_number == 0){
777 return FAILED;
779 pHba = (adpt_hba*) cmd->device->host->hostdata[0];
780 printk(KERN_INFO"%s: Trying to Abort cmd=%ld\n",pHba->name, cmd->serial_number);
781 if ((dptdevice = (void*) (cmd->device->hostdata)) == NULL) {
782 printk(KERN_ERR "%s: Unable to abort: No device in cmnd\n",pHba->name);
783 return FAILED;
786 memset(msg, 0, sizeof(msg));
787 msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
788 msg[1] = I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|dptdevice->tid;
789 msg[2] = 0;
790 msg[3]= 0;
791 msg[4] = adpt_cmd_to_context(cmd);
792 if (pHba->host)
793 spin_lock_irq(pHba->host->host_lock);
794 rcode = adpt_i2o_post_wait(pHba, msg, sizeof(msg), FOREVER);
795 if (pHba->host)
796 spin_unlock_irq(pHba->host->host_lock);
797 if (rcode != 0) {
798 if(rcode == -EOPNOTSUPP ){
799 printk(KERN_INFO"%s: Abort cmd not supported\n",pHba->name);
800 return FAILED;
802 printk(KERN_INFO"%s: Abort cmd=%ld failed.\n",pHba->name, cmd->serial_number);
803 return FAILED;
805 printk(KERN_INFO"%s: Abort cmd=%ld complete.\n",pHba->name, cmd->serial_number);
806 return SUCCESS;
810 #define I2O_DEVICE_RESET 0x27
811 // This is the same for BLK and SCSI devices
812 // NOTE this is wrong in the i2o.h definitions
813 // This is not currently supported by our adapter but we issue it anyway
814 static int adpt_device_reset(struct scsi_cmnd* cmd)
816 adpt_hba* pHba;
817 u32 msg[4];
818 u32 rcode;
819 int old_state;
820 struct adpt_device* d = cmd->device->hostdata;
822 pHba = (void*) cmd->device->host->hostdata[0];
823 printk(KERN_INFO"%s: Trying to reset device\n",pHba->name);
824 if (!d) {
825 printk(KERN_INFO"%s: Reset Device: Device Not found\n",pHba->name);
826 return FAILED;
828 memset(msg, 0, sizeof(msg));
829 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
830 msg[1] = (I2O_DEVICE_RESET<<24|HOST_TID<<12|d->tid);
831 msg[2] = 0;
832 msg[3] = 0;
834 if (pHba->host)
835 spin_lock_irq(pHba->host->host_lock);
836 old_state = d->state;
837 d->state |= DPTI_DEV_RESET;
838 rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
839 d->state = old_state;
840 if (pHba->host)
841 spin_unlock_irq(pHba->host->host_lock);
842 if (rcode != 0) {
843 if(rcode == -EOPNOTSUPP ){
844 printk(KERN_INFO"%s: Device reset not supported\n",pHba->name);
845 return FAILED;
847 printk(KERN_INFO"%s: Device reset failed\n",pHba->name);
848 return FAILED;
849 } else {
850 printk(KERN_INFO"%s: Device reset successful\n",pHba->name);
851 return SUCCESS;
856 #define I2O_HBA_BUS_RESET 0x87
857 // This version of bus reset is called by the eh_error handler
858 static int adpt_bus_reset(struct scsi_cmnd* cmd)
860 adpt_hba* pHba;
861 u32 msg[4];
862 u32 rcode;
864 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
865 memset(msg, 0, sizeof(msg));
866 printk(KERN_WARNING"%s: Bus reset: SCSI Bus %d: tid: %d\n",pHba->name, cmd->device->channel,pHba->channel[cmd->device->channel].tid );
867 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
868 msg[1] = (I2O_HBA_BUS_RESET<<24|HOST_TID<<12|pHba->channel[cmd->device->channel].tid);
869 msg[2] = 0;
870 msg[3] = 0;
871 if (pHba->host)
872 spin_lock_irq(pHba->host->host_lock);
873 rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
874 if (pHba->host)
875 spin_unlock_irq(pHba->host->host_lock);
876 if (rcode != 0) {
877 printk(KERN_WARNING"%s: Bus reset failed.\n",pHba->name);
878 return FAILED;
879 } else {
880 printk(KERN_WARNING"%s: Bus reset success.\n",pHba->name);
881 return SUCCESS;
885 // This version of reset is called by the eh_error_handler
886 static int __adpt_reset(struct scsi_cmnd* cmd)
888 adpt_hba* pHba;
889 int rcode;
890 pHba = (adpt_hba*)cmd->device->host->hostdata[0];
891 printk(KERN_WARNING"%s: Hba Reset: scsi id %d: tid: %d\n",pHba->name,cmd->device->channel,pHba->channel[cmd->device->channel].tid );
892 rcode = adpt_hba_reset(pHba);
893 if(rcode == 0){
894 printk(KERN_WARNING"%s: HBA reset complete\n",pHba->name);
895 return SUCCESS;
896 } else {
897 printk(KERN_WARNING"%s: HBA reset failed (%x)\n",pHba->name, rcode);
898 return FAILED;
902 static int adpt_reset(struct scsi_cmnd* cmd)
904 int rc;
906 spin_lock_irq(cmd->device->host->host_lock);
907 rc = __adpt_reset(cmd);
908 spin_unlock_irq(cmd->device->host->host_lock);
910 return rc;
913 // This version of reset is called by the ioctls and indirectly from eh_error_handler via adpt_reset
914 static int adpt_hba_reset(adpt_hba* pHba)
916 int rcode;
918 pHba->state |= DPTI_STATE_RESET;
920 // Activate does get status , init outbound, and get hrt
921 if ((rcode=adpt_i2o_activate_hba(pHba)) < 0) {
922 printk(KERN_ERR "%s: Could not activate\n", pHba->name);
923 adpt_i2o_delete_hba(pHba);
924 return rcode;
927 if ((rcode=adpt_i2o_build_sys_table()) < 0) {
928 adpt_i2o_delete_hba(pHba);
929 return rcode;
931 PDEBUG("%s: in HOLD state\n",pHba->name);
933 if ((rcode=adpt_i2o_online_hba(pHba)) < 0) {
934 adpt_i2o_delete_hba(pHba);
935 return rcode;
937 PDEBUG("%s: in OPERATIONAL state\n",pHba->name);
939 if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
940 adpt_i2o_delete_hba(pHba);
941 return rcode;
944 if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
945 adpt_i2o_delete_hba(pHba);
946 return rcode;
948 pHba->state &= ~DPTI_STATE_RESET;
950 adpt_fail_posted_scbs(pHba);
951 return 0; /* return success */
954 /*===========================================================================
956 *===========================================================================
960 static void adpt_i2o_sys_shutdown(void)
962 adpt_hba *pHba, *pNext;
963 struct adpt_i2o_post_wait_data *p1, *old;
965 printk(KERN_INFO"Shutting down Adaptec I2O controllers.\n");
966 printk(KERN_INFO" This could take a few minutes if there are many devices attached\n");
967 /* Delete all IOPs from the controller chain */
968 /* They should have already been released by the
969 * scsi-core
971 for (pHba = hba_chain; pHba; pHba = pNext) {
972 pNext = pHba->next;
973 adpt_i2o_delete_hba(pHba);
976 /* Remove any timedout entries from the wait queue. */
977 // spin_lock_irqsave(&adpt_post_wait_lock, flags);
978 /* Nothing should be outstanding at this point so just
979 * free them
981 for(p1 = adpt_post_wait_queue; p1;) {
982 old = p1;
983 p1 = p1->next;
984 kfree(old);
986 // spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
987 adpt_post_wait_queue = NULL;
989 printk(KERN_INFO "Adaptec I2O controllers down.\n");
992 static int adpt_install_hba(struct scsi_host_template* sht, struct pci_dev* pDev)
995 adpt_hba* pHba = NULL;
996 adpt_hba* p = NULL;
997 ulong base_addr0_phys = 0;
998 ulong base_addr1_phys = 0;
999 u32 hba_map0_area_size = 0;
1000 u32 hba_map1_area_size = 0;
1001 void __iomem *base_addr_virt = NULL;
1002 void __iomem *msg_addr_virt = NULL;
1003 int dma64 = 0;
1005 int raptorFlag = FALSE;
1007 if(pci_enable_device(pDev)) {
1008 return -EINVAL;
1011 if (pci_request_regions(pDev, "dpt_i2o")) {
1012 PERROR("dpti: adpt_config_hba: pci request region failed\n");
1013 return -EINVAL;
1016 pci_set_master(pDev);
1019 * See if we should enable dma64 mode.
1021 if (sizeof(dma_addr_t) > 4 &&
1022 pci_set_dma_mask(pDev, DMA_BIT_MASK(64)) == 0) {
1023 if (dma_get_required_mask(&pDev->dev) > DMA_BIT_MASK(32))
1024 dma64 = 1;
1026 if (!dma64 && pci_set_dma_mask(pDev, DMA_BIT_MASK(32)) != 0)
1027 return -EINVAL;
1029 /* adapter only supports message blocks below 4GB */
1030 pci_set_consistent_dma_mask(pDev, DMA_BIT_MASK(32));
1032 base_addr0_phys = pci_resource_start(pDev,0);
1033 hba_map0_area_size = pci_resource_len(pDev,0);
1035 // Check if standard PCI card or single BAR Raptor
1036 if(pDev->device == PCI_DPT_DEVICE_ID){
1037 if(pDev->subsystem_device >=0xc032 && pDev->subsystem_device <= 0xc03b){
1038 // Raptor card with this device id needs 4M
1039 hba_map0_area_size = 0x400000;
1040 } else { // Not Raptor - it is a PCI card
1041 if(hba_map0_area_size > 0x100000 ){
1042 hba_map0_area_size = 0x100000;
1045 } else {// Raptor split BAR config
1046 // Use BAR1 in this configuration
1047 base_addr1_phys = pci_resource_start(pDev,1);
1048 hba_map1_area_size = pci_resource_len(pDev,1);
1049 raptorFlag = TRUE;
1052 #if BITS_PER_LONG == 64
1054 * The original Adaptec 64 bit driver has this comment here:
1055 * "x86_64 machines need more optimal mappings"
1057 * I assume some HBAs report ridiculously large mappings
1058 * and we need to limit them on platforms with IOMMUs.
1060 if (raptorFlag == TRUE) {
1061 if (hba_map0_area_size > 128)
1062 hba_map0_area_size = 128;
1063 if (hba_map1_area_size > 524288)
1064 hba_map1_area_size = 524288;
1065 } else {
1066 if (hba_map0_area_size > 524288)
1067 hba_map0_area_size = 524288;
1069 #endif
1071 base_addr_virt = ioremap(base_addr0_phys,hba_map0_area_size);
1072 if (!base_addr_virt) {
1073 pci_release_regions(pDev);
1074 PERROR("dpti: adpt_config_hba: io remap failed\n");
1075 return -EINVAL;
1078 if(raptorFlag == TRUE) {
1079 msg_addr_virt = ioremap(base_addr1_phys, hba_map1_area_size );
1080 if (!msg_addr_virt) {
1081 PERROR("dpti: adpt_config_hba: io remap failed on BAR1\n");
1082 iounmap(base_addr_virt);
1083 pci_release_regions(pDev);
1084 return -EINVAL;
1086 } else {
1087 msg_addr_virt = base_addr_virt;
1090 // Allocate and zero the data structure
1091 pHba = kzalloc(sizeof(adpt_hba), GFP_KERNEL);
1092 if (!pHba) {
1093 if (msg_addr_virt != base_addr_virt)
1094 iounmap(msg_addr_virt);
1095 iounmap(base_addr_virt);
1096 pci_release_regions(pDev);
1097 return -ENOMEM;
1100 mutex_lock(&adpt_configuration_lock);
1102 if(hba_chain != NULL){
1103 for(p = hba_chain; p->next; p = p->next);
1104 p->next = pHba;
1105 } else {
1106 hba_chain = pHba;
1108 pHba->next = NULL;
1109 pHba->unit = hba_count;
1110 sprintf(pHba->name, "dpti%d", hba_count);
1111 hba_count++;
1113 mutex_unlock(&adpt_configuration_lock);
1115 pHba->pDev = pDev;
1116 pHba->base_addr_phys = base_addr0_phys;
1118 // Set up the Virtual Base Address of the I2O Device
1119 pHba->base_addr_virt = base_addr_virt;
1120 pHba->msg_addr_virt = msg_addr_virt;
1121 pHba->irq_mask = base_addr_virt+0x30;
1122 pHba->post_port = base_addr_virt+0x40;
1123 pHba->reply_port = base_addr_virt+0x44;
1125 pHba->hrt = NULL;
1126 pHba->lct = NULL;
1127 pHba->lct_size = 0;
1128 pHba->status_block = NULL;
1129 pHba->post_count = 0;
1130 pHba->state = DPTI_STATE_RESET;
1131 pHba->pDev = pDev;
1132 pHba->devices = NULL;
1133 pHba->dma64 = dma64;
1135 // Initializing the spinlocks
1136 spin_lock_init(&pHba->state_lock);
1137 spin_lock_init(&adpt_post_wait_lock);
1139 if(raptorFlag == 0){
1140 printk(KERN_INFO "Adaptec I2O RAID controller"
1141 " %d at %p size=%x irq=%d%s\n",
1142 hba_count-1, base_addr_virt,
1143 hba_map0_area_size, pDev->irq,
1144 dma64 ? " (64-bit DMA)" : "");
1145 } else {
1146 printk(KERN_INFO"Adaptec I2O RAID controller %d irq=%d%s\n",
1147 hba_count-1, pDev->irq,
1148 dma64 ? " (64-bit DMA)" : "");
1149 printk(KERN_INFO" BAR0 %p - size= %x\n",base_addr_virt,hba_map0_area_size);
1150 printk(KERN_INFO" BAR1 %p - size= %x\n",msg_addr_virt,hba_map1_area_size);
1153 if (request_irq (pDev->irq, adpt_isr, IRQF_SHARED, pHba->name, pHba)) {
1154 printk(KERN_ERR"%s: Couldn't register IRQ %d\n", pHba->name, pDev->irq);
1155 adpt_i2o_delete_hba(pHba);
1156 return -EINVAL;
1159 return 0;
1163 static void adpt_i2o_delete_hba(adpt_hba* pHba)
1165 adpt_hba* p1;
1166 adpt_hba* p2;
1167 struct i2o_device* d;
1168 struct i2o_device* next;
1169 int i;
1170 int j;
1171 struct adpt_device* pDev;
1172 struct adpt_device* pNext;
1175 mutex_lock(&adpt_configuration_lock);
1176 // scsi_unregister calls our adpt_release which
1177 // does a quiese
1178 if(pHba->host){
1179 free_irq(pHba->host->irq, pHba);
1181 p2 = NULL;
1182 for( p1 = hba_chain; p1; p2 = p1,p1=p1->next){
1183 if(p1 == pHba) {
1184 if(p2) {
1185 p2->next = p1->next;
1186 } else {
1187 hba_chain = p1->next;
1189 break;
1193 hba_count--;
1194 mutex_unlock(&adpt_configuration_lock);
1196 iounmap(pHba->base_addr_virt);
1197 pci_release_regions(pHba->pDev);
1198 if(pHba->msg_addr_virt != pHba->base_addr_virt){
1199 iounmap(pHba->msg_addr_virt);
1201 if(pHba->FwDebugBuffer_P)
1202 iounmap(pHba->FwDebugBuffer_P);
1203 if(pHba->hrt) {
1204 dma_free_coherent(&pHba->pDev->dev,
1205 pHba->hrt->num_entries * pHba->hrt->entry_len << 2,
1206 pHba->hrt, pHba->hrt_pa);
1208 if(pHba->lct) {
1209 dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
1210 pHba->lct, pHba->lct_pa);
1212 if(pHba->status_block) {
1213 dma_free_coherent(&pHba->pDev->dev, sizeof(i2o_status_block),
1214 pHba->status_block, pHba->status_block_pa);
1216 if(pHba->reply_pool) {
1217 dma_free_coherent(&pHba->pDev->dev,
1218 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
1219 pHba->reply_pool, pHba->reply_pool_pa);
1222 for(d = pHba->devices; d ; d = next){
1223 next = d->next;
1224 kfree(d);
1226 for(i = 0 ; i < pHba->top_scsi_channel ; i++){
1227 for(j = 0; j < MAX_ID; j++){
1228 if(pHba->channel[i].device[j] != NULL){
1229 for(pDev = pHba->channel[i].device[j]; pDev; pDev = pNext){
1230 pNext = pDev->next_lun;
1231 kfree(pDev);
1236 pci_dev_put(pHba->pDev);
1237 if (adpt_sysfs_class)
1238 device_destroy(adpt_sysfs_class,
1239 MKDEV(DPTI_I2O_MAJOR, pHba->unit));
1240 kfree(pHba);
1242 if(hba_count <= 0){
1243 unregister_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER);
1244 if (adpt_sysfs_class) {
1245 class_destroy(adpt_sysfs_class);
1246 adpt_sysfs_class = NULL;
1251 static struct adpt_device* adpt_find_device(adpt_hba* pHba, u32 chan, u32 id, u32 lun)
1253 struct adpt_device* d;
1255 if(chan < 0 || chan >= MAX_CHANNEL)
1256 return NULL;
1258 if( pHba->channel[chan].device == NULL){
1259 printk(KERN_DEBUG"Adaptec I2O RAID: Trying to find device before they are allocated\n");
1260 return NULL;
1263 d = pHba->channel[chan].device[id];
1264 if(!d || d->tid == 0) {
1265 return NULL;
1268 /* If it is the only lun at that address then this should match*/
1269 if(d->scsi_lun == lun){
1270 return d;
1273 /* else we need to look through all the luns */
1274 for(d=d->next_lun ; d ; d = d->next_lun){
1275 if(d->scsi_lun == lun){
1276 return d;
1279 return NULL;
1283 static int adpt_i2o_post_wait(adpt_hba* pHba, u32* msg, int len, int timeout)
1285 // I used my own version of the WAIT_QUEUE_HEAD
1286 // to handle some version differences
1287 // When embedded in the kernel this could go back to the vanilla one
1288 ADPT_DECLARE_WAIT_QUEUE_HEAD(adpt_wq_i2o_post);
1289 int status = 0;
1290 ulong flags = 0;
1291 struct adpt_i2o_post_wait_data *p1, *p2;
1292 struct adpt_i2o_post_wait_data *wait_data =
1293 kmalloc(sizeof(struct adpt_i2o_post_wait_data),GFP_KERNEL);
1294 DECLARE_WAITQUEUE(wait, current);
1296 if (!wait_data)
1297 return -ENOMEM;
1300 * The spin locking is needed to keep anyone from playing
1301 * with the queue pointers and id while we do the same
1303 spin_lock_irqsave(&adpt_post_wait_lock, flags);
1304 // TODO we need a MORE unique way of getting ids
1305 // to support async LCT get
1306 wait_data->next = adpt_post_wait_queue;
1307 adpt_post_wait_queue = wait_data;
1308 adpt_post_wait_id++;
1309 adpt_post_wait_id &= 0x7fff;
1310 wait_data->id = adpt_post_wait_id;
1311 spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1313 wait_data->wq = &adpt_wq_i2o_post;
1314 wait_data->status = -ETIMEDOUT;
1316 add_wait_queue(&adpt_wq_i2o_post, &wait);
1318 msg[2] |= 0x80000000 | ((u32)wait_data->id);
1319 timeout *= HZ;
1320 if((status = adpt_i2o_post_this(pHba, msg, len)) == 0){
1321 set_current_state(TASK_INTERRUPTIBLE);
1322 if(pHba->host)
1323 spin_unlock_irq(pHba->host->host_lock);
1324 if (!timeout)
1325 schedule();
1326 else{
1327 timeout = schedule_timeout(timeout);
1328 if (timeout == 0) {
1329 // I/O issued, but cannot get result in
1330 // specified time. Freeing resorces is
1331 // dangerous.
1332 status = -ETIME;
1335 if(pHba->host)
1336 spin_lock_irq(pHba->host->host_lock);
1338 remove_wait_queue(&adpt_wq_i2o_post, &wait);
1340 if(status == -ETIMEDOUT){
1341 printk(KERN_INFO"dpti%d: POST WAIT TIMEOUT\n",pHba->unit);
1342 // We will have to free the wait_data memory during shutdown
1343 return status;
1346 /* Remove the entry from the queue. */
1347 p2 = NULL;
1348 spin_lock_irqsave(&adpt_post_wait_lock, flags);
1349 for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p1->next) {
1350 if(p1 == wait_data) {
1351 if(p1->status == I2O_DETAIL_STATUS_UNSUPPORTED_FUNCTION ) {
1352 status = -EOPNOTSUPP;
1354 if(p2) {
1355 p2->next = p1->next;
1356 } else {
1357 adpt_post_wait_queue = p1->next;
1359 break;
1362 spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1364 kfree(wait_data);
1366 return status;
1370 static s32 adpt_i2o_post_this(adpt_hba* pHba, u32* data, int len)
1373 u32 m = EMPTY_QUEUE;
1374 u32 __iomem *msg;
1375 ulong timeout = jiffies + 30*HZ;
1376 do {
1377 rmb();
1378 m = readl(pHba->post_port);
1379 if (m != EMPTY_QUEUE) {
1380 break;
1382 if(time_after(jiffies,timeout)){
1383 printk(KERN_WARNING"dpti%d: Timeout waiting for message frame!\n", pHba->unit);
1384 return -ETIMEDOUT;
1386 schedule_timeout_uninterruptible(1);
1387 } while(m == EMPTY_QUEUE);
1389 msg = pHba->msg_addr_virt + m;
1390 memcpy_toio(msg, data, len);
1391 wmb();
1393 //post message
1394 writel(m, pHba->post_port);
1395 wmb();
1397 return 0;
1401 static void adpt_i2o_post_wait_complete(u32 context, int status)
1403 struct adpt_i2o_post_wait_data *p1 = NULL;
1405 * We need to search through the adpt_post_wait
1406 * queue to see if the given message is still
1407 * outstanding. If not, it means that the IOP
1408 * took longer to respond to the message than we
1409 * had allowed and timer has already expired.
1410 * Not much we can do about that except log
1411 * it for debug purposes, increase timeout, and recompile
1413 * Lock needed to keep anyone from moving queue pointers
1414 * around while we're looking through them.
1417 context &= 0x7fff;
1419 spin_lock(&adpt_post_wait_lock);
1420 for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1421 if(p1->id == context) {
1422 p1->status = status;
1423 spin_unlock(&adpt_post_wait_lock);
1424 wake_up_interruptible(p1->wq);
1425 return;
1428 spin_unlock(&adpt_post_wait_lock);
1429 // If this happens we lose commands that probably really completed
1430 printk(KERN_DEBUG"dpti: Could Not find task %d in wait queue\n",context);
1431 printk(KERN_DEBUG" Tasks in wait queue:\n");
1432 for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1433 printk(KERN_DEBUG" %d\n",p1->id);
1435 return;
1438 static s32 adpt_i2o_reset_hba(adpt_hba* pHba)
1440 u32 msg[8];
1441 u8* status;
1442 dma_addr_t addr;
1443 u32 m = EMPTY_QUEUE ;
1444 ulong timeout = jiffies + (TMOUT_IOPRESET*HZ);
1446 if(pHba->initialized == FALSE) { // First time reset should be quick
1447 timeout = jiffies + (25*HZ);
1448 } else {
1449 adpt_i2o_quiesce_hba(pHba);
1452 do {
1453 rmb();
1454 m = readl(pHba->post_port);
1455 if (m != EMPTY_QUEUE) {
1456 break;
1458 if(time_after(jiffies,timeout)){
1459 printk(KERN_WARNING"Timeout waiting for message!\n");
1460 return -ETIMEDOUT;
1462 schedule_timeout_uninterruptible(1);
1463 } while (m == EMPTY_QUEUE);
1465 status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
1466 if(status == NULL) {
1467 adpt_send_nop(pHba, m);
1468 printk(KERN_ERR"IOP reset failed - no free memory.\n");
1469 return -ENOMEM;
1471 memset(status,0,4);
1473 msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
1474 msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
1475 msg[2]=0;
1476 msg[3]=0;
1477 msg[4]=0;
1478 msg[5]=0;
1479 msg[6]=dma_low(addr);
1480 msg[7]=dma_high(addr);
1482 memcpy_toio(pHba->msg_addr_virt+m, msg, sizeof(msg));
1483 wmb();
1484 writel(m, pHba->post_port);
1485 wmb();
1487 while(*status == 0){
1488 if(time_after(jiffies,timeout)){
1489 printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
1490 /* We lose 4 bytes of "status" here, but we cannot
1491 free these because controller may awake and corrupt
1492 those bytes at any time */
1493 /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
1494 return -ETIMEDOUT;
1496 rmb();
1497 schedule_timeout_uninterruptible(1);
1500 if(*status == 0x01 /*I2O_EXEC_IOP_RESET_IN_PROGRESS*/) {
1501 PDEBUG("%s: Reset in progress...\n", pHba->name);
1502 // Here we wait for message frame to become available
1503 // indicated that reset has finished
1504 do {
1505 rmb();
1506 m = readl(pHba->post_port);
1507 if (m != EMPTY_QUEUE) {
1508 break;
1510 if(time_after(jiffies,timeout)){
1511 printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
1512 /* We lose 4 bytes of "status" here, but we
1513 cannot free these because controller may
1514 awake and corrupt those bytes at any time */
1515 /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
1516 return -ETIMEDOUT;
1518 schedule_timeout_uninterruptible(1);
1519 } while (m == EMPTY_QUEUE);
1520 // Flush the offset
1521 adpt_send_nop(pHba, m);
1523 adpt_i2o_status_get(pHba);
1524 if(*status == 0x02 ||
1525 pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
1526 printk(KERN_WARNING"%s: Reset reject, trying to clear\n",
1527 pHba->name);
1528 } else {
1529 PDEBUG("%s: Reset completed.\n", pHba->name);
1532 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
1533 #ifdef UARTDELAY
1534 // This delay is to allow someone attached to the card through the debug UART to
1535 // set up the dump levels that they want before the rest of the initialization sequence
1536 adpt_delay(20000);
1537 #endif
1538 return 0;
1542 static int adpt_i2o_parse_lct(adpt_hba* pHba)
1544 int i;
1545 int max;
1546 int tid;
1547 struct i2o_device *d;
1548 i2o_lct *lct = pHba->lct;
1549 u8 bus_no = 0;
1550 s16 scsi_id;
1551 s16 scsi_lun;
1552 u32 buf[10]; // larger than 7, or 8 ...
1553 struct adpt_device* pDev;
1555 if (lct == NULL) {
1556 printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
1557 return -1;
1560 max = lct->table_size;
1561 max -= 3;
1562 max /= 9;
1564 for(i=0;i<max;i++) {
1565 if( lct->lct_entry[i].user_tid != 0xfff){
1567 * If we have hidden devices, we need to inform the upper layers about
1568 * the possible maximum id reference to handle device access when
1569 * an array is disassembled. This code has no other purpose but to
1570 * allow us future access to devices that are currently hidden
1571 * behind arrays, hotspares or have not been configured (JBOD mode).
1573 if( lct->lct_entry[i].class_id != I2O_CLASS_RANDOM_BLOCK_STORAGE &&
1574 lct->lct_entry[i].class_id != I2O_CLASS_SCSI_PERIPHERAL &&
1575 lct->lct_entry[i].class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1576 continue;
1578 tid = lct->lct_entry[i].tid;
1579 // I2O_DPT_DEVICE_INFO_GROUP_NO;
1580 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
1581 continue;
1583 bus_no = buf[0]>>16;
1584 scsi_id = buf[1];
1585 scsi_lun = (buf[2]>>8 )&0xff;
1586 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1587 printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
1588 continue;
1590 if (scsi_id >= MAX_ID){
1591 printk(KERN_WARNING"%s: SCSI ID %d out of range \n", pHba->name, bus_no);
1592 continue;
1594 if(bus_no > pHba->top_scsi_channel){
1595 pHba->top_scsi_channel = bus_no;
1597 if(scsi_id > pHba->top_scsi_id){
1598 pHba->top_scsi_id = scsi_id;
1600 if(scsi_lun > pHba->top_scsi_lun){
1601 pHba->top_scsi_lun = scsi_lun;
1603 continue;
1605 d = kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
1606 if(d==NULL)
1608 printk(KERN_CRIT"%s: Out of memory for I2O device data.\n",pHba->name);
1609 return -ENOMEM;
1612 d->controller = pHba;
1613 d->next = NULL;
1615 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
1617 d->flags = 0;
1618 tid = d->lct_data.tid;
1619 adpt_i2o_report_hba_unit(pHba, d);
1620 adpt_i2o_install_device(pHba, d);
1622 bus_no = 0;
1623 for(d = pHba->devices; d ; d = d->next) {
1624 if(d->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT ||
1625 d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PORT){
1626 tid = d->lct_data.tid;
1627 // TODO get the bus_no from hrt-but for now they are in order
1628 //bus_no =
1629 if(bus_no > pHba->top_scsi_channel){
1630 pHba->top_scsi_channel = bus_no;
1632 pHba->channel[bus_no].type = d->lct_data.class_id;
1633 pHba->channel[bus_no].tid = tid;
1634 if(adpt_i2o_query_scalar(pHba, tid, 0x0200, -1, buf, 28)>=0)
1636 pHba->channel[bus_no].scsi_id = buf[1];
1637 PDEBUG("Bus %d - SCSI ID %d.\n", bus_no, buf[1]);
1639 // TODO remove - this is just until we get from hrt
1640 bus_no++;
1641 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1642 printk(KERN_WARNING"%s: Channel number %d out of range - LCT\n", pHba->name, bus_no);
1643 break;
1648 // Setup adpt_device table
1649 for(d = pHba->devices; d ; d = d->next) {
1650 if(d->lct_data.class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
1651 d->lct_data.class_id == I2O_CLASS_SCSI_PERIPHERAL ||
1652 d->lct_data.class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1654 tid = d->lct_data.tid;
1655 scsi_id = -1;
1656 // I2O_DPT_DEVICE_INFO_GROUP_NO;
1657 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)>=0) {
1658 bus_no = buf[0]>>16;
1659 scsi_id = buf[1];
1660 scsi_lun = (buf[2]>>8 )&0xff;
1661 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
1662 continue;
1664 if (scsi_id >= MAX_ID) {
1665 continue;
1667 if( pHba->channel[bus_no].device[scsi_id] == NULL){
1668 pDev = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
1669 if(pDev == NULL) {
1670 return -ENOMEM;
1672 pHba->channel[bus_no].device[scsi_id] = pDev;
1673 } else {
1674 for( pDev = pHba->channel[bus_no].device[scsi_id];
1675 pDev->next_lun; pDev = pDev->next_lun){
1677 pDev->next_lun = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
1678 if(pDev->next_lun == NULL) {
1679 return -ENOMEM;
1681 pDev = pDev->next_lun;
1683 pDev->tid = tid;
1684 pDev->scsi_channel = bus_no;
1685 pDev->scsi_id = scsi_id;
1686 pDev->scsi_lun = scsi_lun;
1687 pDev->pI2o_dev = d;
1688 d->owner = pDev;
1689 pDev->type = (buf[0])&0xff;
1690 pDev->flags = (buf[0]>>8)&0xff;
1691 if(scsi_id > pHba->top_scsi_id){
1692 pHba->top_scsi_id = scsi_id;
1694 if(scsi_lun > pHba->top_scsi_lun){
1695 pHba->top_scsi_lun = scsi_lun;
1698 if(scsi_id == -1){
1699 printk(KERN_WARNING"Could not find SCSI ID for %s\n",
1700 d->lct_data.identity_tag);
1704 return 0;
1709 * Each I2O controller has a chain of devices on it - these match
1710 * the useful parts of the LCT of the board.
1713 static int adpt_i2o_install_device(adpt_hba* pHba, struct i2o_device *d)
1715 mutex_lock(&adpt_configuration_lock);
1716 d->controller=pHba;
1717 d->owner=NULL;
1718 d->next=pHba->devices;
1719 d->prev=NULL;
1720 if (pHba->devices != NULL){
1721 pHba->devices->prev=d;
1723 pHba->devices=d;
1724 *d->dev_name = 0;
1726 mutex_unlock(&adpt_configuration_lock);
1727 return 0;
1730 static int adpt_open(struct inode *inode, struct file *file)
1732 int minor;
1733 adpt_hba* pHba;
1735 lock_kernel();
1736 //TODO check for root access
1738 minor = iminor(inode);
1739 if (minor >= hba_count) {
1740 unlock_kernel();
1741 return -ENXIO;
1743 mutex_lock(&adpt_configuration_lock);
1744 for (pHba = hba_chain; pHba; pHba = pHba->next) {
1745 if (pHba->unit == minor) {
1746 break; /* found adapter */
1749 if (pHba == NULL) {
1750 mutex_unlock(&adpt_configuration_lock);
1751 unlock_kernel();
1752 return -ENXIO;
1755 // if(pHba->in_use){
1756 // mutex_unlock(&adpt_configuration_lock);
1757 // return -EBUSY;
1758 // }
1760 pHba->in_use = 1;
1761 mutex_unlock(&adpt_configuration_lock);
1762 unlock_kernel();
1764 return 0;
1767 static int adpt_close(struct inode *inode, struct file *file)
1769 int minor;
1770 adpt_hba* pHba;
1772 minor = iminor(inode);
1773 if (minor >= hba_count) {
1774 return -ENXIO;
1776 mutex_lock(&adpt_configuration_lock);
1777 for (pHba = hba_chain; pHba; pHba = pHba->next) {
1778 if (pHba->unit == minor) {
1779 break; /* found adapter */
1782 mutex_unlock(&adpt_configuration_lock);
1783 if (pHba == NULL) {
1784 return -ENXIO;
1787 pHba->in_use = 0;
1789 return 0;
1793 static int adpt_i2o_passthru(adpt_hba* pHba, u32 __user *arg)
1795 u32 msg[MAX_MESSAGE_SIZE];
1796 u32* reply = NULL;
1797 u32 size = 0;
1798 u32 reply_size = 0;
1799 u32 __user *user_msg = arg;
1800 u32 __user * user_reply = NULL;
1801 void *sg_list[pHba->sg_tablesize];
1802 u32 sg_offset = 0;
1803 u32 sg_count = 0;
1804 int sg_index = 0;
1805 u32 i = 0;
1806 u32 rcode = 0;
1807 void *p = NULL;
1808 dma_addr_t addr;
1809 ulong flags = 0;
1811 memset(&msg, 0, MAX_MESSAGE_SIZE*4);
1812 // get user msg size in u32s
1813 if(get_user(size, &user_msg[0])){
1814 return -EFAULT;
1816 size = size>>16;
1818 user_reply = &user_msg[size];
1819 if(size > MAX_MESSAGE_SIZE){
1820 return -EFAULT;
1822 size *= 4; // Convert to bytes
1824 /* Copy in the user's I2O command */
1825 if(copy_from_user(msg, user_msg, size)) {
1826 return -EFAULT;
1828 get_user(reply_size, &user_reply[0]);
1829 reply_size = reply_size>>16;
1830 if(reply_size > REPLY_FRAME_SIZE){
1831 reply_size = REPLY_FRAME_SIZE;
1833 reply_size *= 4;
1834 reply = kzalloc(REPLY_FRAME_SIZE*4, GFP_KERNEL);
1835 if(reply == NULL) {
1836 printk(KERN_WARNING"%s: Could not allocate reply buffer\n",pHba->name);
1837 return -ENOMEM;
1839 sg_offset = (msg[0]>>4)&0xf;
1840 msg[2] = 0x40000000; // IOCTL context
1841 msg[3] = adpt_ioctl_to_context(pHba, reply);
1842 if (msg[3] == (u32)-1)
1843 return -EBUSY;
1845 memset(sg_list,0, sizeof(sg_list[0])*pHba->sg_tablesize);
1846 if(sg_offset) {
1847 // TODO add 64 bit API
1848 struct sg_simple_element *sg = (struct sg_simple_element*) (msg+sg_offset);
1849 sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
1850 if (sg_count > pHba->sg_tablesize){
1851 printk(KERN_DEBUG"%s:IOCTL SG List too large (%u)\n", pHba->name,sg_count);
1852 kfree (reply);
1853 return -EINVAL;
1856 for(i = 0; i < sg_count; i++) {
1857 int sg_size;
1859 if (!(sg[i].flag_count & 0x10000000 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT*/)) {
1860 printk(KERN_DEBUG"%s:Bad SG element %d - not simple (%x)\n",pHba->name,i, sg[i].flag_count);
1861 rcode = -EINVAL;
1862 goto cleanup;
1864 sg_size = sg[i].flag_count & 0xffffff;
1865 /* Allocate memory for the transfer */
1866 p = dma_alloc_coherent(&pHba->pDev->dev, sg_size, &addr, GFP_KERNEL);
1867 if(!p) {
1868 printk(KERN_DEBUG"%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
1869 pHba->name,sg_size,i,sg_count);
1870 rcode = -ENOMEM;
1871 goto cleanup;
1873 sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
1874 /* Copy in the user's SG buffer if necessary */
1875 if(sg[i].flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR*/) {
1876 // sg_simple_element API is 32 bit
1877 if (copy_from_user(p,(void __user *)(ulong)sg[i].addr_bus, sg_size)) {
1878 printk(KERN_DEBUG"%s: Could not copy SG buf %d FROM user\n",pHba->name,i);
1879 rcode = -EFAULT;
1880 goto cleanup;
1883 /* sg_simple_element API is 32 bit, but addr < 4GB */
1884 sg[i].addr_bus = addr;
1888 do {
1889 if(pHba->host)
1890 spin_lock_irqsave(pHba->host->host_lock, flags);
1891 // This state stops any new commands from enterring the
1892 // controller while processing the ioctl
1893 // pHba->state |= DPTI_STATE_IOCTL;
1894 // We can't set this now - The scsi subsystem sets host_blocked and
1895 // the queue empties and stops. We need a way to restart the queue
1896 rcode = adpt_i2o_post_wait(pHba, msg, size, FOREVER);
1897 if (rcode != 0)
1898 printk("adpt_i2o_passthru: post wait failed %d %p\n",
1899 rcode, reply);
1900 // pHba->state &= ~DPTI_STATE_IOCTL;
1901 if(pHba->host)
1902 spin_unlock_irqrestore(pHba->host->host_lock, flags);
1903 } while(rcode == -ETIMEDOUT);
1905 if(rcode){
1906 goto cleanup;
1909 if(sg_offset) {
1910 /* Copy back the Scatter Gather buffers back to user space */
1911 u32 j;
1912 // TODO add 64 bit API
1913 struct sg_simple_element* sg;
1914 int sg_size;
1916 // re-acquire the original message to handle correctly the sg copy operation
1917 memset(&msg, 0, MAX_MESSAGE_SIZE*4);
1918 // get user msg size in u32s
1919 if(get_user(size, &user_msg[0])){
1920 rcode = -EFAULT;
1921 goto cleanup;
1923 size = size>>16;
1924 size *= 4;
1925 if (size > MAX_MESSAGE_SIZE) {
1926 rcode = -EINVAL;
1927 goto cleanup;
1929 /* Copy in the user's I2O command */
1930 if (copy_from_user (msg, user_msg, size)) {
1931 rcode = -EFAULT;
1932 goto cleanup;
1934 sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
1936 // TODO add 64 bit API
1937 sg = (struct sg_simple_element*)(msg + sg_offset);
1938 for (j = 0; j < sg_count; j++) {
1939 /* Copy out the SG list to user's buffer if necessary */
1940 if(! (sg[j].flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR*/)) {
1941 sg_size = sg[j].flag_count & 0xffffff;
1942 // sg_simple_element API is 32 bit
1943 if (copy_to_user((void __user *)(ulong)sg[j].addr_bus,sg_list[j], sg_size)) {
1944 printk(KERN_WARNING"%s: Could not copy %p TO user %x\n",pHba->name, sg_list[j], sg[j].addr_bus);
1945 rcode = -EFAULT;
1946 goto cleanup;
1952 /* Copy back the reply to user space */
1953 if (reply_size) {
1954 // we wrote our own values for context - now restore the user supplied ones
1955 if(copy_from_user(reply+2, user_msg+2, sizeof(u32)*2)) {
1956 printk(KERN_WARNING"%s: Could not copy message context FROM user\n",pHba->name);
1957 rcode = -EFAULT;
1959 if(copy_to_user(user_reply, reply, reply_size)) {
1960 printk(KERN_WARNING"%s: Could not copy reply TO user\n",pHba->name);
1961 rcode = -EFAULT;
1966 cleanup:
1967 if (rcode != -ETIME && rcode != -EINTR) {
1968 struct sg_simple_element *sg =
1969 (struct sg_simple_element*) (msg +sg_offset);
1970 kfree (reply);
1971 while(sg_index) {
1972 if(sg_list[--sg_index]) {
1973 dma_free_coherent(&pHba->pDev->dev,
1974 sg[sg_index].flag_count & 0xffffff,
1975 sg_list[sg_index],
1976 sg[sg_index].addr_bus);
1980 return rcode;
1983 #if defined __ia64__
1984 static void adpt_ia64_info(sysInfo_S* si)
1986 // This is all the info we need for now
1987 // We will add more info as our new
1988 // managmenent utility requires it
1989 si->processorType = PROC_IA64;
1991 #endif
1993 #if defined __sparc__
1994 static void adpt_sparc_info(sysInfo_S* si)
1996 // This is all the info we need for now
1997 // We will add more info as our new
1998 // managmenent utility requires it
1999 si->processorType = PROC_ULTRASPARC;
2001 #endif
2002 #if defined __alpha__
2003 static void adpt_alpha_info(sysInfo_S* si)
2005 // This is all the info we need for now
2006 // We will add more info as our new
2007 // managmenent utility requires it
2008 si->processorType = PROC_ALPHA;
2010 #endif
2012 #if defined __i386__
2013 static void adpt_i386_info(sysInfo_S* si)
2015 // This is all the info we need for now
2016 // We will add more info as our new
2017 // managmenent utility requires it
2018 switch (boot_cpu_data.x86) {
2019 case CPU_386:
2020 si->processorType = PROC_386;
2021 break;
2022 case CPU_486:
2023 si->processorType = PROC_486;
2024 break;
2025 case CPU_586:
2026 si->processorType = PROC_PENTIUM;
2027 break;
2028 default: // Just in case
2029 si->processorType = PROC_PENTIUM;
2030 break;
2033 #endif
2036 * This routine returns information about the system. This does not effect
2037 * any logic and if the info is wrong - it doesn't matter.
2040 /* Get all the info we can not get from kernel services */
2041 static int adpt_system_info(void __user *buffer)
2043 sysInfo_S si;
2045 memset(&si, 0, sizeof(si));
2047 si.osType = OS_LINUX;
2048 si.osMajorVersion = 0;
2049 si.osMinorVersion = 0;
2050 si.osRevision = 0;
2051 si.busType = SI_PCI_BUS;
2052 si.processorFamily = DPTI_sig.dsProcessorFamily;
2054 #if defined __i386__
2055 adpt_i386_info(&si);
2056 #elif defined (__ia64__)
2057 adpt_ia64_info(&si);
2058 #elif defined(__sparc__)
2059 adpt_sparc_info(&si);
2060 #elif defined (__alpha__)
2061 adpt_alpha_info(&si);
2062 #else
2063 si.processorType = 0xff ;
2064 #endif
2065 if (copy_to_user(buffer, &si, sizeof(si))){
2066 printk(KERN_WARNING"dpti: Could not copy buffer TO user\n");
2067 return -EFAULT;
2070 return 0;
2073 static int adpt_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
2075 int minor;
2076 int error = 0;
2077 adpt_hba* pHba;
2078 ulong flags = 0;
2079 void __user *argp = (void __user *)arg;
2081 minor = iminor(inode);
2082 if (minor >= DPTI_MAX_HBA){
2083 return -ENXIO;
2085 mutex_lock(&adpt_configuration_lock);
2086 for (pHba = hba_chain; pHba; pHba = pHba->next) {
2087 if (pHba->unit == minor) {
2088 break; /* found adapter */
2091 mutex_unlock(&adpt_configuration_lock);
2092 if(pHba == NULL){
2093 return -ENXIO;
2096 while((volatile u32) pHba->state & DPTI_STATE_RESET )
2097 schedule_timeout_uninterruptible(2);
2099 switch (cmd) {
2100 // TODO: handle 3 cases
2101 case DPT_SIGNATURE:
2102 if (copy_to_user(argp, &DPTI_sig, sizeof(DPTI_sig))) {
2103 return -EFAULT;
2105 break;
2106 case I2OUSRCMD:
2107 return adpt_i2o_passthru(pHba, argp);
2109 case DPT_CTRLINFO:{
2110 drvrHBAinfo_S HbaInfo;
2112 #define FLG_OSD_PCI_VALID 0x0001
2113 #define FLG_OSD_DMA 0x0002
2114 #define FLG_OSD_I2O 0x0004
2115 memset(&HbaInfo, 0, sizeof(HbaInfo));
2116 HbaInfo.drvrHBAnum = pHba->unit;
2117 HbaInfo.baseAddr = (ulong) pHba->base_addr_phys;
2118 HbaInfo.blinkState = adpt_read_blink_led(pHba);
2119 HbaInfo.pciBusNum = pHba->pDev->bus->number;
2120 HbaInfo.pciDeviceNum=PCI_SLOT(pHba->pDev->devfn);
2121 HbaInfo.Interrupt = pHba->pDev->irq;
2122 HbaInfo.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
2123 if(copy_to_user(argp, &HbaInfo, sizeof(HbaInfo))){
2124 printk(KERN_WARNING"%s: Could not copy HbaInfo TO user\n",pHba->name);
2125 return -EFAULT;
2127 break;
2129 case DPT_SYSINFO:
2130 return adpt_system_info(argp);
2131 case DPT_BLINKLED:{
2132 u32 value;
2133 value = (u32)adpt_read_blink_led(pHba);
2134 if (copy_to_user(argp, &value, sizeof(value))) {
2135 return -EFAULT;
2137 break;
2139 case I2ORESETCMD:
2140 if(pHba->host)
2141 spin_lock_irqsave(pHba->host->host_lock, flags);
2142 adpt_hba_reset(pHba);
2143 if(pHba->host)
2144 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2145 break;
2146 case I2ORESCANCMD:
2147 adpt_rescan(pHba);
2148 break;
2149 default:
2150 return -EINVAL;
2153 return error;
2156 static long adpt_unlocked_ioctl(struct file *file, uint cmd, ulong arg)
2158 struct inode *inode;
2159 long ret;
2161 inode = file->f_dentry->d_inode;
2163 lock_kernel();
2164 ret = adpt_ioctl(inode, file, cmd, arg);
2165 unlock_kernel();
2167 return ret;
2170 #ifdef CONFIG_COMPAT
2171 static long compat_adpt_ioctl(struct file *file,
2172 unsigned int cmd, unsigned long arg)
2174 struct inode *inode;
2175 long ret;
2177 inode = file->f_dentry->d_inode;
2179 lock_kernel();
2181 switch(cmd) {
2182 case DPT_SIGNATURE:
2183 case I2OUSRCMD:
2184 case DPT_CTRLINFO:
2185 case DPT_SYSINFO:
2186 case DPT_BLINKLED:
2187 case I2ORESETCMD:
2188 case I2ORESCANCMD:
2189 case (DPT_TARGET_BUSY & 0xFFFF):
2190 case DPT_TARGET_BUSY:
2191 ret = adpt_ioctl(inode, file, cmd, arg);
2192 break;
2193 default:
2194 ret = -ENOIOCTLCMD;
2197 unlock_kernel();
2199 return ret;
2201 #endif
2203 static irqreturn_t adpt_isr(int irq, void *dev_id)
2205 struct scsi_cmnd* cmd;
2206 adpt_hba* pHba = dev_id;
2207 u32 m;
2208 void __iomem *reply;
2209 u32 status=0;
2210 u32 context;
2211 ulong flags = 0;
2212 int handled = 0;
2214 if (pHba == NULL){
2215 printk(KERN_WARNING"adpt_isr: NULL dev_id\n");
2216 return IRQ_NONE;
2218 if(pHba->host)
2219 spin_lock_irqsave(pHba->host->host_lock, flags);
2221 while( readl(pHba->irq_mask) & I2O_INTERRUPT_PENDING_B) {
2222 m = readl(pHba->reply_port);
2223 if(m == EMPTY_QUEUE){
2224 // Try twice then give up
2225 rmb();
2226 m = readl(pHba->reply_port);
2227 if(m == EMPTY_QUEUE){
2228 // This really should not happen
2229 printk(KERN_ERR"dpti: Could not get reply frame\n");
2230 goto out;
2233 if (pHba->reply_pool_pa <= m &&
2234 m < pHba->reply_pool_pa +
2235 (pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4)) {
2236 reply = (u8 *)pHba->reply_pool +
2237 (m - pHba->reply_pool_pa);
2238 } else {
2239 /* Ick, we should *never* be here */
2240 printk(KERN_ERR "dpti: reply frame not from pool\n");
2241 reply = (u8 *)bus_to_virt(m);
2244 if (readl(reply) & MSG_FAIL) {
2245 u32 old_m = readl(reply+28);
2246 void __iomem *msg;
2247 u32 old_context;
2248 PDEBUG("%s: Failed message\n",pHba->name);
2249 if(old_m >= 0x100000){
2250 printk(KERN_ERR"%s: Bad preserved MFA (%x)- dropping frame\n",pHba->name,old_m);
2251 writel(m,pHba->reply_port);
2252 continue;
2254 // Transaction context is 0 in failed reply frame
2255 msg = pHba->msg_addr_virt + old_m;
2256 old_context = readl(msg+12);
2257 writel(old_context, reply+12);
2258 adpt_send_nop(pHba, old_m);
2260 context = readl(reply+8);
2261 if(context & 0x40000000){ // IOCTL
2262 void *p = adpt_ioctl_from_context(pHba, readl(reply+12));
2263 if( p != NULL) {
2264 memcpy_fromio(p, reply, REPLY_FRAME_SIZE * 4);
2266 // All IOCTLs will also be post wait
2268 if(context & 0x80000000){ // Post wait message
2269 status = readl(reply+16);
2270 if(status >> 24){
2271 status &= 0xffff; /* Get detail status */
2272 } else {
2273 status = I2O_POST_WAIT_OK;
2275 if(!(context & 0x40000000)) {
2276 cmd = adpt_cmd_from_context(pHba,
2277 readl(reply+12));
2278 if(cmd != NULL) {
2279 printk(KERN_WARNING"%s: Apparent SCSI cmd in Post Wait Context - cmd=%p context=%x\n", pHba->name, cmd, context);
2282 adpt_i2o_post_wait_complete(context, status);
2283 } else { // SCSI message
2284 cmd = adpt_cmd_from_context (pHba, readl(reply+12));
2285 if(cmd != NULL){
2286 scsi_dma_unmap(cmd);
2287 if(cmd->serial_number != 0) { // If not timedout
2288 adpt_i2o_to_scsi(reply, cmd);
2292 writel(m, pHba->reply_port);
2293 wmb();
2294 rmb();
2296 handled = 1;
2297 out: if(pHba->host)
2298 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2299 return IRQ_RETVAL(handled);
2302 static s32 adpt_scsi_to_i2o(adpt_hba* pHba, struct scsi_cmnd* cmd, struct adpt_device* d)
2304 int i;
2305 u32 msg[MAX_MESSAGE_SIZE];
2306 u32* mptr;
2307 u32* lptr;
2308 u32 *lenptr;
2309 int direction;
2310 int scsidir;
2311 int nseg;
2312 u32 len;
2313 u32 reqlen;
2314 s32 rcode;
2315 dma_addr_t addr;
2317 memset(msg, 0 , sizeof(msg));
2318 len = scsi_bufflen(cmd);
2319 direction = 0x00000000;
2321 scsidir = 0x00000000; // DATA NO XFER
2322 if(len) {
2324 * Set SCBFlags to indicate if data is being transferred
2325 * in or out, or no data transfer
2326 * Note: Do not have to verify index is less than 0 since
2327 * cmd->cmnd[0] is an unsigned char
2329 switch(cmd->sc_data_direction){
2330 case DMA_FROM_DEVICE:
2331 scsidir =0x40000000; // DATA IN (iop<--dev)
2332 break;
2333 case DMA_TO_DEVICE:
2334 direction=0x04000000; // SGL OUT
2335 scsidir =0x80000000; // DATA OUT (iop-->dev)
2336 break;
2337 case DMA_NONE:
2338 break;
2339 case DMA_BIDIRECTIONAL:
2340 scsidir =0x40000000; // DATA IN (iop<--dev)
2341 // Assume In - and continue;
2342 break;
2343 default:
2344 printk(KERN_WARNING"%s: scsi opcode 0x%x not supported.\n",
2345 pHba->name, cmd->cmnd[0]);
2346 cmd->result = (DID_OK <<16) | (INITIATOR_ERROR << 8);
2347 cmd->scsi_done(cmd);
2348 return 0;
2351 // msg[0] is set later
2352 // I2O_CMD_SCSI_EXEC
2353 msg[1] = ((0xff<<24)|(HOST_TID<<12)|d->tid);
2354 msg[2] = 0;
2355 msg[3] = adpt_cmd_to_context(cmd); /* Want SCSI control block back */
2356 // Our cards use the transaction context as the tag for queueing
2357 // Adaptec/DPT Private stuff
2358 msg[4] = I2O_CMD_SCSI_EXEC|(DPT_ORGANIZATION_ID<<16);
2359 msg[5] = d->tid;
2360 /* Direction, disconnect ok | sense data | simple queue , CDBLen */
2361 // I2O_SCB_FLAG_ENABLE_DISCONNECT |
2362 // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG |
2363 // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
2364 msg[6] = scsidir|0x20a00000|cmd->cmd_len;
2366 mptr=msg+7;
2368 // Write SCSI command into the message - always 16 byte block
2369 memset(mptr, 0, 16);
2370 memcpy(mptr, cmd->cmnd, cmd->cmd_len);
2371 mptr+=4;
2372 lenptr=mptr++; /* Remember me - fill in when we know */
2373 if (dpt_dma64(pHba)) {
2374 reqlen = 16; // SINGLE SGE
2375 *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
2376 *mptr++ = 1 << PAGE_SHIFT;
2377 } else {
2378 reqlen = 14; // SINGLE SGE
2380 /* Now fill in the SGList and command */
2382 nseg = scsi_dma_map(cmd);
2383 BUG_ON(nseg < 0);
2384 if (nseg) {
2385 struct scatterlist *sg;
2387 len = 0;
2388 scsi_for_each_sg(cmd, sg, nseg, i) {
2389 lptr = mptr;
2390 *mptr++ = direction|0x10000000|sg_dma_len(sg);
2391 len+=sg_dma_len(sg);
2392 addr = sg_dma_address(sg);
2393 *mptr++ = dma_low(addr);
2394 if (dpt_dma64(pHba))
2395 *mptr++ = dma_high(addr);
2396 /* Make this an end of list */
2397 if (i == nseg - 1)
2398 *lptr = direction|0xD0000000|sg_dma_len(sg);
2400 reqlen = mptr - msg;
2401 *lenptr = len;
2403 if(cmd->underflow && len != cmd->underflow){
2404 printk(KERN_WARNING"Cmd len %08X Cmd underflow %08X\n",
2405 len, cmd->underflow);
2407 } else {
2408 *lenptr = len = 0;
2409 reqlen = 12;
2412 /* Stick the headers on */
2413 msg[0] = reqlen<<16 | ((reqlen > 12) ? SGL_OFFSET_12 : SGL_OFFSET_0);
2415 // Send it on it's way
2416 rcode = adpt_i2o_post_this(pHba, msg, reqlen<<2);
2417 if (rcode == 0) {
2418 return 0;
2420 return rcode;
2424 static s32 adpt_scsi_host_alloc(adpt_hba* pHba, struct scsi_host_template *sht)
2426 struct Scsi_Host *host;
2428 host = scsi_host_alloc(sht, sizeof(adpt_hba*));
2429 if (host == NULL) {
2430 printk("%s: scsi_host_alloc returned NULL\n", pHba->name);
2431 return -1;
2433 host->hostdata[0] = (unsigned long)pHba;
2434 pHba->host = host;
2436 host->irq = pHba->pDev->irq;
2437 /* no IO ports, so don't have to set host->io_port and
2438 * host->n_io_port
2440 host->io_port = 0;
2441 host->n_io_port = 0;
2442 /* see comments in scsi_host.h */
2443 host->max_id = 16;
2444 host->max_lun = 256;
2445 host->max_channel = pHba->top_scsi_channel + 1;
2446 host->cmd_per_lun = 1;
2447 host->unique_id = (u32)sys_tbl_pa + pHba->unit;
2448 host->sg_tablesize = pHba->sg_tablesize;
2449 host->can_queue = pHba->post_fifo_size;
2451 return 0;
2455 static s32 adpt_i2o_to_scsi(void __iomem *reply, struct scsi_cmnd* cmd)
2457 adpt_hba* pHba;
2458 u32 hba_status;
2459 u32 dev_status;
2460 u32 reply_flags = readl(reply) & 0xff00; // Leave it shifted up 8 bits
2461 // I know this would look cleaner if I just read bytes
2462 // but the model I have been using for all the rest of the
2463 // io is in 4 byte words - so I keep that model
2464 u16 detailed_status = readl(reply+16) &0xffff;
2465 dev_status = (detailed_status & 0xff);
2466 hba_status = detailed_status >> 8;
2468 // calculate resid for sg
2469 scsi_set_resid(cmd, scsi_bufflen(cmd) - readl(reply+20));
2471 pHba = (adpt_hba*) cmd->device->host->hostdata[0];
2473 cmd->sense_buffer[0] = '\0'; // initialize sense valid flag to false
2475 if(!(reply_flags & MSG_FAIL)) {
2476 switch(detailed_status & I2O_SCSI_DSC_MASK) {
2477 case I2O_SCSI_DSC_SUCCESS:
2478 cmd->result = (DID_OK << 16);
2479 // handle underflow
2480 if (readl(reply+20) < cmd->underflow) {
2481 cmd->result = (DID_ERROR <<16);
2482 printk(KERN_WARNING"%s: SCSI CMD underflow\n",pHba->name);
2484 break;
2485 case I2O_SCSI_DSC_REQUEST_ABORTED:
2486 cmd->result = (DID_ABORT << 16);
2487 break;
2488 case I2O_SCSI_DSC_PATH_INVALID:
2489 case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
2490 case I2O_SCSI_DSC_SELECTION_TIMEOUT:
2491 case I2O_SCSI_DSC_COMMAND_TIMEOUT:
2492 case I2O_SCSI_DSC_NO_ADAPTER:
2493 case I2O_SCSI_DSC_RESOURCE_UNAVAILABLE:
2494 printk(KERN_WARNING"%s: SCSI Timeout-Device (%d,%d,%d) hba status=0x%x, dev status=0x%x, cmd=0x%x\n",
2495 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun, hba_status, dev_status, cmd->cmnd[0]);
2496 cmd->result = (DID_TIME_OUT << 16);
2497 break;
2498 case I2O_SCSI_DSC_ADAPTER_BUSY:
2499 case I2O_SCSI_DSC_BUS_BUSY:
2500 cmd->result = (DID_BUS_BUSY << 16);
2501 break;
2502 case I2O_SCSI_DSC_SCSI_BUS_RESET:
2503 case I2O_SCSI_DSC_BDR_MESSAGE_SENT:
2504 cmd->result = (DID_RESET << 16);
2505 break;
2506 case I2O_SCSI_DSC_PARITY_ERROR_FAILURE:
2507 printk(KERN_WARNING"%s: SCSI CMD parity error\n",pHba->name);
2508 cmd->result = (DID_PARITY << 16);
2509 break;
2510 case I2O_SCSI_DSC_UNABLE_TO_ABORT:
2511 case I2O_SCSI_DSC_COMPLETE_WITH_ERROR:
2512 case I2O_SCSI_DSC_UNABLE_TO_TERMINATE:
2513 case I2O_SCSI_DSC_MR_MESSAGE_RECEIVED:
2514 case I2O_SCSI_DSC_AUTOSENSE_FAILED:
2515 case I2O_SCSI_DSC_DATA_OVERRUN:
2516 case I2O_SCSI_DSC_UNEXPECTED_BUS_FREE:
2517 case I2O_SCSI_DSC_SEQUENCE_FAILURE:
2518 case I2O_SCSI_DSC_REQUEST_LENGTH_ERROR:
2519 case I2O_SCSI_DSC_PROVIDE_FAILURE:
2520 case I2O_SCSI_DSC_REQUEST_TERMINATED:
2521 case I2O_SCSI_DSC_IDE_MESSAGE_SENT:
2522 case I2O_SCSI_DSC_UNACKNOWLEDGED_EVENT:
2523 case I2O_SCSI_DSC_MESSAGE_RECEIVED:
2524 case I2O_SCSI_DSC_INVALID_CDB:
2525 case I2O_SCSI_DSC_LUN_INVALID:
2526 case I2O_SCSI_DSC_SCSI_TID_INVALID:
2527 case I2O_SCSI_DSC_FUNCTION_UNAVAILABLE:
2528 case I2O_SCSI_DSC_NO_NEXUS:
2529 case I2O_SCSI_DSC_CDB_RECEIVED:
2530 case I2O_SCSI_DSC_LUN_ALREADY_ENABLED:
2531 case I2O_SCSI_DSC_QUEUE_FROZEN:
2532 case I2O_SCSI_DSC_REQUEST_INVALID:
2533 default:
2534 printk(KERN_WARNING"%s: SCSI error %0x-Device(%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2535 pHba->name, detailed_status & I2O_SCSI_DSC_MASK, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
2536 hba_status, dev_status, cmd->cmnd[0]);
2537 cmd->result = (DID_ERROR << 16);
2538 break;
2541 // copy over the request sense data if it was a check
2542 // condition status
2543 if (dev_status == SAM_STAT_CHECK_CONDITION) {
2544 u32 len = min(SCSI_SENSE_BUFFERSIZE, 40);
2545 // Copy over the sense data
2546 memcpy_fromio(cmd->sense_buffer, (reply+28) , len);
2547 if(cmd->sense_buffer[0] == 0x70 /* class 7 */ &&
2548 cmd->sense_buffer[2] == DATA_PROTECT ){
2549 /* This is to handle an array failed */
2550 cmd->result = (DID_TIME_OUT << 16);
2551 printk(KERN_WARNING"%s: SCSI Data Protect-Device (%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2552 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
2553 hba_status, dev_status, cmd->cmnd[0]);
2557 } else {
2558 /* In this condtion we could not talk to the tid
2559 * the card rejected it. We should signal a retry
2560 * for a limitted number of retries.
2562 cmd->result = (DID_TIME_OUT << 16);
2563 printk(KERN_WARNING"%s: I2O MSG_FAIL - Device (%d,%d,%d) tid=%d, cmd=0x%x\n",
2564 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, (u32)cmd->device->lun,
2565 ((struct adpt_device*)(cmd->device->hostdata))->tid, cmd->cmnd[0]);
2568 cmd->result |= (dev_status);
2570 if(cmd->scsi_done != NULL){
2571 cmd->scsi_done(cmd);
2573 return cmd->result;
2577 static s32 adpt_rescan(adpt_hba* pHba)
2579 s32 rcode;
2580 ulong flags = 0;
2582 if(pHba->host)
2583 spin_lock_irqsave(pHba->host->host_lock, flags);
2584 if ((rcode=adpt_i2o_lct_get(pHba)) < 0)
2585 goto out;
2586 if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0)
2587 goto out;
2588 rcode = 0;
2589 out: if(pHba->host)
2590 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2591 return rcode;
2595 static s32 adpt_i2o_reparse_lct(adpt_hba* pHba)
2597 int i;
2598 int max;
2599 int tid;
2600 struct i2o_device *d;
2601 i2o_lct *lct = pHba->lct;
2602 u8 bus_no = 0;
2603 s16 scsi_id;
2604 s16 scsi_lun;
2605 u32 buf[10]; // at least 8 u32's
2606 struct adpt_device* pDev = NULL;
2607 struct i2o_device* pI2o_dev = NULL;
2609 if (lct == NULL) {
2610 printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
2611 return -1;
2614 max = lct->table_size;
2615 max -= 3;
2616 max /= 9;
2618 // Mark each drive as unscanned
2619 for (d = pHba->devices; d; d = d->next) {
2620 pDev =(struct adpt_device*) d->owner;
2621 if(!pDev){
2622 continue;
2624 pDev->state |= DPTI_DEV_UNSCANNED;
2627 printk(KERN_INFO "%s: LCT has %d entries.\n", pHba->name,max);
2629 for(i=0;i<max;i++) {
2630 if( lct->lct_entry[i].user_tid != 0xfff){
2631 continue;
2634 if( lct->lct_entry[i].class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
2635 lct->lct_entry[i].class_id == I2O_CLASS_SCSI_PERIPHERAL ||
2636 lct->lct_entry[i].class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
2637 tid = lct->lct_entry[i].tid;
2638 if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
2639 printk(KERN_ERR"%s: Could not query device\n",pHba->name);
2640 continue;
2642 bus_no = buf[0]>>16;
2643 scsi_id = buf[1];
2644 scsi_lun = (buf[2]>>8 )&0xff;
2645 pDev = pHba->channel[bus_no].device[scsi_id];
2646 /* da lun */
2647 while(pDev) {
2648 if(pDev->scsi_lun == scsi_lun) {
2649 break;
2651 pDev = pDev->next_lun;
2653 if(!pDev ) { // Something new add it
2654 d = kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
2655 if(d==NULL)
2657 printk(KERN_CRIT "Out of memory for I2O device data.\n");
2658 return -ENOMEM;
2661 d->controller = pHba;
2662 d->next = NULL;
2664 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2666 d->flags = 0;
2667 adpt_i2o_report_hba_unit(pHba, d);
2668 adpt_i2o_install_device(pHba, d);
2670 if(bus_no >= MAX_CHANNEL) { // Something wrong skip it
2671 printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
2672 continue;
2674 pDev = pHba->channel[bus_no].device[scsi_id];
2675 if( pDev == NULL){
2676 pDev = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
2677 if(pDev == NULL) {
2678 return -ENOMEM;
2680 pHba->channel[bus_no].device[scsi_id] = pDev;
2681 } else {
2682 while (pDev->next_lun) {
2683 pDev = pDev->next_lun;
2685 pDev = pDev->next_lun = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
2686 if(pDev == NULL) {
2687 return -ENOMEM;
2690 pDev->tid = d->lct_data.tid;
2691 pDev->scsi_channel = bus_no;
2692 pDev->scsi_id = scsi_id;
2693 pDev->scsi_lun = scsi_lun;
2694 pDev->pI2o_dev = d;
2695 d->owner = pDev;
2696 pDev->type = (buf[0])&0xff;
2697 pDev->flags = (buf[0]>>8)&0xff;
2698 // Too late, SCSI system has made up it's mind, but what the hey ...
2699 if(scsi_id > pHba->top_scsi_id){
2700 pHba->top_scsi_id = scsi_id;
2702 if(scsi_lun > pHba->top_scsi_lun){
2703 pHba->top_scsi_lun = scsi_lun;
2705 continue;
2706 } // end of new i2o device
2708 // We found an old device - check it
2709 while(pDev) {
2710 if(pDev->scsi_lun == scsi_lun) {
2711 if(!scsi_device_online(pDev->pScsi_dev)) {
2712 printk(KERN_WARNING"%s: Setting device (%d,%d,%d) back online\n",
2713 pHba->name,bus_no,scsi_id,scsi_lun);
2714 if (pDev->pScsi_dev) {
2715 scsi_device_set_state(pDev->pScsi_dev, SDEV_RUNNING);
2718 d = pDev->pI2o_dev;
2719 if(d->lct_data.tid != tid) { // something changed
2720 pDev->tid = tid;
2721 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2722 if (pDev->pScsi_dev) {
2723 pDev->pScsi_dev->changed = TRUE;
2724 pDev->pScsi_dev->removable = TRUE;
2727 // Found it - mark it scanned
2728 pDev->state = DPTI_DEV_ONLINE;
2729 break;
2731 pDev = pDev->next_lun;
2735 for (pI2o_dev = pHba->devices; pI2o_dev; pI2o_dev = pI2o_dev->next) {
2736 pDev =(struct adpt_device*) pI2o_dev->owner;
2737 if(!pDev){
2738 continue;
2740 // Drive offline drives that previously existed but could not be found
2741 // in the LCT table
2742 if (pDev->state & DPTI_DEV_UNSCANNED){
2743 pDev->state = DPTI_DEV_OFFLINE;
2744 printk(KERN_WARNING"%s: Device (%d,%d,%d) offline\n",pHba->name,pDev->scsi_channel,pDev->scsi_id,pDev->scsi_lun);
2745 if (pDev->pScsi_dev) {
2746 scsi_device_set_state(pDev->pScsi_dev, SDEV_OFFLINE);
2750 return 0;
2753 static void adpt_fail_posted_scbs(adpt_hba* pHba)
2755 struct scsi_cmnd* cmd = NULL;
2756 struct scsi_device* d = NULL;
2758 shost_for_each_device(d, pHba->host) {
2759 unsigned long flags;
2760 spin_lock_irqsave(&d->list_lock, flags);
2761 list_for_each_entry(cmd, &d->cmd_list, list) {
2762 if(cmd->serial_number == 0){
2763 continue;
2765 cmd->result = (DID_OK << 16) | (QUEUE_FULL <<1);
2766 cmd->scsi_done(cmd);
2768 spin_unlock_irqrestore(&d->list_lock, flags);
2773 /*============================================================================
2774 * Routines from i2o subsystem
2775 *============================================================================
2781 * Bring an I2O controller into HOLD state. See the spec.
2783 static int adpt_i2o_activate_hba(adpt_hba* pHba)
2785 int rcode;
2787 if(pHba->initialized ) {
2788 if (adpt_i2o_status_get(pHba) < 0) {
2789 if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2790 printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2791 return rcode;
2793 if (adpt_i2o_status_get(pHba) < 0) {
2794 printk(KERN_INFO "HBA not responding.\n");
2795 return -1;
2799 if(pHba->status_block->iop_state == ADAPTER_STATE_FAULTED) {
2800 printk(KERN_CRIT "%s: hardware fault\n", pHba->name);
2801 return -1;
2804 if (pHba->status_block->iop_state == ADAPTER_STATE_READY ||
2805 pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
2806 pHba->status_block->iop_state == ADAPTER_STATE_HOLD ||
2807 pHba->status_block->iop_state == ADAPTER_STATE_FAILED) {
2808 adpt_i2o_reset_hba(pHba);
2809 if (adpt_i2o_status_get(pHba) < 0 || pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
2810 printk(KERN_ERR "%s: Failed to initialize.\n", pHba->name);
2811 return -1;
2814 } else {
2815 if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2816 printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2817 return rcode;
2822 if (adpt_i2o_init_outbound_q(pHba) < 0) {
2823 return -1;
2826 /* In HOLD state */
2828 if (adpt_i2o_hrt_get(pHba) < 0) {
2829 return -1;
2832 return 0;
2836 * Bring a controller online into OPERATIONAL state.
2839 static int adpt_i2o_online_hba(adpt_hba* pHba)
2841 if (adpt_i2o_systab_send(pHba) < 0) {
2842 adpt_i2o_delete_hba(pHba);
2843 return -1;
2845 /* In READY state */
2847 if (adpt_i2o_enable_hba(pHba) < 0) {
2848 adpt_i2o_delete_hba(pHba);
2849 return -1;
2852 /* In OPERATIONAL state */
2853 return 0;
2856 static s32 adpt_send_nop(adpt_hba*pHba,u32 m)
2858 u32 __iomem *msg;
2859 ulong timeout = jiffies + 5*HZ;
2861 while(m == EMPTY_QUEUE){
2862 rmb();
2863 m = readl(pHba->post_port);
2864 if(m != EMPTY_QUEUE){
2865 break;
2867 if(time_after(jiffies,timeout)){
2868 printk(KERN_ERR "%s: Timeout waiting for message frame!\n",pHba->name);
2869 return 2;
2871 schedule_timeout_uninterruptible(1);
2873 msg = (u32 __iomem *)(pHba->msg_addr_virt + m);
2874 writel( THREE_WORD_MSG_SIZE | SGL_OFFSET_0,&msg[0]);
2875 writel( I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0,&msg[1]);
2876 writel( 0,&msg[2]);
2877 wmb();
2879 writel(m, pHba->post_port);
2880 wmb();
2881 return 0;
2884 static s32 adpt_i2o_init_outbound_q(adpt_hba* pHba)
2886 u8 *status;
2887 dma_addr_t addr;
2888 u32 __iomem *msg = NULL;
2889 int i;
2890 ulong timeout = jiffies + TMOUT_INITOUTBOUND*HZ;
2891 u32 m;
2893 do {
2894 rmb();
2895 m = readl(pHba->post_port);
2896 if (m != EMPTY_QUEUE) {
2897 break;
2900 if(time_after(jiffies,timeout)){
2901 printk(KERN_WARNING"%s: Timeout waiting for message frame\n",pHba->name);
2902 return -ETIMEDOUT;
2904 schedule_timeout_uninterruptible(1);
2905 } while(m == EMPTY_QUEUE);
2907 msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
2909 status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
2910 if (!status) {
2911 adpt_send_nop(pHba, m);
2912 printk(KERN_WARNING"%s: IOP reset failed - no free memory.\n",
2913 pHba->name);
2914 return -ENOMEM;
2916 memset(status, 0, 4);
2918 writel(EIGHT_WORD_MSG_SIZE| SGL_OFFSET_6, &msg[0]);
2919 writel(I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID, &msg[1]);
2920 writel(0, &msg[2]);
2921 writel(0x0106, &msg[3]); /* Transaction context */
2922 writel(4096, &msg[4]); /* Host page frame size */
2923 writel((REPLY_FRAME_SIZE)<<16|0x80, &msg[5]); /* Outbound msg frame size and Initcode */
2924 writel(0xD0000004, &msg[6]); /* Simple SG LE, EOB */
2925 writel((u32)addr, &msg[7]);
2927 writel(m, pHba->post_port);
2928 wmb();
2930 // Wait for the reply status to come back
2931 do {
2932 if (*status) {
2933 if (*status != 0x01 /*I2O_EXEC_OUTBOUND_INIT_IN_PROGRESS*/) {
2934 break;
2937 rmb();
2938 if(time_after(jiffies,timeout)){
2939 printk(KERN_WARNING"%s: Timeout Initializing\n",pHba->name);
2940 /* We lose 4 bytes of "status" here, but we
2941 cannot free these because controller may
2942 awake and corrupt those bytes at any time */
2943 /* dma_free_coherent(&pHba->pDev->dev, 4, status, addr); */
2944 return -ETIMEDOUT;
2946 schedule_timeout_uninterruptible(1);
2947 } while (1);
2949 // If the command was successful, fill the fifo with our reply
2950 // message packets
2951 if(*status != 0x04 /*I2O_EXEC_OUTBOUND_INIT_COMPLETE*/) {
2952 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
2953 return -2;
2955 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
2957 if(pHba->reply_pool != NULL) {
2958 dma_free_coherent(&pHba->pDev->dev,
2959 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
2960 pHba->reply_pool, pHba->reply_pool_pa);
2963 pHba->reply_pool = dma_alloc_coherent(&pHba->pDev->dev,
2964 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
2965 &pHba->reply_pool_pa, GFP_KERNEL);
2966 if (!pHba->reply_pool) {
2967 printk(KERN_ERR "%s: Could not allocate reply pool\n", pHba->name);
2968 return -ENOMEM;
2970 memset(pHba->reply_pool, 0 , pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4);
2972 for(i = 0; i < pHba->reply_fifo_size; i++) {
2973 writel(pHba->reply_pool_pa + (i * REPLY_FRAME_SIZE * 4),
2974 pHba->reply_port);
2975 wmb();
2977 adpt_i2o_status_get(pHba);
2978 return 0;
2983 * I2O System Table. Contains information about
2984 * all the IOPs in the system. Used to inform IOPs
2985 * about each other's existence.
2987 * sys_tbl_ver is the CurrentChangeIndicator that is
2988 * used by IOPs to track changes.
2993 static s32 adpt_i2o_status_get(adpt_hba* pHba)
2995 ulong timeout;
2996 u32 m;
2997 u32 __iomem *msg;
2998 u8 *status_block=NULL;
3000 if(pHba->status_block == NULL) {
3001 pHba->status_block = dma_alloc_coherent(&pHba->pDev->dev,
3002 sizeof(i2o_status_block),
3003 &pHba->status_block_pa, GFP_KERNEL);
3004 if(pHba->status_block == NULL) {
3005 printk(KERN_ERR
3006 "dpti%d: Get Status Block failed; Out of memory. \n",
3007 pHba->unit);
3008 return -ENOMEM;
3011 memset(pHba->status_block, 0, sizeof(i2o_status_block));
3012 status_block = (u8*)(pHba->status_block);
3013 timeout = jiffies+TMOUT_GETSTATUS*HZ;
3014 do {
3015 rmb();
3016 m = readl(pHba->post_port);
3017 if (m != EMPTY_QUEUE) {
3018 break;
3020 if(time_after(jiffies,timeout)){
3021 printk(KERN_ERR "%s: Timeout waiting for message !\n",
3022 pHba->name);
3023 return -ETIMEDOUT;
3025 schedule_timeout_uninterruptible(1);
3026 } while(m==EMPTY_QUEUE);
3029 msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
3031 writel(NINE_WORD_MSG_SIZE|SGL_OFFSET_0, &msg[0]);
3032 writel(I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID, &msg[1]);
3033 writel(1, &msg[2]);
3034 writel(0, &msg[3]);
3035 writel(0, &msg[4]);
3036 writel(0, &msg[5]);
3037 writel( dma_low(pHba->status_block_pa), &msg[6]);
3038 writel( dma_high(pHba->status_block_pa), &msg[7]);
3039 writel(sizeof(i2o_status_block), &msg[8]); // 88 bytes
3041 //post message
3042 writel(m, pHba->post_port);
3043 wmb();
3045 while(status_block[87]!=0xff){
3046 if(time_after(jiffies,timeout)){
3047 printk(KERN_ERR"dpti%d: Get status timeout.\n",
3048 pHba->unit);
3049 return -ETIMEDOUT;
3051 rmb();
3052 schedule_timeout_uninterruptible(1);
3055 // Set up our number of outbound and inbound messages
3056 pHba->post_fifo_size = pHba->status_block->max_inbound_frames;
3057 if (pHba->post_fifo_size > MAX_TO_IOP_MESSAGES) {
3058 pHba->post_fifo_size = MAX_TO_IOP_MESSAGES;
3061 pHba->reply_fifo_size = pHba->status_block->max_outbound_frames;
3062 if (pHba->reply_fifo_size > MAX_FROM_IOP_MESSAGES) {
3063 pHba->reply_fifo_size = MAX_FROM_IOP_MESSAGES;
3066 // Calculate the Scatter Gather list size
3067 if (dpt_dma64(pHba)) {
3068 pHba->sg_tablesize
3069 = ((pHba->status_block->inbound_frame_size * 4
3070 - 14 * sizeof(u32))
3071 / (sizeof(struct sg_simple_element) + sizeof(u32)));
3072 } else {
3073 pHba->sg_tablesize
3074 = ((pHba->status_block->inbound_frame_size * 4
3075 - 12 * sizeof(u32))
3076 / sizeof(struct sg_simple_element));
3078 if (pHba->sg_tablesize > SG_LIST_ELEMENTS) {
3079 pHba->sg_tablesize = SG_LIST_ELEMENTS;
3083 #ifdef DEBUG
3084 printk("dpti%d: State = ",pHba->unit);
3085 switch(pHba->status_block->iop_state) {
3086 case 0x01:
3087 printk("INIT\n");
3088 break;
3089 case 0x02:
3090 printk("RESET\n");
3091 break;
3092 case 0x04:
3093 printk("HOLD\n");
3094 break;
3095 case 0x05:
3096 printk("READY\n");
3097 break;
3098 case 0x08:
3099 printk("OPERATIONAL\n");
3100 break;
3101 case 0x10:
3102 printk("FAILED\n");
3103 break;
3104 case 0x11:
3105 printk("FAULTED\n");
3106 break;
3107 default:
3108 printk("%x (unknown!!)\n",pHba->status_block->iop_state);
3110 #endif
3111 return 0;
3115 * Get the IOP's Logical Configuration Table
3117 static int adpt_i2o_lct_get(adpt_hba* pHba)
3119 u32 msg[8];
3120 int ret;
3121 u32 buf[16];
3123 if ((pHba->lct_size == 0) || (pHba->lct == NULL)){
3124 pHba->lct_size = pHba->status_block->expected_lct_size;
3126 do {
3127 if (pHba->lct == NULL) {
3128 pHba->lct = dma_alloc_coherent(&pHba->pDev->dev,
3129 pHba->lct_size, &pHba->lct_pa,
3130 GFP_KERNEL);
3131 if(pHba->lct == NULL) {
3132 printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
3133 pHba->name);
3134 return -ENOMEM;
3137 memset(pHba->lct, 0, pHba->lct_size);
3139 msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
3140 msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
3141 msg[2] = 0;
3142 msg[3] = 0;
3143 msg[4] = 0xFFFFFFFF; /* All devices */
3144 msg[5] = 0x00000000; /* Report now */
3145 msg[6] = 0xD0000000|pHba->lct_size;
3146 msg[7] = (u32)pHba->lct_pa;
3148 if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 360))) {
3149 printk(KERN_ERR "%s: LCT Get failed (status=%#10x.\n",
3150 pHba->name, ret);
3151 printk(KERN_ERR"Adaptec: Error Reading Hardware.\n");
3152 return ret;
3155 if ((pHba->lct->table_size << 2) > pHba->lct_size) {
3156 pHba->lct_size = pHba->lct->table_size << 2;
3157 dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
3158 pHba->lct, pHba->lct_pa);
3159 pHba->lct = NULL;
3161 } while (pHba->lct == NULL);
3163 PDEBUG("%s: Hardware resource table read.\n", pHba->name);
3166 // I2O_DPT_EXEC_IOP_BUFFERS_GROUP_NO;
3167 if(adpt_i2o_query_scalar(pHba, 0 , 0x8000, -1, buf, sizeof(buf))>=0) {
3168 pHba->FwDebugBufferSize = buf[1];
3169 pHba->FwDebugBuffer_P = ioremap(pHba->base_addr_phys + buf[0],
3170 pHba->FwDebugBufferSize);
3171 if (pHba->FwDebugBuffer_P) {
3172 pHba->FwDebugFlags_P = pHba->FwDebugBuffer_P +
3173 FW_DEBUG_FLAGS_OFFSET;
3174 pHba->FwDebugBLEDvalue_P = pHba->FwDebugBuffer_P +
3175 FW_DEBUG_BLED_OFFSET;
3176 pHba->FwDebugBLEDflag_P = pHba->FwDebugBLEDvalue_P + 1;
3177 pHba->FwDebugStrLength_P = pHba->FwDebugBuffer_P +
3178 FW_DEBUG_STR_LENGTH_OFFSET;
3179 pHba->FwDebugBuffer_P += buf[2];
3180 pHba->FwDebugFlags = 0;
3184 return 0;
3187 static int adpt_i2o_build_sys_table(void)
3189 adpt_hba* pHba = hba_chain;
3190 int count = 0;
3192 if (sys_tbl)
3193 dma_free_coherent(&pHba->pDev->dev, sys_tbl_len,
3194 sys_tbl, sys_tbl_pa);
3196 sys_tbl_len = sizeof(struct i2o_sys_tbl) + // Header + IOPs
3197 (hba_count) * sizeof(struct i2o_sys_tbl_entry);
3199 sys_tbl = dma_alloc_coherent(&pHba->pDev->dev,
3200 sys_tbl_len, &sys_tbl_pa, GFP_KERNEL);
3201 if (!sys_tbl) {
3202 printk(KERN_WARNING "SysTab Set failed. Out of memory.\n");
3203 return -ENOMEM;
3205 memset(sys_tbl, 0, sys_tbl_len);
3207 sys_tbl->num_entries = hba_count;
3208 sys_tbl->version = I2OVERSION;
3209 sys_tbl->change_ind = sys_tbl_ind++;
3211 for(pHba = hba_chain; pHba; pHba = pHba->next) {
3212 u64 addr;
3213 // Get updated Status Block so we have the latest information
3214 if (adpt_i2o_status_get(pHba)) {
3215 sys_tbl->num_entries--;
3216 continue; // try next one
3219 sys_tbl->iops[count].org_id = pHba->status_block->org_id;
3220 sys_tbl->iops[count].iop_id = pHba->unit + 2;
3221 sys_tbl->iops[count].seg_num = 0;
3222 sys_tbl->iops[count].i2o_version = pHba->status_block->i2o_version;
3223 sys_tbl->iops[count].iop_state = pHba->status_block->iop_state;
3224 sys_tbl->iops[count].msg_type = pHba->status_block->msg_type;
3225 sys_tbl->iops[count].frame_size = pHba->status_block->inbound_frame_size;
3226 sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
3227 sys_tbl->iops[count].iop_capabilities = pHba->status_block->iop_capabilities;
3228 addr = pHba->base_addr_phys + 0x40;
3229 sys_tbl->iops[count].inbound_low = dma_low(addr);
3230 sys_tbl->iops[count].inbound_high = dma_high(addr);
3232 count++;
3235 #ifdef DEBUG
3237 u32 *table = (u32*)sys_tbl;
3238 printk(KERN_DEBUG"sys_tbl_len=%d in 32bit words\n",(sys_tbl_len >>2));
3239 for(count = 0; count < (sys_tbl_len >>2); count++) {
3240 printk(KERN_INFO "sys_tbl[%d] = %0#10x\n",
3241 count, table[count]);
3244 #endif
3246 return 0;
3251 * Dump the information block associated with a given unit (TID)
3254 static void adpt_i2o_report_hba_unit(adpt_hba* pHba, struct i2o_device *d)
3256 char buf[64];
3257 int unit = d->lct_data.tid;
3259 printk(KERN_INFO "TID %3.3d ", unit);
3261 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 3, buf, 16)>=0)
3263 buf[16]=0;
3264 printk(" Vendor: %-12.12s", buf);
3266 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 4, buf, 16)>=0)
3268 buf[16]=0;
3269 printk(" Device: %-12.12s", buf);
3271 if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 6, buf, 8)>=0)
3273 buf[8]=0;
3274 printk(" Rev: %-12.12s\n", buf);
3276 #ifdef DEBUG
3277 printk(KERN_INFO "\tClass: %.21s\n", adpt_i2o_get_class_name(d->lct_data.class_id));
3278 printk(KERN_INFO "\tSubclass: 0x%04X\n", d->lct_data.sub_class);
3279 printk(KERN_INFO "\tFlags: ");
3281 if(d->lct_data.device_flags&(1<<0))
3282 printk("C"); // ConfigDialog requested
3283 if(d->lct_data.device_flags&(1<<1))
3284 printk("U"); // Multi-user capable
3285 if(!(d->lct_data.device_flags&(1<<4)))
3286 printk("P"); // Peer service enabled!
3287 if(!(d->lct_data.device_flags&(1<<5)))
3288 printk("M"); // Mgmt service enabled!
3289 printk("\n");
3290 #endif
3293 #ifdef DEBUG
3295 * Do i2o class name lookup
3297 static const char *adpt_i2o_get_class_name(int class)
3299 int idx = 16;
3300 static char *i2o_class_name[] = {
3301 "Executive",
3302 "Device Driver Module",
3303 "Block Device",
3304 "Tape Device",
3305 "LAN Interface",
3306 "WAN Interface",
3307 "Fibre Channel Port",
3308 "Fibre Channel Device",
3309 "SCSI Device",
3310 "ATE Port",
3311 "ATE Device",
3312 "Floppy Controller",
3313 "Floppy Device",
3314 "Secondary Bus Port",
3315 "Peer Transport Agent",
3316 "Peer Transport",
3317 "Unknown"
3320 switch(class&0xFFF) {
3321 case I2O_CLASS_EXECUTIVE:
3322 idx = 0; break;
3323 case I2O_CLASS_DDM:
3324 idx = 1; break;
3325 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
3326 idx = 2; break;
3327 case I2O_CLASS_SEQUENTIAL_STORAGE:
3328 idx = 3; break;
3329 case I2O_CLASS_LAN:
3330 idx = 4; break;
3331 case I2O_CLASS_WAN:
3332 idx = 5; break;
3333 case I2O_CLASS_FIBRE_CHANNEL_PORT:
3334 idx = 6; break;
3335 case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
3336 idx = 7; break;
3337 case I2O_CLASS_SCSI_PERIPHERAL:
3338 idx = 8; break;
3339 case I2O_CLASS_ATE_PORT:
3340 idx = 9; break;
3341 case I2O_CLASS_ATE_PERIPHERAL:
3342 idx = 10; break;
3343 case I2O_CLASS_FLOPPY_CONTROLLER:
3344 idx = 11; break;
3345 case I2O_CLASS_FLOPPY_DEVICE:
3346 idx = 12; break;
3347 case I2O_CLASS_BUS_ADAPTER_PORT:
3348 idx = 13; break;
3349 case I2O_CLASS_PEER_TRANSPORT_AGENT:
3350 idx = 14; break;
3351 case I2O_CLASS_PEER_TRANSPORT:
3352 idx = 15; break;
3354 return i2o_class_name[idx];
3356 #endif
3359 static s32 adpt_i2o_hrt_get(adpt_hba* pHba)
3361 u32 msg[6];
3362 int ret, size = sizeof(i2o_hrt);
3364 do {
3365 if (pHba->hrt == NULL) {
3366 pHba->hrt = dma_alloc_coherent(&pHba->pDev->dev,
3367 size, &pHba->hrt_pa, GFP_KERNEL);
3368 if (pHba->hrt == NULL) {
3369 printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", pHba->name);
3370 return -ENOMEM;
3374 msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
3375 msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
3376 msg[2]= 0;
3377 msg[3]= 0;
3378 msg[4]= (0xD0000000 | size); /* Simple transaction */
3379 msg[5]= (u32)pHba->hrt_pa; /* Dump it here */
3381 if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg),20))) {
3382 printk(KERN_ERR "%s: Unable to get HRT (status=%#10x)\n", pHba->name, ret);
3383 return ret;
3386 if (pHba->hrt->num_entries * pHba->hrt->entry_len << 2 > size) {
3387 int newsize = pHba->hrt->num_entries * pHba->hrt->entry_len << 2;
3388 dma_free_coherent(&pHba->pDev->dev, size,
3389 pHba->hrt, pHba->hrt_pa);
3390 size = newsize;
3391 pHba->hrt = NULL;
3393 } while(pHba->hrt == NULL);
3394 return 0;
3398 * Query one scalar group value or a whole scalar group.
3400 static int adpt_i2o_query_scalar(adpt_hba* pHba, int tid,
3401 int group, int field, void *buf, int buflen)
3403 u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
3404 u8 *opblk_va;
3405 dma_addr_t opblk_pa;
3406 u8 *resblk_va;
3407 dma_addr_t resblk_pa;
3409 int size;
3411 /* 8 bytes for header */
3412 resblk_va = dma_alloc_coherent(&pHba->pDev->dev,
3413 sizeof(u8) * (8 + buflen), &resblk_pa, GFP_KERNEL);
3414 if (resblk_va == NULL) {
3415 printk(KERN_CRIT "%s: query scalar failed; Out of memory.\n", pHba->name);
3416 return -ENOMEM;
3419 opblk_va = dma_alloc_coherent(&pHba->pDev->dev,
3420 sizeof(opblk), &opblk_pa, GFP_KERNEL);
3421 if (opblk_va == NULL) {
3422 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3423 resblk_va, resblk_pa);
3424 printk(KERN_CRIT "%s: query operatio failed; Out of memory.\n",
3425 pHba->name);
3426 return -ENOMEM;
3428 if (field == -1) /* whole group */
3429 opblk[4] = -1;
3431 memcpy(opblk_va, opblk, sizeof(opblk));
3432 size = adpt_i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, pHba, tid,
3433 opblk_va, opblk_pa, sizeof(opblk),
3434 resblk_va, resblk_pa, sizeof(u8)*(8+buflen));
3435 dma_free_coherent(&pHba->pDev->dev, sizeof(opblk), opblk_va, opblk_pa);
3436 if (size == -ETIME) {
3437 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3438 resblk_va, resblk_pa);
3439 printk(KERN_WARNING "%s: issue params failed; Timed out.\n", pHba->name);
3440 return -ETIME;
3441 } else if (size == -EINTR) {
3442 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3443 resblk_va, resblk_pa);
3444 printk(KERN_WARNING "%s: issue params failed; Interrupted.\n", pHba->name);
3445 return -EINTR;
3448 memcpy(buf, resblk_va+8, buflen); /* cut off header */
3450 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3451 resblk_va, resblk_pa);
3452 if (size < 0)
3453 return size;
3455 return buflen;
3459 /* Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
3461 * This function can be used for all UtilParamsGet/Set operations.
3462 * The OperationBlock is given in opblk-buffer,
3463 * and results are returned in resblk-buffer.
3464 * Note that the minimum sized resblk is 8 bytes and contains
3465 * ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
3467 static int adpt_i2o_issue_params(int cmd, adpt_hba* pHba, int tid,
3468 void *opblk_va, dma_addr_t opblk_pa, int oplen,
3469 void *resblk_va, dma_addr_t resblk_pa, int reslen)
3471 u32 msg[9];
3472 u32 *res = (u32 *)resblk_va;
3473 int wait_status;
3475 msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
3476 msg[1] = cmd << 24 | HOST_TID << 12 | tid;
3477 msg[2] = 0;
3478 msg[3] = 0;
3479 msg[4] = 0;
3480 msg[5] = 0x54000000 | oplen; /* OperationBlock */
3481 msg[6] = (u32)opblk_pa;
3482 msg[7] = 0xD0000000 | reslen; /* ResultBlock */
3483 msg[8] = (u32)resblk_pa;
3485 if ((wait_status = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 20))) {
3486 printk("adpt_i2o_issue_params: post_wait failed (%p)\n", resblk_va);
3487 return wait_status; /* -DetailedStatus */
3490 if (res[1]&0x00FF0000) { /* BlockStatus != SUCCESS */
3491 printk(KERN_WARNING "%s: %s - Error:\n ErrorInfoSize = 0x%02x, "
3492 "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
3493 pHba->name,
3494 (cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
3495 : "PARAMS_GET",
3496 res[1]>>24, (res[1]>>16)&0xFF, res[1]&0xFFFF);
3497 return -((res[1] >> 16) & 0xFF); /* -BlockStatus */
3500 return 4 + ((res[1] & 0x0000FFFF) << 2); /* bytes used in resblk */
3504 static s32 adpt_i2o_quiesce_hba(adpt_hba* pHba)
3506 u32 msg[4];
3507 int ret;
3509 adpt_i2o_status_get(pHba);
3511 /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
3513 if((pHba->status_block->iop_state != ADAPTER_STATE_READY) &&
3514 (pHba->status_block->iop_state != ADAPTER_STATE_OPERATIONAL)){
3515 return 0;
3518 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3519 msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
3520 msg[2] = 0;
3521 msg[3] = 0;
3523 if((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3524 printk(KERN_INFO"dpti%d: Unable to quiesce (status=%#x).\n",
3525 pHba->unit, -ret);
3526 } else {
3527 printk(KERN_INFO"dpti%d: Quiesced.\n",pHba->unit);
3530 adpt_i2o_status_get(pHba);
3531 return ret;
3536 * Enable IOP. Allows the IOP to resume external operations.
3538 static int adpt_i2o_enable_hba(adpt_hba* pHba)
3540 u32 msg[4];
3541 int ret;
3543 adpt_i2o_status_get(pHba);
3544 if(!pHba->status_block){
3545 return -ENOMEM;
3547 /* Enable only allowed on READY state */
3548 if(pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
3549 return 0;
3551 if(pHba->status_block->iop_state != ADAPTER_STATE_READY)
3552 return -EINVAL;
3554 msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3555 msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
3556 msg[2]= 0;
3557 msg[3]= 0;
3559 if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3560 printk(KERN_WARNING"%s: Could not enable (status=%#10x).\n",
3561 pHba->name, ret);
3562 } else {
3563 PDEBUG("%s: Enabled.\n", pHba->name);
3566 adpt_i2o_status_get(pHba);
3567 return ret;
3571 static int adpt_i2o_systab_send(adpt_hba* pHba)
3573 u32 msg[12];
3574 int ret;
3576 msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
3577 msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
3578 msg[2] = 0;
3579 msg[3] = 0;
3580 msg[4] = (0<<16) | ((pHba->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
3581 msg[5] = 0; /* Segment 0 */
3584 * Provide three SGL-elements:
3585 * System table (SysTab), Private memory space declaration and
3586 * Private i/o space declaration
3588 msg[6] = 0x54000000 | sys_tbl_len;
3589 msg[7] = (u32)sys_tbl_pa;
3590 msg[8] = 0x54000000 | 0;
3591 msg[9] = 0;
3592 msg[10] = 0xD4000000 | 0;
3593 msg[11] = 0;
3595 if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 120))) {
3596 printk(KERN_INFO "%s: Unable to set SysTab (status=%#10x).\n",
3597 pHba->name, ret);
3599 #ifdef DEBUG
3600 else {
3601 PINFO("%s: SysTab set.\n", pHba->name);
3603 #endif
3605 return ret;
3609 /*============================================================================
3611 *============================================================================
3615 #ifdef UARTDELAY
3617 static static void adpt_delay(int millisec)
3619 int i;
3620 for (i = 0; i < millisec; i++) {
3621 udelay(1000); /* delay for one millisecond */
3625 #endif
3627 static struct scsi_host_template driver_template = {
3628 .module = THIS_MODULE,
3629 .name = "dpt_i2o",
3630 .proc_name = "dpt_i2o",
3631 .proc_info = adpt_proc_info,
3632 .info = adpt_info,
3633 .queuecommand = adpt_queue,
3634 .eh_abort_handler = adpt_abort,
3635 .eh_device_reset_handler = adpt_device_reset,
3636 .eh_bus_reset_handler = adpt_bus_reset,
3637 .eh_host_reset_handler = adpt_reset,
3638 .bios_param = adpt_bios_param,
3639 .slave_configure = adpt_slave_configure,
3640 .can_queue = MAX_TO_IOP_MESSAGES,
3641 .this_id = 7,
3642 .cmd_per_lun = 1,
3643 .use_clustering = ENABLE_CLUSTERING,
3646 static int __init adpt_init(void)
3648 int error;
3649 adpt_hba *pHba, *next;
3651 printk("Loading Adaptec I2O RAID: Version " DPT_I2O_VERSION "\n");
3653 error = adpt_detect(&driver_template);
3654 if (error < 0)
3655 return error;
3656 if (hba_chain == NULL)
3657 return -ENODEV;
3659 for (pHba = hba_chain; pHba; pHba = pHba->next) {
3660 error = scsi_add_host(pHba->host, &pHba->pDev->dev);
3661 if (error)
3662 goto fail;
3663 scsi_scan_host(pHba->host);
3665 return 0;
3666 fail:
3667 for (pHba = hba_chain; pHba; pHba = next) {
3668 next = pHba->next;
3669 scsi_remove_host(pHba->host);
3671 return error;
3674 static void __exit adpt_exit(void)
3676 adpt_hba *pHba, *next;
3678 for (pHba = hba_chain; pHba; pHba = pHba->next)
3679 scsi_remove_host(pHba->host);
3680 for (pHba = hba_chain; pHba; pHba = next) {
3681 next = pHba->next;
3682 adpt_release(pHba->host);
3686 module_init(adpt_init);
3687 module_exit(adpt_exit);
3689 MODULE_LICENSE("GPL");