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