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