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