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