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