FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SCSI RDMA (SRP) transport class |
| 3 | * |
| 4 | * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation, version 2 of the |
| 9 | * License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 | * 02110-1301 USA |
| 20 | */ |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/jiffies.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/string.h> |
| 27 | |
| 28 | #include <scsi/scsi.h> |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 29 | #include <scsi/scsi_cmnd.h> |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 30 | #include <scsi/scsi_device.h> |
| 31 | #include <scsi/scsi_host.h> |
| 32 | #include <scsi/scsi_transport.h> |
| 33 | #include <scsi/scsi_transport_srp.h> |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 34 | #include "scsi_priv.h" |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 35 | |
| 36 | struct srp_host_attrs { |
| 37 | atomic_t next_port_id; |
| 38 | }; |
| 39 | #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data) |
| 40 | |
| 41 | #define SRP_HOST_ATTRS 0 |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 42 | #define SRP_RPORT_ATTRS 8 |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 43 | |
| 44 | struct srp_internal { |
| 45 | struct scsi_transport_template t; |
| 46 | struct srp_function_template *f; |
| 47 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 48 | struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1]; |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 49 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 50 | struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1]; |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 51 | struct transport_container rport_attr_cont; |
| 52 | }; |
| 53 | |
Bart Van Assche | c9ddf73 | 2018-05-21 11:17:29 -0700 | [diff] [blame] | 54 | static int scsi_is_srp_rport(const struct device *dev); |
| 55 | |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 56 | #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t) |
| 57 | |
| 58 | #define dev_to_rport(d) container_of(d, struct srp_rport, dev) |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 59 | #define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent) |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 60 | static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r) |
| 61 | { |
| 62 | return dev_to_shost(r->dev.parent); |
| 63 | } |
| 64 | |
Bart Van Assche | c9ddf73 | 2018-05-21 11:17:29 -0700 | [diff] [blame] | 65 | static int find_child_rport(struct device *dev, void *data) |
| 66 | { |
| 67 | struct device **child = data; |
| 68 | |
| 69 | if (scsi_is_srp_rport(dev)) { |
| 70 | WARN_ON_ONCE(*child); |
| 71 | *child = dev; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
Bart Van Assche | e68ca75 | 2015-05-18 13:24:38 +0200 | [diff] [blame] | 76 | static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost) |
| 77 | { |
Bart Van Assche | c9ddf73 | 2018-05-21 11:17:29 -0700 | [diff] [blame] | 78 | struct device *child = NULL; |
| 79 | |
| 80 | WARN_ON_ONCE(device_for_each_child(&shost->shost_gendev, &child, |
| 81 | find_child_rport) < 0); |
| 82 | return child ? dev_to_rport(child) : NULL; |
Bart Van Assche | e68ca75 | 2015-05-18 13:24:38 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 85 | /** |
| 86 | * srp_tmo_valid() - check timeout combination validity |
Bart Van Assche | 0c7f821 | 2014-01-13 12:39:35 +0100 | [diff] [blame] | 87 | * @reconnect_delay: Reconnect delay in seconds. |
| 88 | * @fast_io_fail_tmo: Fast I/O fail timeout in seconds. |
| 89 | * @dev_loss_tmo: Device loss timeout in seconds. |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 90 | * |
| 91 | * The combination of the timeout parameters must be such that SCSI commands |
| 92 | * are finished in a reasonable time. Hence do not allow the fast I/O fail |
Bart Van Assche | 18cc4e0 | 2013-12-11 17:05:22 +0100 | [diff] [blame] | 93 | * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to |
| 94 | * exceed that limit if failing I/O fast has been disabled. Furthermore, these |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 95 | * parameters must be such that multipath can detect failed paths timely. |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 96 | * Hence do not allow all three parameters to be disabled simultaneously. |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 97 | */ |
Bart Van Assche | 85aa93b | 2017-08-25 13:46:41 -0700 | [diff] [blame] | 98 | int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, long dev_loss_tmo) |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 99 | { |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 100 | if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0) |
| 101 | return -EINVAL; |
| 102 | if (reconnect_delay == 0) |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 103 | return -EINVAL; |
| 104 | if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT) |
| 105 | return -EINVAL; |
Bart Van Assche | 18cc4e0 | 2013-12-11 17:05:22 +0100 | [diff] [blame] | 106 | if (fast_io_fail_tmo < 0 && |
| 107 | dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT) |
| 108 | return -EINVAL; |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 109 | if (dev_loss_tmo >= LONG_MAX / HZ) |
| 110 | return -EINVAL; |
| 111 | if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 && |
| 112 | fast_io_fail_tmo >= dev_loss_tmo) |
| 113 | return -EINVAL; |
| 114 | return 0; |
| 115 | } |
| 116 | EXPORT_SYMBOL_GPL(srp_tmo_valid); |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 117 | |
| 118 | static int srp_host_setup(struct transport_container *tc, struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 119 | struct device *cdev) |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 120 | { |
| 121 | struct Scsi_Host *shost = dev_to_shost(dev); |
| 122 | struct srp_host_attrs *srp_host = to_srp_host_attrs(shost); |
| 123 | |
| 124 | atomic_set(&srp_host->next_port_id, 0); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup, |
| 129 | NULL, NULL); |
| 130 | |
| 131 | static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports", |
| 132 | NULL, NULL, NULL); |
| 133 | |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 134 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 135 | show_srp_rport_id(struct device *dev, struct device_attribute *attr, |
| 136 | char *buf) |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 137 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 138 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
Andy Shevchenko | df441cc | 2016-10-22 20:32:30 +0300 | [diff] [blame] | 139 | return sprintf(buf, "%16phC\n", rport->port_id); |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 140 | } |
| 141 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 142 | static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL); |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 143 | |
FUJITA Tomonori | aebd5e4 | 2007-07-11 15:08:15 +0900 | [diff] [blame] | 144 | static const struct { |
| 145 | u32 value; |
| 146 | char *name; |
| 147 | } srp_rport_role_names[] = { |
| 148 | {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"}, |
| 149 | {SRP_RPORT_ROLE_TARGET, "SRP Target"}, |
| 150 | }; |
| 151 | |
| 152 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 153 | show_srp_rport_roles(struct device *dev, struct device_attribute *attr, |
| 154 | char *buf) |
FUJITA Tomonori | aebd5e4 | 2007-07-11 15:08:15 +0900 | [diff] [blame] | 155 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 156 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
FUJITA Tomonori | aebd5e4 | 2007-07-11 15:08:15 +0900 | [diff] [blame] | 157 | int i; |
| 158 | char *name = NULL; |
| 159 | |
| 160 | for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++) |
| 161 | if (srp_rport_role_names[i].value == rport->roles) { |
| 162 | name = srp_rport_role_names[i].name; |
| 163 | break; |
| 164 | } |
| 165 | return sprintf(buf, "%s\n", name ? : "unknown"); |
| 166 | } |
| 167 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 168 | static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL); |
FUJITA Tomonori | aebd5e4 | 2007-07-11 15:08:15 +0900 | [diff] [blame] | 169 | |
Bart Van Assche | dc1bdbd | 2011-09-16 20:41:13 +0200 | [diff] [blame] | 170 | static ssize_t store_srp_rport_delete(struct device *dev, |
| 171 | struct device_attribute *attr, |
| 172 | const char *buf, size_t count) |
| 173 | { |
| 174 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
| 175 | struct Scsi_Host *shost = dev_to_shost(dev); |
| 176 | struct srp_internal *i = to_srp_internal(shost->transportt); |
| 177 | |
| 178 | if (i->f->rport_delete) { |
| 179 | i->f->rport_delete(rport); |
| 180 | return count; |
| 181 | } else { |
| 182 | return -ENOSYS; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete); |
| 187 | |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 188 | static ssize_t show_srp_rport_state(struct device *dev, |
| 189 | struct device_attribute *attr, |
| 190 | char *buf) |
| 191 | { |
| 192 | static const char *const state_name[] = { |
| 193 | [SRP_RPORT_RUNNING] = "running", |
| 194 | [SRP_RPORT_BLOCKED] = "blocked", |
| 195 | [SRP_RPORT_FAIL_FAST] = "fail-fast", |
| 196 | [SRP_RPORT_LOST] = "lost", |
| 197 | }; |
| 198 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
| 199 | enum srp_rport_state state = rport->state; |
| 200 | |
| 201 | return sprintf(buf, "%s\n", |
| 202 | (unsigned)state < ARRAY_SIZE(state_name) ? |
| 203 | state_name[state] : "???"); |
| 204 | } |
| 205 | |
| 206 | static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL); |
| 207 | |
| 208 | static ssize_t srp_show_tmo(char *buf, int tmo) |
| 209 | { |
| 210 | return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n"); |
| 211 | } |
| 212 | |
Sagi Grimberg | 3fdf70a | 2015-06-25 13:34:15 +0300 | [diff] [blame] | 213 | int srp_parse_tmo(int *tmo, const char *buf) |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 214 | { |
| 215 | int res = 0; |
| 216 | |
| 217 | if (strncmp(buf, "off", 3) != 0) |
| 218 | res = kstrtoint(buf, 0, tmo); |
| 219 | else |
| 220 | *tmo = -1; |
| 221 | |
| 222 | return res; |
| 223 | } |
Sagi Grimberg | 3fdf70a | 2015-06-25 13:34:15 +0300 | [diff] [blame] | 224 | EXPORT_SYMBOL(srp_parse_tmo); |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 225 | |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 226 | static ssize_t show_reconnect_delay(struct device *dev, |
| 227 | struct device_attribute *attr, char *buf) |
| 228 | { |
| 229 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
| 230 | |
| 231 | return srp_show_tmo(buf, rport->reconnect_delay); |
| 232 | } |
| 233 | |
| 234 | static ssize_t store_reconnect_delay(struct device *dev, |
| 235 | struct device_attribute *attr, |
| 236 | const char *buf, const size_t count) |
| 237 | { |
| 238 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
| 239 | int res, delay; |
| 240 | |
| 241 | res = srp_parse_tmo(&delay, buf); |
| 242 | if (res) |
| 243 | goto out; |
| 244 | res = srp_tmo_valid(delay, rport->fast_io_fail_tmo, |
| 245 | rport->dev_loss_tmo); |
| 246 | if (res) |
| 247 | goto out; |
| 248 | |
| 249 | if (rport->reconnect_delay <= 0 && delay > 0 && |
| 250 | rport->state != SRP_RPORT_RUNNING) { |
| 251 | queue_delayed_work(system_long_wq, &rport->reconnect_work, |
| 252 | delay * HZ); |
| 253 | } else if (delay <= 0) { |
| 254 | cancel_delayed_work(&rport->reconnect_work); |
| 255 | } |
| 256 | rport->reconnect_delay = delay; |
| 257 | res = count; |
| 258 | |
| 259 | out: |
| 260 | return res; |
| 261 | } |
| 262 | |
| 263 | static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay, |
| 264 | store_reconnect_delay); |
| 265 | |
| 266 | static ssize_t show_failed_reconnects(struct device *dev, |
| 267 | struct device_attribute *attr, char *buf) |
| 268 | { |
| 269 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
| 270 | |
| 271 | return sprintf(buf, "%d\n", rport->failed_reconnects); |
| 272 | } |
| 273 | |
| 274 | static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL); |
| 275 | |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 276 | static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev, |
| 277 | struct device_attribute *attr, |
| 278 | char *buf) |
| 279 | { |
| 280 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
| 281 | |
| 282 | return srp_show_tmo(buf, rport->fast_io_fail_tmo); |
| 283 | } |
| 284 | |
| 285 | static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev, |
| 286 | struct device_attribute *attr, |
| 287 | const char *buf, size_t count) |
| 288 | { |
| 289 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
| 290 | int res; |
| 291 | int fast_io_fail_tmo; |
| 292 | |
| 293 | res = srp_parse_tmo(&fast_io_fail_tmo, buf); |
| 294 | if (res) |
| 295 | goto out; |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 296 | res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo, |
| 297 | rport->dev_loss_tmo); |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 298 | if (res) |
| 299 | goto out; |
| 300 | rport->fast_io_fail_tmo = fast_io_fail_tmo; |
| 301 | res = count; |
| 302 | |
| 303 | out: |
| 304 | return res; |
| 305 | } |
| 306 | |
| 307 | static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR, |
| 308 | show_srp_rport_fast_io_fail_tmo, |
| 309 | store_srp_rport_fast_io_fail_tmo); |
| 310 | |
| 311 | static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev, |
| 312 | struct device_attribute *attr, |
| 313 | char *buf) |
| 314 | { |
| 315 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
| 316 | |
| 317 | return srp_show_tmo(buf, rport->dev_loss_tmo); |
| 318 | } |
| 319 | |
| 320 | static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev, |
| 321 | struct device_attribute *attr, |
| 322 | const char *buf, size_t count) |
| 323 | { |
| 324 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
| 325 | int res; |
| 326 | int dev_loss_tmo; |
| 327 | |
| 328 | res = srp_parse_tmo(&dev_loss_tmo, buf); |
| 329 | if (res) |
| 330 | goto out; |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 331 | res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo, |
| 332 | dev_loss_tmo); |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 333 | if (res) |
| 334 | goto out; |
| 335 | rport->dev_loss_tmo = dev_loss_tmo; |
| 336 | res = count; |
| 337 | |
| 338 | out: |
| 339 | return res; |
| 340 | } |
| 341 | |
| 342 | static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR, |
| 343 | show_srp_rport_dev_loss_tmo, |
| 344 | store_srp_rport_dev_loss_tmo); |
| 345 | |
| 346 | static int srp_rport_set_state(struct srp_rport *rport, |
| 347 | enum srp_rport_state new_state) |
| 348 | { |
| 349 | enum srp_rport_state old_state = rport->state; |
| 350 | |
| 351 | lockdep_assert_held(&rport->mutex); |
| 352 | |
| 353 | switch (new_state) { |
| 354 | case SRP_RPORT_RUNNING: |
| 355 | switch (old_state) { |
| 356 | case SRP_RPORT_LOST: |
| 357 | goto invalid; |
| 358 | default: |
| 359 | break; |
| 360 | } |
| 361 | break; |
| 362 | case SRP_RPORT_BLOCKED: |
| 363 | switch (old_state) { |
| 364 | case SRP_RPORT_RUNNING: |
| 365 | break; |
| 366 | default: |
| 367 | goto invalid; |
| 368 | } |
| 369 | break; |
| 370 | case SRP_RPORT_FAIL_FAST: |
| 371 | switch (old_state) { |
| 372 | case SRP_RPORT_LOST: |
| 373 | goto invalid; |
| 374 | default: |
| 375 | break; |
| 376 | } |
| 377 | break; |
| 378 | case SRP_RPORT_LOST: |
| 379 | break; |
| 380 | } |
| 381 | rport->state = new_state; |
| 382 | return 0; |
| 383 | |
| 384 | invalid: |
| 385 | return -EINVAL; |
| 386 | } |
| 387 | |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 388 | /** |
| 389 | * srp_reconnect_work() - reconnect and schedule a new attempt if necessary |
Bart Van Assche | 0c7f821 | 2014-01-13 12:39:35 +0100 | [diff] [blame] | 390 | * @work: Work structure used for scheduling this operation. |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 391 | */ |
| 392 | static void srp_reconnect_work(struct work_struct *work) |
| 393 | { |
| 394 | struct srp_rport *rport = container_of(to_delayed_work(work), |
| 395 | struct srp_rport, reconnect_work); |
| 396 | struct Scsi_Host *shost = rport_to_shost(rport); |
| 397 | int delay, res; |
| 398 | |
| 399 | res = srp_reconnect_rport(rport); |
| 400 | if (res != 0) { |
| 401 | shost_printk(KERN_ERR, shost, |
| 402 | "reconnect attempt %d failed (%d)\n", |
| 403 | ++rport->failed_reconnects, res); |
| 404 | delay = rport->reconnect_delay * |
| 405 | min(100, max(1, rport->failed_reconnects - 10)); |
| 406 | if (delay > 0) |
| 407 | queue_delayed_work(system_long_wq, |
| 408 | &rport->reconnect_work, delay * HZ); |
| 409 | } |
| 410 | } |
| 411 | |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 412 | static void __rport_fail_io_fast(struct srp_rport *rport) |
| 413 | { |
| 414 | struct Scsi_Host *shost = rport_to_shost(rport); |
| 415 | struct srp_internal *i; |
| 416 | |
| 417 | lockdep_assert_held(&rport->mutex); |
| 418 | |
| 419 | if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST)) |
| 420 | return; |
Bart Van Assche | 669f044 | 2016-11-22 16:17:13 -0800 | [diff] [blame] | 421 | /* |
| 422 | * Call scsi_target_block() to wait for ongoing shost->queuecommand() |
| 423 | * calls before invoking i->f->terminate_rport_io(). |
| 424 | */ |
| 425 | scsi_target_block(rport->dev.parent); |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 426 | scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE); |
| 427 | |
| 428 | /* Involve the LLD if possible to terminate all I/O on the rport. */ |
| 429 | i = to_srp_internal(shost->transportt); |
Bart Van Assche | 669f044 | 2016-11-22 16:17:13 -0800 | [diff] [blame] | 430 | if (i->f->terminate_rport_io) |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 431 | i->f->terminate_rport_io(rport); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * rport_fast_io_fail_timedout() - fast I/O failure timeout handler |
Bart Van Assche | 0c7f821 | 2014-01-13 12:39:35 +0100 | [diff] [blame] | 436 | * @work: Work structure used for scheduling this operation. |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 437 | */ |
| 438 | static void rport_fast_io_fail_timedout(struct work_struct *work) |
| 439 | { |
| 440 | struct srp_rport *rport = container_of(to_delayed_work(work), |
| 441 | struct srp_rport, fast_io_fail_work); |
| 442 | struct Scsi_Host *shost = rport_to_shost(rport); |
| 443 | |
| 444 | pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n", |
| 445 | dev_name(&rport->dev), dev_name(&shost->shost_gendev)); |
| 446 | |
| 447 | mutex_lock(&rport->mutex); |
| 448 | if (rport->state == SRP_RPORT_BLOCKED) |
| 449 | __rport_fail_io_fast(rport); |
| 450 | mutex_unlock(&rport->mutex); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * rport_dev_loss_timedout() - device loss timeout handler |
Bart Van Assche | 0c7f821 | 2014-01-13 12:39:35 +0100 | [diff] [blame] | 455 | * @work: Work structure used for scheduling this operation. |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 456 | */ |
| 457 | static void rport_dev_loss_timedout(struct work_struct *work) |
| 458 | { |
| 459 | struct srp_rport *rport = container_of(to_delayed_work(work), |
| 460 | struct srp_rport, dev_loss_work); |
| 461 | struct Scsi_Host *shost = rport_to_shost(rport); |
| 462 | struct srp_internal *i = to_srp_internal(shost->transportt); |
| 463 | |
| 464 | pr_info("dev_loss_tmo expired for SRP %s / %s.\n", |
| 465 | dev_name(&rport->dev), dev_name(&shost->shost_gendev)); |
| 466 | |
| 467 | mutex_lock(&rport->mutex); |
| 468 | WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0); |
| 469 | scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE); |
| 470 | mutex_unlock(&rport->mutex); |
| 471 | |
| 472 | i->f->rport_delete(rport); |
| 473 | } |
| 474 | |
| 475 | static void __srp_start_tl_fail_timers(struct srp_rport *rport) |
| 476 | { |
| 477 | struct Scsi_Host *shost = rport_to_shost(rport); |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 478 | int delay, fast_io_fail_tmo, dev_loss_tmo; |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 479 | |
| 480 | lockdep_assert_held(&rport->mutex); |
| 481 | |
Bart Van Assche | 9307916 | 2013-12-11 17:06:14 +0100 | [diff] [blame] | 482 | delay = rport->reconnect_delay; |
| 483 | fast_io_fail_tmo = rport->fast_io_fail_tmo; |
| 484 | dev_loss_tmo = rport->dev_loss_tmo; |
| 485 | pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev), |
| 486 | rport->state); |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 487 | |
Bart Van Assche | 9307916 | 2013-12-11 17:06:14 +0100 | [diff] [blame] | 488 | if (rport->state == SRP_RPORT_LOST) |
| 489 | return; |
| 490 | if (delay > 0) |
| 491 | queue_delayed_work(system_long_wq, &rport->reconnect_work, |
| 492 | 1UL * delay * HZ); |
Bart Van Assche | cd53eb6 | 2014-07-09 15:56:43 +0200 | [diff] [blame] | 493 | if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) && |
| 494 | srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) { |
Bart Van Assche | 9307916 | 2013-12-11 17:06:14 +0100 | [diff] [blame] | 495 | pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev), |
| 496 | rport->state); |
| 497 | scsi_target_block(&shost->shost_gendev); |
| 498 | if (fast_io_fail_tmo >= 0) |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 499 | queue_delayed_work(system_long_wq, |
Bart Van Assche | 9307916 | 2013-12-11 17:06:14 +0100 | [diff] [blame] | 500 | &rport->fast_io_fail_work, |
| 501 | 1UL * fast_io_fail_tmo * HZ); |
| 502 | if (dev_loss_tmo >= 0) |
| 503 | queue_delayed_work(system_long_wq, |
| 504 | &rport->dev_loss_work, |
| 505 | 1UL * dev_loss_tmo * HZ); |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * srp_start_tl_fail_timers() - start the transport layer failure timers |
Bart Van Assche | 0c7f821 | 2014-01-13 12:39:35 +0100 | [diff] [blame] | 511 | * @rport: SRP target port. |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 512 | * |
| 513 | * Start the transport layer fast I/O failure and device loss timers. Do not |
| 514 | * modify a timer that was already started. |
| 515 | */ |
| 516 | void srp_start_tl_fail_timers(struct srp_rport *rport) |
| 517 | { |
| 518 | mutex_lock(&rport->mutex); |
| 519 | __srp_start_tl_fail_timers(rport); |
| 520 | mutex_unlock(&rport->mutex); |
| 521 | } |
| 522 | EXPORT_SYMBOL(srp_start_tl_fail_timers); |
| 523 | |
| 524 | /** |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 525 | * srp_reconnect_rport() - reconnect to an SRP target port |
Bart Van Assche | 0c7f821 | 2014-01-13 12:39:35 +0100 | [diff] [blame] | 526 | * @rport: SRP target port. |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 527 | * |
| 528 | * Blocks SCSI command queueing before invoking reconnect() such that |
| 529 | * queuecommand() won't be invoked concurrently with reconnect() from outside |
| 530 | * the SCSI EH. This is important since a reconnect() implementation may |
| 531 | * reallocate resources needed by queuecommand(). |
| 532 | * |
| 533 | * Notes: |
| 534 | * - This function neither waits until outstanding requests have finished nor |
| 535 | * tries to abort these. It is the responsibility of the reconnect() |
| 536 | * function to finish outstanding commands before reconnecting to the target |
| 537 | * port. |
| 538 | * - It is the responsibility of the caller to ensure that the resources |
| 539 | * reallocated by the reconnect() function won't be used while this function |
| 540 | * is in progress. One possible strategy is to invoke this function from |
| 541 | * the context of the SCSI EH thread only. Another possible strategy is to |
| 542 | * lock the rport mutex inside each SCSI LLD callback that can be invoked by |
| 543 | * the SCSI EH (the scsi_host_template.eh_*() functions and also the |
| 544 | * scsi_host_template.queuecommand() function). |
| 545 | */ |
| 546 | int srp_reconnect_rport(struct srp_rport *rport) |
| 547 | { |
| 548 | struct Scsi_Host *shost = rport_to_shost(rport); |
| 549 | struct srp_internal *i = to_srp_internal(shost->transportt); |
| 550 | struct scsi_device *sdev; |
| 551 | int res; |
| 552 | |
| 553 | pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev)); |
| 554 | |
| 555 | res = mutex_lock_interruptible(&rport->mutex); |
| 556 | if (res) |
| 557 | goto out; |
| 558 | scsi_target_block(&shost->shost_gendev); |
Bart Van Assche | 9307916 | 2013-12-11 17:06:14 +0100 | [diff] [blame] | 559 | res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV; |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 560 | pr_debug("%s (state %d): transport.reconnect() returned %d\n", |
| 561 | dev_name(&shost->shost_gendev), rport->state, res); |
| 562 | if (res == 0) { |
| 563 | cancel_delayed_work(&rport->fast_io_fail_work); |
| 564 | cancel_delayed_work(&rport->dev_loss_work); |
| 565 | |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 566 | rport->failed_reconnects = 0; |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 567 | srp_rport_set_state(rport, SRP_RPORT_RUNNING); |
| 568 | scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING); |
| 569 | /* |
| 570 | * If the SCSI error handler has offlined one or more devices, |
| 571 | * invoking scsi_target_unblock() won't change the state of |
| 572 | * these devices into running so do that explicitly. |
| 573 | */ |
Bart Van Assche | 0db6ca8 | 2017-06-02 14:21:55 -0700 | [diff] [blame] | 574 | shost_for_each_device(sdev, shost) { |
| 575 | mutex_lock(&sdev->state_mutex); |
Linus Torvalds | a817e73 | 2017-11-07 09:04:32 -0800 | [diff] [blame] | 576 | if (sdev->sdev_state == SDEV_OFFLINE) |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 577 | sdev->sdev_state = SDEV_RUNNING; |
Bart Van Assche | 0db6ca8 | 2017-06-02 14:21:55 -0700 | [diff] [blame] | 578 | mutex_unlock(&sdev->state_mutex); |
| 579 | } |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 580 | } else if (rport->state == SRP_RPORT_RUNNING) { |
| 581 | /* |
Bart Van Assche | 18cc4e0 | 2013-12-11 17:05:22 +0100 | [diff] [blame] | 582 | * srp_reconnect_rport() has been invoked with fast_io_fail |
| 583 | * and dev_loss off. Mark the port as failed and start the TL |
| 584 | * failure timers if these had not yet been started. |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 585 | */ |
| 586 | __rport_fail_io_fast(rport); |
| 587 | scsi_target_unblock(&shost->shost_gendev, |
| 588 | SDEV_TRANSPORT_OFFLINE); |
| 589 | __srp_start_tl_fail_timers(rport); |
| 590 | } else if (rport->state != SRP_RPORT_BLOCKED) { |
| 591 | scsi_target_unblock(&shost->shost_gendev, |
| 592 | SDEV_TRANSPORT_OFFLINE); |
| 593 | } |
| 594 | mutex_unlock(&rport->mutex); |
| 595 | |
| 596 | out: |
| 597 | return res; |
| 598 | } |
| 599 | EXPORT_SYMBOL(srp_reconnect_rport); |
| 600 | |
| 601 | /** |
| 602 | * srp_timed_out() - SRP transport intercept of the SCSI timeout EH |
Bart Van Assche | 0c7f821 | 2014-01-13 12:39:35 +0100 | [diff] [blame] | 603 | * @scmd: SCSI command. |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 604 | * |
| 605 | * If a timeout occurs while an rport is in the blocked state, ask the SCSI |
| 606 | * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core |
Christoph Hellwig | 6600593 | 2018-05-29 15:52:29 +0200 | [diff] [blame] | 607 | * handle the timeout (BLK_EH_DONE). |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 608 | * |
| 609 | * Note: This function is called from soft-IRQ context and with the request |
| 610 | * queue lock held. |
| 611 | */ |
Christoph Hellwig | b6a05c8 | 2017-01-30 13:18:58 +0100 | [diff] [blame] | 612 | enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd) |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 613 | { |
| 614 | struct scsi_device *sdev = scmd->device; |
| 615 | struct Scsi_Host *shost = sdev->host; |
| 616 | struct srp_internal *i = to_srp_internal(shost->transportt); |
Bart Van Assche | e68ca75 | 2015-05-18 13:24:38 +0200 | [diff] [blame] | 617 | struct srp_rport *rport = shost_to_rport(shost); |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 618 | |
| 619 | pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev)); |
Bart Van Assche | c9ddf73 | 2018-05-21 11:17:29 -0700 | [diff] [blame] | 620 | return rport && rport->fast_io_fail_tmo < 0 && |
| 621 | rport->dev_loss_tmo < 0 && |
Bart Van Assche | e68ca75 | 2015-05-18 13:24:38 +0200 | [diff] [blame] | 622 | i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ? |
Christoph Hellwig | 6600593 | 2018-05-29 15:52:29 +0200 | [diff] [blame] | 623 | BLK_EH_RESET_TIMER : BLK_EH_DONE; |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 624 | } |
Christoph Hellwig | b6a05c8 | 2017-01-30 13:18:58 +0100 | [diff] [blame] | 625 | EXPORT_SYMBOL(srp_timed_out); |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 626 | |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 627 | static void srp_rport_release(struct device *dev) |
| 628 | { |
| 629 | struct srp_rport *rport = dev_to_rport(dev); |
| 630 | |
| 631 | put_device(dev->parent); |
| 632 | kfree(rport); |
| 633 | } |
| 634 | |
| 635 | static int scsi_is_srp_rport(const struct device *dev) |
| 636 | { |
| 637 | return dev->release == srp_rport_release; |
| 638 | } |
| 639 | |
| 640 | static int srp_rport_match(struct attribute_container *cont, |
| 641 | struct device *dev) |
| 642 | { |
| 643 | struct Scsi_Host *shost; |
| 644 | struct srp_internal *i; |
| 645 | |
| 646 | if (!scsi_is_srp_rport(dev)) |
| 647 | return 0; |
| 648 | |
| 649 | shost = dev_to_shost(dev->parent); |
| 650 | if (!shost->transportt) |
| 651 | return 0; |
| 652 | if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) |
| 653 | return 0; |
| 654 | |
| 655 | i = to_srp_internal(shost->transportt); |
| 656 | return &i->rport_attr_cont.ac == cont; |
| 657 | } |
| 658 | |
| 659 | static int srp_host_match(struct attribute_container *cont, struct device *dev) |
| 660 | { |
| 661 | struct Scsi_Host *shost; |
| 662 | struct srp_internal *i; |
| 663 | |
| 664 | if (!scsi_is_host_device(dev)) |
| 665 | return 0; |
| 666 | |
| 667 | shost = dev_to_shost(dev); |
| 668 | if (!shost->transportt) |
| 669 | return 0; |
| 670 | if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) |
| 671 | return 0; |
| 672 | |
| 673 | i = to_srp_internal(shost->transportt); |
| 674 | return &i->t.host_attrs.ac == cont; |
| 675 | } |
| 676 | |
| 677 | /** |
Bart Van Assche | 9dd69a6 | 2013-10-26 14:32:30 +0200 | [diff] [blame] | 678 | * srp_rport_get() - increment rport reference count |
Bart Van Assche | 0c7f821 | 2014-01-13 12:39:35 +0100 | [diff] [blame] | 679 | * @rport: SRP target port. |
Bart Van Assche | 9dd69a6 | 2013-10-26 14:32:30 +0200 | [diff] [blame] | 680 | */ |
| 681 | void srp_rport_get(struct srp_rport *rport) |
| 682 | { |
| 683 | get_device(&rport->dev); |
| 684 | } |
| 685 | EXPORT_SYMBOL(srp_rport_get); |
| 686 | |
| 687 | /** |
| 688 | * srp_rport_put() - decrement rport reference count |
Bart Van Assche | 0c7f821 | 2014-01-13 12:39:35 +0100 | [diff] [blame] | 689 | * @rport: SRP target port. |
Bart Van Assche | 9dd69a6 | 2013-10-26 14:32:30 +0200 | [diff] [blame] | 690 | */ |
| 691 | void srp_rport_put(struct srp_rport *rport) |
| 692 | { |
| 693 | put_device(&rport->dev); |
| 694 | } |
| 695 | EXPORT_SYMBOL(srp_rport_put); |
| 696 | |
| 697 | /** |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 698 | * srp_rport_add - add a SRP remote port to the device hierarchy |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 699 | * @shost: scsi host the remote port is connected to. |
| 700 | * @ids: The port id for the remote port. |
| 701 | * |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 702 | * Publishes a port to the rest of the system. |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 703 | */ |
| 704 | struct srp_rport *srp_rport_add(struct Scsi_Host *shost, |
| 705 | struct srp_rport_identifiers *ids) |
| 706 | { |
| 707 | struct srp_rport *rport; |
| 708 | struct device *parent = &shost->shost_gendev; |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 709 | struct srp_internal *i = to_srp_internal(shost->transportt); |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 710 | int id, ret; |
| 711 | |
| 712 | rport = kzalloc(sizeof(*rport), GFP_KERNEL); |
| 713 | if (!rport) |
| 714 | return ERR_PTR(-ENOMEM); |
| 715 | |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 716 | mutex_init(&rport->mutex); |
| 717 | |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 718 | device_initialize(&rport->dev); |
| 719 | |
| 720 | rport->dev.parent = get_device(parent); |
| 721 | rport->dev.release = srp_rport_release; |
| 722 | |
| 723 | memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id)); |
FUJITA Tomonori | aebd5e4 | 2007-07-11 15:08:15 +0900 | [diff] [blame] | 724 | rport->roles = ids->roles; |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 725 | |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 726 | if (i->f->reconnect) |
| 727 | rport->reconnect_delay = i->f->reconnect_delay ? |
| 728 | *i->f->reconnect_delay : 10; |
| 729 | INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work); |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 730 | rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ? |
| 731 | *i->f->fast_io_fail_tmo : 15; |
| 732 | rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60; |
| 733 | INIT_DELAYED_WORK(&rport->fast_io_fail_work, |
| 734 | rport_fast_io_fail_timedout); |
| 735 | INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout); |
| 736 | |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 737 | id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id); |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 738 | dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id); |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 739 | |
| 740 | transport_setup_device(&rport->dev); |
| 741 | |
| 742 | ret = device_add(&rport->dev); |
| 743 | if (ret) { |
| 744 | transport_destroy_device(&rport->dev); |
| 745 | put_device(&rport->dev); |
| 746 | return ERR_PTR(ret); |
| 747 | } |
| 748 | |
| 749 | transport_add_device(&rport->dev); |
| 750 | transport_configure_device(&rport->dev); |
| 751 | |
| 752 | return rport; |
| 753 | } |
| 754 | EXPORT_SYMBOL_GPL(srp_rport_add); |
| 755 | |
| 756 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 757 | * srp_rport_del - remove a SRP remote port |
| 758 | * @rport: SRP remote port to remove |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 759 | * |
| 760 | * Removes the specified SRP remote port. |
| 761 | */ |
| 762 | void srp_rport_del(struct srp_rport *rport) |
| 763 | { |
| 764 | struct device *dev = &rport->dev; |
FUJITA Tomonori | 62fe882 | 2007-07-11 15:08:19 +0900 | [diff] [blame] | 765 | |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 766 | transport_remove_device(dev); |
| 767 | device_del(dev); |
| 768 | transport_destroy_device(dev); |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 769 | |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 770 | put_device(dev); |
| 771 | } |
| 772 | EXPORT_SYMBOL_GPL(srp_rport_del); |
| 773 | |
| 774 | static int do_srp_rport_del(struct device *dev, void *data) |
| 775 | { |
Dave Dillow | 9118334 | 2008-01-03 21:34:49 -0500 | [diff] [blame] | 776 | if (scsi_is_srp_rport(dev)) |
| 777 | srp_rport_del(dev_to_rport(dev)); |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 782 | * srp_remove_host - tear down a Scsi_Host's SRP data structures |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 783 | * @shost: Scsi Host that is torn down |
| 784 | * |
| 785 | * Removes all SRP remote ports for a given Scsi_Host. |
| 786 | * Must be called just before scsi_remove_host for SRP HBAs. |
| 787 | */ |
| 788 | void srp_remove_host(struct Scsi_Host *shost) |
| 789 | { |
| 790 | device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del); |
| 791 | } |
| 792 | EXPORT_SYMBOL_GPL(srp_remove_host); |
| 793 | |
Bart Van Assche | 9307916 | 2013-12-11 17:06:14 +0100 | [diff] [blame] | 794 | /** |
| 795 | * srp_stop_rport_timers - stop the transport layer recovery timers |
Bart Van Assche | 6751360 | 2014-03-14 13:51:19 +0100 | [diff] [blame] | 796 | * @rport: SRP remote port for which to stop the timers. |
Bart Van Assche | 9307916 | 2013-12-11 17:06:14 +0100 | [diff] [blame] | 797 | * |
| 798 | * Must be called after srp_remove_host() and scsi_remove_host(). The caller |
| 799 | * must hold a reference on the rport (rport->dev) and on the SCSI host |
| 800 | * (rport->dev.parent). |
| 801 | */ |
| 802 | void srp_stop_rport_timers(struct srp_rport *rport) |
| 803 | { |
| 804 | mutex_lock(&rport->mutex); |
| 805 | if (rport->state == SRP_RPORT_BLOCKED) |
| 806 | __rport_fail_io_fast(rport); |
| 807 | srp_rport_set_state(rport, SRP_RPORT_LOST); |
| 808 | mutex_unlock(&rport->mutex); |
| 809 | |
| 810 | cancel_delayed_work_sync(&rport->reconnect_work); |
| 811 | cancel_delayed_work_sync(&rport->fast_io_fail_work); |
| 812 | cancel_delayed_work_sync(&rport->dev_loss_work); |
| 813 | } |
| 814 | EXPORT_SYMBOL_GPL(srp_stop_rport_timers); |
| 815 | |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 816 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 817 | * srp_attach_transport - instantiate SRP transport template |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 818 | * @ft: SRP transport class function template |
| 819 | */ |
| 820 | struct scsi_transport_template * |
| 821 | srp_attach_transport(struct srp_function_template *ft) |
| 822 | { |
| 823 | int count; |
| 824 | struct srp_internal *i; |
| 825 | |
| 826 | i = kzalloc(sizeof(*i), GFP_KERNEL); |
| 827 | if (!i) |
| 828 | return NULL; |
| 829 | |
| 830 | i->t.host_size = sizeof(struct srp_host_attrs); |
| 831 | i->t.host_attrs.ac.attrs = &i->host_attrs[0]; |
| 832 | i->t.host_attrs.ac.class = &srp_host_class.class; |
| 833 | i->t.host_attrs.ac.match = srp_host_match; |
| 834 | i->host_attrs[0] = NULL; |
| 835 | transport_container_register(&i->t.host_attrs); |
| 836 | |
| 837 | i->rport_attr_cont.ac.attrs = &i->rport_attrs[0]; |
| 838 | i->rport_attr_cont.ac.class = &srp_rport_class.class; |
| 839 | i->rport_attr_cont.ac.match = srp_rport_match; |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 840 | |
| 841 | count = 0; |
Bart Van Assche | 9134a85 | 2011-09-10 16:31:57 +0200 | [diff] [blame] | 842 | i->rport_attrs[count++] = &dev_attr_port_id; |
| 843 | i->rport_attrs[count++] = &dev_attr_roles; |
Bart Van Assche | 29c1732 | 2013-10-26 14:33:30 +0200 | [diff] [blame] | 844 | if (ft->has_rport_state) { |
| 845 | i->rport_attrs[count++] = &dev_attr_state; |
| 846 | i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo; |
| 847 | i->rport_attrs[count++] = &dev_attr_dev_loss_tmo; |
| 848 | } |
Bart Van Assche | 8c64e45 | 2013-10-26 14:35:59 +0200 | [diff] [blame] | 849 | if (ft->reconnect) { |
| 850 | i->rport_attrs[count++] = &dev_attr_reconnect_delay; |
| 851 | i->rport_attrs[count++] = &dev_attr_failed_reconnects; |
| 852 | } |
Bart Van Assche | dc1bdbd | 2011-09-16 20:41:13 +0200 | [diff] [blame] | 853 | if (ft->rport_delete) |
| 854 | i->rport_attrs[count++] = &dev_attr_delete; |
Bart Van Assche | 9134a85 | 2011-09-10 16:31:57 +0200 | [diff] [blame] | 855 | i->rport_attrs[count++] = NULL; |
| 856 | BUG_ON(count > ARRAY_SIZE(i->rport_attrs)); |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 857 | |
Bart Van Assche | ac9be30 | 2011-10-21 19:31:07 +0200 | [diff] [blame] | 858 | transport_container_register(&i->rport_attr_cont); |
| 859 | |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 860 | i->f = ft; |
| 861 | |
| 862 | return &i->t; |
| 863 | } |
| 864 | EXPORT_SYMBOL_GPL(srp_attach_transport); |
| 865 | |
| 866 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 867 | * srp_release_transport - release SRP transport template instance |
FUJITA Tomonori | 09345f6 | 2007-06-27 16:32:39 +0900 | [diff] [blame] | 868 | * @t: transport template instance |
| 869 | */ |
| 870 | void srp_release_transport(struct scsi_transport_template *t) |
| 871 | { |
| 872 | struct srp_internal *i = to_srp_internal(t); |
| 873 | |
| 874 | transport_container_unregister(&i->t.host_attrs); |
| 875 | transport_container_unregister(&i->rport_attr_cont); |
| 876 | |
| 877 | kfree(i); |
| 878 | } |
| 879 | EXPORT_SYMBOL_GPL(srp_release_transport); |
| 880 | |
| 881 | static __init int srp_transport_init(void) |
| 882 | { |
| 883 | int ret; |
| 884 | |
| 885 | ret = transport_class_register(&srp_host_class); |
| 886 | if (ret) |
| 887 | return ret; |
| 888 | ret = transport_class_register(&srp_rport_class); |
| 889 | if (ret) |
| 890 | goto unregister_host_class; |
| 891 | |
| 892 | return 0; |
| 893 | unregister_host_class: |
| 894 | transport_class_unregister(&srp_host_class); |
| 895 | return ret; |
| 896 | } |
| 897 | |
| 898 | static void __exit srp_transport_exit(void) |
| 899 | { |
| 900 | transport_class_unregister(&srp_host_class); |
| 901 | transport_class_unregister(&srp_rport_class); |
| 902 | } |
| 903 | |
| 904 | MODULE_AUTHOR("FUJITA Tomonori"); |
| 905 | MODULE_DESCRIPTION("SRP Transport Attributes"); |
| 906 | MODULE_LICENSE("GPL"); |
| 907 | |
| 908 | module_init(srp_transport_init); |
| 909 | module_exit(srp_transport_exit); |