• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *	Adaptec AAC series RAID controller driver
3  *	(c) Copyright 2001 Red Hat Inc.
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000-2010 Adaptec, Inc.
9  *               2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10  *		 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; see the file COPYING.  If not, write to
24  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * Module Name:
27  *  aachba.c
28  *
29  * Abstract: Contains Interfaces to manage IOs.
30  *
31  */
32 
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/spinlock.h>
38 #include <linux/slab.h>
39 #include <linux/completion.h>
40 #include <linux/blkdev.h>
41 #include <linux/uaccess.h>
42 #include <linux/highmem.h> /* For flush_kernel_dcache_page */
43 #include <linux/module.h>
44 
45 #include <scsi/scsi.h>
46 #include <scsi/scsi_cmnd.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_host.h>
49 
50 #include "aacraid.h"
51 
52 /* values for inqd_pdt: Peripheral device type in plain English */
53 #define	INQD_PDT_DA	0x00	/* Direct-access (DISK) device */
54 #define	INQD_PDT_PROC	0x03	/* Processor device */
55 #define	INQD_PDT_CHNGR	0x08	/* Changer (jukebox, scsi2) */
56 #define	INQD_PDT_COMM	0x09	/* Communication device (scsi2) */
57 #define	INQD_PDT_NOLUN2 0x1f	/* Unknown Device (scsi2) */
58 #define	INQD_PDT_NOLUN	0x7f	/* Logical Unit Not Present */
59 
60 #define	INQD_PDT_DMASK	0x1F	/* Peripheral Device Type Mask */
61 #define	INQD_PDT_QMASK	0xE0	/* Peripheral Device Qualifer Mask */
62 
63 /*
64  *	Sense codes
65  */
66 
67 #define SENCODE_NO_SENSE			0x00
68 #define SENCODE_END_OF_DATA			0x00
69 #define SENCODE_BECOMING_READY			0x04
70 #define SENCODE_INIT_CMD_REQUIRED		0x04
71 #define SENCODE_UNRECOVERED_READ_ERROR		0x11
72 #define SENCODE_PARAM_LIST_LENGTH_ERROR		0x1A
73 #define SENCODE_INVALID_COMMAND			0x20
74 #define SENCODE_LBA_OUT_OF_RANGE		0x21
75 #define SENCODE_INVALID_CDB_FIELD		0x24
76 #define SENCODE_LUN_NOT_SUPPORTED		0x25
77 #define SENCODE_INVALID_PARAM_FIELD		0x26
78 #define SENCODE_PARAM_NOT_SUPPORTED		0x26
79 #define SENCODE_PARAM_VALUE_INVALID		0x26
80 #define SENCODE_RESET_OCCURRED			0x29
81 #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET	0x3E
82 #define SENCODE_INQUIRY_DATA_CHANGED		0x3F
83 #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED	0x39
84 #define SENCODE_DIAGNOSTIC_FAILURE		0x40
85 #define SENCODE_INTERNAL_TARGET_FAILURE		0x44
86 #define SENCODE_INVALID_MESSAGE_ERROR		0x49
87 #define SENCODE_LUN_FAILED_SELF_CONFIG		0x4c
88 #define SENCODE_OVERLAPPED_COMMAND		0x4E
89 
90 /*
91  *	Additional sense codes
92  */
93 
94 #define ASENCODE_NO_SENSE			0x00
95 #define ASENCODE_END_OF_DATA			0x05
96 #define ASENCODE_BECOMING_READY			0x01
97 #define ASENCODE_INIT_CMD_REQUIRED		0x02
98 #define ASENCODE_PARAM_LIST_LENGTH_ERROR	0x00
99 #define ASENCODE_INVALID_COMMAND		0x00
100 #define ASENCODE_LBA_OUT_OF_RANGE		0x00
101 #define ASENCODE_INVALID_CDB_FIELD		0x00
102 #define ASENCODE_LUN_NOT_SUPPORTED		0x00
103 #define ASENCODE_INVALID_PARAM_FIELD		0x00
104 #define ASENCODE_PARAM_NOT_SUPPORTED		0x01
105 #define ASENCODE_PARAM_VALUE_INVALID		0x02
106 #define ASENCODE_RESET_OCCURRED			0x00
107 #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET	0x00
108 #define ASENCODE_INQUIRY_DATA_CHANGED		0x03
109 #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED	0x00
110 #define ASENCODE_DIAGNOSTIC_FAILURE		0x80
111 #define ASENCODE_INTERNAL_TARGET_FAILURE	0x00
112 #define ASENCODE_INVALID_MESSAGE_ERROR		0x00
113 #define ASENCODE_LUN_FAILED_SELF_CONFIG		0x00
114 #define ASENCODE_OVERLAPPED_COMMAND		0x00
115 
116 #define AAC_STAT_GOOD (DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD)
117 
118 #define BYTE0(x) (unsigned char)(x)
119 #define BYTE1(x) (unsigned char)((x) >> 8)
120 #define BYTE2(x) (unsigned char)((x) >> 16)
121 #define BYTE3(x) (unsigned char)((x) >> 24)
122 
123 /* MODE_SENSE data format */
124 typedef struct {
125 	struct {
126 		u8	data_length;
127 		u8	med_type;
128 		u8	dev_par;
129 		u8	bd_length;
130 	} __attribute__((packed)) hd;
131 	struct {
132 		u8	dens_code;
133 		u8	block_count[3];
134 		u8	reserved;
135 		u8	block_length[3];
136 	} __attribute__((packed)) bd;
137 		u8	mpc_buf[3];
138 } __attribute__((packed)) aac_modep_data;
139 
140 /* MODE_SENSE_10 data format */
141 typedef struct {
142 	struct {
143 		u8	data_length[2];
144 		u8	med_type;
145 		u8	dev_par;
146 		u8	rsrvd[2];
147 		u8	bd_length[2];
148 	} __attribute__((packed)) hd;
149 	struct {
150 		u8	dens_code;
151 		u8	block_count[3];
152 		u8	reserved;
153 		u8	block_length[3];
154 	} __attribute__((packed)) bd;
155 		u8	mpc_buf[3];
156 } __attribute__((packed)) aac_modep10_data;
157 
158 /*------------------------------------------------------------------------------
159  *              S T R U C T S / T Y P E D E F S
160  *----------------------------------------------------------------------------*/
161 /* SCSI inquiry data */
162 struct inquiry_data {
163 	u8 inqd_pdt;	/* Peripheral qualifier | Peripheral Device Type */
164 	u8 inqd_dtq;	/* RMB | Device Type Qualifier */
165 	u8 inqd_ver;	/* ISO version | ECMA version | ANSI-approved version */
166 	u8 inqd_rdf;	/* AENC | TrmIOP | Response data format */
167 	u8 inqd_len;	/* Additional length (n-4) */
168 	u8 inqd_pad1[2];/* Reserved - must be zero */
169 	u8 inqd_pad2;	/* RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
170 	u8 inqd_vid[8];	/* Vendor ID */
171 	u8 inqd_pid[16];/* Product ID */
172 	u8 inqd_prl[4];	/* Product Revision Level */
173 };
174 
175 /* Added for VPD 0x83 */
176 struct  tvpd_id_descriptor_type_1 {
177 	u8 codeset:4;		/* VPD_CODE_SET */
178 	u8 reserved:4;
179 	u8 identifiertype:4;	/* VPD_IDENTIFIER_TYPE */
180 	u8 reserved2:4;
181 	u8 reserved3;
182 	u8 identifierlength;
183 	u8 venid[8];
184 	u8 productid[16];
185 	u8 serialnumber[8];	/* SN in ASCII */
186 
187 };
188 
189 struct tvpd_id_descriptor_type_2 {
190 	u8 codeset:4;		/* VPD_CODE_SET */
191 	u8 reserved:4;
192 	u8 identifiertype:4;	/* VPD_IDENTIFIER_TYPE */
193 	u8 reserved2:4;
194 	u8 reserved3;
195 	u8 identifierlength;
196 	struct teu64id {
197 		u32 Serial;
198 		 /* The serial number supposed to be 40 bits,
199 		  * bit we only support 32, so make the last byte zero. */
200 		u8 reserved;
201 		u8 venid[3];
202 	} eu64id;
203 
204 };
205 
206 struct tvpd_id_descriptor_type_3 {
207 	u8 codeset : 4;          /* VPD_CODE_SET */
208 	u8 reserved : 4;
209 	u8 identifiertype : 4;   /* VPD_IDENTIFIER_TYPE */
210 	u8 reserved2 : 4;
211 	u8 reserved3;
212 	u8 identifierlength;
213 	u8 Identifier[16];
214 };
215 
216 struct tvpd_page83 {
217 	u8 DeviceType:5;
218 	u8 DeviceTypeQualifier:3;
219 	u8 PageCode;
220 	u8 reserved;
221 	u8 PageLength;
222 	struct tvpd_id_descriptor_type_1 type1;
223 	struct tvpd_id_descriptor_type_2 type2;
224 	struct tvpd_id_descriptor_type_3 type3;
225 };
226 
227 /*
228  *              M O D U L E   G L O B A L S
229  */
230 
231 static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *sgmap);
232 static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg);
233 static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg);
234 static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
235 				struct aac_raw_io2 *rio2, int sg_max);
236 static long aac_build_sghba(struct scsi_cmnd *scsicmd,
237 				struct aac_hba_cmd_req *hbacmd,
238 				int sg_max, u64 sg_address);
239 static int aac_convert_sgraw2(struct aac_raw_io2 *rio2,
240 				int pages, int nseg, int nseg_new);
241 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
242 static int aac_send_hba_fib(struct scsi_cmnd *scsicmd);
243 #ifdef AAC_DETAILED_STATUS_INFO
244 static char *aac_get_status_string(u32 status);
245 #endif
246 
247 /*
248  *	Non dasd selection is handled entirely in aachba now
249  */
250 
251 static int nondasd = -1;
252 static int aac_cache = 2;	/* WCE=0 to avoid performance problems */
253 static int dacmode = -1;
254 int aac_msi;
255 int aac_commit = -1;
256 int startup_timeout = 180;
257 int aif_timeout = 120;
258 int aac_sync_mode;  /* Only Sync. transfer - disabled */
259 int aac_convert_sgl = 1;	/* convert non-conformable s/g list - enabled */
260 
261 module_param(aac_sync_mode, int, S_IRUGO|S_IWUSR);
262 MODULE_PARM_DESC(aac_sync_mode, "Force sync. transfer mode"
263 	" 0=off, 1=on");
264 module_param(aac_convert_sgl, int, S_IRUGO|S_IWUSR);
265 MODULE_PARM_DESC(aac_convert_sgl, "Convert non-conformable s/g list"
266 	" 0=off, 1=on");
267 module_param(nondasd, int, S_IRUGO|S_IWUSR);
268 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
269 	" 0=off, 1=on");
270 module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
271 MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
272 	"\tbit 0 - Disable FUA in WRITE SCSI commands\n"
273 	"\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
274 	"\tbit 2 - Disable only if Battery is protecting Cache");
275 module_param(dacmode, int, S_IRUGO|S_IWUSR);
276 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
277 	" 0=off, 1=on");
278 module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
279 MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
280 	" adapter for foreign arrays.\n"
281 	"This is typically needed in systems that do not have a BIOS."
282 	" 0=off, 1=on");
283 module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
284 MODULE_PARM_DESC(msi, "IRQ handling."
285 	" 0=PIC(default), 1=MSI, 2=MSI-X)");
286 module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
287 MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
288 	" adapter to have it's kernel up and\n"
289 	"running. This is typically adjusted for large systems that do not"
290 	" have a BIOS.");
291 module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
292 MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
293 	" applications to pick up AIFs before\n"
294 	"deregistering them. This is typically adjusted for heavily burdened"
295 	" systems.");
296 
297 int aac_fib_dump;
298 module_param(aac_fib_dump, int, 0644);
299 MODULE_PARM_DESC(aac_fib_dump, "Dump controller fibs prior to IOP_RESET 0=off, 1=on");
300 
301 int numacb = -1;
302 module_param(numacb, int, S_IRUGO|S_IWUSR);
303 MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
304 	" blocks (FIB) allocated. Valid values are 512 and down. Default is"
305 	" to use suggestion from Firmware.");
306 
307 int acbsize = -1;
308 module_param(acbsize, int, S_IRUGO|S_IWUSR);
309 MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
310 	" size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
311 	" suggestion from Firmware.");
312 
313 int update_interval = 30 * 60;
314 module_param(update_interval, int, S_IRUGO|S_IWUSR);
315 MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
316 	" updates issued to adapter.");
317 
318 int check_interval = 60;
319 module_param(check_interval, int, S_IRUGO|S_IWUSR);
320 MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
321 	" checks.");
322 
323 int aac_check_reset = 1;
324 module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
325 MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
326 	" adapter. a value of -1 forces the reset to adapters programmed to"
327 	" ignore it.");
328 
329 int expose_physicals = -1;
330 module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
331 MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
332 	" -1=protect 0=off, 1=on");
333 
334 int aac_reset_devices;
335 module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
336 MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
337 
338 int aac_wwn = 1;
339 module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
340 MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
341 	"\t0 - Disable\n"
342 	"\t1 - Array Meta Data Signature (default)\n"
343 	"\t2 - Adapter Serial Number");
344 
345 
aac_valid_context(struct scsi_cmnd * scsicmd,struct fib * fibptr)346 static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
347 		struct fib *fibptr) {
348 	struct scsi_device *device;
349 
350 	if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
351 		dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
352 		aac_fib_complete(fibptr);
353 		return 0;
354 	}
355 	scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
356 	device = scsicmd->device;
357 	if (unlikely(!device)) {
358 		dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
359 		aac_fib_complete(fibptr);
360 		return 0;
361 	}
362 	return 1;
363 }
364 
365 /**
366  *	aac_get_config_status	-	check the adapter configuration
367  *	@common: adapter to query
368  *
369  *	Query config status, and commit the configuration if needed.
370  */
aac_get_config_status(struct aac_dev * dev,int commit_flag)371 int aac_get_config_status(struct aac_dev *dev, int commit_flag)
372 {
373 	int status = 0;
374 	struct fib * fibptr;
375 
376 	if (!(fibptr = aac_fib_alloc(dev)))
377 		return -ENOMEM;
378 
379 	aac_fib_init(fibptr);
380 	{
381 		struct aac_get_config_status *dinfo;
382 		dinfo = (struct aac_get_config_status *) fib_data(fibptr);
383 
384 		dinfo->command = cpu_to_le32(VM_ContainerConfig);
385 		dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
386 		dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
387 	}
388 
389 	status = aac_fib_send(ContainerCommand,
390 			    fibptr,
391 			    sizeof (struct aac_get_config_status),
392 			    FsaNormal,
393 			    1, 1,
394 			    NULL, NULL);
395 	if (status < 0) {
396 		printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
397 	} else {
398 		struct aac_get_config_status_resp *reply
399 		  = (struct aac_get_config_status_resp *) fib_data(fibptr);
400 		dprintk((KERN_WARNING
401 		  "aac_get_config_status: response=%d status=%d action=%d\n",
402 		  le32_to_cpu(reply->response),
403 		  le32_to_cpu(reply->status),
404 		  le32_to_cpu(reply->data.action)));
405 		if ((le32_to_cpu(reply->response) != ST_OK) ||
406 		     (le32_to_cpu(reply->status) != CT_OK) ||
407 		     (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
408 			printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
409 			status = -EINVAL;
410 		}
411 	}
412 	/* Do not set XferState to zero unless receives a response from F/W */
413 	if (status >= 0)
414 		aac_fib_complete(fibptr);
415 
416 	/* Send a CT_COMMIT_CONFIG to enable discovery of devices */
417 	if (status >= 0) {
418 		if ((aac_commit == 1) || commit_flag) {
419 			struct aac_commit_config * dinfo;
420 			aac_fib_init(fibptr);
421 			dinfo = (struct aac_commit_config *) fib_data(fibptr);
422 
423 			dinfo->command = cpu_to_le32(VM_ContainerConfig);
424 			dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
425 
426 			status = aac_fib_send(ContainerCommand,
427 				    fibptr,
428 				    sizeof (struct aac_commit_config),
429 				    FsaNormal,
430 				    1, 1,
431 				    NULL, NULL);
432 			/* Do not set XferState to zero unless
433 			 * receives a response from F/W */
434 			if (status >= 0)
435 				aac_fib_complete(fibptr);
436 		} else if (aac_commit == 0) {
437 			printk(KERN_WARNING
438 			  "aac_get_config_status: Foreign device configurations are being ignored\n");
439 		}
440 	}
441 	/* FIB should be freed only after getting the response from the F/W */
442 	if (status != -ERESTARTSYS)
443 		aac_fib_free(fibptr);
444 	return status;
445 }
446 
aac_expose_phy_device(struct scsi_cmnd * scsicmd)447 static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
448 {
449 	char inq_data;
450 	scsi_sg_copy_to_buffer(scsicmd,  &inq_data, sizeof(inq_data));
451 	if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
452 		inq_data &= 0xdf;
453 		scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
454 	}
455 }
456 
457 /**
458  *	aac_get_containers	-	list containers
459  *	@common: adapter to probe
460  *
461  *	Make a list of all containers on this controller
462  */
aac_get_containers(struct aac_dev * dev)463 int aac_get_containers(struct aac_dev *dev)
464 {
465 	struct fsa_dev_info *fsa_dev_ptr;
466 	u32 index;
467 	int status = 0;
468 	struct fib * fibptr;
469 	struct aac_get_container_count *dinfo;
470 	struct aac_get_container_count_resp *dresp;
471 	int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
472 
473 	if (!(fibptr = aac_fib_alloc(dev)))
474 		return -ENOMEM;
475 
476 	aac_fib_init(fibptr);
477 	dinfo = (struct aac_get_container_count *) fib_data(fibptr);
478 	dinfo->command = cpu_to_le32(VM_ContainerConfig);
479 	dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
480 
481 	status = aac_fib_send(ContainerCommand,
482 		    fibptr,
483 		    sizeof (struct aac_get_container_count),
484 		    FsaNormal,
485 		    1, 1,
486 		    NULL, NULL);
487 	if (status >= 0) {
488 		dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
489 		maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
490 		if (fibptr->dev->supplement_adapter_info.supported_options2 &
491 		    AAC_OPTION_SUPPORTED_240_VOLUMES) {
492 			maximum_num_containers =
493 				le32_to_cpu(dresp->MaxSimpleVolumes);
494 		}
495 		aac_fib_complete(fibptr);
496 	}
497 	/* FIB should be freed only after getting the response from the F/W */
498 	if (status != -ERESTARTSYS)
499 		aac_fib_free(fibptr);
500 
501 	if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
502 		maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
503 	if (dev->fsa_dev == NULL ||
504 		dev->maximum_num_containers != maximum_num_containers) {
505 
506 		fsa_dev_ptr = dev->fsa_dev;
507 
508 		dev->fsa_dev = kcalloc(maximum_num_containers,
509 					sizeof(*fsa_dev_ptr), GFP_KERNEL);
510 
511 		kfree(fsa_dev_ptr);
512 		fsa_dev_ptr = NULL;
513 
514 
515 		if (!dev->fsa_dev)
516 			return -ENOMEM;
517 
518 		dev->maximum_num_containers = maximum_num_containers;
519 	}
520 	for (index = 0; index < dev->maximum_num_containers; index++) {
521 		dev->fsa_dev[index].devname[0] = '\0';
522 		dev->fsa_dev[index].valid = 0;
523 
524 		status = aac_probe_container(dev, index);
525 
526 		if (status < 0) {
527 			printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
528 			break;
529 		}
530 	}
531 	return status;
532 }
533 
get_container_name_callback(void * context,struct fib * fibptr)534 static void get_container_name_callback(void *context, struct fib * fibptr)
535 {
536 	struct aac_get_name_resp * get_name_reply;
537 	struct scsi_cmnd * scsicmd;
538 
539 	scsicmd = (struct scsi_cmnd *) context;
540 
541 	if (!aac_valid_context(scsicmd, fibptr))
542 		return;
543 
544 	dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
545 	BUG_ON(fibptr == NULL);
546 
547 	get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
548 	/* Failure is irrelevant, using default value instead */
549 	if ((le32_to_cpu(get_name_reply->status) == CT_OK)
550 	 && (get_name_reply->data[0] != '\0')) {
551 		char *sp = get_name_reply->data;
552 		int data_size = FIELD_SIZEOF(struct aac_get_name_resp, data);
553 
554 		sp[data_size - 1] = '\0';
555 		while (*sp == ' ')
556 			++sp;
557 		if (*sp) {
558 			struct inquiry_data inq;
559 			char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
560 			int count = sizeof(d);
561 			char *dp = d;
562 			do {
563 				*dp++ = (*sp) ? *sp++ : ' ';
564 			} while (--count > 0);
565 
566 			scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
567 			memcpy(inq.inqd_pid, d, sizeof(d));
568 			scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
569 		}
570 	}
571 
572 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
573 
574 	aac_fib_complete(fibptr);
575 	scsicmd->scsi_done(scsicmd);
576 }
577 
578 /**
579  *	aac_get_container_name	-	get container name, none blocking.
580  */
aac_get_container_name(struct scsi_cmnd * scsicmd)581 static int aac_get_container_name(struct scsi_cmnd * scsicmd)
582 {
583 	int status;
584 	int data_size;
585 	struct aac_get_name *dinfo;
586 	struct fib * cmd_fibcontext;
587 	struct aac_dev * dev;
588 
589 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
590 
591 	data_size = FIELD_SIZEOF(struct aac_get_name_resp, data);
592 
593 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
594 
595 	aac_fib_init(cmd_fibcontext);
596 	dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
597 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
598 
599 	dinfo->command = cpu_to_le32(VM_ContainerConfig);
600 	dinfo->type = cpu_to_le32(CT_READ_NAME);
601 	dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
602 	dinfo->count = cpu_to_le32(data_size - 1);
603 
604 	status = aac_fib_send(ContainerCommand,
605 		  cmd_fibcontext,
606 		  sizeof(struct aac_get_name_resp),
607 		  FsaNormal,
608 		  0, 1,
609 		  (fib_callback)get_container_name_callback,
610 		  (void *) scsicmd);
611 
612 	/*
613 	 *	Check that the command queued to the controller
614 	 */
615 	if (status == -EINPROGRESS)
616 		return 0;
617 
618 	printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
619 	aac_fib_complete(cmd_fibcontext);
620 	return -1;
621 }
622 
aac_probe_container_callback2(struct scsi_cmnd * scsicmd)623 static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
624 {
625 	struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
626 
627 	if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
628 		return aac_scsi_cmd(scsicmd);
629 
630 	scsicmd->result = DID_NO_CONNECT << 16;
631 	scsicmd->scsi_done(scsicmd);
632 	return 0;
633 }
634 
_aac_probe_container2(void * context,struct fib * fibptr)635 static void _aac_probe_container2(void * context, struct fib * fibptr)
636 {
637 	struct fsa_dev_info *fsa_dev_ptr;
638 	int (*callback)(struct scsi_cmnd *);
639 	struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
640 	int i;
641 
642 
643 	if (!aac_valid_context(scsicmd, fibptr))
644 		return;
645 
646 	scsicmd->SCp.Status = 0;
647 	fsa_dev_ptr = fibptr->dev->fsa_dev;
648 	if (fsa_dev_ptr) {
649 		struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
650 		__le32 sup_options2;
651 
652 		fsa_dev_ptr += scmd_id(scsicmd);
653 		sup_options2 =
654 			fibptr->dev->supplement_adapter_info.supported_options2;
655 
656 		if ((le32_to_cpu(dresp->status) == ST_OK) &&
657 		    (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
658 		    (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
659 			if (!(sup_options2 & AAC_OPTION_VARIABLE_BLOCK_SIZE)) {
660 				dresp->mnt[0].fileinfo.bdevinfo.block_size = 0x200;
661 				fsa_dev_ptr->block_size = 0x200;
662 			} else {
663 				fsa_dev_ptr->block_size =
664 					le32_to_cpu(dresp->mnt[0].fileinfo.bdevinfo.block_size);
665 			}
666 			for (i = 0; i < 16; i++)
667 				fsa_dev_ptr->identifier[i] =
668 					dresp->mnt[0].fileinfo.bdevinfo
669 								.identifier[i];
670 			fsa_dev_ptr->valid = 1;
671 			/* sense_key holds the current state of the spin-up */
672 			if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
673 				fsa_dev_ptr->sense_data.sense_key = NOT_READY;
674 			else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
675 				fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
676 			fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
677 			fsa_dev_ptr->size
678 			  = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
679 			    (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
680 			fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
681 		}
682 		if ((fsa_dev_ptr->valid & 1) == 0)
683 			fsa_dev_ptr->valid = 0;
684 		scsicmd->SCp.Status = le32_to_cpu(dresp->count);
685 	}
686 	aac_fib_complete(fibptr);
687 	aac_fib_free(fibptr);
688 	callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
689 	scsicmd->SCp.ptr = NULL;
690 	(*callback)(scsicmd);
691 	return;
692 }
693 
_aac_probe_container1(void * context,struct fib * fibptr)694 static void _aac_probe_container1(void * context, struct fib * fibptr)
695 {
696 	struct scsi_cmnd * scsicmd;
697 	struct aac_mount * dresp;
698 	struct aac_query_mount *dinfo;
699 	int status;
700 
701 	dresp = (struct aac_mount *) fib_data(fibptr);
702 	if (!aac_supports_2T(fibptr->dev)) {
703 		dresp->mnt[0].capacityhigh = 0;
704 		if ((le32_to_cpu(dresp->status) == ST_OK) &&
705 			(le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
706 			_aac_probe_container2(context, fibptr);
707 			return;
708 		}
709 	}
710 	scsicmd = (struct scsi_cmnd *) context;
711 
712 	if (!aac_valid_context(scsicmd, fibptr))
713 		return;
714 
715 	aac_fib_init(fibptr);
716 
717 	dinfo = (struct aac_query_mount *)fib_data(fibptr);
718 
719 	if (fibptr->dev->supplement_adapter_info.supported_options2 &
720 	    AAC_OPTION_VARIABLE_BLOCK_SIZE)
721 		dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
722 	else
723 		dinfo->command = cpu_to_le32(VM_NameServe64);
724 
725 	dinfo->count = cpu_to_le32(scmd_id(scsicmd));
726 	dinfo->type = cpu_to_le32(FT_FILESYS);
727 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
728 
729 	status = aac_fib_send(ContainerCommand,
730 			  fibptr,
731 			  sizeof(struct aac_query_mount),
732 			  FsaNormal,
733 			  0, 1,
734 			  _aac_probe_container2,
735 			  (void *) scsicmd);
736 	/*
737 	 *	Check that the command queued to the controller
738 	 */
739 	if (status < 0 && status != -EINPROGRESS) {
740 		/* Inherit results from VM_NameServe, if any */
741 		dresp->status = cpu_to_le32(ST_OK);
742 		_aac_probe_container2(context, fibptr);
743 	}
744 }
745 
_aac_probe_container(struct scsi_cmnd * scsicmd,int (* callback)(struct scsi_cmnd *))746 static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
747 {
748 	struct fib * fibptr;
749 	int status = -ENOMEM;
750 
751 	if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
752 		struct aac_query_mount *dinfo;
753 
754 		aac_fib_init(fibptr);
755 
756 		dinfo = (struct aac_query_mount *)fib_data(fibptr);
757 
758 		if (fibptr->dev->supplement_adapter_info.supported_options2 &
759 		    AAC_OPTION_VARIABLE_BLOCK_SIZE)
760 			dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
761 		else
762 			dinfo->command = cpu_to_le32(VM_NameServe);
763 
764 		dinfo->count = cpu_to_le32(scmd_id(scsicmd));
765 		dinfo->type = cpu_to_le32(FT_FILESYS);
766 		scsicmd->SCp.ptr = (char *)callback;
767 		scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
768 
769 		status = aac_fib_send(ContainerCommand,
770 			  fibptr,
771 			  sizeof(struct aac_query_mount),
772 			  FsaNormal,
773 			  0, 1,
774 			  _aac_probe_container1,
775 			  (void *) scsicmd);
776 		/*
777 		 *	Check that the command queued to the controller
778 		 */
779 		if (status == -EINPROGRESS)
780 			return 0;
781 
782 		if (status < 0) {
783 			scsicmd->SCp.ptr = NULL;
784 			aac_fib_complete(fibptr);
785 			aac_fib_free(fibptr);
786 		}
787 	}
788 	if (status < 0) {
789 		struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
790 		if (fsa_dev_ptr) {
791 			fsa_dev_ptr += scmd_id(scsicmd);
792 			if ((fsa_dev_ptr->valid & 1) == 0) {
793 				fsa_dev_ptr->valid = 0;
794 				return (*callback)(scsicmd);
795 			}
796 		}
797 	}
798 	return status;
799 }
800 
801 /**
802  *	aac_probe_container		-	query a logical volume
803  *	@dev: device to query
804  *	@cid: container identifier
805  *
806  *	Queries the controller about the given volume. The volume information
807  *	is updated in the struct fsa_dev_info structure rather than returned.
808  */
aac_probe_container_callback1(struct scsi_cmnd * scsicmd)809 static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
810 {
811 	scsicmd->device = NULL;
812 	return 0;
813 }
814 
aac_probe_container(struct aac_dev * dev,int cid)815 int aac_probe_container(struct aac_dev *dev, int cid)
816 {
817 	struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
818 	struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
819 	int status;
820 
821 	if (!scsicmd || !scsidev) {
822 		kfree(scsicmd);
823 		kfree(scsidev);
824 		return -ENOMEM;
825 	}
826 	scsicmd->list.next = NULL;
827 	scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
828 
829 	scsicmd->device = scsidev;
830 	scsidev->sdev_state = 0;
831 	scsidev->id = cid;
832 	scsidev->host = dev->scsi_host_ptr;
833 
834 	if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
835 		while (scsicmd->device == scsidev)
836 			schedule();
837 	kfree(scsidev);
838 	status = scsicmd->SCp.Status;
839 	kfree(scsicmd);
840 	return status;
841 }
842 
843 /* Local Structure to set SCSI inquiry data strings */
844 struct scsi_inq {
845 	char vid[8];         /* Vendor ID */
846 	char pid[16];        /* Product ID */
847 	char prl[4];         /* Product Revision Level */
848 };
849 
850 /**
851  *	InqStrCopy	-	string merge
852  *	@a:	string to copy from
853  *	@b:	string to copy to
854  *
855  *	Copy a String from one location to another
856  *	without copying \0
857  */
858 
inqstrcpy(char * a,char * b)859 static void inqstrcpy(char *a, char *b)
860 {
861 
862 	while (*a != (char)0)
863 		*b++ = *a++;
864 }
865 
866 static char *container_types[] = {
867 	"None",
868 	"Volume",
869 	"Mirror",
870 	"Stripe",
871 	"RAID5",
872 	"SSRW",
873 	"SSRO",
874 	"Morph",
875 	"Legacy",
876 	"RAID4",
877 	"RAID10",
878 	"RAID00",
879 	"V-MIRRORS",
880 	"PSEUDO R4",
881 	"RAID50",
882 	"RAID5D",
883 	"RAID5D0",
884 	"RAID1E",
885 	"RAID6",
886 	"RAID60",
887 	"Unknown"
888 };
889 
get_container_type(unsigned tindex)890 char * get_container_type(unsigned tindex)
891 {
892 	if (tindex >= ARRAY_SIZE(container_types))
893 		tindex = ARRAY_SIZE(container_types) - 1;
894 	return container_types[tindex];
895 }
896 
897 /* Function: setinqstr
898  *
899  * Arguments: [1] pointer to void [1] int
900  *
901  * Purpose: Sets SCSI inquiry data strings for vendor, product
902  * and revision level. Allows strings to be set in platform dependent
903  * files instead of in OS dependent driver source.
904  */
905 
setinqstr(struct aac_dev * dev,void * data,int tindex)906 static void setinqstr(struct aac_dev *dev, void *data, int tindex)
907 {
908 	struct scsi_inq *str;
909 	struct aac_supplement_adapter_info *sup_adap_info;
910 
911 	sup_adap_info = &dev->supplement_adapter_info;
912 	str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
913 	memset(str, ' ', sizeof(*str));
914 
915 	if (sup_adap_info->adapter_type_text[0]) {
916 		int c;
917 		char *cp;
918 		char *cname = kmemdup(sup_adap_info->adapter_type_text,
919 				sizeof(sup_adap_info->adapter_type_text),
920 								GFP_ATOMIC);
921 		if (!cname)
922 			return;
923 
924 		cp = cname;
925 		if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
926 			inqstrcpy("SMC", str->vid);
927 		else {
928 			c = sizeof(str->vid);
929 			while (*cp && *cp != ' ' && --c)
930 				++cp;
931 			c = *cp;
932 			*cp = '\0';
933 			inqstrcpy(cname, str->vid);
934 			*cp = c;
935 			while (*cp && *cp != ' ')
936 				++cp;
937 		}
938 		while (*cp == ' ')
939 			++cp;
940 		/* last six chars reserved for vol type */
941 		c = 0;
942 		if (strlen(cp) > sizeof(str->pid)) {
943 			c = cp[sizeof(str->pid)];
944 			cp[sizeof(str->pid)] = '\0';
945 		}
946 		inqstrcpy (cp, str->pid);
947 
948 		kfree(cname);
949 	} else {
950 		struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
951 
952 		inqstrcpy (mp->vname, str->vid);
953 		/* last six chars reserved for vol type */
954 		inqstrcpy (mp->model, str->pid);
955 	}
956 
957 	if (tindex < ARRAY_SIZE(container_types)){
958 		char *findit = str->pid;
959 
960 		for ( ; *findit != ' '; findit++); /* walk till we find a space */
961 		/* RAID is superfluous in the context of a RAID device */
962 		if (memcmp(findit-4, "RAID", 4) == 0)
963 			*(findit -= 4) = ' ';
964 		if (((findit - str->pid) + strlen(container_types[tindex]))
965 		 < (sizeof(str->pid) + sizeof(str->prl)))
966 			inqstrcpy (container_types[tindex], findit + 1);
967 	}
968 	inqstrcpy ("V1.0", str->prl);
969 }
970 
build_vpd83_type3(struct tvpd_page83 * vpdpage83data,struct aac_dev * dev,struct scsi_cmnd * scsicmd)971 static void build_vpd83_type3(struct tvpd_page83 *vpdpage83data,
972 		struct aac_dev *dev, struct scsi_cmnd *scsicmd)
973 {
974 	int container;
975 
976 	vpdpage83data->type3.codeset = 1;
977 	vpdpage83data->type3.identifiertype = 3;
978 	vpdpage83data->type3.identifierlength = sizeof(vpdpage83data->type3)
979 			- 4;
980 
981 	for (container = 0; container < dev->maximum_num_containers;
982 			container++) {
983 
984 		if (scmd_id(scsicmd) == container) {
985 			memcpy(vpdpage83data->type3.Identifier,
986 					dev->fsa_dev[container].identifier,
987 					16);
988 			break;
989 		}
990 	}
991 }
992 
get_container_serial_callback(void * context,struct fib * fibptr)993 static void get_container_serial_callback(void *context, struct fib * fibptr)
994 {
995 	struct aac_get_serial_resp * get_serial_reply;
996 	struct scsi_cmnd * scsicmd;
997 
998 	BUG_ON(fibptr == NULL);
999 
1000 	scsicmd = (struct scsi_cmnd *) context;
1001 	if (!aac_valid_context(scsicmd, fibptr))
1002 		return;
1003 
1004 	get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
1005 	/* Failure is irrelevant, using default value instead */
1006 	if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
1007 		/*Check to see if it's for VPD 0x83 or 0x80 */
1008 		if (scsicmd->cmnd[2] == 0x83) {
1009 			/* vpd page 0x83 - Device Identification Page */
1010 			struct aac_dev *dev;
1011 			int i;
1012 			struct tvpd_page83 vpdpage83data;
1013 
1014 			dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1015 
1016 			memset(((u8 *)&vpdpage83data), 0,
1017 			       sizeof(vpdpage83data));
1018 
1019 			/* DIRECT_ACCESS_DEVIC */
1020 			vpdpage83data.DeviceType = 0;
1021 			/* DEVICE_CONNECTED */
1022 			vpdpage83data.DeviceTypeQualifier = 0;
1023 			/* VPD_DEVICE_IDENTIFIERS */
1024 			vpdpage83data.PageCode = 0x83;
1025 			vpdpage83data.reserved = 0;
1026 			vpdpage83data.PageLength =
1027 				sizeof(vpdpage83data.type1) +
1028 				sizeof(vpdpage83data.type2);
1029 
1030 			/* VPD 83 Type 3 is not supported for ARC */
1031 			if (dev->sa_firmware)
1032 				vpdpage83data.PageLength +=
1033 				sizeof(vpdpage83data.type3);
1034 
1035 			/* T10 Vendor Identifier Field Format */
1036 			/* VpdcodesetAscii */
1037 			vpdpage83data.type1.codeset = 2;
1038 			/* VpdIdentifierTypeVendorId */
1039 			vpdpage83data.type1.identifiertype = 1;
1040 			vpdpage83data.type1.identifierlength =
1041 				sizeof(vpdpage83data.type1) - 4;
1042 
1043 			/* "ADAPTEC " for adaptec */
1044 			memcpy(vpdpage83data.type1.venid,
1045 				"ADAPTEC ",
1046 				sizeof(vpdpage83data.type1.venid));
1047 			memcpy(vpdpage83data.type1.productid,
1048 				"ARRAY           ",
1049 				sizeof(
1050 				vpdpage83data.type1.productid));
1051 
1052 			/* Convert to ascii based serial number.
1053 			 * The LSB is the the end.
1054 			 */
1055 			for (i = 0; i < 8; i++) {
1056 				u8 temp =
1057 					(u8)((get_serial_reply->uid >> ((7 - i) * 4)) & 0xF);
1058 				if (temp  > 0x9) {
1059 					vpdpage83data.type1.serialnumber[i] =
1060 							'A' + (temp - 0xA);
1061 				} else {
1062 					vpdpage83data.type1.serialnumber[i] =
1063 							'0' + temp;
1064 				}
1065 			}
1066 
1067 			/* VpdCodeSetBinary */
1068 			vpdpage83data.type2.codeset = 1;
1069 			/* VpdidentifiertypeEUI64 */
1070 			vpdpage83data.type2.identifiertype = 2;
1071 			vpdpage83data.type2.identifierlength =
1072 				sizeof(vpdpage83data.type2) - 4;
1073 
1074 			vpdpage83data.type2.eu64id.venid[0] = 0xD0;
1075 			vpdpage83data.type2.eu64id.venid[1] = 0;
1076 			vpdpage83data.type2.eu64id.venid[2] = 0;
1077 
1078 			vpdpage83data.type2.eu64id.Serial =
1079 							get_serial_reply->uid;
1080 			vpdpage83data.type2.eu64id.reserved = 0;
1081 
1082 			/*
1083 			 * VpdIdentifierTypeFCPHName
1084 			 * VPD 0x83 Type 3 not supported for ARC
1085 			 */
1086 			if (dev->sa_firmware) {
1087 				build_vpd83_type3(&vpdpage83data,
1088 						dev, scsicmd);
1089 			}
1090 
1091 			/* Move the inquiry data to the response buffer. */
1092 			scsi_sg_copy_from_buffer(scsicmd, &vpdpage83data,
1093 						 sizeof(vpdpage83data));
1094 		} else {
1095 			/* It must be for VPD 0x80 */
1096 			char sp[13];
1097 			/* EVPD bit set */
1098 			sp[0] = INQD_PDT_DA;
1099 			sp[1] = scsicmd->cmnd[2];
1100 			sp[2] = 0;
1101 			sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
1102 				le32_to_cpu(get_serial_reply->uid));
1103 			scsi_sg_copy_from_buffer(scsicmd, sp,
1104 						 sizeof(sp));
1105 		}
1106 	}
1107 
1108 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1109 
1110 	aac_fib_complete(fibptr);
1111 	scsicmd->scsi_done(scsicmd);
1112 }
1113 
1114 /**
1115  *	aac_get_container_serial - get container serial, none blocking.
1116  */
aac_get_container_serial(struct scsi_cmnd * scsicmd)1117 static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
1118 {
1119 	int status;
1120 	struct aac_get_serial *dinfo;
1121 	struct fib * cmd_fibcontext;
1122 	struct aac_dev * dev;
1123 
1124 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1125 
1126 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
1127 
1128 	aac_fib_init(cmd_fibcontext);
1129 	dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
1130 
1131 	dinfo->command = cpu_to_le32(VM_ContainerConfig);
1132 	dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
1133 	dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
1134 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1135 
1136 	status = aac_fib_send(ContainerCommand,
1137 		  cmd_fibcontext,
1138 		  sizeof(struct aac_get_serial_resp),
1139 		  FsaNormal,
1140 		  0, 1,
1141 		  (fib_callback) get_container_serial_callback,
1142 		  (void *) scsicmd);
1143 
1144 	/*
1145 	 *	Check that the command queued to the controller
1146 	 */
1147 	if (status == -EINPROGRESS)
1148 		return 0;
1149 
1150 	printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
1151 	aac_fib_complete(cmd_fibcontext);
1152 	return -1;
1153 }
1154 
1155 /* Function: setinqserial
1156  *
1157  * Arguments: [1] pointer to void [1] int
1158  *
1159  * Purpose: Sets SCSI Unit Serial number.
1160  *          This is a fake. We should read a proper
1161  *          serial number from the container. <SuSE>But
1162  *          without docs it's quite hard to do it :-)
1163  *          So this will have to do in the meantime.</SuSE>
1164  */
1165 
setinqserial(struct aac_dev * dev,void * data,int cid)1166 static int setinqserial(struct aac_dev *dev, void *data, int cid)
1167 {
1168 	/*
1169 	 *	This breaks array migration.
1170 	 */
1171 	return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
1172 			le32_to_cpu(dev->adapter_info.serial[0]), cid);
1173 }
1174 
set_sense(struct sense_data * sense_data,u8 sense_key,u8 sense_code,u8 a_sense_code,u8 bit_pointer,u16 field_pointer)1175 static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
1176 	u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
1177 {
1178 	u8 *sense_buf = (u8 *)sense_data;
1179 	/* Sense data valid, err code 70h */
1180 	sense_buf[0] = 0x70; /* No info field */
1181 	sense_buf[1] = 0;	/* Segment number, always zero */
1182 
1183 	sense_buf[2] = sense_key;	/* Sense key */
1184 
1185 	sense_buf[12] = sense_code;	/* Additional sense code */
1186 	sense_buf[13] = a_sense_code;	/* Additional sense code qualifier */
1187 
1188 	if (sense_key == ILLEGAL_REQUEST) {
1189 		sense_buf[7] = 10;	/* Additional sense length */
1190 
1191 		sense_buf[15] = bit_pointer;
1192 		/* Illegal parameter is in the parameter block */
1193 		if (sense_code == SENCODE_INVALID_CDB_FIELD)
1194 			sense_buf[15] |= 0xc0;/* Std sense key specific field */
1195 		/* Illegal parameter is in the CDB block */
1196 		sense_buf[16] = field_pointer >> 8;	/* MSB */
1197 		sense_buf[17] = field_pointer;		/* LSB */
1198 	} else
1199 		sense_buf[7] = 6;	/* Additional sense length */
1200 }
1201 
aac_bounds_32(struct aac_dev * dev,struct scsi_cmnd * cmd,u64 lba)1202 static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1203 {
1204 	if (lba & 0xffffffff00000000LL) {
1205 		int cid = scmd_id(cmd);
1206 		dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1207 		cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1208 			SAM_STAT_CHECK_CONDITION;
1209 		set_sense(&dev->fsa_dev[cid].sense_data,
1210 		  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1211 		  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1212 		memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1213 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1214 			     SCSI_SENSE_BUFFERSIZE));
1215 		cmd->scsi_done(cmd);
1216 		return 1;
1217 	}
1218 	return 0;
1219 }
1220 
aac_bounds_64(struct aac_dev * dev,struct scsi_cmnd * cmd,u64 lba)1221 static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1222 {
1223 	return 0;
1224 }
1225 
1226 static void io_callback(void *context, struct fib * fibptr);
1227 
aac_read_raw_io(struct fib * fib,struct scsi_cmnd * cmd,u64 lba,u32 count)1228 static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1229 {
1230 	struct aac_dev *dev = fib->dev;
1231 	u16 fibsize, command;
1232 	long ret;
1233 
1234 	aac_fib_init(fib);
1235 	if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1236 		dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1237 		!dev->sync_mode) {
1238 		struct aac_raw_io2 *readcmd2;
1239 		readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
1240 		memset(readcmd2, 0, sizeof(struct aac_raw_io2));
1241 		readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1242 		readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1243 		readcmd2->byteCount = cpu_to_le32(count *
1244 			dev->fsa_dev[scmd_id(cmd)].block_size);
1245 		readcmd2->cid = cpu_to_le16(scmd_id(cmd));
1246 		readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
1247 		ret = aac_build_sgraw2(cmd, readcmd2,
1248 				dev->scsi_host_ptr->sg_tablesize);
1249 		if (ret < 0)
1250 			return ret;
1251 		command = ContainerRawIo2;
1252 		fibsize = sizeof(struct aac_raw_io2) +
1253 			((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1254 	} else {
1255 		struct aac_raw_io *readcmd;
1256 		readcmd = (struct aac_raw_io *) fib_data(fib);
1257 		readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1258 		readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1259 		readcmd->count = cpu_to_le32(count *
1260 			dev->fsa_dev[scmd_id(cmd)].block_size);
1261 		readcmd->cid = cpu_to_le16(scmd_id(cmd));
1262 		readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
1263 		readcmd->bpTotal = 0;
1264 		readcmd->bpComplete = 0;
1265 		ret = aac_build_sgraw(cmd, &readcmd->sg);
1266 		if (ret < 0)
1267 			return ret;
1268 		command = ContainerRawIo;
1269 		fibsize = sizeof(struct aac_raw_io) +
1270 			((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
1271 	}
1272 
1273 	BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1274 	/*
1275 	 *	Now send the Fib to the adapter
1276 	 */
1277 	return aac_fib_send(command,
1278 			  fib,
1279 			  fibsize,
1280 			  FsaNormal,
1281 			  0, 1,
1282 			  (fib_callback) io_callback,
1283 			  (void *) cmd);
1284 }
1285 
aac_read_block64(struct fib * fib,struct scsi_cmnd * cmd,u64 lba,u32 count)1286 static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1287 {
1288 	u16 fibsize;
1289 	struct aac_read64 *readcmd;
1290 	long ret;
1291 
1292 	aac_fib_init(fib);
1293 	readcmd = (struct aac_read64 *) fib_data(fib);
1294 	readcmd->command = cpu_to_le32(VM_CtHostRead64);
1295 	readcmd->cid = cpu_to_le16(scmd_id(cmd));
1296 	readcmd->sector_count = cpu_to_le16(count);
1297 	readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1298 	readcmd->pad   = 0;
1299 	readcmd->flags = 0;
1300 
1301 	ret = aac_build_sg64(cmd, &readcmd->sg);
1302 	if (ret < 0)
1303 		return ret;
1304 	fibsize = sizeof(struct aac_read64) +
1305 		((le32_to_cpu(readcmd->sg.count) - 1) *
1306 		 sizeof (struct sgentry64));
1307 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1308 				sizeof(struct aac_fibhdr)));
1309 	/*
1310 	 *	Now send the Fib to the adapter
1311 	 */
1312 	return aac_fib_send(ContainerCommand64,
1313 			  fib,
1314 			  fibsize,
1315 			  FsaNormal,
1316 			  0, 1,
1317 			  (fib_callback) io_callback,
1318 			  (void *) cmd);
1319 }
1320 
aac_read_block(struct fib * fib,struct scsi_cmnd * cmd,u64 lba,u32 count)1321 static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1322 {
1323 	u16 fibsize;
1324 	struct aac_read *readcmd;
1325 	struct aac_dev *dev = fib->dev;
1326 	long ret;
1327 
1328 	aac_fib_init(fib);
1329 	readcmd = (struct aac_read *) fib_data(fib);
1330 	readcmd->command = cpu_to_le32(VM_CtBlockRead);
1331 	readcmd->cid = cpu_to_le32(scmd_id(cmd));
1332 	readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1333 	readcmd->count = cpu_to_le32(count *
1334 		dev->fsa_dev[scmd_id(cmd)].block_size);
1335 
1336 	ret = aac_build_sg(cmd, &readcmd->sg);
1337 	if (ret < 0)
1338 		return ret;
1339 	fibsize = sizeof(struct aac_read) +
1340 			((le32_to_cpu(readcmd->sg.count) - 1) *
1341 			 sizeof (struct sgentry));
1342 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1343 				sizeof(struct aac_fibhdr)));
1344 	/*
1345 	 *	Now send the Fib to the adapter
1346 	 */
1347 	return aac_fib_send(ContainerCommand,
1348 			  fib,
1349 			  fibsize,
1350 			  FsaNormal,
1351 			  0, 1,
1352 			  (fib_callback) io_callback,
1353 			  (void *) cmd);
1354 }
1355 
aac_write_raw_io(struct fib * fib,struct scsi_cmnd * cmd,u64 lba,u32 count,int fua)1356 static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1357 {
1358 	struct aac_dev *dev = fib->dev;
1359 	u16 fibsize, command;
1360 	long ret;
1361 
1362 	aac_fib_init(fib);
1363 	if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1364 		dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1365 		!dev->sync_mode) {
1366 		struct aac_raw_io2 *writecmd2;
1367 		writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
1368 		memset(writecmd2, 0, sizeof(struct aac_raw_io2));
1369 		writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1370 		writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1371 		writecmd2->byteCount = cpu_to_le32(count *
1372 			dev->fsa_dev[scmd_id(cmd)].block_size);
1373 		writecmd2->cid = cpu_to_le16(scmd_id(cmd));
1374 		writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
1375 						   (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1376 			cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
1377 			cpu_to_le16(RIO2_IO_TYPE_WRITE);
1378 		ret = aac_build_sgraw2(cmd, writecmd2,
1379 				dev->scsi_host_ptr->sg_tablesize);
1380 		if (ret < 0)
1381 			return ret;
1382 		command = ContainerRawIo2;
1383 		fibsize = sizeof(struct aac_raw_io2) +
1384 			((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1385 	} else {
1386 		struct aac_raw_io *writecmd;
1387 		writecmd = (struct aac_raw_io *) fib_data(fib);
1388 		writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1389 		writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1390 		writecmd->count = cpu_to_le32(count *
1391 			dev->fsa_dev[scmd_id(cmd)].block_size);
1392 		writecmd->cid = cpu_to_le16(scmd_id(cmd));
1393 		writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1394 						   (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1395 			cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
1396 			cpu_to_le16(RIO_TYPE_WRITE);
1397 		writecmd->bpTotal = 0;
1398 		writecmd->bpComplete = 0;
1399 		ret = aac_build_sgraw(cmd, &writecmd->sg);
1400 		if (ret < 0)
1401 			return ret;
1402 		command = ContainerRawIo;
1403 		fibsize = sizeof(struct aac_raw_io) +
1404 			((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
1405 	}
1406 
1407 	BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1408 	/*
1409 	 *	Now send the Fib to the adapter
1410 	 */
1411 	return aac_fib_send(command,
1412 			  fib,
1413 			  fibsize,
1414 			  FsaNormal,
1415 			  0, 1,
1416 			  (fib_callback) io_callback,
1417 			  (void *) cmd);
1418 }
1419 
aac_write_block64(struct fib * fib,struct scsi_cmnd * cmd,u64 lba,u32 count,int fua)1420 static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1421 {
1422 	u16 fibsize;
1423 	struct aac_write64 *writecmd;
1424 	long ret;
1425 
1426 	aac_fib_init(fib);
1427 	writecmd = (struct aac_write64 *) fib_data(fib);
1428 	writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1429 	writecmd->cid = cpu_to_le16(scmd_id(cmd));
1430 	writecmd->sector_count = cpu_to_le16(count);
1431 	writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1432 	writecmd->pad	= 0;
1433 	writecmd->flags	= 0;
1434 
1435 	ret = aac_build_sg64(cmd, &writecmd->sg);
1436 	if (ret < 0)
1437 		return ret;
1438 	fibsize = sizeof(struct aac_write64) +
1439 		((le32_to_cpu(writecmd->sg.count) - 1) *
1440 		 sizeof (struct sgentry64));
1441 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1442 				sizeof(struct aac_fibhdr)));
1443 	/*
1444 	 *	Now send the Fib to the adapter
1445 	 */
1446 	return aac_fib_send(ContainerCommand64,
1447 			  fib,
1448 			  fibsize,
1449 			  FsaNormal,
1450 			  0, 1,
1451 			  (fib_callback) io_callback,
1452 			  (void *) cmd);
1453 }
1454 
aac_write_block(struct fib * fib,struct scsi_cmnd * cmd,u64 lba,u32 count,int fua)1455 static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1456 {
1457 	u16 fibsize;
1458 	struct aac_write *writecmd;
1459 	struct aac_dev *dev = fib->dev;
1460 	long ret;
1461 
1462 	aac_fib_init(fib);
1463 	writecmd = (struct aac_write *) fib_data(fib);
1464 	writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1465 	writecmd->cid = cpu_to_le32(scmd_id(cmd));
1466 	writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1467 	writecmd->count = cpu_to_le32(count *
1468 		dev->fsa_dev[scmd_id(cmd)].block_size);
1469 	writecmd->sg.count = cpu_to_le32(1);
1470 	/* ->stable is not used - it did mean which type of write */
1471 
1472 	ret = aac_build_sg(cmd, &writecmd->sg);
1473 	if (ret < 0)
1474 		return ret;
1475 	fibsize = sizeof(struct aac_write) +
1476 		((le32_to_cpu(writecmd->sg.count) - 1) *
1477 		 sizeof (struct sgentry));
1478 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1479 				sizeof(struct aac_fibhdr)));
1480 	/*
1481 	 *	Now send the Fib to the adapter
1482 	 */
1483 	return aac_fib_send(ContainerCommand,
1484 			  fib,
1485 			  fibsize,
1486 			  FsaNormal,
1487 			  0, 1,
1488 			  (fib_callback) io_callback,
1489 			  (void *) cmd);
1490 }
1491 
aac_scsi_common(struct fib * fib,struct scsi_cmnd * cmd)1492 static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1493 {
1494 	struct aac_srb * srbcmd;
1495 	u32 flag;
1496 	u32 timeout;
1497 
1498 	aac_fib_init(fib);
1499 	switch(cmd->sc_data_direction){
1500 	case DMA_TO_DEVICE:
1501 		flag = SRB_DataOut;
1502 		break;
1503 	case DMA_BIDIRECTIONAL:
1504 		flag = SRB_DataIn | SRB_DataOut;
1505 		break;
1506 	case DMA_FROM_DEVICE:
1507 		flag = SRB_DataIn;
1508 		break;
1509 	case DMA_NONE:
1510 	default:	/* shuts up some versions of gcc */
1511 		flag = SRB_NoDataXfer;
1512 		break;
1513 	}
1514 
1515 	srbcmd = (struct aac_srb*) fib_data(fib);
1516 	srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1517 	srbcmd->channel  = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1518 	srbcmd->id       = cpu_to_le32(scmd_id(cmd));
1519 	srbcmd->lun      = cpu_to_le32(cmd->device->lun);
1520 	srbcmd->flags    = cpu_to_le32(flag);
1521 	timeout = cmd->request->timeout/HZ;
1522 	if (timeout == 0)
1523 		timeout = 1;
1524 	srbcmd->timeout  = cpu_to_le32(timeout);  // timeout in seconds
1525 	srbcmd->retry_limit = 0; /* Obsolete parameter */
1526 	srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1527 	return srbcmd;
1528 }
1529 
aac_construct_hbacmd(struct fib * fib,struct scsi_cmnd * cmd)1530 static struct aac_hba_cmd_req *aac_construct_hbacmd(struct fib *fib,
1531 							struct scsi_cmnd *cmd)
1532 {
1533 	struct aac_hba_cmd_req *hbacmd;
1534 	struct aac_dev *dev;
1535 	int bus, target;
1536 	u64 address;
1537 
1538 	dev = (struct aac_dev *)cmd->device->host->hostdata;
1539 
1540 	hbacmd = (struct aac_hba_cmd_req *)fib->hw_fib_va;
1541 	memset(hbacmd, 0, 96);	/* sizeof(*hbacmd) is not necessary */
1542 	/* iu_type is a parameter of aac_hba_send */
1543 	switch (cmd->sc_data_direction) {
1544 	case DMA_TO_DEVICE:
1545 		hbacmd->byte1 = 2;
1546 		break;
1547 	case DMA_FROM_DEVICE:
1548 	case DMA_BIDIRECTIONAL:
1549 		hbacmd->byte1 = 1;
1550 		break;
1551 	case DMA_NONE:
1552 	default:
1553 		break;
1554 	}
1555 	hbacmd->lun[1] = cpu_to_le32(cmd->device->lun);
1556 
1557 	bus = aac_logical_to_phys(scmd_channel(cmd));
1558 	target = scmd_id(cmd);
1559 	hbacmd->it_nexus = dev->hba_map[bus][target].rmw_nexus;
1560 
1561 	/* we fill in reply_qid later in aac_src_deliver_message */
1562 	/* we fill in iu_type, request_id later in aac_hba_send */
1563 	/* we fill in emb_data_desc_count later in aac_build_sghba */
1564 
1565 	memcpy(hbacmd->cdb, cmd->cmnd, cmd->cmd_len);
1566 	hbacmd->data_length = cpu_to_le32(scsi_bufflen(cmd));
1567 
1568 	address = (u64)fib->hw_error_pa;
1569 	hbacmd->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
1570 	hbacmd->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
1571 	hbacmd->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
1572 
1573 	return hbacmd;
1574 }
1575 
1576 static void aac_srb_callback(void *context, struct fib * fibptr);
1577 
aac_scsi_64(struct fib * fib,struct scsi_cmnd * cmd)1578 static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1579 {
1580 	u16 fibsize;
1581 	struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1582 	long ret;
1583 
1584 	ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
1585 	if (ret < 0)
1586 		return ret;
1587 	srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1588 
1589 	memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1590 	memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1591 	/*
1592 	 *	Build Scatter/Gather list
1593 	 */
1594 	fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1595 		((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1596 		 sizeof (struct sgentry64));
1597 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1598 				sizeof(struct aac_fibhdr)));
1599 
1600 	/*
1601 	 *	Now send the Fib to the adapter
1602 	 */
1603 	return aac_fib_send(ScsiPortCommand64, fib,
1604 				fibsize, FsaNormal, 0, 1,
1605 				  (fib_callback) aac_srb_callback,
1606 				  (void *) cmd);
1607 }
1608 
aac_scsi_32(struct fib * fib,struct scsi_cmnd * cmd)1609 static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1610 {
1611 	u16 fibsize;
1612 	struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1613 	long ret;
1614 
1615 	ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
1616 	if (ret < 0)
1617 		return ret;
1618 	srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1619 
1620 	memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1621 	memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1622 	/*
1623 	 *	Build Scatter/Gather list
1624 	 */
1625 	fibsize = sizeof (struct aac_srb) +
1626 		(((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1627 		 sizeof (struct sgentry));
1628 	BUG_ON (fibsize > (fib->dev->max_fib_size -
1629 				sizeof(struct aac_fibhdr)));
1630 
1631 	/*
1632 	 *	Now send the Fib to the adapter
1633 	 */
1634 	return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1635 				  (fib_callback) aac_srb_callback, (void *) cmd);
1636 }
1637 
aac_scsi_32_64(struct fib * fib,struct scsi_cmnd * cmd)1638 static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1639 {
1640 	if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1641 	    (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1642 		return FAILED;
1643 	return aac_scsi_32(fib, cmd);
1644 }
1645 
aac_adapter_hba(struct fib * fib,struct scsi_cmnd * cmd)1646 static int aac_adapter_hba(struct fib *fib, struct scsi_cmnd *cmd)
1647 {
1648 	struct aac_hba_cmd_req *hbacmd = aac_construct_hbacmd(fib, cmd);
1649 	struct aac_dev *dev;
1650 	long ret;
1651 
1652 	dev = (struct aac_dev *)cmd->device->host->hostdata;
1653 
1654 	ret = aac_build_sghba(cmd, hbacmd,
1655 		dev->scsi_host_ptr->sg_tablesize, (u64)fib->hw_sgl_pa);
1656 	if (ret < 0)
1657 		return ret;
1658 
1659 	/*
1660 	 *	Now send the HBA command to the adapter
1661 	 */
1662 	fib->hbacmd_size = 64 + le32_to_cpu(hbacmd->emb_data_desc_count) *
1663 		sizeof(struct aac_hba_sgl);
1664 
1665 	return aac_hba_send(HBA_IU_TYPE_SCSI_CMD_REQ, fib,
1666 				  (fib_callback) aac_hba_callback,
1667 				  (void *) cmd);
1668 }
1669 
aac_issue_bmic_identify(struct aac_dev * dev,u32 bus,u32 target)1670 int aac_issue_bmic_identify(struct aac_dev *dev, u32 bus, u32 target)
1671 {
1672 	struct fib *fibptr;
1673 	struct aac_srb *srbcmd;
1674 	struct sgmap64 *sg64;
1675 	struct aac_ciss_identify_pd *identify_resp;
1676 	dma_addr_t addr;
1677 	u32 vbus, vid;
1678 	u16 fibsize, datasize;
1679 	int rcode = -ENOMEM;
1680 
1681 
1682 	fibptr = aac_fib_alloc(dev);
1683 	if (!fibptr)
1684 		goto out;
1685 
1686 	fibsize = sizeof(struct aac_srb) -
1687 			sizeof(struct sgentry) + sizeof(struct sgentry64);
1688 	datasize = sizeof(struct aac_ciss_identify_pd);
1689 
1690 	identify_resp = dma_alloc_coherent(&dev->pdev->dev, datasize, &addr,
1691 					   GFP_KERNEL);
1692 	if (!identify_resp)
1693 		goto fib_free_ptr;
1694 
1695 	vbus = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_bus);
1696 	vid = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_target);
1697 
1698 	aac_fib_init(fibptr);
1699 
1700 	srbcmd = (struct aac_srb *) fib_data(fibptr);
1701 	srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1702 	srbcmd->channel  = cpu_to_le32(vbus);
1703 	srbcmd->id       = cpu_to_le32(vid);
1704 	srbcmd->lun      = 0;
1705 	srbcmd->flags    = cpu_to_le32(SRB_DataIn);
1706 	srbcmd->timeout  = cpu_to_le32(10);
1707 	srbcmd->retry_limit = 0;
1708 	srbcmd->cdb_size = cpu_to_le32(12);
1709 	srbcmd->count = cpu_to_le32(datasize);
1710 
1711 	memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1712 	srbcmd->cdb[0] = 0x26;
1713 	srbcmd->cdb[2] = (u8)((AAC_MAX_LUN + target) & 0x00FF);
1714 	srbcmd->cdb[6] = CISS_IDENTIFY_PHYSICAL_DEVICE;
1715 
1716 	sg64 = (struct sgmap64 *)&srbcmd->sg;
1717 	sg64->count = cpu_to_le32(1);
1718 	sg64->sg[0].addr[1] = cpu_to_le32((u32)(((addr) >> 16) >> 16));
1719 	sg64->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
1720 	sg64->sg[0].count = cpu_to_le32(datasize);
1721 
1722 	rcode = aac_fib_send(ScsiPortCommand64,
1723 		fibptr, fibsize, FsaNormal, 1, 1, NULL, NULL);
1724 
1725 	if (identify_resp->current_queue_depth_limit <= 0 ||
1726 		identify_resp->current_queue_depth_limit > 32)
1727 		dev->hba_map[bus][target].qd_limit = 32;
1728 	else
1729 		dev->hba_map[bus][target].qd_limit =
1730 			identify_resp->current_queue_depth_limit;
1731 
1732 	dma_free_coherent(&dev->pdev->dev, datasize, identify_resp, addr);
1733 
1734 	aac_fib_complete(fibptr);
1735 
1736 fib_free_ptr:
1737 	aac_fib_free(fibptr);
1738 out:
1739 	return rcode;
1740 }
1741 
1742 /**
1743  *	aac_update hba_map()-	update current hba map with data from FW
1744  *	@dev:	aac_dev structure
1745  *	@phys_luns: FW information from report phys luns
1746  *
1747  *	Update our hba map with the information gathered from the FW
1748  */
aac_update_hba_map(struct aac_dev * dev,struct aac_ciss_phys_luns_resp * phys_luns,int rescan)1749 void aac_update_hba_map(struct aac_dev *dev,
1750 		struct aac_ciss_phys_luns_resp *phys_luns, int rescan)
1751 {
1752 	/* ok and extended reporting */
1753 	u32 lun_count, nexus;
1754 	u32 i, bus, target;
1755 	u8 expose_flag, attribs;
1756 	u8 devtype;
1757 
1758 	lun_count = ((phys_luns->list_length[0] << 24)
1759 			+ (phys_luns->list_length[1] << 16)
1760 			+ (phys_luns->list_length[2] << 8)
1761 			+ (phys_luns->list_length[3])) / 24;
1762 
1763 	for (i = 0; i < lun_count; ++i) {
1764 
1765 		bus = phys_luns->lun[i].level2[1] & 0x3f;
1766 		target = phys_luns->lun[i].level2[0];
1767 		expose_flag = phys_luns->lun[i].bus >> 6;
1768 		attribs = phys_luns->lun[i].node_ident[9];
1769 		nexus = *((u32 *) &phys_luns->lun[i].node_ident[12]);
1770 
1771 		if (bus >= AAC_MAX_BUSES || target >= AAC_MAX_TARGETS)
1772 			continue;
1773 
1774 		dev->hba_map[bus][target].expose = expose_flag;
1775 
1776 		if (expose_flag != 0) {
1777 			devtype = AAC_DEVTYPE_RAID_MEMBER;
1778 			goto update_devtype;
1779 		}
1780 
1781 		if (nexus != 0 && (attribs & 8)) {
1782 			devtype = AAC_DEVTYPE_NATIVE_RAW;
1783 			dev->hba_map[bus][target].rmw_nexus =
1784 					nexus;
1785 		} else
1786 			devtype = AAC_DEVTYPE_ARC_RAW;
1787 
1788 		if (devtype != AAC_DEVTYPE_NATIVE_RAW)
1789 			goto update_devtype;
1790 
1791 		if (aac_issue_bmic_identify(dev, bus, target) < 0)
1792 			dev->hba_map[bus][target].qd_limit = 32;
1793 
1794 update_devtype:
1795 		if (rescan == AAC_INIT)
1796 			dev->hba_map[bus][target].devtype = devtype;
1797 		else
1798 			dev->hba_map[bus][target].new_devtype = devtype;
1799 	}
1800 }
1801 
1802 /**
1803  *	aac_report_phys_luns()	Process topology change
1804  *	@dev:		aac_dev structure
1805  *	@fibptr:	fib pointer
1806  *
1807  *	Execute a CISS REPORT PHYS LUNS and process the results into
1808  *	the current hba_map.
1809  */
aac_report_phys_luns(struct aac_dev * dev,struct fib * fibptr,int rescan)1810 int aac_report_phys_luns(struct aac_dev *dev, struct fib *fibptr, int rescan)
1811 {
1812 	int fibsize, datasize;
1813 	struct aac_ciss_phys_luns_resp *phys_luns;
1814 	struct aac_srb *srbcmd;
1815 	struct sgmap64 *sg64;
1816 	dma_addr_t addr;
1817 	u32 vbus, vid;
1818 	int rcode = 0;
1819 
1820 	/* Thor SA Firmware -> CISS_REPORT_PHYSICAL_LUNS */
1821 	fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry)
1822 			+ sizeof(struct sgentry64);
1823 	datasize = sizeof(struct aac_ciss_phys_luns_resp)
1824 			+ (AAC_MAX_TARGETS - 1) * sizeof(struct _ciss_lun);
1825 
1826 	phys_luns = dma_alloc_coherent(&dev->pdev->dev, datasize, &addr,
1827 				       GFP_KERNEL);
1828 	if (phys_luns == NULL) {
1829 		rcode = -ENOMEM;
1830 		goto err_out;
1831 	}
1832 
1833 	vbus = (u32) le16_to_cpu(
1834 			dev->supplement_adapter_info.virt_device_bus);
1835 	vid = (u32) le16_to_cpu(
1836 			dev->supplement_adapter_info.virt_device_target);
1837 
1838 	aac_fib_init(fibptr);
1839 
1840 	srbcmd = (struct aac_srb *) fib_data(fibptr);
1841 	srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1842 	srbcmd->channel = cpu_to_le32(vbus);
1843 	srbcmd->id = cpu_to_le32(vid);
1844 	srbcmd->lun = 0;
1845 	srbcmd->flags = cpu_to_le32(SRB_DataIn);
1846 	srbcmd->timeout = cpu_to_le32(10);
1847 	srbcmd->retry_limit = 0;
1848 	srbcmd->cdb_size = cpu_to_le32(12);
1849 	srbcmd->count = cpu_to_le32(datasize);
1850 
1851 	memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1852 	srbcmd->cdb[0] = CISS_REPORT_PHYSICAL_LUNS;
1853 	srbcmd->cdb[1] = 2; /* extended reporting */
1854 	srbcmd->cdb[8] = (u8)(datasize >> 8);
1855 	srbcmd->cdb[9] = (u8)(datasize);
1856 
1857 	sg64 = (struct sgmap64 *) &srbcmd->sg;
1858 	sg64->count = cpu_to_le32(1);
1859 	sg64->sg[0].addr[1] = cpu_to_le32(upper_32_bits(addr));
1860 	sg64->sg[0].addr[0] = cpu_to_le32(lower_32_bits(addr));
1861 	sg64->sg[0].count = cpu_to_le32(datasize);
1862 
1863 	rcode = aac_fib_send(ScsiPortCommand64, fibptr, fibsize,
1864 			FsaNormal, 1, 1, NULL, NULL);
1865 
1866 	/* analyse data */
1867 	if (rcode >= 0 && phys_luns->resp_flag == 2) {
1868 		/* ok and extended reporting */
1869 		aac_update_hba_map(dev, phys_luns, rescan);
1870 	}
1871 
1872 	dma_free_coherent(&dev->pdev->dev, datasize, phys_luns, addr);
1873 err_out:
1874 	return rcode;
1875 }
1876 
aac_get_adapter_info(struct aac_dev * dev)1877 int aac_get_adapter_info(struct aac_dev* dev)
1878 {
1879 	struct fib* fibptr;
1880 	int rcode;
1881 	u32 tmp, bus, target;
1882 	struct aac_adapter_info *info;
1883 	struct aac_bus_info *command;
1884 	struct aac_bus_info_response *bus_info;
1885 
1886 	if (!(fibptr = aac_fib_alloc(dev)))
1887 		return -ENOMEM;
1888 
1889 	aac_fib_init(fibptr);
1890 	info = (struct aac_adapter_info *) fib_data(fibptr);
1891 	memset(info,0,sizeof(*info));
1892 
1893 	rcode = aac_fib_send(RequestAdapterInfo,
1894 			 fibptr,
1895 			 sizeof(*info),
1896 			 FsaNormal,
1897 			 -1, 1, /* First `interrupt' command uses special wait */
1898 			 NULL,
1899 			 NULL);
1900 
1901 	if (rcode < 0) {
1902 		/* FIB should be freed only after
1903 		 * getting the response from the F/W */
1904 		if (rcode != -ERESTARTSYS) {
1905 			aac_fib_complete(fibptr);
1906 			aac_fib_free(fibptr);
1907 		}
1908 		return rcode;
1909 	}
1910 	memcpy(&dev->adapter_info, info, sizeof(*info));
1911 
1912 	dev->supplement_adapter_info.virt_device_bus = 0xffff;
1913 	if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1914 		struct aac_supplement_adapter_info * sinfo;
1915 
1916 		aac_fib_init(fibptr);
1917 
1918 		sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1919 
1920 		memset(sinfo,0,sizeof(*sinfo));
1921 
1922 		rcode = aac_fib_send(RequestSupplementAdapterInfo,
1923 				 fibptr,
1924 				 sizeof(*sinfo),
1925 				 FsaNormal,
1926 				 1, 1,
1927 				 NULL,
1928 				 NULL);
1929 
1930 		if (rcode >= 0)
1931 			memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
1932 		if (rcode == -ERESTARTSYS) {
1933 			fibptr = aac_fib_alloc(dev);
1934 			if (!fibptr)
1935 				return -ENOMEM;
1936 		}
1937 
1938 	}
1939 
1940 	/* reset all previous mapped devices (i.e. for init. after IOP_RESET) */
1941 	for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
1942 		for (target = 0; target < AAC_MAX_TARGETS; target++) {
1943 			dev->hba_map[bus][target].devtype = 0;
1944 			dev->hba_map[bus][target].qd_limit = 0;
1945 		}
1946 	}
1947 
1948 	/*
1949 	 * GetBusInfo
1950 	 */
1951 
1952 	aac_fib_init(fibptr);
1953 
1954 	bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1955 
1956 	memset(bus_info, 0, sizeof(*bus_info));
1957 
1958 	command = (struct aac_bus_info *)bus_info;
1959 
1960 	command->Command = cpu_to_le32(VM_Ioctl);
1961 	command->ObjType = cpu_to_le32(FT_DRIVE);
1962 	command->MethodId = cpu_to_le32(1);
1963 	command->CtlCmd = cpu_to_le32(GetBusInfo);
1964 
1965 	rcode = aac_fib_send(ContainerCommand,
1966 			 fibptr,
1967 			 sizeof (*bus_info),
1968 			 FsaNormal,
1969 			 1, 1,
1970 			 NULL, NULL);
1971 
1972 	/* reasoned default */
1973 	dev->maximum_num_physicals = 16;
1974 	if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1975 		dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1976 		dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1977 	}
1978 
1979 	if (!dev->sync_mode && dev->sa_firmware &&
1980 		dev->supplement_adapter_info.virt_device_bus != 0xffff) {
1981 		/* Thor SA Firmware -> CISS_REPORT_PHYSICAL_LUNS */
1982 		rcode = aac_report_phys_luns(dev, fibptr, AAC_INIT);
1983 	}
1984 
1985 	if (!dev->in_reset) {
1986 		char buffer[16];
1987 		tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1988 		printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
1989 			dev->name,
1990 			dev->id,
1991 			tmp>>24,
1992 			(tmp>>16)&0xff,
1993 			tmp&0xff,
1994 			le32_to_cpu(dev->adapter_info.kernelbuild),
1995 			(int)sizeof(dev->supplement_adapter_info.build_date),
1996 			dev->supplement_adapter_info.build_date);
1997 		tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1998 		printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
1999 			dev->name, dev->id,
2000 			tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2001 			le32_to_cpu(dev->adapter_info.monitorbuild));
2002 		tmp = le32_to_cpu(dev->adapter_info.biosrev);
2003 		printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
2004 			dev->name, dev->id,
2005 			tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2006 			le32_to_cpu(dev->adapter_info.biosbuild));
2007 		buffer[0] = '\0';
2008 		if (aac_get_serial_number(
2009 		  shost_to_class(dev->scsi_host_ptr), buffer))
2010 			printk(KERN_INFO "%s%d: serial %s",
2011 			  dev->name, dev->id, buffer);
2012 		if (dev->supplement_adapter_info.vpd_info.tsid[0]) {
2013 			printk(KERN_INFO "%s%d: TSID %.*s\n",
2014 			  dev->name, dev->id,
2015 			  (int)sizeof(dev->supplement_adapter_info
2016 							.vpd_info.tsid),
2017 				dev->supplement_adapter_info.vpd_info.tsid);
2018 		}
2019 		if (!aac_check_reset || ((aac_check_reset == 1) &&
2020 		  (dev->supplement_adapter_info.supported_options2 &
2021 		  AAC_OPTION_IGNORE_RESET))) {
2022 			printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
2023 			  dev->name, dev->id);
2024 		}
2025 	}
2026 
2027 	dev->cache_protected = 0;
2028 	dev->jbod = ((dev->supplement_adapter_info.feature_bits &
2029 		AAC_FEATURE_JBOD) != 0);
2030 	dev->nondasd_support = 0;
2031 	dev->raid_scsi_mode = 0;
2032 	if(dev->adapter_info.options & AAC_OPT_NONDASD)
2033 		dev->nondasd_support = 1;
2034 
2035 	/*
2036 	 * If the firmware supports ROMB RAID/SCSI mode and we are currently
2037 	 * in RAID/SCSI mode, set the flag. For now if in this mode we will
2038 	 * force nondasd support on. If we decide to allow the non-dasd flag
2039 	 * additional changes changes will have to be made to support
2040 	 * RAID/SCSI.  the function aac_scsi_cmd in this module will have to be
2041 	 * changed to support the new dev->raid_scsi_mode flag instead of
2042 	 * leaching off of the dev->nondasd_support flag. Also in linit.c the
2043 	 * function aac_detect will have to be modified where it sets up the
2044 	 * max number of channels based on the aac->nondasd_support flag only.
2045 	 */
2046 	if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
2047 	    (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
2048 		dev->nondasd_support = 1;
2049 		dev->raid_scsi_mode = 1;
2050 	}
2051 	if (dev->raid_scsi_mode != 0)
2052 		printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
2053 				dev->name, dev->id);
2054 
2055 	if (nondasd != -1)
2056 		dev->nondasd_support = (nondasd!=0);
2057 	if (dev->nondasd_support && !dev->in_reset)
2058 		printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
2059 
2060 	if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
2061 		dev->needs_dac = 1;
2062 	dev->dac_support = 0;
2063 	if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
2064 	    (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
2065 		if (!dev->in_reset)
2066 			printk(KERN_INFO "%s%d: 64bit support enabled.\n",
2067 				dev->name, dev->id);
2068 		dev->dac_support = 1;
2069 	}
2070 
2071 	if(dacmode != -1) {
2072 		dev->dac_support = (dacmode!=0);
2073 	}
2074 
2075 	/* avoid problems with AAC_QUIRK_SCSI_32 controllers */
2076 	if (dev->dac_support &&	(aac_get_driver_ident(dev->cardtype)->quirks
2077 		& AAC_QUIRK_SCSI_32)) {
2078 		dev->nondasd_support = 0;
2079 		dev->jbod = 0;
2080 		expose_physicals = 0;
2081 	}
2082 
2083 	if (dev->dac_support) {
2084 		if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
2085 			if (!dev->in_reset)
2086 				dev_info(&dev->pdev->dev, "64 Bit DAC enabled\n");
2087 		} else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
2088 			dev_info(&dev->pdev->dev, "DMA mask set failed, 64 Bit DAC disabled\n");
2089 			dev->dac_support = 0;
2090 		} else {
2091 			dev_info(&dev->pdev->dev, "No suitable DMA available\n");
2092 			rcode = -ENOMEM;
2093 		}
2094 	}
2095 	/*
2096 	 * Deal with configuring for the individualized limits of each packet
2097 	 * interface.
2098 	 */
2099 	dev->a_ops.adapter_scsi = (dev->dac_support)
2100 	  ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
2101 				? aac_scsi_32_64
2102 				: aac_scsi_64)
2103 				: aac_scsi_32;
2104 	if (dev->raw_io_interface) {
2105 		dev->a_ops.adapter_bounds = (dev->raw_io_64)
2106 					? aac_bounds_64
2107 					: aac_bounds_32;
2108 		dev->a_ops.adapter_read = aac_read_raw_io;
2109 		dev->a_ops.adapter_write = aac_write_raw_io;
2110 	} else {
2111 		dev->a_ops.adapter_bounds = aac_bounds_32;
2112 		dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
2113 			sizeof(struct aac_fibhdr) -
2114 			sizeof(struct aac_write) + sizeof(struct sgentry)) /
2115 				sizeof(struct sgentry);
2116 		if (dev->dac_support) {
2117 			dev->a_ops.adapter_read = aac_read_block64;
2118 			dev->a_ops.adapter_write = aac_write_block64;
2119 			/*
2120 			 * 38 scatter gather elements
2121 			 */
2122 			dev->scsi_host_ptr->sg_tablesize =
2123 				(dev->max_fib_size -
2124 				sizeof(struct aac_fibhdr) -
2125 				sizeof(struct aac_write64) +
2126 				sizeof(struct sgentry64)) /
2127 					sizeof(struct sgentry64);
2128 		} else {
2129 			dev->a_ops.adapter_read = aac_read_block;
2130 			dev->a_ops.adapter_write = aac_write_block;
2131 		}
2132 		dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
2133 		if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
2134 			/*
2135 			 * Worst case size that could cause sg overflow when
2136 			 * we break up SG elements that are larger than 64KB.
2137 			 * Would be nice if we could tell the SCSI layer what
2138 			 * the maximum SG element size can be. Worst case is
2139 			 * (sg_tablesize-1) 4KB elements with one 64KB
2140 			 * element.
2141 			 *	32bit -> 468 or 238KB	64bit -> 424 or 212KB
2142 			 */
2143 			dev->scsi_host_ptr->max_sectors =
2144 			  (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
2145 		}
2146 	}
2147 	if (!dev->sync_mode && dev->sa_firmware &&
2148 		dev->scsi_host_ptr->sg_tablesize > HBA_MAX_SG_SEPARATE)
2149 		dev->scsi_host_ptr->sg_tablesize = dev->sg_tablesize =
2150 			HBA_MAX_SG_SEPARATE;
2151 
2152 	/* FIB should be freed only after getting the response from the F/W */
2153 	if (rcode != -ERESTARTSYS) {
2154 		aac_fib_complete(fibptr);
2155 		aac_fib_free(fibptr);
2156 	}
2157 
2158 	return rcode;
2159 }
2160 
2161 
io_callback(void * context,struct fib * fibptr)2162 static void io_callback(void *context, struct fib * fibptr)
2163 {
2164 	struct aac_dev *dev;
2165 	struct aac_read_reply *readreply;
2166 	struct scsi_cmnd *scsicmd;
2167 	u32 cid;
2168 
2169 	scsicmd = (struct scsi_cmnd *) context;
2170 
2171 	if (!aac_valid_context(scsicmd, fibptr))
2172 		return;
2173 
2174 	dev = fibptr->dev;
2175 	cid = scmd_id(scsicmd);
2176 
2177 	if (nblank(dprintk(x))) {
2178 		u64 lba;
2179 		switch (scsicmd->cmnd[0]) {
2180 		case WRITE_6:
2181 		case READ_6:
2182 			lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2183 			    (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2184 			break;
2185 		case WRITE_16:
2186 		case READ_16:
2187 			lba = ((u64)scsicmd->cmnd[2] << 56) |
2188 			      ((u64)scsicmd->cmnd[3] << 48) |
2189 			      ((u64)scsicmd->cmnd[4] << 40) |
2190 			      ((u64)scsicmd->cmnd[5] << 32) |
2191 			      ((u64)scsicmd->cmnd[6] << 24) |
2192 			      (scsicmd->cmnd[7] << 16) |
2193 			      (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2194 			break;
2195 		case WRITE_12:
2196 		case READ_12:
2197 			lba = ((u64)scsicmd->cmnd[2] << 24) |
2198 			      (scsicmd->cmnd[3] << 16) |
2199 			      (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2200 			break;
2201 		default:
2202 			lba = ((u64)scsicmd->cmnd[2] << 24) |
2203 			       (scsicmd->cmnd[3] << 16) |
2204 			       (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2205 			break;
2206 		}
2207 		printk(KERN_DEBUG
2208 		  "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
2209 		  smp_processor_id(), (unsigned long long)lba, jiffies);
2210 	}
2211 
2212 	BUG_ON(fibptr == NULL);
2213 
2214 	scsi_dma_unmap(scsicmd);
2215 
2216 	readreply = (struct aac_read_reply *)fib_data(fibptr);
2217 	switch (le32_to_cpu(readreply->status)) {
2218 	case ST_OK:
2219 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2220 			SAM_STAT_GOOD;
2221 		dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
2222 		break;
2223 	case ST_NOT_READY:
2224 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2225 			SAM_STAT_CHECK_CONDITION;
2226 		set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
2227 		  SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
2228 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2229 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2230 			     SCSI_SENSE_BUFFERSIZE));
2231 		break;
2232 	case ST_MEDERR:
2233 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2234 			SAM_STAT_CHECK_CONDITION;
2235 		set_sense(&dev->fsa_dev[cid].sense_data, MEDIUM_ERROR,
2236 		  SENCODE_UNRECOVERED_READ_ERROR, ASENCODE_NO_SENSE, 0, 0);
2237 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2238 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2239 			     SCSI_SENSE_BUFFERSIZE));
2240 		break;
2241 	default:
2242 #ifdef AAC_DETAILED_STATUS_INFO
2243 		printk(KERN_WARNING "io_callback: io failed, status = %d\n",
2244 		  le32_to_cpu(readreply->status));
2245 #endif
2246 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2247 			SAM_STAT_CHECK_CONDITION;
2248 		set_sense(&dev->fsa_dev[cid].sense_data,
2249 		  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2250 		  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2251 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2252 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2253 			     SCSI_SENSE_BUFFERSIZE));
2254 		break;
2255 	}
2256 	aac_fib_complete(fibptr);
2257 
2258 	scsicmd->scsi_done(scsicmd);
2259 }
2260 
aac_read(struct scsi_cmnd * scsicmd)2261 static int aac_read(struct scsi_cmnd * scsicmd)
2262 {
2263 	u64 lba;
2264 	u32 count;
2265 	int status;
2266 	struct aac_dev *dev;
2267 	struct fib * cmd_fibcontext;
2268 	int cid;
2269 
2270 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2271 	/*
2272 	 *	Get block address and transfer length
2273 	 */
2274 	switch (scsicmd->cmnd[0]) {
2275 	case READ_6:
2276 		dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
2277 
2278 		lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2279 			(scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2280 		count = scsicmd->cmnd[4];
2281 
2282 		if (count == 0)
2283 			count = 256;
2284 		break;
2285 	case READ_16:
2286 		dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
2287 
2288 		lba =	((u64)scsicmd->cmnd[2] << 56) |
2289 			((u64)scsicmd->cmnd[3] << 48) |
2290 			((u64)scsicmd->cmnd[4] << 40) |
2291 			((u64)scsicmd->cmnd[5] << 32) |
2292 			((u64)scsicmd->cmnd[6] << 24) |
2293 			(scsicmd->cmnd[7] << 16) |
2294 			(scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2295 		count = (scsicmd->cmnd[10] << 24) |
2296 			(scsicmd->cmnd[11] << 16) |
2297 			(scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2298 		break;
2299 	case READ_12:
2300 		dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
2301 
2302 		lba = ((u64)scsicmd->cmnd[2] << 24) |
2303 			(scsicmd->cmnd[3] << 16) |
2304 			(scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2305 		count = (scsicmd->cmnd[6] << 24) |
2306 			(scsicmd->cmnd[7] << 16) |
2307 			(scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2308 		break;
2309 	default:
2310 		dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
2311 
2312 		lba = ((u64)scsicmd->cmnd[2] << 24) |
2313 			(scsicmd->cmnd[3] << 16) |
2314 			(scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2315 		count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2316 		break;
2317 	}
2318 
2319 	if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2320 		cid = scmd_id(scsicmd);
2321 		dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2322 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2323 			SAM_STAT_CHECK_CONDITION;
2324 		set_sense(&dev->fsa_dev[cid].sense_data,
2325 			  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2326 			  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2327 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2328 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2329 			     SCSI_SENSE_BUFFERSIZE));
2330 		scsicmd->scsi_done(scsicmd);
2331 		return 1;
2332 	}
2333 
2334 	dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
2335 	  smp_processor_id(), (unsigned long long)lba, jiffies));
2336 	if (aac_adapter_bounds(dev,scsicmd,lba))
2337 		return 0;
2338 	/*
2339 	 *	Alocate and initialize a Fib
2340 	 */
2341 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2342 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2343 	status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
2344 
2345 	/*
2346 	 *	Check that the command queued to the controller
2347 	 */
2348 	if (status == -EINPROGRESS)
2349 		return 0;
2350 
2351 	printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
2352 	/*
2353 	 *	For some reason, the Fib didn't queue, return QUEUE_FULL
2354 	 */
2355 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
2356 	scsicmd->scsi_done(scsicmd);
2357 	aac_fib_complete(cmd_fibcontext);
2358 	aac_fib_free(cmd_fibcontext);
2359 	return 0;
2360 }
2361 
aac_write(struct scsi_cmnd * scsicmd)2362 static int aac_write(struct scsi_cmnd * scsicmd)
2363 {
2364 	u64 lba;
2365 	u32 count;
2366 	int fua;
2367 	int status;
2368 	struct aac_dev *dev;
2369 	struct fib * cmd_fibcontext;
2370 	int cid;
2371 
2372 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2373 	/*
2374 	 *	Get block address and transfer length
2375 	 */
2376 	if (scsicmd->cmnd[0] == WRITE_6)	/* 6 byte command */
2377 	{
2378 		lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2379 		count = scsicmd->cmnd[4];
2380 		if (count == 0)
2381 			count = 256;
2382 		fua = 0;
2383 	} else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
2384 		dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
2385 
2386 		lba =	((u64)scsicmd->cmnd[2] << 56) |
2387 			((u64)scsicmd->cmnd[3] << 48) |
2388 			((u64)scsicmd->cmnd[4] << 40) |
2389 			((u64)scsicmd->cmnd[5] << 32) |
2390 			((u64)scsicmd->cmnd[6] << 24) |
2391 			(scsicmd->cmnd[7] << 16) |
2392 			(scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2393 		count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
2394 			(scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2395 		fua = scsicmd->cmnd[1] & 0x8;
2396 	} else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
2397 		dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
2398 
2399 		lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
2400 		    | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2401 		count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
2402 		      | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2403 		fua = scsicmd->cmnd[1] & 0x8;
2404 	} else {
2405 		dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
2406 		lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2407 		count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2408 		fua = scsicmd->cmnd[1] & 0x8;
2409 	}
2410 
2411 	if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2412 		cid = scmd_id(scsicmd);
2413 		dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2414 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2415 			SAM_STAT_CHECK_CONDITION;
2416 		set_sense(&dev->fsa_dev[cid].sense_data,
2417 			  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2418 			  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2419 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2420 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2421 			     SCSI_SENSE_BUFFERSIZE));
2422 		scsicmd->scsi_done(scsicmd);
2423 		return 1;
2424 	}
2425 
2426 	dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
2427 	  smp_processor_id(), (unsigned long long)lba, jiffies));
2428 	if (aac_adapter_bounds(dev,scsicmd,lba))
2429 		return 0;
2430 	/*
2431 	 *	Allocate and initialize a Fib then setup a BlockWrite command
2432 	 */
2433 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2434 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2435 	status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
2436 
2437 	/*
2438 	 *	Check that the command queued to the controller
2439 	 */
2440 	if (status == -EINPROGRESS)
2441 		return 0;
2442 
2443 	printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
2444 	/*
2445 	 *	For some reason, the Fib didn't queue, return QUEUE_FULL
2446 	 */
2447 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
2448 	scsicmd->scsi_done(scsicmd);
2449 
2450 	aac_fib_complete(cmd_fibcontext);
2451 	aac_fib_free(cmd_fibcontext);
2452 	return 0;
2453 }
2454 
synchronize_callback(void * context,struct fib * fibptr)2455 static void synchronize_callback(void *context, struct fib *fibptr)
2456 {
2457 	struct aac_synchronize_reply *synchronizereply;
2458 	struct scsi_cmnd *cmd;
2459 
2460 	cmd = context;
2461 
2462 	if (!aac_valid_context(cmd, fibptr))
2463 		return;
2464 
2465 	dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
2466 				smp_processor_id(), jiffies));
2467 	BUG_ON(fibptr == NULL);
2468 
2469 
2470 	synchronizereply = fib_data(fibptr);
2471 	if (le32_to_cpu(synchronizereply->status) == CT_OK)
2472 		cmd->result = DID_OK << 16 |
2473 			COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2474 	else {
2475 		struct scsi_device *sdev = cmd->device;
2476 		struct aac_dev *dev = fibptr->dev;
2477 		u32 cid = sdev_id(sdev);
2478 		printk(KERN_WARNING
2479 		     "synchronize_callback: synchronize failed, status = %d\n",
2480 		     le32_to_cpu(synchronizereply->status));
2481 		cmd->result = DID_OK << 16 |
2482 			COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2483 		set_sense(&dev->fsa_dev[cid].sense_data,
2484 		  HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2485 		  ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2486 		memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2487 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2488 			     SCSI_SENSE_BUFFERSIZE));
2489 	}
2490 
2491 	aac_fib_complete(fibptr);
2492 	aac_fib_free(fibptr);
2493 	cmd->scsi_done(cmd);
2494 }
2495 
aac_synchronize(struct scsi_cmnd * scsicmd)2496 static int aac_synchronize(struct scsi_cmnd *scsicmd)
2497 {
2498 	int status;
2499 	struct fib *cmd_fibcontext;
2500 	struct aac_synchronize *synchronizecmd;
2501 	struct scsi_cmnd *cmd;
2502 	struct scsi_device *sdev = scsicmd->device;
2503 	int active = 0;
2504 	struct aac_dev *aac;
2505 	u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
2506 		(scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2507 	u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2508 	unsigned long flags;
2509 
2510 	/*
2511 	 * Wait for all outstanding queued commands to complete to this
2512 	 * specific target (block).
2513 	 */
2514 	spin_lock_irqsave(&sdev->list_lock, flags);
2515 	list_for_each_entry(cmd, &sdev->cmd_list, list)
2516 		if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
2517 			u64 cmnd_lba;
2518 			u32 cmnd_count;
2519 
2520 			if (cmd->cmnd[0] == WRITE_6) {
2521 				cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
2522 					(cmd->cmnd[2] << 8) |
2523 					cmd->cmnd[3];
2524 				cmnd_count = cmd->cmnd[4];
2525 				if (cmnd_count == 0)
2526 					cmnd_count = 256;
2527 			} else if (cmd->cmnd[0] == WRITE_16) {
2528 				cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
2529 					((u64)cmd->cmnd[3] << 48) |
2530 					((u64)cmd->cmnd[4] << 40) |
2531 					((u64)cmd->cmnd[5] << 32) |
2532 					((u64)cmd->cmnd[6] << 24) |
2533 					(cmd->cmnd[7] << 16) |
2534 					(cmd->cmnd[8] << 8) |
2535 					cmd->cmnd[9];
2536 				cmnd_count = (cmd->cmnd[10] << 24) |
2537 					(cmd->cmnd[11] << 16) |
2538 					(cmd->cmnd[12] << 8) |
2539 					cmd->cmnd[13];
2540 			} else if (cmd->cmnd[0] == WRITE_12) {
2541 				cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2542 					(cmd->cmnd[3] << 16) |
2543 					(cmd->cmnd[4] << 8) |
2544 					cmd->cmnd[5];
2545 				cmnd_count = (cmd->cmnd[6] << 24) |
2546 					(cmd->cmnd[7] << 16) |
2547 					(cmd->cmnd[8] << 8) |
2548 					cmd->cmnd[9];
2549 			} else if (cmd->cmnd[0] == WRITE_10) {
2550 				cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2551 					(cmd->cmnd[3] << 16) |
2552 					(cmd->cmnd[4] << 8) |
2553 					cmd->cmnd[5];
2554 				cmnd_count = (cmd->cmnd[7] << 8) |
2555 					cmd->cmnd[8];
2556 			} else
2557 				continue;
2558 			if (((cmnd_lba + cmnd_count) < lba) ||
2559 			  (count && ((lba + count) < cmnd_lba)))
2560 				continue;
2561 			++active;
2562 			break;
2563 		}
2564 
2565 	spin_unlock_irqrestore(&sdev->list_lock, flags);
2566 
2567 	/*
2568 	 *	Yield the processor (requeue for later)
2569 	 */
2570 	if (active)
2571 		return SCSI_MLQUEUE_DEVICE_BUSY;
2572 
2573 	aac = (struct aac_dev *)sdev->host->hostdata;
2574 	if (aac->in_reset)
2575 		return SCSI_MLQUEUE_HOST_BUSY;
2576 
2577 	/*
2578 	 *	Allocate and initialize a Fib
2579 	 */
2580 	if (!(cmd_fibcontext = aac_fib_alloc(aac)))
2581 		return SCSI_MLQUEUE_HOST_BUSY;
2582 
2583 	aac_fib_init(cmd_fibcontext);
2584 
2585 	synchronizecmd = fib_data(cmd_fibcontext);
2586 	synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
2587 	synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
2588 	synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
2589 	synchronizecmd->count =
2590 	     cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
2591 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2592 
2593 	/*
2594 	 *	Now send the Fib to the adapter
2595 	 */
2596 	status = aac_fib_send(ContainerCommand,
2597 		  cmd_fibcontext,
2598 		  sizeof(struct aac_synchronize),
2599 		  FsaNormal,
2600 		  0, 1,
2601 		  (fib_callback)synchronize_callback,
2602 		  (void *)scsicmd);
2603 
2604 	/*
2605 	 *	Check that the command queued to the controller
2606 	 */
2607 	if (status == -EINPROGRESS)
2608 		return 0;
2609 
2610 	printk(KERN_WARNING
2611 		"aac_synchronize: aac_fib_send failed with status: %d.\n", status);
2612 	aac_fib_complete(cmd_fibcontext);
2613 	aac_fib_free(cmd_fibcontext);
2614 	return SCSI_MLQUEUE_HOST_BUSY;
2615 }
2616 
aac_start_stop_callback(void * context,struct fib * fibptr)2617 static void aac_start_stop_callback(void *context, struct fib *fibptr)
2618 {
2619 	struct scsi_cmnd *scsicmd = context;
2620 
2621 	if (!aac_valid_context(scsicmd, fibptr))
2622 		return;
2623 
2624 	BUG_ON(fibptr == NULL);
2625 
2626 	scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2627 
2628 	aac_fib_complete(fibptr);
2629 	aac_fib_free(fibptr);
2630 	scsicmd->scsi_done(scsicmd);
2631 }
2632 
aac_start_stop(struct scsi_cmnd * scsicmd)2633 static int aac_start_stop(struct scsi_cmnd *scsicmd)
2634 {
2635 	int status;
2636 	struct fib *cmd_fibcontext;
2637 	struct aac_power_management *pmcmd;
2638 	struct scsi_device *sdev = scsicmd->device;
2639 	struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2640 
2641 	if (!(aac->supplement_adapter_info.supported_options2 &
2642 	      AAC_OPTION_POWER_MANAGEMENT)) {
2643 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2644 				  SAM_STAT_GOOD;
2645 		scsicmd->scsi_done(scsicmd);
2646 		return 0;
2647 	}
2648 
2649 	if (aac->in_reset)
2650 		return SCSI_MLQUEUE_HOST_BUSY;
2651 
2652 	/*
2653 	 *	Allocate and initialize a Fib
2654 	 */
2655 	cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
2656 
2657 	aac_fib_init(cmd_fibcontext);
2658 
2659 	pmcmd = fib_data(cmd_fibcontext);
2660 	pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2661 	pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2662 	/* Eject bit ignored, not relevant */
2663 	pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2664 		cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2665 	pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2666 	pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2667 		cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2668 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2669 
2670 	/*
2671 	 *	Now send the Fib to the adapter
2672 	 */
2673 	status = aac_fib_send(ContainerCommand,
2674 		  cmd_fibcontext,
2675 		  sizeof(struct aac_power_management),
2676 		  FsaNormal,
2677 		  0, 1,
2678 		  (fib_callback)aac_start_stop_callback,
2679 		  (void *)scsicmd);
2680 
2681 	/*
2682 	 *	Check that the command queued to the controller
2683 	 */
2684 	if (status == -EINPROGRESS)
2685 		return 0;
2686 
2687 	aac_fib_complete(cmd_fibcontext);
2688 	aac_fib_free(cmd_fibcontext);
2689 	return SCSI_MLQUEUE_HOST_BUSY;
2690 }
2691 
2692 /**
2693  *	aac_scsi_cmd()		-	Process SCSI command
2694  *	@scsicmd:		SCSI command block
2695  *
2696  *	Emulate a SCSI command and queue the required request for the
2697  *	aacraid firmware.
2698  */
2699 
aac_scsi_cmd(struct scsi_cmnd * scsicmd)2700 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2701 {
2702 	u32 cid, bus;
2703 	struct Scsi_Host *host = scsicmd->device->host;
2704 	struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2705 	struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
2706 
2707 	if (fsa_dev_ptr == NULL)
2708 		return -1;
2709 	/*
2710 	 *	If the bus, id or lun is out of range, return fail
2711 	 *	Test does not apply to ID 16, the pseudo id for the controller
2712 	 *	itself.
2713 	 */
2714 	cid = scmd_id(scsicmd);
2715 	if (cid != host->this_id) {
2716 		if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2717 			if((cid >= dev->maximum_num_containers) ||
2718 					(scsicmd->device->lun != 0)) {
2719 				scsicmd->result = DID_NO_CONNECT << 16;
2720 				goto scsi_done_ret;
2721 			}
2722 
2723 			/*
2724 			 *	If the target container doesn't exist, it may have
2725 			 *	been newly created
2726 			 */
2727 			if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2728 			  (fsa_dev_ptr[cid].sense_data.sense_key ==
2729 			   NOT_READY)) {
2730 				switch (scsicmd->cmnd[0]) {
2731 				case SERVICE_ACTION_IN_16:
2732 					if (!(dev->raw_io_interface) ||
2733 					    !(dev->raw_io_64) ||
2734 					    ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2735 						break;
2736 				case INQUIRY:
2737 				case READ_CAPACITY:
2738 				case TEST_UNIT_READY:
2739 					if (dev->in_reset)
2740 						return -1;
2741 					return _aac_probe_container(scsicmd,
2742 							aac_probe_container_callback2);
2743 				default:
2744 					break;
2745 				}
2746 			}
2747 		} else {  /* check for physical non-dasd devices */
2748 			bus = aac_logical_to_phys(scmd_channel(scsicmd));
2749 			if (bus < AAC_MAX_BUSES && cid < AAC_MAX_TARGETS &&
2750 				(dev->hba_map[bus][cid].expose
2751 						== AAC_HIDE_DISK)){
2752 				if (scsicmd->cmnd[0] == INQUIRY) {
2753 					scsicmd->result = DID_NO_CONNECT << 16;
2754 					goto scsi_done_ret;
2755 				}
2756 			}
2757 
2758 			if (bus < AAC_MAX_BUSES && cid < AAC_MAX_TARGETS &&
2759 				dev->hba_map[bus][cid].devtype
2760 					== AAC_DEVTYPE_NATIVE_RAW) {
2761 				if (dev->in_reset)
2762 					return -1;
2763 				return aac_send_hba_fib(scsicmd);
2764 			} else if (dev->nondasd_support || expose_physicals ||
2765 				dev->jbod) {
2766 				if (dev->in_reset)
2767 					return -1;
2768 				return aac_send_srb_fib(scsicmd);
2769 			} else {
2770 				scsicmd->result = DID_NO_CONNECT << 16;
2771 				goto scsi_done_ret;
2772 			}
2773 		}
2774 	}
2775 	/*
2776 	 * else Command for the controller itself
2777 	 */
2778 	else if ((scsicmd->cmnd[0] != INQUIRY) &&	/* only INQUIRY & TUR cmnd supported for controller */
2779 		(scsicmd->cmnd[0] != TEST_UNIT_READY))
2780 	{
2781 		dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2782 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2783 		set_sense(&dev->fsa_dev[cid].sense_data,
2784 		  ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2785 		  ASENCODE_INVALID_COMMAND, 0, 0);
2786 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2787 		       min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2788 			     SCSI_SENSE_BUFFERSIZE));
2789 		goto scsi_done_ret;
2790 	}
2791 
2792 	switch (scsicmd->cmnd[0]) {
2793 	case READ_6:
2794 	case READ_10:
2795 	case READ_12:
2796 	case READ_16:
2797 		if (dev->in_reset)
2798 			return -1;
2799 		return aac_read(scsicmd);
2800 
2801 	case WRITE_6:
2802 	case WRITE_10:
2803 	case WRITE_12:
2804 	case WRITE_16:
2805 		if (dev->in_reset)
2806 			return -1;
2807 		return aac_write(scsicmd);
2808 
2809 	case SYNCHRONIZE_CACHE:
2810 		if (((aac_cache & 6) == 6) && dev->cache_protected) {
2811 			scsicmd->result = AAC_STAT_GOOD;
2812 			break;
2813 		}
2814 		/* Issue FIB to tell Firmware to flush it's cache */
2815 		if ((aac_cache & 6) != 2)
2816 			return aac_synchronize(scsicmd);
2817 	case INQUIRY:
2818 	{
2819 		struct inquiry_data inq_data;
2820 
2821 		dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
2822 		memset(&inq_data, 0, sizeof (struct inquiry_data));
2823 
2824 		if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
2825 			char *arr = (char *)&inq_data;
2826 
2827 			/* EVPD bit set */
2828 			arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2829 			  INQD_PDT_PROC : INQD_PDT_DA;
2830 			if (scsicmd->cmnd[2] == 0) {
2831 				/* supported vital product data pages */
2832 				arr[3] = 3;
2833 				arr[4] = 0x0;
2834 				arr[5] = 0x80;
2835 				arr[6] = 0x83;
2836 				arr[1] = scsicmd->cmnd[2];
2837 				scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2838 							 sizeof(inq_data));
2839 				scsicmd->result = AAC_STAT_GOOD;
2840 			} else if (scsicmd->cmnd[2] == 0x80) {
2841 				/* unit serial number page */
2842 				arr[3] = setinqserial(dev, &arr[4],
2843 				  scmd_id(scsicmd));
2844 				arr[1] = scsicmd->cmnd[2];
2845 				scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2846 							 sizeof(inq_data));
2847 				if (aac_wwn != 2)
2848 					return aac_get_container_serial(
2849 						scsicmd);
2850 				scsicmd->result = AAC_STAT_GOOD;
2851 			} else if (scsicmd->cmnd[2] == 0x83) {
2852 				/* vpd page 0x83 - Device Identification Page */
2853 				char *sno = (char *)&inq_data;
2854 				sno[3] = setinqserial(dev, &sno[4],
2855 						      scmd_id(scsicmd));
2856 				if (aac_wwn != 2)
2857 					return aac_get_container_serial(
2858 						scsicmd);
2859 				scsicmd->result = AAC_STAT_GOOD;
2860 			} else {
2861 				/* vpd page not implemented */
2862 				scsicmd->result = DID_OK << 16 |
2863 				  COMMAND_COMPLETE << 8 |
2864 				  SAM_STAT_CHECK_CONDITION;
2865 				set_sense(&dev->fsa_dev[cid].sense_data,
2866 				  ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
2867 				  ASENCODE_NO_SENSE, 7, 2);
2868 				memcpy(scsicmd->sense_buffer,
2869 				  &dev->fsa_dev[cid].sense_data,
2870 				  min_t(size_t,
2871 					sizeof(dev->fsa_dev[cid].sense_data),
2872 					SCSI_SENSE_BUFFERSIZE));
2873 			}
2874 			break;
2875 		}
2876 		inq_data.inqd_ver = 2;	/* claim compliance to SCSI-2 */
2877 		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 */
2878 		inq_data.inqd_len = 31;
2879 		/*Format for "pad2" is  RelAdr | WBus32 | WBus16 |  Sync  | Linked |Reserved| CmdQue | SftRe */
2880 		inq_data.inqd_pad2= 0x32 ;	 /*WBus16|Sync|CmdQue */
2881 		/*
2882 		 *	Set the Vendor, Product, and Revision Level
2883 		 *	see: <vendor>.c i.e. aac.c
2884 		 */
2885 		if (cid == host->this_id) {
2886 			setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
2887 			inq_data.inqd_pdt = INQD_PDT_PROC;	/* Processor device */
2888 			scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2889 						 sizeof(inq_data));
2890 			scsicmd->result = AAC_STAT_GOOD;
2891 			break;
2892 		}
2893 		if (dev->in_reset)
2894 			return -1;
2895 		setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
2896 		inq_data.inqd_pdt = INQD_PDT_DA;	/* Direct/random access device */
2897 		scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
2898 		return aac_get_container_name(scsicmd);
2899 	}
2900 	case SERVICE_ACTION_IN_16:
2901 		if (!(dev->raw_io_interface) ||
2902 		    !(dev->raw_io_64) ||
2903 		    ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2904 			break;
2905 	{
2906 		u64 capacity;
2907 		char cp[13];
2908 		unsigned int alloc_len;
2909 
2910 		dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2911 		capacity = fsa_dev_ptr[cid].size - 1;
2912 		cp[0] = (capacity >> 56) & 0xff;
2913 		cp[1] = (capacity >> 48) & 0xff;
2914 		cp[2] = (capacity >> 40) & 0xff;
2915 		cp[3] = (capacity >> 32) & 0xff;
2916 		cp[4] = (capacity >> 24) & 0xff;
2917 		cp[5] = (capacity >> 16) & 0xff;
2918 		cp[6] = (capacity >> 8) & 0xff;
2919 		cp[7] = (capacity >> 0) & 0xff;
2920 		cp[8] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2921 		cp[9] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2922 		cp[10] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2923 		cp[11] = (fsa_dev_ptr[cid].block_size) & 0xff;
2924 		cp[12] = 0;
2925 
2926 		alloc_len = ((scsicmd->cmnd[10] << 24)
2927 			     + (scsicmd->cmnd[11] << 16)
2928 			     + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
2929 
2930 		alloc_len = min_t(size_t, alloc_len, sizeof(cp));
2931 		scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
2932 		if (alloc_len < scsi_bufflen(scsicmd))
2933 			scsi_set_resid(scsicmd,
2934 				       scsi_bufflen(scsicmd) - alloc_len);
2935 
2936 		/* Do not cache partition table for arrays */
2937 		scsicmd->device->removable = 1;
2938 
2939 		scsicmd->result = AAC_STAT_GOOD;
2940 		break;
2941 	}
2942 
2943 	case READ_CAPACITY:
2944 	{
2945 		u32 capacity;
2946 		char cp[8];
2947 
2948 		dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
2949 		if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2950 			capacity = fsa_dev_ptr[cid].size - 1;
2951 		else
2952 			capacity = (u32)-1;
2953 
2954 		cp[0] = (capacity >> 24) & 0xff;
2955 		cp[1] = (capacity >> 16) & 0xff;
2956 		cp[2] = (capacity >> 8) & 0xff;
2957 		cp[3] = (capacity >> 0) & 0xff;
2958 		cp[4] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2959 		cp[5] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2960 		cp[6] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2961 		cp[7] = (fsa_dev_ptr[cid].block_size) & 0xff;
2962 		scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
2963 		/* Do not cache partition table for arrays */
2964 		scsicmd->device->removable = 1;
2965 		scsicmd->result = AAC_STAT_GOOD;
2966 		break;
2967 	}
2968 
2969 	case MODE_SENSE:
2970 	{
2971 		int mode_buf_length = 4;
2972 		u32 capacity;
2973 		aac_modep_data mpd;
2974 
2975 		if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2976 			capacity = fsa_dev_ptr[cid].size - 1;
2977 		else
2978 			capacity = (u32)-1;
2979 
2980 		dprintk((KERN_DEBUG "MODE SENSE command.\n"));
2981 		memset((char *)&mpd, 0, sizeof(aac_modep_data));
2982 
2983 		/* Mode data length */
2984 		mpd.hd.data_length = sizeof(mpd.hd) - 1;
2985 		/* Medium type - default */
2986 		mpd.hd.med_type = 0;
2987 		/* Device-specific param,
2988 		   bit 8: 0/1 = write enabled/protected
2989 		   bit 4: 0/1 = FUA enabled */
2990 		mpd.hd.dev_par = 0;
2991 
2992 		if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2993 			mpd.hd.dev_par = 0x10;
2994 		if (scsicmd->cmnd[1] & 0x8)
2995 			mpd.hd.bd_length = 0;	/* Block descriptor length */
2996 		else {
2997 			mpd.hd.bd_length = sizeof(mpd.bd);
2998 			mpd.hd.data_length += mpd.hd.bd_length;
2999 			mpd.bd.block_length[0] =
3000 				(fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3001 			mpd.bd.block_length[1] =
3002 				(fsa_dev_ptr[cid].block_size >> 8) &  0xff;
3003 			mpd.bd.block_length[2] =
3004 				fsa_dev_ptr[cid].block_size  & 0xff;
3005 
3006 			mpd.mpc_buf[0] = scsicmd->cmnd[2];
3007 			if (scsicmd->cmnd[2] == 0x1C) {
3008 				/* page length */
3009 				mpd.mpc_buf[1] = 0xa;
3010 				/* Mode data length */
3011 				mpd.hd.data_length = 23;
3012 			} else {
3013 				/* Mode data length */
3014 				mpd.hd.data_length = 15;
3015 			}
3016 
3017 			if (capacity > 0xffffff) {
3018 				mpd.bd.block_count[0] = 0xff;
3019 				mpd.bd.block_count[1] = 0xff;
3020 				mpd.bd.block_count[2] = 0xff;
3021 			} else {
3022 				mpd.bd.block_count[0] = (capacity >> 16) & 0xff;
3023 				mpd.bd.block_count[1] = (capacity >> 8) & 0xff;
3024 				mpd.bd.block_count[2] = capacity  & 0xff;
3025 			}
3026 		}
3027 		if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3028 		  ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
3029 			mpd.hd.data_length += 3;
3030 			mpd.mpc_buf[0] = 8;
3031 			mpd.mpc_buf[1] = 1;
3032 			mpd.mpc_buf[2] = ((aac_cache & 6) == 2)
3033 				? 0 : 0x04; /* WCE */
3034 			mode_buf_length = sizeof(mpd);
3035 		}
3036 
3037 		if (mode_buf_length > scsicmd->cmnd[4])
3038 			mode_buf_length = scsicmd->cmnd[4];
3039 		else
3040 			mode_buf_length = sizeof(mpd);
3041 		scsi_sg_copy_from_buffer(scsicmd,
3042 					 (char *)&mpd,
3043 					 mode_buf_length);
3044 		scsicmd->result = AAC_STAT_GOOD;
3045 		break;
3046 	}
3047 	case MODE_SENSE_10:
3048 	{
3049 		u32 capacity;
3050 		int mode_buf_length = 8;
3051 		aac_modep10_data mpd10;
3052 
3053 		if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3054 			capacity = fsa_dev_ptr[cid].size - 1;
3055 		else
3056 			capacity = (u32)-1;
3057 
3058 		dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
3059 		memset((char *)&mpd10, 0, sizeof(aac_modep10_data));
3060 		/* Mode data length (MSB) */
3061 		mpd10.hd.data_length[0] = 0;
3062 		/* Mode data length (LSB) */
3063 		mpd10.hd.data_length[1] = sizeof(mpd10.hd) - 1;
3064 		/* Medium type - default */
3065 		mpd10.hd.med_type = 0;
3066 		/* Device-specific param,
3067 		   bit 8: 0/1 = write enabled/protected
3068 		   bit 4: 0/1 = FUA enabled */
3069 		mpd10.hd.dev_par = 0;
3070 
3071 		if (dev->raw_io_interface && ((aac_cache & 5) != 1))
3072 			mpd10.hd.dev_par = 0x10;
3073 		mpd10.hd.rsrvd[0] = 0;	/* reserved */
3074 		mpd10.hd.rsrvd[1] = 0;	/* reserved */
3075 		if (scsicmd->cmnd[1] & 0x8) {
3076 			/* Block descriptor length (MSB) */
3077 			mpd10.hd.bd_length[0] = 0;
3078 			/* Block descriptor length (LSB) */
3079 			mpd10.hd.bd_length[1] = 0;
3080 		} else {
3081 			mpd10.hd.bd_length[0] = 0;
3082 			mpd10.hd.bd_length[1] = sizeof(mpd10.bd);
3083 
3084 			mpd10.hd.data_length[1] += mpd10.hd.bd_length[1];
3085 
3086 			mpd10.bd.block_length[0] =
3087 				(fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3088 			mpd10.bd.block_length[1] =
3089 				(fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3090 			mpd10.bd.block_length[2] =
3091 				fsa_dev_ptr[cid].block_size  & 0xff;
3092 
3093 			if (capacity > 0xffffff) {
3094 				mpd10.bd.block_count[0] = 0xff;
3095 				mpd10.bd.block_count[1] = 0xff;
3096 				mpd10.bd.block_count[2] = 0xff;
3097 			} else {
3098 				mpd10.bd.block_count[0] =
3099 					(capacity >> 16) & 0xff;
3100 				mpd10.bd.block_count[1] =
3101 					(capacity >> 8) & 0xff;
3102 				mpd10.bd.block_count[2] =
3103 					capacity  & 0xff;
3104 			}
3105 		}
3106 		if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3107 		  ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
3108 			mpd10.hd.data_length[1] += 3;
3109 			mpd10.mpc_buf[0] = 8;
3110 			mpd10.mpc_buf[1] = 1;
3111 			mpd10.mpc_buf[2] = ((aac_cache & 6) == 2)
3112 				? 0 : 0x04; /* WCE */
3113 			mode_buf_length = sizeof(mpd10);
3114 			if (mode_buf_length > scsicmd->cmnd[8])
3115 				mode_buf_length = scsicmd->cmnd[8];
3116 		}
3117 		scsi_sg_copy_from_buffer(scsicmd,
3118 					 (char *)&mpd10,
3119 					 mode_buf_length);
3120 
3121 		scsicmd->result = AAC_STAT_GOOD;
3122 		break;
3123 	}
3124 	case REQUEST_SENSE:
3125 		dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
3126 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3127 				sizeof(struct sense_data));
3128 		memset(&dev->fsa_dev[cid].sense_data, 0,
3129 				sizeof(struct sense_data));
3130 		scsicmd->result = AAC_STAT_GOOD;
3131 		break;
3132 
3133 	case ALLOW_MEDIUM_REMOVAL:
3134 		dprintk((KERN_DEBUG "LOCK command.\n"));
3135 		if (scsicmd->cmnd[4])
3136 			fsa_dev_ptr[cid].locked = 1;
3137 		else
3138 			fsa_dev_ptr[cid].locked = 0;
3139 
3140 		scsicmd->result = AAC_STAT_GOOD;
3141 		break;
3142 	/*
3143 	 *	These commands are all No-Ops
3144 	 */
3145 	case TEST_UNIT_READY:
3146 		if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
3147 			scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3148 				SAM_STAT_CHECK_CONDITION;
3149 			set_sense(&dev->fsa_dev[cid].sense_data,
3150 				  NOT_READY, SENCODE_BECOMING_READY,
3151 				  ASENCODE_BECOMING_READY, 0, 0);
3152 			memcpy(scsicmd->sense_buffer,
3153 			       &dev->fsa_dev[cid].sense_data,
3154 			       min_t(size_t,
3155 				     sizeof(dev->fsa_dev[cid].sense_data),
3156 				     SCSI_SENSE_BUFFERSIZE));
3157 		break;
3158 		}
3159 	case RESERVE:
3160 	case RELEASE:
3161 	case REZERO_UNIT:
3162 	case REASSIGN_BLOCKS:
3163 	case SEEK_10:
3164 		scsicmd->result = AAC_STAT_GOOD;
3165 		break;
3166 
3167 	case START_STOP:
3168 		return aac_start_stop(scsicmd);
3169 
3170 	/* FALLTHRU */
3171 	default:
3172 	/*
3173 	 *	Unhandled commands
3174 	 */
3175 		dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n",
3176 				scsicmd->cmnd[0]));
3177 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3178 				SAM_STAT_CHECK_CONDITION;
3179 		set_sense(&dev->fsa_dev[cid].sense_data,
3180 			  ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
3181 			  ASENCODE_INVALID_COMMAND, 0, 0);
3182 		memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3183 				min_t(size_t,
3184 				      sizeof(dev->fsa_dev[cid].sense_data),
3185 				      SCSI_SENSE_BUFFERSIZE));
3186 	}
3187 
3188 scsi_done_ret:
3189 
3190 	scsicmd->scsi_done(scsicmd);
3191 	return 0;
3192 }
3193 
query_disk(struct aac_dev * dev,void __user * arg)3194 static int query_disk(struct aac_dev *dev, void __user *arg)
3195 {
3196 	struct aac_query_disk qd;
3197 	struct fsa_dev_info *fsa_dev_ptr;
3198 
3199 	fsa_dev_ptr = dev->fsa_dev;
3200 	if (!fsa_dev_ptr)
3201 		return -EBUSY;
3202 	if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
3203 		return -EFAULT;
3204 	if (qd.cnum == -1) {
3205 		if (qd.id < 0 || qd.id >= dev->maximum_num_containers)
3206 			return -EINVAL;
3207 		qd.cnum = qd.id;
3208 	} else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1)) {
3209 		if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
3210 			return -EINVAL;
3211 		qd.instance = dev->scsi_host_ptr->host_no;
3212 		qd.bus = 0;
3213 		qd.id = CONTAINER_TO_ID(qd.cnum);
3214 		qd.lun = CONTAINER_TO_LUN(qd.cnum);
3215 	}
3216 	else return -EINVAL;
3217 
3218 	qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
3219 	qd.locked = fsa_dev_ptr[qd.cnum].locked;
3220 	qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
3221 
3222 	if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
3223 		qd.unmapped = 1;
3224 	else
3225 		qd.unmapped = 0;
3226 
3227 	strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
3228 	  min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
3229 
3230 	if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
3231 		return -EFAULT;
3232 	return 0;
3233 }
3234 
force_delete_disk(struct aac_dev * dev,void __user * arg)3235 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
3236 {
3237 	struct aac_delete_disk dd;
3238 	struct fsa_dev_info *fsa_dev_ptr;
3239 
3240 	fsa_dev_ptr = dev->fsa_dev;
3241 	if (!fsa_dev_ptr)
3242 		return -EBUSY;
3243 
3244 	if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3245 		return -EFAULT;
3246 
3247 	if (dd.cnum >= dev->maximum_num_containers)
3248 		return -EINVAL;
3249 	/*
3250 	 *	Mark this container as being deleted.
3251 	 */
3252 	fsa_dev_ptr[dd.cnum].deleted = 1;
3253 	/*
3254 	 *	Mark the container as no longer valid
3255 	 */
3256 	fsa_dev_ptr[dd.cnum].valid = 0;
3257 	return 0;
3258 }
3259 
delete_disk(struct aac_dev * dev,void __user * arg)3260 static int delete_disk(struct aac_dev *dev, void __user *arg)
3261 {
3262 	struct aac_delete_disk dd;
3263 	struct fsa_dev_info *fsa_dev_ptr;
3264 
3265 	fsa_dev_ptr = dev->fsa_dev;
3266 	if (!fsa_dev_ptr)
3267 		return -EBUSY;
3268 
3269 	if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3270 		return -EFAULT;
3271 
3272 	if (dd.cnum >= dev->maximum_num_containers)
3273 		return -EINVAL;
3274 	/*
3275 	 *	If the container is locked, it can not be deleted by the API.
3276 	 */
3277 	if (fsa_dev_ptr[dd.cnum].locked)
3278 		return -EBUSY;
3279 	else {
3280 		/*
3281 		 *	Mark the container as no longer being valid.
3282 		 */
3283 		fsa_dev_ptr[dd.cnum].valid = 0;
3284 		fsa_dev_ptr[dd.cnum].devname[0] = '\0';
3285 		return 0;
3286 	}
3287 }
3288 
aac_dev_ioctl(struct aac_dev * dev,int cmd,void __user * arg)3289 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
3290 {
3291 	switch (cmd) {
3292 	case FSACTL_QUERY_DISK:
3293 		return query_disk(dev, arg);
3294 	case FSACTL_DELETE_DISK:
3295 		return delete_disk(dev, arg);
3296 	case FSACTL_FORCE_DELETE_DISK:
3297 		return force_delete_disk(dev, arg);
3298 	case FSACTL_GET_CONTAINERS:
3299 		return aac_get_containers(dev);
3300 	default:
3301 		return -ENOTTY;
3302 	}
3303 }
3304 
3305 /**
3306  *
3307  * aac_srb_callback
3308  * @context: the context set in the fib - here it is scsi cmd
3309  * @fibptr: pointer to the fib
3310  *
3311  * Handles the completion of a scsi command to a non dasd device
3312  *
3313  */
3314 
aac_srb_callback(void * context,struct fib * fibptr)3315 static void aac_srb_callback(void *context, struct fib * fibptr)
3316 {
3317 	struct aac_dev *dev;
3318 	struct aac_srb_reply *srbreply;
3319 	struct scsi_cmnd *scsicmd;
3320 
3321 	scsicmd = (struct scsi_cmnd *) context;
3322 
3323 	if (!aac_valid_context(scsicmd, fibptr))
3324 		return;
3325 
3326 	BUG_ON(fibptr == NULL);
3327 
3328 	dev = fibptr->dev;
3329 
3330 	srbreply = (struct aac_srb_reply *) fib_data(fibptr);
3331 
3332 	scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
3333 
3334 	if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3335 		/* fast response */
3336 		srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
3337 		srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
3338 	} else {
3339 		/*
3340 		 *	Calculate resid for sg
3341 		 */
3342 		scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
3343 				   - le32_to_cpu(srbreply->data_xfer_length));
3344 	}
3345 
3346 
3347 	scsi_dma_unmap(scsicmd);
3348 
3349 	/* expose physical device if expose_physicald flag is on */
3350 	if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
3351 	  && expose_physicals > 0)
3352 		aac_expose_phy_device(scsicmd);
3353 
3354 	/*
3355 	 * First check the fib status
3356 	 */
3357 
3358 	if (le32_to_cpu(srbreply->status) != ST_OK) {
3359 		int len;
3360 
3361 		pr_warn("aac_srb_callback: srb failed, status = %d\n",
3362 				le32_to_cpu(srbreply->status));
3363 		len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3364 			    SCSI_SENSE_BUFFERSIZE);
3365 		scsicmd->result = DID_ERROR << 16
3366 				| COMMAND_COMPLETE << 8
3367 				| SAM_STAT_CHECK_CONDITION;
3368 		memcpy(scsicmd->sense_buffer,
3369 				srbreply->sense_data, len);
3370 	}
3371 
3372 	/*
3373 	 * Next check the srb status
3374 	 */
3375 	switch ((le32_to_cpu(srbreply->srb_status))&0x3f) {
3376 	case SRB_STATUS_ERROR_RECOVERY:
3377 	case SRB_STATUS_PENDING:
3378 	case SRB_STATUS_SUCCESS:
3379 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3380 		break;
3381 	case SRB_STATUS_DATA_OVERRUN:
3382 		switch (scsicmd->cmnd[0]) {
3383 		case  READ_6:
3384 		case  WRITE_6:
3385 		case  READ_10:
3386 		case  WRITE_10:
3387 		case  READ_12:
3388 		case  WRITE_12:
3389 		case  READ_16:
3390 		case  WRITE_16:
3391 			if (le32_to_cpu(srbreply->data_xfer_length)
3392 						< scsicmd->underflow)
3393 				pr_warn("aacraid: SCSI CMD underflow\n");
3394 			else
3395 				pr_warn("aacraid: SCSI CMD Data Overrun\n");
3396 			scsicmd->result = DID_ERROR << 16
3397 					| COMMAND_COMPLETE << 8;
3398 			break;
3399 		case INQUIRY:
3400 			scsicmd->result = DID_OK << 16
3401 					| COMMAND_COMPLETE << 8;
3402 			break;
3403 		default:
3404 			scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3405 			break;
3406 		}
3407 		break;
3408 	case SRB_STATUS_ABORTED:
3409 		scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3410 		break;
3411 	case SRB_STATUS_ABORT_FAILED:
3412 		/*
3413 		 * Not sure about this one - but assuming the
3414 		 * hba was trying to abort for some reason
3415 		 */
3416 		scsicmd->result = DID_ERROR << 16 | ABORT << 8;
3417 		break;
3418 	case SRB_STATUS_PARITY_ERROR:
3419 		scsicmd->result = DID_PARITY << 16
3420 				| MSG_PARITY_ERROR << 8;
3421 		break;
3422 	case SRB_STATUS_NO_DEVICE:
3423 	case SRB_STATUS_INVALID_PATH_ID:
3424 	case SRB_STATUS_INVALID_TARGET_ID:
3425 	case SRB_STATUS_INVALID_LUN:
3426 	case SRB_STATUS_SELECTION_TIMEOUT:
3427 		scsicmd->result = DID_NO_CONNECT << 16
3428 				| COMMAND_COMPLETE << 8;
3429 		break;
3430 
3431 	case SRB_STATUS_COMMAND_TIMEOUT:
3432 	case SRB_STATUS_TIMEOUT:
3433 		scsicmd->result = DID_TIME_OUT << 16
3434 				| COMMAND_COMPLETE << 8;
3435 		break;
3436 
3437 	case SRB_STATUS_BUSY:
3438 		scsicmd->result = DID_BUS_BUSY << 16
3439 				| COMMAND_COMPLETE << 8;
3440 		break;
3441 
3442 	case SRB_STATUS_BUS_RESET:
3443 		scsicmd->result = DID_RESET << 16
3444 				| COMMAND_COMPLETE << 8;
3445 		break;
3446 
3447 	case SRB_STATUS_MESSAGE_REJECTED:
3448 		scsicmd->result = DID_ERROR << 16
3449 				| MESSAGE_REJECT << 8;
3450 		break;
3451 	case SRB_STATUS_REQUEST_FLUSHED:
3452 	case SRB_STATUS_ERROR:
3453 	case SRB_STATUS_INVALID_REQUEST:
3454 	case SRB_STATUS_REQUEST_SENSE_FAILED:
3455 	case SRB_STATUS_NO_HBA:
3456 	case SRB_STATUS_UNEXPECTED_BUS_FREE:
3457 	case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
3458 	case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
3459 	case SRB_STATUS_DELAYED_RETRY:
3460 	case SRB_STATUS_BAD_FUNCTION:
3461 	case SRB_STATUS_NOT_STARTED:
3462 	case SRB_STATUS_NOT_IN_USE:
3463 	case SRB_STATUS_FORCE_ABORT:
3464 	case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
3465 	default:
3466 #ifdef AAC_DETAILED_STATUS_INFO
3467 		pr_info("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x -scsi status 0x%x\n",
3468 			le32_to_cpu(srbreply->srb_status) & 0x3F,
3469 			aac_get_status_string(
3470 				le32_to_cpu(srbreply->srb_status) & 0x3F),
3471 			scsicmd->cmnd[0],
3472 			le32_to_cpu(srbreply->scsi_status));
3473 #endif
3474 		/*
3475 		 * When the CC bit is SET by the host in ATA pass thru CDB,
3476 		 *  driver is supposed to return DID_OK
3477 		 *
3478 		 * When the CC bit is RESET by the host, driver should
3479 		 *  return DID_ERROR
3480 		 */
3481 		if ((scsicmd->cmnd[0] == ATA_12)
3482 			|| (scsicmd->cmnd[0] == ATA_16)) {
3483 
3484 			if (scsicmd->cmnd[2] & (0x01 << 5)) {
3485 				scsicmd->result = DID_OK << 16
3486 					| COMMAND_COMPLETE << 8;
3487 			break;
3488 			} else {
3489 				scsicmd->result = DID_ERROR << 16
3490 					| COMMAND_COMPLETE << 8;
3491 			break;
3492 			}
3493 		} else {
3494 			scsicmd->result = DID_ERROR << 16
3495 				| COMMAND_COMPLETE << 8;
3496 			break;
3497 		}
3498 	}
3499 	if (le32_to_cpu(srbreply->scsi_status)
3500 			== SAM_STAT_CHECK_CONDITION) {
3501 		int len;
3502 
3503 		scsicmd->result |= SAM_STAT_CHECK_CONDITION;
3504 		len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3505 			    SCSI_SENSE_BUFFERSIZE);
3506 #ifdef AAC_DETAILED_STATUS_INFO
3507 		pr_warn("aac_srb_callback: check condition, status = %d len=%d\n",
3508 					le32_to_cpu(srbreply->status), len);
3509 #endif
3510 		memcpy(scsicmd->sense_buffer,
3511 				srbreply->sense_data, len);
3512 	}
3513 
3514 	/*
3515 	 * OR in the scsi status (already shifted up a bit)
3516 	 */
3517 	scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
3518 
3519 	aac_fib_complete(fibptr);
3520 	scsicmd->scsi_done(scsicmd);
3521 }
3522 
hba_resp_task_complete(struct aac_dev * dev,struct scsi_cmnd * scsicmd,struct aac_hba_resp * err)3523 static void hba_resp_task_complete(struct aac_dev *dev,
3524 					struct scsi_cmnd *scsicmd,
3525 					struct aac_hba_resp *err) {
3526 
3527 	scsicmd->result = err->status;
3528 	/* set residual count */
3529 	scsi_set_resid(scsicmd, le32_to_cpu(err->residual_count));
3530 
3531 	switch (err->status) {
3532 	case SAM_STAT_GOOD:
3533 		scsicmd->result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
3534 		break;
3535 	case SAM_STAT_CHECK_CONDITION:
3536 	{
3537 		int len;
3538 
3539 		len = min_t(u8, err->sense_response_data_len,
3540 			SCSI_SENSE_BUFFERSIZE);
3541 		if (len)
3542 			memcpy(scsicmd->sense_buffer,
3543 				err->sense_response_buf, len);
3544 		scsicmd->result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
3545 		break;
3546 	}
3547 	case SAM_STAT_BUSY:
3548 		scsicmd->result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
3549 		break;
3550 	case SAM_STAT_TASK_ABORTED:
3551 		scsicmd->result |= DID_ABORT << 16 | ABORT << 8;
3552 		break;
3553 	case SAM_STAT_RESERVATION_CONFLICT:
3554 	case SAM_STAT_TASK_SET_FULL:
3555 	default:
3556 		scsicmd->result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3557 		break;
3558 	}
3559 }
3560 
hba_resp_task_failure(struct aac_dev * dev,struct scsi_cmnd * scsicmd,struct aac_hba_resp * err)3561 static void hba_resp_task_failure(struct aac_dev *dev,
3562 					struct scsi_cmnd *scsicmd,
3563 					struct aac_hba_resp *err)
3564 {
3565 	switch (err->status) {
3566 	case HBA_RESP_STAT_HBAMODE_DISABLED:
3567 	{
3568 		u32 bus, cid;
3569 
3570 		bus = aac_logical_to_phys(scmd_channel(scsicmd));
3571 		cid = scmd_id(scsicmd);
3572 		if (dev->hba_map[bus][cid].devtype == AAC_DEVTYPE_NATIVE_RAW) {
3573 			dev->hba_map[bus][cid].devtype = AAC_DEVTYPE_ARC_RAW;
3574 			dev->hba_map[bus][cid].rmw_nexus = 0xffffffff;
3575 		}
3576 		scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3577 		break;
3578 	}
3579 	case HBA_RESP_STAT_IO_ERROR:
3580 	case HBA_RESP_STAT_NO_PATH_TO_DEVICE:
3581 		scsicmd->result = DID_OK << 16 |
3582 			COMMAND_COMPLETE << 8 | SAM_STAT_BUSY;
3583 		break;
3584 	case HBA_RESP_STAT_IO_ABORTED:
3585 		scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3586 		break;
3587 	case HBA_RESP_STAT_INVALID_DEVICE:
3588 		scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3589 		break;
3590 	case HBA_RESP_STAT_UNDERRUN:
3591 		/* UNDERRUN is OK */
3592 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3593 		break;
3594 	case HBA_RESP_STAT_OVERRUN:
3595 	default:
3596 		scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3597 		break;
3598 	}
3599 }
3600 
3601 /**
3602  *
3603  * aac_hba_callback
3604  * @context: the context set in the fib - here it is scsi cmd
3605  * @fibptr: pointer to the fib
3606  *
3607  * Handles the completion of a native HBA scsi command
3608  *
3609  */
aac_hba_callback(void * context,struct fib * fibptr)3610 void aac_hba_callback(void *context, struct fib *fibptr)
3611 {
3612 	struct aac_dev *dev;
3613 	struct scsi_cmnd *scsicmd;
3614 
3615 	struct aac_hba_resp *err =
3616 			&((struct aac_native_hba *)fibptr->hw_fib_va)->resp.err;
3617 
3618 	scsicmd = (struct scsi_cmnd *) context;
3619 
3620 	if (!aac_valid_context(scsicmd, fibptr))
3621 		return;
3622 
3623 	WARN_ON(fibptr == NULL);
3624 	dev = fibptr->dev;
3625 
3626 	if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF))
3627 		scsi_dma_unmap(scsicmd);
3628 
3629 	if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3630 		/* fast response */
3631 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3632 		goto out;
3633 	}
3634 
3635 	switch (err->service_response) {
3636 	case HBA_RESP_SVCRES_TASK_COMPLETE:
3637 		hba_resp_task_complete(dev, scsicmd, err);
3638 		break;
3639 	case HBA_RESP_SVCRES_FAILURE:
3640 		hba_resp_task_failure(dev, scsicmd, err);
3641 		break;
3642 	case HBA_RESP_SVCRES_TMF_REJECTED:
3643 		scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
3644 		break;
3645 	case HBA_RESP_SVCRES_TMF_LUN_INVALID:
3646 		scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3647 		break;
3648 	case HBA_RESP_SVCRES_TMF_COMPLETE:
3649 	case HBA_RESP_SVCRES_TMF_SUCCEEDED:
3650 		scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3651 		break;
3652 	default:
3653 		scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3654 		break;
3655 	}
3656 
3657 out:
3658 	aac_fib_complete(fibptr);
3659 
3660 	if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF)
3661 		scsicmd->SCp.sent_command = 1;
3662 	else
3663 		scsicmd->scsi_done(scsicmd);
3664 }
3665 
3666 /**
3667  *
3668  * aac_send_srb_fib
3669  * @scsicmd: the scsi command block
3670  *
3671  * This routine will form a FIB and fill in the aac_srb from the
3672  * scsicmd passed in.
3673  */
3674 
aac_send_srb_fib(struct scsi_cmnd * scsicmd)3675 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
3676 {
3677 	struct fib* cmd_fibcontext;
3678 	struct aac_dev* dev;
3679 	int status;
3680 
3681 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3682 	if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3683 			scsicmd->device->lun > 7) {
3684 		scsicmd->result = DID_NO_CONNECT << 16;
3685 		scsicmd->scsi_done(scsicmd);
3686 		return 0;
3687 	}
3688 
3689 	/*
3690 	 *	Allocate and initialize a Fib then setup a BlockWrite command
3691 	 */
3692 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3693 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3694 	status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
3695 
3696 	/*
3697 	 *	Check that the command queued to the controller
3698 	 */
3699 	if (status == -EINPROGRESS)
3700 		return 0;
3701 
3702 	printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
3703 	aac_fib_complete(cmd_fibcontext);
3704 	aac_fib_free(cmd_fibcontext);
3705 
3706 	return -1;
3707 }
3708 
3709 /**
3710  *
3711  * aac_send_hba_fib
3712  * @scsicmd: the scsi command block
3713  *
3714  * This routine will form a FIB and fill in the aac_hba_cmd_req from the
3715  * scsicmd passed in.
3716  */
aac_send_hba_fib(struct scsi_cmnd * scsicmd)3717 static int aac_send_hba_fib(struct scsi_cmnd *scsicmd)
3718 {
3719 	struct fib *cmd_fibcontext;
3720 	struct aac_dev *dev;
3721 	int status;
3722 
3723 	dev = shost_priv(scsicmd->device->host);
3724 	if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3725 			scsicmd->device->lun > AAC_MAX_LUN - 1) {
3726 		scsicmd->result = DID_NO_CONNECT << 16;
3727 		scsicmd->scsi_done(scsicmd);
3728 		return 0;
3729 	}
3730 
3731 	/*
3732 	 *	Allocate and initialize a Fib then setup a BlockWrite command
3733 	 */
3734 	cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3735 	if (!cmd_fibcontext)
3736 		return -1;
3737 
3738 	scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3739 	status = aac_adapter_hba(cmd_fibcontext, scsicmd);
3740 
3741 	/*
3742 	 *	Check that the command queued to the controller
3743 	 */
3744 	if (status == -EINPROGRESS)
3745 		return 0;
3746 
3747 	pr_warn("aac_hba_cmd_req: aac_fib_send failed with status: %d\n",
3748 		status);
3749 	aac_fib_complete(cmd_fibcontext);
3750 	aac_fib_free(cmd_fibcontext);
3751 
3752 	return -1;
3753 }
3754 
3755 
aac_build_sg(struct scsi_cmnd * scsicmd,struct sgmap * psg)3756 static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
3757 {
3758 	struct aac_dev *dev;
3759 	unsigned long byte_count = 0;
3760 	int nseg;
3761 	struct scatterlist *sg;
3762 	int i;
3763 
3764 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3765 	// Get rid of old data
3766 	psg->count = 0;
3767 	psg->sg[0].addr = 0;
3768 	psg->sg[0].count = 0;
3769 
3770 	nseg = scsi_dma_map(scsicmd);
3771 	if (nseg <= 0)
3772 		return nseg;
3773 
3774 	psg->count = cpu_to_le32(nseg);
3775 
3776 	scsi_for_each_sg(scsicmd, sg, nseg, i) {
3777 		psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
3778 		psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
3779 		byte_count += sg_dma_len(sg);
3780 	}
3781 	/* hba wants the size to be exact */
3782 	if (byte_count > scsi_bufflen(scsicmd)) {
3783 		u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3784 			(byte_count - scsi_bufflen(scsicmd));
3785 		psg->sg[i-1].count = cpu_to_le32(temp);
3786 		byte_count = scsi_bufflen(scsicmd);
3787 	}
3788 	/* Check for command underflow */
3789 	if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3790 		printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3791 		       byte_count, scsicmd->underflow);
3792 	}
3793 
3794 	return byte_count;
3795 }
3796 
3797 
aac_build_sg64(struct scsi_cmnd * scsicmd,struct sgmap64 * psg)3798 static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
3799 {
3800 	struct aac_dev *dev;
3801 	unsigned long byte_count = 0;
3802 	u64 addr;
3803 	int nseg;
3804 	struct scatterlist *sg;
3805 	int i;
3806 
3807 	dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3808 	// Get rid of old data
3809 	psg->count = 0;
3810 	psg->sg[0].addr[0] = 0;
3811 	psg->sg[0].addr[1] = 0;
3812 	psg->sg[0].count = 0;
3813 
3814 	nseg = scsi_dma_map(scsicmd);
3815 	if (nseg <= 0)
3816 		return nseg;
3817 
3818 	scsi_for_each_sg(scsicmd, sg, nseg, i) {
3819 		int count = sg_dma_len(sg);
3820 		addr = sg_dma_address(sg);
3821 		psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
3822 		psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
3823 		psg->sg[i].count = cpu_to_le32(count);
3824 		byte_count += count;
3825 	}
3826 	psg->count = cpu_to_le32(nseg);
3827 	/* hba wants the size to be exact */
3828 	if (byte_count > scsi_bufflen(scsicmd)) {
3829 		u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3830 			(byte_count - scsi_bufflen(scsicmd));
3831 		psg->sg[i-1].count = cpu_to_le32(temp);
3832 		byte_count = scsi_bufflen(scsicmd);
3833 	}
3834 	/* Check for command underflow */
3835 	if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3836 		printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3837 		       byte_count, scsicmd->underflow);
3838 	}
3839 
3840 	return byte_count;
3841 }
3842 
aac_build_sgraw(struct scsi_cmnd * scsicmd,struct sgmapraw * psg)3843 static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
3844 {
3845 	unsigned long byte_count = 0;
3846 	int nseg;
3847 	struct scatterlist *sg;
3848 	int i;
3849 
3850 	// Get rid of old data
3851 	psg->count = 0;
3852 	psg->sg[0].next = 0;
3853 	psg->sg[0].prev = 0;
3854 	psg->sg[0].addr[0] = 0;
3855 	psg->sg[0].addr[1] = 0;
3856 	psg->sg[0].count = 0;
3857 	psg->sg[0].flags = 0;
3858 
3859 	nseg = scsi_dma_map(scsicmd);
3860 	if (nseg <= 0)
3861 		return nseg;
3862 
3863 	scsi_for_each_sg(scsicmd, sg, nseg, i) {
3864 		int count = sg_dma_len(sg);
3865 		u64 addr = sg_dma_address(sg);
3866 		psg->sg[i].next = 0;
3867 		psg->sg[i].prev = 0;
3868 		psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
3869 		psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
3870 		psg->sg[i].count = cpu_to_le32(count);
3871 		psg->sg[i].flags = 0;
3872 		byte_count += count;
3873 	}
3874 	psg->count = cpu_to_le32(nseg);
3875 	/* hba wants the size to be exact */
3876 	if (byte_count > scsi_bufflen(scsicmd)) {
3877 		u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3878 			(byte_count - scsi_bufflen(scsicmd));
3879 		psg->sg[i-1].count = cpu_to_le32(temp);
3880 		byte_count = scsi_bufflen(scsicmd);
3881 	}
3882 	/* Check for command underflow */
3883 	if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3884 		printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3885 		       byte_count, scsicmd->underflow);
3886 	}
3887 
3888 	return byte_count;
3889 }
3890 
aac_build_sgraw2(struct scsi_cmnd * scsicmd,struct aac_raw_io2 * rio2,int sg_max)3891 static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
3892 				struct aac_raw_io2 *rio2, int sg_max)
3893 {
3894 	unsigned long byte_count = 0;
3895 	int nseg;
3896 	struct scatterlist *sg;
3897 	int i, conformable = 0;
3898 	u32 min_size = PAGE_SIZE, cur_size;
3899 
3900 	nseg = scsi_dma_map(scsicmd);
3901 	if (nseg <= 0)
3902 		return nseg;
3903 
3904 	scsi_for_each_sg(scsicmd, sg, nseg, i) {
3905 		int count = sg_dma_len(sg);
3906 		u64 addr = sg_dma_address(sg);
3907 
3908 		BUG_ON(i >= sg_max);
3909 		rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
3910 		rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
3911 		cur_size = cpu_to_le32(count);
3912 		rio2->sge[i].length = cur_size;
3913 		rio2->sge[i].flags = 0;
3914 		if (i == 0) {
3915 			conformable = 1;
3916 			rio2->sgeFirstSize = cur_size;
3917 		} else if (i == 1) {
3918 			rio2->sgeNominalSize = cur_size;
3919 			min_size = cur_size;
3920 		} else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
3921 			conformable = 0;
3922 			if (cur_size < min_size)
3923 				min_size = cur_size;
3924 		}
3925 		byte_count += count;
3926 	}
3927 
3928 	/* hba wants the size to be exact */
3929 	if (byte_count > scsi_bufflen(scsicmd)) {
3930 		u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
3931 			(byte_count - scsi_bufflen(scsicmd));
3932 		rio2->sge[i-1].length = cpu_to_le32(temp);
3933 		byte_count = scsi_bufflen(scsicmd);
3934 	}
3935 
3936 	rio2->sgeCnt = cpu_to_le32(nseg);
3937 	rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
3938 	/* not conformable: evaluate required sg elements */
3939 	if (!conformable) {
3940 		int j, nseg_new = nseg, err_found;
3941 		for (i = min_size / PAGE_SIZE; i >= 1; --i) {
3942 			err_found = 0;
3943 			nseg_new = 2;
3944 			for (j = 1; j < nseg - 1; ++j) {
3945 				if (rio2->sge[j].length % (i*PAGE_SIZE)) {
3946 					err_found = 1;
3947 					break;
3948 				}
3949 				nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
3950 			}
3951 			if (!err_found)
3952 				break;
3953 		}
3954 		if (i > 0 && nseg_new <= sg_max) {
3955 			int ret = aac_convert_sgraw2(rio2, i, nseg, nseg_new);
3956 
3957 			if (ret < 0)
3958 				return ret;
3959 		}
3960 	} else
3961 		rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3962 
3963 	/* Check for command underflow */
3964 	if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3965 		printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3966 		       byte_count, scsicmd->underflow);
3967 	}
3968 
3969 	return byte_count;
3970 }
3971 
aac_convert_sgraw2(struct aac_raw_io2 * rio2,int pages,int nseg,int nseg_new)3972 static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
3973 {
3974 	struct sge_ieee1212 *sge;
3975 	int i, j, pos;
3976 	u32 addr_low;
3977 
3978 	if (aac_convert_sgl == 0)
3979 		return 0;
3980 
3981 	sge = kmalloc(nseg_new * sizeof(struct sge_ieee1212), GFP_ATOMIC);
3982 	if (sge == NULL)
3983 		return -ENOMEM;
3984 
3985 	for (i = 1, pos = 1; i < nseg-1; ++i) {
3986 		for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
3987 			addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
3988 			sge[pos].addrLow = addr_low;
3989 			sge[pos].addrHigh = rio2->sge[i].addrHigh;
3990 			if (addr_low < rio2->sge[i].addrLow)
3991 				sge[pos].addrHigh++;
3992 			sge[pos].length = pages * PAGE_SIZE;
3993 			sge[pos].flags = 0;
3994 			pos++;
3995 		}
3996 	}
3997 	sge[pos] = rio2->sge[nseg-1];
3998 	memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
3999 
4000 	kfree(sge);
4001 	rio2->sgeCnt = cpu_to_le32(nseg_new);
4002 	rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
4003 	rio2->sgeNominalSize = pages * PAGE_SIZE;
4004 	return 0;
4005 }
4006 
aac_build_sghba(struct scsi_cmnd * scsicmd,struct aac_hba_cmd_req * hbacmd,int sg_max,u64 sg_address)4007 static long aac_build_sghba(struct scsi_cmnd *scsicmd,
4008 			struct aac_hba_cmd_req *hbacmd,
4009 			int sg_max,
4010 			u64 sg_address)
4011 {
4012 	unsigned long byte_count = 0;
4013 	int nseg;
4014 	struct scatterlist *sg;
4015 	int i;
4016 	u32 cur_size;
4017 	struct aac_hba_sgl *sge;
4018 
4019 	nseg = scsi_dma_map(scsicmd);
4020 	if (nseg <= 0) {
4021 		byte_count = nseg;
4022 		goto out;
4023 	}
4024 
4025 	if (nseg > HBA_MAX_SG_EMBEDDED)
4026 		sge = &hbacmd->sge[2];
4027 	else
4028 		sge = &hbacmd->sge[0];
4029 
4030 	scsi_for_each_sg(scsicmd, sg, nseg, i) {
4031 		int count = sg_dma_len(sg);
4032 		u64 addr = sg_dma_address(sg);
4033 
4034 		WARN_ON(i >= sg_max);
4035 		sge->addr_hi = cpu_to_le32((u32)(addr>>32));
4036 		sge->addr_lo = cpu_to_le32((u32)(addr & 0xffffffff));
4037 		cur_size = cpu_to_le32(count);
4038 		sge->len = cur_size;
4039 		sge->flags = 0;
4040 		byte_count += count;
4041 		sge++;
4042 	}
4043 
4044 	sge--;
4045 	/* hba wants the size to be exact */
4046 	if (byte_count > scsi_bufflen(scsicmd)) {
4047 		u32 temp;
4048 
4049 		temp = le32_to_cpu(sge->len) - byte_count
4050 						- scsi_bufflen(scsicmd);
4051 		sge->len = cpu_to_le32(temp);
4052 		byte_count = scsi_bufflen(scsicmd);
4053 	}
4054 
4055 	if (nseg <= HBA_MAX_SG_EMBEDDED) {
4056 		hbacmd->emb_data_desc_count = cpu_to_le32(nseg);
4057 		sge->flags = cpu_to_le32(0x40000000);
4058 	} else {
4059 		/* not embedded */
4060 		hbacmd->sge[0].flags = cpu_to_le32(0x80000000);
4061 		hbacmd->emb_data_desc_count = (u8)cpu_to_le32(1);
4062 		hbacmd->sge[0].addr_hi = (u32)cpu_to_le32(sg_address >> 32);
4063 		hbacmd->sge[0].addr_lo =
4064 			cpu_to_le32((u32)(sg_address & 0xffffffff));
4065 	}
4066 
4067 	/* Check for command underflow */
4068 	if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4069 		pr_warn("aacraid: cmd len %08lX cmd underflow %08X\n",
4070 				byte_count, scsicmd->underflow);
4071 	}
4072 out:
4073 	return byte_count;
4074 }
4075 
4076 #ifdef AAC_DETAILED_STATUS_INFO
4077 
4078 struct aac_srb_status_info {
4079 	u32	status;
4080 	char	*str;
4081 };
4082 
4083 
4084 static struct aac_srb_status_info srb_status_info[] = {
4085 	{ SRB_STATUS_PENDING,		"Pending Status"},
4086 	{ SRB_STATUS_SUCCESS,		"Success"},
4087 	{ SRB_STATUS_ABORTED,		"Aborted Command"},
4088 	{ SRB_STATUS_ABORT_FAILED,	"Abort Failed"},
4089 	{ SRB_STATUS_ERROR,		"Error Event"},
4090 	{ SRB_STATUS_BUSY,		"Device Busy"},
4091 	{ SRB_STATUS_INVALID_REQUEST,	"Invalid Request"},
4092 	{ SRB_STATUS_INVALID_PATH_ID,	"Invalid Path ID"},
4093 	{ SRB_STATUS_NO_DEVICE,		"No Device"},
4094 	{ SRB_STATUS_TIMEOUT,		"Timeout"},
4095 	{ SRB_STATUS_SELECTION_TIMEOUT,	"Selection Timeout"},
4096 	{ SRB_STATUS_COMMAND_TIMEOUT,	"Command Timeout"},
4097 	{ SRB_STATUS_MESSAGE_REJECTED,	"Message Rejected"},
4098 	{ SRB_STATUS_BUS_RESET,		"Bus Reset"},
4099 	{ SRB_STATUS_PARITY_ERROR,	"Parity Error"},
4100 	{ SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
4101 	{ SRB_STATUS_NO_HBA,		"No HBA"},
4102 	{ SRB_STATUS_DATA_OVERRUN,	"Data Overrun/Data Underrun"},
4103 	{ SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
4104 	{ SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
4105 	{ SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
4106 	{ SRB_STATUS_REQUEST_FLUSHED,	"Request Flushed"},
4107 	{ SRB_STATUS_DELAYED_RETRY,	"Delayed Retry"},
4108 	{ SRB_STATUS_INVALID_LUN,	"Invalid LUN"},
4109 	{ SRB_STATUS_INVALID_TARGET_ID,	"Invalid TARGET ID"},
4110 	{ SRB_STATUS_BAD_FUNCTION,	"Bad Function"},
4111 	{ SRB_STATUS_ERROR_RECOVERY,	"Error Recovery"},
4112 	{ SRB_STATUS_NOT_STARTED,	"Not Started"},
4113 	{ SRB_STATUS_NOT_IN_USE,	"Not In Use"},
4114 	{ SRB_STATUS_FORCE_ABORT,	"Force Abort"},
4115 	{ SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
4116 	{ 0xff,				"Unknown Error"}
4117 };
4118 
aac_get_status_string(u32 status)4119 char *aac_get_status_string(u32 status)
4120 {
4121 	int i;
4122 
4123 	for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
4124 		if (srb_status_info[i].status == status)
4125 			return srb_status_info[i].str;
4126 
4127 	return "Bad Status Code";
4128 }
4129 
4130 #endif
4131