2 * SCSI RDMA (SRP) transport class
4 * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org>
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
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.
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
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>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_srp.h>
34 #include "scsi_priv.h"
36 struct srp_host_attrs {
37 atomic_t next_port_id;
39 #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data)
41 #define SRP_HOST_ATTRS 0
42 #define SRP_RPORT_ATTRS 8
45 struct scsi_transport_template t;
46 struct srp_function_template *f;
48 struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
50 struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
51 struct transport_container rport_attr_cont;
54 static int scsi_is_srp_rport(const struct device *dev);
56 #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
58 #define dev_to_rport(d) container_of(d, struct srp_rport, dev)
59 #define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
60 static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r)
62 return dev_to_shost(r->dev.parent);
65 static int find_child_rport(struct device *dev, void *data)
67 struct device **child = data;
69 if (scsi_is_srp_rport(dev)) {
76 static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost)
78 struct device *child = NULL;
80 WARN_ON_ONCE(device_for_each_child(&shost->shost_gendev, &child,
81 find_child_rport) < 0);
82 return child ? dev_to_rport(child) : NULL;
86 * srp_tmo_valid() - check timeout combination validity
87 * @reconnect_delay: Reconnect delay in seconds.
88 * @fast_io_fail_tmo: Fast I/O fail timeout in seconds.
89 * @dev_loss_tmo: Device loss timeout in seconds.
91 * The combination of the timeout parameters must be such that SCSI commands
92 * are finished in a reasonable time. Hence do not allow the fast I/O fail
93 * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to
94 * exceed that limit if failing I/O fast has been disabled. Furthermore, these
95 * parameters must be such that multipath can detect failed paths timely.
96 * Hence do not allow all three parameters to be disabled simultaneously.
98 int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, long dev_loss_tmo)
100 if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
102 if (reconnect_delay == 0)
104 if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
106 if (fast_io_fail_tmo < 0 &&
107 dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
109 if (dev_loss_tmo >= LONG_MAX / HZ)
111 if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
112 fast_io_fail_tmo >= dev_loss_tmo)
116 EXPORT_SYMBOL_GPL(srp_tmo_valid);
118 static int srp_host_setup(struct transport_container *tc, struct device *dev,
121 struct Scsi_Host *shost = dev_to_shost(dev);
122 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
124 atomic_set(&srp_host->next_port_id, 0);
128 static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
131 static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
135 show_srp_rport_id(struct device *dev, struct device_attribute *attr,
138 struct srp_rport *rport = transport_class_to_srp_rport(dev);
139 return sprintf(buf, "%16phC\n", rport->port_id);
142 static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
144 static const struct {
147 } srp_rport_role_names[] = {
148 {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
149 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
153 show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
156 struct srp_rport *rport = transport_class_to_srp_rport(dev);
160 for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
161 if (srp_rport_role_names[i].value == rport->roles) {
162 name = srp_rport_role_names[i].name;
165 return sprintf(buf, "%s\n", name ? : "unknown");
168 static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
170 static ssize_t store_srp_rport_delete(struct device *dev,
171 struct device_attribute *attr,
172 const char *buf, size_t count)
174 struct srp_rport *rport = transport_class_to_srp_rport(dev);
175 struct Scsi_Host *shost = dev_to_shost(dev);
176 struct srp_internal *i = to_srp_internal(shost->transportt);
178 if (i->f->rport_delete) {
179 i->f->rport_delete(rport);
186 static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
188 static ssize_t show_srp_rport_state(struct device *dev,
189 struct device_attribute *attr,
192 static const char *const state_name[] = {
193 [SRP_RPORT_RUNNING] = "running",
194 [SRP_RPORT_BLOCKED] = "blocked",
195 [SRP_RPORT_FAIL_FAST] = "fail-fast",
196 [SRP_RPORT_LOST] = "lost",
198 struct srp_rport *rport = transport_class_to_srp_rport(dev);
199 enum srp_rport_state state = rport->state;
201 return sprintf(buf, "%s\n",
202 (unsigned)state < ARRAY_SIZE(state_name) ?
203 state_name[state] : "???");
206 static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
208 static ssize_t srp_show_tmo(char *buf, int tmo)
210 return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
213 int srp_parse_tmo(int *tmo, const char *buf)
217 if (strncmp(buf, "off", 3) != 0)
218 res = kstrtoint(buf, 0, tmo);
224 EXPORT_SYMBOL(srp_parse_tmo);
226 static ssize_t show_reconnect_delay(struct device *dev,
227 struct device_attribute *attr, char *buf)
229 struct srp_rport *rport = transport_class_to_srp_rport(dev);
231 return srp_show_tmo(buf, rport->reconnect_delay);
234 static ssize_t store_reconnect_delay(struct device *dev,
235 struct device_attribute *attr,
236 const char *buf, const size_t count)
238 struct srp_rport *rport = transport_class_to_srp_rport(dev);
241 res = srp_parse_tmo(&delay, buf);
244 res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
245 rport->dev_loss_tmo);
249 if (rport->reconnect_delay <= 0 && delay > 0 &&
250 rport->state != SRP_RPORT_RUNNING) {
251 queue_delayed_work(system_long_wq, &rport->reconnect_work,
253 } else if (delay <= 0) {
254 cancel_delayed_work(&rport->reconnect_work);
256 rport->reconnect_delay = delay;
263 static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
264 store_reconnect_delay);
266 static ssize_t show_failed_reconnects(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 struct srp_rport *rport = transport_class_to_srp_rport(dev);
271 return sprintf(buf, "%d\n", rport->failed_reconnects);
274 static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
276 static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
277 struct device_attribute *attr,
280 struct srp_rport *rport = transport_class_to_srp_rport(dev);
282 return srp_show_tmo(buf, rport->fast_io_fail_tmo);
285 static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
286 struct device_attribute *attr,
287 const char *buf, size_t count)
289 struct srp_rport *rport = transport_class_to_srp_rport(dev);
291 int fast_io_fail_tmo;
293 res = srp_parse_tmo(&fast_io_fail_tmo, buf);
296 res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
297 rport->dev_loss_tmo);
300 rport->fast_io_fail_tmo = fast_io_fail_tmo;
307 static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
308 show_srp_rport_fast_io_fail_tmo,
309 store_srp_rport_fast_io_fail_tmo);
311 static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
312 struct device_attribute *attr,
315 struct srp_rport *rport = transport_class_to_srp_rport(dev);
317 return srp_show_tmo(buf, rport->dev_loss_tmo);
320 static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
321 struct device_attribute *attr,
322 const char *buf, size_t count)
324 struct srp_rport *rport = transport_class_to_srp_rport(dev);
328 res = srp_parse_tmo(&dev_loss_tmo, buf);
331 res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
335 rport->dev_loss_tmo = dev_loss_tmo;
342 static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
343 show_srp_rport_dev_loss_tmo,
344 store_srp_rport_dev_loss_tmo);
346 static int srp_rport_set_state(struct srp_rport *rport,
347 enum srp_rport_state new_state)
349 enum srp_rport_state old_state = rport->state;
351 lockdep_assert_held(&rport->mutex);
354 case SRP_RPORT_RUNNING:
362 case SRP_RPORT_BLOCKED:
364 case SRP_RPORT_RUNNING:
370 case SRP_RPORT_FAIL_FAST:
381 rport->state = new_state;
389 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
390 * @work: Work structure used for scheduling this operation.
392 static void srp_reconnect_work(struct work_struct *work)
394 struct srp_rport *rport = container_of(to_delayed_work(work),
395 struct srp_rport, reconnect_work);
396 struct Scsi_Host *shost = rport_to_shost(rport);
399 res = srp_reconnect_rport(rport);
401 shost_printk(KERN_ERR, shost,
402 "reconnect attempt %d failed (%d)\n",
403 ++rport->failed_reconnects, res);
404 delay = rport->reconnect_delay *
405 min(100, max(1, rport->failed_reconnects - 10));
407 queue_delayed_work(system_long_wq,
408 &rport->reconnect_work, delay * HZ);
412 static void __rport_fail_io_fast(struct srp_rport *rport)
414 struct Scsi_Host *shost = rport_to_shost(rport);
415 struct srp_internal *i;
417 lockdep_assert_held(&rport->mutex);
419 if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
422 * Call scsi_target_block() to wait for ongoing shost->queuecommand()
423 * calls before invoking i->f->terminate_rport_io().
425 scsi_target_block(rport->dev.parent);
426 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
428 /* Involve the LLD if possible to terminate all I/O on the rport. */
429 i = to_srp_internal(shost->transportt);
430 if (i->f->terminate_rport_io)
431 i->f->terminate_rport_io(rport);
435 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
436 * @work: Work structure used for scheduling this operation.
438 static void rport_fast_io_fail_timedout(struct work_struct *work)
440 struct srp_rport *rport = container_of(to_delayed_work(work),
441 struct srp_rport, fast_io_fail_work);
442 struct Scsi_Host *shost = rport_to_shost(rport);
444 pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
445 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
447 mutex_lock(&rport->mutex);
448 if (rport->state == SRP_RPORT_BLOCKED)
449 __rport_fail_io_fast(rport);
450 mutex_unlock(&rport->mutex);
454 * rport_dev_loss_timedout() - device loss timeout handler
455 * @work: Work structure used for scheduling this operation.
457 static void rport_dev_loss_timedout(struct work_struct *work)
459 struct srp_rport *rport = container_of(to_delayed_work(work),
460 struct srp_rport, dev_loss_work);
461 struct Scsi_Host *shost = rport_to_shost(rport);
462 struct srp_internal *i = to_srp_internal(shost->transportt);
464 pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
465 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
467 mutex_lock(&rport->mutex);
468 WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
469 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
470 mutex_unlock(&rport->mutex);
472 i->f->rport_delete(rport);
475 static void __srp_start_tl_fail_timers(struct srp_rport *rport)
477 struct Scsi_Host *shost = rport_to_shost(rport);
478 int delay, fast_io_fail_tmo, dev_loss_tmo;
480 lockdep_assert_held(&rport->mutex);
482 delay = rport->reconnect_delay;
483 fast_io_fail_tmo = rport->fast_io_fail_tmo;
484 dev_loss_tmo = rport->dev_loss_tmo;
485 pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
488 if (rport->state == SRP_RPORT_LOST)
491 queue_delayed_work(system_long_wq, &rport->reconnect_work,
493 if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) &&
494 srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
495 pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
497 scsi_target_block(&shost->shost_gendev);
498 if (fast_io_fail_tmo >= 0)
499 queue_delayed_work(system_long_wq,
500 &rport->fast_io_fail_work,
501 1UL * fast_io_fail_tmo * HZ);
502 if (dev_loss_tmo >= 0)
503 queue_delayed_work(system_long_wq,
504 &rport->dev_loss_work,
505 1UL * dev_loss_tmo * HZ);
510 * srp_start_tl_fail_timers() - start the transport layer failure timers
511 * @rport: SRP target port.
513 * Start the transport layer fast I/O failure and device loss timers. Do not
514 * modify a timer that was already started.
516 void srp_start_tl_fail_timers(struct srp_rport *rport)
518 mutex_lock(&rport->mutex);
519 __srp_start_tl_fail_timers(rport);
520 mutex_unlock(&rport->mutex);
522 EXPORT_SYMBOL(srp_start_tl_fail_timers);
525 * srp_reconnect_rport() - reconnect to an SRP target port
526 * @rport: SRP target port.
528 * Blocks SCSI command queueing before invoking reconnect() such that
529 * queuecommand() won't be invoked concurrently with reconnect() from outside
530 * the SCSI EH. This is important since a reconnect() implementation may
531 * reallocate resources needed by queuecommand().
534 * - This function neither waits until outstanding requests have finished nor
535 * tries to abort these. It is the responsibility of the reconnect()
536 * function to finish outstanding commands before reconnecting to the target
538 * - It is the responsibility of the caller to ensure that the resources
539 * reallocated by the reconnect() function won't be used while this function
540 * is in progress. One possible strategy is to invoke this function from
541 * the context of the SCSI EH thread only. Another possible strategy is to
542 * lock the rport mutex inside each SCSI LLD callback that can be invoked by
543 * the SCSI EH (the scsi_host_template.eh_*() functions and also the
544 * scsi_host_template.queuecommand() function).
546 int srp_reconnect_rport(struct srp_rport *rport)
548 struct Scsi_Host *shost = rport_to_shost(rport);
549 struct srp_internal *i = to_srp_internal(shost->transportt);
550 struct scsi_device *sdev;
553 pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
555 res = mutex_lock_interruptible(&rport->mutex);
558 if (rport->state != SRP_RPORT_FAIL_FAST && rport->state != SRP_RPORT_LOST)
560 * sdev state must be SDEV_TRANSPORT_OFFLINE, transition
561 * to SDEV_BLOCK is illegal. Calling scsi_target_unblock()
562 * later is ok though, scsi_internal_device_unblock_nowait()
563 * treats SDEV_TRANSPORT_OFFLINE like SDEV_BLOCK.
565 scsi_target_block(&shost->shost_gendev);
566 res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
567 pr_debug("%s (state %d): transport.reconnect() returned %d\n",
568 dev_name(&shost->shost_gendev), rport->state, res);
570 cancel_delayed_work(&rport->fast_io_fail_work);
571 cancel_delayed_work(&rport->dev_loss_work);
573 rport->failed_reconnects = 0;
574 srp_rport_set_state(rport, SRP_RPORT_RUNNING);
575 scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
577 * If the SCSI error handler has offlined one or more devices,
578 * invoking scsi_target_unblock() won't change the state of
579 * these devices into running so do that explicitly.
581 shost_for_each_device(sdev, shost) {
582 mutex_lock(&sdev->state_mutex);
583 if (sdev->sdev_state == SDEV_OFFLINE)
584 sdev->sdev_state = SDEV_RUNNING;
585 mutex_unlock(&sdev->state_mutex);
587 } else if (rport->state == SRP_RPORT_RUNNING) {
589 * srp_reconnect_rport() has been invoked with fast_io_fail
590 * and dev_loss off. Mark the port as failed and start the TL
591 * failure timers if these had not yet been started.
593 __rport_fail_io_fast(rport);
594 scsi_target_unblock(&shost->shost_gendev,
595 SDEV_TRANSPORT_OFFLINE);
596 __srp_start_tl_fail_timers(rport);
597 } else if (rport->state != SRP_RPORT_BLOCKED) {
598 scsi_target_unblock(&shost->shost_gendev,
599 SDEV_TRANSPORT_OFFLINE);
601 mutex_unlock(&rport->mutex);
606 EXPORT_SYMBOL(srp_reconnect_rport);
609 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
610 * @scmd: SCSI command.
612 * If a timeout occurs while an rport is in the blocked state, ask the SCSI
613 * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
614 * handle the timeout (BLK_EH_NOT_HANDLED).
616 * Note: This function is called from soft-IRQ context and with the request
619 enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
621 struct scsi_device *sdev = scmd->device;
622 struct Scsi_Host *shost = sdev->host;
623 struct srp_internal *i = to_srp_internal(shost->transportt);
624 struct srp_rport *rport = shost_to_rport(shost);
626 pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
627 return rport && rport->fast_io_fail_tmo < 0 &&
628 rport->dev_loss_tmo < 0 &&
629 i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
630 BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
632 EXPORT_SYMBOL(srp_timed_out);
634 static void srp_rport_release(struct device *dev)
636 struct srp_rport *rport = dev_to_rport(dev);
638 put_device(dev->parent);
642 static int scsi_is_srp_rport(const struct device *dev)
644 return dev->release == srp_rport_release;
647 static int srp_rport_match(struct attribute_container *cont,
650 struct Scsi_Host *shost;
651 struct srp_internal *i;
653 if (!scsi_is_srp_rport(dev))
656 shost = dev_to_shost(dev->parent);
657 if (!shost->transportt)
659 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
662 i = to_srp_internal(shost->transportt);
663 return &i->rport_attr_cont.ac == cont;
666 static int srp_host_match(struct attribute_container *cont, struct device *dev)
668 struct Scsi_Host *shost;
669 struct srp_internal *i;
671 if (!scsi_is_host_device(dev))
674 shost = dev_to_shost(dev);
675 if (!shost->transportt)
677 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
680 i = to_srp_internal(shost->transportt);
681 return &i->t.host_attrs.ac == cont;
685 * srp_rport_get() - increment rport reference count
686 * @rport: SRP target port.
688 void srp_rport_get(struct srp_rport *rport)
690 get_device(&rport->dev);
692 EXPORT_SYMBOL(srp_rport_get);
695 * srp_rport_put() - decrement rport reference count
696 * @rport: SRP target port.
698 void srp_rport_put(struct srp_rport *rport)
700 put_device(&rport->dev);
702 EXPORT_SYMBOL(srp_rport_put);
705 * srp_rport_add - add a SRP remote port to the device hierarchy
706 * @shost: scsi host the remote port is connected to.
707 * @ids: The port id for the remote port.
709 * Publishes a port to the rest of the system.
711 struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
712 struct srp_rport_identifiers *ids)
714 struct srp_rport *rport;
715 struct device *parent = &shost->shost_gendev;
716 struct srp_internal *i = to_srp_internal(shost->transportt);
719 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
721 return ERR_PTR(-ENOMEM);
723 mutex_init(&rport->mutex);
725 device_initialize(&rport->dev);
727 rport->dev.parent = get_device(parent);
728 rport->dev.release = srp_rport_release;
730 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
731 rport->roles = ids->roles;
734 rport->reconnect_delay = i->f->reconnect_delay ?
735 *i->f->reconnect_delay : 10;
736 INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
737 rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
738 *i->f->fast_io_fail_tmo : 15;
739 rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
740 INIT_DELAYED_WORK(&rport->fast_io_fail_work,
741 rport_fast_io_fail_timedout);
742 INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
744 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
745 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
747 transport_setup_device(&rport->dev);
749 ret = device_add(&rport->dev);
751 transport_destroy_device(&rport->dev);
752 put_device(&rport->dev);
756 transport_add_device(&rport->dev);
757 transport_configure_device(&rport->dev);
761 EXPORT_SYMBOL_GPL(srp_rport_add);
764 * srp_rport_del - remove a SRP remote port
765 * @rport: SRP remote port to remove
767 * Removes the specified SRP remote port.
769 void srp_rport_del(struct srp_rport *rport)
771 struct device *dev = &rport->dev;
773 transport_remove_device(dev);
775 transport_destroy_device(dev);
779 EXPORT_SYMBOL_GPL(srp_rport_del);
781 static int do_srp_rport_del(struct device *dev, void *data)
783 if (scsi_is_srp_rport(dev))
784 srp_rport_del(dev_to_rport(dev));
789 * srp_remove_host - tear down a Scsi_Host's SRP data structures
790 * @shost: Scsi Host that is torn down
792 * Removes all SRP remote ports for a given Scsi_Host.
793 * Must be called just before scsi_remove_host for SRP HBAs.
795 void srp_remove_host(struct Scsi_Host *shost)
797 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
799 EXPORT_SYMBOL_GPL(srp_remove_host);
802 * srp_stop_rport_timers - stop the transport layer recovery timers
803 * @rport: SRP remote port for which to stop the timers.
805 * Must be called after srp_remove_host() and scsi_remove_host(). The caller
806 * must hold a reference on the rport (rport->dev) and on the SCSI host
807 * (rport->dev.parent).
809 void srp_stop_rport_timers(struct srp_rport *rport)
811 mutex_lock(&rport->mutex);
812 if (rport->state == SRP_RPORT_BLOCKED)
813 __rport_fail_io_fast(rport);
814 srp_rport_set_state(rport, SRP_RPORT_LOST);
815 mutex_unlock(&rport->mutex);
817 cancel_delayed_work_sync(&rport->reconnect_work);
818 cancel_delayed_work_sync(&rport->fast_io_fail_work);
819 cancel_delayed_work_sync(&rport->dev_loss_work);
821 EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
824 * srp_attach_transport - instantiate SRP transport template
825 * @ft: SRP transport class function template
827 struct scsi_transport_template *
828 srp_attach_transport(struct srp_function_template *ft)
831 struct srp_internal *i;
833 i = kzalloc(sizeof(*i), GFP_KERNEL);
837 i->t.host_size = sizeof(struct srp_host_attrs);
838 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
839 i->t.host_attrs.ac.class = &srp_host_class.class;
840 i->t.host_attrs.ac.match = srp_host_match;
841 i->host_attrs[0] = NULL;
842 transport_container_register(&i->t.host_attrs);
844 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
845 i->rport_attr_cont.ac.class = &srp_rport_class.class;
846 i->rport_attr_cont.ac.match = srp_rport_match;
849 i->rport_attrs[count++] = &dev_attr_port_id;
850 i->rport_attrs[count++] = &dev_attr_roles;
851 if (ft->has_rport_state) {
852 i->rport_attrs[count++] = &dev_attr_state;
853 i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
854 i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
857 i->rport_attrs[count++] = &dev_attr_reconnect_delay;
858 i->rport_attrs[count++] = &dev_attr_failed_reconnects;
860 if (ft->rport_delete)
861 i->rport_attrs[count++] = &dev_attr_delete;
862 i->rport_attrs[count++] = NULL;
863 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
865 transport_container_register(&i->rport_attr_cont);
871 EXPORT_SYMBOL_GPL(srp_attach_transport);
874 * srp_release_transport - release SRP transport template instance
875 * @t: transport template instance
877 void srp_release_transport(struct scsi_transport_template *t)
879 struct srp_internal *i = to_srp_internal(t);
881 transport_container_unregister(&i->t.host_attrs);
882 transport_container_unregister(&i->rport_attr_cont);
886 EXPORT_SYMBOL_GPL(srp_release_transport);
888 static __init int srp_transport_init(void)
892 ret = transport_class_register(&srp_host_class);
895 ret = transport_class_register(&srp_rport_class);
897 goto unregister_host_class;
900 unregister_host_class:
901 transport_class_unregister(&srp_host_class);
905 static void __exit srp_transport_exit(void)
907 transport_class_unregister(&srp_host_class);
908 transport_class_unregister(&srp_rport_class);
911 MODULE_AUTHOR("FUJITA Tomonori");
912 MODULE_DESCRIPTION("SRP Transport Attributes");
913 MODULE_LICENSE("GPL");
915 module_init(srp_transport_init);
916 module_exit(srp_transport_exit);