blob: 2700a5a09bd45a103b857b2f6a81da42834e4d28 [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
70 * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT. Furthermore, these
71 * parameters must be such that multipath can detect failed paths timely.
Bart Van Assche8c64e452013-10-26 14:35:59 +020072 * Hence do not allow all three parameters to be disabled simultaneously.
Bart Van Assche29c17322013-10-26 14:33:30 +020073 */
Bart Van Assche8c64e452013-10-26 14:35:59 +020074int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, int dev_loss_tmo)
Bart Van Assche29c17322013-10-26 14:33:30 +020075{
Bart Van Assche8c64e452013-10-26 14:35:59 +020076 if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
77 return -EINVAL;
78 if (reconnect_delay == 0)
Bart Van Assche29c17322013-10-26 14:33:30 +020079 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;
88}
89EXPORT_SYMBOL_GPL(srp_tmo_valid);
FUJITA Tomonori09345f62007-06-27 16:32:39 +090090
91static int srp_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +010092 struct device *cdev)
FUJITA Tomonori09345f62007-06-27 16:32:39 +090093{
94 struct Scsi_Host *shost = dev_to_shost(dev);
95 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
96
97 atomic_set(&srp_host->next_port_id, 0);
98 return 0;
99}
100
101static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
102 NULL, NULL);
103
104static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
105 NULL, NULL, NULL);
106
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900107#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]
112
113#define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
114 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
115
116static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100117show_srp_rport_id(struct device *dev, struct device_attribute *attr,
118 char *buf)
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900119{
Tony Jonesee959b02008-02-22 00:13:36 +0100120 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900121 return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
122}
123
Tony Jonesee959b02008-02-22 00:13:36 +0100124static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900125
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900126static 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"},
132};
133
134static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100135show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
136 char *buf)
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900137{
Tony Jonesee959b02008-02-22 00:13:36 +0100138 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900139 int i;
140 char *name = NULL;
141
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;
146 }
147 return sprintf(buf, "%s\n", name ? : "unknown");
148}
149
Tony Jonesee959b02008-02-22 00:13:36 +0100150static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900151
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200152static ssize_t store_srp_rport_delete(struct device *dev,
153 struct device_attribute *attr,
154 const char *buf, size_t count)
155{
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);
159
160 if (i->f->rport_delete) {
161 i->f->rport_delete(rport);
162 return count;
163 } else {
164 return -ENOSYS;
165 }
166}
167
168static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
169
Bart Van Assche29c17322013-10-26 14:33:30 +0200170static ssize_t show_srp_rport_state(struct device *dev,
171 struct device_attribute *attr,
172 char *buf)
173{
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",
179 };
180 struct srp_rport *rport = transport_class_to_srp_rport(dev);
181 enum srp_rport_state state = rport->state;
182
183 return sprintf(buf, "%s\n",
184 (unsigned)state < ARRAY_SIZE(state_name) ?
185 state_name[state] : "???");
186}
187
188static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
189
190static ssize_t srp_show_tmo(char *buf, int tmo)
191{
192 return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
193}
194
195static int srp_parse_tmo(int *tmo, const char *buf)
196{
197 int res = 0;
198
199 if (strncmp(buf, "off", 3) != 0)
200 res = kstrtoint(buf, 0, tmo);
201 else
202 *tmo = -1;
203
204 return res;
205}
206
Bart Van Assche8c64e452013-10-26 14:35:59 +0200207static ssize_t show_reconnect_delay(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 struct srp_rport *rport = transport_class_to_srp_rport(dev);
211
212 return srp_show_tmo(buf, rport->reconnect_delay);
213}
214
215static ssize_t store_reconnect_delay(struct device *dev,
216 struct device_attribute *attr,
217 const char *buf, const size_t count)
218{
219 struct srp_rport *rport = transport_class_to_srp_rport(dev);
220 int res, delay;
221
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;
229
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);
236 }
237 rport->reconnect_delay = delay;
238 res = count;
239
240out:
241 return res;
242}
243
244static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
245 store_reconnect_delay);
246
247static ssize_t show_failed_reconnects(struct device *dev,
248 struct device_attribute *attr, char *buf)
249{
250 struct srp_rport *rport = transport_class_to_srp_rport(dev);
251
252 return sprintf(buf, "%d\n", rport->failed_reconnects);
253}
254
255static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
256
Bart Van Assche29c17322013-10-26 14:33:30 +0200257static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
258 struct device_attribute *attr,
259 char *buf)
260{
261 struct srp_rport *rport = transport_class_to_srp_rport(dev);
262
263 return srp_show_tmo(buf, rport->fast_io_fail_tmo);
264}
265
266static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
267 struct device_attribute *attr,
268 const char *buf, size_t count)
269{
270 struct srp_rport *rport = transport_class_to_srp_rport(dev);
271 int res;
272 int fast_io_fail_tmo;
273
274 res = srp_parse_tmo(&fast_io_fail_tmo, buf);
275 if (res)
276 goto out;
Bart Van Assche8c64e452013-10-26 14:35:59 +0200277 res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
278 rport->dev_loss_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200279 if (res)
280 goto out;
281 rport->fast_io_fail_tmo = fast_io_fail_tmo;
282 res = count;
283
284out:
285 return res;
286}
287
288static 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);
291
292static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
293 struct device_attribute *attr,
294 char *buf)
295{
296 struct srp_rport *rport = transport_class_to_srp_rport(dev);
297
298 return srp_show_tmo(buf, rport->dev_loss_tmo);
299}
300
301static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
304{
305 struct srp_rport *rport = transport_class_to_srp_rport(dev);
306 int res;
307 int dev_loss_tmo;
308
309 res = srp_parse_tmo(&dev_loss_tmo, buf);
310 if (res)
311 goto out;
Bart Van Assche8c64e452013-10-26 14:35:59 +0200312 res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
313 dev_loss_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200314 if (res)
315 goto out;
316 rport->dev_loss_tmo = dev_loss_tmo;
317 res = count;
318
319out:
320 return res;
321}
322
323static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
324 show_srp_rport_dev_loss_tmo,
325 store_srp_rport_dev_loss_tmo);
326
327static int srp_rport_set_state(struct srp_rport *rport,
328 enum srp_rport_state new_state)
329{
330 enum srp_rport_state old_state = rport->state;
331
332 lockdep_assert_held(&rport->mutex);
333
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;
341 }
342 break;
343 case SRP_RPORT_BLOCKED:
344 switch (old_state) {
345 case SRP_RPORT_RUNNING:
346 break;
347 default:
348 goto invalid;
349 }
350 break;
351 case SRP_RPORT_FAIL_FAST:
352 switch (old_state) {
353 case SRP_RPORT_LOST:
354 goto invalid;
355 default:
356 break;
357 }
358 break;
359 case SRP_RPORT_LOST:
360 break;
361 }
362 rport->state = new_state;
363 return 0;
364
365invalid:
366 return -EINVAL;
367}
368
Bart Van Assche8c64e452013-10-26 14:35:59 +0200369/**
370 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
371 */
372static void srp_reconnect_work(struct work_struct *work)
373{
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;
378
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);
389 }
390}
391
Bart Van Assche29c17322013-10-26 14:33:30 +0200392static void __rport_fail_io_fast(struct srp_rport *rport)
393{
394 struct Scsi_Host *shost = rport_to_shost(rport);
395 struct srp_internal *i;
396
397 lockdep_assert_held(&rport->mutex);
398
399 if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
400 return;
401 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
402
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);
407}
408
409/**
410 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
411 */
412static void rport_fast_io_fail_timedout(struct work_struct *work)
413{
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);
417
418 pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
419 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
420
421 mutex_lock(&rport->mutex);
422 if (rport->state == SRP_RPORT_BLOCKED)
423 __rport_fail_io_fast(rport);
424 mutex_unlock(&rport->mutex);
425}
426
427/**
428 * rport_dev_loss_timedout() - device loss timeout handler
429 */
430static void rport_dev_loss_timedout(struct work_struct *work)
431{
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);
436
437 pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
438 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
439
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);
444
445 i->f->rport_delete(rport);
446}
447
448static void __srp_start_tl_fail_timers(struct srp_rport *rport)
449{
450 struct Scsi_Host *shost = rport_to_shost(rport);
Bart Van Assche8c64e452013-10-26 14:35:59 +0200451 int delay, fast_io_fail_tmo, dev_loss_tmo;
Bart Van Assche29c17322013-10-26 14:33:30 +0200452
453 lockdep_assert_held(&rport->mutex);
454
455 if (!rport->deleted) {
Bart Van Assche8c64e452013-10-26 14:35:59 +0200456 delay = rport->reconnect_delay;
Bart Van Assche29c17322013-10-26 14:33:30 +0200457 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);
461
Bart Van Assche8c64e452013-10-26 14:35:59 +0200462 if (delay > 0)
463 queue_delayed_work(system_long_wq,
464 &rport->reconnect_work,
465 1UL * delay * HZ);
Bart Van Assche29c17322013-10-26 14:33:30 +0200466 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);
475 }
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);
486 }
487}
488
489/**
490 * srp_start_tl_fail_timers() - start the transport layer failure timers
491 *
492 * Start the transport layer fast I/O failure and device loss timers. Do not
493 * modify a timer that was already started.
494 */
495void srp_start_tl_fail_timers(struct srp_rport *rport)
496{
497 mutex_lock(&rport->mutex);
498 __srp_start_tl_fail_timers(rport);
499 mutex_unlock(&rport->mutex);
500}
501EXPORT_SYMBOL(srp_start_tl_fail_timers);
502
503/**
504 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
505 */
506static int scsi_request_fn_active(struct Scsi_Host *shost)
507{
508 struct scsi_device *sdev;
509 struct request_queue *q;
510 int request_fn_active = 0;
511
512 shost_for_each_device(sdev, shost) {
513 q = sdev->request_queue;
514
515 spin_lock_irq(q->queue_lock);
516 request_fn_active += q->request_fn_active;
517 spin_unlock_irq(q->queue_lock);
518 }
519
520 return request_fn_active;
521}
522
523/**
524 * srp_reconnect_rport() - reconnect to an SRP target port
525 *
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().
530 *
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).
543 */
544int srp_reconnect_rport(struct srp_rport *rport)
545{
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;
550
551 pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
552
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);
565
Bart Van Assche8c64e452013-10-26 14:35:59 +0200566 rport->failed_reconnects = 0;
Bart Van Assche29c17322013-10-26 14:33:30 +0200567 srp_rport_set_state(rport, SRP_RPORT_RUNNING);
568 scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
569 /*
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.
573 */
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) {
580 /*
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.
584 */
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);
592 }
593 mutex_unlock(&rport->mutex);
594
595out:
596 return res;
597}
598EXPORT_SYMBOL(srp_reconnect_rport);
599
600/**
601 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
602 *
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).
606 *
607 * Note: This function is called from soft-IRQ context and with the request
608 * queue lock held.
609 */
610static enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
611{
612 struct scsi_device *sdev = scmd->device;
613 struct Scsi_Host *shost = sdev->host;
614 struct srp_internal *i = to_srp_internal(shost->transportt);
615
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;
619}
620
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900621static void srp_rport_release(struct device *dev)
622{
623 struct srp_rport *rport = dev_to_rport(dev);
624
Bart Van Assche8c64e452013-10-26 14:35:59 +0200625 cancel_delayed_work_sync(&rport->reconnect_work);
Bart Van Assche29c17322013-10-26 14:33:30 +0200626 cancel_delayed_work_sync(&rport->fast_io_fail_work);
627 cancel_delayed_work_sync(&rport->dev_loss_work);
628
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900629 put_device(dev->parent);
630 kfree(rport);
631}
632
633static int scsi_is_srp_rport(const struct device *dev)
634{
635 return dev->release == srp_rport_release;
636}
637
638static int srp_rport_match(struct attribute_container *cont,
639 struct device *dev)
640{
641 struct Scsi_Host *shost;
642 struct srp_internal *i;
643
644 if (!scsi_is_srp_rport(dev))
645 return 0;
646
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;
652
653 i = to_srp_internal(shost->transportt);
654 return &i->rport_attr_cont.ac == cont;
655}
656
657static int srp_host_match(struct attribute_container *cont, struct device *dev)
658{
659 struct Scsi_Host *shost;
660 struct srp_internal *i;
661
662 if (!scsi_is_host_device(dev))
663 return 0;
664
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;
670
671 i = to_srp_internal(shost->transportt);
672 return &i->t.host_attrs.ac == cont;
673}
674
675/**
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200676 * srp_rport_get() - increment rport reference count
677 */
678void srp_rport_get(struct srp_rport *rport)
679{
680 get_device(&rport->dev);
681}
682EXPORT_SYMBOL(srp_rport_get);
683
684/**
685 * srp_rport_put() - decrement rport reference count
686 */
687void srp_rport_put(struct srp_rport *rport)
688{
689 put_device(&rport->dev);
690}
691EXPORT_SYMBOL(srp_rport_put);
692
693/**
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900694 * srp_rport_add - add a SRP remote port to the device hierarchy
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900695 * @shost: scsi host the remote port is connected to.
696 * @ids: The port id for the remote port.
697 *
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800698 * Publishes a port to the rest of the system.
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900699 */
700struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
701 struct srp_rport_identifiers *ids)
702{
703 struct srp_rport *rport;
704 struct device *parent = &shost->shost_gendev;
Bart Van Assche29c17322013-10-26 14:33:30 +0200705 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900706 int id, ret;
707
708 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
709 if (!rport)
710 return ERR_PTR(-ENOMEM);
711
Bart Van Assche29c17322013-10-26 14:33:30 +0200712 mutex_init(&rport->mutex);
713
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900714 device_initialize(&rport->dev);
715
716 rport->dev.parent = get_device(parent);
717 rport->dev.release = srp_rport_release;
718
719 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900720 rport->roles = ids->roles;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900721
Bart Van Assche8c64e452013-10-26 14:35:59 +0200722 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);
Bart Van Assche29c17322013-10-26 14:33:30 +0200726 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);
732
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900733 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
Kay Sievers71610f52008-12-03 22:41:36 +0100734 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900735
736 transport_setup_device(&rport->dev);
737
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);
743 }
744
FUJITA Tomonori72e39ea2007-09-01 02:03:39 +0900745 if (shost->active_mode & MODE_TARGET &&
746 ids->roles == SRP_RPORT_ROLE_INITIATOR) {
FUJITA Tomonori0012fdf2007-08-02 00:20:34 +0900747 ret = srp_tgt_it_nexus_create(shost, (unsigned long)rport,
748 rport->port_id);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900749 if (ret) {
750 device_del(&rport->dev);
751 transport_destroy_device(&rport->dev);
752 put_device(&rport->dev);
753 return ERR_PTR(ret);
754 }
755 }
756
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900757 transport_add_device(&rport->dev);
758 transport_configure_device(&rport->dev);
759
760 return rport;
761}
762EXPORT_SYMBOL_GPL(srp_rport_add);
763
764/**
Rob Landleyeb448202007-11-03 13:30:39 -0500765 * srp_rport_del - remove a SRP remote port
766 * @rport: SRP remote port to remove
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900767 *
768 * Removes the specified SRP remote port.
769 */
770void srp_rport_del(struct srp_rport *rport)
771{
772 struct device *dev = &rport->dev;
FUJITA Tomonori72e39ea2007-09-01 02:03:39 +0900773 struct Scsi_Host *shost = dev_to_shost(dev->parent);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900774
FUJITA Tomonori72e39ea2007-09-01 02:03:39 +0900775 if (shost->active_mode & MODE_TARGET &&
776 rport->roles == SRP_RPORT_ROLE_INITIATOR)
777 srp_tgt_it_nexus_destroy(shost, (unsigned long)rport);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900778
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900779 transport_remove_device(dev);
780 device_del(dev);
781 transport_destroy_device(dev);
Bart Van Assche29c17322013-10-26 14:33:30 +0200782
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);
788
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900789 put_device(dev);
790}
791EXPORT_SYMBOL_GPL(srp_rport_del);
792
793static int do_srp_rport_del(struct device *dev, void *data)
794{
Dave Dillow91183342008-01-03 21:34:49 -0500795 if (scsi_is_srp_rport(dev))
796 srp_rport_del(dev_to_rport(dev));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900797 return 0;
798}
799
800/**
Rob Landleyeb448202007-11-03 13:30:39 -0500801 * srp_remove_host - tear down a Scsi_Host's SRP data structures
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900802 * @shost: Scsi Host that is torn down
803 *
804 * Removes all SRP remote ports for a given Scsi_Host.
805 * Must be called just before scsi_remove_host for SRP HBAs.
806 */
807void srp_remove_host(struct Scsi_Host *shost)
808{
809 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
810}
811EXPORT_SYMBOL_GPL(srp_remove_host);
812
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900813static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
814 int result)
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900815{
816 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900817 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
818}
819
820static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
821{
822 struct srp_internal *i = to_srp_internal(shost->transportt);
823 return i->f->it_nexus_response(shost, nexus, result);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900824}
825
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900826/**
Rob Landleyeb448202007-11-03 13:30:39 -0500827 * srp_attach_transport - instantiate SRP transport template
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900828 * @ft: SRP transport class function template
829 */
830struct scsi_transport_template *
831srp_attach_transport(struct srp_function_template *ft)
832{
833 int count;
834 struct srp_internal *i;
835
836 i = kzalloc(sizeof(*i), GFP_KERNEL);
837 if (!i)
838 return NULL;
839
Bart Van Assche29c17322013-10-26 14:33:30 +0200840 i->t.eh_timed_out = srp_timed_out;
841
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900842 i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900843 i->t.it_nexus_response = srp_it_nexus_response;
844
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900845 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);
851
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;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900855
856 count = 0;
Bart Van Assche9134a852011-09-10 16:31:57 +0200857 i->rport_attrs[count++] = &dev_attr_port_id;
858 i->rport_attrs[count++] = &dev_attr_roles;
Bart Van Assche29c17322013-10-26 14:33:30 +0200859 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;
863 }
Bart Van Assche8c64e452013-10-26 14:35:59 +0200864 if (ft->reconnect) {
865 i->rport_attrs[count++] = &dev_attr_reconnect_delay;
866 i->rport_attrs[count++] = &dev_attr_failed_reconnects;
867 }
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200868 if (ft->rport_delete)
869 i->rport_attrs[count++] = &dev_attr_delete;
Bart Van Assche9134a852011-09-10 16:31:57 +0200870 i->rport_attrs[count++] = NULL;
871 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900872
Bart Van Asscheac9be302011-10-21 19:31:07 +0200873 transport_container_register(&i->rport_attr_cont);
874
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900875 i->f = ft;
876
877 return &i->t;
878}
879EXPORT_SYMBOL_GPL(srp_attach_transport);
880
881/**
Rob Landleyeb448202007-11-03 13:30:39 -0500882 * srp_release_transport - release SRP transport template instance
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900883 * @t: transport template instance
884 */
885void srp_release_transport(struct scsi_transport_template *t)
886{
887 struct srp_internal *i = to_srp_internal(t);
888
889 transport_container_unregister(&i->t.host_attrs);
890 transport_container_unregister(&i->rport_attr_cont);
891
892 kfree(i);
893}
894EXPORT_SYMBOL_GPL(srp_release_transport);
895
896static __init int srp_transport_init(void)
897{
898 int ret;
899
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;
906
907 return 0;
908unregister_host_class:
909 transport_class_unregister(&srp_host_class);
910 return ret;
911}
912
913static void __exit srp_transport_exit(void)
914{
915 transport_class_unregister(&srp_host_class);
916 transport_class_unregister(&srp_rport_class);
917}
918
919MODULE_AUTHOR("FUJITA Tomonori");
920MODULE_DESCRIPTION("SRP Transport Attributes");
921MODULE_LICENSE("GPL");
922
923module_init(srp_transport_init);
924module_exit(srp_transport_exit);