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