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