watchdog/core: Rename some softlockup_* functions
[linux/fpc-iii.git] / drivers / scsi / scsi_transport_srp.c
blob4f6f01cf9968e70c81e4dbd6d5603a3ae4fa4f1c
1 /*
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
9 * License.
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
19 * 02110-1301 USA
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
44 struct srp_internal {
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 #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
56 #define dev_to_rport(d) container_of(d, struct srp_rport, dev)
57 #define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
58 static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r)
60 return dev_to_shost(r->dev.parent);
63 static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost)
65 return transport_class_to_srp_rport(&shost->shost_gendev);
68 /**
69 * srp_tmo_valid() - check timeout combination validity
70 * @reconnect_delay: Reconnect delay in seconds.
71 * @fast_io_fail_tmo: Fast I/O fail timeout in seconds.
72 * @dev_loss_tmo: Device loss timeout in seconds.
74 * The combination of the timeout parameters must be such that SCSI commands
75 * are finished in a reasonable time. Hence do not allow the fast I/O fail
76 * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to
77 * exceed that limit if failing I/O fast has been disabled. Furthermore, these
78 * parameters must be such that multipath can detect failed paths timely.
79 * Hence do not allow all three parameters to be disabled simultaneously.
81 int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, long dev_loss_tmo)
83 if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
84 return -EINVAL;
85 if (reconnect_delay == 0)
86 return -EINVAL;
87 if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
88 return -EINVAL;
89 if (fast_io_fail_tmo < 0 &&
90 dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
91 return -EINVAL;
92 if (dev_loss_tmo >= LONG_MAX / HZ)
93 return -EINVAL;
94 if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
95 fast_io_fail_tmo >= dev_loss_tmo)
96 return -EINVAL;
97 return 0;
99 EXPORT_SYMBOL_GPL(srp_tmo_valid);
101 static int srp_host_setup(struct transport_container *tc, struct device *dev,
102 struct device *cdev)
104 struct Scsi_Host *shost = dev_to_shost(dev);
105 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
107 atomic_set(&srp_host->next_port_id, 0);
108 return 0;
111 static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
112 NULL, NULL);
114 static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
115 NULL, NULL, NULL);
117 static ssize_t
118 show_srp_rport_id(struct device *dev, struct device_attribute *attr,
119 char *buf)
121 struct srp_rport *rport = transport_class_to_srp_rport(dev);
122 return sprintf(buf, "%16phC\n", rport->port_id);
125 static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
127 static const struct {
128 u32 value;
129 char *name;
130 } srp_rport_role_names[] = {
131 {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
132 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
135 static ssize_t
136 show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
137 char *buf)
139 struct srp_rport *rport = transport_class_to_srp_rport(dev);
140 int i;
141 char *name = NULL;
143 for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
144 if (srp_rport_role_names[i].value == rport->roles) {
145 name = srp_rport_role_names[i].name;
146 break;
148 return sprintf(buf, "%s\n", name ? : "unknown");
151 static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
153 static ssize_t store_srp_rport_delete(struct device *dev,
154 struct device_attribute *attr,
155 const char *buf, size_t count)
157 struct srp_rport *rport = transport_class_to_srp_rport(dev);
158 struct Scsi_Host *shost = dev_to_shost(dev);
159 struct srp_internal *i = to_srp_internal(shost->transportt);
161 if (i->f->rport_delete) {
162 i->f->rport_delete(rport);
163 return count;
164 } else {
165 return -ENOSYS;
169 static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
171 static ssize_t show_srp_rport_state(struct device *dev,
172 struct device_attribute *attr,
173 char *buf)
175 static const char *const state_name[] = {
176 [SRP_RPORT_RUNNING] = "running",
177 [SRP_RPORT_BLOCKED] = "blocked",
178 [SRP_RPORT_FAIL_FAST] = "fail-fast",
179 [SRP_RPORT_LOST] = "lost",
181 struct srp_rport *rport = transport_class_to_srp_rport(dev);
182 enum srp_rport_state state = rport->state;
184 return sprintf(buf, "%s\n",
185 (unsigned)state < ARRAY_SIZE(state_name) ?
186 state_name[state] : "???");
189 static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
191 static ssize_t srp_show_tmo(char *buf, int tmo)
193 return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
196 int srp_parse_tmo(int *tmo, const char *buf)
198 int res = 0;
200 if (strncmp(buf, "off", 3) != 0)
201 res = kstrtoint(buf, 0, tmo);
202 else
203 *tmo = -1;
205 return res;
207 EXPORT_SYMBOL(srp_parse_tmo);
209 static ssize_t show_reconnect_delay(struct device *dev,
210 struct device_attribute *attr, char *buf)
212 struct srp_rport *rport = transport_class_to_srp_rport(dev);
214 return srp_show_tmo(buf, rport->reconnect_delay);
217 static ssize_t store_reconnect_delay(struct device *dev,
218 struct device_attribute *attr,
219 const char *buf, const size_t count)
221 struct srp_rport *rport = transport_class_to_srp_rport(dev);
222 int res, delay;
224 res = srp_parse_tmo(&delay, buf);
225 if (res)
226 goto out;
227 res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
228 rport->dev_loss_tmo);
229 if (res)
230 goto out;
232 if (rport->reconnect_delay <= 0 && delay > 0 &&
233 rport->state != SRP_RPORT_RUNNING) {
234 queue_delayed_work(system_long_wq, &rport->reconnect_work,
235 delay * HZ);
236 } else if (delay <= 0) {
237 cancel_delayed_work(&rport->reconnect_work);
239 rport->reconnect_delay = delay;
240 res = count;
242 out:
243 return res;
246 static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
247 store_reconnect_delay);
249 static ssize_t show_failed_reconnects(struct device *dev,
250 struct device_attribute *attr, char *buf)
252 struct srp_rport *rport = transport_class_to_srp_rport(dev);
254 return sprintf(buf, "%d\n", rport->failed_reconnects);
257 static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
259 static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
260 struct device_attribute *attr,
261 char *buf)
263 struct srp_rport *rport = transport_class_to_srp_rport(dev);
265 return srp_show_tmo(buf, rport->fast_io_fail_tmo);
268 static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
269 struct device_attribute *attr,
270 const char *buf, size_t count)
272 struct srp_rport *rport = transport_class_to_srp_rport(dev);
273 int res;
274 int fast_io_fail_tmo;
276 res = srp_parse_tmo(&fast_io_fail_tmo, buf);
277 if (res)
278 goto out;
279 res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
280 rport->dev_loss_tmo);
281 if (res)
282 goto out;
283 rport->fast_io_fail_tmo = fast_io_fail_tmo;
284 res = count;
286 out:
287 return res;
290 static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
291 show_srp_rport_fast_io_fail_tmo,
292 store_srp_rport_fast_io_fail_tmo);
294 static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
295 struct device_attribute *attr,
296 char *buf)
298 struct srp_rport *rport = transport_class_to_srp_rport(dev);
300 return srp_show_tmo(buf, rport->dev_loss_tmo);
303 static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
304 struct device_attribute *attr,
305 const char *buf, size_t count)
307 struct srp_rport *rport = transport_class_to_srp_rport(dev);
308 int res;
309 int dev_loss_tmo;
311 res = srp_parse_tmo(&dev_loss_tmo, buf);
312 if (res)
313 goto out;
314 res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
315 dev_loss_tmo);
316 if (res)
317 goto out;
318 rport->dev_loss_tmo = dev_loss_tmo;
319 res = count;
321 out:
322 return res;
325 static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
326 show_srp_rport_dev_loss_tmo,
327 store_srp_rport_dev_loss_tmo);
329 static int srp_rport_set_state(struct srp_rport *rport,
330 enum srp_rport_state new_state)
332 enum srp_rport_state old_state = rport->state;
334 lockdep_assert_held(&rport->mutex);
336 switch (new_state) {
337 case SRP_RPORT_RUNNING:
338 switch (old_state) {
339 case SRP_RPORT_LOST:
340 goto invalid;
341 default:
342 break;
344 break;
345 case SRP_RPORT_BLOCKED:
346 switch (old_state) {
347 case SRP_RPORT_RUNNING:
348 break;
349 default:
350 goto invalid;
352 break;
353 case SRP_RPORT_FAIL_FAST:
354 switch (old_state) {
355 case SRP_RPORT_LOST:
356 goto invalid;
357 default:
358 break;
360 break;
361 case SRP_RPORT_LOST:
362 break;
364 rport->state = new_state;
365 return 0;
367 invalid:
368 return -EINVAL;
372 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
373 * @work: Work structure used for scheduling this operation.
375 static void srp_reconnect_work(struct work_struct *work)
377 struct srp_rport *rport = container_of(to_delayed_work(work),
378 struct srp_rport, reconnect_work);
379 struct Scsi_Host *shost = rport_to_shost(rport);
380 int delay, res;
382 res = srp_reconnect_rport(rport);
383 if (res != 0) {
384 shost_printk(KERN_ERR, shost,
385 "reconnect attempt %d failed (%d)\n",
386 ++rport->failed_reconnects, res);
387 delay = rport->reconnect_delay *
388 min(100, max(1, rport->failed_reconnects - 10));
389 if (delay > 0)
390 queue_delayed_work(system_long_wq,
391 &rport->reconnect_work, delay * HZ);
395 static void __rport_fail_io_fast(struct srp_rport *rport)
397 struct Scsi_Host *shost = rport_to_shost(rport);
398 struct srp_internal *i;
400 lockdep_assert_held(&rport->mutex);
402 if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
403 return;
405 * Call scsi_target_block() to wait for ongoing shost->queuecommand()
406 * calls before invoking i->f->terminate_rport_io().
408 scsi_target_block(rport->dev.parent);
409 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
411 /* Involve the LLD if possible to terminate all I/O on the rport. */
412 i = to_srp_internal(shost->transportt);
413 if (i->f->terminate_rport_io)
414 i->f->terminate_rport_io(rport);
418 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
419 * @work: Work structure used for scheduling this operation.
421 static void rport_fast_io_fail_timedout(struct work_struct *work)
423 struct srp_rport *rport = container_of(to_delayed_work(work),
424 struct srp_rport, fast_io_fail_work);
425 struct Scsi_Host *shost = rport_to_shost(rport);
427 pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
428 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
430 mutex_lock(&rport->mutex);
431 if (rport->state == SRP_RPORT_BLOCKED)
432 __rport_fail_io_fast(rport);
433 mutex_unlock(&rport->mutex);
437 * rport_dev_loss_timedout() - device loss timeout handler
438 * @work: Work structure used for scheduling this operation.
440 static void rport_dev_loss_timedout(struct work_struct *work)
442 struct srp_rport *rport = container_of(to_delayed_work(work),
443 struct srp_rport, dev_loss_work);
444 struct Scsi_Host *shost = rport_to_shost(rport);
445 struct srp_internal *i = to_srp_internal(shost->transportt);
447 pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
448 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
450 mutex_lock(&rport->mutex);
451 WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
452 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
453 mutex_unlock(&rport->mutex);
455 i->f->rport_delete(rport);
458 static void __srp_start_tl_fail_timers(struct srp_rport *rport)
460 struct Scsi_Host *shost = rport_to_shost(rport);
461 int delay, fast_io_fail_tmo, dev_loss_tmo;
463 lockdep_assert_held(&rport->mutex);
465 delay = rport->reconnect_delay;
466 fast_io_fail_tmo = rport->fast_io_fail_tmo;
467 dev_loss_tmo = rport->dev_loss_tmo;
468 pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
469 rport->state);
471 if (rport->state == SRP_RPORT_LOST)
472 return;
473 if (delay > 0)
474 queue_delayed_work(system_long_wq, &rport->reconnect_work,
475 1UL * delay * HZ);
476 if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) &&
477 srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
478 pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
479 rport->state);
480 scsi_target_block(&shost->shost_gendev);
481 if (fast_io_fail_tmo >= 0)
482 queue_delayed_work(system_long_wq,
483 &rport->fast_io_fail_work,
484 1UL * fast_io_fail_tmo * HZ);
485 if (dev_loss_tmo >= 0)
486 queue_delayed_work(system_long_wq,
487 &rport->dev_loss_work,
488 1UL * dev_loss_tmo * HZ);
493 * srp_start_tl_fail_timers() - start the transport layer failure timers
494 * @rport: SRP target port.
496 * Start the transport layer fast I/O failure and device loss timers. Do not
497 * modify a timer that was already started.
499 void srp_start_tl_fail_timers(struct srp_rport *rport)
501 mutex_lock(&rport->mutex);
502 __srp_start_tl_fail_timers(rport);
503 mutex_unlock(&rport->mutex);
505 EXPORT_SYMBOL(srp_start_tl_fail_timers);
508 * srp_reconnect_rport() - reconnect to an SRP target port
509 * @rport: SRP target port.
511 * Blocks SCSI command queueing before invoking reconnect() such that
512 * queuecommand() won't be invoked concurrently with reconnect() from outside
513 * the SCSI EH. This is important since a reconnect() implementation may
514 * reallocate resources needed by queuecommand().
516 * Notes:
517 * - This function neither waits until outstanding requests have finished nor
518 * tries to abort these. It is the responsibility of the reconnect()
519 * function to finish outstanding commands before reconnecting to the target
520 * port.
521 * - It is the responsibility of the caller to ensure that the resources
522 * reallocated by the reconnect() function won't be used while this function
523 * is in progress. One possible strategy is to invoke this function from
524 * the context of the SCSI EH thread only. Another possible strategy is to
525 * lock the rport mutex inside each SCSI LLD callback that can be invoked by
526 * the SCSI EH (the scsi_host_template.eh_*() functions and also the
527 * scsi_host_template.queuecommand() function).
529 int srp_reconnect_rport(struct srp_rport *rport)
531 struct Scsi_Host *shost = rport_to_shost(rport);
532 struct srp_internal *i = to_srp_internal(shost->transportt);
533 struct scsi_device *sdev;
534 int res;
536 pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
538 res = mutex_lock_interruptible(&rport->mutex);
539 if (res)
540 goto out;
541 scsi_target_block(&shost->shost_gendev);
542 res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
543 pr_debug("%s (state %d): transport.reconnect() returned %d\n",
544 dev_name(&shost->shost_gendev), rport->state, res);
545 if (res == 0) {
546 cancel_delayed_work(&rport->fast_io_fail_work);
547 cancel_delayed_work(&rport->dev_loss_work);
549 rport->failed_reconnects = 0;
550 srp_rport_set_state(rport, SRP_RPORT_RUNNING);
551 scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
553 * If the SCSI error handler has offlined one or more devices,
554 * invoking scsi_target_unblock() won't change the state of
555 * these devices into running so do that explicitly.
557 shost_for_each_device(sdev, shost) {
558 mutex_lock(&sdev->state_mutex);
559 if (sdev->sdev_state == SDEV_OFFLINE) {
560 sdev->sdev_state = SDEV_RUNNING;
561 sysfs_notify(&sdev->sdev_gendev.kobj,
562 NULL, "state");
564 mutex_unlock(&sdev->state_mutex);
566 } else if (rport->state == SRP_RPORT_RUNNING) {
568 * srp_reconnect_rport() has been invoked with fast_io_fail
569 * and dev_loss off. Mark the port as failed and start the TL
570 * failure timers if these had not yet been started.
572 __rport_fail_io_fast(rport);
573 scsi_target_unblock(&shost->shost_gendev,
574 SDEV_TRANSPORT_OFFLINE);
575 __srp_start_tl_fail_timers(rport);
576 } else if (rport->state != SRP_RPORT_BLOCKED) {
577 scsi_target_unblock(&shost->shost_gendev,
578 SDEV_TRANSPORT_OFFLINE);
580 mutex_unlock(&rport->mutex);
582 out:
583 return res;
585 EXPORT_SYMBOL(srp_reconnect_rport);
588 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
589 * @scmd: SCSI command.
591 * If a timeout occurs while an rport is in the blocked state, ask the SCSI
592 * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
593 * handle the timeout (BLK_EH_NOT_HANDLED).
595 * Note: This function is called from soft-IRQ context and with the request
596 * queue lock held.
598 enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
600 struct scsi_device *sdev = scmd->device;
601 struct Scsi_Host *shost = sdev->host;
602 struct srp_internal *i = to_srp_internal(shost->transportt);
603 struct srp_rport *rport = shost_to_rport(shost);
605 pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
606 return rport->fast_io_fail_tmo < 0 && rport->dev_loss_tmo < 0 &&
607 i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
608 BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
610 EXPORT_SYMBOL(srp_timed_out);
612 static void srp_rport_release(struct device *dev)
614 struct srp_rport *rport = dev_to_rport(dev);
616 put_device(dev->parent);
617 kfree(rport);
620 static int scsi_is_srp_rport(const struct device *dev)
622 return dev->release == srp_rport_release;
625 static int srp_rport_match(struct attribute_container *cont,
626 struct device *dev)
628 struct Scsi_Host *shost;
629 struct srp_internal *i;
631 if (!scsi_is_srp_rport(dev))
632 return 0;
634 shost = dev_to_shost(dev->parent);
635 if (!shost->transportt)
636 return 0;
637 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
638 return 0;
640 i = to_srp_internal(shost->transportt);
641 return &i->rport_attr_cont.ac == cont;
644 static int srp_host_match(struct attribute_container *cont, struct device *dev)
646 struct Scsi_Host *shost;
647 struct srp_internal *i;
649 if (!scsi_is_host_device(dev))
650 return 0;
652 shost = dev_to_shost(dev);
653 if (!shost->transportt)
654 return 0;
655 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
656 return 0;
658 i = to_srp_internal(shost->transportt);
659 return &i->t.host_attrs.ac == cont;
663 * srp_rport_get() - increment rport reference count
664 * @rport: SRP target port.
666 void srp_rport_get(struct srp_rport *rport)
668 get_device(&rport->dev);
670 EXPORT_SYMBOL(srp_rport_get);
673 * srp_rport_put() - decrement rport reference count
674 * @rport: SRP target port.
676 void srp_rport_put(struct srp_rport *rport)
678 put_device(&rport->dev);
680 EXPORT_SYMBOL(srp_rport_put);
683 * srp_rport_add - add a SRP remote port to the device hierarchy
684 * @shost: scsi host the remote port is connected to.
685 * @ids: The port id for the remote port.
687 * Publishes a port to the rest of the system.
689 struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
690 struct srp_rport_identifiers *ids)
692 struct srp_rport *rport;
693 struct device *parent = &shost->shost_gendev;
694 struct srp_internal *i = to_srp_internal(shost->transportt);
695 int id, ret;
697 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
698 if (!rport)
699 return ERR_PTR(-ENOMEM);
701 mutex_init(&rport->mutex);
703 device_initialize(&rport->dev);
705 rport->dev.parent = get_device(parent);
706 rport->dev.release = srp_rport_release;
708 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
709 rport->roles = ids->roles;
711 if (i->f->reconnect)
712 rport->reconnect_delay = i->f->reconnect_delay ?
713 *i->f->reconnect_delay : 10;
714 INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
715 rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
716 *i->f->fast_io_fail_tmo : 15;
717 rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
718 INIT_DELAYED_WORK(&rport->fast_io_fail_work,
719 rport_fast_io_fail_timedout);
720 INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
722 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
723 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
725 transport_setup_device(&rport->dev);
727 ret = device_add(&rport->dev);
728 if (ret) {
729 transport_destroy_device(&rport->dev);
730 put_device(&rport->dev);
731 return ERR_PTR(ret);
734 transport_add_device(&rport->dev);
735 transport_configure_device(&rport->dev);
737 return rport;
739 EXPORT_SYMBOL_GPL(srp_rport_add);
742 * srp_rport_del - remove a SRP remote port
743 * @rport: SRP remote port to remove
745 * Removes the specified SRP remote port.
747 void srp_rport_del(struct srp_rport *rport)
749 struct device *dev = &rport->dev;
751 transport_remove_device(dev);
752 device_del(dev);
753 transport_destroy_device(dev);
755 put_device(dev);
757 EXPORT_SYMBOL_GPL(srp_rport_del);
759 static int do_srp_rport_del(struct device *dev, void *data)
761 if (scsi_is_srp_rport(dev))
762 srp_rport_del(dev_to_rport(dev));
763 return 0;
767 * srp_remove_host - tear down a Scsi_Host's SRP data structures
768 * @shost: Scsi Host that is torn down
770 * Removes all SRP remote ports for a given Scsi_Host.
771 * Must be called just before scsi_remove_host for SRP HBAs.
773 void srp_remove_host(struct Scsi_Host *shost)
775 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
777 EXPORT_SYMBOL_GPL(srp_remove_host);
780 * srp_stop_rport_timers - stop the transport layer recovery timers
781 * @rport: SRP remote port for which to stop the timers.
783 * Must be called after srp_remove_host() and scsi_remove_host(). The caller
784 * must hold a reference on the rport (rport->dev) and on the SCSI host
785 * (rport->dev.parent).
787 void srp_stop_rport_timers(struct srp_rport *rport)
789 mutex_lock(&rport->mutex);
790 if (rport->state == SRP_RPORT_BLOCKED)
791 __rport_fail_io_fast(rport);
792 srp_rport_set_state(rport, SRP_RPORT_LOST);
793 mutex_unlock(&rport->mutex);
795 cancel_delayed_work_sync(&rport->reconnect_work);
796 cancel_delayed_work_sync(&rport->fast_io_fail_work);
797 cancel_delayed_work_sync(&rport->dev_loss_work);
799 EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
802 * srp_attach_transport - instantiate SRP transport template
803 * @ft: SRP transport class function template
805 struct scsi_transport_template *
806 srp_attach_transport(struct srp_function_template *ft)
808 int count;
809 struct srp_internal *i;
811 i = kzalloc(sizeof(*i), GFP_KERNEL);
812 if (!i)
813 return NULL;
815 i->t.host_size = sizeof(struct srp_host_attrs);
816 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
817 i->t.host_attrs.ac.class = &srp_host_class.class;
818 i->t.host_attrs.ac.match = srp_host_match;
819 i->host_attrs[0] = NULL;
820 transport_container_register(&i->t.host_attrs);
822 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
823 i->rport_attr_cont.ac.class = &srp_rport_class.class;
824 i->rport_attr_cont.ac.match = srp_rport_match;
826 count = 0;
827 i->rport_attrs[count++] = &dev_attr_port_id;
828 i->rport_attrs[count++] = &dev_attr_roles;
829 if (ft->has_rport_state) {
830 i->rport_attrs[count++] = &dev_attr_state;
831 i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
832 i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
834 if (ft->reconnect) {
835 i->rport_attrs[count++] = &dev_attr_reconnect_delay;
836 i->rport_attrs[count++] = &dev_attr_failed_reconnects;
838 if (ft->rport_delete)
839 i->rport_attrs[count++] = &dev_attr_delete;
840 i->rport_attrs[count++] = NULL;
841 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
843 transport_container_register(&i->rport_attr_cont);
845 i->f = ft;
847 return &i->t;
849 EXPORT_SYMBOL_GPL(srp_attach_transport);
852 * srp_release_transport - release SRP transport template instance
853 * @t: transport template instance
855 void srp_release_transport(struct scsi_transport_template *t)
857 struct srp_internal *i = to_srp_internal(t);
859 transport_container_unregister(&i->t.host_attrs);
860 transport_container_unregister(&i->rport_attr_cont);
862 kfree(i);
864 EXPORT_SYMBOL_GPL(srp_release_transport);
866 static __init int srp_transport_init(void)
868 int ret;
870 ret = transport_class_register(&srp_host_class);
871 if (ret)
872 return ret;
873 ret = transport_class_register(&srp_rport_class);
874 if (ret)
875 goto unregister_host_class;
877 return 0;
878 unregister_host_class:
879 transport_class_unregister(&srp_host_class);
880 return ret;
883 static void __exit srp_transport_exit(void)
885 transport_class_unregister(&srp_host_class);
886 transport_class_unregister(&srp_rport_class);
889 MODULE_AUTHOR("FUJITA Tomonori");
890 MODULE_DESCRIPTION("SRP Transport Attributes");
891 MODULE_LICENSE("GPL");
893 module_init(srp_transport_init);
894 module_exit(srp_transport_exit);