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