ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / aacraid / aachba.c
1 /*
2  *      Adaptec AAC series RAID controller driver
3  *      (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; see the file COPYING.  If not, write to
22  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/sched.h>
30 #include <linux/pci.h>
31 #include <linux/spinlock.h>
32 #include <linux/slab.h>
33 #include <linux/completion.h>
34 #include <linux/blkdev.h>
35 #include <asm/semaphore.h>
36 #include <asm/uaccess.h>
37
38 #include <scsi/scsi.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_host.h>
42
43 #include "aacraid.h"
44
45 /* values for inqd_pdt: Peripheral device type in plain English */
46 #define INQD_PDT_DA     0x00    /* Direct-access (DISK) device */
47 #define INQD_PDT_PROC   0x03    /* Processor device */
48 #define INQD_PDT_CHNGR  0x08    /* Changer (jukebox, scsi2) */
49 #define INQD_PDT_COMM   0x09    /* Communication device (scsi2) */
50 #define INQD_PDT_NOLUN2 0x1f    /* Unknown Device (scsi2) */
51 #define INQD_PDT_NOLUN  0x7f    /* Logical Unit Not Present */
52
53 #define INQD_PDT_DMASK  0x1F    /* Peripheral Device Type Mask */
54 #define INQD_PDT_QMASK  0xE0    /* Peripheral Device Qualifer Mask */
55
56 #define TARGET_LUN_TO_CONTAINER(target, lun)    (target)
57 #define CONTAINER_TO_TARGET(cont)               ((cont))
58 #define CONTAINER_TO_LUN(cont)                  (0)
59
60 #define MAX_FIB_DATA (sizeof(struct hw_fib) - sizeof(FIB_HEADER))
61
62 #define MAX_DRIVER_SG_SEGMENT_COUNT 17
63
64 /*
65  *      Sense keys
66  */
67 #define SENKEY_NO_SENSE      0x00       
68 #define SENKEY_UNDEFINED     0x01       
69 #define SENKEY_NOT_READY     0x02       
70 #define SENKEY_MEDIUM_ERR    0x03       
71 #define SENKEY_HW_ERR        0x04       
72 #define SENKEY_ILLEGAL       0x05       
73 #define SENKEY_ATTENTION     0x06       
74 #define SENKEY_PROTECTED     0x07       
75 #define SENKEY_BLANK         0x08       
76 #define SENKEY_V_UNIQUE      0x09       
77 #define SENKEY_CPY_ABORT     0x0A       
78 #define SENKEY_ABORT         0x0B       
79 #define SENKEY_EQUAL         0x0C       
80 #define SENKEY_VOL_OVERFLOW  0x0D       
81 #define SENKEY_MISCOMP       0x0E       
82 #define SENKEY_RESERVED      0x0F       
83
84 /*
85  *      Sense codes
86  */
87  
88 #define SENCODE_NO_SENSE                        0x00
89 #define SENCODE_END_OF_DATA                     0x00
90 #define SENCODE_BECOMING_READY                  0x04
91 #define SENCODE_INIT_CMD_REQUIRED               0x04
92 #define SENCODE_PARAM_LIST_LENGTH_ERROR         0x1A
93 #define SENCODE_INVALID_COMMAND                 0x20
94 #define SENCODE_LBA_OUT_OF_RANGE                0x21
95 #define SENCODE_INVALID_CDB_FIELD               0x24
96 #define SENCODE_LUN_NOT_SUPPORTED               0x25
97 #define SENCODE_INVALID_PARAM_FIELD             0x26
98 #define SENCODE_PARAM_NOT_SUPPORTED             0x26
99 #define SENCODE_PARAM_VALUE_INVALID             0x26
100 #define SENCODE_RESET_OCCURRED                  0x29
101 #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET     0x3E
102 #define SENCODE_INQUIRY_DATA_CHANGED            0x3F
103 #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED     0x39
104 #define SENCODE_DIAGNOSTIC_FAILURE              0x40
105 #define SENCODE_INTERNAL_TARGET_FAILURE         0x44
106 #define SENCODE_INVALID_MESSAGE_ERROR           0x49
107 #define SENCODE_LUN_FAILED_SELF_CONFIG          0x4c
108 #define SENCODE_OVERLAPPED_COMMAND              0x4E
109
110 /*
111  *      Additional sense codes
112  */
113  
114 #define ASENCODE_NO_SENSE                       0x00
115 #define ASENCODE_END_OF_DATA                    0x05
116 #define ASENCODE_BECOMING_READY                 0x01
117 #define ASENCODE_INIT_CMD_REQUIRED              0x02
118 #define ASENCODE_PARAM_LIST_LENGTH_ERROR        0x00
119 #define ASENCODE_INVALID_COMMAND                0x00
120 #define ASENCODE_LBA_OUT_OF_RANGE               0x00
121 #define ASENCODE_INVALID_CDB_FIELD              0x00
122 #define ASENCODE_LUN_NOT_SUPPORTED              0x00
123 #define ASENCODE_INVALID_PARAM_FIELD            0x00
124 #define ASENCODE_PARAM_NOT_SUPPORTED            0x01
125 #define ASENCODE_PARAM_VALUE_INVALID            0x02
126 #define ASENCODE_RESET_OCCURRED                 0x00
127 #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET    0x00
128 #define ASENCODE_INQUIRY_DATA_CHANGED           0x03
129 #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED    0x00
130 #define ASENCODE_DIAGNOSTIC_FAILURE             0x80
131 #define ASENCODE_INTERNAL_TARGET_FAILURE        0x00
132 #define ASENCODE_INVALID_MESSAGE_ERROR          0x00
133 #define ASENCODE_LUN_FAILED_SELF_CONFIG         0x00
134 #define ASENCODE_OVERLAPPED_COMMAND             0x00
135
136 #define BYTE0(x) (unsigned char)(x)
137 #define BYTE1(x) (unsigned char)((x) >> 8)
138 #define BYTE2(x) (unsigned char)((x) >> 16)
139 #define BYTE3(x) (unsigned char)((x) >> 24)
140
141 /*------------------------------------------------------------------------------
142  *              S T R U C T S / T Y P E D E F S
143  *----------------------------------------------------------------------------*/
144 /* SCSI inquiry data */
145 struct inquiry_data {
146         u8 inqd_pdt;    /* Peripheral qualifier | Peripheral Device Type  */
147         u8 inqd_dtq;    /* RMB | Device Type Qualifier  */
148         u8 inqd_ver;    /* ISO version | ECMA version | ANSI-approved version */
149         u8 inqd_rdf;    /* AENC | TrmIOP | Response data format */
150         u8 inqd_len;    /* Additional length (n-4) */
151         u8 inqd_pad1[2];/* Reserved - must be zero */
152         u8 inqd_pad2;   /* RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
153         u8 inqd_vid[8]; /* Vendor ID */
154         u8 inqd_pid[16];/* Product ID */
155         u8 inqd_prl[4]; /* Product Revision Level */
156 };
157
158 struct sense_data {
159         u8 error_code;          /* 70h (current errors), 71h(deferred errors) */
160         u8 valid:1;             /* A valid bit of one indicates that the information  */
161                                 /* field contains valid information as defined in the
162                                  * SCSI-2 Standard.
163                                  */
164         u8 segment_number;      /* Only used for COPY, COMPARE, or COPY AND VERIFY Commands */
165         u8 sense_key:4;         /* Sense Key */
166         u8 reserved:1;
167         u8 ILI:1;               /* Incorrect Length Indicator */
168         u8 EOM:1;               /* End Of Medium - reserved for random access devices */
169         u8 filemark:1;          /* Filemark - reserved for random access devices */
170
171         u8 information[4];      /* for direct-access devices, contains the unsigned 
172                                  * logical block address or residue associated with 
173                                  * the sense key 
174                                  */
175         u8 add_sense_len;       /* number of additional sense bytes to follow this field */
176         u8 cmnd_info[4];        /* not used */
177         u8 ASC;                 /* Additional Sense Code */
178         u8 ASCQ;                /* Additional Sense Code Qualifier */
179         u8 FRUC;                /* Field Replaceable Unit Code - not used */
180         u8 bit_ptr:3;           /* indicates which byte of the CDB or parameter data
181                                  * was in error
182                                  */
183         u8 BPV:1;               /* bit pointer valid (BPV): 1- indicates that 
184                                  * the bit_ptr field has valid value
185                                  */
186         u8 reserved2:2;
187         u8 CD:1;                /* command data bit: 1- illegal parameter in CDB.
188                                  * 0- illegal parameter in data.
189                                  */
190         u8 SKSV:1;
191         u8 field_ptr[2];        /* byte of the CDB or parameter data in error */
192 };
193
194 /*
195  *              M O D U L E   G L O B A L S
196  */
197  
198 static struct fsa_scsi_hba *fsa_dev[MAXIMUM_NUM_ADAPTERS];      /*  SCSI Device Instance Pointers */
199 static struct sense_data sense_data[MAXIMUM_NUM_CONTAINERS];
200 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
201 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
202 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
203 #ifdef AAC_DETAILED_STATUS_INFO
204 static char *aac_get_status_string(u32 status);
205 #endif
206
207 /**
208  *      aac_get_containers      -       list containers
209  *      @common: adapter to probe
210  *
211  *      Make a list of all containers on this controller
212  */
213 int aac_get_containers(struct aac_dev *dev)
214 {
215         struct fsa_scsi_hba *fsa_dev_ptr;
216         u32 index; 
217         int status = 0;
218         struct aac_query_mount *dinfo;
219         struct aac_mount *dresp;
220         struct fib * fibptr;
221         unsigned instance;
222
223         fsa_dev_ptr = &(dev->fsa_dev);
224         instance = dev->scsi_host_ptr->unique_id;
225
226         if (!(fibptr = fib_alloc(dev)))
227                 return -ENOMEM;
228
229         for (index = 0; index < MAXIMUM_NUM_CONTAINERS; index++) {
230                 fib_init(fibptr);
231                 dinfo = (struct aac_query_mount *) fib_data(fibptr);
232
233                 dinfo->command = cpu_to_le32(VM_NameServe);
234                 dinfo->count = cpu_to_le32(index);
235                 dinfo->type = cpu_to_le32(FT_FILESYS);
236
237                 status = fib_send(ContainerCommand,
238                                     fibptr,
239                                     sizeof (struct aac_query_mount),
240                                     FsaNormal,
241                                     1, 1,
242                                     NULL, NULL);
243                 if (status < 0 ) {
244                         printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
245                         break;
246                 }
247                 dresp = (struct aac_mount *)fib_data(fibptr);
248
249                 if ((le32_to_cpu(dresp->status) == ST_OK) &&
250                     (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
251                     (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
252                         fsa_dev_ptr->valid[index] = 1;
253                         fsa_dev_ptr->type[index] = le32_to_cpu(dresp->mnt[0].vol);
254                         fsa_dev_ptr->size[index] = le32_to_cpu(dresp->mnt[0].capacity);
255                         if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
256                                     fsa_dev_ptr->ro[index] = 1;
257                 }
258                 fib_complete(fibptr);
259                 /*
260                  *      If there are no more containers, then stop asking.
261                  */
262                 if ((index + 1) >= le32_to_cpu(dresp->count)){
263                         break;
264                 }
265         }
266         fib_free(fibptr);
267         fsa_dev[instance] = fsa_dev_ptr;
268         return status;
269 }
270
271 /**
272  *      probe_container         -       query a logical volume
273  *      @dev: device to query
274  *      @cid: container identifier
275  *
276  *      Queries the controller about the given volume. The volume information
277  *      is updated in the struct fsa_scsi_hba structure rather than returned.
278  */
279  
280 static int probe_container(struct aac_dev *dev, int cid)
281 {
282         struct fsa_scsi_hba *fsa_dev_ptr;
283         int status;
284         struct aac_query_mount *dinfo;
285         struct aac_mount *dresp;
286         struct fib * fibptr;
287         unsigned instance;
288
289         fsa_dev_ptr = &(dev->fsa_dev);
290         instance = dev->scsi_host_ptr->unique_id;
291
292         if (!(fibptr = fib_alloc(dev)))
293                 return -ENOMEM;
294
295         fib_init(fibptr);
296
297         dinfo = (struct aac_query_mount *)fib_data(fibptr);
298
299         dinfo->command = cpu_to_le32(VM_NameServe);
300         dinfo->count = cpu_to_le32(cid);
301         dinfo->type = cpu_to_le32(FT_FILESYS);
302
303         status = fib_send(ContainerCommand,
304                             fibptr,
305                             sizeof(struct aac_query_mount),
306                             FsaNormal,
307                             1, 1,
308                             NULL, NULL);
309         if (status < 0) {
310                 printk(KERN_WARNING "aacraid: probe_containers query failed.\n");
311                 goto error;
312         }
313
314         dresp = (struct aac_mount *) fib_data(fibptr);
315
316         if ((le32_to_cpu(dresp->status) == ST_OK) &&
317             (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
318             (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
319                 fsa_dev_ptr->valid[cid] = 1;
320                 fsa_dev_ptr->type[cid] = le32_to_cpu(dresp->mnt[0].vol);
321                 fsa_dev_ptr->size[cid] = le32_to_cpu(dresp->mnt[0].capacity);
322                 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
323                         fsa_dev_ptr->ro[cid] = 1;
324         }
325
326 error:
327         fib_complete(fibptr);
328         fib_free(fibptr);
329
330         return status;
331 }
332
333 /* Local Structure to set SCSI inquiry data strings */
334 struct scsi_inq {
335         char vid[8];         /* Vendor ID */
336         char pid[16];        /* Product ID */
337         char prl[4];         /* Product Revision Level */
338 };
339
340 /**
341  *      InqStrCopy      -       string merge
342  *      @a:     string to copy from
343  *      @b:     string to copy to
344  *
345  *      Copy a String from one location to another
346  *      without copying \0
347  */
348
349 static void inqstrcpy(char *a, char *b)
350 {
351
352         while(*a != (char)0) 
353                 *b++ = *a++;
354 }
355
356 static char *container_types[] = {
357         "None",
358         "Volume",
359         "Mirror",
360         "Stripe",
361         "RAID5",
362         "SSRW",
363         "SSRO",
364         "Morph",
365         "Legacy",
366         "RAID4",
367         "RAID10",             
368         "RAID00",             
369         "V-MIRRORS",          
370         "PSEUDO R4",          
371         "RAID50",
372         "Unknown"
373 };
374
375
376
377 /* Function: setinqstr
378  *
379  * Arguments: [1] pointer to void [1] int
380  *
381  * Purpose: Sets SCSI inquiry data strings for vendor, product
382  * and revision level. Allows strings to be set in platform dependent
383  * files instead of in OS dependent driver source.
384  */
385
386 static void setinqstr(int devtype, void *data, int tindex)
387 {
388         struct scsi_inq *str;
389         char *findit;
390         struct aac_driver_ident *mp;
391
392         mp = aac_get_driver_ident(devtype);
393    
394         str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
395
396         inqstrcpy (mp->vname, str->vid); 
397         inqstrcpy (mp->model, str->pid); /* last six chars reserved for vol type */
398
399         findit = str->pid;
400
401         for ( ; *findit != ' '; findit++); /* walk till we find a space then incr by 1 */
402                 findit++;
403         
404         if (tindex < (sizeof(container_types)/sizeof(char *))){
405                 inqstrcpy (container_types[tindex], findit);
406         }
407         inqstrcpy ("V1.0", str->prl);
408 }
409
410 void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
411                     u8 a_sense_code, u8 incorrect_length,
412                     u8 bit_pointer, u16 field_pointer,
413                     u32 residue)
414 {
415         sense_buf[0] = 0xF0;    /* Sense data valid, err code 70h (current error) */
416         sense_buf[1] = 0;       /* Segment number, always zero */
417
418         if (incorrect_length) {
419                 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
420                 sense_buf[3] = BYTE3(residue);
421                 sense_buf[4] = BYTE2(residue);
422                 sense_buf[5] = BYTE1(residue);
423                 sense_buf[6] = BYTE0(residue);
424         } else
425                 sense_buf[2] = sense_key;       /* Sense key */
426
427         if (sense_key == SENKEY_ILLEGAL)
428                 sense_buf[7] = 10;      /* Additional sense length */
429         else
430                 sense_buf[7] = 6;       /* Additional sense length */
431
432         sense_buf[12] = sense_code;     /* Additional sense code */
433         sense_buf[13] = a_sense_code;   /* Additional sense code qualifier */
434         if (sense_key == SENKEY_ILLEGAL) {
435                 sense_buf[15] = 0;
436
437                 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
438                         sense_buf[15] = 0x80;/* Std sense key specific field */
439                 /* Illegal parameter is in the parameter block */
440
441                 if (sense_code == SENCODE_INVALID_CDB_FIELD)
442                         sense_buf[15] = 0xc0;/* Std sense key specific field */
443                 /* Illegal parameter is in the CDB block */
444                 sense_buf[15] |= bit_pointer;
445                 sense_buf[16] = field_pointer >> 8;     /* MSB */
446                 sense_buf[17] = field_pointer;          /* LSB */
447         }
448 }
449
450 static void aac_io_done(struct scsi_cmnd * scsicmd)
451 {
452         unsigned long cpu_flags;
453         struct Scsi_Host *host = scsicmd->device->host;
454         spin_lock_irqsave(host->host_lock, cpu_flags);
455         scsicmd->scsi_done(scsicmd);
456         spin_unlock_irqrestore(host->host_lock, cpu_flags);
457 }
458
459 static void __aac_io_done(struct scsi_cmnd * scsicmd)
460 {
461         scsicmd->scsi_done(scsicmd);
462 }
463
464 int aac_get_adapter_info(struct aac_dev* dev)
465 {
466         struct fib* fibptr;
467         struct aac_adapter_info* info;
468         int rcode;
469         u32 tmp;
470         if (!(fibptr = fib_alloc(dev)))
471                 return -ENOMEM;
472
473         fib_init(fibptr);
474         info = (struct aac_adapter_info*) fib_data(fibptr);
475
476         memset(info,0,sizeof(struct aac_adapter_info));
477
478         rcode = fib_send(RequestAdapterInfo,
479                         fibptr, 
480                         sizeof(struct aac_adapter_info),
481                         FsaNormal, 
482                         1, 1, 
483                         NULL, 
484                         NULL);
485
486         memcpy(&dev->adapter_info, info, sizeof(struct aac_adapter_info));
487
488         tmp = dev->adapter_info.kernelrev;
489         printk(KERN_INFO"%s%d: kernel %d.%d.%d build %d\n", 
490                         dev->name, dev->id,
491                         tmp>>24,(tmp>>16)&0xff,(tmp>>8)&0xff,
492                         dev->adapter_info.kernelbuild);
493         tmp = dev->adapter_info.monitorrev;
494         printk(KERN_INFO"%s%d: monitor %d.%d.%d build %d\n", 
495                         dev->name, dev->id,
496                         tmp>>24,(tmp>>16)&0xff,(tmp>>8)&0xff,
497                         dev->adapter_info.monitorbuild);
498         tmp = dev->adapter_info.biosrev;
499         printk(KERN_INFO"%s%d: bios %d.%d.%d build %d\n", 
500                         dev->name, dev->id,
501                         tmp>>24,(tmp>>16)&0xff,(tmp>>8)&0xff,
502                         dev->adapter_info.biosbuild);
503         printk(KERN_INFO"%s%d: serial %x%x\n",
504                         dev->name, dev->id,
505                         dev->adapter_info.serial[0],
506                         dev->adapter_info.serial[1]);
507
508         dev->nondasd_support = 0;
509         if(dev->adapter_info.options & AAC_OPT_NONDASD){
510 //              dev->nondasd_support = 1;
511 // dmb - temporarily disable nondasd
512         }
513         if(nondasd != -1) {  
514                 dev->nondasd_support = (nondasd!=0);
515         }
516         if(dev->nondasd_support != 0){
517                 printk(KERN_INFO"%s%d: Non-DASD support enabled\n",dev->name, dev->id);
518         }
519
520         dev->pae_support = 0;
521         if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
522                 dev->pae_support = 1;
523         }
524
525         if(paemode != -1){
526                 dev->pae_support = (paemode!=0);
527         }
528         if(dev->pae_support != 0) {
529                 printk(KERN_INFO"%s%d: 64 Bit PAE enabled\n", dev->name, dev->id);
530                 pci_set_dma_mask(dev->pdev, (dma_addr_t)0xFFFFFFFFFFFFFFFFULL);
531         }
532
533         fib_complete(fibptr);
534         fib_free(fibptr);
535
536         return rcode;
537 }
538
539
540 static void read_callback(void *context, struct fib * fibptr)
541 {
542         struct aac_dev *dev;
543         struct aac_read_reply *readreply;
544         struct scsi_cmnd *scsicmd;
545         u32 lba;
546         u32 cid;
547
548         scsicmd = (struct scsi_cmnd *) context;
549
550         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
551         cid =TARGET_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
552
553         lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
554         dprintk((KERN_DEBUG "read_callback[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
555
556         if (fibptr == NULL)
557                 BUG();
558                 
559         if(scsicmd->use_sg)
560                 pci_unmap_sg(dev->pdev, 
561                         (struct scatterlist *)scsicmd->buffer,
562                         scsicmd->use_sg,
563                         scsicmd->sc_data_direction);
564         else if(scsicmd->request_bufflen)
565                 pci_unmap_single(dev->pdev, (dma_addr_t)(ulong)scsicmd->SCp.ptr,
566                                  scsicmd->request_bufflen,
567                                  scsicmd->sc_data_direction);
568         readreply = (struct aac_read_reply *)fib_data(fibptr);
569         if (le32_to_cpu(readreply->status) == ST_OK)
570                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
571         else {
572                 printk(KERN_WARNING "read_callback: read failed, status = %d\n", readreply->status);
573                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
574                 set_sense((u8 *) &sense_data[cid],
575                                     SENKEY_HW_ERR,
576                                     SENCODE_INTERNAL_TARGET_FAILURE,
577                                     ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
578                                     0, 0);
579         }
580         fib_complete(fibptr);
581         fib_free(fibptr);
582
583         aac_io_done(scsicmd);
584 }
585
586 static void write_callback(void *context, struct fib * fibptr)
587 {
588         struct aac_dev *dev;
589         struct aac_write_reply *writereply;
590         struct scsi_cmnd *scsicmd;
591         u32 lba;
592         u32 cid;
593
594         scsicmd = (struct scsi_cmnd *) context;
595         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
596         cid = TARGET_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
597
598         lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
599         dprintk((KERN_DEBUG "write_callback[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
600         if (fibptr == NULL)
601                 BUG();
602
603         if(scsicmd->use_sg)
604                 pci_unmap_sg(dev->pdev, 
605                         (struct scatterlist *)scsicmd->buffer,
606                         scsicmd->use_sg,
607                         scsicmd->sc_data_direction);
608         else if(scsicmd->request_bufflen)
609                 pci_unmap_single(dev->pdev, (dma_addr_t)(ulong)scsicmd->SCp.ptr,
610                                  scsicmd->request_bufflen,
611                                  scsicmd->sc_data_direction);
612
613         writereply = (struct aac_write_reply *) fib_data(fibptr);
614         if (le32_to_cpu(writereply->status) == ST_OK)
615                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
616         else {
617                 printk(KERN_WARNING "write_callback: write failed, status = %d\n", writereply->status);
618                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
619                 set_sense((u8 *) &sense_data[cid],
620                                     SENKEY_HW_ERR,
621                                     SENCODE_INTERNAL_TARGET_FAILURE,
622                                     ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
623                                     0, 0);
624         }
625
626         fib_complete(fibptr);
627         fib_free(fibptr);
628         aac_io_done(scsicmd);
629 }
630
631 int aac_read(struct scsi_cmnd * scsicmd, int cid)
632 {
633         u32 lba;
634         u32 count;
635         int status;
636
637         u16 fibsize;
638         struct aac_dev *dev;
639         struct fib * cmd_fibcontext;
640
641         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
642         /*
643          *      Get block address and transfer length
644          */
645         if (scsicmd->cmnd[0] == READ_6) /* 6 byte command */
646         {
647                 dprintk((KERN_DEBUG "aachba: received a read(6) command on target %d.\n", cid));
648
649                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
650                 count = scsicmd->cmnd[4];
651
652                 if (count == 0)
653                         count = 256;
654         } else {
655                 dprintk((KERN_DEBUG "aachba: received a read(10) command on target %d.\n", cid));
656
657                 lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
658                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
659         }
660         dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
661         /*
662          *      Alocate and initialize a Fib
663          */
664         if (!(cmd_fibcontext = fib_alloc(dev))) {
665                 scsicmd->result = DID_ERROR << 16;
666                 aac_io_done(scsicmd);
667                 return (-1);
668         }
669
670         fib_init(cmd_fibcontext);
671
672         if(dev->pae_support == 1){
673                 struct aac_read64 *readcmd;
674                 readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
675                 readcmd->command = cpu_to_le32(VM_CtHostRead64);
676                 readcmd->cid = cpu_to_le16(cid);
677                 readcmd->sector_count = cpu_to_le16(count);
678                 readcmd->block = cpu_to_le32(lba);
679                 readcmd->pad   = cpu_to_le16(0);
680                 readcmd->flags = cpu_to_le16(0); 
681
682                 aac_build_sg64(scsicmd, &readcmd->sg);
683                 if(readcmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
684                         BUG();
685                 fibsize = sizeof(struct aac_read64) + ((readcmd->sg.count - 1) * sizeof (struct sgentry64));
686                 /*
687                  *      Now send the Fib to the adapter
688                  */
689                 status = fib_send(ContainerCommand64, 
690                           cmd_fibcontext, 
691                           fibsize, 
692                           FsaNormal, 
693                           0, 1, 
694                           (fib_callback) read_callback, 
695                           (void *) scsicmd);
696         } else {
697                 struct aac_read *readcmd;
698                 readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
699                 readcmd->command = cpu_to_le32(VM_CtBlockRead);
700                 readcmd->cid = cpu_to_le32(cid);
701                 readcmd->block = cpu_to_le32(lba);
702                 readcmd->count = cpu_to_le32(count * 512);
703
704                 if (count * 512 > (64 * 1024))
705                         BUG();
706
707                 aac_build_sg(scsicmd, &readcmd->sg);
708                 if(readcmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
709                         BUG();
710                 fibsize = sizeof(struct aac_read) + ((readcmd->sg.count - 1) * sizeof (struct sgentry));
711                 /*
712                  *      Now send the Fib to the adapter
713                  */
714                 status = fib_send(ContainerCommand, 
715                           cmd_fibcontext, 
716                           fibsize, 
717                           FsaNormal, 
718                           0, 1, 
719                           (fib_callback) read_callback, 
720                           (void *) scsicmd);
721         }
722
723         
724
725         /*
726          *      Check that the command queued to the controller
727          */
728         if (status == -EINPROGRESS) 
729                 return 0;
730                 
731         printk(KERN_WARNING "aac_read: fib_send failed with status: %d.\n", status);
732         /*
733          *      For some reason, the Fib didn't queue, return QUEUE_FULL
734          */
735         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
736         aac_io_done(scsicmd);
737         fib_complete(cmd_fibcontext);
738         fib_free(cmd_fibcontext);
739         return -1;
740 }
741
742 static int aac_write(struct scsi_cmnd * scsicmd, int cid)
743 {
744         u32 lba;
745         u32 count;
746         int status;
747         u16 fibsize;
748         struct aac_dev *dev;
749         struct fib * cmd_fibcontext;
750
751         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
752         /*
753          *      Get block address and transfer length
754          */
755         if (scsicmd->cmnd[0] == WRITE_6)        /* 6 byte command */
756         {
757                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
758                 count = scsicmd->cmnd[4];
759                 if (count == 0)
760                         count = 256;
761         } else {
762                 dprintk((KERN_DEBUG "aachba: received a write(10) command on target %d.\n", cid));
763                 lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
764                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
765         }
766         dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
767         /*
768          *      Allocate and initialize a Fib then setup a BlockWrite command
769          */
770         if (!(cmd_fibcontext = fib_alloc(dev))) {
771                 scsicmd->result = DID_ERROR << 16;
772                 aac_io_done(scsicmd);
773                 return -1;
774         }
775         fib_init(cmd_fibcontext);
776
777         if(dev->pae_support == 1){
778                 struct aac_write64 *writecmd;
779                 writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
780                 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
781                 writecmd->cid = cpu_to_le16(cid);
782                 writecmd->sector_count = cpu_to_le16(count); 
783                 writecmd->block = cpu_to_le32(lba);
784                 writecmd->pad   = cpu_to_le16(0);
785                 writecmd->flags = cpu_to_le16(0);
786
787                 aac_build_sg64(scsicmd, &writecmd->sg);
788                 if(writecmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
789                         BUG();
790                 fibsize = sizeof(struct aac_write64) + ((writecmd->sg.count - 1) * sizeof (struct sgentry64));
791                 /*
792                  *      Now send the Fib to the adapter
793                  */
794                 status = fib_send(ContainerCommand64, 
795                           cmd_fibcontext, 
796                           fibsize, 
797                           FsaNormal, 
798                           0, 1, 
799                           (fib_callback) write_callback, 
800                           (void *) scsicmd);
801         } else {
802                 struct aac_write *writecmd;
803                 writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
804                 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
805                 writecmd->cid = cpu_to_le32(cid);
806                 writecmd->block = cpu_to_le32(lba);
807                 writecmd->count = cpu_to_le32(count * 512);
808                 writecmd->sg.count = cpu_to_le32(1);
809                 /* ->stable is not used - it did mean which type of write */
810
811                 if (count * 512 > (64 * 1024)) {
812                         BUG();
813                 }
814
815                 aac_build_sg(scsicmd, &writecmd->sg);
816                 if(writecmd->sg.count > MAX_DRIVER_SG_SEGMENT_COUNT)
817                         BUG();
818                 fibsize = sizeof(struct aac_write) + ((writecmd->sg.count - 1) * sizeof (struct sgentry));
819                 /*
820                  *      Now send the Fib to the adapter
821                  */
822                 status = fib_send(ContainerCommand, 
823                           cmd_fibcontext, 
824                           fibsize, 
825                           FsaNormal, 
826                           0, 1, 
827                           (fib_callback) write_callback, 
828                           (void *) scsicmd);
829         }
830
831         /*
832          *      Check that the command queued to the controller
833          */
834         if (status == -EINPROGRESS)
835                 return 0;
836
837         printk(KERN_WARNING "aac_write: fib_send failed with status: %d\n", status);
838         /*
839          *      For some reason, the Fib didn't queue, return QUEUE_FULL
840          */
841         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
842         aac_io_done(scsicmd);
843
844         fib_complete(cmd_fibcontext);
845         fib_free(cmd_fibcontext);
846         return -1;
847 }
848
849
850 /**
851  *      aac_scsi_cmd()          -       Process SCSI command
852  *      @scsicmd:               SCSI command block
853  *      @wait:                  1 if the user wants to await completion
854  *
855  *      Emulate a SCSI command and queue the required request for the
856  *      aacraid firmware.
857  */
858  
859 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
860 {
861         u32 cid = 0;
862         struct fsa_scsi_hba *fsa_dev_ptr;
863         int cardtype;
864         int ret;
865         struct Scsi_Host *host = scsicmd->device->host;
866         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
867         
868         cardtype = dev->cardtype;
869
870         fsa_dev_ptr = fsa_dev[host->unique_id];
871
872         /*
873          *      If the bus, target or lun is out of range, return fail
874          *      Test does not apply to ID 16, the pseudo id for the controller
875          *      itself.
876          */
877         if (scsicmd->device->id != host->this_id) {
878                 if ((scsicmd->device->channel == 0) ){
879                         if( (scsicmd->device->id >= AAC_MAX_TARGET) || (scsicmd->device->lun != 0)){ 
880                                 scsicmd->result = DID_NO_CONNECT << 16;
881                                 __aac_io_done(scsicmd);
882                                 return 0;
883                         }
884                         cid = TARGET_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
885
886                         /*
887                          *      If the target container doesn't exist, it may have
888                          *      been newly created
889                          */
890                         if (fsa_dev_ptr->valid[cid] == 0) {
891                                 switch (scsicmd->cmnd[0]) {
892                                 case INQUIRY:
893                                 case READ_CAPACITY:
894                                 case TEST_UNIT_READY:
895                                         spin_unlock_irq(host->host_lock);
896                                         probe_container(dev, cid);
897                                         spin_lock_irq(host->host_lock);
898                                         if (fsa_dev_ptr->valid[cid] == 0) {
899                                                 scsicmd->result = DID_NO_CONNECT << 16;
900                                                 __aac_io_done(scsicmd);
901                                                 return 0;
902                                         }
903                                 default:
904                                         break;
905                                 }
906                         }
907                         /*
908                          *      If the target container still doesn't exist, 
909                          *      return failure
910                          */
911                         if (fsa_dev_ptr->valid[cid] == 0) {
912                                 scsicmd->result = DID_BAD_TARGET << 16;
913                                 __aac_io_done(scsicmd);
914                                 return -1;
915                         }
916                 } else {  /* check for physical non-dasd devices */
917                         if(dev->nondasd_support == 1){
918                                 return aac_send_srb_fib(scsicmd);
919                         } else {
920                                 scsicmd->result = DID_NO_CONNECT << 16;
921                                 __aac_io_done(scsicmd);
922                                 return 0;
923                         }
924                 }
925         }
926         /*
927          * else Command for the controller itself
928          */
929         else if ((scsicmd->cmnd[0] != INQUIRY) &&       /* only INQUIRY & TUR cmnd supported for controller */
930                 (scsicmd->cmnd[0] != TEST_UNIT_READY)) 
931         {
932                 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
933                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
934                 set_sense((u8 *) &sense_data[cid],
935                             SENKEY_ILLEGAL,
936                             SENCODE_INVALID_COMMAND,
937                             ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
938                 __aac_io_done(scsicmd);
939                 return -1;
940         }
941
942
943         /* Handle commands here that don't really require going out to the adapter */
944         switch (scsicmd->cmnd[0]) {
945         case INQUIRY:
946         {
947                 struct inquiry_data *inq_data_ptr;
948
949                 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scsicmd->device->id));
950                 inq_data_ptr = (struct inquiry_data *)scsicmd->request_buffer;
951                 memset(inq_data_ptr, 0, sizeof (struct inquiry_data));
952
953                 inq_data_ptr->inqd_ver = 2;     /* claim compliance to SCSI-2 */
954                 inq_data_ptr->inqd_dtq = 0x80;  /* set RMB bit to one indicating that the medium is removable */
955                 inq_data_ptr->inqd_rdf = 2;     /* A response data format value of two indicates that the data shall be in the format specified in SCSI-2 */
956                 inq_data_ptr->inqd_len = 31;
957                 /*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
958                 inq_data_ptr->inqd_pad2= 0x32 ;  /*WBus16|Sync|CmdQue */
959                 /*
960                  *      Set the Vendor, Product, and Revision Level
961                  *      see: <vendor>.c i.e. aac.c
962                  */
963                 setinqstr(cardtype, (void *) (inq_data_ptr->inqd_vid), fsa_dev_ptr->type[cid]);
964                 if (scsicmd->device->id == host->this_id)
965                         inq_data_ptr->inqd_pdt = INQD_PDT_PROC; /* Processor device */
966                 else
967                         inq_data_ptr->inqd_pdt = INQD_PDT_DA;   /* Direct/random access device */
968                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
969                 __aac_io_done(scsicmd);
970                 return 0;
971         }
972         case READ_CAPACITY:
973         {
974                 int capacity;
975                 char *cp;
976
977                 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
978                 capacity = fsa_dev_ptr->size[cid] - 1;
979                 cp = scsicmd->request_buffer;
980                 cp[0] = (capacity >> 24) & 0xff;
981                 cp[1] = (capacity >> 16) & 0xff;
982                 cp[2] = (capacity >> 8) & 0xff;
983                 cp[3] = (capacity >> 0) & 0xff;
984                 cp[4] = 0;
985                 cp[5] = 0;
986                 cp[6] = 2;
987                 cp[7] = 0;
988
989                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
990                 __aac_io_done(scsicmd);
991
992                 return 0;
993         }
994
995         case MODE_SENSE:
996         {
997                 char *mode_buf;
998
999                 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
1000                 mode_buf = scsicmd->request_buffer;
1001                 mode_buf[0] = 3;        /* Mode data length */
1002                 mode_buf[1] = 0;        /* Medium type - default */
1003                 mode_buf[2] = 0;        /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1004                 mode_buf[3] = 0;        /* Block descriptor length */
1005
1006                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1007                 __aac_io_done(scsicmd);
1008
1009                 return 0;
1010         }
1011         case MODE_SENSE_10:
1012         {
1013                 char *mode_buf;
1014
1015                 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
1016                 mode_buf = scsicmd->request_buffer;
1017                 mode_buf[0] = 0;        /* Mode data length (MSB) */
1018                 mode_buf[1] = 6;        /* Mode data length (LSB) */
1019                 mode_buf[2] = 0;        /* Medium type - default */
1020                 mode_buf[3] = 0;        /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1021                 mode_buf[4] = 0;        /* reserved */
1022                 mode_buf[5] = 0;        /* reserved */
1023                 mode_buf[6] = 0;        /* Block descriptor length (MSB) */
1024                 mode_buf[7] = 0;        /* Block descriptor length (LSB) */
1025
1026                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1027                 __aac_io_done(scsicmd);
1028
1029                 return 0;
1030         }
1031         case REQUEST_SENSE:
1032                 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1033                 memcpy(scsicmd->sense_buffer, &sense_data[cid], sizeof (struct sense_data));
1034                 memset(&sense_data[cid], 0, sizeof (struct sense_data));
1035                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1036                 __aac_io_done(scsicmd);
1037                 return (0);
1038
1039         case ALLOW_MEDIUM_REMOVAL:
1040                 dprintk((KERN_DEBUG "LOCK command.\n"));
1041                 if (scsicmd->cmnd[4])
1042                         fsa_dev_ptr->locked[cid] = 1;
1043                 else
1044                         fsa_dev_ptr->locked[cid] = 0;
1045
1046                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1047                 __aac_io_done(scsicmd);
1048                 return 0;
1049         /*
1050          *      These commands are all No-Ops
1051          */
1052         case TEST_UNIT_READY:
1053         case RESERVE:
1054         case RELEASE:
1055         case REZERO_UNIT:
1056         case REASSIGN_BLOCKS:
1057         case SEEK_10:
1058         case START_STOP:
1059                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1060                 __aac_io_done(scsicmd);
1061                 return (0);
1062         }
1063
1064         switch (scsicmd->cmnd[0]) 
1065         {
1066                 case READ_6:
1067                 case READ_10:
1068                         /*
1069                          *      Hack to keep track of ordinal number of the device that
1070                          *      corresponds to a container. Needed to convert
1071                          *      containers to /dev/sd device names
1072                          */
1073                          
1074                         spin_unlock_irq(host->host_lock);
1075                         if  (scsicmd->request->rq_disk)
1076                                 memcpy(fsa_dev_ptr->devname[cid],
1077                                         scsicmd->request->rq_disk->disk_name,
1078                                         8);
1079
1080                         ret = aac_read(scsicmd, cid);
1081                         spin_lock_irq(host->host_lock);
1082                         return ret;
1083
1084                 case WRITE_6:
1085                 case WRITE_10:
1086                         spin_unlock_irq(host->host_lock);
1087                         ret = aac_write(scsicmd, cid);
1088                         spin_lock_irq(host->host_lock);
1089                         return ret;
1090                 default:
1091                         /*
1092                          *      Unhandled commands
1093                          */
1094                         printk(KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]);
1095                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1096                         set_sense((u8 *) &sense_data[cid],
1097                                 SENKEY_ILLEGAL, SENCODE_INVALID_COMMAND,
1098                         ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1099                         __aac_io_done(scsicmd);
1100                         return -1;
1101         }
1102 }
1103
1104 static int query_disk(struct aac_dev *dev, void *arg)
1105 {
1106         struct aac_query_disk qd;
1107         struct fsa_scsi_hba *fsa_dev_ptr;
1108
1109         fsa_dev_ptr = &(dev->fsa_dev);
1110         if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1111                 return -EFAULT;
1112         if (qd.cnum == -1)
1113                 qd.cnum = TARGET_LUN_TO_CONTAINER(qd.target, qd.lun);
1114         else if ((qd.bus == -1) && (qd.target == -1) && (qd.lun == -1)) 
1115         {
1116                 if (qd.cnum < 0 || qd.cnum >= MAXIMUM_NUM_CONTAINERS)
1117                         return -EINVAL;
1118                 qd.instance = dev->scsi_host_ptr->host_no;
1119                 qd.bus = 0;
1120                 qd.target = CONTAINER_TO_TARGET(qd.cnum);
1121                 qd.lun = CONTAINER_TO_LUN(qd.cnum);
1122         }
1123         else return -EINVAL;
1124
1125         qd.valid = fsa_dev_ptr->valid[qd.cnum];
1126         qd.locked = fsa_dev_ptr->locked[qd.cnum];
1127         qd.deleted = fsa_dev_ptr->deleted[qd.cnum];
1128
1129         if (fsa_dev_ptr->devname[qd.cnum][0] == '\0')
1130                 qd.unmapped = 1;
1131         else
1132                 qd.unmapped = 0;
1133
1134         strlcpy(qd.name, fsa_dev_ptr->devname[qd.cnum], sizeof(qd.name));
1135
1136         if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1137                 return -EFAULT;
1138         return 0;
1139 }
1140
1141 static int force_delete_disk(struct aac_dev *dev, void *arg)
1142 {
1143         struct aac_delete_disk dd;
1144         struct fsa_scsi_hba *fsa_dev_ptr;
1145
1146         fsa_dev_ptr = &(dev->fsa_dev);
1147
1148         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1149                 return -EFAULT;
1150
1151         if (dd.cnum >= MAXIMUM_NUM_CONTAINERS)
1152                 return -EINVAL;
1153         /*
1154          *      Mark this container as being deleted.
1155          */
1156         fsa_dev_ptr->deleted[dd.cnum] = 1;
1157         /*
1158          *      Mark the container as no longer valid
1159          */
1160         fsa_dev_ptr->valid[dd.cnum] = 0;
1161         return 0;
1162 }
1163
1164 static int delete_disk(struct aac_dev *dev, void *arg)
1165 {
1166         struct aac_delete_disk dd;
1167         struct fsa_scsi_hba *fsa_dev_ptr;
1168
1169         fsa_dev_ptr = &(dev->fsa_dev);
1170
1171         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1172                 return -EFAULT;
1173
1174         if (dd.cnum >= MAXIMUM_NUM_CONTAINERS)
1175                 return -EINVAL;
1176         /*
1177          *      If the container is locked, it can not be deleted by the API.
1178          */
1179         if (fsa_dev_ptr->locked[dd.cnum])
1180                 return -EBUSY;
1181         else {
1182                 /*
1183                  *      Mark the container as no longer being valid.
1184                  */
1185                 fsa_dev_ptr->valid[dd.cnum] = 0;
1186                 fsa_dev_ptr->devname[dd.cnum][0] = '\0';
1187                 return 0;
1188         }
1189 }
1190
1191 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void *arg)
1192 {
1193         switch (cmd) {
1194         case FSACTL_QUERY_DISK:
1195                 return query_disk(dev, arg);
1196         case FSACTL_DELETE_DISK:
1197                 return delete_disk(dev, arg);
1198         case FSACTL_FORCE_DELETE_DISK:
1199                 return force_delete_disk(dev, arg);
1200         case 2131:
1201                 return aac_get_containers(dev);
1202         default:
1203                 return -ENOTTY;
1204         }
1205 }
1206
1207 /**
1208  *
1209  * aac_srb_callback
1210  * @context: the context set in the fib - here it is scsi cmd
1211  * @fibptr: pointer to the fib
1212  *
1213  * Handles the completion of a scsi command to a non dasd device
1214  *
1215  */
1216
1217 static void aac_srb_callback(void *context, struct fib * fibptr)
1218 {
1219         struct aac_dev *dev;
1220         struct aac_srb_reply *srbreply;
1221         struct scsi_cmnd *scsicmd;
1222
1223         scsicmd = (struct scsi_cmnd *) context;
1224         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1225
1226         if (fibptr == NULL)
1227                 BUG();
1228
1229         srbreply = (struct aac_srb_reply *) fib_data(fibptr);
1230
1231         scsicmd->sense_buffer[0] = '\0';  // initialize sense valid flag to false
1232         // calculate resid for sg 
1233         scsicmd->resid = scsicmd->request_bufflen - srbreply->data_xfer_length;
1234
1235         if(scsicmd->use_sg)
1236                 pci_unmap_sg(dev->pdev, 
1237                         (struct scatterlist *)scsicmd->buffer,
1238                         scsicmd->use_sg,
1239                         scsicmd->sc_data_direction);
1240         else if(scsicmd->request_bufflen)
1241                 pci_unmap_single(dev->pdev, (ulong)scsicmd->SCp.ptr, scsicmd->request_bufflen,
1242                         scsicmd->sc_data_direction);
1243
1244         /*
1245          * First check the fib status
1246          */
1247
1248         if (le32_to_cpu(srbreply->status) != ST_OK){
1249                 int len;
1250                 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
1251                 len = (srbreply->sense_data_size > sizeof(scsicmd->sense_buffer))?
1252                                 sizeof(scsicmd->sense_buffer):srbreply->sense_data_size;
1253                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1254                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1255         }
1256
1257         /*
1258          * Next check the srb status
1259          */
1260         switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
1261         case SRB_STATUS_ERROR_RECOVERY:
1262         case SRB_STATUS_PENDING:
1263         case SRB_STATUS_SUCCESS:
1264                 if(scsicmd->cmnd[0] == INQUIRY ){
1265                         u8 b;
1266                         u8 b1;
1267                         /* We can't expose disk devices because we can't tell whether they
1268                          * are the raw container drives or stand alone drives.  If they have
1269                          * the removable bit set then we should expose them though.
1270                          */
1271                         b = (*(u8*)scsicmd->buffer)&0x1f;
1272                         b1 = ((u8*)scsicmd->buffer)[1];
1273                         if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER 
1274                                         || (b==TYPE_DISK && (b1&0x80)) ){
1275                                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1276                         } else {
1277                                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1278                         }
1279                 } else {
1280                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1281                 }
1282                 break;
1283         case SRB_STATUS_DATA_OVERRUN:
1284                 switch(scsicmd->cmnd[0]){
1285                 case  READ_6:
1286                 case  WRITE_6:
1287                 case  READ_10:
1288                 case  WRITE_10:
1289                 case  READ_12:
1290                 case  WRITE_12:
1291                         if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
1292                                 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
1293                         } else {
1294                                 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
1295                         }
1296                         scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1297                         break;
1298                 case INQUIRY: {
1299                         u8 b;
1300                         u8 b1;
1301                         /* We can't expose disk devices because we can't tell whether they
1302                         * are the raw container drives or stand alone drives
1303                         */
1304                         b = (*(u8*)scsicmd->buffer)&0x0f;
1305                         b1 = ((u8*)scsicmd->buffer)[1];
1306                         if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
1307                                         || (b==TYPE_DISK && (b1&0x80)) ){
1308                                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1309                         } else {
1310                                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1311                         }
1312                         break;
1313                 }
1314                 default:
1315                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1316                         break;
1317                 }
1318                 break;
1319         case SRB_STATUS_ABORTED:
1320                 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
1321                 break;
1322         case SRB_STATUS_ABORT_FAILED:
1323                 // Not sure about this one - but assuming the hba was trying to abort for some reason
1324                 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
1325                 break;
1326         case SRB_STATUS_PARITY_ERROR:
1327                 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
1328                 break;
1329         case SRB_STATUS_NO_DEVICE:
1330         case SRB_STATUS_INVALID_PATH_ID:
1331         case SRB_STATUS_INVALID_TARGET_ID:
1332         case SRB_STATUS_INVALID_LUN:
1333         case SRB_STATUS_SELECTION_TIMEOUT:
1334                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1335                 break;
1336
1337         case SRB_STATUS_COMMAND_TIMEOUT:
1338         case SRB_STATUS_TIMEOUT:
1339                 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
1340                 break;
1341
1342         case SRB_STATUS_BUSY:
1343                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1344                 break;
1345
1346         case SRB_STATUS_BUS_RESET:
1347                 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
1348                 break;
1349
1350         case SRB_STATUS_MESSAGE_REJECTED:
1351                 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
1352                 break;
1353         case SRB_STATUS_REQUEST_FLUSHED:
1354         case SRB_STATUS_ERROR:
1355         case SRB_STATUS_INVALID_REQUEST:
1356         case SRB_STATUS_REQUEST_SENSE_FAILED:
1357         case SRB_STATUS_NO_HBA:
1358         case SRB_STATUS_UNEXPECTED_BUS_FREE:
1359         case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
1360         case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
1361         case SRB_STATUS_DELAYED_RETRY:
1362         case SRB_STATUS_BAD_FUNCTION:
1363         case SRB_STATUS_NOT_STARTED:
1364         case SRB_STATUS_NOT_IN_USE:
1365         case SRB_STATUS_FORCE_ABORT:
1366         case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
1367         default:
1368 #ifdef AAC_DETAILED_STATUS_INFO
1369                 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",le32_to_cpu(srbreply->srb_status&0x3f),aac_get_status_string(le32_to_cpu(srbreply->srb_status)), scsicmd->cmnd[0], le32_to_cpu(srbreply->scsi_status) );
1370 #endif
1371                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1372                 break;
1373         }
1374         if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){  // Check Condition
1375                 int len;
1376                 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
1377                 len = (srbreply->sense_data_size > sizeof(scsicmd->sense_buffer))?
1378                                 sizeof(scsicmd->sense_buffer):srbreply->sense_data_size;
1379                 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n", le32_to_cpu(srbreply->status), len);
1380                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1381                 
1382         }
1383         /*
1384          * OR in the scsi status (already shifted up a bit)
1385          */
1386         scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
1387
1388         fib_complete(fibptr);
1389         fib_free(fibptr);
1390         aac_io_done(scsicmd);
1391 }
1392
1393 /**
1394  *
1395  * aac_send_scb_fib
1396  * @scsicmd: the scsi command block
1397  *
1398  * This routine will form a FIB and fill in the aac_srb from the 
1399  * scsicmd passed in.
1400  */
1401
1402 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
1403 {
1404         struct fib* cmd_fibcontext;
1405         struct aac_dev* dev;
1406         int status;
1407         struct aac_srb *srbcmd;
1408         u16 fibsize;
1409         u32 flag;
1410         u32 timeout;
1411
1412         if( scsicmd->device->id > 15 || scsicmd->device->lun > 7) {
1413                 scsicmd->result = DID_NO_CONNECT << 16;
1414                 __aac_io_done(scsicmd);
1415                 return 0;
1416         }
1417
1418         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1419         switch(scsicmd->sc_data_direction){
1420         case DMA_TO_DEVICE:
1421                 flag = SRB_DataOut;
1422                 break;
1423         case DMA_BIDIRECTIONAL:
1424                 flag = SRB_DataIn | SRB_DataOut;
1425                 break;
1426         case DMA_FROM_DEVICE:
1427                 flag = SRB_DataIn;
1428                 break;
1429         case DMA_NONE:
1430         default:        /* shuts up some versions of gcc */
1431                 flag = SRB_NoDataXfer;
1432                 break;
1433         }
1434
1435
1436         /*
1437          *      Allocate and initialize a Fib then setup a BlockWrite command
1438          */
1439         if (!(cmd_fibcontext = fib_alloc(dev))) {
1440                 scsicmd->result = DID_ERROR << 16;
1441                 __aac_io_done(scsicmd);
1442                 return -1;
1443         }
1444         fib_init(cmd_fibcontext);
1445
1446         srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
1447         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1448         srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scsicmd->device->channel));
1449         srbcmd->target   = cpu_to_le32(scsicmd->device->id);
1450         srbcmd->lun      = cpu_to_le32(scsicmd->device->lun);
1451         srbcmd->flags    = cpu_to_le32(flag);
1452         timeout = (scsicmd->timeout-jiffies)/HZ;
1453         if(timeout == 0){
1454                 timeout = 1;
1455         }
1456         srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1457         srbcmd->retry_limit =cpu_to_le32(0); // Obsolete parameter
1458         srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
1459         
1460         if( dev->pae_support ==1 ) {
1461                 aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
1462                 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
1463
1464                 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1465                 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
1466                 /*
1467                  *      Build Scatter/Gather list
1468                  */
1469                 fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry64));
1470
1471                 /*
1472                  *      Now send the Fib to the adapter
1473                  */
1474                 status = fib_send(ScsiPortCommand64, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
1475                                   (fib_callback) aac_srb_callback, (void *) scsicmd);
1476         } else {
1477                 aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
1478                 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
1479
1480                 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1481                 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
1482                 /*
1483                  *      Build Scatter/Gather list
1484                  */
1485                 fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
1486
1487                 /*
1488                  *      Now send the Fib to the adapter
1489                  */
1490                 status = fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
1491                                   (fib_callback) aac_srb_callback, (void *) scsicmd);
1492         }
1493         /*
1494          *      Check that the command queued to the controller
1495          */
1496         if (status == -EINPROGRESS){
1497                 return 0;
1498         }
1499
1500         printk(KERN_WARNING "aac_srb: fib_send failed with status: %d\n", status);
1501         /*
1502          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1503          */
1504         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1505         __aac_io_done(scsicmd);
1506
1507         fib_complete(cmd_fibcontext);
1508         fib_free(cmd_fibcontext);
1509
1510         return -1;
1511 }
1512
1513 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
1514 {
1515         struct aac_dev *dev;
1516         unsigned long byte_count = 0;
1517
1518         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1519         // Get rid of old data
1520         psg->count = cpu_to_le32(0);
1521         psg->sg[0].addr = cpu_to_le32(0);
1522         psg->sg[0].count = cpu_to_le32(0);  
1523         if (scsicmd->use_sg) {
1524                 struct scatterlist *sg;
1525                 int i;
1526                 int sg_count;
1527                 sg = (struct scatterlist *) scsicmd->request_buffer;
1528
1529                 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
1530                         scsicmd->sc_data_direction);
1531                 psg->count = cpu_to_le32(sg_count);
1532
1533                 byte_count = 0;
1534
1535                 for (i = 0; i < sg_count; i++) {
1536                         psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
1537                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
1538                         byte_count += sg_dma_len(sg);
1539                         sg++;
1540                 }
1541                 /* hba wants the size to be exact */
1542                 if(byte_count > scsicmd->request_bufflen){
1543                         psg->sg[i-1].count -= (byte_count - scsicmd->request_bufflen);
1544                         byte_count = scsicmd->request_bufflen;
1545                 }
1546                 /* Check for command underflow */
1547                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
1548                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
1549                                         byte_count, scsicmd->underflow);
1550                 }
1551         }
1552         else if(scsicmd->request_bufflen) {
1553                 dma_addr_t addr; 
1554                 addr = pci_map_single(dev->pdev,
1555                                 scsicmd->request_buffer,
1556                                 scsicmd->request_bufflen,
1557                                 scsicmd->sc_data_direction);
1558                 psg->count = cpu_to_le32(1);
1559                 psg->sg[0].addr = cpu_to_le32(addr);
1560                 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);  
1561                 scsicmd->SCp.ptr = (char *)(ulong)addr;
1562                 byte_count = scsicmd->request_bufflen;
1563         }
1564         return byte_count;
1565 }
1566
1567
1568 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
1569 {
1570         struct aac_dev *dev;
1571         unsigned long byte_count = 0;
1572         u64 le_addr;
1573
1574         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1575         // Get rid of old data
1576         psg->count = cpu_to_le32(0);
1577         psg->sg[0].addr[0] = cpu_to_le32(0);
1578         psg->sg[0].addr[1] = cpu_to_le32(0);
1579         psg->sg[0].count = cpu_to_le32(0);  
1580         if (scsicmd->use_sg) {
1581                 struct scatterlist *sg;
1582                 int i;
1583                 int sg_count;
1584                 sg = (struct scatterlist *) scsicmd->request_buffer;
1585
1586                 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
1587                         scsicmd->sc_data_direction);
1588                 psg->count = cpu_to_le32(sg_count);
1589
1590                 byte_count = 0;
1591
1592                 for (i = 0; i < sg_count; i++) {
1593                         le_addr = cpu_to_le64(sg_dma_address(sg));
1594                         psg->sg[i].addr[1] = (u32)(le_addr>>32);
1595                         psg->sg[i].addr[0] = (u32)(le_addr & 0xffffffff);
1596                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
1597                         byte_count += sg_dma_len(sg);
1598                         sg++;
1599                 }
1600                 /* hba wants the size to be exact */
1601                 if(byte_count > scsicmd->request_bufflen){
1602                         psg->sg[i-1].count -= (byte_count - scsicmd->request_bufflen);
1603                         byte_count = scsicmd->request_bufflen;
1604                 }
1605                 /* Check for command underflow */
1606                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
1607                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
1608                                         byte_count, scsicmd->underflow);
1609                 }
1610         }
1611         else if(scsicmd->request_bufflen) {
1612                 dma_addr_t addr; 
1613                 addr = pci_map_single(dev->pdev,
1614                                 scsicmd->request_buffer,
1615                                 scsicmd->request_bufflen,
1616                                 scsicmd->sc_data_direction);
1617                 psg->count = cpu_to_le32(1);
1618                 le_addr = cpu_to_le64(addr);
1619                 psg->sg[0].addr[1] = (u32)(le_addr>>32);
1620                 psg->sg[0].addr[0] = (u32)(le_addr & 0xffffffff);
1621                 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);  
1622                 scsicmd->SCp.ptr = (char *)(ulong)addr;
1623                 byte_count = scsicmd->request_bufflen;
1624         }
1625         return byte_count;
1626 }
1627
1628 #ifdef AAC_DETAILED_STATUS_INFO
1629
1630 struct aac_srb_status_info {
1631         u32     status;
1632         char    *str;
1633 };
1634
1635
1636 static struct aac_srb_status_info srb_status_info[] = {
1637         { SRB_STATUS_PENDING,           "Pending Status"},
1638         { SRB_STATUS_SUCCESS,           "Success"},
1639         { SRB_STATUS_ABORTED,           "Aborted Command"},
1640         { SRB_STATUS_ABORT_FAILED,      "Abort Failed"},
1641         { SRB_STATUS_ERROR,             "Error Event"}, 
1642         { SRB_STATUS_BUSY,              "Device Busy"},
1643         { SRB_STATUS_INVALID_REQUEST,   "Invalid Request"},
1644         { SRB_STATUS_INVALID_PATH_ID,   "Invalid Path ID"},
1645         { SRB_STATUS_NO_DEVICE,         "No Device"},
1646         { SRB_STATUS_TIMEOUT,           "Timeout"},
1647         { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
1648         { SRB_STATUS_COMMAND_TIMEOUT,   "Command Timeout"},
1649         { SRB_STATUS_MESSAGE_REJECTED,  "Message Rejected"},
1650         { SRB_STATUS_BUS_RESET,         "Bus Reset"},
1651         { SRB_STATUS_PARITY_ERROR,      "Parity Error"},
1652         { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
1653         { SRB_STATUS_NO_HBA,            "No HBA"},
1654         { SRB_STATUS_DATA_OVERRUN,      "Data Overrun/Data Underrun"},
1655         { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
1656         { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
1657         { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
1658         { SRB_STATUS_REQUEST_FLUSHED,   "Request Flushed"},
1659         { SRB_STATUS_DELAYED_RETRY,     "Delayed Retry"},
1660         { SRB_STATUS_INVALID_LUN,       "Invalid LUN"}, 
1661         { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
1662         { SRB_STATUS_BAD_FUNCTION,      "Bad Function"},
1663         { SRB_STATUS_ERROR_RECOVERY,    "Error Recovery"},
1664         { SRB_STATUS_NOT_STARTED,       "Not Started"},
1665         { SRB_STATUS_NOT_IN_USE,        "Not In Use"},
1666         { SRB_STATUS_FORCE_ABORT,       "Force Abort"},
1667         { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
1668         { 0xff,                         "Unknown Error"}
1669 };
1670
1671 char *aac_get_status_string(u32 status)
1672 {
1673         int i;
1674
1675         for(i=0; i < (sizeof(srb_status_info)/sizeof(struct aac_srb_status_info)); i++ ){
1676                 if(srb_status_info[i].status == status){
1677                         return srb_status_info[i].str;
1678                 }
1679         }
1680
1681         return "Bad Status Code";
1682 }
1683
1684 #endif