xref: /linux/drivers/scsi/esas2r/esas2r_main.c (revision 957e3facd147510f2cf8780e38606f1d707f0e33)
1 /*
2  *  linux/drivers/scsi/esas2r/esas2r_main.c
3  *      For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
4  *
5  *  Copyright (c) 2001-2013 ATTO Technology, Inc.
6  *  (mailto:linuxdrivers@attotech.com)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * NO WARRANTY
19  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23  * solely responsible for determining the appropriateness of using and
24  * distributing the Program and assumes all risks associated with its
25  * exercise of rights under this Agreement, including but not limited to
26  * the risks and costs of program errors, damage to or loss of data,
27  * programs or equipment, and unavailability or interruption of operations.
28  *
29  * DISCLAIMER OF LIABILITY
30  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program; if not, write to the Free Software
40  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
41  * USA.
42  */
43 
44 #include "esas2r.h"
45 
46 MODULE_DESCRIPTION(ESAS2R_DRVR_NAME ": " ESAS2R_LONGNAME " driver");
47 MODULE_AUTHOR("ATTO Technology, Inc.");
48 MODULE_LICENSE("GPL");
49 MODULE_VERSION(ESAS2R_VERSION_STR);
50 
51 /* global definitions */
52 
53 static int found_adapters;
54 struct esas2r_adapter *esas2r_adapters[MAX_ADAPTERS];
55 
56 #define ESAS2R_VDA_EVENT_PORT1       54414
57 #define ESAS2R_VDA_EVENT_PORT2       54415
58 #define ESAS2R_VDA_EVENT_SOCK_COUNT  2
59 
60 static struct esas2r_adapter *esas2r_adapter_from_kobj(struct kobject *kobj)
61 {
62 	struct device *dev = container_of(kobj, struct device, kobj);
63 	struct Scsi_Host *host = class_to_shost(dev);
64 
65 	return (struct esas2r_adapter *)host->hostdata;
66 }
67 
68 static ssize_t read_fw(struct file *file, struct kobject *kobj,
69 		       struct bin_attribute *attr,
70 		       char *buf, loff_t off, size_t count)
71 {
72 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
73 
74 	return esas2r_read_fw(a, buf, off, count);
75 }
76 
77 static ssize_t write_fw(struct file *file, struct kobject *kobj,
78 			struct bin_attribute *attr,
79 			char *buf, loff_t off, size_t count)
80 {
81 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
82 
83 	return esas2r_write_fw(a, buf, off, count);
84 }
85 
86 static ssize_t read_fs(struct file *file, struct kobject *kobj,
87 		       struct bin_attribute *attr,
88 		       char *buf, loff_t off, size_t count)
89 {
90 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
91 
92 	return esas2r_read_fs(a, buf, off, count);
93 }
94 
95 static ssize_t write_fs(struct file *file, struct kobject *kobj,
96 			struct bin_attribute *attr,
97 			char *buf, loff_t off, size_t count)
98 {
99 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
100 	int length = min(sizeof(struct esas2r_ioctl_fs), count);
101 	int result = 0;
102 
103 	result = esas2r_write_fs(a, buf, off, count);
104 
105 	if (result < 0)
106 		result = 0;
107 
108 	return length;
109 }
110 
111 static ssize_t read_vda(struct file *file, struct kobject *kobj,
112 			struct bin_attribute *attr,
113 			char *buf, loff_t off, size_t count)
114 {
115 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
116 
117 	return esas2r_read_vda(a, buf, off, count);
118 }
119 
120 static ssize_t write_vda(struct file *file, struct kobject *kobj,
121 			 struct bin_attribute *attr,
122 			 char *buf, loff_t off, size_t count)
123 {
124 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
125 
126 	return esas2r_write_vda(a, buf, off, count);
127 }
128 
129 static ssize_t read_live_nvram(struct file *file, struct kobject *kobj,
130 			       struct bin_attribute *attr,
131 			       char *buf, loff_t off, size_t count)
132 {
133 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
134 	int length = min_t(size_t, sizeof(struct esas2r_sas_nvram), PAGE_SIZE);
135 
136 	memcpy(buf, a->nvram, length);
137 	return length;
138 }
139 
140 static ssize_t write_live_nvram(struct file *file, struct kobject *kobj,
141 				struct bin_attribute *attr,
142 				char *buf, loff_t off, size_t count)
143 {
144 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
145 	struct esas2r_request *rq;
146 	int result = -EFAULT;
147 
148 	rq = esas2r_alloc_request(a);
149 	if (rq == NULL)
150 		return -ENOMEM;
151 
152 	if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf))
153 		result = count;
154 
155 	esas2r_free_request(a, rq);
156 
157 	return result;
158 }
159 
160 static ssize_t read_default_nvram(struct file *file, struct kobject *kobj,
161 				  struct bin_attribute *attr,
162 				  char *buf, loff_t off, size_t count)
163 {
164 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
165 
166 	esas2r_nvram_get_defaults(a, (struct esas2r_sas_nvram *)buf);
167 
168 	return sizeof(struct esas2r_sas_nvram);
169 }
170 
171 static ssize_t read_hw(struct file *file, struct kobject *kobj,
172 		       struct bin_attribute *attr,
173 		       char *buf, loff_t off, size_t count)
174 {
175 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
176 	int length = min_t(size_t, sizeof(struct atto_ioctl), PAGE_SIZE);
177 
178 	if (!a->local_atto_ioctl)
179 		return -ENOMEM;
180 
181 	if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS)
182 		return -ENOMEM;
183 
184 	memcpy(buf, a->local_atto_ioctl, length);
185 
186 	return length;
187 }
188 
189 static ssize_t write_hw(struct file *file, struct kobject *kobj,
190 			struct bin_attribute *attr,
191 			char *buf, loff_t off, size_t count)
192 {
193 	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
194 	int length = min(sizeof(struct atto_ioctl), count);
195 
196 	if (!a->local_atto_ioctl) {
197 		a->local_atto_ioctl = kzalloc(sizeof(struct atto_ioctl),
198 					      GFP_KERNEL);
199 		if (a->local_atto_ioctl == NULL) {
200 			esas2r_log(ESAS2R_LOG_WARN,
201 				   "write_hw kzalloc failed for %d bytes",
202 				   sizeof(struct atto_ioctl));
203 			return -ENOMEM;
204 		}
205 	}
206 
207 	memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl));
208 	memcpy(a->local_atto_ioctl, buf, length);
209 
210 	return length;
211 }
212 
213 #define ESAS2R_RW_BIN_ATTR(_name) \
214 	struct bin_attribute bin_attr_ ## _name = { \
215 		.attr	= \
216 		{ .name = __stringify(_name), .mode  = S_IRUSR | S_IWUSR }, \
217 		.size	= 0, \
218 		.read	= read_ ## _name, \
219 		.write	= write_ ## _name }
220 
221 ESAS2R_RW_BIN_ATTR(fw);
222 ESAS2R_RW_BIN_ATTR(fs);
223 ESAS2R_RW_BIN_ATTR(vda);
224 ESAS2R_RW_BIN_ATTR(hw);
225 ESAS2R_RW_BIN_ATTR(live_nvram);
226 
227 struct bin_attribute bin_attr_default_nvram = {
228 	.attr	= { .name = "default_nvram", .mode = S_IRUGO },
229 	.size	= 0,
230 	.read	= read_default_nvram,
231 	.write	= NULL
232 };
233 
234 static struct scsi_host_template driver_template = {
235 	.module				= THIS_MODULE,
236 	.show_info			= esas2r_show_info,
237 	.name				= ESAS2R_LONGNAME,
238 	.release			= esas2r_release,
239 	.info				= esas2r_info,
240 	.ioctl				= esas2r_ioctl,
241 	.queuecommand			= esas2r_queuecommand,
242 	.eh_abort_handler		= esas2r_eh_abort,
243 	.eh_device_reset_handler	= esas2r_device_reset,
244 	.eh_bus_reset_handler		= esas2r_bus_reset,
245 	.eh_host_reset_handler		= esas2r_host_reset,
246 	.eh_target_reset_handler	= esas2r_target_reset,
247 	.can_queue			= 128,
248 	.this_id			= -1,
249 	.sg_tablesize			= SCSI_MAX_SG_SEGMENTS,
250 	.cmd_per_lun			=
251 		ESAS2R_DEFAULT_CMD_PER_LUN,
252 	.present			= 0,
253 	.unchecked_isa_dma		= 0,
254 	.use_clustering			= ENABLE_CLUSTERING,
255 	.emulated			= 0,
256 	.proc_name			= ESAS2R_DRVR_NAME,
257 	.change_queue_depth		= scsi_change_queue_depth,
258 	.change_queue_type		= scsi_change_queue_type,
259 	.max_sectors			= 0xFFFF,
260 	.use_blk_tags			= 1,
261 };
262 
263 int sgl_page_size = 512;
264 module_param(sgl_page_size, int, 0);
265 MODULE_PARM_DESC(sgl_page_size,
266 		 "Scatter/gather list (SGL) page size in number of S/G "
267 		 "entries.  If your application is doing a lot of very large "
268 		 "transfers, you may want to increase the SGL page size.  "
269 		 "Default 512.");
270 
271 int num_sg_lists = 1024;
272 module_param(num_sg_lists, int, 0);
273 MODULE_PARM_DESC(num_sg_lists,
274 		 "Number of scatter/gather lists.  Default 1024.");
275 
276 int sg_tablesize = SCSI_MAX_SG_SEGMENTS;
277 module_param(sg_tablesize, int, 0);
278 MODULE_PARM_DESC(sg_tablesize,
279 		 "Maximum number of entries in a scatter/gather table.");
280 
281 int num_requests = 256;
282 module_param(num_requests, int, 0);
283 MODULE_PARM_DESC(num_requests,
284 		 "Number of requests.  Default 256.");
285 
286 int num_ae_requests = 4;
287 module_param(num_ae_requests, int, 0);
288 MODULE_PARM_DESC(num_ae_requests,
289 		 "Number of VDA asynchromous event requests.  Default 4.");
290 
291 int cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN;
292 module_param(cmd_per_lun, int, 0);
293 MODULE_PARM_DESC(cmd_per_lun,
294 		 "Maximum number of commands per LUN.  Default "
295 		 DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) ".");
296 
297 int can_queue = 128;
298 module_param(can_queue, int, 0);
299 MODULE_PARM_DESC(can_queue,
300 		 "Maximum number of commands per adapter.  Default 128.");
301 
302 int esas2r_max_sectors = 0xFFFF;
303 module_param(esas2r_max_sectors, int, 0);
304 MODULE_PARM_DESC(esas2r_max_sectors,
305 		 "Maximum number of disk sectors in a single data transfer.  "
306 		 "Default 65535 (largest possible setting).");
307 
308 int interrupt_mode = 1;
309 module_param(interrupt_mode, int, 0);
310 MODULE_PARM_DESC(interrupt_mode,
311 		 "Defines the interrupt mode to use.  0 for legacy"
312 		 ", 1 for MSI.  Default is MSI (1).");
313 
314 static struct pci_device_id
315 	esas2r_pci_table[] = {
316 	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x0049,
317 	  0,
318 	  0, 0 },
319 	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004A,
320 	  0,
321 	  0, 0 },
322 	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004B,
323 	  0,
324 	  0, 0 },
325 	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004C,
326 	  0,
327 	  0, 0 },
328 	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004D,
329 	  0,
330 	  0, 0 },
331 	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004E,
332 	  0,
333 	  0, 0 },
334 	{ 0,		  0,		  0,		  0,
335 	  0,
336 	  0, 0 }
337 };
338 
339 MODULE_DEVICE_TABLE(pci, esas2r_pci_table);
340 
341 static int
342 esas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id);
343 
344 static void
345 esas2r_remove(struct pci_dev *pcid);
346 
347 static struct pci_driver
348 	esas2r_pci_driver = {
349 	.name		= ESAS2R_DRVR_NAME,
350 	.id_table	= esas2r_pci_table,
351 	.probe		= esas2r_probe,
352 	.remove		= esas2r_remove,
353 	.suspend	= esas2r_suspend,
354 	.resume		= esas2r_resume,
355 };
356 
357 static int esas2r_probe(struct pci_dev *pcid,
358 			const struct pci_device_id *id)
359 {
360 	struct Scsi_Host *host = NULL;
361 	struct esas2r_adapter *a;
362 	int err;
363 
364 	size_t host_alloc_size = sizeof(struct esas2r_adapter)
365 				 + ((num_requests) +
366 				    1) * sizeof(struct esas2r_request);
367 
368 	esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev),
369 		       "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
370 		       pcid->vendor,
371 		       pcid->device,
372 		       pcid->subsystem_vendor,
373 		       pcid->subsystem_device);
374 
375 	esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
376 		       "before pci_enable_device() "
377 		       "enable_cnt: %d",
378 		       pcid->enable_cnt.counter);
379 
380 	err = pci_enable_device(pcid);
381 	if (err != 0) {
382 		esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev),
383 			       "pci_enable_device() FAIL (%d)",
384 			       err);
385 		return -ENODEV;
386 	}
387 
388 	esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
389 		       "pci_enable_device() OK");
390 	esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
391 		       "after pci_enable_device() enable_cnt: %d",
392 		       pcid->enable_cnt.counter);
393 
394 	host = scsi_host_alloc(&driver_template, host_alloc_size);
395 	if (host == NULL) {
396 		esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL");
397 		return -ENODEV;
398 	}
399 
400 	memset(host->hostdata, 0, host_alloc_size);
401 
402 	a = (struct esas2r_adapter *)host->hostdata;
403 
404 	esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host);
405 
406 	/* override max LUN and max target id */
407 
408 	host->max_id = ESAS2R_MAX_ID + 1;
409 	host->max_lun = 255;
410 
411 	/* we can handle 16-byte CDbs */
412 
413 	host->max_cmd_len = 16;
414 
415 	host->can_queue = can_queue;
416 	host->cmd_per_lun = cmd_per_lun;
417 	host->this_id = host->max_id + 1;
418 	host->max_channel = 0;
419 	host->unique_id = found_adapters;
420 	host->sg_tablesize = sg_tablesize;
421 	host->max_sectors = esas2r_max_sectors;
422 
423 	/* set to bus master for BIOses that don't do it for us */
424 
425 	esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called");
426 
427 	pci_set_master(pcid);
428 
429 	if (!esas2r_init_adapter(host, pcid, found_adapters)) {
430 		esas2r_log(ESAS2R_LOG_CRIT,
431 			   "unable to initialize device at PCI bus %x:%x",
432 			   pcid->bus->number,
433 			   pcid->devfn);
434 
435 		esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
436 			       "scsi_host_put() called");
437 
438 		scsi_host_put(host);
439 
440 		return 0;
441 
442 	}
443 
444 	esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid,
445 		   host->hostdata);
446 
447 	pci_set_drvdata(pcid, host);
448 
449 	esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called");
450 
451 	err = scsi_add_host(host, &pcid->dev);
452 
453 	if (err) {
454 		esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err);
455 		esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev),
456 			       "scsi_add_host() FAIL");
457 
458 		esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
459 			       "scsi_host_put() called");
460 
461 		scsi_host_put(host);
462 
463 		esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
464 			       "pci_set_drvdata(%p, NULL) called",
465 			       pcid);
466 
467 		pci_set_drvdata(pcid, NULL);
468 
469 		return -ENODEV;
470 	}
471 
472 
473 	esas2r_fw_event_on(a);
474 
475 	esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
476 		       "scsi_scan_host() called");
477 
478 	scsi_scan_host(host);
479 
480 	/* Add sysfs binary files */
481 	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw))
482 		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
483 			       "Failed to create sysfs binary file: fw");
484 	else
485 		a->sysfs_fw_created = 1;
486 
487 	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs))
488 		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
489 			       "Failed to create sysfs binary file: fs");
490 	else
491 		a->sysfs_fs_created = 1;
492 
493 	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda))
494 		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
495 			       "Failed to create sysfs binary file: vda");
496 	else
497 		a->sysfs_vda_created = 1;
498 
499 	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw))
500 		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
501 			       "Failed to create sysfs binary file: hw");
502 	else
503 		a->sysfs_hw_created = 1;
504 
505 	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram))
506 		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
507 			       "Failed to create sysfs binary file: live_nvram");
508 	else
509 		a->sysfs_live_nvram_created = 1;
510 
511 	if (sysfs_create_bin_file(&host->shost_dev.kobj,
512 				  &bin_attr_default_nvram))
513 		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
514 			       "Failed to create sysfs binary file: default_nvram");
515 	else
516 		a->sysfs_default_nvram_created = 1;
517 
518 	found_adapters++;
519 
520 	return 0;
521 }
522 
523 static void esas2r_remove(struct pci_dev *pdev)
524 {
525 	struct Scsi_Host *host;
526 	int index;
527 
528 	if (pdev == NULL) {
529 		esas2r_log(ESAS2R_LOG_WARN, "esas2r_remove pdev==NULL");
530 		return;
531 	}
532 
533 	host = pci_get_drvdata(pdev);
534 
535 	if (host == NULL) {
536 		/*
537 		 * this can happen if pci_set_drvdata was already called
538 		 * to clear the host pointer.  if this is the case, we
539 		 * are okay; this channel has already been cleaned up.
540 		 */
541 
542 		return;
543 	}
544 
545 	esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
546 		       "esas2r_remove(%p) called; "
547 		       "host:%p", pdev,
548 		       host);
549 
550 	index = esas2r_cleanup(host);
551 
552 	if (index < 0)
553 		esas2r_log_dev(ESAS2R_LOG_WARN, &(pdev->dev),
554 			       "unknown host in %s",
555 			       __func__);
556 
557 	found_adapters--;
558 
559 	/* if this was the last adapter, clean up the rest of the driver */
560 
561 	if (found_adapters == 0)
562 		esas2r_cleanup(NULL);
563 }
564 
565 static int __init esas2r_init(void)
566 {
567 	int i;
568 
569 	esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
570 
571 	/* verify valid parameters */
572 
573 	if (can_queue < 1) {
574 		esas2r_log(ESAS2R_LOG_WARN,
575 			   "warning: can_queue must be at least 1, value "
576 			   "forced.");
577 		can_queue = 1;
578 	} else if (can_queue > 2048) {
579 		esas2r_log(ESAS2R_LOG_WARN,
580 			   "warning: can_queue must be no larger than 2048, "
581 			   "value forced.");
582 		can_queue = 2048;
583 	}
584 
585 	if (cmd_per_lun < 1) {
586 		esas2r_log(ESAS2R_LOG_WARN,
587 			   "warning: cmd_per_lun must be at least 1, value "
588 			   "forced.");
589 		cmd_per_lun = 1;
590 	} else if (cmd_per_lun > 2048) {
591 		esas2r_log(ESAS2R_LOG_WARN,
592 			   "warning: cmd_per_lun must be no larger than "
593 			   "2048, value forced.");
594 		cmd_per_lun = 2048;
595 	}
596 
597 	if (sg_tablesize < 32) {
598 		esas2r_log(ESAS2R_LOG_WARN,
599 			   "warning: sg_tablesize must be at least 32, "
600 			   "value forced.");
601 		sg_tablesize = 32;
602 	}
603 
604 	if (esas2r_max_sectors < 1) {
605 		esas2r_log(ESAS2R_LOG_WARN,
606 			   "warning: esas2r_max_sectors must be at least "
607 			   "1, value forced.");
608 		esas2r_max_sectors = 1;
609 	} else if (esas2r_max_sectors > 0xffff) {
610 		esas2r_log(ESAS2R_LOG_WARN,
611 			   "warning: esas2r_max_sectors must be no larger "
612 			   "than 0xffff, value forced.");
613 		esas2r_max_sectors = 0xffff;
614 	}
615 
616 	sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1);
617 
618 	if (sgl_page_size < SGL_PG_SZ_MIN)
619 		sgl_page_size = SGL_PG_SZ_MIN;
620 	else if (sgl_page_size > SGL_PG_SZ_MAX)
621 		sgl_page_size = SGL_PG_SZ_MAX;
622 
623 	if (num_sg_lists < NUM_SGL_MIN)
624 		num_sg_lists = NUM_SGL_MIN;
625 	else if (num_sg_lists > NUM_SGL_MAX)
626 		num_sg_lists = NUM_SGL_MAX;
627 
628 	if (num_requests < NUM_REQ_MIN)
629 		num_requests = NUM_REQ_MIN;
630 	else if (num_requests > NUM_REQ_MAX)
631 		num_requests = NUM_REQ_MAX;
632 
633 	if (num_ae_requests < NUM_AE_MIN)
634 		num_ae_requests = NUM_AE_MIN;
635 	else if (num_ae_requests > NUM_AE_MAX)
636 		num_ae_requests = NUM_AE_MAX;
637 
638 	/* set up other globals */
639 
640 	for (i = 0; i < MAX_ADAPTERS; i++)
641 		esas2r_adapters[i] = NULL;
642 
643 	/* initialize */
644 
645 	driver_template.module = THIS_MODULE;
646 
647 	if (pci_register_driver(&esas2r_pci_driver) != 0)
648 		esas2r_log(ESAS2R_LOG_CRIT, "pci_register_driver FAILED");
649 	else
650 		esas2r_log(ESAS2R_LOG_INFO, "pci_register_driver() OK");
651 
652 	if (!found_adapters) {
653 		pci_unregister_driver(&esas2r_pci_driver);
654 		esas2r_cleanup(NULL);
655 
656 		esas2r_log(ESAS2R_LOG_CRIT,
657 			   "driver will not be loaded because no ATTO "
658 			   "%s devices were found",
659 			   ESAS2R_DRVR_NAME);
660 		return -1;
661 	} else {
662 		esas2r_log(ESAS2R_LOG_INFO, "found %d adapters",
663 			   found_adapters);
664 	}
665 
666 	return 0;
667 }
668 
669 /* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
670 static const struct file_operations esas2r_proc_fops = {
671 	.compat_ioctl	= esas2r_proc_ioctl,
672 	.unlocked_ioctl = esas2r_proc_ioctl,
673 };
674 
675 static struct Scsi_Host *esas2r_proc_host;
676 static int esas2r_proc_major;
677 
678 long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
679 {
680 	return esas2r_ioctl_handler(esas2r_proc_host->hostdata,
681 				    (int)cmd, (void __user *)arg);
682 }
683 
684 static void __exit esas2r_exit(void)
685 {
686 	esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
687 
688 	if (esas2r_proc_major > 0) {
689 		esas2r_log(ESAS2R_LOG_INFO, "unregister proc");
690 
691 		remove_proc_entry(ATTONODE_NAME,
692 				  esas2r_proc_host->hostt->proc_dir);
693 		unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME);
694 
695 		esas2r_proc_major = 0;
696 	}
697 
698 	esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called");
699 
700 	pci_unregister_driver(&esas2r_pci_driver);
701 }
702 
703 int esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh)
704 {
705 	struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
706 
707 	struct esas2r_target *t;
708 	int dev_count = 0;
709 
710 	esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no);
711 
712 	seq_printf(m, ESAS2R_LONGNAME "\n"
713 		   "Driver version: "ESAS2R_VERSION_STR "\n"
714 		   "Flash version: %s\n"
715 		   "Firmware version: %s\n"
716 		   "Copyright "ESAS2R_COPYRIGHT_YEARS "\n"
717 		   "http://www.attotech.com\n"
718 		   "\n",
719 		   a->flash_rev,
720 		   a->fw_rev[0] ? a->fw_rev : "(none)");
721 
722 
723 	seq_printf(m, "Adapter information:\n"
724 		   "--------------------\n"
725 		   "Model: %s\n"
726 		   "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
727 		   esas2r_get_model_name(a),
728 		   a->nvram->sas_addr[0],
729 		   a->nvram->sas_addr[1],
730 		   a->nvram->sas_addr[2],
731 		   a->nvram->sas_addr[3],
732 		   a->nvram->sas_addr[4],
733 		   a->nvram->sas_addr[5],
734 		   a->nvram->sas_addr[6],
735 		   a->nvram->sas_addr[7]);
736 
737 	seq_puts(m, "\n"
738 		   "Discovered devices:\n"
739 		   "\n"
740 		   "   #  Target ID\n"
741 		   "---------------\n");
742 
743 	for (t = a->targetdb; t < a->targetdb_end; t++)
744 		if (t->buffered_target_state == TS_PRESENT) {
745 			seq_printf(m, " %3d   %3d\n",
746 				   ++dev_count,
747 				   (u16)(uintptr_t)(t - a->targetdb));
748 		}
749 
750 	if (dev_count == 0)
751 		seq_puts(m, "none\n");
752 
753 	seq_puts(m, "\n");
754 	return 0;
755 
756 }
757 
758 int esas2r_release(struct Scsi_Host *sh)
759 {
760 	esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
761 		       "esas2r_release() called");
762 
763 	esas2r_cleanup(sh);
764 	if (sh->irq)
765 		free_irq(sh->irq, NULL);
766 	scsi_unregister(sh);
767 	return 0;
768 }
769 
770 const char *esas2r_info(struct Scsi_Host *sh)
771 {
772 	struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
773 	static char esas2r_info_str[512];
774 
775 	esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
776 		       "esas2r_info() called");
777 
778 	/*
779 	 * if we haven't done so already, register as a char driver
780 	 * and stick a node under "/proc/scsi/esas2r/ATTOnode"
781 	 */
782 
783 	if (esas2r_proc_major <= 0) {
784 		esas2r_proc_host = sh;
785 
786 		esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME,
787 						    &esas2r_proc_fops);
788 
789 		esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev),
790 			       "register_chrdev (major %d)",
791 			       esas2r_proc_major);
792 
793 		if (esas2r_proc_major > 0) {
794 			struct proc_dir_entry *pde;
795 
796 			pde = proc_create(ATTONODE_NAME, 0,
797 					  sh->hostt->proc_dir,
798 					  &esas2r_proc_fops);
799 
800 			if (!pde) {
801 				esas2r_log_dev(ESAS2R_LOG_WARN,
802 					       &(sh->shost_gendev),
803 					       "failed to create_proc_entry");
804 				esas2r_proc_major = -1;
805 			}
806 		}
807 	}
808 
809 	sprintf(esas2r_info_str,
810 		ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
811 		" driver version: "ESAS2R_VERSION_STR "  firmware version: "
812 		"%s\n",
813 		a->pcid->bus->number, a->pcid->devfn, a->pcid->irq,
814 		a->fw_rev[0] ? a->fw_rev : "(none)");
815 
816 	return esas2r_info_str;
817 }
818 
819 /* Callback for building a request scatter/gather list */
820 static u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr)
821 {
822 	u32 len;
823 
824 	if (likely(sgc->cur_offset == sgc->exp_offset)) {
825 		/*
826 		 * the normal case: caller used all bytes from previous call, so
827 		 * expected offset is the same as the current offset.
828 		 */
829 
830 		if (sgc->sgel_count < sgc->num_sgel) {
831 			/* retrieve next segment, except for first time */
832 			if (sgc->exp_offset > (u8 *)0) {
833 				/* advance current segment */
834 				sgc->cur_sgel = sg_next(sgc->cur_sgel);
835 				++(sgc->sgel_count);
836 			}
837 
838 
839 			len = sg_dma_len(sgc->cur_sgel);
840 			(*addr) = sg_dma_address(sgc->cur_sgel);
841 
842 			/* save the total # bytes returned to caller so far */
843 			sgc->exp_offset += len;
844 
845 		} else {
846 			len = 0;
847 		}
848 	} else if (sgc->cur_offset < sgc->exp_offset) {
849 		/*
850 		 * caller did not use all bytes from previous call. need to
851 		 * compute the address based on current segment.
852 		 */
853 
854 		len = sg_dma_len(sgc->cur_sgel);
855 		(*addr) = sg_dma_address(sgc->cur_sgel);
856 
857 		sgc->exp_offset -= len;
858 
859 		/* calculate PA based on prev segment address and offsets */
860 		*addr = *addr +
861 			(sgc->cur_offset - sgc->exp_offset);
862 
863 		sgc->exp_offset += len;
864 
865 		/* re-calculate length based on offset */
866 		len = lower_32_bits(
867 			sgc->exp_offset - sgc->cur_offset);
868 	} else {   /* if ( sgc->cur_offset > sgc->exp_offset ) */
869 		   /*
870 		    * we don't expect the caller to skip ahead.
871 		    * cur_offset will never exceed the len we return
872 		    */
873 		len = 0;
874 	}
875 
876 	return len;
877 }
878 
879 int esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
880 {
881 	struct esas2r_adapter *a =
882 		(struct esas2r_adapter *)cmd->device->host->hostdata;
883 	struct esas2r_request *rq;
884 	struct esas2r_sg_context sgc;
885 	unsigned bufflen;
886 
887 	/* Assume success, if it fails we will fix the result later. */
888 	cmd->result = DID_OK << 16;
889 
890 	if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) {
891 		cmd->result = DID_NO_CONNECT << 16;
892 		cmd->scsi_done(cmd);
893 		return 0;
894 	}
895 
896 	rq = esas2r_alloc_request(a);
897 	if (unlikely(rq == NULL)) {
898 		esas2r_debug("esas2r_alloc_request failed");
899 		return SCSI_MLQUEUE_HOST_BUSY;
900 	}
901 
902 	rq->cmd = cmd;
903 	bufflen = scsi_bufflen(cmd);
904 
905 	if (likely(bufflen != 0)) {
906 		if (cmd->sc_data_direction == DMA_TO_DEVICE)
907 			rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
908 		else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
909 			rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
910 	}
911 
912 	memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len);
913 	rq->vrq->scsi.length = cpu_to_le32(bufflen);
914 	rq->target_id = cmd->device->id;
915 	rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
916 	rq->sense_buf = cmd->sense_buffer;
917 	rq->sense_len = SCSI_SENSE_BUFFERSIZE;
918 
919 	esas2r_sgc_init(&sgc, a, rq, NULL);
920 
921 	sgc.length = bufflen;
922 	sgc.cur_offset = NULL;
923 
924 	sgc.cur_sgel = scsi_sglist(cmd);
925 	sgc.exp_offset = NULL;
926 	sgc.num_sgel = scsi_dma_map(cmd);
927 	sgc.sgel_count = 0;
928 
929 	if (unlikely(sgc.num_sgel < 0)) {
930 		esas2r_free_request(a, rq);
931 		return SCSI_MLQUEUE_HOST_BUSY;
932 	}
933 
934 	sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc;
935 
936 	if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) {
937 		scsi_dma_unmap(cmd);
938 		esas2r_free_request(a, rq);
939 		return SCSI_MLQUEUE_HOST_BUSY;
940 	}
941 
942 	esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id,
943 		     (int)cmd->device->lun);
944 
945 	esas2r_start_request(a, rq);
946 
947 	return 0;
948 }
949 
950 static void complete_task_management_request(struct esas2r_adapter *a,
951 					     struct esas2r_request *rq)
952 {
953 	(*rq->task_management_status_ptr) = rq->req_stat;
954 	esas2r_free_request(a, rq);
955 }
956 
957 /**
958  * Searches the specified queue for the specified queue for the command
959  * to abort.
960  *
961  * @param [in] a
962  * @param [in] abort_request
963  * @param [in] cmd
964  * t
965  * @return 0 on failure, 1 if command was not found, 2 if command was found
966  */
967 static int esas2r_check_active_queue(struct esas2r_adapter *a,
968 				     struct esas2r_request **abort_request,
969 				     struct scsi_cmnd *cmd,
970 				     struct list_head *queue)
971 {
972 	bool found = false;
973 	struct esas2r_request *ar = *abort_request;
974 	struct esas2r_request *rq;
975 	struct list_head *element, *next;
976 
977 	list_for_each_safe(element, next, queue) {
978 
979 		rq = list_entry(element, struct esas2r_request, req_list);
980 
981 		if (rq->cmd == cmd) {
982 
983 			/* Found the request.  See what to do with it. */
984 			if (queue == &a->active_list) {
985 				/*
986 				 * We are searching the active queue, which
987 				 * means that we need to send an abort request
988 				 * to the firmware.
989 				 */
990 				ar = esas2r_alloc_request(a);
991 				if (ar == NULL) {
992 					esas2r_log_dev(ESAS2R_LOG_WARN,
993 						       &(a->host->shost_gendev),
994 						       "unable to allocate an abort request for cmd %p",
995 						       cmd);
996 					return 0; /* Failure */
997 				}
998 
999 				/*
1000 				 * Task management request must be formatted
1001 				 * with a lock held.
1002 				 */
1003 				ar->sense_len = 0;
1004 				ar->vrq->scsi.length = 0;
1005 				ar->target_id = rq->target_id;
1006 				ar->vrq->scsi.flags |= cpu_to_le32(
1007 					(u8)le32_to_cpu(rq->vrq->scsi.flags));
1008 
1009 				memset(ar->vrq->scsi.cdb, 0,
1010 				       sizeof(ar->vrq->scsi.cdb));
1011 
1012 				ar->vrq->scsi.flags |= cpu_to_le32(
1013 					FCP_CMND_TRM);
1014 				ar->vrq->scsi.u.abort_handle =
1015 					rq->vrq->scsi.handle;
1016 			} else {
1017 				/*
1018 				 * The request is pending but not active on
1019 				 * the firmware.  Just free it now and we'll
1020 				 * report the successful abort below.
1021 				 */
1022 				list_del_init(&rq->req_list);
1023 				esas2r_free_request(a, rq);
1024 			}
1025 
1026 			found = true;
1027 			break;
1028 		}
1029 
1030 	}
1031 
1032 	if (!found)
1033 		return 1;       /* Not found */
1034 
1035 	return 2;               /* found */
1036 
1037 
1038 }
1039 
1040 int esas2r_eh_abort(struct scsi_cmnd *cmd)
1041 {
1042 	struct esas2r_adapter *a =
1043 		(struct esas2r_adapter *)cmd->device->host->hostdata;
1044 	struct esas2r_request *abort_request = NULL;
1045 	unsigned long flags;
1046 	struct list_head *queue;
1047 	int result;
1048 
1049 	esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd);
1050 
1051 	if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
1052 		cmd->result = DID_ABORT << 16;
1053 
1054 		scsi_set_resid(cmd, 0);
1055 
1056 		cmd->scsi_done(cmd);
1057 
1058 		return SUCCESS;
1059 	}
1060 
1061 	spin_lock_irqsave(&a->queue_lock, flags);
1062 
1063 	/*
1064 	 * Run through the defer and active queues looking for the request
1065 	 * to abort.
1066 	 */
1067 
1068 	queue = &a->defer_list;
1069 
1070 check_active_queue:
1071 
1072 	result = esas2r_check_active_queue(a, &abort_request, cmd, queue);
1073 
1074 	if (!result) {
1075 		spin_unlock_irqrestore(&a->queue_lock, flags);
1076 		return FAILED;
1077 	} else if (result == 2 && (queue == &a->defer_list)) {
1078 		queue = &a->active_list;
1079 		goto check_active_queue;
1080 	}
1081 
1082 	spin_unlock_irqrestore(&a->queue_lock, flags);
1083 
1084 	if (abort_request) {
1085 		u8 task_management_status = RS_PENDING;
1086 
1087 		/*
1088 		 * the request is already active, so we need to tell
1089 		 * the firmware to abort it and wait for the response.
1090 		 */
1091 
1092 		abort_request->comp_cb = complete_task_management_request;
1093 		abort_request->task_management_status_ptr =
1094 			&task_management_status;
1095 
1096 		esas2r_start_request(a, abort_request);
1097 
1098 		if (atomic_read(&a->disable_cnt) == 0)
1099 			esas2r_do_deferred_processes(a);
1100 
1101 		while (task_management_status == RS_PENDING)
1102 			msleep(10);
1103 
1104 		/*
1105 		 * Once we get here, the original request will have been
1106 		 * completed by the firmware and the abort request will have
1107 		 * been cleaned up.  we're done!
1108 		 */
1109 
1110 		return SUCCESS;
1111 	}
1112 
1113 	/*
1114 	 * If we get here, either we found the inactive request and
1115 	 * freed it, or we didn't find it at all.  Either way, success!
1116 	 */
1117 
1118 	cmd->result = DID_ABORT << 16;
1119 
1120 	scsi_set_resid(cmd, 0);
1121 
1122 	cmd->scsi_done(cmd);
1123 
1124 	return SUCCESS;
1125 }
1126 
1127 static int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset)
1128 {
1129 	struct esas2r_adapter *a =
1130 		(struct esas2r_adapter *)cmd->device->host->hostdata;
1131 
1132 	if (test_bit(AF_DEGRADED_MODE, &a->flags))
1133 		return FAILED;
1134 
1135 	if (host_reset)
1136 		esas2r_reset_adapter(a);
1137 	else
1138 		esas2r_reset_bus(a);
1139 
1140 	/* above call sets the AF_OS_RESET flag.  wait for it to clear. */
1141 
1142 	while (test_bit(AF_OS_RESET, &a->flags)) {
1143 		msleep(10);
1144 
1145 		if (test_bit(AF_DEGRADED_MODE, &a->flags))
1146 			return FAILED;
1147 	}
1148 
1149 	if (test_bit(AF_DEGRADED_MODE, &a->flags))
1150 		return FAILED;
1151 
1152 	return SUCCESS;
1153 }
1154 
1155 int esas2r_host_reset(struct scsi_cmnd *cmd)
1156 {
1157 	esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd);
1158 
1159 	return esas2r_host_bus_reset(cmd, true);
1160 }
1161 
1162 int esas2r_bus_reset(struct scsi_cmnd *cmd)
1163 {
1164 	esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd);
1165 
1166 	return esas2r_host_bus_reset(cmd, false);
1167 }
1168 
1169 static int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset)
1170 {
1171 	struct esas2r_adapter *a =
1172 		(struct esas2r_adapter *)cmd->device->host->hostdata;
1173 	struct esas2r_request *rq;
1174 	u8 task_management_status = RS_PENDING;
1175 	bool completed;
1176 
1177 	if (test_bit(AF_DEGRADED_MODE, &a->flags))
1178 		return FAILED;
1179 
1180 retry:
1181 	rq = esas2r_alloc_request(a);
1182 	if (rq == NULL) {
1183 		if (target_reset) {
1184 			esas2r_log(ESAS2R_LOG_CRIT,
1185 				   "unable to allocate a request for a "
1186 				   "target reset (%d)!",
1187 				   cmd->device->id);
1188 		} else {
1189 			esas2r_log(ESAS2R_LOG_CRIT,
1190 				   "unable to allocate a request for a "
1191 				   "device reset (%d:%d)!",
1192 				   cmd->device->id,
1193 				   cmd->device->lun);
1194 		}
1195 
1196 
1197 		return FAILED;
1198 	}
1199 
1200 	rq->target_id = cmd->device->id;
1201 	rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
1202 	rq->req_stat = RS_PENDING;
1203 
1204 	rq->comp_cb = complete_task_management_request;
1205 	rq->task_management_status_ptr = &task_management_status;
1206 
1207 	if (target_reset) {
1208 		esas2r_debug("issuing target reset (%p) to id %d", rq,
1209 			     cmd->device->id);
1210 		completed = esas2r_send_task_mgmt(a, rq, 0x20);
1211 	} else {
1212 		esas2r_debug("issuing device reset (%p) to id %d lun %d", rq,
1213 			     cmd->device->id, cmd->device->lun);
1214 		completed = esas2r_send_task_mgmt(a, rq, 0x10);
1215 	}
1216 
1217 	if (completed) {
1218 		/* Task management cmd completed right away, need to free it. */
1219 
1220 		esas2r_free_request(a, rq);
1221 	} else {
1222 		/*
1223 		 * Wait for firmware to complete the request.  Completion
1224 		 * callback will free it.
1225 		 */
1226 		while (task_management_status == RS_PENDING)
1227 			msleep(10);
1228 	}
1229 
1230 	if (test_bit(AF_DEGRADED_MODE, &a->flags))
1231 		return FAILED;
1232 
1233 	if (task_management_status == RS_BUSY) {
1234 		/*
1235 		 * Busy, probably because we are flashing.  Wait a bit and
1236 		 * try again.
1237 		 */
1238 		msleep(100);
1239 		goto retry;
1240 	}
1241 
1242 	return SUCCESS;
1243 }
1244 
1245 int esas2r_device_reset(struct scsi_cmnd *cmd)
1246 {
1247 	esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd);
1248 
1249 	return esas2r_dev_targ_reset(cmd, false);
1250 
1251 }
1252 
1253 int esas2r_target_reset(struct scsi_cmnd *cmd)
1254 {
1255 	esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd);
1256 
1257 	return esas2r_dev_targ_reset(cmd, true);
1258 }
1259 
1260 void esas2r_log_request_failure(struct esas2r_adapter *a,
1261 				struct esas2r_request *rq)
1262 {
1263 	u8 reqstatus = rq->req_stat;
1264 
1265 	if (reqstatus == RS_SUCCESS)
1266 		return;
1267 
1268 	if (rq->vrq->scsi.function == VDA_FUNC_SCSI) {
1269 		if (reqstatus == RS_SCSI_ERROR) {
1270 			if (rq->func_rsp.scsi_rsp.sense_len >= 13) {
1271 				esas2r_log(ESAS2R_LOG_WARN,
1272 					   "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
1273 					   rq->sense_buf[2], rq->sense_buf[12],
1274 					   rq->sense_buf[13],
1275 					   rq->vrq->scsi.cdb[0]);
1276 			} else {
1277 				esas2r_log(ESAS2R_LOG_WARN,
1278 					   "request failure - SCSI error CDB:%x\n",
1279 					   rq->vrq->scsi.cdb[0]);
1280 			}
1281 		} else if ((rq->vrq->scsi.cdb[0] != INQUIRY
1282 			    && rq->vrq->scsi.cdb[0] != REPORT_LUNS)
1283 			   || (reqstatus != RS_SEL
1284 			       && reqstatus != RS_SEL2)) {
1285 			if ((reqstatus == RS_UNDERRUN) &&
1286 			    (rq->vrq->scsi.cdb[0] == INQUIRY)) {
1287 				/* Don't log inquiry underruns */
1288 			} else {
1289 				esas2r_log(ESAS2R_LOG_WARN,
1290 					   "request failure - cdb:%x reqstatus:%d target:%d",
1291 					   rq->vrq->scsi.cdb[0], reqstatus,
1292 					   rq->target_id);
1293 			}
1294 		}
1295 	}
1296 }
1297 
1298 void esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1299 {
1300 	u32 starttime;
1301 	u32 timeout;
1302 
1303 	starttime = jiffies_to_msecs(jiffies);
1304 	timeout = rq->timeout ? rq->timeout : 5000;
1305 
1306 	while (true) {
1307 		esas2r_polled_interrupt(a);
1308 
1309 		if (rq->req_stat != RS_STARTED)
1310 			break;
1311 
1312 		schedule_timeout_interruptible(msecs_to_jiffies(100));
1313 
1314 		if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
1315 			esas2r_hdebug("request TMO");
1316 			esas2r_bugon();
1317 
1318 			rq->req_stat = RS_TIMEOUT;
1319 
1320 			esas2r_local_reset_adapter(a);
1321 			return;
1322 		}
1323 	}
1324 }
1325 
1326 u32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo)
1327 {
1328 	u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1);
1329 	u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE;
1330 
1331 	if (a->window_base != base) {
1332 		esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP,
1333 					    base | MVRPW1R_ENABLE);
1334 		esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP);
1335 		a->window_base = base;
1336 	}
1337 
1338 	return offset;
1339 }
1340 
1341 /* Read a block of data from chip memory */
1342 bool esas2r_read_mem_block(struct esas2r_adapter *a,
1343 			   void *to,
1344 			   u32 from,
1345 			   u32 size)
1346 {
1347 	u8 *end = (u8 *)to;
1348 
1349 	while (size) {
1350 		u32 len;
1351 		u32 offset;
1352 		u32 iatvr;
1353 
1354 		iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE);
1355 
1356 		esas2r_map_data_window(a, iatvr);
1357 
1358 		offset = from & (MW_DATA_WINDOW_SIZE - 1);
1359 		len = size;
1360 
1361 		if (len > MW_DATA_WINDOW_SIZE - offset)
1362 			len = MW_DATA_WINDOW_SIZE - offset;
1363 
1364 		from += len;
1365 		size -= len;
1366 
1367 		while (len--) {
1368 			*end++ = esas2r_read_data_byte(a, offset);
1369 			offset++;
1370 		}
1371 	}
1372 
1373 	return true;
1374 }
1375 
1376 void esas2r_nuxi_mgt_data(u8 function, void *data)
1377 {
1378 	struct atto_vda_grp_info *g;
1379 	struct atto_vda_devinfo *d;
1380 	struct atto_vdapart_info *p;
1381 	struct atto_vda_dh_info *h;
1382 	struct atto_vda_metrics_info *m;
1383 	struct atto_vda_schedule_info *s;
1384 	struct atto_vda_buzzer_info *b;
1385 	u8 i;
1386 
1387 	switch (function) {
1388 	case VDAMGT_BUZZER_INFO:
1389 	case VDAMGT_BUZZER_SET:
1390 
1391 		b = (struct atto_vda_buzzer_info *)data;
1392 
1393 		b->duration = le32_to_cpu(b->duration);
1394 		break;
1395 
1396 	case VDAMGT_SCHEDULE_INFO:
1397 	case VDAMGT_SCHEDULE_EVENT:
1398 
1399 		s = (struct atto_vda_schedule_info *)data;
1400 
1401 		s->id = le32_to_cpu(s->id);
1402 
1403 		break;
1404 
1405 	case VDAMGT_DEV_INFO:
1406 	case VDAMGT_DEV_CLEAN:
1407 	case VDAMGT_DEV_PT_INFO:
1408 	case VDAMGT_DEV_FEATURES:
1409 	case VDAMGT_DEV_PT_FEATURES:
1410 	case VDAMGT_DEV_OPERATION:
1411 
1412 		d = (struct atto_vda_devinfo *)data;
1413 
1414 		d->capacity = le64_to_cpu(d->capacity);
1415 		d->block_size = le32_to_cpu(d->block_size);
1416 		d->ses_dev_index = le16_to_cpu(d->ses_dev_index);
1417 		d->target_id = le16_to_cpu(d->target_id);
1418 		d->lun = le16_to_cpu(d->lun);
1419 		d->features = le16_to_cpu(d->features);
1420 		break;
1421 
1422 	case VDAMGT_GRP_INFO:
1423 	case VDAMGT_GRP_CREATE:
1424 	case VDAMGT_GRP_DELETE:
1425 	case VDAMGT_ADD_STORAGE:
1426 	case VDAMGT_MEMBER_ADD:
1427 	case VDAMGT_GRP_COMMIT:
1428 	case VDAMGT_GRP_REBUILD:
1429 	case VDAMGT_GRP_COMMIT_INIT:
1430 	case VDAMGT_QUICK_RAID:
1431 	case VDAMGT_GRP_FEATURES:
1432 	case VDAMGT_GRP_COMMIT_INIT_AUTOMAP:
1433 	case VDAMGT_QUICK_RAID_INIT_AUTOMAP:
1434 	case VDAMGT_SPARE_LIST:
1435 	case VDAMGT_SPARE_ADD:
1436 	case VDAMGT_SPARE_REMOVE:
1437 	case VDAMGT_LOCAL_SPARE_ADD:
1438 	case VDAMGT_GRP_OPERATION:
1439 
1440 		g = (struct atto_vda_grp_info *)data;
1441 
1442 		g->capacity = le64_to_cpu(g->capacity);
1443 		g->block_size = le32_to_cpu(g->block_size);
1444 		g->interleave = le32_to_cpu(g->interleave);
1445 		g->features = le16_to_cpu(g->features);
1446 
1447 		for (i = 0; i < 32; i++)
1448 			g->members[i] = le16_to_cpu(g->members[i]);
1449 
1450 		break;
1451 
1452 	case VDAMGT_PART_INFO:
1453 	case VDAMGT_PART_MAP:
1454 	case VDAMGT_PART_UNMAP:
1455 	case VDAMGT_PART_AUTOMAP:
1456 	case VDAMGT_PART_SPLIT:
1457 	case VDAMGT_PART_MERGE:
1458 
1459 		p = (struct atto_vdapart_info *)data;
1460 
1461 		p->part_size = le64_to_cpu(p->part_size);
1462 		p->start_lba = le32_to_cpu(p->start_lba);
1463 		p->block_size = le32_to_cpu(p->block_size);
1464 		p->target_id = le16_to_cpu(p->target_id);
1465 		break;
1466 
1467 	case VDAMGT_DEV_HEALTH_REQ:
1468 
1469 		h = (struct atto_vda_dh_info *)data;
1470 
1471 		h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt);
1472 		h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt);
1473 		break;
1474 
1475 	case VDAMGT_DEV_METRICS:
1476 
1477 		m = (struct atto_vda_metrics_info *)data;
1478 
1479 		for (i = 0; i < 32; i++)
1480 			m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]);
1481 
1482 		break;
1483 
1484 	default:
1485 		break;
1486 	}
1487 }
1488 
1489 void esas2r_nuxi_cfg_data(u8 function, void *data)
1490 {
1491 	struct atto_vda_cfg_init *ci;
1492 
1493 	switch (function) {
1494 	case VDA_CFG_INIT:
1495 	case VDA_CFG_GET_INIT:
1496 	case VDA_CFG_GET_INIT2:
1497 
1498 		ci = (struct atto_vda_cfg_init *)data;
1499 
1500 		ci->date_time.year = le16_to_cpu(ci->date_time.year);
1501 		ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size);
1502 		ci->vda_version = le32_to_cpu(ci->vda_version);
1503 		ci->epoch_time = le32_to_cpu(ci->epoch_time);
1504 		ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel);
1505 		ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend);
1506 		break;
1507 
1508 	default:
1509 		break;
1510 	}
1511 }
1512 
1513 void esas2r_nuxi_ae_data(union atto_vda_ae *ae)
1514 {
1515 	struct atto_vda_ae_raid *r = &ae->raid;
1516 	struct atto_vda_ae_lu *l = &ae->lu;
1517 
1518 	switch (ae->hdr.bytype) {
1519 	case VDAAE_HDR_TYPE_RAID:
1520 
1521 		r->dwflags = le32_to_cpu(r->dwflags);
1522 		break;
1523 
1524 	case VDAAE_HDR_TYPE_LU:
1525 
1526 		l->dwevent = le32_to_cpu(l->dwevent);
1527 		l->wphys_target_id = le16_to_cpu(l->wphys_target_id);
1528 		l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id);
1529 
1530 		if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id)
1531 		    + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) {
1532 			l->id.tgtlun_raid.dwinterleave
1533 				= le32_to_cpu(l->id.tgtlun_raid.dwinterleave);
1534 			l->id.tgtlun_raid.dwblock_size
1535 				= le32_to_cpu(l->id.tgtlun_raid.dwblock_size);
1536 		}
1537 
1538 		break;
1539 
1540 	case VDAAE_HDR_TYPE_DISK:
1541 	default:
1542 		break;
1543 	}
1544 }
1545 
1546 void esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1547 {
1548 	unsigned long flags;
1549 
1550 	esas2r_rq_destroy_request(rq, a);
1551 	spin_lock_irqsave(&a->request_lock, flags);
1552 	list_add(&rq->comp_list, &a->avail_request);
1553 	spin_unlock_irqrestore(&a->request_lock, flags);
1554 }
1555 
1556 struct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a)
1557 {
1558 	struct esas2r_request *rq;
1559 	unsigned long flags;
1560 
1561 	spin_lock_irqsave(&a->request_lock, flags);
1562 
1563 	if (unlikely(list_empty(&a->avail_request))) {
1564 		spin_unlock_irqrestore(&a->request_lock, flags);
1565 		return NULL;
1566 	}
1567 
1568 	rq = list_first_entry(&a->avail_request, struct esas2r_request,
1569 			      comp_list);
1570 	list_del(&rq->comp_list);
1571 	spin_unlock_irqrestore(&a->request_lock, flags);
1572 	esas2r_rq_init_request(rq, a);
1573 
1574 	return rq;
1575 
1576 }
1577 
1578 void esas2r_complete_request_cb(struct esas2r_adapter *a,
1579 				struct esas2r_request *rq)
1580 {
1581 	esas2r_debug("completing request %p\n", rq);
1582 
1583 	scsi_dma_unmap(rq->cmd);
1584 
1585 	if (unlikely(rq->req_stat != RS_SUCCESS)) {
1586 		esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id,
1587 			     rq->req_stat,
1588 			     rq->func_rsp.scsi_rsp.scsi_stat,
1589 			     rq->cmd);
1590 
1591 		rq->cmd->result =
1592 			((esas2r_req_status_to_error(rq->req_stat) << 16)
1593 			 | (rq->func_rsp.scsi_rsp.scsi_stat & STATUS_MASK));
1594 
1595 		if (rq->req_stat == RS_UNDERRUN)
1596 			scsi_set_resid(rq->cmd,
1597 				       le32_to_cpu(rq->func_rsp.scsi_rsp.
1598 						   residual_length));
1599 		else
1600 			scsi_set_resid(rq->cmd, 0);
1601 	}
1602 
1603 	rq->cmd->scsi_done(rq->cmd);
1604 
1605 	esas2r_free_request(a, rq);
1606 }
1607 
1608 /* Run tasklet to handle stuff outside of interrupt context. */
1609 void esas2r_adapter_tasklet(unsigned long context)
1610 {
1611 	struct esas2r_adapter *a = (struct esas2r_adapter *)context;
1612 
1613 	if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) {
1614 		clear_bit(AF2_TIMER_TICK, &a->flags2);
1615 		esas2r_timer_tick(a);
1616 	}
1617 
1618 	if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) {
1619 		clear_bit(AF2_INT_PENDING, &a->flags2);
1620 		esas2r_adapter_interrupt(a);
1621 	}
1622 
1623 	if (esas2r_is_tasklet_pending(a))
1624 		esas2r_do_tasklet_tasks(a);
1625 
1626 	if (esas2r_is_tasklet_pending(a)
1627 	    || (test_bit(AF2_INT_PENDING, &a->flags2))
1628 	    || (test_bit(AF2_TIMER_TICK, &a->flags2))) {
1629 		clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1630 		esas2r_schedule_tasklet(a);
1631 	} else {
1632 		clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1633 	}
1634 }
1635 
1636 static void esas2r_timer_callback(unsigned long context);
1637 
1638 void esas2r_kickoff_timer(struct esas2r_adapter *a)
1639 {
1640 	init_timer(&a->timer);
1641 
1642 	a->timer.function = esas2r_timer_callback;
1643 	a->timer.data = (unsigned long)a;
1644 	a->timer.expires = jiffies +
1645 			   msecs_to_jiffies(100);
1646 
1647 	add_timer(&a->timer);
1648 }
1649 
1650 static void esas2r_timer_callback(unsigned long context)
1651 {
1652 	struct esas2r_adapter *a = (struct esas2r_adapter *)context;
1653 
1654 	set_bit(AF2_TIMER_TICK, &a->flags2);
1655 
1656 	esas2r_schedule_tasklet(a);
1657 
1658 	esas2r_kickoff_timer(a);
1659 }
1660 
1661 /*
1662  * Firmware events need to be handled outside of interrupt context
1663  * so we schedule a delayed_work to handle them.
1664  */
1665 
1666 static void
1667 esas2r_free_fw_event(struct esas2r_fw_event_work *fw_event)
1668 {
1669 	unsigned long flags;
1670 	struct esas2r_adapter *a = fw_event->a;
1671 
1672 	spin_lock_irqsave(&a->fw_event_lock, flags);
1673 	list_del(&fw_event->list);
1674 	kfree(fw_event);
1675 	spin_unlock_irqrestore(&a->fw_event_lock, flags);
1676 }
1677 
1678 void
1679 esas2r_fw_event_off(struct esas2r_adapter *a)
1680 {
1681 	unsigned long flags;
1682 
1683 	spin_lock_irqsave(&a->fw_event_lock, flags);
1684 	a->fw_events_off = 1;
1685 	spin_unlock_irqrestore(&a->fw_event_lock, flags);
1686 }
1687 
1688 void
1689 esas2r_fw_event_on(struct esas2r_adapter *a)
1690 {
1691 	unsigned long flags;
1692 
1693 	spin_lock_irqsave(&a->fw_event_lock, flags);
1694 	a->fw_events_off = 0;
1695 	spin_unlock_irqrestore(&a->fw_event_lock, flags);
1696 }
1697 
1698 static void esas2r_add_device(struct esas2r_adapter *a, u16 target_id)
1699 {
1700 	int ret;
1701 	struct scsi_device *scsi_dev;
1702 
1703 	scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1704 
1705 	if (scsi_dev) {
1706 		esas2r_log_dev(
1707 			ESAS2R_LOG_WARN,
1708 			&(scsi_dev->
1709 			  sdev_gendev),
1710 			"scsi device already exists at id %d", target_id);
1711 
1712 		scsi_device_put(scsi_dev);
1713 	} else {
1714 		esas2r_log_dev(
1715 			ESAS2R_LOG_INFO,
1716 			&(a->host->
1717 			  shost_gendev),
1718 			"scsi_add_device() called for 0:%d:0",
1719 			target_id);
1720 
1721 		ret = scsi_add_device(a->host, 0, target_id, 0);
1722 		if (ret) {
1723 			esas2r_log_dev(
1724 				ESAS2R_LOG_CRIT,
1725 				&(a->host->
1726 				  shost_gendev),
1727 				"scsi_add_device failed with %d for id %d",
1728 				ret, target_id);
1729 		}
1730 	}
1731 }
1732 
1733 static void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id)
1734 {
1735 	struct scsi_device *scsi_dev;
1736 
1737 	scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1738 
1739 	if (scsi_dev) {
1740 		scsi_device_set_state(scsi_dev, SDEV_OFFLINE);
1741 
1742 		esas2r_log_dev(
1743 			ESAS2R_LOG_INFO,
1744 			&(scsi_dev->
1745 			  sdev_gendev),
1746 			"scsi_remove_device() called for 0:%d:0",
1747 			target_id);
1748 
1749 		scsi_remove_device(scsi_dev);
1750 
1751 		esas2r_log_dev(
1752 			ESAS2R_LOG_INFO,
1753 			&(scsi_dev->
1754 			  sdev_gendev),
1755 			"scsi_device_put() called");
1756 
1757 		scsi_device_put(scsi_dev);
1758 	} else {
1759 		esas2r_log_dev(
1760 			ESAS2R_LOG_WARN,
1761 			&(a->host->shost_gendev),
1762 			"no target found at id %d",
1763 			target_id);
1764 	}
1765 }
1766 
1767 /*
1768  * Sends a firmware asynchronous event to anyone who happens to be
1769  * listening on the defined ATTO VDA event ports.
1770  */
1771 static void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event)
1772 {
1773 	struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data;
1774 	char *type;
1775 
1776 	switch (ae->vda_ae.hdr.bytype) {
1777 	case VDAAE_HDR_TYPE_RAID:
1778 		type = "RAID group state change";
1779 		break;
1780 
1781 	case VDAAE_HDR_TYPE_LU:
1782 		type = "Mapped destination LU change";
1783 		break;
1784 
1785 	case VDAAE_HDR_TYPE_DISK:
1786 		type = "Physical disk inventory change";
1787 		break;
1788 
1789 	case VDAAE_HDR_TYPE_RESET:
1790 		type = "Firmware reset";
1791 		break;
1792 
1793 	case VDAAE_HDR_TYPE_LOG_INFO:
1794 		type = "Event Log message (INFO level)";
1795 		break;
1796 
1797 	case VDAAE_HDR_TYPE_LOG_WARN:
1798 		type = "Event Log message (WARN level)";
1799 		break;
1800 
1801 	case VDAAE_HDR_TYPE_LOG_CRIT:
1802 		type = "Event Log message (CRIT level)";
1803 		break;
1804 
1805 	case VDAAE_HDR_TYPE_LOG_FAIL:
1806 		type = "Event Log message (FAIL level)";
1807 		break;
1808 
1809 	case VDAAE_HDR_TYPE_NVC:
1810 		type = "NVCache change";
1811 		break;
1812 
1813 	case VDAAE_HDR_TYPE_TLG_INFO:
1814 		type = "Time stamped log message (INFO level)";
1815 		break;
1816 
1817 	case VDAAE_HDR_TYPE_TLG_WARN:
1818 		type = "Time stamped log message (WARN level)";
1819 		break;
1820 
1821 	case VDAAE_HDR_TYPE_TLG_CRIT:
1822 		type = "Time stamped log message (CRIT level)";
1823 		break;
1824 
1825 	case VDAAE_HDR_TYPE_PWRMGT:
1826 		type = "Power management";
1827 		break;
1828 
1829 	case VDAAE_HDR_TYPE_MUTE:
1830 		type = "Mute button pressed";
1831 		break;
1832 
1833 	case VDAAE_HDR_TYPE_DEV:
1834 		type = "Device attribute change";
1835 		break;
1836 
1837 	default:
1838 		type = "Unknown";
1839 		break;
1840 	}
1841 
1842 	esas2r_log(ESAS2R_LOG_WARN,
1843 		   "An async event of type \"%s\" was received from the firmware.  The event contents are:",
1844 		   type);
1845 	esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae,
1846 			   ae->vda_ae.hdr.bylength);
1847 
1848 }
1849 
1850 static void
1851 esas2r_firmware_event_work(struct work_struct *work)
1852 {
1853 	struct esas2r_fw_event_work *fw_event =
1854 		container_of(work, struct esas2r_fw_event_work, work.work);
1855 
1856 	struct esas2r_adapter *a = fw_event->a;
1857 
1858 	u16 target_id = *(u16 *)&fw_event->data[0];
1859 
1860 	if (a->fw_events_off)
1861 		goto done;
1862 
1863 	switch (fw_event->type) {
1864 	case fw_event_null:
1865 		break; /* do nothing */
1866 
1867 	case fw_event_lun_change:
1868 		esas2r_remove_device(a, target_id);
1869 		esas2r_add_device(a, target_id);
1870 		break;
1871 
1872 	case fw_event_present:
1873 		esas2r_add_device(a, target_id);
1874 		break;
1875 
1876 	case fw_event_not_present:
1877 		esas2r_remove_device(a, target_id);
1878 		break;
1879 
1880 	case fw_event_vda_ae:
1881 		esas2r_send_ae_event(fw_event);
1882 		break;
1883 	}
1884 
1885 done:
1886 	esas2r_free_fw_event(fw_event);
1887 }
1888 
1889 void esas2r_queue_fw_event(struct esas2r_adapter *a,
1890 			   enum fw_event_type type,
1891 			   void *data,
1892 			   int data_sz)
1893 {
1894 	struct esas2r_fw_event_work *fw_event;
1895 	unsigned long flags;
1896 
1897 	fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC);
1898 	if (!fw_event) {
1899 		esas2r_log(ESAS2R_LOG_WARN,
1900 			   "esas2r_queue_fw_event failed to alloc");
1901 		return;
1902 	}
1903 
1904 	if (type == fw_event_vda_ae) {
1905 		struct esas2r_vda_ae *ae =
1906 			(struct esas2r_vda_ae *)fw_event->data;
1907 
1908 		ae->signature = ESAS2R_VDA_EVENT_SIG;
1909 		ae->bus_number = a->pcid->bus->number;
1910 		ae->devfn = a->pcid->devfn;
1911 		memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae));
1912 	} else {
1913 		memcpy(fw_event->data, data, data_sz);
1914 	}
1915 
1916 	fw_event->type = type;
1917 	fw_event->a = a;
1918 
1919 	spin_lock_irqsave(&a->fw_event_lock, flags);
1920 	list_add_tail(&fw_event->list, &a->fw_event_list);
1921 	INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work);
1922 	queue_delayed_work_on(
1923 		smp_processor_id(), a->fw_event_q, &fw_event->work,
1924 		msecs_to_jiffies(1));
1925 	spin_unlock_irqrestore(&a->fw_event_lock, flags);
1926 }
1927 
1928 void esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id,
1929 				 u8 state)
1930 {
1931 	if (state == TS_LUN_CHANGE)
1932 		esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id,
1933 				      sizeof(targ_id));
1934 	else if (state == TS_PRESENT)
1935 		esas2r_queue_fw_event(a, fw_event_present, &targ_id,
1936 				      sizeof(targ_id));
1937 	else if (state == TS_NOT_PRESENT)
1938 		esas2r_queue_fw_event(a, fw_event_not_present, &targ_id,
1939 				      sizeof(targ_id));
1940 }
1941 
1942 /* Translate status to a Linux SCSI mid-layer error code */
1943 int esas2r_req_status_to_error(u8 req_stat)
1944 {
1945 	switch (req_stat) {
1946 	case RS_OVERRUN:
1947 	case RS_UNDERRUN:
1948 	case RS_SUCCESS:
1949 	/*
1950 	 * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
1951 	 *       it will check the scsi_stat value in the completion anyway.
1952 	 */
1953 	case RS_SCSI_ERROR:
1954 		return DID_OK;
1955 
1956 	case RS_SEL:
1957 	case RS_SEL2:
1958 		return DID_NO_CONNECT;
1959 
1960 	case RS_RESET:
1961 		return DID_RESET;
1962 
1963 	case RS_ABORTED:
1964 		return DID_ABORT;
1965 
1966 	case RS_BUSY:
1967 		return DID_BUS_BUSY;
1968 	}
1969 
1970 	/* everything else is just an error. */
1971 
1972 	return DID_ERROR;
1973 }
1974 
1975 module_init(esas2r_init);
1976 module_exit(esas2r_exit);
1977