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