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