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