blob: 02cfc6b1ee5a7b713a153db262ad59448e1522bb [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 Tomonori09345f62007-06-27 16:32:39 +090036
37struct srp_host_attrs {
38 atomic_t next_port_id;
39};
40#define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data)
41
42#define SRP_HOST_ATTRS 0
Bart Van Assche8c64e452013-10-26 14:35:59 +020043#define SRP_RPORT_ATTRS 8
FUJITA Tomonori09345f62007-06-27 16:32:39 +090044
45struct srp_internal {
46 struct scsi_transport_template t;
47 struct srp_function_template *f;
48
Tony Jonesee959b02008-02-22 00:13:36 +010049 struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
FUJITA Tomonori09345f62007-06-27 16:32:39 +090050
Tony Jonesee959b02008-02-22 00:13:36 +010051 struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
FUJITA Tomonori09345f62007-06-27 16:32:39 +090052 struct transport_container rport_attr_cont;
53};
54
55#define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
56
57#define dev_to_rport(d) container_of(d, struct srp_rport, dev)
Tony Jonesee959b02008-02-22 00:13:36 +010058#define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
Bart Van Assche29c17322013-10-26 14:33:30 +020059static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r)
60{
61 return dev_to_shost(r->dev.parent);
62}
63
Bart Van Asschee68ca752015-05-18 13:24:38 +020064static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost)
65{
66 return transport_class_to_srp_rport(&shost->shost_gendev);
67}
68
Bart Van Assche29c17322013-10-26 14:33:30 +020069/**
70 * srp_tmo_valid() - check timeout combination validity
Bart Van Assche0c7f8212014-01-13 12:39:35 +010071 * @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.
Bart Van Assche29c17322013-10-26 14:33:30 +020074 *
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
Bart Van Assche18cc4e02013-12-11 17:05:22 +010077 * 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
Bart Van Assche29c17322013-10-26 14:33:30 +020079 * parameters must be such that multipath can detect failed paths timely.
Bart Van Assche8c64e452013-10-26 14:35:59 +020080 * Hence do not allow all three parameters to be disabled simultaneously.
Bart Van Assche29c17322013-10-26 14:33:30 +020081 */
Bart Van Assche8c64e452013-10-26 14:35:59 +020082int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, int dev_loss_tmo)
Bart Van Assche29c17322013-10-26 14:33:30 +020083{
Bart Van Assche8c64e452013-10-26 14:35:59 +020084 if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
85 return -EINVAL;
86 if (reconnect_delay == 0)
Bart Van Assche29c17322013-10-26 14:33:30 +020087 return -EINVAL;
88 if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
89 return -EINVAL;
Bart Van Assche18cc4e02013-12-11 17:05:22 +010090 if (fast_io_fail_tmo < 0 &&
91 dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
92 return -EINVAL;
Bart Van Assche29c17322013-10-26 14:33:30 +020093 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;
99}
100EXPORT_SYMBOL_GPL(srp_tmo_valid);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900101
102static int srp_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100103 struct device *cdev)
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900104{
105 struct Scsi_Host *shost = dev_to_shost(dev);
106 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
107
108 atomic_set(&srp_host->next_port_id, 0);
109 return 0;
110}
111
112static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
113 NULL, NULL);
114
115static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
116 NULL, NULL, NULL);
117
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900118static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100119show_srp_rport_id(struct device *dev, struct device_attribute *attr,
120 char *buf)
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900121{
Tony Jonesee959b02008-02-22 00:13:36 +0100122 struct srp_rport *rport = transport_class_to_srp_rport(dev);
Andy Shevchenkodf441cc2016-10-22 20:32:30 +0300123 return sprintf(buf, "%16phC\n", rport->port_id);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900124}
125
Tony Jonesee959b02008-02-22 00:13:36 +0100126static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900127
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900128static const struct {
129 u32 value;
130 char *name;
131} srp_rport_role_names[] = {
132 {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
133 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
134};
135
136static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100137show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
138 char *buf)
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900139{
Tony Jonesee959b02008-02-22 00:13:36 +0100140 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900141 int i;
142 char *name = NULL;
143
144 for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
145 if (srp_rport_role_names[i].value == rport->roles) {
146 name = srp_rport_role_names[i].name;
147 break;
148 }
149 return sprintf(buf, "%s\n", name ? : "unknown");
150}
151
Tony Jonesee959b02008-02-22 00:13:36 +0100152static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900153
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200154static ssize_t store_srp_rport_delete(struct device *dev,
155 struct device_attribute *attr,
156 const char *buf, size_t count)
157{
158 struct srp_rport *rport = transport_class_to_srp_rport(dev);
159 struct Scsi_Host *shost = dev_to_shost(dev);
160 struct srp_internal *i = to_srp_internal(shost->transportt);
161
162 if (i->f->rport_delete) {
163 i->f->rport_delete(rport);
164 return count;
165 } else {
166 return -ENOSYS;
167 }
168}
169
170static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
171
Bart Van Assche29c17322013-10-26 14:33:30 +0200172static ssize_t show_srp_rport_state(struct device *dev,
173 struct device_attribute *attr,
174 char *buf)
175{
176 static const char *const state_name[] = {
177 [SRP_RPORT_RUNNING] = "running",
178 [SRP_RPORT_BLOCKED] = "blocked",
179 [SRP_RPORT_FAIL_FAST] = "fail-fast",
180 [SRP_RPORT_LOST] = "lost",
181 };
182 struct srp_rport *rport = transport_class_to_srp_rport(dev);
183 enum srp_rport_state state = rport->state;
184
185 return sprintf(buf, "%s\n",
186 (unsigned)state < ARRAY_SIZE(state_name) ?
187 state_name[state] : "???");
188}
189
190static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
191
192static ssize_t srp_show_tmo(char *buf, int tmo)
193{
194 return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
195}
196
Sagi Grimberg3fdf70a2015-06-25 13:34:15 +0300197int srp_parse_tmo(int *tmo, const char *buf)
Bart Van Assche29c17322013-10-26 14:33:30 +0200198{
199 int res = 0;
200
201 if (strncmp(buf, "off", 3) != 0)
202 res = kstrtoint(buf, 0, tmo);
203 else
204 *tmo = -1;
205
206 return res;
207}
Sagi Grimberg3fdf70a2015-06-25 13:34:15 +0300208EXPORT_SYMBOL(srp_parse_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200209
Bart Van Assche8c64e452013-10-26 14:35:59 +0200210static ssize_t show_reconnect_delay(struct device *dev,
211 struct device_attribute *attr, char *buf)
212{
213 struct srp_rport *rport = transport_class_to_srp_rport(dev);
214
215 return srp_show_tmo(buf, rport->reconnect_delay);
216}
217
218static ssize_t store_reconnect_delay(struct device *dev,
219 struct device_attribute *attr,
220 const char *buf, const size_t count)
221{
222 struct srp_rport *rport = transport_class_to_srp_rport(dev);
223 int res, delay;
224
225 res = srp_parse_tmo(&delay, buf);
226 if (res)
227 goto out;
228 res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
229 rport->dev_loss_tmo);
230 if (res)
231 goto out;
232
233 if (rport->reconnect_delay <= 0 && delay > 0 &&
234 rport->state != SRP_RPORT_RUNNING) {
235 queue_delayed_work(system_long_wq, &rport->reconnect_work,
236 delay * HZ);
237 } else if (delay <= 0) {
238 cancel_delayed_work(&rport->reconnect_work);
239 }
240 rport->reconnect_delay = delay;
241 res = count;
242
243out:
244 return res;
245}
246
247static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
248 store_reconnect_delay);
249
250static ssize_t show_failed_reconnects(struct device *dev,
251 struct device_attribute *attr, char *buf)
252{
253 struct srp_rport *rport = transport_class_to_srp_rport(dev);
254
255 return sprintf(buf, "%d\n", rport->failed_reconnects);
256}
257
258static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
259
Bart Van Assche29c17322013-10-26 14:33:30 +0200260static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
261 struct device_attribute *attr,
262 char *buf)
263{
264 struct srp_rport *rport = transport_class_to_srp_rport(dev);
265
266 return srp_show_tmo(buf, rport->fast_io_fail_tmo);
267}
268
269static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
270 struct device_attribute *attr,
271 const char *buf, size_t count)
272{
273 struct srp_rport *rport = transport_class_to_srp_rport(dev);
274 int res;
275 int fast_io_fail_tmo;
276
277 res = srp_parse_tmo(&fast_io_fail_tmo, buf);
278 if (res)
279 goto out;
Bart Van Assche8c64e452013-10-26 14:35:59 +0200280 res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
281 rport->dev_loss_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200282 if (res)
283 goto out;
284 rport->fast_io_fail_tmo = fast_io_fail_tmo;
285 res = count;
286
287out:
288 return res;
289}
290
291static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
292 show_srp_rport_fast_io_fail_tmo,
293 store_srp_rport_fast_io_fail_tmo);
294
295static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
296 struct device_attribute *attr,
297 char *buf)
298{
299 struct srp_rport *rport = transport_class_to_srp_rport(dev);
300
301 return srp_show_tmo(buf, rport->dev_loss_tmo);
302}
303
304static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
305 struct device_attribute *attr,
306 const char *buf, size_t count)
307{
308 struct srp_rport *rport = transport_class_to_srp_rport(dev);
309 int res;
310 int dev_loss_tmo;
311
312 res = srp_parse_tmo(&dev_loss_tmo, buf);
313 if (res)
314 goto out;
Bart Van Assche8c64e452013-10-26 14:35:59 +0200315 res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
316 dev_loss_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200317 if (res)
318 goto out;
319 rport->dev_loss_tmo = dev_loss_tmo;
320 res = count;
321
322out:
323 return res;
324}
325
326static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
327 show_srp_rport_dev_loss_tmo,
328 store_srp_rport_dev_loss_tmo);
329
330static int srp_rport_set_state(struct srp_rport *rport,
331 enum srp_rport_state new_state)
332{
333 enum srp_rport_state old_state = rport->state;
334
335 lockdep_assert_held(&rport->mutex);
336
337 switch (new_state) {
338 case SRP_RPORT_RUNNING:
339 switch (old_state) {
340 case SRP_RPORT_LOST:
341 goto invalid;
342 default:
343 break;
344 }
345 break;
346 case SRP_RPORT_BLOCKED:
347 switch (old_state) {
348 case SRP_RPORT_RUNNING:
349 break;
350 default:
351 goto invalid;
352 }
353 break;
354 case SRP_RPORT_FAIL_FAST:
355 switch (old_state) {
356 case SRP_RPORT_LOST:
357 goto invalid;
358 default:
359 break;
360 }
361 break;
362 case SRP_RPORT_LOST:
363 break;
364 }
365 rport->state = new_state;
366 return 0;
367
368invalid:
369 return -EINVAL;
370}
371
Bart Van Assche8c64e452013-10-26 14:35:59 +0200372/**
373 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100374 * @work: Work structure used for scheduling this operation.
Bart Van Assche8c64e452013-10-26 14:35:59 +0200375 */
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 Asschebe34c622015-05-18 13:22:19 +0200396/**
397 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
398 * @shost: SCSI host for which to count the number of scsi_request_fn() callers.
399 *
400 * To do: add support for scsi-mq in this function.
401 */
402static int scsi_request_fn_active(struct Scsi_Host *shost)
403{
404 struct scsi_device *sdev;
405 struct request_queue *q;
406 int request_fn_active = 0;
407
408 shost_for_each_device(sdev, shost) {
409 q = sdev->request_queue;
410
411 spin_lock_irq(q->queue_lock);
412 request_fn_active += q->request_fn_active;
413 spin_unlock_irq(q->queue_lock);
414 }
415
416 return request_fn_active;
417}
418
419/* Wait until ongoing shost->hostt->queuecommand() calls have finished. */
420static void srp_wait_for_queuecommand(struct Scsi_Host *shost)
421{
422 while (scsi_request_fn_active(shost))
423 msleep(20);
424}
425
Bart Van Assche29c17322013-10-26 14:33:30 +0200426static void __rport_fail_io_fast(struct srp_rport *rport)
427{
428 struct Scsi_Host *shost = rport_to_shost(rport);
429 struct srp_internal *i;
430
431 lockdep_assert_held(&rport->mutex);
432
433 if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
434 return;
435 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
436
437 /* Involve the LLD if possible to terminate all I/O on the rport. */
438 i = to_srp_internal(shost->transportt);
Bart Van Assche535fb902015-05-18 13:22:44 +0200439 if (i->f->terminate_rport_io) {
440 srp_wait_for_queuecommand(shost);
Bart Van Assche29c17322013-10-26 14:33:30 +0200441 i->f->terminate_rport_io(rport);
Bart Van Assche535fb902015-05-18 13:22:44 +0200442 }
Bart Van Assche29c17322013-10-26 14:33:30 +0200443}
444
445/**
446 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100447 * @work: Work structure used for scheduling this operation.
Bart Van Assche29c17322013-10-26 14:33:30 +0200448 */
449static void rport_fast_io_fail_timedout(struct work_struct *work)
450{
451 struct srp_rport *rport = container_of(to_delayed_work(work),
452 struct srp_rport, fast_io_fail_work);
453 struct Scsi_Host *shost = rport_to_shost(rport);
454
455 pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
456 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
457
458 mutex_lock(&rport->mutex);
459 if (rport->state == SRP_RPORT_BLOCKED)
460 __rport_fail_io_fast(rport);
461 mutex_unlock(&rport->mutex);
462}
463
464/**
465 * rport_dev_loss_timedout() - device loss timeout handler
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100466 * @work: Work structure used for scheduling this operation.
Bart Van Assche29c17322013-10-26 14:33:30 +0200467 */
468static void rport_dev_loss_timedout(struct work_struct *work)
469{
470 struct srp_rport *rport = container_of(to_delayed_work(work),
471 struct srp_rport, dev_loss_work);
472 struct Scsi_Host *shost = rport_to_shost(rport);
473 struct srp_internal *i = to_srp_internal(shost->transportt);
474
475 pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
476 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
477
478 mutex_lock(&rport->mutex);
479 WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
480 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
481 mutex_unlock(&rport->mutex);
482
483 i->f->rport_delete(rport);
484}
485
486static void __srp_start_tl_fail_timers(struct srp_rport *rport)
487{
488 struct Scsi_Host *shost = rport_to_shost(rport);
Bart Van Assche8c64e452013-10-26 14:35:59 +0200489 int delay, fast_io_fail_tmo, dev_loss_tmo;
Bart Van Assche29c17322013-10-26 14:33:30 +0200490
491 lockdep_assert_held(&rport->mutex);
492
Bart Van Assche93079162013-12-11 17:06:14 +0100493 delay = rport->reconnect_delay;
494 fast_io_fail_tmo = rport->fast_io_fail_tmo;
495 dev_loss_tmo = rport->dev_loss_tmo;
496 pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
497 rport->state);
Bart Van Assche29c17322013-10-26 14:33:30 +0200498
Bart Van Assche93079162013-12-11 17:06:14 +0100499 if (rport->state == SRP_RPORT_LOST)
500 return;
501 if (delay > 0)
502 queue_delayed_work(system_long_wq, &rport->reconnect_work,
503 1UL * delay * HZ);
Bart Van Asschecd53eb62014-07-09 15:56:43 +0200504 if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) &&
505 srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
Bart Van Assche93079162013-12-11 17:06:14 +0100506 pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
507 rport->state);
508 scsi_target_block(&shost->shost_gendev);
509 if (fast_io_fail_tmo >= 0)
Bart Van Assche8c64e452013-10-26 14:35:59 +0200510 queue_delayed_work(system_long_wq,
Bart Van Assche93079162013-12-11 17:06:14 +0100511 &rport->fast_io_fail_work,
512 1UL * fast_io_fail_tmo * HZ);
513 if (dev_loss_tmo >= 0)
514 queue_delayed_work(system_long_wq,
515 &rport->dev_loss_work,
516 1UL * dev_loss_tmo * HZ);
Bart Van Assche29c17322013-10-26 14:33:30 +0200517 }
518}
519
520/**
521 * srp_start_tl_fail_timers() - start the transport layer failure timers
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100522 * @rport: SRP target port.
Bart Van Assche29c17322013-10-26 14:33:30 +0200523 *
524 * Start the transport layer fast I/O failure and device loss timers. Do not
525 * modify a timer that was already started.
526 */
527void srp_start_tl_fail_timers(struct srp_rport *rport)
528{
529 mutex_lock(&rport->mutex);
530 __srp_start_tl_fail_timers(rport);
531 mutex_unlock(&rport->mutex);
532}
533EXPORT_SYMBOL(srp_start_tl_fail_timers);
534
535/**
Bart Van Assche29c17322013-10-26 14:33:30 +0200536 * srp_reconnect_rport() - reconnect to an SRP target port
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100537 * @rport: SRP target port.
Bart Van Assche29c17322013-10-26 14:33:30 +0200538 *
539 * Blocks SCSI command queueing before invoking reconnect() such that
540 * queuecommand() won't be invoked concurrently with reconnect() from outside
541 * the SCSI EH. This is important since a reconnect() implementation may
542 * reallocate resources needed by queuecommand().
543 *
544 * Notes:
545 * - This function neither waits until outstanding requests have finished nor
546 * tries to abort these. It is the responsibility of the reconnect()
547 * function to finish outstanding commands before reconnecting to the target
548 * port.
549 * - It is the responsibility of the caller to ensure that the resources
550 * reallocated by the reconnect() function won't be used while this function
551 * is in progress. One possible strategy is to invoke this function from
552 * the context of the SCSI EH thread only. Another possible strategy is to
553 * lock the rport mutex inside each SCSI LLD callback that can be invoked by
554 * the SCSI EH (the scsi_host_template.eh_*() functions and also the
555 * scsi_host_template.queuecommand() function).
556 */
557int srp_reconnect_rport(struct srp_rport *rport)
558{
559 struct Scsi_Host *shost = rport_to_shost(rport);
560 struct srp_internal *i = to_srp_internal(shost->transportt);
561 struct scsi_device *sdev;
562 int res;
563
564 pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
565
566 res = mutex_lock_interruptible(&rport->mutex);
567 if (res)
568 goto out;
569 scsi_target_block(&shost->shost_gendev);
Bart Van Asschebe34c622015-05-18 13:22:19 +0200570 srp_wait_for_queuecommand(shost);
Bart Van Assche93079162013-12-11 17:06:14 +0100571 res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
Bart Van Assche29c17322013-10-26 14:33:30 +0200572 pr_debug("%s (state %d): transport.reconnect() returned %d\n",
573 dev_name(&shost->shost_gendev), rport->state, res);
574 if (res == 0) {
575 cancel_delayed_work(&rport->fast_io_fail_work);
576 cancel_delayed_work(&rport->dev_loss_work);
577
Bart Van Assche8c64e452013-10-26 14:35:59 +0200578 rport->failed_reconnects = 0;
Bart Van Assche29c17322013-10-26 14:33:30 +0200579 srp_rport_set_state(rport, SRP_RPORT_RUNNING);
580 scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
581 /*
582 * If the SCSI error handler has offlined one or more devices,
583 * invoking scsi_target_unblock() won't change the state of
584 * these devices into running so do that explicitly.
585 */
586 spin_lock_irq(shost->host_lock);
587 __shost_for_each_device(sdev, shost)
588 if (sdev->sdev_state == SDEV_OFFLINE)
589 sdev->sdev_state = SDEV_RUNNING;
590 spin_unlock_irq(shost->host_lock);
591 } else if (rport->state == SRP_RPORT_RUNNING) {
592 /*
Bart Van Assche18cc4e02013-12-11 17:05:22 +0100593 * srp_reconnect_rport() has been invoked with fast_io_fail
594 * and dev_loss off. Mark the port as failed and start the TL
595 * failure timers if these had not yet been started.
Bart Van Assche29c17322013-10-26 14:33:30 +0200596 */
597 __rport_fail_io_fast(rport);
598 scsi_target_unblock(&shost->shost_gendev,
599 SDEV_TRANSPORT_OFFLINE);
600 __srp_start_tl_fail_timers(rport);
601 } else if (rport->state != SRP_RPORT_BLOCKED) {
602 scsi_target_unblock(&shost->shost_gendev,
603 SDEV_TRANSPORT_OFFLINE);
604 }
605 mutex_unlock(&rport->mutex);
606
607out:
608 return res;
609}
610EXPORT_SYMBOL(srp_reconnect_rport);
611
612/**
613 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100614 * @scmd: SCSI command.
Bart Van Assche29c17322013-10-26 14:33:30 +0200615 *
616 * If a timeout occurs while an rport is in the blocked state, ask the SCSI
617 * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
618 * handle the timeout (BLK_EH_NOT_HANDLED).
619 *
620 * Note: This function is called from soft-IRQ context and with the request
621 * queue lock held.
622 */
623static enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
624{
625 struct scsi_device *sdev = scmd->device;
626 struct Scsi_Host *shost = sdev->host;
627 struct srp_internal *i = to_srp_internal(shost->transportt);
Bart Van Asschee68ca752015-05-18 13:24:38 +0200628 struct srp_rport *rport = shost_to_rport(shost);
Bart Van Assche29c17322013-10-26 14:33:30 +0200629
630 pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
Bart Van Asschee68ca752015-05-18 13:24:38 +0200631 return rport->fast_io_fail_tmo < 0 && rport->dev_loss_tmo < 0 &&
632 i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
Bart Van Assche29c17322013-10-26 14:33:30 +0200633 BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
634}
635
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900636static void srp_rport_release(struct device *dev)
637{
638 struct srp_rport *rport = dev_to_rport(dev);
639
640 put_device(dev->parent);
641 kfree(rport);
642}
643
644static int scsi_is_srp_rport(const struct device *dev)
645{
646 return dev->release == srp_rport_release;
647}
648
649static int srp_rport_match(struct attribute_container *cont,
650 struct device *dev)
651{
652 struct Scsi_Host *shost;
653 struct srp_internal *i;
654
655 if (!scsi_is_srp_rport(dev))
656 return 0;
657
658 shost = dev_to_shost(dev->parent);
659 if (!shost->transportt)
660 return 0;
661 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
662 return 0;
663
664 i = to_srp_internal(shost->transportt);
665 return &i->rport_attr_cont.ac == cont;
666}
667
668static int srp_host_match(struct attribute_container *cont, struct device *dev)
669{
670 struct Scsi_Host *shost;
671 struct srp_internal *i;
672
673 if (!scsi_is_host_device(dev))
674 return 0;
675
676 shost = dev_to_shost(dev);
677 if (!shost->transportt)
678 return 0;
679 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
680 return 0;
681
682 i = to_srp_internal(shost->transportt);
683 return &i->t.host_attrs.ac == cont;
684}
685
686/**
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200687 * srp_rport_get() - increment rport reference count
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100688 * @rport: SRP target port.
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200689 */
690void srp_rport_get(struct srp_rport *rport)
691{
692 get_device(&rport->dev);
693}
694EXPORT_SYMBOL(srp_rport_get);
695
696/**
697 * srp_rport_put() - decrement rport reference count
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100698 * @rport: SRP target port.
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200699 */
700void srp_rport_put(struct srp_rport *rport)
701{
702 put_device(&rport->dev);
703}
704EXPORT_SYMBOL(srp_rport_put);
705
706/**
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900707 * srp_rport_add - add a SRP remote port to the device hierarchy
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900708 * @shost: scsi host the remote port is connected to.
709 * @ids: The port id for the remote port.
710 *
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800711 * Publishes a port to the rest of the system.
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900712 */
713struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
714 struct srp_rport_identifiers *ids)
715{
716 struct srp_rport *rport;
717 struct device *parent = &shost->shost_gendev;
Bart Van Assche29c17322013-10-26 14:33:30 +0200718 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900719 int id, ret;
720
721 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
722 if (!rport)
723 return ERR_PTR(-ENOMEM);
724
Bart Van Assche29c17322013-10-26 14:33:30 +0200725 mutex_init(&rport->mutex);
726
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900727 device_initialize(&rport->dev);
728
729 rport->dev.parent = get_device(parent);
730 rport->dev.release = srp_rport_release;
731
732 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900733 rport->roles = ids->roles;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900734
Bart Van Assche8c64e452013-10-26 14:35:59 +0200735 if (i->f->reconnect)
736 rport->reconnect_delay = i->f->reconnect_delay ?
737 *i->f->reconnect_delay : 10;
738 INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
Bart Van Assche29c17322013-10-26 14:33:30 +0200739 rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
740 *i->f->fast_io_fail_tmo : 15;
741 rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
742 INIT_DELAYED_WORK(&rport->fast_io_fail_work,
743 rport_fast_io_fail_timedout);
744 INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
745
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900746 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
Kay Sievers71610f52008-12-03 22:41:36 +0100747 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900748
749 transport_setup_device(&rport->dev);
750
751 ret = device_add(&rport->dev);
752 if (ret) {
753 transport_destroy_device(&rport->dev);
754 put_device(&rport->dev);
755 return ERR_PTR(ret);
756 }
757
758 transport_add_device(&rport->dev);
759 transport_configure_device(&rport->dev);
760
761 return rport;
762}
763EXPORT_SYMBOL_GPL(srp_rport_add);
764
765/**
Rob Landleyeb448202007-11-03 13:30:39 -0500766 * srp_rport_del - remove a SRP remote port
767 * @rport: SRP remote port to remove
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900768 *
769 * Removes the specified SRP remote port.
770 */
771void srp_rport_del(struct srp_rport *rport)
772{
773 struct device *dev = &rport->dev;
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900774
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900775 transport_remove_device(dev);
776 device_del(dev);
777 transport_destroy_device(dev);
Bart Van Assche29c17322013-10-26 14:33:30 +0200778
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900779 put_device(dev);
780}
781EXPORT_SYMBOL_GPL(srp_rport_del);
782
783static int do_srp_rport_del(struct device *dev, void *data)
784{
Dave Dillow91183342008-01-03 21:34:49 -0500785 if (scsi_is_srp_rport(dev))
786 srp_rport_del(dev_to_rport(dev));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900787 return 0;
788}
789
790/**
Rob Landleyeb448202007-11-03 13:30:39 -0500791 * srp_remove_host - tear down a Scsi_Host's SRP data structures
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900792 * @shost: Scsi Host that is torn down
793 *
794 * Removes all SRP remote ports for a given Scsi_Host.
795 * Must be called just before scsi_remove_host for SRP HBAs.
796 */
797void srp_remove_host(struct Scsi_Host *shost)
798{
799 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
800}
801EXPORT_SYMBOL_GPL(srp_remove_host);
802
Bart Van Assche93079162013-12-11 17:06:14 +0100803/**
804 * srp_stop_rport_timers - stop the transport layer recovery timers
Bart Van Assche67513602014-03-14 13:51:19 +0100805 * @rport: SRP remote port for which to stop the timers.
Bart Van Assche93079162013-12-11 17:06:14 +0100806 *
807 * Must be called after srp_remove_host() and scsi_remove_host(). The caller
808 * must hold a reference on the rport (rport->dev) and on the SCSI host
809 * (rport->dev.parent).
810 */
811void srp_stop_rport_timers(struct srp_rport *rport)
812{
813 mutex_lock(&rport->mutex);
814 if (rport->state == SRP_RPORT_BLOCKED)
815 __rport_fail_io_fast(rport);
816 srp_rport_set_state(rport, SRP_RPORT_LOST);
817 mutex_unlock(&rport->mutex);
818
819 cancel_delayed_work_sync(&rport->reconnect_work);
820 cancel_delayed_work_sync(&rport->fast_io_fail_work);
821 cancel_delayed_work_sync(&rport->dev_loss_work);
822}
823EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
824
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900825static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
826 int result)
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900827{
828 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900829 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
830}
831
832static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
833{
834 struct srp_internal *i = to_srp_internal(shost->transportt);
835 return i->f->it_nexus_response(shost, nexus, result);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900836}
837
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900838/**
Rob Landleyeb448202007-11-03 13:30:39 -0500839 * srp_attach_transport - instantiate SRP transport template
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900840 * @ft: SRP transport class function template
841 */
842struct scsi_transport_template *
843srp_attach_transport(struct srp_function_template *ft)
844{
845 int count;
846 struct srp_internal *i;
847
848 i = kzalloc(sizeof(*i), GFP_KERNEL);
849 if (!i)
850 return NULL;
851
Bart Van Assche29c17322013-10-26 14:33:30 +0200852 i->t.eh_timed_out = srp_timed_out;
853
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900854 i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900855 i->t.it_nexus_response = srp_it_nexus_response;
856
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900857 i->t.host_size = sizeof(struct srp_host_attrs);
858 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
859 i->t.host_attrs.ac.class = &srp_host_class.class;
860 i->t.host_attrs.ac.match = srp_host_match;
861 i->host_attrs[0] = NULL;
862 transport_container_register(&i->t.host_attrs);
863
864 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
865 i->rport_attr_cont.ac.class = &srp_rport_class.class;
866 i->rport_attr_cont.ac.match = srp_rport_match;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900867
868 count = 0;
Bart Van Assche9134a852011-09-10 16:31:57 +0200869 i->rport_attrs[count++] = &dev_attr_port_id;
870 i->rport_attrs[count++] = &dev_attr_roles;
Bart Van Assche29c17322013-10-26 14:33:30 +0200871 if (ft->has_rport_state) {
872 i->rport_attrs[count++] = &dev_attr_state;
873 i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
874 i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
875 }
Bart Van Assche8c64e452013-10-26 14:35:59 +0200876 if (ft->reconnect) {
877 i->rport_attrs[count++] = &dev_attr_reconnect_delay;
878 i->rport_attrs[count++] = &dev_attr_failed_reconnects;
879 }
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200880 if (ft->rport_delete)
881 i->rport_attrs[count++] = &dev_attr_delete;
Bart Van Assche9134a852011-09-10 16:31:57 +0200882 i->rport_attrs[count++] = NULL;
883 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900884
Bart Van Asscheac9be302011-10-21 19:31:07 +0200885 transport_container_register(&i->rport_attr_cont);
886
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900887 i->f = ft;
888
889 return &i->t;
890}
891EXPORT_SYMBOL_GPL(srp_attach_transport);
892
893/**
Rob Landleyeb448202007-11-03 13:30:39 -0500894 * srp_release_transport - release SRP transport template instance
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900895 * @t: transport template instance
896 */
897void srp_release_transport(struct scsi_transport_template *t)
898{
899 struct srp_internal *i = to_srp_internal(t);
900
901 transport_container_unregister(&i->t.host_attrs);
902 transport_container_unregister(&i->rport_attr_cont);
903
904 kfree(i);
905}
906EXPORT_SYMBOL_GPL(srp_release_transport);
907
908static __init int srp_transport_init(void)
909{
910 int ret;
911
912 ret = transport_class_register(&srp_host_class);
913 if (ret)
914 return ret;
915 ret = transport_class_register(&srp_rport_class);
916 if (ret)
917 goto unregister_host_class;
918
919 return 0;
920unregister_host_class:
921 transport_class_unregister(&srp_host_class);
922 return ret;
923}
924
925static void __exit srp_transport_exit(void)
926{
927 transport_class_unregister(&srp_host_class);
928 transport_class_unregister(&srp_rport_class);
929}
930
931MODULE_AUTHOR("FUJITA Tomonori");
932MODULE_DESCRIPTION("SRP Transport Attributes");
933MODULE_LICENSE("GPL");
934
935module_init(srp_transport_init);
936module_exit(srp_transport_exit);