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