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