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