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