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