109345f65SFUJITA Tomonori /* 209345f65SFUJITA Tomonori * SCSI RDMA (SRP) transport class 309345f65SFUJITA Tomonori * 409345f65SFUJITA Tomonori * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org> 509345f65SFUJITA Tomonori * 609345f65SFUJITA Tomonori * This program is free software; you can redistribute it and/or 709345f65SFUJITA Tomonori * modify it under the terms of the GNU General Public License as 809345f65SFUJITA Tomonori * published by the Free Software Foundation, version 2 of the 909345f65SFUJITA Tomonori * License. 1009345f65SFUJITA Tomonori * 1109345f65SFUJITA Tomonori * This program is distributed in the hope that it will be useful, but 1209345f65SFUJITA Tomonori * WITHOUT ANY WARRANTY; without even the implied warranty of 1309345f65SFUJITA Tomonori * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1409345f65SFUJITA Tomonori * General Public License for more details. 1509345f65SFUJITA Tomonori * 1609345f65SFUJITA Tomonori * You should have received a copy of the GNU General Public License 1709345f65SFUJITA Tomonori * along with this program; if not, write to the Free Software 1809345f65SFUJITA Tomonori * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 1909345f65SFUJITA Tomonori * 02110-1301 USA 2009345f65SFUJITA Tomonori */ 2109345f65SFUJITA Tomonori #include <linux/init.h> 2209345f65SFUJITA Tomonori #include <linux/module.h> 2309345f65SFUJITA Tomonori #include <linux/jiffies.h> 2409345f65SFUJITA Tomonori #include <linux/err.h> 2509345f65SFUJITA Tomonori #include <linux/slab.h> 2609345f65SFUJITA Tomonori #include <linux/string.h> 2709345f65SFUJITA Tomonori 2809345f65SFUJITA Tomonori #include <scsi/scsi.h> 2929c17324SBart Van Assche #include <scsi/scsi_cmnd.h> 3009345f65SFUJITA Tomonori #include <scsi/scsi_device.h> 3109345f65SFUJITA Tomonori #include <scsi/scsi_host.h> 3209345f65SFUJITA Tomonori #include <scsi/scsi_transport.h> 3309345f65SFUJITA Tomonori #include <scsi/scsi_transport_srp.h> 3429c17324SBart Van Assche #include "scsi_priv.h" 3509345f65SFUJITA Tomonori 3609345f65SFUJITA Tomonori struct srp_host_attrs { 3709345f65SFUJITA Tomonori atomic_t next_port_id; 3809345f65SFUJITA Tomonori }; 3909345f65SFUJITA Tomonori #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data) 4009345f65SFUJITA Tomonori 4109345f65SFUJITA Tomonori #define SRP_HOST_ATTRS 0 428c64e453SBart Van Assche #define SRP_RPORT_ATTRS 8 4309345f65SFUJITA Tomonori 4409345f65SFUJITA Tomonori struct srp_internal { 4509345f65SFUJITA Tomonori struct scsi_transport_template t; 4609345f65SFUJITA Tomonori struct srp_function_template *f; 4709345f65SFUJITA Tomonori 48ee959b00STony Jones struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1]; 4909345f65SFUJITA Tomonori 50ee959b00STony Jones struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1]; 5109345f65SFUJITA Tomonori struct transport_container rport_attr_cont; 5209345f65SFUJITA Tomonori }; 5309345f65SFUJITA Tomonori 5409345f65SFUJITA Tomonori #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t) 5509345f65SFUJITA Tomonori 5609345f65SFUJITA Tomonori #define dev_to_rport(d) container_of(d, struct srp_rport, dev) 57ee959b00STony Jones #define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent) 5829c17324SBart Van Assche static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r) 5929c17324SBart Van Assche { 6029c17324SBart Van Assche return dev_to_shost(r->dev.parent); 6129c17324SBart Van Assche } 6229c17324SBart Van Assche 63e68ca752SBart Van Assche static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost) 64e68ca752SBart Van Assche { 65e68ca752SBart Van Assche return transport_class_to_srp_rport(&shost->shost_gendev); 66e68ca752SBart Van Assche } 67e68ca752SBart Van Assche 6829c17324SBart Van Assche /** 6929c17324SBart Van Assche * srp_tmo_valid() - check timeout combination validity 700c7f8218SBart Van Assche * @reconnect_delay: Reconnect delay in seconds. 710c7f8218SBart Van Assche * @fast_io_fail_tmo: Fast I/O fail timeout in seconds. 720c7f8218SBart Van Assche * @dev_loss_tmo: Device loss timeout in seconds. 7329c17324SBart Van Assche * 7429c17324SBart Van Assche * The combination of the timeout parameters must be such that SCSI commands 7529c17324SBart Van Assche * are finished in a reasonable time. Hence do not allow the fast I/O fail 7618cc4e02SBart Van Assche * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to 7718cc4e02SBart Van Assche * exceed that limit if failing I/O fast has been disabled. Furthermore, these 7829c17324SBart Van Assche * parameters must be such that multipath can detect failed paths timely. 798c64e453SBart Van Assche * Hence do not allow all three parameters to be disabled simultaneously. 8029c17324SBart Van Assche */ 818c64e453SBart Van Assche int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, int dev_loss_tmo) 8229c17324SBart Van Assche { 838c64e453SBart Van Assche if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0) 848c64e453SBart Van Assche return -EINVAL; 858c64e453SBart Van Assche if (reconnect_delay == 0) 8629c17324SBart Van Assche return -EINVAL; 8729c17324SBart Van Assche if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT) 8829c17324SBart Van Assche return -EINVAL; 8918cc4e02SBart Van Assche if (fast_io_fail_tmo < 0 && 9018cc4e02SBart Van Assche dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT) 9118cc4e02SBart Van Assche return -EINVAL; 9229c17324SBart Van Assche if (dev_loss_tmo >= LONG_MAX / HZ) 9329c17324SBart Van Assche return -EINVAL; 9429c17324SBart Van Assche if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 && 9529c17324SBart Van Assche fast_io_fail_tmo >= dev_loss_tmo) 9629c17324SBart Van Assche return -EINVAL; 9729c17324SBart Van Assche return 0; 9829c17324SBart Van Assche } 9929c17324SBart Van Assche EXPORT_SYMBOL_GPL(srp_tmo_valid); 10009345f65SFUJITA Tomonori 10109345f65SFUJITA Tomonori static int srp_host_setup(struct transport_container *tc, struct device *dev, 102ee959b00STony Jones struct device *cdev) 10309345f65SFUJITA Tomonori { 10409345f65SFUJITA Tomonori struct Scsi_Host *shost = dev_to_shost(dev); 10509345f65SFUJITA Tomonori struct srp_host_attrs *srp_host = to_srp_host_attrs(shost); 10609345f65SFUJITA Tomonori 10709345f65SFUJITA Tomonori atomic_set(&srp_host->next_port_id, 0); 10809345f65SFUJITA Tomonori return 0; 10909345f65SFUJITA Tomonori } 11009345f65SFUJITA Tomonori 11109345f65SFUJITA Tomonori static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup, 11209345f65SFUJITA Tomonori NULL, NULL); 11309345f65SFUJITA Tomonori 11409345f65SFUJITA Tomonori static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports", 11509345f65SFUJITA Tomonori NULL, NULL, NULL); 11609345f65SFUJITA Tomonori 11709345f65SFUJITA Tomonori static ssize_t 118ee959b00STony Jones show_srp_rport_id(struct device *dev, struct device_attribute *attr, 119ee959b00STony Jones char *buf) 12009345f65SFUJITA Tomonori { 121ee959b00STony Jones struct srp_rport *rport = transport_class_to_srp_rport(dev); 122df441cc0SAndy Shevchenko return sprintf(buf, "%16phC\n", rport->port_id); 12309345f65SFUJITA Tomonori } 12409345f65SFUJITA Tomonori 125ee959b00STony Jones static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL); 12609345f65SFUJITA Tomonori 127aebd5e47SFUJITA Tomonori static const struct { 128aebd5e47SFUJITA Tomonori u32 value; 129aebd5e47SFUJITA Tomonori char *name; 130aebd5e47SFUJITA Tomonori } srp_rport_role_names[] = { 131aebd5e47SFUJITA Tomonori {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"}, 132aebd5e47SFUJITA Tomonori {SRP_RPORT_ROLE_TARGET, "SRP Target"}, 133aebd5e47SFUJITA Tomonori }; 134aebd5e47SFUJITA Tomonori 135aebd5e47SFUJITA Tomonori static ssize_t 136ee959b00STony Jones show_srp_rport_roles(struct device *dev, struct device_attribute *attr, 137ee959b00STony Jones char *buf) 138aebd5e47SFUJITA Tomonori { 139ee959b00STony Jones struct srp_rport *rport = transport_class_to_srp_rport(dev); 140aebd5e47SFUJITA Tomonori int i; 141aebd5e47SFUJITA Tomonori char *name = NULL; 142aebd5e47SFUJITA Tomonori 143aebd5e47SFUJITA Tomonori for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++) 144aebd5e47SFUJITA Tomonori if (srp_rport_role_names[i].value == rport->roles) { 145aebd5e47SFUJITA Tomonori name = srp_rport_role_names[i].name; 146aebd5e47SFUJITA Tomonori break; 147aebd5e47SFUJITA Tomonori } 148aebd5e47SFUJITA Tomonori return sprintf(buf, "%s\n", name ? : "unknown"); 149aebd5e47SFUJITA Tomonori } 150aebd5e47SFUJITA Tomonori 151ee959b00STony Jones static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL); 152aebd5e47SFUJITA Tomonori 153dc1bdbd9SBart Van Assche static ssize_t store_srp_rport_delete(struct device *dev, 154dc1bdbd9SBart Van Assche struct device_attribute *attr, 155dc1bdbd9SBart Van Assche const char *buf, size_t count) 156dc1bdbd9SBart Van Assche { 157dc1bdbd9SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 158dc1bdbd9SBart Van Assche struct Scsi_Host *shost = dev_to_shost(dev); 159dc1bdbd9SBart Van Assche struct srp_internal *i = to_srp_internal(shost->transportt); 160dc1bdbd9SBart Van Assche 161dc1bdbd9SBart Van Assche if (i->f->rport_delete) { 162dc1bdbd9SBart Van Assche i->f->rport_delete(rport); 163dc1bdbd9SBart Van Assche return count; 164dc1bdbd9SBart Van Assche } else { 165dc1bdbd9SBart Van Assche return -ENOSYS; 166dc1bdbd9SBart Van Assche } 167dc1bdbd9SBart Van Assche } 168dc1bdbd9SBart Van Assche 169dc1bdbd9SBart Van Assche static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete); 170dc1bdbd9SBart Van Assche 17129c17324SBart Van Assche static ssize_t show_srp_rport_state(struct device *dev, 17229c17324SBart Van Assche struct device_attribute *attr, 17329c17324SBart Van Assche char *buf) 17429c17324SBart Van Assche { 17529c17324SBart Van Assche static const char *const state_name[] = { 17629c17324SBart Van Assche [SRP_RPORT_RUNNING] = "running", 17729c17324SBart Van Assche [SRP_RPORT_BLOCKED] = "blocked", 17829c17324SBart Van Assche [SRP_RPORT_FAIL_FAST] = "fail-fast", 17929c17324SBart Van Assche [SRP_RPORT_LOST] = "lost", 18029c17324SBart Van Assche }; 18129c17324SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 18229c17324SBart Van Assche enum srp_rport_state state = rport->state; 18329c17324SBart Van Assche 18429c17324SBart Van Assche return sprintf(buf, "%s\n", 18529c17324SBart Van Assche (unsigned)state < ARRAY_SIZE(state_name) ? 18629c17324SBart Van Assche state_name[state] : "???"); 18729c17324SBart Van Assche } 18829c17324SBart Van Assche 18929c17324SBart Van Assche static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL); 19029c17324SBart Van Assche 19129c17324SBart Van Assche static ssize_t srp_show_tmo(char *buf, int tmo) 19229c17324SBart Van Assche { 19329c17324SBart Van Assche return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n"); 19429c17324SBart Van Assche } 19529c17324SBart Van Assche 1963fdf70acSSagi Grimberg int srp_parse_tmo(int *tmo, const char *buf) 19729c17324SBart Van Assche { 19829c17324SBart Van Assche int res = 0; 19929c17324SBart Van Assche 20029c17324SBart Van Assche if (strncmp(buf, "off", 3) != 0) 20129c17324SBart Van Assche res = kstrtoint(buf, 0, tmo); 20229c17324SBart Van Assche else 20329c17324SBart Van Assche *tmo = -1; 20429c17324SBart Van Assche 20529c17324SBart Van Assche return res; 20629c17324SBart Van Assche } 2073fdf70acSSagi Grimberg EXPORT_SYMBOL(srp_parse_tmo); 20829c17324SBart Van Assche 2098c64e453SBart Van Assche static ssize_t show_reconnect_delay(struct device *dev, 2108c64e453SBart Van Assche struct device_attribute *attr, char *buf) 2118c64e453SBart Van Assche { 2128c64e453SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 2138c64e453SBart Van Assche 2148c64e453SBart Van Assche return srp_show_tmo(buf, rport->reconnect_delay); 2158c64e453SBart Van Assche } 2168c64e453SBart Van Assche 2178c64e453SBart Van Assche static ssize_t store_reconnect_delay(struct device *dev, 2188c64e453SBart Van Assche struct device_attribute *attr, 2198c64e453SBart Van Assche const char *buf, const size_t count) 2208c64e453SBart Van Assche { 2218c64e453SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 2228c64e453SBart Van Assche int res, delay; 2238c64e453SBart Van Assche 2248c64e453SBart Van Assche res = srp_parse_tmo(&delay, buf); 2258c64e453SBart Van Assche if (res) 2268c64e453SBart Van Assche goto out; 2278c64e453SBart Van Assche res = srp_tmo_valid(delay, rport->fast_io_fail_tmo, 2288c64e453SBart Van Assche rport->dev_loss_tmo); 2298c64e453SBart Van Assche if (res) 2308c64e453SBart Van Assche goto out; 2318c64e453SBart Van Assche 2328c64e453SBart Van Assche if (rport->reconnect_delay <= 0 && delay > 0 && 2338c64e453SBart Van Assche rport->state != SRP_RPORT_RUNNING) { 2348c64e453SBart Van Assche queue_delayed_work(system_long_wq, &rport->reconnect_work, 2358c64e453SBart Van Assche delay * HZ); 2368c64e453SBart Van Assche } else if (delay <= 0) { 2378c64e453SBart Van Assche cancel_delayed_work(&rport->reconnect_work); 2388c64e453SBart Van Assche } 2398c64e453SBart Van Assche rport->reconnect_delay = delay; 2408c64e453SBart Van Assche res = count; 2418c64e453SBart Van Assche 2428c64e453SBart Van Assche out: 2438c64e453SBart Van Assche return res; 2448c64e453SBart Van Assche } 2458c64e453SBart Van Assche 2468c64e453SBart Van Assche static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay, 2478c64e453SBart Van Assche store_reconnect_delay); 2488c64e453SBart Van Assche 2498c64e453SBart Van Assche static ssize_t show_failed_reconnects(struct device *dev, 2508c64e453SBart Van Assche struct device_attribute *attr, char *buf) 2518c64e453SBart Van Assche { 2528c64e453SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 2538c64e453SBart Van Assche 2548c64e453SBart Van Assche return sprintf(buf, "%d\n", rport->failed_reconnects); 2558c64e453SBart Van Assche } 2568c64e453SBart Van Assche 2578c64e453SBart Van Assche static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL); 2588c64e453SBart Van Assche 25929c17324SBart Van Assche static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev, 26029c17324SBart Van Assche struct device_attribute *attr, 26129c17324SBart Van Assche char *buf) 26229c17324SBart Van Assche { 26329c17324SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 26429c17324SBart Van Assche 26529c17324SBart Van Assche return srp_show_tmo(buf, rport->fast_io_fail_tmo); 26629c17324SBart Van Assche } 26729c17324SBart Van Assche 26829c17324SBart Van Assche static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev, 26929c17324SBart Van Assche struct device_attribute *attr, 27029c17324SBart Van Assche const char *buf, size_t count) 27129c17324SBart Van Assche { 27229c17324SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 27329c17324SBart Van Assche int res; 27429c17324SBart Van Assche int fast_io_fail_tmo; 27529c17324SBart Van Assche 27629c17324SBart Van Assche res = srp_parse_tmo(&fast_io_fail_tmo, buf); 27729c17324SBart Van Assche if (res) 27829c17324SBart Van Assche goto out; 2798c64e453SBart Van Assche res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo, 2808c64e453SBart Van Assche rport->dev_loss_tmo); 28129c17324SBart Van Assche if (res) 28229c17324SBart Van Assche goto out; 28329c17324SBart Van Assche rport->fast_io_fail_tmo = fast_io_fail_tmo; 28429c17324SBart Van Assche res = count; 28529c17324SBart Van Assche 28629c17324SBart Van Assche out: 28729c17324SBart Van Assche return res; 28829c17324SBart Van Assche } 28929c17324SBart Van Assche 29029c17324SBart Van Assche static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR, 29129c17324SBart Van Assche show_srp_rport_fast_io_fail_tmo, 29229c17324SBart Van Assche store_srp_rport_fast_io_fail_tmo); 29329c17324SBart Van Assche 29429c17324SBart Van Assche static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev, 29529c17324SBart Van Assche struct device_attribute *attr, 29629c17324SBart Van Assche char *buf) 29729c17324SBart Van Assche { 29829c17324SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 29929c17324SBart Van Assche 30029c17324SBart Van Assche return srp_show_tmo(buf, rport->dev_loss_tmo); 30129c17324SBart Van Assche } 30229c17324SBart Van Assche 30329c17324SBart Van Assche static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev, 30429c17324SBart Van Assche struct device_attribute *attr, 30529c17324SBart Van Assche const char *buf, size_t count) 30629c17324SBart Van Assche { 30729c17324SBart Van Assche struct srp_rport *rport = transport_class_to_srp_rport(dev); 30829c17324SBart Van Assche int res; 30929c17324SBart Van Assche int dev_loss_tmo; 31029c17324SBart Van Assche 31129c17324SBart Van Assche res = srp_parse_tmo(&dev_loss_tmo, buf); 31229c17324SBart Van Assche if (res) 31329c17324SBart Van Assche goto out; 3148c64e453SBart Van Assche res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo, 3158c64e453SBart Van Assche dev_loss_tmo); 31629c17324SBart Van Assche if (res) 31729c17324SBart Van Assche goto out; 31829c17324SBart Van Assche rport->dev_loss_tmo = dev_loss_tmo; 31929c17324SBart Van Assche res = count; 32029c17324SBart Van Assche 32129c17324SBart Van Assche out: 32229c17324SBart Van Assche return res; 32329c17324SBart Van Assche } 32429c17324SBart Van Assche 32529c17324SBart Van Assche static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR, 32629c17324SBart Van Assche show_srp_rport_dev_loss_tmo, 32729c17324SBart Van Assche store_srp_rport_dev_loss_tmo); 32829c17324SBart Van Assche 32929c17324SBart Van Assche static int srp_rport_set_state(struct srp_rport *rport, 33029c17324SBart Van Assche enum srp_rport_state new_state) 33129c17324SBart Van Assche { 33229c17324SBart Van Assche enum srp_rport_state old_state = rport->state; 33329c17324SBart Van Assche 33429c17324SBart Van Assche lockdep_assert_held(&rport->mutex); 33529c17324SBart Van Assche 33629c17324SBart Van Assche switch (new_state) { 33729c17324SBart Van Assche case SRP_RPORT_RUNNING: 33829c17324SBart Van Assche switch (old_state) { 33929c17324SBart Van Assche case SRP_RPORT_LOST: 34029c17324SBart Van Assche goto invalid; 34129c17324SBart Van Assche default: 34229c17324SBart Van Assche break; 34329c17324SBart Van Assche } 34429c17324SBart Van Assche break; 34529c17324SBart Van Assche case SRP_RPORT_BLOCKED: 34629c17324SBart Van Assche switch (old_state) { 34729c17324SBart Van Assche case SRP_RPORT_RUNNING: 34829c17324SBart Van Assche break; 34929c17324SBart Van Assche default: 35029c17324SBart Van Assche goto invalid; 35129c17324SBart Van Assche } 35229c17324SBart Van Assche break; 35329c17324SBart Van Assche case SRP_RPORT_FAIL_FAST: 35429c17324SBart Van Assche switch (old_state) { 35529c17324SBart Van Assche case SRP_RPORT_LOST: 35629c17324SBart Van Assche goto invalid; 35729c17324SBart Van Assche default: 35829c17324SBart Van Assche break; 35929c17324SBart Van Assche } 36029c17324SBart Van Assche break; 36129c17324SBart Van Assche case SRP_RPORT_LOST: 36229c17324SBart Van Assche break; 36329c17324SBart Van Assche } 36429c17324SBart Van Assche rport->state = new_state; 36529c17324SBart Van Assche return 0; 36629c17324SBart Van Assche 36729c17324SBart Van Assche invalid: 36829c17324SBart Van Assche return -EINVAL; 36929c17324SBart Van Assche } 37029c17324SBart Van Assche 3718c64e453SBart Van Assche /** 3728c64e453SBart Van Assche * srp_reconnect_work() - reconnect and schedule a new attempt if necessary 3730c7f8218SBart Van Assche * @work: Work structure used for scheduling this operation. 3748c64e453SBart Van Assche */ 3758c64e453SBart Van Assche static void srp_reconnect_work(struct work_struct *work) 3768c64e453SBart Van Assche { 3778c64e453SBart Van Assche struct srp_rport *rport = container_of(to_delayed_work(work), 3788c64e453SBart Van Assche struct srp_rport, reconnect_work); 3798c64e453SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 3808c64e453SBart Van Assche int delay, res; 3818c64e453SBart Van Assche 3828c64e453SBart Van Assche res = srp_reconnect_rport(rport); 3838c64e453SBart Van Assche if (res != 0) { 3848c64e453SBart Van Assche shost_printk(KERN_ERR, shost, 3858c64e453SBart Van Assche "reconnect attempt %d failed (%d)\n", 3868c64e453SBart Van Assche ++rport->failed_reconnects, res); 3878c64e453SBart Van Assche delay = rport->reconnect_delay * 3888c64e453SBart Van Assche min(100, max(1, rport->failed_reconnects - 10)); 3898c64e453SBart Van Assche if (delay > 0) 3908c64e453SBart Van Assche queue_delayed_work(system_long_wq, 3918c64e453SBart Van Assche &rport->reconnect_work, delay * HZ); 3928c64e453SBart Van Assche } 3938c64e453SBart Van Assche } 3948c64e453SBart Van Assche 39529c17324SBart Van Assche static void __rport_fail_io_fast(struct srp_rport *rport) 39629c17324SBart Van Assche { 39729c17324SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 39829c17324SBart Van Assche struct srp_internal *i; 39929c17324SBart Van Assche 40029c17324SBart Van Assche lockdep_assert_held(&rport->mutex); 40129c17324SBart Van Assche 40229c17324SBart Van Assche if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST)) 40329c17324SBart Van Assche return; 404*669f0441SBart Van Assche /* 405*669f0441SBart Van Assche * Call scsi_target_block() to wait for ongoing shost->queuecommand() 406*669f0441SBart Van Assche * calls before invoking i->f->terminate_rport_io(). 407*669f0441SBart Van Assche */ 408*669f0441SBart Van Assche scsi_target_block(rport->dev.parent); 40929c17324SBart Van Assche scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE); 41029c17324SBart Van Assche 41129c17324SBart Van Assche /* Involve the LLD if possible to terminate all I/O on the rport. */ 41229c17324SBart Van Assche i = to_srp_internal(shost->transportt); 413*669f0441SBart Van Assche if (i->f->terminate_rport_io) 41429c17324SBart Van Assche i->f->terminate_rport_io(rport); 41529c17324SBart Van Assche } 41629c17324SBart Van Assche 41729c17324SBart Van Assche /** 41829c17324SBart Van Assche * rport_fast_io_fail_timedout() - fast I/O failure timeout handler 4190c7f8218SBart Van Assche * @work: Work structure used for scheduling this operation. 42029c17324SBart Van Assche */ 42129c17324SBart Van Assche static void rport_fast_io_fail_timedout(struct work_struct *work) 42229c17324SBart Van Assche { 42329c17324SBart Van Assche struct srp_rport *rport = container_of(to_delayed_work(work), 42429c17324SBart Van Assche struct srp_rport, fast_io_fail_work); 42529c17324SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 42629c17324SBart Van Assche 42729c17324SBart Van Assche pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n", 42829c17324SBart Van Assche dev_name(&rport->dev), dev_name(&shost->shost_gendev)); 42929c17324SBart Van Assche 43029c17324SBart Van Assche mutex_lock(&rport->mutex); 43129c17324SBart Van Assche if (rport->state == SRP_RPORT_BLOCKED) 43229c17324SBart Van Assche __rport_fail_io_fast(rport); 43329c17324SBart Van Assche mutex_unlock(&rport->mutex); 43429c17324SBart Van Assche } 43529c17324SBart Van Assche 43629c17324SBart Van Assche /** 43729c17324SBart Van Assche * rport_dev_loss_timedout() - device loss timeout handler 4380c7f8218SBart Van Assche * @work: Work structure used for scheduling this operation. 43929c17324SBart Van Assche */ 44029c17324SBart Van Assche static void rport_dev_loss_timedout(struct work_struct *work) 44129c17324SBart Van Assche { 44229c17324SBart Van Assche struct srp_rport *rport = container_of(to_delayed_work(work), 44329c17324SBart Van Assche struct srp_rport, dev_loss_work); 44429c17324SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 44529c17324SBart Van Assche struct srp_internal *i = to_srp_internal(shost->transportt); 44629c17324SBart Van Assche 44729c17324SBart Van Assche pr_info("dev_loss_tmo expired for SRP %s / %s.\n", 44829c17324SBart Van Assche dev_name(&rport->dev), dev_name(&shost->shost_gendev)); 44929c17324SBart Van Assche 45029c17324SBart Van Assche mutex_lock(&rport->mutex); 45129c17324SBart Van Assche WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0); 45229c17324SBart Van Assche scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE); 45329c17324SBart Van Assche mutex_unlock(&rport->mutex); 45429c17324SBart Van Assche 45529c17324SBart Van Assche i->f->rport_delete(rport); 45629c17324SBart Van Assche } 45729c17324SBart Van Assche 45829c17324SBart Van Assche static void __srp_start_tl_fail_timers(struct srp_rport *rport) 45929c17324SBart Van Assche { 46029c17324SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 4618c64e453SBart Van Assche int delay, fast_io_fail_tmo, dev_loss_tmo; 46229c17324SBart Van Assche 46329c17324SBart Van Assche lockdep_assert_held(&rport->mutex); 46429c17324SBart Van Assche 4658c64e453SBart Van Assche delay = rport->reconnect_delay; 46629c17324SBart Van Assche fast_io_fail_tmo = rport->fast_io_fail_tmo; 46729c17324SBart Van Assche dev_loss_tmo = rport->dev_loss_tmo; 46893079162SBart Van Assche pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev), 46993079162SBart Van Assche rport->state); 47029c17324SBart Van Assche 47193079162SBart Van Assche if (rport->state == SRP_RPORT_LOST) 47293079162SBart Van Assche return; 4738c64e453SBart Van Assche if (delay > 0) 47493079162SBart Van Assche queue_delayed_work(system_long_wq, &rport->reconnect_work, 4758c64e453SBart Van Assche 1UL * delay * HZ); 476cd53eb68SBart Van Assche if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) && 477cd53eb68SBart Van Assche srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) { 47893079162SBart Van Assche pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev), 47929c17324SBart Van Assche rport->state); 48029c17324SBart Van Assche scsi_target_block(&shost->shost_gendev); 48118cc4e02SBart Van Assche if (fast_io_fail_tmo >= 0) 48229c17324SBart Van Assche queue_delayed_work(system_long_wq, 48329c17324SBart Van Assche &rport->fast_io_fail_work, 48429c17324SBart Van Assche 1UL * fast_io_fail_tmo * HZ); 48529c17324SBart Van Assche if (dev_loss_tmo >= 0) 48629c17324SBart Van Assche queue_delayed_work(system_long_wq, 48729c17324SBart Van Assche &rport->dev_loss_work, 48829c17324SBart Van Assche 1UL * dev_loss_tmo * HZ); 48918cc4e02SBart Van Assche } 49029c17324SBart Van Assche } 49129c17324SBart Van Assche 49229c17324SBart Van Assche /** 49329c17324SBart Van Assche * srp_start_tl_fail_timers() - start the transport layer failure timers 4940c7f8218SBart Van Assche * @rport: SRP target port. 49529c17324SBart Van Assche * 49629c17324SBart Van Assche * Start the transport layer fast I/O failure and device loss timers. Do not 49729c17324SBart Van Assche * modify a timer that was already started. 49829c17324SBart Van Assche */ 49929c17324SBart Van Assche void srp_start_tl_fail_timers(struct srp_rport *rport) 50029c17324SBart Van Assche { 50129c17324SBart Van Assche mutex_lock(&rport->mutex); 50229c17324SBart Van Assche __srp_start_tl_fail_timers(rport); 50329c17324SBart Van Assche mutex_unlock(&rport->mutex); 50429c17324SBart Van Assche } 50529c17324SBart Van Assche EXPORT_SYMBOL(srp_start_tl_fail_timers); 50629c17324SBart Van Assche 50729c17324SBart Van Assche /** 50829c17324SBart Van Assche * srp_reconnect_rport() - reconnect to an SRP target port 5090c7f8218SBart Van Assche * @rport: SRP target port. 51029c17324SBart Van Assche * 51129c17324SBart Van Assche * Blocks SCSI command queueing before invoking reconnect() such that 51229c17324SBart Van Assche * queuecommand() won't be invoked concurrently with reconnect() from outside 51329c17324SBart Van Assche * the SCSI EH. This is important since a reconnect() implementation may 51429c17324SBart Van Assche * reallocate resources needed by queuecommand(). 51529c17324SBart Van Assche * 51629c17324SBart Van Assche * Notes: 51729c17324SBart Van Assche * - This function neither waits until outstanding requests have finished nor 51829c17324SBart Van Assche * tries to abort these. It is the responsibility of the reconnect() 51929c17324SBart Van Assche * function to finish outstanding commands before reconnecting to the target 52029c17324SBart Van Assche * port. 52129c17324SBart Van Assche * - It is the responsibility of the caller to ensure that the resources 52229c17324SBart Van Assche * reallocated by the reconnect() function won't be used while this function 52329c17324SBart Van Assche * is in progress. One possible strategy is to invoke this function from 52429c17324SBart Van Assche * the context of the SCSI EH thread only. Another possible strategy is to 52529c17324SBart Van Assche * lock the rport mutex inside each SCSI LLD callback that can be invoked by 52629c17324SBart Van Assche * the SCSI EH (the scsi_host_template.eh_*() functions and also the 52729c17324SBart Van Assche * scsi_host_template.queuecommand() function). 52829c17324SBart Van Assche */ 52929c17324SBart Van Assche int srp_reconnect_rport(struct srp_rport *rport) 53029c17324SBart Van Assche { 53129c17324SBart Van Assche struct Scsi_Host *shost = rport_to_shost(rport); 53229c17324SBart Van Assche struct srp_internal *i = to_srp_internal(shost->transportt); 53329c17324SBart Van Assche struct scsi_device *sdev; 53429c17324SBart Van Assche int res; 53529c17324SBart Van Assche 53629c17324SBart Van Assche pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev)); 53729c17324SBart Van Assche 53829c17324SBart Van Assche res = mutex_lock_interruptible(&rport->mutex); 53929c17324SBart Van Assche if (res) 54029c17324SBart Van Assche goto out; 54129c17324SBart Van Assche scsi_target_block(&shost->shost_gendev); 54293079162SBart Van Assche res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV; 54329c17324SBart Van Assche pr_debug("%s (state %d): transport.reconnect() returned %d\n", 54429c17324SBart Van Assche dev_name(&shost->shost_gendev), rport->state, res); 54529c17324SBart Van Assche if (res == 0) { 54629c17324SBart Van Assche cancel_delayed_work(&rport->fast_io_fail_work); 54729c17324SBart Van Assche cancel_delayed_work(&rport->dev_loss_work); 54829c17324SBart Van Assche 5498c64e453SBart Van Assche rport->failed_reconnects = 0; 55029c17324SBart Van Assche srp_rport_set_state(rport, SRP_RPORT_RUNNING); 55129c17324SBart Van Assche scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING); 55229c17324SBart Van Assche /* 55329c17324SBart Van Assche * If the SCSI error handler has offlined one or more devices, 55429c17324SBart Van Assche * invoking scsi_target_unblock() won't change the state of 55529c17324SBart Van Assche * these devices into running so do that explicitly. 55629c17324SBart Van Assche */ 55729c17324SBart Van Assche spin_lock_irq(shost->host_lock); 55829c17324SBart Van Assche __shost_for_each_device(sdev, shost) 55929c17324SBart Van Assche if (sdev->sdev_state == SDEV_OFFLINE) 56029c17324SBart Van Assche sdev->sdev_state = SDEV_RUNNING; 56129c17324SBart Van Assche spin_unlock_irq(shost->host_lock); 56229c17324SBart Van Assche } else if (rport->state == SRP_RPORT_RUNNING) { 56329c17324SBart Van Assche /* 56418cc4e02SBart Van Assche * srp_reconnect_rport() has been invoked with fast_io_fail 56518cc4e02SBart Van Assche * and dev_loss off. Mark the port as failed and start the TL 56618cc4e02SBart Van Assche * failure timers if these had not yet been started. 56729c17324SBart Van Assche */ 56829c17324SBart Van Assche __rport_fail_io_fast(rport); 56929c17324SBart Van Assche scsi_target_unblock(&shost->shost_gendev, 57029c17324SBart Van Assche SDEV_TRANSPORT_OFFLINE); 57129c17324SBart Van Assche __srp_start_tl_fail_timers(rport); 57229c17324SBart Van Assche } else if (rport->state != SRP_RPORT_BLOCKED) { 57329c17324SBart Van Assche scsi_target_unblock(&shost->shost_gendev, 57429c17324SBart Van Assche SDEV_TRANSPORT_OFFLINE); 57529c17324SBart Van Assche } 57629c17324SBart Van Assche mutex_unlock(&rport->mutex); 57729c17324SBart Van Assche 57829c17324SBart Van Assche out: 57929c17324SBart Van Assche return res; 58029c17324SBart Van Assche } 58129c17324SBart Van Assche EXPORT_SYMBOL(srp_reconnect_rport); 58229c17324SBart Van Assche 58329c17324SBart Van Assche /** 58429c17324SBart Van Assche * srp_timed_out() - SRP transport intercept of the SCSI timeout EH 5850c7f8218SBart Van Assche * @scmd: SCSI command. 58629c17324SBart Van Assche * 58729c17324SBart Van Assche * If a timeout occurs while an rport is in the blocked state, ask the SCSI 58829c17324SBart Van Assche * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core 58929c17324SBart Van Assche * handle the timeout (BLK_EH_NOT_HANDLED). 59029c17324SBart Van Assche * 59129c17324SBart Van Assche * Note: This function is called from soft-IRQ context and with the request 59229c17324SBart Van Assche * queue lock held. 59329c17324SBart Van Assche */ 59429c17324SBart Van Assche static enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd) 59529c17324SBart Van Assche { 59629c17324SBart Van Assche struct scsi_device *sdev = scmd->device; 59729c17324SBart Van Assche struct Scsi_Host *shost = sdev->host; 59829c17324SBart Van Assche struct srp_internal *i = to_srp_internal(shost->transportt); 599e68ca752SBart Van Assche struct srp_rport *rport = shost_to_rport(shost); 60029c17324SBart Van Assche 60129c17324SBart Van Assche pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev)); 602e68ca752SBart Van Assche return rport->fast_io_fail_tmo < 0 && rport->dev_loss_tmo < 0 && 603e68ca752SBart Van Assche i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ? 60429c17324SBart Van Assche BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED; 60529c17324SBart Van Assche } 60629c17324SBart Van Assche 60709345f65SFUJITA Tomonori static void srp_rport_release(struct device *dev) 60809345f65SFUJITA Tomonori { 60909345f65SFUJITA Tomonori struct srp_rport *rport = dev_to_rport(dev); 61009345f65SFUJITA Tomonori 61109345f65SFUJITA Tomonori put_device(dev->parent); 61209345f65SFUJITA Tomonori kfree(rport); 61309345f65SFUJITA Tomonori } 61409345f65SFUJITA Tomonori 61509345f65SFUJITA Tomonori static int scsi_is_srp_rport(const struct device *dev) 61609345f65SFUJITA Tomonori { 61709345f65SFUJITA Tomonori return dev->release == srp_rport_release; 61809345f65SFUJITA Tomonori } 61909345f65SFUJITA Tomonori 62009345f65SFUJITA Tomonori static int srp_rport_match(struct attribute_container *cont, 62109345f65SFUJITA Tomonori struct device *dev) 62209345f65SFUJITA Tomonori { 62309345f65SFUJITA Tomonori struct Scsi_Host *shost; 62409345f65SFUJITA Tomonori struct srp_internal *i; 62509345f65SFUJITA Tomonori 62609345f65SFUJITA Tomonori if (!scsi_is_srp_rport(dev)) 62709345f65SFUJITA Tomonori return 0; 62809345f65SFUJITA Tomonori 62909345f65SFUJITA Tomonori shost = dev_to_shost(dev->parent); 63009345f65SFUJITA Tomonori if (!shost->transportt) 63109345f65SFUJITA Tomonori return 0; 63209345f65SFUJITA Tomonori if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) 63309345f65SFUJITA Tomonori return 0; 63409345f65SFUJITA Tomonori 63509345f65SFUJITA Tomonori i = to_srp_internal(shost->transportt); 63609345f65SFUJITA Tomonori return &i->rport_attr_cont.ac == cont; 63709345f65SFUJITA Tomonori } 63809345f65SFUJITA Tomonori 63909345f65SFUJITA Tomonori static int srp_host_match(struct attribute_container *cont, struct device *dev) 64009345f65SFUJITA Tomonori { 64109345f65SFUJITA Tomonori struct Scsi_Host *shost; 64209345f65SFUJITA Tomonori struct srp_internal *i; 64309345f65SFUJITA Tomonori 64409345f65SFUJITA Tomonori if (!scsi_is_host_device(dev)) 64509345f65SFUJITA Tomonori return 0; 64609345f65SFUJITA Tomonori 64709345f65SFUJITA Tomonori shost = dev_to_shost(dev); 64809345f65SFUJITA Tomonori if (!shost->transportt) 64909345f65SFUJITA Tomonori return 0; 65009345f65SFUJITA Tomonori if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) 65109345f65SFUJITA Tomonori return 0; 65209345f65SFUJITA Tomonori 65309345f65SFUJITA Tomonori i = to_srp_internal(shost->transportt); 65409345f65SFUJITA Tomonori return &i->t.host_attrs.ac == cont; 65509345f65SFUJITA Tomonori } 65609345f65SFUJITA Tomonori 65709345f65SFUJITA Tomonori /** 6589dd69a60SBart Van Assche * srp_rport_get() - increment rport reference count 6590c7f8218SBart Van Assche * @rport: SRP target port. 6609dd69a60SBart Van Assche */ 6619dd69a60SBart Van Assche void srp_rport_get(struct srp_rport *rport) 6629dd69a60SBart Van Assche { 6639dd69a60SBart Van Assche get_device(&rport->dev); 6649dd69a60SBart Van Assche } 6659dd69a60SBart Van Assche EXPORT_SYMBOL(srp_rport_get); 6669dd69a60SBart Van Assche 6679dd69a60SBart Van Assche /** 6689dd69a60SBart Van Assche * srp_rport_put() - decrement rport reference count 6690c7f8218SBart Van Assche * @rport: SRP target port. 6709dd69a60SBart Van Assche */ 6719dd69a60SBart Van Assche void srp_rport_put(struct srp_rport *rport) 6729dd69a60SBart Van Assche { 6739dd69a60SBart Van Assche put_device(&rport->dev); 6749dd69a60SBart Van Assche } 6759dd69a60SBart Van Assche EXPORT_SYMBOL(srp_rport_put); 6769dd69a60SBart Van Assche 6779dd69a60SBart Van Assche /** 67809345f65SFUJITA Tomonori * srp_rport_add - add a SRP remote port to the device hierarchy 67909345f65SFUJITA Tomonori * @shost: scsi host the remote port is connected to. 68009345f65SFUJITA Tomonori * @ids: The port id for the remote port. 68109345f65SFUJITA Tomonori * 682dc8875e1SRandy Dunlap * Publishes a port to the rest of the system. 68309345f65SFUJITA Tomonori */ 68409345f65SFUJITA Tomonori struct srp_rport *srp_rport_add(struct Scsi_Host *shost, 68509345f65SFUJITA Tomonori struct srp_rport_identifiers *ids) 68609345f65SFUJITA Tomonori { 68709345f65SFUJITA Tomonori struct srp_rport *rport; 68809345f65SFUJITA Tomonori struct device *parent = &shost->shost_gendev; 68929c17324SBart Van Assche struct srp_internal *i = to_srp_internal(shost->transportt); 69009345f65SFUJITA Tomonori int id, ret; 69109345f65SFUJITA Tomonori 69209345f65SFUJITA Tomonori rport = kzalloc(sizeof(*rport), GFP_KERNEL); 69309345f65SFUJITA Tomonori if (!rport) 69409345f65SFUJITA Tomonori return ERR_PTR(-ENOMEM); 69509345f65SFUJITA Tomonori 69629c17324SBart Van Assche mutex_init(&rport->mutex); 69729c17324SBart Van Assche 69809345f65SFUJITA Tomonori device_initialize(&rport->dev); 69909345f65SFUJITA Tomonori 70009345f65SFUJITA Tomonori rport->dev.parent = get_device(parent); 70109345f65SFUJITA Tomonori rport->dev.release = srp_rport_release; 70209345f65SFUJITA Tomonori 70309345f65SFUJITA Tomonori memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id)); 704aebd5e47SFUJITA Tomonori rport->roles = ids->roles; 70509345f65SFUJITA Tomonori 7068c64e453SBart Van Assche if (i->f->reconnect) 7078c64e453SBart Van Assche rport->reconnect_delay = i->f->reconnect_delay ? 7088c64e453SBart Van Assche *i->f->reconnect_delay : 10; 7098c64e453SBart Van Assche INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work); 71029c17324SBart Van Assche rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ? 71129c17324SBart Van Assche *i->f->fast_io_fail_tmo : 15; 71229c17324SBart Van Assche rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60; 71329c17324SBart Van Assche INIT_DELAYED_WORK(&rport->fast_io_fail_work, 71429c17324SBart Van Assche rport_fast_io_fail_timedout); 71529c17324SBart Van Assche INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout); 71629c17324SBart Van Assche 71709345f65SFUJITA Tomonori id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id); 71871610f55SKay Sievers dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id); 71909345f65SFUJITA Tomonori 72009345f65SFUJITA Tomonori transport_setup_device(&rport->dev); 72109345f65SFUJITA Tomonori 72209345f65SFUJITA Tomonori ret = device_add(&rport->dev); 72309345f65SFUJITA Tomonori if (ret) { 72409345f65SFUJITA Tomonori transport_destroy_device(&rport->dev); 72509345f65SFUJITA Tomonori put_device(&rport->dev); 72609345f65SFUJITA Tomonori return ERR_PTR(ret); 72709345f65SFUJITA Tomonori } 72809345f65SFUJITA Tomonori 72909345f65SFUJITA Tomonori transport_add_device(&rport->dev); 73009345f65SFUJITA Tomonori transport_configure_device(&rport->dev); 73109345f65SFUJITA Tomonori 73209345f65SFUJITA Tomonori return rport; 73309345f65SFUJITA Tomonori } 73409345f65SFUJITA Tomonori EXPORT_SYMBOL_GPL(srp_rport_add); 73509345f65SFUJITA Tomonori 73609345f65SFUJITA Tomonori /** 737eb44820cSRob Landley * srp_rport_del - remove a SRP remote port 738eb44820cSRob Landley * @rport: SRP remote port to remove 73909345f65SFUJITA Tomonori * 74009345f65SFUJITA Tomonori * Removes the specified SRP remote port. 74109345f65SFUJITA Tomonori */ 74209345f65SFUJITA Tomonori void srp_rport_del(struct srp_rport *rport) 74309345f65SFUJITA Tomonori { 74409345f65SFUJITA Tomonori struct device *dev = &rport->dev; 74562fe8826SFUJITA Tomonori 74609345f65SFUJITA Tomonori transport_remove_device(dev); 74709345f65SFUJITA Tomonori device_del(dev); 74809345f65SFUJITA Tomonori transport_destroy_device(dev); 74929c17324SBart Van Assche 75009345f65SFUJITA Tomonori put_device(dev); 75109345f65SFUJITA Tomonori } 75209345f65SFUJITA Tomonori EXPORT_SYMBOL_GPL(srp_rport_del); 75309345f65SFUJITA Tomonori 75409345f65SFUJITA Tomonori static int do_srp_rport_del(struct device *dev, void *data) 75509345f65SFUJITA Tomonori { 75691183344SDave Dillow if (scsi_is_srp_rport(dev)) 75709345f65SFUJITA Tomonori srp_rport_del(dev_to_rport(dev)); 75809345f65SFUJITA Tomonori return 0; 75909345f65SFUJITA Tomonori } 76009345f65SFUJITA Tomonori 76109345f65SFUJITA Tomonori /** 762eb44820cSRob Landley * srp_remove_host - tear down a Scsi_Host's SRP data structures 76309345f65SFUJITA Tomonori * @shost: Scsi Host that is torn down 76409345f65SFUJITA Tomonori * 76509345f65SFUJITA Tomonori * Removes all SRP remote ports for a given Scsi_Host. 76609345f65SFUJITA Tomonori * Must be called just before scsi_remove_host for SRP HBAs. 76709345f65SFUJITA Tomonori */ 76809345f65SFUJITA Tomonori void srp_remove_host(struct Scsi_Host *shost) 76909345f65SFUJITA Tomonori { 77009345f65SFUJITA Tomonori device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del); 77109345f65SFUJITA Tomonori } 77209345f65SFUJITA Tomonori EXPORT_SYMBOL_GPL(srp_remove_host); 77309345f65SFUJITA Tomonori 77493079162SBart Van Assche /** 77593079162SBart Van Assche * srp_stop_rport_timers - stop the transport layer recovery timers 77667513605SBart Van Assche * @rport: SRP remote port for which to stop the timers. 77793079162SBart Van Assche * 77893079162SBart Van Assche * Must be called after srp_remove_host() and scsi_remove_host(). The caller 77993079162SBart Van Assche * must hold a reference on the rport (rport->dev) and on the SCSI host 78093079162SBart Van Assche * (rport->dev.parent). 78193079162SBart Van Assche */ 78293079162SBart Van Assche void srp_stop_rport_timers(struct srp_rport *rport) 78393079162SBart Van Assche { 78493079162SBart Van Assche mutex_lock(&rport->mutex); 78593079162SBart Van Assche if (rport->state == SRP_RPORT_BLOCKED) 78693079162SBart Van Assche __rport_fail_io_fast(rport); 78793079162SBart Van Assche srp_rport_set_state(rport, SRP_RPORT_LOST); 78893079162SBart Van Assche mutex_unlock(&rport->mutex); 78993079162SBart Van Assche 79093079162SBart Van Assche cancel_delayed_work_sync(&rport->reconnect_work); 79193079162SBart Van Assche cancel_delayed_work_sync(&rport->fast_io_fail_work); 79293079162SBart Van Assche cancel_delayed_work_sync(&rport->dev_loss_work); 79393079162SBart Van Assche } 79493079162SBart Van Assche EXPORT_SYMBOL_GPL(srp_stop_rport_timers); 79593079162SBart Van Assche 796bfb74374SFUJITA Tomonori static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id, 797bfb74374SFUJITA Tomonori int result) 79862fe8826SFUJITA Tomonori { 79962fe8826SFUJITA Tomonori struct srp_internal *i = to_srp_internal(shost->transportt); 800bfb74374SFUJITA Tomonori return i->f->tsk_mgmt_response(shost, nexus, tm_id, result); 801bfb74374SFUJITA Tomonori } 802bfb74374SFUJITA Tomonori 803bfb74374SFUJITA Tomonori static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result) 804bfb74374SFUJITA Tomonori { 805bfb74374SFUJITA Tomonori struct srp_internal *i = to_srp_internal(shost->transportt); 806bfb74374SFUJITA Tomonori return i->f->it_nexus_response(shost, nexus, result); 80762fe8826SFUJITA Tomonori } 80862fe8826SFUJITA Tomonori 80909345f65SFUJITA Tomonori /** 810eb44820cSRob Landley * srp_attach_transport - instantiate SRP transport template 81109345f65SFUJITA Tomonori * @ft: SRP transport class function template 81209345f65SFUJITA Tomonori */ 81309345f65SFUJITA Tomonori struct scsi_transport_template * 81409345f65SFUJITA Tomonori srp_attach_transport(struct srp_function_template *ft) 81509345f65SFUJITA Tomonori { 81609345f65SFUJITA Tomonori int count; 81709345f65SFUJITA Tomonori struct srp_internal *i; 81809345f65SFUJITA Tomonori 81909345f65SFUJITA Tomonori i = kzalloc(sizeof(*i), GFP_KERNEL); 82009345f65SFUJITA Tomonori if (!i) 82109345f65SFUJITA Tomonori return NULL; 82209345f65SFUJITA Tomonori 82329c17324SBart Van Assche i->t.eh_timed_out = srp_timed_out; 82429c17324SBart Van Assche 825bfb74374SFUJITA Tomonori i->t.tsk_mgmt_response = srp_tsk_mgmt_response; 82662fe8826SFUJITA Tomonori i->t.it_nexus_response = srp_it_nexus_response; 82762fe8826SFUJITA Tomonori 82809345f65SFUJITA Tomonori i->t.host_size = sizeof(struct srp_host_attrs); 82909345f65SFUJITA Tomonori i->t.host_attrs.ac.attrs = &i->host_attrs[0]; 83009345f65SFUJITA Tomonori i->t.host_attrs.ac.class = &srp_host_class.class; 83109345f65SFUJITA Tomonori i->t.host_attrs.ac.match = srp_host_match; 83209345f65SFUJITA Tomonori i->host_attrs[0] = NULL; 83309345f65SFUJITA Tomonori transport_container_register(&i->t.host_attrs); 83409345f65SFUJITA Tomonori 83509345f65SFUJITA Tomonori i->rport_attr_cont.ac.attrs = &i->rport_attrs[0]; 83609345f65SFUJITA Tomonori i->rport_attr_cont.ac.class = &srp_rport_class.class; 83709345f65SFUJITA Tomonori i->rport_attr_cont.ac.match = srp_rport_match; 83809345f65SFUJITA Tomonori 83909345f65SFUJITA Tomonori count = 0; 8409134a855SBart Van Assche i->rport_attrs[count++] = &dev_attr_port_id; 8419134a855SBart Van Assche i->rport_attrs[count++] = &dev_attr_roles; 84229c17324SBart Van Assche if (ft->has_rport_state) { 84329c17324SBart Van Assche i->rport_attrs[count++] = &dev_attr_state; 84429c17324SBart Van Assche i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo; 84529c17324SBart Van Assche i->rport_attrs[count++] = &dev_attr_dev_loss_tmo; 84629c17324SBart Van Assche } 8478c64e453SBart Van Assche if (ft->reconnect) { 8488c64e453SBart Van Assche i->rport_attrs[count++] = &dev_attr_reconnect_delay; 8498c64e453SBart Van Assche i->rport_attrs[count++] = &dev_attr_failed_reconnects; 8508c64e453SBart Van Assche } 851dc1bdbd9SBart Van Assche if (ft->rport_delete) 852dc1bdbd9SBart Van Assche i->rport_attrs[count++] = &dev_attr_delete; 8539134a855SBart Van Assche i->rport_attrs[count++] = NULL; 8549134a855SBart Van Assche BUG_ON(count > ARRAY_SIZE(i->rport_attrs)); 85509345f65SFUJITA Tomonori 856ac9be30eSBart Van Assche transport_container_register(&i->rport_attr_cont); 857ac9be30eSBart Van Assche 85809345f65SFUJITA Tomonori i->f = ft; 85909345f65SFUJITA Tomonori 86009345f65SFUJITA Tomonori return &i->t; 86109345f65SFUJITA Tomonori } 86209345f65SFUJITA Tomonori EXPORT_SYMBOL_GPL(srp_attach_transport); 86309345f65SFUJITA Tomonori 86409345f65SFUJITA Tomonori /** 865eb44820cSRob Landley * srp_release_transport - release SRP transport template instance 86609345f65SFUJITA Tomonori * @t: transport template instance 86709345f65SFUJITA Tomonori */ 86809345f65SFUJITA Tomonori void srp_release_transport(struct scsi_transport_template *t) 86909345f65SFUJITA Tomonori { 87009345f65SFUJITA Tomonori struct srp_internal *i = to_srp_internal(t); 87109345f65SFUJITA Tomonori 87209345f65SFUJITA Tomonori transport_container_unregister(&i->t.host_attrs); 87309345f65SFUJITA Tomonori transport_container_unregister(&i->rport_attr_cont); 87409345f65SFUJITA Tomonori 87509345f65SFUJITA Tomonori kfree(i); 87609345f65SFUJITA Tomonori } 87709345f65SFUJITA Tomonori EXPORT_SYMBOL_GPL(srp_release_transport); 87809345f65SFUJITA Tomonori 87909345f65SFUJITA Tomonori static __init int srp_transport_init(void) 88009345f65SFUJITA Tomonori { 88109345f65SFUJITA Tomonori int ret; 88209345f65SFUJITA Tomonori 88309345f65SFUJITA Tomonori ret = transport_class_register(&srp_host_class); 88409345f65SFUJITA Tomonori if (ret) 88509345f65SFUJITA Tomonori return ret; 88609345f65SFUJITA Tomonori ret = transport_class_register(&srp_rport_class); 88709345f65SFUJITA Tomonori if (ret) 88809345f65SFUJITA Tomonori goto unregister_host_class; 88909345f65SFUJITA Tomonori 89009345f65SFUJITA Tomonori return 0; 89109345f65SFUJITA Tomonori unregister_host_class: 89209345f65SFUJITA Tomonori transport_class_unregister(&srp_host_class); 89309345f65SFUJITA Tomonori return ret; 89409345f65SFUJITA Tomonori } 89509345f65SFUJITA Tomonori 89609345f65SFUJITA Tomonori static void __exit srp_transport_exit(void) 89709345f65SFUJITA Tomonori { 89809345f65SFUJITA Tomonori transport_class_unregister(&srp_host_class); 89909345f65SFUJITA Tomonori transport_class_unregister(&srp_rport_class); 90009345f65SFUJITA Tomonori } 90109345f65SFUJITA Tomonori 90209345f65SFUJITA Tomonori MODULE_AUTHOR("FUJITA Tomonori"); 90309345f65SFUJITA Tomonori MODULE_DESCRIPTION("SRP Transport Attributes"); 90409345f65SFUJITA Tomonori MODULE_LICENSE("GPL"); 90509345f65SFUJITA Tomonori 90609345f65SFUJITA Tomonori module_init(srp_transport_init); 90709345f65SFUJITA Tomonori module_exit(srp_transport_exit); 908