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