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