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