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