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