1*0e086994SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 209345f65SFUJITA Tomonori /* 309345f65SFUJITA Tomonori * SCSI RDMA (SRP) transport class 409345f65SFUJITA Tomonori * 509345f65SFUJITA Tomonori * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org> 609345f65SFUJITA Tomonori */ 709345f65SFUJITA Tomonori #include <linux/init.h> 809345f65SFUJITA Tomonori #include <linux/module.h> 909345f65SFUJITA Tomonori #include <linux/jiffies.h> 1009345f65SFUJITA Tomonori #include <linux/err.h> 1109345f65SFUJITA Tomonori #include <linux/slab.h> 1209345f65SFUJITA Tomonori #include <linux/string.h> 1309345f65SFUJITA Tomonori 1409345f65SFUJITA Tomonori #include <scsi/scsi.h> 1529c17324SBart Van Assche #include <scsi/scsi_cmnd.h> 1609345f65SFUJITA Tomonori #include <scsi/scsi_device.h> 1709345f65SFUJITA Tomonori #include <scsi/scsi_host.h> 1809345f65SFUJITA Tomonori #include <scsi/scsi_transport.h> 1909345f65SFUJITA Tomonori #include <scsi/scsi_transport_srp.h> 2029c17324SBart Van Assche #include "scsi_priv.h" 2109345f65SFUJITA Tomonori 2209345f65SFUJITA Tomonori struct srp_host_attrs { 2309345f65SFUJITA Tomonori atomic_t next_port_id; 2409345f65SFUJITA Tomonori }; 2509345f65SFUJITA Tomonori #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data) 2609345f65SFUJITA Tomonori 2709345f65SFUJITA Tomonori #define SRP_HOST_ATTRS 0 288c64e453SBart Van Assche #define SRP_RPORT_ATTRS 8 2909345f65SFUJITA Tomonori 3009345f65SFUJITA Tomonori struct srp_internal { 3109345f65SFUJITA Tomonori struct scsi_transport_template t; 3209345f65SFUJITA Tomonori struct srp_function_template *f; 3309345f65SFUJITA Tomonori 34ee959b00STony Jones struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1]; 3509345f65SFUJITA Tomonori 36ee959b00STony Jones struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1]; 3709345f65SFUJITA Tomonori struct transport_container rport_attr_cont; 3809345f65SFUJITA Tomonori }; 3909345f65SFUJITA Tomonori 40c9ddf734SBart Van Assche static int scsi_is_srp_rport(const struct device *dev); 41c9ddf734SBart Van Assche 4209345f65SFUJITA Tomonori #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t) 4309345f65SFUJITA Tomonori 4409345f65SFUJITA Tomonori #define dev_to_rport(d) container_of(d, struct srp_rport, dev) 45ee959b00STony Jones #define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent) 4629c17324SBart Van Assche static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r) 4729c17324SBart Van Assche { 4829c17324SBart Van Assche return dev_to_shost(r->dev.parent); 4929c17324SBart Van Assche } 5029c17324SBart Van Assche 51c9ddf734SBart Van Assche static int find_child_rport(struct device *dev, void *data) 52c9ddf734SBart Van Assche { 53c9ddf734SBart Van Assche struct device **child = data; 54c9ddf734SBart Van Assche 55c9ddf734SBart Van Assche if (scsi_is_srp_rport(dev)) { 56c9ddf734SBart Van Assche WARN_ON_ONCE(*child); 57c9ddf734SBart Van Assche *child = dev; 58c9ddf734SBart Van Assche } 59c9ddf734SBart Van Assche return 0; 60c9ddf734SBart Van Assche } 61c9ddf734SBart Van Assche 62e68ca752SBart Van Assche static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost) 63e68ca752SBart Van Assche { 64c9ddf734SBart Van Assche struct device *child = NULL; 65c9ddf734SBart Van Assche 66c9ddf734SBart Van Assche WARN_ON_ONCE(device_for_each_child(&shost->shost_gendev, &child, 67c9ddf734SBart Van Assche find_child_rport) < 0); 68c9ddf734SBart Van Assche return child ? dev_to_rport(child) : NULL; 69e68ca752SBart Van Assche } 70e68ca752SBart Van Assche 7129c17324SBart Van Assche /** 7229c17324SBart Van Assche * srp_tmo_valid() - check timeout combination validity 730c7f8218SBart Van Assche * @reconnect_delay: Reconnect delay in seconds. 740c7f8218SBart Van Assche * @fast_io_fail_tmo: Fast I/O fail timeout in seconds. 750c7f8218SBart Van Assche * @dev_loss_tmo: Device loss timeout in seconds. 7629c17324SBart Van Assche * 7729c17324SBart Van Assche * The combination of the timeout parameters must be such that SCSI commands 7829c17324SBart Van Assche * are finished in a reasonable time. Hence do not allow the fast I/O fail 7918cc4e02SBart Van Assche * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to 8018cc4e02SBart Van Assche * exceed that limit if failing I/O fast has been disabled. Furthermore, these 8129c17324SBart Van Assche * parameters must be such that multipath can detect failed paths timely. 828c64e453SBart Van Assche * Hence do not allow all three parameters to be disabled simultaneously. 8329c17324SBart Van Assche */ 8485aa93beSBart Van Assche int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, long dev_loss_tmo) 8529c17324SBart Van Assche { 868c64e453SBart Van Assche if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0) 878c64e453SBart Van Assche return -EINVAL; 888c64e453SBart Van Assche if (reconnect_delay == 0) 8929c17324SBart Van Assche return -EINVAL; 9029c17324SBart Van Assche if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT) 9129c17324SBart Van Assche return -EINVAL; 9218cc4e02SBart Van Assche if (fast_io_fail_tmo < 0 && 9318cc4e02SBart Van Assche dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT) 9418cc4e02SBart Van Assche return -EINVAL; 9529c17324SBart Van Assche if (dev_loss_tmo >= LONG_MAX / HZ) 9629c17324SBart Van Assche return -EINVAL; 9729c17324SBart Van Assche if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 && 9829c17324SBart Van Assche fast_io_fail_tmo >= dev_loss_tmo) 9929c17324SBart Van Assche return -EINVAL; 10029c17324SBart Van Assche return 0; 10129c17324SBart Van Assche } 10229c17324SBart Van Assche EXPORT_SYMBOL_GPL(srp_tmo_valid); 10309345f65SFUJITA Tomonori 10409345f65SFUJITA Tomonori static int srp_host_setup(struct transport_container *tc, struct device *dev, 105ee959b00STony Jones struct device *cdev) 10609345f65SFUJITA Tomonori { 10709345f65SFUJITA Tomonori struct Scsi_Host *shost = dev_to_shost(dev); 10809345f65SFUJITA Tomonori struct srp_host_attrs *srp_host = to_srp_host_attrs(shost); 10909345f65SFUJITA Tomonori 11009345f65SFUJITA Tomonori atomic_set(&srp_host->next_port_id, 0); 11109345f65SFUJITA Tomonori return 0; 11209345f65SFUJITA Tomonori } 11309345f65SFUJITA Tomonori 11409345f65SFUJITA Tomonori static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup, 11509345f65SFUJITA Tomonori NULL, NULL); 11609345f65SFUJITA Tomonori 11709345f65SFUJITA Tomonori static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports", 11809345f65SFUJITA Tomonori NULL, NULL, NULL); 11909345f65SFUJITA Tomonori 12009345f65SFUJITA Tomonori static ssize_t 121ee959b00STony Jones show_srp_rport_id(struct device *dev, struct device_attribute *attr, 122ee959b00STony Jones char *buf) 12309345f65SFUJITA Tomonori { 124ee959b00STony Jones struct srp_rport *rport = transport_class_to_srp_rport(dev); 125df441cc0SAndy Shevchenko return sprintf(buf, "%16phC\n", rport->port_id); 12609345f65SFUJITA Tomonori } 12709345f65SFUJITA Tomonori 128ee959b00STony Jones static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL); 12909345f65SFUJITA Tomonori 130aebd5e47SFUJITA Tomonori static const struct { 131aebd5e47SFUJITA Tomonori u32 value; 132aebd5e47SFUJITA Tomonori char *name; 133aebd5e47SFUJITA Tomonori } srp_rport_role_names[] = { 134aebd5e47SFUJITA Tomonori {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"}, 135aebd5e47SFUJITA Tomonori {SRP_RPORT_ROLE_TARGET, "SRP Target"}, 136aebd5e47SFUJITA Tomonori }; 137aebd5e47SFUJITA Tomonori 138aebd5e47SFUJITA Tomonori static ssize_t 139ee959b00STony Jones show_srp_rport_roles(struct device *dev, struct device_attribute *attr, 140ee959b00STony Jones char *buf) 141aebd5e47SFUJITA Tomonori { 142ee959b00STony Jones struct srp_rport *rport = transport_class_to_srp_rport(dev); 143aebd5e47SFUJITA Tomonori int i; 144aebd5e47SFUJITA Tomonori char *name = NULL; 145aebd5e47SFUJITA Tomonori 146aebd5e47SFUJITA Tomonori for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++) 147aebd5e47SFUJITA Tomonori if (srp_rport_role_names[i].value == rport->roles) { 148aebd5e47SFUJITA Tomonori name = srp_rport_role_names[i].name; 149aebd5e47SFUJITA Tomonori break; 150aebd5e47SFUJITA Tomonori } 151aebd5e47SFUJITA Tomonori return sprintf(buf, "%s\n", name ? : "unknown"); 152aebd5e47SFUJITA Tomonori } 153aebd5e47SFUJITA Tomonori 154ee959b00STony Jones static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL); 155aebd5e47SFUJITA Tomonori 156dc1bdbd9SBart Van Assche static ssize_t store_srp_rport_delete(struct device *dev, 157dc1bdbd9SBart Van Assche struct device_attribute *attr, 158dc1bdbd9SBart Van Assche const char *buf, size_t count) 159dc1bdbd9SBart Van Assche { 160dc1bdbd9SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 161dc1bdbd9SBart Van Assche struct Scsi_Host *shost = dev_to_shost(dev); 162dc1bdbd9SBart Van Assche struct srp_internal *i = to_srp_internal(shost->transportt); 163dc1bdbd9SBart Van Assche 164dc1bdbd9SBart Van Assche if (i->f->rport_delete) { 165dc1bdbd9SBart Van Assche i->f->rport_delete(rport); 166dc1bdbd9SBart Van Assche return count; 167dc1bdbd9SBart Van Assche } else { 168dc1bdbd9SBart Van Assche return -ENOSYS; 169dc1bdbd9SBart Van Assche } 170dc1bdbd9SBart Van Assche } 171dc1bdbd9SBart Van Assche 172dc1bdbd9SBart Van Assche static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete); 173dc1bdbd9SBart Van Assche 17429c17324SBart Van Assche static ssize_t show_srp_rport_state(struct device *dev, 17529c17324SBart Van Assche struct device_attribute *attr, 17629c17324SBart Van Assche char *buf) 17729c17324SBart Van Assche { 17829c17324SBart Van Assche static const char *const state_name[] = { 17929c17324SBart Van Assche [SRP_RPORT_RUNNING] = "running", 18029c17324SBart Van Assche [SRP_RPORT_BLOCKED] = "blocked", 18129c17324SBart Van Assche [SRP_RPORT_FAIL_FAST] = "fail-fast", 18229c17324SBart Van Assche [SRP_RPORT_LOST] = "lost", 18329c17324SBart Van Assche }; 18429c17324SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 18529c17324SBart Van Assche enum srp_rport_state state = rport->state; 18629c17324SBart Van Assche 18729c17324SBart Van Assche return sprintf(buf, "%s\n", 18829c17324SBart Van Assche (unsigned)state < ARRAY_SIZE(state_name) ? 18929c17324SBart Van Assche state_name[state] : "???"); 19029c17324SBart Van Assche } 19129c17324SBart Van Assche 19229c17324SBart Van Assche static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL); 19329c17324SBart Van Assche 19429c17324SBart Van Assche static ssize_t srp_show_tmo(char *buf, int tmo) 19529c17324SBart Van Assche { 19629c17324SBart Van Assche return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n"); 19729c17324SBart Van Assche } 19829c17324SBart Van Assche 1993fdf70acSSagi Grimberg int srp_parse_tmo(int *tmo, const char *buf) 20029c17324SBart Van Assche { 20129c17324SBart Van Assche int res = 0; 20229c17324SBart Van Assche 20329c17324SBart Van Assche if (strncmp(buf, "off", 3) != 0) 20429c17324SBart Van Assche res = kstrtoint(buf, 0, tmo); 20529c17324SBart Van Assche else 20629c17324SBart Van Assche *tmo = -1; 20729c17324SBart Van Assche 20829c17324SBart Van Assche return res; 20929c17324SBart Van Assche } 2103fdf70acSSagi Grimberg EXPORT_SYMBOL(srp_parse_tmo); 21129c17324SBart Van Assche 2128c64e453SBart Van Assche static ssize_t show_reconnect_delay(struct device *dev, 2138c64e453SBart Van Assche struct device_attribute *attr, char *buf) 2148c64e453SBart Van Assche { 2158c64e453SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 2168c64e453SBart Van Assche 2178c64e453SBart Van Assche return srp_show_tmo(buf, rport->reconnect_delay); 2188c64e453SBart Van Assche } 2198c64e453SBart Van Assche 2208c64e453SBart Van Assche static ssize_t store_reconnect_delay(struct device *dev, 2218c64e453SBart Van Assche struct device_attribute *attr, 2228c64e453SBart Van Assche const char *buf, const size_t count) 2238c64e453SBart Van Assche { 2248c64e453SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 2258c64e453SBart Van Assche int res, delay; 2268c64e453SBart Van Assche 2278c64e453SBart Van Assche res = srp_parse_tmo(&delay, buf); 2288c64e453SBart Van Assche if (res) 2298c64e453SBart Van Assche goto out; 2308c64e453SBart Van Assche res = srp_tmo_valid(delay, rport->fast_io_fail_tmo, 2318c64e453SBart Van Assche rport->dev_loss_tmo); 2328c64e453SBart Van Assche if (res) 2338c64e453SBart Van Assche goto out; 2348c64e453SBart Van Assche 2358c64e453SBart Van Assche if (rport->reconnect_delay <= 0 && delay > 0 && 2368c64e453SBart Van Assche rport->state != SRP_RPORT_RUNNING) { 2378c64e453SBart Van Assche queue_delayed_work(system_long_wq, &rport->reconnect_work, 2388c64e453SBart Van Assche delay * HZ); 2398c64e453SBart Van Assche } else if (delay <= 0) { 2408c64e453SBart Van Assche cancel_delayed_work(&rport->reconnect_work); 2418c64e453SBart Van Assche } 2428c64e453SBart Van Assche rport->reconnect_delay = delay; 2438c64e453SBart Van Assche res = count; 2448c64e453SBart Van Assche 2458c64e453SBart Van Assche out: 2468c64e453SBart Van Assche return res; 2478c64e453SBart Van Assche } 2488c64e453SBart Van Assche 2498c64e453SBart Van Assche static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay, 2508c64e453SBart Van Assche store_reconnect_delay); 2518c64e453SBart Van Assche 2528c64e453SBart Van Assche static ssize_t show_failed_reconnects(struct device *dev, 2538c64e453SBart Van Assche struct device_attribute *attr, char *buf) 2548c64e453SBart Van Assche { 2558c64e453SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 2568c64e453SBart Van Assche 2578c64e453SBart Van Assche return sprintf(buf, "%d\n", rport->failed_reconnects); 2588c64e453SBart Van Assche } 2598c64e453SBart Van Assche 2608c64e453SBart Van Assche static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL); 2618c64e453SBart Van Assche 26229c17324SBart Van Assche static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev, 26329c17324SBart Van Assche struct device_attribute *attr, 26429c17324SBart Van Assche char *buf) 26529c17324SBart Van Assche { 26629c17324SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 26729c17324SBart Van Assche 26829c17324SBart Van Assche return srp_show_tmo(buf, rport->fast_io_fail_tmo); 26929c17324SBart Van Assche } 27029c17324SBart Van Assche 27129c17324SBart Van Assche static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev, 27229c17324SBart Van Assche struct device_attribute *attr, 27329c17324SBart Van Assche const char *buf, size_t count) 27429c17324SBart Van Assche { 27529c17324SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 27629c17324SBart Van Assche int res; 27729c17324SBart Van Assche int fast_io_fail_tmo; 27829c17324SBart Van Assche 27929c17324SBart Van Assche res = srp_parse_tmo(&fast_io_fail_tmo, buf); 28029c17324SBart Van Assche if (res) 28129c17324SBart Van Assche goto out; 2828c64e453SBart Van Assche res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo, 2838c64e453SBart Van Assche rport->dev_loss_tmo); 28429c17324SBart Van Assche if (res) 28529c17324SBart Van Assche goto out; 28629c17324SBart Van Assche rport->fast_io_fail_tmo = fast_io_fail_tmo; 28729c17324SBart Van Assche res = count; 28829c17324SBart Van Assche 28929c17324SBart Van Assche out: 29029c17324SBart Van Assche return res; 29129c17324SBart Van Assche } 29229c17324SBart Van Assche 29329c17324SBart Van Assche static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR, 29429c17324SBart Van Assche show_srp_rport_fast_io_fail_tmo, 29529c17324SBart Van Assche store_srp_rport_fast_io_fail_tmo); 29629c17324SBart Van Assche 29729c17324SBart Van Assche static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev, 29829c17324SBart Van Assche struct device_attribute *attr, 29929c17324SBart Van Assche char *buf) 30029c17324SBart Van Assche { 30129c17324SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 30229c17324SBart Van Assche 30329c17324SBart Van Assche return srp_show_tmo(buf, rport->dev_loss_tmo); 30429c17324SBart Van Assche } 30529c17324SBart Van Assche 30629c17324SBart Van Assche static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev, 30729c17324SBart Van Assche struct device_attribute *attr, 30829c17324SBart Van Assche const char *buf, size_t count) 30929c17324SBart Van Assche { 31029c17324SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 31129c17324SBart Van Assche int res; 31229c17324SBart Van Assche int dev_loss_tmo; 31329c17324SBart Van Assche 31429c17324SBart Van Assche res = srp_parse_tmo(&dev_loss_tmo, buf); 31529c17324SBart Van Assche if (res) 31629c17324SBart Van Assche goto out; 3178c64e453SBart Van Assche res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo, 3188c64e453SBart Van Assche dev_loss_tmo); 31929c17324SBart Van Assche if (res) 32029c17324SBart Van Assche goto out; 32129c17324SBart Van Assche rport->dev_loss_tmo = dev_loss_tmo; 32229c17324SBart Van Assche res = count; 32329c17324SBart Van Assche 32429c17324SBart Van Assche out: 32529c17324SBart Van Assche return res; 32629c17324SBart Van Assche } 32729c17324SBart Van Assche 32829c17324SBart Van Assche static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR, 32929c17324SBart Van Assche show_srp_rport_dev_loss_tmo, 33029c17324SBart Van Assche store_srp_rport_dev_loss_tmo); 33129c17324SBart Van Assche 33229c17324SBart Van Assche static int srp_rport_set_state(struct srp_rport *rport, 33329c17324SBart Van Assche enum srp_rport_state new_state) 33429c17324SBart Van Assche { 33529c17324SBart Van Assche enum srp_rport_state old_state = rport->state; 33629c17324SBart Van Assche 33729c17324SBart Van Assche lockdep_assert_held(&rport->mutex); 33829c17324SBart Van Assche 33929c17324SBart Van Assche switch (new_state) { 34029c17324SBart Van Assche case SRP_RPORT_RUNNING: 34129c17324SBart Van Assche switch (old_state) { 34229c17324SBart Van Assche case SRP_RPORT_LOST: 34329c17324SBart Van Assche goto invalid; 34429c17324SBart Van Assche default: 34529c17324SBart Van Assche break; 34629c17324SBart Van Assche } 34729c17324SBart Van Assche break; 34829c17324SBart Van Assche case SRP_RPORT_BLOCKED: 34929c17324SBart Van Assche switch (old_state) { 35029c17324SBart Van Assche case SRP_RPORT_RUNNING: 35129c17324SBart Van Assche break; 35229c17324SBart Van Assche default: 35329c17324SBart Van Assche goto invalid; 35429c17324SBart Van Assche } 35529c17324SBart Van Assche break; 35629c17324SBart Van Assche case SRP_RPORT_FAIL_FAST: 35729c17324SBart Van Assche switch (old_state) { 35829c17324SBart Van Assche case SRP_RPORT_LOST: 35929c17324SBart Van Assche goto invalid; 36029c17324SBart Van Assche default: 36129c17324SBart Van Assche break; 36229c17324SBart Van Assche } 36329c17324SBart Van Assche break; 36429c17324SBart Van Assche case SRP_RPORT_LOST: 36529c17324SBart Van Assche break; 36629c17324SBart Van Assche } 36729c17324SBart Van Assche rport->state = new_state; 36829c17324SBart Van Assche return 0; 36929c17324SBart Van Assche 37029c17324SBart Van Assche invalid: 37129c17324SBart Van Assche return -EINVAL; 37229c17324SBart Van Assche } 37329c17324SBart Van Assche 3748c64e453SBart Van Assche /** 3758c64e453SBart Van Assche * srp_reconnect_work() - reconnect and schedule a new attempt if necessary 3760c7f8218SBart Van Assche * @work: Work structure used for scheduling this operation. 3778c64e453SBart Van Assche */ 3788c64e453SBart Van Assche static void srp_reconnect_work(struct work_struct *work) 3798c64e453SBart Van Assche { 3808c64e453SBart Van Assche struct srp_rport *rport = container_of(to_delayed_work(work), 3818c64e453SBart Van Assche struct srp_rport, reconnect_work); 3828c64e453SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 3838c64e453SBart Van Assche int delay, res; 3848c64e453SBart Van Assche 3858c64e453SBart Van Assche res = srp_reconnect_rport(rport); 3868c64e453SBart Van Assche if (res != 0) { 3878c64e453SBart Van Assche shost_printk(KERN_ERR, shost, 3888c64e453SBart Van Assche "reconnect attempt %d failed (%d)\n", 3898c64e453SBart Van Assche ++rport->failed_reconnects, res); 3908c64e453SBart Van Assche delay = rport->reconnect_delay * 3918c64e453SBart Van Assche min(100, max(1, rport->failed_reconnects - 10)); 3928c64e453SBart Van Assche if (delay > 0) 3938c64e453SBart Van Assche queue_delayed_work(system_long_wq, 3948c64e453SBart Van Assche &rport->reconnect_work, delay * HZ); 3958c64e453SBart Van Assche } 3968c64e453SBart Van Assche } 3978c64e453SBart Van Assche 39829c17324SBart Van Assche static void __rport_fail_io_fast(struct srp_rport *rport) 39929c17324SBart Van Assche { 40029c17324SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 40129c17324SBart Van Assche struct srp_internal *i; 40229c17324SBart Van Assche 40329c17324SBart Van Assche lockdep_assert_held(&rport->mutex); 40429c17324SBart Van Assche 40529c17324SBart Van Assche if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST)) 40629c17324SBart Van Assche return; 407669f0441SBart Van Assche /* 408669f0441SBart Van Assche * Call scsi_target_block() to wait for ongoing shost->queuecommand() 409669f0441SBart Van Assche * calls before invoking i->f->terminate_rport_io(). 410669f0441SBart Van Assche */ 411669f0441SBart Van Assche scsi_target_block(rport->dev.parent); 41229c17324SBart Van Assche scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE); 41329c17324SBart Van Assche 41429c17324SBart Van Assche /* Involve the LLD if possible to terminate all I/O on the rport. */ 41529c17324SBart Van Assche i = to_srp_internal(shost->transportt); 416669f0441SBart Van Assche if (i->f->terminate_rport_io) 41729c17324SBart Van Assche i->f->terminate_rport_io(rport); 41829c17324SBart Van Assche } 41929c17324SBart Van Assche 42029c17324SBart Van Assche /** 42129c17324SBart Van Assche * rport_fast_io_fail_timedout() - fast I/O failure timeout handler 4220c7f8218SBart Van Assche * @work: Work structure used for scheduling this operation. 42329c17324SBart Van Assche */ 42429c17324SBart Van Assche static void rport_fast_io_fail_timedout(struct work_struct *work) 42529c17324SBart Van Assche { 42629c17324SBart Van Assche struct srp_rport *rport = container_of(to_delayed_work(work), 42729c17324SBart Van Assche struct srp_rport, fast_io_fail_work); 42829c17324SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 42929c17324SBart Van Assche 43029c17324SBart Van Assche pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n", 43129c17324SBart Van Assche dev_name(&rport->dev), dev_name(&shost->shost_gendev)); 43229c17324SBart Van Assche 43329c17324SBart Van Assche mutex_lock(&rport->mutex); 43429c17324SBart Van Assche if (rport->state == SRP_RPORT_BLOCKED) 43529c17324SBart Van Assche __rport_fail_io_fast(rport); 43629c17324SBart Van Assche mutex_unlock(&rport->mutex); 43729c17324SBart Van Assche } 43829c17324SBart Van Assche 43929c17324SBart Van Assche /** 44029c17324SBart Van Assche * rport_dev_loss_timedout() - device loss timeout handler 4410c7f8218SBart Van Assche * @work: Work structure used for scheduling this operation. 44229c17324SBart Van Assche */ 44329c17324SBart Van Assche static void rport_dev_loss_timedout(struct work_struct *work) 44429c17324SBart Van Assche { 44529c17324SBart Van Assche struct srp_rport *rport = container_of(to_delayed_work(work), 44629c17324SBart Van Assche struct srp_rport, dev_loss_work); 44729c17324SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 44829c17324SBart Van Assche struct srp_internal *i = to_srp_internal(shost->transportt); 44929c17324SBart Van Assche 45029c17324SBart Van Assche pr_info("dev_loss_tmo expired for SRP %s / %s.\n", 45129c17324SBart Van Assche dev_name(&rport->dev), dev_name(&shost->shost_gendev)); 45229c17324SBart Van Assche 45329c17324SBart Van Assche mutex_lock(&rport->mutex); 45429c17324SBart Van Assche WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0); 45529c17324SBart Van Assche scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE); 45629c17324SBart Van Assche mutex_unlock(&rport->mutex); 45729c17324SBart Van Assche 45829c17324SBart Van Assche i->f->rport_delete(rport); 45929c17324SBart Van Assche } 46029c17324SBart Van Assche 46129c17324SBart Van Assche static void __srp_start_tl_fail_timers(struct srp_rport *rport) 46229c17324SBart Van Assche { 46329c17324SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 4648c64e453SBart Van Assche int delay, fast_io_fail_tmo, dev_loss_tmo; 46529c17324SBart Van Assche 46629c17324SBart Van Assche lockdep_assert_held(&rport->mutex); 46729c17324SBart Van Assche 4688c64e453SBart Van Assche delay = rport->reconnect_delay; 46929c17324SBart Van Assche fast_io_fail_tmo = rport->fast_io_fail_tmo; 47029c17324SBart Van Assche dev_loss_tmo = rport->dev_loss_tmo; 47193079162SBart Van Assche pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev), 47293079162SBart Van Assche rport->state); 47329c17324SBart Van Assche 47493079162SBart Van Assche if (rport->state == SRP_RPORT_LOST) 47593079162SBart Van Assche return; 4768c64e453SBart Van Assche if (delay > 0) 47793079162SBart Van Assche queue_delayed_work(system_long_wq, &rport->reconnect_work, 4788c64e453SBart Van Assche 1UL * delay * HZ); 479cd53eb68SBart Van Assche if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) && 480cd53eb68SBart Van Assche srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) { 48193079162SBart Van Assche pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev), 48229c17324SBart Van Assche rport->state); 48329c17324SBart Van Assche scsi_target_block(&shost->shost_gendev); 48418cc4e02SBart Van Assche if (fast_io_fail_tmo >= 0) 48529c17324SBart Van Assche queue_delayed_work(system_long_wq, 48629c17324SBart Van Assche &rport->fast_io_fail_work, 48729c17324SBart Van Assche 1UL * fast_io_fail_tmo * HZ); 48829c17324SBart Van Assche if (dev_loss_tmo >= 0) 48929c17324SBart Van Assche queue_delayed_work(system_long_wq, 49029c17324SBart Van Assche &rport->dev_loss_work, 49129c17324SBart Van Assche 1UL * dev_loss_tmo * HZ); 49218cc4e02SBart Van Assche } 49329c17324SBart Van Assche } 49429c17324SBart Van Assche 49529c17324SBart Van Assche /** 49629c17324SBart Van Assche * srp_start_tl_fail_timers() - start the transport layer failure timers 4970c7f8218SBart Van Assche * @rport: SRP target port. 49829c17324SBart Van Assche * 49929c17324SBart Van Assche * Start the transport layer fast I/O failure and device loss timers. Do not 50029c17324SBart Van Assche * modify a timer that was already started. 50129c17324SBart Van Assche */ 50229c17324SBart Van Assche void srp_start_tl_fail_timers(struct srp_rport *rport) 50329c17324SBart Van Assche { 50429c17324SBart Van Assche mutex_lock(&rport->mutex); 50529c17324SBart Van Assche __srp_start_tl_fail_timers(rport); 50629c17324SBart Van Assche mutex_unlock(&rport->mutex); 50729c17324SBart Van Assche } 50829c17324SBart Van Assche EXPORT_SYMBOL(srp_start_tl_fail_timers); 50929c17324SBart Van Assche 51029c17324SBart Van Assche /** 51129c17324SBart Van Assche * srp_reconnect_rport() - reconnect to an SRP target port 5120c7f8218SBart Van Assche * @rport: SRP target port. 51329c17324SBart Van Assche * 51429c17324SBart Van Assche * Blocks SCSI command queueing before invoking reconnect() such that 51529c17324SBart Van Assche * queuecommand() won't be invoked concurrently with reconnect() from outside 51629c17324SBart Van Assche * the SCSI EH. This is important since a reconnect() implementation may 51729c17324SBart Van Assche * reallocate resources needed by queuecommand(). 51829c17324SBart Van Assche * 51929c17324SBart Van Assche * Notes: 52029c17324SBart Van Assche * - This function neither waits until outstanding requests have finished nor 52129c17324SBart Van Assche * tries to abort these. It is the responsibility of the reconnect() 52229c17324SBart Van Assche * function to finish outstanding commands before reconnecting to the target 52329c17324SBart Van Assche * port. 52429c17324SBart Van Assche * - It is the responsibility of the caller to ensure that the resources 52529c17324SBart Van Assche * reallocated by the reconnect() function won't be used while this function 52629c17324SBart Van Assche * is in progress. One possible strategy is to invoke this function from 52729c17324SBart Van Assche * the context of the SCSI EH thread only. Another possible strategy is to 52829c17324SBart Van Assche * lock the rport mutex inside each SCSI LLD callback that can be invoked by 52929c17324SBart Van Assche * the SCSI EH (the scsi_host_template.eh_*() functions and also the 53029c17324SBart Van Assche * scsi_host_template.queuecommand() function). 53129c17324SBart Van Assche */ 53229c17324SBart Van Assche int srp_reconnect_rport(struct srp_rport *rport) 53329c17324SBart Van Assche { 53429c17324SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 53529c17324SBart Van Assche struct srp_internal *i = to_srp_internal(shost->transportt); 53629c17324SBart Van Assche struct scsi_device *sdev; 53729c17324SBart Van Assche int res; 53829c17324SBart Van Assche 53929c17324SBart Van Assche pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev)); 54029c17324SBart Van Assche 54129c17324SBart Van Assche res = mutex_lock_interruptible(&rport->mutex); 54229c17324SBart Van Assche if (res) 54329c17324SBart Van Assche goto out; 54429c17324SBart Van Assche scsi_target_block(&shost->shost_gendev); 54593079162SBart Van Assche res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV; 54629c17324SBart Van Assche pr_debug("%s (state %d): transport.reconnect() returned %d\n", 54729c17324SBart Van Assche dev_name(&shost->shost_gendev), rport->state, res); 54829c17324SBart Van Assche if (res == 0) { 54929c17324SBart Van Assche cancel_delayed_work(&rport->fast_io_fail_work); 55029c17324SBart Van Assche cancel_delayed_work(&rport->dev_loss_work); 55129c17324SBart Van Assche 5528c64e453SBart Van Assche rport->failed_reconnects = 0; 55329c17324SBart Van Assche srp_rport_set_state(rport, SRP_RPORT_RUNNING); 55429c17324SBart Van Assche scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING); 55529c17324SBart Van Assche /* 55629c17324SBart Van Assche * If the SCSI error handler has offlined one or more devices, 55729c17324SBart Van Assche * invoking scsi_target_unblock() won't change the state of 55829c17324SBart Van Assche * these devices into running so do that explicitly. 55929c17324SBart Van Assche */ 5600db6ca8aSBart Van Assche shost_for_each_device(sdev, shost) { 5610db6ca8aSBart Van Assche mutex_lock(&sdev->state_mutex); 562a817e73fSLinus Torvalds if (sdev->sdev_state == SDEV_OFFLINE) 56329c17324SBart Van Assche sdev->sdev_state = SDEV_RUNNING; 5640db6ca8aSBart Van Assche mutex_unlock(&sdev->state_mutex); 5650db6ca8aSBart Van Assche } 56629c17324SBart Van Assche } else if (rport->state == SRP_RPORT_RUNNING) { 56729c17324SBart Van Assche /* 56818cc4e02SBart Van Assche * srp_reconnect_rport() has been invoked with fast_io_fail 56918cc4e02SBart Van Assche * and dev_loss off. Mark the port as failed and start the TL 57018cc4e02SBart Van Assche * failure timers if these had not yet been started. 57129c17324SBart Van Assche */ 57229c17324SBart Van Assche __rport_fail_io_fast(rport); 57329c17324SBart Van Assche scsi_target_unblock(&shost->shost_gendev, 57429c17324SBart Van Assche SDEV_TRANSPORT_OFFLINE); 57529c17324SBart Van Assche __srp_start_tl_fail_timers(rport); 57629c17324SBart Van Assche } else if (rport->state != SRP_RPORT_BLOCKED) { 57729c17324SBart Van Assche scsi_target_unblock(&shost->shost_gendev, 57829c17324SBart Van Assche SDEV_TRANSPORT_OFFLINE); 57929c17324SBart Van Assche } 58029c17324SBart Van Assche mutex_unlock(&rport->mutex); 58129c17324SBart Van Assche 58229c17324SBart Van Assche out: 58329c17324SBart Van Assche return res; 58429c17324SBart Van Assche } 58529c17324SBart Van Assche EXPORT_SYMBOL(srp_reconnect_rport); 58629c17324SBart Van Assche 58729c17324SBart Van Assche /** 58829c17324SBart Van Assche * srp_timed_out() - SRP transport intercept of the SCSI timeout EH 5890c7f8218SBart Van Assche * @scmd: SCSI command. 59029c17324SBart Van Assche * 59129c17324SBart Van Assche * If a timeout occurs while an rport is in the blocked state, ask the SCSI 59229c17324SBart Van Assche * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core 5936600593cSChristoph Hellwig * handle the timeout (BLK_EH_DONE). 59429c17324SBart Van Assche * 59529c17324SBart Van Assche * Note: This function is called from soft-IRQ context and with the request 59629c17324SBart Van Assche * queue lock held. 59729c17324SBart Van Assche */ 598b6a05c82SChristoph Hellwig enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd) 59929c17324SBart Van Assche { 60029c17324SBart Van Assche struct scsi_device *sdev = scmd->device; 60129c17324SBart Van Assche struct Scsi_Host *shost = sdev->host; 60229c17324SBart Van Assche struct srp_internal *i = to_srp_internal(shost->transportt); 603e68ca752SBart Van Assche struct srp_rport *rport = shost_to_rport(shost); 60429c17324SBart Van Assche 60529c17324SBart Van Assche pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev)); 606c9ddf734SBart Van Assche return rport && rport->fast_io_fail_tmo < 0 && 607c9ddf734SBart Van Assche rport->dev_loss_tmo < 0 && 608e68ca752SBart Van Assche i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ? 6096600593cSChristoph Hellwig BLK_EH_RESET_TIMER : BLK_EH_DONE; 61029c17324SBart Van Assche } 611b6a05c82SChristoph Hellwig EXPORT_SYMBOL(srp_timed_out); 61229c17324SBart Van Assche 61309345f65SFUJITA Tomonori static void srp_rport_release(struct device *dev) 61409345f65SFUJITA Tomonori { 61509345f65SFUJITA Tomonori struct srp_rport *rport = dev_to_rport(dev); 61609345f65SFUJITA Tomonori 61709345f65SFUJITA Tomonori put_device(dev->parent); 61809345f65SFUJITA Tomonori kfree(rport); 61909345f65SFUJITA Tomonori } 62009345f65SFUJITA Tomonori 62109345f65SFUJITA Tomonori static int scsi_is_srp_rport(const struct device *dev) 62209345f65SFUJITA Tomonori { 62309345f65SFUJITA Tomonori return dev->release == srp_rport_release; 62409345f65SFUJITA Tomonori } 62509345f65SFUJITA Tomonori 62609345f65SFUJITA Tomonori static int srp_rport_match(struct attribute_container *cont, 62709345f65SFUJITA Tomonori struct device *dev) 62809345f65SFUJITA Tomonori { 62909345f65SFUJITA Tomonori struct Scsi_Host *shost; 63009345f65SFUJITA Tomonori struct srp_internal *i; 63109345f65SFUJITA Tomonori 63209345f65SFUJITA Tomonori if (!scsi_is_srp_rport(dev)) 63309345f65SFUJITA Tomonori return 0; 63409345f65SFUJITA Tomonori 63509345f65SFUJITA Tomonori shost = dev_to_shost(dev->parent); 63609345f65SFUJITA Tomonori if (!shost->transportt) 63709345f65SFUJITA Tomonori return 0; 63809345f65SFUJITA Tomonori if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) 63909345f65SFUJITA Tomonori return 0; 64009345f65SFUJITA Tomonori 64109345f65SFUJITA Tomonori i = to_srp_internal(shost->transportt); 64209345f65SFUJITA Tomonori return &i->rport_attr_cont.ac == cont; 64309345f65SFUJITA Tomonori } 64409345f65SFUJITA Tomonori 64509345f65SFUJITA Tomonori static int srp_host_match(struct attribute_container *cont, struct device *dev) 64609345f65SFUJITA Tomonori { 64709345f65SFUJITA Tomonori struct Scsi_Host *shost; 64809345f65SFUJITA Tomonori struct srp_internal *i; 64909345f65SFUJITA Tomonori 65009345f65SFUJITA Tomonori if (!scsi_is_host_device(dev)) 65109345f65SFUJITA Tomonori return 0; 65209345f65SFUJITA Tomonori 65309345f65SFUJITA Tomonori shost = dev_to_shost(dev); 65409345f65SFUJITA Tomonori if (!shost->transportt) 65509345f65SFUJITA Tomonori return 0; 65609345f65SFUJITA Tomonori if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) 65709345f65SFUJITA Tomonori return 0; 65809345f65SFUJITA Tomonori 65909345f65SFUJITA Tomonori i = to_srp_internal(shost->transportt); 66009345f65SFUJITA Tomonori return &i->t.host_attrs.ac == cont; 66109345f65SFUJITA Tomonori } 66209345f65SFUJITA Tomonori 66309345f65SFUJITA Tomonori /** 6649dd69a60SBart Van Assche * srp_rport_get() - increment rport reference count 6650c7f8218SBart Van Assche * @rport: SRP target port. 6669dd69a60SBart Van Assche */ 6679dd69a60SBart Van Assche void srp_rport_get(struct srp_rport *rport) 6689dd69a60SBart Van Assche { 6699dd69a60SBart Van Assche get_device(&rport->dev); 6709dd69a60SBart Van Assche } 6719dd69a60SBart Van Assche EXPORT_SYMBOL(srp_rport_get); 6729dd69a60SBart Van Assche 6739dd69a60SBart Van Assche /** 6749dd69a60SBart Van Assche * srp_rport_put() - decrement rport reference count 6750c7f8218SBart Van Assche * @rport: SRP target port. 6769dd69a60SBart Van Assche */ 6779dd69a60SBart Van Assche void srp_rport_put(struct srp_rport *rport) 6789dd69a60SBart Van Assche { 6799dd69a60SBart Van Assche put_device(&rport->dev); 6809dd69a60SBart Van Assche } 6819dd69a60SBart Van Assche EXPORT_SYMBOL(srp_rport_put); 6829dd69a60SBart Van Assche 6839dd69a60SBart Van Assche /** 68409345f65SFUJITA Tomonori * srp_rport_add - add a SRP remote port to the device hierarchy 68509345f65SFUJITA Tomonori * @shost: scsi host the remote port is connected to. 68609345f65SFUJITA Tomonori * @ids: The port id for the remote port. 68709345f65SFUJITA Tomonori * 688dc8875e1SRandy Dunlap * Publishes a port to the rest of the system. 68909345f65SFUJITA Tomonori */ 69009345f65SFUJITA Tomonori struct srp_rport *srp_rport_add(struct Scsi_Host *shost, 69109345f65SFUJITA Tomonori struct srp_rport_identifiers *ids) 69209345f65SFUJITA Tomonori { 69309345f65SFUJITA Tomonori struct srp_rport *rport; 69409345f65SFUJITA Tomonori struct device *parent = &shost->shost_gendev; 69529c17324SBart Van Assche struct srp_internal *i = to_srp_internal(shost->transportt); 69609345f65SFUJITA Tomonori int id, ret; 69709345f65SFUJITA Tomonori 69809345f65SFUJITA Tomonori rport = kzalloc(sizeof(*rport), GFP_KERNEL); 69909345f65SFUJITA Tomonori if (!rport) 70009345f65SFUJITA Tomonori return ERR_PTR(-ENOMEM); 70109345f65SFUJITA Tomonori 70229c17324SBart Van Assche mutex_init(&rport->mutex); 70329c17324SBart Van Assche 70409345f65SFUJITA Tomonori device_initialize(&rport->dev); 70509345f65SFUJITA Tomonori 70609345f65SFUJITA Tomonori rport->dev.parent = get_device(parent); 70709345f65SFUJITA Tomonori rport->dev.release = srp_rport_release; 70809345f65SFUJITA Tomonori 70909345f65SFUJITA Tomonori memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id)); 710aebd5e47SFUJITA Tomonori rport->roles = ids->roles; 71109345f65SFUJITA Tomonori 7128c64e453SBart Van Assche if (i->f->reconnect) 7138c64e453SBart Van Assche rport->reconnect_delay = i->f->reconnect_delay ? 7148c64e453SBart Van Assche *i->f->reconnect_delay : 10; 7158c64e453SBart Van Assche INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work); 71629c17324SBart Van Assche rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ? 71729c17324SBart Van Assche *i->f->fast_io_fail_tmo : 15; 71829c17324SBart Van Assche rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60; 71929c17324SBart Van Assche INIT_DELAYED_WORK(&rport->fast_io_fail_work, 72029c17324SBart Van Assche rport_fast_io_fail_timedout); 72129c17324SBart Van Assche INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout); 72229c17324SBart Van Assche 72309345f65SFUJITA Tomonori id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id); 72471610f55SKay Sievers dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id); 72509345f65SFUJITA Tomonori 72609345f65SFUJITA Tomonori transport_setup_device(&rport->dev); 72709345f65SFUJITA Tomonori 72809345f65SFUJITA Tomonori ret = device_add(&rport->dev); 72909345f65SFUJITA Tomonori if (ret) { 73009345f65SFUJITA Tomonori transport_destroy_device(&rport->dev); 73109345f65SFUJITA Tomonori put_device(&rport->dev); 73209345f65SFUJITA Tomonori return ERR_PTR(ret); 73309345f65SFUJITA Tomonori } 73409345f65SFUJITA Tomonori 73509345f65SFUJITA Tomonori transport_add_device(&rport->dev); 73609345f65SFUJITA Tomonori transport_configure_device(&rport->dev); 73709345f65SFUJITA Tomonori 73809345f65SFUJITA Tomonori return rport; 73909345f65SFUJITA Tomonori } 74009345f65SFUJITA Tomonori EXPORT_SYMBOL_GPL(srp_rport_add); 74109345f65SFUJITA Tomonori 74209345f65SFUJITA Tomonori /** 743eb44820cSRob Landley * srp_rport_del - remove a SRP remote port 744eb44820cSRob Landley * @rport: SRP remote port to remove 74509345f65SFUJITA Tomonori * 74609345f65SFUJITA Tomonori * Removes the specified SRP remote port. 74709345f65SFUJITA Tomonori */ 74809345f65SFUJITA Tomonori void srp_rport_del(struct srp_rport *rport) 74909345f65SFUJITA Tomonori { 75009345f65SFUJITA Tomonori struct device *dev = &rport->dev; 75162fe8826SFUJITA Tomonori 75209345f65SFUJITA Tomonori transport_remove_device(dev); 75309345f65SFUJITA Tomonori device_del(dev); 75409345f65SFUJITA Tomonori transport_destroy_device(dev); 75529c17324SBart Van Assche 75609345f65SFUJITA Tomonori put_device(dev); 75709345f65SFUJITA Tomonori } 75809345f65SFUJITA Tomonori EXPORT_SYMBOL_GPL(srp_rport_del); 75909345f65SFUJITA Tomonori 76009345f65SFUJITA Tomonori static int do_srp_rport_del(struct device *dev, void *data) 76109345f65SFUJITA Tomonori { 76291183344SDave Dillow if (scsi_is_srp_rport(dev)) 76309345f65SFUJITA Tomonori srp_rport_del(dev_to_rport(dev)); 76409345f65SFUJITA Tomonori return 0; 76509345f65SFUJITA Tomonori } 76609345f65SFUJITA Tomonori 76709345f65SFUJITA Tomonori /** 768eb44820cSRob Landley * srp_remove_host - tear down a Scsi_Host's SRP data structures 76909345f65SFUJITA Tomonori * @shost: Scsi Host that is torn down 77009345f65SFUJITA Tomonori * 77109345f65SFUJITA Tomonori * Removes all SRP remote ports for a given Scsi_Host. 77209345f65SFUJITA Tomonori * Must be called just before scsi_remove_host for SRP HBAs. 77309345f65SFUJITA Tomonori */ 77409345f65SFUJITA Tomonori void srp_remove_host(struct Scsi_Host *shost) 77509345f65SFUJITA Tomonori { 77609345f65SFUJITA Tomonori device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del); 77709345f65SFUJITA Tomonori } 77809345f65SFUJITA Tomonori EXPORT_SYMBOL_GPL(srp_remove_host); 77909345f65SFUJITA Tomonori 78093079162SBart Van Assche /** 78193079162SBart Van Assche * srp_stop_rport_timers - stop the transport layer recovery timers 78267513605SBart Van Assche * @rport: SRP remote port for which to stop the timers. 78393079162SBart Van Assche * 78493079162SBart Van Assche * Must be called after srp_remove_host() and scsi_remove_host(). The caller 78593079162SBart Van Assche * must hold a reference on the rport (rport->dev) and on the SCSI host 78693079162SBart Van Assche * (rport->dev.parent). 78793079162SBart Van Assche */ 78893079162SBart Van Assche void srp_stop_rport_timers(struct srp_rport *rport) 78993079162SBart Van Assche { 79093079162SBart Van Assche mutex_lock(&rport->mutex); 79193079162SBart Van Assche if (rport->state == SRP_RPORT_BLOCKED) 79293079162SBart Van Assche __rport_fail_io_fast(rport); 79393079162SBart Van Assche srp_rport_set_state(rport, SRP_RPORT_LOST); 79493079162SBart Van Assche mutex_unlock(&rport->mutex); 79593079162SBart Van Assche 79693079162SBart Van Assche cancel_delayed_work_sync(&rport->reconnect_work); 79793079162SBart Van Assche cancel_delayed_work_sync(&rport->fast_io_fail_work); 79893079162SBart Van Assche cancel_delayed_work_sync(&rport->dev_loss_work); 79993079162SBart Van Assche } 80093079162SBart Van Assche EXPORT_SYMBOL_GPL(srp_stop_rport_timers); 80193079162SBart Van Assche 80209345f65SFUJITA Tomonori /** 803eb44820cSRob Landley * srp_attach_transport - instantiate SRP transport template 80409345f65SFUJITA Tomonori * @ft: SRP transport class function template 80509345f65SFUJITA Tomonori */ 80609345f65SFUJITA Tomonori struct scsi_transport_template * 80709345f65SFUJITA Tomonori srp_attach_transport(struct srp_function_template *ft) 80809345f65SFUJITA Tomonori { 80909345f65SFUJITA Tomonori int count; 81009345f65SFUJITA Tomonori struct srp_internal *i; 81109345f65SFUJITA Tomonori 81209345f65SFUJITA Tomonori i = kzalloc(sizeof(*i), GFP_KERNEL); 81309345f65SFUJITA Tomonori if (!i) 81409345f65SFUJITA Tomonori return NULL; 81509345f65SFUJITA Tomonori 81609345f65SFUJITA Tomonori i->t.host_size = sizeof(struct srp_host_attrs); 81709345f65SFUJITA Tomonori i->t.host_attrs.ac.attrs = &i->host_attrs[0]; 81809345f65SFUJITA Tomonori i->t.host_attrs.ac.class = &srp_host_class.class; 81909345f65SFUJITA Tomonori i->t.host_attrs.ac.match = srp_host_match; 82009345f65SFUJITA Tomonori i->host_attrs[0] = NULL; 82109345f65SFUJITA Tomonori transport_container_register(&i->t.host_attrs); 82209345f65SFUJITA Tomonori 82309345f65SFUJITA Tomonori i->rport_attr_cont.ac.attrs = &i->rport_attrs[0]; 82409345f65SFUJITA Tomonori i->rport_attr_cont.ac.class = &srp_rport_class.class; 82509345f65SFUJITA Tomonori i->rport_attr_cont.ac.match = srp_rport_match; 82609345f65SFUJITA Tomonori 82709345f65SFUJITA Tomonori count = 0; 8289134a855SBart Van Assche i->rport_attrs[count++] = &dev_attr_port_id; 8299134a855SBart Van Assche i->rport_attrs[count++] = &dev_attr_roles; 83029c17324SBart Van Assche if (ft->has_rport_state) { 83129c17324SBart Van Assche i->rport_attrs[count++] = &dev_attr_state; 83229c17324SBart Van Assche i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo; 83329c17324SBart Van Assche i->rport_attrs[count++] = &dev_attr_dev_loss_tmo; 83429c17324SBart Van Assche } 8358c64e453SBart Van Assche if (ft->reconnect) { 8368c64e453SBart Van Assche i->rport_attrs[count++] = &dev_attr_reconnect_delay; 8378c64e453SBart Van Assche i->rport_attrs[count++] = &dev_attr_failed_reconnects; 8388c64e453SBart Van Assche } 839dc1bdbd9SBart Van Assche if (ft->rport_delete) 840dc1bdbd9SBart Van Assche i->rport_attrs[count++] = &dev_attr_delete; 8419134a855SBart Van Assche i->rport_attrs[count++] = NULL; 8429134a855SBart Van Assche BUG_ON(count > ARRAY_SIZE(i->rport_attrs)); 84309345f65SFUJITA Tomonori 844ac9be30eSBart Van Assche transport_container_register(&i->rport_attr_cont); 845ac9be30eSBart Van Assche 84609345f65SFUJITA Tomonori i->f = ft; 84709345f65SFUJITA Tomonori 84809345f65SFUJITA Tomonori return &i->t; 84909345f65SFUJITA Tomonori } 85009345f65SFUJITA Tomonori EXPORT_SYMBOL_GPL(srp_attach_transport); 85109345f65SFUJITA Tomonori 85209345f65SFUJITA Tomonori /** 853eb44820cSRob Landley * srp_release_transport - release SRP transport template instance 85409345f65SFUJITA Tomonori * @t: transport template instance 85509345f65SFUJITA Tomonori */ 85609345f65SFUJITA Tomonori void srp_release_transport(struct scsi_transport_template *t) 85709345f65SFUJITA Tomonori { 85809345f65SFUJITA Tomonori struct srp_internal *i = to_srp_internal(t); 85909345f65SFUJITA Tomonori 86009345f65SFUJITA Tomonori transport_container_unregister(&i->t.host_attrs); 86109345f65SFUJITA Tomonori transport_container_unregister(&i->rport_attr_cont); 86209345f65SFUJITA Tomonori 86309345f65SFUJITA Tomonori kfree(i); 86409345f65SFUJITA Tomonori } 86509345f65SFUJITA Tomonori EXPORT_SYMBOL_GPL(srp_release_transport); 86609345f65SFUJITA Tomonori 86709345f65SFUJITA Tomonori static __init int srp_transport_init(void) 86809345f65SFUJITA Tomonori { 86909345f65SFUJITA Tomonori int ret; 87009345f65SFUJITA Tomonori 87109345f65SFUJITA Tomonori ret = transport_class_register(&srp_host_class); 87209345f65SFUJITA Tomonori if (ret) 87309345f65SFUJITA Tomonori return ret; 87409345f65SFUJITA Tomonori ret = transport_class_register(&srp_rport_class); 87509345f65SFUJITA Tomonori if (ret) 87609345f65SFUJITA Tomonori goto unregister_host_class; 87709345f65SFUJITA Tomonori 87809345f65SFUJITA Tomonori return 0; 87909345f65SFUJITA Tomonori unregister_host_class: 88009345f65SFUJITA Tomonori transport_class_unregister(&srp_host_class); 88109345f65SFUJITA Tomonori return ret; 88209345f65SFUJITA Tomonori } 88309345f65SFUJITA Tomonori 88409345f65SFUJITA Tomonori static void __exit srp_transport_exit(void) 88509345f65SFUJITA Tomonori { 88609345f65SFUJITA Tomonori transport_class_unregister(&srp_host_class); 88709345f65SFUJITA Tomonori transport_class_unregister(&srp_rport_class); 88809345f65SFUJITA Tomonori } 88909345f65SFUJITA Tomonori 89009345f65SFUJITA Tomonori MODULE_AUTHOR("FUJITA Tomonori"); 89109345f65SFUJITA Tomonori MODULE_DESCRIPTION("SRP Transport Attributes"); 89209345f65SFUJITA Tomonori MODULE_LICENSE("GPL"); 89309345f65SFUJITA Tomonori 89409345f65SFUJITA Tomonori module_init(srp_transport_init); 89509345f65SFUJITA Tomonori module_exit(srp_transport_exit); 896