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