xref: /linux/drivers/scsi/aacraid/aachba.c (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
1 /*
2  *	Adaptec AAC series RAID controller driver
3  *	(c) Copyright 2001 Red Hat Inc.	<alan@redhat.com>
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; see the file COPYING.  If not, write to
22  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25 
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/sched.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 <linux/dma-mapping.h>
36 #include <asm/semaphore.h>
37 #include <asm/uaccess.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 dacmode = -1;
149 
150 static int commit = -1;
151 int startup_timeout = 180;
152 int aif_timeout = 120;
153 
154 module_param(nondasd, int, S_IRUGO|S_IWUSR);
155 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
156 module_param(dacmode, int, S_IRUGO|S_IWUSR);
157 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
158 module_param(commit, int, S_IRUGO|S_IWUSR);
159 MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the adapter for foreign arrays.\nThis is typically needed in systems that do not have a BIOS. 0=off, 1=on");
160 module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
161 MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for adapter to have it's kernel up and\nrunning. This is typically adjusted for large systems that do not have a BIOS.");
162 module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
163 MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for applications to pick up AIFs before\nderegistering them. This is typically adjusted for heavily burdened systems.");
164 
165 int numacb = -1;
166 module_param(numacb, int, S_IRUGO|S_IWUSR);
167 MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control blocks (FIB) allocated. Valid values are 512 and down. Default is to use suggestion from Firmware.");
168 
169 int acbsize = -1;
170 module_param(acbsize, int, S_IRUGO|S_IWUSR);
171 MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB) size. Valid values are 512, 2048, 4096 and 8192. Default is to use suggestion from Firmware.");
172 /**
173  *	aac_get_config_status	-	check the adapter configuration
174  *	@common: adapter to query
175  *
176  *	Query config status, and commit the configuration if needed.
177  */
178 int aac_get_config_status(struct aac_dev *dev)
179 {
180 	int status = 0;
181 	struct fib * fibptr;
182 
183 	if (!(fibptr = aac_fib_alloc(dev)))
184 		return -ENOMEM;
185 
186 	aac_fib_init(fibptr);
187 	{
188 		struct aac_get_config_status *dinfo;
189 		dinfo = (struct aac_get_config_status *) fib_data(fibptr);
190 
191 		dinfo->command = cpu_to_le32(VM_ContainerConfig);
192 		dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
193 		dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
194 	}
195 
196 	status = aac_fib_send(ContainerCommand,
197 			    fibptr,
198 			    sizeof (struct aac_get_config_status),
199 			    FsaNormal,
200 			    1, 1,
201 			    NULL, NULL);
202 	if (status < 0 ) {
203 		printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
204 	} else {
205 		struct aac_get_config_status_resp *reply
206 		  = (struct aac_get_config_status_resp *) fib_data(fibptr);
207 		dprintk((KERN_WARNING
208 		  "aac_get_config_status: response=%d status=%d action=%d\n",
209 		  le32_to_cpu(reply->response),
210 		  le32_to_cpu(reply->status),
211 		  le32_to_cpu(reply->data.action)));
212 		if ((le32_to_cpu(reply->response) != ST_OK) ||
213 		     (le32_to_cpu(reply->status) != CT_OK) ||
214 		     (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
215 			printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
216 			status = -EINVAL;
217 		}
218 	}
219 	aac_fib_complete(fibptr);
220 	/* Send a CT_COMMIT_CONFIG to enable discovery of devices */
221 	if (status >= 0) {
222 		if (commit == 1) {
223 			struct aac_commit_config * dinfo;
224 			aac_fib_init(fibptr);
225 			dinfo = (struct aac_commit_config *) fib_data(fibptr);
226 
227 			dinfo->command = cpu_to_le32(VM_ContainerConfig);
228 			dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
229 
230 			status = aac_fib_send(ContainerCommand,
231 				    fibptr,
232 				    sizeof (struct aac_commit_config),
233 				    FsaNormal,
234 				    1, 1,
235 				    NULL, NULL);
236 			aac_fib_complete(fibptr);
237 		} else if (commit == 0) {
238 			printk(KERN_WARNING
239 			  "aac_get_config_status: Foreign device configurations are being ignored\n");
240 		}
241 	}
242 	aac_fib_free(fibptr);
243 	return status;
244 }
245 
246 /**
247  *	aac_get_containers	-	list containers
248  *	@common: adapter to probe
249  *
250  *	Make a list of all containers on this controller
251  */
252 int aac_get_containers(struct aac_dev *dev)
253 {
254 	struct fsa_dev_info *fsa_dev_ptr;
255 	u32 index;
256 	int status = 0;
257 	struct fib * fibptr;
258 	unsigned instance;
259 	struct aac_get_container_count *dinfo;
260 	struct aac_get_container_count_resp *dresp;
261 	int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
262 
263 	instance = dev->scsi_host_ptr->unique_id;
264 
265 	if (!(fibptr = aac_fib_alloc(dev)))
266 		return -ENOMEM;
267 
268 	aac_fib_init(fibptr);
269 	dinfo = (struct aac_get_container_count *) fib_data(fibptr);
270 	dinfo->command = cpu_to_le32(VM_ContainerConfig);
271 	dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
272 
273 	status = aac_fib_send(ContainerCommand,
274 		    fibptr,
275 		    sizeof (struct aac_get_container_count),
276 		    FsaNormal,
277 		    1, 1,
278 		    NULL, NULL);
279 	if (status >= 0) {
280 		dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
281 		maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
282 		aac_fib_complete(fibptr);
283 	}
284 
285 	if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
286 		maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
287 	fsa_dev_ptr = (struct fsa_dev_info *) kmalloc(
288 	  sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
289 	if (!fsa_dev_ptr) {
290 		aac_fib_free(fibptr);
291 		return -ENOMEM;
292 	}
293 	memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
294 
295 	dev->fsa_dev = fsa_dev_ptr;
296 	dev->maximum_num_containers = maximum_num_containers;
297 
298 	for (index = 0; index < dev->maximum_num_containers; index++) {
299 		struct aac_query_mount *dinfo;
300 		struct aac_mount *dresp;
301 
302 		fsa_dev_ptr[index].devname[0] = '\0';
303 
304 		aac_fib_init(fibptr);
305 		dinfo = (struct aac_query_mount *) fib_data(fibptr);
306 
307 		dinfo->command = cpu_to_le32(VM_NameServe);
308 		dinfo->count = cpu_to_le32(index);
309 		dinfo->type = cpu_to_le32(FT_FILESYS);
310 
311 		status = aac_fib_send(ContainerCommand,
312 				    fibptr,
313 				    sizeof (struct aac_query_mount),
314 				    FsaNormal,
315 				    1, 1,
316 				    NULL, NULL);
317 		if (status < 0 ) {
318 			printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
319 			break;
320 		}
321 		dresp = (struct aac_mount *)fib_data(fibptr);
322 
323 		if ((le32_to_cpu(dresp->status) == ST_OK) &&
324 		    (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
325 			dinfo->command = cpu_to_le32(VM_NameServe64);
326 			dinfo->count = cpu_to_le32(index);
327 			dinfo->type = cpu_to_le32(FT_FILESYS);
328 
329 			if (aac_fib_send(ContainerCommand,
330 				    fibptr,
331 				    sizeof(struct aac_query_mount),
332 				    FsaNormal,
333 				    1, 1,
334 				    NULL, NULL) < 0)
335 				continue;
336 		} else
337 			dresp->mnt[0].capacityhigh = 0;
338 
339 		dprintk ((KERN_DEBUG
340 		  "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%llu\n",
341 		  (int)index, (int)le32_to_cpu(dresp->status),
342 		  (int)le32_to_cpu(dresp->mnt[0].vol),
343 		  (int)le32_to_cpu(dresp->mnt[0].state),
344 		  ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
345 		    (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32)));
346 		if ((le32_to_cpu(dresp->status) == ST_OK) &&
347 		    (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
348 		    (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
349 			fsa_dev_ptr[index].valid = 1;
350 			fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
351 			fsa_dev_ptr[index].size
352 			  = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
353 			    (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
354 			if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
355 				    fsa_dev_ptr[index].ro = 1;
356 		}
357 		aac_fib_complete(fibptr);
358 		/*
359 		 *	If there are no more containers, then stop asking.
360 		 */
361 		if ((index + 1) >= le32_to_cpu(dresp->count)){
362 			break;
363 		}
364 	}
365 	aac_fib_free(fibptr);
366 	return status;
367 }
368 
369 static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
370 {
371 	void *buf;
372 	unsigned int transfer_len;
373 	struct scatterlist *sg = scsicmd->request_buffer;
374 
375 	if (scsicmd->use_sg) {
376 		buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
377 		transfer_len = min(sg->length, len + offset);
378 	} else {
379 		buf = scsicmd->request_buffer;
380 		transfer_len = min(scsicmd->request_bufflen, len + offset);
381 	}
382 
383 	memcpy(buf + offset, data, transfer_len - offset);
384 
385 	if (scsicmd->use_sg)
386 		kunmap_atomic(buf - sg->offset, KM_IRQ0);
387 
388 }
389 
390 static void get_container_name_callback(void *context, struct fib * fibptr)
391 {
392 	struct aac_get_name_resp * get_name_reply;
393 	struct scsi_cmnd * scsicmd;
394 
395 	scsicmd = (struct scsi_cmnd *) context;
396 	scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
397 
398 	dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
399 	BUG_ON(fibptr == NULL);
400 
401 	get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
402 	/* Failure is irrelevant, using default value instead */
403 	if ((le32_to_cpu(get_name_reply->status) == CT_OK)
404 	 && (get_name_reply->data[0] != '\0')) {
405 		char *sp = get_name_reply->data;
406 		sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
407 		while (*sp == ' ')
408 			++sp;
409 		if (*sp) {
410 			char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
411 			int count = sizeof(d);
412 			char *dp = d;
413 			do {
414 				*dp++ = (*sp) ? *sp++ : ' ';
415 			} while (--count > 0);
416 			aac_internal_transfer(scsicmd, d,
417 			  offsetof(struct inquiry_data, inqd_pid), sizeof(d));
418 		}
419 	}
420 
421 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
422 
423 	aac_fib_complete(fibptr);
424 	aac_fib_free(fibptr);
425 	scsicmd->scsi_done(scsicmd);
426 }
427 
428 /**
429  *	aac_get_container_name	-	get container name, none blocking.
430  */
431 static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
432 {
433 	int status;
434 	struct aac_get_name *dinfo;
435 	struct fib * cmd_fibcontext;
436 	struct aac_dev * dev;
437 
438 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
439 
440 	if (!(cmd_fibcontext = aac_fib_alloc(dev)))
441 		return -ENOMEM;
442 
443 	aac_fib_init(cmd_fibcontext);
444 	dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
445 
446 	dinfo->command = cpu_to_le32(VM_ContainerConfig);
447 	dinfo->type = cpu_to_le32(CT_READ_NAME);
448 	dinfo->cid = cpu_to_le32(cid);
449 	dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
450 
451 	status = aac_fib_send(ContainerCommand,
452 		  cmd_fibcontext,
453 		  sizeof (struct aac_get_name),
454 		  FsaNormal,
455 		  0, 1,
456 		  (fib_callback) get_container_name_callback,
457 		  (void *) scsicmd);
458 
459 	/*
460 	 *	Check that the command queued to the controller
461 	 */
462 	if (status == -EINPROGRESS) {
463 		scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
464 		return 0;
465 	}
466 
467 	printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
468 	aac_fib_complete(cmd_fibcontext);
469 	aac_fib_free(cmd_fibcontext);
470 	return -1;
471 }
472 
473 /**
474  *	aac_probe_container		-	query a logical volume
475  *	@dev: device to query
476  *	@cid: container identifier
477  *
478  *	Queries the controller about the given volume. The volume information
479  *	is updated in the struct fsa_dev_info structure rather than returned.
480  */
481 
482 int aac_probe_container(struct aac_dev *dev, int cid)
483 {
484 	struct fsa_dev_info *fsa_dev_ptr;
485 	int status;
486 	struct aac_query_mount *dinfo;
487 	struct aac_mount *dresp;
488 	struct fib * fibptr;
489 	unsigned instance;
490 
491 	fsa_dev_ptr = dev->fsa_dev;
492 	instance = dev->scsi_host_ptr->unique_id;
493 
494 	if (!(fibptr = aac_fib_alloc(dev)))
495 		return -ENOMEM;
496 
497 	aac_fib_init(fibptr);
498 
499 	dinfo = (struct aac_query_mount *)fib_data(fibptr);
500 
501 	dinfo->command = cpu_to_le32(VM_NameServe);
502 	dinfo->count = cpu_to_le32(cid);
503 	dinfo->type = cpu_to_le32(FT_FILESYS);
504 
505 	status = aac_fib_send(ContainerCommand,
506 			    fibptr,
507 			    sizeof(struct aac_query_mount),
508 			    FsaNormal,
509 			    1, 1,
510 			    NULL, NULL);
511 	if (status < 0) {
512 		printk(KERN_WARNING "aacraid: aac_probe_container query failed.\n");
513 		goto error;
514 	}
515 
516 	dresp = (struct aac_mount *) fib_data(fibptr);
517 
518 	if ((le32_to_cpu(dresp->status) == ST_OK) &&
519 	    (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
520 		dinfo->command = cpu_to_le32(VM_NameServe64);
521 		dinfo->count = cpu_to_le32(cid);
522 		dinfo->type = cpu_to_le32(FT_FILESYS);
523 
524 		if (aac_fib_send(ContainerCommand,
525 			    fibptr,
526 			    sizeof(struct aac_query_mount),
527 			    FsaNormal,
528 			    1, 1,
529 			    NULL, NULL) < 0)
530 			goto error;
531 	} else
532 		dresp->mnt[0].capacityhigh = 0;
533 
534 	if ((le32_to_cpu(dresp->status) == ST_OK) &&
535 	    (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
536 	    (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
537 		fsa_dev_ptr[cid].valid = 1;
538 		fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
539 		fsa_dev_ptr[cid].size
540 		  = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
541 		    (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
542 		if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
543 			fsa_dev_ptr[cid].ro = 1;
544 	}
545 
546 error:
547 	aac_fib_complete(fibptr);
548 	aac_fib_free(fibptr);
549 
550 	return status;
551 }
552 
553 /* Local Structure to set SCSI inquiry data strings */
554 struct scsi_inq {
555 	char vid[8];         /* Vendor ID */
556 	char pid[16];        /* Product ID */
557 	char prl[4];         /* Product Revision Level */
558 };
559 
560 /**
561  *	InqStrCopy	-	string merge
562  *	@a:	string to copy from
563  *	@b:	string to copy to
564  *
565  * 	Copy a String from one location to another
566  *	without copying \0
567  */
568 
569 static void inqstrcpy(char *a, char *b)
570 {
571 
572 	while(*a != (char)0)
573 		*b++ = *a++;
574 }
575 
576 static char *container_types[] = {
577         "None",
578         "Volume",
579         "Mirror",
580         "Stripe",
581         "RAID5",
582         "SSRW",
583         "SSRO",
584         "Morph",
585         "Legacy",
586         "RAID4",
587         "RAID10",
588         "RAID00",
589         "V-MIRRORS",
590         "PSEUDO R4",
591 	"RAID50",
592 	"RAID5D",
593 	"RAID5D0",
594 	"RAID1E",
595 	"RAID6",
596 	"RAID60",
597         "Unknown"
598 };
599 
600 
601 
602 /* Function: setinqstr
603  *
604  * Arguments: [1] pointer to void [1] int
605  *
606  * Purpose: Sets SCSI inquiry data strings for vendor, product
607  * and revision level. Allows strings to be set in platform dependant
608  * files instead of in OS dependant driver source.
609  */
610 
611 static void setinqstr(struct aac_dev *dev, void *data, int tindex)
612 {
613 	struct scsi_inq *str;
614 
615 	str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
616 	memset(str, ' ', sizeof(*str));
617 
618 	if (dev->supplement_adapter_info.AdapterTypeText[0]) {
619 		char * cp = dev->supplement_adapter_info.AdapterTypeText;
620 		int c = sizeof(str->vid);
621 		while (*cp && *cp != ' ' && --c)
622 			++cp;
623 		c = *cp;
624 		*cp = '\0';
625 		inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
626 		  str->vid);
627 		*cp = c;
628 		while (*cp && *cp != ' ')
629 			++cp;
630 		while (*cp == ' ')
631 			++cp;
632 		/* last six chars reserved for vol type */
633 		c = 0;
634 		if (strlen(cp) > sizeof(str->pid)) {
635 			c = cp[sizeof(str->pid)];
636 			cp[sizeof(str->pid)] = '\0';
637 		}
638 		inqstrcpy (cp, str->pid);
639 		if (c)
640 			cp[sizeof(str->pid)] = c;
641 	} else {
642 		struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
643 
644 		inqstrcpy (mp->vname, str->vid);
645 		/* last six chars reserved for vol type */
646 		inqstrcpy (mp->model, str->pid);
647 	}
648 
649 	if (tindex < ARRAY_SIZE(container_types)){
650 		char *findit = str->pid;
651 
652 		for ( ; *findit != ' '; findit++); /* walk till we find a space */
653 		/* RAID is superfluous in the context of a RAID device */
654 		if (memcmp(findit-4, "RAID", 4) == 0)
655 			*(findit -= 4) = ' ';
656 		if (((findit - str->pid) + strlen(container_types[tindex]))
657 		 < (sizeof(str->pid) + sizeof(str->prl)))
658 			inqstrcpy (container_types[tindex], findit + 1);
659 	}
660 	inqstrcpy ("V1.0", str->prl);
661 }
662 
663 static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
664 		      u8 a_sense_code, u8 incorrect_length,
665 		      u8 bit_pointer, u16 field_pointer,
666 		      u32 residue)
667 {
668 	sense_buf[0] = 0xF0;	/* Sense data valid, err code 70h (current error) */
669 	sense_buf[1] = 0;	/* Segment number, always zero */
670 
671 	if (incorrect_length) {
672 		sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
673 		sense_buf[3] = BYTE3(residue);
674 		sense_buf[4] = BYTE2(residue);
675 		sense_buf[5] = BYTE1(residue);
676 		sense_buf[6] = BYTE0(residue);
677 	} else
678 		sense_buf[2] = sense_key;	/* Sense key */
679 
680 	if (sense_key == ILLEGAL_REQUEST)
681 		sense_buf[7] = 10;	/* Additional sense length */
682 	else
683 		sense_buf[7] = 6;	/* Additional sense length */
684 
685 	sense_buf[12] = sense_code;	/* Additional sense code */
686 	sense_buf[13] = a_sense_code;	/* Additional sense code qualifier */
687 	if (sense_key == ILLEGAL_REQUEST) {
688 		sense_buf[15] = 0;
689 
690 		if (sense_code == SENCODE_INVALID_PARAM_FIELD)
691 			sense_buf[15] = 0x80;/* Std sense key specific field */
692 		/* Illegal parameter is in the parameter block */
693 
694 		if (sense_code == SENCODE_INVALID_CDB_FIELD)
695 			sense_buf[15] = 0xc0;/* Std sense key specific field */
696 		/* Illegal parameter is in the CDB block */
697 		sense_buf[15] |= bit_pointer;
698 		sense_buf[16] = field_pointer >> 8;	/* MSB */
699 		sense_buf[17] = field_pointer;		/* LSB */
700 	}
701 }
702 
703 int aac_get_adapter_info(struct aac_dev* dev)
704 {
705 	struct fib* fibptr;
706 	int rcode;
707 	u32 tmp;
708 	struct aac_adapter_info *info;
709 	struct aac_bus_info *command;
710 	struct aac_bus_info_response *bus_info;
711 
712 	if (!(fibptr = aac_fib_alloc(dev)))
713 		return -ENOMEM;
714 
715 	aac_fib_init(fibptr);
716 	info = (struct aac_adapter_info *) fib_data(fibptr);
717 	memset(info,0,sizeof(*info));
718 
719 	rcode = aac_fib_send(RequestAdapterInfo,
720 			 fibptr,
721 			 sizeof(*info),
722 			 FsaNormal,
723 			 -1, 1, /* First `interrupt' command uses special wait */
724 			 NULL,
725 			 NULL);
726 
727 	if (rcode < 0) {
728 		aac_fib_complete(fibptr);
729 		aac_fib_free(fibptr);
730 		return rcode;
731 	}
732 	memcpy(&dev->adapter_info, info, sizeof(*info));
733 
734 	if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
735 		struct aac_supplement_adapter_info * info;
736 
737 		aac_fib_init(fibptr);
738 
739 		info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
740 
741 		memset(info,0,sizeof(*info));
742 
743 		rcode = aac_fib_send(RequestSupplementAdapterInfo,
744 				 fibptr,
745 				 sizeof(*info),
746 				 FsaNormal,
747 				 1, 1,
748 				 NULL,
749 				 NULL);
750 
751 		if (rcode >= 0)
752 			memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
753 	}
754 
755 
756 	/*
757 	 * GetBusInfo
758 	 */
759 
760 	aac_fib_init(fibptr);
761 
762 	bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
763 
764 	memset(bus_info, 0, sizeof(*bus_info));
765 
766 	command = (struct aac_bus_info *)bus_info;
767 
768 	command->Command = cpu_to_le32(VM_Ioctl);
769 	command->ObjType = cpu_to_le32(FT_DRIVE);
770 	command->MethodId = cpu_to_le32(1);
771 	command->CtlCmd = cpu_to_le32(GetBusInfo);
772 
773 	rcode = aac_fib_send(ContainerCommand,
774 			 fibptr,
775 			 sizeof (*bus_info),
776 			 FsaNormal,
777 			 1, 1,
778 			 NULL, NULL);
779 
780 	if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
781 		dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
782 		dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
783 	}
784 
785 	tmp = le32_to_cpu(dev->adapter_info.kernelrev);
786 	printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
787 			dev->name,
788 			dev->id,
789 			tmp>>24,
790 			(tmp>>16)&0xff,
791 			tmp&0xff,
792 			le32_to_cpu(dev->adapter_info.kernelbuild),
793 			(int)sizeof(dev->supplement_adapter_info.BuildDate),
794 			dev->supplement_adapter_info.BuildDate);
795 	tmp = le32_to_cpu(dev->adapter_info.monitorrev);
796 	printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
797 			dev->name, dev->id,
798 			tmp>>24,(tmp>>16)&0xff,tmp&0xff,
799 			le32_to_cpu(dev->adapter_info.monitorbuild));
800 	tmp = le32_to_cpu(dev->adapter_info.biosrev);
801 	printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
802 			dev->name, dev->id,
803 			tmp>>24,(tmp>>16)&0xff,tmp&0xff,
804 			le32_to_cpu(dev->adapter_info.biosbuild));
805 	if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
806 		printk(KERN_INFO "%s%d: serial %x\n",
807 			dev->name, dev->id,
808 			le32_to_cpu(dev->adapter_info.serial[0]));
809 
810 	dev->nondasd_support = 0;
811 	dev->raid_scsi_mode = 0;
812 	if(dev->adapter_info.options & AAC_OPT_NONDASD){
813 		dev->nondasd_support = 1;
814 	}
815 
816 	/*
817 	 * If the firmware supports ROMB RAID/SCSI mode and we are currently
818 	 * in RAID/SCSI mode, set the flag. For now if in this mode we will
819 	 * force nondasd support on. If we decide to allow the non-dasd flag
820 	 * additional changes changes will have to be made to support
821 	 * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
822 	 * changed to support the new dev->raid_scsi_mode flag instead of
823 	 * leaching off of the dev->nondasd_support flag. Also in linit.c the
824 	 * function aac_detect will have to be modified where it sets up the
825 	 * max number of channels based on the aac->nondasd_support flag only.
826 	 */
827 	if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
828 	    (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
829 		dev->nondasd_support = 1;
830 		dev->raid_scsi_mode = 1;
831 	}
832 	if (dev->raid_scsi_mode != 0)
833 		printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
834 				dev->name, dev->id);
835 
836 	if(nondasd != -1) {
837 		dev->nondasd_support = (nondasd!=0);
838 	}
839 	if(dev->nondasd_support != 0){
840 		printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
841 	}
842 
843 	dev->dac_support = 0;
844 	if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
845 		printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
846 		dev->dac_support = 1;
847 	}
848 
849 	if(dacmode != -1) {
850 		dev->dac_support = (dacmode!=0);
851 	}
852 	if(dev->dac_support != 0) {
853 		if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
854 			!pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
855 			printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
856 				dev->name, dev->id);
857 		} else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
858 			!pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
859 			printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
860 				dev->name, dev->id);
861 			dev->dac_support = 0;
862 		} else {
863 			printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
864 				dev->name, dev->id);
865 			rcode = -ENOMEM;
866 		}
867 	}
868 	/*
869 	 * 57 scatter gather elements
870 	 */
871 	if (!(dev->raw_io_interface)) {
872 		dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
873 			sizeof(struct aac_fibhdr) -
874 			sizeof(struct aac_write) + sizeof(struct sgentry)) /
875 				sizeof(struct sgentry);
876 		if (dev->dac_support) {
877 			/*
878 			 * 38 scatter gather elements
879 			 */
880 			dev->scsi_host_ptr->sg_tablesize =
881 				(dev->max_fib_size -
882 				sizeof(struct aac_fibhdr) -
883 				sizeof(struct aac_write64) +
884 				sizeof(struct sgentry64)) /
885 					sizeof(struct sgentry64);
886 		}
887 		dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
888 		if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
889 			/*
890 			 * Worst case size that could cause sg overflow when
891 			 * we break up SG elements that are larger than 64KB.
892 			 * Would be nice if we could tell the SCSI layer what
893 			 * the maximum SG element size can be. Worst case is
894 			 * (sg_tablesize-1) 4KB elements with one 64KB
895 			 * element.
896 			 *	32bit -> 468 or 238KB	64bit -> 424 or 212KB
897 			 */
898 			dev->scsi_host_ptr->max_sectors =
899 			  (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
900 		}
901 	}
902 
903 	aac_fib_complete(fibptr);
904 	aac_fib_free(fibptr);
905 
906 	return rcode;
907 }
908 
909 
910 static void io_callback(void *context, struct fib * fibptr)
911 {
912 	struct aac_dev *dev;
913 	struct aac_read_reply *readreply;
914 	struct scsi_cmnd *scsicmd;
915 	u32 cid;
916 
917 	scsicmd = (struct scsi_cmnd *) context;
918 	scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
919 
920 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
921 	cid = scmd_id(scsicmd);
922 
923 	if (nblank(dprintk(x))) {
924 		u64 lba;
925 		switch (scsicmd->cmnd[0]) {
926 		case WRITE_6:
927 		case READ_6:
928 			lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
929 			    (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
930 			break;
931 		case WRITE_16:
932 		case READ_16:
933 			lba = ((u64)scsicmd->cmnd[2] << 56) |
934 			      ((u64)scsicmd->cmnd[3] << 48) |
935 			      ((u64)scsicmd->cmnd[4] << 40) |
936 			      ((u64)scsicmd->cmnd[5] << 32) |
937 			      ((u64)scsicmd->cmnd[6] << 24) |
938 			      (scsicmd->cmnd[7] << 16) |
939 			      (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
940 			break;
941 		case WRITE_12:
942 		case READ_12:
943 			lba = ((u64)scsicmd->cmnd[2] << 24) |
944 			      (scsicmd->cmnd[3] << 16) |
945 			      (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
946 			break;
947 		default:
948 			lba = ((u64)scsicmd->cmnd[2] << 24) |
949 			       (scsicmd->cmnd[3] << 16) |
950 			       (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
951 			break;
952 		}
953 		printk(KERN_DEBUG
954 		  "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
955 		  smp_processor_id(), (unsigned long long)lba, jiffies);
956 	}
957 
958 	BUG_ON(fibptr == NULL);
959 
960 	if(scsicmd->use_sg)
961 		pci_unmap_sg(dev->pdev,
962 			(struct scatterlist *)scsicmd->request_buffer,
963 			scsicmd->use_sg,
964 			scsicmd->sc_data_direction);
965 	else if(scsicmd->request_bufflen)
966 		pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
967 				 scsicmd->request_bufflen,
968 				 scsicmd->sc_data_direction);
969 	readreply = (struct aac_read_reply *)fib_data(fibptr);
970 	if (le32_to_cpu(readreply->status) == ST_OK)
971 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
972 	else {
973 #ifdef AAC_DETAILED_STATUS_INFO
974 		printk(KERN_WARNING "io_callback: io failed, status = %d\n",
975 		  le32_to_cpu(readreply->status));
976 #endif
977 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
978 		set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
979 				    HARDWARE_ERROR,
980 				    SENCODE_INTERNAL_TARGET_FAILURE,
981 				    ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
982 				    0, 0);
983 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
984 		  (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
985 		    ? sizeof(scsicmd->sense_buffer)
986 		    : sizeof(dev->fsa_dev[cid].sense_data));
987 	}
988 	aac_fib_complete(fibptr);
989 	aac_fib_free(fibptr);
990 
991 	scsicmd->scsi_done(scsicmd);
992 }
993 
994 static int aac_read(struct scsi_cmnd * scsicmd, int cid)
995 {
996 	u64 lba;
997 	u32 count;
998 	int status;
999 
1000 	u16 fibsize;
1001 	struct aac_dev *dev;
1002 	struct fib * cmd_fibcontext;
1003 
1004 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1005 	/*
1006 	 *	Get block address and transfer length
1007 	 */
1008 	switch (scsicmd->cmnd[0]) {
1009 	case READ_6:
1010 		dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
1011 
1012 		lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1013 			(scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1014 		count = scsicmd->cmnd[4];
1015 
1016 		if (count == 0)
1017 			count = 256;
1018 		break;
1019 	case READ_16:
1020 		dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", cid));
1021 
1022 		lba = 	((u64)scsicmd->cmnd[2] << 56) |
1023 		 	((u64)scsicmd->cmnd[3] << 48) |
1024 			((u64)scsicmd->cmnd[4] << 40) |
1025 			((u64)scsicmd->cmnd[5] << 32) |
1026 			((u64)scsicmd->cmnd[6] << 24) |
1027 			(scsicmd->cmnd[7] << 16) |
1028 			(scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1029 		count = (scsicmd->cmnd[10] << 24) |
1030 			(scsicmd->cmnd[11] << 16) |
1031 			(scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1032 		break;
1033 	case READ_12:
1034 		dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", cid));
1035 
1036 		lba = ((u64)scsicmd->cmnd[2] << 24) |
1037 			(scsicmd->cmnd[3] << 16) |
1038 		    	(scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1039 		count = (scsicmd->cmnd[6] << 24) |
1040 			(scsicmd->cmnd[7] << 16) |
1041 		      	(scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1042 		break;
1043 	default:
1044 		dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
1045 
1046 		lba = ((u64)scsicmd->cmnd[2] << 24) |
1047 			(scsicmd->cmnd[3] << 16) |
1048 			(scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1049 		count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1050 		break;
1051 	}
1052 	dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
1053 	  smp_processor_id(), (unsigned long long)lba, jiffies));
1054 	if ((!(dev->raw_io_interface) || !(dev->raw_io_64)) &&
1055 		(lba & 0xffffffff00000000LL)) {
1056 		dprintk((KERN_DEBUG "aac_read: Illegal lba\n"));
1057 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1058 			SAM_STAT_CHECK_CONDITION;
1059 		set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1060 			    HARDWARE_ERROR,
1061 			    SENCODE_INTERNAL_TARGET_FAILURE,
1062 			    ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1063 			    0, 0);
1064 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1065 		  (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1066 		    ? sizeof(scsicmd->sense_buffer)
1067 		    : sizeof(dev->fsa_dev[cid].sense_data));
1068 		scsicmd->scsi_done(scsicmd);
1069 		return 0;
1070 	}
1071 	/*
1072 	 *	Alocate and initialize a Fib
1073 	 */
1074 	if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1075 		return -1;
1076 	}
1077 
1078 	aac_fib_init(cmd_fibcontext);
1079 
1080 	if (dev->raw_io_interface) {
1081 		struct aac_raw_io *readcmd;
1082 		readcmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
1083 		readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1084 		readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1085 		readcmd->count = cpu_to_le32(count<<9);
1086 		readcmd->cid = cpu_to_le16(cid);
1087 		readcmd->flags = cpu_to_le16(1);
1088 		readcmd->bpTotal = 0;
1089 		readcmd->bpComplete = 0;
1090 
1091 		aac_build_sgraw(scsicmd, &readcmd->sg);
1092 		fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
1093 		BUG_ON(fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)));
1094 		/*
1095 		 *	Now send the Fib to the adapter
1096 		 */
1097 		status = aac_fib_send(ContainerRawIo,
1098 			  cmd_fibcontext,
1099 			  fibsize,
1100 			  FsaNormal,
1101 			  0, 1,
1102 			  (fib_callback) io_callback,
1103 			  (void *) scsicmd);
1104 	} else if (dev->dac_support == 1) {
1105 		struct aac_read64 *readcmd;
1106 		readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
1107 		readcmd->command = cpu_to_le32(VM_CtHostRead64);
1108 		readcmd->cid = cpu_to_le16(cid);
1109 		readcmd->sector_count = cpu_to_le16(count);
1110 		readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1111 		readcmd->pad   = 0;
1112 		readcmd->flags = 0;
1113 
1114 		aac_build_sg64(scsicmd, &readcmd->sg);
1115 		fibsize = sizeof(struct aac_read64) +
1116 			((le32_to_cpu(readcmd->sg.count) - 1) *
1117 			 sizeof (struct sgentry64));
1118 		BUG_ON (fibsize > (dev->max_fib_size -
1119 					sizeof(struct aac_fibhdr)));
1120 		/*
1121 		 *	Now send the Fib to the adapter
1122 		 */
1123 		status = aac_fib_send(ContainerCommand64,
1124 			  cmd_fibcontext,
1125 			  fibsize,
1126 			  FsaNormal,
1127 			  0, 1,
1128 			  (fib_callback) io_callback,
1129 			  (void *) scsicmd);
1130 	} else {
1131 		struct aac_read *readcmd;
1132 		readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
1133 		readcmd->command = cpu_to_le32(VM_CtBlockRead);
1134 		readcmd->cid = cpu_to_le32(cid);
1135 		readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1136 		readcmd->count = cpu_to_le32(count * 512);
1137 
1138 		aac_build_sg(scsicmd, &readcmd->sg);
1139 		fibsize = sizeof(struct aac_read) +
1140 			((le32_to_cpu(readcmd->sg.count) - 1) *
1141 			 sizeof (struct sgentry));
1142 		BUG_ON (fibsize > (dev->max_fib_size -
1143 					sizeof(struct aac_fibhdr)));
1144 		/*
1145 		 *	Now send the Fib to the adapter
1146 		 */
1147 		status = aac_fib_send(ContainerCommand,
1148 			  cmd_fibcontext,
1149 			  fibsize,
1150 			  FsaNormal,
1151 			  0, 1,
1152 			  (fib_callback) io_callback,
1153 			  (void *) scsicmd);
1154 	}
1155 
1156 
1157 
1158 	/*
1159 	 *	Check that the command queued to the controller
1160 	 */
1161 	if (status == -EINPROGRESS) {
1162 		scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1163 		return 0;
1164 	}
1165 
1166 	printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
1167 	/*
1168 	 *	For some reason, the Fib didn't queue, return QUEUE_FULL
1169 	 */
1170 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1171 	scsicmd->scsi_done(scsicmd);
1172 	aac_fib_complete(cmd_fibcontext);
1173 	aac_fib_free(cmd_fibcontext);
1174 	return 0;
1175 }
1176 
1177 static int aac_write(struct scsi_cmnd * scsicmd, int cid)
1178 {
1179 	u64 lba;
1180 	u32 count;
1181 	int status;
1182 	u16 fibsize;
1183 	struct aac_dev *dev;
1184 	struct fib * cmd_fibcontext;
1185 
1186 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1187 	/*
1188 	 *	Get block address and transfer length
1189 	 */
1190 	if (scsicmd->cmnd[0] == WRITE_6)	/* 6 byte command */
1191 	{
1192 		lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1193 		count = scsicmd->cmnd[4];
1194 		if (count == 0)
1195 			count = 256;
1196 	} else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
1197 		dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", cid));
1198 
1199 		lba = 	((u64)scsicmd->cmnd[2] << 56) |
1200 			((u64)scsicmd->cmnd[3] << 48) |
1201 			((u64)scsicmd->cmnd[4] << 40) |
1202 			((u64)scsicmd->cmnd[5] << 32) |
1203 			((u64)scsicmd->cmnd[6] << 24) |
1204 			(scsicmd->cmnd[7] << 16) |
1205 			(scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1206 		count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1207 			(scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1208 	} else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
1209 		dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", cid));
1210 
1211 		lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1212 		    | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1213 		count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1214 		      | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1215 	} else {
1216 		dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
1217 		lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1218 		count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
1219 	}
1220 	dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
1221 	  smp_processor_id(), (unsigned long long)lba, jiffies));
1222 	if ((!(dev->raw_io_interface) || !(dev->raw_io_64))
1223 	 && (lba & 0xffffffff00000000LL)) {
1224 		dprintk((KERN_DEBUG "aac_write: Illegal lba\n"));
1225 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1226 		set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1227 			    HARDWARE_ERROR,
1228 			    SENCODE_INTERNAL_TARGET_FAILURE,
1229 			    ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1230 			    0, 0);
1231 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1232 		  (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1233 		    ? sizeof(scsicmd->sense_buffer)
1234 		    : sizeof(dev->fsa_dev[cid].sense_data));
1235 		scsicmd->scsi_done(scsicmd);
1236 		return 0;
1237 	}
1238 	/*
1239 	 *	Allocate and initialize a Fib then setup a BlockWrite command
1240 	 */
1241 	if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
1242 		scsicmd->result = DID_ERROR << 16;
1243 		scsicmd->scsi_done(scsicmd);
1244 		return 0;
1245 	}
1246 	aac_fib_init(cmd_fibcontext);
1247 
1248 	if (dev->raw_io_interface) {
1249 		struct aac_raw_io *writecmd;
1250 		writecmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
1251 		writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1252 		writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1253 		writecmd->count = cpu_to_le32(count<<9);
1254 		writecmd->cid = cpu_to_le16(cid);
1255 		writecmd->flags = 0;
1256 		writecmd->bpTotal = 0;
1257 		writecmd->bpComplete = 0;
1258 
1259 		aac_build_sgraw(scsicmd, &writecmd->sg);
1260 		fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
1261 		BUG_ON(fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)));
1262 		/*
1263 		 *	Now send the Fib to the adapter
1264 		 */
1265 		status = aac_fib_send(ContainerRawIo,
1266 			  cmd_fibcontext,
1267 			  fibsize,
1268 			  FsaNormal,
1269 			  0, 1,
1270 			  (fib_callback) io_callback,
1271 			  (void *) scsicmd);
1272 	} else if (dev->dac_support == 1) {
1273 		struct aac_write64 *writecmd;
1274 		writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
1275 		writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1276 		writecmd->cid = cpu_to_le16(cid);
1277 		writecmd->sector_count = cpu_to_le16(count);
1278 		writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1279 		writecmd->pad	= 0;
1280 		writecmd->flags	= 0;
1281 
1282 		aac_build_sg64(scsicmd, &writecmd->sg);
1283 		fibsize = sizeof(struct aac_write64) +
1284 			((le32_to_cpu(writecmd->sg.count) - 1) *
1285 			 sizeof (struct sgentry64));
1286 		BUG_ON (fibsize > (dev->max_fib_size -
1287 					sizeof(struct aac_fibhdr)));
1288 		/*
1289 		 *	Now send the Fib to the adapter
1290 		 */
1291 		status = aac_fib_send(ContainerCommand64,
1292 			  cmd_fibcontext,
1293 			  fibsize,
1294 			  FsaNormal,
1295 			  0, 1,
1296 			  (fib_callback) io_callback,
1297 			  (void *) scsicmd);
1298 	} else {
1299 		struct aac_write *writecmd;
1300 		writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
1301 		writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1302 		writecmd->cid = cpu_to_le32(cid);
1303 		writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1304 		writecmd->count = cpu_to_le32(count * 512);
1305 		writecmd->sg.count = cpu_to_le32(1);
1306 		/* ->stable is not used - it did mean which type of write */
1307 
1308 		aac_build_sg(scsicmd, &writecmd->sg);
1309 		fibsize = sizeof(struct aac_write) +
1310 			((le32_to_cpu(writecmd->sg.count) - 1) *
1311 			 sizeof (struct sgentry));
1312 		BUG_ON (fibsize > (dev->max_fib_size -
1313 					sizeof(struct aac_fibhdr)));
1314 		/*
1315 		 *	Now send the Fib to the adapter
1316 		 */
1317 		status = aac_fib_send(ContainerCommand,
1318 			  cmd_fibcontext,
1319 			  fibsize,
1320 			  FsaNormal,
1321 			  0, 1,
1322 			  (fib_callback) io_callback,
1323 			  (void *) scsicmd);
1324 	}
1325 
1326 	/*
1327 	 *	Check that the command queued to the controller
1328 	 */
1329 	if (status == -EINPROGRESS) {
1330 		scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1331 		return 0;
1332 	}
1333 
1334 	printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
1335 	/*
1336 	 *	For some reason, the Fib didn't queue, return QUEUE_FULL
1337 	 */
1338 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
1339 	scsicmd->scsi_done(scsicmd);
1340 
1341 	aac_fib_complete(cmd_fibcontext);
1342 	aac_fib_free(cmd_fibcontext);
1343 	return 0;
1344 }
1345 
1346 static void synchronize_callback(void *context, struct fib *fibptr)
1347 {
1348 	struct aac_synchronize_reply *synchronizereply;
1349 	struct scsi_cmnd *cmd;
1350 
1351 	cmd = context;
1352 	cmd->SCp.phase = AAC_OWNER_MIDLEVEL;
1353 
1354 	dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
1355 				smp_processor_id(), jiffies));
1356 	BUG_ON(fibptr == NULL);
1357 
1358 
1359 	synchronizereply = fib_data(fibptr);
1360 	if (le32_to_cpu(synchronizereply->status) == CT_OK)
1361 		cmd->result = DID_OK << 16 |
1362 			COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1363 	else {
1364 		struct scsi_device *sdev = cmd->device;
1365 		struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
1366 		u32 cid = sdev_id(sdev);
1367 		printk(KERN_WARNING
1368 		     "synchronize_callback: synchronize failed, status = %d\n",
1369 		     le32_to_cpu(synchronizereply->status));
1370 		cmd->result = DID_OK << 16 |
1371 			COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1372 		set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
1373 				    HARDWARE_ERROR,
1374 				    SENCODE_INTERNAL_TARGET_FAILURE,
1375 				    ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
1376 				    0, 0);
1377 		memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1378 		  min(sizeof(dev->fsa_dev[cid].sense_data),
1379 			  sizeof(cmd->sense_buffer)));
1380 	}
1381 
1382 	aac_fib_complete(fibptr);
1383 	aac_fib_free(fibptr);
1384 	cmd->scsi_done(cmd);
1385 }
1386 
1387 static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
1388 {
1389 	int status;
1390 	struct fib *cmd_fibcontext;
1391 	struct aac_synchronize *synchronizecmd;
1392 	struct scsi_cmnd *cmd;
1393 	struct scsi_device *sdev = scsicmd->device;
1394 	int active = 0;
1395 	unsigned long flags;
1396 
1397 	/*
1398 	 * Wait for all outstanding queued commands to complete to this
1399 	 * specific target (block).
1400 	 */
1401 	spin_lock_irqsave(&sdev->list_lock, flags);
1402 	list_for_each_entry(cmd, &sdev->cmd_list, list)
1403 		if (cmd != scsicmd && cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
1404 			++active;
1405 			break;
1406 		}
1407 
1408 	spin_unlock_irqrestore(&sdev->list_lock, flags);
1409 
1410 	/*
1411 	 *	Yield the processor (requeue for later)
1412 	 */
1413 	if (active)
1414 		return SCSI_MLQUEUE_DEVICE_BUSY;
1415 
1416 	/*
1417 	 *	Allocate and initialize a Fib
1418 	 */
1419 	if (!(cmd_fibcontext =
1420 	    aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata)))
1421 		return SCSI_MLQUEUE_HOST_BUSY;
1422 
1423 	aac_fib_init(cmd_fibcontext);
1424 
1425 	synchronizecmd = fib_data(cmd_fibcontext);
1426 	synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
1427 	synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
1428 	synchronizecmd->cid = cpu_to_le32(cid);
1429 	synchronizecmd->count =
1430 	     cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
1431 
1432 	/*
1433 	 *	Now send the Fib to the adapter
1434 	 */
1435 	status = aac_fib_send(ContainerCommand,
1436 		  cmd_fibcontext,
1437 		  sizeof(struct aac_synchronize),
1438 		  FsaNormal,
1439 		  0, 1,
1440 		  (fib_callback)synchronize_callback,
1441 		  (void *)scsicmd);
1442 
1443 	/*
1444 	 *	Check that the command queued to the controller
1445 	 */
1446 	if (status == -EINPROGRESS) {
1447 		scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1448 		return 0;
1449 	}
1450 
1451 	printk(KERN_WARNING
1452 		"aac_synchronize: aac_fib_send failed with status: %d.\n", status);
1453 	aac_fib_complete(cmd_fibcontext);
1454 	aac_fib_free(cmd_fibcontext);
1455 	return SCSI_MLQUEUE_HOST_BUSY;
1456 }
1457 
1458 /**
1459  *	aac_scsi_cmd()		-	Process SCSI command
1460  *	@scsicmd:		SCSI command block
1461  *
1462  *	Emulate a SCSI command and queue the required request for the
1463  *	aacraid firmware.
1464  */
1465 
1466 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1467 {
1468 	u32 cid = 0;
1469 	struct Scsi_Host *host = scsicmd->device->host;
1470 	struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1471 	struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
1472 
1473 	/*
1474 	 *	If the bus, id or lun is out of range, return fail
1475 	 *	Test does not apply to ID 16, the pseudo id for the controller
1476 	 *	itself.
1477 	 */
1478 	if (scmd_id(scsicmd) != host->this_id) {
1479 		if ((scmd_channel(scsicmd) == CONTAINER_CHANNEL)) {
1480 			if((scmd_id(scsicmd) >= dev->maximum_num_containers) ||
1481 					(scsicmd->device->lun != 0)) {
1482 				scsicmd->result = DID_NO_CONNECT << 16;
1483 				scsicmd->scsi_done(scsicmd);
1484 				return 0;
1485 			}
1486 			cid = scmd_id(scsicmd);
1487 
1488 			/*
1489 			 *	If the target container doesn't exist, it may have
1490 			 *	been newly created
1491 			 */
1492 			if ((fsa_dev_ptr[cid].valid & 1) == 0) {
1493 				switch (scsicmd->cmnd[0]) {
1494 				case SERVICE_ACTION_IN:
1495 					if (!(dev->raw_io_interface) ||
1496 					    !(dev->raw_io_64) ||
1497 					    ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1498 						break;
1499 				case INQUIRY:
1500 				case READ_CAPACITY:
1501 				case TEST_UNIT_READY:
1502 					spin_unlock_irq(host->host_lock);
1503 					aac_probe_container(dev, cid);
1504 					if ((fsa_dev_ptr[cid].valid & 1) == 0)
1505 						fsa_dev_ptr[cid].valid = 0;
1506 					spin_lock_irq(host->host_lock);
1507 					if (fsa_dev_ptr[cid].valid == 0) {
1508 						scsicmd->result = DID_NO_CONNECT << 16;
1509 						scsicmd->scsi_done(scsicmd);
1510 						return 0;
1511 					}
1512 				default:
1513 					break;
1514 				}
1515 			}
1516 			/*
1517 			 *	If the target container still doesn't exist,
1518 			 *	return failure
1519 			 */
1520 			if (fsa_dev_ptr[cid].valid == 0) {
1521 				scsicmd->result = DID_BAD_TARGET << 16;
1522 				scsicmd->scsi_done(scsicmd);
1523 				return 0;
1524 			}
1525 		} else {  /* check for physical non-dasd devices */
1526 			if(dev->nondasd_support == 1){
1527 				return aac_send_srb_fib(scsicmd);
1528 			} else {
1529 				scsicmd->result = DID_NO_CONNECT << 16;
1530 				scsicmd->scsi_done(scsicmd);
1531 				return 0;
1532 			}
1533 		}
1534 	}
1535 	/*
1536 	 * else Command for the controller itself
1537 	 */
1538 	else if ((scsicmd->cmnd[0] != INQUIRY) &&	/* only INQUIRY & TUR cmnd supported for controller */
1539 		(scsicmd->cmnd[0] != TEST_UNIT_READY))
1540 	{
1541 		dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
1542 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1543 		set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1544 			    ILLEGAL_REQUEST,
1545 			    SENCODE_INVALID_COMMAND,
1546 			    ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1547 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1548 		  (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1549 		    ? sizeof(scsicmd->sense_buffer)
1550 		    : sizeof(dev->fsa_dev[cid].sense_data));
1551 		scsicmd->scsi_done(scsicmd);
1552 		return 0;
1553 	}
1554 
1555 
1556 	/* Handle commands here that don't really require going out to the adapter */
1557 	switch (scsicmd->cmnd[0]) {
1558 	case INQUIRY:
1559 	{
1560 		struct inquiry_data inq_data;
1561 
1562 		dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scmd_id(scsicmd)));
1563 		memset(&inq_data, 0, sizeof (struct inquiry_data));
1564 
1565 		inq_data.inqd_ver = 2;	/* claim compliance to SCSI-2 */
1566 		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 */
1567 		inq_data.inqd_len = 31;
1568 		/*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
1569 		inq_data.inqd_pad2= 0x32 ;	 /*WBus16|Sync|CmdQue */
1570 		/*
1571 		 *	Set the Vendor, Product, and Revision Level
1572 		 *	see: <vendor>.c i.e. aac.c
1573 		 */
1574 		if (scmd_id(scsicmd) == host->this_id) {
1575 			setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
1576 			inq_data.inqd_pdt = INQD_PDT_PROC;	/* Processor device */
1577 			aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
1578 			scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1579 			scsicmd->scsi_done(scsicmd);
1580 			return 0;
1581 		}
1582 		setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
1583 		inq_data.inqd_pdt = INQD_PDT_DA;	/* Direct/random access device */
1584 		aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
1585 		return aac_get_container_name(scsicmd, cid);
1586 	}
1587 	case SERVICE_ACTION_IN:
1588 		if (!(dev->raw_io_interface) ||
1589 		    !(dev->raw_io_64) ||
1590 		    ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
1591 			break;
1592 	{
1593 		u64 capacity;
1594 		char cp[13];
1595 
1596 		dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
1597 		capacity = fsa_dev_ptr[cid].size - 1;
1598 		cp[0] = (capacity >> 56) & 0xff;
1599 		cp[1] = (capacity >> 48) & 0xff;
1600 		cp[2] = (capacity >> 40) & 0xff;
1601 		cp[3] = (capacity >> 32) & 0xff;
1602 		cp[4] = (capacity >> 24) & 0xff;
1603 		cp[5] = (capacity >> 16) & 0xff;
1604 		cp[6] = (capacity >> 8) & 0xff;
1605 		cp[7] = (capacity >> 0) & 0xff;
1606 		cp[8] = 0;
1607 		cp[9] = 0;
1608 		cp[10] = 2;
1609 		cp[11] = 0;
1610 		cp[12] = 0;
1611 		aac_internal_transfer(scsicmd, cp, 0,
1612 		  min_t(size_t, scsicmd->cmnd[13], sizeof(cp)));
1613 		if (sizeof(cp) < scsicmd->cmnd[13]) {
1614 			unsigned int len, offset = sizeof(cp);
1615 
1616 			memset(cp, 0, offset);
1617 			do {
1618 				len = min_t(size_t, scsicmd->cmnd[13] - offset,
1619 						sizeof(cp));
1620 				aac_internal_transfer(scsicmd, cp, offset, len);
1621 			} while ((offset += len) < scsicmd->cmnd[13]);
1622 		}
1623 
1624 		/* Do not cache partition table for arrays */
1625 		scsicmd->device->removable = 1;
1626 
1627 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1628 		scsicmd->scsi_done(scsicmd);
1629 
1630 		return 0;
1631 	}
1632 
1633 	case READ_CAPACITY:
1634 	{
1635 		u32 capacity;
1636 		char cp[8];
1637 
1638 		dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
1639 		if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
1640 			capacity = fsa_dev_ptr[cid].size - 1;
1641 		else
1642 			capacity = (u32)-1;
1643 
1644 		cp[0] = (capacity >> 24) & 0xff;
1645 		cp[1] = (capacity >> 16) & 0xff;
1646 		cp[2] = (capacity >> 8) & 0xff;
1647 		cp[3] = (capacity >> 0) & 0xff;
1648 		cp[4] = 0;
1649 		cp[5] = 0;
1650 		cp[6] = 2;
1651 		cp[7] = 0;
1652 		aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
1653 		/* Do not cache partition table for arrays */
1654 		scsicmd->device->removable = 1;
1655 
1656 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1657 		scsicmd->scsi_done(scsicmd);
1658 
1659 		return 0;
1660 	}
1661 
1662 	case MODE_SENSE:
1663 	{
1664 		char mode_buf[4];
1665 
1666 		dprintk((KERN_DEBUG "MODE SENSE command.\n"));
1667 		mode_buf[0] = 3;	/* Mode data length */
1668 		mode_buf[1] = 0;	/* Medium type - default */
1669 		mode_buf[2] = 0;	/* Device-specific param, bit 8: 0/1 = write enabled/protected */
1670 		mode_buf[3] = 0;	/* Block descriptor length */
1671 
1672 		aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
1673 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1674 		scsicmd->scsi_done(scsicmd);
1675 
1676 		return 0;
1677 	}
1678 	case MODE_SENSE_10:
1679 	{
1680 		char mode_buf[8];
1681 
1682 		dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
1683 		mode_buf[0] = 0;	/* Mode data length (MSB) */
1684 		mode_buf[1] = 6;	/* Mode data length (LSB) */
1685 		mode_buf[2] = 0;	/* Medium type - default */
1686 		mode_buf[3] = 0;	/* Device-specific param, bit 8: 0/1 = write enabled/protected */
1687 		mode_buf[4] = 0;	/* reserved */
1688 		mode_buf[5] = 0;	/* reserved */
1689 		mode_buf[6] = 0;	/* Block descriptor length (MSB) */
1690 		mode_buf[7] = 0;	/* Block descriptor length (LSB) */
1691 		aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
1692 
1693 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1694 		scsicmd->scsi_done(scsicmd);
1695 
1696 		return 0;
1697 	}
1698 	case REQUEST_SENSE:
1699 		dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
1700 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
1701 		memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
1702 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1703 		scsicmd->scsi_done(scsicmd);
1704 		return 0;
1705 
1706 	case ALLOW_MEDIUM_REMOVAL:
1707 		dprintk((KERN_DEBUG "LOCK command.\n"));
1708 		if (scsicmd->cmnd[4])
1709 			fsa_dev_ptr[cid].locked = 1;
1710 		else
1711 			fsa_dev_ptr[cid].locked = 0;
1712 
1713 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1714 		scsicmd->scsi_done(scsicmd);
1715 		return 0;
1716 	/*
1717 	 *	These commands are all No-Ops
1718 	 */
1719 	case TEST_UNIT_READY:
1720 	case RESERVE:
1721 	case RELEASE:
1722 	case REZERO_UNIT:
1723 	case REASSIGN_BLOCKS:
1724 	case SEEK_10:
1725 	case START_STOP:
1726 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1727 		scsicmd->scsi_done(scsicmd);
1728 		return 0;
1729 	}
1730 
1731 	switch (scsicmd->cmnd[0])
1732 	{
1733 		case READ_6:
1734 		case READ_10:
1735 		case READ_12:
1736 		case READ_16:
1737 			/*
1738 			 *	Hack to keep track of ordinal number of the device that
1739 			 *	corresponds to a container. Needed to convert
1740 			 *	containers to /dev/sd device names
1741 			 */
1742 
1743 			if (scsicmd->request->rq_disk)
1744 				strlcpy(fsa_dev_ptr[cid].devname,
1745 				scsicmd->request->rq_disk->disk_name,
1746 			  	min(sizeof(fsa_dev_ptr[cid].devname),
1747 				sizeof(scsicmd->request->rq_disk->disk_name) + 1));
1748 
1749 			return aac_read(scsicmd, cid);
1750 
1751 		case WRITE_6:
1752 		case WRITE_10:
1753 		case WRITE_12:
1754 		case WRITE_16:
1755 			return aac_write(scsicmd, cid);
1756 
1757 		case SYNCHRONIZE_CACHE:
1758 			/* Issue FIB to tell Firmware to flush it's cache */
1759 			return aac_synchronize(scsicmd, cid);
1760 
1761 		default:
1762 			/*
1763 			 *	Unhandled commands
1764 			 */
1765 			dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
1766 			scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1767 			set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
1768 				ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
1769 				ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
1770 			memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1771 			  (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
1772 			    ? sizeof(scsicmd->sense_buffer)
1773 			    : sizeof(dev->fsa_dev[cid].sense_data));
1774 			scsicmd->scsi_done(scsicmd);
1775 			return 0;
1776 	}
1777 }
1778 
1779 static int query_disk(struct aac_dev *dev, void __user *arg)
1780 {
1781 	struct aac_query_disk qd;
1782 	struct fsa_dev_info *fsa_dev_ptr;
1783 
1784 	fsa_dev_ptr = dev->fsa_dev;
1785 	if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
1786 		return -EFAULT;
1787 	if (qd.cnum == -1)
1788 		qd.cnum = qd.id;
1789 	else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
1790 	{
1791 		if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
1792 			return -EINVAL;
1793 		qd.instance = dev->scsi_host_ptr->host_no;
1794 		qd.bus = 0;
1795 		qd.id = CONTAINER_TO_ID(qd.cnum);
1796 		qd.lun = CONTAINER_TO_LUN(qd.cnum);
1797 	}
1798 	else return -EINVAL;
1799 
1800 	qd.valid = fsa_dev_ptr[qd.cnum].valid;
1801 	qd.locked = fsa_dev_ptr[qd.cnum].locked;
1802 	qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
1803 
1804 	if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
1805 		qd.unmapped = 1;
1806 	else
1807 		qd.unmapped = 0;
1808 
1809 	strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
1810 	  min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
1811 
1812 	if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
1813 		return -EFAULT;
1814 	return 0;
1815 }
1816 
1817 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
1818 {
1819 	struct aac_delete_disk dd;
1820 	struct fsa_dev_info *fsa_dev_ptr;
1821 
1822 	fsa_dev_ptr = dev->fsa_dev;
1823 
1824 	if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1825 		return -EFAULT;
1826 
1827 	if (dd.cnum >= dev->maximum_num_containers)
1828 		return -EINVAL;
1829 	/*
1830 	 *	Mark this container as being deleted.
1831 	 */
1832 	fsa_dev_ptr[dd.cnum].deleted = 1;
1833 	/*
1834 	 *	Mark the container as no longer valid
1835 	 */
1836 	fsa_dev_ptr[dd.cnum].valid = 0;
1837 	return 0;
1838 }
1839 
1840 static int delete_disk(struct aac_dev *dev, void __user *arg)
1841 {
1842 	struct aac_delete_disk dd;
1843 	struct fsa_dev_info *fsa_dev_ptr;
1844 
1845 	fsa_dev_ptr = dev->fsa_dev;
1846 
1847 	if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
1848 		return -EFAULT;
1849 
1850 	if (dd.cnum >= dev->maximum_num_containers)
1851 		return -EINVAL;
1852 	/*
1853 	 *	If the container is locked, it can not be deleted by the API.
1854 	 */
1855 	if (fsa_dev_ptr[dd.cnum].locked)
1856 		return -EBUSY;
1857 	else {
1858 		/*
1859 		 *	Mark the container as no longer being valid.
1860 		 */
1861 		fsa_dev_ptr[dd.cnum].valid = 0;
1862 		fsa_dev_ptr[dd.cnum].devname[0] = '\0';
1863 		return 0;
1864 	}
1865 }
1866 
1867 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
1868 {
1869 	switch (cmd) {
1870 	case FSACTL_QUERY_DISK:
1871 		return query_disk(dev, arg);
1872 	case FSACTL_DELETE_DISK:
1873 		return delete_disk(dev, arg);
1874 	case FSACTL_FORCE_DELETE_DISK:
1875 		return force_delete_disk(dev, arg);
1876 	case FSACTL_GET_CONTAINERS:
1877 		return aac_get_containers(dev);
1878 	default:
1879 		return -ENOTTY;
1880 	}
1881 }
1882 
1883 /**
1884  *
1885  * aac_srb_callback
1886  * @context: the context set in the fib - here it is scsi cmd
1887  * @fibptr: pointer to the fib
1888  *
1889  * Handles the completion of a scsi command to a non dasd device
1890  *
1891  */
1892 
1893 static void aac_srb_callback(void *context, struct fib * fibptr)
1894 {
1895 	struct aac_dev *dev;
1896 	struct aac_srb_reply *srbreply;
1897 	struct scsi_cmnd *scsicmd;
1898 
1899 	scsicmd = (struct scsi_cmnd *) context;
1900 	scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
1901 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1902 
1903 	BUG_ON(fibptr == NULL);
1904 
1905 	srbreply = (struct aac_srb_reply *) fib_data(fibptr);
1906 
1907 	scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
1908 	/*
1909 	 *	Calculate resid for sg
1910 	 */
1911 
1912 	scsicmd->resid = scsicmd->request_bufflen -
1913 		le32_to_cpu(srbreply->data_xfer_length);
1914 
1915 	if(scsicmd->use_sg)
1916 		pci_unmap_sg(dev->pdev,
1917 			(struct scatterlist *)scsicmd->request_buffer,
1918 			scsicmd->use_sg,
1919 			scsicmd->sc_data_direction);
1920 	else if(scsicmd->request_bufflen)
1921 		pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
1922 			scsicmd->sc_data_direction);
1923 
1924 	/*
1925 	 * First check the fib status
1926 	 */
1927 
1928 	if (le32_to_cpu(srbreply->status) != ST_OK){
1929 		int len;
1930 		printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
1931 		len = (le32_to_cpu(srbreply->sense_data_size) >
1932 				sizeof(scsicmd->sense_buffer)) ?
1933 				sizeof(scsicmd->sense_buffer) :
1934 				le32_to_cpu(srbreply->sense_data_size);
1935 		scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
1936 		memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
1937 	}
1938 
1939 	/*
1940 	 * Next check the srb status
1941 	 */
1942 	switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
1943 	case SRB_STATUS_ERROR_RECOVERY:
1944 	case SRB_STATUS_PENDING:
1945 	case SRB_STATUS_SUCCESS:
1946 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1947 		break;
1948 	case SRB_STATUS_DATA_OVERRUN:
1949 		switch(scsicmd->cmnd[0]){
1950 		case  READ_6:
1951 		case  WRITE_6:
1952 		case  READ_10:
1953 		case  WRITE_10:
1954 		case  READ_12:
1955 		case  WRITE_12:
1956 		case  READ_16:
1957 		case  WRITE_16:
1958 			if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
1959 				printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
1960 			} else {
1961 				printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
1962 			}
1963 			scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
1964 			break;
1965 		case INQUIRY: {
1966 			scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1967 			break;
1968 		}
1969 		default:
1970 			scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
1971 			break;
1972 		}
1973 		break;
1974 	case SRB_STATUS_ABORTED:
1975 		scsicmd->result = DID_ABORT << 16 | ABORT << 8;
1976 		break;
1977 	case SRB_STATUS_ABORT_FAILED:
1978 		// Not sure about this one - but assuming the hba was trying to abort for some reason
1979 		scsicmd->result = DID_ERROR << 16 | ABORT << 8;
1980 		break;
1981 	case SRB_STATUS_PARITY_ERROR:
1982 		scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
1983 		break;
1984 	case SRB_STATUS_NO_DEVICE:
1985 	case SRB_STATUS_INVALID_PATH_ID:
1986 	case SRB_STATUS_INVALID_TARGET_ID:
1987 	case SRB_STATUS_INVALID_LUN:
1988 	case SRB_STATUS_SELECTION_TIMEOUT:
1989 		scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1990 		break;
1991 
1992 	case SRB_STATUS_COMMAND_TIMEOUT:
1993 	case SRB_STATUS_TIMEOUT:
1994 		scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
1995 		break;
1996 
1997 	case SRB_STATUS_BUSY:
1998 		scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
1999 		break;
2000 
2001 	case SRB_STATUS_BUS_RESET:
2002 		scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2003 		break;
2004 
2005 	case SRB_STATUS_MESSAGE_REJECTED:
2006 		scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2007 		break;
2008 	case SRB_STATUS_REQUEST_FLUSHED:
2009 	case SRB_STATUS_ERROR:
2010 	case SRB_STATUS_INVALID_REQUEST:
2011 	case SRB_STATUS_REQUEST_SENSE_FAILED:
2012 	case SRB_STATUS_NO_HBA:
2013 	case SRB_STATUS_UNEXPECTED_BUS_FREE:
2014 	case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2015 	case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2016 	case SRB_STATUS_DELAYED_RETRY:
2017 	case SRB_STATUS_BAD_FUNCTION:
2018 	case SRB_STATUS_NOT_STARTED:
2019 	case SRB_STATUS_NOT_IN_USE:
2020 	case SRB_STATUS_FORCE_ABORT:
2021 	case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2022 	default:
2023 #ifdef AAC_DETAILED_STATUS_INFO
2024 		printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2025 			le32_to_cpu(srbreply->srb_status) & 0x3F,
2026 			aac_get_status_string(
2027 				le32_to_cpu(srbreply->srb_status) & 0x3F),
2028 			scsicmd->cmnd[0],
2029 			le32_to_cpu(srbreply->scsi_status));
2030 #endif
2031 		scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2032 		break;
2033 	}
2034 	if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){  // Check Condition
2035 		int len;
2036 		scsicmd->result |= SAM_STAT_CHECK_CONDITION;
2037 		len = (le32_to_cpu(srbreply->sense_data_size) >
2038 				sizeof(scsicmd->sense_buffer)) ?
2039 				sizeof(scsicmd->sense_buffer) :
2040 				le32_to_cpu(srbreply->sense_data_size);
2041 #ifdef AAC_DETAILED_STATUS_INFO
2042 		printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2043 					le32_to_cpu(srbreply->status), len);
2044 #endif
2045 		memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2046 
2047 	}
2048 	/*
2049 	 * OR in the scsi status (already shifted up a bit)
2050 	 */
2051 	scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2052 
2053 	aac_fib_complete(fibptr);
2054 	aac_fib_free(fibptr);
2055 	scsicmd->scsi_done(scsicmd);
2056 }
2057 
2058 /**
2059  *
2060  * aac_send_scb_fib
2061  * @scsicmd: the scsi command block
2062  *
2063  * This routine will form a FIB and fill in the aac_srb from the
2064  * scsicmd passed in.
2065  */
2066 
2067 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
2068 {
2069 	struct fib* cmd_fibcontext;
2070 	struct aac_dev* dev;
2071 	int status;
2072 	struct aac_srb *srbcmd;
2073 	u16 fibsize;
2074 	u32 flag;
2075 	u32 timeout;
2076 
2077 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2078 	if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
2079 			scsicmd->device->lun > 7) {
2080 		scsicmd->result = DID_NO_CONNECT << 16;
2081 		scsicmd->scsi_done(scsicmd);
2082 		return 0;
2083 	}
2084 
2085 	switch(scsicmd->sc_data_direction){
2086 	case DMA_TO_DEVICE:
2087 		flag = SRB_DataOut;
2088 		break;
2089 	case DMA_BIDIRECTIONAL:
2090 		flag = SRB_DataIn | SRB_DataOut;
2091 		break;
2092 	case DMA_FROM_DEVICE:
2093 		flag = SRB_DataIn;
2094 		break;
2095 	case DMA_NONE:
2096 	default:	/* shuts up some versions of gcc */
2097 		flag = SRB_NoDataXfer;
2098 		break;
2099 	}
2100 
2101 
2102 	/*
2103 	 *	Allocate and initialize a Fib then setup a BlockWrite command
2104 	 */
2105 	if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
2106 		return -1;
2107 	}
2108 	aac_fib_init(cmd_fibcontext);
2109 
2110 	srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
2111 	srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
2112 	srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scmd_channel(scsicmd)));
2113 	srbcmd->id   = cpu_to_le32(scmd_id(scsicmd));
2114 	srbcmd->lun      = cpu_to_le32(scsicmd->device->lun);
2115 	srbcmd->flags    = cpu_to_le32(flag);
2116 	timeout = scsicmd->timeout_per_command/HZ;
2117 	if(timeout == 0){
2118 		timeout = 1;
2119 	}
2120 	srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
2121 	srbcmd->retry_limit = 0; /* Obsolete parameter */
2122 	srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
2123 
2124 	if( dev->dac_support == 1 ) {
2125 		aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
2126 		srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
2127 
2128 		memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2129 		memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
2130 		/*
2131 		 *	Build Scatter/Gather list
2132 		 */
2133 		fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
2134 			((le32_to_cpu(srbcmd->sg.count) & 0xff) *
2135 			 sizeof (struct sgentry64));
2136 		BUG_ON (fibsize > (dev->max_fib_size -
2137 					sizeof(struct aac_fibhdr)));
2138 
2139 		/*
2140 		 *	Now send the Fib to the adapter
2141 		 */
2142 		status = aac_fib_send(ScsiPortCommand64, cmd_fibcontext,
2143 				fibsize, FsaNormal, 0, 1,
2144 				  (fib_callback) aac_srb_callback,
2145 				  (void *) scsicmd);
2146 	} else {
2147 		aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
2148 		srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
2149 
2150 		memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
2151 		memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
2152 		/*
2153 		 *	Build Scatter/Gather list
2154 		 */
2155 		fibsize = sizeof (struct aac_srb) +
2156 			(((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
2157 			 sizeof (struct sgentry));
2158 		BUG_ON (fibsize > (dev->max_fib_size -
2159 					sizeof(struct aac_fibhdr)));
2160 
2161 		/*
2162 		 *	Now send the Fib to the adapter
2163 		 */
2164 		status = aac_fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
2165 				  (fib_callback) aac_srb_callback, (void *) scsicmd);
2166 	}
2167 	/*
2168 	 *	Check that the command queued to the controller
2169 	 */
2170 	if (status == -EINPROGRESS) {
2171 		scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2172 		return 0;
2173 	}
2174 
2175 	printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
2176 	aac_fib_complete(cmd_fibcontext);
2177 	aac_fib_free(cmd_fibcontext);
2178 
2179 	return -1;
2180 }
2181 
2182 static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
2183 {
2184 	struct aac_dev *dev;
2185 	unsigned long byte_count = 0;
2186 
2187 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2188 	// Get rid of old data
2189 	psg->count = 0;
2190 	psg->sg[0].addr = 0;
2191 	psg->sg[0].count = 0;
2192 	if (scsicmd->use_sg) {
2193 		struct scatterlist *sg;
2194 		int i;
2195 		int sg_count;
2196 		sg = (struct scatterlist *) scsicmd->request_buffer;
2197 
2198 		sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2199 			scsicmd->sc_data_direction);
2200 		psg->count = cpu_to_le32(sg_count);
2201 
2202 		for (i = 0; i < sg_count; i++) {
2203 			psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
2204 			psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
2205 			byte_count += sg_dma_len(sg);
2206 			sg++;
2207 		}
2208 		/* hba wants the size to be exact */
2209 		if(byte_count > scsicmd->request_bufflen){
2210 			u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2211 				(byte_count - scsicmd->request_bufflen);
2212 			psg->sg[i-1].count = cpu_to_le32(temp);
2213 			byte_count = scsicmd->request_bufflen;
2214 		}
2215 		/* Check for command underflow */
2216 		if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2217 			printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2218 					byte_count, scsicmd->underflow);
2219 		}
2220 	}
2221 	else if(scsicmd->request_bufflen) {
2222 		u32 addr;
2223 		scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2224 				scsicmd->request_buffer,
2225 				scsicmd->request_bufflen,
2226 				scsicmd->sc_data_direction);
2227 		addr = scsicmd->SCp.dma_handle;
2228 		psg->count = cpu_to_le32(1);
2229 		psg->sg[0].addr = cpu_to_le32(addr);
2230 		psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
2231 		byte_count = scsicmd->request_bufflen;
2232 	}
2233 	return byte_count;
2234 }
2235 
2236 
2237 static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
2238 {
2239 	struct aac_dev *dev;
2240 	unsigned long byte_count = 0;
2241 	u64 addr;
2242 
2243 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2244 	// Get rid of old data
2245 	psg->count = 0;
2246 	psg->sg[0].addr[0] = 0;
2247 	psg->sg[0].addr[1] = 0;
2248 	psg->sg[0].count = 0;
2249 	if (scsicmd->use_sg) {
2250 		struct scatterlist *sg;
2251 		int i;
2252 		int sg_count;
2253 		sg = (struct scatterlist *) scsicmd->request_buffer;
2254 
2255 		sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2256 			scsicmd->sc_data_direction);
2257 
2258 		for (i = 0; i < sg_count; i++) {
2259 			int count = sg_dma_len(sg);
2260 			addr = sg_dma_address(sg);
2261 			psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
2262 			psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
2263 			psg->sg[i].count = cpu_to_le32(count);
2264 			byte_count += count;
2265 			sg++;
2266 		}
2267 		psg->count = cpu_to_le32(sg_count);
2268 		/* hba wants the size to be exact */
2269 		if(byte_count > scsicmd->request_bufflen){
2270 			u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2271 				(byte_count - scsicmd->request_bufflen);
2272 			psg->sg[i-1].count = cpu_to_le32(temp);
2273 			byte_count = scsicmd->request_bufflen;
2274 		}
2275 		/* Check for command underflow */
2276 		if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2277 			printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2278 					byte_count, scsicmd->underflow);
2279 		}
2280 	}
2281 	else if(scsicmd->request_bufflen) {
2282 		scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2283 				scsicmd->request_buffer,
2284 				scsicmd->request_bufflen,
2285 				scsicmd->sc_data_direction);
2286 		addr = scsicmd->SCp.dma_handle;
2287 		psg->count = cpu_to_le32(1);
2288 		psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
2289 		psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
2290 		psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
2291 		byte_count = scsicmd->request_bufflen;
2292 	}
2293 	return byte_count;
2294 }
2295 
2296 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
2297 {
2298 	struct Scsi_Host *host = scsicmd->device->host;
2299 	struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2300 	unsigned long byte_count = 0;
2301 
2302 	// Get rid of old data
2303 	psg->count = 0;
2304 	psg->sg[0].next = 0;
2305 	psg->sg[0].prev = 0;
2306 	psg->sg[0].addr[0] = 0;
2307 	psg->sg[0].addr[1] = 0;
2308 	psg->sg[0].count = 0;
2309 	psg->sg[0].flags = 0;
2310 	if (scsicmd->use_sg) {
2311 		struct scatterlist *sg;
2312 		int i;
2313 		int sg_count;
2314 		sg = (struct scatterlist *) scsicmd->request_buffer;
2315 
2316 		sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
2317 			scsicmd->sc_data_direction);
2318 
2319 		for (i = 0; i < sg_count; i++) {
2320 			int count = sg_dma_len(sg);
2321 			u64 addr = sg_dma_address(sg);
2322 			psg->sg[i].next = 0;
2323 			psg->sg[i].prev = 0;
2324 			psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
2325 			psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2326 			psg->sg[i].count = cpu_to_le32(count);
2327 			psg->sg[i].flags = 0;
2328 			byte_count += count;
2329 			sg++;
2330 		}
2331 		psg->count = cpu_to_le32(sg_count);
2332 		/* hba wants the size to be exact */
2333 		if(byte_count > scsicmd->request_bufflen){
2334 			u32 temp = le32_to_cpu(psg->sg[i-1].count) -
2335 				(byte_count - scsicmd->request_bufflen);
2336 			psg->sg[i-1].count = cpu_to_le32(temp);
2337 			byte_count = scsicmd->request_bufflen;
2338 		}
2339 		/* Check for command underflow */
2340 		if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
2341 			printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
2342 					byte_count, scsicmd->underflow);
2343 		}
2344 	}
2345 	else if(scsicmd->request_bufflen) {
2346 		int count;
2347 		u64 addr;
2348 		scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
2349 				scsicmd->request_buffer,
2350 				scsicmd->request_bufflen,
2351 				scsicmd->sc_data_direction);
2352 		addr = scsicmd->SCp.dma_handle;
2353 		count = scsicmd->request_bufflen;
2354 		psg->count = cpu_to_le32(1);
2355 		psg->sg[0].next = 0;
2356 		psg->sg[0].prev = 0;
2357 		psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
2358 		psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
2359 		psg->sg[0].count = cpu_to_le32(count);
2360 		psg->sg[0].flags = 0;
2361 		byte_count = scsicmd->request_bufflen;
2362 	}
2363 	return byte_count;
2364 }
2365 
2366 #ifdef AAC_DETAILED_STATUS_INFO
2367 
2368 struct aac_srb_status_info {
2369 	u32	status;
2370 	char	*str;
2371 };
2372 
2373 
2374 static struct aac_srb_status_info srb_status_info[] = {
2375 	{ SRB_STATUS_PENDING,		"Pending Status"},
2376 	{ SRB_STATUS_SUCCESS,		"Success"},
2377 	{ SRB_STATUS_ABORTED,		"Aborted Command"},
2378 	{ SRB_STATUS_ABORT_FAILED,	"Abort Failed"},
2379 	{ SRB_STATUS_ERROR,		"Error Event"},
2380 	{ SRB_STATUS_BUSY,		"Device Busy"},
2381 	{ SRB_STATUS_INVALID_REQUEST,	"Invalid Request"},
2382 	{ SRB_STATUS_INVALID_PATH_ID,	"Invalid Path ID"},
2383 	{ SRB_STATUS_NO_DEVICE,		"No Device"},
2384 	{ SRB_STATUS_TIMEOUT,		"Timeout"},
2385 	{ SRB_STATUS_SELECTION_TIMEOUT,	"Selection Timeout"},
2386 	{ SRB_STATUS_COMMAND_TIMEOUT,	"Command Timeout"},
2387 	{ SRB_STATUS_MESSAGE_REJECTED,	"Message Rejected"},
2388 	{ SRB_STATUS_BUS_RESET,		"Bus Reset"},
2389 	{ SRB_STATUS_PARITY_ERROR,	"Parity Error"},
2390 	{ SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
2391 	{ SRB_STATUS_NO_HBA,		"No HBA"},
2392 	{ SRB_STATUS_DATA_OVERRUN,	"Data Overrun/Data Underrun"},
2393 	{ SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
2394 	{ SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
2395 	{ SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
2396 	{ SRB_STATUS_REQUEST_FLUSHED,	"Request Flushed"},
2397 	{ SRB_STATUS_DELAYED_RETRY,	"Delayed Retry"},
2398 	{ SRB_STATUS_INVALID_LUN,	"Invalid LUN"},
2399 	{ SRB_STATUS_INVALID_TARGET_ID,	"Invalid TARGET ID"},
2400 	{ SRB_STATUS_BAD_FUNCTION,	"Bad Function"},
2401 	{ SRB_STATUS_ERROR_RECOVERY,	"Error Recovery"},
2402 	{ SRB_STATUS_NOT_STARTED,	"Not Started"},
2403 	{ SRB_STATUS_NOT_IN_USE,	"Not In Use"},
2404     	{ SRB_STATUS_FORCE_ABORT,	"Force Abort"},
2405 	{ SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
2406 	{ 0xff,				"Unknown Error"}
2407 };
2408 
2409 char *aac_get_status_string(u32 status)
2410 {
2411 	int i;
2412 
2413 	for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
2414 		if (srb_status_info[i].status == status)
2415 			return srb_status_info[i].str;
2416 
2417 	return "Bad Status Code";
2418 }
2419 
2420 #endif
2421