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
= kmalloc(sizeof(struct atto_ioctl
),
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
));
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
,
239 .ioctl
= esas2r_ioctl
,
240 .queuecommand
= esas2r_queuecommand
,
241 .eh_abort_handler
= esas2r_eh_abort
,
242 .eh_device_reset_handler
= esas2r_device_reset
,
243 .eh_bus_reset_handler
= esas2r_bus_reset
,
244 .eh_host_reset_handler
= esas2r_host_reset
,
245 .eh_target_reset_handler
= esas2r_target_reset
,
248 .sg_tablesize
= SG_CHUNK_SIZE
,
250 ESAS2R_DEFAULT_CMD_PER_LUN
,
252 .unchecked_isa_dma
= 0,
254 .proc_name
= ESAS2R_DRVR_NAME
,
255 .change_queue_depth
= scsi_change_queue_depth
,
256 .max_sectors
= 0xFFFF,
259 int sgl_page_size
= 512;
260 module_param(sgl_page_size
, int, 0);
261 MODULE_PARM_DESC(sgl_page_size
,
262 "Scatter/gather list (SGL) page size in number of S/G "
263 "entries. If your application is doing a lot of very large "
264 "transfers, you may want to increase the SGL page size. "
267 int num_sg_lists
= 1024;
268 module_param(num_sg_lists
, int, 0);
269 MODULE_PARM_DESC(num_sg_lists
,
270 "Number of scatter/gather lists. Default 1024.");
272 int sg_tablesize
= SG_CHUNK_SIZE
;
273 module_param(sg_tablesize
, int, 0);
274 MODULE_PARM_DESC(sg_tablesize
,
275 "Maximum number of entries in a scatter/gather table.");
277 int num_requests
= 256;
278 module_param(num_requests
, int, 0);
279 MODULE_PARM_DESC(num_requests
,
280 "Number of requests. Default 256.");
282 int num_ae_requests
= 4;
283 module_param(num_ae_requests
, int, 0);
284 MODULE_PARM_DESC(num_ae_requests
,
285 "Number of VDA asynchronous event requests. Default 4.");
287 int cmd_per_lun
= ESAS2R_DEFAULT_CMD_PER_LUN
;
288 module_param(cmd_per_lun
, int, 0);
289 MODULE_PARM_DESC(cmd_per_lun
,
290 "Maximum number of commands per LUN. Default "
291 DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN
) ".");
294 module_param(can_queue
, int, 0);
295 MODULE_PARM_DESC(can_queue
,
296 "Maximum number of commands per adapter. Default 128.");
298 int esas2r_max_sectors
= 0xFFFF;
299 module_param(esas2r_max_sectors
, int, 0);
300 MODULE_PARM_DESC(esas2r_max_sectors
,
301 "Maximum number of disk sectors in a single data transfer. "
302 "Default 65535 (largest possible setting).");
304 int interrupt_mode
= 1;
305 module_param(interrupt_mode
, int, 0);
306 MODULE_PARM_DESC(interrupt_mode
,
307 "Defines the interrupt mode to use. 0 for legacy"
308 ", 1 for MSI. Default is MSI (1).");
310 static const struct pci_device_id
311 esas2r_pci_table
[] = {
312 { ATTO_VENDOR_ID
, 0x0049, ATTO_VENDOR_ID
, 0x0049,
315 { ATTO_VENDOR_ID
, 0x0049, ATTO_VENDOR_ID
, 0x004A,
318 { ATTO_VENDOR_ID
, 0x0049, ATTO_VENDOR_ID
, 0x004B,
321 { ATTO_VENDOR_ID
, 0x0049, ATTO_VENDOR_ID
, 0x004C,
324 { ATTO_VENDOR_ID
, 0x0049, ATTO_VENDOR_ID
, 0x004D,
327 { ATTO_VENDOR_ID
, 0x0049, ATTO_VENDOR_ID
, 0x004E,
335 MODULE_DEVICE_TABLE(pci
, esas2r_pci_table
);
338 esas2r_probe(struct pci_dev
*pcid
, const struct pci_device_id
*id
);
341 esas2r_remove(struct pci_dev
*pcid
);
343 static struct pci_driver
344 esas2r_pci_driver
= {
345 .name
= ESAS2R_DRVR_NAME
,
346 .id_table
= esas2r_pci_table
,
347 .probe
= esas2r_probe
,
348 .remove
= esas2r_remove
,
349 .suspend
= esas2r_suspend
,
350 .resume
= esas2r_resume
,
353 static int esas2r_probe(struct pci_dev
*pcid
,
354 const struct pci_device_id
*id
)
356 struct Scsi_Host
*host
= NULL
;
357 struct esas2r_adapter
*a
;
360 size_t host_alloc_size
= sizeof(struct esas2r_adapter
)
362 1) * sizeof(struct esas2r_request
);
364 esas2r_log_dev(ESAS2R_LOG_DEBG
, &(pcid
->dev
),
365 "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
368 pcid
->subsystem_vendor
,
369 pcid
->subsystem_device
);
371 esas2r_log_dev(ESAS2R_LOG_INFO
, &(pcid
->dev
),
372 "before pci_enable_device() "
374 pcid
->enable_cnt
.counter
);
376 err
= pci_enable_device(pcid
);
378 esas2r_log_dev(ESAS2R_LOG_CRIT
, &(pcid
->dev
),
379 "pci_enable_device() FAIL (%d)",
384 esas2r_log_dev(ESAS2R_LOG_INFO
, &(pcid
->dev
),
385 "pci_enable_device() OK");
386 esas2r_log_dev(ESAS2R_LOG_INFO
, &(pcid
->dev
),
387 "after pci_enable_device() enable_cnt: %d",
388 pcid
->enable_cnt
.counter
);
390 host
= scsi_host_alloc(&driver_template
, host_alloc_size
);
392 esas2r_log(ESAS2R_LOG_CRIT
, "scsi_host_alloc() FAIL");
396 memset(host
->hostdata
, 0, host_alloc_size
);
398 a
= (struct esas2r_adapter
*)host
->hostdata
;
400 esas2r_log(ESAS2R_LOG_INFO
, "scsi_host_alloc() OK host: %p", host
);
402 /* override max LUN and max target id */
404 host
->max_id
= ESAS2R_MAX_ID
+ 1;
407 /* we can handle 16-byte CDbs */
409 host
->max_cmd_len
= 16;
411 host
->can_queue
= can_queue
;
412 host
->cmd_per_lun
= cmd_per_lun
;
413 host
->this_id
= host
->max_id
+ 1;
414 host
->max_channel
= 0;
415 host
->unique_id
= found_adapters
;
416 host
->sg_tablesize
= sg_tablesize
;
417 host
->max_sectors
= esas2r_max_sectors
;
419 /* set to bus master for BIOses that don't do it for us */
421 esas2r_log(ESAS2R_LOG_INFO
, "pci_set_master() called");
423 pci_set_master(pcid
);
425 if (!esas2r_init_adapter(host
, pcid
, found_adapters
)) {
426 esas2r_log(ESAS2R_LOG_CRIT
,
427 "unable to initialize device at PCI bus %x:%x",
431 esas2r_log_dev(ESAS2R_LOG_INFO
, &(host
->shost_gendev
),
432 "scsi_host_put() called");
440 esas2r_log(ESAS2R_LOG_INFO
, "pci_set_drvdata(%p, %p) called", pcid
,
443 pci_set_drvdata(pcid
, host
);
445 esas2r_log(ESAS2R_LOG_INFO
, "scsi_add_host() called");
447 err
= scsi_add_host(host
, &pcid
->dev
);
450 esas2r_log(ESAS2R_LOG_CRIT
, "scsi_add_host returned %d", err
);
451 esas2r_log_dev(ESAS2R_LOG_CRIT
, &(host
->shost_gendev
),
452 "scsi_add_host() FAIL");
454 esas2r_log_dev(ESAS2R_LOG_INFO
, &(host
->shost_gendev
),
455 "scsi_host_put() called");
459 esas2r_log_dev(ESAS2R_LOG_INFO
, &(host
->shost_gendev
),
460 "pci_set_drvdata(%p, NULL) called",
463 pci_set_drvdata(pcid
, NULL
);
469 esas2r_fw_event_on(a
);
471 esas2r_log_dev(ESAS2R_LOG_INFO
, &(host
->shost_gendev
),
472 "scsi_scan_host() called");
474 scsi_scan_host(host
);
476 /* Add sysfs binary files */
477 if (sysfs_create_bin_file(&host
->shost_dev
.kobj
, &bin_attr_fw
))
478 esas2r_log_dev(ESAS2R_LOG_WARN
, &(host
->shost_gendev
),
479 "Failed to create sysfs binary file: fw");
481 a
->sysfs_fw_created
= 1;
483 if (sysfs_create_bin_file(&host
->shost_dev
.kobj
, &bin_attr_fs
))
484 esas2r_log_dev(ESAS2R_LOG_WARN
, &(host
->shost_gendev
),
485 "Failed to create sysfs binary file: fs");
487 a
->sysfs_fs_created
= 1;
489 if (sysfs_create_bin_file(&host
->shost_dev
.kobj
, &bin_attr_vda
))
490 esas2r_log_dev(ESAS2R_LOG_WARN
, &(host
->shost_gendev
),
491 "Failed to create sysfs binary file: vda");
493 a
->sysfs_vda_created
= 1;
495 if (sysfs_create_bin_file(&host
->shost_dev
.kobj
, &bin_attr_hw
))
496 esas2r_log_dev(ESAS2R_LOG_WARN
, &(host
->shost_gendev
),
497 "Failed to create sysfs binary file: hw");
499 a
->sysfs_hw_created
= 1;
501 if (sysfs_create_bin_file(&host
->shost_dev
.kobj
, &bin_attr_live_nvram
))
502 esas2r_log_dev(ESAS2R_LOG_WARN
, &(host
->shost_gendev
),
503 "Failed to create sysfs binary file: live_nvram");
505 a
->sysfs_live_nvram_created
= 1;
507 if (sysfs_create_bin_file(&host
->shost_dev
.kobj
,
508 &bin_attr_default_nvram
))
509 esas2r_log_dev(ESAS2R_LOG_WARN
, &(host
->shost_gendev
),
510 "Failed to create sysfs binary file: default_nvram");
512 a
->sysfs_default_nvram_created
= 1;
519 static void esas2r_remove(struct pci_dev
*pdev
)
521 struct Scsi_Host
*host
= pci_get_drvdata(pdev
);
522 struct esas2r_adapter
*a
= (struct esas2r_adapter
*)host
->hostdata
;
524 esas2r_log_dev(ESAS2R_LOG_INFO
, &(pdev
->dev
),
525 "esas2r_remove(%p) called; "
529 esas2r_kill_adapter(a
->index
);
533 static int __init
esas2r_init(void)
537 esas2r_log(ESAS2R_LOG_INFO
, "%s called", __func__
);
539 /* verify valid parameters */
542 esas2r_log(ESAS2R_LOG_WARN
,
543 "warning: can_queue must be at least 1, value "
546 } else if (can_queue
> 2048) {
547 esas2r_log(ESAS2R_LOG_WARN
,
548 "warning: can_queue must be no larger than 2048, "
553 if (cmd_per_lun
< 1) {
554 esas2r_log(ESAS2R_LOG_WARN
,
555 "warning: cmd_per_lun must be at least 1, value "
558 } else if (cmd_per_lun
> 2048) {
559 esas2r_log(ESAS2R_LOG_WARN
,
560 "warning: cmd_per_lun must be no larger than "
561 "2048, value forced.");
565 if (sg_tablesize
< 32) {
566 esas2r_log(ESAS2R_LOG_WARN
,
567 "warning: sg_tablesize must be at least 32, "
572 if (esas2r_max_sectors
< 1) {
573 esas2r_log(ESAS2R_LOG_WARN
,
574 "warning: esas2r_max_sectors must be at least "
576 esas2r_max_sectors
= 1;
577 } else if (esas2r_max_sectors
> 0xffff) {
578 esas2r_log(ESAS2R_LOG_WARN
,
579 "warning: esas2r_max_sectors must be no larger "
580 "than 0xffff, value forced.");
581 esas2r_max_sectors
= 0xffff;
584 sgl_page_size
&= ~(ESAS2R_SGL_ALIGN
- 1);
586 if (sgl_page_size
< SGL_PG_SZ_MIN
)
587 sgl_page_size
= SGL_PG_SZ_MIN
;
588 else if (sgl_page_size
> SGL_PG_SZ_MAX
)
589 sgl_page_size
= SGL_PG_SZ_MAX
;
591 if (num_sg_lists
< NUM_SGL_MIN
)
592 num_sg_lists
= NUM_SGL_MIN
;
593 else if (num_sg_lists
> NUM_SGL_MAX
)
594 num_sg_lists
= NUM_SGL_MAX
;
596 if (num_requests
< NUM_REQ_MIN
)
597 num_requests
= NUM_REQ_MIN
;
598 else if (num_requests
> NUM_REQ_MAX
)
599 num_requests
= NUM_REQ_MAX
;
601 if (num_ae_requests
< NUM_AE_MIN
)
602 num_ae_requests
= NUM_AE_MIN
;
603 else if (num_ae_requests
> NUM_AE_MAX
)
604 num_ae_requests
= NUM_AE_MAX
;
606 /* set up other globals */
608 for (i
= 0; i
< MAX_ADAPTERS
; i
++)
609 esas2r_adapters
[i
] = NULL
;
611 return pci_register_driver(&esas2r_pci_driver
);
614 /* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
615 static const struct file_operations esas2r_proc_fops
= {
616 .compat_ioctl
= compat_ptr_ioctl
,
617 .unlocked_ioctl
= esas2r_proc_ioctl
,
620 static const struct proc_ops esas2r_proc_ops
= {
621 .proc_ioctl
= esas2r_proc_ioctl
,
623 .proc_compat_ioctl
= compat_ptr_ioctl
,
627 static struct Scsi_Host
*esas2r_proc_host
;
628 static int esas2r_proc_major
;
630 long esas2r_proc_ioctl(struct file
*fp
, unsigned int cmd
, unsigned long arg
)
632 return esas2r_ioctl_handler(esas2r_proc_host
->hostdata
,
633 cmd
, (void __user
*)arg
);
636 static void __exit
esas2r_exit(void)
638 esas2r_log(ESAS2R_LOG_INFO
, "%s called", __func__
);
640 if (esas2r_proc_major
> 0) {
641 esas2r_log(ESAS2R_LOG_INFO
, "unregister proc");
643 remove_proc_entry(ATTONODE_NAME
,
644 esas2r_proc_host
->hostt
->proc_dir
);
645 unregister_chrdev(esas2r_proc_major
, ESAS2R_DRVR_NAME
);
647 esas2r_proc_major
= 0;
650 esas2r_log(ESAS2R_LOG_INFO
, "pci_unregister_driver() called");
652 pci_unregister_driver(&esas2r_pci_driver
);
655 int esas2r_show_info(struct seq_file
*m
, struct Scsi_Host
*sh
)
657 struct esas2r_adapter
*a
= (struct esas2r_adapter
*)sh
->hostdata
;
659 struct esas2r_target
*t
;
662 esas2r_log(ESAS2R_LOG_DEBG
, "esas2r_show_info (%p,%d)", m
, sh
->host_no
);
664 seq_printf(m
, ESAS2R_LONGNAME
"\n"
665 "Driver version: "ESAS2R_VERSION_STR
"\n"
666 "Flash version: %s\n"
667 "Firmware version: %s\n"
668 "Copyright "ESAS2R_COPYRIGHT_YEARS
"\n"
669 "http://www.attotech.com\n"
672 a
->fw_rev
[0] ? a
->fw_rev
: "(none)");
675 seq_printf(m
, "Adapter information:\n"
676 "--------------------\n"
678 "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
679 esas2r_get_model_name(a
),
680 a
->nvram
->sas_addr
[0],
681 a
->nvram
->sas_addr
[1],
682 a
->nvram
->sas_addr
[2],
683 a
->nvram
->sas_addr
[3],
684 a
->nvram
->sas_addr
[4],
685 a
->nvram
->sas_addr
[5],
686 a
->nvram
->sas_addr
[6],
687 a
->nvram
->sas_addr
[7]);
690 "Discovered devices:\n"
693 "---------------\n");
695 for (t
= a
->targetdb
; t
< a
->targetdb_end
; t
++)
696 if (t
->buffered_target_state
== TS_PRESENT
) {
697 seq_printf(m
, " %3d %3d\n",
699 (u16
)(uintptr_t)(t
- a
->targetdb
));
703 seq_puts(m
, "none\n");
710 const char *esas2r_info(struct Scsi_Host
*sh
)
712 struct esas2r_adapter
*a
= (struct esas2r_adapter
*)sh
->hostdata
;
713 static char esas2r_info_str
[512];
715 esas2r_log_dev(ESAS2R_LOG_INFO
, &(sh
->shost_gendev
),
716 "esas2r_info() called");
719 * if we haven't done so already, register as a char driver
720 * and stick a node under "/proc/scsi/esas2r/ATTOnode"
723 if (esas2r_proc_major
<= 0) {
724 esas2r_proc_host
= sh
;
726 esas2r_proc_major
= register_chrdev(0, ESAS2R_DRVR_NAME
,
729 esas2r_log_dev(ESAS2R_LOG_DEBG
, &(sh
->shost_gendev
),
730 "register_chrdev (major %d)",
733 if (esas2r_proc_major
> 0) {
734 struct proc_dir_entry
*pde
;
736 pde
= proc_create(ATTONODE_NAME
, 0,
741 esas2r_log_dev(ESAS2R_LOG_WARN
,
743 "failed to create_proc_entry");
744 esas2r_proc_major
= -1;
749 sprintf(esas2r_info_str
,
750 ESAS2R_LONGNAME
" (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
751 " driver version: "ESAS2R_VERSION_STR
" firmware version: "
753 a
->pcid
->bus
->number
, a
->pcid
->devfn
, a
->pcid
->irq
,
754 a
->fw_rev
[0] ? a
->fw_rev
: "(none)");
756 return esas2r_info_str
;
759 /* Callback for building a request scatter/gather list */
760 static u32
get_physaddr_from_sgc(struct esas2r_sg_context
*sgc
, u64
*addr
)
764 if (likely(sgc
->cur_offset
== sgc
->exp_offset
)) {
766 * the normal case: caller used all bytes from previous call, so
767 * expected offset is the same as the current offset.
770 if (sgc
->sgel_count
< sgc
->num_sgel
) {
771 /* retrieve next segment, except for first time */
772 if (sgc
->exp_offset
> (u8
*)0) {
773 /* advance current segment */
774 sgc
->cur_sgel
= sg_next(sgc
->cur_sgel
);
779 len
= sg_dma_len(sgc
->cur_sgel
);
780 (*addr
) = sg_dma_address(sgc
->cur_sgel
);
782 /* save the total # bytes returned to caller so far */
783 sgc
->exp_offset
+= len
;
788 } else if (sgc
->cur_offset
< sgc
->exp_offset
) {
790 * caller did not use all bytes from previous call. need to
791 * compute the address based on current segment.
794 len
= sg_dma_len(sgc
->cur_sgel
);
795 (*addr
) = sg_dma_address(sgc
->cur_sgel
);
797 sgc
->exp_offset
-= len
;
799 /* calculate PA based on prev segment address and offsets */
801 (sgc
->cur_offset
- sgc
->exp_offset
);
803 sgc
->exp_offset
+= len
;
805 /* re-calculate length based on offset */
807 sgc
->exp_offset
- sgc
->cur_offset
);
808 } else { /* if ( sgc->cur_offset > sgc->exp_offset ) */
810 * we don't expect the caller to skip ahead.
811 * cur_offset will never exceed the len we return
819 int esas2r_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*cmd
)
821 struct esas2r_adapter
*a
=
822 (struct esas2r_adapter
*)cmd
->device
->host
->hostdata
;
823 struct esas2r_request
*rq
;
824 struct esas2r_sg_context sgc
;
827 /* Assume success, if it fails we will fix the result later. */
828 cmd
->result
= DID_OK
<< 16;
830 if (unlikely(test_bit(AF_DEGRADED_MODE
, &a
->flags
))) {
831 cmd
->result
= DID_NO_CONNECT
<< 16;
836 rq
= esas2r_alloc_request(a
);
837 if (unlikely(rq
== NULL
)) {
838 esas2r_debug("esas2r_alloc_request failed");
839 return SCSI_MLQUEUE_HOST_BUSY
;
843 bufflen
= scsi_bufflen(cmd
);
845 if (likely(bufflen
!= 0)) {
846 if (cmd
->sc_data_direction
== DMA_TO_DEVICE
)
847 rq
->vrq
->scsi
.flags
|= cpu_to_le32(FCP_CMND_WRD
);
848 else if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
)
849 rq
->vrq
->scsi
.flags
|= cpu_to_le32(FCP_CMND_RDD
);
852 memcpy(rq
->vrq
->scsi
.cdb
, cmd
->cmnd
, cmd
->cmd_len
);
853 rq
->vrq
->scsi
.length
= cpu_to_le32(bufflen
);
854 rq
->target_id
= cmd
->device
->id
;
855 rq
->vrq
->scsi
.flags
|= cpu_to_le32(cmd
->device
->lun
);
856 rq
->sense_buf
= cmd
->sense_buffer
;
857 rq
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
859 esas2r_sgc_init(&sgc
, a
, rq
, NULL
);
861 sgc
.length
= bufflen
;
862 sgc
.cur_offset
= NULL
;
864 sgc
.cur_sgel
= scsi_sglist(cmd
);
865 sgc
.exp_offset
= NULL
;
866 sgc
.num_sgel
= scsi_dma_map(cmd
);
869 if (unlikely(sgc
.num_sgel
< 0)) {
870 esas2r_free_request(a
, rq
);
871 return SCSI_MLQUEUE_HOST_BUSY
;
874 sgc
.get_phys_addr
= (PGETPHYSADDR
)get_physaddr_from_sgc
;
876 if (unlikely(!esas2r_build_sg_list(a
, rq
, &sgc
))) {
878 esas2r_free_request(a
, rq
);
879 return SCSI_MLQUEUE_HOST_BUSY
;
882 esas2r_debug("start request %p to %d:%d\n", rq
, (int)cmd
->device
->id
,
883 (int)cmd
->device
->lun
);
885 esas2r_start_request(a
, rq
);
890 static void complete_task_management_request(struct esas2r_adapter
*a
,
891 struct esas2r_request
*rq
)
893 (*rq
->task_management_status_ptr
) = rq
->req_stat
;
894 esas2r_free_request(a
, rq
);
898 * Searches the specified queue for the specified queue for the command
902 * @param [in] abort_request
905 * @return 0 on failure, 1 if command was not found, 2 if command was found
907 static int esas2r_check_active_queue(struct esas2r_adapter
*a
,
908 struct esas2r_request
**abort_request
,
909 struct scsi_cmnd
*cmd
,
910 struct list_head
*queue
)
913 struct esas2r_request
*ar
= *abort_request
;
914 struct esas2r_request
*rq
;
915 struct list_head
*element
, *next
;
917 list_for_each_safe(element
, next
, queue
) {
919 rq
= list_entry(element
, struct esas2r_request
, req_list
);
921 if (rq
->cmd
== cmd
) {
923 /* Found the request. See what to do with it. */
924 if (queue
== &a
->active_list
) {
926 * We are searching the active queue, which
927 * means that we need to send an abort request
930 ar
= esas2r_alloc_request(a
);
932 esas2r_log_dev(ESAS2R_LOG_WARN
,
933 &(a
->host
->shost_gendev
),
934 "unable to allocate an abort request for cmd %p",
936 return 0; /* Failure */
940 * Task management request must be formatted
944 ar
->vrq
->scsi
.length
= 0;
945 ar
->target_id
= rq
->target_id
;
946 ar
->vrq
->scsi
.flags
|= cpu_to_le32(
947 (u8
)le32_to_cpu(rq
->vrq
->scsi
.flags
));
949 memset(ar
->vrq
->scsi
.cdb
, 0,
950 sizeof(ar
->vrq
->scsi
.cdb
));
952 ar
->vrq
->scsi
.flags
|= cpu_to_le32(
954 ar
->vrq
->scsi
.u
.abort_handle
=
955 rq
->vrq
->scsi
.handle
;
958 * The request is pending but not active on
959 * the firmware. Just free it now and we'll
960 * report the successful abort below.
962 list_del_init(&rq
->req_list
);
963 esas2r_free_request(a
, rq
);
973 return 1; /* Not found */
975 return 2; /* found */
980 int esas2r_eh_abort(struct scsi_cmnd
*cmd
)
982 struct esas2r_adapter
*a
=
983 (struct esas2r_adapter
*)cmd
->device
->host
->hostdata
;
984 struct esas2r_request
*abort_request
= NULL
;
986 struct list_head
*queue
;
989 esas2r_log(ESAS2R_LOG_INFO
, "eh_abort (%p)", cmd
);
991 if (test_bit(AF_DEGRADED_MODE
, &a
->flags
)) {
992 cmd
->result
= DID_ABORT
<< 16;
994 scsi_set_resid(cmd
, 0);
1001 spin_lock_irqsave(&a
->queue_lock
, flags
);
1004 * Run through the defer and active queues looking for the request
1008 queue
= &a
->defer_list
;
1012 result
= esas2r_check_active_queue(a
, &abort_request
, cmd
, queue
);
1015 spin_unlock_irqrestore(&a
->queue_lock
, flags
);
1017 } else if (result
== 2 && (queue
== &a
->defer_list
)) {
1018 queue
= &a
->active_list
;
1019 goto check_active_queue
;
1022 spin_unlock_irqrestore(&a
->queue_lock
, flags
);
1024 if (abort_request
) {
1025 u8 task_management_status
= RS_PENDING
;
1028 * the request is already active, so we need to tell
1029 * the firmware to abort it and wait for the response.
1032 abort_request
->comp_cb
= complete_task_management_request
;
1033 abort_request
->task_management_status_ptr
=
1034 &task_management_status
;
1036 esas2r_start_request(a
, abort_request
);
1038 if (atomic_read(&a
->disable_cnt
) == 0)
1039 esas2r_do_deferred_processes(a
);
1041 while (task_management_status
== RS_PENDING
)
1045 * Once we get here, the original request will have been
1046 * completed by the firmware and the abort request will have
1047 * been cleaned up. we're done!
1054 * If we get here, either we found the inactive request and
1055 * freed it, or we didn't find it at all. Either way, success!
1058 cmd
->result
= DID_ABORT
<< 16;
1060 scsi_set_resid(cmd
, 0);
1062 cmd
->scsi_done(cmd
);
1067 static int esas2r_host_bus_reset(struct scsi_cmnd
*cmd
, bool host_reset
)
1069 struct esas2r_adapter
*a
=
1070 (struct esas2r_adapter
*)cmd
->device
->host
->hostdata
;
1072 if (test_bit(AF_DEGRADED_MODE
, &a
->flags
))
1076 esas2r_reset_adapter(a
);
1078 esas2r_reset_bus(a
);
1080 /* above call sets the AF_OS_RESET flag. wait for it to clear. */
1082 while (test_bit(AF_OS_RESET
, &a
->flags
)) {
1085 if (test_bit(AF_DEGRADED_MODE
, &a
->flags
))
1089 if (test_bit(AF_DEGRADED_MODE
, &a
->flags
))
1095 int esas2r_host_reset(struct scsi_cmnd
*cmd
)
1097 esas2r_log(ESAS2R_LOG_INFO
, "host_reset (%p)", cmd
);
1099 return esas2r_host_bus_reset(cmd
, true);
1102 int esas2r_bus_reset(struct scsi_cmnd
*cmd
)
1104 esas2r_log(ESAS2R_LOG_INFO
, "bus_reset (%p)", cmd
);
1106 return esas2r_host_bus_reset(cmd
, false);
1109 static int esas2r_dev_targ_reset(struct scsi_cmnd
*cmd
, bool target_reset
)
1111 struct esas2r_adapter
*a
=
1112 (struct esas2r_adapter
*)cmd
->device
->host
->hostdata
;
1113 struct esas2r_request
*rq
;
1114 u8 task_management_status
= RS_PENDING
;
1117 if (test_bit(AF_DEGRADED_MODE
, &a
->flags
))
1121 rq
= esas2r_alloc_request(a
);
1124 esas2r_log(ESAS2R_LOG_CRIT
,
1125 "unable to allocate a request for a "
1126 "target reset (%d)!",
1129 esas2r_log(ESAS2R_LOG_CRIT
,
1130 "unable to allocate a request for a "
1131 "device reset (%d:%llu)!",
1140 rq
->target_id
= cmd
->device
->id
;
1141 rq
->vrq
->scsi
.flags
|= cpu_to_le32(cmd
->device
->lun
);
1142 rq
->req_stat
= RS_PENDING
;
1144 rq
->comp_cb
= complete_task_management_request
;
1145 rq
->task_management_status_ptr
= &task_management_status
;
1148 esas2r_debug("issuing target reset (%p) to id %d", rq
,
1150 completed
= esas2r_send_task_mgmt(a
, rq
, 0x20);
1152 esas2r_debug("issuing device reset (%p) to id %d lun %d", rq
,
1153 cmd
->device
->id
, cmd
->device
->lun
);
1154 completed
= esas2r_send_task_mgmt(a
, rq
, 0x10);
1158 /* Task management cmd completed right away, need to free it. */
1160 esas2r_free_request(a
, rq
);
1163 * Wait for firmware to complete the request. Completion
1164 * callback will free it.
1166 while (task_management_status
== RS_PENDING
)
1170 if (test_bit(AF_DEGRADED_MODE
, &a
->flags
))
1173 if (task_management_status
== RS_BUSY
) {
1175 * Busy, probably because we are flashing. Wait a bit and
1185 int esas2r_device_reset(struct scsi_cmnd
*cmd
)
1187 esas2r_log(ESAS2R_LOG_INFO
, "device_reset (%p)", cmd
);
1189 return esas2r_dev_targ_reset(cmd
, false);
1193 int esas2r_target_reset(struct scsi_cmnd
*cmd
)
1195 esas2r_log(ESAS2R_LOG_INFO
, "target_reset (%p)", cmd
);
1197 return esas2r_dev_targ_reset(cmd
, true);
1200 void esas2r_log_request_failure(struct esas2r_adapter
*a
,
1201 struct esas2r_request
*rq
)
1203 u8 reqstatus
= rq
->req_stat
;
1205 if (reqstatus
== RS_SUCCESS
)
1208 if (rq
->vrq
->scsi
.function
== VDA_FUNC_SCSI
) {
1209 if (reqstatus
== RS_SCSI_ERROR
) {
1210 if (rq
->func_rsp
.scsi_rsp
.sense_len
>= 13) {
1211 esas2r_log(ESAS2R_LOG_WARN
,
1212 "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
1213 rq
->sense_buf
[2], rq
->sense_buf
[12],
1215 rq
->vrq
->scsi
.cdb
[0]);
1217 esas2r_log(ESAS2R_LOG_WARN
,
1218 "request failure - SCSI error CDB:%x\n",
1219 rq
->vrq
->scsi
.cdb
[0]);
1221 } else if ((rq
->vrq
->scsi
.cdb
[0] != INQUIRY
1222 && rq
->vrq
->scsi
.cdb
[0] != REPORT_LUNS
)
1223 || (reqstatus
!= RS_SEL
1224 && reqstatus
!= RS_SEL2
)) {
1225 if ((reqstatus
== RS_UNDERRUN
) &&
1226 (rq
->vrq
->scsi
.cdb
[0] == INQUIRY
)) {
1227 /* Don't log inquiry underruns */
1229 esas2r_log(ESAS2R_LOG_WARN
,
1230 "request failure - cdb:%x reqstatus:%d target:%d",
1231 rq
->vrq
->scsi
.cdb
[0], reqstatus
,
1238 void esas2r_wait_request(struct esas2r_adapter
*a
, struct esas2r_request
*rq
)
1243 starttime
= jiffies_to_msecs(jiffies
);
1244 timeout
= rq
->timeout
? rq
->timeout
: 5000;
1247 esas2r_polled_interrupt(a
);
1249 if (rq
->req_stat
!= RS_STARTED
)
1252 schedule_timeout_interruptible(msecs_to_jiffies(100));
1254 if ((jiffies_to_msecs(jiffies
) - starttime
) > timeout
) {
1255 esas2r_hdebug("request TMO");
1258 rq
->req_stat
= RS_TIMEOUT
;
1260 esas2r_local_reset_adapter(a
);
1266 u32
esas2r_map_data_window(struct esas2r_adapter
*a
, u32 addr_lo
)
1268 u32 offset
= addr_lo
& (MW_DATA_WINDOW_SIZE
- 1);
1269 u32 base
= addr_lo
& -(signed int)MW_DATA_WINDOW_SIZE
;
1271 if (a
->window_base
!= base
) {
1272 esas2r_write_register_dword(a
, MVR_PCI_WIN1_REMAP
,
1273 base
| MVRPW1R_ENABLE
);
1274 esas2r_flush_register_dword(a
, MVR_PCI_WIN1_REMAP
);
1275 a
->window_base
= base
;
1281 /* Read a block of data from chip memory */
1282 bool esas2r_read_mem_block(struct esas2r_adapter
*a
,
1294 iatvr
= (from
& -(signed int)MW_DATA_WINDOW_SIZE
);
1296 esas2r_map_data_window(a
, iatvr
);
1298 offset
= from
& (MW_DATA_WINDOW_SIZE
- 1);
1301 if (len
> MW_DATA_WINDOW_SIZE
- offset
)
1302 len
= MW_DATA_WINDOW_SIZE
- offset
;
1308 *end
++ = esas2r_read_data_byte(a
, offset
);
1316 void esas2r_nuxi_mgt_data(u8 function
, void *data
)
1318 struct atto_vda_grp_info
*g
;
1319 struct atto_vda_devinfo
*d
;
1320 struct atto_vdapart_info
*p
;
1321 struct atto_vda_dh_info
*h
;
1322 struct atto_vda_metrics_info
*m
;
1323 struct atto_vda_schedule_info
*s
;
1324 struct atto_vda_buzzer_info
*b
;
1328 case VDAMGT_BUZZER_INFO
:
1329 case VDAMGT_BUZZER_SET
:
1331 b
= (struct atto_vda_buzzer_info
*)data
;
1333 b
->duration
= le32_to_cpu(b
->duration
);
1336 case VDAMGT_SCHEDULE_INFO
:
1337 case VDAMGT_SCHEDULE_EVENT
:
1339 s
= (struct atto_vda_schedule_info
*)data
;
1341 s
->id
= le32_to_cpu(s
->id
);
1345 case VDAMGT_DEV_INFO
:
1346 case VDAMGT_DEV_CLEAN
:
1347 case VDAMGT_DEV_PT_INFO
:
1348 case VDAMGT_DEV_FEATURES
:
1349 case VDAMGT_DEV_PT_FEATURES
:
1350 case VDAMGT_DEV_OPERATION
:
1352 d
= (struct atto_vda_devinfo
*)data
;
1354 d
->capacity
= le64_to_cpu(d
->capacity
);
1355 d
->block_size
= le32_to_cpu(d
->block_size
);
1356 d
->ses_dev_index
= le16_to_cpu(d
->ses_dev_index
);
1357 d
->target_id
= le16_to_cpu(d
->target_id
);
1358 d
->lun
= le16_to_cpu(d
->lun
);
1359 d
->features
= le16_to_cpu(d
->features
);
1362 case VDAMGT_GRP_INFO
:
1363 case VDAMGT_GRP_CREATE
:
1364 case VDAMGT_GRP_DELETE
:
1365 case VDAMGT_ADD_STORAGE
:
1366 case VDAMGT_MEMBER_ADD
:
1367 case VDAMGT_GRP_COMMIT
:
1368 case VDAMGT_GRP_REBUILD
:
1369 case VDAMGT_GRP_COMMIT_INIT
:
1370 case VDAMGT_QUICK_RAID
:
1371 case VDAMGT_GRP_FEATURES
:
1372 case VDAMGT_GRP_COMMIT_INIT_AUTOMAP
:
1373 case VDAMGT_QUICK_RAID_INIT_AUTOMAP
:
1374 case VDAMGT_SPARE_LIST
:
1375 case VDAMGT_SPARE_ADD
:
1376 case VDAMGT_SPARE_REMOVE
:
1377 case VDAMGT_LOCAL_SPARE_ADD
:
1378 case VDAMGT_GRP_OPERATION
:
1380 g
= (struct atto_vda_grp_info
*)data
;
1382 g
->capacity
= le64_to_cpu(g
->capacity
);
1383 g
->block_size
= le32_to_cpu(g
->block_size
);
1384 g
->interleave
= le32_to_cpu(g
->interleave
);
1385 g
->features
= le16_to_cpu(g
->features
);
1387 for (i
= 0; i
< 32; i
++)
1388 g
->members
[i
] = le16_to_cpu(g
->members
[i
]);
1392 case VDAMGT_PART_INFO
:
1393 case VDAMGT_PART_MAP
:
1394 case VDAMGT_PART_UNMAP
:
1395 case VDAMGT_PART_AUTOMAP
:
1396 case VDAMGT_PART_SPLIT
:
1397 case VDAMGT_PART_MERGE
:
1399 p
= (struct atto_vdapart_info
*)data
;
1401 p
->part_size
= le64_to_cpu(p
->part_size
);
1402 p
->start_lba
= le32_to_cpu(p
->start_lba
);
1403 p
->block_size
= le32_to_cpu(p
->block_size
);
1404 p
->target_id
= le16_to_cpu(p
->target_id
);
1407 case VDAMGT_DEV_HEALTH_REQ
:
1409 h
= (struct atto_vda_dh_info
*)data
;
1411 h
->med_defect_cnt
= le32_to_cpu(h
->med_defect_cnt
);
1412 h
->info_exc_cnt
= le32_to_cpu(h
->info_exc_cnt
);
1415 case VDAMGT_DEV_METRICS
:
1417 m
= (struct atto_vda_metrics_info
*)data
;
1419 for (i
= 0; i
< 32; i
++)
1420 m
->dev_indexes
[i
] = le16_to_cpu(m
->dev_indexes
[i
]);
1429 void esas2r_nuxi_cfg_data(u8 function
, void *data
)
1431 struct atto_vda_cfg_init
*ci
;
1435 case VDA_CFG_GET_INIT
:
1436 case VDA_CFG_GET_INIT2
:
1438 ci
= (struct atto_vda_cfg_init
*)data
;
1440 ci
->date_time
.year
= le16_to_cpu(ci
->date_time
.year
);
1441 ci
->sgl_page_size
= le32_to_cpu(ci
->sgl_page_size
);
1442 ci
->vda_version
= le32_to_cpu(ci
->vda_version
);
1443 ci
->epoch_time
= le32_to_cpu(ci
->epoch_time
);
1444 ci
->ioctl_tunnel
= le32_to_cpu(ci
->ioctl_tunnel
);
1445 ci
->num_targets_backend
= le32_to_cpu(ci
->num_targets_backend
);
1453 void esas2r_nuxi_ae_data(union atto_vda_ae
*ae
)
1455 struct atto_vda_ae_raid
*r
= &ae
->raid
;
1456 struct atto_vda_ae_lu
*l
= &ae
->lu
;
1458 switch (ae
->hdr
.bytype
) {
1459 case VDAAE_HDR_TYPE_RAID
:
1461 r
->dwflags
= le32_to_cpu(r
->dwflags
);
1464 case VDAAE_HDR_TYPE_LU
:
1466 l
->dwevent
= le32_to_cpu(l
->dwevent
);
1467 l
->wphys_target_id
= le16_to_cpu(l
->wphys_target_id
);
1468 l
->id
.tgtlun
.wtarget_id
= le16_to_cpu(l
->id
.tgtlun
.wtarget_id
);
1470 if (l
->hdr
.bylength
>= offsetof(struct atto_vda_ae_lu
, id
)
1471 + sizeof(struct atto_vda_ae_lu_tgt_lun_raid
)) {
1472 l
->id
.tgtlun_raid
.dwinterleave
1473 = le32_to_cpu(l
->id
.tgtlun_raid
.dwinterleave
);
1474 l
->id
.tgtlun_raid
.dwblock_size
1475 = le32_to_cpu(l
->id
.tgtlun_raid
.dwblock_size
);
1480 case VDAAE_HDR_TYPE_DISK
:
1486 void esas2r_free_request(struct esas2r_adapter
*a
, struct esas2r_request
*rq
)
1488 unsigned long flags
;
1490 esas2r_rq_destroy_request(rq
, a
);
1491 spin_lock_irqsave(&a
->request_lock
, flags
);
1492 list_add(&rq
->comp_list
, &a
->avail_request
);
1493 spin_unlock_irqrestore(&a
->request_lock
, flags
);
1496 struct esas2r_request
*esas2r_alloc_request(struct esas2r_adapter
*a
)
1498 struct esas2r_request
*rq
;
1499 unsigned long flags
;
1501 spin_lock_irqsave(&a
->request_lock
, flags
);
1503 if (unlikely(list_empty(&a
->avail_request
))) {
1504 spin_unlock_irqrestore(&a
->request_lock
, flags
);
1508 rq
= list_first_entry(&a
->avail_request
, struct esas2r_request
,
1510 list_del(&rq
->comp_list
);
1511 spin_unlock_irqrestore(&a
->request_lock
, flags
);
1512 esas2r_rq_init_request(rq
, a
);
1518 void esas2r_complete_request_cb(struct esas2r_adapter
*a
,
1519 struct esas2r_request
*rq
)
1521 esas2r_debug("completing request %p\n", rq
);
1523 scsi_dma_unmap(rq
->cmd
);
1525 if (unlikely(rq
->req_stat
!= RS_SUCCESS
)) {
1526 esas2r_debug("[%x STATUS %x:%x (%x)]", rq
->target_id
,
1528 rq
->func_rsp
.scsi_rsp
.scsi_stat
,
1532 ((esas2r_req_status_to_error(rq
->req_stat
) << 16)
1533 | (rq
->func_rsp
.scsi_rsp
.scsi_stat
& STATUS_MASK
));
1535 if (rq
->req_stat
== RS_UNDERRUN
)
1536 scsi_set_resid(rq
->cmd
,
1537 le32_to_cpu(rq
->func_rsp
.scsi_rsp
.
1540 scsi_set_resid(rq
->cmd
, 0);
1543 rq
->cmd
->scsi_done(rq
->cmd
);
1545 esas2r_free_request(a
, rq
);
1548 /* Run tasklet to handle stuff outside of interrupt context. */
1549 void esas2r_adapter_tasklet(unsigned long context
)
1551 struct esas2r_adapter
*a
= (struct esas2r_adapter
*)context
;
1553 if (unlikely(test_bit(AF2_TIMER_TICK
, &a
->flags2
))) {
1554 clear_bit(AF2_TIMER_TICK
, &a
->flags2
);
1555 esas2r_timer_tick(a
);
1558 if (likely(test_bit(AF2_INT_PENDING
, &a
->flags2
))) {
1559 clear_bit(AF2_INT_PENDING
, &a
->flags2
);
1560 esas2r_adapter_interrupt(a
);
1563 if (esas2r_is_tasklet_pending(a
))
1564 esas2r_do_tasklet_tasks(a
);
1566 if (esas2r_is_tasklet_pending(a
)
1567 || (test_bit(AF2_INT_PENDING
, &a
->flags2
))
1568 || (test_bit(AF2_TIMER_TICK
, &a
->flags2
))) {
1569 clear_bit(AF_TASKLET_SCHEDULED
, &a
->flags
);
1570 esas2r_schedule_tasklet(a
);
1572 clear_bit(AF_TASKLET_SCHEDULED
, &a
->flags
);
1576 static void esas2r_timer_callback(struct timer_list
*t
);
1578 void esas2r_kickoff_timer(struct esas2r_adapter
*a
)
1580 timer_setup(&a
->timer
, esas2r_timer_callback
, 0);
1582 a
->timer
.expires
= jiffies
+
1583 msecs_to_jiffies(100);
1585 add_timer(&a
->timer
);
1588 static void esas2r_timer_callback(struct timer_list
*t
)
1590 struct esas2r_adapter
*a
= from_timer(a
, t
, timer
);
1592 set_bit(AF2_TIMER_TICK
, &a
->flags2
);
1594 esas2r_schedule_tasklet(a
);
1596 esas2r_kickoff_timer(a
);
1600 * Firmware events need to be handled outside of interrupt context
1601 * so we schedule a delayed_work to handle them.
1605 esas2r_free_fw_event(struct esas2r_fw_event_work
*fw_event
)
1607 unsigned long flags
;
1608 struct esas2r_adapter
*a
= fw_event
->a
;
1610 spin_lock_irqsave(&a
->fw_event_lock
, flags
);
1611 list_del(&fw_event
->list
);
1613 spin_unlock_irqrestore(&a
->fw_event_lock
, flags
);
1617 esas2r_fw_event_off(struct esas2r_adapter
*a
)
1619 unsigned long flags
;
1621 spin_lock_irqsave(&a
->fw_event_lock
, flags
);
1622 a
->fw_events_off
= 1;
1623 spin_unlock_irqrestore(&a
->fw_event_lock
, flags
);
1627 esas2r_fw_event_on(struct esas2r_adapter
*a
)
1629 unsigned long flags
;
1631 spin_lock_irqsave(&a
->fw_event_lock
, flags
);
1632 a
->fw_events_off
= 0;
1633 spin_unlock_irqrestore(&a
->fw_event_lock
, flags
);
1636 static void esas2r_add_device(struct esas2r_adapter
*a
, u16 target_id
)
1639 struct scsi_device
*scsi_dev
;
1641 scsi_dev
= scsi_device_lookup(a
->host
, 0, target_id
, 0);
1648 "scsi device already exists at id %d", target_id
);
1650 scsi_device_put(scsi_dev
);
1656 "scsi_add_device() called for 0:%d:0",
1659 ret
= scsi_add_device(a
->host
, 0, target_id
, 0);
1665 "scsi_add_device failed with %d for id %d",
1671 static void esas2r_remove_device(struct esas2r_adapter
*a
, u16 target_id
)
1673 struct scsi_device
*scsi_dev
;
1675 scsi_dev
= scsi_device_lookup(a
->host
, 0, target_id
, 0);
1678 scsi_device_set_state(scsi_dev
, SDEV_OFFLINE
);
1684 "scsi_remove_device() called for 0:%d:0",
1687 scsi_remove_device(scsi_dev
);
1693 "scsi_device_put() called");
1695 scsi_device_put(scsi_dev
);
1699 &(a
->host
->shost_gendev
),
1700 "no target found at id %d",
1706 * Sends a firmware asynchronous event to anyone who happens to be
1707 * listening on the defined ATTO VDA event ports.
1709 static void esas2r_send_ae_event(struct esas2r_fw_event_work
*fw_event
)
1711 struct esas2r_vda_ae
*ae
= (struct esas2r_vda_ae
*)fw_event
->data
;
1714 switch (ae
->vda_ae
.hdr
.bytype
) {
1715 case VDAAE_HDR_TYPE_RAID
:
1716 type
= "RAID group state change";
1719 case VDAAE_HDR_TYPE_LU
:
1720 type
= "Mapped destination LU change";
1723 case VDAAE_HDR_TYPE_DISK
:
1724 type
= "Physical disk inventory change";
1727 case VDAAE_HDR_TYPE_RESET
:
1728 type
= "Firmware reset";
1731 case VDAAE_HDR_TYPE_LOG_INFO
:
1732 type
= "Event Log message (INFO level)";
1735 case VDAAE_HDR_TYPE_LOG_WARN
:
1736 type
= "Event Log message (WARN level)";
1739 case VDAAE_HDR_TYPE_LOG_CRIT
:
1740 type
= "Event Log message (CRIT level)";
1743 case VDAAE_HDR_TYPE_LOG_FAIL
:
1744 type
= "Event Log message (FAIL level)";
1747 case VDAAE_HDR_TYPE_NVC
:
1748 type
= "NVCache change";
1751 case VDAAE_HDR_TYPE_TLG_INFO
:
1752 type
= "Time stamped log message (INFO level)";
1755 case VDAAE_HDR_TYPE_TLG_WARN
:
1756 type
= "Time stamped log message (WARN level)";
1759 case VDAAE_HDR_TYPE_TLG_CRIT
:
1760 type
= "Time stamped log message (CRIT level)";
1763 case VDAAE_HDR_TYPE_PWRMGT
:
1764 type
= "Power management";
1767 case VDAAE_HDR_TYPE_MUTE
:
1768 type
= "Mute button pressed";
1771 case VDAAE_HDR_TYPE_DEV
:
1772 type
= "Device attribute change";
1780 esas2r_log(ESAS2R_LOG_WARN
,
1781 "An async event of type \"%s\" was received from the firmware. The event contents are:",
1783 esas2r_log_hexdump(ESAS2R_LOG_WARN
, &ae
->vda_ae
,
1784 ae
->vda_ae
.hdr
.bylength
);
1789 esas2r_firmware_event_work(struct work_struct
*work
)
1791 struct esas2r_fw_event_work
*fw_event
=
1792 container_of(work
, struct esas2r_fw_event_work
, work
.work
);
1794 struct esas2r_adapter
*a
= fw_event
->a
;
1796 u16 target_id
= *(u16
*)&fw_event
->data
[0];
1798 if (a
->fw_events_off
)
1801 switch (fw_event
->type
) {
1803 break; /* do nothing */
1805 case fw_event_lun_change
:
1806 esas2r_remove_device(a
, target_id
);
1807 esas2r_add_device(a
, target_id
);
1810 case fw_event_present
:
1811 esas2r_add_device(a
, target_id
);
1814 case fw_event_not_present
:
1815 esas2r_remove_device(a
, target_id
);
1818 case fw_event_vda_ae
:
1819 esas2r_send_ae_event(fw_event
);
1824 esas2r_free_fw_event(fw_event
);
1827 void esas2r_queue_fw_event(struct esas2r_adapter
*a
,
1828 enum fw_event_type type
,
1832 struct esas2r_fw_event_work
*fw_event
;
1833 unsigned long flags
;
1835 fw_event
= kzalloc(sizeof(struct esas2r_fw_event_work
), GFP_ATOMIC
);
1837 esas2r_log(ESAS2R_LOG_WARN
,
1838 "esas2r_queue_fw_event failed to alloc");
1842 if (type
== fw_event_vda_ae
) {
1843 struct esas2r_vda_ae
*ae
=
1844 (struct esas2r_vda_ae
*)fw_event
->data
;
1846 ae
->signature
= ESAS2R_VDA_EVENT_SIG
;
1847 ae
->bus_number
= a
->pcid
->bus
->number
;
1848 ae
->devfn
= a
->pcid
->devfn
;
1849 memcpy(&ae
->vda_ae
, data
, sizeof(ae
->vda_ae
));
1851 memcpy(fw_event
->data
, data
, data_sz
);
1854 fw_event
->type
= type
;
1857 spin_lock_irqsave(&a
->fw_event_lock
, flags
);
1858 list_add_tail(&fw_event
->list
, &a
->fw_event_list
);
1859 INIT_DELAYED_WORK(&fw_event
->work
, esas2r_firmware_event_work
);
1860 queue_delayed_work_on(
1861 smp_processor_id(), a
->fw_event_q
, &fw_event
->work
,
1862 msecs_to_jiffies(1));
1863 spin_unlock_irqrestore(&a
->fw_event_lock
, flags
);
1866 void esas2r_target_state_changed(struct esas2r_adapter
*a
, u16 targ_id
,
1869 if (state
== TS_LUN_CHANGE
)
1870 esas2r_queue_fw_event(a
, fw_event_lun_change
, &targ_id
,
1872 else if (state
== TS_PRESENT
)
1873 esas2r_queue_fw_event(a
, fw_event_present
, &targ_id
,
1875 else if (state
== TS_NOT_PRESENT
)
1876 esas2r_queue_fw_event(a
, fw_event_not_present
, &targ_id
,
1880 /* Translate status to a Linux SCSI mid-layer error code */
1881 int esas2r_req_status_to_error(u8 req_stat
)
1888 * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
1889 * it will check the scsi_stat value in the completion anyway.
1896 return DID_NO_CONNECT
;
1905 return DID_BUS_BUSY
;
1908 /* everything else is just an error. */
1913 module_init(esas2r_init
);
1914 module_exit(esas2r_exit
);