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