Linux 3.14.51
[linux/fpc-iii.git] / drivers / scsi / scsi_transport_srp.c
blobb85eaa0d75c2e56e4d2deffdd7077f347ad967c3
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
67 * @reconnect_delay: Reconnect delay in seconds.
68 * @fast_io_fail_tmo: Fast I/O fail timeout in seconds.
69 * @dev_loss_tmo: Device loss timeout in seconds.
71 * The combination of the timeout parameters must be such that SCSI commands
72 * are finished in a reasonable time. Hence do not allow the fast I/O fail
73 * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to
74 * exceed that limit if failing I/O fast has been disabled. Furthermore, these
75 * parameters must be such that multipath can detect failed paths timely.
76 * Hence do not allow all three parameters to be disabled simultaneously.
78 int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, int dev_loss_tmo)
80 if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
81 return -EINVAL;
82 if (reconnect_delay == 0)
83 return -EINVAL;
84 if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
85 return -EINVAL;
86 if (fast_io_fail_tmo < 0 &&
87 dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
88 return -EINVAL;
89 if (dev_loss_tmo >= LONG_MAX / HZ)
90 return -EINVAL;
91 if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
92 fast_io_fail_tmo >= dev_loss_tmo)
93 return -EINVAL;
94 return 0;
96 EXPORT_SYMBOL_GPL(srp_tmo_valid);
98 static int srp_host_setup(struct transport_container *tc, struct device *dev,
99 struct device *cdev)
101 struct Scsi_Host *shost = dev_to_shost(dev);
102 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
104 atomic_set(&srp_host->next_port_id, 0);
105 return 0;
108 static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
109 NULL, NULL);
111 static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
112 NULL, NULL, NULL);
114 #define SRP_PID(p) \
115 (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
116 (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
117 (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
118 (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
120 #define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
121 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
123 static ssize_t
124 show_srp_rport_id(struct device *dev, struct device_attribute *attr,
125 char *buf)
127 struct srp_rport *rport = transport_class_to_srp_rport(dev);
128 return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
131 static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
133 static const struct {
134 u32 value;
135 char *name;
136 } srp_rport_role_names[] = {
137 {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
138 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
141 static ssize_t
142 show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
143 char *buf)
145 struct srp_rport *rport = transport_class_to_srp_rport(dev);
146 int i;
147 char *name = NULL;
149 for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
150 if (srp_rport_role_names[i].value == rport->roles) {
151 name = srp_rport_role_names[i].name;
152 break;
154 return sprintf(buf, "%s\n", name ? : "unknown");
157 static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
159 static ssize_t store_srp_rport_delete(struct device *dev,
160 struct device_attribute *attr,
161 const char *buf, size_t count)
163 struct srp_rport *rport = transport_class_to_srp_rport(dev);
164 struct Scsi_Host *shost = dev_to_shost(dev);
165 struct srp_internal *i = to_srp_internal(shost->transportt);
167 if (i->f->rport_delete) {
168 i->f->rport_delete(rport);
169 return count;
170 } else {
171 return -ENOSYS;
175 static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
177 static ssize_t show_srp_rport_state(struct device *dev,
178 struct device_attribute *attr,
179 char *buf)
181 static const char *const state_name[] = {
182 [SRP_RPORT_RUNNING] = "running",
183 [SRP_RPORT_BLOCKED] = "blocked",
184 [SRP_RPORT_FAIL_FAST] = "fail-fast",
185 [SRP_RPORT_LOST] = "lost",
187 struct srp_rport *rport = transport_class_to_srp_rport(dev);
188 enum srp_rport_state state = rport->state;
190 return sprintf(buf, "%s\n",
191 (unsigned)state < ARRAY_SIZE(state_name) ?
192 state_name[state] : "???");
195 static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
197 static ssize_t srp_show_tmo(char *buf, int tmo)
199 return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
202 static int srp_parse_tmo(int *tmo, const char *buf)
204 int res = 0;
206 if (strncmp(buf, "off", 3) != 0)
207 res = kstrtoint(buf, 0, tmo);
208 else
209 *tmo = -1;
211 return res;
214 static ssize_t show_reconnect_delay(struct device *dev,
215 struct device_attribute *attr, char *buf)
217 struct srp_rport *rport = transport_class_to_srp_rport(dev);
219 return srp_show_tmo(buf, rport->reconnect_delay);
222 static ssize_t store_reconnect_delay(struct device *dev,
223 struct device_attribute *attr,
224 const char *buf, const size_t count)
226 struct srp_rport *rport = transport_class_to_srp_rport(dev);
227 int res, delay;
229 res = srp_parse_tmo(&delay, buf);
230 if (res)
231 goto out;
232 res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
233 rport->dev_loss_tmo);
234 if (res)
235 goto out;
237 if (rport->reconnect_delay <= 0 && delay > 0 &&
238 rport->state != SRP_RPORT_RUNNING) {
239 queue_delayed_work(system_long_wq, &rport->reconnect_work,
240 delay * HZ);
241 } else if (delay <= 0) {
242 cancel_delayed_work(&rport->reconnect_work);
244 rport->reconnect_delay = delay;
245 res = count;
247 out:
248 return res;
251 static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
252 store_reconnect_delay);
254 static ssize_t show_failed_reconnects(struct device *dev,
255 struct device_attribute *attr, char *buf)
257 struct srp_rport *rport = transport_class_to_srp_rport(dev);
259 return sprintf(buf, "%d\n", rport->failed_reconnects);
262 static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
264 static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
265 struct device_attribute *attr,
266 char *buf)
268 struct srp_rport *rport = transport_class_to_srp_rport(dev);
270 return srp_show_tmo(buf, rport->fast_io_fail_tmo);
273 static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
274 struct device_attribute *attr,
275 const char *buf, size_t count)
277 struct srp_rport *rport = transport_class_to_srp_rport(dev);
278 int res;
279 int fast_io_fail_tmo;
281 res = srp_parse_tmo(&fast_io_fail_tmo, buf);
282 if (res)
283 goto out;
284 res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
285 rport->dev_loss_tmo);
286 if (res)
287 goto out;
288 rport->fast_io_fail_tmo = fast_io_fail_tmo;
289 res = count;
291 out:
292 return res;
295 static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
296 show_srp_rport_fast_io_fail_tmo,
297 store_srp_rport_fast_io_fail_tmo);
299 static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
300 struct device_attribute *attr,
301 char *buf)
303 struct srp_rport *rport = transport_class_to_srp_rport(dev);
305 return srp_show_tmo(buf, rport->dev_loss_tmo);
308 static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
309 struct device_attribute *attr,
310 const char *buf, size_t count)
312 struct srp_rport *rport = transport_class_to_srp_rport(dev);
313 int res;
314 int dev_loss_tmo;
316 res = srp_parse_tmo(&dev_loss_tmo, buf);
317 if (res)
318 goto out;
319 res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
320 dev_loss_tmo);
321 if (res)
322 goto out;
323 rport->dev_loss_tmo = dev_loss_tmo;
324 res = count;
326 out:
327 return res;
330 static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
331 show_srp_rport_dev_loss_tmo,
332 store_srp_rport_dev_loss_tmo);
334 static int srp_rport_set_state(struct srp_rport *rport,
335 enum srp_rport_state new_state)
337 enum srp_rport_state old_state = rport->state;
339 lockdep_assert_held(&rport->mutex);
341 switch (new_state) {
342 case SRP_RPORT_RUNNING:
343 switch (old_state) {
344 case SRP_RPORT_LOST:
345 goto invalid;
346 default:
347 break;
349 break;
350 case SRP_RPORT_BLOCKED:
351 switch (old_state) {
352 case SRP_RPORT_RUNNING:
353 break;
354 default:
355 goto invalid;
357 break;
358 case SRP_RPORT_FAIL_FAST:
359 switch (old_state) {
360 case SRP_RPORT_LOST:
361 goto invalid;
362 default:
363 break;
365 break;
366 case SRP_RPORT_LOST:
367 break;
369 rport->state = new_state;
370 return 0;
372 invalid:
373 return -EINVAL;
377 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
378 * @work: Work structure used for scheduling this operation.
380 static void srp_reconnect_work(struct work_struct *work)
382 struct srp_rport *rport = container_of(to_delayed_work(work),
383 struct srp_rport, reconnect_work);
384 struct Scsi_Host *shost = rport_to_shost(rport);
385 int delay, res;
387 res = srp_reconnect_rport(rport);
388 if (res != 0) {
389 shost_printk(KERN_ERR, shost,
390 "reconnect attempt %d failed (%d)\n",
391 ++rport->failed_reconnects, res);
392 delay = rport->reconnect_delay *
393 min(100, max(1, rport->failed_reconnects - 10));
394 if (delay > 0)
395 queue_delayed_work(system_long_wq,
396 &rport->reconnect_work, delay * HZ);
401 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
402 * @shost: SCSI host for which to count the number of scsi_request_fn() callers.
404 * To do: add support for scsi-mq in this function.
406 static int scsi_request_fn_active(struct Scsi_Host *shost)
408 struct scsi_device *sdev;
409 struct request_queue *q;
410 int request_fn_active = 0;
412 shost_for_each_device(sdev, shost) {
413 q = sdev->request_queue;
415 spin_lock_irq(q->queue_lock);
416 request_fn_active += q->request_fn_active;
417 spin_unlock_irq(q->queue_lock);
420 return request_fn_active;
423 /* Wait until ongoing shost->hostt->queuecommand() calls have finished. */
424 static void srp_wait_for_queuecommand(struct Scsi_Host *shost)
426 while (scsi_request_fn_active(shost))
427 msleep(20);
430 static void __rport_fail_io_fast(struct srp_rport *rport)
432 struct Scsi_Host *shost = rport_to_shost(rport);
433 struct srp_internal *i;
435 lockdep_assert_held(&rport->mutex);
437 if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
438 return;
439 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
441 /* Involve the LLD if possible to terminate all I/O on the rport. */
442 i = to_srp_internal(shost->transportt);
443 if (i->f->terminate_rport_io) {
444 srp_wait_for_queuecommand(shost);
445 i->f->terminate_rport_io(rport);
450 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
451 * @work: Work structure used for scheduling this operation.
453 static void rport_fast_io_fail_timedout(struct work_struct *work)
455 struct srp_rport *rport = container_of(to_delayed_work(work),
456 struct srp_rport, fast_io_fail_work);
457 struct Scsi_Host *shost = rport_to_shost(rport);
459 pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
460 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
462 mutex_lock(&rport->mutex);
463 if (rport->state == SRP_RPORT_BLOCKED)
464 __rport_fail_io_fast(rport);
465 mutex_unlock(&rport->mutex);
469 * rport_dev_loss_timedout() - device loss timeout handler
470 * @work: Work structure used for scheduling this operation.
472 static void rport_dev_loss_timedout(struct work_struct *work)
474 struct srp_rport *rport = container_of(to_delayed_work(work),
475 struct srp_rport, dev_loss_work);
476 struct Scsi_Host *shost = rport_to_shost(rport);
477 struct srp_internal *i = to_srp_internal(shost->transportt);
479 pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
480 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
482 mutex_lock(&rport->mutex);
483 WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
484 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
485 mutex_unlock(&rport->mutex);
487 i->f->rport_delete(rport);
490 static void __srp_start_tl_fail_timers(struct srp_rport *rport)
492 struct Scsi_Host *shost = rport_to_shost(rport);
493 int delay, fast_io_fail_tmo, dev_loss_tmo;
495 lockdep_assert_held(&rport->mutex);
497 delay = rport->reconnect_delay;
498 fast_io_fail_tmo = rport->fast_io_fail_tmo;
499 dev_loss_tmo = rport->dev_loss_tmo;
500 pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
501 rport->state);
503 if (rport->state == SRP_RPORT_LOST)
504 return;
505 if (delay > 0)
506 queue_delayed_work(system_long_wq, &rport->reconnect_work,
507 1UL * delay * HZ);
508 if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) &&
509 srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
510 pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
511 rport->state);
512 scsi_target_block(&shost->shost_gendev);
513 if (fast_io_fail_tmo >= 0)
514 queue_delayed_work(system_long_wq,
515 &rport->fast_io_fail_work,
516 1UL * fast_io_fail_tmo * HZ);
517 if (dev_loss_tmo >= 0)
518 queue_delayed_work(system_long_wq,
519 &rport->dev_loss_work,
520 1UL * dev_loss_tmo * HZ);
525 * srp_start_tl_fail_timers() - start the transport layer failure timers
526 * @rport: SRP target port.
528 * Start the transport layer fast I/O failure and device loss timers. Do not
529 * modify a timer that was already started.
531 void srp_start_tl_fail_timers(struct srp_rport *rport)
533 mutex_lock(&rport->mutex);
534 __srp_start_tl_fail_timers(rport);
535 mutex_unlock(&rport->mutex);
537 EXPORT_SYMBOL(srp_start_tl_fail_timers);
540 * srp_reconnect_rport() - reconnect to an SRP target port
541 * @rport: SRP target port.
543 * Blocks SCSI command queueing before invoking reconnect() such that
544 * queuecommand() won't be invoked concurrently with reconnect() from outside
545 * the SCSI EH. This is important since a reconnect() implementation may
546 * reallocate resources needed by queuecommand().
548 * Notes:
549 * - This function neither waits until outstanding requests have finished nor
550 * tries to abort these. It is the responsibility of the reconnect()
551 * function to finish outstanding commands before reconnecting to the target
552 * port.
553 * - It is the responsibility of the caller to ensure that the resources
554 * reallocated by the reconnect() function won't be used while this function
555 * is in progress. One possible strategy is to invoke this function from
556 * the context of the SCSI EH thread only. Another possible strategy is to
557 * lock the rport mutex inside each SCSI LLD callback that can be invoked by
558 * the SCSI EH (the scsi_host_template.eh_*() functions and also the
559 * scsi_host_template.queuecommand() function).
561 int srp_reconnect_rport(struct srp_rport *rport)
563 struct Scsi_Host *shost = rport_to_shost(rport);
564 struct srp_internal *i = to_srp_internal(shost->transportt);
565 struct scsi_device *sdev;
566 int res;
568 pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
570 res = mutex_lock_interruptible(&rport->mutex);
571 if (res)
572 goto out;
573 scsi_target_block(&shost->shost_gendev);
574 srp_wait_for_queuecommand(shost);
575 res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
576 pr_debug("%s (state %d): transport.reconnect() returned %d\n",
577 dev_name(&shost->shost_gendev), rport->state, res);
578 if (res == 0) {
579 cancel_delayed_work(&rport->fast_io_fail_work);
580 cancel_delayed_work(&rport->dev_loss_work);
582 rport->failed_reconnects = 0;
583 srp_rport_set_state(rport, SRP_RPORT_RUNNING);
584 scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
586 * If the SCSI error handler has offlined one or more devices,
587 * invoking scsi_target_unblock() won't change the state of
588 * these devices into running so do that explicitly.
590 spin_lock_irq(shost->host_lock);
591 __shost_for_each_device(sdev, shost)
592 if (sdev->sdev_state == SDEV_OFFLINE)
593 sdev->sdev_state = SDEV_RUNNING;
594 spin_unlock_irq(shost->host_lock);
595 } else if (rport->state == SRP_RPORT_RUNNING) {
597 * srp_reconnect_rport() has been invoked with fast_io_fail
598 * and dev_loss off. Mark the port as failed and start the TL
599 * failure timers if these had not yet been started.
601 __rport_fail_io_fast(rport);
602 scsi_target_unblock(&shost->shost_gendev,
603 SDEV_TRANSPORT_OFFLINE);
604 __srp_start_tl_fail_timers(rport);
605 } else if (rport->state != SRP_RPORT_BLOCKED) {
606 scsi_target_unblock(&shost->shost_gendev,
607 SDEV_TRANSPORT_OFFLINE);
609 mutex_unlock(&rport->mutex);
611 out:
612 return res;
614 EXPORT_SYMBOL(srp_reconnect_rport);
617 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
618 * @scmd: SCSI command.
620 * If a timeout occurs while an rport is in the blocked state, ask the SCSI
621 * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
622 * handle the timeout (BLK_EH_NOT_HANDLED).
624 * Note: This function is called from soft-IRQ context and with the request
625 * queue lock held.
627 static enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
629 struct scsi_device *sdev = scmd->device;
630 struct Scsi_Host *shost = sdev->host;
631 struct srp_internal *i = to_srp_internal(shost->transportt);
633 pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
634 return i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
635 BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
638 static void srp_rport_release(struct device *dev)
640 struct srp_rport *rport = dev_to_rport(dev);
642 put_device(dev->parent);
643 kfree(rport);
646 static int scsi_is_srp_rport(const struct device *dev)
648 return dev->release == srp_rport_release;
651 static int srp_rport_match(struct attribute_container *cont,
652 struct device *dev)
654 struct Scsi_Host *shost;
655 struct srp_internal *i;
657 if (!scsi_is_srp_rport(dev))
658 return 0;
660 shost = dev_to_shost(dev->parent);
661 if (!shost->transportt)
662 return 0;
663 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
664 return 0;
666 i = to_srp_internal(shost->transportt);
667 return &i->rport_attr_cont.ac == cont;
670 static int srp_host_match(struct attribute_container *cont, struct device *dev)
672 struct Scsi_Host *shost;
673 struct srp_internal *i;
675 if (!scsi_is_host_device(dev))
676 return 0;
678 shost = dev_to_shost(dev);
679 if (!shost->transportt)
680 return 0;
681 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
682 return 0;
684 i = to_srp_internal(shost->transportt);
685 return &i->t.host_attrs.ac == cont;
689 * srp_rport_get() - increment rport reference count
690 * @rport: SRP target port.
692 void srp_rport_get(struct srp_rport *rport)
694 get_device(&rport->dev);
696 EXPORT_SYMBOL(srp_rport_get);
699 * srp_rport_put() - decrement rport reference count
700 * @rport: SRP target port.
702 void srp_rport_put(struct srp_rport *rport)
704 put_device(&rport->dev);
706 EXPORT_SYMBOL(srp_rport_put);
709 * srp_rport_add - add a SRP remote port to the device hierarchy
710 * @shost: scsi host the remote port is connected to.
711 * @ids: The port id for the remote port.
713 * Publishes a port to the rest of the system.
715 struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
716 struct srp_rport_identifiers *ids)
718 struct srp_rport *rport;
719 struct device *parent = &shost->shost_gendev;
720 struct srp_internal *i = to_srp_internal(shost->transportt);
721 int id, ret;
723 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
724 if (!rport)
725 return ERR_PTR(-ENOMEM);
727 mutex_init(&rport->mutex);
729 device_initialize(&rport->dev);
731 rport->dev.parent = get_device(parent);
732 rport->dev.release = srp_rport_release;
734 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
735 rport->roles = ids->roles;
737 if (i->f->reconnect)
738 rport->reconnect_delay = i->f->reconnect_delay ?
739 *i->f->reconnect_delay : 10;
740 INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
741 rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
742 *i->f->fast_io_fail_tmo : 15;
743 rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
744 INIT_DELAYED_WORK(&rport->fast_io_fail_work,
745 rport_fast_io_fail_timedout);
746 INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
748 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
749 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
751 transport_setup_device(&rport->dev);
753 ret = device_add(&rport->dev);
754 if (ret) {
755 transport_destroy_device(&rport->dev);
756 put_device(&rport->dev);
757 return ERR_PTR(ret);
760 if (shost->active_mode & MODE_TARGET &&
761 ids->roles == SRP_RPORT_ROLE_INITIATOR) {
762 ret = srp_tgt_it_nexus_create(shost, (unsigned long)rport,
763 rport->port_id);
764 if (ret) {
765 device_del(&rport->dev);
766 transport_destroy_device(&rport->dev);
767 put_device(&rport->dev);
768 return ERR_PTR(ret);
772 transport_add_device(&rport->dev);
773 transport_configure_device(&rport->dev);
775 return rport;
777 EXPORT_SYMBOL_GPL(srp_rport_add);
780 * srp_rport_del - remove a SRP remote port
781 * @rport: SRP remote port to remove
783 * Removes the specified SRP remote port.
785 void srp_rport_del(struct srp_rport *rport)
787 struct device *dev = &rport->dev;
788 struct Scsi_Host *shost = dev_to_shost(dev->parent);
790 if (shost->active_mode & MODE_TARGET &&
791 rport->roles == SRP_RPORT_ROLE_INITIATOR)
792 srp_tgt_it_nexus_destroy(shost, (unsigned long)rport);
794 transport_remove_device(dev);
795 device_del(dev);
796 transport_destroy_device(dev);
798 put_device(dev);
800 EXPORT_SYMBOL_GPL(srp_rport_del);
802 static int do_srp_rport_del(struct device *dev, void *data)
804 if (scsi_is_srp_rport(dev))
805 srp_rport_del(dev_to_rport(dev));
806 return 0;
810 * srp_remove_host - tear down a Scsi_Host's SRP data structures
811 * @shost: Scsi Host that is torn down
813 * Removes all SRP remote ports for a given Scsi_Host.
814 * Must be called just before scsi_remove_host for SRP HBAs.
816 void srp_remove_host(struct Scsi_Host *shost)
818 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
820 EXPORT_SYMBOL_GPL(srp_remove_host);
823 * srp_stop_rport_timers - stop the transport layer recovery timers
825 * Must be called after srp_remove_host() and scsi_remove_host(). The caller
826 * must hold a reference on the rport (rport->dev) and on the SCSI host
827 * (rport->dev.parent).
829 void srp_stop_rport_timers(struct srp_rport *rport)
831 mutex_lock(&rport->mutex);
832 if (rport->state == SRP_RPORT_BLOCKED)
833 __rport_fail_io_fast(rport);
834 srp_rport_set_state(rport, SRP_RPORT_LOST);
835 mutex_unlock(&rport->mutex);
837 cancel_delayed_work_sync(&rport->reconnect_work);
838 cancel_delayed_work_sync(&rport->fast_io_fail_work);
839 cancel_delayed_work_sync(&rport->dev_loss_work);
841 EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
843 static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
844 int result)
846 struct srp_internal *i = to_srp_internal(shost->transportt);
847 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
850 static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
852 struct srp_internal *i = to_srp_internal(shost->transportt);
853 return i->f->it_nexus_response(shost, nexus, result);
857 * srp_attach_transport - instantiate SRP transport template
858 * @ft: SRP transport class function template
860 struct scsi_transport_template *
861 srp_attach_transport(struct srp_function_template *ft)
863 int count;
864 struct srp_internal *i;
866 i = kzalloc(sizeof(*i), GFP_KERNEL);
867 if (!i)
868 return NULL;
870 i->t.eh_timed_out = srp_timed_out;
872 i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
873 i->t.it_nexus_response = srp_it_nexus_response;
875 i->t.host_size = sizeof(struct srp_host_attrs);
876 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
877 i->t.host_attrs.ac.class = &srp_host_class.class;
878 i->t.host_attrs.ac.match = srp_host_match;
879 i->host_attrs[0] = NULL;
880 transport_container_register(&i->t.host_attrs);
882 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
883 i->rport_attr_cont.ac.class = &srp_rport_class.class;
884 i->rport_attr_cont.ac.match = srp_rport_match;
886 count = 0;
887 i->rport_attrs[count++] = &dev_attr_port_id;
888 i->rport_attrs[count++] = &dev_attr_roles;
889 if (ft->has_rport_state) {
890 i->rport_attrs[count++] = &dev_attr_state;
891 i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
892 i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
894 if (ft->reconnect) {
895 i->rport_attrs[count++] = &dev_attr_reconnect_delay;
896 i->rport_attrs[count++] = &dev_attr_failed_reconnects;
898 if (ft->rport_delete)
899 i->rport_attrs[count++] = &dev_attr_delete;
900 i->rport_attrs[count++] = NULL;
901 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
903 transport_container_register(&i->rport_attr_cont);
905 i->f = ft;
907 return &i->t;
909 EXPORT_SYMBOL_GPL(srp_attach_transport);
912 * srp_release_transport - release SRP transport template instance
913 * @t: transport template instance
915 void srp_release_transport(struct scsi_transport_template *t)
917 struct srp_internal *i = to_srp_internal(t);
919 transport_container_unregister(&i->t.host_attrs);
920 transport_container_unregister(&i->rport_attr_cont);
922 kfree(i);
924 EXPORT_SYMBOL_GPL(srp_release_transport);
926 static __init int srp_transport_init(void)
928 int ret;
930 ret = transport_class_register(&srp_host_class);
931 if (ret)
932 return ret;
933 ret = transport_class_register(&srp_rport_class);
934 if (ret)
935 goto unregister_host_class;
937 return 0;
938 unregister_host_class:
939 transport_class_unregister(&srp_host_class);
940 return ret;
943 static void __exit srp_transport_exit(void)
945 transport_class_unregister(&srp_host_class);
946 transport_class_unregister(&srp_rport_class);
949 MODULE_AUTHOR("FUJITA Tomonori");
950 MODULE_DESCRIPTION("SRP Transport Attributes");
951 MODULE_LICENSE("GPL");
953 module_init(srp_transport_init);
954 module_exit(srp_transport_exit);