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