xref: /openbmc/linux/drivers/scsi/aacraid/aachba.c (revision c6fddb28)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Adaptec AAC series RAID controller driver
4  *	(c) Copyright 2001 Red Hat Inc.
5  *
6  * based on the old aacraid driver that is..
7  * Adaptec aacraid device driver for Linux.
8  *
9  * Copyright (c) 2000-2010 Adaptec, Inc.
10  *               2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
11  *		 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
12  *
13  * Module Name:
14  *  aachba.c
15  *
16  * Abstract: Contains Interfaces to manage IOs.
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/completion.h>
26 #include <linux/blkdev.h>
27 #include <linux/uaccess.h>
28 #include <linux/highmem.h> /* For flush_kernel_dcache_page */
29 #include <linux/module.h>
30 
31 #include <asm/unaligned.h>
32 
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_cmnd.h>
35 #include <scsi/scsi_device.h>
36 #include <scsi/scsi_host.h>
37 
38 #include "aacraid.h"
39 
40 /* values for inqd_pdt: Peripheral device type in plain English */
41 #define	INQD_PDT_DA	0x00	/* Direct-access (DISK) device */
42 #define	INQD_PDT_PROC	0x03	/* Processor device */
43 #define	INQD_PDT_CHNGR	0x08	/* Changer (jukebox, scsi2) */
44 #define	INQD_PDT_COMM	0x09	/* Communication device (scsi2) */
45 #define	INQD_PDT_NOLUN2 0x1f	/* Unknown Device (scsi2) */
46 #define	INQD_PDT_NOLUN	0x7f	/* Logical Unit Not Present */
47 
48 #define	INQD_PDT_DMASK	0x1F	/* Peripheral Device Type Mask */
49 #define	INQD_PDT_QMASK	0xE0	/* Peripheral Device Qualifer Mask */
50 
51 /*
52  *	Sense codes
53  */
54 
55 #define SENCODE_NO_SENSE			0x00
56 #define SENCODE_END_OF_DATA			0x00
57 #define SENCODE_BECOMING_READY			0x04
58 #define SENCODE_INIT_CMD_REQUIRED		0x04
59 #define SENCODE_UNRECOVERED_READ_ERROR		0x11
60 #define SENCODE_PARAM_LIST_LENGTH_ERROR		0x1A
61 #define SENCODE_INVALID_COMMAND			0x20
62 #define SENCODE_LBA_OUT_OF_RANGE		0x21
63 #define SENCODE_INVALID_CDB_FIELD		0x24
64 #define SENCODE_LUN_NOT_SUPPORTED		0x25
65 #define SENCODE_INVALID_PARAM_FIELD		0x26
66 #define SENCODE_PARAM_NOT_SUPPORTED		0x26
67 #define SENCODE_PARAM_VALUE_INVALID		0x26
68 #define SENCODE_RESET_OCCURRED			0x29
69 #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET	0x3E
70 #define SENCODE_INQUIRY_DATA_CHANGED		0x3F
71 #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED	0x39
72 #define SENCODE_DIAGNOSTIC_FAILURE		0x40
73 #define SENCODE_INTERNAL_TARGET_FAILURE		0x44
74 #define SENCODE_INVALID_MESSAGE_ERROR		0x49
75 #define SENCODE_LUN_FAILED_SELF_CONFIG		0x4c
76 #define SENCODE_OVERLAPPED_COMMAND		0x4E
77 
78 /*
79  *	Additional sense codes
80  */
81 
82 #define ASENCODE_NO_SENSE			0x00
83 #define ASENCODE_END_OF_DATA			0x05
84 #define ASENCODE_BECOMING_READY			0x01
85 #define ASENCODE_INIT_CMD_REQUIRED		0x02
86 #define ASENCODE_PARAM_LIST_LENGTH_ERROR	0x00
87 #define ASENCODE_INVALID_COMMAND		0x00
88 #define ASENCODE_LBA_OUT_OF_RANGE		0x00
89 #define ASENCODE_INVALID_CDB_FIELD		0x00
90 #define ASENCODE_LUN_NOT_SUPPORTED		0x00
91 #define ASENCODE_INVALID_PARAM_FIELD		0x00
92 #define ASENCODE_PARAM_NOT_SUPPORTED		0x01
93 #define ASENCODE_PARAM_VALUE_INVALID		0x02
94 #define ASENCODE_RESET_OCCURRED			0x00
95 #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET	0x00
96 #define ASENCODE_INQUIRY_DATA_CHANGED		0x03
97 #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED	0x00
98 #define ASENCODE_DIAGNOSTIC_FAILURE		0x80
99 #define ASENCODE_INTERNAL_TARGET_FAILURE	0x00
100 #define ASENCODE_INVALID_MESSAGE_ERROR		0x00
101 #define ASENCODE_LUN_FAILED_SELF_CONFIG		0x00
102 #define ASENCODE_OVERLAPPED_COMMAND		0x00
103 
104 #define BYTE0(x) (unsigned char)(x)
105 #define BYTE1(x) (unsigned char)((x) >> 8)
106 #define BYTE2(x) (unsigned char)((x) >> 16)
107 #define BYTE3(x) (unsigned char)((x) >> 24)
108 
109 /* MODE_SENSE data format */
110 typedef struct {
111 	struct {
112 		u8	data_length;
113 		u8	med_type;
114 		u8	dev_par;
115 		u8	bd_length;
116 	} __attribute__((packed)) hd;
117 	struct {
118 		u8	dens_code;
119 		u8	block_count[3];
120 		u8	reserved;
121 		u8	block_length[3];
122 	} __attribute__((packed)) bd;
123 		u8	mpc_buf[3];
124 } __attribute__((packed)) aac_modep_data;
125 
126 /* MODE_SENSE_10 data format */
127 typedef struct {
128 	struct {
129 		u8	data_length[2];
130 		u8	med_type;
131 		u8	dev_par;
132 		u8	rsrvd[2];
133 		u8	bd_length[2];
134 	} __attribute__((packed)) hd;
135 	struct {
136 		u8	dens_code;
137 		u8	block_count[3];
138 		u8	reserved;
139 		u8	block_length[3];
140 	} __attribute__((packed)) bd;
141 		u8	mpc_buf[3];
142 } __attribute__((packed)) aac_modep10_data;
143 
144 /*------------------------------------------------------------------------------
145  *              S T R U C T S / T Y P E D E F S
146  *----------------------------------------------------------------------------*/
147 /* SCSI inquiry data */
148 struct inquiry_data {
149 	u8 inqd_pdt;	/* Peripheral qualifier | Peripheral Device Type */
150 	u8 inqd_dtq;	/* RMB | Device Type Qualifier */
151 	u8 inqd_ver;	/* ISO version | ECMA version | ANSI-approved version */
152 	u8 inqd_rdf;	/* AENC | TrmIOP | Response data format */
153 	u8 inqd_len;	/* Additional length (n-4) */
154 	u8 inqd_pad1[2];/* Reserved - must be zero */
155 	u8 inqd_pad2;	/* RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
156 	u8 inqd_vid[8];	/* Vendor ID */
157 	u8 inqd_pid[16];/* Product ID */
158 	u8 inqd_prl[4];	/* Product Revision Level */
159 };
160 
161 /* Added for VPD 0x83 */
162 struct  tvpd_id_descriptor_type_1 {
163 	u8 codeset:4;		/* VPD_CODE_SET */
164 	u8 reserved:4;
165 	u8 identifiertype:4;	/* VPD_IDENTIFIER_TYPE */
166 	u8 reserved2:4;
167 	u8 reserved3;
168 	u8 identifierlength;
169 	u8 venid[8];
170 	u8 productid[16];
171 	u8 serialnumber[8];	/* SN in ASCII */
172 
173 };
174 
175 struct tvpd_id_descriptor_type_2 {
176 	u8 codeset:4;		/* VPD_CODE_SET */
177 	u8 reserved:4;
178 	u8 identifiertype:4;	/* VPD_IDENTIFIER_TYPE */
179 	u8 reserved2:4;
180 	u8 reserved3;
181 	u8 identifierlength;
182 	struct teu64id {
183 		u32 Serial;
184 		 /* The serial number supposed to be 40 bits,
185 		  * bit we only support 32, so make the last byte zero. */
186 		u8 reserved;
187 		u8 venid[3];
188 	} eu64id;
189 
190 };
191 
192 struct tvpd_id_descriptor_type_3 {
193 	u8 codeset : 4;          /* VPD_CODE_SET */
194 	u8 reserved : 4;
195 	u8 identifiertype : 4;   /* VPD_IDENTIFIER_TYPE */
196 	u8 reserved2 : 4;
197 	u8 reserved3;
198 	u8 identifierlength;
199 	u8 Identifier[16];
200 };
201 
202 struct tvpd_page83 {
203 	u8 DeviceType:5;
204 	u8 DeviceTypeQualifier:3;
205 	u8 PageCode;
206 	u8 reserved;
207 	u8 PageLength;
208 	struct tvpd_id_descriptor_type_1 type1;
209 	struct tvpd_id_descriptor_type_2 type2;
210 	struct tvpd_id_descriptor_type_3 type3;
211 };
212 
213 /*
214  *              M O D U L E   G L O B A L S
215  */
216 
217 static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *sgmap);
218 static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg);
219 static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg);
220 static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
221 				struct aac_raw_io2 *rio2, int sg_max);
222 static long aac_build_sghba(struct scsi_cmnd *scsicmd,
223 				struct aac_hba_cmd_req *hbacmd,
224 				int sg_max, u64 sg_address);
225 static int aac_convert_sgraw2(struct aac_raw_io2 *rio2,
226 				int pages, int nseg, int nseg_new);
227 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
228 static int aac_send_hba_fib(struct scsi_cmnd *scsicmd);
229 #ifdef AAC_DETAILED_STATUS_INFO
230 static char *aac_get_status_string(u32 status);
231 #endif
232 
233 /*
234  *	Non dasd selection is handled entirely in aachba now
235  */
236 
237 static int nondasd = -1;
238 static int aac_cache = 2;	/* WCE=0 to avoid performance problems */
239 static int dacmode = -1;
240 int aac_msi;
241 int aac_commit = -1;
242 int startup_timeout = 180;
243 int aif_timeout = 120;
244 int aac_sync_mode;  /* Only Sync. transfer - disabled */
245 int aac_convert_sgl = 1;	/* convert non-conformable s/g list - enabled */
246 
247 module_param(aac_sync_mode, int, S_IRUGO|S_IWUSR);
248 MODULE_PARM_DESC(aac_sync_mode, "Force sync. transfer mode"
249 	" 0=off, 1=on");
250 module_param(aac_convert_sgl, int, S_IRUGO|S_IWUSR);
251 MODULE_PARM_DESC(aac_convert_sgl, "Convert non-conformable s/g list"
252 	" 0=off, 1=on");
253 module_param(nondasd, int, S_IRUGO|S_IWUSR);
254 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
255 	" 0=off, 1=on");
256 module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
257 MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
258 	"\tbit 0 - Disable FUA in WRITE SCSI commands\n"
259 	"\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
260 	"\tbit 2 - Disable only if Battery is protecting Cache");
261 module_param(dacmode, int, S_IRUGO|S_IWUSR);
262 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
263 	" 0=off, 1=on");
264 module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
265 MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
266 	" adapter for foreign arrays.\n"
267 	"This is typically needed in systems that do not have a BIOS."
268 	" 0=off, 1=on");
269 module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
270 MODULE_PARM_DESC(msi, "IRQ handling."
271 	" 0=PIC(default), 1=MSI, 2=MSI-X)");
272 module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
273 MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
274 	" adapter to have it's kernel up and\n"
275 	"running. This is typically adjusted for large systems that do not"
276 	" have a BIOS.");
277 module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
278 MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
279 	" applications to pick up AIFs before\n"
280 	"deregistering them. This is typically adjusted for heavily burdened"
281 	" systems.");
282 
283 int aac_fib_dump;
284 module_param(aac_fib_dump, int, 0644);
285 MODULE_PARM_DESC(aac_fib_dump, "Dump controller fibs prior to IOP_RESET 0=off, 1=on");
286 
287 int numacb = -1;
288 module_param(numacb, int, S_IRUGO|S_IWUSR);
289 MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
290 	" blocks (FIB) allocated. Valid values are 512 and down. Default is"
291 	" to use suggestion from Firmware.");
292 
293 int acbsize = -1;
294 module_param(acbsize, int, S_IRUGO|S_IWUSR);
295 MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
296 	" size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
297 	" suggestion from Firmware.");
298 
299 int update_interval = 30 * 60;
300 module_param(update_interval, int, S_IRUGO|S_IWUSR);
301 MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
302 	" updates issued to adapter.");
303 
304 int check_interval = 60;
305 module_param(check_interval, int, S_IRUGO|S_IWUSR);
306 MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
307 	" checks.");
308 
309 int aac_check_reset = 1;
310 module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
311 MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
312 	" adapter. a value of -1 forces the reset to adapters programmed to"
313 	" ignore it.");
314 
315 int expose_physicals = -1;
316 module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
317 MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
318 	" -1=protect 0=off, 1=on");
319 
320 int aac_reset_devices;
321 module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
322 MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
323 
324 int aac_wwn = 1;
325 module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
326 MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
327 	"\t0 - Disable\n"
328 	"\t1 - Array Meta Data Signature (default)\n"
329 	"\t2 - Adapter Serial Number");
330 
331 
332 static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
333 		struct fib *fibptr) {
334 	struct scsi_device *device;
335 
336 	if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
337 		dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
338 		aac_fib_complete(fibptr);
339 		return 0;
340 	}
341 	scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
342 	device = scsicmd->device;
343 	if (unlikely(!device)) {
344 		dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
345 		aac_fib_complete(fibptr);
346 		return 0;
347 	}
348 	return 1;
349 }
350 
351 /**
352  *	aac_get_config_status	-	check the adapter configuration
353  *	@common: adapter to query
354  *
355  *	Query config status, and commit the configuration if needed.
356  */
357 int aac_get_config_status(struct aac_dev *dev, int commit_flag)
358 {
359 	int status = 0;
360 	struct fib * fibptr;
361 
362 	if (!(fibptr = aac_fib_alloc(dev)))
363 		return -ENOMEM;
364 
365 	aac_fib_init(fibptr);
366 	{
367 		struct aac_get_config_status *dinfo;
368 		dinfo = (struct aac_get_config_status *) fib_data(fibptr);
369 
370 		dinfo->command = cpu_to_le32(VM_ContainerConfig);
371 		dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
372 		dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
373 	}
374 
375 	status = aac_fib_send(ContainerCommand,
376 			    fibptr,
377 			    sizeof (struct aac_get_config_status),
378 			    FsaNormal,
379 			    1, 1,
380 			    NULL, NULL);
381 	if (status < 0) {
382 		printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
383 	} else {
384 		struct aac_get_config_status_resp *reply
385 		  = (struct aac_get_config_status_resp *) fib_data(fibptr);
386 		dprintk((KERN_WARNING
387 		  "aac_get_config_status: response=%d status=%d action=%d\n",
388 		  le32_to_cpu(reply->response),
389 		  le32_to_cpu(reply->status),
390 		  le32_to_cpu(reply->data.action)));
391 		if ((le32_to_cpu(reply->response) != ST_OK) ||
392 		     (le32_to_cpu(reply->status) != CT_OK) ||
393 		     (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
394 			printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
395 			status = -EINVAL;
396 		}
397 	}
398 	/* Do not set XferState to zero unless receives a response from F/W */
399 	if (status >= 0)
400 		aac_fib_complete(fibptr);
401 
402 	/* Send a CT_COMMIT_CONFIG to enable discovery of devices */
403 	if (status >= 0) {
404 		if ((aac_commit == 1) || commit_flag) {
405 			struct aac_commit_config * dinfo;
406 			aac_fib_init(fibptr);
407 			dinfo = (struct aac_commit_config *) fib_data(fibptr);
408 
409 			dinfo->command = cpu_to_le32(VM_ContainerConfig);
410 			dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
411 
412 			status = aac_fib_send(ContainerCommand,
413 				    fibptr,
414 				    sizeof (struct aac_commit_config),
415 				    FsaNormal,
416 				    1, 1,
417 				    NULL, NULL);
418 			/* Do not set XferState to zero unless
419 			 * receives a response from F/W */
420 			if (status >= 0)
421 				aac_fib_complete(fibptr);
422 		} else if (aac_commit == 0) {
423 			printk(KERN_WARNING
424 			  "aac_get_config_status: Foreign device configurations are being ignored\n");
425 		}
426 	}
427 	/* FIB should be freed only after getting the response from the F/W */
428 	if (status != -ERESTARTSYS)
429 		aac_fib_free(fibptr);
430 	return status;
431 }
432 
433 static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
434 {
435 	char inq_data;
436 	scsi_sg_copy_to_buffer(scsicmd,  &inq_data, sizeof(inq_data));
437 	if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
438 		inq_data &= 0xdf;
439 		scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
440 	}
441 }
442 
443 /**
444  *	aac_get_containers	-	list containers
445  *	@common: adapter to probe
446  *
447  *	Make a list of all containers on this controller
448  */
449 int aac_get_containers(struct aac_dev *dev)
450 {
451 	struct fsa_dev_info *fsa_dev_ptr;
452 	u32 index;
453 	int status = 0;
454 	struct fib * fibptr;
455 	struct aac_get_container_count *dinfo;
456 	struct aac_get_container_count_resp *dresp;
457 	int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
458 
459 	if (!(fibptr = aac_fib_alloc(dev)))
460 		return -ENOMEM;
461 
462 	aac_fib_init(fibptr);
463 	dinfo = (struct aac_get_container_count *) fib_data(fibptr);
464 	dinfo->command = cpu_to_le32(VM_ContainerConfig);
465 	dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
466 
467 	status = aac_fib_send(ContainerCommand,
468 		    fibptr,
469 		    sizeof (struct aac_get_container_count),
470 		    FsaNormal,
471 		    1, 1,
472 		    NULL, NULL);
473 	if (status >= 0) {
474 		dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
475 		maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
476 		if (fibptr->dev->supplement_adapter_info.supported_options2 &
477 		    AAC_OPTION_SUPPORTED_240_VOLUMES) {
478 			maximum_num_containers =
479 				le32_to_cpu(dresp->MaxSimpleVolumes);
480 		}
481 		aac_fib_complete(fibptr);
482 	}
483 	/* FIB should be freed only after getting the response from the F/W */
484 	if (status != -ERESTARTSYS)
485 		aac_fib_free(fibptr);
486 
487 	if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
488 		maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
489 	if (dev->fsa_dev == NULL ||
490 		dev->maximum_num_containers != maximum_num_containers) {
491 
492 		fsa_dev_ptr = dev->fsa_dev;
493 
494 		dev->fsa_dev = kcalloc(maximum_num_containers,
495 					sizeof(*fsa_dev_ptr), GFP_KERNEL);
496 
497 		kfree(fsa_dev_ptr);
498 		fsa_dev_ptr = NULL;
499 
500 
501 		if (!dev->fsa_dev)
502 			return -ENOMEM;
503 
504 		dev->maximum_num_containers = maximum_num_containers;
505 	}
506 	for (index = 0; index < dev->maximum_num_containers; index++) {
507 		dev->fsa_dev[index].devname[0] = '\0';
508 		dev->fsa_dev[index].valid = 0;
509 
510 		status = aac_probe_container(dev, index);
511 
512 		if (status < 0) {
513 			printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
514 			break;
515 		}
516 	}
517 	return status;
518 }
519 
520 static void get_container_name_callback(void *context, struct fib * fibptr)
521 {
522 	struct aac_get_name_resp * get_name_reply;
523 	struct scsi_cmnd * scsicmd;
524 
525 	scsicmd = (struct scsi_cmnd *) context;
526 
527 	if (!aac_valid_context(scsicmd, fibptr))
528 		return;
529 
530 	dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
531 	BUG_ON(fibptr == NULL);
532 
533 	get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
534 	/* Failure is irrelevant, using default value instead */
535 	if ((le32_to_cpu(get_name_reply->status) == CT_OK)
536 	 && (get_name_reply->data[0] != '\0')) {
537 		char *sp = get_name_reply->data;
538 		int data_size = sizeof_field(struct aac_get_name_resp, data);
539 
540 		sp[data_size - 1] = '\0';
541 		while (*sp == ' ')
542 			++sp;
543 		if (*sp) {
544 			struct inquiry_data inq;
545 			char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
546 			int count = sizeof(d);
547 			char *dp = d;
548 			do {
549 				*dp++ = (*sp) ? *sp++ : ' ';
550 			} while (--count > 0);
551 
552 			scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
553 			memcpy(inq.inqd_pid, d, sizeof(d));
554 			scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
555 		}
556 	}
557 
558 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
559 
560 	aac_fib_complete(fibptr);
561 	scsicmd->scsi_done(scsicmd);
562 }
563 
564 /**
565  *	aac_get_container_name	-	get container name, none blocking.
566  */
567 static int aac_get_container_name(struct scsi_cmnd * scsicmd)
568 {
569 	int status;
570 	int data_size;
571 	struct aac_get_name *dinfo;
572 	struct fib * cmd_fibcontext;
573 	struct aac_dev * dev;
574 
575 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
576 
577 	data_size = sizeof_field(struct aac_get_name_resp, data);
578 
579 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
580 
581 	aac_fib_init(cmd_fibcontext);
582 	dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
583 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
584 
585 	dinfo->command = cpu_to_le32(VM_ContainerConfig);
586 	dinfo->type = cpu_to_le32(CT_READ_NAME);
587 	dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
588 	dinfo->count = cpu_to_le32(data_size - 1);
589 
590 	status = aac_fib_send(ContainerCommand,
591 		  cmd_fibcontext,
592 		  sizeof(struct aac_get_name_resp),
593 		  FsaNormal,
594 		  0, 1,
595 		  (fib_callback)get_container_name_callback,
596 		  (void *) scsicmd);
597 
598 	/*
599 	 *	Check that the command queued to the controller
600 	 */
601 	if (status == -EINPROGRESS)
602 		return 0;
603 
604 	printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
605 	aac_fib_complete(cmd_fibcontext);
606 	return -1;
607 }
608 
609 static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
610 {
611 	struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
612 
613 	if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
614 		return aac_scsi_cmd(scsicmd);
615 
616 	scsicmd->result = DID_NO_CONNECT << 16;
617 	scsicmd->scsi_done(scsicmd);
618 	return 0;
619 }
620 
621 static void _aac_probe_container2(void * context, struct fib * fibptr)
622 {
623 	struct fsa_dev_info *fsa_dev_ptr;
624 	int (*callback)(struct scsi_cmnd *);
625 	struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
626 	int i;
627 
628 
629 	if (!aac_valid_context(scsicmd, fibptr))
630 		return;
631 
632 	scsicmd->SCp.Status = 0;
633 	fsa_dev_ptr = fibptr->dev->fsa_dev;
634 	if (fsa_dev_ptr) {
635 		struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
636 		__le32 sup_options2;
637 
638 		fsa_dev_ptr += scmd_id(scsicmd);
639 		sup_options2 =
640 			fibptr->dev->supplement_adapter_info.supported_options2;
641 
642 		if ((le32_to_cpu(dresp->status) == ST_OK) &&
643 		    (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
644 		    (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
645 			if (!(sup_options2 & AAC_OPTION_VARIABLE_BLOCK_SIZE)) {
646 				dresp->mnt[0].fileinfo.bdevinfo.block_size = 0x200;
647 				fsa_dev_ptr->block_size = 0x200;
648 			} else {
649 				fsa_dev_ptr->block_size =
650 					le32_to_cpu(dresp->mnt[0].fileinfo.bdevinfo.block_size);
651 			}
652 			for (i = 0; i < 16; i++)
653 				fsa_dev_ptr->identifier[i] =
654 					dresp->mnt[0].fileinfo.bdevinfo
655 								.identifier[i];
656 			fsa_dev_ptr->valid = 1;
657 			/* sense_key holds the current state of the spin-up */
658 			if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
659 				fsa_dev_ptr->sense_data.sense_key = NOT_READY;
660 			else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
661 				fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
662 			fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
663 			fsa_dev_ptr->size
664 			  = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
665 			    (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
666 			fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
667 		}
668 		if ((fsa_dev_ptr->valid & 1) == 0)
669 			fsa_dev_ptr->valid = 0;
670 		scsicmd->SCp.Status = le32_to_cpu(dresp->count);
671 	}
672 	aac_fib_complete(fibptr);
673 	aac_fib_free(fibptr);
674 	callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
675 	scsicmd->SCp.ptr = NULL;
676 	(*callback)(scsicmd);
677 	return;
678 }
679 
680 static void _aac_probe_container1(void * context, struct fib * fibptr)
681 {
682 	struct scsi_cmnd * scsicmd;
683 	struct aac_mount * dresp;
684 	struct aac_query_mount *dinfo;
685 	int status;
686 
687 	dresp = (struct aac_mount *) fib_data(fibptr);
688 	if (!aac_supports_2T(fibptr->dev)) {
689 		dresp->mnt[0].capacityhigh = 0;
690 		if ((le32_to_cpu(dresp->status) == ST_OK) &&
691 			(le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
692 			_aac_probe_container2(context, fibptr);
693 			return;
694 		}
695 	}
696 	scsicmd = (struct scsi_cmnd *) context;
697 
698 	if (!aac_valid_context(scsicmd, fibptr))
699 		return;
700 
701 	aac_fib_init(fibptr);
702 
703 	dinfo = (struct aac_query_mount *)fib_data(fibptr);
704 
705 	if (fibptr->dev->supplement_adapter_info.supported_options2 &
706 	    AAC_OPTION_VARIABLE_BLOCK_SIZE)
707 		dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
708 	else
709 		dinfo->command = cpu_to_le32(VM_NameServe64);
710 
711 	dinfo->count = cpu_to_le32(scmd_id(scsicmd));
712 	dinfo->type = cpu_to_le32(FT_FILESYS);
713 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
714 
715 	status = aac_fib_send(ContainerCommand,
716 			  fibptr,
717 			  sizeof(struct aac_query_mount),
718 			  FsaNormal,
719 			  0, 1,
720 			  _aac_probe_container2,
721 			  (void *) scsicmd);
722 	/*
723 	 *	Check that the command queued to the controller
724 	 */
725 	if (status < 0 && status != -EINPROGRESS) {
726 		/* Inherit results from VM_NameServe, if any */
727 		dresp->status = cpu_to_le32(ST_OK);
728 		_aac_probe_container2(context, fibptr);
729 	}
730 }
731 
732 static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
733 {
734 	struct fib * fibptr;
735 	int status = -ENOMEM;
736 
737 	if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
738 		struct aac_query_mount *dinfo;
739 
740 		aac_fib_init(fibptr);
741 
742 		dinfo = (struct aac_query_mount *)fib_data(fibptr);
743 
744 		if (fibptr->dev->supplement_adapter_info.supported_options2 &
745 		    AAC_OPTION_VARIABLE_BLOCK_SIZE)
746 			dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
747 		else
748 			dinfo->command = cpu_to_le32(VM_NameServe);
749 
750 		dinfo->count = cpu_to_le32(scmd_id(scsicmd));
751 		dinfo->type = cpu_to_le32(FT_FILESYS);
752 		scsicmd->SCp.ptr = (char *)callback;
753 		scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
754 
755 		status = aac_fib_send(ContainerCommand,
756 			  fibptr,
757 			  sizeof(struct aac_query_mount),
758 			  FsaNormal,
759 			  0, 1,
760 			  _aac_probe_container1,
761 			  (void *) scsicmd);
762 		/*
763 		 *	Check that the command queued to the controller
764 		 */
765 		if (status == -EINPROGRESS)
766 			return 0;
767 
768 		if (status < 0) {
769 			scsicmd->SCp.ptr = NULL;
770 			aac_fib_complete(fibptr);
771 			aac_fib_free(fibptr);
772 		}
773 	}
774 	if (status < 0) {
775 		struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
776 		if (fsa_dev_ptr) {
777 			fsa_dev_ptr += scmd_id(scsicmd);
778 			if ((fsa_dev_ptr->valid & 1) == 0) {
779 				fsa_dev_ptr->valid = 0;
780 				return (*callback)(scsicmd);
781 			}
782 		}
783 	}
784 	return status;
785 }
786 
787 /**
788  *	aac_probe_container		-	query a logical volume
789  *	@dev: device to query
790  *	@cid: container identifier
791  *
792  *	Queries the controller about the given volume. The volume information
793  *	is updated in the struct fsa_dev_info structure rather than returned.
794  */
795 static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
796 {
797 	scsicmd->device = NULL;
798 	return 0;
799 }
800 
801 static void aac_probe_container_scsi_done(struct scsi_cmnd *scsi_cmnd)
802 {
803 	aac_probe_container_callback1(scsi_cmnd);
804 }
805 
806 int aac_probe_container(struct aac_dev *dev, int cid)
807 {
808 	struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
809 	struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
810 	int status;
811 
812 	if (!scsicmd || !scsidev) {
813 		kfree(scsicmd);
814 		kfree(scsidev);
815 		return -ENOMEM;
816 	}
817 	scsicmd->list.next = NULL;
818 	scsicmd->scsi_done = aac_probe_container_scsi_done;
819 
820 	scsicmd->device = scsidev;
821 	scsidev->sdev_state = 0;
822 	scsidev->id = cid;
823 	scsidev->host = dev->scsi_host_ptr;
824 
825 	if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
826 		while (scsicmd->device == scsidev)
827 			schedule();
828 	kfree(scsidev);
829 	status = scsicmd->SCp.Status;
830 	kfree(scsicmd);
831 	return status;
832 }
833 
834 /* Local Structure to set SCSI inquiry data strings */
835 struct scsi_inq {
836 	char vid[8];         /* Vendor ID */
837 	char pid[16];        /* Product ID */
838 	char prl[4];         /* Product Revision Level */
839 };
840 
841 /**
842  *	InqStrCopy	-	string merge
843  *	@a:	string to copy from
844  *	@b:	string to copy to
845  *
846  *	Copy a String from one location to another
847  *	without copying \0
848  */
849 
850 static void inqstrcpy(char *a, char *b)
851 {
852 
853 	while (*a != (char)0)
854 		*b++ = *a++;
855 }
856 
857 static char *container_types[] = {
858 	"None",
859 	"Volume",
860 	"Mirror",
861 	"Stripe",
862 	"RAID5",
863 	"SSRW",
864 	"SSRO",
865 	"Morph",
866 	"Legacy",
867 	"RAID4",
868 	"RAID10",
869 	"RAID00",
870 	"V-MIRRORS",
871 	"PSEUDO R4",
872 	"RAID50",
873 	"RAID5D",
874 	"RAID5D0",
875 	"RAID1E",
876 	"RAID6",
877 	"RAID60",
878 	"Unknown"
879 };
880 
881 char * get_container_type(unsigned tindex)
882 {
883 	if (tindex >= ARRAY_SIZE(container_types))
884 		tindex = ARRAY_SIZE(container_types) - 1;
885 	return container_types[tindex];
886 }
887 
888 /* Function: setinqstr
889  *
890  * Arguments: [1] pointer to void [1] int
891  *
892  * Purpose: Sets SCSI inquiry data strings for vendor, product
893  * and revision level. Allows strings to be set in platform dependent
894  * files instead of in OS dependent driver source.
895  */
896 
897 static void setinqstr(struct aac_dev *dev, void *data, int tindex)
898 {
899 	struct scsi_inq *str;
900 	struct aac_supplement_adapter_info *sup_adap_info;
901 
902 	sup_adap_info = &dev->supplement_adapter_info;
903 	str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
904 	memset(str, ' ', sizeof(*str));
905 
906 	if (sup_adap_info->adapter_type_text[0]) {
907 		int c;
908 		char *cp;
909 		char *cname = kmemdup(sup_adap_info->adapter_type_text,
910 				sizeof(sup_adap_info->adapter_type_text),
911 								GFP_ATOMIC);
912 		if (!cname)
913 			return;
914 
915 		cp = cname;
916 		if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
917 			inqstrcpy("SMC", str->vid);
918 		else {
919 			c = sizeof(str->vid);
920 			while (*cp && *cp != ' ' && --c)
921 				++cp;
922 			c = *cp;
923 			*cp = '\0';
924 			inqstrcpy(cname, str->vid);
925 			*cp = c;
926 			while (*cp && *cp != ' ')
927 				++cp;
928 		}
929 		while (*cp == ' ')
930 			++cp;
931 		/* last six chars reserved for vol type */
932 		if (strlen(cp) > sizeof(str->pid))
933 			cp[sizeof(str->pid)] = '\0';
934 		inqstrcpy (cp, str->pid);
935 
936 		kfree(cname);
937 	} else {
938 		struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
939 
940 		inqstrcpy (mp->vname, str->vid);
941 		/* last six chars reserved for vol type */
942 		inqstrcpy (mp->model, str->pid);
943 	}
944 
945 	if (tindex < ARRAY_SIZE(container_types)){
946 		char *findit = str->pid;
947 
948 		for ( ; *findit != ' '; findit++); /* walk till we find a space */
949 		/* RAID is superfluous in the context of a RAID device */
950 		if (memcmp(findit-4, "RAID", 4) == 0)
951 			*(findit -= 4) = ' ';
952 		if (((findit - str->pid) + strlen(container_types[tindex]))
953 		 < (sizeof(str->pid) + sizeof(str->prl)))
954 			inqstrcpy (container_types[tindex], findit + 1);
955 	}
956 	inqstrcpy ("V1.0", str->prl);
957 }
958 
959 static void build_vpd83_type3(struct tvpd_page83 *vpdpage83data,
960 		struct aac_dev *dev, struct scsi_cmnd *scsicmd)
961 {
962 	int container;
963 
964 	vpdpage83data->type3.codeset = 1;
965 	vpdpage83data->type3.identifiertype = 3;
966 	vpdpage83data->type3.identifierlength = sizeof(vpdpage83data->type3)
967 			- 4;
968 
969 	for (container = 0; container < dev->maximum_num_containers;
970 			container++) {
971 
972 		if (scmd_id(scsicmd) == container) {
973 			memcpy(vpdpage83data->type3.Identifier,
974 					dev->fsa_dev[container].identifier,
975 					16);
976 			break;
977 		}
978 	}
979 }
980 
981 static void get_container_serial_callback(void *context, struct fib * fibptr)
982 {
983 	struct aac_get_serial_resp * get_serial_reply;
984 	struct scsi_cmnd * scsicmd;
985 
986 	BUG_ON(fibptr == NULL);
987 
988 	scsicmd = (struct scsi_cmnd *) context;
989 	if (!aac_valid_context(scsicmd, fibptr))
990 		return;
991 
992 	get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
993 	/* Failure is irrelevant, using default value instead */
994 	if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
995 		/*Check to see if it's for VPD 0x83 or 0x80 */
996 		if (scsicmd->cmnd[2] == 0x83) {
997 			/* vpd page 0x83 - Device Identification Page */
998 			struct aac_dev *dev;
999 			int i;
1000 			struct tvpd_page83 vpdpage83data;
1001 
1002 			dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1003 
1004 			memset(((u8 *)&vpdpage83data), 0,
1005 			       sizeof(vpdpage83data));
1006 
1007 			/* DIRECT_ACCESS_DEVIC */
1008 			vpdpage83data.DeviceType = 0;
1009 			/* DEVICE_CONNECTED */
1010 			vpdpage83data.DeviceTypeQualifier = 0;
1011 			/* VPD_DEVICE_IDENTIFIERS */
1012 			vpdpage83data.PageCode = 0x83;
1013 			vpdpage83data.reserved = 0;
1014 			vpdpage83data.PageLength =
1015 				sizeof(vpdpage83data.type1) +
1016 				sizeof(vpdpage83data.type2);
1017 
1018 			/* VPD 83 Type 3 is not supported for ARC */
1019 			if (dev->sa_firmware)
1020 				vpdpage83data.PageLength +=
1021 				sizeof(vpdpage83data.type3);
1022 
1023 			/* T10 Vendor Identifier Field Format */
1024 			/* VpdcodesetAscii */
1025 			vpdpage83data.type1.codeset = 2;
1026 			/* VpdIdentifierTypeVendorId */
1027 			vpdpage83data.type1.identifiertype = 1;
1028 			vpdpage83data.type1.identifierlength =
1029 				sizeof(vpdpage83data.type1) - 4;
1030 
1031 			/* "ADAPTEC " for adaptec */
1032 			memcpy(vpdpage83data.type1.venid,
1033 				"ADAPTEC ",
1034 				sizeof(vpdpage83data.type1.venid));
1035 			memcpy(vpdpage83data.type1.productid,
1036 				"ARRAY           ",
1037 				sizeof(
1038 				vpdpage83data.type1.productid));
1039 
1040 			/* Convert to ascii based serial number.
1041 			 * The LSB is the the end.
1042 			 */
1043 			for (i = 0; i < 8; i++) {
1044 				u8 temp =
1045 					(u8)((get_serial_reply->uid >> ((7 - i) * 4)) & 0xF);
1046 				if (temp  > 0x9) {
1047 					vpdpage83data.type1.serialnumber[i] =
1048 							'A' + (temp - 0xA);
1049 				} else {
1050 					vpdpage83data.type1.serialnumber[i] =
1051 							'0' + temp;
1052 				}
1053 			}
1054 
1055 			/* VpdCodeSetBinary */
1056 			vpdpage83data.type2.codeset = 1;
1057 			/* VpdidentifiertypeEUI64 */
1058 			vpdpage83data.type2.identifiertype = 2;
1059 			vpdpage83data.type2.identifierlength =
1060 				sizeof(vpdpage83data.type2) - 4;
1061 
1062 			vpdpage83data.type2.eu64id.venid[0] = 0xD0;
1063 			vpdpage83data.type2.eu64id.venid[1] = 0;
1064 			vpdpage83data.type2.eu64id.venid[2] = 0;
1065 
1066 			vpdpage83data.type2.eu64id.Serial =
1067 							get_serial_reply->uid;
1068 			vpdpage83data.type2.eu64id.reserved = 0;
1069 
1070 			/*
1071 			 * VpdIdentifierTypeFCPHName
1072 			 * VPD 0x83 Type 3 not supported for ARC
1073 			 */
1074 			if (dev->sa_firmware) {
1075 				build_vpd83_type3(&vpdpage83data,
1076 						dev, scsicmd);
1077 			}
1078 
1079 			/* Move the inquiry data to the response buffer. */
1080 			scsi_sg_copy_from_buffer(scsicmd, &vpdpage83data,
1081 						 sizeof(vpdpage83data));
1082 		} else {
1083 			/* It must be for VPD 0x80 */
1084 			char sp[13];
1085 			/* EVPD bit set */
1086 			sp[0] = INQD_PDT_DA;
1087 			sp[1] = scsicmd->cmnd[2];
1088 			sp[2] = 0;
1089 			sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
1090 				le32_to_cpu(get_serial_reply->uid));
1091 			scsi_sg_copy_from_buffer(scsicmd, sp,
1092 						 sizeof(sp));
1093 		}
1094 	}
1095 
1096 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1097 
1098 	aac_fib_complete(fibptr);
1099 	scsicmd->scsi_done(scsicmd);
1100 }
1101 
1102 /**
1103  *	aac_get_container_serial - get container serial, none blocking.
1104  */
1105 static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
1106 {
1107 	int status;
1108 	struct aac_get_serial *dinfo;
1109 	struct fib * cmd_fibcontext;
1110 	struct aac_dev * dev;
1111 
1112 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1113 
1114 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
1115 
1116 	aac_fib_init(cmd_fibcontext);
1117 	dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
1118 
1119 	dinfo->command = cpu_to_le32(VM_ContainerConfig);
1120 	dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
1121 	dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
1122 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1123 
1124 	status = aac_fib_send(ContainerCommand,
1125 		  cmd_fibcontext,
1126 		  sizeof(struct aac_get_serial_resp),
1127 		  FsaNormal,
1128 		  0, 1,
1129 		  (fib_callback) get_container_serial_callback,
1130 		  (void *) scsicmd);
1131 
1132 	/*
1133 	 *	Check that the command queued to the controller
1134 	 */
1135 	if (status == -EINPROGRESS)
1136 		return 0;
1137 
1138 	printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
1139 	aac_fib_complete(cmd_fibcontext);
1140 	return -1;
1141 }
1142 
1143 /* Function: setinqserial
1144  *
1145  * Arguments: [1] pointer to void [1] int
1146  *
1147  * Purpose: Sets SCSI Unit Serial number.
1148  *          This is a fake. We should read a proper
1149  *          serial number from the container. <SuSE>But
1150  *          without docs it's quite hard to do it :-)
1151  *          So this will have to do in the meantime.</SuSE>
1152  */
1153 
1154 static int setinqserial(struct aac_dev *dev, void *data, int cid)
1155 {
1156 	/*
1157 	 *	This breaks array migration.
1158 	 */
1159 	return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
1160 			le32_to_cpu(dev->adapter_info.serial[0]), cid);
1161 }
1162 
1163 static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
1164 	u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
1165 {
1166 	u8 *sense_buf = (u8 *)sense_data;
1167 	/* Sense data valid, err code 70h */
1168 	sense_buf[0] = 0x70; /* No info field */
1169 	sense_buf[1] = 0;	/* Segment number, always zero */
1170 
1171 	sense_buf[2] = sense_key;	/* Sense key */
1172 
1173 	sense_buf[12] = sense_code;	/* Additional sense code */
1174 	sense_buf[13] = a_sense_code;	/* Additional sense code qualifier */
1175 
1176 	if (sense_key == ILLEGAL_REQUEST) {
1177 		sense_buf[7] = 10;	/* Additional sense length */
1178 
1179 		sense_buf[15] = bit_pointer;
1180 		/* Illegal parameter is in the parameter block */
1181 		if (sense_code == SENCODE_INVALID_CDB_FIELD)
1182 			sense_buf[15] |= 0xc0;/* Std sense key specific field */
1183 		/* Illegal parameter is in the CDB block */
1184 		sense_buf[16] = field_pointer >> 8;	/* MSB */
1185 		sense_buf[17] = field_pointer;		/* LSB */
1186 	} else
1187 		sense_buf[7] = 6;	/* Additional sense length */
1188 }
1189 
1190 static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1191 {
1192 	if (lba & 0xffffffff00000000LL) {
1193 		int cid = scmd_id(cmd);
1194 		dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1195 		cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1196 			SAM_STAT_CHECK_CONDITION;
1197 		set_sense(&dev->fsa_dev[cid].sense_data,
1198 		  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1199 		  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1200 		memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1201 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1202 			     SCSI_SENSE_BUFFERSIZE));
1203 		cmd->scsi_done(cmd);
1204 		return 1;
1205 	}
1206 	return 0;
1207 }
1208 
1209 static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1210 {
1211 	return 0;
1212 }
1213 
1214 static void io_callback(void *context, struct fib * fibptr);
1215 
1216 static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1217 {
1218 	struct aac_dev *dev = fib->dev;
1219 	u16 fibsize, command;
1220 	long ret;
1221 
1222 	aac_fib_init(fib);
1223 	if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1224 		dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1225 		!dev->sync_mode) {
1226 		struct aac_raw_io2 *readcmd2;
1227 		readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
1228 		memset(readcmd2, 0, sizeof(struct aac_raw_io2));
1229 		readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1230 		readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1231 		readcmd2->byteCount = cpu_to_le32(count *
1232 			dev->fsa_dev[scmd_id(cmd)].block_size);
1233 		readcmd2->cid = cpu_to_le16(scmd_id(cmd));
1234 		readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
1235 		ret = aac_build_sgraw2(cmd, readcmd2,
1236 				dev->scsi_host_ptr->sg_tablesize);
1237 		if (ret < 0)
1238 			return ret;
1239 		command = ContainerRawIo2;
1240 		fibsize = sizeof(struct aac_raw_io2) +
1241 			((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1242 	} else {
1243 		struct aac_raw_io *readcmd;
1244 		readcmd = (struct aac_raw_io *) fib_data(fib);
1245 		readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1246 		readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1247 		readcmd->count = cpu_to_le32(count *
1248 			dev->fsa_dev[scmd_id(cmd)].block_size);
1249 		readcmd->cid = cpu_to_le16(scmd_id(cmd));
1250 		readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
1251 		readcmd->bpTotal = 0;
1252 		readcmd->bpComplete = 0;
1253 		ret = aac_build_sgraw(cmd, &readcmd->sg);
1254 		if (ret < 0)
1255 			return ret;
1256 		command = ContainerRawIo;
1257 		fibsize = sizeof(struct aac_raw_io) +
1258 			((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
1259 	}
1260 
1261 	BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1262 	/*
1263 	 *	Now send the Fib to the adapter
1264 	 */
1265 	return aac_fib_send(command,
1266 			  fib,
1267 			  fibsize,
1268 			  FsaNormal,
1269 			  0, 1,
1270 			  (fib_callback) io_callback,
1271 			  (void *) cmd);
1272 }
1273 
1274 static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1275 {
1276 	u16 fibsize;
1277 	struct aac_read64 *readcmd;
1278 	long ret;
1279 
1280 	aac_fib_init(fib);
1281 	readcmd = (struct aac_read64 *) fib_data(fib);
1282 	readcmd->command = cpu_to_le32(VM_CtHostRead64);
1283 	readcmd->cid = cpu_to_le16(scmd_id(cmd));
1284 	readcmd->sector_count = cpu_to_le16(count);
1285 	readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1286 	readcmd->pad   = 0;
1287 	readcmd->flags = 0;
1288 
1289 	ret = aac_build_sg64(cmd, &readcmd->sg);
1290 	if (ret < 0)
1291 		return ret;
1292 	fibsize = sizeof(struct aac_read64) +
1293 		((le32_to_cpu(readcmd->sg.count) - 1) *
1294 		 sizeof (struct sgentry64));
1295 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1296 				sizeof(struct aac_fibhdr)));
1297 	/*
1298 	 *	Now send the Fib to the adapter
1299 	 */
1300 	return aac_fib_send(ContainerCommand64,
1301 			  fib,
1302 			  fibsize,
1303 			  FsaNormal,
1304 			  0, 1,
1305 			  (fib_callback) io_callback,
1306 			  (void *) cmd);
1307 }
1308 
1309 static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1310 {
1311 	u16 fibsize;
1312 	struct aac_read *readcmd;
1313 	struct aac_dev *dev = fib->dev;
1314 	long ret;
1315 
1316 	aac_fib_init(fib);
1317 	readcmd = (struct aac_read *) fib_data(fib);
1318 	readcmd->command = cpu_to_le32(VM_CtBlockRead);
1319 	readcmd->cid = cpu_to_le32(scmd_id(cmd));
1320 	readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1321 	readcmd->count = cpu_to_le32(count *
1322 		dev->fsa_dev[scmd_id(cmd)].block_size);
1323 
1324 	ret = aac_build_sg(cmd, &readcmd->sg);
1325 	if (ret < 0)
1326 		return ret;
1327 	fibsize = sizeof(struct aac_read) +
1328 			((le32_to_cpu(readcmd->sg.count) - 1) *
1329 			 sizeof (struct sgentry));
1330 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1331 				sizeof(struct aac_fibhdr)));
1332 	/*
1333 	 *	Now send the Fib to the adapter
1334 	 */
1335 	return aac_fib_send(ContainerCommand,
1336 			  fib,
1337 			  fibsize,
1338 			  FsaNormal,
1339 			  0, 1,
1340 			  (fib_callback) io_callback,
1341 			  (void *) cmd);
1342 }
1343 
1344 static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1345 {
1346 	struct aac_dev *dev = fib->dev;
1347 	u16 fibsize, command;
1348 	long ret;
1349 
1350 	aac_fib_init(fib);
1351 	if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1352 		dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1353 		!dev->sync_mode) {
1354 		struct aac_raw_io2 *writecmd2;
1355 		writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
1356 		memset(writecmd2, 0, sizeof(struct aac_raw_io2));
1357 		writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1358 		writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1359 		writecmd2->byteCount = cpu_to_le32(count *
1360 			dev->fsa_dev[scmd_id(cmd)].block_size);
1361 		writecmd2->cid = cpu_to_le16(scmd_id(cmd));
1362 		writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
1363 						   (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1364 			cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
1365 			cpu_to_le16(RIO2_IO_TYPE_WRITE);
1366 		ret = aac_build_sgraw2(cmd, writecmd2,
1367 				dev->scsi_host_ptr->sg_tablesize);
1368 		if (ret < 0)
1369 			return ret;
1370 		command = ContainerRawIo2;
1371 		fibsize = sizeof(struct aac_raw_io2) +
1372 			((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1373 	} else {
1374 		struct aac_raw_io *writecmd;
1375 		writecmd = (struct aac_raw_io *) fib_data(fib);
1376 		writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1377 		writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1378 		writecmd->count = cpu_to_le32(count *
1379 			dev->fsa_dev[scmd_id(cmd)].block_size);
1380 		writecmd->cid = cpu_to_le16(scmd_id(cmd));
1381 		writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1382 						   (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1383 			cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
1384 			cpu_to_le16(RIO_TYPE_WRITE);
1385 		writecmd->bpTotal = 0;
1386 		writecmd->bpComplete = 0;
1387 		ret = aac_build_sgraw(cmd, &writecmd->sg);
1388 		if (ret < 0)
1389 			return ret;
1390 		command = ContainerRawIo;
1391 		fibsize = sizeof(struct aac_raw_io) +
1392 			((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
1393 	}
1394 
1395 	BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1396 	/*
1397 	 *	Now send the Fib to the adapter
1398 	 */
1399 	return aac_fib_send(command,
1400 			  fib,
1401 			  fibsize,
1402 			  FsaNormal,
1403 			  0, 1,
1404 			  (fib_callback) io_callback,
1405 			  (void *) cmd);
1406 }
1407 
1408 static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1409 {
1410 	u16 fibsize;
1411 	struct aac_write64 *writecmd;
1412 	long ret;
1413 
1414 	aac_fib_init(fib);
1415 	writecmd = (struct aac_write64 *) fib_data(fib);
1416 	writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1417 	writecmd->cid = cpu_to_le16(scmd_id(cmd));
1418 	writecmd->sector_count = cpu_to_le16(count);
1419 	writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1420 	writecmd->pad	= 0;
1421 	writecmd->flags	= 0;
1422 
1423 	ret = aac_build_sg64(cmd, &writecmd->sg);
1424 	if (ret < 0)
1425 		return ret;
1426 	fibsize = sizeof(struct aac_write64) +
1427 		((le32_to_cpu(writecmd->sg.count) - 1) *
1428 		 sizeof (struct sgentry64));
1429 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1430 				sizeof(struct aac_fibhdr)));
1431 	/*
1432 	 *	Now send the Fib to the adapter
1433 	 */
1434 	return aac_fib_send(ContainerCommand64,
1435 			  fib,
1436 			  fibsize,
1437 			  FsaNormal,
1438 			  0, 1,
1439 			  (fib_callback) io_callback,
1440 			  (void *) cmd);
1441 }
1442 
1443 static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1444 {
1445 	u16 fibsize;
1446 	struct aac_write *writecmd;
1447 	struct aac_dev *dev = fib->dev;
1448 	long ret;
1449 
1450 	aac_fib_init(fib);
1451 	writecmd = (struct aac_write *) fib_data(fib);
1452 	writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1453 	writecmd->cid = cpu_to_le32(scmd_id(cmd));
1454 	writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1455 	writecmd->count = cpu_to_le32(count *
1456 		dev->fsa_dev[scmd_id(cmd)].block_size);
1457 	writecmd->sg.count = cpu_to_le32(1);
1458 	/* ->stable is not used - it did mean which type of write */
1459 
1460 	ret = aac_build_sg(cmd, &writecmd->sg);
1461 	if (ret < 0)
1462 		return ret;
1463 	fibsize = sizeof(struct aac_write) +
1464 		((le32_to_cpu(writecmd->sg.count) - 1) *
1465 		 sizeof (struct sgentry));
1466 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1467 				sizeof(struct aac_fibhdr)));
1468 	/*
1469 	 *	Now send the Fib to the adapter
1470 	 */
1471 	return aac_fib_send(ContainerCommand,
1472 			  fib,
1473 			  fibsize,
1474 			  FsaNormal,
1475 			  0, 1,
1476 			  (fib_callback) io_callback,
1477 			  (void *) cmd);
1478 }
1479 
1480 static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1481 {
1482 	struct aac_srb * srbcmd;
1483 	u32 flag;
1484 	u32 timeout;
1485 	struct aac_dev *dev = fib->dev;
1486 
1487 	aac_fib_init(fib);
1488 	switch(cmd->sc_data_direction){
1489 	case DMA_TO_DEVICE:
1490 		flag = SRB_DataOut;
1491 		break;
1492 	case DMA_BIDIRECTIONAL:
1493 		flag = SRB_DataIn | SRB_DataOut;
1494 		break;
1495 	case DMA_FROM_DEVICE:
1496 		flag = SRB_DataIn;
1497 		break;
1498 	case DMA_NONE:
1499 	default:	/* shuts up some versions of gcc */
1500 		flag = SRB_NoDataXfer;
1501 		break;
1502 	}
1503 
1504 	srbcmd = (struct aac_srb*) fib_data(fib);
1505 	srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1506 	srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1507 	srbcmd->id       = cpu_to_le32(scmd_id(cmd));
1508 	srbcmd->lun      = cpu_to_le32(cmd->device->lun);
1509 	srbcmd->flags    = cpu_to_le32(flag);
1510 	timeout = cmd->request->timeout/HZ;
1511 	if (timeout == 0)
1512 		timeout = (dev->sa_firmware ? AAC_SA_TIMEOUT : AAC_ARC_TIMEOUT);
1513 	srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1514 	srbcmd->retry_limit = 0; /* Obsolete parameter */
1515 	srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1516 	return srbcmd;
1517 }
1518 
1519 static struct aac_hba_cmd_req *aac_construct_hbacmd(struct fib *fib,
1520 							struct scsi_cmnd *cmd)
1521 {
1522 	struct aac_hba_cmd_req *hbacmd;
1523 	struct aac_dev *dev;
1524 	int bus, target;
1525 	u64 address;
1526 
1527 	dev = (struct aac_dev *)cmd->device->host->hostdata;
1528 
1529 	hbacmd = (struct aac_hba_cmd_req *)fib->hw_fib_va;
1530 	memset(hbacmd, 0, 96);	/* sizeof(*hbacmd) is not necessary */
1531 	/* iu_type is a parameter of aac_hba_send */
1532 	switch (cmd->sc_data_direction) {
1533 	case DMA_TO_DEVICE:
1534 		hbacmd->byte1 = 2;
1535 		break;
1536 	case DMA_FROM_DEVICE:
1537 	case DMA_BIDIRECTIONAL:
1538 		hbacmd->byte1 = 1;
1539 		break;
1540 	case DMA_NONE:
1541 	default:
1542 		break;
1543 	}
1544 	hbacmd->lun[1] = cpu_to_le32(cmd->device->lun);
1545 
1546 	bus = aac_logical_to_phys(scmd_channel(cmd));
1547 	target = scmd_id(cmd);
1548 	hbacmd->it_nexus = dev->hba_map[bus][target].rmw_nexus;
1549 
1550 	/* we fill in reply_qid later in aac_src_deliver_message */
1551 	/* we fill in iu_type, request_id later in aac_hba_send */
1552 	/* we fill in emb_data_desc_count later in aac_build_sghba */
1553 
1554 	memcpy(hbacmd->cdb, cmd->cmnd, cmd->cmd_len);
1555 	hbacmd->data_length = cpu_to_le32(scsi_bufflen(cmd));
1556 
1557 	address = (u64)fib->hw_error_pa;
1558 	hbacmd->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
1559 	hbacmd->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
1560 	hbacmd->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
1561 
1562 	return hbacmd;
1563 }
1564 
1565 static void aac_srb_callback(void *context, struct fib * fibptr);
1566 
1567 static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1568 {
1569 	u16 fibsize;
1570 	struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1571 	long ret;
1572 
1573 	ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
1574 	if (ret < 0)
1575 		return ret;
1576 	srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1577 
1578 	memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1579 	memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1580 	/*
1581 	 *	Build Scatter/Gather list
1582 	 */
1583 	fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1584 		((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1585 		 sizeof (struct sgentry64));
1586 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1587 				sizeof(struct aac_fibhdr)));
1588 
1589 	/*
1590 	 *	Now send the Fib to the adapter
1591 	 */
1592 	return aac_fib_send(ScsiPortCommand64, fib,
1593 				fibsize, FsaNormal, 0, 1,
1594 				  (fib_callback) aac_srb_callback,
1595 				  (void *) cmd);
1596 }
1597 
1598 static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1599 {
1600 	u16 fibsize;
1601 	struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1602 	long ret;
1603 
1604 	ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
1605 	if (ret < 0)
1606 		return ret;
1607 	srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1608 
1609 	memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1610 	memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1611 	/*
1612 	 *	Build Scatter/Gather list
1613 	 */
1614 	fibsize = sizeof (struct aac_srb) +
1615 		(((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1616 		 sizeof (struct sgentry));
1617 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1618 				sizeof(struct aac_fibhdr)));
1619 
1620 	/*
1621 	 *	Now send the Fib to the adapter
1622 	 */
1623 	return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1624 				  (fib_callback) aac_srb_callback, (void *) cmd);
1625 }
1626 
1627 static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1628 {
1629 	if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1630 	    (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1631 		return FAILED;
1632 	return aac_scsi_32(fib, cmd);
1633 }
1634 
1635 static int aac_adapter_hba(struct fib *fib, struct scsi_cmnd *cmd)
1636 {
1637 	struct aac_hba_cmd_req *hbacmd = aac_construct_hbacmd(fib, cmd);
1638 	struct aac_dev *dev;
1639 	long ret;
1640 
1641 	dev = (struct aac_dev *)cmd->device->host->hostdata;
1642 
1643 	ret = aac_build_sghba(cmd, hbacmd,
1644 		dev->scsi_host_ptr->sg_tablesize, (u64)fib->hw_sgl_pa);
1645 	if (ret < 0)
1646 		return ret;
1647 
1648 	/*
1649 	 *	Now send the HBA command to the adapter
1650 	 */
1651 	fib->hbacmd_size = 64 + le32_to_cpu(hbacmd->emb_data_desc_count) *
1652 		sizeof(struct aac_hba_sgl);
1653 
1654 	return aac_hba_send(HBA_IU_TYPE_SCSI_CMD_REQ, fib,
1655 				  (fib_callback) aac_hba_callback,
1656 				  (void *) cmd);
1657 }
1658 
1659 static int aac_send_safw_bmic_cmd(struct aac_dev *dev,
1660 	struct aac_srb_unit *srbu, void *xfer_buf, int xfer_len)
1661 {
1662 	struct fib	*fibptr;
1663 	dma_addr_t	addr;
1664 	int		rcode;
1665 	int		fibsize;
1666 	struct aac_srb	*srb;
1667 	struct aac_srb_reply *srb_reply;
1668 	struct sgmap64	*sg64;
1669 	u32 vbus;
1670 	u32 vid;
1671 
1672 	if (!dev->sa_firmware)
1673 		return 0;
1674 
1675 	/* allocate FIB */
1676 	fibptr = aac_fib_alloc(dev);
1677 	if (!fibptr)
1678 		return -ENOMEM;
1679 
1680 	aac_fib_init(fibptr);
1681 	fibptr->hw_fib_va->header.XferState &=
1682 		~cpu_to_le32(FastResponseCapable);
1683 
1684 	fibsize  = sizeof(struct aac_srb) - sizeof(struct sgentry) +
1685 						sizeof(struct sgentry64);
1686 
1687 	/* allocate DMA buffer for response */
1688 	addr = dma_map_single(&dev->pdev->dev, xfer_buf, xfer_len,
1689 							DMA_BIDIRECTIONAL);
1690 	if (dma_mapping_error(&dev->pdev->dev, addr)) {
1691 		rcode = -ENOMEM;
1692 		goto fib_error;
1693 	}
1694 
1695 	srb = fib_data(fibptr);
1696 	memcpy(srb, &srbu->srb, sizeof(struct aac_srb));
1697 
1698 	vbus = (u32)le16_to_cpu(
1699 			dev->supplement_adapter_info.virt_device_bus);
1700 	vid  = (u32)le16_to_cpu(
1701 			dev->supplement_adapter_info.virt_device_target);
1702 
1703 	/* set the common request fields */
1704 	srb->channel		= cpu_to_le32(vbus);
1705 	srb->id			= cpu_to_le32(vid);
1706 	srb->lun		= 0;
1707 	srb->function		= cpu_to_le32(SRBF_ExecuteScsi);
1708 	srb->timeout		= 0;
1709 	srb->retry_limit	= 0;
1710 	srb->cdb_size		= cpu_to_le32(16);
1711 	srb->count		= cpu_to_le32(xfer_len);
1712 
1713 	sg64 = (struct sgmap64 *)&srb->sg;
1714 	sg64->count		= cpu_to_le32(1);
1715 	sg64->sg[0].addr[1]	= cpu_to_le32(upper_32_bits(addr));
1716 	sg64->sg[0].addr[0]	= cpu_to_le32(lower_32_bits(addr));
1717 	sg64->sg[0].count	= cpu_to_le32(xfer_len);
1718 
1719 	/*
1720 	 * Copy the updated data for other dumping or other usage if needed
1721 	 */
1722 	memcpy(&srbu->srb, srb, sizeof(struct aac_srb));
1723 
1724 	/* issue request to the controller */
1725 	rcode = aac_fib_send(ScsiPortCommand64, fibptr, fibsize, FsaNormal,
1726 					1, 1, NULL, NULL);
1727 
1728 	if (rcode == -ERESTARTSYS)
1729 		rcode = -ERESTART;
1730 
1731 	if (unlikely(rcode < 0))
1732 		goto bmic_error;
1733 
1734 	srb_reply = (struct aac_srb_reply *)fib_data(fibptr);
1735 	memcpy(&srbu->srb_reply, srb_reply, sizeof(struct aac_srb_reply));
1736 
1737 bmic_error:
1738 	dma_unmap_single(&dev->pdev->dev, addr, xfer_len, DMA_BIDIRECTIONAL);
1739 fib_error:
1740 	aac_fib_complete(fibptr);
1741 	aac_fib_free(fibptr);
1742 	return rcode;
1743 }
1744 
1745 static void aac_set_safw_target_qd(struct aac_dev *dev, int bus, int target)
1746 {
1747 
1748 	struct aac_ciss_identify_pd *identify_resp;
1749 
1750 	if (dev->hba_map[bus][target].devtype != AAC_DEVTYPE_NATIVE_RAW)
1751 		return;
1752 
1753 	identify_resp = dev->hba_map[bus][target].safw_identify_resp;
1754 	if (identify_resp == NULL) {
1755 		dev->hba_map[bus][target].qd_limit = 32;
1756 		return;
1757 	}
1758 
1759 	if (identify_resp->current_queue_depth_limit <= 0 ||
1760 		identify_resp->current_queue_depth_limit > 255)
1761 		dev->hba_map[bus][target].qd_limit = 32;
1762 	else
1763 		dev->hba_map[bus][target].qd_limit =
1764 			identify_resp->current_queue_depth_limit;
1765 }
1766 
1767 static int aac_issue_safw_bmic_identify(struct aac_dev *dev,
1768 	struct aac_ciss_identify_pd **identify_resp, u32 bus, u32 target)
1769 {
1770 	int rcode = -ENOMEM;
1771 	int datasize;
1772 	struct aac_srb_unit srbu;
1773 	struct aac_srb *srbcmd;
1774 	struct aac_ciss_identify_pd *identify_reply;
1775 
1776 	datasize = sizeof(struct aac_ciss_identify_pd);
1777 	identify_reply = kmalloc(datasize, GFP_KERNEL);
1778 	if (!identify_reply)
1779 		goto out;
1780 
1781 	memset(&srbu, 0, sizeof(struct aac_srb_unit));
1782 
1783 	srbcmd = &srbu.srb;
1784 	srbcmd->flags	= cpu_to_le32(SRB_DataIn);
1785 	srbcmd->cdb[0]	= 0x26;
1786 	srbcmd->cdb[2]	= (u8)((AAC_MAX_LUN + target) & 0x00FF);
1787 	srbcmd->cdb[6]	= CISS_IDENTIFY_PHYSICAL_DEVICE;
1788 
1789 	rcode = aac_send_safw_bmic_cmd(dev, &srbu, identify_reply, datasize);
1790 	if (unlikely(rcode < 0))
1791 		goto mem_free_all;
1792 
1793 	*identify_resp = identify_reply;
1794 
1795 out:
1796 	return rcode;
1797 mem_free_all:
1798 	kfree(identify_reply);
1799 	goto out;
1800 }
1801 
1802 static inline void aac_free_safw_ciss_luns(struct aac_dev *dev)
1803 {
1804 	kfree(dev->safw_phys_luns);
1805 	dev->safw_phys_luns = NULL;
1806 }
1807 
1808 /**
1809  *	aac_get_safw_ciss_luns()	Process topology change
1810  *	@dev:		aac_dev structure
1811  *
1812  *	Execute a CISS REPORT PHYS LUNS and process the results into
1813  *	the current hba_map.
1814  */
1815 static int aac_get_safw_ciss_luns(struct aac_dev *dev)
1816 {
1817 	int rcode = -ENOMEM;
1818 	int datasize;
1819 	struct aac_srb *srbcmd;
1820 	struct aac_srb_unit srbu;
1821 	struct aac_ciss_phys_luns_resp *phys_luns;
1822 
1823 	datasize = sizeof(struct aac_ciss_phys_luns_resp) +
1824 		(AAC_MAX_TARGETS - 1) * sizeof(struct _ciss_lun);
1825 	phys_luns = kmalloc(datasize, GFP_KERNEL);
1826 	if (phys_luns == NULL)
1827 		goto out;
1828 
1829 	memset(&srbu, 0, sizeof(struct aac_srb_unit));
1830 
1831 	srbcmd = &srbu.srb;
1832 	srbcmd->flags	= cpu_to_le32(SRB_DataIn);
1833 	srbcmd->cdb[0]	= CISS_REPORT_PHYSICAL_LUNS;
1834 	srbcmd->cdb[1]	= 2; /* extended reporting */
1835 	srbcmd->cdb[8]	= (u8)(datasize >> 8);
1836 	srbcmd->cdb[9]	= (u8)(datasize);
1837 
1838 	rcode = aac_send_safw_bmic_cmd(dev, &srbu, phys_luns, datasize);
1839 	if (unlikely(rcode < 0))
1840 		goto mem_free_all;
1841 
1842 	if (phys_luns->resp_flag != 2) {
1843 		rcode = -ENOMSG;
1844 		goto mem_free_all;
1845 	}
1846 
1847 	dev->safw_phys_luns = phys_luns;
1848 
1849 out:
1850 	return rcode;
1851 mem_free_all:
1852 	kfree(phys_luns);
1853 	goto out;
1854 }
1855 
1856 static inline u32 aac_get_safw_phys_lun_count(struct aac_dev *dev)
1857 {
1858 	return get_unaligned_be32(&dev->safw_phys_luns->list_length[0])/24;
1859 }
1860 
1861 static inline u32 aac_get_safw_phys_bus(struct aac_dev *dev, int lun)
1862 {
1863 	return dev->safw_phys_luns->lun[lun].level2[1] & 0x3f;
1864 }
1865 
1866 static inline u32 aac_get_safw_phys_target(struct aac_dev *dev, int lun)
1867 {
1868 	return dev->safw_phys_luns->lun[lun].level2[0];
1869 }
1870 
1871 static inline u32 aac_get_safw_phys_expose_flag(struct aac_dev *dev, int lun)
1872 {
1873 	return dev->safw_phys_luns->lun[lun].bus >> 6;
1874 }
1875 
1876 static inline u32 aac_get_safw_phys_attribs(struct aac_dev *dev, int lun)
1877 {
1878 	return dev->safw_phys_luns->lun[lun].node_ident[9];
1879 }
1880 
1881 static inline u32 aac_get_safw_phys_nexus(struct aac_dev *dev, int lun)
1882 {
1883 	return *((u32 *)&dev->safw_phys_luns->lun[lun].node_ident[12]);
1884 }
1885 
1886 static inline u32 aac_get_safw_phys_device_type(struct aac_dev *dev, int lun)
1887 {
1888 	return dev->safw_phys_luns->lun[lun].node_ident[8];
1889 }
1890 
1891 static inline void aac_free_safw_identify_resp(struct aac_dev *dev,
1892 						int bus, int target)
1893 {
1894 	kfree(dev->hba_map[bus][target].safw_identify_resp);
1895 	dev->hba_map[bus][target].safw_identify_resp = NULL;
1896 }
1897 
1898 static inline void aac_free_safw_all_identify_resp(struct aac_dev *dev,
1899 	int lun_count)
1900 {
1901 	int luns;
1902 	int i;
1903 	u32 bus;
1904 	u32 target;
1905 
1906 	luns = aac_get_safw_phys_lun_count(dev);
1907 
1908 	if (luns < lun_count)
1909 		lun_count = luns;
1910 	else if (lun_count < 0)
1911 		lun_count = luns;
1912 
1913 	for (i = 0; i < lun_count; i++) {
1914 		bus = aac_get_safw_phys_bus(dev, i);
1915 		target = aac_get_safw_phys_target(dev, i);
1916 
1917 		aac_free_safw_identify_resp(dev, bus, target);
1918 	}
1919 }
1920 
1921 static int aac_get_safw_attr_all_targets(struct aac_dev *dev)
1922 {
1923 	int i;
1924 	int rcode = 0;
1925 	u32 lun_count;
1926 	u32 bus;
1927 	u32 target;
1928 	struct aac_ciss_identify_pd *identify_resp = NULL;
1929 
1930 	lun_count = aac_get_safw_phys_lun_count(dev);
1931 
1932 	for (i = 0; i < lun_count; ++i) {
1933 
1934 		bus = aac_get_safw_phys_bus(dev, i);
1935 		target = aac_get_safw_phys_target(dev, i);
1936 
1937 		rcode = aac_issue_safw_bmic_identify(dev,
1938 						&identify_resp, bus, target);
1939 
1940 		if (unlikely(rcode < 0))
1941 			goto free_identify_resp;
1942 
1943 		dev->hba_map[bus][target].safw_identify_resp = identify_resp;
1944 	}
1945 
1946 out:
1947 	return rcode;
1948 free_identify_resp:
1949 	aac_free_safw_all_identify_resp(dev, i);
1950 	goto out;
1951 }
1952 
1953 /**
1954  *	aac_set_safw_attr_all_targets-	update current hba map with data from FW
1955  *	@dev:	aac_dev structure
1956  *	@phys_luns: FW information from report phys luns
1957  *	@rescan: Indicates scan type
1958  *
1959  *	Update our hba map with the information gathered from the FW
1960  */
1961 static void aac_set_safw_attr_all_targets(struct aac_dev *dev)
1962 {
1963 	/* ok and extended reporting */
1964 	u32 lun_count, nexus;
1965 	u32 i, bus, target;
1966 	u8 expose_flag, attribs;
1967 
1968 	lun_count = aac_get_safw_phys_lun_count(dev);
1969 
1970 	dev->scan_counter++;
1971 
1972 	for (i = 0; i < lun_count; ++i) {
1973 
1974 		bus = aac_get_safw_phys_bus(dev, i);
1975 		target = aac_get_safw_phys_target(dev, i);
1976 		expose_flag = aac_get_safw_phys_expose_flag(dev, i);
1977 		attribs = aac_get_safw_phys_attribs(dev, i);
1978 		nexus = aac_get_safw_phys_nexus(dev, i);
1979 
1980 		if (bus >= AAC_MAX_BUSES || target >= AAC_MAX_TARGETS)
1981 			continue;
1982 
1983 		if (expose_flag != 0) {
1984 			dev->hba_map[bus][target].devtype =
1985 				AAC_DEVTYPE_RAID_MEMBER;
1986 			continue;
1987 		}
1988 
1989 		if (nexus != 0 && (attribs & 8)) {
1990 			dev->hba_map[bus][target].devtype =
1991 				AAC_DEVTYPE_NATIVE_RAW;
1992 			dev->hba_map[bus][target].rmw_nexus =
1993 					nexus;
1994 		} else
1995 			dev->hba_map[bus][target].devtype =
1996 				AAC_DEVTYPE_ARC_RAW;
1997 
1998 		dev->hba_map[bus][target].scan_counter = dev->scan_counter;
1999 
2000 		aac_set_safw_target_qd(dev, bus, target);
2001 	}
2002 }
2003 
2004 static int aac_setup_safw_targets(struct aac_dev *dev)
2005 {
2006 	int rcode = 0;
2007 
2008 	rcode = aac_get_containers(dev);
2009 	if (unlikely(rcode < 0))
2010 		goto out;
2011 
2012 	rcode = aac_get_safw_ciss_luns(dev);
2013 	if (unlikely(rcode < 0))
2014 		goto out;
2015 
2016 	rcode = aac_get_safw_attr_all_targets(dev);
2017 	if (unlikely(rcode < 0))
2018 		goto free_ciss_luns;
2019 
2020 	aac_set_safw_attr_all_targets(dev);
2021 
2022 	aac_free_safw_all_identify_resp(dev, -1);
2023 free_ciss_luns:
2024 	aac_free_safw_ciss_luns(dev);
2025 out:
2026 	return rcode;
2027 }
2028 
2029 int aac_setup_safw_adapter(struct aac_dev *dev)
2030 {
2031 	return aac_setup_safw_targets(dev);
2032 }
2033 
2034 int aac_get_adapter_info(struct aac_dev* dev)
2035 {
2036 	struct fib* fibptr;
2037 	int rcode;
2038 	u32 tmp, bus, target;
2039 	struct aac_adapter_info *info;
2040 	struct aac_bus_info *command;
2041 	struct aac_bus_info_response *bus_info;
2042 
2043 	if (!(fibptr = aac_fib_alloc(dev)))
2044 		return -ENOMEM;
2045 
2046 	aac_fib_init(fibptr);
2047 	info = (struct aac_adapter_info *) fib_data(fibptr);
2048 	memset(info,0,sizeof(*info));
2049 
2050 	rcode = aac_fib_send(RequestAdapterInfo,
2051 			 fibptr,
2052 			 sizeof(*info),
2053 			 FsaNormal,
2054 			 -1, 1, /* First `interrupt' command uses special wait */
2055 			 NULL,
2056 			 NULL);
2057 
2058 	if (rcode < 0) {
2059 		/* FIB should be freed only after
2060 		 * getting the response from the F/W */
2061 		if (rcode != -ERESTARTSYS) {
2062 			aac_fib_complete(fibptr);
2063 			aac_fib_free(fibptr);
2064 		}
2065 		return rcode;
2066 	}
2067 	memcpy(&dev->adapter_info, info, sizeof(*info));
2068 
2069 	dev->supplement_adapter_info.virt_device_bus = 0xffff;
2070 	if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
2071 		struct aac_supplement_adapter_info * sinfo;
2072 
2073 		aac_fib_init(fibptr);
2074 
2075 		sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
2076 
2077 		memset(sinfo,0,sizeof(*sinfo));
2078 
2079 		rcode = aac_fib_send(RequestSupplementAdapterInfo,
2080 				 fibptr,
2081 				 sizeof(*sinfo),
2082 				 FsaNormal,
2083 				 1, 1,
2084 				 NULL,
2085 				 NULL);
2086 
2087 		if (rcode >= 0)
2088 			memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
2089 		if (rcode == -ERESTARTSYS) {
2090 			fibptr = aac_fib_alloc(dev);
2091 			if (!fibptr)
2092 				return -ENOMEM;
2093 		}
2094 
2095 	}
2096 
2097 	/* reset all previous mapped devices (i.e. for init. after IOP_RESET) */
2098 	for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
2099 		for (target = 0; target < AAC_MAX_TARGETS; target++) {
2100 			dev->hba_map[bus][target].devtype = 0;
2101 			dev->hba_map[bus][target].qd_limit = 0;
2102 		}
2103 	}
2104 
2105 	/*
2106 	 * GetBusInfo
2107 	 */
2108 
2109 	aac_fib_init(fibptr);
2110 
2111 	bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
2112 
2113 	memset(bus_info, 0, sizeof(*bus_info));
2114 
2115 	command = (struct aac_bus_info *)bus_info;
2116 
2117 	command->Command = cpu_to_le32(VM_Ioctl);
2118 	command->ObjType = cpu_to_le32(FT_DRIVE);
2119 	command->MethodId = cpu_to_le32(1);
2120 	command->CtlCmd = cpu_to_le32(GetBusInfo);
2121 
2122 	rcode = aac_fib_send(ContainerCommand,
2123 			 fibptr,
2124 			 sizeof (*bus_info),
2125 			 FsaNormal,
2126 			 1, 1,
2127 			 NULL, NULL);
2128 
2129 	/* reasoned default */
2130 	dev->maximum_num_physicals = 16;
2131 	if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
2132 		dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
2133 		dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
2134 	}
2135 
2136 	if (!dev->in_reset) {
2137 		char buffer[16];
2138 		tmp = le32_to_cpu(dev->adapter_info.kernelrev);
2139 		printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
2140 			dev->name,
2141 			dev->id,
2142 			tmp>>24,
2143 			(tmp>>16)&0xff,
2144 			tmp&0xff,
2145 			le32_to_cpu(dev->adapter_info.kernelbuild),
2146 			(int)sizeof(dev->supplement_adapter_info.build_date),
2147 			dev->supplement_adapter_info.build_date);
2148 		tmp = le32_to_cpu(dev->adapter_info.monitorrev);
2149 		printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
2150 			dev->name, dev->id,
2151 			tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2152 			le32_to_cpu(dev->adapter_info.monitorbuild));
2153 		tmp = le32_to_cpu(dev->adapter_info.biosrev);
2154 		printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
2155 			dev->name, dev->id,
2156 			tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2157 			le32_to_cpu(dev->adapter_info.biosbuild));
2158 		buffer[0] = '\0';
2159 		if (aac_get_serial_number(
2160 		  shost_to_class(dev->scsi_host_ptr), buffer))
2161 			printk(KERN_INFO "%s%d: serial %s",
2162 			  dev->name, dev->id, buffer);
2163 		if (dev->supplement_adapter_info.vpd_info.tsid[0]) {
2164 			printk(KERN_INFO "%s%d: TSID %.*s\n",
2165 			  dev->name, dev->id,
2166 			  (int)sizeof(dev->supplement_adapter_info
2167 							.vpd_info.tsid),
2168 				dev->supplement_adapter_info.vpd_info.tsid);
2169 		}
2170 		if (!aac_check_reset || ((aac_check_reset == 1) &&
2171 		  (dev->supplement_adapter_info.supported_options2 &
2172 		  AAC_OPTION_IGNORE_RESET))) {
2173 			printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
2174 			  dev->name, dev->id);
2175 		}
2176 	}
2177 
2178 	dev->cache_protected = 0;
2179 	dev->jbod = ((dev->supplement_adapter_info.feature_bits &
2180 		AAC_FEATURE_JBOD) != 0);
2181 	dev->nondasd_support = 0;
2182 	dev->raid_scsi_mode = 0;
2183 	if(dev->adapter_info.options & AAC_OPT_NONDASD)
2184 		dev->nondasd_support = 1;
2185 
2186 	/*
2187 	 * If the firmware supports ROMB RAID/SCSI mode and we are currently
2188 	 * in RAID/SCSI mode, set the flag. For now if in this mode we will
2189 	 * force nondasd support on. If we decide to allow the non-dasd flag
2190 	 * additional changes changes will have to be made to support
2191 	 * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
2192 	 * changed to support the new dev->raid_scsi_mode flag instead of
2193 	 * leaching off of the dev->nondasd_support flag. Also in linit.c the
2194 	 * function aac_detect will have to be modified where it sets up the
2195 	 * max number of channels based on the aac->nondasd_support flag only.
2196 	 */
2197 	if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
2198 	    (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
2199 		dev->nondasd_support = 1;
2200 		dev->raid_scsi_mode = 1;
2201 	}
2202 	if (dev->raid_scsi_mode != 0)
2203 		printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
2204 				dev->name, dev->id);
2205 
2206 	if (nondasd != -1)
2207 		dev->nondasd_support = (nondasd!=0);
2208 	if (dev->nondasd_support && !dev->in_reset)
2209 		printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
2210 
2211 	if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
2212 		dev->needs_dac = 1;
2213 	dev->dac_support = 0;
2214 	if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
2215 	    (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
2216 		if (!dev->in_reset)
2217 			printk(KERN_INFO "%s%d: 64bit support enabled.\n",
2218 				dev->name, dev->id);
2219 		dev->dac_support = 1;
2220 	}
2221 
2222 	if(dacmode != -1) {
2223 		dev->dac_support = (dacmode!=0);
2224 	}
2225 
2226 	/* avoid problems with AAC_QUIRK_SCSI_32 controllers */
2227 	if (dev->dac_support &&	(aac_get_driver_ident(dev->cardtype)->quirks
2228 		& AAC_QUIRK_SCSI_32)) {
2229 		dev->nondasd_support = 0;
2230 		dev->jbod = 0;
2231 		expose_physicals = 0;
2232 	}
2233 
2234 	if (dev->dac_support) {
2235 		if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
2236 			if (!dev->in_reset)
2237 				dev_info(&dev->pdev->dev, "64 Bit DAC enabled\n");
2238 		} else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
2239 			dev_info(&dev->pdev->dev, "DMA mask set failed, 64 Bit DAC disabled\n");
2240 			dev->dac_support = 0;
2241 		} else {
2242 			dev_info(&dev->pdev->dev, "No suitable DMA available\n");
2243 			rcode = -ENOMEM;
2244 		}
2245 	}
2246 	/*
2247 	 * Deal with configuring for the individualized limits of each packet
2248 	 * interface.
2249 	 */
2250 	dev->a_ops.adapter_scsi = (dev->dac_support)
2251 	  ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
2252 				? aac_scsi_32_64
2253 				: aac_scsi_64)
2254 				: aac_scsi_32;
2255 	if (dev->raw_io_interface) {
2256 		dev->a_ops.adapter_bounds = (dev->raw_io_64)
2257 					? aac_bounds_64
2258 					: aac_bounds_32;
2259 		dev->a_ops.adapter_read = aac_read_raw_io;
2260 		dev->a_ops.adapter_write = aac_write_raw_io;
2261 	} else {
2262 		dev->a_ops.adapter_bounds = aac_bounds_32;
2263 		dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
2264 			sizeof(struct aac_fibhdr) -
2265 			sizeof(struct aac_write) + sizeof(struct sgentry)) /
2266 				sizeof(struct sgentry);
2267 		if (dev->dac_support) {
2268 			dev->a_ops.adapter_read = aac_read_block64;
2269 			dev->a_ops.adapter_write = aac_write_block64;
2270 			/*
2271 			 * 38 scatter gather elements
2272 			 */
2273 			dev->scsi_host_ptr->sg_tablesize =
2274 				(dev->max_fib_size -
2275 				sizeof(struct aac_fibhdr) -
2276 				sizeof(struct aac_write64) +
2277 				sizeof(struct sgentry64)) /
2278 					sizeof(struct sgentry64);
2279 		} else {
2280 			dev->a_ops.adapter_read = aac_read_block;
2281 			dev->a_ops.adapter_write = aac_write_block;
2282 		}
2283 		dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
2284 		if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
2285 			/*
2286 			 * Worst case size that could cause sg overflow when
2287 			 * we break up SG elements that are larger than 64KB.
2288 			 * Would be nice if we could tell the SCSI layer what
2289 			 * the maximum SG element size can be. Worst case is
2290 			 * (sg_tablesize-1) 4KB elements with one 64KB
2291 			 * element.
2292 			 *	32bit -> 468 or 238KB	64bit -> 424 or 212KB
2293 			 */
2294 			dev->scsi_host_ptr->max_sectors =
2295 			  (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
2296 		}
2297 	}
2298 	if (!dev->sync_mode && dev->sa_firmware &&
2299 		dev->scsi_host_ptr->sg_tablesize > HBA_MAX_SG_SEPARATE)
2300 		dev->scsi_host_ptr->sg_tablesize = dev->sg_tablesize =
2301 			HBA_MAX_SG_SEPARATE;
2302 
2303 	/* FIB should be freed only after getting the response from the F/W */
2304 	if (rcode != -ERESTARTSYS) {
2305 		aac_fib_complete(fibptr);
2306 		aac_fib_free(fibptr);
2307 	}
2308 
2309 	return rcode;
2310 }
2311 
2312 
2313 static void io_callback(void *context, struct fib * fibptr)
2314 {
2315 	struct aac_dev *dev;
2316 	struct aac_read_reply *readreply;
2317 	struct scsi_cmnd *scsicmd;
2318 	u32 cid;
2319 
2320 	scsicmd = (struct scsi_cmnd *) context;
2321 
2322 	if (!aac_valid_context(scsicmd, fibptr))
2323 		return;
2324 
2325 	dev = fibptr->dev;
2326 	cid = scmd_id(scsicmd);
2327 
2328 	if (nblank(dprintk(x))) {
2329 		u64 lba;
2330 		switch (scsicmd->cmnd[0]) {
2331 		case WRITE_6:
2332 		case READ_6:
2333 			lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2334 			    (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2335 			break;
2336 		case WRITE_16:
2337 		case READ_16:
2338 			lba = ((u64)scsicmd->cmnd[2] << 56) |
2339 			      ((u64)scsicmd->cmnd[3] << 48) |
2340 			      ((u64)scsicmd->cmnd[4] << 40) |
2341 			      ((u64)scsicmd->cmnd[5] << 32) |
2342 			      ((u64)scsicmd->cmnd[6] << 24) |
2343 			      (scsicmd->cmnd[7] << 16) |
2344 			      (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2345 			break;
2346 		case WRITE_12:
2347 		case READ_12:
2348 			lba = ((u64)scsicmd->cmnd[2] << 24) |
2349 			      (scsicmd->cmnd[3] << 16) |
2350 			      (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2351 			break;
2352 		default:
2353 			lba = ((u64)scsicmd->cmnd[2] << 24) |
2354 			       (scsicmd->cmnd[3] << 16) |
2355 			       (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2356 			break;
2357 		}
2358 		printk(KERN_DEBUG
2359 		  "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
2360 		  smp_processor_id(), (unsigned long long)lba, jiffies);
2361 	}
2362 
2363 	BUG_ON(fibptr == NULL);
2364 
2365 	scsi_dma_unmap(scsicmd);
2366 
2367 	readreply = (struct aac_read_reply *)fib_data(fibptr);
2368 	switch (le32_to_cpu(readreply->status)) {
2369 	case ST_OK:
2370 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2371 			SAM_STAT_GOOD;
2372 		dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
2373 		break;
2374 	case ST_NOT_READY:
2375 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2376 			SAM_STAT_CHECK_CONDITION;
2377 		set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
2378 		  SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
2379 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2380 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2381 			     SCSI_SENSE_BUFFERSIZE));
2382 		break;
2383 	case ST_MEDERR:
2384 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2385 			SAM_STAT_CHECK_CONDITION;
2386 		set_sense(&dev->fsa_dev[cid].sense_data, MEDIUM_ERROR,
2387 		  SENCODE_UNRECOVERED_READ_ERROR, ASENCODE_NO_SENSE, 0, 0);
2388 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2389 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2390 			     SCSI_SENSE_BUFFERSIZE));
2391 		break;
2392 	default:
2393 #ifdef AAC_DETAILED_STATUS_INFO
2394 		printk(KERN_WARNING "io_callback: io failed, status = %d\n",
2395 		  le32_to_cpu(readreply->status));
2396 #endif
2397 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2398 			SAM_STAT_CHECK_CONDITION;
2399 		set_sense(&dev->fsa_dev[cid].sense_data,
2400 		  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2401 		  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2402 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2403 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2404 			     SCSI_SENSE_BUFFERSIZE));
2405 		break;
2406 	}
2407 	aac_fib_complete(fibptr);
2408 
2409 	scsicmd->scsi_done(scsicmd);
2410 }
2411 
2412 static int aac_read(struct scsi_cmnd * scsicmd)
2413 {
2414 	u64 lba;
2415 	u32 count;
2416 	int status;
2417 	struct aac_dev *dev;
2418 	struct fib * cmd_fibcontext;
2419 	int cid;
2420 
2421 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2422 	/*
2423 	 *	Get block address and transfer length
2424 	 */
2425 	switch (scsicmd->cmnd[0]) {
2426 	case READ_6:
2427 		dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
2428 
2429 		lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2430 			(scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2431 		count = scsicmd->cmnd[4];
2432 
2433 		if (count == 0)
2434 			count = 256;
2435 		break;
2436 	case READ_16:
2437 		dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
2438 
2439 		lba =	((u64)scsicmd->cmnd[2] << 56) |
2440 			((u64)scsicmd->cmnd[3] << 48) |
2441 			((u64)scsicmd->cmnd[4] << 40) |
2442 			((u64)scsicmd->cmnd[5] << 32) |
2443 			((u64)scsicmd->cmnd[6] << 24) |
2444 			(scsicmd->cmnd[7] << 16) |
2445 			(scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2446 		count = (scsicmd->cmnd[10] << 24) |
2447 			(scsicmd->cmnd[11] << 16) |
2448 			(scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2449 		break;
2450 	case READ_12:
2451 		dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
2452 
2453 		lba = ((u64)scsicmd->cmnd[2] << 24) |
2454 			(scsicmd->cmnd[3] << 16) |
2455 			(scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2456 		count = (scsicmd->cmnd[6] << 24) |
2457 			(scsicmd->cmnd[7] << 16) |
2458 			(scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2459 		break;
2460 	default:
2461 		dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
2462 
2463 		lba = ((u64)scsicmd->cmnd[2] << 24) |
2464 			(scsicmd->cmnd[3] << 16) |
2465 			(scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2466 		count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2467 		break;
2468 	}
2469 
2470 	if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2471 		cid = scmd_id(scsicmd);
2472 		dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2473 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2474 			SAM_STAT_CHECK_CONDITION;
2475 		set_sense(&dev->fsa_dev[cid].sense_data,
2476 			  ILLEGAL_REQUEST, SENCODE_LBA_OUT_OF_RANGE,
2477 			  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2478 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2479 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2480 			     SCSI_SENSE_BUFFERSIZE));
2481 		scsicmd->scsi_done(scsicmd);
2482 		return 0;
2483 	}
2484 
2485 	dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
2486 	  smp_processor_id(), (unsigned long long)lba, jiffies));
2487 	if (aac_adapter_bounds(dev,scsicmd,lba))
2488 		return 0;
2489 	/*
2490 	 *	Alocate and initialize a Fib
2491 	 */
2492 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2493 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2494 	status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
2495 
2496 	/*
2497 	 *	Check that the command queued to the controller
2498 	 */
2499 	if (status == -EINPROGRESS)
2500 		return 0;
2501 
2502 	printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
2503 	/*
2504 	 *	For some reason, the Fib didn't queue, return QUEUE_FULL
2505 	 */
2506 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
2507 	scsicmd->scsi_done(scsicmd);
2508 	aac_fib_complete(cmd_fibcontext);
2509 	aac_fib_free(cmd_fibcontext);
2510 	return 0;
2511 }
2512 
2513 static int aac_write(struct scsi_cmnd * scsicmd)
2514 {
2515 	u64 lba;
2516 	u32 count;
2517 	int fua;
2518 	int status;
2519 	struct aac_dev *dev;
2520 	struct fib * cmd_fibcontext;
2521 	int cid;
2522 
2523 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2524 	/*
2525 	 *	Get block address and transfer length
2526 	 */
2527 	if (scsicmd->cmnd[0] == WRITE_6)	/* 6 byte command */
2528 	{
2529 		lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2530 		count = scsicmd->cmnd[4];
2531 		if (count == 0)
2532 			count = 256;
2533 		fua = 0;
2534 	} else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
2535 		dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
2536 
2537 		lba =	((u64)scsicmd->cmnd[2] << 56) |
2538 			((u64)scsicmd->cmnd[3] << 48) |
2539 			((u64)scsicmd->cmnd[4] << 40) |
2540 			((u64)scsicmd->cmnd[5] << 32) |
2541 			((u64)scsicmd->cmnd[6] << 24) |
2542 			(scsicmd->cmnd[7] << 16) |
2543 			(scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2544 		count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
2545 			(scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2546 		fua = scsicmd->cmnd[1] & 0x8;
2547 	} else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
2548 		dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
2549 
2550 		lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
2551 		    | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2552 		count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
2553 		      | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2554 		fua = scsicmd->cmnd[1] & 0x8;
2555 	} else {
2556 		dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
2557 		lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2558 		count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2559 		fua = scsicmd->cmnd[1] & 0x8;
2560 	}
2561 
2562 	if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2563 		cid = scmd_id(scsicmd);
2564 		dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2565 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2566 			SAM_STAT_CHECK_CONDITION;
2567 		set_sense(&dev->fsa_dev[cid].sense_data,
2568 			  ILLEGAL_REQUEST, SENCODE_LBA_OUT_OF_RANGE,
2569 			  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2570 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2571 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2572 			     SCSI_SENSE_BUFFERSIZE));
2573 		scsicmd->scsi_done(scsicmd);
2574 		return 0;
2575 	}
2576 
2577 	dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
2578 	  smp_processor_id(), (unsigned long long)lba, jiffies));
2579 	if (aac_adapter_bounds(dev,scsicmd,lba))
2580 		return 0;
2581 	/*
2582 	 *	Allocate and initialize a Fib then setup a BlockWrite command
2583 	 */
2584 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2585 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2586 	status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
2587 
2588 	/*
2589 	 *	Check that the command queued to the controller
2590 	 */
2591 	if (status == -EINPROGRESS)
2592 		return 0;
2593 
2594 	printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
2595 	/*
2596 	 *	For some reason, the Fib didn't queue, return QUEUE_FULL
2597 	 */
2598 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
2599 	scsicmd->scsi_done(scsicmd);
2600 
2601 	aac_fib_complete(cmd_fibcontext);
2602 	aac_fib_free(cmd_fibcontext);
2603 	return 0;
2604 }
2605 
2606 static void synchronize_callback(void *context, struct fib *fibptr)
2607 {
2608 	struct aac_synchronize_reply *synchronizereply;
2609 	struct scsi_cmnd *cmd = context;
2610 
2611 	if (!aac_valid_context(cmd, fibptr))
2612 		return;
2613 
2614 	dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
2615 				smp_processor_id(), jiffies));
2616 	BUG_ON(fibptr == NULL);
2617 
2618 
2619 	synchronizereply = fib_data(fibptr);
2620 	if (le32_to_cpu(synchronizereply->status) == CT_OK)
2621 		cmd->result = DID_OK << 16 |
2622 			COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2623 	else {
2624 		struct scsi_device *sdev = cmd->device;
2625 		struct aac_dev *dev = fibptr->dev;
2626 		u32 cid = sdev_id(sdev);
2627 		printk(KERN_WARNING
2628 		     "synchronize_callback: synchronize failed, status = %d\n",
2629 		     le32_to_cpu(synchronizereply->status));
2630 		cmd->result = DID_OK << 16 |
2631 			COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2632 		set_sense(&dev->fsa_dev[cid].sense_data,
2633 		  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2634 		  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2635 		memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2636 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2637 			     SCSI_SENSE_BUFFERSIZE));
2638 	}
2639 
2640 	aac_fib_complete(fibptr);
2641 	aac_fib_free(fibptr);
2642 	cmd->scsi_done(cmd);
2643 }
2644 
2645 static int aac_synchronize(struct scsi_cmnd *scsicmd)
2646 {
2647 	int status;
2648 	struct fib *cmd_fibcontext;
2649 	struct aac_synchronize *synchronizecmd;
2650 	struct scsi_device *sdev = scsicmd->device;
2651 	struct aac_dev *aac;
2652 
2653 	aac = (struct aac_dev *)sdev->host->hostdata;
2654 	if (aac->in_reset)
2655 		return SCSI_MLQUEUE_HOST_BUSY;
2656 
2657 	/*
2658 	 *	Allocate and initialize a Fib
2659 	 */
2660 	cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
2661 
2662 	aac_fib_init(cmd_fibcontext);
2663 
2664 	synchronizecmd = fib_data(cmd_fibcontext);
2665 	synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
2666 	synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
2667 	synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
2668 	synchronizecmd->count =
2669 	     cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
2670 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2671 
2672 	/*
2673 	 *	Now send the Fib to the adapter
2674 	 */
2675 	status = aac_fib_send(ContainerCommand,
2676 		  cmd_fibcontext,
2677 		  sizeof(struct aac_synchronize),
2678 		  FsaNormal,
2679 		  0, 1,
2680 		  (fib_callback)synchronize_callback,
2681 		  (void *)scsicmd);
2682 
2683 	/*
2684 	 *	Check that the command queued to the controller
2685 	 */
2686 	if (status == -EINPROGRESS)
2687 		return 0;
2688 
2689 	printk(KERN_WARNING
2690 		"aac_synchronize: aac_fib_send failed with status: %d.\n", status);
2691 	aac_fib_complete(cmd_fibcontext);
2692 	aac_fib_free(cmd_fibcontext);
2693 	return SCSI_MLQUEUE_HOST_BUSY;
2694 }
2695 
2696 static void aac_start_stop_callback(void *context, struct fib *fibptr)
2697 {
2698 	struct scsi_cmnd *scsicmd = context;
2699 
2700 	if (!aac_valid_context(scsicmd, fibptr))
2701 		return;
2702 
2703 	BUG_ON(fibptr == NULL);
2704 
2705 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2706 
2707 	aac_fib_complete(fibptr);
2708 	aac_fib_free(fibptr);
2709 	scsicmd->scsi_done(scsicmd);
2710 }
2711 
2712 static int aac_start_stop(struct scsi_cmnd *scsicmd)
2713 {
2714 	int status;
2715 	struct fib *cmd_fibcontext;
2716 	struct aac_power_management *pmcmd;
2717 	struct scsi_device *sdev = scsicmd->device;
2718 	struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2719 
2720 	if (!(aac->supplement_adapter_info.supported_options2 &
2721 	      AAC_OPTION_POWER_MANAGEMENT)) {
2722 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2723 				  SAM_STAT_GOOD;
2724 		scsicmd->scsi_done(scsicmd);
2725 		return 0;
2726 	}
2727 
2728 	if (aac->in_reset)
2729 		return SCSI_MLQUEUE_HOST_BUSY;
2730 
2731 	/*
2732 	 *	Allocate and initialize a Fib
2733 	 */
2734 	cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
2735 
2736 	aac_fib_init(cmd_fibcontext);
2737 
2738 	pmcmd = fib_data(cmd_fibcontext);
2739 	pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2740 	pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2741 	/* Eject bit ignored, not relevant */
2742 	pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2743 		cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2744 	pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2745 	pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2746 		cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2747 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2748 
2749 	/*
2750 	 *	Now send the Fib to the adapter
2751 	 */
2752 	status = aac_fib_send(ContainerCommand,
2753 		  cmd_fibcontext,
2754 		  sizeof(struct aac_power_management),
2755 		  FsaNormal,
2756 		  0, 1,
2757 		  (fib_callback)aac_start_stop_callback,
2758 		  (void *)scsicmd);
2759 
2760 	/*
2761 	 *	Check that the command queued to the controller
2762 	 */
2763 	if (status == -EINPROGRESS)
2764 		return 0;
2765 
2766 	aac_fib_complete(cmd_fibcontext);
2767 	aac_fib_free(cmd_fibcontext);
2768 	return SCSI_MLQUEUE_HOST_BUSY;
2769 }
2770 
2771 /**
2772  *	aac_scsi_cmd()		-	Process SCSI command
2773  *	@scsicmd:		SCSI command block
2774  *
2775  *	Emulate a SCSI command and queue the required request for the
2776  *	aacraid firmware.
2777  */
2778 
2779 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2780 {
2781 	u32 cid, bus;
2782 	struct Scsi_Host *host = scsicmd->device->host;
2783 	struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2784 	struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
2785 
2786 	if (fsa_dev_ptr == NULL)
2787 		return -1;
2788 	/*
2789 	 *	If the bus, id or lun is out of range, return fail
2790 	 *	Test does not apply to ID 16, the pseudo id for the controller
2791 	 *	itself.
2792 	 */
2793 	cid = scmd_id(scsicmd);
2794 	if (cid != host->this_id) {
2795 		if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2796 			if((cid >= dev->maximum_num_containers) ||
2797 					(scsicmd->device->lun != 0)) {
2798 				scsicmd->result = DID_NO_CONNECT << 16;
2799 				goto scsi_done_ret;
2800 			}
2801 
2802 			/*
2803 			 *	If the target container doesn't exist, it may have
2804 			 *	been newly created
2805 			 */
2806 			if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2807 			  (fsa_dev_ptr[cid].sense_data.sense_key ==
2808 			   NOT_READY)) {
2809 				switch (scsicmd->cmnd[0]) {
2810 				case SERVICE_ACTION_IN_16:
2811 					if (!(dev->raw_io_interface) ||
2812 					    !(dev->raw_io_64) ||
2813 					    ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2814 						break;
2815 					/* fall through */
2816 				case INQUIRY:
2817 				case READ_CAPACITY:
2818 				case TEST_UNIT_READY:
2819 					if (dev->in_reset)
2820 						return -1;
2821 					return _aac_probe_container(scsicmd,
2822 							aac_probe_container_callback2);
2823 				default:
2824 					break;
2825 				}
2826 			}
2827 		} else {  /* check for physical non-dasd devices */
2828 			bus = aac_logical_to_phys(scmd_channel(scsicmd));
2829 
2830 			if (bus < AAC_MAX_BUSES && cid < AAC_MAX_TARGETS &&
2831 				dev->hba_map[bus][cid].devtype
2832 					== AAC_DEVTYPE_NATIVE_RAW) {
2833 				if (dev->in_reset)
2834 					return -1;
2835 				return aac_send_hba_fib(scsicmd);
2836 			} else if (dev->nondasd_support || expose_physicals ||
2837 				dev->jbod) {
2838 				if (dev->in_reset)
2839 					return -1;
2840 				return aac_send_srb_fib(scsicmd);
2841 			} else {
2842 				scsicmd->result = DID_NO_CONNECT << 16;
2843 				goto scsi_done_ret;
2844 			}
2845 		}
2846 	}
2847 	/*
2848 	 * else Command for the controller itself
2849 	 */
2850 	else if ((scsicmd->cmnd[0] != INQUIRY) &&	/* only INQUIRY & TUR cmnd supported for controller */
2851 		(scsicmd->cmnd[0] != TEST_UNIT_READY))
2852 	{
2853 		dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2854 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2855 		set_sense(&dev->fsa_dev[cid].sense_data,
2856 		  ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2857 		  ASENCODE_INVALID_COMMAND, 0, 0);
2858 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2859 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2860 			     SCSI_SENSE_BUFFERSIZE));
2861 		goto scsi_done_ret;
2862 	}
2863 
2864 	switch (scsicmd->cmnd[0]) {
2865 	case READ_6:
2866 	case READ_10:
2867 	case READ_12:
2868 	case READ_16:
2869 		if (dev->in_reset)
2870 			return -1;
2871 		return aac_read(scsicmd);
2872 
2873 	case WRITE_6:
2874 	case WRITE_10:
2875 	case WRITE_12:
2876 	case WRITE_16:
2877 		if (dev->in_reset)
2878 			return -1;
2879 		return aac_write(scsicmd);
2880 
2881 	case SYNCHRONIZE_CACHE:
2882 		if (((aac_cache & 6) == 6) && dev->cache_protected) {
2883 			scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2884 					  SAM_STAT_GOOD;
2885 			break;
2886 		}
2887 		/* Issue FIB to tell Firmware to flush it's cache */
2888 		if ((aac_cache & 6) != 2)
2889 			return aac_synchronize(scsicmd);
2890 		/* fall through */
2891 	case INQUIRY:
2892 	{
2893 		struct inquiry_data inq_data;
2894 
2895 		dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
2896 		memset(&inq_data, 0, sizeof (struct inquiry_data));
2897 
2898 		if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
2899 			char *arr = (char *)&inq_data;
2900 
2901 			/* EVPD bit set */
2902 			arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2903 			  INQD_PDT_PROC : INQD_PDT_DA;
2904 			if (scsicmd->cmnd[2] == 0) {
2905 				/* supported vital product data pages */
2906 				arr[3] = 3;
2907 				arr[4] = 0x0;
2908 				arr[5] = 0x80;
2909 				arr[6] = 0x83;
2910 				arr[1] = scsicmd->cmnd[2];
2911 				scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2912 							 sizeof(inq_data));
2913 				scsicmd->result = DID_OK << 16 |
2914 						  COMMAND_COMPLETE << 8 |
2915 						  SAM_STAT_GOOD;
2916 			} else if (scsicmd->cmnd[2] == 0x80) {
2917 				/* unit serial number page */
2918 				arr[3] = setinqserial(dev, &arr[4],
2919 				  scmd_id(scsicmd));
2920 				arr[1] = scsicmd->cmnd[2];
2921 				scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2922 							 sizeof(inq_data));
2923 				if (aac_wwn != 2)
2924 					return aac_get_container_serial(
2925 						scsicmd);
2926 				scsicmd->result = DID_OK << 16 |
2927 						  COMMAND_COMPLETE << 8 |
2928 						  SAM_STAT_GOOD;
2929 			} else if (scsicmd->cmnd[2] == 0x83) {
2930 				/* vpd page 0x83 - Device Identification Page */
2931 				char *sno = (char *)&inq_data;
2932 				sno[3] = setinqserial(dev, &sno[4],
2933 						      scmd_id(scsicmd));
2934 				if (aac_wwn != 2)
2935 					return aac_get_container_serial(
2936 						scsicmd);
2937 				scsicmd->result = DID_OK << 16 |
2938 						  COMMAND_COMPLETE << 8 |
2939 						  SAM_STAT_GOOD;
2940 			} else {
2941 				/* vpd page not implemented */
2942 				scsicmd->result = DID_OK << 16 |
2943 				  COMMAND_COMPLETE << 8 |
2944 				  SAM_STAT_CHECK_CONDITION;
2945 				set_sense(&dev->fsa_dev[cid].sense_data,
2946 				  ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
2947 				  ASENCODE_NO_SENSE, 7, 2);
2948 				memcpy(scsicmd->sense_buffer,
2949 				  &dev->fsa_dev[cid].sense_data,
2950 				  min_t(size_t,
2951 					sizeof(dev->fsa_dev[cid].sense_data),
2952 					SCSI_SENSE_BUFFERSIZE));
2953 			}
2954 			break;
2955 		}
2956 		inq_data.inqd_ver = 2;	/* claim compliance to SCSI-2 */
2957 		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 */
2958 		inq_data.inqd_len = 31;
2959 		/*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
2960 		inq_data.inqd_pad2= 0x32 ;	 /*WBus16|Sync|CmdQue */
2961 		/*
2962 		 *	Set the Vendor, Product, and Revision Level
2963 		 *	see: <vendor>.c i.e. aac.c
2964 		 */
2965 		if (cid == host->this_id) {
2966 			setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
2967 			inq_data.inqd_pdt = INQD_PDT_PROC;	/* Processor device */
2968 			scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2969 						 sizeof(inq_data));
2970 			scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2971 					  SAM_STAT_GOOD;
2972 			break;
2973 		}
2974 		if (dev->in_reset)
2975 			return -1;
2976 		setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
2977 		inq_data.inqd_pdt = INQD_PDT_DA;	/* Direct/random access device */
2978 		scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
2979 		return aac_get_container_name(scsicmd);
2980 	}
2981 	case SERVICE_ACTION_IN_16:
2982 		if (!(dev->raw_io_interface) ||
2983 		    !(dev->raw_io_64) ||
2984 		    ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2985 			break;
2986 	{
2987 		u64 capacity;
2988 		char cp[13];
2989 		unsigned int alloc_len;
2990 
2991 		dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2992 		capacity = fsa_dev_ptr[cid].size - 1;
2993 		cp[0] = (capacity >> 56) & 0xff;
2994 		cp[1] = (capacity >> 48) & 0xff;
2995 		cp[2] = (capacity >> 40) & 0xff;
2996 		cp[3] = (capacity >> 32) & 0xff;
2997 		cp[4] = (capacity >> 24) & 0xff;
2998 		cp[5] = (capacity >> 16) & 0xff;
2999 		cp[6] = (capacity >> 8) & 0xff;
3000 		cp[7] = (capacity >> 0) & 0xff;
3001 		cp[8] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
3002 		cp[9] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3003 		cp[10] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3004 		cp[11] = (fsa_dev_ptr[cid].block_size) & 0xff;
3005 		cp[12] = 0;
3006 
3007 		alloc_len = ((scsicmd->cmnd[10] << 24)
3008 			     + (scsicmd->cmnd[11] << 16)
3009 			     + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
3010 
3011 		alloc_len = min_t(size_t, alloc_len, sizeof(cp));
3012 		scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
3013 		if (alloc_len < scsi_bufflen(scsicmd))
3014 			scsi_set_resid(scsicmd,
3015 				       scsi_bufflen(scsicmd) - alloc_len);
3016 
3017 		/* Do not cache partition table for arrays */
3018 		scsicmd->device->removable = 1;
3019 
3020 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3021 				  SAM_STAT_GOOD;
3022 		break;
3023 	}
3024 
3025 	case READ_CAPACITY:
3026 	{
3027 		u32 capacity;
3028 		char cp[8];
3029 
3030 		dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
3031 		if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3032 			capacity = fsa_dev_ptr[cid].size - 1;
3033 		else
3034 			capacity = (u32)-1;
3035 
3036 		cp[0] = (capacity >> 24) & 0xff;
3037 		cp[1] = (capacity >> 16) & 0xff;
3038 		cp[2] = (capacity >> 8) & 0xff;
3039 		cp[3] = (capacity >> 0) & 0xff;
3040 		cp[4] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
3041 		cp[5] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3042 		cp[6] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3043 		cp[7] = (fsa_dev_ptr[cid].block_size) & 0xff;
3044 		scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
3045 		/* Do not cache partition table for arrays */
3046 		scsicmd->device->removable = 1;
3047 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3048 				  SAM_STAT_GOOD;
3049 		break;
3050 	}
3051 
3052 	case MODE_SENSE:
3053 	{
3054 		int mode_buf_length = 4;
3055 		u32 capacity;
3056 		aac_modep_data mpd;
3057 
3058 		if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3059 			capacity = fsa_dev_ptr[cid].size - 1;
3060 		else
3061 			capacity = (u32)-1;
3062 
3063 		dprintk((KERN_DEBUG "MODE SENSE command.\n"));
3064 		memset((char *)&mpd, 0, sizeof(aac_modep_data));
3065 
3066 		/* Mode data length */
3067 		mpd.hd.data_length = sizeof(mpd.hd) - 1;
3068 		/* Medium type - default */
3069 		mpd.hd.med_type = 0;
3070 		/* Device-specific param,
3071 		   bit 8: 0/1 = write enabled/protected
3072 		   bit 4: 0/1 = FUA enabled */
3073 		mpd.hd.dev_par = 0;
3074 
3075 		if (dev->raw_io_interface && ((aac_cache & 5) != 1))
3076 			mpd.hd.dev_par = 0x10;
3077 		if (scsicmd->cmnd[1] & 0x8)
3078 			mpd.hd.bd_length = 0;	/* Block descriptor length */
3079 		else {
3080 			mpd.hd.bd_length = sizeof(mpd.bd);
3081 			mpd.hd.data_length += mpd.hd.bd_length;
3082 			mpd.bd.block_length[0] =
3083 				(fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3084 			mpd.bd.block_length[1] =
3085 				(fsa_dev_ptr[cid].block_size >> 8) &  0xff;
3086 			mpd.bd.block_length[2] =
3087 				fsa_dev_ptr[cid].block_size  & 0xff;
3088 
3089 			mpd.mpc_buf[0] = scsicmd->cmnd[2];
3090 			if (scsicmd->cmnd[2] == 0x1C) {
3091 				/* page length */
3092 				mpd.mpc_buf[1] = 0xa;
3093 				/* Mode data length */
3094 				mpd.hd.data_length = 23;
3095 			} else {
3096 				/* Mode data length */
3097 				mpd.hd.data_length = 15;
3098 			}
3099 
3100 			if (capacity > 0xffffff) {
3101 				mpd.bd.block_count[0] = 0xff;
3102 				mpd.bd.block_count[1] = 0xff;
3103 				mpd.bd.block_count[2] = 0xff;
3104 			} else {
3105 				mpd.bd.block_count[0] = (capacity >> 16) & 0xff;
3106 				mpd.bd.block_count[1] = (capacity >> 8) & 0xff;
3107 				mpd.bd.block_count[2] = capacity  & 0xff;
3108 			}
3109 		}
3110 		if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3111 		  ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
3112 			mpd.hd.data_length += 3;
3113 			mpd.mpc_buf[0] = 8;
3114 			mpd.mpc_buf[1] = 1;
3115 			mpd.mpc_buf[2] = ((aac_cache & 6) == 2)
3116 				? 0 : 0x04; /* WCE */
3117 			mode_buf_length = sizeof(mpd);
3118 		}
3119 
3120 		if (mode_buf_length > scsicmd->cmnd[4])
3121 			mode_buf_length = scsicmd->cmnd[4];
3122 		else
3123 			mode_buf_length = sizeof(mpd);
3124 		scsi_sg_copy_from_buffer(scsicmd,
3125 					 (char *)&mpd,
3126 					 mode_buf_length);
3127 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3128 				  SAM_STAT_GOOD;
3129 		break;
3130 	}
3131 	case MODE_SENSE_10:
3132 	{
3133 		u32 capacity;
3134 		int mode_buf_length = 8;
3135 		aac_modep10_data mpd10;
3136 
3137 		if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3138 			capacity = fsa_dev_ptr[cid].size - 1;
3139 		else
3140 			capacity = (u32)-1;
3141 
3142 		dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
3143 		memset((char *)&mpd10, 0, sizeof(aac_modep10_data));
3144 		/* Mode data length (MSB) */
3145 		mpd10.hd.data_length[0] = 0;
3146 		/* Mode data length (LSB) */
3147 		mpd10.hd.data_length[1] = sizeof(mpd10.hd) - 1;
3148 		/* Medium type - default */
3149 		mpd10.hd.med_type = 0;
3150 		/* Device-specific param,
3151 		   bit 8: 0/1 = write enabled/protected
3152 		   bit 4: 0/1 = FUA enabled */
3153 		mpd10.hd.dev_par = 0;
3154 
3155 		if (dev->raw_io_interface && ((aac_cache & 5) != 1))
3156 			mpd10.hd.dev_par = 0x10;
3157 		mpd10.hd.rsrvd[0] = 0;	/* reserved */
3158 		mpd10.hd.rsrvd[1] = 0;	/* reserved */
3159 		if (scsicmd->cmnd[1] & 0x8) {
3160 			/* Block descriptor length (MSB) */
3161 			mpd10.hd.bd_length[0] = 0;
3162 			/* Block descriptor length (LSB) */
3163 			mpd10.hd.bd_length[1] = 0;
3164 		} else {
3165 			mpd10.hd.bd_length[0] = 0;
3166 			mpd10.hd.bd_length[1] = sizeof(mpd10.bd);
3167 
3168 			mpd10.hd.data_length[1] += mpd10.hd.bd_length[1];
3169 
3170 			mpd10.bd.block_length[0] =
3171 				(fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3172 			mpd10.bd.block_length[1] =
3173 				(fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3174 			mpd10.bd.block_length[2] =
3175 				fsa_dev_ptr[cid].block_size  & 0xff;
3176 
3177 			if (capacity > 0xffffff) {
3178 				mpd10.bd.block_count[0] = 0xff;
3179 				mpd10.bd.block_count[1] = 0xff;
3180 				mpd10.bd.block_count[2] = 0xff;
3181 			} else {
3182 				mpd10.bd.block_count[0] =
3183 					(capacity >> 16) & 0xff;
3184 				mpd10.bd.block_count[1] =
3185 					(capacity >> 8) & 0xff;
3186 				mpd10.bd.block_count[2] =
3187 					capacity  & 0xff;
3188 			}
3189 		}
3190 		if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3191 		  ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
3192 			mpd10.hd.data_length[1] += 3;
3193 			mpd10.mpc_buf[0] = 8;
3194 			mpd10.mpc_buf[1] = 1;
3195 			mpd10.mpc_buf[2] = ((aac_cache & 6) == 2)
3196 				? 0 : 0x04; /* WCE */
3197 			mode_buf_length = sizeof(mpd10);
3198 			if (mode_buf_length > scsicmd->cmnd[8])
3199 				mode_buf_length = scsicmd->cmnd[8];
3200 		}
3201 		scsi_sg_copy_from_buffer(scsicmd,
3202 					 (char *)&mpd10,
3203 					 mode_buf_length);
3204 
3205 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3206 				  SAM_STAT_GOOD;
3207 		break;
3208 	}
3209 	case REQUEST_SENSE:
3210 		dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
3211 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3212 				sizeof(struct sense_data));
3213 		memset(&dev->fsa_dev[cid].sense_data, 0,
3214 				sizeof(struct sense_data));
3215 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3216 				  SAM_STAT_GOOD;
3217 		break;
3218 
3219 	case ALLOW_MEDIUM_REMOVAL:
3220 		dprintk((KERN_DEBUG "LOCK command.\n"));
3221 		if (scsicmd->cmnd[4])
3222 			fsa_dev_ptr[cid].locked = 1;
3223 		else
3224 			fsa_dev_ptr[cid].locked = 0;
3225 
3226 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3227 				  SAM_STAT_GOOD;
3228 		break;
3229 	/*
3230 	 *	These commands are all No-Ops
3231 	 */
3232 	case TEST_UNIT_READY:
3233 		if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
3234 			scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3235 				SAM_STAT_CHECK_CONDITION;
3236 			set_sense(&dev->fsa_dev[cid].sense_data,
3237 				  NOT_READY, SENCODE_BECOMING_READY,
3238 				  ASENCODE_BECOMING_READY, 0, 0);
3239 			memcpy(scsicmd->sense_buffer,
3240 			       &dev->fsa_dev[cid].sense_data,
3241 			       min_t(size_t,
3242 				     sizeof(dev->fsa_dev[cid].sense_data),
3243 				     SCSI_SENSE_BUFFERSIZE));
3244 			break;
3245 		}
3246 		/* fall through */
3247 	case RESERVE:
3248 	case RELEASE:
3249 	case REZERO_UNIT:
3250 	case REASSIGN_BLOCKS:
3251 	case SEEK_10:
3252 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3253 				  SAM_STAT_GOOD;
3254 		break;
3255 
3256 	case START_STOP:
3257 		return aac_start_stop(scsicmd);
3258 
3259 	/* FALLTHRU */
3260 	default:
3261 	/*
3262 	 *	Unhandled commands
3263 	 */
3264 		dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n",
3265 				scsicmd->cmnd[0]));
3266 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3267 				SAM_STAT_CHECK_CONDITION;
3268 		set_sense(&dev->fsa_dev[cid].sense_data,
3269 			  ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
3270 			  ASENCODE_INVALID_COMMAND, 0, 0);
3271 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3272 				min_t(size_t,
3273 				      sizeof(dev->fsa_dev[cid].sense_data),
3274 				      SCSI_SENSE_BUFFERSIZE));
3275 	}
3276 
3277 scsi_done_ret:
3278 
3279 	scsicmd->scsi_done(scsicmd);
3280 	return 0;
3281 }
3282 
3283 static int query_disk(struct aac_dev *dev, void __user *arg)
3284 {
3285 	struct aac_query_disk qd;
3286 	struct fsa_dev_info *fsa_dev_ptr;
3287 
3288 	fsa_dev_ptr = dev->fsa_dev;
3289 	if (!fsa_dev_ptr)
3290 		return -EBUSY;
3291 	if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
3292 		return -EFAULT;
3293 	if (qd.cnum == -1) {
3294 		if (qd.id < 0 || qd.id >= dev->maximum_num_containers)
3295 			return -EINVAL;
3296 		qd.cnum = qd.id;
3297 	} else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1)) {
3298 		if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
3299 			return -EINVAL;
3300 		qd.instance = dev->scsi_host_ptr->host_no;
3301 		qd.bus = 0;
3302 		qd.id = CONTAINER_TO_ID(qd.cnum);
3303 		qd.lun = CONTAINER_TO_LUN(qd.cnum);
3304 	}
3305 	else return -EINVAL;
3306 
3307 	qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
3308 	qd.locked = fsa_dev_ptr[qd.cnum].locked;
3309 	qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
3310 
3311 	if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
3312 		qd.unmapped = 1;
3313 	else
3314 		qd.unmapped = 0;
3315 
3316 	strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
3317 	  min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
3318 
3319 	if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
3320 		return -EFAULT;
3321 	return 0;
3322 }
3323 
3324 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
3325 {
3326 	struct aac_delete_disk dd;
3327 	struct fsa_dev_info *fsa_dev_ptr;
3328 
3329 	fsa_dev_ptr = dev->fsa_dev;
3330 	if (!fsa_dev_ptr)
3331 		return -EBUSY;
3332 
3333 	if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3334 		return -EFAULT;
3335 
3336 	if (dd.cnum >= dev->maximum_num_containers)
3337 		return -EINVAL;
3338 	/*
3339 	 *	Mark this container as being deleted.
3340 	 */
3341 	fsa_dev_ptr[dd.cnum].deleted = 1;
3342 	/*
3343 	 *	Mark the container as no longer valid
3344 	 */
3345 	fsa_dev_ptr[dd.cnum].valid = 0;
3346 	return 0;
3347 }
3348 
3349 static int delete_disk(struct aac_dev *dev, void __user *arg)
3350 {
3351 	struct aac_delete_disk dd;
3352 	struct fsa_dev_info *fsa_dev_ptr;
3353 
3354 	fsa_dev_ptr = dev->fsa_dev;
3355 	if (!fsa_dev_ptr)
3356 		return -EBUSY;
3357 
3358 	if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3359 		return -EFAULT;
3360 
3361 	if (dd.cnum >= dev->maximum_num_containers)
3362 		return -EINVAL;
3363 	/*
3364 	 *	If the container is locked, it can not be deleted by the API.
3365 	 */
3366 	if (fsa_dev_ptr[dd.cnum].locked)
3367 		return -EBUSY;
3368 	else {
3369 		/*
3370 		 *	Mark the container as no longer being valid.
3371 		 */
3372 		fsa_dev_ptr[dd.cnum].valid = 0;
3373 		fsa_dev_ptr[dd.cnum].devname[0] = '\0';
3374 		return 0;
3375 	}
3376 }
3377 
3378 int aac_dev_ioctl(struct aac_dev *dev, unsigned int cmd, void __user *arg)
3379 {
3380 	switch (cmd) {
3381 	case FSACTL_QUERY_DISK:
3382 		return query_disk(dev, arg);
3383 	case FSACTL_DELETE_DISK:
3384 		return delete_disk(dev, arg);
3385 	case FSACTL_FORCE_DELETE_DISK:
3386 		return force_delete_disk(dev, arg);
3387 	case FSACTL_GET_CONTAINERS:
3388 		return aac_get_containers(dev);
3389 	default:
3390 		return -ENOTTY;
3391 	}
3392 }
3393 
3394 /**
3395  *
3396  * aac_srb_callback
3397  * @context: the context set in the fib - here it is scsi cmd
3398  * @fibptr: pointer to the fib
3399  *
3400  * Handles the completion of a scsi command to a non dasd device
3401  *
3402  */
3403 
3404 static void aac_srb_callback(void *context, struct fib * fibptr)
3405 {
3406 	struct aac_srb_reply *srbreply;
3407 	struct scsi_cmnd *scsicmd;
3408 
3409 	scsicmd = (struct scsi_cmnd *) context;
3410 
3411 	if (!aac_valid_context(scsicmd, fibptr))
3412 		return;
3413 
3414 	BUG_ON(fibptr == NULL);
3415 
3416 	srbreply = (struct aac_srb_reply *) fib_data(fibptr);
3417 
3418 	scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
3419 
3420 	if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3421 		/* fast response */
3422 		srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
3423 		srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
3424 	} else {
3425 		/*
3426 		 *	Calculate resid for sg
3427 		 */
3428 		scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
3429 				   - le32_to_cpu(srbreply->data_xfer_length));
3430 	}
3431 
3432 
3433 	scsi_dma_unmap(scsicmd);
3434 
3435 	/* expose physical device if expose_physicald flag is on */
3436 	if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
3437 	  && expose_physicals > 0)
3438 		aac_expose_phy_device(scsicmd);
3439 
3440 	/*
3441 	 * First check the fib status
3442 	 */
3443 
3444 	if (le32_to_cpu(srbreply->status) != ST_OK) {
3445 		int len;
3446 
3447 		pr_warn("aac_srb_callback: srb failed, status = %d\n",
3448 				le32_to_cpu(srbreply->status));
3449 		len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3450 			    SCSI_SENSE_BUFFERSIZE);
3451 		scsicmd->result = DID_ERROR << 16
3452 				| COMMAND_COMPLETE << 8
3453 				| SAM_STAT_CHECK_CONDITION;
3454 		memcpy(scsicmd->sense_buffer,
3455 				srbreply->sense_data, len);
3456 	}
3457 
3458 	/*
3459 	 * Next check the srb status
3460 	 */
3461 	switch ((le32_to_cpu(srbreply->srb_status))&0x3f) {
3462 	case SRB_STATUS_ERROR_RECOVERY:
3463 	case SRB_STATUS_PENDING:
3464 	case SRB_STATUS_SUCCESS:
3465 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3466 		break;
3467 	case SRB_STATUS_DATA_OVERRUN:
3468 		switch (scsicmd->cmnd[0]) {
3469 		case  READ_6:
3470 		case  WRITE_6:
3471 		case  READ_10:
3472 		case  WRITE_10:
3473 		case  READ_12:
3474 		case  WRITE_12:
3475 		case  READ_16:
3476 		case  WRITE_16:
3477 			if (le32_to_cpu(srbreply->data_xfer_length)
3478 						< scsicmd->underflow)
3479 				pr_warn("aacraid: SCSI CMD underflow\n");
3480 			else
3481 				pr_warn("aacraid: SCSI CMD Data Overrun\n");
3482 			scsicmd->result = DID_ERROR << 16
3483 					| COMMAND_COMPLETE << 8;
3484 			break;
3485 		case INQUIRY:
3486 			scsicmd->result = DID_OK << 16
3487 					| COMMAND_COMPLETE << 8;
3488 			break;
3489 		default:
3490 			scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3491 			break;
3492 		}
3493 		break;
3494 	case SRB_STATUS_ABORTED:
3495 		scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3496 		break;
3497 	case SRB_STATUS_ABORT_FAILED:
3498 		/*
3499 		 * Not sure about this one - but assuming the
3500 		 * hba was trying to abort for some reason
3501 		 */
3502 		scsicmd->result = DID_ERROR << 16 | ABORT << 8;
3503 		break;
3504 	case SRB_STATUS_PARITY_ERROR:
3505 		scsicmd->result = DID_PARITY << 16
3506 				| MSG_PARITY_ERROR << 8;
3507 		break;
3508 	case SRB_STATUS_NO_DEVICE:
3509 	case SRB_STATUS_INVALID_PATH_ID:
3510 	case SRB_STATUS_INVALID_TARGET_ID:
3511 	case SRB_STATUS_INVALID_LUN:
3512 	case SRB_STATUS_SELECTION_TIMEOUT:
3513 		scsicmd->result = DID_NO_CONNECT << 16
3514 				| COMMAND_COMPLETE << 8;
3515 		break;
3516 
3517 	case SRB_STATUS_COMMAND_TIMEOUT:
3518 	case SRB_STATUS_TIMEOUT:
3519 		scsicmd->result = DID_TIME_OUT << 16
3520 				| COMMAND_COMPLETE << 8;
3521 		break;
3522 
3523 	case SRB_STATUS_BUSY:
3524 		scsicmd->result = DID_BUS_BUSY << 16
3525 				| COMMAND_COMPLETE << 8;
3526 		break;
3527 
3528 	case SRB_STATUS_BUS_RESET:
3529 		scsicmd->result = DID_RESET << 16
3530 				| COMMAND_COMPLETE << 8;
3531 		break;
3532 
3533 	case SRB_STATUS_MESSAGE_REJECTED:
3534 		scsicmd->result = DID_ERROR << 16
3535 				| MESSAGE_REJECT << 8;
3536 		break;
3537 	case SRB_STATUS_REQUEST_FLUSHED:
3538 	case SRB_STATUS_ERROR:
3539 	case SRB_STATUS_INVALID_REQUEST:
3540 	case SRB_STATUS_REQUEST_SENSE_FAILED:
3541 	case SRB_STATUS_NO_HBA:
3542 	case SRB_STATUS_UNEXPECTED_BUS_FREE:
3543 	case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
3544 	case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
3545 	case SRB_STATUS_DELAYED_RETRY:
3546 	case SRB_STATUS_BAD_FUNCTION:
3547 	case SRB_STATUS_NOT_STARTED:
3548 	case SRB_STATUS_NOT_IN_USE:
3549 	case SRB_STATUS_FORCE_ABORT:
3550 	case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
3551 	default:
3552 #ifdef AAC_DETAILED_STATUS_INFO
3553 		pr_info("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x -scsi status 0x%x\n",
3554 			le32_to_cpu(srbreply->srb_status) & 0x3F,
3555 			aac_get_status_string(
3556 				le32_to_cpu(srbreply->srb_status) & 0x3F),
3557 			scsicmd->cmnd[0],
3558 			le32_to_cpu(srbreply->scsi_status));
3559 #endif
3560 		/*
3561 		 * When the CC bit is SET by the host in ATA pass thru CDB,
3562 		 *  driver is supposed to return DID_OK
3563 		 *
3564 		 * When the CC bit is RESET by the host, driver should
3565 		 *  return DID_ERROR
3566 		 */
3567 		if ((scsicmd->cmnd[0] == ATA_12)
3568 			|| (scsicmd->cmnd[0] == ATA_16)) {
3569 
3570 			if (scsicmd->cmnd[2] & (0x01 << 5)) {
3571 				scsicmd->result = DID_OK << 16
3572 					| COMMAND_COMPLETE << 8;
3573 			break;
3574 			} else {
3575 				scsicmd->result = DID_ERROR << 16
3576 					| COMMAND_COMPLETE << 8;
3577 			break;
3578 			}
3579 		} else {
3580 			scsicmd->result = DID_ERROR << 16
3581 				| COMMAND_COMPLETE << 8;
3582 			break;
3583 		}
3584 	}
3585 	if (le32_to_cpu(srbreply->scsi_status)
3586 			== SAM_STAT_CHECK_CONDITION) {
3587 		int len;
3588 
3589 		scsicmd->result |= SAM_STAT_CHECK_CONDITION;
3590 		len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3591 			    SCSI_SENSE_BUFFERSIZE);
3592 #ifdef AAC_DETAILED_STATUS_INFO
3593 		pr_warn("aac_srb_callback: check condition, status = %d len=%d\n",
3594 					le32_to_cpu(srbreply->status), len);
3595 #endif
3596 		memcpy(scsicmd->sense_buffer,
3597 				srbreply->sense_data, len);
3598 	}
3599 
3600 	/*
3601 	 * OR in the scsi status (already shifted up a bit)
3602 	 */
3603 	scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
3604 
3605 	aac_fib_complete(fibptr);
3606 	scsicmd->scsi_done(scsicmd);
3607 }
3608 
3609 static void hba_resp_task_complete(struct aac_dev *dev,
3610 					struct scsi_cmnd *scsicmd,
3611 					struct aac_hba_resp *err) {
3612 
3613 	scsicmd->result = err->status;
3614 	/* set residual count */
3615 	scsi_set_resid(scsicmd, le32_to_cpu(err->residual_count));
3616 
3617 	switch (err->status) {
3618 	case SAM_STAT_GOOD:
3619 		scsicmd->result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
3620 		break;
3621 	case SAM_STAT_CHECK_CONDITION:
3622 	{
3623 		int len;
3624 
3625 		len = min_t(u8, err->sense_response_data_len,
3626 			SCSI_SENSE_BUFFERSIZE);
3627 		if (len)
3628 			memcpy(scsicmd->sense_buffer,
3629 				err->sense_response_buf, len);
3630 		scsicmd->result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
3631 		break;
3632 	}
3633 	case SAM_STAT_BUSY:
3634 		scsicmd->result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
3635 		break;
3636 	case SAM_STAT_TASK_ABORTED:
3637 		scsicmd->result |= DID_ABORT << 16 | ABORT << 8;
3638 		break;
3639 	case SAM_STAT_RESERVATION_CONFLICT:
3640 	case SAM_STAT_TASK_SET_FULL:
3641 	default:
3642 		scsicmd->result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3643 		break;
3644 	}
3645 }
3646 
3647 static void hba_resp_task_failure(struct aac_dev *dev,
3648 					struct scsi_cmnd *scsicmd,
3649 					struct aac_hba_resp *err)
3650 {
3651 	switch (err->status) {
3652 	case HBA_RESP_STAT_HBAMODE_DISABLED:
3653 	{
3654 		u32 bus, cid;
3655 
3656 		bus = aac_logical_to_phys(scmd_channel(scsicmd));
3657 		cid = scmd_id(scsicmd);
3658 		if (dev->hba_map[bus][cid].devtype == AAC_DEVTYPE_NATIVE_RAW) {
3659 			dev->hba_map[bus][cid].devtype = AAC_DEVTYPE_ARC_RAW;
3660 			dev->hba_map[bus][cid].rmw_nexus = 0xffffffff;
3661 		}
3662 		scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3663 		break;
3664 	}
3665 	case HBA_RESP_STAT_IO_ERROR:
3666 	case HBA_RESP_STAT_NO_PATH_TO_DEVICE:
3667 		scsicmd->result = DID_OK << 16 |
3668 			COMMAND_COMPLETE << 8 | SAM_STAT_BUSY;
3669 		break;
3670 	case HBA_RESP_STAT_IO_ABORTED:
3671 		scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3672 		break;
3673 	case HBA_RESP_STAT_INVALID_DEVICE:
3674 		scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3675 		break;
3676 	case HBA_RESP_STAT_UNDERRUN:
3677 		/* UNDERRUN is OK */
3678 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3679 		break;
3680 	case HBA_RESP_STAT_OVERRUN:
3681 	default:
3682 		scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3683 		break;
3684 	}
3685 }
3686 
3687 /**
3688  *
3689  * aac_hba_callback
3690  * @context: the context set in the fib - here it is scsi cmd
3691  * @fibptr: pointer to the fib
3692  *
3693  * Handles the completion of a native HBA scsi command
3694  *
3695  */
3696 void aac_hba_callback(void *context, struct fib *fibptr)
3697 {
3698 	struct aac_dev *dev;
3699 	struct scsi_cmnd *scsicmd;
3700 
3701 	struct aac_hba_resp *err =
3702 			&((struct aac_native_hba *)fibptr->hw_fib_va)->resp.err;
3703 
3704 	scsicmd = (struct scsi_cmnd *) context;
3705 
3706 	if (!aac_valid_context(scsicmd, fibptr))
3707 		return;
3708 
3709 	WARN_ON(fibptr == NULL);
3710 	dev = fibptr->dev;
3711 
3712 	if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF))
3713 		scsi_dma_unmap(scsicmd);
3714 
3715 	if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3716 		/* fast response */
3717 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3718 		goto out;
3719 	}
3720 
3721 	switch (err->service_response) {
3722 	case HBA_RESP_SVCRES_TASK_COMPLETE:
3723 		hba_resp_task_complete(dev, scsicmd, err);
3724 		break;
3725 	case HBA_RESP_SVCRES_FAILURE:
3726 		hba_resp_task_failure(dev, scsicmd, err);
3727 		break;
3728 	case HBA_RESP_SVCRES_TMF_REJECTED:
3729 		scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
3730 		break;
3731 	case HBA_RESP_SVCRES_TMF_LUN_INVALID:
3732 		scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3733 		break;
3734 	case HBA_RESP_SVCRES_TMF_COMPLETE:
3735 	case HBA_RESP_SVCRES_TMF_SUCCEEDED:
3736 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3737 		break;
3738 	default:
3739 		scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3740 		break;
3741 	}
3742 
3743 out:
3744 	aac_fib_complete(fibptr);
3745 
3746 	if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF)
3747 		scsicmd->SCp.sent_command = 1;
3748 	else
3749 		scsicmd->scsi_done(scsicmd);
3750 }
3751 
3752 /**
3753  *
3754  * aac_send_srb_fib
3755  * @scsicmd: the scsi command block
3756  *
3757  * This routine will form a FIB and fill in the aac_srb from the
3758  * scsicmd passed in.
3759  */
3760 
3761 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
3762 {
3763 	struct fib* cmd_fibcontext;
3764 	struct aac_dev* dev;
3765 	int status;
3766 
3767 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3768 	if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3769 			scsicmd->device->lun > 7) {
3770 		scsicmd->result = DID_NO_CONNECT << 16;
3771 		scsicmd->scsi_done(scsicmd);
3772 		return 0;
3773 	}
3774 
3775 	/*
3776 	 *	Allocate and initialize a Fib then setup a BlockWrite command
3777 	 */
3778 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3779 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3780 	status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
3781 
3782 	/*
3783 	 *	Check that the command queued to the controller
3784 	 */
3785 	if (status == -EINPROGRESS)
3786 		return 0;
3787 
3788 	printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
3789 	aac_fib_complete(cmd_fibcontext);
3790 	aac_fib_free(cmd_fibcontext);
3791 
3792 	return -1;
3793 }
3794 
3795 /**
3796  *
3797  * aac_send_hba_fib
3798  * @scsicmd: the scsi command block
3799  *
3800  * This routine will form a FIB and fill in the aac_hba_cmd_req from the
3801  * scsicmd passed in.
3802  */
3803 static int aac_send_hba_fib(struct scsi_cmnd *scsicmd)
3804 {
3805 	struct fib *cmd_fibcontext;
3806 	struct aac_dev *dev;
3807 	int status;
3808 
3809 	dev = shost_priv(scsicmd->device->host);
3810 	if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3811 			scsicmd->device->lun > AAC_MAX_LUN - 1) {
3812 		scsicmd->result = DID_NO_CONNECT << 16;
3813 		scsicmd->scsi_done(scsicmd);
3814 		return 0;
3815 	}
3816 
3817 	/*
3818 	 *	Allocate and initialize a Fib then setup a BlockWrite command
3819 	 */
3820 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3821 	if (!cmd_fibcontext)
3822 		return -1;
3823 
3824 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3825 	status = aac_adapter_hba(cmd_fibcontext, scsicmd);
3826 
3827 	/*
3828 	 *	Check that the command queued to the controller
3829 	 */
3830 	if (status == -EINPROGRESS)
3831 		return 0;
3832 
3833 	pr_warn("aac_hba_cmd_req: aac_fib_send failed with status: %d\n",
3834 		status);
3835 	aac_fib_complete(cmd_fibcontext);
3836 	aac_fib_free(cmd_fibcontext);
3837 
3838 	return -1;
3839 }
3840 
3841 
3842 static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
3843 {
3844 	unsigned long byte_count = 0;
3845 	int nseg;
3846 	struct scatterlist *sg;
3847 	int i;
3848 
3849 	// Get rid of old data
3850 	psg->count = 0;
3851 	psg->sg[0].addr = 0;
3852 	psg->sg[0].count = 0;
3853 
3854 	nseg = scsi_dma_map(scsicmd);
3855 	if (nseg <= 0)
3856 		return nseg;
3857 
3858 	psg->count = cpu_to_le32(nseg);
3859 
3860 	scsi_for_each_sg(scsicmd, sg, nseg, i) {
3861 		psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
3862 		psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
3863 		byte_count += sg_dma_len(sg);
3864 	}
3865 	/* hba wants the size to be exact */
3866 	if (byte_count > scsi_bufflen(scsicmd)) {
3867 		u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3868 			(byte_count - scsi_bufflen(scsicmd));
3869 		psg->sg[i-1].count = cpu_to_le32(temp);
3870 		byte_count = scsi_bufflen(scsicmd);
3871 	}
3872 	/* Check for command underflow */
3873 	if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3874 		printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3875 		       byte_count, scsicmd->underflow);
3876 	}
3877 
3878 	return byte_count;
3879 }
3880 
3881 
3882 static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
3883 {
3884 	unsigned long byte_count = 0;
3885 	u64 addr;
3886 	int nseg;
3887 	struct scatterlist *sg;
3888 	int i;
3889 
3890 	// Get rid of old data
3891 	psg->count = 0;
3892 	psg->sg[0].addr[0] = 0;
3893 	psg->sg[0].addr[1] = 0;
3894 	psg->sg[0].count = 0;
3895 
3896 	nseg = scsi_dma_map(scsicmd);
3897 	if (nseg <= 0)
3898 		return nseg;
3899 
3900 	scsi_for_each_sg(scsicmd, sg, nseg, i) {
3901 		int count = sg_dma_len(sg);
3902 		addr = sg_dma_address(sg);
3903 		psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
3904 		psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
3905 		psg->sg[i].count = cpu_to_le32(count);
3906 		byte_count += count;
3907 	}
3908 	psg->count = cpu_to_le32(nseg);
3909 	/* hba wants the size to be exact */
3910 	if (byte_count > scsi_bufflen(scsicmd)) {
3911 		u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3912 			(byte_count - scsi_bufflen(scsicmd));
3913 		psg->sg[i-1].count = cpu_to_le32(temp);
3914 		byte_count = scsi_bufflen(scsicmd);
3915 	}
3916 	/* Check for command underflow */
3917 	if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3918 		printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3919 		       byte_count, scsicmd->underflow);
3920 	}
3921 
3922 	return byte_count;
3923 }
3924 
3925 static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
3926 {
3927 	unsigned long byte_count = 0;
3928 	int nseg;
3929 	struct scatterlist *sg;
3930 	int i;
3931 
3932 	// Get rid of old data
3933 	psg->count = 0;
3934 	psg->sg[0].next = 0;
3935 	psg->sg[0].prev = 0;
3936 	psg->sg[0].addr[0] = 0;
3937 	psg->sg[0].addr[1] = 0;
3938 	psg->sg[0].count = 0;
3939 	psg->sg[0].flags = 0;
3940 
3941 	nseg = scsi_dma_map(scsicmd);
3942 	if (nseg <= 0)
3943 		return nseg;
3944 
3945 	scsi_for_each_sg(scsicmd, sg, nseg, i) {
3946 		int count = sg_dma_len(sg);
3947 		u64 addr = sg_dma_address(sg);
3948 		psg->sg[i].next = 0;
3949 		psg->sg[i].prev = 0;
3950 		psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
3951 		psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
3952 		psg->sg[i].count = cpu_to_le32(count);
3953 		psg->sg[i].flags = 0;
3954 		byte_count += count;
3955 	}
3956 	psg->count = cpu_to_le32(nseg);
3957 	/* hba wants the size to be exact */
3958 	if (byte_count > scsi_bufflen(scsicmd)) {
3959 		u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3960 			(byte_count - scsi_bufflen(scsicmd));
3961 		psg->sg[i-1].count = cpu_to_le32(temp);
3962 		byte_count = scsi_bufflen(scsicmd);
3963 	}
3964 	/* Check for command underflow */
3965 	if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3966 		printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3967 		       byte_count, scsicmd->underflow);
3968 	}
3969 
3970 	return byte_count;
3971 }
3972 
3973 static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
3974 				struct aac_raw_io2 *rio2, int sg_max)
3975 {
3976 	unsigned long byte_count = 0;
3977 	int nseg;
3978 	struct scatterlist *sg;
3979 	int i, conformable = 0;
3980 	u32 min_size = PAGE_SIZE, cur_size;
3981 
3982 	nseg = scsi_dma_map(scsicmd);
3983 	if (nseg <= 0)
3984 		return nseg;
3985 
3986 	scsi_for_each_sg(scsicmd, sg, nseg, i) {
3987 		int count = sg_dma_len(sg);
3988 		u64 addr = sg_dma_address(sg);
3989 
3990 		BUG_ON(i >= sg_max);
3991 		rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
3992 		rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
3993 		cur_size = cpu_to_le32(count);
3994 		rio2->sge[i].length = cur_size;
3995 		rio2->sge[i].flags = 0;
3996 		if (i == 0) {
3997 			conformable = 1;
3998 			rio2->sgeFirstSize = cur_size;
3999 		} else if (i == 1) {
4000 			rio2->sgeNominalSize = cur_size;
4001 			min_size = cur_size;
4002 		} else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
4003 			conformable = 0;
4004 			if (cur_size < min_size)
4005 				min_size = cur_size;
4006 		}
4007 		byte_count += count;
4008 	}
4009 
4010 	/* hba wants the size to be exact */
4011 	if (byte_count > scsi_bufflen(scsicmd)) {
4012 		u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
4013 			(byte_count - scsi_bufflen(scsicmd));
4014 		rio2->sge[i-1].length = cpu_to_le32(temp);
4015 		byte_count = scsi_bufflen(scsicmd);
4016 	}
4017 
4018 	rio2->sgeCnt = cpu_to_le32(nseg);
4019 	rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
4020 	/* not conformable: evaluate required sg elements */
4021 	if (!conformable) {
4022 		int j, nseg_new = nseg, err_found;
4023 		for (i = min_size / PAGE_SIZE; i >= 1; --i) {
4024 			err_found = 0;
4025 			nseg_new = 2;
4026 			for (j = 1; j < nseg - 1; ++j) {
4027 				if (rio2->sge[j].length % (i*PAGE_SIZE)) {
4028 					err_found = 1;
4029 					break;
4030 				}
4031 				nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
4032 			}
4033 			if (!err_found)
4034 				break;
4035 		}
4036 		if (i > 0 && nseg_new <= sg_max) {
4037 			int ret = aac_convert_sgraw2(rio2, i, nseg, nseg_new);
4038 
4039 			if (ret < 0)
4040 				return ret;
4041 		}
4042 	} else
4043 		rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
4044 
4045 	/* Check for command underflow */
4046 	if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4047 		printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
4048 		       byte_count, scsicmd->underflow);
4049 	}
4050 
4051 	return byte_count;
4052 }
4053 
4054 static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
4055 {
4056 	struct sge_ieee1212 *sge;
4057 	int i, j, pos;
4058 	u32 addr_low;
4059 
4060 	if (aac_convert_sgl == 0)
4061 		return 0;
4062 
4063 	sge = kmalloc_array(nseg_new, sizeof(struct sge_ieee1212), GFP_ATOMIC);
4064 	if (sge == NULL)
4065 		return -ENOMEM;
4066 
4067 	for (i = 1, pos = 1; i < nseg-1; ++i) {
4068 		for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
4069 			addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
4070 			sge[pos].addrLow = addr_low;
4071 			sge[pos].addrHigh = rio2->sge[i].addrHigh;
4072 			if (addr_low < rio2->sge[i].addrLow)
4073 				sge[pos].addrHigh++;
4074 			sge[pos].length = pages * PAGE_SIZE;
4075 			sge[pos].flags = 0;
4076 			pos++;
4077 		}
4078 	}
4079 	sge[pos] = rio2->sge[nseg-1];
4080 	memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
4081 
4082 	kfree(sge);
4083 	rio2->sgeCnt = cpu_to_le32(nseg_new);
4084 	rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
4085 	rio2->sgeNominalSize = pages * PAGE_SIZE;
4086 	return 0;
4087 }
4088 
4089 static long aac_build_sghba(struct scsi_cmnd *scsicmd,
4090 			struct aac_hba_cmd_req *hbacmd,
4091 			int sg_max,
4092 			u64 sg_address)
4093 {
4094 	unsigned long byte_count = 0;
4095 	int nseg;
4096 	struct scatterlist *sg;
4097 	int i;
4098 	u32 cur_size;
4099 	struct aac_hba_sgl *sge;
4100 
4101 	nseg = scsi_dma_map(scsicmd);
4102 	if (nseg <= 0) {
4103 		byte_count = nseg;
4104 		goto out;
4105 	}
4106 
4107 	if (nseg > HBA_MAX_SG_EMBEDDED)
4108 		sge = &hbacmd->sge[2];
4109 	else
4110 		sge = &hbacmd->sge[0];
4111 
4112 	scsi_for_each_sg(scsicmd, sg, nseg, i) {
4113 		int count = sg_dma_len(sg);
4114 		u64 addr = sg_dma_address(sg);
4115 
4116 		WARN_ON(i >= sg_max);
4117 		sge->addr_hi = cpu_to_le32((u32)(addr>>32));
4118 		sge->addr_lo = cpu_to_le32((u32)(addr & 0xffffffff));
4119 		cur_size = cpu_to_le32(count);
4120 		sge->len = cur_size;
4121 		sge->flags = 0;
4122 		byte_count += count;
4123 		sge++;
4124 	}
4125 
4126 	sge--;
4127 	/* hba wants the size to be exact */
4128 	if (byte_count > scsi_bufflen(scsicmd)) {
4129 		u32 temp;
4130 
4131 		temp = le32_to_cpu(sge->len) - byte_count
4132 						- scsi_bufflen(scsicmd);
4133 		sge->len = cpu_to_le32(temp);
4134 		byte_count = scsi_bufflen(scsicmd);
4135 	}
4136 
4137 	if (nseg <= HBA_MAX_SG_EMBEDDED) {
4138 		hbacmd->emb_data_desc_count = cpu_to_le32(nseg);
4139 		sge->flags = cpu_to_le32(0x40000000);
4140 	} else {
4141 		/* not embedded */
4142 		hbacmd->sge[0].flags = cpu_to_le32(0x80000000);
4143 		hbacmd->emb_data_desc_count = (u8)cpu_to_le32(1);
4144 		hbacmd->sge[0].addr_hi = (u32)cpu_to_le32(sg_address >> 32);
4145 		hbacmd->sge[0].addr_lo =
4146 			cpu_to_le32((u32)(sg_address & 0xffffffff));
4147 	}
4148 
4149 	/* Check for command underflow */
4150 	if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4151 		pr_warn("aacraid: cmd len %08lX cmd underflow %08X\n",
4152 				byte_count, scsicmd->underflow);
4153 	}
4154 out:
4155 	return byte_count;
4156 }
4157 
4158 #ifdef AAC_DETAILED_STATUS_INFO
4159 
4160 struct aac_srb_status_info {
4161 	u32	status;
4162 	char	*str;
4163 };
4164 
4165 
4166 static struct aac_srb_status_info srb_status_info[] = {
4167 	{ SRB_STATUS_PENDING,		"Pending Status"},
4168 	{ SRB_STATUS_SUCCESS,		"Success"},
4169 	{ SRB_STATUS_ABORTED,		"Aborted Command"},
4170 	{ SRB_STATUS_ABORT_FAILED,	"Abort Failed"},
4171 	{ SRB_STATUS_ERROR,		"Error Event"},
4172 	{ SRB_STATUS_BUSY,		"Device Busy"},
4173 	{ SRB_STATUS_INVALID_REQUEST,	"Invalid Request"},
4174 	{ SRB_STATUS_INVALID_PATH_ID,	"Invalid Path ID"},
4175 	{ SRB_STATUS_NO_DEVICE,		"No Device"},
4176 	{ SRB_STATUS_TIMEOUT,		"Timeout"},
4177 	{ SRB_STATUS_SELECTION_TIMEOUT,	"Selection Timeout"},
4178 	{ SRB_STATUS_COMMAND_TIMEOUT,	"Command Timeout"},
4179 	{ SRB_STATUS_MESSAGE_REJECTED,	"Message Rejected"},
4180 	{ SRB_STATUS_BUS_RESET,		"Bus Reset"},
4181 	{ SRB_STATUS_PARITY_ERROR,	"Parity Error"},
4182 	{ SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
4183 	{ SRB_STATUS_NO_HBA,		"No HBA"},
4184 	{ SRB_STATUS_DATA_OVERRUN,	"Data Overrun/Data Underrun"},
4185 	{ SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
4186 	{ SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
4187 	{ SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
4188 	{ SRB_STATUS_REQUEST_FLUSHED,	"Request Flushed"},
4189 	{ SRB_STATUS_DELAYED_RETRY,	"Delayed Retry"},
4190 	{ SRB_STATUS_INVALID_LUN,	"Invalid LUN"},
4191 	{ SRB_STATUS_INVALID_TARGET_ID,	"Invalid TARGET ID"},
4192 	{ SRB_STATUS_BAD_FUNCTION,	"Bad Function"},
4193 	{ SRB_STATUS_ERROR_RECOVERY,	"Error Recovery"},
4194 	{ SRB_STATUS_NOT_STARTED,	"Not Started"},
4195 	{ SRB_STATUS_NOT_IN_USE,	"Not In Use"},
4196 	{ SRB_STATUS_FORCE_ABORT,	"Force Abort"},
4197 	{ SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
4198 	{ 0xff,				"Unknown Error"}
4199 };
4200 
4201 char *aac_get_status_string(u32 status)
4202 {
4203 	int i;
4204 
4205 	for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
4206 		if (srb_status_info[i].status == status)
4207 			return srb_status_info[i].str;
4208 
4209 	return "Bad Status Code";
4210 }
4211 
4212 #endif
4213