ARM: ep93xx: move timer to its own file
[linux/fpc-iii.git] / drivers / scsi / scsi_transport_srp.c
bloba85292b1d09d090261f89f2f6eed018f6277abb9
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"
37 struct srp_host_attrs {
38 atomic_t next_port_id;
40 #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data)
42 #define SRP_HOST_ATTRS 0
43 #define SRP_RPORT_ATTRS 8
45 struct srp_internal {
46 struct scsi_transport_template t;
47 struct srp_function_template *f;
49 struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
51 struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
52 struct transport_container rport_attr_cont;
55 #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
57 #define dev_to_rport(d) container_of(d, struct srp_rport, dev)
58 #define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
59 static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r)
61 return dev_to_shost(r->dev.parent);
64 static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost)
66 return transport_class_to_srp_rport(&shost->shost_gendev);
69 /**
70 * srp_tmo_valid() - check timeout combination validity
71 * @reconnect_delay: Reconnect delay in seconds.
72 * @fast_io_fail_tmo: Fast I/O fail timeout in seconds.
73 * @dev_loss_tmo: Device loss timeout in seconds.
75 * The combination of the timeout parameters must be such that SCSI commands
76 * are finished in a reasonable time. Hence do not allow the fast I/O fail
77 * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to
78 * exceed that limit if failing I/O fast has been disabled. Furthermore, these
79 * parameters must be such that multipath can detect failed paths timely.
80 * Hence do not allow all three parameters to be disabled simultaneously.
82 int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, int dev_loss_tmo)
84 if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
85 return -EINVAL;
86 if (reconnect_delay == 0)
87 return -EINVAL;
88 if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
89 return -EINVAL;
90 if (fast_io_fail_tmo < 0 &&
91 dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
92 return -EINVAL;
93 if (dev_loss_tmo >= LONG_MAX / HZ)
94 return -EINVAL;
95 if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
96 fast_io_fail_tmo >= dev_loss_tmo)
97 return -EINVAL;
98 return 0;
100 EXPORT_SYMBOL_GPL(srp_tmo_valid);
102 static int srp_host_setup(struct transport_container *tc, struct device *dev,
103 struct device *cdev)
105 struct Scsi_Host *shost = dev_to_shost(dev);
106 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
108 atomic_set(&srp_host->next_port_id, 0);
109 return 0;
112 static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
113 NULL, NULL);
115 static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
116 NULL, NULL, NULL);
118 #define SRP_PID(p) \
119 (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
120 (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
121 (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
122 (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
124 #define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
125 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
127 static ssize_t
128 show_srp_rport_id(struct device *dev, struct device_attribute *attr,
129 char *buf)
131 struct srp_rport *rport = transport_class_to_srp_rport(dev);
132 return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
135 static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
137 static const struct {
138 u32 value;
139 char *name;
140 } srp_rport_role_names[] = {
141 {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
142 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
145 static ssize_t
146 show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
147 char *buf)
149 struct srp_rport *rport = transport_class_to_srp_rport(dev);
150 int i;
151 char *name = NULL;
153 for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
154 if (srp_rport_role_names[i].value == rport->roles) {
155 name = srp_rport_role_names[i].name;
156 break;
158 return sprintf(buf, "%s\n", name ? : "unknown");
161 static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
163 static ssize_t store_srp_rport_delete(struct device *dev,
164 struct device_attribute *attr,
165 const char *buf, size_t count)
167 struct srp_rport *rport = transport_class_to_srp_rport(dev);
168 struct Scsi_Host *shost = dev_to_shost(dev);
169 struct srp_internal *i = to_srp_internal(shost->transportt);
171 if (i->f->rport_delete) {
172 i->f->rport_delete(rport);
173 return count;
174 } else {
175 return -ENOSYS;
179 static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
181 static ssize_t show_srp_rport_state(struct device *dev,
182 struct device_attribute *attr,
183 char *buf)
185 static const char *const state_name[] = {
186 [SRP_RPORT_RUNNING] = "running",
187 [SRP_RPORT_BLOCKED] = "blocked",
188 [SRP_RPORT_FAIL_FAST] = "fail-fast",
189 [SRP_RPORT_LOST] = "lost",
191 struct srp_rport *rport = transport_class_to_srp_rport(dev);
192 enum srp_rport_state state = rport->state;
194 return sprintf(buf, "%s\n",
195 (unsigned)state < ARRAY_SIZE(state_name) ?
196 state_name[state] : "???");
199 static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
201 static ssize_t srp_show_tmo(char *buf, int tmo)
203 return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
206 static int srp_parse_tmo(int *tmo, const char *buf)
208 int res = 0;
210 if (strncmp(buf, "off", 3) != 0)
211 res = kstrtoint(buf, 0, tmo);
212 else
213 *tmo = -1;
215 return res;
218 static ssize_t show_reconnect_delay(struct device *dev,
219 struct device_attribute *attr, char *buf)
221 struct srp_rport *rport = transport_class_to_srp_rport(dev);
223 return srp_show_tmo(buf, rport->reconnect_delay);
226 static ssize_t store_reconnect_delay(struct device *dev,
227 struct device_attribute *attr,
228 const char *buf, const size_t count)
230 struct srp_rport *rport = transport_class_to_srp_rport(dev);
231 int res, delay;
233 res = srp_parse_tmo(&delay, buf);
234 if (res)
235 goto out;
236 res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
237 rport->dev_loss_tmo);
238 if (res)
239 goto out;
241 if (rport->reconnect_delay <= 0 && delay > 0 &&
242 rport->state != SRP_RPORT_RUNNING) {
243 queue_delayed_work(system_long_wq, &rport->reconnect_work,
244 delay * HZ);
245 } else if (delay <= 0) {
246 cancel_delayed_work(&rport->reconnect_work);
248 rport->reconnect_delay = delay;
249 res = count;
251 out:
252 return res;
255 static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
256 store_reconnect_delay);
258 static ssize_t show_failed_reconnects(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 struct srp_rport *rport = transport_class_to_srp_rport(dev);
263 return sprintf(buf, "%d\n", rport->failed_reconnects);
266 static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
268 static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
269 struct device_attribute *attr,
270 char *buf)
272 struct srp_rport *rport = transport_class_to_srp_rport(dev);
274 return srp_show_tmo(buf, rport->fast_io_fail_tmo);
277 static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
278 struct device_attribute *attr,
279 const char *buf, size_t count)
281 struct srp_rport *rport = transport_class_to_srp_rport(dev);
282 int res;
283 int fast_io_fail_tmo;
285 res = srp_parse_tmo(&fast_io_fail_tmo, buf);
286 if (res)
287 goto out;
288 res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
289 rport->dev_loss_tmo);
290 if (res)
291 goto out;
292 rport->fast_io_fail_tmo = fast_io_fail_tmo;
293 res = count;
295 out:
296 return res;
299 static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
300 show_srp_rport_fast_io_fail_tmo,
301 store_srp_rport_fast_io_fail_tmo);
303 static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
304 struct device_attribute *attr,
305 char *buf)
307 struct srp_rport *rport = transport_class_to_srp_rport(dev);
309 return srp_show_tmo(buf, rport->dev_loss_tmo);
312 static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
313 struct device_attribute *attr,
314 const char *buf, size_t count)
316 struct srp_rport *rport = transport_class_to_srp_rport(dev);
317 int res;
318 int dev_loss_tmo;
320 res = srp_parse_tmo(&dev_loss_tmo, buf);
321 if (res)
322 goto out;
323 res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
324 dev_loss_tmo);
325 if (res)
326 goto out;
327 rport->dev_loss_tmo = dev_loss_tmo;
328 res = count;
330 out:
331 return res;
334 static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
335 show_srp_rport_dev_loss_tmo,
336 store_srp_rport_dev_loss_tmo);
338 static int srp_rport_set_state(struct srp_rport *rport,
339 enum srp_rport_state new_state)
341 enum srp_rport_state old_state = rport->state;
343 lockdep_assert_held(&rport->mutex);
345 switch (new_state) {
346 case SRP_RPORT_RUNNING:
347 switch (old_state) {
348 case SRP_RPORT_LOST:
349 goto invalid;
350 default:
351 break;
353 break;
354 case SRP_RPORT_BLOCKED:
355 switch (old_state) {
356 case SRP_RPORT_RUNNING:
357 break;
358 default:
359 goto invalid;
361 break;
362 case SRP_RPORT_FAIL_FAST:
363 switch (old_state) {
364 case SRP_RPORT_LOST:
365 goto invalid;
366 default:
367 break;
369 break;
370 case SRP_RPORT_LOST:
371 break;
373 rport->state = new_state;
374 return 0;
376 invalid:
377 return -EINVAL;
381 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
382 * @work: Work structure used for scheduling this operation.
384 static void srp_reconnect_work(struct work_struct *work)
386 struct srp_rport *rport = container_of(to_delayed_work(work),
387 struct srp_rport, reconnect_work);
388 struct Scsi_Host *shost = rport_to_shost(rport);
389 int delay, res;
391 res = srp_reconnect_rport(rport);
392 if (res != 0) {
393 shost_printk(KERN_ERR, shost,
394 "reconnect attempt %d failed (%d)\n",
395 ++rport->failed_reconnects, res);
396 delay = rport->reconnect_delay *
397 min(100, max(1, rport->failed_reconnects - 10));
398 if (delay > 0)
399 queue_delayed_work(system_long_wq,
400 &rport->reconnect_work, delay * HZ);
405 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
406 * @shost: SCSI host for which to count the number of scsi_request_fn() callers.
408 * To do: add support for scsi-mq in this function.
410 static int scsi_request_fn_active(struct Scsi_Host *shost)
412 struct scsi_device *sdev;
413 struct request_queue *q;
414 int request_fn_active = 0;
416 shost_for_each_device(sdev, shost) {
417 q = sdev->request_queue;
419 spin_lock_irq(q->queue_lock);
420 request_fn_active += q->request_fn_active;
421 spin_unlock_irq(q->queue_lock);
424 return request_fn_active;
427 /* Wait until ongoing shost->hostt->queuecommand() calls have finished. */
428 static void srp_wait_for_queuecommand(struct Scsi_Host *shost)
430 while (scsi_request_fn_active(shost))
431 msleep(20);
434 static void __rport_fail_io_fast(struct srp_rport *rport)
436 struct Scsi_Host *shost = rport_to_shost(rport);
437 struct srp_internal *i;
439 lockdep_assert_held(&rport->mutex);
441 if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
442 return;
443 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
445 /* Involve the LLD if possible to terminate all I/O on the rport. */
446 i = to_srp_internal(shost->transportt);
447 if (i->f->terminate_rport_io) {
448 srp_wait_for_queuecommand(shost);
449 i->f->terminate_rport_io(rport);
454 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
455 * @work: Work structure used for scheduling this operation.
457 static void rport_fast_io_fail_timedout(struct work_struct *work)
459 struct srp_rport *rport = container_of(to_delayed_work(work),
460 struct srp_rport, fast_io_fail_work);
461 struct Scsi_Host *shost = rport_to_shost(rport);
463 pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
464 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
466 mutex_lock(&rport->mutex);
467 if (rport->state == SRP_RPORT_BLOCKED)
468 __rport_fail_io_fast(rport);
469 mutex_unlock(&rport->mutex);
473 * rport_dev_loss_timedout() - device loss timeout handler
474 * @work: Work structure used for scheduling this operation.
476 static void rport_dev_loss_timedout(struct work_struct *work)
478 struct srp_rport *rport = container_of(to_delayed_work(work),
479 struct srp_rport, dev_loss_work);
480 struct Scsi_Host *shost = rport_to_shost(rport);
481 struct srp_internal *i = to_srp_internal(shost->transportt);
483 pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
484 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
486 mutex_lock(&rport->mutex);
487 WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
488 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
489 mutex_unlock(&rport->mutex);
491 i->f->rport_delete(rport);
494 static void __srp_start_tl_fail_timers(struct srp_rport *rport)
496 struct Scsi_Host *shost = rport_to_shost(rport);
497 int delay, fast_io_fail_tmo, dev_loss_tmo;
499 lockdep_assert_held(&rport->mutex);
501 delay = rport->reconnect_delay;
502 fast_io_fail_tmo = rport->fast_io_fail_tmo;
503 dev_loss_tmo = rport->dev_loss_tmo;
504 pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
505 rport->state);
507 if (rport->state == SRP_RPORT_LOST)
508 return;
509 if (delay > 0)
510 queue_delayed_work(system_long_wq, &rport->reconnect_work,
511 1UL * delay * HZ);
512 if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) &&
513 srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
514 pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
515 rport->state);
516 scsi_target_block(&shost->shost_gendev);
517 if (fast_io_fail_tmo >= 0)
518 queue_delayed_work(system_long_wq,
519 &rport->fast_io_fail_work,
520 1UL * fast_io_fail_tmo * HZ);
521 if (dev_loss_tmo >= 0)
522 queue_delayed_work(system_long_wq,
523 &rport->dev_loss_work,
524 1UL * dev_loss_tmo * HZ);
529 * srp_start_tl_fail_timers() - start the transport layer failure timers
530 * @rport: SRP target port.
532 * Start the transport layer fast I/O failure and device loss timers. Do not
533 * modify a timer that was already started.
535 void srp_start_tl_fail_timers(struct srp_rport *rport)
537 mutex_lock(&rport->mutex);
538 __srp_start_tl_fail_timers(rport);
539 mutex_unlock(&rport->mutex);
541 EXPORT_SYMBOL(srp_start_tl_fail_timers);
544 * srp_reconnect_rport() - reconnect to an SRP target port
545 * @rport: SRP target port.
547 * Blocks SCSI command queueing before invoking reconnect() such that
548 * queuecommand() won't be invoked concurrently with reconnect() from outside
549 * the SCSI EH. This is important since a reconnect() implementation may
550 * reallocate resources needed by queuecommand().
552 * Notes:
553 * - This function neither waits until outstanding requests have finished nor
554 * tries to abort these. It is the responsibility of the reconnect()
555 * function to finish outstanding commands before reconnecting to the target
556 * port.
557 * - It is the responsibility of the caller to ensure that the resources
558 * reallocated by the reconnect() function won't be used while this function
559 * is in progress. One possible strategy is to invoke this function from
560 * the context of the SCSI EH thread only. Another possible strategy is to
561 * lock the rport mutex inside each SCSI LLD callback that can be invoked by
562 * the SCSI EH (the scsi_host_template.eh_*() functions and also the
563 * scsi_host_template.queuecommand() function).
565 int srp_reconnect_rport(struct srp_rport *rport)
567 struct Scsi_Host *shost = rport_to_shost(rport);
568 struct srp_internal *i = to_srp_internal(shost->transportt);
569 struct scsi_device *sdev;
570 int res;
572 pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
574 res = mutex_lock_interruptible(&rport->mutex);
575 if (res)
576 goto out;
577 scsi_target_block(&shost->shost_gendev);
578 srp_wait_for_queuecommand(shost);
579 res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
580 pr_debug("%s (state %d): transport.reconnect() returned %d\n",
581 dev_name(&shost->shost_gendev), rport->state, res);
582 if (res == 0) {
583 cancel_delayed_work(&rport->fast_io_fail_work);
584 cancel_delayed_work(&rport->dev_loss_work);
586 rport->failed_reconnects = 0;
587 srp_rport_set_state(rport, SRP_RPORT_RUNNING);
588 scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
590 * If the SCSI error handler has offlined one or more devices,
591 * invoking scsi_target_unblock() won't change the state of
592 * these devices into running so do that explicitly.
594 spin_lock_irq(shost->host_lock);
595 __shost_for_each_device(sdev, shost)
596 if (sdev->sdev_state == SDEV_OFFLINE)
597 sdev->sdev_state = SDEV_RUNNING;
598 spin_unlock_irq(shost->host_lock);
599 } else if (rport->state == SRP_RPORT_RUNNING) {
601 * srp_reconnect_rport() has been invoked with fast_io_fail
602 * and dev_loss off. Mark the port as failed and start the TL
603 * failure timers if these had not yet been started.
605 __rport_fail_io_fast(rport);
606 scsi_target_unblock(&shost->shost_gendev,
607 SDEV_TRANSPORT_OFFLINE);
608 __srp_start_tl_fail_timers(rport);
609 } else if (rport->state != SRP_RPORT_BLOCKED) {
610 scsi_target_unblock(&shost->shost_gendev,
611 SDEV_TRANSPORT_OFFLINE);
613 mutex_unlock(&rport->mutex);
615 out:
616 return res;
618 EXPORT_SYMBOL(srp_reconnect_rport);
621 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
622 * @scmd: SCSI command.
624 * If a timeout occurs while an rport is in the blocked state, ask the SCSI
625 * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
626 * handle the timeout (BLK_EH_NOT_HANDLED).
628 * Note: This function is called from soft-IRQ context and with the request
629 * queue lock held.
631 static enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
633 struct scsi_device *sdev = scmd->device;
634 struct Scsi_Host *shost = sdev->host;
635 struct srp_internal *i = to_srp_internal(shost->transportt);
636 struct srp_rport *rport = shost_to_rport(shost);
638 pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
639 return rport->fast_io_fail_tmo < 0 && rport->dev_loss_tmo < 0 &&
640 i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
641 BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
644 static void srp_rport_release(struct device *dev)
646 struct srp_rport *rport = dev_to_rport(dev);
648 put_device(dev->parent);
649 kfree(rport);
652 static int scsi_is_srp_rport(const struct device *dev)
654 return dev->release == srp_rport_release;
657 static int srp_rport_match(struct attribute_container *cont,
658 struct device *dev)
660 struct Scsi_Host *shost;
661 struct srp_internal *i;
663 if (!scsi_is_srp_rport(dev))
664 return 0;
666 shost = dev_to_shost(dev->parent);
667 if (!shost->transportt)
668 return 0;
669 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
670 return 0;
672 i = to_srp_internal(shost->transportt);
673 return &i->rport_attr_cont.ac == cont;
676 static int srp_host_match(struct attribute_container *cont, struct device *dev)
678 struct Scsi_Host *shost;
679 struct srp_internal *i;
681 if (!scsi_is_host_device(dev))
682 return 0;
684 shost = dev_to_shost(dev);
685 if (!shost->transportt)
686 return 0;
687 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
688 return 0;
690 i = to_srp_internal(shost->transportt);
691 return &i->t.host_attrs.ac == cont;
695 * srp_rport_get() - increment rport reference count
696 * @rport: SRP target port.
698 void srp_rport_get(struct srp_rport *rport)
700 get_device(&rport->dev);
702 EXPORT_SYMBOL(srp_rport_get);
705 * srp_rport_put() - decrement rport reference count
706 * @rport: SRP target port.
708 void srp_rport_put(struct srp_rport *rport)
710 put_device(&rport->dev);
712 EXPORT_SYMBOL(srp_rport_put);
715 * srp_rport_add - add a SRP remote port to the device hierarchy
716 * @shost: scsi host the remote port is connected to.
717 * @ids: The port id for the remote port.
719 * Publishes a port to the rest of the system.
721 struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
722 struct srp_rport_identifiers *ids)
724 struct srp_rport *rport;
725 struct device *parent = &shost->shost_gendev;
726 struct srp_internal *i = to_srp_internal(shost->transportt);
727 int id, ret;
729 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
730 if (!rport)
731 return ERR_PTR(-ENOMEM);
733 mutex_init(&rport->mutex);
735 device_initialize(&rport->dev);
737 rport->dev.parent = get_device(parent);
738 rport->dev.release = srp_rport_release;
740 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
741 rport->roles = ids->roles;
743 if (i->f->reconnect)
744 rport->reconnect_delay = i->f->reconnect_delay ?
745 *i->f->reconnect_delay : 10;
746 INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
747 rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
748 *i->f->fast_io_fail_tmo : 15;
749 rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
750 INIT_DELAYED_WORK(&rport->fast_io_fail_work,
751 rport_fast_io_fail_timedout);
752 INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
754 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
755 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
757 transport_setup_device(&rport->dev);
759 ret = device_add(&rport->dev);
760 if (ret) {
761 transport_destroy_device(&rport->dev);
762 put_device(&rport->dev);
763 return ERR_PTR(ret);
766 transport_add_device(&rport->dev);
767 transport_configure_device(&rport->dev);
769 return rport;
771 EXPORT_SYMBOL_GPL(srp_rport_add);
774 * srp_rport_del - remove a SRP remote port
775 * @rport: SRP remote port to remove
777 * Removes the specified SRP remote port.
779 void srp_rport_del(struct srp_rport *rport)
781 struct device *dev = &rport->dev;
783 transport_remove_device(dev);
784 device_del(dev);
785 transport_destroy_device(dev);
787 put_device(dev);
789 EXPORT_SYMBOL_GPL(srp_rport_del);
791 static int do_srp_rport_del(struct device *dev, void *data)
793 if (scsi_is_srp_rport(dev))
794 srp_rport_del(dev_to_rport(dev));
795 return 0;
799 * srp_remove_host - tear down a Scsi_Host's SRP data structures
800 * @shost: Scsi Host that is torn down
802 * Removes all SRP remote ports for a given Scsi_Host.
803 * Must be called just before scsi_remove_host for SRP HBAs.
805 void srp_remove_host(struct Scsi_Host *shost)
807 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
809 EXPORT_SYMBOL_GPL(srp_remove_host);
812 * srp_stop_rport_timers - stop the transport layer recovery timers
813 * @rport: SRP remote port for which to stop the timers.
815 * Must be called after srp_remove_host() and scsi_remove_host(). The caller
816 * must hold a reference on the rport (rport->dev) and on the SCSI host
817 * (rport->dev.parent).
819 void srp_stop_rport_timers(struct srp_rport *rport)
821 mutex_lock(&rport->mutex);
822 if (rport->state == SRP_RPORT_BLOCKED)
823 __rport_fail_io_fast(rport);
824 srp_rport_set_state(rport, SRP_RPORT_LOST);
825 mutex_unlock(&rport->mutex);
827 cancel_delayed_work_sync(&rport->reconnect_work);
828 cancel_delayed_work_sync(&rport->fast_io_fail_work);
829 cancel_delayed_work_sync(&rport->dev_loss_work);
831 EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
833 static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
834 int result)
836 struct srp_internal *i = to_srp_internal(shost->transportt);
837 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
840 static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
842 struct srp_internal *i = to_srp_internal(shost->transportt);
843 return i->f->it_nexus_response(shost, nexus, result);
847 * srp_attach_transport - instantiate SRP transport template
848 * @ft: SRP transport class function template
850 struct scsi_transport_template *
851 srp_attach_transport(struct srp_function_template *ft)
853 int count;
854 struct srp_internal *i;
856 i = kzalloc(sizeof(*i), GFP_KERNEL);
857 if (!i)
858 return NULL;
860 i->t.eh_timed_out = srp_timed_out;
862 i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
863 i->t.it_nexus_response = srp_it_nexus_response;
865 i->t.host_size = sizeof(struct srp_host_attrs);
866 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
867 i->t.host_attrs.ac.class = &srp_host_class.class;
868 i->t.host_attrs.ac.match = srp_host_match;
869 i->host_attrs[0] = NULL;
870 transport_container_register(&i->t.host_attrs);
872 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
873 i->rport_attr_cont.ac.class = &srp_rport_class.class;
874 i->rport_attr_cont.ac.match = srp_rport_match;
876 count = 0;
877 i->rport_attrs[count++] = &dev_attr_port_id;
878 i->rport_attrs[count++] = &dev_attr_roles;
879 if (ft->has_rport_state) {
880 i->rport_attrs[count++] = &dev_attr_state;
881 i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
882 i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
884 if (ft->reconnect) {
885 i->rport_attrs[count++] = &dev_attr_reconnect_delay;
886 i->rport_attrs[count++] = &dev_attr_failed_reconnects;
888 if (ft->rport_delete)
889 i->rport_attrs[count++] = &dev_attr_delete;
890 i->rport_attrs[count++] = NULL;
891 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
893 transport_container_register(&i->rport_attr_cont);
895 i->f = ft;
897 return &i->t;
899 EXPORT_SYMBOL_GPL(srp_attach_transport);
902 * srp_release_transport - release SRP transport template instance
903 * @t: transport template instance
905 void srp_release_transport(struct scsi_transport_template *t)
907 struct srp_internal *i = to_srp_internal(t);
909 transport_container_unregister(&i->t.host_attrs);
910 transport_container_unregister(&i->rport_attr_cont);
912 kfree(i);
914 EXPORT_SYMBOL_GPL(srp_release_transport);
916 static __init int srp_transport_init(void)
918 int ret;
920 ret = transport_class_register(&srp_host_class);
921 if (ret)
922 return ret;
923 ret = transport_class_register(&srp_rport_class);
924 if (ret)
925 goto unregister_host_class;
927 return 0;
928 unregister_host_class:
929 transport_class_unregister(&srp_host_class);
930 return ret;
933 static void __exit srp_transport_exit(void)
935 transport_class_unregister(&srp_host_class);
936 transport_class_unregister(&srp_rport_class);
939 MODULE_AUTHOR("FUJITA Tomonori");
940 MODULE_DESCRIPTION("SRP Transport Attributes");
941 MODULE_LICENSE("GPL");
943 module_init(srp_transport_init);
944 module_exit(srp_transport_exit);