blob: a349d44c4c36b2478d5453664c18bb909eaeed76 [file] [log] [blame]
FUJITA Tomonori09345f62007-06-27 16:32:39 +09001/*
2 * SCSI RDMA (SRP) transport class
3 *
4 * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org>
5 *
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.
10 *
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.
15 *
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
20 */
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>
Bart Van Assche29c17322013-10-26 14:33:30 +020027#include <linux/delay.h>
FUJITA Tomonori09345f62007-06-27 16:32:39 +090028
29#include <scsi/scsi.h>
Bart Van Assche29c17322013-10-26 14:33:30 +020030#include <scsi/scsi_cmnd.h>
FUJITA Tomonori09345f62007-06-27 16:32:39 +090031#include <scsi/scsi_device.h>
32#include <scsi/scsi_host.h>
33#include <scsi/scsi_transport.h>
34#include <scsi/scsi_transport_srp.h>
Bart Van Assche29c17322013-10-26 14:33:30 +020035#include "scsi_priv.h"
FUJITA Tomonori0012fdf2007-08-02 00:20:34 +090036#include "scsi_transport_srp_internal.h"
FUJITA Tomonori09345f62007-06-27 16:32:39 +090037
38struct srp_host_attrs {
39 atomic_t next_port_id;
40};
41#define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data)
42
43#define SRP_HOST_ATTRS 0
Bart Van Assche8c64e452013-10-26 14:35:59 +020044#define SRP_RPORT_ATTRS 8
FUJITA Tomonori09345f62007-06-27 16:32:39 +090045
46struct srp_internal {
47 struct scsi_transport_template t;
48 struct srp_function_template *f;
49
Tony Jonesee959b02008-02-22 00:13:36 +010050 struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
FUJITA Tomonori09345f62007-06-27 16:32:39 +090051
Tony Jonesee959b02008-02-22 00:13:36 +010052 struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
FUJITA Tomonori09345f62007-06-27 16:32:39 +090053 struct transport_container rport_attr_cont;
54};
55
56#define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
57
58#define dev_to_rport(d) container_of(d, struct srp_rport, dev)
Tony Jonesee959b02008-02-22 00:13:36 +010059#define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
Bart Van Assche29c17322013-10-26 14:33:30 +020060static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r)
61{
62 return dev_to_shost(r->dev.parent);
63}
64
65/**
66 * srp_tmo_valid() - check timeout combination validity
67 *
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
Bart Van Assche18cc4e02013-12-11 17:05:22 +010070 * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to
71 * exceed that limit if failing I/O fast has been disabled. Furthermore, these
Bart Van Assche29c17322013-10-26 14:33:30 +020072 * parameters must be such that multipath can detect failed paths timely.
Bart Van Assche8c64e452013-10-26 14:35:59 +020073 * Hence do not allow all three parameters to be disabled simultaneously.
Bart Van Assche29c17322013-10-26 14:33:30 +020074 */
Bart Van Assche8c64e452013-10-26 14:35:59 +020075int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, int dev_loss_tmo)
Bart Van Assche29c17322013-10-26 14:33:30 +020076{
Bart Van Assche8c64e452013-10-26 14:35:59 +020077 if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
78 return -EINVAL;
79 if (reconnect_delay == 0)
Bart Van Assche29c17322013-10-26 14:33:30 +020080 return -EINVAL;
81 if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
82 return -EINVAL;
Bart Van Assche18cc4e02013-12-11 17:05:22 +010083 if (fast_io_fail_tmo < 0 &&
84 dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
85 return -EINVAL;
Bart Van Assche29c17322013-10-26 14:33:30 +020086 if (dev_loss_tmo >= LONG_MAX / HZ)
87 return -EINVAL;
88 if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
89 fast_io_fail_tmo >= dev_loss_tmo)
90 return -EINVAL;
91 return 0;
92}
93EXPORT_SYMBOL_GPL(srp_tmo_valid);
FUJITA Tomonori09345f62007-06-27 16:32:39 +090094
95static int srp_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +010096 struct device *cdev)
FUJITA Tomonori09345f62007-06-27 16:32:39 +090097{
98 struct Scsi_Host *shost = dev_to_shost(dev);
99 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
100
101 atomic_set(&srp_host->next_port_id, 0);
102 return 0;
103}
104
105static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
106 NULL, NULL);
107
108static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
109 NULL, NULL, NULL);
110
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900111#define SRP_PID(p) \
112 (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
113 (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
114 (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
115 (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
116
117#define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
118 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
119
120static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100121show_srp_rport_id(struct device *dev, struct device_attribute *attr,
122 char *buf)
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900123{
Tony Jonesee959b02008-02-22 00:13:36 +0100124 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900125 return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
126}
127
Tony Jonesee959b02008-02-22 00:13:36 +0100128static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900129
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900130static const struct {
131 u32 value;
132 char *name;
133} srp_rport_role_names[] = {
134 {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
135 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
136};
137
138static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100139show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
140 char *buf)
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900141{
Tony Jonesee959b02008-02-22 00:13:36 +0100142 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900143 int i;
144 char *name = NULL;
145
146 for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
147 if (srp_rport_role_names[i].value == rport->roles) {
148 name = srp_rport_role_names[i].name;
149 break;
150 }
151 return sprintf(buf, "%s\n", name ? : "unknown");
152}
153
Tony Jonesee959b02008-02-22 00:13:36 +0100154static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900155
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200156static ssize_t store_srp_rport_delete(struct device *dev,
157 struct device_attribute *attr,
158 const char *buf, size_t count)
159{
160 struct srp_rport *rport = transport_class_to_srp_rport(dev);
161 struct Scsi_Host *shost = dev_to_shost(dev);
162 struct srp_internal *i = to_srp_internal(shost->transportt);
163
164 if (i->f->rport_delete) {
165 i->f->rport_delete(rport);
166 return count;
167 } else {
168 return -ENOSYS;
169 }
170}
171
172static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
173
Bart Van Assche29c17322013-10-26 14:33:30 +0200174static ssize_t show_srp_rport_state(struct device *dev,
175 struct device_attribute *attr,
176 char *buf)
177{
178 static const char *const state_name[] = {
179 [SRP_RPORT_RUNNING] = "running",
180 [SRP_RPORT_BLOCKED] = "blocked",
181 [SRP_RPORT_FAIL_FAST] = "fail-fast",
182 [SRP_RPORT_LOST] = "lost",
183 };
184 struct srp_rport *rport = transport_class_to_srp_rport(dev);
185 enum srp_rport_state state = rport->state;
186
187 return sprintf(buf, "%s\n",
188 (unsigned)state < ARRAY_SIZE(state_name) ?
189 state_name[state] : "???");
190}
191
192static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
193
194static ssize_t srp_show_tmo(char *buf, int tmo)
195{
196 return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
197}
198
199static int srp_parse_tmo(int *tmo, const char *buf)
200{
201 int res = 0;
202
203 if (strncmp(buf, "off", 3) != 0)
204 res = kstrtoint(buf, 0, tmo);
205 else
206 *tmo = -1;
207
208 return res;
209}
210
Bart Van Assche8c64e452013-10-26 14:35:59 +0200211static ssize_t show_reconnect_delay(struct device *dev,
212 struct device_attribute *attr, char *buf)
213{
214 struct srp_rport *rport = transport_class_to_srp_rport(dev);
215
216 return srp_show_tmo(buf, rport->reconnect_delay);
217}
218
219static ssize_t store_reconnect_delay(struct device *dev,
220 struct device_attribute *attr,
221 const char *buf, const size_t count)
222{
223 struct srp_rport *rport = transport_class_to_srp_rport(dev);
224 int res, delay;
225
226 res = srp_parse_tmo(&delay, buf);
227 if (res)
228 goto out;
229 res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
230 rport->dev_loss_tmo);
231 if (res)
232 goto out;
233
234 if (rport->reconnect_delay <= 0 && delay > 0 &&
235 rport->state != SRP_RPORT_RUNNING) {
236 queue_delayed_work(system_long_wq, &rport->reconnect_work,
237 delay * HZ);
238 } else if (delay <= 0) {
239 cancel_delayed_work(&rport->reconnect_work);
240 }
241 rport->reconnect_delay = delay;
242 res = count;
243
244out:
245 return res;
246}
247
248static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
249 store_reconnect_delay);
250
251static ssize_t show_failed_reconnects(struct device *dev,
252 struct device_attribute *attr, char *buf)
253{
254 struct srp_rport *rport = transport_class_to_srp_rport(dev);
255
256 return sprintf(buf, "%d\n", rport->failed_reconnects);
257}
258
259static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
260
Bart Van Assche29c17322013-10-26 14:33:30 +0200261static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
262 struct device_attribute *attr,
263 char *buf)
264{
265 struct srp_rport *rport = transport_class_to_srp_rport(dev);
266
267 return srp_show_tmo(buf, rport->fast_io_fail_tmo);
268}
269
270static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
271 struct device_attribute *attr,
272 const char *buf, size_t count)
273{
274 struct srp_rport *rport = transport_class_to_srp_rport(dev);
275 int res;
276 int fast_io_fail_tmo;
277
278 res = srp_parse_tmo(&fast_io_fail_tmo, buf);
279 if (res)
280 goto out;
Bart Van Assche8c64e452013-10-26 14:35:59 +0200281 res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
282 rport->dev_loss_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200283 if (res)
284 goto out;
285 rport->fast_io_fail_tmo = fast_io_fail_tmo;
286 res = count;
287
288out:
289 return res;
290}
291
292static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
293 show_srp_rport_fast_io_fail_tmo,
294 store_srp_rport_fast_io_fail_tmo);
295
296static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
297 struct device_attribute *attr,
298 char *buf)
299{
300 struct srp_rport *rport = transport_class_to_srp_rport(dev);
301
302 return srp_show_tmo(buf, rport->dev_loss_tmo);
303}
304
305static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
306 struct device_attribute *attr,
307 const char *buf, size_t count)
308{
309 struct srp_rport *rport = transport_class_to_srp_rport(dev);
310 int res;
311 int dev_loss_tmo;
312
313 res = srp_parse_tmo(&dev_loss_tmo, buf);
314 if (res)
315 goto out;
Bart Van Assche8c64e452013-10-26 14:35:59 +0200316 res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
317 dev_loss_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200318 if (res)
319 goto out;
320 rport->dev_loss_tmo = dev_loss_tmo;
321 res = count;
322
323out:
324 return res;
325}
326
327static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
328 show_srp_rport_dev_loss_tmo,
329 store_srp_rport_dev_loss_tmo);
330
331static int srp_rport_set_state(struct srp_rport *rport,
332 enum srp_rport_state new_state)
333{
334 enum srp_rport_state old_state = rport->state;
335
336 lockdep_assert_held(&rport->mutex);
337
338 switch (new_state) {
339 case SRP_RPORT_RUNNING:
340 switch (old_state) {
341 case SRP_RPORT_LOST:
342 goto invalid;
343 default:
344 break;
345 }
346 break;
347 case SRP_RPORT_BLOCKED:
348 switch (old_state) {
349 case SRP_RPORT_RUNNING:
350 break;
351 default:
352 goto invalid;
353 }
354 break;
355 case SRP_RPORT_FAIL_FAST:
356 switch (old_state) {
357 case SRP_RPORT_LOST:
358 goto invalid;
359 default:
360 break;
361 }
362 break;
363 case SRP_RPORT_LOST:
364 break;
365 }
366 rport->state = new_state;
367 return 0;
368
369invalid:
370 return -EINVAL;
371}
372
Bart Van Assche8c64e452013-10-26 14:35:59 +0200373/**
374 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
375 */
376static void srp_reconnect_work(struct work_struct *work)
377{
378 struct srp_rport *rport = container_of(to_delayed_work(work),
379 struct srp_rport, reconnect_work);
380 struct Scsi_Host *shost = rport_to_shost(rport);
381 int delay, res;
382
383 res = srp_reconnect_rport(rport);
384 if (res != 0) {
385 shost_printk(KERN_ERR, shost,
386 "reconnect attempt %d failed (%d)\n",
387 ++rport->failed_reconnects, res);
388 delay = rport->reconnect_delay *
389 min(100, max(1, rport->failed_reconnects - 10));
390 if (delay > 0)
391 queue_delayed_work(system_long_wq,
392 &rport->reconnect_work, delay * HZ);
393 }
394}
395
Bart Van Assche29c17322013-10-26 14:33:30 +0200396static void __rport_fail_io_fast(struct srp_rport *rport)
397{
398 struct Scsi_Host *shost = rport_to_shost(rport);
399 struct srp_internal *i;
400
401 lockdep_assert_held(&rport->mutex);
402
403 if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
404 return;
405 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
406
407 /* Involve the LLD if possible to terminate all I/O on the rport. */
408 i = to_srp_internal(shost->transportt);
409 if (i->f->terminate_rport_io)
410 i->f->terminate_rport_io(rport);
411}
412
413/**
414 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
415 */
416static void rport_fast_io_fail_timedout(struct work_struct *work)
417{
418 struct srp_rport *rport = container_of(to_delayed_work(work),
419 struct srp_rport, fast_io_fail_work);
420 struct Scsi_Host *shost = rport_to_shost(rport);
421
422 pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
423 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
424
425 mutex_lock(&rport->mutex);
426 if (rport->state == SRP_RPORT_BLOCKED)
427 __rport_fail_io_fast(rport);
428 mutex_unlock(&rport->mutex);
429}
430
431/**
432 * rport_dev_loss_timedout() - device loss timeout handler
433 */
434static void rport_dev_loss_timedout(struct work_struct *work)
435{
436 struct srp_rport *rport = container_of(to_delayed_work(work),
437 struct srp_rport, dev_loss_work);
438 struct Scsi_Host *shost = rport_to_shost(rport);
439 struct srp_internal *i = to_srp_internal(shost->transportt);
440
441 pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
442 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
443
444 mutex_lock(&rport->mutex);
445 WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
446 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
447 mutex_unlock(&rport->mutex);
448
449 i->f->rport_delete(rport);
450}
451
452static void __srp_start_tl_fail_timers(struct srp_rport *rport)
453{
454 struct Scsi_Host *shost = rport_to_shost(rport);
Bart Van Assche8c64e452013-10-26 14:35:59 +0200455 int delay, fast_io_fail_tmo, dev_loss_tmo;
Bart Van Assche29c17322013-10-26 14:33:30 +0200456
457 lockdep_assert_held(&rport->mutex);
458
Bart Van Assche93079162013-12-11 17:06:14 +0100459 delay = rport->reconnect_delay;
460 fast_io_fail_tmo = rport->fast_io_fail_tmo;
461 dev_loss_tmo = rport->dev_loss_tmo;
462 pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
463 rport->state);
Bart Van Assche29c17322013-10-26 14:33:30 +0200464
Bart Van Assche93079162013-12-11 17:06:14 +0100465 if (rport->state == SRP_RPORT_LOST)
466 return;
467 if (delay > 0)
468 queue_delayed_work(system_long_wq, &rport->reconnect_work,
469 1UL * delay * HZ);
470 if (srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
471 pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
472 rport->state);
473 scsi_target_block(&shost->shost_gendev);
474 if (fast_io_fail_tmo >= 0)
Bart Van Assche8c64e452013-10-26 14:35:59 +0200475 queue_delayed_work(system_long_wq,
Bart Van Assche93079162013-12-11 17:06:14 +0100476 &rport->fast_io_fail_work,
477 1UL * fast_io_fail_tmo * HZ);
478 if (dev_loss_tmo >= 0)
479 queue_delayed_work(system_long_wq,
480 &rport->dev_loss_work,
481 1UL * dev_loss_tmo * HZ);
Bart Van Assche29c17322013-10-26 14:33:30 +0200482 }
483}
484
485/**
486 * srp_start_tl_fail_timers() - start the transport layer failure timers
487 *
488 * Start the transport layer fast I/O failure and device loss timers. Do not
489 * modify a timer that was already started.
490 */
491void srp_start_tl_fail_timers(struct srp_rport *rport)
492{
493 mutex_lock(&rport->mutex);
494 __srp_start_tl_fail_timers(rport);
495 mutex_unlock(&rport->mutex);
496}
497EXPORT_SYMBOL(srp_start_tl_fail_timers);
498
499/**
500 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
501 */
502static int scsi_request_fn_active(struct Scsi_Host *shost)
503{
504 struct scsi_device *sdev;
505 struct request_queue *q;
506 int request_fn_active = 0;
507
508 shost_for_each_device(sdev, shost) {
509 q = sdev->request_queue;
510
511 spin_lock_irq(q->queue_lock);
512 request_fn_active += q->request_fn_active;
513 spin_unlock_irq(q->queue_lock);
514 }
515
516 return request_fn_active;
517}
518
519/**
520 * srp_reconnect_rport() - reconnect to an SRP target port
521 *
522 * Blocks SCSI command queueing before invoking reconnect() such that
523 * queuecommand() won't be invoked concurrently with reconnect() from outside
524 * the SCSI EH. This is important since a reconnect() implementation may
525 * reallocate resources needed by queuecommand().
526 *
527 * Notes:
528 * - This function neither waits until outstanding requests have finished nor
529 * tries to abort these. It is the responsibility of the reconnect()
530 * function to finish outstanding commands before reconnecting to the target
531 * port.
532 * - It is the responsibility of the caller to ensure that the resources
533 * reallocated by the reconnect() function won't be used while this function
534 * is in progress. One possible strategy is to invoke this function from
535 * the context of the SCSI EH thread only. Another possible strategy is to
536 * lock the rport mutex inside each SCSI LLD callback that can be invoked by
537 * the SCSI EH (the scsi_host_template.eh_*() functions and also the
538 * scsi_host_template.queuecommand() function).
539 */
540int srp_reconnect_rport(struct srp_rport *rport)
541{
542 struct Scsi_Host *shost = rport_to_shost(rport);
543 struct srp_internal *i = to_srp_internal(shost->transportt);
544 struct scsi_device *sdev;
545 int res;
546
547 pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
548
549 res = mutex_lock_interruptible(&rport->mutex);
550 if (res)
551 goto out;
552 scsi_target_block(&shost->shost_gendev);
553 while (scsi_request_fn_active(shost))
554 msleep(20);
Bart Van Assche93079162013-12-11 17:06:14 +0100555 res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
Bart Van Assche29c17322013-10-26 14:33:30 +0200556 pr_debug("%s (state %d): transport.reconnect() returned %d\n",
557 dev_name(&shost->shost_gendev), rport->state, res);
558 if (res == 0) {
559 cancel_delayed_work(&rport->fast_io_fail_work);
560 cancel_delayed_work(&rport->dev_loss_work);
561
Bart Van Assche8c64e452013-10-26 14:35:59 +0200562 rport->failed_reconnects = 0;
Bart Van Assche29c17322013-10-26 14:33:30 +0200563 srp_rport_set_state(rport, SRP_RPORT_RUNNING);
564 scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
565 /*
566 * If the SCSI error handler has offlined one or more devices,
567 * invoking scsi_target_unblock() won't change the state of
568 * these devices into running so do that explicitly.
569 */
570 spin_lock_irq(shost->host_lock);
571 __shost_for_each_device(sdev, shost)
572 if (sdev->sdev_state == SDEV_OFFLINE)
573 sdev->sdev_state = SDEV_RUNNING;
574 spin_unlock_irq(shost->host_lock);
575 } else if (rport->state == SRP_RPORT_RUNNING) {
576 /*
Bart Van Assche18cc4e02013-12-11 17:05:22 +0100577 * srp_reconnect_rport() has been invoked with fast_io_fail
578 * and dev_loss off. Mark the port as failed and start the TL
579 * failure timers if these had not yet been started.
Bart Van Assche29c17322013-10-26 14:33:30 +0200580 */
581 __rport_fail_io_fast(rport);
582 scsi_target_unblock(&shost->shost_gendev,
583 SDEV_TRANSPORT_OFFLINE);
584 __srp_start_tl_fail_timers(rport);
585 } else if (rport->state != SRP_RPORT_BLOCKED) {
586 scsi_target_unblock(&shost->shost_gendev,
587 SDEV_TRANSPORT_OFFLINE);
588 }
589 mutex_unlock(&rport->mutex);
590
591out:
592 return res;
593}
594EXPORT_SYMBOL(srp_reconnect_rport);
595
596/**
597 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
598 *
599 * If a timeout occurs while an rport is in the blocked state, ask the SCSI
600 * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
601 * handle the timeout (BLK_EH_NOT_HANDLED).
602 *
603 * Note: This function is called from soft-IRQ context and with the request
604 * queue lock held.
605 */
606static enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
607{
608 struct scsi_device *sdev = scmd->device;
609 struct Scsi_Host *shost = sdev->host;
610 struct srp_internal *i = to_srp_internal(shost->transportt);
611
612 pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
613 return i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
614 BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
615}
616
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900617static void srp_rport_release(struct device *dev)
618{
619 struct srp_rport *rport = dev_to_rport(dev);
620
621 put_device(dev->parent);
622 kfree(rport);
623}
624
625static int scsi_is_srp_rport(const struct device *dev)
626{
627 return dev->release == srp_rport_release;
628}
629
630static int srp_rport_match(struct attribute_container *cont,
631 struct device *dev)
632{
633 struct Scsi_Host *shost;
634 struct srp_internal *i;
635
636 if (!scsi_is_srp_rport(dev))
637 return 0;
638
639 shost = dev_to_shost(dev->parent);
640 if (!shost->transportt)
641 return 0;
642 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
643 return 0;
644
645 i = to_srp_internal(shost->transportt);
646 return &i->rport_attr_cont.ac == cont;
647}
648
649static int srp_host_match(struct attribute_container *cont, struct device *dev)
650{
651 struct Scsi_Host *shost;
652 struct srp_internal *i;
653
654 if (!scsi_is_host_device(dev))
655 return 0;
656
657 shost = dev_to_shost(dev);
658 if (!shost->transportt)
659 return 0;
660 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
661 return 0;
662
663 i = to_srp_internal(shost->transportt);
664 return &i->t.host_attrs.ac == cont;
665}
666
667/**
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200668 * srp_rport_get() - increment rport reference count
669 */
670void srp_rport_get(struct srp_rport *rport)
671{
672 get_device(&rport->dev);
673}
674EXPORT_SYMBOL(srp_rport_get);
675
676/**
677 * srp_rport_put() - decrement rport reference count
678 */
679void srp_rport_put(struct srp_rport *rport)
680{
681 put_device(&rport->dev);
682}
683EXPORT_SYMBOL(srp_rport_put);
684
685/**
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900686 * srp_rport_add - add a SRP remote port to the device hierarchy
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900687 * @shost: scsi host the remote port is connected to.
688 * @ids: The port id for the remote port.
689 *
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800690 * Publishes a port to the rest of the system.
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900691 */
692struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
693 struct srp_rport_identifiers *ids)
694{
695 struct srp_rport *rport;
696 struct device *parent = &shost->shost_gendev;
Bart Van Assche29c17322013-10-26 14:33:30 +0200697 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900698 int id, ret;
699
700 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
701 if (!rport)
702 return ERR_PTR(-ENOMEM);
703
Bart Van Assche29c17322013-10-26 14:33:30 +0200704 mutex_init(&rport->mutex);
705
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900706 device_initialize(&rport->dev);
707
708 rport->dev.parent = get_device(parent);
709 rport->dev.release = srp_rport_release;
710
711 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900712 rport->roles = ids->roles;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900713
Bart Van Assche8c64e452013-10-26 14:35:59 +0200714 if (i->f->reconnect)
715 rport->reconnect_delay = i->f->reconnect_delay ?
716 *i->f->reconnect_delay : 10;
717 INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
Bart Van Assche29c17322013-10-26 14:33:30 +0200718 rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
719 *i->f->fast_io_fail_tmo : 15;
720 rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
721 INIT_DELAYED_WORK(&rport->fast_io_fail_work,
722 rport_fast_io_fail_timedout);
723 INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
724
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900725 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
Kay Sievers71610f52008-12-03 22:41:36 +0100726 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900727
728 transport_setup_device(&rport->dev);
729
730 ret = device_add(&rport->dev);
731 if (ret) {
732 transport_destroy_device(&rport->dev);
733 put_device(&rport->dev);
734 return ERR_PTR(ret);
735 }
736
FUJITA Tomonori72e39ea2007-09-01 02:03:39 +0900737 if (shost->active_mode & MODE_TARGET &&
738 ids->roles == SRP_RPORT_ROLE_INITIATOR) {
FUJITA Tomonori0012fdf2007-08-02 00:20:34 +0900739 ret = srp_tgt_it_nexus_create(shost, (unsigned long)rport,
740 rport->port_id);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900741 if (ret) {
742 device_del(&rport->dev);
743 transport_destroy_device(&rport->dev);
744 put_device(&rport->dev);
745 return ERR_PTR(ret);
746 }
747 }
748
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900749 transport_add_device(&rport->dev);
750 transport_configure_device(&rport->dev);
751
752 return rport;
753}
754EXPORT_SYMBOL_GPL(srp_rport_add);
755
756/**
Rob Landleyeb448202007-11-03 13:30:39 -0500757 * srp_rport_del - remove a SRP remote port
758 * @rport: SRP remote port to remove
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900759 *
760 * Removes the specified SRP remote port.
761 */
762void srp_rport_del(struct srp_rport *rport)
763{
764 struct device *dev = &rport->dev;
FUJITA Tomonori72e39ea2007-09-01 02:03:39 +0900765 struct Scsi_Host *shost = dev_to_shost(dev->parent);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900766
FUJITA Tomonori72e39ea2007-09-01 02:03:39 +0900767 if (shost->active_mode & MODE_TARGET &&
768 rport->roles == SRP_RPORT_ROLE_INITIATOR)
769 srp_tgt_it_nexus_destroy(shost, (unsigned long)rport);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900770
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900771 transport_remove_device(dev);
772 device_del(dev);
773 transport_destroy_device(dev);
Bart Van Assche29c17322013-10-26 14:33:30 +0200774
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900775 put_device(dev);
776}
777EXPORT_SYMBOL_GPL(srp_rport_del);
778
779static int do_srp_rport_del(struct device *dev, void *data)
780{
Dave Dillow91183342008-01-03 21:34:49 -0500781 if (scsi_is_srp_rport(dev))
782 srp_rport_del(dev_to_rport(dev));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900783 return 0;
784}
785
786/**
Rob Landleyeb448202007-11-03 13:30:39 -0500787 * srp_remove_host - tear down a Scsi_Host's SRP data structures
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900788 * @shost: Scsi Host that is torn down
789 *
790 * Removes all SRP remote ports for a given Scsi_Host.
791 * Must be called just before scsi_remove_host for SRP HBAs.
792 */
793void srp_remove_host(struct Scsi_Host *shost)
794{
795 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
796}
797EXPORT_SYMBOL_GPL(srp_remove_host);
798
Bart Van Assche93079162013-12-11 17:06:14 +0100799/**
800 * srp_stop_rport_timers - stop the transport layer recovery timers
801 *
802 * Must be called after srp_remove_host() and scsi_remove_host(). The caller
803 * must hold a reference on the rport (rport->dev) and on the SCSI host
804 * (rport->dev.parent).
805 */
806void srp_stop_rport_timers(struct srp_rport *rport)
807{
808 mutex_lock(&rport->mutex);
809 if (rport->state == SRP_RPORT_BLOCKED)
810 __rport_fail_io_fast(rport);
811 srp_rport_set_state(rport, SRP_RPORT_LOST);
812 mutex_unlock(&rport->mutex);
813
814 cancel_delayed_work_sync(&rport->reconnect_work);
815 cancel_delayed_work_sync(&rport->fast_io_fail_work);
816 cancel_delayed_work_sync(&rport->dev_loss_work);
817}
818EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
819
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900820static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
821 int result)
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900822{
823 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900824 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
825}
826
827static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
828{
829 struct srp_internal *i = to_srp_internal(shost->transportt);
830 return i->f->it_nexus_response(shost, nexus, result);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900831}
832
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900833/**
Rob Landleyeb448202007-11-03 13:30:39 -0500834 * srp_attach_transport - instantiate SRP transport template
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900835 * @ft: SRP transport class function template
836 */
837struct scsi_transport_template *
838srp_attach_transport(struct srp_function_template *ft)
839{
840 int count;
841 struct srp_internal *i;
842
843 i = kzalloc(sizeof(*i), GFP_KERNEL);
844 if (!i)
845 return NULL;
846
Bart Van Assche29c17322013-10-26 14:33:30 +0200847 i->t.eh_timed_out = srp_timed_out;
848
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900849 i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900850 i->t.it_nexus_response = srp_it_nexus_response;
851
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900852 i->t.host_size = sizeof(struct srp_host_attrs);
853 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
854 i->t.host_attrs.ac.class = &srp_host_class.class;
855 i->t.host_attrs.ac.match = srp_host_match;
856 i->host_attrs[0] = NULL;
857 transport_container_register(&i->t.host_attrs);
858
859 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
860 i->rport_attr_cont.ac.class = &srp_rport_class.class;
861 i->rport_attr_cont.ac.match = srp_rport_match;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900862
863 count = 0;
Bart Van Assche9134a852011-09-10 16:31:57 +0200864 i->rport_attrs[count++] = &dev_attr_port_id;
865 i->rport_attrs[count++] = &dev_attr_roles;
Bart Van Assche29c17322013-10-26 14:33:30 +0200866 if (ft->has_rport_state) {
867 i->rport_attrs[count++] = &dev_attr_state;
868 i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
869 i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
870 }
Bart Van Assche8c64e452013-10-26 14:35:59 +0200871 if (ft->reconnect) {
872 i->rport_attrs[count++] = &dev_attr_reconnect_delay;
873 i->rport_attrs[count++] = &dev_attr_failed_reconnects;
874 }
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200875 if (ft->rport_delete)
876 i->rport_attrs[count++] = &dev_attr_delete;
Bart Van Assche9134a852011-09-10 16:31:57 +0200877 i->rport_attrs[count++] = NULL;
878 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900879
Bart Van Asscheac9be302011-10-21 19:31:07 +0200880 transport_container_register(&i->rport_attr_cont);
881
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900882 i->f = ft;
883
884 return &i->t;
885}
886EXPORT_SYMBOL_GPL(srp_attach_transport);
887
888/**
Rob Landleyeb448202007-11-03 13:30:39 -0500889 * srp_release_transport - release SRP transport template instance
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900890 * @t: transport template instance
891 */
892void srp_release_transport(struct scsi_transport_template *t)
893{
894 struct srp_internal *i = to_srp_internal(t);
895
896 transport_container_unregister(&i->t.host_attrs);
897 transport_container_unregister(&i->rport_attr_cont);
898
899 kfree(i);
900}
901EXPORT_SYMBOL_GPL(srp_release_transport);
902
903static __init int srp_transport_init(void)
904{
905 int ret;
906
907 ret = transport_class_register(&srp_host_class);
908 if (ret)
909 return ret;
910 ret = transport_class_register(&srp_rport_class);
911 if (ret)
912 goto unregister_host_class;
913
914 return 0;
915unregister_host_class:
916 transport_class_unregister(&srp_host_class);
917 return ret;
918}
919
920static void __exit srp_transport_exit(void)
921{
922 transport_class_unregister(&srp_host_class);
923 transport_class_unregister(&srp_rport_class);
924}
925
926MODULE_AUTHOR("FUJITA Tomonori");
927MODULE_DESCRIPTION("SRP Transport Attributes");
928MODULE_LICENSE("GPL");
929
930module_init(srp_transport_init);
931module_exit(srp_transport_exit);