[SCSI] aacraid: add parameter to control FUA and SYNCHRONIZE_CACHE policy
[safe/jmp/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-2007 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/pci.h>
30 #include <linux/spinlock.h>
31 #include <linux/slab.h>
32 #include <linux/completion.h>
33 #include <linux/blkdev.h>
34 #include <linux/dma-mapping.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 aac_cache = 0;
148 static int dacmode = -1;
149
150 int aac_commit = -1;
151 int startup_timeout = 180;
152 int aif_timeout = 120;
153
154 module_param(nondasd, int, S_IRUGO|S_IWUSR);
155 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
156 module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
157 MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n\tbit 0 - Disable FUA in WRITE SCSI commands\n\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n\tbit 2 - Disable only if Battery not protecting Cache");
158 module_param(dacmode, int, S_IRUGO|S_IWUSR);
159 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
160 module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
161 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");
162 module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
163 MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for adapter to have it's kernel up and\nrunning. This is typically adjusted for large systems that do not have a BIOS.");
164 module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
165 MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for applications to pick up AIFs before\nderegistering them. This is typically adjusted for heavily burdened systems.");
166
167 int numacb = -1;
168 module_param(numacb, int, S_IRUGO|S_IWUSR);
169 MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control blocks (FIB) allocated. Valid values are 512 and down. Default is to use suggestion from Firmware.");
170
171 int acbsize = -1;
172 module_param(acbsize, int, S_IRUGO|S_IWUSR);
173 MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB) size. Valid values are 512, 2048, 4096 and 8192. Default is to use suggestion from Firmware.");
174
175 int update_interval = 30 * 60;
176 module_param(update_interval, int, S_IRUGO|S_IWUSR);
177 MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync updates issued to adapter.");
178
179 int check_interval = 24 * 60 * 60;
180 module_param(check_interval, int, S_IRUGO|S_IWUSR);
181 MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health checks.");
182
183 int aac_check_reset = 1;
184 module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
185 MODULE_PARM_DESC(aac_check_reset, "If adapter fails health check, reset the adapter. a value of -1 forces the reset to adapters programmed to ignore it.");
186
187 int expose_physicals = -1;
188 module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
189 MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays. -1=protect 0=off, 1=on");
190
191 int aac_reset_devices = 0;
192 module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
193 MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
194
195 static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
196                 struct fib *fibptr) {
197         struct scsi_device *device;
198
199         if (unlikely(!scsicmd || !scsicmd->scsi_done )) {
200                 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
201                 aac_fib_complete(fibptr);
202                 aac_fib_free(fibptr);
203                 return 0;
204         }
205         scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
206         device = scsicmd->device;
207         if (unlikely(!device || !scsi_device_online(device))) {
208                 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
209                 aac_fib_complete(fibptr);
210                 aac_fib_free(fibptr);
211                 return 0;
212         }
213         return 1;
214 }
215
216 /**
217  *      aac_get_config_status   -       check the adapter configuration
218  *      @common: adapter to query
219  *
220  *      Query config status, and commit the configuration if needed.
221  */
222 int aac_get_config_status(struct aac_dev *dev, int commit_flag)
223 {
224         int status = 0;
225         struct fib * fibptr;
226
227         if (!(fibptr = aac_fib_alloc(dev)))
228                 return -ENOMEM;
229
230         aac_fib_init(fibptr);
231         {
232                 struct aac_get_config_status *dinfo;
233                 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
234
235                 dinfo->command = cpu_to_le32(VM_ContainerConfig);
236                 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
237                 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
238         }
239
240         status = aac_fib_send(ContainerCommand,
241                             fibptr,
242                             sizeof (struct aac_get_config_status),
243                             FsaNormal,
244                             1, 1,
245                             NULL, NULL);
246         if (status < 0 ) {
247                 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
248         } else {
249                 struct aac_get_config_status_resp *reply
250                   = (struct aac_get_config_status_resp *) fib_data(fibptr);
251                 dprintk((KERN_WARNING
252                   "aac_get_config_status: response=%d status=%d action=%d\n",
253                   le32_to_cpu(reply->response),
254                   le32_to_cpu(reply->status),
255                   le32_to_cpu(reply->data.action)));
256                 if ((le32_to_cpu(reply->response) != ST_OK) ||
257                      (le32_to_cpu(reply->status) != CT_OK) ||
258                      (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
259                         printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
260                         status = -EINVAL;
261                 }
262         }
263         aac_fib_complete(fibptr);
264         /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
265         if (status >= 0) {
266                 if ((aac_commit == 1) || commit_flag) {
267                         struct aac_commit_config * dinfo;
268                         aac_fib_init(fibptr);
269                         dinfo = (struct aac_commit_config *) fib_data(fibptr);
270         
271                         dinfo->command = cpu_to_le32(VM_ContainerConfig);
272                         dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
273         
274                         status = aac_fib_send(ContainerCommand,
275                                     fibptr,
276                                     sizeof (struct aac_commit_config),
277                                     FsaNormal,
278                                     1, 1,
279                                     NULL, NULL);
280                         aac_fib_complete(fibptr);
281                 } else if (aac_commit == 0) {
282                         printk(KERN_WARNING
283                           "aac_get_config_status: Foreign device configurations are being ignored\n");
284                 }
285         }
286         aac_fib_free(fibptr);
287         return status;
288 }
289
290 /**
291  *      aac_get_containers      -       list containers
292  *      @common: adapter to probe
293  *
294  *      Make a list of all containers on this controller
295  */
296 int aac_get_containers(struct aac_dev *dev)
297 {
298         struct fsa_dev_info *fsa_dev_ptr;
299         u32 index; 
300         int status = 0;
301         struct fib * fibptr;
302         struct aac_get_container_count *dinfo;
303         struct aac_get_container_count_resp *dresp;
304         int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
305
306         if (!(fibptr = aac_fib_alloc(dev)))
307                 return -ENOMEM;
308
309         aac_fib_init(fibptr);
310         dinfo = (struct aac_get_container_count *) fib_data(fibptr);
311         dinfo->command = cpu_to_le32(VM_ContainerConfig);
312         dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
313
314         status = aac_fib_send(ContainerCommand,
315                     fibptr,
316                     sizeof (struct aac_get_container_count),
317                     FsaNormal,
318                     1, 1,
319                     NULL, NULL);
320         if (status >= 0) {
321                 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
322                 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
323                 aac_fib_complete(fibptr);
324         }
325         aac_fib_free(fibptr);
326
327         if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
328                 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
329         fsa_dev_ptr = kzalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
330                         GFP_KERNEL);
331         if (!fsa_dev_ptr)
332                 return -ENOMEM;
333
334         dev->fsa_dev = fsa_dev_ptr;
335         dev->maximum_num_containers = maximum_num_containers;
336
337         for (index = 0; index < dev->maximum_num_containers; ) {
338                 fsa_dev_ptr[index].devname[0] = '\0';
339
340                 status = aac_probe_container(dev, index);
341
342                 if (status < 0) {
343                         printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
344                         break;
345                 }
346
347                 /*
348                  *      If there are no more containers, then stop asking.
349                  */
350                 if (++index >= status)
351                         break;
352         }
353         return status;
354 }
355
356 static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
357 {
358         void *buf;
359         int transfer_len;
360         struct scatterlist *sg = scsi_sglist(scsicmd);
361
362         buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
363         transfer_len = min(sg->length, len + offset);
364
365         transfer_len -= offset;
366         if (buf && transfer_len > 0)
367                 memcpy(buf + offset, data, transfer_len);
368
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         if (!aac_valid_context(scsicmd, fibptr))
381                 return;
382
383         dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
384         BUG_ON(fibptr == NULL);
385
386         get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
387         /* Failure is irrelevant, using default value instead */
388         if ((le32_to_cpu(get_name_reply->status) == CT_OK)
389          && (get_name_reply->data[0] != '\0')) {
390                 char *sp = get_name_reply->data;
391                 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
392                 while (*sp == ' ')
393                         ++sp;
394                 if (*sp) {
395                         char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
396                         int count = sizeof(d);
397                         char *dp = d;
398                         do {
399                                 *dp++ = (*sp) ? *sp++ : ' ';
400                         } while (--count > 0);
401                         aac_internal_transfer(scsicmd, d, 
402                           offsetof(struct inquiry_data, inqd_pid), sizeof(d));
403                 }
404         }
405
406         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
407
408         aac_fib_complete(fibptr);
409         aac_fib_free(fibptr);
410         scsicmd->scsi_done(scsicmd);
411 }
412
413 /**
414  *      aac_get_container_name  -       get container name, none blocking.
415  */
416 static int aac_get_container_name(struct scsi_cmnd * scsicmd)
417 {
418         int status;
419         struct aac_get_name *dinfo;
420         struct fib * cmd_fibcontext;
421         struct aac_dev * dev;
422
423         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
424
425         if (!(cmd_fibcontext = aac_fib_alloc(dev)))
426                 return -ENOMEM;
427
428         aac_fib_init(cmd_fibcontext);
429         dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
430
431         dinfo->command = cpu_to_le32(VM_ContainerConfig);
432         dinfo->type = cpu_to_le32(CT_READ_NAME);
433         dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
434         dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
435
436         status = aac_fib_send(ContainerCommand,
437                   cmd_fibcontext, 
438                   sizeof (struct aac_get_name),
439                   FsaNormal, 
440                   0, 1, 
441                   (fib_callback) get_container_name_callback, 
442                   (void *) scsicmd);
443         
444         /*
445          *      Check that the command queued to the controller
446          */
447         if (status == -EINPROGRESS) {
448                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
449                 return 0;
450         }
451                 
452         printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
453         aac_fib_complete(cmd_fibcontext);
454         aac_fib_free(cmd_fibcontext);
455         return -1;
456 }
457
458 static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
459 {
460         struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
461
462         if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
463                 return aac_scsi_cmd(scsicmd);
464
465         scsicmd->result = DID_NO_CONNECT << 16;
466         scsicmd->scsi_done(scsicmd);
467         return 0;
468 }
469
470 static void _aac_probe_container2(void * context, struct fib * fibptr)
471 {
472         struct fsa_dev_info *fsa_dev_ptr;
473         int (*callback)(struct scsi_cmnd *);
474         struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
475
476
477         if (!aac_valid_context(scsicmd, fibptr))
478                 return;
479
480         scsicmd->SCp.Status = 0;
481         fsa_dev_ptr = fibptr->dev->fsa_dev;
482         if (fsa_dev_ptr) {
483                 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
484                 fsa_dev_ptr += scmd_id(scsicmd);
485
486                 if ((le32_to_cpu(dresp->status) == ST_OK) &&
487                     (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
488                     (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
489                         fsa_dev_ptr->valid = 1;
490                         fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
491                         fsa_dev_ptr->size
492                           = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
493                             (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
494                         fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
495                 }
496                 if ((fsa_dev_ptr->valid & 1) == 0)
497                         fsa_dev_ptr->valid = 0;
498                 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
499         }
500         aac_fib_complete(fibptr);
501         aac_fib_free(fibptr);
502         callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
503         scsicmd->SCp.ptr = NULL;
504         (*callback)(scsicmd);
505         return;
506 }
507
508 static void _aac_probe_container1(void * context, struct fib * fibptr)
509 {
510         struct scsi_cmnd * scsicmd;
511         struct aac_mount * dresp;
512         struct aac_query_mount *dinfo;
513         int status;
514
515         dresp = (struct aac_mount *) fib_data(fibptr);
516         dresp->mnt[0].capacityhigh = 0;
517         if ((le32_to_cpu(dresp->status) != ST_OK) ||
518             (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
519                 _aac_probe_container2(context, fibptr);
520                 return;
521         }
522         scsicmd = (struct scsi_cmnd *) context;
523
524         if (!aac_valid_context(scsicmd, fibptr))
525                 return;
526
527         aac_fib_init(fibptr);
528
529         dinfo = (struct aac_query_mount *)fib_data(fibptr);
530
531         dinfo->command = cpu_to_le32(VM_NameServe64);
532         dinfo->count = cpu_to_le32(scmd_id(scsicmd));
533         dinfo->type = cpu_to_le32(FT_FILESYS);
534
535         status = aac_fib_send(ContainerCommand,
536                           fibptr,
537                           sizeof(struct aac_query_mount),
538                           FsaNormal,
539                           0, 1,
540                           _aac_probe_container2,
541                           (void *) scsicmd);
542         /*
543          *      Check that the command queued to the controller
544          */
545         if (status == -EINPROGRESS)
546                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
547         else if (status < 0) {
548                 /* Inherit results from VM_NameServe, if any */
549                 dresp->status = cpu_to_le32(ST_OK);
550                 _aac_probe_container2(context, fibptr);
551         }
552 }
553
554 static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
555 {
556         struct fib * fibptr;
557         int status = -ENOMEM;
558
559         if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
560                 struct aac_query_mount *dinfo;
561
562                 aac_fib_init(fibptr);
563
564                 dinfo = (struct aac_query_mount *)fib_data(fibptr);
565
566                 dinfo->command = cpu_to_le32(VM_NameServe);
567                 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
568                 dinfo->type = cpu_to_le32(FT_FILESYS);
569                 scsicmd->SCp.ptr = (char *)callback;
570
571                 status = aac_fib_send(ContainerCommand,
572                           fibptr,
573                           sizeof(struct aac_query_mount),
574                           FsaNormal,
575                           0, 1,
576                           _aac_probe_container1,
577                           (void *) scsicmd);
578                 /*
579                  *      Check that the command queued to the controller
580                  */
581                 if (status == -EINPROGRESS) {
582                         scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
583                         return 0;
584                 }
585                 if (status < 0) {
586                         scsicmd->SCp.ptr = NULL;
587                         aac_fib_complete(fibptr);
588                         aac_fib_free(fibptr);
589                 }
590         }
591         if (status < 0) {
592                 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
593                 if (fsa_dev_ptr) {
594                         fsa_dev_ptr += scmd_id(scsicmd);
595                         if ((fsa_dev_ptr->valid & 1) == 0) {
596                                 fsa_dev_ptr->valid = 0;
597                                 return (*callback)(scsicmd);
598                         }
599                 }
600         }
601         return status;
602 }
603
604 /**
605  *      aac_probe_container             -       query a logical volume
606  *      @dev: device to query
607  *      @cid: container identifier
608  *
609  *      Queries the controller about the given volume. The volume information
610  *      is updated in the struct fsa_dev_info structure rather than returned.
611  */
612 static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
613 {
614         scsicmd->device = NULL;
615         return 0;
616 }
617
618 int aac_probe_container(struct aac_dev *dev, int cid)
619 {
620         struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
621         struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
622         int status;
623
624         if (!scsicmd || !scsidev) {
625                 kfree(scsicmd);
626                 kfree(scsidev);
627                 return -ENOMEM;
628         }
629         scsicmd->list.next = NULL;
630         scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
631
632         scsicmd->device = scsidev;
633         scsidev->sdev_state = 0;
634         scsidev->id = cid;
635         scsidev->host = dev->scsi_host_ptr;
636
637         if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
638                 while (scsicmd->device == scsidev)
639                         schedule();
640         kfree(scsidev);
641         status = scsicmd->SCp.Status;
642         kfree(scsicmd);
643         return status;
644 }
645
646 /* Local Structure to set SCSI inquiry data strings */
647 struct scsi_inq {
648         char vid[8];         /* Vendor ID */
649         char pid[16];        /* Product ID */
650         char prl[4];         /* Product Revision Level */
651 };
652
653 /**
654  *      InqStrCopy      -       string merge
655  *      @a:     string to copy from
656  *      @b:     string to copy to
657  *
658  *      Copy a String from one location to another
659  *      without copying \0
660  */
661
662 static void inqstrcpy(char *a, char *b)
663 {
664
665         while(*a != (char)0) 
666                 *b++ = *a++;
667 }
668
669 static char *container_types[] = {
670         "None",
671         "Volume",
672         "Mirror",
673         "Stripe",
674         "RAID5",
675         "SSRW",
676         "SSRO",
677         "Morph",
678         "Legacy",
679         "RAID4",
680         "RAID10",             
681         "RAID00",             
682         "V-MIRRORS",          
683         "PSEUDO R4",          
684         "RAID50",
685         "RAID5D",
686         "RAID5D0",
687         "RAID1E",
688         "RAID6",
689         "RAID60",
690         "Unknown"
691 };
692
693
694
695 /* Function: setinqstr
696  *
697  * Arguments: [1] pointer to void [1] int
698  *
699  * Purpose: Sets SCSI inquiry data strings for vendor, product
700  * and revision level. Allows strings to be set in platform dependant
701  * files instead of in OS dependant driver source.
702  */
703
704 static void setinqstr(struct aac_dev *dev, void *data, int tindex)
705 {
706         struct scsi_inq *str;
707
708         str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
709         memset(str, ' ', sizeof(*str));
710
711         if (dev->supplement_adapter_info.AdapterTypeText[0]) {
712                 char * cp = dev->supplement_adapter_info.AdapterTypeText;
713                 int c = sizeof(str->vid);
714                 while (*cp && *cp != ' ' && --c)
715                         ++cp;
716                 c = *cp;
717                 *cp = '\0';
718                 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
719                   str->vid); 
720                 *cp = c;
721                 while (*cp && *cp != ' ')
722                         ++cp;
723                 while (*cp == ' ')
724                         ++cp;
725                 /* last six chars reserved for vol type */
726                 c = 0;
727                 if (strlen(cp) > sizeof(str->pid)) {
728                         c = cp[sizeof(str->pid)];
729                         cp[sizeof(str->pid)] = '\0';
730                 }
731                 inqstrcpy (cp, str->pid);
732                 if (c)
733                         cp[sizeof(str->pid)] = c;
734         } else {
735                 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
736
737                 inqstrcpy (mp->vname, str->vid);
738                 /* last six chars reserved for vol type */
739                 inqstrcpy (mp->model, str->pid);
740         }
741
742         if (tindex < ARRAY_SIZE(container_types)){
743                 char *findit = str->pid;
744
745                 for ( ; *findit != ' '; findit++); /* walk till we find a space */
746                 /* RAID is superfluous in the context of a RAID device */
747                 if (memcmp(findit-4, "RAID", 4) == 0)
748                         *(findit -= 4) = ' ';
749                 if (((findit - str->pid) + strlen(container_types[tindex]))
750                  < (sizeof(str->pid) + sizeof(str->prl)))
751                         inqstrcpy (container_types[tindex], findit + 1);
752         }
753         inqstrcpy ("V1.0", str->prl);
754 }
755
756 static void get_container_serial_callback(void *context, struct fib * fibptr)
757 {
758         struct aac_get_serial_resp * get_serial_reply;
759         struct scsi_cmnd * scsicmd;
760
761         BUG_ON(fibptr == NULL);
762
763         scsicmd = (struct scsi_cmnd *) context;
764         if (!aac_valid_context(scsicmd, fibptr))
765                 return;
766
767         get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
768         /* Failure is irrelevant, using default value instead */
769         if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
770                 char sp[13];
771                 /* EVPD bit set */
772                 sp[0] = INQD_PDT_DA;
773                 sp[1] = scsicmd->cmnd[2];
774                 sp[2] = 0;
775                 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
776                   le32_to_cpu(get_serial_reply->uid));
777                 aac_internal_transfer(scsicmd, sp, 0, sizeof(sp));
778         }
779
780         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
781
782         aac_fib_complete(fibptr);
783         aac_fib_free(fibptr);
784         scsicmd->scsi_done(scsicmd);
785 }
786
787 /**
788  *      aac_get_container_serial - get container serial, none blocking.
789  */
790 static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
791 {
792         int status;
793         struct aac_get_serial *dinfo;
794         struct fib * cmd_fibcontext;
795         struct aac_dev * dev;
796
797         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
798
799         if (!(cmd_fibcontext = aac_fib_alloc(dev)))
800                 return -ENOMEM;
801
802         aac_fib_init(cmd_fibcontext);
803         dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
804
805         dinfo->command = cpu_to_le32(VM_ContainerConfig);
806         dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
807         dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
808
809         status = aac_fib_send(ContainerCommand,
810                   cmd_fibcontext,
811                   sizeof (struct aac_get_serial),
812                   FsaNormal,
813                   0, 1,
814                   (fib_callback) get_container_serial_callback,
815                   (void *) scsicmd);
816
817         /*
818          *      Check that the command queued to the controller
819          */
820         if (status == -EINPROGRESS) {
821                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
822                 return 0;
823         }
824
825         printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
826         aac_fib_complete(cmd_fibcontext);
827         aac_fib_free(cmd_fibcontext);
828         return -1;
829 }
830
831 /* Function: setinqserial
832  *
833  * Arguments: [1] pointer to void [1] int
834  *
835  * Purpose: Sets SCSI Unit Serial number.
836  *          This is a fake. We should read a proper
837  *          serial number from the container. <SuSE>But
838  *          without docs it's quite hard to do it :-)
839  *          So this will have to do in the meantime.</SuSE>
840  */
841
842 static int setinqserial(struct aac_dev *dev, void *data, int cid)
843 {
844         /*
845          *      This breaks array migration.
846          */
847         return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
848                         le32_to_cpu(dev->adapter_info.serial[0]), cid);
849 }
850
851 static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
852                       u8 a_sense_code, u8 incorrect_length,
853                       u8 bit_pointer, u16 field_pointer,
854                       u32 residue)
855 {
856         sense_buf[0] = 0xF0;    /* Sense data valid, err code 70h (current error) */
857         sense_buf[1] = 0;       /* Segment number, always zero */
858
859         if (incorrect_length) {
860                 sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
861                 sense_buf[3] = BYTE3(residue);
862                 sense_buf[4] = BYTE2(residue);
863                 sense_buf[5] = BYTE1(residue);
864                 sense_buf[6] = BYTE0(residue);
865         } else
866                 sense_buf[2] = sense_key;       /* Sense key */
867
868         if (sense_key == ILLEGAL_REQUEST)
869                 sense_buf[7] = 10;      /* Additional sense length */
870         else
871                 sense_buf[7] = 6;       /* Additional sense length */
872
873         sense_buf[12] = sense_code;     /* Additional sense code */
874         sense_buf[13] = a_sense_code;   /* Additional sense code qualifier */
875         if (sense_key == ILLEGAL_REQUEST) {
876                 sense_buf[15] = 0;
877
878                 if (sense_code == SENCODE_INVALID_PARAM_FIELD)
879                         sense_buf[15] = 0x80;/* Std sense key specific field */
880                 /* Illegal parameter is in the parameter block */
881
882                 if (sense_code == SENCODE_INVALID_CDB_FIELD)
883                         sense_buf[15] = 0xc0;/* Std sense key specific field */
884                 /* Illegal parameter is in the CDB block */
885                 sense_buf[15] |= bit_pointer;
886                 sense_buf[16] = field_pointer >> 8;     /* MSB */
887                 sense_buf[17] = field_pointer;          /* LSB */
888         }
889 }
890
891 static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
892 {
893         if (lba & 0xffffffff00000000LL) {
894                 int cid = scmd_id(cmd);
895                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
896                 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
897                         SAM_STAT_CHECK_CONDITION;
898                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
899                             HARDWARE_ERROR,
900                             SENCODE_INTERNAL_TARGET_FAILURE,
901                             ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
902                             0, 0);
903                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
904                   (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(cmd->sense_buffer))
905                     ? sizeof(cmd->sense_buffer)
906                     : sizeof(dev->fsa_dev[cid].sense_data));
907                 cmd->scsi_done(cmd);
908                 return 1;
909         }
910         return 0;
911 }
912
913 static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
914 {
915         return 0;
916 }
917
918 static void io_callback(void *context, struct fib * fibptr);
919
920 static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
921 {
922         u16 fibsize;
923         struct aac_raw_io *readcmd;
924         aac_fib_init(fib);
925         readcmd = (struct aac_raw_io *) fib_data(fib);
926         readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
927         readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
928         readcmd->count = cpu_to_le32(count<<9);
929         readcmd->cid = cpu_to_le16(scmd_id(cmd));
930         readcmd->flags = cpu_to_le16(IO_TYPE_READ);
931         readcmd->bpTotal = 0;
932         readcmd->bpComplete = 0;
933
934         aac_build_sgraw(cmd, &readcmd->sg);
935         fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
936         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
937         /*
938          *      Now send the Fib to the adapter
939          */
940         return aac_fib_send(ContainerRawIo,
941                           fib,
942                           fibsize,
943                           FsaNormal,
944                           0, 1,
945                           (fib_callback) io_callback,
946                           (void *) cmd);
947 }
948
949 static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
950 {
951         u16 fibsize;
952         struct aac_read64 *readcmd;
953         aac_fib_init(fib);
954         readcmd = (struct aac_read64 *) fib_data(fib);
955         readcmd->command = cpu_to_le32(VM_CtHostRead64);
956         readcmd->cid = cpu_to_le16(scmd_id(cmd));
957         readcmd->sector_count = cpu_to_le16(count);
958         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
959         readcmd->pad   = 0;
960         readcmd->flags = 0;
961
962         aac_build_sg64(cmd, &readcmd->sg);
963         fibsize = sizeof(struct aac_read64) +
964                 ((le32_to_cpu(readcmd->sg.count) - 1) *
965                  sizeof (struct sgentry64));
966         BUG_ON (fibsize > (fib->dev->max_fib_size -
967                                 sizeof(struct aac_fibhdr)));
968         /*
969          *      Now send the Fib to the adapter
970          */
971         return aac_fib_send(ContainerCommand64,
972                           fib,
973                           fibsize,
974                           FsaNormal,
975                           0, 1,
976                           (fib_callback) io_callback,
977                           (void *) cmd);
978 }
979
980 static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
981 {
982         u16 fibsize;
983         struct aac_read *readcmd;
984         aac_fib_init(fib);
985         readcmd = (struct aac_read *) fib_data(fib);
986         readcmd->command = cpu_to_le32(VM_CtBlockRead);
987         readcmd->cid = cpu_to_le32(scmd_id(cmd));
988         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
989         readcmd->count = cpu_to_le32(count * 512);
990
991         aac_build_sg(cmd, &readcmd->sg);
992         fibsize = sizeof(struct aac_read) +
993                         ((le32_to_cpu(readcmd->sg.count) - 1) *
994                          sizeof (struct sgentry));
995         BUG_ON (fibsize > (fib->dev->max_fib_size -
996                                 sizeof(struct aac_fibhdr)));
997         /*
998          *      Now send the Fib to the adapter
999          */
1000         return aac_fib_send(ContainerCommand,
1001                           fib,
1002                           fibsize,
1003                           FsaNormal,
1004                           0, 1,
1005                           (fib_callback) io_callback,
1006                           (void *) cmd);
1007 }
1008
1009 static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1010 {
1011         u16 fibsize;
1012         struct aac_raw_io *writecmd;
1013         aac_fib_init(fib);
1014         writecmd = (struct aac_raw_io *) fib_data(fib);
1015         writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1016         writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1017         writecmd->count = cpu_to_le32(count<<9);
1018         writecmd->cid = cpu_to_le16(scmd_id(cmd));
1019         writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1020           (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1021                 cpu_to_le16(IO_TYPE_WRITE|IO_SUREWRITE) :
1022                 cpu_to_le16(IO_TYPE_WRITE);
1023         writecmd->bpTotal = 0;
1024         writecmd->bpComplete = 0;
1025
1026         aac_build_sgraw(cmd, &writecmd->sg);
1027         fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1028         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1029         /*
1030          *      Now send the Fib to the adapter
1031          */
1032         return aac_fib_send(ContainerRawIo,
1033                           fib,
1034                           fibsize,
1035                           FsaNormal,
1036                           0, 1,
1037                           (fib_callback) io_callback,
1038                           (void *) cmd);
1039 }
1040
1041 static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1042 {
1043         u16 fibsize;
1044         struct aac_write64 *writecmd;
1045         aac_fib_init(fib);
1046         writecmd = (struct aac_write64 *) fib_data(fib);
1047         writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1048         writecmd->cid = cpu_to_le16(scmd_id(cmd));
1049         writecmd->sector_count = cpu_to_le16(count);
1050         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1051         writecmd->pad   = 0;
1052         writecmd->flags = 0;
1053
1054         aac_build_sg64(cmd, &writecmd->sg);
1055         fibsize = sizeof(struct aac_write64) +
1056                 ((le32_to_cpu(writecmd->sg.count) - 1) *
1057                  sizeof (struct sgentry64));
1058         BUG_ON (fibsize > (fib->dev->max_fib_size -
1059                                 sizeof(struct aac_fibhdr)));
1060         /*
1061          *      Now send the Fib to the adapter
1062          */
1063         return aac_fib_send(ContainerCommand64,
1064                           fib,
1065                           fibsize,
1066                           FsaNormal,
1067                           0, 1,
1068                           (fib_callback) io_callback,
1069                           (void *) cmd);
1070 }
1071
1072 static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1073 {
1074         u16 fibsize;
1075         struct aac_write *writecmd;
1076         aac_fib_init(fib);
1077         writecmd = (struct aac_write *) fib_data(fib);
1078         writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1079         writecmd->cid = cpu_to_le32(scmd_id(cmd));
1080         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1081         writecmd->count = cpu_to_le32(count * 512);
1082         writecmd->sg.count = cpu_to_le32(1);
1083         /* ->stable is not used - it did mean which type of write */
1084
1085         aac_build_sg(cmd, &writecmd->sg);
1086         fibsize = sizeof(struct aac_write) +
1087                 ((le32_to_cpu(writecmd->sg.count) - 1) *
1088                  sizeof (struct sgentry));
1089         BUG_ON (fibsize > (fib->dev->max_fib_size -
1090                                 sizeof(struct aac_fibhdr)));
1091         /*
1092          *      Now send the Fib to the adapter
1093          */
1094         return aac_fib_send(ContainerCommand,
1095                           fib,
1096                           fibsize,
1097                           FsaNormal,
1098                           0, 1,
1099                           (fib_callback) io_callback,
1100                           (void *) cmd);
1101 }
1102
1103 static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1104 {
1105         struct aac_srb * srbcmd;
1106         u32 flag;
1107         u32 timeout;
1108
1109         aac_fib_init(fib);
1110         switch(cmd->sc_data_direction){
1111         case DMA_TO_DEVICE:
1112                 flag = SRB_DataOut;
1113                 break;
1114         case DMA_BIDIRECTIONAL:
1115                 flag = SRB_DataIn | SRB_DataOut;
1116                 break;
1117         case DMA_FROM_DEVICE:
1118                 flag = SRB_DataIn;
1119                 break;
1120         case DMA_NONE:
1121         default:        /* shuts up some versions of gcc */
1122                 flag = SRB_NoDataXfer;
1123                 break;
1124         }
1125
1126         srbcmd = (struct aac_srb*) fib_data(fib);
1127         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1128         srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1129         srbcmd->id       = cpu_to_le32(scmd_id(cmd));
1130         srbcmd->lun      = cpu_to_le32(cmd->device->lun);
1131         srbcmd->flags    = cpu_to_le32(flag);
1132         timeout = cmd->timeout_per_command/HZ;
1133         if (timeout == 0)
1134                 timeout = 1;
1135         srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1136         srbcmd->retry_limit = 0; /* Obsolete parameter */
1137         srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1138         return srbcmd;
1139 }
1140
1141 static void aac_srb_callback(void *context, struct fib * fibptr);
1142
1143 static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1144 {
1145         u16 fibsize;
1146         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1147
1148         aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
1149         srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1150
1151         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1152         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1153         /*
1154          *      Build Scatter/Gather list
1155          */
1156         fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1157                 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1158                  sizeof (struct sgentry64));
1159         BUG_ON (fibsize > (fib->dev->max_fib_size -
1160                                 sizeof(struct aac_fibhdr)));
1161
1162         /*
1163          *      Now send the Fib to the adapter
1164          */
1165         return aac_fib_send(ScsiPortCommand64, fib,
1166                                 fibsize, FsaNormal, 0, 1,
1167                                   (fib_callback) aac_srb_callback,
1168                                   (void *) cmd);
1169 }
1170
1171 static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1172 {
1173         u16 fibsize;
1174         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1175
1176         aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
1177         srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1178
1179         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1180         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1181         /*
1182          *      Build Scatter/Gather list
1183          */
1184         fibsize = sizeof (struct aac_srb) +
1185                 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1186                  sizeof (struct sgentry));
1187         BUG_ON (fibsize > (fib->dev->max_fib_size -
1188                                 sizeof(struct aac_fibhdr)));
1189
1190         /*
1191          *      Now send the Fib to the adapter
1192          */
1193         return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1194                                   (fib_callback) aac_srb_callback, (void *) cmd);
1195 }
1196
1197 static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1198 {
1199         if ((sizeof(dma_addr_t) > 4) &&
1200          (num_physpages > (0xFFFFFFFFULL >> PAGE_SHIFT)) &&
1201          (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1202                 return FAILED;
1203         return aac_scsi_32(fib, cmd);
1204 }
1205
1206 int aac_get_adapter_info(struct aac_dev* dev)
1207 {
1208         struct fib* fibptr;
1209         int rcode;
1210         u32 tmp;
1211         struct aac_adapter_info *info;
1212         struct aac_bus_info *command;
1213         struct aac_bus_info_response *bus_info;
1214
1215         if (!(fibptr = aac_fib_alloc(dev)))
1216                 return -ENOMEM;
1217
1218         aac_fib_init(fibptr);
1219         info = (struct aac_adapter_info *) fib_data(fibptr);
1220         memset(info,0,sizeof(*info));
1221
1222         rcode = aac_fib_send(RequestAdapterInfo,
1223                          fibptr, 
1224                          sizeof(*info),
1225                          FsaNormal, 
1226                          -1, 1, /* First `interrupt' command uses special wait */
1227                          NULL, 
1228                          NULL);
1229
1230         if (rcode < 0) {
1231                 aac_fib_complete(fibptr);
1232                 aac_fib_free(fibptr);
1233                 return rcode;
1234         }
1235         memcpy(&dev->adapter_info, info, sizeof(*info));
1236
1237         if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1238                 struct aac_supplement_adapter_info * info;
1239
1240                 aac_fib_init(fibptr);
1241
1242                 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1243
1244                 memset(info,0,sizeof(*info));
1245
1246                 rcode = aac_fib_send(RequestSupplementAdapterInfo,
1247                                  fibptr,
1248                                  sizeof(*info),
1249                                  FsaNormal,
1250                                  1, 1,
1251                                  NULL,
1252                                  NULL);
1253
1254                 if (rcode >= 0)
1255                         memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
1256         }
1257
1258
1259         /* 
1260          * GetBusInfo 
1261          */
1262
1263         aac_fib_init(fibptr);
1264
1265         bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1266
1267         memset(bus_info, 0, sizeof(*bus_info));
1268
1269         command = (struct aac_bus_info *)bus_info;
1270
1271         command->Command = cpu_to_le32(VM_Ioctl);
1272         command->ObjType = cpu_to_le32(FT_DRIVE);
1273         command->MethodId = cpu_to_le32(1);
1274         command->CtlCmd = cpu_to_le32(GetBusInfo);
1275
1276         rcode = aac_fib_send(ContainerCommand,
1277                          fibptr,
1278                          sizeof (*bus_info),
1279                          FsaNormal,
1280                          1, 1,
1281                          NULL, NULL);
1282
1283         /* reasoned default */
1284         dev->maximum_num_physicals = 16;
1285         if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1286                 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1287                 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1288         }
1289
1290         if (!dev->in_reset) {
1291                 char buffer[16];
1292                 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1293                 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
1294                         dev->name, 
1295                         dev->id,
1296                         tmp>>24,
1297                         (tmp>>16)&0xff,
1298                         tmp&0xff,
1299                         le32_to_cpu(dev->adapter_info.kernelbuild),
1300                         (int)sizeof(dev->supplement_adapter_info.BuildDate),
1301                         dev->supplement_adapter_info.BuildDate);
1302                 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1303                 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
1304                         dev->name, dev->id,
1305                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1306                         le32_to_cpu(dev->adapter_info.monitorbuild));
1307                 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1308                 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
1309                         dev->name, dev->id,
1310                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1311                         le32_to_cpu(dev->adapter_info.biosbuild));
1312                 buffer[0] = '\0';
1313                 if (aac_show_serial_number(
1314                   shost_to_class(dev->scsi_host_ptr), buffer))
1315                         printk(KERN_INFO "%s%d: serial %s",
1316                           dev->name, dev->id, buffer);
1317                 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1318                         printk(KERN_INFO "%s%d: TSID %.*s\n",
1319                           dev->name, dev->id,
1320                           (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1321                           dev->supplement_adapter_info.VpdInfo.Tsid);
1322                 }
1323                 if (!aac_check_reset ||
1324                     ((aac_check_reset != 1) &&
1325                      (dev->supplement_adapter_info.SupportedOptions2 &
1326                       cpu_to_le32(AAC_OPTION_IGNORE_RESET)))) {
1327                         printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1328                           dev->name, dev->id);
1329                 }
1330         }
1331
1332         dev->cache_protected = 0;
1333         dev->nondasd_support = 0;
1334         dev->raid_scsi_mode = 0;
1335         if(dev->adapter_info.options & AAC_OPT_NONDASD)
1336                 dev->nondasd_support = 1;
1337
1338         /*
1339          * If the firmware supports ROMB RAID/SCSI mode and we are currently
1340          * in RAID/SCSI mode, set the flag. For now if in this mode we will
1341          * force nondasd support on. If we decide to allow the non-dasd flag
1342          * additional changes changes will have to be made to support
1343          * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
1344          * changed to support the new dev->raid_scsi_mode flag instead of
1345          * leaching off of the dev->nondasd_support flag. Also in linit.c the
1346          * function aac_detect will have to be modified where it sets up the
1347          * max number of channels based on the aac->nondasd_support flag only.
1348          */
1349         if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1350             (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1351                 dev->nondasd_support = 1;
1352                 dev->raid_scsi_mode = 1;
1353         }
1354         if (dev->raid_scsi_mode != 0)
1355                 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1356                                 dev->name, dev->id);
1357                 
1358         if (nondasd != -1)
1359                 dev->nondasd_support = (nondasd!=0);
1360         if(dev->nondasd_support != 0) {
1361                 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1362         }
1363
1364         dev->dac_support = 0;
1365         if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
1366                 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
1367                 dev->dac_support = 1;
1368         }
1369
1370         if(dacmode != -1) {
1371                 dev->dac_support = (dacmode!=0);
1372         }
1373         if(dev->dac_support != 0) {
1374                 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
1375                         !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
1376                         printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1377                                 dev->name, dev->id);
1378                 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
1379                         !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
1380                         printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1381                                 dev->name, dev->id);
1382                         dev->dac_support = 0;
1383                 } else {
1384                         printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1385                                 dev->name, dev->id);
1386                         rcode = -ENOMEM;
1387                 }
1388         }
1389         /* 
1390          * Deal with configuring for the individualized limits of each packet
1391          * interface.
1392          */
1393         dev->a_ops.adapter_scsi = (dev->dac_support)
1394           ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
1395                                 ? aac_scsi_32_64
1396                                 : aac_scsi_64)
1397                                 : aac_scsi_32;
1398         if (dev->raw_io_interface) {
1399                 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1400                                         ? aac_bounds_64
1401                                         : aac_bounds_32;
1402                 dev->a_ops.adapter_read = aac_read_raw_io;
1403                 dev->a_ops.adapter_write = aac_write_raw_io;
1404         } else {
1405                 dev->a_ops.adapter_bounds = aac_bounds_32;
1406                 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
1407                         sizeof(struct aac_fibhdr) -
1408                         sizeof(struct aac_write) + sizeof(struct sgentry)) /
1409                                 sizeof(struct sgentry);
1410                 if (dev->dac_support) {
1411                         dev->a_ops.adapter_read = aac_read_block64;
1412                         dev->a_ops.adapter_write = aac_write_block64;
1413                         /* 
1414                          * 38 scatter gather elements 
1415                          */
1416                         dev->scsi_host_ptr->sg_tablesize =
1417                                 (dev->max_fib_size -
1418                                 sizeof(struct aac_fibhdr) -
1419                                 sizeof(struct aac_write64) +
1420                                 sizeof(struct sgentry64)) /
1421                                         sizeof(struct sgentry64);
1422                 } else {
1423                         dev->a_ops.adapter_read = aac_read_block;
1424                         dev->a_ops.adapter_write = aac_write_block;
1425                 }
1426                 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1427                 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1428                         /*
1429                          * Worst case size that could cause sg overflow when
1430                          * we break up SG elements that are larger than 64KB.
1431                          * Would be nice if we could tell the SCSI layer what
1432                          * the maximum SG element size can be. Worst case is
1433                          * (sg_tablesize-1) 4KB elements with one 64KB
1434                          * element.
1435                          *      32bit -> 468 or 238KB   64bit -> 424 or 212KB
1436                          */
1437                         dev->scsi_host_ptr->max_sectors =
1438                           (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1439                 }
1440         }
1441
1442         aac_fib_complete(fibptr);
1443         aac_fib_free(fibptr);
1444
1445         return rcode;
1446 }
1447
1448
1449 static void io_callback(void *context, struct fib * fibptr)
1450 {
1451         struct aac_dev *dev;
1452         struct aac_read_reply *readreply;
1453         struct scsi_cmnd *scsicmd;
1454         u32 cid;
1455
1456         scsicmd = (struct scsi_cmnd *) context;
1457
1458         if (!aac_valid_context(scsicmd, fibptr))
1459                 return;
1460
1461         dev = fibptr->dev;
1462         cid = scmd_id(scsicmd);
1463
1464         if (nblank(dprintk(x))) {
1465                 u64 lba;
1466                 switch (scsicmd->cmnd[0]) {
1467                 case WRITE_6:
1468                 case READ_6:
1469                         lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1470                             (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1471                         break;
1472                 case WRITE_16:
1473                 case READ_16:
1474                         lba = ((u64)scsicmd->cmnd[2] << 56) |
1475                               ((u64)scsicmd->cmnd[3] << 48) |
1476                               ((u64)scsicmd->cmnd[4] << 40) |
1477                               ((u64)scsicmd->cmnd[5] << 32) |
1478                               ((u64)scsicmd->cmnd[6] << 24) |
1479                               (scsicmd->cmnd[7] << 16) |
1480                               (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1481                         break;
1482                 case WRITE_12:
1483                 case READ_12:
1484                         lba = ((u64)scsicmd->cmnd[2] << 24) |
1485                               (scsicmd->cmnd[3] << 16) |
1486                               (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1487                         break;
1488                 default:
1489                         lba = ((u64)scsicmd->cmnd[2] << 24) |
1490                                (scsicmd->cmnd[3] << 16) |
1491                                (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1492                         break;
1493                 }
1494                 printk(KERN_DEBUG
1495                   "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1496                   smp_processor_id(), (unsigned long long)lba, jiffies);
1497         }
1498
1499         BUG_ON(fibptr == NULL);
1500
1501         scsi_dma_unmap(scsicmd);
1502
1503         readreply = (struct aac_read_reply *)fib_data(fibptr);
1504         if (le32_to_cpu(readreply->status) == ST_OK)
1505                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1506         else {
1507 #ifdef AAC_DETAILED_STATUS_INFO
1508                 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
1509                   le32_to_cpu(readreply->status));
1510 #endif
1511                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1512                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1513                                     HARDWARE_ERROR,
1514                                     SENCODE_INTERNAL_TARGET_FAILURE,
1515                                     ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1516                                     0, 0);
1517                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1518                   (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1519                     ? sizeof(scsicmd->sense_buffer)
1520                     : sizeof(dev->fsa_dev[cid].sense_data));
1521         }
1522         aac_fib_complete(fibptr);
1523         aac_fib_free(fibptr);
1524
1525         scsicmd->scsi_done(scsicmd);
1526 }
1527
1528 static int aac_read(struct scsi_cmnd * scsicmd)
1529 {
1530         u64 lba;
1531         u32 count;
1532         int status;
1533         struct aac_dev *dev;
1534         struct fib * cmd_fibcontext;
1535
1536         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1537         /*
1538          *      Get block address and transfer length
1539          */
1540         switch (scsicmd->cmnd[0]) {
1541         case READ_6:
1542                 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
1543
1544                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | 
1545                         (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1546                 count = scsicmd->cmnd[4];
1547
1548                 if (count == 0)
1549                         count = 256;
1550                 break;
1551         case READ_16:
1552                 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
1553
1554                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
1555                         ((u64)scsicmd->cmnd[3] << 48) |
1556                         ((u64)scsicmd->cmnd[4] << 40) |
1557                         ((u64)scsicmd->cmnd[5] << 32) |
1558                         ((u64)scsicmd->cmnd[6] << 24) | 
1559                         (scsicmd->cmnd[7] << 16) |
1560                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1561                 count = (scsicmd->cmnd[10] << 24) | 
1562                         (scsicmd->cmnd[11] << 16) |
1563                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1564                 break;
1565         case READ_12:
1566                 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
1567
1568                 lba = ((u64)scsicmd->cmnd[2] << 24) | 
1569                         (scsicmd->cmnd[3] << 16) |
1570                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1571                 count = (scsicmd->cmnd[6] << 24) | 
1572                         (scsicmd->cmnd[7] << 16) |
1573                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1574                 break;
1575         default:
1576                 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
1577
1578                 lba = ((u64)scsicmd->cmnd[2] << 24) | 
1579                         (scsicmd->cmnd[3] << 16) | 
1580                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1581                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1582                 break;
1583         }
1584         dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
1585           smp_processor_id(), (unsigned long long)lba, jiffies));
1586         if (aac_adapter_bounds(dev,scsicmd,lba))
1587                 return 0;
1588         /*
1589          *      Alocate and initialize a Fib
1590          */
1591         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1592                 return -1;
1593         }
1594
1595         status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
1596
1597         /*
1598          *      Check that the command queued to the controller
1599          */
1600         if (status == -EINPROGRESS) {
1601                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1602                 return 0;
1603         }
1604                 
1605         printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
1606         /*
1607          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1608          */
1609         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1610         scsicmd->scsi_done(scsicmd);
1611         aac_fib_complete(cmd_fibcontext);
1612         aac_fib_free(cmd_fibcontext);
1613         return 0;
1614 }
1615
1616 static int aac_write(struct scsi_cmnd * scsicmd)
1617 {
1618         u64 lba;
1619         u32 count;
1620         int fua;
1621         int status;
1622         struct aac_dev *dev;
1623         struct fib * cmd_fibcontext;
1624
1625         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1626         /*
1627          *      Get block address and transfer length
1628          */
1629         if (scsicmd->cmnd[0] == WRITE_6)        /* 6 byte command */
1630         {
1631                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1632                 count = scsicmd->cmnd[4];
1633                 if (count == 0)
1634                         count = 256;
1635                 fua = 0;
1636         } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1637                 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
1638
1639                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
1640                         ((u64)scsicmd->cmnd[3] << 48) |
1641                         ((u64)scsicmd->cmnd[4] << 40) |
1642                         ((u64)scsicmd->cmnd[5] << 32) |
1643                         ((u64)scsicmd->cmnd[6] << 24) | 
1644                         (scsicmd->cmnd[7] << 16) |
1645                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1646                 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1647                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1648                 fua = scsicmd->cmnd[1] & 0x8;
1649         } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
1650                 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
1651
1652                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1653                     | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1654                 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1655                       | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1656                 fua = scsicmd->cmnd[1] & 0x8;
1657         } else {
1658                 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
1659                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1660                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1661                 fua = scsicmd->cmnd[1] & 0x8;
1662         }
1663         dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
1664           smp_processor_id(), (unsigned long long)lba, jiffies));
1665         if (aac_adapter_bounds(dev,scsicmd,lba))
1666                 return 0;
1667         /*
1668          *      Allocate and initialize a Fib then setup a BlockWrite command
1669          */
1670         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1671                 scsicmd->result = DID_ERROR << 16;
1672                 scsicmd->scsi_done(scsicmd);
1673                 return 0;
1674         }
1675
1676         status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
1677
1678         /*
1679          *      Check that the command queued to the controller
1680          */
1681         if (status == -EINPROGRESS) {
1682                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1683                 return 0;
1684         }
1685
1686         printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
1687         /*
1688          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1689          */
1690         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1691         scsicmd->scsi_done(scsicmd);
1692
1693         aac_fib_complete(cmd_fibcontext);
1694         aac_fib_free(cmd_fibcontext);
1695         return 0;
1696 }
1697
1698 static void synchronize_callback(void *context, struct fib *fibptr)
1699 {
1700         struct aac_synchronize_reply *synchronizereply;
1701         struct scsi_cmnd *cmd;
1702
1703         cmd = context;
1704
1705         if (!aac_valid_context(cmd, fibptr))
1706                 return;
1707
1708         dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1709                                 smp_processor_id(), jiffies));
1710         BUG_ON(fibptr == NULL);
1711
1712
1713         synchronizereply = fib_data(fibptr);
1714         if (le32_to_cpu(synchronizereply->status) == CT_OK)
1715                 cmd->result = DID_OK << 16 |
1716                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1717         else {
1718                 struct scsi_device *sdev = cmd->device;
1719                 struct aac_dev *dev = fibptr->dev;
1720                 u32 cid = sdev_id(sdev);
1721                 printk(KERN_WARNING
1722                      "synchronize_callback: synchronize failed, status = %d\n",
1723                      le32_to_cpu(synchronizereply->status));
1724                 cmd->result = DID_OK << 16 |
1725                         COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1726                 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1727                                     HARDWARE_ERROR,
1728                                     SENCODE_INTERNAL_TARGET_FAILURE,
1729                                     ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1730                                     0, 0);
1731                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1732                   min(sizeof(dev->fsa_dev[cid].sense_data),
1733                           sizeof(cmd->sense_buffer)));
1734         }
1735
1736         aac_fib_complete(fibptr);
1737         aac_fib_free(fibptr);
1738         cmd->scsi_done(cmd);
1739 }
1740
1741 static int aac_synchronize(struct scsi_cmnd *scsicmd)
1742 {
1743         int status;
1744         struct fib *cmd_fibcontext;
1745         struct aac_synchronize *synchronizecmd;
1746         struct scsi_cmnd *cmd;
1747         struct scsi_device *sdev = scsicmd->device;
1748         int active = 0;
1749         struct aac_dev *aac;
1750         u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
1751                 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1752         u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1753         unsigned long flags;
1754
1755         /*
1756          * Wait for all outstanding queued commands to complete to this
1757          * specific target (block).
1758          */
1759         spin_lock_irqsave(&sdev->list_lock, flags);
1760         list_for_each_entry(cmd, &sdev->cmd_list, list)
1761                 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
1762                         u64 cmnd_lba;
1763                         u32 cmnd_count;
1764
1765                         if (cmd->cmnd[0] == WRITE_6) {
1766                                 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
1767                                         (cmd->cmnd[2] << 8) |
1768                                         cmd->cmnd[3];
1769                                 cmnd_count = cmd->cmnd[4];
1770                                 if (cmnd_count == 0)
1771                                         cmnd_count = 256;
1772                         } else if (cmd->cmnd[0] == WRITE_16) {
1773                                 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
1774                                         ((u64)cmd->cmnd[3] << 48) |
1775                                         ((u64)cmd->cmnd[4] << 40) |
1776                                         ((u64)cmd->cmnd[5] << 32) |
1777                                         ((u64)cmd->cmnd[6] << 24) |
1778                                         (cmd->cmnd[7] << 16) |
1779                                         (cmd->cmnd[8] << 8) |
1780                                         cmd->cmnd[9];
1781                                 cmnd_count = (cmd->cmnd[10] << 24) |
1782                                         (cmd->cmnd[11] << 16) |
1783                                         (cmd->cmnd[12] << 8) |
1784                                         cmd->cmnd[13];
1785                         } else if (cmd->cmnd[0] == WRITE_12) {
1786                                 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1787                                         (cmd->cmnd[3] << 16) |
1788                                         (cmd->cmnd[4] << 8) |
1789                                         cmd->cmnd[5];
1790                                 cmnd_count = (cmd->cmnd[6] << 24) |
1791                                         (cmd->cmnd[7] << 16) |
1792                                         (cmd->cmnd[8] << 8) |
1793                                         cmd->cmnd[9];
1794                         } else if (cmd->cmnd[0] == WRITE_10) {
1795                                 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
1796                                         (cmd->cmnd[3] << 16) |
1797                                         (cmd->cmnd[4] << 8) |
1798                                         cmd->cmnd[5];
1799                                 cmnd_count = (cmd->cmnd[7] << 8) |
1800                                         cmd->cmnd[8];
1801                         } else
1802                                 continue;
1803                         if (((cmnd_lba + cmnd_count) < lba) ||
1804                           (count && ((lba + count) < cmnd_lba)))
1805                                 continue;
1806                         ++active;
1807                         break;
1808                 }
1809
1810         spin_unlock_irqrestore(&sdev->list_lock, flags);
1811
1812         /*
1813          *      Yield the processor (requeue for later)
1814          */
1815         if (active)
1816                 return SCSI_MLQUEUE_DEVICE_BUSY;
1817
1818         aac = (struct aac_dev *)sdev->host->hostdata;
1819         if (aac->in_reset)
1820                 return SCSI_MLQUEUE_HOST_BUSY;
1821
1822         /*
1823          *      Allocate and initialize a Fib
1824          */
1825         if (!(cmd_fibcontext = aac_fib_alloc(aac)))
1826                 return SCSI_MLQUEUE_HOST_BUSY;
1827
1828         aac_fib_init(cmd_fibcontext);
1829
1830         synchronizecmd = fib_data(cmd_fibcontext);
1831         synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1832         synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1833         synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
1834         synchronizecmd->count =
1835              cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1836
1837         /*
1838          *      Now send the Fib to the adapter
1839          */
1840         status = aac_fib_send(ContainerCommand,
1841                   cmd_fibcontext,
1842                   sizeof(struct aac_synchronize),
1843                   FsaNormal,
1844                   0, 1,
1845                   (fib_callback)synchronize_callback,
1846                   (void *)scsicmd);
1847
1848         /*
1849          *      Check that the command queued to the controller
1850          */
1851         if (status == -EINPROGRESS) {
1852                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1853                 return 0;
1854         }
1855
1856         printk(KERN_WARNING
1857                 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1858         aac_fib_complete(cmd_fibcontext);
1859         aac_fib_free(cmd_fibcontext);
1860         return SCSI_MLQUEUE_HOST_BUSY;
1861 }
1862
1863 /**
1864  *      aac_scsi_cmd()          -       Process SCSI command
1865  *      @scsicmd:               SCSI command block
1866  *
1867  *      Emulate a SCSI command and queue the required request for the
1868  *      aacraid firmware.
1869  */
1870  
1871 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1872 {
1873         u32 cid;
1874         struct Scsi_Host *host = scsicmd->device->host;
1875         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1876         struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
1877         
1878         if (fsa_dev_ptr == NULL)
1879                 return -1;
1880         /*
1881          *      If the bus, id or lun is out of range, return fail
1882          *      Test does not apply to ID 16, the pseudo id for the controller
1883          *      itself.
1884          */
1885         cid = scmd_id(scsicmd);
1886         if (cid != host->this_id) {
1887                 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
1888                         if((cid >= dev->maximum_num_containers) ||
1889                                         (scsicmd->device->lun != 0)) {
1890                                 scsicmd->result = DID_NO_CONNECT << 16;
1891                                 scsicmd->scsi_done(scsicmd);
1892                                 return 0;
1893                         }
1894
1895                         /*
1896                          *      If the target container doesn't exist, it may have
1897                          *      been newly created
1898                          */
1899                         if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1900                                 switch (scsicmd->cmnd[0]) {
1901                                 case SERVICE_ACTION_IN:
1902                                         if (!(dev->raw_io_interface) ||
1903                                             !(dev->raw_io_64) ||
1904                                             ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1905                                                 break;
1906                                 case INQUIRY:
1907                                 case READ_CAPACITY:
1908                                 case TEST_UNIT_READY:
1909                                         if (dev->in_reset)
1910                                                 return -1;
1911                                         return _aac_probe_container(scsicmd,
1912                                                         aac_probe_container_callback2);
1913                                 default:
1914                                         break;
1915                                 }
1916                         }
1917                 } else {  /* check for physical non-dasd devices */
1918                         if ((dev->nondasd_support == 1) || expose_physicals) {
1919                                 if (dev->in_reset)
1920                                         return -1;
1921                                 return aac_send_srb_fib(scsicmd);
1922                         } else {
1923                                 scsicmd->result = DID_NO_CONNECT << 16;
1924                                 scsicmd->scsi_done(scsicmd);
1925                                 return 0;
1926                         }
1927                 }
1928         }
1929         /*
1930          * else Command for the controller itself
1931          */
1932         else if ((scsicmd->cmnd[0] != INQUIRY) &&       /* only INQUIRY & TUR cmnd supported for controller */
1933                 (scsicmd->cmnd[0] != TEST_UNIT_READY)) 
1934         {
1935                 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1936                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1937                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1938                             ILLEGAL_REQUEST,
1939                             SENCODE_INVALID_COMMAND,
1940                             ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1941                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1942                   (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1943                     ? sizeof(scsicmd->sense_buffer)
1944                     : sizeof(dev->fsa_dev[cid].sense_data));
1945                 scsicmd->scsi_done(scsicmd);
1946                 return 0;
1947         }
1948
1949
1950         /* Handle commands here that don't really require going out to the adapter */
1951         switch (scsicmd->cmnd[0]) {
1952         case INQUIRY:
1953         {
1954                 struct inquiry_data inq_data;
1955
1956                 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
1957                 memset(&inq_data, 0, sizeof (struct inquiry_data));
1958
1959                 if (scsicmd->cmnd[1] & 0x1 ) {
1960                         char *arr = (char *)&inq_data;
1961
1962                         /* EVPD bit set */
1963                         arr[0] = (scmd_id(scsicmd) == host->this_id) ?
1964                           INQD_PDT_PROC : INQD_PDT_DA;
1965                         if (scsicmd->cmnd[2] == 0) {
1966                                 /* supported vital product data pages */
1967                                 arr[3] = 2;
1968                                 arr[4] = 0x0;
1969                                 arr[5] = 0x80;
1970                                 arr[1] = scsicmd->cmnd[2];
1971                                 aac_internal_transfer(scsicmd, &inq_data, 0,
1972                                   sizeof(inq_data));
1973                                 scsicmd->result = DID_OK << 16 |
1974                                   COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1975                         } else if (scsicmd->cmnd[2] == 0x80) {
1976                                 /* unit serial number page */
1977                                 arr[3] = setinqserial(dev, &arr[4],
1978                                   scmd_id(scsicmd));
1979                                 arr[1] = scsicmd->cmnd[2];
1980                                 aac_internal_transfer(scsicmd, &inq_data, 0,
1981                                   sizeof(inq_data));
1982                                 return aac_get_container_serial(scsicmd);
1983                         } else {
1984                                 /* vpd page not implemented */
1985                                 scsicmd->result = DID_OK << 16 |
1986                                   COMMAND_COMPLETE << 8 |
1987                                   SAM_STAT_CHECK_CONDITION;
1988                                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1989                                   ILLEGAL_REQUEST,
1990                                   SENCODE_INVALID_CDB_FIELD,
1991                                   ASENCODE_NO_SENSE, 0, 7, 2, 0);
1992                                 memcpy(scsicmd->sense_buffer,
1993                                   &dev->fsa_dev[cid].sense_data,
1994                                   (sizeof(dev->fsa_dev[cid].sense_data) >
1995                                     sizeof(scsicmd->sense_buffer))
1996                                        ? sizeof(scsicmd->sense_buffer)
1997                                        : sizeof(dev->fsa_dev[cid].sense_data));
1998                         }
1999                         scsicmd->scsi_done(scsicmd);
2000                         return 0;
2001                 }
2002                 inq_data.inqd_ver = 2;  /* claim compliance to SCSI-2 */
2003                 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 */
2004                 inq_data.inqd_len = 31;
2005                 /*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
2006                 inq_data.inqd_pad2= 0x32 ;       /*WBus16|Sync|CmdQue */
2007                 /*
2008                  *      Set the Vendor, Product, and Revision Level
2009                  *      see: <vendor>.c i.e. aac.c
2010                  */
2011                 if (cid == host->this_id) {
2012                         setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
2013                         inq_data.inqd_pdt = INQD_PDT_PROC;      /* Processor device */
2014                         aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
2015                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2016                         scsicmd->scsi_done(scsicmd);
2017                         return 0;
2018                 }
2019                 if (dev->in_reset)
2020                         return -1;
2021                 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
2022                 inq_data.inqd_pdt = INQD_PDT_DA;        /* Direct/random access device */
2023                 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
2024                 return aac_get_container_name(scsicmd);
2025         }
2026         case SERVICE_ACTION_IN:
2027                 if (!(dev->raw_io_interface) ||
2028                     !(dev->raw_io_64) ||
2029                     ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2030                         break;
2031         {
2032                 u64 capacity;
2033                 char cp[13];
2034
2035                 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2036                 capacity = fsa_dev_ptr[cid].size - 1;
2037                 cp[0] = (capacity >> 56) & 0xff;
2038                 cp[1] = (capacity >> 48) & 0xff;
2039                 cp[2] = (capacity >> 40) & 0xff;
2040                 cp[3] = (capacity >> 32) & 0xff;
2041                 cp[4] = (capacity >> 24) & 0xff;
2042                 cp[5] = (capacity >> 16) & 0xff;
2043                 cp[6] = (capacity >> 8) & 0xff;
2044                 cp[7] = (capacity >> 0) & 0xff;
2045                 cp[8] = 0;
2046                 cp[9] = 0;
2047                 cp[10] = 2;
2048                 cp[11] = 0;
2049                 cp[12] = 0;
2050                 aac_internal_transfer(scsicmd, cp, 0,
2051                   min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
2052                 if (sizeof(cp) < scsicmd->cmnd[13]) {
2053                         unsigned int len, offset = sizeof(cp);
2054
2055                         memset(cp, 0, offset);
2056                         do {
2057                                 len = min_t(size_t, scsicmd->cmnd[13] - offset,
2058                                                 sizeof(cp));
2059                                 aac_internal_transfer(scsicmd, cp, offset, len);
2060                         } while ((offset += len) < scsicmd->cmnd[13]);
2061                 }
2062
2063                 /* Do not cache partition table for arrays */
2064                 scsicmd->device->removable = 1;
2065
2066                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2067                 scsicmd->scsi_done(scsicmd);
2068
2069                 return 0;
2070         }
2071
2072         case READ_CAPACITY:
2073         {
2074                 u32 capacity;
2075                 char cp[8];
2076
2077                 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
2078                 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2079                         capacity = fsa_dev_ptr[cid].size - 1;
2080                 else
2081                         capacity = (u32)-1;
2082
2083                 cp[0] = (capacity >> 24) & 0xff;
2084                 cp[1] = (capacity >> 16) & 0xff;
2085                 cp[2] = (capacity >> 8) & 0xff;
2086                 cp[3] = (capacity >> 0) & 0xff;
2087                 cp[4] = 0;
2088                 cp[5] = 0;
2089                 cp[6] = 2;
2090                 cp[7] = 0;
2091                 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
2092                 /* Do not cache partition table for arrays */
2093                 scsicmd->device->removable = 1;
2094
2095                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2096                 scsicmd->scsi_done(scsicmd);
2097
2098                 return 0;
2099         }
2100
2101         case MODE_SENSE:
2102         {
2103                 char mode_buf[7];
2104                 int mode_buf_length = 4;
2105
2106                 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
2107                 mode_buf[0] = 3;        /* Mode data length */
2108                 mode_buf[1] = 0;        /* Medium type - default */
2109                 mode_buf[2] = 0;        /* Device-specific param,
2110                                            bit 8: 0/1 = write enabled/protected
2111                                            bit 4: 0/1 = FUA enabled */
2112                 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2113                         mode_buf[2] = 0x10;
2114                 mode_buf[3] = 0;        /* Block descriptor length */
2115                 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2116                   ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2117                         mode_buf[0] = 6;
2118                         mode_buf[4] = 8;
2119                         mode_buf[5] = 1;
2120                         mode_buf[6] = ((aac_cache & 6) == 2)
2121                                 ? 0 : 0x04; /* WCE */
2122                         mode_buf_length = 7;
2123                         if (mode_buf_length > scsicmd->cmnd[4])
2124                                 mode_buf_length = scsicmd->cmnd[4];
2125                 }
2126                 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
2127                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2128                 scsicmd->scsi_done(scsicmd);
2129
2130                 return 0;
2131         }
2132         case MODE_SENSE_10:
2133         {
2134                 char mode_buf[11];
2135                 int mode_buf_length = 8;
2136
2137                 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
2138                 mode_buf[0] = 0;        /* Mode data length (MSB) */
2139                 mode_buf[1] = 6;        /* Mode data length (LSB) */
2140                 mode_buf[2] = 0;        /* Medium type - default */
2141                 mode_buf[3] = 0;        /* Device-specific param,
2142                                            bit 8: 0/1 = write enabled/protected
2143                                            bit 4: 0/1 = FUA enabled */
2144                 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2145                         mode_buf[3] = 0x10;
2146                 mode_buf[4] = 0;        /* reserved */
2147                 mode_buf[5] = 0;        /* reserved */
2148                 mode_buf[6] = 0;        /* Block descriptor length (MSB) */
2149                 mode_buf[7] = 0;        /* Block descriptor length (LSB) */
2150                 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2151                   ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2152                         mode_buf[1] = 9;
2153                         mode_buf[8] = 8;
2154                         mode_buf[9] = 1;
2155                         mode_buf[10] = ((aac_cache & 6) == 2)
2156                                 ? 0 : 0x04; /* WCE */
2157                         mode_buf_length = 11;
2158                         if (mode_buf_length > scsicmd->cmnd[8])
2159                                 mode_buf_length = scsicmd->cmnd[8];
2160                 }
2161                 aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
2162
2163                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2164                 scsicmd->scsi_done(scsicmd);
2165
2166                 return 0;
2167         }
2168         case REQUEST_SENSE:
2169                 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2170                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
2171                 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
2172                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2173                 scsicmd->scsi_done(scsicmd);
2174                 return 0;
2175
2176         case ALLOW_MEDIUM_REMOVAL:
2177                 dprintk((KERN_DEBUG "LOCK command.\n"));
2178                 if (scsicmd->cmnd[4])
2179                         fsa_dev_ptr[cid].locked = 1;
2180                 else
2181                         fsa_dev_ptr[cid].locked = 0;
2182
2183                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2184                 scsicmd->scsi_done(scsicmd);
2185                 return 0;
2186         /*
2187          *      These commands are all No-Ops
2188          */
2189         case TEST_UNIT_READY:
2190         case RESERVE:
2191         case RELEASE:
2192         case REZERO_UNIT:
2193         case REASSIGN_BLOCKS:
2194         case SEEK_10:
2195         case START_STOP:
2196                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2197                 scsicmd->scsi_done(scsicmd);
2198                 return 0;
2199         }
2200
2201         switch (scsicmd->cmnd[0]) 
2202         {
2203                 case READ_6:
2204                 case READ_10:
2205                 case READ_12:
2206                 case READ_16:
2207                         if (dev->in_reset)
2208                                 return -1;
2209                         /*
2210                          *      Hack to keep track of ordinal number of the device that
2211                          *      corresponds to a container. Needed to convert
2212                          *      containers to /dev/sd device names
2213                          */
2214                          
2215                         if (scsicmd->request->rq_disk)
2216                                 strlcpy(fsa_dev_ptr[cid].devname,
2217                                 scsicmd->request->rq_disk->disk_name,
2218                                 min(sizeof(fsa_dev_ptr[cid].devname),
2219                                 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
2220
2221                         return aac_read(scsicmd);
2222
2223                 case WRITE_6:
2224                 case WRITE_10:
2225                 case WRITE_12:
2226                 case WRITE_16:
2227                         if (dev->in_reset)
2228                                 return -1;
2229                         return aac_write(scsicmd);
2230
2231                 case SYNCHRONIZE_CACHE:
2232                         if (((aac_cache & 6) == 6) && dev->cache_protected) {
2233                                 scsicmd->result = DID_OK << 16 |
2234                                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2235                                 scsicmd->scsi_done(scsicmd);
2236                                 return 0;
2237                         }
2238                         /* Issue FIB to tell Firmware to flush it's cache */
2239                         if ((aac_cache & 6) != 2)
2240                                 return aac_synchronize(scsicmd);
2241                         /* FALLTHRU */
2242                 default:
2243                         /*
2244                          *      Unhandled commands
2245                          */
2246                         dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
2247                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2248                         set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
2249                                 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2250                                 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
2251                         memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2252                           (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
2253                             ? sizeof(scsicmd->sense_buffer)
2254                             : sizeof(dev->fsa_dev[cid].sense_data));
2255                         scsicmd->scsi_done(scsicmd);
2256                         return 0;
2257         }
2258 }
2259
2260 static int query_disk(struct aac_dev *dev, void __user *arg)
2261 {
2262         struct aac_query_disk qd;
2263         struct fsa_dev_info *fsa_dev_ptr;
2264
2265         fsa_dev_ptr = dev->fsa_dev;
2266         if (!fsa_dev_ptr)
2267                 return -EBUSY;
2268         if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2269                 return -EFAULT;
2270         if (qd.cnum == -1)
2271                 qd.cnum = qd.id;
2272         else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1)) 
2273         {
2274                 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2275                         return -EINVAL;
2276                 qd.instance = dev->scsi_host_ptr->host_no;
2277                 qd.bus = 0;
2278                 qd.id = CONTAINER_TO_ID(qd.cnum);
2279                 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2280         }
2281         else return -EINVAL;
2282
2283         qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
2284         qd.locked = fsa_dev_ptr[qd.cnum].locked;
2285         qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2286
2287         if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2288                 qd.unmapped = 1;
2289         else
2290                 qd.unmapped = 0;
2291
2292         strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2293           min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2294
2295         if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2296                 return -EFAULT;
2297         return 0;
2298 }
2299
2300 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2301 {
2302         struct aac_delete_disk dd;
2303         struct fsa_dev_info *fsa_dev_ptr;
2304
2305         fsa_dev_ptr = dev->fsa_dev;
2306         if (!fsa_dev_ptr)
2307                 return -EBUSY;
2308
2309         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2310                 return -EFAULT;
2311
2312         if (dd.cnum >= dev->maximum_num_containers)
2313                 return -EINVAL;
2314         /*
2315          *      Mark this container as being deleted.
2316          */
2317         fsa_dev_ptr[dd.cnum].deleted = 1;
2318         /*
2319          *      Mark the container as no longer valid
2320          */
2321         fsa_dev_ptr[dd.cnum].valid = 0;
2322         return 0;
2323 }
2324
2325 static int delete_disk(struct aac_dev *dev, void __user *arg)
2326 {
2327         struct aac_delete_disk dd;
2328         struct fsa_dev_info *fsa_dev_ptr;
2329
2330         fsa_dev_ptr = dev->fsa_dev;
2331         if (!fsa_dev_ptr)
2332                 return -EBUSY;
2333
2334         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2335                 return -EFAULT;
2336
2337         if (dd.cnum >= dev->maximum_num_containers)
2338                 return -EINVAL;
2339         /*
2340          *      If the container is locked, it can not be deleted by the API.
2341          */
2342         if (fsa_dev_ptr[dd.cnum].locked)
2343                 return -EBUSY;
2344         else {
2345                 /*
2346                  *      Mark the container as no longer being valid.
2347                  */
2348                 fsa_dev_ptr[dd.cnum].valid = 0;
2349                 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2350                 return 0;
2351         }
2352 }
2353
2354 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2355 {
2356         switch (cmd) {
2357         case FSACTL_QUERY_DISK:
2358                 return query_disk(dev, arg);
2359         case FSACTL_DELETE_DISK:
2360                 return delete_disk(dev, arg);
2361         case FSACTL_FORCE_DELETE_DISK:
2362                 return force_delete_disk(dev, arg);
2363         case FSACTL_GET_CONTAINERS:
2364                 return aac_get_containers(dev);
2365         default:
2366                 return -ENOTTY;
2367         }
2368 }
2369
2370 /**
2371  *
2372  * aac_srb_callback
2373  * @context: the context set in the fib - here it is scsi cmd
2374  * @fibptr: pointer to the fib
2375  *
2376  * Handles the completion of a scsi command to a non dasd device
2377  *
2378  */
2379
2380 static void aac_srb_callback(void *context, struct fib * fibptr)
2381 {
2382         struct aac_dev *dev;
2383         struct aac_srb_reply *srbreply;
2384         struct scsi_cmnd *scsicmd;
2385
2386         scsicmd = (struct scsi_cmnd *) context;
2387
2388         if (!aac_valid_context(scsicmd, fibptr))
2389                 return;
2390
2391         BUG_ON(fibptr == NULL);
2392
2393         dev = fibptr->dev;
2394
2395         srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2396
2397         scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
2398         /*
2399          *      Calculate resid for sg 
2400          */
2401
2402         scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
2403                        - le32_to_cpu(srbreply->data_xfer_length));
2404
2405         scsi_dma_unmap(scsicmd);
2406
2407         /*
2408          * First check the fib status
2409          */
2410
2411         if (le32_to_cpu(srbreply->status) != ST_OK){
2412                 int len;
2413                 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2414                 len = (le32_to_cpu(srbreply->sense_data_size) > 
2415                                 sizeof(scsicmd->sense_buffer)) ?
2416                                 sizeof(scsicmd->sense_buffer) : 
2417                                 le32_to_cpu(srbreply->sense_data_size);
2418                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2419                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2420         }
2421
2422         /*
2423          * Next check the srb status
2424          */
2425         switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2426         case SRB_STATUS_ERROR_RECOVERY:
2427         case SRB_STATUS_PENDING:
2428         case SRB_STATUS_SUCCESS:
2429                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2430                 break;
2431         case SRB_STATUS_DATA_OVERRUN:
2432                 switch(scsicmd->cmnd[0]){
2433                 case  READ_6:
2434                 case  WRITE_6:
2435                 case  READ_10:
2436                 case  WRITE_10:
2437                 case  READ_12:
2438                 case  WRITE_12:
2439                 case  READ_16:
2440                 case  WRITE_16:
2441                         if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
2442                                 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2443                         } else {
2444                                 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2445                         }
2446                         scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2447                         break;
2448                 case INQUIRY: {
2449                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2450                         break;
2451                 }
2452                 default:
2453                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2454                         break;
2455                 }
2456                 break;
2457         case SRB_STATUS_ABORTED:
2458                 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2459                 break;
2460         case SRB_STATUS_ABORT_FAILED:
2461                 // Not sure about this one - but assuming the hba was trying to abort for some reason
2462                 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2463                 break;
2464         case SRB_STATUS_PARITY_ERROR:
2465                 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2466                 break;
2467         case SRB_STATUS_NO_DEVICE:
2468         case SRB_STATUS_INVALID_PATH_ID:
2469         case SRB_STATUS_INVALID_TARGET_ID:
2470         case SRB_STATUS_INVALID_LUN:
2471         case SRB_STATUS_SELECTION_TIMEOUT:
2472                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2473                 break;
2474
2475         case SRB_STATUS_COMMAND_TIMEOUT:
2476         case SRB_STATUS_TIMEOUT:
2477                 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2478                 break;
2479
2480         case SRB_STATUS_BUSY:
2481                 scsicmd->result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
2482                 break;
2483
2484         case SRB_STATUS_BUS_RESET:
2485                 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2486                 break;
2487
2488         case SRB_STATUS_MESSAGE_REJECTED:
2489                 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2490                 break;
2491         case SRB_STATUS_REQUEST_FLUSHED:
2492         case SRB_STATUS_ERROR:
2493         case SRB_STATUS_INVALID_REQUEST:
2494         case SRB_STATUS_REQUEST_SENSE_FAILED:
2495         case SRB_STATUS_NO_HBA:
2496         case SRB_STATUS_UNEXPECTED_BUS_FREE:
2497         case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2498         case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2499         case SRB_STATUS_DELAYED_RETRY:
2500         case SRB_STATUS_BAD_FUNCTION:
2501         case SRB_STATUS_NOT_STARTED:
2502         case SRB_STATUS_NOT_IN_USE:
2503         case SRB_STATUS_FORCE_ABORT:
2504         case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2505         default:
2506 #ifdef AAC_DETAILED_STATUS_INFO
2507                 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2508                         le32_to_cpu(srbreply->srb_status) & 0x3F,
2509                         aac_get_status_string(
2510                                 le32_to_cpu(srbreply->srb_status) & 0x3F), 
2511                         scsicmd->cmnd[0], 
2512                         le32_to_cpu(srbreply->scsi_status));
2513 #endif
2514                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2515                 break;
2516         }
2517         if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){  // Check Condition
2518                 int len;
2519                 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2520                 len = (le32_to_cpu(srbreply->sense_data_size) > 
2521                                 sizeof(scsicmd->sense_buffer)) ?
2522                                 sizeof(scsicmd->sense_buffer) :
2523                                 le32_to_cpu(srbreply->sense_data_size);
2524 #ifdef AAC_DETAILED_STATUS_INFO
2525                 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2526                                         le32_to_cpu(srbreply->status), len);
2527 #endif
2528                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2529                 
2530         }
2531         /*
2532          * OR in the scsi status (already shifted up a bit)
2533          */
2534         scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2535
2536         aac_fib_complete(fibptr);
2537         aac_fib_free(fibptr);
2538         scsicmd->scsi_done(scsicmd);
2539 }
2540
2541 /**
2542  *
2543  * aac_send_scb_fib
2544  * @scsicmd: the scsi command block
2545  *
2546  * This routine will form a FIB and fill in the aac_srb from the 
2547  * scsicmd passed in.
2548  */
2549
2550 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2551 {
2552         struct fib* cmd_fibcontext;
2553         struct aac_dev* dev;
2554         int status;
2555
2556         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2557         if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
2558                         scsicmd->device->lun > 7) {
2559                 scsicmd->result = DID_NO_CONNECT << 16;
2560                 scsicmd->scsi_done(scsicmd);
2561                 return 0;
2562         }
2563
2564         /*
2565          *      Allocate and initialize a Fib then setup a BlockWrite command
2566          */
2567         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
2568                 return -1;
2569         }
2570         status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
2571
2572         /*
2573          *      Check that the command queued to the controller
2574          */
2575         if (status == -EINPROGRESS) {
2576                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2577                 return 0;
2578         }
2579
2580         printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2581         aac_fib_complete(cmd_fibcontext);
2582         aac_fib_free(cmd_fibcontext);
2583
2584         return -1;
2585 }
2586
2587 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2588 {
2589         struct aac_dev *dev;
2590         unsigned long byte_count = 0;
2591         int nseg;
2592
2593         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2594         // Get rid of old data
2595         psg->count = 0;
2596         psg->sg[0].addr = 0;
2597         psg->sg[0].count = 0;
2598
2599         nseg = scsi_dma_map(scsicmd);
2600         BUG_ON(nseg < 0);
2601         if (nseg) {
2602                 struct scatterlist *sg;
2603                 int i;
2604
2605                 psg->count = cpu_to_le32(nseg);
2606
2607                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
2608                         psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2609                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2610                         byte_count += sg_dma_len(sg);
2611                 }
2612                 /* hba wants the size to be exact */
2613                 if (byte_count > scsi_bufflen(scsicmd)) {
2614                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2615                                 (byte_count - scsi_bufflen(scsicmd));
2616                         psg->sg[i-1].count = cpu_to_le32(temp);
2617                         byte_count = scsi_bufflen(scsicmd);
2618                 }
2619                 /* Check for command underflow */
2620                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2621                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2622                                         byte_count, scsicmd->underflow);
2623                 }
2624         }
2625         return byte_count;
2626 }
2627
2628
2629 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2630 {
2631         struct aac_dev *dev;
2632         unsigned long byte_count = 0;
2633         u64 addr;
2634         int nseg;
2635
2636         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2637         // Get rid of old data
2638         psg->count = 0;
2639         psg->sg[0].addr[0] = 0;
2640         psg->sg[0].addr[1] = 0;
2641         psg->sg[0].count = 0;
2642
2643         nseg = scsi_dma_map(scsicmd);
2644         BUG_ON(nseg < 0);
2645         if (nseg) {
2646                 struct scatterlist *sg;
2647                 int i;
2648
2649                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
2650                         int count = sg_dma_len(sg);
2651                         addr = sg_dma_address(sg);
2652                         psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2653                         psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
2654                         psg->sg[i].count = cpu_to_le32(count);
2655                         byte_count += count;
2656                 }
2657                 psg->count = cpu_to_le32(nseg);
2658                 /* hba wants the size to be exact */
2659                 if (byte_count > scsi_bufflen(scsicmd)) {
2660                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2661                                 (byte_count - scsi_bufflen(scsicmd));
2662                         psg->sg[i-1].count = cpu_to_le32(temp);
2663                         byte_count = scsi_bufflen(scsicmd);
2664                 }
2665                 /* Check for command underflow */
2666                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2667                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2668                                         byte_count, scsicmd->underflow);
2669                 }
2670         }
2671         return byte_count;
2672 }
2673
2674 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2675 {
2676         unsigned long byte_count = 0;
2677         int nseg;
2678
2679         // Get rid of old data
2680         psg->count = 0;
2681         psg->sg[0].next = 0;
2682         psg->sg[0].prev = 0;
2683         psg->sg[0].addr[0] = 0;
2684         psg->sg[0].addr[1] = 0;
2685         psg->sg[0].count = 0;
2686         psg->sg[0].flags = 0;
2687
2688         nseg = scsi_dma_map(scsicmd);
2689         BUG_ON(nseg < 0);
2690         if (nseg) {
2691                 struct scatterlist *sg;
2692                 int i;
2693
2694                 scsi_for_each_sg(scsicmd, sg, nseg, i) {
2695                         int count = sg_dma_len(sg);
2696                         u64 addr = sg_dma_address(sg);
2697                         psg->sg[i].next = 0;
2698                         psg->sg[i].prev = 0;
2699                         psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2700                         psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2701                         psg->sg[i].count = cpu_to_le32(count);
2702                         psg->sg[i].flags = 0;
2703                         byte_count += count;
2704                 }
2705                 psg->count = cpu_to_le32(nseg);
2706                 /* hba wants the size to be exact */
2707                 if (byte_count > scsi_bufflen(scsicmd)) {
2708                         u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2709                                 (byte_count - scsi_bufflen(scsicmd));
2710                         psg->sg[i-1].count = cpu_to_le32(temp);
2711                         byte_count = scsi_bufflen(scsicmd);
2712                 }
2713                 /* Check for command underflow */
2714                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2715                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2716                                         byte_count, scsicmd->underflow);
2717                 }
2718         }
2719         return byte_count;
2720 }
2721
2722 #ifdef AAC_DETAILED_STATUS_INFO
2723
2724 struct aac_srb_status_info {
2725         u32     status;
2726         char    *str;
2727 };
2728
2729
2730 static struct aac_srb_status_info srb_status_info[] = {
2731         { SRB_STATUS_PENDING,           "Pending Status"},
2732         { SRB_STATUS_SUCCESS,           "Success"},
2733         { SRB_STATUS_ABORTED,           "Aborted Command"},
2734         { SRB_STATUS_ABORT_FAILED,      "Abort Failed"},
2735         { SRB_STATUS_ERROR,             "Error Event"},
2736         { SRB_STATUS_BUSY,              "Device Busy"},
2737         { SRB_STATUS_INVALID_REQUEST,   "Invalid Request"},
2738         { SRB_STATUS_INVALID_PATH_ID,   "Invalid Path ID"},
2739         { SRB_STATUS_NO_DEVICE,         "No Device"},
2740         { SRB_STATUS_TIMEOUT,           "Timeout"},
2741         { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2742         { SRB_STATUS_COMMAND_TIMEOUT,   "Command Timeout"},
2743         { SRB_STATUS_MESSAGE_REJECTED,  "Message Rejected"},
2744         { SRB_STATUS_BUS_RESET,         "Bus Reset"},
2745         { SRB_STATUS_PARITY_ERROR,      "Parity Error"},
2746         { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2747         { SRB_STATUS_NO_HBA,            "No HBA"},
2748         { SRB_STATUS_DATA_OVERRUN,      "Data Overrun/Data Underrun"},
2749         { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2750         { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2751         { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2752         { SRB_STATUS_REQUEST_FLUSHED,   "Request Flushed"},
2753         { SRB_STATUS_DELAYED_RETRY,     "Delayed Retry"},
2754         { SRB_STATUS_INVALID_LUN,       "Invalid LUN"},
2755         { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2756         { SRB_STATUS_BAD_FUNCTION,      "Bad Function"},
2757         { SRB_STATUS_ERROR_RECOVERY,    "Error Recovery"},
2758         { SRB_STATUS_NOT_STARTED,       "Not Started"},
2759         { SRB_STATUS_NOT_IN_USE,        "Not In Use"},
2760         { SRB_STATUS_FORCE_ABORT,       "Force Abort"},
2761         { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2762         { 0xff,                         "Unknown Error"}
2763 };
2764
2765 char *aac_get_status_string(u32 status)
2766 {
2767         int i;
2768
2769         for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2770                 if (srb_status_info[i].status == status)
2771                         return srb_status_info[i].str;
2772
2773         return "Bad Status Code";
2774 }
2775
2776 #endif