blob: c3d1891d2d3f32dccf62966fc59140bf6d362b7b [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
Bart Van Assche3875d1b2018-05-21 11:17:29 -070055static int scsi_is_srp_rport(const struct device *dev);
56
FUJITA Tomonori09345f62007-06-27 16:32:39 +090057#define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
58
59#define dev_to_rport(d) container_of(d, struct srp_rport, dev)
Tony Jonesee959b02008-02-22 00:13:36 +010060#define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
Bart Van Assche29c17322013-10-26 14:33:30 +020061static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r)
62{
63 return dev_to_shost(r->dev.parent);
64}
65
Bart Van Assche3875d1b2018-05-21 11:17:29 -070066static int find_child_rport(struct device *dev, void *data)
67{
68 struct device **child = data;
69
70 if (scsi_is_srp_rport(dev)) {
71 WARN_ON_ONCE(*child);
72 *child = dev;
73 }
74 return 0;
75}
76
Bart Van Asschee68ca752015-05-18 13:24:38 +020077static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost)
78{
Bart Van Assche3875d1b2018-05-21 11:17:29 -070079 struct device *child = NULL;
80
81 WARN_ON_ONCE(device_for_each_child(&shost->shost_gendev, &child,
82 find_child_rport) < 0);
83 return child ? dev_to_rport(child) : NULL;
Bart Van Asschee68ca752015-05-18 13:24:38 +020084}
85
Bart Van Assche29c17322013-10-26 14:33:30 +020086/**
87 * srp_tmo_valid() - check timeout combination validity
Bart Van Assche0c7f8212014-01-13 12:39:35 +010088 * @reconnect_delay: Reconnect delay in seconds.
89 * @fast_io_fail_tmo: Fast I/O fail timeout in seconds.
90 * @dev_loss_tmo: Device loss timeout in seconds.
Bart Van Assche29c17322013-10-26 14:33:30 +020091 *
92 * The combination of the timeout parameters must be such that SCSI commands
93 * are finished in a reasonable time. Hence do not allow the fast I/O fail
Bart Van Assche18cc4e02013-12-11 17:05:22 +010094 * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to
95 * exceed that limit if failing I/O fast has been disabled. Furthermore, these
Bart Van Assche29c17322013-10-26 14:33:30 +020096 * parameters must be such that multipath can detect failed paths timely.
Bart Van Assche8c64e452013-10-26 14:35:59 +020097 * Hence do not allow all three parameters to be disabled simultaneously.
Bart Van Assche29c17322013-10-26 14:33:30 +020098 */
Bart Van Assche8c64e452013-10-26 14:35:59 +020099int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, int dev_loss_tmo)
Bart Van Assche29c17322013-10-26 14:33:30 +0200100{
Bart Van Assche8c64e452013-10-26 14:35:59 +0200101 if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
102 return -EINVAL;
103 if (reconnect_delay == 0)
Bart Van Assche29c17322013-10-26 14:33:30 +0200104 return -EINVAL;
105 if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
106 return -EINVAL;
Bart Van Assche18cc4e02013-12-11 17:05:22 +0100107 if (fast_io_fail_tmo < 0 &&
108 dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
109 return -EINVAL;
Bart Van Assche29c17322013-10-26 14:33:30 +0200110 if (dev_loss_tmo >= LONG_MAX / HZ)
111 return -EINVAL;
112 if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
113 fast_io_fail_tmo >= dev_loss_tmo)
114 return -EINVAL;
115 return 0;
116}
117EXPORT_SYMBOL_GPL(srp_tmo_valid);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900118
119static int srp_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100120 struct device *cdev)
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900121{
122 struct Scsi_Host *shost = dev_to_shost(dev);
123 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
124
125 atomic_set(&srp_host->next_port_id, 0);
126 return 0;
127}
128
129static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
130 NULL, NULL);
131
132static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
133 NULL, NULL, NULL);
134
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900135#define SRP_PID(p) \
136 (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
137 (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
138 (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
139 (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
140
141#define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
142 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
143
144static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100145show_srp_rport_id(struct device *dev, struct device_attribute *attr,
146 char *buf)
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900147{
Tony Jonesee959b02008-02-22 00:13:36 +0100148 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900149 return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
150}
151
Tony Jonesee959b02008-02-22 00:13:36 +0100152static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900153
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900154static const struct {
155 u32 value;
156 char *name;
157} srp_rport_role_names[] = {
158 {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
159 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
160};
161
162static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100163show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
164 char *buf)
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900165{
Tony Jonesee959b02008-02-22 00:13:36 +0100166 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900167 int i;
168 char *name = NULL;
169
170 for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
171 if (srp_rport_role_names[i].value == rport->roles) {
172 name = srp_rport_role_names[i].name;
173 break;
174 }
175 return sprintf(buf, "%s\n", name ? : "unknown");
176}
177
Tony Jonesee959b02008-02-22 00:13:36 +0100178static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900179
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200180static ssize_t store_srp_rport_delete(struct device *dev,
181 struct device_attribute *attr,
182 const char *buf, size_t count)
183{
184 struct srp_rport *rport = transport_class_to_srp_rport(dev);
185 struct Scsi_Host *shost = dev_to_shost(dev);
186 struct srp_internal *i = to_srp_internal(shost->transportt);
187
188 if (i->f->rport_delete) {
189 i->f->rport_delete(rport);
190 return count;
191 } else {
192 return -ENOSYS;
193 }
194}
195
196static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
197
Bart Van Assche29c17322013-10-26 14:33:30 +0200198static ssize_t show_srp_rport_state(struct device *dev,
199 struct device_attribute *attr,
200 char *buf)
201{
202 static const char *const state_name[] = {
203 [SRP_RPORT_RUNNING] = "running",
204 [SRP_RPORT_BLOCKED] = "blocked",
205 [SRP_RPORT_FAIL_FAST] = "fail-fast",
206 [SRP_RPORT_LOST] = "lost",
207 };
208 struct srp_rport *rport = transport_class_to_srp_rport(dev);
209 enum srp_rport_state state = rport->state;
210
211 return sprintf(buf, "%s\n",
212 (unsigned)state < ARRAY_SIZE(state_name) ?
213 state_name[state] : "???");
214}
215
216static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
217
218static ssize_t srp_show_tmo(char *buf, int tmo)
219{
220 return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
221}
222
Sagi Grimberg3fdf70a2015-06-25 13:34:15 +0300223int srp_parse_tmo(int *tmo, const char *buf)
Bart Van Assche29c17322013-10-26 14:33:30 +0200224{
225 int res = 0;
226
227 if (strncmp(buf, "off", 3) != 0)
228 res = kstrtoint(buf, 0, tmo);
229 else
230 *tmo = -1;
231
232 return res;
233}
Sagi Grimberg3fdf70a2015-06-25 13:34:15 +0300234EXPORT_SYMBOL(srp_parse_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200235
Bart Van Assche8c64e452013-10-26 14:35:59 +0200236static ssize_t show_reconnect_delay(struct device *dev,
237 struct device_attribute *attr, char *buf)
238{
239 struct srp_rport *rport = transport_class_to_srp_rport(dev);
240
241 return srp_show_tmo(buf, rport->reconnect_delay);
242}
243
244static ssize_t store_reconnect_delay(struct device *dev,
245 struct device_attribute *attr,
246 const char *buf, const size_t count)
247{
248 struct srp_rport *rport = transport_class_to_srp_rport(dev);
249 int res, delay;
250
251 res = srp_parse_tmo(&delay, buf);
252 if (res)
253 goto out;
254 res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
255 rport->dev_loss_tmo);
256 if (res)
257 goto out;
258
259 if (rport->reconnect_delay <= 0 && delay > 0 &&
260 rport->state != SRP_RPORT_RUNNING) {
261 queue_delayed_work(system_long_wq, &rport->reconnect_work,
262 delay * HZ);
263 } else if (delay <= 0) {
264 cancel_delayed_work(&rport->reconnect_work);
265 }
266 rport->reconnect_delay = delay;
267 res = count;
268
269out:
270 return res;
271}
272
273static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
274 store_reconnect_delay);
275
276static ssize_t show_failed_reconnects(struct device *dev,
277 struct device_attribute *attr, char *buf)
278{
279 struct srp_rport *rport = transport_class_to_srp_rport(dev);
280
281 return sprintf(buf, "%d\n", rport->failed_reconnects);
282}
283
284static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
285
Bart Van Assche29c17322013-10-26 14:33:30 +0200286static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
287 struct device_attribute *attr,
288 char *buf)
289{
290 struct srp_rport *rport = transport_class_to_srp_rport(dev);
291
292 return srp_show_tmo(buf, rport->fast_io_fail_tmo);
293}
294
295static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
296 struct device_attribute *attr,
297 const char *buf, size_t count)
298{
299 struct srp_rport *rport = transport_class_to_srp_rport(dev);
300 int res;
301 int fast_io_fail_tmo;
302
303 res = srp_parse_tmo(&fast_io_fail_tmo, buf);
304 if (res)
305 goto out;
Bart Van Assche8c64e452013-10-26 14:35:59 +0200306 res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
307 rport->dev_loss_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200308 if (res)
309 goto out;
310 rport->fast_io_fail_tmo = fast_io_fail_tmo;
311 res = count;
312
313out:
314 return res;
315}
316
317static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
318 show_srp_rport_fast_io_fail_tmo,
319 store_srp_rport_fast_io_fail_tmo);
320
321static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
322 struct device_attribute *attr,
323 char *buf)
324{
325 struct srp_rport *rport = transport_class_to_srp_rport(dev);
326
327 return srp_show_tmo(buf, rport->dev_loss_tmo);
328}
329
330static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
331 struct device_attribute *attr,
332 const char *buf, size_t count)
333{
334 struct srp_rport *rport = transport_class_to_srp_rport(dev);
335 int res;
336 int dev_loss_tmo;
337
338 res = srp_parse_tmo(&dev_loss_tmo, buf);
339 if (res)
340 goto out;
Bart Van Assche8c64e452013-10-26 14:35:59 +0200341 res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
342 dev_loss_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200343 if (res)
344 goto out;
345 rport->dev_loss_tmo = dev_loss_tmo;
346 res = count;
347
348out:
349 return res;
350}
351
352static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
353 show_srp_rport_dev_loss_tmo,
354 store_srp_rport_dev_loss_tmo);
355
356static int srp_rport_set_state(struct srp_rport *rport,
357 enum srp_rport_state new_state)
358{
359 enum srp_rport_state old_state = rport->state;
360
361 lockdep_assert_held(&rport->mutex);
362
363 switch (new_state) {
364 case SRP_RPORT_RUNNING:
365 switch (old_state) {
366 case SRP_RPORT_LOST:
367 goto invalid;
368 default:
369 break;
370 }
371 break;
372 case SRP_RPORT_BLOCKED:
373 switch (old_state) {
374 case SRP_RPORT_RUNNING:
375 break;
376 default:
377 goto invalid;
378 }
379 break;
380 case SRP_RPORT_FAIL_FAST:
381 switch (old_state) {
382 case SRP_RPORT_LOST:
383 goto invalid;
384 default:
385 break;
386 }
387 break;
388 case SRP_RPORT_LOST:
389 break;
390 }
391 rport->state = new_state;
392 return 0;
393
394invalid:
395 return -EINVAL;
396}
397
Bart Van Assche8c64e452013-10-26 14:35:59 +0200398/**
399 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100400 * @work: Work structure used for scheduling this operation.
Bart Van Assche8c64e452013-10-26 14:35:59 +0200401 */
402static void srp_reconnect_work(struct work_struct *work)
403{
404 struct srp_rport *rport = container_of(to_delayed_work(work),
405 struct srp_rport, reconnect_work);
406 struct Scsi_Host *shost = rport_to_shost(rport);
407 int delay, res;
408
409 res = srp_reconnect_rport(rport);
410 if (res != 0) {
411 shost_printk(KERN_ERR, shost,
412 "reconnect attempt %d failed (%d)\n",
413 ++rport->failed_reconnects, res);
414 delay = rport->reconnect_delay *
415 min(100, max(1, rport->failed_reconnects - 10));
416 if (delay > 0)
417 queue_delayed_work(system_long_wq,
418 &rport->reconnect_work, delay * HZ);
419 }
420}
421
Bart Van Asschebe34c622015-05-18 13:22:19 +0200422/**
423 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
424 * @shost: SCSI host for which to count the number of scsi_request_fn() callers.
425 *
426 * To do: add support for scsi-mq in this function.
427 */
428static int scsi_request_fn_active(struct Scsi_Host *shost)
429{
430 struct scsi_device *sdev;
431 struct request_queue *q;
432 int request_fn_active = 0;
433
434 shost_for_each_device(sdev, shost) {
435 q = sdev->request_queue;
436
437 spin_lock_irq(q->queue_lock);
438 request_fn_active += q->request_fn_active;
439 spin_unlock_irq(q->queue_lock);
440 }
441
442 return request_fn_active;
443}
444
445/* Wait until ongoing shost->hostt->queuecommand() calls have finished. */
446static void srp_wait_for_queuecommand(struct Scsi_Host *shost)
447{
448 while (scsi_request_fn_active(shost))
449 msleep(20);
450}
451
Bart Van Assche29c17322013-10-26 14:33:30 +0200452static void __rport_fail_io_fast(struct srp_rport *rport)
453{
454 struct Scsi_Host *shost = rport_to_shost(rport);
455 struct srp_internal *i;
456
457 lockdep_assert_held(&rport->mutex);
458
459 if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
460 return;
461 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
462
463 /* Involve the LLD if possible to terminate all I/O on the rport. */
464 i = to_srp_internal(shost->transportt);
Bart Van Assche535fb902015-05-18 13:22:44 +0200465 if (i->f->terminate_rport_io) {
466 srp_wait_for_queuecommand(shost);
Bart Van Assche29c17322013-10-26 14:33:30 +0200467 i->f->terminate_rport_io(rport);
Bart Van Assche535fb902015-05-18 13:22:44 +0200468 }
Bart Van Assche29c17322013-10-26 14:33:30 +0200469}
470
471/**
472 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100473 * @work: Work structure used for scheduling this operation.
Bart Van Assche29c17322013-10-26 14:33:30 +0200474 */
475static void rport_fast_io_fail_timedout(struct work_struct *work)
476{
477 struct srp_rport *rport = container_of(to_delayed_work(work),
478 struct srp_rport, fast_io_fail_work);
479 struct Scsi_Host *shost = rport_to_shost(rport);
480
481 pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
482 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
483
484 mutex_lock(&rport->mutex);
485 if (rport->state == SRP_RPORT_BLOCKED)
486 __rport_fail_io_fast(rport);
487 mutex_unlock(&rport->mutex);
488}
489
490/**
491 * rport_dev_loss_timedout() - device loss timeout handler
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100492 * @work: Work structure used for scheduling this operation.
Bart Van Assche29c17322013-10-26 14:33:30 +0200493 */
494static void rport_dev_loss_timedout(struct work_struct *work)
495{
496 struct srp_rport *rport = container_of(to_delayed_work(work),
497 struct srp_rport, dev_loss_work);
498 struct Scsi_Host *shost = rport_to_shost(rport);
499 struct srp_internal *i = to_srp_internal(shost->transportt);
500
501 pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
502 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
503
504 mutex_lock(&rport->mutex);
505 WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
506 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
507 mutex_unlock(&rport->mutex);
508
509 i->f->rport_delete(rport);
510}
511
512static void __srp_start_tl_fail_timers(struct srp_rport *rport)
513{
514 struct Scsi_Host *shost = rport_to_shost(rport);
Bart Van Assche8c64e452013-10-26 14:35:59 +0200515 int delay, fast_io_fail_tmo, dev_loss_tmo;
Bart Van Assche29c17322013-10-26 14:33:30 +0200516
517 lockdep_assert_held(&rport->mutex);
518
Bart Van Assche93079162013-12-11 17:06:14 +0100519 delay = rport->reconnect_delay;
520 fast_io_fail_tmo = rport->fast_io_fail_tmo;
521 dev_loss_tmo = rport->dev_loss_tmo;
522 pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
523 rport->state);
Bart Van Assche29c17322013-10-26 14:33:30 +0200524
Bart Van Assche93079162013-12-11 17:06:14 +0100525 if (rport->state == SRP_RPORT_LOST)
526 return;
527 if (delay > 0)
528 queue_delayed_work(system_long_wq, &rport->reconnect_work,
529 1UL * delay * HZ);
Bart Van Asschecd53eb62014-07-09 15:56:43 +0200530 if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) &&
531 srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
Bart Van Assche93079162013-12-11 17:06:14 +0100532 pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
533 rport->state);
534 scsi_target_block(&shost->shost_gendev);
535 if (fast_io_fail_tmo >= 0)
Bart Van Assche8c64e452013-10-26 14:35:59 +0200536 queue_delayed_work(system_long_wq,
Bart Van Assche93079162013-12-11 17:06:14 +0100537 &rport->fast_io_fail_work,
538 1UL * fast_io_fail_tmo * HZ);
539 if (dev_loss_tmo >= 0)
540 queue_delayed_work(system_long_wq,
541 &rport->dev_loss_work,
542 1UL * dev_loss_tmo * HZ);
Bart Van Assche29c17322013-10-26 14:33:30 +0200543 }
544}
545
546/**
547 * srp_start_tl_fail_timers() - start the transport layer failure timers
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100548 * @rport: SRP target port.
Bart Van Assche29c17322013-10-26 14:33:30 +0200549 *
550 * Start the transport layer fast I/O failure and device loss timers. Do not
551 * modify a timer that was already started.
552 */
553void srp_start_tl_fail_timers(struct srp_rport *rport)
554{
555 mutex_lock(&rport->mutex);
556 __srp_start_tl_fail_timers(rport);
557 mutex_unlock(&rport->mutex);
558}
559EXPORT_SYMBOL(srp_start_tl_fail_timers);
560
561/**
Bart Van Assche29c17322013-10-26 14:33:30 +0200562 * srp_reconnect_rport() - reconnect to an SRP target port
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100563 * @rport: SRP target port.
Bart Van Assche29c17322013-10-26 14:33:30 +0200564 *
565 * Blocks SCSI command queueing before invoking reconnect() such that
566 * queuecommand() won't be invoked concurrently with reconnect() from outside
567 * the SCSI EH. This is important since a reconnect() implementation may
568 * reallocate resources needed by queuecommand().
569 *
570 * Notes:
571 * - This function neither waits until outstanding requests have finished nor
572 * tries to abort these. It is the responsibility of the reconnect()
573 * function to finish outstanding commands before reconnecting to the target
574 * port.
575 * - It is the responsibility of the caller to ensure that the resources
576 * reallocated by the reconnect() function won't be used while this function
577 * is in progress. One possible strategy is to invoke this function from
578 * the context of the SCSI EH thread only. Another possible strategy is to
579 * lock the rport mutex inside each SCSI LLD callback that can be invoked by
580 * the SCSI EH (the scsi_host_template.eh_*() functions and also the
581 * scsi_host_template.queuecommand() function).
582 */
583int srp_reconnect_rport(struct srp_rport *rport)
584{
585 struct Scsi_Host *shost = rport_to_shost(rport);
586 struct srp_internal *i = to_srp_internal(shost->transportt);
587 struct scsi_device *sdev;
588 int res;
589
590 pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
591
592 res = mutex_lock_interruptible(&rport->mutex);
593 if (res)
594 goto out;
595 scsi_target_block(&shost->shost_gendev);
Bart Van Asschebe34c622015-05-18 13:22:19 +0200596 srp_wait_for_queuecommand(shost);
Bart Van Assche93079162013-12-11 17:06:14 +0100597 res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
Bart Van Assche29c17322013-10-26 14:33:30 +0200598 pr_debug("%s (state %d): transport.reconnect() returned %d\n",
599 dev_name(&shost->shost_gendev), rport->state, res);
600 if (res == 0) {
601 cancel_delayed_work(&rport->fast_io_fail_work);
602 cancel_delayed_work(&rport->dev_loss_work);
603
Bart Van Assche8c64e452013-10-26 14:35:59 +0200604 rport->failed_reconnects = 0;
Bart Van Assche29c17322013-10-26 14:33:30 +0200605 srp_rport_set_state(rport, SRP_RPORT_RUNNING);
606 scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
607 /*
608 * If the SCSI error handler has offlined one or more devices,
609 * invoking scsi_target_unblock() won't change the state of
610 * these devices into running so do that explicitly.
611 */
612 spin_lock_irq(shost->host_lock);
613 __shost_for_each_device(sdev, shost)
614 if (sdev->sdev_state == SDEV_OFFLINE)
615 sdev->sdev_state = SDEV_RUNNING;
616 spin_unlock_irq(shost->host_lock);
617 } else if (rport->state == SRP_RPORT_RUNNING) {
618 /*
Bart Van Assche18cc4e02013-12-11 17:05:22 +0100619 * srp_reconnect_rport() has been invoked with fast_io_fail
620 * and dev_loss off. Mark the port as failed and start the TL
621 * failure timers if these had not yet been started.
Bart Van Assche29c17322013-10-26 14:33:30 +0200622 */
623 __rport_fail_io_fast(rport);
624 scsi_target_unblock(&shost->shost_gendev,
625 SDEV_TRANSPORT_OFFLINE);
626 __srp_start_tl_fail_timers(rport);
627 } else if (rport->state != SRP_RPORT_BLOCKED) {
628 scsi_target_unblock(&shost->shost_gendev,
629 SDEV_TRANSPORT_OFFLINE);
630 }
631 mutex_unlock(&rport->mutex);
632
633out:
634 return res;
635}
636EXPORT_SYMBOL(srp_reconnect_rport);
637
638/**
639 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100640 * @scmd: SCSI command.
Bart Van Assche29c17322013-10-26 14:33:30 +0200641 *
642 * If a timeout occurs while an rport is in the blocked state, ask the SCSI
643 * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
644 * handle the timeout (BLK_EH_NOT_HANDLED).
645 *
646 * Note: This function is called from soft-IRQ context and with the request
647 * queue lock held.
648 */
649static enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
650{
651 struct scsi_device *sdev = scmd->device;
652 struct Scsi_Host *shost = sdev->host;
653 struct srp_internal *i = to_srp_internal(shost->transportt);
Bart Van Asschee68ca752015-05-18 13:24:38 +0200654 struct srp_rport *rport = shost_to_rport(shost);
Bart Van Assche29c17322013-10-26 14:33:30 +0200655
656 pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
Bart Van Assche3875d1b2018-05-21 11:17:29 -0700657 return rport && rport->fast_io_fail_tmo < 0 &&
658 rport->dev_loss_tmo < 0 &&
Bart Van Asschee68ca752015-05-18 13:24:38 +0200659 i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
Bart Van Assche29c17322013-10-26 14:33:30 +0200660 BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
661}
662
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900663static void srp_rport_release(struct device *dev)
664{
665 struct srp_rport *rport = dev_to_rport(dev);
666
667 put_device(dev->parent);
668 kfree(rport);
669}
670
671static int scsi_is_srp_rport(const struct device *dev)
672{
673 return dev->release == srp_rport_release;
674}
675
676static int srp_rport_match(struct attribute_container *cont,
677 struct device *dev)
678{
679 struct Scsi_Host *shost;
680 struct srp_internal *i;
681
682 if (!scsi_is_srp_rport(dev))
683 return 0;
684
685 shost = dev_to_shost(dev->parent);
686 if (!shost->transportt)
687 return 0;
688 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
689 return 0;
690
691 i = to_srp_internal(shost->transportt);
692 return &i->rport_attr_cont.ac == cont;
693}
694
695static int srp_host_match(struct attribute_container *cont, struct device *dev)
696{
697 struct Scsi_Host *shost;
698 struct srp_internal *i;
699
700 if (!scsi_is_host_device(dev))
701 return 0;
702
703 shost = dev_to_shost(dev);
704 if (!shost->transportt)
705 return 0;
706 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
707 return 0;
708
709 i = to_srp_internal(shost->transportt);
710 return &i->t.host_attrs.ac == cont;
711}
712
713/**
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200714 * srp_rport_get() - increment rport reference count
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100715 * @rport: SRP target port.
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200716 */
717void srp_rport_get(struct srp_rport *rport)
718{
719 get_device(&rport->dev);
720}
721EXPORT_SYMBOL(srp_rport_get);
722
723/**
724 * srp_rport_put() - decrement rport reference count
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100725 * @rport: SRP target port.
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200726 */
727void srp_rport_put(struct srp_rport *rport)
728{
729 put_device(&rport->dev);
730}
731EXPORT_SYMBOL(srp_rport_put);
732
733/**
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900734 * srp_rport_add - add a SRP remote port to the device hierarchy
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900735 * @shost: scsi host the remote port is connected to.
736 * @ids: The port id for the remote port.
737 *
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800738 * Publishes a port to the rest of the system.
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900739 */
740struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
741 struct srp_rport_identifiers *ids)
742{
743 struct srp_rport *rport;
744 struct device *parent = &shost->shost_gendev;
Bart Van Assche29c17322013-10-26 14:33:30 +0200745 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900746 int id, ret;
747
748 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
749 if (!rport)
750 return ERR_PTR(-ENOMEM);
751
Bart Van Assche29c17322013-10-26 14:33:30 +0200752 mutex_init(&rport->mutex);
753
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900754 device_initialize(&rport->dev);
755
756 rport->dev.parent = get_device(parent);
757 rport->dev.release = srp_rport_release;
758
759 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900760 rport->roles = ids->roles;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900761
Bart Van Assche8c64e452013-10-26 14:35:59 +0200762 if (i->f->reconnect)
763 rport->reconnect_delay = i->f->reconnect_delay ?
764 *i->f->reconnect_delay : 10;
765 INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
Bart Van Assche29c17322013-10-26 14:33:30 +0200766 rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
767 *i->f->fast_io_fail_tmo : 15;
768 rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
769 INIT_DELAYED_WORK(&rport->fast_io_fail_work,
770 rport_fast_io_fail_timedout);
771 INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
772
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900773 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
Kay Sievers71610f52008-12-03 22:41:36 +0100774 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900775
776 transport_setup_device(&rport->dev);
777
778 ret = device_add(&rport->dev);
779 if (ret) {
780 transport_destroy_device(&rport->dev);
781 put_device(&rport->dev);
782 return ERR_PTR(ret);
783 }
784
785 transport_add_device(&rport->dev);
786 transport_configure_device(&rport->dev);
787
788 return rport;
789}
790EXPORT_SYMBOL_GPL(srp_rport_add);
791
792/**
Rob Landleyeb448202007-11-03 13:30:39 -0500793 * srp_rport_del - remove a SRP remote port
794 * @rport: SRP remote port to remove
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900795 *
796 * Removes the specified SRP remote port.
797 */
798void srp_rport_del(struct srp_rport *rport)
799{
800 struct device *dev = &rport->dev;
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900801
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900802 transport_remove_device(dev);
803 device_del(dev);
804 transport_destroy_device(dev);
Bart Van Assche29c17322013-10-26 14:33:30 +0200805
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900806 put_device(dev);
807}
808EXPORT_SYMBOL_GPL(srp_rport_del);
809
810static int do_srp_rport_del(struct device *dev, void *data)
811{
Dave Dillow91183342008-01-03 21:34:49 -0500812 if (scsi_is_srp_rport(dev))
813 srp_rport_del(dev_to_rport(dev));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900814 return 0;
815}
816
817/**
Rob Landleyeb448202007-11-03 13:30:39 -0500818 * srp_remove_host - tear down a Scsi_Host's SRP data structures
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900819 * @shost: Scsi Host that is torn down
820 *
821 * Removes all SRP remote ports for a given Scsi_Host.
822 * Must be called just before scsi_remove_host for SRP HBAs.
823 */
824void srp_remove_host(struct Scsi_Host *shost)
825{
826 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
827}
828EXPORT_SYMBOL_GPL(srp_remove_host);
829
Bart Van Assche93079162013-12-11 17:06:14 +0100830/**
831 * srp_stop_rport_timers - stop the transport layer recovery timers
Bart Van Assche67513602014-03-14 13:51:19 +0100832 * @rport: SRP remote port for which to stop the timers.
Bart Van Assche93079162013-12-11 17:06:14 +0100833 *
834 * Must be called after srp_remove_host() and scsi_remove_host(). The caller
835 * must hold a reference on the rport (rport->dev) and on the SCSI host
836 * (rport->dev.parent).
837 */
838void srp_stop_rport_timers(struct srp_rport *rport)
839{
840 mutex_lock(&rport->mutex);
841 if (rport->state == SRP_RPORT_BLOCKED)
842 __rport_fail_io_fast(rport);
843 srp_rport_set_state(rport, SRP_RPORT_LOST);
844 mutex_unlock(&rport->mutex);
845
846 cancel_delayed_work_sync(&rport->reconnect_work);
847 cancel_delayed_work_sync(&rport->fast_io_fail_work);
848 cancel_delayed_work_sync(&rport->dev_loss_work);
849}
850EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
851
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900852static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
853 int result)
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900854{
855 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900856 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
857}
858
859static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
860{
861 struct srp_internal *i = to_srp_internal(shost->transportt);
862 return i->f->it_nexus_response(shost, nexus, result);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900863}
864
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900865/**
Rob Landleyeb448202007-11-03 13:30:39 -0500866 * srp_attach_transport - instantiate SRP transport template
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900867 * @ft: SRP transport class function template
868 */
869struct scsi_transport_template *
870srp_attach_transport(struct srp_function_template *ft)
871{
872 int count;
873 struct srp_internal *i;
874
875 i = kzalloc(sizeof(*i), GFP_KERNEL);
876 if (!i)
877 return NULL;
878
Bart Van Assche29c17322013-10-26 14:33:30 +0200879 i->t.eh_timed_out = srp_timed_out;
880
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900881 i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900882 i->t.it_nexus_response = srp_it_nexus_response;
883
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900884 i->t.host_size = sizeof(struct srp_host_attrs);
885 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
886 i->t.host_attrs.ac.class = &srp_host_class.class;
887 i->t.host_attrs.ac.match = srp_host_match;
888 i->host_attrs[0] = NULL;
889 transport_container_register(&i->t.host_attrs);
890
891 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
892 i->rport_attr_cont.ac.class = &srp_rport_class.class;
893 i->rport_attr_cont.ac.match = srp_rport_match;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900894
895 count = 0;
Bart Van Assche9134a852011-09-10 16:31:57 +0200896 i->rport_attrs[count++] = &dev_attr_port_id;
897 i->rport_attrs[count++] = &dev_attr_roles;
Bart Van Assche29c17322013-10-26 14:33:30 +0200898 if (ft->has_rport_state) {
899 i->rport_attrs[count++] = &dev_attr_state;
900 i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
901 i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
902 }
Bart Van Assche8c64e452013-10-26 14:35:59 +0200903 if (ft->reconnect) {
904 i->rport_attrs[count++] = &dev_attr_reconnect_delay;
905 i->rport_attrs[count++] = &dev_attr_failed_reconnects;
906 }
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200907 if (ft->rport_delete)
908 i->rport_attrs[count++] = &dev_attr_delete;
Bart Van Assche9134a852011-09-10 16:31:57 +0200909 i->rport_attrs[count++] = NULL;
910 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900911
Bart Van Asscheac9be302011-10-21 19:31:07 +0200912 transport_container_register(&i->rport_attr_cont);
913
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900914 i->f = ft;
915
916 return &i->t;
917}
918EXPORT_SYMBOL_GPL(srp_attach_transport);
919
920/**
Rob Landleyeb448202007-11-03 13:30:39 -0500921 * srp_release_transport - release SRP transport template instance
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900922 * @t: transport template instance
923 */
924void srp_release_transport(struct scsi_transport_template *t)
925{
926 struct srp_internal *i = to_srp_internal(t);
927
928 transport_container_unregister(&i->t.host_attrs);
929 transport_container_unregister(&i->rport_attr_cont);
930
931 kfree(i);
932}
933EXPORT_SYMBOL_GPL(srp_release_transport);
934
935static __init int srp_transport_init(void)
936{
937 int ret;
938
939 ret = transport_class_register(&srp_host_class);
940 if (ret)
941 return ret;
942 ret = transport_class_register(&srp_rport_class);
943 if (ret)
944 goto unregister_host_class;
945
946 return 0;
947unregister_host_class:
948 transport_class_unregister(&srp_host_class);
949 return ret;
950}
951
952static void __exit srp_transport_exit(void)
953{
954 transport_class_unregister(&srp_host_class);
955 transport_class_unregister(&srp_rport_class);
956}
957
958MODULE_AUTHOR("FUJITA Tomonori");
959MODULE_DESCRIPTION("SRP Transport Attributes");
960MODULE_LICENSE("GPL");
961
962module_init(srp_transport_init);
963module_exit(srp_transport_exit);