Merge git://oss.sgi.com:8090/xfs/xfs-2.6
[linux-drm-fsl-dcu.git] / drivers / scsi / aacraid / aachba.c
1 /*
2  *      Adaptec AAC series RAID controller driver
3  *      (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000 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 = -1;
174 module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
175 MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays. -1=protect 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 = 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 static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
710 {
711         if (lba & 0xffffffff00000000LL) {
712                 int cid = scmd_id(cmd);
713                 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
714                 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
715                         SAM_STAT_CHECK_CONDITION;
716                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
717                             HARDWARE_ERROR,
718                             SENCODE_INTERNAL_TARGET_FAILURE,
719                             ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
720                             0, 0);
721                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
722                   (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(cmd->sense_buffer))
723                     ? sizeof(cmd->sense_buffer)
724                     : sizeof(dev->fsa_dev[cid].sense_data));
725                 cmd->scsi_done(cmd);
726                 return 1;
727         }
728         return 0;
729 }
730
731 static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
732 {
733         return 0;
734 }
735
736 static void io_callback(void *context, struct fib * fibptr);
737
738 static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
739 {
740         u16 fibsize;
741         struct aac_raw_io *readcmd;
742         aac_fib_init(fib);
743         readcmd = (struct aac_raw_io *) fib_data(fib);
744         readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
745         readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
746         readcmd->count = cpu_to_le32(count<<9);
747         readcmd->cid = cpu_to_le16(scmd_id(cmd));
748         readcmd->flags = cpu_to_le16(1);
749         readcmd->bpTotal = 0;
750         readcmd->bpComplete = 0;
751
752         aac_build_sgraw(cmd, &readcmd->sg);
753         fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
754         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
755         /*
756          *      Now send the Fib to the adapter
757          */
758         return aac_fib_send(ContainerRawIo,
759                           fib,
760                           fibsize,
761                           FsaNormal,
762                           0, 1,
763                           (fib_callback) io_callback,
764                           (void *) cmd);
765 }
766
767 static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
768 {
769         u16 fibsize;
770         struct aac_read64 *readcmd;
771         aac_fib_init(fib);
772         readcmd = (struct aac_read64 *) fib_data(fib);
773         readcmd->command = cpu_to_le32(VM_CtHostRead64);
774         readcmd->cid = cpu_to_le16(scmd_id(cmd));
775         readcmd->sector_count = cpu_to_le16(count);
776         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
777         readcmd->pad   = 0;
778         readcmd->flags = 0;
779
780         aac_build_sg64(cmd, &readcmd->sg);
781         fibsize = sizeof(struct aac_read64) +
782                 ((le32_to_cpu(readcmd->sg.count) - 1) *
783                  sizeof (struct sgentry64));
784         BUG_ON (fibsize > (fib->dev->max_fib_size -
785                                 sizeof(struct aac_fibhdr)));
786         /*
787          *      Now send the Fib to the adapter
788          */
789         return aac_fib_send(ContainerCommand64,
790                           fib,
791                           fibsize,
792                           FsaNormal,
793                           0, 1,
794                           (fib_callback) io_callback,
795                           (void *) cmd);
796 }
797
798 static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
799 {
800         u16 fibsize;
801         struct aac_read *readcmd;
802         aac_fib_init(fib);
803         readcmd = (struct aac_read *) fib_data(fib);
804         readcmd->command = cpu_to_le32(VM_CtBlockRead);
805         readcmd->cid = cpu_to_le16(scmd_id(cmd));
806         readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
807         readcmd->count = cpu_to_le32(count * 512);
808
809         aac_build_sg(cmd, &readcmd->sg);
810         fibsize = sizeof(struct aac_read) +
811                         ((le32_to_cpu(readcmd->sg.count) - 1) *
812                          sizeof (struct sgentry));
813         BUG_ON (fibsize > (fib->dev->max_fib_size -
814                                 sizeof(struct aac_fibhdr)));
815         /*
816          *      Now send the Fib to the adapter
817          */
818         return aac_fib_send(ContainerCommand,
819                           fib,
820                           fibsize,
821                           FsaNormal,
822                           0, 1,
823                           (fib_callback) io_callback,
824                           (void *) cmd);
825 }
826
827 static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
828 {
829         u16 fibsize;
830         struct aac_raw_io *writecmd;
831         aac_fib_init(fib);
832         writecmd = (struct aac_raw_io *) fib_data(fib);
833         writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
834         writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
835         writecmd->count = cpu_to_le32(count<<9);
836         writecmd->cid = cpu_to_le16(scmd_id(cmd));
837         writecmd->flags = 0;
838         writecmd->bpTotal = 0;
839         writecmd->bpComplete = 0;
840
841         aac_build_sgraw(cmd, &writecmd->sg);
842         fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
843         BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
844         /*
845          *      Now send the Fib to the adapter
846          */
847         return aac_fib_send(ContainerRawIo,
848                           fib,
849                           fibsize,
850                           FsaNormal,
851                           0, 1,
852                           (fib_callback) io_callback,
853                           (void *) cmd);
854 }
855
856 static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
857 {
858         u16 fibsize;
859         struct aac_write64 *writecmd;
860         aac_fib_init(fib);
861         writecmd = (struct aac_write64 *) fib_data(fib);
862         writecmd->command = cpu_to_le32(VM_CtHostWrite64);
863         writecmd->cid = cpu_to_le16(scmd_id(cmd));
864         writecmd->sector_count = cpu_to_le16(count);
865         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
866         writecmd->pad   = 0;
867         writecmd->flags = 0;
868
869         aac_build_sg64(cmd, &writecmd->sg);
870         fibsize = sizeof(struct aac_write64) +
871                 ((le32_to_cpu(writecmd->sg.count) - 1) *
872                  sizeof (struct sgentry64));
873         BUG_ON (fibsize > (fib->dev->max_fib_size -
874                                 sizeof(struct aac_fibhdr)));
875         /*
876          *      Now send the Fib to the adapter
877          */
878         return aac_fib_send(ContainerCommand64,
879                           fib,
880                           fibsize,
881                           FsaNormal,
882                           0, 1,
883                           (fib_callback) io_callback,
884                           (void *) cmd);
885 }
886
887 static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
888 {
889         u16 fibsize;
890         struct aac_write *writecmd;
891         aac_fib_init(fib);
892         writecmd = (struct aac_write *) fib_data(fib);
893         writecmd->command = cpu_to_le32(VM_CtBlockWrite);
894         writecmd->cid = cpu_to_le16(scmd_id(cmd));
895         writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
896         writecmd->count = cpu_to_le32(count * 512);
897         writecmd->sg.count = cpu_to_le32(1);
898         /* ->stable is not used - it did mean which type of write */
899
900         aac_build_sg(cmd, &writecmd->sg);
901         fibsize = sizeof(struct aac_write) +
902                 ((le32_to_cpu(writecmd->sg.count) - 1) *
903                  sizeof (struct sgentry));
904         BUG_ON (fibsize > (fib->dev->max_fib_size -
905                                 sizeof(struct aac_fibhdr)));
906         /*
907          *      Now send the Fib to the adapter
908          */
909         return aac_fib_send(ContainerCommand,
910                           fib,
911                           fibsize,
912                           FsaNormal,
913                           0, 1,
914                           (fib_callback) io_callback,
915                           (void *) cmd);
916 }
917
918 static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
919 {
920         struct aac_srb * srbcmd;
921         u32 flag;
922         u32 timeout;
923
924         aac_fib_init(fib);
925         switch(cmd->sc_data_direction){
926         case DMA_TO_DEVICE:
927                 flag = SRB_DataOut;
928                 break;
929         case DMA_BIDIRECTIONAL:
930                 flag = SRB_DataIn | SRB_DataOut;
931                 break;
932         case DMA_FROM_DEVICE:
933                 flag = SRB_DataIn;
934                 break;
935         case DMA_NONE:
936         default:        /* shuts up some versions of gcc */
937                 flag = SRB_NoDataXfer;
938                 break;
939         }
940
941         srbcmd = (struct aac_srb*) fib_data(fib);
942         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
943         srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
944         srbcmd->id       = cpu_to_le32(scmd_id(cmd));
945         srbcmd->lun      = cpu_to_le32(cmd->device->lun);
946         srbcmd->flags    = cpu_to_le32(flag);
947         timeout = cmd->timeout_per_command/HZ;
948         if (timeout == 0)
949                 timeout = 1;
950         srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
951         srbcmd->retry_limit = 0; /* Obsolete parameter */
952         srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
953         return srbcmd;
954 }
955
956 static void aac_srb_callback(void *context, struct fib * fibptr);
957
958 static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
959 {
960         u16 fibsize;
961         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
962
963         aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
964         srbcmd->count = cpu_to_le32(cmd->request_bufflen);
965
966         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
967         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
968         /*
969          *      Build Scatter/Gather list
970          */
971         fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
972                 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
973                  sizeof (struct sgentry64));
974         BUG_ON (fibsize > (fib->dev->max_fib_size -
975                                 sizeof(struct aac_fibhdr)));
976
977         /*
978          *      Now send the Fib to the adapter
979          */
980         return aac_fib_send(ScsiPortCommand64, fib,
981                                 fibsize, FsaNormal, 0, 1,
982                                   (fib_callback) aac_srb_callback,
983                                   (void *) cmd);
984 }
985
986 static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
987 {
988         u16 fibsize;
989         struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
990
991         aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
992         srbcmd->count = cpu_to_le32(cmd->request_bufflen);
993
994         memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
995         memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
996         /*
997          *      Build Scatter/Gather list
998          */
999         fibsize = sizeof (struct aac_srb) +
1000                 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1001                  sizeof (struct sgentry));
1002         BUG_ON (fibsize > (fib->dev->max_fib_size -
1003                                 sizeof(struct aac_fibhdr)));
1004
1005         /*
1006          *      Now send the Fib to the adapter
1007          */
1008         return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1009                                   (fib_callback) aac_srb_callback, (void *) cmd);
1010 }
1011
1012 int aac_get_adapter_info(struct aac_dev* dev)
1013 {
1014         struct fib* fibptr;
1015         int rcode;
1016         u32 tmp;
1017         struct aac_adapter_info *info;
1018         struct aac_bus_info *command;
1019         struct aac_bus_info_response *bus_info;
1020
1021         if (!(fibptr = aac_fib_alloc(dev)))
1022                 return -ENOMEM;
1023
1024         aac_fib_init(fibptr);
1025         info = (struct aac_adapter_info *) fib_data(fibptr);
1026         memset(info,0,sizeof(*info));
1027
1028         rcode = aac_fib_send(RequestAdapterInfo,
1029                          fibptr, 
1030                          sizeof(*info),
1031                          FsaNormal, 
1032                          -1, 1, /* First `interrupt' command uses special wait */
1033                          NULL, 
1034                          NULL);
1035
1036         if (rcode < 0) {
1037                 aac_fib_complete(fibptr);
1038                 aac_fib_free(fibptr);
1039                 return rcode;
1040         }
1041         memcpy(&dev->adapter_info, info, sizeof(*info));
1042
1043         if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1044                 struct aac_supplement_adapter_info * info;
1045
1046                 aac_fib_init(fibptr);
1047
1048                 info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1049
1050                 memset(info,0,sizeof(*info));
1051
1052                 rcode = aac_fib_send(RequestSupplementAdapterInfo,
1053                                  fibptr,
1054                                  sizeof(*info),
1055                                  FsaNormal,
1056                                  1, 1,
1057                                  NULL,
1058                                  NULL);
1059
1060                 if (rcode >= 0)
1061                         memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
1062         }
1063
1064
1065         /* 
1066          * GetBusInfo 
1067          */
1068
1069         aac_fib_init(fibptr);
1070
1071         bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1072
1073         memset(bus_info, 0, sizeof(*bus_info));
1074
1075         command = (struct aac_bus_info *)bus_info;
1076
1077         command->Command = cpu_to_le32(VM_Ioctl);
1078         command->ObjType = cpu_to_le32(FT_DRIVE);
1079         command->MethodId = cpu_to_le32(1);
1080         command->CtlCmd = cpu_to_le32(GetBusInfo);
1081
1082         rcode = aac_fib_send(ContainerCommand,
1083                          fibptr,
1084                          sizeof (*bus_info),
1085                          FsaNormal,
1086                          1, 1,
1087                          NULL, NULL);
1088
1089         if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1090                 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1091                 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1092         }
1093
1094         if (!dev->in_reset) {
1095                 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1096                 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
1097                         dev->name, 
1098                         dev->id,
1099                         tmp>>24,
1100                         (tmp>>16)&0xff,
1101                         tmp&0xff,
1102                         le32_to_cpu(dev->adapter_info.kernelbuild),
1103                         (int)sizeof(dev->supplement_adapter_info.BuildDate),
1104                         dev->supplement_adapter_info.BuildDate);
1105                 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1106                 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
1107                         dev->name, dev->id,
1108                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1109                         le32_to_cpu(dev->adapter_info.monitorbuild));
1110                 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1111                 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
1112                         dev->name, dev->id,
1113                         tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1114                         le32_to_cpu(dev->adapter_info.biosbuild));
1115                 if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
1116                         printk(KERN_INFO "%s%d: serial %x\n",
1117                                 dev->name, dev->id,
1118                                 le32_to_cpu(dev->adapter_info.serial[0]));
1119         }
1120
1121         dev->nondasd_support = 0;
1122         dev->raid_scsi_mode = 0;
1123         if(dev->adapter_info.options & AAC_OPT_NONDASD){
1124                 dev->nondasd_support = 1;
1125         }
1126
1127         /*
1128          * If the firmware supports ROMB RAID/SCSI mode and we are currently
1129          * in RAID/SCSI mode, set the flag. For now if in this mode we will
1130          * force nondasd support on. If we decide to allow the non-dasd flag
1131          * additional changes changes will have to be made to support
1132          * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
1133          * changed to support the new dev->raid_scsi_mode flag instead of
1134          * leaching off of the dev->nondasd_support flag. Also in linit.c the
1135          * function aac_detect will have to be modified where it sets up the
1136          * max number of channels based on the aac->nondasd_support flag only.
1137          */
1138         if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1139             (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1140                 dev->nondasd_support = 1;
1141                 dev->raid_scsi_mode = 1;
1142         }
1143         if (dev->raid_scsi_mode != 0)
1144                 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1145                                 dev->name, dev->id);
1146                 
1147         if(nondasd != -1) {  
1148                 dev->nondasd_support = (nondasd!=0);
1149         }
1150         if(dev->nondasd_support != 0){
1151                 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1152         }
1153
1154         dev->dac_support = 0;
1155         if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
1156                 printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
1157                 dev->dac_support = 1;
1158         }
1159
1160         if(dacmode != -1) {
1161                 dev->dac_support = (dacmode!=0);
1162         }
1163         if(dev->dac_support != 0) {
1164                 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
1165                         !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
1166                         printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1167                                 dev->name, dev->id);
1168                 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
1169                         !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
1170                         printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1171                                 dev->name, dev->id);
1172                         dev->dac_support = 0;
1173                 } else {
1174                         printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1175                                 dev->name, dev->id);
1176                         rcode = -ENOMEM;
1177                 }
1178         }
1179         /* 
1180          * Deal with configuring for the individualized limits of each packet
1181          * interface.
1182          */
1183         dev->a_ops.adapter_scsi = (dev->dac_support)
1184                                 ? aac_scsi_64
1185                                 : aac_scsi_32;
1186         if (dev->raw_io_interface) {
1187                 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1188                                         ? aac_bounds_64
1189                                         : aac_bounds_32;
1190                 dev->a_ops.adapter_read = aac_read_raw_io;
1191                 dev->a_ops.adapter_write = aac_write_raw_io;
1192         } else {
1193                 dev->a_ops.adapter_bounds = aac_bounds_32;
1194                 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
1195                         sizeof(struct aac_fibhdr) -
1196                         sizeof(struct aac_write) + sizeof(struct sgentry)) /
1197                                 sizeof(struct sgentry);
1198                 if (dev->dac_support) {
1199                         dev->a_ops.adapter_read = aac_read_block64;
1200                         dev->a_ops.adapter_write = aac_write_block64;
1201                         /* 
1202                          * 38 scatter gather elements 
1203                          */
1204                         dev->scsi_host_ptr->sg_tablesize =
1205                                 (dev->max_fib_size -
1206                                 sizeof(struct aac_fibhdr) -
1207                                 sizeof(struct aac_write64) +
1208                                 sizeof(struct sgentry64)) /
1209                                         sizeof(struct sgentry64);
1210                 } else {
1211                         dev->a_ops.adapter_read = aac_read_block;
1212                         dev->a_ops.adapter_write = aac_write_block;
1213                 }
1214                 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1215                 if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1216                         /*
1217                          * Worst case size that could cause sg overflow when
1218                          * we break up SG elements that are larger than 64KB.
1219                          * Would be nice if we could tell the SCSI layer what
1220                          * the maximum SG element size can be. Worst case is
1221                          * (sg_tablesize-1) 4KB elements with one 64KB
1222                          * element.
1223                          *      32bit -> 468 or 238KB   64bit -> 424 or 212KB
1224                          */
1225                         dev->scsi_host_ptr->max_sectors =
1226                           (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1227                 }
1228         }
1229
1230         aac_fib_complete(fibptr);
1231         aac_fib_free(fibptr);
1232
1233         return rcode;
1234 }
1235
1236
1237 static void io_callback(void *context, struct fib * fibptr)
1238 {
1239         struct aac_dev *dev;
1240         struct aac_read_reply *readreply;
1241         struct scsi_cmnd *scsicmd;
1242         u32 cid;
1243
1244         scsicmd = (struct scsi_cmnd *) context;
1245         scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
1246
1247         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1248         cid = scmd_id(scsicmd);
1249
1250         if (nblank(dprintk(x))) {
1251                 u64 lba;
1252                 switch (scsicmd->cmnd[0]) {
1253                 case WRITE_6:
1254                 case READ_6:
1255                         lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1256                             (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1257                         break;
1258                 case WRITE_16:
1259                 case READ_16:
1260                         lba = ((u64)scsicmd->cmnd[2] << 56) |
1261                               ((u64)scsicmd->cmnd[3] << 48) |
1262                               ((u64)scsicmd->cmnd[4] << 40) |
1263                               ((u64)scsicmd->cmnd[5] << 32) |
1264                               ((u64)scsicmd->cmnd[6] << 24) |
1265                               (scsicmd->cmnd[7] << 16) |
1266                               (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1267                         break;
1268                 case WRITE_12:
1269                 case READ_12:
1270                         lba = ((u64)scsicmd->cmnd[2] << 24) |
1271                               (scsicmd->cmnd[3] << 16) |
1272                               (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1273                         break;
1274                 default:
1275                         lba = ((u64)scsicmd->cmnd[2] << 24) |
1276                                (scsicmd->cmnd[3] << 16) |
1277                                (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1278                         break;
1279                 }
1280                 printk(KERN_DEBUG
1281                   "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1282                   smp_processor_id(), (unsigned long long)lba, jiffies);
1283         }
1284
1285         BUG_ON(fibptr == NULL);
1286                 
1287         if(scsicmd->use_sg)
1288                 pci_unmap_sg(dev->pdev, 
1289                         (struct scatterlist *)scsicmd->request_buffer,
1290                         scsicmd->use_sg,
1291                         scsicmd->sc_data_direction);
1292         else if(scsicmd->request_bufflen)
1293                 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
1294                                  scsicmd->request_bufflen,
1295                                  scsicmd->sc_data_direction);
1296         readreply = (struct aac_read_reply *)fib_data(fibptr);
1297         if (le32_to_cpu(readreply->status) == ST_OK)
1298                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1299         else {
1300 #ifdef AAC_DETAILED_STATUS_INFO
1301                 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
1302                   le32_to_cpu(readreply->status));
1303 #endif
1304                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1305                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1306                                     HARDWARE_ERROR,
1307                                     SENCODE_INTERNAL_TARGET_FAILURE,
1308                                     ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1309                                     0, 0);
1310                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1311                   (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1312                     ? sizeof(scsicmd->sense_buffer)
1313                     : sizeof(dev->fsa_dev[cid].sense_data));
1314         }
1315         aac_fib_complete(fibptr);
1316         aac_fib_free(fibptr);
1317
1318         scsicmd->scsi_done(scsicmd);
1319 }
1320
1321 static int aac_read(struct scsi_cmnd * scsicmd, int cid)
1322 {
1323         u64 lba;
1324         u32 count;
1325         int status;
1326         struct aac_dev *dev;
1327         struct fib * cmd_fibcontext;
1328
1329         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1330         /*
1331          *      Get block address and transfer length
1332          */
1333         switch (scsicmd->cmnd[0]) {
1334         case READ_6:
1335                 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
1336
1337                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | 
1338                         (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1339                 count = scsicmd->cmnd[4];
1340
1341                 if (count == 0)
1342                         count = 256;
1343                 break;
1344         case READ_16:
1345                 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", cid));
1346
1347                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
1348                         ((u64)scsicmd->cmnd[3] << 48) |
1349                         ((u64)scsicmd->cmnd[4] << 40) |
1350                         ((u64)scsicmd->cmnd[5] << 32) |
1351                         ((u64)scsicmd->cmnd[6] << 24) | 
1352                         (scsicmd->cmnd[7] << 16) |
1353                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1354                 count = (scsicmd->cmnd[10] << 24) | 
1355                         (scsicmd->cmnd[11] << 16) |
1356                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1357                 break;
1358         case READ_12:
1359                 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", cid));
1360
1361                 lba = ((u64)scsicmd->cmnd[2] << 24) | 
1362                         (scsicmd->cmnd[3] << 16) |
1363                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1364                 count = (scsicmd->cmnd[6] << 24) | 
1365                         (scsicmd->cmnd[7] << 16) |
1366                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1367                 break;
1368         default:
1369                 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
1370
1371                 lba = ((u64)scsicmd->cmnd[2] << 24) | 
1372                         (scsicmd->cmnd[3] << 16) | 
1373                         (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1374                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1375                 break;
1376         }
1377         dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
1378           smp_processor_id(), (unsigned long long)lba, jiffies));
1379         if (aac_adapter_bounds(dev,scsicmd,lba))
1380                 return 0;
1381         /*
1382          *      Alocate and initialize a Fib
1383          */
1384         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1385                 return -1;
1386         }
1387
1388         status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
1389
1390         /*
1391          *      Check that the command queued to the controller
1392          */
1393         if (status == -EINPROGRESS) {
1394                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1395                 return 0;
1396         }
1397                 
1398         printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
1399         /*
1400          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1401          */
1402         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1403         scsicmd->scsi_done(scsicmd);
1404         aac_fib_complete(cmd_fibcontext);
1405         aac_fib_free(cmd_fibcontext);
1406         return 0;
1407 }
1408
1409 static int aac_write(struct scsi_cmnd * scsicmd, int cid)
1410 {
1411         u64 lba;
1412         u32 count;
1413         int status;
1414         struct aac_dev *dev;
1415         struct fib * cmd_fibcontext;
1416
1417         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1418         /*
1419          *      Get block address and transfer length
1420          */
1421         if (scsicmd->cmnd[0] == WRITE_6)        /* 6 byte command */
1422         {
1423                 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1424                 count = scsicmd->cmnd[4];
1425                 if (count == 0)
1426                         count = 256;
1427         } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1428                 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", cid));
1429
1430                 lba =   ((u64)scsicmd->cmnd[2] << 56) |
1431                         ((u64)scsicmd->cmnd[3] << 48) |
1432                         ((u64)scsicmd->cmnd[4] << 40) |
1433                         ((u64)scsicmd->cmnd[5] << 32) |
1434                         ((u64)scsicmd->cmnd[6] << 24) | 
1435                         (scsicmd->cmnd[7] << 16) |
1436                         (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1437                 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1438                         (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1439         } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
1440                 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", cid));
1441
1442                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1443                     | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1444                 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1445                       | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1446         } else {
1447                 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
1448                 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1449                 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1450         }
1451         dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
1452           smp_processor_id(), (unsigned long long)lba, jiffies));
1453         if (aac_adapter_bounds(dev,scsicmd,lba))
1454                 return 0;
1455         /*
1456          *      Allocate and initialize a Fib then setup a BlockWrite command
1457          */
1458         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1459                 scsicmd->result = DID_ERROR << 16;
1460                 scsicmd->scsi_done(scsicmd);
1461                 return 0;
1462         }
1463
1464         status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count);
1465
1466         /*
1467          *      Check that the command queued to the controller
1468          */
1469         if (status == -EINPROGRESS) {
1470                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1471                 return 0;
1472         }
1473
1474         printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
1475         /*
1476          *      For some reason, the Fib didn't queue, return QUEUE_FULL
1477          */
1478         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1479         scsicmd->scsi_done(scsicmd);
1480
1481         aac_fib_complete(cmd_fibcontext);
1482         aac_fib_free(cmd_fibcontext);
1483         return 0;
1484 }
1485
1486 static void synchronize_callback(void *context, struct fib *fibptr)
1487 {
1488         struct aac_synchronize_reply *synchronizereply;
1489         struct scsi_cmnd *cmd;
1490
1491         cmd = context;
1492         cmd->SCp.phase = AAC_OWNER_MIDLEVEL;
1493
1494         dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n", 
1495                                 smp_processor_id(), jiffies));
1496         BUG_ON(fibptr == NULL);
1497
1498
1499         synchronizereply = fib_data(fibptr);
1500         if (le32_to_cpu(synchronizereply->status) == CT_OK)
1501                 cmd->result = DID_OK << 16 | 
1502                         COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1503         else {
1504                 struct scsi_device *sdev = cmd->device;
1505                 struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
1506                 u32 cid = sdev_id(sdev);
1507                 printk(KERN_WARNING 
1508                      "synchronize_callback: synchronize failed, status = %d\n",
1509                      le32_to_cpu(synchronizereply->status));
1510                 cmd->result = DID_OK << 16 | 
1511                         COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1512                 set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1513                                     HARDWARE_ERROR,
1514                                     SENCODE_INTERNAL_TARGET_FAILURE,
1515                                     ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1516                                     0, 0);
1517                 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1518                   min(sizeof(dev->fsa_dev[cid].sense_data), 
1519                           sizeof(cmd->sense_buffer)));
1520         }
1521
1522         aac_fib_complete(fibptr);
1523         aac_fib_free(fibptr);
1524         cmd->scsi_done(cmd);
1525 }
1526
1527 static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
1528 {
1529         int status;
1530         struct fib *cmd_fibcontext;
1531         struct aac_synchronize *synchronizecmd;
1532         struct scsi_cmnd *cmd;
1533         struct scsi_device *sdev = scsicmd->device;
1534         int active = 0;
1535         struct aac_dev *aac;
1536         unsigned long flags;
1537
1538         /*
1539          * Wait for all outstanding queued commands to complete to this
1540          * specific target (block).
1541          */
1542         spin_lock_irqsave(&sdev->list_lock, flags);
1543         list_for_each_entry(cmd, &sdev->cmd_list, list)
1544                 if (cmd != scsicmd && cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
1545                         ++active;
1546                         break;
1547                 }
1548
1549         spin_unlock_irqrestore(&sdev->list_lock, flags);
1550
1551         /*
1552          *      Yield the processor (requeue for later)
1553          */
1554         if (active)
1555                 return SCSI_MLQUEUE_DEVICE_BUSY;
1556
1557         aac = (struct aac_dev *)scsicmd->device->host->hostdata;
1558         if (aac->in_reset)
1559                 return SCSI_MLQUEUE_HOST_BUSY;
1560
1561         /*
1562          *      Allocate and initialize a Fib
1563          */
1564         if (!(cmd_fibcontext = aac_fib_alloc(aac)))
1565                 return SCSI_MLQUEUE_HOST_BUSY;
1566
1567         aac_fib_init(cmd_fibcontext);
1568
1569         synchronizecmd = fib_data(cmd_fibcontext);
1570         synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1571         synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1572         synchronizecmd->cid = cpu_to_le32(cid);
1573         synchronizecmd->count = 
1574              cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1575
1576         /*
1577          *      Now send the Fib to the adapter
1578          */
1579         status = aac_fib_send(ContainerCommand,
1580                   cmd_fibcontext,
1581                   sizeof(struct aac_synchronize),
1582                   FsaNormal,
1583                   0, 1,
1584                   (fib_callback)synchronize_callback,
1585                   (void *)scsicmd);
1586
1587         /*
1588          *      Check that the command queued to the controller
1589          */
1590         if (status == -EINPROGRESS) {
1591                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1592                 return 0;
1593         }
1594
1595         printk(KERN_WARNING 
1596                 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1597         aac_fib_complete(cmd_fibcontext);
1598         aac_fib_free(cmd_fibcontext);
1599         return SCSI_MLQUEUE_HOST_BUSY;
1600 }
1601
1602 /**
1603  *      aac_scsi_cmd()          -       Process SCSI command
1604  *      @scsicmd:               SCSI command block
1605  *
1606  *      Emulate a SCSI command and queue the required request for the
1607  *      aacraid firmware.
1608  */
1609  
1610 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1611 {
1612         u32 cid = 0;
1613         struct Scsi_Host *host = scsicmd->device->host;
1614         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1615         struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
1616         
1617         if (fsa_dev_ptr == NULL)
1618                 return -1;
1619         /*
1620          *      If the bus, id or lun is out of range, return fail
1621          *      Test does not apply to ID 16, the pseudo id for the controller
1622          *      itself.
1623          */
1624         if (scmd_id(scsicmd) != host->this_id) {
1625                 if ((scmd_channel(scsicmd) == CONTAINER_CHANNEL)) {
1626                         if((scmd_id(scsicmd) >= dev->maximum_num_containers) ||
1627                                         (scsicmd->device->lun != 0)) {
1628                                 scsicmd->result = DID_NO_CONNECT << 16;
1629                                 scsicmd->scsi_done(scsicmd);
1630                                 return 0;
1631                         }
1632                         cid = scmd_id(scsicmd);
1633
1634                         /*
1635                          *      If the target container doesn't exist, it may have
1636                          *      been newly created
1637                          */
1638                         if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1639                                 switch (scsicmd->cmnd[0]) {
1640                                 case SERVICE_ACTION_IN:
1641                                         if (!(dev->raw_io_interface) ||
1642                                             !(dev->raw_io_64) ||
1643                                             ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1644                                                 break;
1645                                 case INQUIRY:
1646                                 case READ_CAPACITY:
1647                                 case TEST_UNIT_READY:
1648                                         if (dev->in_reset)
1649                                                 return -1;
1650                                         spin_unlock_irq(host->host_lock);
1651                                         aac_probe_container(dev, cid);
1652                                         if ((fsa_dev_ptr[cid].valid & 1) == 0)
1653                                                 fsa_dev_ptr[cid].valid = 0;
1654                                         spin_lock_irq(host->host_lock);
1655                                         if (fsa_dev_ptr[cid].valid == 0) {
1656                                                 scsicmd->result = DID_NO_CONNECT << 16;
1657                                                 scsicmd->scsi_done(scsicmd);
1658                                                 return 0;
1659                                         }
1660                                 default:
1661                                         break;
1662                                 }
1663                         }
1664                         /*
1665                          *      If the target container still doesn't exist, 
1666                          *      return failure
1667                          */
1668                         if (fsa_dev_ptr[cid].valid == 0) {
1669                                 scsicmd->result = DID_BAD_TARGET << 16;
1670                                 scsicmd->scsi_done(scsicmd);
1671                                 return 0;
1672                         }
1673                 } else {  /* check for physical non-dasd devices */
1674                         if ((dev->nondasd_support == 1) || expose_physicals) {
1675                                 if (dev->in_reset)
1676                                         return -1;
1677                                 return aac_send_srb_fib(scsicmd);
1678                         } else {
1679                                 scsicmd->result = DID_NO_CONNECT << 16;
1680                                 scsicmd->scsi_done(scsicmd);
1681                                 return 0;
1682                         }
1683                 }
1684         }
1685         /*
1686          * else Command for the controller itself
1687          */
1688         else if ((scsicmd->cmnd[0] != INQUIRY) &&       /* only INQUIRY & TUR cmnd supported for controller */
1689                 (scsicmd->cmnd[0] != TEST_UNIT_READY)) 
1690         {
1691                 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1692                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1693                 set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1694                             ILLEGAL_REQUEST,
1695                             SENCODE_INVALID_COMMAND,
1696                             ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1697                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1698                   (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1699                     ? sizeof(scsicmd->sense_buffer)
1700                     : sizeof(dev->fsa_dev[cid].sense_data));
1701                 scsicmd->scsi_done(scsicmd);
1702                 return 0;
1703         }
1704
1705
1706         /* Handle commands here that don't really require going out to the adapter */
1707         switch (scsicmd->cmnd[0]) {
1708         case INQUIRY:
1709         {
1710                 struct inquiry_data inq_data;
1711
1712                 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scmd_id(scsicmd)));
1713                 memset(&inq_data, 0, sizeof (struct inquiry_data));
1714
1715                 inq_data.inqd_ver = 2;  /* claim compliance to SCSI-2 */
1716                 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 */
1717                 inq_data.inqd_len = 31;
1718                 /*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
1719                 inq_data.inqd_pad2= 0x32 ;       /*WBus16|Sync|CmdQue */
1720                 /*
1721                  *      Set the Vendor, Product, and Revision Level
1722                  *      see: <vendor>.c i.e. aac.c
1723                  */
1724                 if (scmd_id(scsicmd) == host->this_id) {
1725                         setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
1726                         inq_data.inqd_pdt = INQD_PDT_PROC;      /* Processor device */
1727                         aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
1728                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1729                         scsicmd->scsi_done(scsicmd);
1730                         return 0;
1731                 }
1732                 if (dev->in_reset)
1733                         return -1;
1734                 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
1735                 inq_data.inqd_pdt = INQD_PDT_DA;        /* Direct/random access device */
1736                 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
1737                 return aac_get_container_name(scsicmd, cid);
1738         }
1739         case SERVICE_ACTION_IN:
1740                 if (!(dev->raw_io_interface) ||
1741                     !(dev->raw_io_64) ||
1742                     ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1743                         break;
1744         {
1745                 u64 capacity;
1746                 char cp[13];
1747
1748                 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
1749                 capacity = fsa_dev_ptr[cid].size - 1;
1750                 cp[0] = (capacity >> 56) & 0xff;
1751                 cp[1] = (capacity >> 48) & 0xff;
1752                 cp[2] = (capacity >> 40) & 0xff;
1753                 cp[3] = (capacity >> 32) & 0xff;
1754                 cp[4] = (capacity >> 24) & 0xff;
1755                 cp[5] = (capacity >> 16) & 0xff;
1756                 cp[6] = (capacity >> 8) & 0xff;
1757                 cp[7] = (capacity >> 0) & 0xff;
1758                 cp[8] = 0;
1759                 cp[9] = 0;
1760                 cp[10] = 2;
1761                 cp[11] = 0;
1762                 cp[12] = 0;
1763                 aac_internal_transfer(scsicmd, cp, 0,
1764                   min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
1765                 if (sizeof(cp) < scsicmd->cmnd[13]) {
1766                         unsigned int len, offset = sizeof(cp);
1767
1768                         memset(cp, 0, offset);
1769                         do {
1770                                 len = min_t(size_t, scsicmd->cmnd[13] - offset,
1771                                                 sizeof(cp));
1772                                 aac_internal_transfer(scsicmd, cp, offset, len);
1773                         } while ((offset += len) < scsicmd->cmnd[13]);
1774                 }
1775
1776                 /* Do not cache partition table for arrays */
1777                 scsicmd->device->removable = 1;
1778
1779                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1780                 scsicmd->scsi_done(scsicmd);
1781
1782                 return 0;
1783         }
1784
1785         case READ_CAPACITY:
1786         {
1787                 u32 capacity;
1788                 char cp[8];
1789
1790                 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
1791                 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
1792                         capacity = fsa_dev_ptr[cid].size - 1;
1793                 else
1794                         capacity = (u32)-1;
1795
1796                 cp[0] = (capacity >> 24) & 0xff;
1797                 cp[1] = (capacity >> 16) & 0xff;
1798                 cp[2] = (capacity >> 8) & 0xff;
1799                 cp[3] = (capacity >> 0) & 0xff;
1800                 cp[4] = 0;
1801                 cp[5] = 0;
1802                 cp[6] = 2;
1803                 cp[7] = 0;
1804                 aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
1805                 /* Do not cache partition table for arrays */
1806                 scsicmd->device->removable = 1;
1807
1808                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1809                 scsicmd->scsi_done(scsicmd);
1810
1811                 return 0;
1812         }
1813
1814         case MODE_SENSE:
1815         {
1816                 char mode_buf[4];
1817
1818                 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
1819                 mode_buf[0] = 3;        /* Mode data length */
1820                 mode_buf[1] = 0;        /* Medium type - default */
1821                 mode_buf[2] = 0;        /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1822                 mode_buf[3] = 0;        /* Block descriptor length */
1823
1824                 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
1825                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1826                 scsicmd->scsi_done(scsicmd);
1827
1828                 return 0;
1829         }
1830         case MODE_SENSE_10:
1831         {
1832                 char mode_buf[8];
1833
1834                 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
1835                 mode_buf[0] = 0;        /* Mode data length (MSB) */
1836                 mode_buf[1] = 6;        /* Mode data length (LSB) */
1837                 mode_buf[2] = 0;        /* Medium type - default */
1838                 mode_buf[3] = 0;        /* Device-specific param, bit 8: 0/1 = write enabled/protected */
1839                 mode_buf[4] = 0;        /* reserved */
1840                 mode_buf[5] = 0;        /* reserved */
1841                 mode_buf[6] = 0;        /* Block descriptor length (MSB) */
1842                 mode_buf[7] = 0;        /* Block descriptor length (LSB) */
1843                 aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
1844
1845                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1846                 scsicmd->scsi_done(scsicmd);
1847
1848                 return 0;
1849         }
1850         case REQUEST_SENSE:
1851                 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1852                 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1853                 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1854                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1855                 scsicmd->scsi_done(scsicmd);
1856                 return 0;
1857
1858         case ALLOW_MEDIUM_REMOVAL:
1859                 dprintk((KERN_DEBUG "LOCK command.\n"));
1860                 if (scsicmd->cmnd[4])
1861                         fsa_dev_ptr[cid].locked = 1;
1862                 else
1863                         fsa_dev_ptr[cid].locked = 0;
1864
1865                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1866                 scsicmd->scsi_done(scsicmd);
1867                 return 0;
1868         /*
1869          *      These commands are all No-Ops
1870          */
1871         case TEST_UNIT_READY:
1872         case RESERVE:
1873         case RELEASE:
1874         case REZERO_UNIT:
1875         case REASSIGN_BLOCKS:
1876         case SEEK_10:
1877         case START_STOP:
1878                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1879                 scsicmd->scsi_done(scsicmd);
1880                 return 0;
1881         }
1882
1883         switch (scsicmd->cmnd[0]) 
1884         {
1885                 case READ_6:
1886                 case READ_10:
1887                 case READ_12:
1888                 case READ_16:
1889                         if (dev->in_reset)
1890                                 return -1;
1891                         /*
1892                          *      Hack to keep track of ordinal number of the device that
1893                          *      corresponds to a container. Needed to convert
1894                          *      containers to /dev/sd device names
1895                          */
1896                          
1897                         if (scsicmd->request->rq_disk)
1898                                 strlcpy(fsa_dev_ptr[cid].devname,
1899                                 scsicmd->request->rq_disk->disk_name,
1900                                 min(sizeof(fsa_dev_ptr[cid].devname),
1901                                 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
1902
1903                         return aac_read(scsicmd, cid);
1904
1905                 case WRITE_6:
1906                 case WRITE_10:
1907                 case WRITE_12:
1908                 case WRITE_16:
1909                         if (dev->in_reset)
1910                                 return -1;
1911                         return aac_write(scsicmd, cid);
1912
1913                 case SYNCHRONIZE_CACHE:
1914                         /* Issue FIB to tell Firmware to flush it's cache */
1915                         return aac_synchronize(scsicmd, cid);
1916                         
1917                 default:
1918                         /*
1919                          *      Unhandled commands
1920                          */
1921                         dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
1922                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1923                         set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1924                                 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1925                                 ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1926                         memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1927                           (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1928                             ? sizeof(scsicmd->sense_buffer)
1929                             : sizeof(dev->fsa_dev[cid].sense_data));
1930                         scsicmd->scsi_done(scsicmd);
1931                         return 0;
1932         }
1933 }
1934
1935 static int query_disk(struct aac_dev *dev, void __user *arg)
1936 {
1937         struct aac_query_disk qd;
1938         struct fsa_dev_info *fsa_dev_ptr;
1939
1940         fsa_dev_ptr = dev->fsa_dev;
1941         if (!fsa_dev_ptr)
1942                 return -EBUSY;
1943         if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1944                 return -EFAULT;
1945         if (qd.cnum == -1)
1946                 qd.cnum = qd.id;
1947         else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1)) 
1948         {
1949                 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
1950                         return -EINVAL;
1951                 qd.instance = dev->scsi_host_ptr->host_no;
1952                 qd.bus = 0;
1953                 qd.id = CONTAINER_TO_ID(qd.cnum);
1954                 qd.lun = CONTAINER_TO_LUN(qd.cnum);
1955         }
1956         else return -EINVAL;
1957
1958         qd.valid = fsa_dev_ptr[qd.cnum].valid;
1959         qd.locked = fsa_dev_ptr[qd.cnum].locked;
1960         qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
1961
1962         if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
1963                 qd.unmapped = 1;
1964         else
1965                 qd.unmapped = 0;
1966
1967         strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
1968           min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
1969
1970         if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1971                 return -EFAULT;
1972         return 0;
1973 }
1974
1975 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
1976 {
1977         struct aac_delete_disk dd;
1978         struct fsa_dev_info *fsa_dev_ptr;
1979
1980         fsa_dev_ptr = dev->fsa_dev;
1981         if (!fsa_dev_ptr)
1982                 return -EBUSY;
1983
1984         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1985                 return -EFAULT;
1986
1987         if (dd.cnum >= dev->maximum_num_containers)
1988                 return -EINVAL;
1989         /*
1990          *      Mark this container as being deleted.
1991          */
1992         fsa_dev_ptr[dd.cnum].deleted = 1;
1993         /*
1994          *      Mark the container as no longer valid
1995          */
1996         fsa_dev_ptr[dd.cnum].valid = 0;
1997         return 0;
1998 }
1999
2000 static int delete_disk(struct aac_dev *dev, void __user *arg)
2001 {
2002         struct aac_delete_disk dd;
2003         struct fsa_dev_info *fsa_dev_ptr;
2004
2005         fsa_dev_ptr = dev->fsa_dev;
2006         if (!fsa_dev_ptr)
2007                 return -EBUSY;
2008
2009         if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2010                 return -EFAULT;
2011
2012         if (dd.cnum >= dev->maximum_num_containers)
2013                 return -EINVAL;
2014         /*
2015          *      If the container is locked, it can not be deleted by the API.
2016          */
2017         if (fsa_dev_ptr[dd.cnum].locked)
2018                 return -EBUSY;
2019         else {
2020                 /*
2021                  *      Mark the container as no longer being valid.
2022                  */
2023                 fsa_dev_ptr[dd.cnum].valid = 0;
2024                 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2025                 return 0;
2026         }
2027 }
2028
2029 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2030 {
2031         switch (cmd) {
2032         case FSACTL_QUERY_DISK:
2033                 return query_disk(dev, arg);
2034         case FSACTL_DELETE_DISK:
2035                 return delete_disk(dev, arg);
2036         case FSACTL_FORCE_DELETE_DISK:
2037                 return force_delete_disk(dev, arg);
2038         case FSACTL_GET_CONTAINERS:
2039                 return aac_get_containers(dev);
2040         default:
2041                 return -ENOTTY;
2042         }
2043 }
2044
2045 /**
2046  *
2047  * aac_srb_callback
2048  * @context: the context set in the fib - here it is scsi cmd
2049  * @fibptr: pointer to the fib
2050  *
2051  * Handles the completion of a scsi command to a non dasd device
2052  *
2053  */
2054
2055 static void aac_srb_callback(void *context, struct fib * fibptr)
2056 {
2057         struct aac_dev *dev;
2058         struct aac_srb_reply *srbreply;
2059         struct scsi_cmnd *scsicmd;
2060
2061         scsicmd = (struct scsi_cmnd *) context;
2062         scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
2063         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2064
2065         BUG_ON(fibptr == NULL);
2066
2067         srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2068
2069         scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
2070         /*
2071          *      Calculate resid for sg 
2072          */
2073          
2074         scsicmd->resid = scsicmd->request_bufflen - 
2075                 le32_to_cpu(srbreply->data_xfer_length);
2076
2077         if(scsicmd->use_sg)
2078                 pci_unmap_sg(dev->pdev, 
2079                         (struct scatterlist *)scsicmd->request_buffer,
2080                         scsicmd->use_sg,
2081                         scsicmd->sc_data_direction);
2082         else if(scsicmd->request_bufflen)
2083                 pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
2084                         scsicmd->sc_data_direction);
2085
2086         /*
2087          * First check the fib status
2088          */
2089
2090         if (le32_to_cpu(srbreply->status) != ST_OK){
2091                 int len;
2092                 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
2093                 len = (le32_to_cpu(srbreply->sense_data_size) > 
2094                                 sizeof(scsicmd->sense_buffer)) ?
2095                                 sizeof(scsicmd->sense_buffer) : 
2096                                 le32_to_cpu(srbreply->sense_data_size);
2097                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2098                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2099         }
2100
2101         /*
2102          * Next check the srb status
2103          */
2104         switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2105         case SRB_STATUS_ERROR_RECOVERY:
2106         case SRB_STATUS_PENDING:
2107         case SRB_STATUS_SUCCESS:
2108                 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2109                 break;
2110         case SRB_STATUS_DATA_OVERRUN:
2111                 switch(scsicmd->cmnd[0]){
2112                 case  READ_6:
2113                 case  WRITE_6:
2114                 case  READ_10:
2115                 case  WRITE_10:
2116                 case  READ_12:
2117                 case  WRITE_12:
2118                 case  READ_16:
2119                 case  WRITE_16:
2120                         if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
2121                                 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2122                         } else {
2123                                 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2124                         }
2125                         scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2126                         break;
2127                 case INQUIRY: {
2128                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2129                         break;
2130                 }
2131                 default:
2132                         scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2133                         break;
2134                 }
2135                 break;
2136         case SRB_STATUS_ABORTED:
2137                 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2138                 break;
2139         case SRB_STATUS_ABORT_FAILED:
2140                 // Not sure about this one - but assuming the hba was trying to abort for some reason
2141                 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2142                 break;
2143         case SRB_STATUS_PARITY_ERROR:
2144                 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2145                 break;
2146         case SRB_STATUS_NO_DEVICE:
2147         case SRB_STATUS_INVALID_PATH_ID:
2148         case SRB_STATUS_INVALID_TARGET_ID:
2149         case SRB_STATUS_INVALID_LUN:
2150         case SRB_STATUS_SELECTION_TIMEOUT:
2151                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2152                 break;
2153
2154         case SRB_STATUS_COMMAND_TIMEOUT:
2155         case SRB_STATUS_TIMEOUT:
2156                 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2157                 break;
2158
2159         case SRB_STATUS_BUSY:
2160                 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2161                 break;
2162
2163         case SRB_STATUS_BUS_RESET:
2164                 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2165                 break;
2166
2167         case SRB_STATUS_MESSAGE_REJECTED:
2168                 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2169                 break;
2170         case SRB_STATUS_REQUEST_FLUSHED:
2171         case SRB_STATUS_ERROR:
2172         case SRB_STATUS_INVALID_REQUEST:
2173         case SRB_STATUS_REQUEST_SENSE_FAILED:
2174         case SRB_STATUS_NO_HBA:
2175         case SRB_STATUS_UNEXPECTED_BUS_FREE:
2176         case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2177         case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2178         case SRB_STATUS_DELAYED_RETRY:
2179         case SRB_STATUS_BAD_FUNCTION:
2180         case SRB_STATUS_NOT_STARTED:
2181         case SRB_STATUS_NOT_IN_USE:
2182         case SRB_STATUS_FORCE_ABORT:
2183         case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2184         default:
2185 #ifdef AAC_DETAILED_STATUS_INFO
2186                 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2187                         le32_to_cpu(srbreply->srb_status) & 0x3F,
2188                         aac_get_status_string(
2189                                 le32_to_cpu(srbreply->srb_status) & 0x3F), 
2190                         scsicmd->cmnd[0], 
2191                         le32_to_cpu(srbreply->scsi_status));
2192 #endif
2193                 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2194                 break;
2195         }
2196         if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){  // Check Condition
2197                 int len;
2198                 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2199                 len = (le32_to_cpu(srbreply->sense_data_size) > 
2200                                 sizeof(scsicmd->sense_buffer)) ?
2201                                 sizeof(scsicmd->sense_buffer) :
2202                                 le32_to_cpu(srbreply->sense_data_size);
2203 #ifdef AAC_DETAILED_STATUS_INFO
2204                 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2205                                         le32_to_cpu(srbreply->status), len);
2206 #endif
2207                 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2208                 
2209         }
2210         /*
2211          * OR in the scsi status (already shifted up a bit)
2212          */
2213         scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2214
2215         aac_fib_complete(fibptr);
2216         aac_fib_free(fibptr);
2217         scsicmd->scsi_done(scsicmd);
2218 }
2219
2220 /**
2221  *
2222  * aac_send_scb_fib
2223  * @scsicmd: the scsi command block
2224  *
2225  * This routine will form a FIB and fill in the aac_srb from the 
2226  * scsicmd passed in.
2227  */
2228
2229 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2230 {
2231         struct fib* cmd_fibcontext;
2232         struct aac_dev* dev;
2233         int status;
2234
2235         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2236         if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
2237                         scsicmd->device->lun > 7) {
2238                 scsicmd->result = DID_NO_CONNECT << 16;
2239                 scsicmd->scsi_done(scsicmd);
2240                 return 0;
2241         }
2242
2243         /*
2244          *      Allocate and initialize a Fib then setup a BlockWrite command
2245          */
2246         if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
2247                 return -1;
2248         }
2249         status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
2250
2251         /*
2252          *      Check that the command queued to the controller
2253          */
2254         if (status == -EINPROGRESS) {
2255                 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2256                 return 0;
2257         }
2258
2259         printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2260         aac_fib_complete(cmd_fibcontext);
2261         aac_fib_free(cmd_fibcontext);
2262
2263         return -1;
2264 }
2265
2266 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2267 {
2268         struct aac_dev *dev;
2269         unsigned long byte_count = 0;
2270
2271         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2272         // Get rid of old data
2273         psg->count = 0;
2274         psg->sg[0].addr = 0;
2275         psg->sg[0].count = 0;  
2276         if (scsicmd->use_sg) {
2277                 struct scatterlist *sg;
2278                 int i;
2279                 int sg_count;
2280                 sg = (struct scatterlist *) scsicmd->request_buffer;
2281
2282                 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2283                         scsicmd->sc_data_direction);
2284                 psg->count = cpu_to_le32(sg_count);
2285
2286                 for (i = 0; i < sg_count; i++) {
2287                         psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2288                         psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2289                         byte_count += sg_dma_len(sg);
2290                         sg++;
2291                 }
2292                 /* hba wants the size to be exact */
2293                 if(byte_count > scsicmd->request_bufflen){
2294                         u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
2295                                 (byte_count - scsicmd->request_bufflen);
2296                         psg->sg[i-1].count = cpu_to_le32(temp);
2297                         byte_count = scsicmd->request_bufflen;
2298                 }
2299                 /* Check for command underflow */
2300                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2301                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2302                                         byte_count, scsicmd->underflow);
2303                 }
2304         }
2305         else if(scsicmd->request_bufflen) {
2306                 u32 addr;
2307                 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2308                                 scsicmd->request_buffer,
2309                                 scsicmd->request_bufflen,
2310                                 scsicmd->sc_data_direction);
2311                 addr = scsicmd->SCp.dma_handle;
2312                 psg->count = cpu_to_le32(1);
2313                 psg->sg[0].addr = cpu_to_le32(addr);
2314                 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);  
2315                 byte_count = scsicmd->request_bufflen;
2316         }
2317         return byte_count;
2318 }
2319
2320
2321 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2322 {
2323         struct aac_dev *dev;
2324         unsigned long byte_count = 0;
2325         u64 addr;
2326
2327         dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2328         // Get rid of old data
2329         psg->count = 0;
2330         psg->sg[0].addr[0] = 0;
2331         psg->sg[0].addr[1] = 0;
2332         psg->sg[0].count = 0;
2333         if (scsicmd->use_sg) {
2334                 struct scatterlist *sg;
2335                 int i;
2336                 int sg_count;
2337                 sg = (struct scatterlist *) scsicmd->request_buffer;
2338
2339                 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2340                         scsicmd->sc_data_direction);
2341
2342                 for (i = 0; i < sg_count; i++) {
2343                         int count = sg_dma_len(sg);
2344                         addr = sg_dma_address(sg);
2345                         psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2346                         psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
2347                         psg->sg[i].count = cpu_to_le32(count);
2348                         byte_count += count;
2349                         sg++;
2350                 }
2351                 psg->count = cpu_to_le32(sg_count);
2352                 /* hba wants the size to be exact */
2353                 if(byte_count > scsicmd->request_bufflen){
2354                         u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
2355                                 (byte_count - scsicmd->request_bufflen);
2356                         psg->sg[i-1].count = cpu_to_le32(temp);
2357                         byte_count = scsicmd->request_bufflen;
2358                 }
2359                 /* Check for command underflow */
2360                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2361                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2362                                         byte_count, scsicmd->underflow);
2363                 }
2364         }
2365         else if(scsicmd->request_bufflen) {
2366                 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2367                                 scsicmd->request_buffer,
2368                                 scsicmd->request_bufflen,
2369                                 scsicmd->sc_data_direction);
2370                 addr = scsicmd->SCp.dma_handle;
2371                 psg->count = cpu_to_le32(1);
2372                 psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2373                 psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
2374                 psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);  
2375                 byte_count = scsicmd->request_bufflen;
2376         }
2377         return byte_count;
2378 }
2379
2380 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2381 {
2382         struct Scsi_Host *host = scsicmd->device->host;
2383         struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2384         unsigned long byte_count = 0;
2385
2386         // Get rid of old data
2387         psg->count = 0;
2388         psg->sg[0].next = 0;
2389         psg->sg[0].prev = 0;
2390         psg->sg[0].addr[0] = 0;
2391         psg->sg[0].addr[1] = 0;
2392         psg->sg[0].count = 0;
2393         psg->sg[0].flags = 0;
2394         if (scsicmd->use_sg) {
2395                 struct scatterlist *sg;
2396                 int i;
2397                 int sg_count;
2398                 sg = (struct scatterlist *) scsicmd->request_buffer;
2399
2400                 sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2401                         scsicmd->sc_data_direction);
2402
2403                 for (i = 0; i < sg_count; i++) {
2404                         int count = sg_dma_len(sg);
2405                         u64 addr = sg_dma_address(sg);
2406                         psg->sg[i].next = 0;
2407                         psg->sg[i].prev = 0;
2408                         psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2409                         psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2410                         psg->sg[i].count = cpu_to_le32(count);
2411                         psg->sg[i].flags = 0;
2412                         byte_count += count;
2413                         sg++;
2414                 }
2415                 psg->count = cpu_to_le32(sg_count);
2416                 /* hba wants the size to be exact */
2417                 if(byte_count > scsicmd->request_bufflen){
2418                         u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
2419                                 (byte_count - scsicmd->request_bufflen);
2420                         psg->sg[i-1].count = cpu_to_le32(temp);
2421                         byte_count = scsicmd->request_bufflen;
2422                 }
2423                 /* Check for command underflow */
2424                 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2425                         printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2426                                         byte_count, scsicmd->underflow);
2427                 }
2428         }
2429         else if(scsicmd->request_bufflen) {
2430                 int count;
2431                 u64 addr;
2432                 scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2433                                 scsicmd->request_buffer,
2434                                 scsicmd->request_bufflen,
2435                                 scsicmd->sc_data_direction);
2436                 addr = scsicmd->SCp.dma_handle;
2437                 count = scsicmd->request_bufflen;
2438                 psg->count = cpu_to_le32(1);
2439                 psg->sg[0].next = 0;
2440                 psg->sg[0].prev = 0;
2441                 psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
2442                 psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2443                 psg->sg[0].count = cpu_to_le32(count);
2444                 psg->sg[0].flags = 0;
2445                 byte_count = scsicmd->request_bufflen;
2446         }
2447         return byte_count;
2448 }
2449
2450 #ifdef AAC_DETAILED_STATUS_INFO
2451
2452 struct aac_srb_status_info {
2453         u32     status;
2454         char    *str;
2455 };
2456
2457
2458 static struct aac_srb_status_info srb_status_info[] = {
2459         { SRB_STATUS_PENDING,           "Pending Status"},
2460         { SRB_STATUS_SUCCESS,           "Success"},
2461         { SRB_STATUS_ABORTED,           "Aborted Command"},
2462         { SRB_STATUS_ABORT_FAILED,      "Abort Failed"},
2463         { SRB_STATUS_ERROR,             "Error Event"},
2464         { SRB_STATUS_BUSY,              "Device Busy"},
2465         { SRB_STATUS_INVALID_REQUEST,   "Invalid Request"},
2466         { SRB_STATUS_INVALID_PATH_ID,   "Invalid Path ID"},
2467         { SRB_STATUS_NO_DEVICE,         "No Device"},
2468         { SRB_STATUS_TIMEOUT,           "Timeout"},
2469         { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
2470         { SRB_STATUS_COMMAND_TIMEOUT,   "Command Timeout"},
2471         { SRB_STATUS_MESSAGE_REJECTED,  "Message Rejected"},
2472         { SRB_STATUS_BUS_RESET,         "Bus Reset"},
2473         { SRB_STATUS_PARITY_ERROR,      "Parity Error"},
2474         { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2475         { SRB_STATUS_NO_HBA,            "No HBA"},
2476         { SRB_STATUS_DATA_OVERRUN,      "Data Overrun/Data Underrun"},
2477         { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2478         { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2479         { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2480         { SRB_STATUS_REQUEST_FLUSHED,   "Request Flushed"},
2481         { SRB_STATUS_DELAYED_RETRY,     "Delayed Retry"},
2482         { SRB_STATUS_INVALID_LUN,       "Invalid LUN"},
2483         { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
2484         { SRB_STATUS_BAD_FUNCTION,      "Bad Function"},
2485         { SRB_STATUS_ERROR_RECOVERY,    "Error Recovery"},
2486         { SRB_STATUS_NOT_STARTED,       "Not Started"},
2487         { SRB_STATUS_NOT_IN_USE,        "Not In Use"},
2488         { SRB_STATUS_FORCE_ABORT,       "Force Abort"},
2489         { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2490         { 0xff,                         "Unknown Error"}
2491 };
2492
2493 char *aac_get_status_string(u32 status)
2494 {
2495         int i;
2496
2497         for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2498                 if (srb_status_info[i].status == status)
2499                         return srb_status_info[i].str;
2500
2501         return "Bad Status Code";
2502 }
2503
2504 #endif