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