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