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