2 * linux/drivers/scsi/esas2r/esas2r_main.c
3 * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
5 * Copyright (c) 2001-2013 ATTO Technology, Inc.
6 * (mailto:linuxdrivers@attotech.com)
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.
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.
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.
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
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,
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);
51 /* global definitions */
53 static int found_adapters;
54 struct esas2r_adapter *esas2r_adapters[MAX_ADAPTERS];
56 #define ESAS2R_VDA_EVENT_PORT1 54414
57 #define ESAS2R_VDA_EVENT_PORT2 54415
58 #define ESAS2R_VDA_EVENT_SOCK_COUNT 2
60 static struct esas2r_adapter *esas2r_adapter_from_kobj(struct kobject *kobj)
62 struct device *dev = container_of(kobj, struct device, kobj);
63 struct Scsi_Host *host = class_to_shost(dev);
65 return (struct esas2r_adapter *)host->hostdata;
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)
72 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
74 return esas2r_read_fw(a, buf, off, count);
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)
81 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
83 return esas2r_write_fw(a, buf, off, count);
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)
90 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
92 return esas2r_read_fs(a, buf, off, count);
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)
99 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
100 int length = min(sizeof(struct esas2r_ioctl_fs), count);
103 result = esas2r_write_fs(a, buf, off, count);
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)
115 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
117 return esas2r_read_vda(a, buf, off, count);
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)
124 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
126 return esas2r_write_vda(a, buf, off, count);
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)
133 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
134 int length = min_t(size_t, sizeof(struct esas2r_sas_nvram), PAGE_SIZE);
136 memcpy(buf, a->nvram, length);
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)
144 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
145 struct esas2r_request *rq;
146 int result = -EFAULT;
148 rq = esas2r_alloc_request(a);
152 if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf))
155 esas2r_free_request(a, rq);
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)
164 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
166 esas2r_nvram_get_defaults(a, (struct esas2r_sas_nvram *)buf);
168 return sizeof(struct esas2r_sas_nvram);
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)
175 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
176 int length = min_t(size_t, sizeof(struct atto_ioctl), PAGE_SIZE);
178 if (!a->local_atto_ioctl)
181 if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS)
184 memcpy(buf, a->local_atto_ioctl, length);
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)
193 struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
194 int length = min(sizeof(struct atto_ioctl), count);
196 if (!a->local_atto_ioctl) {
197 a->local_atto_ioctl = kzalloc(sizeof(struct atto_ioctl),
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));
207 memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl));
208 memcpy(a->local_atto_ioctl, buf, length);
213 #define ESAS2R_RW_BIN_ATTR(_name) \
214 struct bin_attribute bin_attr_ ## _name = { \
216 { .name = __stringify(_name), .mode = S_IRUSR | S_IWUSR }, \
218 .read = read_ ## _name, \
219 .write = write_ ## _name }
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);
227 struct bin_attribute bin_attr_default_nvram = {
228 .attr = { .name = "default_nvram", .mode = S_IRUGO },
230 .read = read_default_nvram,
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,
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,
249 .sg_tablesize = SCSI_MAX_SG_SEGMENTS,
251 ESAS2R_DEFAULT_CMD_PER_LUN,
253 .unchecked_isa_dma = 0,
254 .use_clustering = ENABLE_CLUSTERING,
256 .proc_name = ESAS2R_DRVR_NAME,
257 .change_queue_depth = scsi_change_queue_depth,
258 .change_queue_type = scsi_change_queue_type,
259 .max_sectors = 0xFFFF,
263 int sgl_page_size = 512;
264 module_param(sgl_page_size, int, 0);
265 MODULE_PARM_DESC(sgl_page_size,
266 "Scatter/gather list (SGL) page size in number of S/G "
267 "entries. If your application is doing a lot of very large "
268 "transfers, you may want to increase the SGL page size. "
271 int num_sg_lists = 1024;
272 module_param(num_sg_lists, int, 0);
273 MODULE_PARM_DESC(num_sg_lists,
274 "Number of scatter/gather lists. Default 1024.");
276 int sg_tablesize = SCSI_MAX_SG_SEGMENTS;
277 module_param(sg_tablesize, int, 0);
278 MODULE_PARM_DESC(sg_tablesize,
279 "Maximum number of entries in a scatter/gather table.");
281 int num_requests = 256;
282 module_param(num_requests, int, 0);
283 MODULE_PARM_DESC(num_requests,
284 "Number of requests. Default 256.");
286 int num_ae_requests = 4;
287 module_param(num_ae_requests, int, 0);
288 MODULE_PARM_DESC(num_ae_requests,
289 "Number of VDA asynchromous event requests. Default 4.");
291 int cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN;
292 module_param(cmd_per_lun, int, 0);
293 MODULE_PARM_DESC(cmd_per_lun,
294 "Maximum number of commands per LUN. Default "
295 DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) ".");
298 module_param(can_queue, int, 0);
299 MODULE_PARM_DESC(can_queue,
300 "Maximum number of commands per adapter. Default 128.");
302 int esas2r_max_sectors = 0xFFFF;
303 module_param(esas2r_max_sectors, int, 0);
304 MODULE_PARM_DESC(esas2r_max_sectors,
305 "Maximum number of disk sectors in a single data transfer. "
306 "Default 65535 (largest possible setting).");
308 int interrupt_mode = 1;
309 module_param(interrupt_mode, int, 0);
310 MODULE_PARM_DESC(interrupt_mode,
311 "Defines the interrupt mode to use. 0 for legacy"
312 ", 1 for MSI. Default is MSI (1).");
314 static struct pci_device_id
315 esas2r_pci_table[] = {
316 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x0049,
319 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004A,
322 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004B,
325 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004C,
328 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004D,
331 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004E,
339 MODULE_DEVICE_TABLE(pci, esas2r_pci_table);
342 esas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id);
345 esas2r_remove(struct pci_dev *pcid);
347 static struct pci_driver
348 esas2r_pci_driver = {
349 .name = ESAS2R_DRVR_NAME,
350 .id_table = esas2r_pci_table,
351 .probe = esas2r_probe,
352 .remove = esas2r_remove,
353 .suspend = esas2r_suspend,
354 .resume = esas2r_resume,
357 static int esas2r_probe(struct pci_dev *pcid,
358 const struct pci_device_id *id)
360 struct Scsi_Host *host = NULL;
361 struct esas2r_adapter *a;
364 size_t host_alloc_size = sizeof(struct esas2r_adapter)
366 1) * sizeof(struct esas2r_request);
368 esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev),
369 "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
372 pcid->subsystem_vendor,
373 pcid->subsystem_device);
375 esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
376 "before pci_enable_device() "
378 pcid->enable_cnt.counter);
380 err = pci_enable_device(pcid);
382 esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev),
383 "pci_enable_device() FAIL (%d)",
388 esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
389 "pci_enable_device() OK");
390 esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
391 "after pci_enable_device() enable_cnt: %d",
392 pcid->enable_cnt.counter);
394 host = scsi_host_alloc(&driver_template, host_alloc_size);
396 esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL");
400 memset(host->hostdata, 0, host_alloc_size);
402 a = (struct esas2r_adapter *)host->hostdata;
404 esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host);
406 /* override max LUN and max target id */
408 host->max_id = ESAS2R_MAX_ID + 1;
411 /* we can handle 16-byte CDbs */
413 host->max_cmd_len = 16;
415 host->can_queue = can_queue;
416 host->cmd_per_lun = cmd_per_lun;
417 host->this_id = host->max_id + 1;
418 host->max_channel = 0;
419 host->unique_id = found_adapters;
420 host->sg_tablesize = sg_tablesize;
421 host->max_sectors = esas2r_max_sectors;
423 /* set to bus master for BIOses that don't do it for us */
425 esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called");
427 pci_set_master(pcid);
429 if (!esas2r_init_adapter(host, pcid, found_adapters)) {
430 esas2r_log(ESAS2R_LOG_CRIT,
431 "unable to initialize device at PCI bus %x:%x",
435 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
436 "scsi_host_put() called");
444 esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid,
447 pci_set_drvdata(pcid, host);
449 esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called");
451 err = scsi_add_host(host, &pcid->dev);
454 esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err);
455 esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev),
456 "scsi_add_host() FAIL");
458 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
459 "scsi_host_put() called");
463 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
464 "pci_set_drvdata(%p, NULL) called",
467 pci_set_drvdata(pcid, NULL);
473 esas2r_fw_event_on(a);
475 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
476 "scsi_scan_host() called");
478 scsi_scan_host(host);
480 /* Add sysfs binary files */
481 if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw))
482 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
483 "Failed to create sysfs binary file: fw");
485 a->sysfs_fw_created = 1;
487 if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs))
488 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
489 "Failed to create sysfs binary file: fs");
491 a->sysfs_fs_created = 1;
493 if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda))
494 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
495 "Failed to create sysfs binary file: vda");
497 a->sysfs_vda_created = 1;
499 if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw))
500 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
501 "Failed to create sysfs binary file: hw");
503 a->sysfs_hw_created = 1;
505 if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram))
506 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
507 "Failed to create sysfs binary file: live_nvram");
509 a->sysfs_live_nvram_created = 1;
511 if (sysfs_create_bin_file(&host->shost_dev.kobj,
512 &bin_attr_default_nvram))
513 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
514 "Failed to create sysfs binary file: default_nvram");
516 a->sysfs_default_nvram_created = 1;
523 static void esas2r_remove(struct pci_dev *pdev)
525 struct Scsi_Host *host;
529 esas2r_log(ESAS2R_LOG_WARN, "esas2r_remove pdev==NULL");
533 host = pci_get_drvdata(pdev);
537 * this can happen if pci_set_drvdata was already called
538 * to clear the host pointer. if this is the case, we
539 * are okay; this channel has already been cleaned up.
545 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
546 "esas2r_remove(%p) called; "
550 index = esas2r_cleanup(host);
553 esas2r_log_dev(ESAS2R_LOG_WARN, &(pdev->dev),
554 "unknown host in %s",
559 /* if this was the last adapter, clean up the rest of the driver */
561 if (found_adapters == 0)
562 esas2r_cleanup(NULL);
565 static int __init esas2r_init(void)
569 esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
571 /* verify valid parameters */
574 esas2r_log(ESAS2R_LOG_WARN,
575 "warning: can_queue must be at least 1, value "
578 } else if (can_queue > 2048) {
579 esas2r_log(ESAS2R_LOG_WARN,
580 "warning: can_queue must be no larger than 2048, "
585 if (cmd_per_lun < 1) {
586 esas2r_log(ESAS2R_LOG_WARN,
587 "warning: cmd_per_lun must be at least 1, value "
590 } else if (cmd_per_lun > 2048) {
591 esas2r_log(ESAS2R_LOG_WARN,
592 "warning: cmd_per_lun must be no larger than "
593 "2048, value forced.");
597 if (sg_tablesize < 32) {
598 esas2r_log(ESAS2R_LOG_WARN,
599 "warning: sg_tablesize must be at least 32, "
604 if (esas2r_max_sectors < 1) {
605 esas2r_log(ESAS2R_LOG_WARN,
606 "warning: esas2r_max_sectors must be at least "
608 esas2r_max_sectors = 1;
609 } else if (esas2r_max_sectors > 0xffff) {
610 esas2r_log(ESAS2R_LOG_WARN,
611 "warning: esas2r_max_sectors must be no larger "
612 "than 0xffff, value forced.");
613 esas2r_max_sectors = 0xffff;
616 sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1);
618 if (sgl_page_size < SGL_PG_SZ_MIN)
619 sgl_page_size = SGL_PG_SZ_MIN;
620 else if (sgl_page_size > SGL_PG_SZ_MAX)
621 sgl_page_size = SGL_PG_SZ_MAX;
623 if (num_sg_lists < NUM_SGL_MIN)
624 num_sg_lists = NUM_SGL_MIN;
625 else if (num_sg_lists > NUM_SGL_MAX)
626 num_sg_lists = NUM_SGL_MAX;
628 if (num_requests < NUM_REQ_MIN)
629 num_requests = NUM_REQ_MIN;
630 else if (num_requests > NUM_REQ_MAX)
631 num_requests = NUM_REQ_MAX;
633 if (num_ae_requests < NUM_AE_MIN)
634 num_ae_requests = NUM_AE_MIN;
635 else if (num_ae_requests > NUM_AE_MAX)
636 num_ae_requests = NUM_AE_MAX;
638 /* set up other globals */
640 for (i = 0; i < MAX_ADAPTERS; i++)
641 esas2r_adapters[i] = NULL;
645 driver_template.module = THIS_MODULE;
647 if (pci_register_driver(&esas2r_pci_driver) != 0)
648 esas2r_log(ESAS2R_LOG_CRIT, "pci_register_driver FAILED");
650 esas2r_log(ESAS2R_LOG_INFO, "pci_register_driver() OK");
652 if (!found_adapters) {
653 pci_unregister_driver(&esas2r_pci_driver);
654 esas2r_cleanup(NULL);
656 esas2r_log(ESAS2R_LOG_CRIT,
657 "driver will not be loaded because no ATTO "
658 "%s devices were found",
662 esas2r_log(ESAS2R_LOG_INFO, "found %d adapters",
669 /* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
670 static const struct file_operations esas2r_proc_fops = {
671 .compat_ioctl = esas2r_proc_ioctl,
672 .unlocked_ioctl = esas2r_proc_ioctl,
675 static struct Scsi_Host *esas2r_proc_host;
676 static int esas2r_proc_major;
678 long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
680 return esas2r_ioctl_handler(esas2r_proc_host->hostdata,
681 (int)cmd, (void __user *)arg);
684 static void __exit esas2r_exit(void)
686 esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
688 if (esas2r_proc_major > 0) {
689 esas2r_log(ESAS2R_LOG_INFO, "unregister proc");
691 remove_proc_entry(ATTONODE_NAME,
692 esas2r_proc_host->hostt->proc_dir);
693 unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME);
695 esas2r_proc_major = 0;
698 esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called");
700 pci_unregister_driver(&esas2r_pci_driver);
703 int esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh)
705 struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
707 struct esas2r_target *t;
710 esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no);
712 seq_printf(m, ESAS2R_LONGNAME "\n"
713 "Driver version: "ESAS2R_VERSION_STR "\n"
714 "Flash version: %s\n"
715 "Firmware version: %s\n"
716 "Copyright "ESAS2R_COPYRIGHT_YEARS "\n"
717 "http://www.attotech.com\n"
720 a->fw_rev[0] ? a->fw_rev : "(none)");
723 seq_printf(m, "Adapter information:\n"
724 "--------------------\n"
726 "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
727 esas2r_get_model_name(a),
728 a->nvram->sas_addr[0],
729 a->nvram->sas_addr[1],
730 a->nvram->sas_addr[2],
731 a->nvram->sas_addr[3],
732 a->nvram->sas_addr[4],
733 a->nvram->sas_addr[5],
734 a->nvram->sas_addr[6],
735 a->nvram->sas_addr[7]);
738 "Discovered devices:\n"
741 "---------------\n");
743 for (t = a->targetdb; t < a->targetdb_end; t++)
744 if (t->buffered_target_state == TS_PRESENT) {
745 seq_printf(m, " %3d %3d\n",
747 (u16)(uintptr_t)(t - a->targetdb));
751 seq_puts(m, "none\n");
758 int esas2r_release(struct Scsi_Host *sh)
760 esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
761 "esas2r_release() called");
765 free_irq(sh->irq, NULL);
770 const char *esas2r_info(struct Scsi_Host *sh)
772 struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
773 static char esas2r_info_str[512];
775 esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
776 "esas2r_info() called");
779 * if we haven't done so already, register as a char driver
780 * and stick a node under "/proc/scsi/esas2r/ATTOnode"
783 if (esas2r_proc_major <= 0) {
784 esas2r_proc_host = sh;
786 esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME,
789 esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev),
790 "register_chrdev (major %d)",
793 if (esas2r_proc_major > 0) {
794 struct proc_dir_entry *pde;
796 pde = proc_create(ATTONODE_NAME, 0,
801 esas2r_log_dev(ESAS2R_LOG_WARN,
803 "failed to create_proc_entry");
804 esas2r_proc_major = -1;
809 sprintf(esas2r_info_str,
810 ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
811 " driver version: "ESAS2R_VERSION_STR " firmware version: "
813 a->pcid->bus->number, a->pcid->devfn, a->pcid->irq,
814 a->fw_rev[0] ? a->fw_rev : "(none)");
816 return esas2r_info_str;
819 /* Callback for building a request scatter/gather list */
820 static u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr)
824 if (likely(sgc->cur_offset == sgc->exp_offset)) {
826 * the normal case: caller used all bytes from previous call, so
827 * expected offset is the same as the current offset.
830 if (sgc->sgel_count < sgc->num_sgel) {
831 /* retrieve next segment, except for first time */
832 if (sgc->exp_offset > (u8 *)0) {
833 /* advance current segment */
834 sgc->cur_sgel = sg_next(sgc->cur_sgel);
839 len = sg_dma_len(sgc->cur_sgel);
840 (*addr) = sg_dma_address(sgc->cur_sgel);
842 /* save the total # bytes returned to caller so far */
843 sgc->exp_offset += len;
848 } else if (sgc->cur_offset < sgc->exp_offset) {
850 * caller did not use all bytes from previous call. need to
851 * compute the address based on current segment.
854 len = sg_dma_len(sgc->cur_sgel);
855 (*addr) = sg_dma_address(sgc->cur_sgel);
857 sgc->exp_offset -= len;
859 /* calculate PA based on prev segment address and offsets */
861 (sgc->cur_offset - sgc->exp_offset);
863 sgc->exp_offset += len;
865 /* re-calculate length based on offset */
867 sgc->exp_offset - sgc->cur_offset);
868 } else { /* if ( sgc->cur_offset > sgc->exp_offset ) */
870 * we don't expect the caller to skip ahead.
871 * cur_offset will never exceed the len we return
879 int esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
881 struct esas2r_adapter *a =
882 (struct esas2r_adapter *)cmd->device->host->hostdata;
883 struct esas2r_request *rq;
884 struct esas2r_sg_context sgc;
887 /* Assume success, if it fails we will fix the result later. */
888 cmd->result = DID_OK << 16;
890 if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) {
891 cmd->result = DID_NO_CONNECT << 16;
896 rq = esas2r_alloc_request(a);
897 if (unlikely(rq == NULL)) {
898 esas2r_debug("esas2r_alloc_request failed");
899 return SCSI_MLQUEUE_HOST_BUSY;
903 bufflen = scsi_bufflen(cmd);
905 if (likely(bufflen != 0)) {
906 if (cmd->sc_data_direction == DMA_TO_DEVICE)
907 rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
908 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
909 rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
912 memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len);
913 rq->vrq->scsi.length = cpu_to_le32(bufflen);
914 rq->target_id = cmd->device->id;
915 rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
916 rq->sense_buf = cmd->sense_buffer;
917 rq->sense_len = SCSI_SENSE_BUFFERSIZE;
919 esas2r_sgc_init(&sgc, a, rq, NULL);
921 sgc.length = bufflen;
922 sgc.cur_offset = NULL;
924 sgc.cur_sgel = scsi_sglist(cmd);
925 sgc.exp_offset = NULL;
926 sgc.num_sgel = scsi_dma_map(cmd);
929 if (unlikely(sgc.num_sgel < 0)) {
930 esas2r_free_request(a, rq);
931 return SCSI_MLQUEUE_HOST_BUSY;
934 sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc;
936 if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) {
938 esas2r_free_request(a, rq);
939 return SCSI_MLQUEUE_HOST_BUSY;
942 esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id,
943 (int)cmd->device->lun);
945 esas2r_start_request(a, rq);
950 static void complete_task_management_request(struct esas2r_adapter *a,
951 struct esas2r_request *rq)
953 (*rq->task_management_status_ptr) = rq->req_stat;
954 esas2r_free_request(a, rq);
958 * Searches the specified queue for the specified queue for the command
962 * @param [in] abort_request
965 * @return 0 on failure, 1 if command was not found, 2 if command was found
967 static int esas2r_check_active_queue(struct esas2r_adapter *a,
968 struct esas2r_request **abort_request,
969 struct scsi_cmnd *cmd,
970 struct list_head *queue)
973 struct esas2r_request *ar = *abort_request;
974 struct esas2r_request *rq;
975 struct list_head *element, *next;
977 list_for_each_safe(element, next, queue) {
979 rq = list_entry(element, struct esas2r_request, req_list);
981 if (rq->cmd == cmd) {
983 /* Found the request. See what to do with it. */
984 if (queue == &a->active_list) {
986 * We are searching the active queue, which
987 * means that we need to send an abort request
990 ar = esas2r_alloc_request(a);
992 esas2r_log_dev(ESAS2R_LOG_WARN,
993 &(a->host->shost_gendev),
994 "unable to allocate an abort request for cmd %p",
996 return 0; /* Failure */
1000 * Task management request must be formatted
1004 ar->vrq->scsi.length = 0;
1005 ar->target_id = rq->target_id;
1006 ar->vrq->scsi.flags |= cpu_to_le32(
1007 (u8)le32_to_cpu(rq->vrq->scsi.flags));
1009 memset(ar->vrq->scsi.cdb, 0,
1010 sizeof(ar->vrq->scsi.cdb));
1012 ar->vrq->scsi.flags |= cpu_to_le32(
1014 ar->vrq->scsi.u.abort_handle =
1015 rq->vrq->scsi.handle;
1018 * The request is pending but not active on
1019 * the firmware. Just free it now and we'll
1020 * report the successful abort below.
1022 list_del_init(&rq->req_list);
1023 esas2r_free_request(a, rq);
1033 return 1; /* Not found */
1035 return 2; /* found */
1040 int esas2r_eh_abort(struct scsi_cmnd *cmd)
1042 struct esas2r_adapter *a =
1043 (struct esas2r_adapter *)cmd->device->host->hostdata;
1044 struct esas2r_request *abort_request = NULL;
1045 unsigned long flags;
1046 struct list_head *queue;
1049 esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd);
1051 if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
1052 cmd->result = DID_ABORT << 16;
1054 scsi_set_resid(cmd, 0);
1056 cmd->scsi_done(cmd);
1061 spin_lock_irqsave(&a->queue_lock, flags);
1064 * Run through the defer and active queues looking for the request
1068 queue = &a->defer_list;
1072 result = esas2r_check_active_queue(a, &abort_request, cmd, queue);
1075 spin_unlock_irqrestore(&a->queue_lock, flags);
1077 } else if (result == 2 && (queue == &a->defer_list)) {
1078 queue = &a->active_list;
1079 goto check_active_queue;
1082 spin_unlock_irqrestore(&a->queue_lock, flags);
1084 if (abort_request) {
1085 u8 task_management_status = RS_PENDING;
1088 * the request is already active, so we need to tell
1089 * the firmware to abort it and wait for the response.
1092 abort_request->comp_cb = complete_task_management_request;
1093 abort_request->task_management_status_ptr =
1094 &task_management_status;
1096 esas2r_start_request(a, abort_request);
1098 if (atomic_read(&a->disable_cnt) == 0)
1099 esas2r_do_deferred_processes(a);
1101 while (task_management_status == RS_PENDING)
1105 * Once we get here, the original request will have been
1106 * completed by the firmware and the abort request will have
1107 * been cleaned up. we're done!
1114 * If we get here, either we found the inactive request and
1115 * freed it, or we didn't find it at all. Either way, success!
1118 cmd->result = DID_ABORT << 16;
1120 scsi_set_resid(cmd, 0);
1122 cmd->scsi_done(cmd);
1127 static int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset)
1129 struct esas2r_adapter *a =
1130 (struct esas2r_adapter *)cmd->device->host->hostdata;
1132 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1136 esas2r_reset_adapter(a);
1138 esas2r_reset_bus(a);
1140 /* above call sets the AF_OS_RESET flag. wait for it to clear. */
1142 while (test_bit(AF_OS_RESET, &a->flags)) {
1145 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1149 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1155 int esas2r_host_reset(struct scsi_cmnd *cmd)
1157 esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd);
1159 return esas2r_host_bus_reset(cmd, true);
1162 int esas2r_bus_reset(struct scsi_cmnd *cmd)
1164 esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd);
1166 return esas2r_host_bus_reset(cmd, false);
1169 static int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset)
1171 struct esas2r_adapter *a =
1172 (struct esas2r_adapter *)cmd->device->host->hostdata;
1173 struct esas2r_request *rq;
1174 u8 task_management_status = RS_PENDING;
1177 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1181 rq = esas2r_alloc_request(a);
1184 esas2r_log(ESAS2R_LOG_CRIT,
1185 "unable to allocate a request for a "
1186 "target reset (%d)!",
1189 esas2r_log(ESAS2R_LOG_CRIT,
1190 "unable to allocate a request for a "
1191 "device reset (%d:%d)!",
1200 rq->target_id = cmd->device->id;
1201 rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
1202 rq->req_stat = RS_PENDING;
1204 rq->comp_cb = complete_task_management_request;
1205 rq->task_management_status_ptr = &task_management_status;
1208 esas2r_debug("issuing target reset (%p) to id %d", rq,
1210 completed = esas2r_send_task_mgmt(a, rq, 0x20);
1212 esas2r_debug("issuing device reset (%p) to id %d lun %d", rq,
1213 cmd->device->id, cmd->device->lun);
1214 completed = esas2r_send_task_mgmt(a, rq, 0x10);
1218 /* Task management cmd completed right away, need to free it. */
1220 esas2r_free_request(a, rq);
1223 * Wait for firmware to complete the request. Completion
1224 * callback will free it.
1226 while (task_management_status == RS_PENDING)
1230 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1233 if (task_management_status == RS_BUSY) {
1235 * Busy, probably because we are flashing. Wait a bit and
1245 int esas2r_device_reset(struct scsi_cmnd *cmd)
1247 esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd);
1249 return esas2r_dev_targ_reset(cmd, false);
1253 int esas2r_target_reset(struct scsi_cmnd *cmd)
1255 esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd);
1257 return esas2r_dev_targ_reset(cmd, true);
1260 void esas2r_log_request_failure(struct esas2r_adapter *a,
1261 struct esas2r_request *rq)
1263 u8 reqstatus = rq->req_stat;
1265 if (reqstatus == RS_SUCCESS)
1268 if (rq->vrq->scsi.function == VDA_FUNC_SCSI) {
1269 if (reqstatus == RS_SCSI_ERROR) {
1270 if (rq->func_rsp.scsi_rsp.sense_len >= 13) {
1271 esas2r_log(ESAS2R_LOG_WARN,
1272 "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
1273 rq->sense_buf[2], rq->sense_buf[12],
1275 rq->vrq->scsi.cdb[0]);
1277 esas2r_log(ESAS2R_LOG_WARN,
1278 "request failure - SCSI error CDB:%x\n",
1279 rq->vrq->scsi.cdb[0]);
1281 } else if ((rq->vrq->scsi.cdb[0] != INQUIRY
1282 && rq->vrq->scsi.cdb[0] != REPORT_LUNS)
1283 || (reqstatus != RS_SEL
1284 && reqstatus != RS_SEL2)) {
1285 if ((reqstatus == RS_UNDERRUN) &&
1286 (rq->vrq->scsi.cdb[0] == INQUIRY)) {
1287 /* Don't log inquiry underruns */
1289 esas2r_log(ESAS2R_LOG_WARN,
1290 "request failure - cdb:%x reqstatus:%d target:%d",
1291 rq->vrq->scsi.cdb[0], reqstatus,
1298 void esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1303 starttime = jiffies_to_msecs(jiffies);
1304 timeout = rq->timeout ? rq->timeout : 5000;
1307 esas2r_polled_interrupt(a);
1309 if (rq->req_stat != RS_STARTED)
1312 schedule_timeout_interruptible(msecs_to_jiffies(100));
1314 if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
1315 esas2r_hdebug("request TMO");
1318 rq->req_stat = RS_TIMEOUT;
1320 esas2r_local_reset_adapter(a);
1326 u32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo)
1328 u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1);
1329 u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE;
1331 if (a->window_base != base) {
1332 esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP,
1333 base | MVRPW1R_ENABLE);
1334 esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP);
1335 a->window_base = base;
1341 /* Read a block of data from chip memory */
1342 bool esas2r_read_mem_block(struct esas2r_adapter *a,
1354 iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE);
1356 esas2r_map_data_window(a, iatvr);
1358 offset = from & (MW_DATA_WINDOW_SIZE - 1);
1361 if (len > MW_DATA_WINDOW_SIZE - offset)
1362 len = MW_DATA_WINDOW_SIZE - offset;
1368 *end++ = esas2r_read_data_byte(a, offset);
1376 void esas2r_nuxi_mgt_data(u8 function, void *data)
1378 struct atto_vda_grp_info *g;
1379 struct atto_vda_devinfo *d;
1380 struct atto_vdapart_info *p;
1381 struct atto_vda_dh_info *h;
1382 struct atto_vda_metrics_info *m;
1383 struct atto_vda_schedule_info *s;
1384 struct atto_vda_buzzer_info *b;
1388 case VDAMGT_BUZZER_INFO:
1389 case VDAMGT_BUZZER_SET:
1391 b = (struct atto_vda_buzzer_info *)data;
1393 b->duration = le32_to_cpu(b->duration);
1396 case VDAMGT_SCHEDULE_INFO:
1397 case VDAMGT_SCHEDULE_EVENT:
1399 s = (struct atto_vda_schedule_info *)data;
1401 s->id = le32_to_cpu(s->id);
1405 case VDAMGT_DEV_INFO:
1406 case VDAMGT_DEV_CLEAN:
1407 case VDAMGT_DEV_PT_INFO:
1408 case VDAMGT_DEV_FEATURES:
1409 case VDAMGT_DEV_PT_FEATURES:
1410 case VDAMGT_DEV_OPERATION:
1412 d = (struct atto_vda_devinfo *)data;
1414 d->capacity = le64_to_cpu(d->capacity);
1415 d->block_size = le32_to_cpu(d->block_size);
1416 d->ses_dev_index = le16_to_cpu(d->ses_dev_index);
1417 d->target_id = le16_to_cpu(d->target_id);
1418 d->lun = le16_to_cpu(d->lun);
1419 d->features = le16_to_cpu(d->features);
1422 case VDAMGT_GRP_INFO:
1423 case VDAMGT_GRP_CREATE:
1424 case VDAMGT_GRP_DELETE:
1425 case VDAMGT_ADD_STORAGE:
1426 case VDAMGT_MEMBER_ADD:
1427 case VDAMGT_GRP_COMMIT:
1428 case VDAMGT_GRP_REBUILD:
1429 case VDAMGT_GRP_COMMIT_INIT:
1430 case VDAMGT_QUICK_RAID:
1431 case VDAMGT_GRP_FEATURES:
1432 case VDAMGT_GRP_COMMIT_INIT_AUTOMAP:
1433 case VDAMGT_QUICK_RAID_INIT_AUTOMAP:
1434 case VDAMGT_SPARE_LIST:
1435 case VDAMGT_SPARE_ADD:
1436 case VDAMGT_SPARE_REMOVE:
1437 case VDAMGT_LOCAL_SPARE_ADD:
1438 case VDAMGT_GRP_OPERATION:
1440 g = (struct atto_vda_grp_info *)data;
1442 g->capacity = le64_to_cpu(g->capacity);
1443 g->block_size = le32_to_cpu(g->block_size);
1444 g->interleave = le32_to_cpu(g->interleave);
1445 g->features = le16_to_cpu(g->features);
1447 for (i = 0; i < 32; i++)
1448 g->members[i] = le16_to_cpu(g->members[i]);
1452 case VDAMGT_PART_INFO:
1453 case VDAMGT_PART_MAP:
1454 case VDAMGT_PART_UNMAP:
1455 case VDAMGT_PART_AUTOMAP:
1456 case VDAMGT_PART_SPLIT:
1457 case VDAMGT_PART_MERGE:
1459 p = (struct atto_vdapart_info *)data;
1461 p->part_size = le64_to_cpu(p->part_size);
1462 p->start_lba = le32_to_cpu(p->start_lba);
1463 p->block_size = le32_to_cpu(p->block_size);
1464 p->target_id = le16_to_cpu(p->target_id);
1467 case VDAMGT_DEV_HEALTH_REQ:
1469 h = (struct atto_vda_dh_info *)data;
1471 h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt);
1472 h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt);
1475 case VDAMGT_DEV_METRICS:
1477 m = (struct atto_vda_metrics_info *)data;
1479 for (i = 0; i < 32; i++)
1480 m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]);
1489 void esas2r_nuxi_cfg_data(u8 function, void *data)
1491 struct atto_vda_cfg_init *ci;
1495 case VDA_CFG_GET_INIT:
1496 case VDA_CFG_GET_INIT2:
1498 ci = (struct atto_vda_cfg_init *)data;
1500 ci->date_time.year = le16_to_cpu(ci->date_time.year);
1501 ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size);
1502 ci->vda_version = le32_to_cpu(ci->vda_version);
1503 ci->epoch_time = le32_to_cpu(ci->epoch_time);
1504 ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel);
1505 ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend);
1513 void esas2r_nuxi_ae_data(union atto_vda_ae *ae)
1515 struct atto_vda_ae_raid *r = &ae->raid;
1516 struct atto_vda_ae_lu *l = &ae->lu;
1518 switch (ae->hdr.bytype) {
1519 case VDAAE_HDR_TYPE_RAID:
1521 r->dwflags = le32_to_cpu(r->dwflags);
1524 case VDAAE_HDR_TYPE_LU:
1526 l->dwevent = le32_to_cpu(l->dwevent);
1527 l->wphys_target_id = le16_to_cpu(l->wphys_target_id);
1528 l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id);
1530 if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id)
1531 + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) {
1532 l->id.tgtlun_raid.dwinterleave
1533 = le32_to_cpu(l->id.tgtlun_raid.dwinterleave);
1534 l->id.tgtlun_raid.dwblock_size
1535 = le32_to_cpu(l->id.tgtlun_raid.dwblock_size);
1540 case VDAAE_HDR_TYPE_DISK:
1546 void esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1548 unsigned long flags;
1550 esas2r_rq_destroy_request(rq, a);
1551 spin_lock_irqsave(&a->request_lock, flags);
1552 list_add(&rq->comp_list, &a->avail_request);
1553 spin_unlock_irqrestore(&a->request_lock, flags);
1556 struct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a)
1558 struct esas2r_request *rq;
1559 unsigned long flags;
1561 spin_lock_irqsave(&a->request_lock, flags);
1563 if (unlikely(list_empty(&a->avail_request))) {
1564 spin_unlock_irqrestore(&a->request_lock, flags);
1568 rq = list_first_entry(&a->avail_request, struct esas2r_request,
1570 list_del(&rq->comp_list);
1571 spin_unlock_irqrestore(&a->request_lock, flags);
1572 esas2r_rq_init_request(rq, a);
1578 void esas2r_complete_request_cb(struct esas2r_adapter *a,
1579 struct esas2r_request *rq)
1581 esas2r_debug("completing request %p\n", rq);
1583 scsi_dma_unmap(rq->cmd);
1585 if (unlikely(rq->req_stat != RS_SUCCESS)) {
1586 esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id,
1588 rq->func_rsp.scsi_rsp.scsi_stat,
1592 ((esas2r_req_status_to_error(rq->req_stat) << 16)
1593 | (rq->func_rsp.scsi_rsp.scsi_stat & STATUS_MASK));
1595 if (rq->req_stat == RS_UNDERRUN)
1596 scsi_set_resid(rq->cmd,
1597 le32_to_cpu(rq->func_rsp.scsi_rsp.
1600 scsi_set_resid(rq->cmd, 0);
1603 rq->cmd->scsi_done(rq->cmd);
1605 esas2r_free_request(a, rq);
1608 /* Run tasklet to handle stuff outside of interrupt context. */
1609 void esas2r_adapter_tasklet(unsigned long context)
1611 struct esas2r_adapter *a = (struct esas2r_adapter *)context;
1613 if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) {
1614 clear_bit(AF2_TIMER_TICK, &a->flags2);
1615 esas2r_timer_tick(a);
1618 if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) {
1619 clear_bit(AF2_INT_PENDING, &a->flags2);
1620 esas2r_adapter_interrupt(a);
1623 if (esas2r_is_tasklet_pending(a))
1624 esas2r_do_tasklet_tasks(a);
1626 if (esas2r_is_tasklet_pending(a)
1627 || (test_bit(AF2_INT_PENDING, &a->flags2))
1628 || (test_bit(AF2_TIMER_TICK, &a->flags2))) {
1629 clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1630 esas2r_schedule_tasklet(a);
1632 clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1636 static void esas2r_timer_callback(unsigned long context);
1638 void esas2r_kickoff_timer(struct esas2r_adapter *a)
1640 init_timer(&a->timer);
1642 a->timer.function = esas2r_timer_callback;
1643 a->timer.data = (unsigned long)a;
1644 a->timer.expires = jiffies +
1645 msecs_to_jiffies(100);
1647 add_timer(&a->timer);
1650 static void esas2r_timer_callback(unsigned long context)
1652 struct esas2r_adapter *a = (struct esas2r_adapter *)context;
1654 set_bit(AF2_TIMER_TICK, &a->flags2);
1656 esas2r_schedule_tasklet(a);
1658 esas2r_kickoff_timer(a);
1662 * Firmware events need to be handled outside of interrupt context
1663 * so we schedule a delayed_work to handle them.
1667 esas2r_free_fw_event(struct esas2r_fw_event_work *fw_event)
1669 unsigned long flags;
1670 struct esas2r_adapter *a = fw_event->a;
1672 spin_lock_irqsave(&a->fw_event_lock, flags);
1673 list_del(&fw_event->list);
1675 spin_unlock_irqrestore(&a->fw_event_lock, flags);
1679 esas2r_fw_event_off(struct esas2r_adapter *a)
1681 unsigned long flags;
1683 spin_lock_irqsave(&a->fw_event_lock, flags);
1684 a->fw_events_off = 1;
1685 spin_unlock_irqrestore(&a->fw_event_lock, flags);
1689 esas2r_fw_event_on(struct esas2r_adapter *a)
1691 unsigned long flags;
1693 spin_lock_irqsave(&a->fw_event_lock, flags);
1694 a->fw_events_off = 0;
1695 spin_unlock_irqrestore(&a->fw_event_lock, flags);
1698 static void esas2r_add_device(struct esas2r_adapter *a, u16 target_id)
1701 struct scsi_device *scsi_dev;
1703 scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1710 "scsi device already exists at id %d", target_id);
1712 scsi_device_put(scsi_dev);
1718 "scsi_add_device() called for 0:%d:0",
1721 ret = scsi_add_device(a->host, 0, target_id, 0);
1727 "scsi_add_device failed with %d for id %d",
1733 static void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id)
1735 struct scsi_device *scsi_dev;
1737 scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1740 scsi_device_set_state(scsi_dev, SDEV_OFFLINE);
1746 "scsi_remove_device() called for 0:%d:0",
1749 scsi_remove_device(scsi_dev);
1755 "scsi_device_put() called");
1757 scsi_device_put(scsi_dev);
1761 &(a->host->shost_gendev),
1762 "no target found at id %d",
1768 * Sends a firmware asynchronous event to anyone who happens to be
1769 * listening on the defined ATTO VDA event ports.
1771 static void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event)
1773 struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data;
1776 switch (ae->vda_ae.hdr.bytype) {
1777 case VDAAE_HDR_TYPE_RAID:
1778 type = "RAID group state change";
1781 case VDAAE_HDR_TYPE_LU:
1782 type = "Mapped destination LU change";
1785 case VDAAE_HDR_TYPE_DISK:
1786 type = "Physical disk inventory change";
1789 case VDAAE_HDR_TYPE_RESET:
1790 type = "Firmware reset";
1793 case VDAAE_HDR_TYPE_LOG_INFO:
1794 type = "Event Log message (INFO level)";
1797 case VDAAE_HDR_TYPE_LOG_WARN:
1798 type = "Event Log message (WARN level)";
1801 case VDAAE_HDR_TYPE_LOG_CRIT:
1802 type = "Event Log message (CRIT level)";
1805 case VDAAE_HDR_TYPE_LOG_FAIL:
1806 type = "Event Log message (FAIL level)";
1809 case VDAAE_HDR_TYPE_NVC:
1810 type = "NVCache change";
1813 case VDAAE_HDR_TYPE_TLG_INFO:
1814 type = "Time stamped log message (INFO level)";
1817 case VDAAE_HDR_TYPE_TLG_WARN:
1818 type = "Time stamped log message (WARN level)";
1821 case VDAAE_HDR_TYPE_TLG_CRIT:
1822 type = "Time stamped log message (CRIT level)";
1825 case VDAAE_HDR_TYPE_PWRMGT:
1826 type = "Power management";
1829 case VDAAE_HDR_TYPE_MUTE:
1830 type = "Mute button pressed";
1833 case VDAAE_HDR_TYPE_DEV:
1834 type = "Device attribute change";
1842 esas2r_log(ESAS2R_LOG_WARN,
1843 "An async event of type \"%s\" was received from the firmware. The event contents are:",
1845 esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae,
1846 ae->vda_ae.hdr.bylength);
1851 esas2r_firmware_event_work(struct work_struct *work)
1853 struct esas2r_fw_event_work *fw_event =
1854 container_of(work, struct esas2r_fw_event_work, work.work);
1856 struct esas2r_adapter *a = fw_event->a;
1858 u16 target_id = *(u16 *)&fw_event->data[0];
1860 if (a->fw_events_off)
1863 switch (fw_event->type) {
1865 break; /* do nothing */
1867 case fw_event_lun_change:
1868 esas2r_remove_device(a, target_id);
1869 esas2r_add_device(a, target_id);
1872 case fw_event_present:
1873 esas2r_add_device(a, target_id);
1876 case fw_event_not_present:
1877 esas2r_remove_device(a, target_id);
1880 case fw_event_vda_ae:
1881 esas2r_send_ae_event(fw_event);
1886 esas2r_free_fw_event(fw_event);
1889 void esas2r_queue_fw_event(struct esas2r_adapter *a,
1890 enum fw_event_type type,
1894 struct esas2r_fw_event_work *fw_event;
1895 unsigned long flags;
1897 fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC);
1899 esas2r_log(ESAS2R_LOG_WARN,
1900 "esas2r_queue_fw_event failed to alloc");
1904 if (type == fw_event_vda_ae) {
1905 struct esas2r_vda_ae *ae =
1906 (struct esas2r_vda_ae *)fw_event->data;
1908 ae->signature = ESAS2R_VDA_EVENT_SIG;
1909 ae->bus_number = a->pcid->bus->number;
1910 ae->devfn = a->pcid->devfn;
1911 memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae));
1913 memcpy(fw_event->data, data, data_sz);
1916 fw_event->type = type;
1919 spin_lock_irqsave(&a->fw_event_lock, flags);
1920 list_add_tail(&fw_event->list, &a->fw_event_list);
1921 INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work);
1922 queue_delayed_work_on(
1923 smp_processor_id(), a->fw_event_q, &fw_event->work,
1924 msecs_to_jiffies(1));
1925 spin_unlock_irqrestore(&a->fw_event_lock, flags);
1928 void esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id,
1931 if (state == TS_LUN_CHANGE)
1932 esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id,
1934 else if (state == TS_PRESENT)
1935 esas2r_queue_fw_event(a, fw_event_present, &targ_id,
1937 else if (state == TS_NOT_PRESENT)
1938 esas2r_queue_fw_event(a, fw_event_not_present, &targ_id,
1942 /* Translate status to a Linux SCSI mid-layer error code */
1943 int esas2r_req_status_to_error(u8 req_stat)
1950 * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
1951 * it will check the scsi_stat value in the completion anyway.
1958 return DID_NO_CONNECT;
1967 return DID_BUS_BUSY;
1970 /* everything else is just an error. */
1975 module_init(esas2r_init);
1976 module_exit(esas2r_exit);