Linux 4.16.11
[linux/fpc-iii.git] / drivers / scsi / esas2r / esas2r_main.c
blob4eb14301a497bdd7371aed487ccee2f210ac3f81
1 /*
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.
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.
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,
41 * USA.
44 #include "esas2r.h"
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);
101 int result = 0;
103 result = esas2r_write_fs(a, buf, off, count);
105 if (result < 0)
106 result = 0;
108 return length;
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);
137 return 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);
149 if (rq == NULL)
150 return -ENOMEM;
152 if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf))
153 result = count;
155 esas2r_free_request(a, rq);
157 return result;
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)
179 return -ENOMEM;
181 if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS)
182 return -ENOMEM;
184 memcpy(buf, a->local_atto_ioctl, length);
186 return 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 = 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;
207 memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl));
208 memcpy(a->local_atto_ioctl, buf, length);
210 return length;
213 #define ESAS2R_RW_BIN_ATTR(_name) \
214 struct bin_attribute bin_attr_ ## _name = { \
215 .attr = \
216 { .name = __stringify(_name), .mode = S_IRUSR | S_IWUSR }, \
217 .size = 0, \
218 .read = read_ ## _name, \
219 .write = write_ ## _name }
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 },
229 .size = 0,
230 .read = read_default_nvram,
231 .write = NULL
234 static struct scsi_host_template driver_template = {
235 .module = THIS_MODULE,
236 .show_info = esas2r_show_info,
237 .name = ESAS2R_LONGNAME,
238 .release = esas2r_release,
239 .info = esas2r_info,
240 .ioctl = esas2r_ioctl,
241 .queuecommand = esas2r_queuecommand,
242 .eh_abort_handler = esas2r_eh_abort,
243 .eh_device_reset_handler = esas2r_device_reset,
244 .eh_bus_reset_handler = esas2r_bus_reset,
245 .eh_host_reset_handler = esas2r_host_reset,
246 .eh_target_reset_handler = esas2r_target_reset,
247 .can_queue = 128,
248 .this_id = -1,
249 .sg_tablesize = SG_CHUNK_SIZE,
250 .cmd_per_lun =
251 ESAS2R_DEFAULT_CMD_PER_LUN,
252 .present = 0,
253 .unchecked_isa_dma = 0,
254 .use_clustering = ENABLE_CLUSTERING,
255 .emulated = 0,
256 .proc_name = ESAS2R_DRVR_NAME,
257 .change_queue_depth = scsi_change_queue_depth,
258 .max_sectors = 0xFFFF,
261 int sgl_page_size = 512;
262 module_param(sgl_page_size, int, 0);
263 MODULE_PARM_DESC(sgl_page_size,
264 "Scatter/gather list (SGL) page size in number of S/G "
265 "entries. If your application is doing a lot of very large "
266 "transfers, you may want to increase the SGL page size. "
267 "Default 512.");
269 int num_sg_lists = 1024;
270 module_param(num_sg_lists, int, 0);
271 MODULE_PARM_DESC(num_sg_lists,
272 "Number of scatter/gather lists. Default 1024.");
274 int sg_tablesize = SG_CHUNK_SIZE;
275 module_param(sg_tablesize, int, 0);
276 MODULE_PARM_DESC(sg_tablesize,
277 "Maximum number of entries in a scatter/gather table.");
279 int num_requests = 256;
280 module_param(num_requests, int, 0);
281 MODULE_PARM_DESC(num_requests,
282 "Number of requests. Default 256.");
284 int num_ae_requests = 4;
285 module_param(num_ae_requests, int, 0);
286 MODULE_PARM_DESC(num_ae_requests,
287 "Number of VDA asynchromous event requests. Default 4.");
289 int cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN;
290 module_param(cmd_per_lun, int, 0);
291 MODULE_PARM_DESC(cmd_per_lun,
292 "Maximum number of commands per LUN. Default "
293 DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) ".");
295 int can_queue = 128;
296 module_param(can_queue, int, 0);
297 MODULE_PARM_DESC(can_queue,
298 "Maximum number of commands per adapter. Default 128.");
300 int esas2r_max_sectors = 0xFFFF;
301 module_param(esas2r_max_sectors, int, 0);
302 MODULE_PARM_DESC(esas2r_max_sectors,
303 "Maximum number of disk sectors in a single data transfer. "
304 "Default 65535 (largest possible setting).");
306 int interrupt_mode = 1;
307 module_param(interrupt_mode, int, 0);
308 MODULE_PARM_DESC(interrupt_mode,
309 "Defines the interrupt mode to use. 0 for legacy"
310 ", 1 for MSI. Default is MSI (1).");
312 static const struct pci_device_id
313 esas2r_pci_table[] = {
314 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x0049,
316 0, 0 },
317 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004A,
319 0, 0 },
320 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004B,
322 0, 0 },
323 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004C,
325 0, 0 },
326 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004D,
328 0, 0 },
329 { ATTO_VENDOR_ID, 0x0049, ATTO_VENDOR_ID, 0x004E,
331 0, 0 },
332 { 0, 0, 0, 0,
334 0, 0 }
337 MODULE_DEVICE_TABLE(pci, esas2r_pci_table);
339 static int
340 esas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id);
342 static void
343 esas2r_remove(struct pci_dev *pcid);
345 static struct pci_driver
346 esas2r_pci_driver = {
347 .name = ESAS2R_DRVR_NAME,
348 .id_table = esas2r_pci_table,
349 .probe = esas2r_probe,
350 .remove = esas2r_remove,
351 .suspend = esas2r_suspend,
352 .resume = esas2r_resume,
355 static int esas2r_probe(struct pci_dev *pcid,
356 const struct pci_device_id *id)
358 struct Scsi_Host *host = NULL;
359 struct esas2r_adapter *a;
360 int err;
362 size_t host_alloc_size = sizeof(struct esas2r_adapter)
363 + ((num_requests) +
364 1) * sizeof(struct esas2r_request);
366 esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev),
367 "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
368 pcid->vendor,
369 pcid->device,
370 pcid->subsystem_vendor,
371 pcid->subsystem_device);
373 esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
374 "before pci_enable_device() "
375 "enable_cnt: %d",
376 pcid->enable_cnt.counter);
378 err = pci_enable_device(pcid);
379 if (err != 0) {
380 esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev),
381 "pci_enable_device() FAIL (%d)",
382 err);
383 return -ENODEV;
386 esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
387 "pci_enable_device() OK");
388 esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
389 "after pci_enable_device() enable_cnt: %d",
390 pcid->enable_cnt.counter);
392 host = scsi_host_alloc(&driver_template, host_alloc_size);
393 if (host == NULL) {
394 esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL");
395 return -ENODEV;
398 memset(host->hostdata, 0, host_alloc_size);
400 a = (struct esas2r_adapter *)host->hostdata;
402 esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host);
404 /* override max LUN and max target id */
406 host->max_id = ESAS2R_MAX_ID + 1;
407 host->max_lun = 255;
409 /* we can handle 16-byte CDbs */
411 host->max_cmd_len = 16;
413 host->can_queue = can_queue;
414 host->cmd_per_lun = cmd_per_lun;
415 host->this_id = host->max_id + 1;
416 host->max_channel = 0;
417 host->unique_id = found_adapters;
418 host->sg_tablesize = sg_tablesize;
419 host->max_sectors = esas2r_max_sectors;
421 /* set to bus master for BIOses that don't do it for us */
423 esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called");
425 pci_set_master(pcid);
427 if (!esas2r_init_adapter(host, pcid, found_adapters)) {
428 esas2r_log(ESAS2R_LOG_CRIT,
429 "unable to initialize device at PCI bus %x:%x",
430 pcid->bus->number,
431 pcid->devfn);
433 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
434 "scsi_host_put() called");
436 scsi_host_put(host);
438 return 0;
442 esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid,
443 host->hostdata);
445 pci_set_drvdata(pcid, host);
447 esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called");
449 err = scsi_add_host(host, &pcid->dev);
451 if (err) {
452 esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err);
453 esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev),
454 "scsi_add_host() FAIL");
456 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
457 "scsi_host_put() called");
459 scsi_host_put(host);
461 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
462 "pci_set_drvdata(%p, NULL) called",
463 pcid);
465 pci_set_drvdata(pcid, NULL);
467 return -ENODEV;
471 esas2r_fw_event_on(a);
473 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
474 "scsi_scan_host() called");
476 scsi_scan_host(host);
478 /* Add sysfs binary files */
479 if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw))
480 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
481 "Failed to create sysfs binary file: fw");
482 else
483 a->sysfs_fw_created = 1;
485 if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs))
486 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
487 "Failed to create sysfs binary file: fs");
488 else
489 a->sysfs_fs_created = 1;
491 if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda))
492 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
493 "Failed to create sysfs binary file: vda");
494 else
495 a->sysfs_vda_created = 1;
497 if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw))
498 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
499 "Failed to create sysfs binary file: hw");
500 else
501 a->sysfs_hw_created = 1;
503 if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram))
504 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
505 "Failed to create sysfs binary file: live_nvram");
506 else
507 a->sysfs_live_nvram_created = 1;
509 if (sysfs_create_bin_file(&host->shost_dev.kobj,
510 &bin_attr_default_nvram))
511 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
512 "Failed to create sysfs binary file: default_nvram");
513 else
514 a->sysfs_default_nvram_created = 1;
516 found_adapters++;
518 return 0;
521 static void esas2r_remove(struct pci_dev *pdev)
523 struct Scsi_Host *host;
524 int index;
526 if (pdev == NULL) {
527 esas2r_log(ESAS2R_LOG_WARN, "esas2r_remove pdev==NULL");
528 return;
531 host = pci_get_drvdata(pdev);
533 if (host == NULL) {
535 * this can happen if pci_set_drvdata was already called
536 * to clear the host pointer. if this is the case, we
537 * are okay; this channel has already been cleaned up.
540 return;
543 esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
544 "esas2r_remove(%p) called; "
545 "host:%p", pdev,
546 host);
548 index = esas2r_cleanup(host);
550 if (index < 0)
551 esas2r_log_dev(ESAS2R_LOG_WARN, &(pdev->dev),
552 "unknown host in %s",
553 __func__);
555 found_adapters--;
557 /* if this was the last adapter, clean up the rest of the driver */
559 if (found_adapters == 0)
560 esas2r_cleanup(NULL);
563 static int __init esas2r_init(void)
565 int i;
567 esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
569 /* verify valid parameters */
571 if (can_queue < 1) {
572 esas2r_log(ESAS2R_LOG_WARN,
573 "warning: can_queue must be at least 1, value "
574 "forced.");
575 can_queue = 1;
576 } else if (can_queue > 2048) {
577 esas2r_log(ESAS2R_LOG_WARN,
578 "warning: can_queue must be no larger than 2048, "
579 "value forced.");
580 can_queue = 2048;
583 if (cmd_per_lun < 1) {
584 esas2r_log(ESAS2R_LOG_WARN,
585 "warning: cmd_per_lun must be at least 1, value "
586 "forced.");
587 cmd_per_lun = 1;
588 } else if (cmd_per_lun > 2048) {
589 esas2r_log(ESAS2R_LOG_WARN,
590 "warning: cmd_per_lun must be no larger than "
591 "2048, value forced.");
592 cmd_per_lun = 2048;
595 if (sg_tablesize < 32) {
596 esas2r_log(ESAS2R_LOG_WARN,
597 "warning: sg_tablesize must be at least 32, "
598 "value forced.");
599 sg_tablesize = 32;
602 if (esas2r_max_sectors < 1) {
603 esas2r_log(ESAS2R_LOG_WARN,
604 "warning: esas2r_max_sectors must be at least "
605 "1, value forced.");
606 esas2r_max_sectors = 1;
607 } else if (esas2r_max_sectors > 0xffff) {
608 esas2r_log(ESAS2R_LOG_WARN,
609 "warning: esas2r_max_sectors must be no larger "
610 "than 0xffff, value forced.");
611 esas2r_max_sectors = 0xffff;
614 sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1);
616 if (sgl_page_size < SGL_PG_SZ_MIN)
617 sgl_page_size = SGL_PG_SZ_MIN;
618 else if (sgl_page_size > SGL_PG_SZ_MAX)
619 sgl_page_size = SGL_PG_SZ_MAX;
621 if (num_sg_lists < NUM_SGL_MIN)
622 num_sg_lists = NUM_SGL_MIN;
623 else if (num_sg_lists > NUM_SGL_MAX)
624 num_sg_lists = NUM_SGL_MAX;
626 if (num_requests < NUM_REQ_MIN)
627 num_requests = NUM_REQ_MIN;
628 else if (num_requests > NUM_REQ_MAX)
629 num_requests = NUM_REQ_MAX;
631 if (num_ae_requests < NUM_AE_MIN)
632 num_ae_requests = NUM_AE_MIN;
633 else if (num_ae_requests > NUM_AE_MAX)
634 num_ae_requests = NUM_AE_MAX;
636 /* set up other globals */
638 for (i = 0; i < MAX_ADAPTERS; i++)
639 esas2r_adapters[i] = NULL;
641 /* initialize */
643 driver_template.module = THIS_MODULE;
645 if (pci_register_driver(&esas2r_pci_driver) != 0)
646 esas2r_log(ESAS2R_LOG_CRIT, "pci_register_driver FAILED");
647 else
648 esas2r_log(ESAS2R_LOG_INFO, "pci_register_driver() OK");
650 if (!found_adapters) {
651 pci_unregister_driver(&esas2r_pci_driver);
652 esas2r_cleanup(NULL);
654 esas2r_log(ESAS2R_LOG_CRIT,
655 "driver will not be loaded because no ATTO "
656 "%s devices were found",
657 ESAS2R_DRVR_NAME);
658 return -1;
659 } else {
660 esas2r_log(ESAS2R_LOG_INFO, "found %d adapters",
661 found_adapters);
664 return 0;
667 /* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
668 static const struct file_operations esas2r_proc_fops = {
669 .compat_ioctl = esas2r_proc_ioctl,
670 .unlocked_ioctl = esas2r_proc_ioctl,
673 static struct Scsi_Host *esas2r_proc_host;
674 static int esas2r_proc_major;
676 long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
678 return esas2r_ioctl_handler(esas2r_proc_host->hostdata,
679 (int)cmd, (void __user *)arg);
682 static void __exit esas2r_exit(void)
684 esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
686 if (esas2r_proc_major > 0) {
687 esas2r_log(ESAS2R_LOG_INFO, "unregister proc");
689 remove_proc_entry(ATTONODE_NAME,
690 esas2r_proc_host->hostt->proc_dir);
691 unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME);
693 esas2r_proc_major = 0;
696 esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called");
698 pci_unregister_driver(&esas2r_pci_driver);
701 int esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh)
703 struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
705 struct esas2r_target *t;
706 int dev_count = 0;
708 esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no);
710 seq_printf(m, ESAS2R_LONGNAME "\n"
711 "Driver version: "ESAS2R_VERSION_STR "\n"
712 "Flash version: %s\n"
713 "Firmware version: %s\n"
714 "Copyright "ESAS2R_COPYRIGHT_YEARS "\n"
715 "http://www.attotech.com\n"
716 "\n",
717 a->flash_rev,
718 a->fw_rev[0] ? a->fw_rev : "(none)");
721 seq_printf(m, "Adapter information:\n"
722 "--------------------\n"
723 "Model: %s\n"
724 "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
725 esas2r_get_model_name(a),
726 a->nvram->sas_addr[0],
727 a->nvram->sas_addr[1],
728 a->nvram->sas_addr[2],
729 a->nvram->sas_addr[3],
730 a->nvram->sas_addr[4],
731 a->nvram->sas_addr[5],
732 a->nvram->sas_addr[6],
733 a->nvram->sas_addr[7]);
735 seq_puts(m, "\n"
736 "Discovered devices:\n"
737 "\n"
738 " # Target ID\n"
739 "---------------\n");
741 for (t = a->targetdb; t < a->targetdb_end; t++)
742 if (t->buffered_target_state == TS_PRESENT) {
743 seq_printf(m, " %3d %3d\n",
744 ++dev_count,
745 (u16)(uintptr_t)(t - a->targetdb));
748 if (dev_count == 0)
749 seq_puts(m, "none\n");
751 seq_putc(m, '\n');
752 return 0;
756 int esas2r_release(struct Scsi_Host *sh)
758 esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
759 "esas2r_release() called");
761 esas2r_cleanup(sh);
762 if (sh->irq)
763 free_irq(sh->irq, NULL);
764 scsi_unregister(sh);
765 return 0;
768 const char *esas2r_info(struct Scsi_Host *sh)
770 struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
771 static char esas2r_info_str[512];
773 esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
774 "esas2r_info() called");
777 * if we haven't done so already, register as a char driver
778 * and stick a node under "/proc/scsi/esas2r/ATTOnode"
781 if (esas2r_proc_major <= 0) {
782 esas2r_proc_host = sh;
784 esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME,
785 &esas2r_proc_fops);
787 esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev),
788 "register_chrdev (major %d)",
789 esas2r_proc_major);
791 if (esas2r_proc_major > 0) {
792 struct proc_dir_entry *pde;
794 pde = proc_create(ATTONODE_NAME, 0,
795 sh->hostt->proc_dir,
796 &esas2r_proc_fops);
798 if (!pde) {
799 esas2r_log_dev(ESAS2R_LOG_WARN,
800 &(sh->shost_gendev),
801 "failed to create_proc_entry");
802 esas2r_proc_major = -1;
807 sprintf(esas2r_info_str,
808 ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
809 " driver version: "ESAS2R_VERSION_STR " firmware version: "
810 "%s\n",
811 a->pcid->bus->number, a->pcid->devfn, a->pcid->irq,
812 a->fw_rev[0] ? a->fw_rev : "(none)");
814 return esas2r_info_str;
817 /* Callback for building a request scatter/gather list */
818 static u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr)
820 u32 len;
822 if (likely(sgc->cur_offset == sgc->exp_offset)) {
824 * the normal case: caller used all bytes from previous call, so
825 * expected offset is the same as the current offset.
828 if (sgc->sgel_count < sgc->num_sgel) {
829 /* retrieve next segment, except for first time */
830 if (sgc->exp_offset > (u8 *)0) {
831 /* advance current segment */
832 sgc->cur_sgel = sg_next(sgc->cur_sgel);
833 ++(sgc->sgel_count);
837 len = sg_dma_len(sgc->cur_sgel);
838 (*addr) = sg_dma_address(sgc->cur_sgel);
840 /* save the total # bytes returned to caller so far */
841 sgc->exp_offset += len;
843 } else {
844 len = 0;
846 } else if (sgc->cur_offset < sgc->exp_offset) {
848 * caller did not use all bytes from previous call. need to
849 * compute the address based on current segment.
852 len = sg_dma_len(sgc->cur_sgel);
853 (*addr) = sg_dma_address(sgc->cur_sgel);
855 sgc->exp_offset -= len;
857 /* calculate PA based on prev segment address and offsets */
858 *addr = *addr +
859 (sgc->cur_offset - sgc->exp_offset);
861 sgc->exp_offset += len;
863 /* re-calculate length based on offset */
864 len = lower_32_bits(
865 sgc->exp_offset - sgc->cur_offset);
866 } else { /* if ( sgc->cur_offset > sgc->exp_offset ) */
868 * we don't expect the caller to skip ahead.
869 * cur_offset will never exceed the len we return
871 len = 0;
874 return len;
877 int esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
879 struct esas2r_adapter *a =
880 (struct esas2r_adapter *)cmd->device->host->hostdata;
881 struct esas2r_request *rq;
882 struct esas2r_sg_context sgc;
883 unsigned bufflen;
885 /* Assume success, if it fails we will fix the result later. */
886 cmd->result = DID_OK << 16;
888 if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) {
889 cmd->result = DID_NO_CONNECT << 16;
890 cmd->scsi_done(cmd);
891 return 0;
894 rq = esas2r_alloc_request(a);
895 if (unlikely(rq == NULL)) {
896 esas2r_debug("esas2r_alloc_request failed");
897 return SCSI_MLQUEUE_HOST_BUSY;
900 rq->cmd = cmd;
901 bufflen = scsi_bufflen(cmd);
903 if (likely(bufflen != 0)) {
904 if (cmd->sc_data_direction == DMA_TO_DEVICE)
905 rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
906 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
907 rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
910 memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len);
911 rq->vrq->scsi.length = cpu_to_le32(bufflen);
912 rq->target_id = cmd->device->id;
913 rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
914 rq->sense_buf = cmd->sense_buffer;
915 rq->sense_len = SCSI_SENSE_BUFFERSIZE;
917 esas2r_sgc_init(&sgc, a, rq, NULL);
919 sgc.length = bufflen;
920 sgc.cur_offset = NULL;
922 sgc.cur_sgel = scsi_sglist(cmd);
923 sgc.exp_offset = NULL;
924 sgc.num_sgel = scsi_dma_map(cmd);
925 sgc.sgel_count = 0;
927 if (unlikely(sgc.num_sgel < 0)) {
928 esas2r_free_request(a, rq);
929 return SCSI_MLQUEUE_HOST_BUSY;
932 sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc;
934 if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) {
935 scsi_dma_unmap(cmd);
936 esas2r_free_request(a, rq);
937 return SCSI_MLQUEUE_HOST_BUSY;
940 esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id,
941 (int)cmd->device->lun);
943 esas2r_start_request(a, rq);
945 return 0;
948 static void complete_task_management_request(struct esas2r_adapter *a,
949 struct esas2r_request *rq)
951 (*rq->task_management_status_ptr) = rq->req_stat;
952 esas2r_free_request(a, rq);
956 * Searches the specified queue for the specified queue for the command
957 * to abort.
959 * @param [in] a
960 * @param [in] abort_request
961 * @param [in] cmd
963 * @return 0 on failure, 1 if command was not found, 2 if command was found
965 static int esas2r_check_active_queue(struct esas2r_adapter *a,
966 struct esas2r_request **abort_request,
967 struct scsi_cmnd *cmd,
968 struct list_head *queue)
970 bool found = false;
971 struct esas2r_request *ar = *abort_request;
972 struct esas2r_request *rq;
973 struct list_head *element, *next;
975 list_for_each_safe(element, next, queue) {
977 rq = list_entry(element, struct esas2r_request, req_list);
979 if (rq->cmd == cmd) {
981 /* Found the request. See what to do with it. */
982 if (queue == &a->active_list) {
984 * We are searching the active queue, which
985 * means that we need to send an abort request
986 * to the firmware.
988 ar = esas2r_alloc_request(a);
989 if (ar == NULL) {
990 esas2r_log_dev(ESAS2R_LOG_WARN,
991 &(a->host->shost_gendev),
992 "unable to allocate an abort request for cmd %p",
993 cmd);
994 return 0; /* Failure */
998 * Task management request must be formatted
999 * with a lock held.
1001 ar->sense_len = 0;
1002 ar->vrq->scsi.length = 0;
1003 ar->target_id = rq->target_id;
1004 ar->vrq->scsi.flags |= cpu_to_le32(
1005 (u8)le32_to_cpu(rq->vrq->scsi.flags));
1007 memset(ar->vrq->scsi.cdb, 0,
1008 sizeof(ar->vrq->scsi.cdb));
1010 ar->vrq->scsi.flags |= cpu_to_le32(
1011 FCP_CMND_TRM);
1012 ar->vrq->scsi.u.abort_handle =
1013 rq->vrq->scsi.handle;
1014 } else {
1016 * The request is pending but not active on
1017 * the firmware. Just free it now and we'll
1018 * report the successful abort below.
1020 list_del_init(&rq->req_list);
1021 esas2r_free_request(a, rq);
1024 found = true;
1025 break;
1030 if (!found)
1031 return 1; /* Not found */
1033 return 2; /* found */
1038 int esas2r_eh_abort(struct scsi_cmnd *cmd)
1040 struct esas2r_adapter *a =
1041 (struct esas2r_adapter *)cmd->device->host->hostdata;
1042 struct esas2r_request *abort_request = NULL;
1043 unsigned long flags;
1044 struct list_head *queue;
1045 int result;
1047 esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd);
1049 if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
1050 cmd->result = DID_ABORT << 16;
1052 scsi_set_resid(cmd, 0);
1054 cmd->scsi_done(cmd);
1056 return SUCCESS;
1059 spin_lock_irqsave(&a->queue_lock, flags);
1062 * Run through the defer and active queues looking for the request
1063 * to abort.
1066 queue = &a->defer_list;
1068 check_active_queue:
1070 result = esas2r_check_active_queue(a, &abort_request, cmd, queue);
1072 if (!result) {
1073 spin_unlock_irqrestore(&a->queue_lock, flags);
1074 return FAILED;
1075 } else if (result == 2 && (queue == &a->defer_list)) {
1076 queue = &a->active_list;
1077 goto check_active_queue;
1080 spin_unlock_irqrestore(&a->queue_lock, flags);
1082 if (abort_request) {
1083 u8 task_management_status = RS_PENDING;
1086 * the request is already active, so we need to tell
1087 * the firmware to abort it and wait for the response.
1090 abort_request->comp_cb = complete_task_management_request;
1091 abort_request->task_management_status_ptr =
1092 &task_management_status;
1094 esas2r_start_request(a, abort_request);
1096 if (atomic_read(&a->disable_cnt) == 0)
1097 esas2r_do_deferred_processes(a);
1099 while (task_management_status == RS_PENDING)
1100 msleep(10);
1103 * Once we get here, the original request will have been
1104 * completed by the firmware and the abort request will have
1105 * been cleaned up. we're done!
1108 return SUCCESS;
1112 * If we get here, either we found the inactive request and
1113 * freed it, or we didn't find it at all. Either way, success!
1116 cmd->result = DID_ABORT << 16;
1118 scsi_set_resid(cmd, 0);
1120 cmd->scsi_done(cmd);
1122 return SUCCESS;
1125 static int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset)
1127 struct esas2r_adapter *a =
1128 (struct esas2r_adapter *)cmd->device->host->hostdata;
1130 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1131 return FAILED;
1133 if (host_reset)
1134 esas2r_reset_adapter(a);
1135 else
1136 esas2r_reset_bus(a);
1138 /* above call sets the AF_OS_RESET flag. wait for it to clear. */
1140 while (test_bit(AF_OS_RESET, &a->flags)) {
1141 msleep(10);
1143 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1144 return FAILED;
1147 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1148 return FAILED;
1150 return SUCCESS;
1153 int esas2r_host_reset(struct scsi_cmnd *cmd)
1155 esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd);
1157 return esas2r_host_bus_reset(cmd, true);
1160 int esas2r_bus_reset(struct scsi_cmnd *cmd)
1162 esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd);
1164 return esas2r_host_bus_reset(cmd, false);
1167 static int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset)
1169 struct esas2r_adapter *a =
1170 (struct esas2r_adapter *)cmd->device->host->hostdata;
1171 struct esas2r_request *rq;
1172 u8 task_management_status = RS_PENDING;
1173 bool completed;
1175 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1176 return FAILED;
1178 retry:
1179 rq = esas2r_alloc_request(a);
1180 if (rq == NULL) {
1181 if (target_reset) {
1182 esas2r_log(ESAS2R_LOG_CRIT,
1183 "unable to allocate a request for a "
1184 "target reset (%d)!",
1185 cmd->device->id);
1186 } else {
1187 esas2r_log(ESAS2R_LOG_CRIT,
1188 "unable to allocate a request for a "
1189 "device reset (%d:%llu)!",
1190 cmd->device->id,
1191 cmd->device->lun);
1195 return FAILED;
1198 rq->target_id = cmd->device->id;
1199 rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
1200 rq->req_stat = RS_PENDING;
1202 rq->comp_cb = complete_task_management_request;
1203 rq->task_management_status_ptr = &task_management_status;
1205 if (target_reset) {
1206 esas2r_debug("issuing target reset (%p) to id %d", rq,
1207 cmd->device->id);
1208 completed = esas2r_send_task_mgmt(a, rq, 0x20);
1209 } else {
1210 esas2r_debug("issuing device reset (%p) to id %d lun %d", rq,
1211 cmd->device->id, cmd->device->lun);
1212 completed = esas2r_send_task_mgmt(a, rq, 0x10);
1215 if (completed) {
1216 /* Task management cmd completed right away, need to free it. */
1218 esas2r_free_request(a, rq);
1219 } else {
1221 * Wait for firmware to complete the request. Completion
1222 * callback will free it.
1224 while (task_management_status == RS_PENDING)
1225 msleep(10);
1228 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1229 return FAILED;
1231 if (task_management_status == RS_BUSY) {
1233 * Busy, probably because we are flashing. Wait a bit and
1234 * try again.
1236 msleep(100);
1237 goto retry;
1240 return SUCCESS;
1243 int esas2r_device_reset(struct scsi_cmnd *cmd)
1245 esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd);
1247 return esas2r_dev_targ_reset(cmd, false);
1251 int esas2r_target_reset(struct scsi_cmnd *cmd)
1253 esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd);
1255 return esas2r_dev_targ_reset(cmd, true);
1258 void esas2r_log_request_failure(struct esas2r_adapter *a,
1259 struct esas2r_request *rq)
1261 u8 reqstatus = rq->req_stat;
1263 if (reqstatus == RS_SUCCESS)
1264 return;
1266 if (rq->vrq->scsi.function == VDA_FUNC_SCSI) {
1267 if (reqstatus == RS_SCSI_ERROR) {
1268 if (rq->func_rsp.scsi_rsp.sense_len >= 13) {
1269 esas2r_log(ESAS2R_LOG_WARN,
1270 "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
1271 rq->sense_buf[2], rq->sense_buf[12],
1272 rq->sense_buf[13],
1273 rq->vrq->scsi.cdb[0]);
1274 } else {
1275 esas2r_log(ESAS2R_LOG_WARN,
1276 "request failure - SCSI error CDB:%x\n",
1277 rq->vrq->scsi.cdb[0]);
1279 } else if ((rq->vrq->scsi.cdb[0] != INQUIRY
1280 && rq->vrq->scsi.cdb[0] != REPORT_LUNS)
1281 || (reqstatus != RS_SEL
1282 && reqstatus != RS_SEL2)) {
1283 if ((reqstatus == RS_UNDERRUN) &&
1284 (rq->vrq->scsi.cdb[0] == INQUIRY)) {
1285 /* Don't log inquiry underruns */
1286 } else {
1287 esas2r_log(ESAS2R_LOG_WARN,
1288 "request failure - cdb:%x reqstatus:%d target:%d",
1289 rq->vrq->scsi.cdb[0], reqstatus,
1290 rq->target_id);
1296 void esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1298 u32 starttime;
1299 u32 timeout;
1301 starttime = jiffies_to_msecs(jiffies);
1302 timeout = rq->timeout ? rq->timeout : 5000;
1304 while (true) {
1305 esas2r_polled_interrupt(a);
1307 if (rq->req_stat != RS_STARTED)
1308 break;
1310 schedule_timeout_interruptible(msecs_to_jiffies(100));
1312 if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
1313 esas2r_hdebug("request TMO");
1314 esas2r_bugon();
1316 rq->req_stat = RS_TIMEOUT;
1318 esas2r_local_reset_adapter(a);
1319 return;
1324 u32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo)
1326 u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1);
1327 u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE;
1329 if (a->window_base != base) {
1330 esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP,
1331 base | MVRPW1R_ENABLE);
1332 esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP);
1333 a->window_base = base;
1336 return offset;
1339 /* Read a block of data from chip memory */
1340 bool esas2r_read_mem_block(struct esas2r_adapter *a,
1341 void *to,
1342 u32 from,
1343 u32 size)
1345 u8 *end = (u8 *)to;
1347 while (size) {
1348 u32 len;
1349 u32 offset;
1350 u32 iatvr;
1352 iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE);
1354 esas2r_map_data_window(a, iatvr);
1356 offset = from & (MW_DATA_WINDOW_SIZE - 1);
1357 len = size;
1359 if (len > MW_DATA_WINDOW_SIZE - offset)
1360 len = MW_DATA_WINDOW_SIZE - offset;
1362 from += len;
1363 size -= len;
1365 while (len--) {
1366 *end++ = esas2r_read_data_byte(a, offset);
1367 offset++;
1371 return true;
1374 void esas2r_nuxi_mgt_data(u8 function, void *data)
1376 struct atto_vda_grp_info *g;
1377 struct atto_vda_devinfo *d;
1378 struct atto_vdapart_info *p;
1379 struct atto_vda_dh_info *h;
1380 struct atto_vda_metrics_info *m;
1381 struct atto_vda_schedule_info *s;
1382 struct atto_vda_buzzer_info *b;
1383 u8 i;
1385 switch (function) {
1386 case VDAMGT_BUZZER_INFO:
1387 case VDAMGT_BUZZER_SET:
1389 b = (struct atto_vda_buzzer_info *)data;
1391 b->duration = le32_to_cpu(b->duration);
1392 break;
1394 case VDAMGT_SCHEDULE_INFO:
1395 case VDAMGT_SCHEDULE_EVENT:
1397 s = (struct atto_vda_schedule_info *)data;
1399 s->id = le32_to_cpu(s->id);
1401 break;
1403 case VDAMGT_DEV_INFO:
1404 case VDAMGT_DEV_CLEAN:
1405 case VDAMGT_DEV_PT_INFO:
1406 case VDAMGT_DEV_FEATURES:
1407 case VDAMGT_DEV_PT_FEATURES:
1408 case VDAMGT_DEV_OPERATION:
1410 d = (struct atto_vda_devinfo *)data;
1412 d->capacity = le64_to_cpu(d->capacity);
1413 d->block_size = le32_to_cpu(d->block_size);
1414 d->ses_dev_index = le16_to_cpu(d->ses_dev_index);
1415 d->target_id = le16_to_cpu(d->target_id);
1416 d->lun = le16_to_cpu(d->lun);
1417 d->features = le16_to_cpu(d->features);
1418 break;
1420 case VDAMGT_GRP_INFO:
1421 case VDAMGT_GRP_CREATE:
1422 case VDAMGT_GRP_DELETE:
1423 case VDAMGT_ADD_STORAGE:
1424 case VDAMGT_MEMBER_ADD:
1425 case VDAMGT_GRP_COMMIT:
1426 case VDAMGT_GRP_REBUILD:
1427 case VDAMGT_GRP_COMMIT_INIT:
1428 case VDAMGT_QUICK_RAID:
1429 case VDAMGT_GRP_FEATURES:
1430 case VDAMGT_GRP_COMMIT_INIT_AUTOMAP:
1431 case VDAMGT_QUICK_RAID_INIT_AUTOMAP:
1432 case VDAMGT_SPARE_LIST:
1433 case VDAMGT_SPARE_ADD:
1434 case VDAMGT_SPARE_REMOVE:
1435 case VDAMGT_LOCAL_SPARE_ADD:
1436 case VDAMGT_GRP_OPERATION:
1438 g = (struct atto_vda_grp_info *)data;
1440 g->capacity = le64_to_cpu(g->capacity);
1441 g->block_size = le32_to_cpu(g->block_size);
1442 g->interleave = le32_to_cpu(g->interleave);
1443 g->features = le16_to_cpu(g->features);
1445 for (i = 0; i < 32; i++)
1446 g->members[i] = le16_to_cpu(g->members[i]);
1448 break;
1450 case VDAMGT_PART_INFO:
1451 case VDAMGT_PART_MAP:
1452 case VDAMGT_PART_UNMAP:
1453 case VDAMGT_PART_AUTOMAP:
1454 case VDAMGT_PART_SPLIT:
1455 case VDAMGT_PART_MERGE:
1457 p = (struct atto_vdapart_info *)data;
1459 p->part_size = le64_to_cpu(p->part_size);
1460 p->start_lba = le32_to_cpu(p->start_lba);
1461 p->block_size = le32_to_cpu(p->block_size);
1462 p->target_id = le16_to_cpu(p->target_id);
1463 break;
1465 case VDAMGT_DEV_HEALTH_REQ:
1467 h = (struct atto_vda_dh_info *)data;
1469 h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt);
1470 h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt);
1471 break;
1473 case VDAMGT_DEV_METRICS:
1475 m = (struct atto_vda_metrics_info *)data;
1477 for (i = 0; i < 32; i++)
1478 m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]);
1480 break;
1482 default:
1483 break;
1487 void esas2r_nuxi_cfg_data(u8 function, void *data)
1489 struct atto_vda_cfg_init *ci;
1491 switch (function) {
1492 case VDA_CFG_INIT:
1493 case VDA_CFG_GET_INIT:
1494 case VDA_CFG_GET_INIT2:
1496 ci = (struct atto_vda_cfg_init *)data;
1498 ci->date_time.year = le16_to_cpu(ci->date_time.year);
1499 ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size);
1500 ci->vda_version = le32_to_cpu(ci->vda_version);
1501 ci->epoch_time = le32_to_cpu(ci->epoch_time);
1502 ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel);
1503 ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend);
1504 break;
1506 default:
1507 break;
1511 void esas2r_nuxi_ae_data(union atto_vda_ae *ae)
1513 struct atto_vda_ae_raid *r = &ae->raid;
1514 struct atto_vda_ae_lu *l = &ae->lu;
1516 switch (ae->hdr.bytype) {
1517 case VDAAE_HDR_TYPE_RAID:
1519 r->dwflags = le32_to_cpu(r->dwflags);
1520 break;
1522 case VDAAE_HDR_TYPE_LU:
1524 l->dwevent = le32_to_cpu(l->dwevent);
1525 l->wphys_target_id = le16_to_cpu(l->wphys_target_id);
1526 l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id);
1528 if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id)
1529 + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) {
1530 l->id.tgtlun_raid.dwinterleave
1531 = le32_to_cpu(l->id.tgtlun_raid.dwinterleave);
1532 l->id.tgtlun_raid.dwblock_size
1533 = le32_to_cpu(l->id.tgtlun_raid.dwblock_size);
1536 break;
1538 case VDAAE_HDR_TYPE_DISK:
1539 default:
1540 break;
1544 void esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1546 unsigned long flags;
1548 esas2r_rq_destroy_request(rq, a);
1549 spin_lock_irqsave(&a->request_lock, flags);
1550 list_add(&rq->comp_list, &a->avail_request);
1551 spin_unlock_irqrestore(&a->request_lock, flags);
1554 struct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a)
1556 struct esas2r_request *rq;
1557 unsigned long flags;
1559 spin_lock_irqsave(&a->request_lock, flags);
1561 if (unlikely(list_empty(&a->avail_request))) {
1562 spin_unlock_irqrestore(&a->request_lock, flags);
1563 return NULL;
1566 rq = list_first_entry(&a->avail_request, struct esas2r_request,
1567 comp_list);
1568 list_del(&rq->comp_list);
1569 spin_unlock_irqrestore(&a->request_lock, flags);
1570 esas2r_rq_init_request(rq, a);
1572 return rq;
1576 void esas2r_complete_request_cb(struct esas2r_adapter *a,
1577 struct esas2r_request *rq)
1579 esas2r_debug("completing request %p\n", rq);
1581 scsi_dma_unmap(rq->cmd);
1583 if (unlikely(rq->req_stat != RS_SUCCESS)) {
1584 esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id,
1585 rq->req_stat,
1586 rq->func_rsp.scsi_rsp.scsi_stat,
1587 rq->cmd);
1589 rq->cmd->result =
1590 ((esas2r_req_status_to_error(rq->req_stat) << 16)
1591 | (rq->func_rsp.scsi_rsp.scsi_stat & STATUS_MASK));
1593 if (rq->req_stat == RS_UNDERRUN)
1594 scsi_set_resid(rq->cmd,
1595 le32_to_cpu(rq->func_rsp.scsi_rsp.
1596 residual_length));
1597 else
1598 scsi_set_resid(rq->cmd, 0);
1601 rq->cmd->scsi_done(rq->cmd);
1603 esas2r_free_request(a, rq);
1606 /* Run tasklet to handle stuff outside of interrupt context. */
1607 void esas2r_adapter_tasklet(unsigned long context)
1609 struct esas2r_adapter *a = (struct esas2r_adapter *)context;
1611 if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) {
1612 clear_bit(AF2_TIMER_TICK, &a->flags2);
1613 esas2r_timer_tick(a);
1616 if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) {
1617 clear_bit(AF2_INT_PENDING, &a->flags2);
1618 esas2r_adapter_interrupt(a);
1621 if (esas2r_is_tasklet_pending(a))
1622 esas2r_do_tasklet_tasks(a);
1624 if (esas2r_is_tasklet_pending(a)
1625 || (test_bit(AF2_INT_PENDING, &a->flags2))
1626 || (test_bit(AF2_TIMER_TICK, &a->flags2))) {
1627 clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1628 esas2r_schedule_tasklet(a);
1629 } else {
1630 clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1634 static void esas2r_timer_callback(struct timer_list *t);
1636 void esas2r_kickoff_timer(struct esas2r_adapter *a)
1638 timer_setup(&a->timer, esas2r_timer_callback, 0);
1640 a->timer.expires = jiffies +
1641 msecs_to_jiffies(100);
1643 add_timer(&a->timer);
1646 static void esas2r_timer_callback(struct timer_list *t)
1648 struct esas2r_adapter *a = from_timer(a, t, timer);
1650 set_bit(AF2_TIMER_TICK, &a->flags2);
1652 esas2r_schedule_tasklet(a);
1654 esas2r_kickoff_timer(a);
1658 * Firmware events need to be handled outside of interrupt context
1659 * so we schedule a delayed_work to handle them.
1662 static void
1663 esas2r_free_fw_event(struct esas2r_fw_event_work *fw_event)
1665 unsigned long flags;
1666 struct esas2r_adapter *a = fw_event->a;
1668 spin_lock_irqsave(&a->fw_event_lock, flags);
1669 list_del(&fw_event->list);
1670 kfree(fw_event);
1671 spin_unlock_irqrestore(&a->fw_event_lock, flags);
1674 void
1675 esas2r_fw_event_off(struct esas2r_adapter *a)
1677 unsigned long flags;
1679 spin_lock_irqsave(&a->fw_event_lock, flags);
1680 a->fw_events_off = 1;
1681 spin_unlock_irqrestore(&a->fw_event_lock, flags);
1684 void
1685 esas2r_fw_event_on(struct esas2r_adapter *a)
1687 unsigned long flags;
1689 spin_lock_irqsave(&a->fw_event_lock, flags);
1690 a->fw_events_off = 0;
1691 spin_unlock_irqrestore(&a->fw_event_lock, flags);
1694 static void esas2r_add_device(struct esas2r_adapter *a, u16 target_id)
1696 int ret;
1697 struct scsi_device *scsi_dev;
1699 scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1701 if (scsi_dev) {
1702 esas2r_log_dev(
1703 ESAS2R_LOG_WARN,
1704 &(scsi_dev->
1705 sdev_gendev),
1706 "scsi device already exists at id %d", target_id);
1708 scsi_device_put(scsi_dev);
1709 } else {
1710 esas2r_log_dev(
1711 ESAS2R_LOG_INFO,
1712 &(a->host->
1713 shost_gendev),
1714 "scsi_add_device() called for 0:%d:0",
1715 target_id);
1717 ret = scsi_add_device(a->host, 0, target_id, 0);
1718 if (ret) {
1719 esas2r_log_dev(
1720 ESAS2R_LOG_CRIT,
1721 &(a->host->
1722 shost_gendev),
1723 "scsi_add_device failed with %d for id %d",
1724 ret, target_id);
1729 static void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id)
1731 struct scsi_device *scsi_dev;
1733 scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1735 if (scsi_dev) {
1736 scsi_device_set_state(scsi_dev, SDEV_OFFLINE);
1738 esas2r_log_dev(
1739 ESAS2R_LOG_INFO,
1740 &(scsi_dev->
1741 sdev_gendev),
1742 "scsi_remove_device() called for 0:%d:0",
1743 target_id);
1745 scsi_remove_device(scsi_dev);
1747 esas2r_log_dev(
1748 ESAS2R_LOG_INFO,
1749 &(scsi_dev->
1750 sdev_gendev),
1751 "scsi_device_put() called");
1753 scsi_device_put(scsi_dev);
1754 } else {
1755 esas2r_log_dev(
1756 ESAS2R_LOG_WARN,
1757 &(a->host->shost_gendev),
1758 "no target found at id %d",
1759 target_id);
1764 * Sends a firmware asynchronous event to anyone who happens to be
1765 * listening on the defined ATTO VDA event ports.
1767 static void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event)
1769 struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data;
1770 char *type;
1772 switch (ae->vda_ae.hdr.bytype) {
1773 case VDAAE_HDR_TYPE_RAID:
1774 type = "RAID group state change";
1775 break;
1777 case VDAAE_HDR_TYPE_LU:
1778 type = "Mapped destination LU change";
1779 break;
1781 case VDAAE_HDR_TYPE_DISK:
1782 type = "Physical disk inventory change";
1783 break;
1785 case VDAAE_HDR_TYPE_RESET:
1786 type = "Firmware reset";
1787 break;
1789 case VDAAE_HDR_TYPE_LOG_INFO:
1790 type = "Event Log message (INFO level)";
1791 break;
1793 case VDAAE_HDR_TYPE_LOG_WARN:
1794 type = "Event Log message (WARN level)";
1795 break;
1797 case VDAAE_HDR_TYPE_LOG_CRIT:
1798 type = "Event Log message (CRIT level)";
1799 break;
1801 case VDAAE_HDR_TYPE_LOG_FAIL:
1802 type = "Event Log message (FAIL level)";
1803 break;
1805 case VDAAE_HDR_TYPE_NVC:
1806 type = "NVCache change";
1807 break;
1809 case VDAAE_HDR_TYPE_TLG_INFO:
1810 type = "Time stamped log message (INFO level)";
1811 break;
1813 case VDAAE_HDR_TYPE_TLG_WARN:
1814 type = "Time stamped log message (WARN level)";
1815 break;
1817 case VDAAE_HDR_TYPE_TLG_CRIT:
1818 type = "Time stamped log message (CRIT level)";
1819 break;
1821 case VDAAE_HDR_TYPE_PWRMGT:
1822 type = "Power management";
1823 break;
1825 case VDAAE_HDR_TYPE_MUTE:
1826 type = "Mute button pressed";
1827 break;
1829 case VDAAE_HDR_TYPE_DEV:
1830 type = "Device attribute change";
1831 break;
1833 default:
1834 type = "Unknown";
1835 break;
1838 esas2r_log(ESAS2R_LOG_WARN,
1839 "An async event of type \"%s\" was received from the firmware. The event contents are:",
1840 type);
1841 esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae,
1842 ae->vda_ae.hdr.bylength);
1846 static void
1847 esas2r_firmware_event_work(struct work_struct *work)
1849 struct esas2r_fw_event_work *fw_event =
1850 container_of(work, struct esas2r_fw_event_work, work.work);
1852 struct esas2r_adapter *a = fw_event->a;
1854 u16 target_id = *(u16 *)&fw_event->data[0];
1856 if (a->fw_events_off)
1857 goto done;
1859 switch (fw_event->type) {
1860 case fw_event_null:
1861 break; /* do nothing */
1863 case fw_event_lun_change:
1864 esas2r_remove_device(a, target_id);
1865 esas2r_add_device(a, target_id);
1866 break;
1868 case fw_event_present:
1869 esas2r_add_device(a, target_id);
1870 break;
1872 case fw_event_not_present:
1873 esas2r_remove_device(a, target_id);
1874 break;
1876 case fw_event_vda_ae:
1877 esas2r_send_ae_event(fw_event);
1878 break;
1881 done:
1882 esas2r_free_fw_event(fw_event);
1885 void esas2r_queue_fw_event(struct esas2r_adapter *a,
1886 enum fw_event_type type,
1887 void *data,
1888 int data_sz)
1890 struct esas2r_fw_event_work *fw_event;
1891 unsigned long flags;
1893 fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC);
1894 if (!fw_event) {
1895 esas2r_log(ESAS2R_LOG_WARN,
1896 "esas2r_queue_fw_event failed to alloc");
1897 return;
1900 if (type == fw_event_vda_ae) {
1901 struct esas2r_vda_ae *ae =
1902 (struct esas2r_vda_ae *)fw_event->data;
1904 ae->signature = ESAS2R_VDA_EVENT_SIG;
1905 ae->bus_number = a->pcid->bus->number;
1906 ae->devfn = a->pcid->devfn;
1907 memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae));
1908 } else {
1909 memcpy(fw_event->data, data, data_sz);
1912 fw_event->type = type;
1913 fw_event->a = a;
1915 spin_lock_irqsave(&a->fw_event_lock, flags);
1916 list_add_tail(&fw_event->list, &a->fw_event_list);
1917 INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work);
1918 queue_delayed_work_on(
1919 smp_processor_id(), a->fw_event_q, &fw_event->work,
1920 msecs_to_jiffies(1));
1921 spin_unlock_irqrestore(&a->fw_event_lock, flags);
1924 void esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id,
1925 u8 state)
1927 if (state == TS_LUN_CHANGE)
1928 esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id,
1929 sizeof(targ_id));
1930 else if (state == TS_PRESENT)
1931 esas2r_queue_fw_event(a, fw_event_present, &targ_id,
1932 sizeof(targ_id));
1933 else if (state == TS_NOT_PRESENT)
1934 esas2r_queue_fw_event(a, fw_event_not_present, &targ_id,
1935 sizeof(targ_id));
1938 /* Translate status to a Linux SCSI mid-layer error code */
1939 int esas2r_req_status_to_error(u8 req_stat)
1941 switch (req_stat) {
1942 case RS_OVERRUN:
1943 case RS_UNDERRUN:
1944 case RS_SUCCESS:
1946 * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
1947 * it will check the scsi_stat value in the completion anyway.
1949 case RS_SCSI_ERROR:
1950 return DID_OK;
1952 case RS_SEL:
1953 case RS_SEL2:
1954 return DID_NO_CONNECT;
1956 case RS_RESET:
1957 return DID_RESET;
1959 case RS_ABORTED:
1960 return DID_ABORT;
1962 case RS_BUSY:
1963 return DID_BUS_BUSY;
1966 /* everything else is just an error. */
1968 return DID_ERROR;
1971 module_init(esas2r_init);
1972 module_exit(esas2r_exit);