GNU Linux-libre 4.19.295-gnu1
[releases.git] / drivers / scsi / dpt_i2o.c
1 /***************************************************************************
2                           dpti.c  -  description
3                              -------------------
4     begin                : Thu Sep 7 2000
5     copyright            : (C) 2000 by Adaptec
6
7                            July 30, 2001 First version being submitted
8                            for inclusion in the kernel.  V2.4
9
10     See Documentation/scsi/dpti.txt for history, notes, license info
11     and credits
12  ***************************************************************************/
13
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  **************************************************************************/
28
29 /*#define DEBUG 1 */
30 /*#define UARTDELAY 1 */
31
32 #include <linux/module.h>
33
34 MODULE_AUTHOR("Deanna Bonds, with _lots_ of help from Mark Salyzyn");
35 MODULE_DESCRIPTION("Adaptec I2O RAID Driver");
36
37 ////////////////////////////////////////////////////////////////
38
39 #include <linux/ioctl.h>        /* For SCSI-Passthrough */
40 #include <linux/uaccess.h>
41
42 #include <linux/stat.h>
43 #include <linux/slab.h>         /* for kmalloc() */
44 #include <linux/pci.h>          /* for PCI support */
45 #include <linux/proc_fs.h>
46 #include <linux/blkdev.h>
47 #include <linux/delay.h>        /* for udelay */
48 #include <linux/interrupt.h>
49 #include <linux/kernel.h>       /* for printk */
50 #include <linux/sched.h>
51 #include <linux/reboot.h>
52 #include <linux/spinlock.h>
53 #include <linux/dma-mapping.h>
54
55 #include <linux/timer.h>
56 #include <linux/string.h>
57 #include <linux/ioport.h>
58 #include <linux/mutex.h>
59
60 #include <asm/processor.h>      /* for boot_cpu_data */
61 #include <asm/pgtable.h>
62 #include <asm/io.h>
63
64 #include <scsi/scsi.h>
65 #include <scsi/scsi_cmnd.h>
66 #include <scsi/scsi_device.h>
67 #include <scsi/scsi_host.h>
68 #include <scsi/scsi_tcq.h>
69
70 #include "dpt/dptsig.h"
71 #include "dpti.h"
72
73 /*============================================================================
74  * Create a binary signature - this is read by dptsig
75  * Needed for our management apps
76  *============================================================================
77  */
78 static DEFINE_MUTEX(adpt_mutex);
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"
95 };
96
97
98
99
100 /*============================================================================
101  * Globals
102  *============================================================================
103  */
104
105 static DEFINE_MUTEX(adpt_configuration_lock);
106
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;
111
112 static adpt_hba* hba_chain = NULL;
113 static int hba_count = 0;
114
115 static struct class *adpt_sysfs_class;
116
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
121
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
129         .llseek         = noop_llseek,
130 };
131
132 /* Structures and definitions for synchronous message posting.
133  * See adpt_i2o_post_wait() for description
134  * */
135 struct adpt_i2o_post_wait_data
136 {
137         int status;
138         u32 id;
139         adpt_wait_queue_head_t *wq;
140         struct adpt_i2o_post_wait_data *next;
141 };
142
143 static struct adpt_i2o_post_wait_data *adpt_post_wait_queue = NULL;
144 static u32 adpt_post_wait_id = 0;
145 static DEFINE_SPINLOCK(adpt_post_wait_lock);
146
147
148 /*============================================================================
149  *                              Functions
150  *============================================================================
151  */
152
153 static inline int dpt_dma64(adpt_hba *pHba)
154 {
155         return (sizeof(dma_addr_t) > 4 && (pHba)->dma64);
156 }
157
158 static inline u32 dma_high(dma_addr_t addr)
159 {
160         return upper_32_bits(addr);
161 }
162
163 static inline u32 dma_low(dma_addr_t addr)
164 {
165         return (u32)addr;
166 }
167
168 static u8 adpt_read_blink_led(adpt_hba* host)
169 {
170         if (host->FwDebugBLEDflag_P) {
171                 if( readb(host->FwDebugBLEDflag_P) == 0xbc ){
172                         return readb(host->FwDebugBLEDvalue_P);
173                 }
174         }
175         return 0;
176 }
177
178 /*============================================================================
179  * Scsi host template interface functions
180  *============================================================================
181  */
182
183 #ifdef MODULE
184 static struct pci_device_id dptids[] = {
185         { PCI_DPT_VENDOR_ID, PCI_DPT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
186         { PCI_DPT_VENDOR_ID, PCI_DPT_RAPTOR_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
187         { 0, }
188 };
189 #endif
190
191 MODULE_DEVICE_TABLE(pci,dptids);
192
193 static int adpt_detect(struct scsi_host_template* sht)
194 {
195         struct pci_dev *pDev = NULL;
196         adpt_hba *pHba;
197         adpt_hba *next;
198
199         PINFO("Detecting Adaptec I2O RAID controllers...\n");
200
201         /* search for all Adatpec I2O RAID cards */
202         while ((pDev = pci_get_device( PCI_DPT_VENDOR_ID, PCI_ANY_ID, pDev))) {
203                 if(pDev->device == PCI_DPT_DEVICE_ID ||
204                    pDev->device == PCI_DPT_RAPTOR_DEVICE_ID){
205                         if(adpt_install_hba(sht, pDev) ){
206                                 PERROR("Could not Init an I2O RAID device\n");
207                                 PERROR("Will not try to detect others.\n");
208                                 return hba_count-1;
209                         }
210                         pci_dev_get(pDev);
211                 }
212         }
213
214         /* In INIT state, Activate IOPs */
215         for (pHba = hba_chain; pHba; pHba = next) {
216                 next = pHba->next;
217                 // Activate does get status , init outbound, and get hrt
218                 if (adpt_i2o_activate_hba(pHba) < 0) {
219                         adpt_i2o_delete_hba(pHba);
220                 }
221         }
222
223
224         /* Active IOPs in HOLD state */
225
226 rebuild_sys_tab:
227         if (hba_chain == NULL) 
228                 return 0;
229
230         /*
231          * If build_sys_table fails, we kill everything and bail
232          * as we can't init the IOPs w/o a system table
233          */     
234         if (adpt_i2o_build_sys_table() < 0) {
235                 adpt_i2o_sys_shutdown();
236                 return 0;
237         }
238
239         PDEBUG("HBA's in HOLD state\n");
240
241         /* If IOP don't get online, we need to rebuild the System table */
242         for (pHba = hba_chain; pHba; pHba = pHba->next) {
243                 if (adpt_i2o_online_hba(pHba) < 0) {
244                         adpt_i2o_delete_hba(pHba);      
245                         goto rebuild_sys_tab;
246                 }
247         }
248
249         /* Active IOPs now in OPERATIONAL state */
250         PDEBUG("HBA's in OPERATIONAL state\n");
251
252         printk("dpti: If you have a lot of devices this could take a few minutes.\n");
253         for (pHba = hba_chain; pHba; pHba = next) {
254                 next = pHba->next;
255                 printk(KERN_INFO"%s: Reading the hardware resource table.\n", pHba->name);
256                 if (adpt_i2o_lct_get(pHba) < 0){
257                         adpt_i2o_delete_hba(pHba);
258                         continue;
259                 }
260
261                 if (adpt_i2o_parse_lct(pHba) < 0){
262                         adpt_i2o_delete_hba(pHba);
263                         continue;
264                 }
265                 adpt_inquiry(pHba);
266         }
267
268         adpt_sysfs_class = class_create(THIS_MODULE, "dpt_i2o");
269         if (IS_ERR(adpt_sysfs_class)) {
270                 printk(KERN_WARNING"dpti: unable to create dpt_i2o class\n");
271                 adpt_sysfs_class = NULL;
272         }
273
274         for (pHba = hba_chain; pHba; pHba = next) {
275                 next = pHba->next;
276                 if (adpt_scsi_host_alloc(pHba, sht) < 0){
277                         adpt_i2o_delete_hba(pHba);
278                         continue;
279                 }
280                 pHba->initialized = TRUE;
281                 pHba->state &= ~DPTI_STATE_RESET;
282                 if (adpt_sysfs_class) {
283                         struct device *dev = device_create(adpt_sysfs_class,
284                                 NULL, MKDEV(DPTI_I2O_MAJOR, pHba->unit), NULL,
285                                 "dpti%d", pHba->unit);
286                         if (IS_ERR(dev)) {
287                                 printk(KERN_WARNING"dpti%d: unable to "
288                                         "create device in dpt_i2o class\n",
289                                         pHba->unit);
290                         }
291                 }
292         }
293
294         // Register our control device node
295         // nodes will need to be created in /dev to access this
296         // the nodes can not be created from within the driver
297         if (hba_count && register_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER, &adpt_fops)) {
298                 adpt_i2o_sys_shutdown();
299                 return 0;
300         }
301         return hba_count;
302 }
303
304
305 static void adpt_release(adpt_hba *pHba)
306 {
307         struct Scsi_Host *shost = pHba->host;
308
309         scsi_remove_host(shost);
310 //      adpt_i2o_quiesce_hba(pHba);
311         adpt_i2o_delete_hba(pHba);
312         scsi_host_put(shost);
313 }
314
315
316 static void adpt_inquiry(adpt_hba* pHba)
317 {
318         u32 msg[17]; 
319         u32 *mptr;
320         u32 *lenptr;
321         int direction;
322         int scsidir;
323         u32 len;
324         u32 reqlen;
325         u8* buf;
326         dma_addr_t addr;
327         u8  scb[16];
328         s32 rcode;
329
330         memset(msg, 0, sizeof(msg));
331         buf = dma_alloc_coherent(&pHba->pDev->dev, 80, &addr, GFP_KERNEL);
332         if(!buf){
333                 printk(KERN_ERR"%s: Could not allocate buffer\n",pHba->name);
334                 return;
335         }
336         memset((void*)buf, 0, 36);
337         
338         len = 36;
339         direction = 0x00000000; 
340         scsidir  =0x40000000;   // DATA IN  (iop<--dev)
341
342         if (dpt_dma64(pHba))
343                 reqlen = 17;            // SINGLE SGE, 64 bit
344         else
345                 reqlen = 14;            // SINGLE SGE, 32 bit
346         /* Stick the headers on */
347         msg[0] = reqlen<<16 | SGL_OFFSET_12;
348         msg[1] = (0xff<<24|HOST_TID<<12|ADAPTER_TID);
349         msg[2] = 0;
350         msg[3]  = 0;
351         // Adaptec/DPT Private stuff 
352         msg[4] = I2O_CMD_SCSI_EXEC|DPT_ORGANIZATION_ID<<16;
353         msg[5] = ADAPTER_TID | 1<<16 /* Interpret*/;
354         /* Direction, disconnect ok | sense data | simple queue , CDBLen */
355         // I2O_SCB_FLAG_ENABLE_DISCONNECT | 
356         // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG | 
357         // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
358         msg[6] = scsidir|0x20a00000| 6 /* cmd len*/;
359
360         mptr=msg+7;
361
362         memset(scb, 0, sizeof(scb));
363         // Write SCSI command into the message - always 16 byte block 
364         scb[0] = INQUIRY;
365         scb[1] = 0;
366         scb[2] = 0;
367         scb[3] = 0;
368         scb[4] = 36;
369         scb[5] = 0;
370         // Don't care about the rest of scb
371
372         memcpy(mptr, scb, sizeof(scb));
373         mptr+=4;
374         lenptr=mptr++;          /* Remember me - fill in when we know */
375
376         /* Now fill in the SGList and command */
377         *lenptr = len;
378         if (dpt_dma64(pHba)) {
379                 *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
380                 *mptr++ = 1 << PAGE_SHIFT;
381                 *mptr++ = 0xD0000000|direction|len;
382                 *mptr++ = dma_low(addr);
383                 *mptr++ = dma_high(addr);
384         } else {
385                 *mptr++ = 0xD0000000|direction|len;
386                 *mptr++ = addr;
387         }
388
389         // Send it on it's way
390         rcode = adpt_i2o_post_wait(pHba, msg, reqlen<<2, 120);
391         if (rcode != 0) {
392                 sprintf(pHba->detail, "Adaptec I2O RAID");
393                 printk(KERN_INFO "%s: Inquiry Error (%d)\n",pHba->name,rcode);
394                 if (rcode != -ETIME && rcode != -EINTR)
395                         dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
396         } else {
397                 memset(pHba->detail, 0, sizeof(pHba->detail));
398                 memcpy(&(pHba->detail), "Vendor: Adaptec ", 16);
399                 memcpy(&(pHba->detail[16]), " Model: ", 8);
400                 memcpy(&(pHba->detail[24]), (u8*) &buf[16], 16);
401                 memcpy(&(pHba->detail[40]), " FW: ", 4);
402                 memcpy(&(pHba->detail[44]), (u8*) &buf[32], 4);
403                 pHba->detail[48] = '\0';        /* precautionary */
404                 dma_free_coherent(&pHba->pDev->dev, 80, buf, addr);
405         }
406         adpt_i2o_status_get(pHba);
407         return ;
408 }
409
410
411 static int adpt_slave_configure(struct scsi_device * device)
412 {
413         struct Scsi_Host *host = device->host;
414         adpt_hba* pHba;
415
416         pHba = (adpt_hba *) host->hostdata[0];
417
418         if (host->can_queue && device->tagged_supported) {
419                 scsi_change_queue_depth(device,
420                                 host->can_queue - 1);
421         }
422         return 0;
423 }
424
425 static int adpt_queue_lck(struct scsi_cmnd * cmd, void (*done) (struct scsi_cmnd *))
426 {
427         adpt_hba* pHba = NULL;
428         struct adpt_device* pDev = NULL;        /* dpt per device information */
429
430         cmd->scsi_done = done;
431         /*
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.
436          */
437
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;
442         }
443
444         pHba = (adpt_hba*)cmd->device->host->hostdata[0];
445         if (!pHba) {
446                 return FAILED;
447         }
448
449         rmb();
450         if ((pHba->state) & DPTI_STATE_RESET)
451                 return SCSI_MLQUEUE_HOST_BUSY;
452
453         // TODO if the cmd->device if offline then I may need to issue a bus rescan
454         // followed by a get_lct to see if the device is there anymore
455         if((pDev = (struct adpt_device*) (cmd->device->hostdata)) == NULL) {
456                 /*
457                  * First command request for this device.  Set up a pointer
458                  * to the device structure.  This should be a TEST_UNIT_READY
459                  * command from scan_scsis_single.
460                  */
461                 if ((pDev = adpt_find_device(pHba, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun)) == NULL) {
462                         // TODO: if any luns are at this bus, scsi id then fake a TEST_UNIT_READY and INQUIRY response 
463                         // with type 7F (for all luns less than the max for this bus,id) so the lun scan will continue.
464                         cmd->result = (DID_NO_CONNECT << 16);
465                         cmd->scsi_done(cmd);
466                         return 0;
467                 }
468                 cmd->device->hostdata = pDev;
469         }
470         pDev->pScsi_dev = cmd->device;
471
472         /*
473          * If we are being called from when the device is being reset, 
474          * delay processing of the command until later.
475          */
476         if (pDev->state & DPTI_DEV_RESET ) {
477                 return FAILED;
478         }
479         return adpt_scsi_to_i2o(pHba, cmd, pDev);
480 }
481
482 static DEF_SCSI_QCMD(adpt_queue)
483
484 static int adpt_bios_param(struct scsi_device *sdev, struct block_device *dev,
485                 sector_t capacity, int geom[])
486 {
487         int heads=-1;
488         int sectors=-1;
489         int cylinders=-1;
490
491         // *** First lets set the default geometry ****
492         
493         // If the capacity is less than ox2000
494         if (capacity < 0x2000 ) {       // floppy
495                 heads = 18;
496                 sectors = 2;
497         } 
498         // else if between 0x2000 and 0x20000
499         else if (capacity < 0x20000) {
500                 heads = 64;
501                 sectors = 32;
502         }
503         // else if between 0x20000 and 0x40000
504         else if (capacity < 0x40000) {
505                 heads = 65;
506                 sectors = 63;
507         }
508         // else if between 0x4000 and 0x80000
509         else if (capacity < 0x80000) {
510                 heads = 128;
511                 sectors = 63;
512         }
513         // else if greater than 0x80000
514         else {
515                 heads = 255;
516                 sectors = 63;
517         }
518         cylinders = sector_div(capacity, heads * sectors);
519
520         // Special case if CDROM
521         if(sdev->type == 5) {  // CDROM
522                 heads = 252;
523                 sectors = 63;
524                 cylinders = 1111;
525         }
526
527         geom[0] = heads;
528         geom[1] = sectors;
529         geom[2] = cylinders;
530         
531         PDEBUG("adpt_bios_param: exit\n");
532         return 0;
533 }
534
535
536 static const char *adpt_info(struct Scsi_Host *host)
537 {
538         adpt_hba* pHba;
539
540         pHba = (adpt_hba *) host->hostdata[0];
541         return (char *) (pHba->detail);
542 }
543
544 static int adpt_show_info(struct seq_file *m, struct Scsi_Host *host)
545 {
546         struct adpt_device* d;
547         int id;
548         int chan;
549         adpt_hba* pHba;
550         int unit;
551
552         // Find HBA (host bus adapter) we are looking for
553         mutex_lock(&adpt_configuration_lock);
554         for (pHba = hba_chain; pHba; pHba = pHba->next) {
555                 if (pHba->host == host) {
556                         break;  /* found adapter */
557                 }
558         }
559         mutex_unlock(&adpt_configuration_lock);
560         if (pHba == NULL) {
561                 return 0;
562         }
563         host = pHba->host;
564
565         seq_printf(m, "Adaptec I2O RAID Driver Version: %s\n\n", DPT_I2O_VERSION);
566         seq_printf(m, "%s\n", pHba->detail);
567         seq_printf(m, "SCSI Host=scsi%d  Control Node=/dev/%s  irq=%d\n", 
568                         pHba->host->host_no, pHba->name, host->irq);
569         seq_printf(m, "\tpost fifo size  = %d\n\treply fifo size = %d\n\tsg table size   = %d\n\n",
570                         host->can_queue, (int) pHba->reply_fifo_size , host->sg_tablesize);
571
572         seq_puts(m, "Devices:\n");
573         for(chan = 0; chan < MAX_CHANNEL; chan++) {
574                 for(id = 0; id < MAX_ID; id++) {
575                         d = pHba->channel[chan].device[id];
576                         while(d) {
577                                 seq_printf(m,"\t%-24.24s", d->pScsi_dev->vendor);
578                                 seq_printf(m," Rev: %-8.8s\n", d->pScsi_dev->rev);
579
580                                 unit = d->pI2o_dev->lct_data.tid;
581                                 seq_printf(m, "\tTID=%d, (Channel=%d, Target=%d, Lun=%llu)  (%s)\n\n",
582                                                unit, (int)d->scsi_channel, (int)d->scsi_id, d->scsi_lun,
583                                                scsi_device_online(d->pScsi_dev)? "online":"offline"); 
584                                 d = d->next_lun;
585                         }
586                 }
587         }
588         return 0;
589 }
590
591 /*
592  *      Turn a struct scsi_cmnd * into a unique 32 bit 'context'.
593  */
594 static u32 adpt_cmd_to_context(struct scsi_cmnd *cmd)
595 {
596         return (u32)cmd->serial_number;
597 }
598
599 /*
600  *      Go from a u32 'context' to a struct scsi_cmnd * .
601  *      This could probably be made more efficient.
602  */
603 static struct scsi_cmnd *
604         adpt_cmd_from_context(adpt_hba * pHba, u32 context)
605 {
606         struct scsi_cmnd * cmd;
607         struct scsi_device * d;
608
609         if (context == 0)
610                 return NULL;
611
612         spin_unlock(pHba->host->host_lock);
613         shost_for_each_device(d, pHba->host) {
614                 unsigned long flags;
615                 spin_lock_irqsave(&d->list_lock, flags);
616                 list_for_each_entry(cmd, &d->cmd_list, list) {
617                         if (((u32)cmd->serial_number == context)) {
618                                 spin_unlock_irqrestore(&d->list_lock, flags);
619                                 scsi_device_put(d);
620                                 spin_lock(pHba->host->host_lock);
621                                 return cmd;
622                         }
623                 }
624                 spin_unlock_irqrestore(&d->list_lock, flags);
625         }
626         spin_lock(pHba->host->host_lock);
627
628         return NULL;
629 }
630
631 /*===========================================================================
632  * Error Handling routines
633  *===========================================================================
634  */
635
636 static int adpt_abort(struct scsi_cmnd * cmd)
637 {
638         adpt_hba* pHba = NULL;  /* host bus adapter structure */
639         struct adpt_device* dptdevice;  /* dpt per device information */
640         u32 msg[5];
641         int rcode;
642
643         if(cmd->serial_number == 0){
644                 return FAILED;
645         }
646         pHba = (adpt_hba*) cmd->device->host->hostdata[0];
647         printk(KERN_INFO"%s: Trying to Abort\n",pHba->name);
648         if ((dptdevice = (void*) (cmd->device->hostdata)) == NULL) {
649                 printk(KERN_ERR "%s: Unable to abort: No device in cmnd\n",pHba->name);
650                 return FAILED;
651         }
652
653         memset(msg, 0, sizeof(msg));
654         msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
655         msg[1] = I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|dptdevice->tid;
656         msg[2] = 0;
657         msg[3]= 0; 
658         msg[4] = adpt_cmd_to_context(cmd);
659         if (pHba->host)
660                 spin_lock_irq(pHba->host->host_lock);
661         rcode = adpt_i2o_post_wait(pHba, msg, sizeof(msg), FOREVER);
662         if (pHba->host)
663                 spin_unlock_irq(pHba->host->host_lock);
664         if (rcode != 0) {
665                 if(rcode == -EOPNOTSUPP ){
666                         printk(KERN_INFO"%s: Abort cmd not supported\n",pHba->name);
667                         return FAILED;
668                 }
669                 printk(KERN_INFO"%s: Abort failed.\n",pHba->name);
670                 return FAILED;
671         } 
672         printk(KERN_INFO"%s: Abort complete.\n",pHba->name);
673         return SUCCESS;
674 }
675
676
677 #define I2O_DEVICE_RESET 0x27
678 // This is the same for BLK and SCSI devices
679 // NOTE this is wrong in the i2o.h definitions
680 // This is not currently supported by our adapter but we issue it anyway
681 static int adpt_device_reset(struct scsi_cmnd* cmd)
682 {
683         adpt_hba* pHba;
684         u32 msg[4];
685         u32 rcode;
686         int old_state;
687         struct adpt_device* d = cmd->device->hostdata;
688
689         pHba = (void*) cmd->device->host->hostdata[0];
690         printk(KERN_INFO"%s: Trying to reset device\n",pHba->name);
691         if (!d) {
692                 printk(KERN_INFO"%s: Reset Device: Device Not found\n",pHba->name);
693                 return FAILED;
694         }
695         memset(msg, 0, sizeof(msg));
696         msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
697         msg[1] = (I2O_DEVICE_RESET<<24|HOST_TID<<12|d->tid);
698         msg[2] = 0;
699         msg[3] = 0;
700
701         if (pHba->host)
702                 spin_lock_irq(pHba->host->host_lock);
703         old_state = d->state;
704         d->state |= DPTI_DEV_RESET;
705         rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
706         d->state = old_state;
707         if (pHba->host)
708                 spin_unlock_irq(pHba->host->host_lock);
709         if (rcode != 0) {
710                 if(rcode == -EOPNOTSUPP ){
711                         printk(KERN_INFO"%s: Device reset not supported\n",pHba->name);
712                         return FAILED;
713                 }
714                 printk(KERN_INFO"%s: Device reset failed\n",pHba->name);
715                 return FAILED;
716         } else {
717                 printk(KERN_INFO"%s: Device reset successful\n",pHba->name);
718                 return SUCCESS;
719         }
720 }
721
722
723 #define I2O_HBA_BUS_RESET 0x87
724 // This version of bus reset is called by the eh_error handler
725 static int adpt_bus_reset(struct scsi_cmnd* cmd)
726 {
727         adpt_hba* pHba;
728         u32 msg[4];
729         u32 rcode;
730
731         pHba = (adpt_hba*)cmd->device->host->hostdata[0];
732         memset(msg, 0, sizeof(msg));
733         printk(KERN_WARNING"%s: Bus reset: SCSI Bus %d: tid: %d\n",pHba->name, cmd->device->channel,pHba->channel[cmd->device->channel].tid );
734         msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
735         msg[1] = (I2O_HBA_BUS_RESET<<24|HOST_TID<<12|pHba->channel[cmd->device->channel].tid);
736         msg[2] = 0;
737         msg[3] = 0;
738         if (pHba->host)
739                 spin_lock_irq(pHba->host->host_lock);
740         rcode = adpt_i2o_post_wait(pHba, msg,sizeof(msg), FOREVER);
741         if (pHba->host)
742                 spin_unlock_irq(pHba->host->host_lock);
743         if (rcode != 0) {
744                 printk(KERN_WARNING"%s: Bus reset failed.\n",pHba->name);
745                 return FAILED;
746         } else {
747                 printk(KERN_WARNING"%s: Bus reset success.\n",pHba->name);
748                 return SUCCESS;
749         }
750 }
751
752 // This version of reset is called by the eh_error_handler
753 static int __adpt_reset(struct scsi_cmnd* cmd)
754 {
755         adpt_hba* pHba;
756         int rcode;
757         char name[32];
758
759         pHba = (adpt_hba*)cmd->device->host->hostdata[0];
760         strncpy(name, pHba->name, sizeof(name));
761         printk(KERN_WARNING"%s: Hba Reset: scsi id %d: tid: %d\n", name, cmd->device->channel, pHba->channel[cmd->device->channel].tid);
762         rcode =  adpt_hba_reset(pHba);
763         if(rcode == 0){
764                 printk(KERN_WARNING"%s: HBA reset complete\n", name);
765                 return SUCCESS;
766         } else {
767                 printk(KERN_WARNING"%s: HBA reset failed (%x)\n", name, rcode);
768                 return FAILED;
769         }
770 }
771
772 static int adpt_reset(struct scsi_cmnd* cmd)
773 {
774         int rc;
775
776         spin_lock_irq(cmd->device->host->host_lock);
777         rc = __adpt_reset(cmd);
778         spin_unlock_irq(cmd->device->host->host_lock);
779
780         return rc;
781 }
782
783 // This version of reset is called by the ioctls and indirectly from eh_error_handler via adpt_reset
784 static int adpt_hba_reset(adpt_hba* pHba)
785 {
786         int rcode;
787
788         pHba->state |= DPTI_STATE_RESET;
789
790         // Activate does get status , init outbound, and get hrt
791         if ((rcode=adpt_i2o_activate_hba(pHba)) < 0) {
792                 printk(KERN_ERR "%s: Could not activate\n", pHba->name);
793                 adpt_i2o_delete_hba(pHba);
794                 return rcode;
795         }
796
797         if ((rcode=adpt_i2o_build_sys_table()) < 0) {
798                 adpt_i2o_delete_hba(pHba);
799                 return rcode;
800         }
801         PDEBUG("%s: in HOLD state\n",pHba->name);
802
803         if ((rcode=adpt_i2o_online_hba(pHba)) < 0) {
804                 adpt_i2o_delete_hba(pHba);      
805                 return rcode;
806         }
807         PDEBUG("%s: in OPERATIONAL state\n",pHba->name);
808
809         if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
810                 adpt_i2o_delete_hba(pHba);
811                 return rcode;
812         }
813
814         if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
815                 adpt_i2o_delete_hba(pHba);
816                 return rcode;
817         }
818         pHba->state &= ~DPTI_STATE_RESET;
819
820         adpt_fail_posted_scbs(pHba);
821         return 0;       /* return success */
822 }
823
824 /*===========================================================================
825  * 
826  *===========================================================================
827  */
828
829
830 static void adpt_i2o_sys_shutdown(void)
831 {
832         adpt_hba *pHba, *pNext;
833         struct adpt_i2o_post_wait_data *p1, *old;
834
835          printk(KERN_INFO"Shutting down Adaptec I2O controllers.\n");
836          printk(KERN_INFO"   This could take a few minutes if there are many devices attached\n");
837         /* Delete all IOPs from the controller chain */
838         /* They should have already been released by the
839          * scsi-core
840          */
841         for (pHba = hba_chain; pHba; pHba = pNext) {
842                 pNext = pHba->next;
843                 adpt_i2o_delete_hba(pHba);
844         }
845
846         /* Remove any timedout entries from the wait queue.  */
847 //      spin_lock_irqsave(&adpt_post_wait_lock, flags);
848         /* Nothing should be outstanding at this point so just
849          * free them 
850          */
851         for(p1 = adpt_post_wait_queue; p1;) {
852                 old = p1;
853                 p1 = p1->next;
854                 kfree(old);
855         }
856 //      spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
857         adpt_post_wait_queue = NULL;
858
859          printk(KERN_INFO "Adaptec I2O controllers down.\n");
860 }
861
862 static int adpt_install_hba(struct scsi_host_template* sht, struct pci_dev* pDev)
863 {
864
865         adpt_hba* pHba = NULL;
866         adpt_hba* p = NULL;
867         ulong base_addr0_phys = 0;
868         ulong base_addr1_phys = 0;
869         u32 hba_map0_area_size = 0;
870         u32 hba_map1_area_size = 0;
871         void __iomem *base_addr_virt = NULL;
872         void __iomem *msg_addr_virt = NULL;
873         int dma64 = 0;
874
875         int raptorFlag = FALSE;
876
877         if(pci_enable_device(pDev)) {
878                 return -EINVAL;
879         }
880
881         if (pci_request_regions(pDev, "dpt_i2o")) {
882                 PERROR("dpti: adpt_config_hba: pci request region failed\n");
883                 return -EINVAL;
884         }
885
886         pci_set_master(pDev);
887
888         /*
889          *      See if we should enable dma64 mode.
890          */
891         if (sizeof(dma_addr_t) > 4 &&
892             pci_set_dma_mask(pDev, DMA_BIT_MASK(64)) == 0) {
893                 if (dma_get_required_mask(&pDev->dev) > DMA_BIT_MASK(32))
894                         dma64 = 1;
895         }
896         if (!dma64 && pci_set_dma_mask(pDev, DMA_BIT_MASK(32)) != 0)
897                 return -EINVAL;
898
899         /* adapter only supports message blocks below 4GB */
900         pci_set_consistent_dma_mask(pDev, DMA_BIT_MASK(32));
901
902         base_addr0_phys = pci_resource_start(pDev,0);
903         hba_map0_area_size = pci_resource_len(pDev,0);
904
905         // Check if standard PCI card or single BAR Raptor
906         if(pDev->device == PCI_DPT_DEVICE_ID){
907                 if(pDev->subsystem_device >=0xc032 && pDev->subsystem_device <= 0xc03b){
908                         // Raptor card with this device id needs 4M
909                         hba_map0_area_size = 0x400000;
910                 } else { // Not Raptor - it is a PCI card
911                         if(hba_map0_area_size > 0x100000 ){ 
912                                 hba_map0_area_size = 0x100000;
913                         }
914                 }
915         } else {// Raptor split BAR config
916                 // Use BAR1 in this configuration
917                 base_addr1_phys = pci_resource_start(pDev,1);
918                 hba_map1_area_size = pci_resource_len(pDev,1);
919                 raptorFlag = TRUE;
920         }
921
922 #if BITS_PER_LONG == 64
923         /*
924          *      The original Adaptec 64 bit driver has this comment here:
925          *      "x86_64 machines need more optimal mappings"
926          *
927          *      I assume some HBAs report ridiculously large mappings
928          *      and we need to limit them on platforms with IOMMUs.
929          */
930         if (raptorFlag == TRUE) {
931                 if (hba_map0_area_size > 128)
932                         hba_map0_area_size = 128;
933                 if (hba_map1_area_size > 524288)
934                         hba_map1_area_size = 524288;
935         } else {
936                 if (hba_map0_area_size > 524288)
937                         hba_map0_area_size = 524288;
938         }
939 #endif
940
941         base_addr_virt = ioremap(base_addr0_phys,hba_map0_area_size);
942         if (!base_addr_virt) {
943                 pci_release_regions(pDev);
944                 PERROR("dpti: adpt_config_hba: io remap failed\n");
945                 return -EINVAL;
946         }
947
948         if(raptorFlag == TRUE) {
949                 msg_addr_virt = ioremap(base_addr1_phys, hba_map1_area_size );
950                 if (!msg_addr_virt) {
951                         PERROR("dpti: adpt_config_hba: io remap failed on BAR1\n");
952                         iounmap(base_addr_virt);
953                         pci_release_regions(pDev);
954                         return -EINVAL;
955                 }
956         } else {
957                 msg_addr_virt = base_addr_virt;
958         }
959         
960         // Allocate and zero the data structure
961         pHba = kzalloc(sizeof(adpt_hba), GFP_KERNEL);
962         if (!pHba) {
963                 if (msg_addr_virt != base_addr_virt)
964                         iounmap(msg_addr_virt);
965                 iounmap(base_addr_virt);
966                 pci_release_regions(pDev);
967                 return -ENOMEM;
968         }
969
970         mutex_lock(&adpt_configuration_lock);
971
972         if(hba_chain != NULL){
973                 for(p = hba_chain; p->next; p = p->next);
974                 p->next = pHba;
975         } else {
976                 hba_chain = pHba;
977         }
978         pHba->next = NULL;
979         pHba->unit = hba_count;
980         sprintf(pHba->name, "dpti%d", hba_count);
981         hba_count++;
982         
983         mutex_unlock(&adpt_configuration_lock);
984
985         pHba->pDev = pDev;
986         pHba->base_addr_phys = base_addr0_phys;
987
988         // Set up the Virtual Base Address of the I2O Device
989         pHba->base_addr_virt = base_addr_virt;
990         pHba->msg_addr_virt = msg_addr_virt;
991         pHba->irq_mask = base_addr_virt+0x30;
992         pHba->post_port = base_addr_virt+0x40;
993         pHba->reply_port = base_addr_virt+0x44;
994
995         pHba->hrt = NULL;
996         pHba->lct = NULL;
997         pHba->lct_size = 0;
998         pHba->status_block = NULL;
999         pHba->post_count = 0;
1000         pHba->state = DPTI_STATE_RESET;
1001         pHba->pDev = pDev;
1002         pHba->devices = NULL;
1003         pHba->dma64 = dma64;
1004
1005         // Initializing the spinlocks
1006         spin_lock_init(&pHba->state_lock);
1007         spin_lock_init(&adpt_post_wait_lock);
1008
1009         if(raptorFlag == 0){
1010                 printk(KERN_INFO "Adaptec I2O RAID controller"
1011                                  " %d at %p size=%x irq=%d%s\n", 
1012                         hba_count-1, base_addr_virt,
1013                         hba_map0_area_size, pDev->irq,
1014                         dma64 ? " (64-bit DMA)" : "");
1015         } else {
1016                 printk(KERN_INFO"Adaptec I2O RAID controller %d irq=%d%s\n",
1017                         hba_count-1, pDev->irq,
1018                         dma64 ? " (64-bit DMA)" : "");
1019                 printk(KERN_INFO"     BAR0 %p - size= %x\n",base_addr_virt,hba_map0_area_size);
1020                 printk(KERN_INFO"     BAR1 %p - size= %x\n",msg_addr_virt,hba_map1_area_size);
1021         }
1022
1023         if (request_irq (pDev->irq, adpt_isr, IRQF_SHARED, pHba->name, pHba)) {
1024                 printk(KERN_ERR"%s: Couldn't register IRQ %d\n", pHba->name, pDev->irq);
1025                 adpt_i2o_delete_hba(pHba);
1026                 return -EINVAL;
1027         }
1028
1029         return 0;
1030 }
1031
1032
1033 static void adpt_i2o_delete_hba(adpt_hba* pHba)
1034 {
1035         adpt_hba* p1;
1036         adpt_hba* p2;
1037         struct i2o_device* d;
1038         struct i2o_device* next;
1039         int i;
1040         int j;
1041         struct adpt_device* pDev;
1042         struct adpt_device* pNext;
1043
1044
1045         mutex_lock(&adpt_configuration_lock);
1046         if(pHba->host){
1047                 free_irq(pHba->host->irq, pHba);
1048         }
1049         p2 = NULL;
1050         for( p1 = hba_chain; p1; p2 = p1,p1=p1->next){
1051                 if(p1 == pHba) {
1052                         if(p2) {
1053                                 p2->next = p1->next;
1054                         } else {
1055                                 hba_chain = p1->next;
1056                         }
1057                         break;
1058                 }
1059         }
1060
1061         hba_count--;
1062         mutex_unlock(&adpt_configuration_lock);
1063
1064         iounmap(pHba->base_addr_virt);
1065         pci_release_regions(pHba->pDev);
1066         if(pHba->msg_addr_virt != pHba->base_addr_virt){
1067                 iounmap(pHba->msg_addr_virt);
1068         }
1069         if(pHba->FwDebugBuffer_P)
1070                 iounmap(pHba->FwDebugBuffer_P);
1071         if(pHba->hrt) {
1072                 dma_free_coherent(&pHba->pDev->dev,
1073                         pHba->hrt->num_entries * pHba->hrt->entry_len << 2,
1074                         pHba->hrt, pHba->hrt_pa);
1075         }
1076         if(pHba->lct) {
1077                 dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
1078                         pHba->lct, pHba->lct_pa);
1079         }
1080         if(pHba->status_block) {
1081                 dma_free_coherent(&pHba->pDev->dev, sizeof(i2o_status_block),
1082                         pHba->status_block, pHba->status_block_pa);
1083         }
1084         if(pHba->reply_pool) {
1085                 dma_free_coherent(&pHba->pDev->dev,
1086                         pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
1087                         pHba->reply_pool, pHba->reply_pool_pa);
1088         }
1089
1090         for(d = pHba->devices; d ; d = next){
1091                 next = d->next;
1092                 kfree(d);
1093         }
1094         for(i = 0 ; i < pHba->top_scsi_channel ; i++){
1095                 for(j = 0; j < MAX_ID; j++){
1096                         if(pHba->channel[i].device[j] != NULL){
1097                                 for(pDev = pHba->channel[i].device[j]; pDev; pDev = pNext){
1098                                         pNext = pDev->next_lun;
1099                                         kfree(pDev);
1100                                 }
1101                         }
1102                 }
1103         }
1104         pci_dev_put(pHba->pDev);
1105         if (adpt_sysfs_class)
1106                 device_destroy(adpt_sysfs_class,
1107                                 MKDEV(DPTI_I2O_MAJOR, pHba->unit));
1108         kfree(pHba);
1109
1110         if(hba_count <= 0){
1111                 unregister_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER);   
1112                 if (adpt_sysfs_class) {
1113                         class_destroy(adpt_sysfs_class);
1114                         adpt_sysfs_class = NULL;
1115                 }
1116         }
1117 }
1118
1119 static struct adpt_device* adpt_find_device(adpt_hba* pHba, u32 chan, u32 id, u64 lun)
1120 {
1121         struct adpt_device* d;
1122
1123         if(chan < 0 || chan >= MAX_CHANNEL)
1124                 return NULL;
1125         
1126         d = pHba->channel[chan].device[id];
1127         if(!d || d->tid == 0) {
1128                 return NULL;
1129         }
1130
1131         /* If it is the only lun at that address then this should match*/
1132         if(d->scsi_lun == lun){
1133                 return d;
1134         }
1135
1136         /* else we need to look through all the luns */
1137         for(d=d->next_lun ; d ; d = d->next_lun){
1138                 if(d->scsi_lun == lun){
1139                         return d;
1140                 }
1141         }
1142         return NULL;
1143 }
1144
1145
1146 static int adpt_i2o_post_wait(adpt_hba* pHba, u32* msg, int len, int timeout)
1147 {
1148         // I used my own version of the WAIT_QUEUE_HEAD
1149         // to handle some version differences
1150         // When embedded in the kernel this could go back to the vanilla one
1151         ADPT_DECLARE_WAIT_QUEUE_HEAD(adpt_wq_i2o_post);
1152         int status = 0;
1153         ulong flags = 0;
1154         struct adpt_i2o_post_wait_data *p1, *p2;
1155         struct adpt_i2o_post_wait_data *wait_data =
1156                 kmalloc(sizeof(struct adpt_i2o_post_wait_data), GFP_ATOMIC);
1157         DECLARE_WAITQUEUE(wait, current);
1158
1159         if (!wait_data)
1160                 return -ENOMEM;
1161
1162         /*
1163          * The spin locking is needed to keep anyone from playing
1164          * with the queue pointers and id while we do the same
1165          */
1166         spin_lock_irqsave(&adpt_post_wait_lock, flags);
1167        // TODO we need a MORE unique way of getting ids
1168        // to support async LCT get
1169         wait_data->next = adpt_post_wait_queue;
1170         adpt_post_wait_queue = wait_data;
1171         adpt_post_wait_id++;
1172         adpt_post_wait_id &= 0x7fff;
1173         wait_data->id =  adpt_post_wait_id;
1174         spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1175
1176         wait_data->wq = &adpt_wq_i2o_post;
1177         wait_data->status = -ETIMEDOUT;
1178
1179         add_wait_queue(&adpt_wq_i2o_post, &wait);
1180
1181         msg[2] |= 0x80000000 | ((u32)wait_data->id);
1182         timeout *= HZ;
1183         if((status = adpt_i2o_post_this(pHba, msg, len)) == 0){
1184                 set_current_state(TASK_INTERRUPTIBLE);
1185                 if(pHba->host)
1186                         spin_unlock_irq(pHba->host->host_lock);
1187                 if (!timeout)
1188                         schedule();
1189                 else{
1190                         timeout = schedule_timeout(timeout);
1191                         if (timeout == 0) {
1192                                 // I/O issued, but cannot get result in
1193                                 // specified time. Freeing resorces is
1194                                 // dangerous.
1195                                 status = -ETIME;
1196                         }
1197                 }
1198                 if(pHba->host)
1199                         spin_lock_irq(pHba->host->host_lock);
1200         }
1201         remove_wait_queue(&adpt_wq_i2o_post, &wait);
1202
1203         if(status == -ETIMEDOUT){
1204                 printk(KERN_INFO"dpti%d: POST WAIT TIMEOUT\n",pHba->unit);
1205                 // We will have to free the wait_data memory during shutdown
1206                 return status;
1207         }
1208
1209         /* Remove the entry from the queue.  */
1210         p2 = NULL;
1211         spin_lock_irqsave(&adpt_post_wait_lock, flags);
1212         for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p1->next) {
1213                 if(p1 == wait_data) {
1214                         if(p1->status == I2O_DETAIL_STATUS_UNSUPPORTED_FUNCTION ) {
1215                                 status = -EOPNOTSUPP;
1216                         }
1217                         if(p2) {
1218                                 p2->next = p1->next;
1219                         } else {
1220                                 adpt_post_wait_queue = p1->next;
1221                         }
1222                         break;
1223                 }
1224         }
1225         spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1226
1227         kfree(wait_data);
1228
1229         return status;
1230 }
1231
1232
1233 static s32 adpt_i2o_post_this(adpt_hba* pHba, u32* data, int len)
1234 {
1235
1236         u32 m = EMPTY_QUEUE;
1237         u32 __iomem *msg;
1238         ulong timeout = jiffies + 30*HZ;
1239         do {
1240                 rmb();
1241                 m = readl(pHba->post_port);
1242                 if (m != EMPTY_QUEUE) {
1243                         break;
1244                 }
1245                 if(time_after(jiffies,timeout)){
1246                         printk(KERN_WARNING"dpti%d: Timeout waiting for message frame!\n", pHba->unit);
1247                         return -ETIMEDOUT;
1248                 }
1249                 schedule_timeout_uninterruptible(1);
1250         } while(m == EMPTY_QUEUE);
1251                 
1252         msg = pHba->msg_addr_virt + m;
1253         memcpy_toio(msg, data, len);
1254         wmb();
1255
1256         //post message
1257         writel(m, pHba->post_port);
1258         wmb();
1259
1260         return 0;
1261 }
1262
1263
1264 static void adpt_i2o_post_wait_complete(u32 context, int status)
1265 {
1266         struct adpt_i2o_post_wait_data *p1 = NULL;
1267         /*
1268          * We need to search through the adpt_post_wait
1269          * queue to see if the given message is still
1270          * outstanding.  If not, it means that the IOP
1271          * took longer to respond to the message than we
1272          * had allowed and timer has already expired.
1273          * Not much we can do about that except log
1274          * it for debug purposes, increase timeout, and recompile
1275          *
1276          * Lock needed to keep anyone from moving queue pointers
1277          * around while we're looking through them.
1278          */
1279
1280         context &= 0x7fff;
1281
1282         spin_lock(&adpt_post_wait_lock);
1283         for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1284                 if(p1->id == context) {
1285                         p1->status = status;
1286                         spin_unlock(&adpt_post_wait_lock);
1287                         wake_up_interruptible(p1->wq);
1288                         return;
1289                 }
1290         }
1291         spin_unlock(&adpt_post_wait_lock);
1292         // If this happens we lose commands that probably really completed
1293         printk(KERN_DEBUG"dpti: Could Not find task %d in wait queue\n",context);
1294         printk(KERN_DEBUG"      Tasks in wait queue:\n");
1295         for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1296                 printk(KERN_DEBUG"           %d\n",p1->id);
1297         }
1298         return;
1299 }
1300
1301 static s32 adpt_i2o_reset_hba(adpt_hba* pHba)                   
1302 {
1303         u32 msg[8];
1304         u8* status;
1305         dma_addr_t addr;
1306         u32 m = EMPTY_QUEUE ;
1307         ulong timeout = jiffies + (TMOUT_IOPRESET*HZ);
1308
1309         if(pHba->initialized  == FALSE) {       // First time reset should be quick
1310                 timeout = jiffies + (25*HZ);
1311         } else {
1312                 adpt_i2o_quiesce_hba(pHba);
1313         }
1314
1315         do {
1316                 rmb();
1317                 m = readl(pHba->post_port);
1318                 if (m != EMPTY_QUEUE) {
1319                         break;
1320                 }
1321                 if(time_after(jiffies,timeout)){
1322                         printk(KERN_WARNING"Timeout waiting for message!\n");
1323                         return -ETIMEDOUT;
1324                 }
1325                 schedule_timeout_uninterruptible(1);
1326         } while (m == EMPTY_QUEUE);
1327
1328         status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
1329         if(status == NULL) {
1330                 adpt_send_nop(pHba, m);
1331                 printk(KERN_ERR"IOP reset failed - no free memory.\n");
1332                 return -ENOMEM;
1333         }
1334         memset(status,0,4);
1335
1336         msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
1337         msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
1338         msg[2]=0;
1339         msg[3]=0;
1340         msg[4]=0;
1341         msg[5]=0;
1342         msg[6]=dma_low(addr);
1343         msg[7]=dma_high(addr);
1344
1345         memcpy_toio(pHba->msg_addr_virt+m, msg, sizeof(msg));
1346         wmb();
1347         writel(m, pHba->post_port);
1348         wmb();
1349
1350         while(*status == 0){
1351                 if(time_after(jiffies,timeout)){
1352                         printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
1353                         /* We lose 4 bytes of "status" here, but we cannot
1354                            free these because controller may awake and corrupt
1355                            those bytes at any time */
1356                         /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
1357                         return -ETIMEDOUT;
1358                 }
1359                 rmb();
1360                 schedule_timeout_uninterruptible(1);
1361         }
1362
1363         if(*status == 0x01 /*I2O_EXEC_IOP_RESET_IN_PROGRESS*/) {
1364                 PDEBUG("%s: Reset in progress...\n", pHba->name);
1365                 // Here we wait for message frame to become available
1366                 // indicated that reset has finished
1367                 do {
1368                         rmb();
1369                         m = readl(pHba->post_port);
1370                         if (m != EMPTY_QUEUE) {
1371                                 break;
1372                         }
1373                         if(time_after(jiffies,timeout)){
1374                                 printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
1375                                 /* We lose 4 bytes of "status" here, but we
1376                                    cannot free these because controller may
1377                                    awake and corrupt those bytes at any time */
1378                                 /* dma_free_coherent(&pHba->pDev->dev, 4, buf, addr); */
1379                                 return -ETIMEDOUT;
1380                         }
1381                         schedule_timeout_uninterruptible(1);
1382                 } while (m == EMPTY_QUEUE);
1383                 // Flush the offset
1384                 adpt_send_nop(pHba, m);
1385         }
1386         adpt_i2o_status_get(pHba);
1387         if(*status == 0x02 ||
1388                         pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
1389                 printk(KERN_WARNING"%s: Reset reject, trying to clear\n",
1390                                 pHba->name);
1391         } else {
1392                 PDEBUG("%s: Reset completed.\n", pHba->name);
1393         }
1394
1395         dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
1396 #ifdef UARTDELAY
1397         // This delay is to allow someone attached to the card through the debug UART to 
1398         // set up the dump levels that they want before the rest of the initialization sequence
1399         adpt_delay(20000);
1400 #endif
1401         return 0;
1402 }
1403
1404
1405 static int adpt_i2o_parse_lct(adpt_hba* pHba)
1406 {
1407         int i;
1408         int max;
1409         int tid;
1410         struct i2o_device *d;
1411         i2o_lct *lct = pHba->lct;
1412         u8 bus_no = 0;
1413         s16 scsi_id;
1414         u64 scsi_lun;
1415         u32 buf[10]; // larger than 7, or 8 ...
1416         struct adpt_device* pDev; 
1417         
1418         if (lct == NULL) {
1419                 printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
1420                 return -1;
1421         }
1422         
1423         max = lct->table_size;  
1424         max -= 3;
1425         max /= 9;
1426
1427         for(i=0;i<max;i++) {
1428                 if( lct->lct_entry[i].user_tid != 0xfff){
1429                         /*
1430                          * If we have hidden devices, we need to inform the upper layers about
1431                          * the possible maximum id reference to handle device access when
1432                          * an array is disassembled. This code has no other purpose but to
1433                          * allow us future access to devices that are currently hidden
1434                          * behind arrays, hotspares or have not been configured (JBOD mode).
1435                          */
1436                         if( lct->lct_entry[i].class_id != I2O_CLASS_RANDOM_BLOCK_STORAGE &&
1437                             lct->lct_entry[i].class_id != I2O_CLASS_SCSI_PERIPHERAL &&
1438                             lct->lct_entry[i].class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1439                                 continue;
1440                         }
1441                         tid = lct->lct_entry[i].tid;
1442                         // I2O_DPT_DEVICE_INFO_GROUP_NO;
1443                         if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
1444                                 continue;
1445                         }
1446                         bus_no = buf[0]>>16;
1447                         scsi_id = buf[1];
1448                         scsi_lun = scsilun_to_int((struct scsi_lun *)&buf[2]);
1449                         if(bus_no >= MAX_CHANNEL) {     // Something wrong skip it
1450                                 printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
1451                                 continue;
1452                         }
1453                         if (scsi_id >= MAX_ID){
1454                                 printk(KERN_WARNING"%s: SCSI ID %d out of range \n", pHba->name, bus_no);
1455                                 continue;
1456                         }
1457                         if(bus_no > pHba->top_scsi_channel){
1458                                 pHba->top_scsi_channel = bus_no;
1459                         }
1460                         if(scsi_id > pHba->top_scsi_id){
1461                                 pHba->top_scsi_id = scsi_id;
1462                         }
1463                         if(scsi_lun > pHba->top_scsi_lun){
1464                                 pHba->top_scsi_lun = scsi_lun;
1465                         }
1466                         continue;
1467                 }
1468                 d = kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
1469                 if(d==NULL)
1470                 {
1471                         printk(KERN_CRIT"%s: Out of memory for I2O device data.\n",pHba->name);
1472                         return -ENOMEM;
1473                 }
1474                 
1475                 d->controller = pHba;
1476                 d->next = NULL;
1477
1478                 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
1479
1480                 d->flags = 0;
1481                 tid = d->lct_data.tid;
1482                 adpt_i2o_report_hba_unit(pHba, d);
1483                 adpt_i2o_install_device(pHba, d);
1484         }
1485         bus_no = 0;
1486         for(d = pHba->devices; d ; d = d->next) {
1487                 if(d->lct_data.class_id  == I2O_CLASS_BUS_ADAPTER_PORT ||
1488                    d->lct_data.class_id  == I2O_CLASS_FIBRE_CHANNEL_PORT){
1489                         tid = d->lct_data.tid;
1490                         // TODO get the bus_no from hrt-but for now they are in order
1491                         //bus_no = 
1492                         if(bus_no > pHba->top_scsi_channel){
1493                                 pHba->top_scsi_channel = bus_no;
1494                         }
1495                         pHba->channel[bus_no].type = d->lct_data.class_id;
1496                         pHba->channel[bus_no].tid = tid;
1497                         if(adpt_i2o_query_scalar(pHba, tid, 0x0200, -1, buf, 28)>=0)
1498                         {
1499                                 pHba->channel[bus_no].scsi_id = buf[1];
1500                                 PDEBUG("Bus %d - SCSI ID %d.\n", bus_no, buf[1]);
1501                         }
1502                         // TODO remove - this is just until we get from hrt
1503                         bus_no++;
1504                         if(bus_no >= MAX_CHANNEL) {     // Something wrong skip it
1505                                 printk(KERN_WARNING"%s: Channel number %d out of range - LCT\n", pHba->name, bus_no);
1506                                 break;
1507                         }
1508                 }
1509         }
1510
1511         // Setup adpt_device table
1512         for(d = pHba->devices; d ; d = d->next) {
1513                 if(d->lct_data.class_id  == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
1514                    d->lct_data.class_id  == I2O_CLASS_SCSI_PERIPHERAL ||
1515                    d->lct_data.class_id  == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1516
1517                         tid = d->lct_data.tid;
1518                         scsi_id = -1;
1519                         // I2O_DPT_DEVICE_INFO_GROUP_NO;
1520                         if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)>=0) {
1521                                 bus_no = buf[0]>>16;
1522                                 scsi_id = buf[1];
1523                                 scsi_lun = scsilun_to_int((struct scsi_lun *)&buf[2]);
1524                                 if(bus_no >= MAX_CHANNEL) {     // Something wrong skip it
1525                                         continue;
1526                                 }
1527                                 if (scsi_id >= MAX_ID) {
1528                                         continue;
1529                                 }
1530                                 if( pHba->channel[bus_no].device[scsi_id] == NULL){
1531                                         pDev =  kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
1532                                         if(pDev == NULL) {
1533                                                 return -ENOMEM;
1534                                         }
1535                                         pHba->channel[bus_no].device[scsi_id] = pDev;
1536                                 } else {
1537                                         for( pDev = pHba->channel[bus_no].device[scsi_id];      
1538                                                         pDev->next_lun; pDev = pDev->next_lun){
1539                                         }
1540                                         pDev->next_lun = kzalloc(sizeof(struct adpt_device),GFP_KERNEL);
1541                                         if(pDev->next_lun == NULL) {
1542                                                 return -ENOMEM;
1543                                         }
1544                                         pDev = pDev->next_lun;
1545                                 }
1546                                 pDev->tid = tid;
1547                                 pDev->scsi_channel = bus_no;
1548                                 pDev->scsi_id = scsi_id;
1549                                 pDev->scsi_lun = scsi_lun;
1550                                 pDev->pI2o_dev = d;
1551                                 d->owner = pDev;
1552                                 pDev->type = (buf[0])&0xff;
1553                                 pDev->flags = (buf[0]>>8)&0xff;
1554                                 if(scsi_id > pHba->top_scsi_id){
1555                                         pHba->top_scsi_id = scsi_id;
1556                                 }
1557                                 if(scsi_lun > pHba->top_scsi_lun){
1558                                         pHba->top_scsi_lun = scsi_lun;
1559                                 }
1560                         }
1561                         if(scsi_id == -1){
1562                                 printk(KERN_WARNING"Could not find SCSI ID for %s\n",
1563                                                 d->lct_data.identity_tag);
1564                         }
1565                 }
1566         }
1567         return 0;
1568 }
1569
1570
1571 /*
1572  *      Each I2O controller has a chain of devices on it - these match
1573  *      the useful parts of the LCT of the board.
1574  */
1575  
1576 static int adpt_i2o_install_device(adpt_hba* pHba, struct i2o_device *d)
1577 {
1578         mutex_lock(&adpt_configuration_lock);
1579         d->controller=pHba;
1580         d->owner=NULL;
1581         d->next=pHba->devices;
1582         d->prev=NULL;
1583         if (pHba->devices != NULL){
1584                 pHba->devices->prev=d;
1585         }
1586         pHba->devices=d;
1587         *d->dev_name = 0;
1588
1589         mutex_unlock(&adpt_configuration_lock);
1590         return 0;
1591 }
1592
1593 static int adpt_open(struct inode *inode, struct file *file)
1594 {
1595         int minor;
1596         adpt_hba* pHba;
1597
1598         mutex_lock(&adpt_mutex);
1599         //TODO check for root access
1600         //
1601         minor = iminor(inode);
1602         if (minor >= hba_count) {
1603                 mutex_unlock(&adpt_mutex);
1604                 return -ENXIO;
1605         }
1606         mutex_lock(&adpt_configuration_lock);
1607         for (pHba = hba_chain; pHba; pHba = pHba->next) {
1608                 if (pHba->unit == minor) {
1609                         break;  /* found adapter */
1610                 }
1611         }
1612         if (pHba == NULL) {
1613                 mutex_unlock(&adpt_configuration_lock);
1614                 mutex_unlock(&adpt_mutex);
1615                 return -ENXIO;
1616         }
1617
1618 //      if(pHba->in_use){
1619         //      mutex_unlock(&adpt_configuration_lock);
1620 //              return -EBUSY;
1621 //      }
1622
1623         pHba->in_use = 1;
1624         mutex_unlock(&adpt_configuration_lock);
1625         mutex_unlock(&adpt_mutex);
1626
1627         return 0;
1628 }
1629
1630 static int adpt_close(struct inode *inode, struct file *file)
1631 {
1632         int minor;
1633         adpt_hba* pHba;
1634
1635         minor = iminor(inode);
1636         if (minor >= hba_count) {
1637                 return -ENXIO;
1638         }
1639         mutex_lock(&adpt_configuration_lock);
1640         for (pHba = hba_chain; pHba; pHba = pHba->next) {
1641                 if (pHba->unit == minor) {
1642                         break;  /* found adapter */
1643                 }
1644         }
1645         mutex_unlock(&adpt_configuration_lock);
1646         if (pHba == NULL) {
1647                 return -ENXIO;
1648         }
1649
1650         pHba->in_use = 0;
1651
1652         return 0;
1653 }
1654
1655 #if defined __ia64__ 
1656 static void adpt_ia64_info(sysInfo_S* si)
1657 {
1658         // This is all the info we need for now
1659         // We will add more info as our new
1660         // managmenent utility requires it
1661         si->processorType = PROC_IA64;
1662 }
1663 #endif
1664
1665 #if defined __sparc__ 
1666 static void adpt_sparc_info(sysInfo_S* si)
1667 {
1668         // This is all the info we need for now
1669         // We will add more info as our new
1670         // managmenent utility requires it
1671         si->processorType = PROC_ULTRASPARC;
1672 }
1673 #endif
1674 #if defined __alpha__ 
1675 static void adpt_alpha_info(sysInfo_S* si)
1676 {
1677         // This is all the info we need for now
1678         // We will add more info as our new
1679         // managmenent utility requires it
1680         si->processorType = PROC_ALPHA;
1681 }
1682 #endif
1683
1684 #if defined __i386__
1685
1686 #include <uapi/asm/vm86.h>
1687
1688 static void adpt_i386_info(sysInfo_S* si)
1689 {
1690         // This is all the info we need for now
1691         // We will add more info as our new
1692         // managmenent utility requires it
1693         switch (boot_cpu_data.x86) {
1694         case CPU_386:
1695                 si->processorType = PROC_386;
1696                 break;
1697         case CPU_486:
1698                 si->processorType = PROC_486;
1699                 break;
1700         case CPU_586:
1701                 si->processorType = PROC_PENTIUM;
1702                 break;
1703         default:  // Just in case 
1704                 si->processorType = PROC_PENTIUM;
1705                 break;
1706         }
1707 }
1708 #endif
1709
1710 /*
1711  * This routine returns information about the system.  This does not effect
1712  * any logic and if the info is wrong - it doesn't matter.
1713  */
1714
1715 /* Get all the info we can not get from kernel services */
1716 static int adpt_system_info(void __user *buffer)
1717 {
1718         sysInfo_S si;
1719
1720         memset(&si, 0, sizeof(si));
1721
1722         si.osType = OS_LINUX;
1723         si.osMajorVersion = 0;
1724         si.osMinorVersion = 0;
1725         si.osRevision = 0;
1726         si.busType = SI_PCI_BUS;
1727         si.processorFamily = DPTI_sig.dsProcessorFamily;
1728
1729 #if defined __i386__
1730         adpt_i386_info(&si);
1731 #elif defined (__ia64__)
1732         adpt_ia64_info(&si);
1733 #elif defined(__sparc__)
1734         adpt_sparc_info(&si);
1735 #elif defined (__alpha__)
1736         adpt_alpha_info(&si);
1737 #else
1738         si.processorType = 0xff ;
1739 #endif
1740         if (copy_to_user(buffer, &si, sizeof(si))){
1741                 printk(KERN_WARNING"dpti: Could not copy buffer TO user\n");
1742                 return -EFAULT;
1743         }
1744
1745         return 0;
1746 }
1747
1748 static int adpt_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
1749 {
1750         int minor;
1751         int error = 0;
1752         adpt_hba* pHba;
1753         ulong flags = 0;
1754         void __user *argp = (void __user *)arg;
1755
1756         minor = iminor(inode);
1757         if (minor >= DPTI_MAX_HBA){
1758                 return -ENXIO;
1759         }
1760         mutex_lock(&adpt_configuration_lock);
1761         for (pHba = hba_chain; pHba; pHba = pHba->next) {
1762                 if (pHba->unit == minor) {
1763                         break;  /* found adapter */
1764                 }
1765         }
1766         mutex_unlock(&adpt_configuration_lock);
1767         if(pHba == NULL){
1768                 return -ENXIO;
1769         }
1770
1771         while((volatile u32) pHba->state & DPTI_STATE_RESET )
1772                 schedule_timeout_uninterruptible(2);
1773
1774         switch (cmd) {
1775         // TODO: handle 3 cases
1776         case DPT_SIGNATURE:
1777                 if (copy_to_user(argp, &DPTI_sig, sizeof(DPTI_sig))) {
1778                         return -EFAULT;
1779                 }
1780                 break;
1781
1782         case DPT_CTRLINFO:{
1783                 drvrHBAinfo_S HbaInfo;
1784
1785 #define FLG_OSD_PCI_VALID 0x0001
1786 #define FLG_OSD_DMA       0x0002
1787 #define FLG_OSD_I2O       0x0004
1788                 memset(&HbaInfo, 0, sizeof(HbaInfo));
1789                 HbaInfo.drvrHBAnum = pHba->unit;
1790                 HbaInfo.baseAddr = (ulong) pHba->base_addr_phys;
1791                 HbaInfo.blinkState = adpt_read_blink_led(pHba);
1792                 HbaInfo.pciBusNum =  pHba->pDev->bus->number;
1793                 HbaInfo.pciDeviceNum=PCI_SLOT(pHba->pDev->devfn); 
1794                 HbaInfo.Interrupt = pHba->pDev->irq; 
1795                 HbaInfo.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
1796                 if(copy_to_user(argp, &HbaInfo, sizeof(HbaInfo))){
1797                         printk(KERN_WARNING"%s: Could not copy HbaInfo TO user\n",pHba->name);
1798                         return -EFAULT;
1799                 }
1800                 break;
1801                 }
1802         case DPT_SYSINFO:
1803                 return adpt_system_info(argp);
1804         case DPT_BLINKLED:{
1805                 u32 value;
1806                 value = (u32)adpt_read_blink_led(pHba);
1807                 if (copy_to_user(argp, &value, sizeof(value))) {
1808                         return -EFAULT;
1809                 }
1810                 break;
1811                 }
1812         case I2ORESETCMD: {
1813                 struct Scsi_Host *shost = pHba->host;
1814
1815                 if (shost)
1816                         spin_lock_irqsave(shost->host_lock, flags);
1817                 adpt_hba_reset(pHba);
1818                 if (shost)
1819                         spin_unlock_irqrestore(shost->host_lock, flags);
1820                 break;
1821         }
1822         case I2ORESCANCMD:
1823                 adpt_rescan(pHba);
1824                 break;
1825         default:
1826                 return -EINVAL;
1827         }
1828
1829         return error;
1830 }
1831
1832 static long adpt_unlocked_ioctl(struct file *file, uint cmd, ulong arg)
1833 {
1834         struct inode *inode;
1835         long ret;
1836  
1837         inode = file_inode(file);
1838  
1839         mutex_lock(&adpt_mutex);
1840         ret = adpt_ioctl(inode, file, cmd, arg);
1841         mutex_unlock(&adpt_mutex);
1842
1843         return ret;
1844 }
1845
1846 #ifdef CONFIG_COMPAT
1847 static long compat_adpt_ioctl(struct file *file,
1848                                 unsigned int cmd, unsigned long arg)
1849 {
1850         struct inode *inode;
1851         long ret;
1852  
1853         inode = file_inode(file);
1854  
1855         mutex_lock(&adpt_mutex);
1856  
1857         switch(cmd) {
1858                 case DPT_SIGNATURE:
1859                 case I2OUSRCMD:
1860                 case DPT_CTRLINFO:
1861                 case DPT_SYSINFO:
1862                 case DPT_BLINKLED:
1863                 case I2ORESETCMD:
1864                 case I2ORESCANCMD:
1865                 case (DPT_TARGET_BUSY & 0xFFFF):
1866                 case DPT_TARGET_BUSY:
1867                         ret = adpt_ioctl(inode, file, cmd, arg);
1868                         break;
1869                 default:
1870                         ret =  -ENOIOCTLCMD;
1871         }
1872  
1873         mutex_unlock(&adpt_mutex);
1874  
1875         return ret;
1876 }
1877 #endif
1878
1879 static irqreturn_t adpt_isr(int irq, void *dev_id)
1880 {
1881         struct scsi_cmnd* cmd;
1882         adpt_hba* pHba = dev_id;
1883         u32 m;
1884         void __iomem *reply;
1885         u32 status=0;
1886         u32 context;
1887         ulong flags = 0;
1888         int handled = 0;
1889
1890         if (pHba == NULL){
1891                 printk(KERN_WARNING"adpt_isr: NULL dev_id\n");
1892                 return IRQ_NONE;
1893         }
1894         if(pHba->host)
1895                 spin_lock_irqsave(pHba->host->host_lock, flags);
1896
1897         while( readl(pHba->irq_mask) & I2O_INTERRUPT_PENDING_B) {
1898                 m = readl(pHba->reply_port);
1899                 if(m == EMPTY_QUEUE){
1900                         // Try twice then give up
1901                         rmb();
1902                         m = readl(pHba->reply_port);
1903                         if(m == EMPTY_QUEUE){ 
1904                                 // This really should not happen
1905                                 printk(KERN_ERR"dpti: Could not get reply frame\n");
1906                                 goto out;
1907                         }
1908                 }
1909                 if (pHba->reply_pool_pa <= m &&
1910                     m < pHba->reply_pool_pa +
1911                         (pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4)) {
1912                         reply = (u8 *)pHba->reply_pool +
1913                                                 (m - pHba->reply_pool_pa);
1914                 } else {
1915                         /* Ick, we should *never* be here */
1916                         printk(KERN_ERR "dpti: reply frame not from pool\n");
1917                         continue;
1918                 }
1919
1920                 if (readl(reply) & MSG_FAIL) {
1921                         u32 old_m = readl(reply+28); 
1922                         void __iomem *msg;
1923                         u32 old_context;
1924                         PDEBUG("%s: Failed message\n",pHba->name);
1925                         if(old_m >= 0x100000){
1926                                 printk(KERN_ERR"%s: Bad preserved MFA (%x)- dropping frame\n",pHba->name,old_m);
1927                                 writel(m,pHba->reply_port);
1928                                 continue;
1929                         }
1930                         // Transaction context is 0 in failed reply frame
1931                         msg = pHba->msg_addr_virt + old_m;
1932                         old_context = readl(msg+12);
1933                         writel(old_context, reply+12);
1934                         adpt_send_nop(pHba, old_m);
1935                 } 
1936                 context = readl(reply+8);
1937                 if(context & 0x80000000){ // Post wait message
1938                         status = readl(reply+16);
1939                         if(status  >> 24){
1940                                 status &=  0xffff; /* Get detail status */
1941                         } else {
1942                                 status = I2O_POST_WAIT_OK;
1943                         }
1944                         cmd = adpt_cmd_from_context(pHba, readl(reply+12));
1945                         if(cmd != NULL) {
1946                                 printk(KERN_WARNING"%s: Apparent SCSI cmd in Post Wait Context - cmd=%p context=%x\n", pHba->name, cmd, context);
1947                         }
1948                         adpt_i2o_post_wait_complete(context, status);
1949                 } else { // SCSI message
1950                         cmd = adpt_cmd_from_context (pHba, readl(reply+12));
1951                         if(cmd != NULL){
1952                                 scsi_dma_unmap(cmd);
1953                                 if(cmd->serial_number != 0) { // If not timedout
1954                                         adpt_i2o_to_scsi(reply, cmd);
1955                                 }
1956                         }
1957                 }
1958                 writel(m, pHba->reply_port);
1959                 wmb();
1960                 rmb();
1961         }
1962         handled = 1;
1963 out:    if(pHba->host)
1964                 spin_unlock_irqrestore(pHba->host->host_lock, flags);
1965         return IRQ_RETVAL(handled);
1966 }
1967
1968 static s32 adpt_scsi_to_i2o(adpt_hba* pHba, struct scsi_cmnd* cmd, struct adpt_device* d)
1969 {
1970         int i;
1971         u32 msg[MAX_MESSAGE_SIZE];
1972         u32* mptr;
1973         u32* lptr;
1974         u32 *lenptr;
1975         int direction;
1976         int scsidir;
1977         int nseg;
1978         u32 len;
1979         u32 reqlen;
1980         s32 rcode;
1981         dma_addr_t addr;
1982
1983         memset(msg, 0 , sizeof(msg));
1984         len = scsi_bufflen(cmd);
1985         direction = 0x00000000; 
1986         
1987         scsidir = 0x00000000;                   // DATA NO XFER
1988         if(len) {
1989                 /*
1990                  * Set SCBFlags to indicate if data is being transferred
1991                  * in or out, or no data transfer
1992                  * Note:  Do not have to verify index is less than 0 since
1993                  * cmd->cmnd[0] is an unsigned char
1994                  */
1995                 switch(cmd->sc_data_direction){
1996                 case DMA_FROM_DEVICE:
1997                         scsidir  =0x40000000;   // DATA IN  (iop<--dev)
1998                         break;
1999                 case DMA_TO_DEVICE:
2000                         direction=0x04000000;   // SGL OUT
2001                         scsidir  =0x80000000;   // DATA OUT (iop-->dev)
2002                         break;
2003                 case DMA_NONE:
2004                         break;
2005                 case DMA_BIDIRECTIONAL:
2006                         scsidir  =0x40000000;   // DATA IN  (iop<--dev)
2007                         // Assume In - and continue;
2008                         break;
2009                 default:
2010                         printk(KERN_WARNING"%s: scsi opcode 0x%x not supported.\n",
2011                              pHba->name, cmd->cmnd[0]);
2012                         cmd->result = (DID_OK <<16) | (INITIATOR_ERROR << 8);
2013                         cmd->scsi_done(cmd);
2014                         return  0;
2015                 }
2016         }
2017         // msg[0] is set later
2018         // I2O_CMD_SCSI_EXEC
2019         msg[1] = ((0xff<<24)|(HOST_TID<<12)|d->tid);
2020         msg[2] = 0;
2021         msg[3] = adpt_cmd_to_context(cmd);  /* Want SCSI control block back */
2022         // Our cards use the transaction context as the tag for queueing
2023         // Adaptec/DPT Private stuff 
2024         msg[4] = I2O_CMD_SCSI_EXEC|(DPT_ORGANIZATION_ID<<16);
2025         msg[5] = d->tid;
2026         /* Direction, disconnect ok | sense data | simple queue , CDBLen */
2027         // I2O_SCB_FLAG_ENABLE_DISCONNECT | 
2028         // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG | 
2029         // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
2030         msg[6] = scsidir|0x20a00000|cmd->cmd_len;
2031
2032         mptr=msg+7;
2033
2034         // Write SCSI command into the message - always 16 byte block 
2035         memset(mptr, 0,  16);
2036         memcpy(mptr, cmd->cmnd, cmd->cmd_len);
2037         mptr+=4;
2038         lenptr=mptr++;          /* Remember me - fill in when we know */
2039         if (dpt_dma64(pHba)) {
2040                 reqlen = 16;            // SINGLE SGE
2041                 *mptr++ = (0x7C<<24)+(2<<16)+0x02; /* Enable 64 bit */
2042                 *mptr++ = 1 << PAGE_SHIFT;
2043         } else {
2044                 reqlen = 14;            // SINGLE SGE
2045         }
2046         /* Now fill in the SGList and command */
2047
2048         nseg = scsi_dma_map(cmd);
2049         BUG_ON(nseg < 0);
2050         if (nseg) {
2051                 struct scatterlist *sg;
2052
2053                 len = 0;
2054                 scsi_for_each_sg(cmd, sg, nseg, i) {
2055                         lptr = mptr;
2056                         *mptr++ = direction|0x10000000|sg_dma_len(sg);
2057                         len+=sg_dma_len(sg);
2058                         addr = sg_dma_address(sg);
2059                         *mptr++ = dma_low(addr);
2060                         if (dpt_dma64(pHba))
2061                                 *mptr++ = dma_high(addr);
2062                         /* Make this an end of list */
2063                         if (i == nseg - 1)
2064                                 *lptr = direction|0xD0000000|sg_dma_len(sg);
2065                 }
2066                 reqlen = mptr - msg;
2067                 *lenptr = len;
2068                 
2069                 if(cmd->underflow && len != cmd->underflow){
2070                         printk(KERN_WARNING"Cmd len %08X Cmd underflow %08X\n",
2071                                 len, cmd->underflow);
2072                 }
2073         } else {
2074                 *lenptr = len = 0;
2075                 reqlen = 12;
2076         }
2077         
2078         /* Stick the headers on */
2079         msg[0] = reqlen<<16 | ((reqlen > 12) ? SGL_OFFSET_12 : SGL_OFFSET_0);
2080         
2081         // Send it on it's way
2082         rcode = adpt_i2o_post_this(pHba, msg, reqlen<<2);
2083         if (rcode == 0) {
2084                 return 0;
2085         }
2086         return rcode;
2087 }
2088
2089
2090 static s32 adpt_scsi_host_alloc(adpt_hba* pHba, struct scsi_host_template *sht)
2091 {
2092         struct Scsi_Host *host;
2093
2094         host = scsi_host_alloc(sht, sizeof(adpt_hba*));
2095         if (host == NULL) {
2096                 printk("%s: scsi_host_alloc returned NULL\n", pHba->name);
2097                 return -1;
2098         }
2099         host->hostdata[0] = (unsigned long)pHba;
2100         pHba->host = host;
2101
2102         host->irq = pHba->pDev->irq;
2103         /* no IO ports, so don't have to set host->io_port and
2104          * host->n_io_port
2105          */
2106         host->io_port = 0;
2107         host->n_io_port = 0;
2108                                 /* see comments in scsi_host.h */
2109         host->max_id = 16;
2110         host->max_lun = 256;
2111         host->max_channel = pHba->top_scsi_channel + 1;
2112         host->cmd_per_lun = 1;
2113         host->unique_id = (u32)sys_tbl_pa + pHba->unit;
2114         host->sg_tablesize = pHba->sg_tablesize;
2115         host->can_queue = pHba->post_fifo_size;
2116         host->use_cmd_list = 1;
2117
2118         return 0;
2119 }
2120
2121
2122 static s32 adpt_i2o_to_scsi(void __iomem *reply, struct scsi_cmnd* cmd)
2123 {
2124         adpt_hba* pHba;
2125         u32 hba_status;
2126         u32 dev_status;
2127         u32 reply_flags = readl(reply) & 0xff00; // Leave it shifted up 8 bits 
2128         // I know this would look cleaner if I just read bytes
2129         // but the model I have been using for all the rest of the
2130         // io is in 4 byte words - so I keep that model
2131         u16 detailed_status = readl(reply+16) &0xffff;
2132         dev_status = (detailed_status & 0xff);
2133         hba_status = detailed_status >> 8;
2134
2135         // calculate resid for sg 
2136         scsi_set_resid(cmd, scsi_bufflen(cmd) - readl(reply+20));
2137
2138         pHba = (adpt_hba*) cmd->device->host->hostdata[0];
2139
2140         cmd->sense_buffer[0] = '\0';  // initialize sense valid flag to false
2141
2142         if(!(reply_flags & MSG_FAIL)) {
2143                 switch(detailed_status & I2O_SCSI_DSC_MASK) {
2144                 case I2O_SCSI_DSC_SUCCESS:
2145                         cmd->result = (DID_OK << 16);
2146                         // handle underflow
2147                         if (readl(reply+20) < cmd->underflow) {
2148                                 cmd->result = (DID_ERROR <<16);
2149                                 printk(KERN_WARNING"%s: SCSI CMD underflow\n",pHba->name);
2150                         }
2151                         break;
2152                 case I2O_SCSI_DSC_REQUEST_ABORTED:
2153                         cmd->result = (DID_ABORT << 16);
2154                         break;
2155                 case I2O_SCSI_DSC_PATH_INVALID:
2156                 case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
2157                 case I2O_SCSI_DSC_SELECTION_TIMEOUT:
2158                 case I2O_SCSI_DSC_COMMAND_TIMEOUT:
2159                 case I2O_SCSI_DSC_NO_ADAPTER:
2160                 case I2O_SCSI_DSC_RESOURCE_UNAVAILABLE:
2161                         printk(KERN_WARNING"%s: SCSI Timeout-Device (%d,%d,%llu) hba status=0x%x, dev status=0x%x, cmd=0x%x\n",
2162                                 pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun, hba_status, dev_status, cmd->cmnd[0]);
2163                         cmd->result = (DID_TIME_OUT << 16);
2164                         break;
2165                 case I2O_SCSI_DSC_ADAPTER_BUSY:
2166                 case I2O_SCSI_DSC_BUS_BUSY:
2167                         cmd->result = (DID_BUS_BUSY << 16);
2168                         break;
2169                 case I2O_SCSI_DSC_SCSI_BUS_RESET:
2170                 case I2O_SCSI_DSC_BDR_MESSAGE_SENT:
2171                         cmd->result = (DID_RESET << 16);
2172                         break;
2173                 case I2O_SCSI_DSC_PARITY_ERROR_FAILURE:
2174                         printk(KERN_WARNING"%s: SCSI CMD parity error\n",pHba->name);
2175                         cmd->result = (DID_PARITY << 16);
2176                         break;
2177                 case I2O_SCSI_DSC_UNABLE_TO_ABORT:
2178                 case I2O_SCSI_DSC_COMPLETE_WITH_ERROR:
2179                 case I2O_SCSI_DSC_UNABLE_TO_TERMINATE:
2180                 case I2O_SCSI_DSC_MR_MESSAGE_RECEIVED:
2181                 case I2O_SCSI_DSC_AUTOSENSE_FAILED:
2182                 case I2O_SCSI_DSC_DATA_OVERRUN:
2183                 case I2O_SCSI_DSC_UNEXPECTED_BUS_FREE:
2184                 case I2O_SCSI_DSC_SEQUENCE_FAILURE:
2185                 case I2O_SCSI_DSC_REQUEST_LENGTH_ERROR:
2186                 case I2O_SCSI_DSC_PROVIDE_FAILURE:
2187                 case I2O_SCSI_DSC_REQUEST_TERMINATED:
2188                 case I2O_SCSI_DSC_IDE_MESSAGE_SENT:
2189                 case I2O_SCSI_DSC_UNACKNOWLEDGED_EVENT:
2190                 case I2O_SCSI_DSC_MESSAGE_RECEIVED:
2191                 case I2O_SCSI_DSC_INVALID_CDB:
2192                 case I2O_SCSI_DSC_LUN_INVALID:
2193                 case I2O_SCSI_DSC_SCSI_TID_INVALID:
2194                 case I2O_SCSI_DSC_FUNCTION_UNAVAILABLE:
2195                 case I2O_SCSI_DSC_NO_NEXUS:
2196                 case I2O_SCSI_DSC_CDB_RECEIVED:
2197                 case I2O_SCSI_DSC_LUN_ALREADY_ENABLED:
2198                 case I2O_SCSI_DSC_QUEUE_FROZEN:
2199                 case I2O_SCSI_DSC_REQUEST_INVALID:
2200                 default:
2201                         printk(KERN_WARNING"%s: SCSI error %0x-Device(%d,%d,%llu) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2202                                 pHba->name, detailed_status & I2O_SCSI_DSC_MASK, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun,
2203                                hba_status, dev_status, cmd->cmnd[0]);
2204                         cmd->result = (DID_ERROR << 16);
2205                         break;
2206                 }
2207
2208                 // copy over the request sense data if it was a check
2209                 // condition status
2210                 if (dev_status == SAM_STAT_CHECK_CONDITION) {
2211                         u32 len = min(SCSI_SENSE_BUFFERSIZE, 40);
2212                         // Copy over the sense data
2213                         memcpy_fromio(cmd->sense_buffer, (reply+28) , len);
2214                         if(cmd->sense_buffer[0] == 0x70 /* class 7 */ && 
2215                            cmd->sense_buffer[2] == DATA_PROTECT ){
2216                                 /* This is to handle an array failed */
2217                                 cmd->result = (DID_TIME_OUT << 16);
2218                                 printk(KERN_WARNING"%s: SCSI Data Protect-Device (%d,%d,%llu) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2219                                         pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun,
2220                                         hba_status, dev_status, cmd->cmnd[0]);
2221
2222                         }
2223                 }
2224         } else {
2225                 /* In this condtion we could not talk to the tid
2226                  * the card rejected it.  We should signal a retry
2227                  * for a limitted number of retries.
2228                  */
2229                 cmd->result = (DID_TIME_OUT << 16);
2230                 printk(KERN_WARNING"%s: I2O MSG_FAIL - Device (%d,%d,%llu) tid=%d, cmd=0x%x\n",
2231                         pHba->name, (u32)cmd->device->channel, (u32)cmd->device->id, cmd->device->lun,
2232                         ((struct adpt_device*)(cmd->device->hostdata))->tid, cmd->cmnd[0]);
2233         }
2234
2235         cmd->result |= (dev_status);
2236
2237         if(cmd->scsi_done != NULL){
2238                 cmd->scsi_done(cmd);
2239         } 
2240         return cmd->result;
2241 }
2242
2243
2244 static s32 adpt_rescan(adpt_hba* pHba)
2245 {
2246         s32 rcode;
2247         ulong flags = 0;
2248
2249         if(pHba->host)
2250                 spin_lock_irqsave(pHba->host->host_lock, flags);
2251         if ((rcode=adpt_i2o_lct_get(pHba)) < 0)
2252                 goto out;
2253         if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0)
2254                 goto out;
2255         rcode = 0;
2256 out:    if(pHba->host)
2257                 spin_unlock_irqrestore(pHba->host->host_lock, flags);
2258         return rcode;
2259 }
2260
2261
2262 static s32 adpt_i2o_reparse_lct(adpt_hba* pHba)
2263 {
2264         int i;
2265         int max;
2266         int tid;
2267         struct i2o_device *d;
2268         i2o_lct *lct = pHba->lct;
2269         u8 bus_no = 0;
2270         s16 scsi_id;
2271         u64 scsi_lun;
2272         u32 buf[10]; // at least 8 u32's
2273         struct adpt_device* pDev = NULL;
2274         struct i2o_device* pI2o_dev = NULL;
2275         
2276         if (lct == NULL) {
2277                 printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
2278                 return -1;
2279         }
2280         
2281         max = lct->table_size;  
2282         max -= 3;
2283         max /= 9;
2284
2285         // Mark each drive as unscanned
2286         for (d = pHba->devices; d; d = d->next) {
2287                 pDev =(struct adpt_device*) d->owner;
2288                 if(!pDev){
2289                         continue;
2290                 }
2291                 pDev->state |= DPTI_DEV_UNSCANNED;
2292         }
2293
2294         printk(KERN_INFO "%s: LCT has %d entries.\n", pHba->name,max);
2295         
2296         for(i=0;i<max;i++) {
2297                 if( lct->lct_entry[i].user_tid != 0xfff){
2298                         continue;
2299                 }
2300
2301                 if( lct->lct_entry[i].class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
2302                     lct->lct_entry[i].class_id == I2O_CLASS_SCSI_PERIPHERAL ||
2303                     lct->lct_entry[i].class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
2304                         tid = lct->lct_entry[i].tid;
2305                         if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
2306                                 printk(KERN_ERR"%s: Could not query device\n",pHba->name);
2307                                 continue;
2308                         }
2309                         bus_no = buf[0]>>16;
2310                         if (bus_no >= MAX_CHANNEL) {    /* Something wrong skip it */
2311                                 printk(KERN_WARNING
2312                                         "%s: Channel number %d out of range\n",
2313                                         pHba->name, bus_no);
2314                                 continue;
2315                         }
2316
2317                         scsi_id = buf[1];
2318                         scsi_lun = scsilun_to_int((struct scsi_lun *)&buf[2]);
2319                         pDev = pHba->channel[bus_no].device[scsi_id];
2320                         /* da lun */
2321                         while(pDev) {
2322                                 if(pDev->scsi_lun == scsi_lun) {
2323                                         break;
2324                                 }
2325                                 pDev = pDev->next_lun;
2326                         }
2327                         if(!pDev ) { // Something new add it
2328                                 d = kmalloc(sizeof(struct i2o_device),
2329                                             GFP_ATOMIC);
2330                                 if(d==NULL)
2331                                 {
2332                                         printk(KERN_CRIT "Out of memory for I2O device data.\n");
2333                                         return -ENOMEM;
2334                                 }
2335                                 
2336                                 d->controller = pHba;
2337                                 d->next = NULL;
2338
2339                                 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2340
2341                                 d->flags = 0;
2342                                 adpt_i2o_report_hba_unit(pHba, d);
2343                                 adpt_i2o_install_device(pHba, d);
2344         
2345                                 pDev = pHba->channel[bus_no].device[scsi_id];   
2346                                 if( pDev == NULL){
2347                                         pDev =
2348                                           kzalloc(sizeof(struct adpt_device),
2349                                                   GFP_ATOMIC);
2350                                         if(pDev == NULL) {
2351                                                 return -ENOMEM;
2352                                         }
2353                                         pHba->channel[bus_no].device[scsi_id] = pDev;
2354                                 } else {
2355                                         while (pDev->next_lun) {
2356                                                 pDev = pDev->next_lun;
2357                                         }
2358                                         pDev = pDev->next_lun =
2359                                           kzalloc(sizeof(struct adpt_device),
2360                                                   GFP_ATOMIC);
2361                                         if(pDev == NULL) {
2362                                                 return -ENOMEM;
2363                                         }
2364                                 }
2365                                 pDev->tid = d->lct_data.tid;
2366                                 pDev->scsi_channel = bus_no;
2367                                 pDev->scsi_id = scsi_id;
2368                                 pDev->scsi_lun = scsi_lun;
2369                                 pDev->pI2o_dev = d;
2370                                 d->owner = pDev;
2371                                 pDev->type = (buf[0])&0xff;
2372                                 pDev->flags = (buf[0]>>8)&0xff;
2373                                 // Too late, SCSI system has made up it's mind, but what the hey ...
2374                                 if(scsi_id > pHba->top_scsi_id){
2375                                         pHba->top_scsi_id = scsi_id;
2376                                 }
2377                                 if(scsi_lun > pHba->top_scsi_lun){
2378                                         pHba->top_scsi_lun = scsi_lun;
2379                                 }
2380                                 continue;
2381                         } // end of new i2o device
2382
2383                         // We found an old device - check it
2384                         while(pDev) {
2385                                 if(pDev->scsi_lun == scsi_lun) {
2386                                         if(!scsi_device_online(pDev->pScsi_dev)) {
2387                                                 printk(KERN_WARNING"%s: Setting device (%d,%d,%llu) back online\n",
2388                                                                 pHba->name,bus_no,scsi_id,scsi_lun);
2389                                                 if (pDev->pScsi_dev) {
2390                                                         scsi_device_set_state(pDev->pScsi_dev, SDEV_RUNNING);
2391                                                 }
2392                                         }
2393                                         d = pDev->pI2o_dev;
2394                                         if(d->lct_data.tid != tid) { // something changed
2395                                                 pDev->tid = tid;
2396                                                 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2397                                                 if (pDev->pScsi_dev) {
2398                                                         pDev->pScsi_dev->changed = TRUE;
2399                                                         pDev->pScsi_dev->removable = TRUE;
2400                                                 }
2401                                         }
2402                                         // Found it - mark it scanned
2403                                         pDev->state = DPTI_DEV_ONLINE;
2404                                         break;
2405                                 }
2406                                 pDev = pDev->next_lun;
2407                         }
2408                 }
2409         }
2410         for (pI2o_dev = pHba->devices; pI2o_dev; pI2o_dev = pI2o_dev->next) {
2411                 pDev =(struct adpt_device*) pI2o_dev->owner;
2412                 if(!pDev){
2413                         continue;
2414                 }
2415                 // Drive offline drives that previously existed but could not be found
2416                 // in the LCT table
2417                 if (pDev->state & DPTI_DEV_UNSCANNED){
2418                         pDev->state = DPTI_DEV_OFFLINE;
2419                         printk(KERN_WARNING"%s: Device (%d,%d,%llu) offline\n",pHba->name,pDev->scsi_channel,pDev->scsi_id,pDev->scsi_lun);
2420                         if (pDev->pScsi_dev) {
2421                                 scsi_device_set_state(pDev->pScsi_dev, SDEV_OFFLINE);
2422                         }
2423                 }
2424         }
2425         return 0;
2426 }
2427
2428 static void adpt_fail_posted_scbs(adpt_hba* pHba)
2429 {
2430         struct scsi_cmnd*       cmd = NULL;
2431         struct scsi_device*     d = NULL;
2432
2433         shost_for_each_device(d, pHba->host) {
2434                 unsigned long flags;
2435                 spin_lock_irqsave(&d->list_lock, flags);
2436                 list_for_each_entry(cmd, &d->cmd_list, list) {
2437                         if(cmd->serial_number == 0){
2438                                 continue;
2439                         }
2440                         cmd->result = (DID_OK << 16) | (QUEUE_FULL <<1);
2441                         cmd->scsi_done(cmd);
2442                 }
2443                 spin_unlock_irqrestore(&d->list_lock, flags);
2444         }
2445 }
2446
2447
2448 /*============================================================================
2449  *  Routines from i2o subsystem
2450  *============================================================================
2451  */
2452
2453
2454
2455 /*
2456  *      Bring an I2O controller into HOLD state. See the spec.
2457  */
2458 static int adpt_i2o_activate_hba(adpt_hba* pHba)
2459 {
2460         int rcode;
2461
2462         if(pHba->initialized ) {
2463                 if (adpt_i2o_status_get(pHba) < 0) {
2464                         if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2465                                 printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2466                                 return rcode;
2467                         }
2468                         if (adpt_i2o_status_get(pHba) < 0) {
2469                                 printk(KERN_INFO "HBA not responding.\n");
2470                                 return -1;
2471                         }
2472                 }
2473
2474                 if(pHba->status_block->iop_state == ADAPTER_STATE_FAULTED) {
2475                         printk(KERN_CRIT "%s: hardware fault\n", pHba->name);
2476                         return -1;
2477                 }
2478
2479                 if (pHba->status_block->iop_state == ADAPTER_STATE_READY ||
2480                     pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
2481                     pHba->status_block->iop_state == ADAPTER_STATE_HOLD ||
2482                     pHba->status_block->iop_state == ADAPTER_STATE_FAILED) {
2483                         adpt_i2o_reset_hba(pHba);                       
2484                         if (adpt_i2o_status_get(pHba) < 0 || pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
2485                                 printk(KERN_ERR "%s: Failed to initialize.\n", pHba->name);
2486                                 return -1;
2487                         }
2488                 }
2489         } else {
2490                 if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2491                         printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2492                         return rcode;
2493                 }
2494
2495         }
2496
2497         if (adpt_i2o_init_outbound_q(pHba) < 0) {
2498                 return -1;
2499         }
2500
2501         /* In HOLD state */
2502         
2503         if (adpt_i2o_hrt_get(pHba) < 0) {
2504                 return -1;
2505         }
2506
2507         return 0;
2508 }
2509
2510 /*
2511  *      Bring a controller online into OPERATIONAL state. 
2512  */
2513  
2514 static int adpt_i2o_online_hba(adpt_hba* pHba)
2515 {
2516         if (adpt_i2o_systab_send(pHba) < 0)
2517                 return -1;
2518         /* In READY state */
2519
2520         if (adpt_i2o_enable_hba(pHba) < 0)
2521                 return -1;
2522
2523         /* In OPERATIONAL state  */
2524         return 0;
2525 }
2526
2527 static s32 adpt_send_nop(adpt_hba*pHba,u32 m)
2528 {
2529         u32 __iomem *msg;
2530         ulong timeout = jiffies + 5*HZ;
2531
2532         while(m == EMPTY_QUEUE){
2533                 rmb();
2534                 m = readl(pHba->post_port);
2535                 if(m != EMPTY_QUEUE){
2536                         break;
2537                 }
2538                 if(time_after(jiffies,timeout)){
2539                         printk(KERN_ERR "%s: Timeout waiting for message frame!\n",pHba->name);
2540                         return 2;
2541                 }
2542                 schedule_timeout_uninterruptible(1);
2543         }
2544         msg = (u32 __iomem *)(pHba->msg_addr_virt + m);
2545         writel( THREE_WORD_MSG_SIZE | SGL_OFFSET_0,&msg[0]);
2546         writel( I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0,&msg[1]);
2547         writel( 0,&msg[2]);
2548         wmb();
2549
2550         writel(m, pHba->post_port);
2551         wmb();
2552         return 0;
2553 }
2554
2555 static s32 adpt_i2o_init_outbound_q(adpt_hba* pHba)
2556 {
2557         u8 *status;
2558         dma_addr_t addr;
2559         u32 __iomem *msg = NULL;
2560         int i;
2561         ulong timeout = jiffies + TMOUT_INITOUTBOUND*HZ;
2562         u32 m;
2563
2564         do {
2565                 rmb();
2566                 m = readl(pHba->post_port);
2567                 if (m != EMPTY_QUEUE) {
2568                         break;
2569                 }
2570
2571                 if(time_after(jiffies,timeout)){
2572                         printk(KERN_WARNING"%s: Timeout waiting for message frame\n",pHba->name);
2573                         return -ETIMEDOUT;
2574                 }
2575                 schedule_timeout_uninterruptible(1);
2576         } while(m == EMPTY_QUEUE);
2577
2578         msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
2579
2580         status = dma_alloc_coherent(&pHba->pDev->dev, 4, &addr, GFP_KERNEL);
2581         if (!status) {
2582                 adpt_send_nop(pHba, m);
2583                 printk(KERN_WARNING"%s: IOP reset failed - no free memory.\n",
2584                         pHba->name);
2585                 return -ENOMEM;
2586         }
2587         memset(status, 0, 4);
2588
2589         writel(EIGHT_WORD_MSG_SIZE| SGL_OFFSET_6, &msg[0]);
2590         writel(I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID, &msg[1]);
2591         writel(0, &msg[2]);
2592         writel(0x0106, &msg[3]);        /* Transaction context */
2593         writel(4096, &msg[4]);          /* Host page frame size */
2594         writel((REPLY_FRAME_SIZE)<<16|0x80, &msg[5]);   /* Outbound msg frame size and Initcode */
2595         writel(0xD0000004, &msg[6]);            /* Simple SG LE, EOB */
2596         writel((u32)addr, &msg[7]);
2597
2598         writel(m, pHba->post_port);
2599         wmb();
2600
2601         // Wait for the reply status to come back
2602         do {
2603                 if (*status) {
2604                         if (*status != 0x01 /*I2O_EXEC_OUTBOUND_INIT_IN_PROGRESS*/) {
2605                                 break;
2606                         }
2607                 }
2608                 rmb();
2609                 if(time_after(jiffies,timeout)){
2610                         printk(KERN_WARNING"%s: Timeout Initializing\n",pHba->name);
2611                         /* We lose 4 bytes of "status" here, but we
2612                            cannot free these because controller may
2613                            awake and corrupt those bytes at any time */
2614                         /* dma_free_coherent(&pHba->pDev->dev, 4, status, addr); */
2615                         return -ETIMEDOUT;
2616                 }
2617                 schedule_timeout_uninterruptible(1);
2618         } while (1);
2619
2620         // If the command was successful, fill the fifo with our reply
2621         // message packets
2622         if(*status != 0x04 /*I2O_EXEC_OUTBOUND_INIT_COMPLETE*/) {
2623                 dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
2624                 return -2;
2625         }
2626         dma_free_coherent(&pHba->pDev->dev, 4, status, addr);
2627
2628         if(pHba->reply_pool != NULL) {
2629                 dma_free_coherent(&pHba->pDev->dev,
2630                         pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
2631                         pHba->reply_pool, pHba->reply_pool_pa);
2632         }
2633
2634         pHba->reply_pool = dma_alloc_coherent(&pHba->pDev->dev,
2635                                 pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4,
2636                                 &pHba->reply_pool_pa, GFP_KERNEL);
2637         if (!pHba->reply_pool) {
2638                 printk(KERN_ERR "%s: Could not allocate reply pool\n", pHba->name);
2639                 return -ENOMEM;
2640         }
2641         memset(pHba->reply_pool, 0 , pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4);
2642
2643         for(i = 0; i < pHba->reply_fifo_size; i++) {
2644                 writel(pHba->reply_pool_pa + (i * REPLY_FRAME_SIZE * 4),
2645                         pHba->reply_port);
2646                 wmb();
2647         }
2648         adpt_i2o_status_get(pHba);
2649         return 0;
2650 }
2651
2652
2653 /*
2654  * I2O System Table.  Contains information about
2655  * all the IOPs in the system.  Used to inform IOPs
2656  * about each other's existence.
2657  *
2658  * sys_tbl_ver is the CurrentChangeIndicator that is
2659  * used by IOPs to track changes.
2660  */
2661
2662
2663
2664 static s32 adpt_i2o_status_get(adpt_hba* pHba)
2665 {
2666         ulong timeout;
2667         u32 m;
2668         u32 __iomem *msg;
2669         u8 *status_block=NULL;
2670
2671         if(pHba->status_block == NULL) {
2672                 pHba->status_block = dma_alloc_coherent(&pHba->pDev->dev,
2673                                         sizeof(i2o_status_block),
2674                                         &pHba->status_block_pa, GFP_KERNEL);
2675                 if(pHba->status_block == NULL) {
2676                         printk(KERN_ERR
2677                         "dpti%d: Get Status Block failed; Out of memory. \n", 
2678                         pHba->unit);
2679                         return -ENOMEM;
2680                 }
2681         }
2682         memset(pHba->status_block, 0, sizeof(i2o_status_block));
2683         status_block = (u8*)(pHba->status_block);
2684         timeout = jiffies+TMOUT_GETSTATUS*HZ;
2685         do {
2686                 rmb();
2687                 m = readl(pHba->post_port);
2688                 if (m != EMPTY_QUEUE) {
2689                         break;
2690                 }
2691                 if(time_after(jiffies,timeout)){
2692                         printk(KERN_ERR "%s: Timeout waiting for message !\n",
2693                                         pHba->name);
2694                         return -ETIMEDOUT;
2695                 }
2696                 schedule_timeout_uninterruptible(1);
2697         } while(m==EMPTY_QUEUE);
2698
2699         
2700         msg=(u32 __iomem *)(pHba->msg_addr_virt+m);
2701
2702         writel(NINE_WORD_MSG_SIZE|SGL_OFFSET_0, &msg[0]);
2703         writel(I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID, &msg[1]);
2704         writel(1, &msg[2]);
2705         writel(0, &msg[3]);
2706         writel(0, &msg[4]);
2707         writel(0, &msg[5]);
2708         writel( dma_low(pHba->status_block_pa), &msg[6]);
2709         writel( dma_high(pHba->status_block_pa), &msg[7]);
2710         writel(sizeof(i2o_status_block), &msg[8]); // 88 bytes
2711
2712         //post message
2713         writel(m, pHba->post_port);
2714         wmb();
2715
2716         while(status_block[87]!=0xff){
2717                 if(time_after(jiffies,timeout)){
2718                         printk(KERN_ERR"dpti%d: Get status timeout.\n",
2719                                 pHba->unit);
2720                         return -ETIMEDOUT;
2721                 }
2722                 rmb();
2723                 schedule_timeout_uninterruptible(1);
2724         }
2725
2726         // Set up our number of outbound and inbound messages
2727         pHba->post_fifo_size = pHba->status_block->max_inbound_frames;
2728         if (pHba->post_fifo_size > MAX_TO_IOP_MESSAGES) {
2729                 pHba->post_fifo_size = MAX_TO_IOP_MESSAGES;
2730         }
2731
2732         pHba->reply_fifo_size = pHba->status_block->max_outbound_frames;
2733         if (pHba->reply_fifo_size > MAX_FROM_IOP_MESSAGES) {
2734                 pHba->reply_fifo_size = MAX_FROM_IOP_MESSAGES;
2735         }
2736
2737         // Calculate the Scatter Gather list size
2738         if (dpt_dma64(pHba)) {
2739                 pHba->sg_tablesize
2740                   = ((pHba->status_block->inbound_frame_size * 4
2741                   - 14 * sizeof(u32))
2742                   / (sizeof(struct sg_simple_element) + sizeof(u32)));
2743         } else {
2744                 pHba->sg_tablesize
2745                   = ((pHba->status_block->inbound_frame_size * 4
2746                   - 12 * sizeof(u32))
2747                   / sizeof(struct sg_simple_element));
2748         }
2749         if (pHba->sg_tablesize > SG_LIST_ELEMENTS) {
2750                 pHba->sg_tablesize = SG_LIST_ELEMENTS;
2751         }
2752
2753
2754 #ifdef DEBUG
2755         printk("dpti%d: State = ",pHba->unit);
2756         switch(pHba->status_block->iop_state) {
2757                 case 0x01:
2758                         printk("INIT\n");
2759                         break;
2760                 case 0x02:
2761                         printk("RESET\n");
2762                         break;
2763                 case 0x04:
2764                         printk("HOLD\n");
2765                         break;
2766                 case 0x05:
2767                         printk("READY\n");
2768                         break;
2769                 case 0x08:
2770                         printk("OPERATIONAL\n");
2771                         break;
2772                 case 0x10:
2773                         printk("FAILED\n");
2774                         break;
2775                 case 0x11:
2776                         printk("FAULTED\n");
2777                         break;
2778                 default:
2779                         printk("%x (unknown!!)\n",pHba->status_block->iop_state);
2780         }
2781 #endif
2782         return 0;
2783 }
2784
2785 /*
2786  * Get the IOP's Logical Configuration Table
2787  */
2788 static int adpt_i2o_lct_get(adpt_hba* pHba)
2789 {
2790         u32 msg[8];
2791         int ret;
2792         u32 buf[16];
2793
2794         if ((pHba->lct_size == 0) || (pHba->lct == NULL)){
2795                 pHba->lct_size = pHba->status_block->expected_lct_size;
2796         }
2797         do {
2798                 if (pHba->lct == NULL) {
2799                         pHba->lct = dma_alloc_coherent(&pHba->pDev->dev,
2800                                         pHba->lct_size, &pHba->lct_pa,
2801                                         GFP_ATOMIC);
2802                         if(pHba->lct == NULL) {
2803                                 printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
2804                                         pHba->name);
2805                                 return -ENOMEM;
2806                         }
2807                 }
2808                 memset(pHba->lct, 0, pHba->lct_size);
2809
2810                 msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
2811                 msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
2812                 msg[2] = 0;
2813                 msg[3] = 0;
2814                 msg[4] = 0xFFFFFFFF;    /* All devices */
2815                 msg[5] = 0x00000000;    /* Report now */
2816                 msg[6] = 0xD0000000|pHba->lct_size;
2817                 msg[7] = (u32)pHba->lct_pa;
2818
2819                 if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 360))) {
2820                         printk(KERN_ERR "%s: LCT Get failed (status=%#10x.\n", 
2821                                 pHba->name, ret);       
2822                         printk(KERN_ERR"Adaptec: Error Reading Hardware.\n");
2823                         return ret;
2824                 }
2825
2826                 if ((pHba->lct->table_size << 2) > pHba->lct_size) {
2827                         pHba->lct_size = pHba->lct->table_size << 2;
2828                         dma_free_coherent(&pHba->pDev->dev, pHba->lct_size,
2829                                         pHba->lct, pHba->lct_pa);
2830                         pHba->lct = NULL;
2831                 }
2832         } while (pHba->lct == NULL);
2833
2834         PDEBUG("%s: Hardware resource table read.\n", pHba->name);
2835
2836
2837         // I2O_DPT_EXEC_IOP_BUFFERS_GROUP_NO;
2838         if(adpt_i2o_query_scalar(pHba, 0 , 0x8000, -1, buf, sizeof(buf))>=0) {
2839                 pHba->FwDebugBufferSize = buf[1];
2840                 pHba->FwDebugBuffer_P = ioremap(pHba->base_addr_phys + buf[0],
2841                                                 pHba->FwDebugBufferSize);
2842                 if (pHba->FwDebugBuffer_P) {
2843                         pHba->FwDebugFlags_P     = pHba->FwDebugBuffer_P +
2844                                                         FW_DEBUG_FLAGS_OFFSET;
2845                         pHba->FwDebugBLEDvalue_P = pHba->FwDebugBuffer_P +
2846                                                         FW_DEBUG_BLED_OFFSET;
2847                         pHba->FwDebugBLEDflag_P  = pHba->FwDebugBLEDvalue_P + 1;
2848                         pHba->FwDebugStrLength_P = pHba->FwDebugBuffer_P +
2849                                                 FW_DEBUG_STR_LENGTH_OFFSET;
2850                         pHba->FwDebugBuffer_P += buf[2]; 
2851                         pHba->FwDebugFlags = 0;
2852                 }
2853         }
2854
2855         return 0;
2856 }
2857
2858 static int adpt_i2o_build_sys_table(void)
2859 {
2860         adpt_hba* pHba = hba_chain;
2861         int count = 0;
2862
2863         if (sys_tbl)
2864                 dma_free_coherent(&pHba->pDev->dev, sys_tbl_len,
2865                                         sys_tbl, sys_tbl_pa);
2866
2867         sys_tbl_len = sizeof(struct i2o_sys_tbl) +      // Header + IOPs
2868                                 (hba_count) * sizeof(struct i2o_sys_tbl_entry);
2869
2870         sys_tbl = dma_alloc_coherent(&pHba->pDev->dev,
2871                                 sys_tbl_len, &sys_tbl_pa, GFP_KERNEL);
2872         if (!sys_tbl) {
2873                 printk(KERN_WARNING "SysTab Set failed. Out of memory.\n");     
2874                 return -ENOMEM;
2875         }
2876         memset(sys_tbl, 0, sys_tbl_len);
2877
2878         sys_tbl->num_entries = hba_count;
2879         sys_tbl->version = I2OVERSION;
2880         sys_tbl->change_ind = sys_tbl_ind++;
2881
2882         for(pHba = hba_chain; pHba; pHba = pHba->next) {
2883                 u64 addr;
2884                 // Get updated Status Block so we have the latest information
2885                 if (adpt_i2o_status_get(pHba)) {
2886                         sys_tbl->num_entries--;
2887                         continue; // try next one       
2888                 }
2889
2890                 sys_tbl->iops[count].org_id = pHba->status_block->org_id;
2891                 sys_tbl->iops[count].iop_id = pHba->unit + 2;
2892                 sys_tbl->iops[count].seg_num = 0;
2893                 sys_tbl->iops[count].i2o_version = pHba->status_block->i2o_version;
2894                 sys_tbl->iops[count].iop_state = pHba->status_block->iop_state;
2895                 sys_tbl->iops[count].msg_type = pHba->status_block->msg_type;
2896                 sys_tbl->iops[count].frame_size = pHba->status_block->inbound_frame_size;
2897                 sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
2898                 sys_tbl->iops[count].iop_capabilities = pHba->status_block->iop_capabilities;
2899                 addr = pHba->base_addr_phys + 0x40;
2900                 sys_tbl->iops[count].inbound_low = dma_low(addr);
2901                 sys_tbl->iops[count].inbound_high = dma_high(addr);
2902
2903                 count++;
2904         }
2905
2906 #ifdef DEBUG
2907 {
2908         u32 *table = (u32*)sys_tbl;
2909         printk(KERN_DEBUG"sys_tbl_len=%d in 32bit words\n",(sys_tbl_len >>2));
2910         for(count = 0; count < (sys_tbl_len >>2); count++) {
2911                 printk(KERN_INFO "sys_tbl[%d] = %0#10x\n", 
2912                         count, table[count]);
2913         }
2914 }
2915 #endif
2916
2917         return 0;
2918 }
2919
2920
2921 /*
2922  *       Dump the information block associated with a given unit (TID)
2923  */
2924  
2925 static void adpt_i2o_report_hba_unit(adpt_hba* pHba, struct i2o_device *d)
2926 {
2927         char buf[64];
2928         int unit = d->lct_data.tid;
2929
2930         printk(KERN_INFO "TID %3.3d ", unit);
2931
2932         if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 3, buf, 16)>=0)
2933         {
2934                 buf[16]=0;
2935                 printk(" Vendor: %-12.12s", buf);
2936         }
2937         if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 4, buf, 16)>=0)
2938         {
2939                 buf[16]=0;
2940                 printk(" Device: %-12.12s", buf);
2941         }
2942         if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 6, buf, 8)>=0)
2943         {
2944                 buf[8]=0;
2945                 printk(" Rev: %-12.12s\n", buf);
2946         }
2947 #ifdef DEBUG
2948          printk(KERN_INFO "\tClass: %.21s\n", adpt_i2o_get_class_name(d->lct_data.class_id));
2949          printk(KERN_INFO "\tSubclass: 0x%04X\n", d->lct_data.sub_class);
2950          printk(KERN_INFO "\tFlags: ");
2951
2952          if(d->lct_data.device_flags&(1<<0))
2953                   printk("C");       // ConfigDialog requested
2954          if(d->lct_data.device_flags&(1<<1))
2955                   printk("U");       // Multi-user capable
2956          if(!(d->lct_data.device_flags&(1<<4)))
2957                   printk("P");       // Peer service enabled!
2958          if(!(d->lct_data.device_flags&(1<<5)))
2959                   printk("M");       // Mgmt service enabled!
2960          printk("\n");
2961 #endif
2962 }
2963
2964 #ifdef DEBUG
2965 /*
2966  *      Do i2o class name lookup
2967  */
2968 static const char *adpt_i2o_get_class_name(int class)
2969 {
2970         int idx = 16;
2971         static char *i2o_class_name[] = {
2972                 "Executive",
2973                 "Device Driver Module",
2974                 "Block Device",
2975                 "Tape Device",
2976                 "LAN Interface",
2977                 "WAN Interface",
2978                 "Fibre Channel Port",
2979                 "Fibre Channel Device",
2980                 "SCSI Device",
2981                 "ATE Port",
2982                 "ATE Device",
2983                 "Floppy Controller",
2984                 "Floppy Device",
2985                 "Secondary Bus Port",
2986                 "Peer Transport Agent",
2987                 "Peer Transport",
2988                 "Unknown"
2989         };
2990         
2991         switch(class&0xFFF) {
2992         case I2O_CLASS_EXECUTIVE:
2993                 idx = 0; break;
2994         case I2O_CLASS_DDM:
2995                 idx = 1; break;
2996         case I2O_CLASS_RANDOM_BLOCK_STORAGE:
2997                 idx = 2; break;
2998         case I2O_CLASS_SEQUENTIAL_STORAGE:
2999                 idx = 3; break;
3000         case I2O_CLASS_LAN:
3001                 idx = 4; break;
3002         case I2O_CLASS_WAN:
3003                 idx = 5; break;
3004         case I2O_CLASS_FIBRE_CHANNEL_PORT:
3005                 idx = 6; break;
3006         case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
3007                 idx = 7; break;
3008         case I2O_CLASS_SCSI_PERIPHERAL:
3009                 idx = 8; break;
3010         case I2O_CLASS_ATE_PORT:
3011                 idx = 9; break;
3012         case I2O_CLASS_ATE_PERIPHERAL:
3013                 idx = 10; break;
3014         case I2O_CLASS_FLOPPY_CONTROLLER:
3015                 idx = 11; break;
3016         case I2O_CLASS_FLOPPY_DEVICE:
3017                 idx = 12; break;
3018         case I2O_CLASS_BUS_ADAPTER_PORT:
3019                 idx = 13; break;
3020         case I2O_CLASS_PEER_TRANSPORT_AGENT:
3021                 idx = 14; break;
3022         case I2O_CLASS_PEER_TRANSPORT:
3023                 idx = 15; break;
3024         }
3025         return i2o_class_name[idx];
3026 }
3027 #endif
3028
3029
3030 static s32 adpt_i2o_hrt_get(adpt_hba* pHba)
3031 {
3032         u32 msg[6];
3033         int ret, size = sizeof(i2o_hrt);
3034
3035         do {
3036                 if (pHba->hrt == NULL) {
3037                         pHba->hrt = dma_alloc_coherent(&pHba->pDev->dev,
3038                                         size, &pHba->hrt_pa, GFP_KERNEL);
3039                         if (pHba->hrt == NULL) {
3040                                 printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", pHba->name);
3041                                 return -ENOMEM;
3042                         }
3043                 }
3044
3045                 msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
3046                 msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
3047                 msg[2]= 0;
3048                 msg[3]= 0;
3049                 msg[4]= (0xD0000000 | size);    /* Simple transaction */
3050                 msg[5]= (u32)pHba->hrt_pa;      /* Dump it here */
3051
3052                 if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg),20))) {
3053                         printk(KERN_ERR "%s: Unable to get HRT (status=%#10x)\n", pHba->name, ret);
3054                         return ret;
3055                 }
3056
3057                 if (pHba->hrt->num_entries * pHba->hrt->entry_len << 2 > size) {
3058                         int newsize = pHba->hrt->num_entries * pHba->hrt->entry_len << 2;
3059                         dma_free_coherent(&pHba->pDev->dev, size,
3060                                 pHba->hrt, pHba->hrt_pa);
3061                         size = newsize;
3062                         pHba->hrt = NULL;
3063                 }
3064         } while(pHba->hrt == NULL);
3065         return 0;
3066 }                                                                                                                                       
3067
3068 /*
3069  *       Query one scalar group value or a whole scalar group.
3070  */                     
3071 static int adpt_i2o_query_scalar(adpt_hba* pHba, int tid, 
3072                         int group, int field, void *buf, int buflen)
3073 {
3074         u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
3075         u8 *opblk_va;
3076         dma_addr_t opblk_pa;
3077         u8 *resblk_va;
3078         dma_addr_t resblk_pa;
3079
3080         int size;
3081
3082         /* 8 bytes for header */
3083         resblk_va = dma_alloc_coherent(&pHba->pDev->dev,
3084                         sizeof(u8) * (8 + buflen), &resblk_pa, GFP_KERNEL);
3085         if (resblk_va == NULL) {
3086                 printk(KERN_CRIT "%s: query scalar failed; Out of memory.\n", pHba->name);
3087                 return -ENOMEM;
3088         }
3089
3090         opblk_va = dma_alloc_coherent(&pHba->pDev->dev,
3091                         sizeof(opblk), &opblk_pa, GFP_KERNEL);
3092         if (opblk_va == NULL) {
3093                 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3094                         resblk_va, resblk_pa);
3095                 printk(KERN_CRIT "%s: query operation failed; Out of memory.\n",
3096                         pHba->name);
3097                 return -ENOMEM;
3098         }
3099         if (field == -1)                /* whole group */
3100                         opblk[4] = -1;
3101
3102         memcpy(opblk_va, opblk, sizeof(opblk));
3103         size = adpt_i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, pHba, tid, 
3104                 opblk_va, opblk_pa, sizeof(opblk),
3105                 resblk_va, resblk_pa, sizeof(u8)*(8+buflen));
3106         dma_free_coherent(&pHba->pDev->dev, sizeof(opblk), opblk_va, opblk_pa);
3107         if (size == -ETIME) {
3108                 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3109                                                         resblk_va, resblk_pa);
3110                 printk(KERN_WARNING "%s: issue params failed; Timed out.\n", pHba->name);
3111                 return -ETIME;
3112         } else if (size == -EINTR) {
3113                 dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3114                                                         resblk_va, resblk_pa);
3115                 printk(KERN_WARNING "%s: issue params failed; Interrupted.\n", pHba->name);
3116                 return -EINTR;
3117         }
3118                         
3119         memcpy(buf, resblk_va+8, buflen);  /* cut off header */
3120
3121         dma_free_coherent(&pHba->pDev->dev, sizeof(u8) * (8+buflen),
3122                                                 resblk_va, resblk_pa);
3123         if (size < 0)
3124                 return size;    
3125
3126         return buflen;
3127 }
3128
3129
3130 /*      Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
3131  *
3132  *      This function can be used for all UtilParamsGet/Set operations.
3133  *      The OperationBlock is given in opblk-buffer, 
3134  *      and results are returned in resblk-buffer.
3135  *      Note that the minimum sized resblk is 8 bytes and contains
3136  *      ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
3137  */
3138 static int adpt_i2o_issue_params(int cmd, adpt_hba* pHba, int tid, 
3139                   void *opblk_va,  dma_addr_t opblk_pa, int oplen,
3140                 void *resblk_va, dma_addr_t resblk_pa, int reslen)
3141 {
3142         u32 msg[9]; 
3143         u32 *res = (u32 *)resblk_va;
3144         int wait_status;
3145
3146         msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
3147         msg[1] = cmd << 24 | HOST_TID << 12 | tid; 
3148         msg[2] = 0;
3149         msg[3] = 0;
3150         msg[4] = 0;
3151         msg[5] = 0x54000000 | oplen;    /* OperationBlock */
3152         msg[6] = (u32)opblk_pa;
3153         msg[7] = 0xD0000000 | reslen;   /* ResultBlock */
3154         msg[8] = (u32)resblk_pa;
3155
3156         if ((wait_status = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 20))) {
3157                 printk("adpt_i2o_issue_params: post_wait failed (%p)\n", resblk_va);
3158                 return wait_status;     /* -DetailedStatus */
3159         }
3160
3161         if (res[1]&0x00FF0000) {        /* BlockStatus != SUCCESS */
3162                 printk(KERN_WARNING "%s: %s - Error:\n  ErrorInfoSize = 0x%02x, "
3163                         "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
3164                         pHba->name,
3165                         (cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
3166                                                          : "PARAMS_GET",   
3167                         res[1]>>24, (res[1]>>16)&0xFF, res[1]&0xFFFF);
3168                 return -((res[1] >> 16) & 0xFF); /* -BlockStatus */
3169         }
3170
3171          return 4 + ((res[1] & 0x0000FFFF) << 2); /* bytes used in resblk */ 
3172 }
3173
3174
3175 static s32 adpt_i2o_quiesce_hba(adpt_hba* pHba)
3176 {
3177         u32 msg[4];
3178         int ret;
3179
3180         adpt_i2o_status_get(pHba);
3181
3182         /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
3183
3184         if((pHba->status_block->iop_state != ADAPTER_STATE_READY) &&
3185            (pHba->status_block->iop_state != ADAPTER_STATE_OPERATIONAL)){
3186                 return 0;
3187         }
3188
3189         msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3190         msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
3191         msg[2] = 0;
3192         msg[3] = 0;
3193
3194         if((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3195                 printk(KERN_INFO"dpti%d: Unable to quiesce (status=%#x).\n",
3196                                 pHba->unit, -ret);
3197         } else {
3198                 printk(KERN_INFO"dpti%d: Quiesced.\n",pHba->unit);
3199         }
3200
3201         adpt_i2o_status_get(pHba);
3202         return ret;
3203 }
3204
3205
3206 /* 
3207  * Enable IOP. Allows the IOP to resume external operations.
3208  */
3209 static int adpt_i2o_enable_hba(adpt_hba* pHba)
3210 {
3211         u32 msg[4];
3212         int ret;
3213         
3214         adpt_i2o_status_get(pHba);
3215         if(!pHba->status_block){
3216                 return -ENOMEM;
3217         }
3218         /* Enable only allowed on READY state */
3219         if(pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
3220                 return 0;
3221
3222         if(pHba->status_block->iop_state != ADAPTER_STATE_READY)
3223                 return -EINVAL;
3224
3225         msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3226         msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
3227         msg[2]= 0;
3228         msg[3]= 0;
3229
3230         if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3231                 printk(KERN_WARNING"%s: Could not enable (status=%#10x).\n", 
3232                         pHba->name, ret);
3233         } else {
3234                 PDEBUG("%s: Enabled.\n", pHba->name);
3235         }
3236
3237         adpt_i2o_status_get(pHba);
3238         return ret;
3239 }
3240
3241
3242 static int adpt_i2o_systab_send(adpt_hba* pHba)
3243 {
3244          u32 msg[12];
3245          int ret;
3246
3247         msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
3248         msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
3249         msg[2] = 0;
3250         msg[3] = 0;
3251         msg[4] = (0<<16) | ((pHba->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
3252         msg[5] = 0;                                /* Segment 0 */
3253
3254         /* 
3255          * Provide three SGL-elements:
3256          * System table (SysTab), Private memory space declaration and 
3257          * Private i/o space declaration  
3258          */
3259         msg[6] = 0x54000000 | sys_tbl_len;
3260         msg[7] = (u32)sys_tbl_pa;
3261         msg[8] = 0x54000000 | 0;
3262         msg[9] = 0;
3263         msg[10] = 0xD4000000 | 0;
3264         msg[11] = 0;
3265
3266         if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 120))) {
3267                 printk(KERN_INFO "%s: Unable to set SysTab (status=%#10x).\n", 
3268                         pHba->name, ret);
3269         }
3270 #ifdef DEBUG
3271         else {
3272                 PINFO("%s: SysTab set.\n", pHba->name);
3273         }
3274 #endif
3275
3276         return ret;     
3277 }
3278
3279
3280 /*============================================================================
3281  *
3282  *============================================================================
3283  */
3284
3285
3286 #ifdef UARTDELAY 
3287
3288 static static void adpt_delay(int millisec)
3289 {
3290         int i;
3291         for (i = 0; i < millisec; i++) {
3292                 udelay(1000);   /* delay for one millisecond */
3293         }
3294 }
3295
3296 #endif
3297
3298 static struct scsi_host_template driver_template = {
3299         .module                 = THIS_MODULE,
3300         .name                   = "dpt_i2o",
3301         .proc_name              = "dpt_i2o",
3302         .show_info              = adpt_show_info,
3303         .info                   = adpt_info,
3304         .queuecommand           = adpt_queue,
3305         .eh_abort_handler       = adpt_abort,
3306         .eh_device_reset_handler = adpt_device_reset,
3307         .eh_bus_reset_handler   = adpt_bus_reset,
3308         .eh_host_reset_handler  = adpt_reset,
3309         .bios_param             = adpt_bios_param,
3310         .slave_configure        = adpt_slave_configure,
3311         .can_queue              = MAX_TO_IOP_MESSAGES,
3312         .this_id                = 7,
3313         .use_clustering         = ENABLE_CLUSTERING,
3314 };
3315
3316 static int __init adpt_init(void)
3317 {
3318         int             error;
3319         adpt_hba        *pHba, *next;
3320
3321         printk("Loading Adaptec I2O RAID: Version " DPT_I2O_VERSION "\n");
3322
3323         error = adpt_detect(&driver_template);
3324         if (error < 0)
3325                 return error;
3326         if (hba_chain == NULL)
3327                 return -ENODEV;
3328
3329         for (pHba = hba_chain; pHba; pHba = pHba->next) {
3330                 error = scsi_add_host(pHba->host, &pHba->pDev->dev);
3331                 if (error)
3332                         goto fail;
3333                 scsi_scan_host(pHba->host);
3334         }
3335         return 0;
3336 fail:
3337         for (pHba = hba_chain; pHba; pHba = next) {
3338                 next = pHba->next;
3339                 scsi_remove_host(pHba->host);
3340         }
3341         return error;
3342 }
3343
3344 static void __exit adpt_exit(void)
3345 {
3346         adpt_hba        *pHba, *next;
3347
3348         for (pHba = hba_chain; pHba; pHba = next) {
3349                 next = pHba->next;
3350                 adpt_release(pHba);
3351         }
3352 }
3353
3354 module_init(adpt_init);
3355 module_exit(adpt_exit);
3356
3357 MODULE_LICENSE("GPL");