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