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