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