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