i2c: acorn: is tristate and should use module.h
[linux-2.6/btrfs-unstable.git] / drivers / scsi / scsi_transport_srp.c
blob2700a5a09bd45a103b857b2f6a81da42834e4d28
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>
27 #include <linux/delay.h>
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 #include "scsi_transport_srp_internal.h"
38 struct srp_host_attrs {
39 atomic_t next_port_id;
41 #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data)
43 #define SRP_HOST_ATTRS 0
44 #define SRP_RPORT_ATTRS 8
46 struct srp_internal {
47 struct scsi_transport_template t;
48 struct srp_function_template *f;
50 struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
52 struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
53 struct transport_container rport_attr_cont;
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 /**
66 * srp_tmo_valid() - check timeout combination validity
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
70 * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT. Furthermore, these
71 * parameters must be such that multipath can detect failed paths timely.
72 * Hence do not allow all three parameters to be disabled simultaneously.
74 int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, int dev_loss_tmo)
76 if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
77 return -EINVAL;
78 if (reconnect_delay == 0)
79 return -EINVAL;
80 if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
81 return -EINVAL;
82 if (dev_loss_tmo >= LONG_MAX / HZ)
83 return -EINVAL;
84 if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
85 fast_io_fail_tmo >= dev_loss_tmo)
86 return -EINVAL;
87 return 0;
89 EXPORT_SYMBOL_GPL(srp_tmo_valid);
91 static int srp_host_setup(struct transport_container *tc, struct device *dev,
92 struct device *cdev)
94 struct Scsi_Host *shost = dev_to_shost(dev);
95 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
97 atomic_set(&srp_host->next_port_id, 0);
98 return 0;
101 static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
102 NULL, NULL);
104 static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
105 NULL, NULL, NULL);
107 #define SRP_PID(p) \
108 (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
109 (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
110 (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
111 (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
113 #define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
114 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
116 static ssize_t
117 show_srp_rport_id(struct device *dev, struct device_attribute *attr,
118 char *buf)
120 struct srp_rport *rport = transport_class_to_srp_rport(dev);
121 return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
124 static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
126 static const struct {
127 u32 value;
128 char *name;
129 } srp_rport_role_names[] = {
130 {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
131 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
134 static ssize_t
135 show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
136 char *buf)
138 struct srp_rport *rport = transport_class_to_srp_rport(dev);
139 int i;
140 char *name = NULL;
142 for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
143 if (srp_rport_role_names[i].value == rport->roles) {
144 name = srp_rport_role_names[i].name;
145 break;
147 return sprintf(buf, "%s\n", name ? : "unknown");
150 static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
152 static ssize_t store_srp_rport_delete(struct device *dev,
153 struct device_attribute *attr,
154 const char *buf, size_t count)
156 struct srp_rport *rport = transport_class_to_srp_rport(dev);
157 struct Scsi_Host *shost = dev_to_shost(dev);
158 struct srp_internal *i = to_srp_internal(shost->transportt);
160 if (i->f->rport_delete) {
161 i->f->rport_delete(rport);
162 return count;
163 } else {
164 return -ENOSYS;
168 static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
170 static ssize_t show_srp_rport_state(struct device *dev,
171 struct device_attribute *attr,
172 char *buf)
174 static const char *const state_name[] = {
175 [SRP_RPORT_RUNNING] = "running",
176 [SRP_RPORT_BLOCKED] = "blocked",
177 [SRP_RPORT_FAIL_FAST] = "fail-fast",
178 [SRP_RPORT_LOST] = "lost",
180 struct srp_rport *rport = transport_class_to_srp_rport(dev);
181 enum srp_rport_state state = rport->state;
183 return sprintf(buf, "%s\n",
184 (unsigned)state < ARRAY_SIZE(state_name) ?
185 state_name[state] : "???");
188 static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
190 static ssize_t srp_show_tmo(char *buf, int tmo)
192 return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
195 static int srp_parse_tmo(int *tmo, const char *buf)
197 int res = 0;
199 if (strncmp(buf, "off", 3) != 0)
200 res = kstrtoint(buf, 0, tmo);
201 else
202 *tmo = -1;
204 return res;
207 static ssize_t show_reconnect_delay(struct device *dev,
208 struct device_attribute *attr, char *buf)
210 struct srp_rport *rport = transport_class_to_srp_rport(dev);
212 return srp_show_tmo(buf, rport->reconnect_delay);
215 static ssize_t store_reconnect_delay(struct device *dev,
216 struct device_attribute *attr,
217 const char *buf, const size_t count)
219 struct srp_rport *rport = transport_class_to_srp_rport(dev);
220 int res, delay;
222 res = srp_parse_tmo(&delay, buf);
223 if (res)
224 goto out;
225 res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
226 rport->dev_loss_tmo);
227 if (res)
228 goto out;
230 if (rport->reconnect_delay <= 0 && delay > 0 &&
231 rport->state != SRP_RPORT_RUNNING) {
232 queue_delayed_work(system_long_wq, &rport->reconnect_work,
233 delay * HZ);
234 } else if (delay <= 0) {
235 cancel_delayed_work(&rport->reconnect_work);
237 rport->reconnect_delay = delay;
238 res = count;
240 out:
241 return res;
244 static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
245 store_reconnect_delay);
247 static ssize_t show_failed_reconnects(struct device *dev,
248 struct device_attribute *attr, char *buf)
250 struct srp_rport *rport = transport_class_to_srp_rport(dev);
252 return sprintf(buf, "%d\n", rport->failed_reconnects);
255 static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
257 static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
258 struct device_attribute *attr,
259 char *buf)
261 struct srp_rport *rport = transport_class_to_srp_rport(dev);
263 return srp_show_tmo(buf, rport->fast_io_fail_tmo);
266 static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
267 struct device_attribute *attr,
268 const char *buf, size_t count)
270 struct srp_rport *rport = transport_class_to_srp_rport(dev);
271 int res;
272 int fast_io_fail_tmo;
274 res = srp_parse_tmo(&fast_io_fail_tmo, buf);
275 if (res)
276 goto out;
277 res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
278 rport->dev_loss_tmo);
279 if (res)
280 goto out;
281 rport->fast_io_fail_tmo = fast_io_fail_tmo;
282 res = count;
284 out:
285 return res;
288 static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
289 show_srp_rport_fast_io_fail_tmo,
290 store_srp_rport_fast_io_fail_tmo);
292 static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
293 struct device_attribute *attr,
294 char *buf)
296 struct srp_rport *rport = transport_class_to_srp_rport(dev);
298 return srp_show_tmo(buf, rport->dev_loss_tmo);
301 static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
305 struct srp_rport *rport = transport_class_to_srp_rport(dev);
306 int res;
307 int dev_loss_tmo;
309 res = srp_parse_tmo(&dev_loss_tmo, buf);
310 if (res)
311 goto out;
312 res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
313 dev_loss_tmo);
314 if (res)
315 goto out;
316 rport->dev_loss_tmo = dev_loss_tmo;
317 res = count;
319 out:
320 return res;
323 static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
324 show_srp_rport_dev_loss_tmo,
325 store_srp_rport_dev_loss_tmo);
327 static int srp_rport_set_state(struct srp_rport *rport,
328 enum srp_rport_state new_state)
330 enum srp_rport_state old_state = rport->state;
332 lockdep_assert_held(&rport->mutex);
334 switch (new_state) {
335 case SRP_RPORT_RUNNING:
336 switch (old_state) {
337 case SRP_RPORT_LOST:
338 goto invalid;
339 default:
340 break;
342 break;
343 case SRP_RPORT_BLOCKED:
344 switch (old_state) {
345 case SRP_RPORT_RUNNING:
346 break;
347 default:
348 goto invalid;
350 break;
351 case SRP_RPORT_FAIL_FAST:
352 switch (old_state) {
353 case SRP_RPORT_LOST:
354 goto invalid;
355 default:
356 break;
358 break;
359 case SRP_RPORT_LOST:
360 break;
362 rport->state = new_state;
363 return 0;
365 invalid:
366 return -EINVAL;
370 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
372 static void srp_reconnect_work(struct work_struct *work)
374 struct srp_rport *rport = container_of(to_delayed_work(work),
375 struct srp_rport, reconnect_work);
376 struct Scsi_Host *shost = rport_to_shost(rport);
377 int delay, res;
379 res = srp_reconnect_rport(rport);
380 if (res != 0) {
381 shost_printk(KERN_ERR, shost,
382 "reconnect attempt %d failed (%d)\n",
383 ++rport->failed_reconnects, res);
384 delay = rport->reconnect_delay *
385 min(100, max(1, rport->failed_reconnects - 10));
386 if (delay > 0)
387 queue_delayed_work(system_long_wq,
388 &rport->reconnect_work, delay * HZ);
392 static void __rport_fail_io_fast(struct srp_rport *rport)
394 struct Scsi_Host *shost = rport_to_shost(rport);
395 struct srp_internal *i;
397 lockdep_assert_held(&rport->mutex);
399 if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
400 return;
401 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
403 /* Involve the LLD if possible to terminate all I/O on the rport. */
404 i = to_srp_internal(shost->transportt);
405 if (i->f->terminate_rport_io)
406 i->f->terminate_rport_io(rport);
410 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
412 static void rport_fast_io_fail_timedout(struct work_struct *work)
414 struct srp_rport *rport = container_of(to_delayed_work(work),
415 struct srp_rport, fast_io_fail_work);
416 struct Scsi_Host *shost = rport_to_shost(rport);
418 pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
419 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
421 mutex_lock(&rport->mutex);
422 if (rport->state == SRP_RPORT_BLOCKED)
423 __rport_fail_io_fast(rport);
424 mutex_unlock(&rport->mutex);
428 * rport_dev_loss_timedout() - device loss timeout handler
430 static void rport_dev_loss_timedout(struct work_struct *work)
432 struct srp_rport *rport = container_of(to_delayed_work(work),
433 struct srp_rport, dev_loss_work);
434 struct Scsi_Host *shost = rport_to_shost(rport);
435 struct srp_internal *i = to_srp_internal(shost->transportt);
437 pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
438 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
440 mutex_lock(&rport->mutex);
441 WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
442 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
443 mutex_unlock(&rport->mutex);
445 i->f->rport_delete(rport);
448 static void __srp_start_tl_fail_timers(struct srp_rport *rport)
450 struct Scsi_Host *shost = rport_to_shost(rport);
451 int delay, fast_io_fail_tmo, dev_loss_tmo;
453 lockdep_assert_held(&rport->mutex);
455 if (!rport->deleted) {
456 delay = rport->reconnect_delay;
457 fast_io_fail_tmo = rport->fast_io_fail_tmo;
458 dev_loss_tmo = rport->dev_loss_tmo;
459 pr_debug("%s current state: %d\n",
460 dev_name(&shost->shost_gendev), rport->state);
462 if (delay > 0)
463 queue_delayed_work(system_long_wq,
464 &rport->reconnect_work,
465 1UL * delay * HZ);
466 if (fast_io_fail_tmo >= 0 &&
467 srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
468 pr_debug("%s new state: %d\n",
469 dev_name(&shost->shost_gendev),
470 rport->state);
471 scsi_target_block(&shost->shost_gendev);
472 queue_delayed_work(system_long_wq,
473 &rport->fast_io_fail_work,
474 1UL * fast_io_fail_tmo * HZ);
476 if (dev_loss_tmo >= 0)
477 queue_delayed_work(system_long_wq,
478 &rport->dev_loss_work,
479 1UL * dev_loss_tmo * HZ);
480 } else {
481 pr_debug("%s has already been deleted\n",
482 dev_name(&shost->shost_gendev));
483 srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST);
484 scsi_target_unblock(&shost->shost_gendev,
485 SDEV_TRANSPORT_OFFLINE);
490 * srp_start_tl_fail_timers() - start the transport layer failure timers
492 * Start the transport layer fast I/O failure and device loss timers. Do not
493 * modify a timer that was already started.
495 void srp_start_tl_fail_timers(struct srp_rport *rport)
497 mutex_lock(&rport->mutex);
498 __srp_start_tl_fail_timers(rport);
499 mutex_unlock(&rport->mutex);
501 EXPORT_SYMBOL(srp_start_tl_fail_timers);
504 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
506 static int scsi_request_fn_active(struct Scsi_Host *shost)
508 struct scsi_device *sdev;
509 struct request_queue *q;
510 int request_fn_active = 0;
512 shost_for_each_device(sdev, shost) {
513 q = sdev->request_queue;
515 spin_lock_irq(q->queue_lock);
516 request_fn_active += q->request_fn_active;
517 spin_unlock_irq(q->queue_lock);
520 return request_fn_active;
524 * srp_reconnect_rport() - reconnect to an SRP target port
526 * Blocks SCSI command queueing before invoking reconnect() such that
527 * queuecommand() won't be invoked concurrently with reconnect() from outside
528 * the SCSI EH. This is important since a reconnect() implementation may
529 * reallocate resources needed by queuecommand().
531 * Notes:
532 * - This function neither waits until outstanding requests have finished nor
533 * tries to abort these. It is the responsibility of the reconnect()
534 * function to finish outstanding commands before reconnecting to the target
535 * port.
536 * - It is the responsibility of the caller to ensure that the resources
537 * reallocated by the reconnect() function won't be used while this function
538 * is in progress. One possible strategy is to invoke this function from
539 * the context of the SCSI EH thread only. Another possible strategy is to
540 * lock the rport mutex inside each SCSI LLD callback that can be invoked by
541 * the SCSI EH (the scsi_host_template.eh_*() functions and also the
542 * scsi_host_template.queuecommand() function).
544 int srp_reconnect_rport(struct srp_rport *rport)
546 struct Scsi_Host *shost = rport_to_shost(rport);
547 struct srp_internal *i = to_srp_internal(shost->transportt);
548 struct scsi_device *sdev;
549 int res;
551 pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
553 res = mutex_lock_interruptible(&rport->mutex);
554 if (res)
555 goto out;
556 scsi_target_block(&shost->shost_gendev);
557 while (scsi_request_fn_active(shost))
558 msleep(20);
559 res = i->f->reconnect(rport);
560 pr_debug("%s (state %d): transport.reconnect() returned %d\n",
561 dev_name(&shost->shost_gendev), rport->state, res);
562 if (res == 0) {
563 cancel_delayed_work(&rport->fast_io_fail_work);
564 cancel_delayed_work(&rport->dev_loss_work);
566 rport->failed_reconnects = 0;
567 srp_rport_set_state(rport, SRP_RPORT_RUNNING);
568 scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
570 * If the SCSI error handler has offlined one or more devices,
571 * invoking scsi_target_unblock() won't change the state of
572 * these devices into running so do that explicitly.
574 spin_lock_irq(shost->host_lock);
575 __shost_for_each_device(sdev, shost)
576 if (sdev->sdev_state == SDEV_OFFLINE)
577 sdev->sdev_state = SDEV_RUNNING;
578 spin_unlock_irq(shost->host_lock);
579 } else if (rport->state == SRP_RPORT_RUNNING) {
581 * srp_reconnect_rport() was invoked with fast_io_fail
582 * off. Mark the port as failed and start the TL failure
583 * timers if these had not yet been started.
585 __rport_fail_io_fast(rport);
586 scsi_target_unblock(&shost->shost_gendev,
587 SDEV_TRANSPORT_OFFLINE);
588 __srp_start_tl_fail_timers(rport);
589 } else if (rport->state != SRP_RPORT_BLOCKED) {
590 scsi_target_unblock(&shost->shost_gendev,
591 SDEV_TRANSPORT_OFFLINE);
593 mutex_unlock(&rport->mutex);
595 out:
596 return res;
598 EXPORT_SYMBOL(srp_reconnect_rport);
601 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
603 * If a timeout occurs while an rport is in the blocked state, ask the SCSI
604 * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
605 * handle the timeout (BLK_EH_NOT_HANDLED).
607 * Note: This function is called from soft-IRQ context and with the request
608 * queue lock held.
610 static enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
612 struct scsi_device *sdev = scmd->device;
613 struct Scsi_Host *shost = sdev->host;
614 struct srp_internal *i = to_srp_internal(shost->transportt);
616 pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
617 return i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
618 BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
621 static void srp_rport_release(struct device *dev)
623 struct srp_rport *rport = dev_to_rport(dev);
625 cancel_delayed_work_sync(&rport->reconnect_work);
626 cancel_delayed_work_sync(&rport->fast_io_fail_work);
627 cancel_delayed_work_sync(&rport->dev_loss_work);
629 put_device(dev->parent);
630 kfree(rport);
633 static int scsi_is_srp_rport(const struct device *dev)
635 return dev->release == srp_rport_release;
638 static int srp_rport_match(struct attribute_container *cont,
639 struct device *dev)
641 struct Scsi_Host *shost;
642 struct srp_internal *i;
644 if (!scsi_is_srp_rport(dev))
645 return 0;
647 shost = dev_to_shost(dev->parent);
648 if (!shost->transportt)
649 return 0;
650 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
651 return 0;
653 i = to_srp_internal(shost->transportt);
654 return &i->rport_attr_cont.ac == cont;
657 static int srp_host_match(struct attribute_container *cont, struct device *dev)
659 struct Scsi_Host *shost;
660 struct srp_internal *i;
662 if (!scsi_is_host_device(dev))
663 return 0;
665 shost = dev_to_shost(dev);
666 if (!shost->transportt)
667 return 0;
668 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
669 return 0;
671 i = to_srp_internal(shost->transportt);
672 return &i->t.host_attrs.ac == cont;
676 * srp_rport_get() - increment rport reference count
678 void srp_rport_get(struct srp_rport *rport)
680 get_device(&rport->dev);
682 EXPORT_SYMBOL(srp_rport_get);
685 * srp_rport_put() - decrement rport reference count
687 void srp_rport_put(struct srp_rport *rport)
689 put_device(&rport->dev);
691 EXPORT_SYMBOL(srp_rport_put);
694 * srp_rport_add - add a SRP remote port to the device hierarchy
695 * @shost: scsi host the remote port is connected to.
696 * @ids: The port id for the remote port.
698 * Publishes a port to the rest of the system.
700 struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
701 struct srp_rport_identifiers *ids)
703 struct srp_rport *rport;
704 struct device *parent = &shost->shost_gendev;
705 struct srp_internal *i = to_srp_internal(shost->transportt);
706 int id, ret;
708 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
709 if (!rport)
710 return ERR_PTR(-ENOMEM);
712 mutex_init(&rport->mutex);
714 device_initialize(&rport->dev);
716 rport->dev.parent = get_device(parent);
717 rport->dev.release = srp_rport_release;
719 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
720 rport->roles = ids->roles;
722 if (i->f->reconnect)
723 rport->reconnect_delay = i->f->reconnect_delay ?
724 *i->f->reconnect_delay : 10;
725 INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
726 rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
727 *i->f->fast_io_fail_tmo : 15;
728 rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
729 INIT_DELAYED_WORK(&rport->fast_io_fail_work,
730 rport_fast_io_fail_timedout);
731 INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
733 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
734 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
736 transport_setup_device(&rport->dev);
738 ret = device_add(&rport->dev);
739 if (ret) {
740 transport_destroy_device(&rport->dev);
741 put_device(&rport->dev);
742 return ERR_PTR(ret);
745 if (shost->active_mode & MODE_TARGET &&
746 ids->roles == SRP_RPORT_ROLE_INITIATOR) {
747 ret = srp_tgt_it_nexus_create(shost, (unsigned long)rport,
748 rport->port_id);
749 if (ret) {
750 device_del(&rport->dev);
751 transport_destroy_device(&rport->dev);
752 put_device(&rport->dev);
753 return ERR_PTR(ret);
757 transport_add_device(&rport->dev);
758 transport_configure_device(&rport->dev);
760 return rport;
762 EXPORT_SYMBOL_GPL(srp_rport_add);
765 * srp_rport_del - remove a SRP remote port
766 * @rport: SRP remote port to remove
768 * Removes the specified SRP remote port.
770 void srp_rport_del(struct srp_rport *rport)
772 struct device *dev = &rport->dev;
773 struct Scsi_Host *shost = dev_to_shost(dev->parent);
775 if (shost->active_mode & MODE_TARGET &&
776 rport->roles == SRP_RPORT_ROLE_INITIATOR)
777 srp_tgt_it_nexus_destroy(shost, (unsigned long)rport);
779 transport_remove_device(dev);
780 device_del(dev);
781 transport_destroy_device(dev);
783 mutex_lock(&rport->mutex);
784 if (rport->state == SRP_RPORT_BLOCKED)
785 __rport_fail_io_fast(rport);
786 rport->deleted = true;
787 mutex_unlock(&rport->mutex);
789 put_device(dev);
791 EXPORT_SYMBOL_GPL(srp_rport_del);
793 static int do_srp_rport_del(struct device *dev, void *data)
795 if (scsi_is_srp_rport(dev))
796 srp_rport_del(dev_to_rport(dev));
797 return 0;
801 * srp_remove_host - tear down a Scsi_Host's SRP data structures
802 * @shost: Scsi Host that is torn down
804 * Removes all SRP remote ports for a given Scsi_Host.
805 * Must be called just before scsi_remove_host for SRP HBAs.
807 void srp_remove_host(struct Scsi_Host *shost)
809 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
811 EXPORT_SYMBOL_GPL(srp_remove_host);
813 static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
814 int result)
816 struct srp_internal *i = to_srp_internal(shost->transportt);
817 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
820 static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
822 struct srp_internal *i = to_srp_internal(shost->transportt);
823 return i->f->it_nexus_response(shost, nexus, result);
827 * srp_attach_transport - instantiate SRP transport template
828 * @ft: SRP transport class function template
830 struct scsi_transport_template *
831 srp_attach_transport(struct srp_function_template *ft)
833 int count;
834 struct srp_internal *i;
836 i = kzalloc(sizeof(*i), GFP_KERNEL);
837 if (!i)
838 return NULL;
840 i->t.eh_timed_out = srp_timed_out;
842 i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
843 i->t.it_nexus_response = srp_it_nexus_response;
845 i->t.host_size = sizeof(struct srp_host_attrs);
846 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
847 i->t.host_attrs.ac.class = &srp_host_class.class;
848 i->t.host_attrs.ac.match = srp_host_match;
849 i->host_attrs[0] = NULL;
850 transport_container_register(&i->t.host_attrs);
852 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
853 i->rport_attr_cont.ac.class = &srp_rport_class.class;
854 i->rport_attr_cont.ac.match = srp_rport_match;
856 count = 0;
857 i->rport_attrs[count++] = &dev_attr_port_id;
858 i->rport_attrs[count++] = &dev_attr_roles;
859 if (ft->has_rport_state) {
860 i->rport_attrs[count++] = &dev_attr_state;
861 i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
862 i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
864 if (ft->reconnect) {
865 i->rport_attrs[count++] = &dev_attr_reconnect_delay;
866 i->rport_attrs[count++] = &dev_attr_failed_reconnects;
868 if (ft->rport_delete)
869 i->rport_attrs[count++] = &dev_attr_delete;
870 i->rport_attrs[count++] = NULL;
871 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
873 transport_container_register(&i->rport_attr_cont);
875 i->f = ft;
877 return &i->t;
879 EXPORT_SYMBOL_GPL(srp_attach_transport);
882 * srp_release_transport - release SRP transport template instance
883 * @t: transport template instance
885 void srp_release_transport(struct scsi_transport_template *t)
887 struct srp_internal *i = to_srp_internal(t);
889 transport_container_unregister(&i->t.host_attrs);
890 transport_container_unregister(&i->rport_attr_cont);
892 kfree(i);
894 EXPORT_SYMBOL_GPL(srp_release_transport);
896 static __init int srp_transport_init(void)
898 int ret;
900 ret = transport_class_register(&srp_host_class);
901 if (ret)
902 return ret;
903 ret = transport_class_register(&srp_rport_class);
904 if (ret)
905 goto unregister_host_class;
907 return 0;
908 unregister_host_class:
909 transport_class_unregister(&srp_host_class);
910 return ret;
913 static void __exit srp_transport_exit(void)
915 transport_class_unregister(&srp_host_class);
916 transport_class_unregister(&srp_rport_class);
919 MODULE_AUTHOR("FUJITA Tomonori");
920 MODULE_DESCRIPTION("SRP Transport Attributes");
921 MODULE_LICENSE("GPL");
923 module_init(srp_transport_init);
924 module_exit(srp_transport_exit);