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