]> err.no Git - linux-2.6/blob - drivers/scsi/aacraid/aachba.c
[SCSI] aacraid: fix inquiry page
[linux-2.6] / 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 /*
57  *      Sense codes
58  */
59  
60 #define SENCODE_NO_SENSE                        0x00
61 #define SENCODE_END_OF_DATA                     0x00
62 #define SENCODE_BECOMING_READY                  0x04
63 #define SENCODE_INIT_CMD_REQUIRED               0x04
64 #define SENCODE_PARAM_LIST_LENGTH_ERROR         0x1A
65 #define SENCODE_INVALID_COMMAND                 0x20
66 #define SENCODE_LBA_OUT_OF_RANGE                0x21
67 #define SENCODE_INVALID_CDB_FIELD               0x24
68 #define SENCODE_LUN_NOT_SUPPORTED               0x25
69 #define SENCODE_INVALID_PARAM_FIELD             0x26
70 #define SENCODE_PARAM_NOT_SUPPORTED             0x26
71 #define SENCODE_PARAM_VALUE_INVALID             0x26
72 #define SENCODE_RESET_OCCURRED                  0x29
73 #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET     0x3E
74 #define SENCODE_INQUIRY_DATA_CHANGED            0x3F
75 #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED     0x39
76 #define SENCODE_DIAGNOSTIC_FAILURE              0x40
77 #define SENCODE_INTERNAL_TARGET_FAILURE         0x44
78 #define SENCODE_INVALID_MESSAGE_ERROR           0x49
79 #define SENCODE_LUN_FAILED_SELF_CONFIG          0x4c
80 #define SENCODE_OVERLAPPED_COMMAND              0x4E
81
82 /*
83  *      Additional sense codes
84  */
85  
86 #define ASENCODE_NO_SENSE                       0x00
87 #define ASENCODE_END_OF_DATA                    0x05
88 #define ASENCODE_BECOMING_READY                 0x01
89 #define ASENCODE_INIT_CMD_REQUIRED              0x02
90 #define ASENCODE_PARAM_LIST_LENGTH_ERROR        0x00
91 #define ASENCODE_INVALID_COMMAND                0x00
92 #define ASENCODE_LBA_OUT_OF_RANGE               0x00
93 #define ASENCODE_INVALID_CDB_FIELD              0x00
94 #define ASENCODE_LUN_NOT_SUPPORTED              0x00
95 #define ASENCODE_INVALID_PARAM_FIELD            0x00
96 #define ASENCODE_PARAM_NOT_SUPPORTED            0x01
97 #define ASENCODE_PARAM_VALUE_INVALID            0x02
98 #define ASENCODE_RESET_OCCURRED                 0x00
99 #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET    0x00
100 #define ASENCODE_INQUIRY_DATA_CHANGED           0x03
101 #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED    0x00
102 #define ASENCODE_DIAGNOSTIC_FAILURE             0x80
103 #define ASENCODE_INTERNAL_TARGET_FAILURE        0x00
104 #define ASENCODE_INVALID_MESSAGE_ERROR          0x00
105 #define ASENCODE_LUN_FAILED_SELF_CONFIG         0x00
106 #define ASENCODE_OVERLAPPED_COMMAND             0x00
107
108 #define BYTE0(x) (unsigned char)(x)
109 #define BYTE1(x) (unsigned char)((x) >> 8)
110 #define BYTE2(x) (unsigned char)((x) >> 16)
111 #define BYTE3(x) (unsigned char)((x) >> 24)
112
113 /*------------------------------------------------------------------------------
114  *              S T R U C T S / T Y P E D E F S
115  *----------------------------------------------------------------------------*/
116 /* SCSI inquiry data */
117 struct inquiry_data {
118         u8 inqd_pdt;    /* Peripheral qualifier | Peripheral Device Type  */
119         u8 inqd_dtq;    /* RMB | Device Type Qualifier  */
120         u8 inqd_ver;    /* ISO version | ECMA version | ANSI-approved version */
121         u8 inqd_rdf;    /* AENC | TrmIOP | Response data format */
122         u8 inqd_len;    /* Additional length (n-4) */
123         u8 inqd_pad1[2];/* Reserved - must be zero */
124         u8 inqd_pad2;   /* RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
125         u8 inqd_vid[8]; /* Vendor ID */
126         u8 inqd_pid[16];/* Product ID */
127         u8 inqd_prl[4]; /* Product Revision Level */
128 };
129
130 /*
131  *              M O D U L E   G L O B A L S
132  */
133  
134 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
135 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
136 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg);
137 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
138 #ifdef AAC_DETAILED_STATUS_INFO
139 static char *aac_get_status_string(u32 status);
140 #endif
141
142 /*
143  *      Non dasd selection is handled entirely in aachba now
144  */     
145  
146 static int nondasd = -1;
147 static int dacmode = -1;
148
149 static int commit = -1;
150
151 module_param(nondasd, int, 0);
152 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
153 module_param(dacmode, int, 0);
154 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
155 module_param(commit, int, 0);
156 MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the adapter for foreign arrays.\nThis is typically needed in systems that do not have a BIOS. 0=off, 1=on");
157
158 int numacb = -1;
159 module_param(numacb, int, S_IRUGO|S_IWUSR);
160 MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control blocks (FIB) allocated. Valid\nvalues are 512 and down. Default is to use suggestion from Firmware.");
161
162 int acbsize = -1;
163 module_param(acbsize, int, S_IRUGO|S_IWUSR);
164 MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB) size. Valid values are 512,\n2048, 4096 and 8192. Default is to use suggestion from Firmware.");
165 /**
166  *      aac_get_config_status   -       check the adapter configuration
167  *      @common: adapter to query
168  *
169  *      Query config status, and commit the configuration if needed.
170  */
171 int aac_get_config_status(struct aac_dev *dev)
172 {
173         int status = 0;
174         struct fib * fibptr;
175
176         if (!(fibptr = fib_alloc(dev)))
177                 return -ENOMEM;
178
179         fib_init(fibptr);
180         {
181                 struct aac_get_config_status *dinfo;
182                 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
183
184                 dinfo->command = cpu_to_le32(VM_ContainerConfig);
185                 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
186                 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
187         }
188
189         status = fib_send(ContainerCommand,
190                             fibptr,
191                             sizeof (struct aac_get_config_status),
192                             FsaNormal,
193                             1, 1,
194                             NULL, NULL);
195         if (status < 0 ) {
196                 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
197         } else {
198                 struct aac_get_config_status_resp *reply
199                   = (struct aac_get_config_status_resp *) fib_data(fibptr);
200                 dprintk((KERN_WARNING
201                   "aac_get_config_status: response=%d status=%d action=%d\n",
202                   le32_to_cpu(reply->response),
203                   le32_to_cpu(reply->status),
204                   le32_to_cpu(reply->data.action)));
205                 if ((le32_to_cpu(reply->response) != ST_OK) ||
206                      (le32_to_cpu(reply->status) != CT_OK) ||
207                      (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
208                         printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
209                         status = -EINVAL;
210                 }
211         }
212         fib_complete(fibptr);
213         /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
214         if (status >= 0) {
215                 if (commit == 1) {
216                         struct aac_commit_config * dinfo;
217                         fib_init(fibptr);
218                         dinfo = (struct aac_commit_config *) fib_data(fibptr);
219         
220                         dinfo->command = cpu_to_le32(VM_ContainerConfig);
221                         dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
222         
223                         status = fib_send(ContainerCommand,
224                                     fibptr,
225                                     sizeof (struct aac_commit_config),
226                                     FsaNormal,
227                                     1, 1,
228                                     NULL, NULL);
229                         fib_complete(fibptr);
230                 } else if (commit == 0) {
231                         printk(KERN_WARNING
232                           "aac_get_config_status: Foreign device configurations are being ignored\n");
233                 }
234         }
235         fib_free(fibptr);
236         return status;
237 }
238
239 /**
240  *      aac_get_containers      -       list containers
241  *      @common: adapter to probe
242  *
243  *      Make a list of all containers on this controller
244  */
245 int aac_get_containers(struct aac_dev *dev)
246 {
247         struct fsa_dev_info *fsa_dev_ptr;
248         u32 index; 
249         int status = 0;
250         struct fib * fibptr;
251         unsigned instance;
252         struct aac_get_container_count *dinfo;
253         struct aac_get_container_count_resp *dresp;
254         int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
255
256         instance = dev->scsi_host_ptr->unique_id;
257
258         if (!(fibptr = fib_alloc(dev)))
259                 return -ENOMEM;
260
261         fib_init(fibptr);
262         dinfo = (struct aac_get_container_count *) fib_data(fibptr);
263         dinfo->command = cpu_to_le32(VM_ContainerConfig);
264         dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
265
266         status = fib_send(ContainerCommand,
267                     fibptr,
268                     sizeof (struct aac_get_container_count),
269                     FsaNormal,
270                     1, 1,
271                     NULL, NULL);
272         if (status >= 0) {
273                 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
274                 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
275                 fib_complete(fibptr);
276         }
277
278         if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
279                 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
280         fsa_dev_ptr = (struct fsa_dev_info *) kmalloc(
281           sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
282         if (!fsa_dev_ptr) {
283                 fib_free(fibptr);
284                 return -ENOMEM;
285         }
286         memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
287
288         dev->fsa_dev = fsa_dev_ptr;
289         dev->maximum_num_containers = maximum_num_containers;
290
291         for (index = 0; index < dev->maximum_num_containers; index++) {
292                 struct aac_query_mount *dinfo;
293                 struct aac_mount *dresp;
294
295                 fsa_dev_ptr[index].devname[0] = '\0';
296
297                 fib_init(fibptr);
298                 dinfo = (struct aac_query_mount *) fib_data(fibptr);
299
300                 dinfo->command = cpu_to_le32(VM_NameServe);
301                 dinfo->count = cpu_to_le32(index);
302                 dinfo->type = cpu_to_le32(FT_FILESYS);
303
304                 status = fib_send(ContainerCommand,
305                                     fibptr,
306                                     sizeof (struct aac_query_mount),
307                                     FsaNormal,
308                                     1, 1,
309                                     NULL, NULL);
310                 if (status < 0 ) {
311                         printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
312                         break;
313                 }
314                 dresp = (struct aac_mount *)fib_data(fibptr);
315
316                 dprintk ((KERN_DEBUG
317                   "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%u\n",
318                   (int)index, (int)le32_to_cpu(dresp->status),
319                   (int)le32_to_cpu(dresp->mnt[0].vol),
320                   (int)le32_to_cpu(dresp->mnt[0].state),
321                   (unsigned)le32_to_cpu(dresp->mnt[0].capacity)));
322                 if ((le32_to_cpu(dresp->status) == ST_OK) &&
323                     (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
324                     (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
325                         fsa_dev_ptr[index].valid = 1;
326                         fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
327                         fsa_dev_ptr[index].size = le32_to_cpu(dresp->mnt[0].capacity);
328                         if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
329                                     fsa_dev_ptr[index].ro = 1;
330                 }
331                 fib_complete(fibptr);
332                 /*
333                  *      If there are no more containers, then stop asking.
334                  */
335                 if ((index + 1) >= le32_to_cpu(dresp->count)){
336                         break;
337                 }
338         }
339         fib_free(fibptr);
340         return status;
341 }
342
343 static void aac_io_done(struct scsi_cmnd * scsicmd)
344 {
345         unsigned long cpu_flags;
346         struct Scsi_Host *host = scsicmd->device->host;
347         spin_lock_irqsave(host->host_lock, cpu_flags);
348         scsicmd->scsi_done(scsicmd);
349         spin_unlock_irqrestore(host->host_lock, cpu_flags);
350 }
351
352 static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
353 {
354         void *buf;
355         unsigned int transfer_len;
356         struct scatterlist *sg = scsicmd->request_buffer;
357
358         if (scsicmd->use_sg) {
359                 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
360                 transfer_len = min(sg->length, len + offset);
361         } else {
362                 buf = scsicmd->request_buffer;
363                 transfer_len = min(scsicmd->request_bufflen, len + offset);
364         }
365
366         memcpy(buf + offset, data, transfer_len - offset);
367
368         if (scsicmd->use_sg) 
369                 kunmap_atomic(buf - sg->offset, KM_IRQ0);
370
371 }
372
373 static void get_container_name_callback(void *context, struct fib * fibptr)
374 {
375         struct aac_get_name_resp * get_name_reply;
376         struct scsi_cmnd * scsicmd;
377
378         scsicmd = (struct scsi_cmnd *) context;
379
380         dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
381         if (fibptr == NULL)
382                 BUG();
383
384         get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
385         /* Failure is irrelevant, using default value instead */
386         if ((le32_to_cpu(get_name_reply->status) == CT_OK)
387          && (get_name_reply->data[0] != '\0')) {
388                 char *sp = get_name_reply->data;
389                 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
390                 while (*sp == ' ')
391                         ++sp;
392                 if (*sp) {
393                         char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
394                         int count = sizeof(d);
395                         char *dp = d;
396                         do {
397                                 *dp++ = (*sp) ? *sp++ : ' ';
398                         } while (--count > 0);
399                         aac_internal_transfer(scsicmd, d, 
400                           offsetof(struct inquiry_data, inqd_pid), sizeof(d));
401                 }
402         }
403
404         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
405
406         fib_complete(fibptr);
407         fib_free(fibptr);
408         aac_io_done(scsicmd);
409 }
410
411 /**
412  *      aac_get_container_name  -       get container name, none blocking.
413  */
414 static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
415 {
416         int status;
417         struct aac_get_name *dinfo;
418         struct fib * cmd_fibcontext;
419         struct aac_dev * dev;
420
421         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
422
423         if (!(cmd_fibcontext = fib_alloc(dev)))
424                 return -ENOMEM;
425
426         fib_init(cmd_fibcontext);
427         dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
428
429         dinfo->command = cpu_to_le32(VM_ContainerConfig);
430         dinfo->type = cpu_to_le32(CT_READ_NAME);
431         dinfo->cid = cpu_to_le32(cid);
432         dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
433
434         status = fib_send(ContainerCommand, 
435                   cmd_fibcontext, 
436                   sizeof (struct aac_get_name),
437                   FsaNormal, 
438                   0, 1, 
439                   (fib_callback) get_container_name_callback, 
440                   (void *) scsicmd);
441         
442         /*
443          *      Check that the command queued to the controller
444          */
445         if (status == -EINPROGRESS) 
446                 return 0;
447                 
448         printk(KERN_WARNING "aac_get_container_name: fib_send failed with status: %d.\n", status);
449         fib_complete(cmd_fibcontext);
450         fib_free(cmd_fibcontext);
451         return -1;
452 }
453
454 /**
455  *      probe_container         -       query a logical volume
456  *      @dev: device to query
457  *      @cid: container identifier
458  *
459  *      Queries the controller about the given volume. The volume information
460  *      is updated in the struct fsa_dev_info structure rather than returned.
461  */
462  
463 static int probe_container(struct aac_dev *dev, int cid)
464 {
465         struct fsa_dev_info *fsa_dev_ptr;
466         int status;
467         struct aac_query_mount *dinfo;
468         struct aac_mount *dresp;
469         struct fib * fibptr;
470         unsigned instance;
471
472         fsa_dev_ptr = dev->fsa_dev;
473         instance = dev->scsi_host_ptr->unique_id;
474
475         if (!(fibptr = fib_alloc(dev)))
476                 return -ENOMEM;
477
478         fib_init(fibptr);
479
480         dinfo = (struct aac_query_mount *)fib_data(fibptr);
481
482         dinfo->command = cpu_to_le32(VM_NameServe);
483         dinfo->count = cpu_to_le32(cid);
484         dinfo->type = cpu_to_le32(FT_FILESYS);
485
486         status = fib_send(ContainerCommand,
487                             fibptr,
488                             sizeof(struct aac_query_mount),
489                             FsaNormal,
490                             1, 1,
491                             NULL, NULL);
492         if (status < 0) {
493                 printk(KERN_WARNING "aacraid: probe_container query failed.\n");
494                 goto error;
495         }
496
497         dresp = (struct aac_mount *) fib_data(fibptr);
498
499         if ((le32_to_cpu(dresp->status) == ST_OK) &&
500             (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
501             (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
502                 fsa_dev_ptr[cid].valid = 1;
503                 fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
504                 fsa_dev_ptr[cid].size = le32_to_cpu(dresp->mnt[0].capacity);
505                 if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
506                         fsa_dev_ptr[cid].ro = 1;
507         }
508
509 error:
510         fib_complete(fibptr);
511         fib_free(fibptr);
512
513         return status;
514 }
515
516 /* Local Structure to set SCSI inquiry data strings */
517 struct scsi_inq {
518         char vid[8];         /* Vendor ID */
519         char pid[16];        /* Product ID */
520         char prl[4];         /* Product Revision Level */
521 };
522
523 /**
524  *      InqStrCopy      -       string merge
525  *      @a:     string to copy from
526  *      @b:     string to copy to
527  *
528  *      Copy a String from one location to another
529  *      without copying \0
530  */
531
532 static void inqstrcpy(char *a, char *b)
533 {
534
535         while(*a != (char)0) 
536                 *b++ = *a++;
537 }
538
539 static char *container_types[] = {
540         "None",
541         "Volume",
542         "Mirror",
543         "Stripe",
544         "RAID5",
545         "SSRW",
546         "SSRO",
547         "Morph",
548         "Legacy",
549         "RAID4",
550         "RAID10",             
551         "RAID00",             
552         "V-MIRRORS",          
553         "PSEUDO R4",          
554         "RAID50",
555         "RAID5D",
556         "RAID5D0",
557         "RAID1E",
558         "RAID6",
559         "RAID60",
560         "Unknown"
561 };
562
563
564
565 /* Function: setinqstr
566  *
567  * Arguments: [1] pointer to void [1] int
568  *
569  * Purpose: Sets SCSI inquiry data strings for vendor, product
570  * and revision level. Allows strings to be set in platform dependant
571  * files instead of in OS dependant driver source.
572  */
573
574 static void setinqstr(struct aac_dev *dev, void *data, int tindex)
575 {
576         struct scsi_inq *str;
577
578         str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
579         memset(str, ' ', sizeof(*str));
580
581         if (dev->supplement_adapter_info.AdapterTypeText[0]) {
582                 char * cp = dev->supplement_adapter_info.AdapterTypeText;
583                 int c = sizeof(str->vid);
584                 while (*cp && *cp != ' ' && --c)
585                         ++cp;
586                 c = *cp;
587                 *cp = '\0';
588                 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
589                   str->vid); 
590                 *cp = c;
591                 while (*cp && *cp != ' ')
592                         ++cp;
593                 while (*cp == ' ')
594                         ++cp;
595                 /* last six chars reserved for vol type */
596                 c = 0;
597                 if (strlen(cp) > sizeof(str->pid)) {
598                         c = cp[sizeof(str->pid)];
599                         cp[sizeof(str->pid)] = '\0';
600                 }
601                 inqstrcpy (cp, str->pid);
602                 if (c)
603                         cp[sizeof(str->pid)] = c;
604         } else {
605                 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
606    
607                 inqstrcpy (mp->vname, str->vid); 
608                 /* last six chars reserved for vol type */
609                 inqstrcpy (mp->model, str->pid);
610         }
611
612         if (tindex < (sizeof(container_types)/sizeof(char *))){
613                 char *findit = str->pid;
614
615                 for ( ; *findit != ' '; findit++); /* walk till we find a space */
616                 /* RAID is superfluous in the context of a RAID device */
617                 if (memcmp(findit-4, "RAID", 4) == 0)
618                         *(findit -= 4) = ' ';
619                 if (((findit - str->pid) + strlen(container_types[tindex]))
620                  < (sizeof(str->pid) + sizeof(str->prl)))
621                         inqstrcpy (container_types[tindex], findit + 1);
622         }
623         inqstrcpy ("V1.0", str->prl);
624 }
625
626 static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
627                       u8 a_sense_code, u8 incorrect_length,
628                       u8 bit_pointer, u16 field_pointer,
629                       u32 residue)
630 {
631         sense_buf[0] = 0xF0;    /* Sense data valid, err code 70h (current error) */
632         sense_buf[1] = 0;       /* Segment number, always zero */
633
634         if (incorrect_length) {
635                 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
636                 sense_buf[3] = BYTE3(residue);
637                 sense_buf[4] = BYTE2(residue);
638                 sense_buf[5] = BYTE1(residue);
639                 sense_buf[6] = BYTE0(residue);
640         } else
641                 sense_buf[2] = sense_key;       /* Sense key */
642
643         if (sense_key == ILLEGAL_REQUEST)
644                 sense_buf[7] = 10;      /* Additional sense length */
645         else
646                 sense_buf[7] = 6;       /* Additional sense length */
647
648         sense_buf[12] = sense_code;     /* Additional sense code */
649         sense_buf[13] = a_sense_code;   /* Additional sense code qualifier */
650         if (sense_key == ILLEGAL_REQUEST) {
651                 sense_buf[15] = 0;
652
653                 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
654                         sense_buf[15] = 0x80;/* Std sense key specific field */
655                 /* Illegal parameter is in the parameter block */
656
657                 if (sense_code == SENCODE_INVALID_CDB_FIELD)
658                         sense_buf[15] = 0xc0;/* Std sense key specific field */
659                 /* Illegal parameter is in the CDB block */
660                 sense_buf[15] |= bit_pointer;
661                 sense_buf[16] = field_pointer >> 8;     /* MSB */
662                 sense_buf[17] = field_pointer;          /* LSB */
663         }
664 }
665
666 int aac_get_adapter_info(struct aac_dev* dev)
667 {
668         struct fib* fibptr;
669         int rcode;
670         u32 tmp;
671         struct aac_adapter_info *info;
672         struct aac_bus_info *command;
673         struct aac_bus_info_response *bus_info;
674
675         if (!(fibptr = fib_alloc(dev)))
676                 return -ENOMEM;
677
678         fib_init(fibptr);
679         info = (struct aac_adapter_info *) fib_data(fibptr);
680         memset(info,0,sizeof(*info));
681
682         rcode = fib_send(RequestAdapterInfo,
683                          fibptr, 
684                          sizeof(*info),
685                          FsaNormal, 
686                          1, 1, 
687                          NULL, 
688                          NULL);
689
690         if (rcode < 0) {
691                 fib_complete(fibptr);
692                 fib_free(fibptr);
693                 return rcode;
694         }
695         memcpy(&dev->adapter_info, info, sizeof(*info));
696
697         if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
698                 struct aac_supplement_adapter_info * info;
699
700                 fib_init(fibptr);
701
702                 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
703
704                 memset(info,0,sizeof(*info));
705
706                 rcode = fib_send(RequestSupplementAdapterInfo,
707                                  fibptr,
708                                  sizeof(*info),
709                                  FsaNormal,
710                                  1, 1,
711                                  NULL,
712                                  NULL);
713
714                 if (rcode >= 0)
715                         memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
716         }
717
718
719         /* 
720          * GetBusInfo 
721          */
722
723         fib_init(fibptr);
724
725         bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
726
727         memset(bus_info, 0, sizeof(*bus_info));
728
729         command = (struct aac_bus_info *)bus_info;
730
731         command->Command = cpu_to_le32(VM_Ioctl);
732         command->ObjType = cpu_to_le32(FT_DRIVE);
733         command->MethodId = cpu_to_le32(1);
734         command->CtlCmd = cpu_to_le32(GetBusInfo);
735
736         rcode = fib_send(ContainerCommand,
737                          fibptr,
738                          sizeof (*bus_info),
739                          FsaNormal,
740                          1, 1,
741                          NULL, NULL);
742
743         if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
744                 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
745                 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
746         }
747
748         tmp = le32_to_cpu(dev->adapter_info.kernelrev);
749         printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n", 
750                         dev->name, 
751                         dev->id,
752                         tmp>>24,
753                         (tmp>>16)&0xff,
754                         tmp&0xff,
755                         le32_to_cpu(dev->adapter_info.kernelbuild),
756                         (int)sizeof(dev->supplement_adapter_info.BuildDate),
757                         dev->supplement_adapter_info.BuildDate);
758         tmp = le32_to_cpu(dev->adapter_info.monitorrev);
759         printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n", 
760                         dev->name, dev->id,
761                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
762                         le32_to_cpu(dev->adapter_info.monitorbuild));
763         tmp = le32_to_cpu(dev->adapter_info.biosrev);
764         printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n", 
765                         dev->name, dev->id,
766                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
767                         le32_to_cpu(dev->adapter_info.biosbuild));
768         if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
769                 printk(KERN_INFO "%s%d: serial %x\n",
770                         dev->name, dev->id,
771                         le32_to_cpu(dev->adapter_info.serial[0]));
772
773         dev->nondasd_support = 0;
774         dev->raid_scsi_mode = 0;
775         if(dev->adapter_info.options & AAC_OPT_NONDASD){
776                 dev->nondasd_support = 1;
777         }
778
779         /*
780          * If the firmware supports ROMB RAID/SCSI mode and we are currently
781          * in RAID/SCSI mode, set the flag. For now if in this mode we will
782          * force nondasd support on. If we decide to allow the non-dasd flag
783          * additional changes changes will have to be made to support
784          * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
785          * changed to support the new dev->raid_scsi_mode flag instead of
786          * leaching off of the dev->nondasd_support flag. Also in linit.c the
787          * function aac_detect will have to be modified where it sets up the
788          * max number of channels based on the aac->nondasd_support flag only.
789          */
790         if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
791             (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
792                 dev->nondasd_support = 1;
793                 dev->raid_scsi_mode = 1;
794         }
795         if (dev->raid_scsi_mode != 0)
796                 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
797                                 dev->name, dev->id);
798                 
799         if(nondasd != -1) {  
800                 dev->nondasd_support = (nondasd!=0);
801         }
802         if(dev->nondasd_support != 0){
803                 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
804         }
805
806         dev->dac_support = 0;
807         if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
808                 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
809                 dev->dac_support = 1;
810         }
811
812         if(dacmode != -1) {
813                 dev->dac_support = (dacmode!=0);
814         }
815         if(dev->dac_support != 0) {
816                 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
817                         !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
818                         printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
819                                 dev->name, dev->id);
820                 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
821                         !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
822                         printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
823                                 dev->name, dev->id);
824                         dev->dac_support = 0;
825                 } else {
826                         printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
827                                 dev->name, dev->id);
828                         rcode = -ENOMEM;
829                 }
830         }
831         /* 
832          * 57 scatter gather elements 
833          */
834         if (!(dev->raw_io_interface)) {
835                 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
836                         sizeof(struct aac_fibhdr) -
837                         sizeof(struct aac_write) + sizeof(struct sgmap)) /
838                                 sizeof(struct sgmap);
839                 if (dev->dac_support) {
840                         /* 
841                          * 38 scatter gather elements 
842                          */
843                         dev->scsi_host_ptr->sg_tablesize =
844                                 (dev->max_fib_size -
845                                 sizeof(struct aac_fibhdr) -
846                                 sizeof(struct aac_write64) +
847                                 sizeof(struct sgmap64)) /
848                                         sizeof(struct sgmap64);
849                 }
850                 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
851                 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
852                         /*
853                          * Worst case size that could cause sg overflow when
854                          * we break up SG elements that are larger than 64KB.
855                          * Would be nice if we could tell the SCSI layer what
856                          * the maximum SG element size can be. Worst case is
857                          * (sg_tablesize-1) 4KB elements with one 64KB
858                          * element.
859                          *      32bit -> 468 or 238KB   64bit -> 424 or 212KB
860                          */
861                         dev->scsi_host_ptr->max_sectors =
862                           (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
863                 }
864         }
865
866         fib_complete(fibptr);
867         fib_free(fibptr);
868
869         return rcode;
870 }
871
872
873 static void io_callback(void *context, struct fib * fibptr)
874 {
875         struct aac_dev *dev;
876         struct aac_read_reply *readreply;
877         struct scsi_cmnd *scsicmd;
878         u32 cid;
879
880         scsicmd = (struct scsi_cmnd *) context;
881
882         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
883         cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
884
885         dprintk((KERN_DEBUG "io_callback[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3], jiffies));
886
887         if (fibptr == NULL)
888                 BUG();
889                 
890         if(scsicmd->use_sg)
891                 pci_unmap_sg(dev->pdev, 
892                         (struct scatterlist *)scsicmd->buffer,
893                         scsicmd->use_sg,
894                         scsicmd->sc_data_direction);
895         else if(scsicmd->request_bufflen)
896                 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
897                                  scsicmd->request_bufflen,
898                                  scsicmd->sc_data_direction);
899         readreply = (struct aac_read_reply *)fib_data(fibptr);
900         if (le32_to_cpu(readreply->status) == ST_OK)
901                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
902         else {
903 #ifdef AAC_DETAILED_STATUS_INFO
904                 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
905                   le32_to_cpu(readreply->status));
906 #endif
907                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
908                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
909                                     HARDWARE_ERROR,
910                                     SENCODE_INTERNAL_TARGET_FAILURE,
911                                     ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
912                                     0, 0);
913                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
914                   (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
915                     ? sizeof(scsicmd->sense_buffer)
916                     : sizeof(dev->fsa_dev[cid].sense_data));
917         }
918         fib_complete(fibptr);
919         fib_free(fibptr);
920
921         aac_io_done(scsicmd);
922 }
923
924 static int aac_read(struct scsi_cmnd * scsicmd, int cid)
925 {
926         u32 lba;
927         u32 count;
928         int status;
929
930         u16 fibsize;
931         struct aac_dev *dev;
932         struct fib * cmd_fibcontext;
933
934         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
935         /*
936          *      Get block address and transfer length
937          */
938         if (scsicmd->cmnd[0] == READ_6) /* 6 byte command */
939         {
940                 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
941
942                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
943                 count = scsicmd->cmnd[4];
944
945                 if (count == 0)
946                         count = 256;
947         } else {
948                 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
949
950                 lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
951                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
952         }
953         dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %u, t = %ld.\n",
954           smp_processor_id(), (unsigned long long)lba, jiffies));
955         /*
956          *      Alocate and initialize a Fib
957          */
958         if (!(cmd_fibcontext = fib_alloc(dev))) {
959                 return -1;
960         }
961
962         fib_init(cmd_fibcontext);
963
964         if (dev->raw_io_interface) {
965                 struct aac_raw_io *readcmd;
966                 readcmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
967                 readcmd->block[0] = cpu_to_le32(lba);
968                 readcmd->block[1] = 0;
969                 readcmd->count = cpu_to_le32(count<<9);
970                 readcmd->cid = cpu_to_le16(cid);
971                 readcmd->flags = cpu_to_le16(1);
972                 readcmd->bpTotal = 0;
973                 readcmd->bpComplete = 0;
974                 
975                 aac_build_sgraw(scsicmd, &readcmd->sg);
976                 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
977                 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))
978                         BUG();
979                 /*
980                  *      Now send the Fib to the adapter
981                  */
982                 status = fib_send(ContainerRawIo,
983                           cmd_fibcontext, 
984                           fibsize, 
985                           FsaNormal, 
986                           0, 1, 
987                           (fib_callback) io_callback, 
988                           (void *) scsicmd);
989         } else if (dev->dac_support == 1) {
990                 struct aac_read64 *readcmd;
991                 readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
992                 readcmd->command = cpu_to_le32(VM_CtHostRead64);
993                 readcmd->cid = cpu_to_le16(cid);
994                 readcmd->sector_count = cpu_to_le16(count);
995                 readcmd->block = cpu_to_le32(lba);
996                 readcmd->pad   = 0;
997                 readcmd->flags = 0; 
998
999                 aac_build_sg64(scsicmd, &readcmd->sg);
1000                 fibsize = sizeof(struct aac_read64) + 
1001                         ((le32_to_cpu(readcmd->sg.count) - 1) * 
1002                          sizeof (struct sgentry64));
1003                 BUG_ON (fibsize > (dev->max_fib_size - 
1004                                         sizeof(struct aac_fibhdr)));
1005                 /*
1006                  *      Now send the Fib to the adapter
1007                  */
1008                 status = fib_send(ContainerCommand64, 
1009                           cmd_fibcontext, 
1010                           fibsize, 
1011                           FsaNormal, 
1012                           0, 1, 
1013                           (fib_callback) io_callback, 
1014                           (void *) scsicmd);
1015         } else {
1016                 struct aac_read *readcmd;
1017                 readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
1018                 readcmd->command = cpu_to_le32(VM_CtBlockRead);
1019                 readcmd->cid = cpu_to_le32(cid);
1020                 readcmd->block = cpu_to_le32(lba);
1021                 readcmd->count = cpu_to_le32(count * 512);
1022
1023                 aac_build_sg(scsicmd, &readcmd->sg);
1024                 fibsize = sizeof(struct aac_read) + 
1025                         ((le32_to_cpu(readcmd->sg.count) - 1) * 
1026                          sizeof (struct sgentry));
1027                 BUG_ON (fibsize > (dev->max_fib_size -
1028                                         sizeof(struct aac_fibhdr)));
1029                 /*
1030                  *      Now send the Fib to the adapter
1031                  */
1032                 status = fib_send(ContainerCommand, 
1033                           cmd_fibcontext, 
1034                           fibsize, 
1035                           FsaNormal, 
1036                           0, 1, 
1037                           (fib_callback) io_callback, 
1038                           (void *) scsicmd);
1039         }
1040
1041         
1042
1043         /*
1044          *      Check that the command queued to the controller
1045          */
1046         if (status == -EINPROGRESS) 
1047                 return 0;
1048                 
1049         printk(KERN_WARNING "aac_read: fib_send failed with status: %d.\n", status);
1050         /*
1051          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1052          */
1053         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1054         aac_io_done(scsicmd);
1055         fib_complete(cmd_fibcontext);
1056         fib_free(cmd_fibcontext);
1057         return 0;
1058 }
1059
1060 static int aac_write(struct scsi_cmnd * scsicmd, int cid)
1061 {
1062         u32 lba;
1063         u32 count;
1064         int status;
1065         u16 fibsize;
1066         struct aac_dev *dev;
1067         struct fib * cmd_fibcontext;
1068
1069         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1070         /*
1071          *      Get block address and transfer length
1072          */
1073         if (scsicmd->cmnd[0] == WRITE_6)        /* 6 byte command */
1074         {
1075                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1076                 count = scsicmd->cmnd[4];
1077                 if (count == 0)
1078                         count = 256;
1079         } else {
1080                 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
1081                 lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1082                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1083         }
1084         dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %u, t = %ld.\n",
1085           smp_processor_id(), (unsigned long long)lba, jiffies));
1086         /*
1087          *      Allocate and initialize a Fib then setup a BlockWrite command
1088          */
1089         if (!(cmd_fibcontext = fib_alloc(dev))) {
1090                 scsicmd->result = DID_ERROR << 16;
1091                 aac_io_done(scsicmd);
1092                 return 0;
1093         }
1094         fib_init(cmd_fibcontext);
1095
1096         if (dev->raw_io_interface) {
1097                 struct aac_raw_io *writecmd;
1098                 writecmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
1099                 writecmd->block[0] = cpu_to_le32(lba);
1100                 writecmd->block[1] = 0;
1101                 writecmd->count = cpu_to_le32(count<<9);
1102                 writecmd->cid = cpu_to_le16(cid);
1103                 writecmd->flags = 0; 
1104                 writecmd->bpTotal = 0;
1105                 writecmd->bpComplete = 0;
1106                 
1107                 aac_build_sgraw(scsicmd, &writecmd->sg);
1108                 fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1109                 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))
1110                         BUG();
1111                 /*
1112                  *      Now send the Fib to the adapter
1113                  */
1114                 status = fib_send(ContainerRawIo,
1115                           cmd_fibcontext, 
1116                           fibsize, 
1117                           FsaNormal, 
1118                           0, 1, 
1119                           (fib_callback) io_callback, 
1120                           (void *) scsicmd);
1121         } else if (dev->dac_support == 1) {
1122                 struct aac_write64 *writecmd;
1123                 writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
1124                 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1125                 writecmd->cid = cpu_to_le16(cid);
1126                 writecmd->sector_count = cpu_to_le16(count); 
1127                 writecmd->block = cpu_to_le32(lba);
1128                 writecmd->pad   = 0;
1129                 writecmd->flags = 0;
1130
1131                 aac_build_sg64(scsicmd, &writecmd->sg);
1132                 fibsize = sizeof(struct aac_write64) + 
1133                         ((le32_to_cpu(writecmd->sg.count) - 1) * 
1134                          sizeof (struct sgentry64));
1135                 BUG_ON (fibsize > (dev->max_fib_size -
1136                                         sizeof(struct aac_fibhdr)));
1137                 /*
1138                  *      Now send the Fib to the adapter
1139                  */
1140                 status = fib_send(ContainerCommand64, 
1141                           cmd_fibcontext, 
1142                           fibsize, 
1143                           FsaNormal, 
1144                           0, 1, 
1145                           (fib_callback) io_callback, 
1146                           (void *) scsicmd);
1147         } else {
1148                 struct aac_write *writecmd;
1149                 writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
1150                 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1151                 writecmd->cid = cpu_to_le32(cid);
1152                 writecmd->block = cpu_to_le32(lba);
1153                 writecmd->count = cpu_to_le32(count * 512);
1154                 writecmd->sg.count = cpu_to_le32(1);
1155                 /* ->stable is not used - it did mean which type of write */
1156
1157                 aac_build_sg(scsicmd, &writecmd->sg);
1158                 fibsize = sizeof(struct aac_write) + 
1159                         ((le32_to_cpu(writecmd->sg.count) - 1) * 
1160                          sizeof (struct sgentry));
1161                 BUG_ON (fibsize > (dev->max_fib_size -
1162                                         sizeof(struct aac_fibhdr)));
1163                 /*
1164                  *      Now send the Fib to the adapter
1165                  */
1166                 status = fib_send(ContainerCommand, 
1167                           cmd_fibcontext, 
1168                           fibsize, 
1169                           FsaNormal, 
1170                           0, 1, 
1171                           (fib_callback) io_callback, 
1172                           (void *) scsicmd);
1173         }
1174
1175         /*
1176          *      Check that the command queued to the controller
1177          */
1178         if (status == -EINPROGRESS)
1179         {
1180                 return 0;
1181         }
1182
1183         printk(KERN_WARNING "aac_write: fib_send failed with status: %d\n", status);
1184         /*
1185          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1186          */
1187         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1188         aac_io_done(scsicmd);
1189
1190         fib_complete(cmd_fibcontext);
1191         fib_free(cmd_fibcontext);
1192         return 0;
1193 }
1194
1195 static void synchronize_callback(void *context, struct fib *fibptr)
1196 {
1197         struct aac_synchronize_reply *synchronizereply;
1198         struct scsi_cmnd *cmd;
1199
1200         cmd = context;
1201
1202         dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n", 
1203                                 smp_processor_id(), jiffies));
1204         BUG_ON(fibptr == NULL);
1205
1206
1207         synchronizereply = fib_data(fibptr);
1208         if (le32_to_cpu(synchronizereply->status) == CT_OK)
1209                 cmd->result = DID_OK << 16 | 
1210                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1211         else {
1212                 struct scsi_device *sdev = cmd->device;
1213                 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
1214                 u32 cid = ID_LUN_TO_CONTAINER(sdev->id, sdev->lun);
1215                 printk(KERN_WARNING 
1216                      "synchronize_callback: synchronize failed, status = %d\n",
1217                      le32_to_cpu(synchronizereply->status));
1218                 cmd->result = DID_OK << 16 | 
1219                         COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1220                 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1221                                     HARDWARE_ERROR,
1222                                     SENCODE_INTERNAL_TARGET_FAILURE,
1223                                     ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1224                                     0, 0);
1225                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1226                   min(sizeof(dev->fsa_dev[cid].sense_data), 
1227                           sizeof(cmd->sense_buffer)));
1228         }
1229
1230         fib_complete(fibptr);
1231         fib_free(fibptr);
1232         aac_io_done(cmd);
1233 }
1234
1235 static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
1236 {
1237         int status;
1238         struct fib *cmd_fibcontext;
1239         struct aac_synchronize *synchronizecmd;
1240         struct scsi_cmnd *cmd;
1241         struct scsi_device *sdev = scsicmd->device;
1242         int active = 0;
1243         unsigned long flags;
1244
1245         /*
1246          * Wait for all commands to complete to this specific
1247          * target (block).
1248          */
1249         spin_lock_irqsave(&sdev->list_lock, flags);
1250         list_for_each_entry(cmd, &sdev->cmd_list, list)
1251                 if (cmd != scsicmd && cmd->serial_number != 0) {
1252                         ++active;
1253                         break;
1254                 }
1255
1256         spin_unlock_irqrestore(&sdev->list_lock, flags);
1257
1258         /*
1259          *      Yield the processor (requeue for later)
1260          */
1261         if (active)
1262                 return SCSI_MLQUEUE_DEVICE_BUSY;
1263
1264         /*
1265          *      Allocate and initialize a Fib
1266          */
1267         if (!(cmd_fibcontext = 
1268             fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) 
1269                 return SCSI_MLQUEUE_HOST_BUSY;
1270
1271         fib_init(cmd_fibcontext);
1272
1273         synchronizecmd = fib_data(cmd_fibcontext);
1274         synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1275         synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1276         synchronizecmd->cid = cpu_to_le32(cid);
1277         synchronizecmd->count = 
1278              cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1279
1280         /*
1281          *      Now send the Fib to the adapter
1282          */
1283         status = fib_send(ContainerCommand,
1284                   cmd_fibcontext,
1285                   sizeof(struct aac_synchronize),
1286                   FsaNormal,
1287                   0, 1,
1288                   (fib_callback)synchronize_callback,
1289                   (void *)scsicmd);
1290
1291         /*
1292          *      Check that the command queued to the controller
1293          */
1294         if (status == -EINPROGRESS)
1295                 return 0;
1296
1297         printk(KERN_WARNING 
1298                 "aac_synchronize: fib_send failed with status: %d.\n", status);
1299         fib_complete(cmd_fibcontext);
1300         fib_free(cmd_fibcontext);
1301         return SCSI_MLQUEUE_HOST_BUSY;
1302 }
1303
1304 /**
1305  *      aac_scsi_cmd()          -       Process SCSI command
1306  *      @scsicmd:               SCSI command block
1307  *
1308  *      Emulate a SCSI command and queue the required request for the
1309  *      aacraid firmware.
1310  */
1311  
1312 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1313 {
1314         u32 cid = 0;
1315         struct Scsi_Host *host = scsicmd->device->host;
1316         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1317         struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
1318         int ret;
1319         
1320         /*
1321          *      If the bus, id or lun is out of range, return fail
1322          *      Test does not apply to ID 16, the pseudo id for the controller
1323          *      itself.
1324          */
1325         if (scsicmd->device->id != host->this_id) {
1326                 if ((scsicmd->device->channel == 0) ){
1327                         if( (scsicmd->device->id >= dev->maximum_num_containers) || (scsicmd->device->lun != 0)){ 
1328                                 scsicmd->result = DID_NO_CONNECT << 16;
1329                                 scsicmd->scsi_done(scsicmd);
1330                                 return 0;
1331                         }
1332                         cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
1333
1334                         /*
1335                          *      If the target container doesn't exist, it may have
1336                          *      been newly created
1337                          */
1338                         if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1339                                 switch (scsicmd->cmnd[0]) {
1340                                 case INQUIRY:
1341                                 case READ_CAPACITY:
1342                                 case TEST_UNIT_READY:
1343                                         spin_unlock_irq(host->host_lock);
1344                                         probe_container(dev, cid);
1345                                         spin_lock_irq(host->host_lock);
1346                                         if (fsa_dev_ptr[cid].valid == 0) {
1347                                                 scsicmd->result = DID_NO_CONNECT << 16;
1348                                                 scsicmd->scsi_done(scsicmd);
1349                                                 return 0;
1350                                         }
1351                                 default:
1352                                         break;
1353                                 }
1354                         }
1355                         /*
1356                          *      If the target container still doesn't exist, 
1357                          *      return failure
1358                          */
1359                         if (fsa_dev_ptr[cid].valid == 0) {
1360                                 scsicmd->result = DID_BAD_TARGET << 16;
1361                                 scsicmd->scsi_done(scsicmd);
1362                                 return 0;
1363                         }
1364                 } else {  /* check for physical non-dasd devices */
1365                         if(dev->nondasd_support == 1){
1366                                 return aac_send_srb_fib(scsicmd);
1367                         } else {
1368                                 scsicmd->result = DID_NO_CONNECT << 16;
1369                                 scsicmd->scsi_done(scsicmd);
1370                                 return 0;
1371                         }
1372                 }
1373         }
1374         /*
1375          * else Command for the controller itself
1376          */
1377         else if ((scsicmd->cmnd[0] != INQUIRY) &&       /* only INQUIRY & TUR cmnd supported for controller */
1378                 (scsicmd->cmnd[0] != TEST_UNIT_READY)) 
1379         {
1380                 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1381                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1382                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1383                             ILLEGAL_REQUEST,
1384                             SENCODE_INVALID_COMMAND,
1385                             ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1386                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1387                   (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1388                     ? sizeof(scsicmd->sense_buffer)
1389                     : sizeof(dev->fsa_dev[cid].sense_data));
1390                 scsicmd->scsi_done(scsicmd);
1391                 return 0;
1392         }
1393
1394
1395         /* Handle commands here that don't really require going out to the adapter */
1396         switch (scsicmd->cmnd[0]) {
1397         case INQUIRY:
1398         {
1399                 struct inquiry_data inq_data;
1400
1401                 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scsicmd->device->id));
1402                 memset(&inq_data, 0, sizeof (struct inquiry_data));
1403
1404                 inq_data.inqd_ver = 2;  /* claim compliance to SCSI-2 */
1405                 inq_data.inqd_dtq = 0x80;       /* set RMB bit to one indicating that the medium is removable */
1406                 inq_data.inqd_rdf = 2;  /* A response data format value of two indicates that the data shall be in the format specified in SCSI-2 */
1407                 inq_data.inqd_len = 31;
1408                 /*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
1409                 inq_data.inqd_pad2= 0x32 ;       /*WBus16|Sync|CmdQue */
1410                 /*
1411                  *      Set the Vendor, Product, and Revision Level
1412                  *      see: <vendor>.c i.e. aac.c
1413                  */
1414                 if (scsicmd->device->id == host->this_id) {
1415                         setinqstr(dev, (void *) (inq_data.inqd_vid), (sizeof(container_types)/sizeof(char *)));
1416                         inq_data.inqd_pdt = INQD_PDT_PROC;      /* Processor device */
1417                         aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
1418                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1419                         scsicmd->scsi_done(scsicmd);
1420                         return 0;
1421                 }
1422                 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
1423                 inq_data.inqd_pdt = INQD_PDT_DA;        /* Direct/random access device */
1424                 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
1425                 return aac_get_container_name(scsicmd, cid);
1426         }
1427         case READ_CAPACITY:
1428         {
1429                 u32 capacity;
1430                 char cp[8];
1431
1432                 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
1433                 if (fsa_dev_ptr[cid].size <= 0x100000000LL)
1434                         capacity = fsa_dev_ptr[cid].size - 1;
1435                 else
1436                         capacity = (u32)-1;
1437
1438                 cp[0] = (capacity >> 24) & 0xff;
1439                 cp[1] = (capacity >> 16) & 0xff;
1440                 cp[2] = (capacity >> 8) & 0xff;
1441                 cp[3] = (capacity >> 0) & 0xff;
1442                 cp[4] = 0;
1443                 cp[5] = 0;
1444                 cp[6] = 2;
1445                 cp[7] = 0;
1446                 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
1447
1448                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1449                 scsicmd->scsi_done(scsicmd);
1450
1451                 return 0;
1452         }
1453
1454         case MODE_SENSE:
1455         {
1456                 char mode_buf[4];
1457
1458                 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
1459                 mode_buf[0] = 3;        /* Mode data length */
1460                 mode_buf[1] = 0;        /* Medium type - default */
1461                 mode_buf[2] = 0;        /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1462                 mode_buf[3] = 0;        /* Block descriptor length */
1463
1464                 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
1465                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1466                 scsicmd->scsi_done(scsicmd);
1467
1468                 return 0;
1469         }
1470         case MODE_SENSE_10:
1471         {
1472                 char mode_buf[8];
1473
1474                 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
1475                 mode_buf[0] = 0;        /* Mode data length (MSB) */
1476                 mode_buf[1] = 6;        /* Mode data length (LSB) */
1477                 mode_buf[2] = 0;        /* Medium type - default */
1478                 mode_buf[3] = 0;        /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1479                 mode_buf[4] = 0;        /* reserved */
1480                 mode_buf[5] = 0;        /* reserved */
1481                 mode_buf[6] = 0;        /* Block descriptor length (MSB) */
1482                 mode_buf[7] = 0;        /* Block descriptor length (LSB) */
1483                 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
1484
1485                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1486                 scsicmd->scsi_done(scsicmd);
1487
1488                 return 0;
1489         }
1490         case REQUEST_SENSE:
1491                 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1492                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1493                 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1494                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1495                 scsicmd->scsi_done(scsicmd);
1496                 return 0;
1497
1498         case ALLOW_MEDIUM_REMOVAL:
1499                 dprintk((KERN_DEBUG "LOCK command.\n"));
1500                 if (scsicmd->cmnd[4])
1501                         fsa_dev_ptr[cid].locked = 1;
1502                 else
1503                         fsa_dev_ptr[cid].locked = 0;
1504
1505                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1506                 scsicmd->scsi_done(scsicmd);
1507                 return 0;
1508         /*
1509          *      These commands are all No-Ops
1510          */
1511         case TEST_UNIT_READY:
1512         case RESERVE:
1513         case RELEASE:
1514         case REZERO_UNIT:
1515         case REASSIGN_BLOCKS:
1516         case SEEK_10:
1517         case START_STOP:
1518                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1519                 scsicmd->scsi_done(scsicmd);
1520                 return 0;
1521         }
1522
1523         switch (scsicmd->cmnd[0]) 
1524         {
1525                 case READ_6:
1526                 case READ_10:
1527                         /*
1528                          *      Hack to keep track of ordinal number of the device that
1529                          *      corresponds to a container. Needed to convert
1530                          *      containers to /dev/sd device names
1531                          */
1532                          
1533                         spin_unlock_irq(host->host_lock);
1534                         if  (scsicmd->request->rq_disk)
1535                                 memcpy(fsa_dev_ptr[cid].devname,
1536                                         scsicmd->request->rq_disk->disk_name,
1537                                         8);
1538
1539                         ret = aac_read(scsicmd, cid);
1540                         spin_lock_irq(host->host_lock);
1541                         return ret;
1542
1543                 case WRITE_6:
1544                 case WRITE_10:
1545                         spin_unlock_irq(host->host_lock);
1546                         ret = aac_write(scsicmd, cid);
1547                         spin_lock_irq(host->host_lock);
1548                         return ret;
1549
1550                 case SYNCHRONIZE_CACHE:
1551                         /* Issue FIB to tell Firmware to flush it's cache */
1552                         return aac_synchronize(scsicmd, cid);
1553                         
1554                 default:
1555                         /*
1556                          *      Unhandled commands
1557                          */
1558                         dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
1559                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1560                         set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1561                                 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1562                                 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1563                         memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1564                           (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1565                             ? sizeof(scsicmd->sense_buffer)
1566                             : sizeof(dev->fsa_dev[cid].sense_data));
1567                         scsicmd->scsi_done(scsicmd);
1568                         return 0;
1569         }
1570 }
1571
1572 static int query_disk(struct aac_dev *dev, void __user *arg)
1573 {
1574         struct aac_query_disk qd;
1575         struct fsa_dev_info *fsa_dev_ptr;
1576
1577         fsa_dev_ptr = dev->fsa_dev;
1578         if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1579                 return -EFAULT;
1580         if (qd.cnum == -1)
1581                 qd.cnum = ID_LUN_TO_CONTAINER(qd.id, qd.lun);
1582         else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1)) 
1583         {
1584                 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
1585                         return -EINVAL;
1586                 qd.instance = dev->scsi_host_ptr->host_no;
1587                 qd.bus = 0;
1588                 qd.id = CONTAINER_TO_ID(qd.cnum);
1589                 qd.lun = CONTAINER_TO_LUN(qd.cnum);
1590         }
1591         else return -EINVAL;
1592
1593         qd.valid = fsa_dev_ptr[qd.cnum].valid;
1594         qd.locked = fsa_dev_ptr[qd.cnum].locked;
1595         qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
1596
1597         if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
1598                 qd.unmapped = 1;
1599         else
1600                 qd.unmapped = 0;
1601
1602         strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
1603           min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
1604
1605         if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1606                 return -EFAULT;
1607         return 0;
1608 }
1609
1610 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
1611 {
1612         struct aac_delete_disk dd;
1613         struct fsa_dev_info *fsa_dev_ptr;
1614
1615         fsa_dev_ptr = dev->fsa_dev;
1616
1617         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1618                 return -EFAULT;
1619
1620         if (dd.cnum >= dev->maximum_num_containers)
1621                 return -EINVAL;
1622         /*
1623          *      Mark this container as being deleted.
1624          */
1625         fsa_dev_ptr[dd.cnum].deleted = 1;
1626         /*
1627          *      Mark the container as no longer valid
1628          */
1629         fsa_dev_ptr[dd.cnum].valid = 0;
1630         return 0;
1631 }
1632
1633 static int delete_disk(struct aac_dev *dev, void __user *arg)
1634 {
1635         struct aac_delete_disk dd;
1636         struct fsa_dev_info *fsa_dev_ptr;
1637
1638         fsa_dev_ptr = dev->fsa_dev;
1639
1640         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1641                 return -EFAULT;
1642
1643         if (dd.cnum >= dev->maximum_num_containers)
1644                 return -EINVAL;
1645         /*
1646          *      If the container is locked, it can not be deleted by the API.
1647          */
1648         if (fsa_dev_ptr[dd.cnum].locked)
1649                 return -EBUSY;
1650         else {
1651                 /*
1652                  *      Mark the container as no longer being valid.
1653                  */
1654                 fsa_dev_ptr[dd.cnum].valid = 0;
1655                 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
1656                 return 0;
1657         }
1658 }
1659
1660 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
1661 {
1662         switch (cmd) {
1663         case FSACTL_QUERY_DISK:
1664                 return query_disk(dev, arg);
1665         case FSACTL_DELETE_DISK:
1666                 return delete_disk(dev, arg);
1667         case FSACTL_FORCE_DELETE_DISK:
1668                 return force_delete_disk(dev, arg);
1669         case FSACTL_GET_CONTAINERS:
1670                 return aac_get_containers(dev);
1671         default:
1672                 return -ENOTTY;
1673         }
1674 }
1675
1676 /**
1677  *
1678  * aac_srb_callback
1679  * @context: the context set in the fib - here it is scsi cmd
1680  * @fibptr: pointer to the fib
1681  *
1682  * Handles the completion of a scsi command to a non dasd device
1683  *
1684  */
1685
1686 static void aac_srb_callback(void *context, struct fib * fibptr)
1687 {
1688         struct aac_dev *dev;
1689         struct aac_srb_reply *srbreply;
1690         struct scsi_cmnd *scsicmd;
1691
1692         scsicmd = (struct scsi_cmnd *) context;
1693         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1694
1695         if (fibptr == NULL)
1696                 BUG();
1697
1698         srbreply = (struct aac_srb_reply *) fib_data(fibptr);
1699
1700         scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
1701         /*
1702          *      Calculate resid for sg 
1703          */
1704          
1705         scsicmd->resid = scsicmd->request_bufflen - 
1706                 le32_to_cpu(srbreply->data_xfer_length);
1707
1708         if(scsicmd->use_sg)
1709                 pci_unmap_sg(dev->pdev, 
1710                         (struct scatterlist *)scsicmd->buffer,
1711                         scsicmd->use_sg,
1712                         scsicmd->sc_data_direction);
1713         else if(scsicmd->request_bufflen)
1714                 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
1715                         scsicmd->sc_data_direction);
1716
1717         /*
1718          * First check the fib status
1719          */
1720
1721         if (le32_to_cpu(srbreply->status) != ST_OK){
1722                 int len;
1723                 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
1724                 len = (le32_to_cpu(srbreply->sense_data_size) > 
1725                                 sizeof(scsicmd->sense_buffer)) ?
1726                                 sizeof(scsicmd->sense_buffer) : 
1727                                 le32_to_cpu(srbreply->sense_data_size);
1728                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1729                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1730         }
1731
1732         /*
1733          * Next check the srb status
1734          */
1735         switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
1736         case SRB_STATUS_ERROR_RECOVERY:
1737         case SRB_STATUS_PENDING:
1738         case SRB_STATUS_SUCCESS:
1739                 if(scsicmd->cmnd[0] == INQUIRY ){
1740                         u8 b;
1741                         u8 b1;
1742                         /* We can't expose disk devices because we can't tell whether they
1743                          * are the raw container drives or stand alone drives.  If they have
1744                          * the removable bit set then we should expose them though.
1745                          */
1746                         b = (*(u8*)scsicmd->buffer)&0x1f;
1747                         b1 = ((u8*)scsicmd->buffer)[1];
1748                         if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER 
1749                                         || (b==TYPE_DISK && (b1&0x80)) ){
1750                                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1751                         /*
1752                          * We will allow disk devices if in RAID/SCSI mode and
1753                          * the channel is 2
1754                          */
1755                         } else if ((dev->raid_scsi_mode) &&
1756                                         (scsicmd->device->channel == 2)) {
1757                                 scsicmd->result = DID_OK << 16 | 
1758                                                 COMMAND_COMPLETE << 8;
1759                         } else {
1760                                 scsicmd->result = DID_NO_CONNECT << 16 | 
1761                                                 COMMAND_COMPLETE << 8;
1762                         }
1763                 } else {
1764                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1765                 }
1766                 break;
1767         case SRB_STATUS_DATA_OVERRUN:
1768                 switch(scsicmd->cmnd[0]){
1769                 case  READ_6:
1770                 case  WRITE_6:
1771                 case  READ_10:
1772                 case  WRITE_10:
1773                 case  READ_12:
1774                 case  WRITE_12:
1775                         if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
1776                                 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
1777                         } else {
1778                                 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
1779                         }
1780                         scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1781                         break;
1782                 case INQUIRY: {
1783                         u8 b;
1784                         u8 b1;
1785                         /* We can't expose disk devices because we can't tell whether they
1786                         * are the raw container drives or stand alone drives
1787                         */
1788                         b = (*(u8*)scsicmd->buffer)&0x0f;
1789                         b1 = ((u8*)scsicmd->buffer)[1];
1790                         if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
1791                                         || (b==TYPE_DISK && (b1&0x80)) ){
1792                                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1793                         /*
1794                          * We will allow disk devices if in RAID/SCSI mode and
1795                          * the channel is 2
1796                          */
1797                         } else if ((dev->raid_scsi_mode) &&
1798                                         (scsicmd->device->channel == 2)) {
1799                                 scsicmd->result = DID_OK << 16 | 
1800                                                 COMMAND_COMPLETE << 8;
1801                         } else {
1802                                 scsicmd->result = DID_NO_CONNECT << 16 | 
1803                                                 COMMAND_COMPLETE << 8;
1804                         }
1805                         break;
1806                 }
1807                 default:
1808                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1809                         break;
1810                 }
1811                 break;
1812         case SRB_STATUS_ABORTED:
1813                 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
1814                 break;
1815         case SRB_STATUS_ABORT_FAILED:
1816                 // Not sure about this one - but assuming the hba was trying to abort for some reason
1817                 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
1818                 break;
1819         case SRB_STATUS_PARITY_ERROR:
1820                 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
1821                 break;
1822         case SRB_STATUS_NO_DEVICE:
1823         case SRB_STATUS_INVALID_PATH_ID:
1824         case SRB_STATUS_INVALID_TARGET_ID:
1825         case SRB_STATUS_INVALID_LUN:
1826         case SRB_STATUS_SELECTION_TIMEOUT:
1827                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1828                 break;
1829
1830         case SRB_STATUS_COMMAND_TIMEOUT:
1831         case SRB_STATUS_TIMEOUT:
1832                 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
1833                 break;
1834
1835         case SRB_STATUS_BUSY:
1836                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1837                 break;
1838
1839         case SRB_STATUS_BUS_RESET:
1840                 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
1841                 break;
1842
1843         case SRB_STATUS_MESSAGE_REJECTED:
1844                 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
1845                 break;
1846         case SRB_STATUS_REQUEST_FLUSHED:
1847         case SRB_STATUS_ERROR:
1848         case SRB_STATUS_INVALID_REQUEST:
1849         case SRB_STATUS_REQUEST_SENSE_FAILED:
1850         case SRB_STATUS_NO_HBA:
1851         case SRB_STATUS_UNEXPECTED_BUS_FREE:
1852         case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
1853         case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
1854         case SRB_STATUS_DELAYED_RETRY:
1855         case SRB_STATUS_BAD_FUNCTION:
1856         case SRB_STATUS_NOT_STARTED:
1857         case SRB_STATUS_NOT_IN_USE:
1858         case SRB_STATUS_FORCE_ABORT:
1859         case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
1860         default:
1861 #ifdef AAC_DETAILED_STATUS_INFO
1862                 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
1863                         le32_to_cpu(srbreply->srb_status) & 0x3F,
1864                         aac_get_status_string(
1865                                 le32_to_cpu(srbreply->srb_status) & 0x3F), 
1866                         scsicmd->cmnd[0], 
1867                         le32_to_cpu(srbreply->scsi_status));
1868 #endif
1869                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1870                 break;
1871         }
1872         if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){  // Check Condition
1873                 int len;
1874                 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
1875                 len = (le32_to_cpu(srbreply->sense_data_size) > 
1876                                 sizeof(scsicmd->sense_buffer)) ?
1877                                 sizeof(scsicmd->sense_buffer) :
1878                                 le32_to_cpu(srbreply->sense_data_size);
1879 #ifdef AAC_DETAILED_STATUS_INFO
1880                 dprintk((KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n", 
1881                                         le32_to_cpu(srbreply->status), len));
1882 #endif
1883                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1884                 
1885         }
1886         /*
1887          * OR in the scsi status (already shifted up a bit)
1888          */
1889         scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
1890
1891         fib_complete(fibptr);
1892         fib_free(fibptr);
1893         aac_io_done(scsicmd);
1894 }
1895
1896 /**
1897  *
1898  * aac_send_scb_fib
1899  * @scsicmd: the scsi command block
1900  *
1901  * This routine will form a FIB and fill in the aac_srb from the 
1902  * scsicmd passed in.
1903  */
1904
1905 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
1906 {
1907         struct fib* cmd_fibcontext;
1908         struct aac_dev* dev;
1909         int status;
1910         struct aac_srb *srbcmd;
1911         u16 fibsize;
1912         u32 flag;
1913         u32 timeout;
1914
1915         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1916         if (scsicmd->device->id >= dev->maximum_num_physicals || 
1917                         scsicmd->device->lun > 7) {
1918                 scsicmd->result = DID_NO_CONNECT << 16;
1919                 scsicmd->scsi_done(scsicmd);
1920                 return 0;
1921         }
1922
1923         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1924         switch(scsicmd->sc_data_direction){
1925         case DMA_TO_DEVICE:
1926                 flag = SRB_DataOut;
1927                 break;
1928         case DMA_BIDIRECTIONAL:
1929                 flag = SRB_DataIn | SRB_DataOut;
1930                 break;
1931         case DMA_FROM_DEVICE:
1932                 flag = SRB_DataIn;
1933                 break;
1934         case DMA_NONE:
1935         default:        /* shuts up some versions of gcc */
1936                 flag = SRB_NoDataXfer;
1937                 break;
1938         }
1939
1940
1941         /*
1942          *      Allocate and initialize a Fib then setup a BlockWrite command
1943          */
1944         if (!(cmd_fibcontext = fib_alloc(dev))) {
1945                 return -1;
1946         }
1947         fib_init(cmd_fibcontext);
1948
1949         srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
1950         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1951         srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scsicmd->device->channel));
1952         srbcmd->id   = cpu_to_le32(scsicmd->device->id);
1953         srbcmd->lun      = cpu_to_le32(scsicmd->device->lun);
1954         srbcmd->flags    = cpu_to_le32(flag);
1955         timeout = scsicmd->timeout_per_command/HZ;
1956         if(timeout == 0){
1957                 timeout = 1;
1958         }
1959         srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1960         srbcmd->retry_limit = 0; /* Obsolete parameter */
1961         srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
1962         
1963         if( dev->dac_support == 1 ) {
1964                 aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
1965                 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
1966
1967                 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1968                 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
1969                 /*
1970                  *      Build Scatter/Gather list
1971                  */
1972                 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1973                         ((le32_to_cpu(srbcmd->sg.count) & 0xff) * 
1974                          sizeof (struct sgentry64));
1975                 BUG_ON (fibsize > (dev->max_fib_size -
1976                                         sizeof(struct aac_fibhdr)));
1977
1978                 /*
1979                  *      Now send the Fib to the adapter
1980                  */
1981                 status = fib_send(ScsiPortCommand64, cmd_fibcontext, 
1982                                 fibsize, FsaNormal, 0, 1,
1983                                   (fib_callback) aac_srb_callback, 
1984                                   (void *) scsicmd);
1985         } else {
1986                 aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
1987                 srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
1988
1989                 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1990                 memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
1991                 /*
1992                  *      Build Scatter/Gather list
1993                  */
1994                 fibsize = sizeof (struct aac_srb) + 
1995                         (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) * 
1996                          sizeof (struct sgentry));
1997                 BUG_ON (fibsize > (dev->max_fib_size -
1998                                         sizeof(struct aac_fibhdr)));
1999
2000                 /*
2001                  *      Now send the Fib to the adapter
2002                  */
2003                 status = fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
2004                                   (fib_callback) aac_srb_callback, (void *) scsicmd);
2005         }
2006         /*
2007          *      Check that the command queued to the controller
2008          */
2009         if (status == -EINPROGRESS){
2010                 return 0;
2011         }
2012
2013         printk(KERN_WARNING "aac_srb: fib_send failed with status: %d\n", status);
2014         fib_complete(cmd_fibcontext);
2015         fib_free(cmd_fibcontext);
2016
2017         return -1;
2018 }
2019
2020 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2021 {
2022         struct aac_dev *dev;
2023         unsigned long byte_count = 0;
2024
2025         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2026         // Get rid of old data
2027         psg->count = 0;
2028         psg->sg[0].addr = 0;
2029         psg->sg[0].count = 0;  
2030         if (scsicmd->use_sg) {
2031                 struct scatterlist *sg;
2032                 int i;
2033                 int sg_count;
2034                 sg = (struct scatterlist *) scsicmd->request_buffer;
2035
2036                 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2037                         scsicmd->sc_data_direction);
2038                 psg->count = cpu_to_le32(sg_count);
2039
2040                 byte_count = 0;
2041
2042                 for (i = 0; i < sg_count; i++) {
2043                         psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2044                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2045                         byte_count += sg_dma_len(sg);
2046                         sg++;
2047                 }
2048                 /* hba wants the size to be exact */
2049                 if(byte_count > scsicmd->request_bufflen){
2050                         u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
2051                                 (byte_count - scsicmd->request_bufflen);
2052                         psg->sg[i-1].count = cpu_to_le32(temp);
2053                         byte_count = scsicmd->request_bufflen;
2054                 }
2055                 /* Check for command underflow */
2056                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2057                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2058                                         byte_count, scsicmd->underflow);
2059                 }
2060         }
2061         else if(scsicmd->request_bufflen) {
2062                 dma_addr_t addr; 
2063                 addr = pci_map_single(dev->pdev,
2064                                 scsicmd->request_buffer,
2065                                 scsicmd->request_bufflen,
2066                                 scsicmd->sc_data_direction);
2067                 psg->count = cpu_to_le32(1);
2068                 psg->sg[0].addr = cpu_to_le32(addr);
2069                 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);  
2070                 scsicmd->SCp.dma_handle = addr;
2071                 byte_count = scsicmd->request_bufflen;
2072         }
2073         return byte_count;
2074 }
2075
2076
2077 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2078 {
2079         struct aac_dev *dev;
2080         unsigned long byte_count = 0;
2081         u64 addr;
2082
2083         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2084         // Get rid of old data
2085         psg->count = 0;
2086         psg->sg[0].addr[0] = 0;
2087         psg->sg[0].addr[1] = 0;
2088         psg->sg[0].count = 0;
2089         if (scsicmd->use_sg) {
2090                 struct scatterlist *sg;
2091                 int i;
2092                 int sg_count;
2093                 sg = (struct scatterlist *) scsicmd->request_buffer;
2094
2095                 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2096                         scsicmd->sc_data_direction);
2097                 psg->count = cpu_to_le32(sg_count);
2098
2099                 byte_count = 0;
2100
2101                 for (i = 0; i < sg_count; i++) {
2102                         addr = sg_dma_address(sg);
2103                         psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2104                         psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
2105                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2106                         byte_count += sg_dma_len(sg);
2107                         sg++;
2108                 }
2109                 /* hba wants the size to be exact */
2110                 if(byte_count > scsicmd->request_bufflen){
2111                         u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
2112                                 (byte_count - scsicmd->request_bufflen);
2113                         psg->sg[i-1].count = cpu_to_le32(temp);
2114                         byte_count = scsicmd->request_bufflen;
2115                 }
2116                 /* Check for command underflow */
2117                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2118                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2119                                         byte_count, scsicmd->underflow);
2120                 }
2121         }
2122         else if(scsicmd->request_bufflen) {
2123                 u64 addr; 
2124                 addr = pci_map_single(dev->pdev,
2125                                 scsicmd->request_buffer,
2126                                 scsicmd->request_bufflen,
2127                                 scsicmd->sc_data_direction);
2128                 psg->count = cpu_to_le32(1);
2129                 psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2130                 psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
2131                 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);  
2132                 scsicmd->SCp.dma_handle = addr;
2133                 byte_count = scsicmd->request_bufflen;
2134         }
2135         return byte_count;
2136 }
2137
2138 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2139 {
2140         struct Scsi_Host *host = scsicmd->device->host;
2141         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2142         unsigned long byte_count = 0;
2143
2144         // Get rid of old data
2145         psg->count = 0;
2146         psg->sg[0].next = 0;
2147         psg->sg[0].prev = 0;
2148         psg->sg[0].addr[0] = 0;
2149         psg->sg[0].addr[1] = 0;
2150         psg->sg[0].count = 0;
2151         psg->sg[0].flags = 0;
2152         if (scsicmd->use_sg) {
2153                 struct scatterlist *sg;
2154                 int i;
2155                 int sg_count;
2156                 sg = (struct scatterlist *) scsicmd->request_buffer;
2157
2158                 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2159                         scsicmd->sc_data_direction);
2160
2161                 for (i = 0; i < sg_count; i++) {
2162                         int count = sg_dma_len(sg);
2163                         u64 addr = sg_dma_address(sg);
2164                         psg->sg[i].next = 0;
2165                         psg->sg[i].prev = 0;
2166                         psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2167                         psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2168                         psg->sg[i].count = cpu_to_le32(count);
2169                         psg->sg[i].flags = 0;
2170                         byte_count += count;
2171                         sg++;
2172                 }
2173                 psg->count = cpu_to_le32(sg_count);
2174                 /* hba wants the size to be exact */
2175                 if(byte_count > scsicmd->request_bufflen){
2176                         u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
2177                                 (byte_count - scsicmd->request_bufflen);
2178                         psg->sg[i-1].count = cpu_to_le32(temp);
2179                         byte_count = scsicmd->request_bufflen;
2180                 }
2181                 /* Check for command underflow */
2182                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2183                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2184                                         byte_count, scsicmd->underflow);
2185                 }
2186         }
2187         else if(scsicmd->request_bufflen) {
2188                 int count;
2189                 u64 addr;
2190                 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2191                                 scsicmd->request_buffer,
2192                                 scsicmd->request_bufflen,
2193                                 scsicmd->sc_data_direction);
2194                 addr = scsicmd->SCp.dma_handle;
2195                 count = scsicmd->request_bufflen;
2196                 psg->count = cpu_to_le32(1);
2197                 psg->sg[0].next = 0;
2198                 psg->sg[0].prev = 0;
2199                 psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
2200                 psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2201                 psg->sg[0].count = cpu_to_le32(count);
2202                 psg->sg[0].flags = 0;
2203                 byte_count = scsicmd->request_bufflen;
2204         }
2205         return byte_count;
2206 }
2207
2208 #ifdef AAC_DETAILED_STATUS_INFO
2209
2210 struct aac_srb_status_info {
2211         u32     status;
2212         char    *str;
2213 };
2214
2215
2216 static struct aac_srb_status_info srb_status_info[] = {
2217         { SRB_STATUS_PENDING,           "Pending Status"},
2218         { SRB_STATUS_SUCCESS,           "Success"},
2219         { SRB_STATUS_ABORTED,           "Aborted Command"},
2220         { SRB_STATUS_ABORT_FAILED,      "Abort Failed"},
2221         { SRB_STATUS_ERROR,             "Error Event"}, 
2222         { SRB_STATUS_BUSY,              "Device Busy"},
2223         { SRB_STATUS_INVALID_REQUEST,   "Invalid Request"},
2224         { SRB_STATUS_INVALID_PATH_ID,   "Invalid Path ID"},
2225         { SRB_STATUS_NO_DEVICE,         "No Device"},
2226         { SRB_STATUS_TIMEOUT,           "Timeout"},
2227         { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2228         { SRB_STATUS_COMMAND_TIMEOUT,   "Command Timeout"},
2229         { SRB_STATUS_MESSAGE_REJECTED,  "Message Rejected"},
2230         { SRB_STATUS_BUS_RESET,         "Bus Reset"},
2231         { SRB_STATUS_PARITY_ERROR,      "Parity Error"},
2232         { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2233         { SRB_STATUS_NO_HBA,            "No HBA"},
2234         { SRB_STATUS_DATA_OVERRUN,      "Data Overrun/Data Underrun"},
2235         { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2236         { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2237         { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2238         { SRB_STATUS_REQUEST_FLUSHED,   "Request Flushed"},
2239         { SRB_STATUS_DELAYED_RETRY,     "Delayed Retry"},
2240         { SRB_STATUS_INVALID_LUN,       "Invalid LUN"}, 
2241         { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2242         { SRB_STATUS_BAD_FUNCTION,      "Bad Function"},
2243         { SRB_STATUS_ERROR_RECOVERY,    "Error Recovery"},
2244         { SRB_STATUS_NOT_STARTED,       "Not Started"},
2245         { SRB_STATUS_NOT_IN_USE,        "Not In Use"},
2246         { SRB_STATUS_FORCE_ABORT,       "Force Abort"},
2247         { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2248         { 0xff,                         "Unknown Error"}
2249 };
2250
2251 char *aac_get_status_string(u32 status)
2252 {
2253         int i;
2254
2255         for(i=0; i < (sizeof(srb_status_info)/sizeof(struct aac_srb_status_info)); i++ ){
2256                 if(srb_status_info[i].status == status){
2257                         return srb_status_info[i].str;
2258                 }
2259         }
2260
2261         return "Bad Status Code";
2262 }
2263
2264 #endif