blob: f115f67a6ba58c65d1cf07cbfcb3a0630c9f6da9 [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
64/**
65 * srp_tmo_valid() - check timeout combination validity
Bart Van Assche0c7f8212014-01-13 12:39:35 +010066 * @reconnect_delay: Reconnect delay in seconds.
67 * @fast_io_fail_tmo: Fast I/O fail timeout in seconds.
68 * @dev_loss_tmo: Device loss timeout in seconds.
Bart Van Assche29c17322013-10-26 14:33:30 +020069 *
70 * The combination of the timeout parameters must be such that SCSI commands
71 * are finished in a reasonable time. Hence do not allow the fast I/O fail
Bart Van Assche18cc4e02013-12-11 17:05:22 +010072 * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to
73 * exceed that limit if failing I/O fast has been disabled. Furthermore, these
Bart Van Assche29c17322013-10-26 14:33:30 +020074 * parameters must be such that multipath can detect failed paths timely.
Bart Van Assche8c64e452013-10-26 14:35:59 +020075 * Hence do not allow all three parameters to be disabled simultaneously.
Bart Van Assche29c17322013-10-26 14:33:30 +020076 */
Bart Van Assche8c64e452013-10-26 14:35:59 +020077int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, int dev_loss_tmo)
Bart Van Assche29c17322013-10-26 14:33:30 +020078{
Bart Van Assche8c64e452013-10-26 14:35:59 +020079 if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
80 return -EINVAL;
81 if (reconnect_delay == 0)
Bart Van Assche29c17322013-10-26 14:33:30 +020082 return -EINVAL;
83 if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
84 return -EINVAL;
Bart Van Assche18cc4e02013-12-11 17:05:22 +010085 if (fast_io_fail_tmo < 0 &&
86 dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
87 return -EINVAL;
Bart Van Assche29c17322013-10-26 14:33:30 +020088 if (dev_loss_tmo >= LONG_MAX / HZ)
89 return -EINVAL;
90 if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
91 fast_io_fail_tmo >= dev_loss_tmo)
92 return -EINVAL;
93 return 0;
94}
95EXPORT_SYMBOL_GPL(srp_tmo_valid);
FUJITA Tomonori09345f62007-06-27 16:32:39 +090096
97static int srp_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +010098 struct device *cdev)
FUJITA Tomonori09345f62007-06-27 16:32:39 +090099{
100 struct Scsi_Host *shost = dev_to_shost(dev);
101 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
102
103 atomic_set(&srp_host->next_port_id, 0);
104 return 0;
105}
106
107static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
108 NULL, NULL);
109
110static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
111 NULL, NULL, NULL);
112
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900113#define SRP_PID(p) \
114 (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
115 (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
116 (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
117 (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
118
119#define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
120 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
121
122static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100123show_srp_rport_id(struct device *dev, struct device_attribute *attr,
124 char *buf)
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900125{
Tony Jonesee959b02008-02-22 00:13:36 +0100126 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900127 return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
128}
129
Tony Jonesee959b02008-02-22 00:13:36 +0100130static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900131
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900132static const struct {
133 u32 value;
134 char *name;
135} srp_rport_role_names[] = {
136 {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
137 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
138};
139
140static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100141show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
142 char *buf)
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900143{
Tony Jonesee959b02008-02-22 00:13:36 +0100144 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900145 int i;
146 char *name = NULL;
147
148 for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
149 if (srp_rport_role_names[i].value == rport->roles) {
150 name = srp_rport_role_names[i].name;
151 break;
152 }
153 return sprintf(buf, "%s\n", name ? : "unknown");
154}
155
Tony Jonesee959b02008-02-22 00:13:36 +0100156static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900157
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200158static ssize_t store_srp_rport_delete(struct device *dev,
159 struct device_attribute *attr,
160 const char *buf, size_t count)
161{
162 struct srp_rport *rport = transport_class_to_srp_rport(dev);
163 struct Scsi_Host *shost = dev_to_shost(dev);
164 struct srp_internal *i = to_srp_internal(shost->transportt);
165
166 if (i->f->rport_delete) {
167 i->f->rport_delete(rport);
168 return count;
169 } else {
170 return -ENOSYS;
171 }
172}
173
174static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
175
Bart Van Assche29c17322013-10-26 14:33:30 +0200176static ssize_t show_srp_rport_state(struct device *dev,
177 struct device_attribute *attr,
178 char *buf)
179{
180 static const char *const state_name[] = {
181 [SRP_RPORT_RUNNING] = "running",
182 [SRP_RPORT_BLOCKED] = "blocked",
183 [SRP_RPORT_FAIL_FAST] = "fail-fast",
184 [SRP_RPORT_LOST] = "lost",
185 };
186 struct srp_rport *rport = transport_class_to_srp_rport(dev);
187 enum srp_rport_state state = rport->state;
188
189 return sprintf(buf, "%s\n",
190 (unsigned)state < ARRAY_SIZE(state_name) ?
191 state_name[state] : "???");
192}
193
194static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
195
196static ssize_t srp_show_tmo(char *buf, int tmo)
197{
198 return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
199}
200
201static int srp_parse_tmo(int *tmo, const char *buf)
202{
203 int res = 0;
204
205 if (strncmp(buf, "off", 3) != 0)
206 res = kstrtoint(buf, 0, tmo);
207 else
208 *tmo = -1;
209
210 return res;
211}
212
Bart Van Assche8c64e452013-10-26 14:35:59 +0200213static ssize_t show_reconnect_delay(struct device *dev,
214 struct device_attribute *attr, char *buf)
215{
216 struct srp_rport *rport = transport_class_to_srp_rport(dev);
217
218 return srp_show_tmo(buf, rport->reconnect_delay);
219}
220
221static ssize_t store_reconnect_delay(struct device *dev,
222 struct device_attribute *attr,
223 const char *buf, const size_t count)
224{
225 struct srp_rport *rport = transport_class_to_srp_rport(dev);
226 int res, delay;
227
228 res = srp_parse_tmo(&delay, buf);
229 if (res)
230 goto out;
231 res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
232 rport->dev_loss_tmo);
233 if (res)
234 goto out;
235
236 if (rport->reconnect_delay <= 0 && delay > 0 &&
237 rport->state != SRP_RPORT_RUNNING) {
238 queue_delayed_work(system_long_wq, &rport->reconnect_work,
239 delay * HZ);
240 } else if (delay <= 0) {
241 cancel_delayed_work(&rport->reconnect_work);
242 }
243 rport->reconnect_delay = delay;
244 res = count;
245
246out:
247 return res;
248}
249
250static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
251 store_reconnect_delay);
252
253static ssize_t show_failed_reconnects(struct device *dev,
254 struct device_attribute *attr, char *buf)
255{
256 struct srp_rport *rport = transport_class_to_srp_rport(dev);
257
258 return sprintf(buf, "%d\n", rport->failed_reconnects);
259}
260
261static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
262
Bart Van Assche29c17322013-10-26 14:33:30 +0200263static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
264 struct device_attribute *attr,
265 char *buf)
266{
267 struct srp_rport *rport = transport_class_to_srp_rport(dev);
268
269 return srp_show_tmo(buf, rport->fast_io_fail_tmo);
270}
271
272static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
273 struct device_attribute *attr,
274 const char *buf, size_t count)
275{
276 struct srp_rport *rport = transport_class_to_srp_rport(dev);
277 int res;
278 int fast_io_fail_tmo;
279
280 res = srp_parse_tmo(&fast_io_fail_tmo, buf);
281 if (res)
282 goto out;
Bart Van Assche8c64e452013-10-26 14:35:59 +0200283 res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
284 rport->dev_loss_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200285 if (res)
286 goto out;
287 rport->fast_io_fail_tmo = fast_io_fail_tmo;
288 res = count;
289
290out:
291 return res;
292}
293
294static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
295 show_srp_rport_fast_io_fail_tmo,
296 store_srp_rport_fast_io_fail_tmo);
297
298static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
299 struct device_attribute *attr,
300 char *buf)
301{
302 struct srp_rport *rport = transport_class_to_srp_rport(dev);
303
304 return srp_show_tmo(buf, rport->dev_loss_tmo);
305}
306
307static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
308 struct device_attribute *attr,
309 const char *buf, size_t count)
310{
311 struct srp_rport *rport = transport_class_to_srp_rport(dev);
312 int res;
313 int dev_loss_tmo;
314
315 res = srp_parse_tmo(&dev_loss_tmo, buf);
316 if (res)
317 goto out;
Bart Van Assche8c64e452013-10-26 14:35:59 +0200318 res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
319 dev_loss_tmo);
Bart Van Assche29c17322013-10-26 14:33:30 +0200320 if (res)
321 goto out;
322 rport->dev_loss_tmo = dev_loss_tmo;
323 res = count;
324
325out:
326 return res;
327}
328
329static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
330 show_srp_rport_dev_loss_tmo,
331 store_srp_rport_dev_loss_tmo);
332
333static int srp_rport_set_state(struct srp_rport *rport,
334 enum srp_rport_state new_state)
335{
336 enum srp_rport_state old_state = rport->state;
337
338 lockdep_assert_held(&rport->mutex);
339
340 switch (new_state) {
341 case SRP_RPORT_RUNNING:
342 switch (old_state) {
343 case SRP_RPORT_LOST:
344 goto invalid;
345 default:
346 break;
347 }
348 break;
349 case SRP_RPORT_BLOCKED:
350 switch (old_state) {
351 case SRP_RPORT_RUNNING:
352 break;
353 default:
354 goto invalid;
355 }
356 break;
357 case SRP_RPORT_FAIL_FAST:
358 switch (old_state) {
359 case SRP_RPORT_LOST:
360 goto invalid;
361 default:
362 break;
363 }
364 break;
365 case SRP_RPORT_LOST:
366 break;
367 }
368 rport->state = new_state;
369 return 0;
370
371invalid:
372 return -EINVAL;
373}
374
Bart Van Assche8c64e452013-10-26 14:35:59 +0200375/**
376 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100377 * @work: Work structure used for scheduling this operation.
Bart Van Assche8c64e452013-10-26 14:35:59 +0200378 */
379static void srp_reconnect_work(struct work_struct *work)
380{
381 struct srp_rport *rport = container_of(to_delayed_work(work),
382 struct srp_rport, reconnect_work);
383 struct Scsi_Host *shost = rport_to_shost(rport);
384 int delay, res;
385
386 res = srp_reconnect_rport(rport);
387 if (res != 0) {
388 shost_printk(KERN_ERR, shost,
389 "reconnect attempt %d failed (%d)\n",
390 ++rport->failed_reconnects, res);
391 delay = rport->reconnect_delay *
392 min(100, max(1, rport->failed_reconnects - 10));
393 if (delay > 0)
394 queue_delayed_work(system_long_wq,
395 &rport->reconnect_work, delay * HZ);
396 }
397}
398
Bart Van Asschebe34c622015-05-18 13:22:19 +0200399/**
400 * scsi_request_fn_active() - number of kernel threads inside scsi_request_fn()
401 * @shost: SCSI host for which to count the number of scsi_request_fn() callers.
402 *
403 * To do: add support for scsi-mq in this function.
404 */
405static int scsi_request_fn_active(struct Scsi_Host *shost)
406{
407 struct scsi_device *sdev;
408 struct request_queue *q;
409 int request_fn_active = 0;
410
411 shost_for_each_device(sdev, shost) {
412 q = sdev->request_queue;
413
414 spin_lock_irq(q->queue_lock);
415 request_fn_active += q->request_fn_active;
416 spin_unlock_irq(q->queue_lock);
417 }
418
419 return request_fn_active;
420}
421
422/* Wait until ongoing shost->hostt->queuecommand() calls have finished. */
423static void srp_wait_for_queuecommand(struct Scsi_Host *shost)
424{
425 while (scsi_request_fn_active(shost))
426 msleep(20);
427}
428
Bart Van Assche29c17322013-10-26 14:33:30 +0200429static void __rport_fail_io_fast(struct srp_rport *rport)
430{
431 struct Scsi_Host *shost = rport_to_shost(rport);
432 struct srp_internal *i;
433
434 lockdep_assert_held(&rport->mutex);
435
436 if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
437 return;
438 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
439
440 /* Involve the LLD if possible to terminate all I/O on the rport. */
441 i = to_srp_internal(shost->transportt);
Bart Van Assche535fb902015-05-18 13:22:44 +0200442 if (i->f->terminate_rport_io) {
443 srp_wait_for_queuecommand(shost);
Bart Van Assche29c17322013-10-26 14:33:30 +0200444 i->f->terminate_rport_io(rport);
Bart Van Assche535fb902015-05-18 13:22:44 +0200445 }
Bart Van Assche29c17322013-10-26 14:33:30 +0200446}
447
448/**
449 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100450 * @work: Work structure used for scheduling this operation.
Bart Van Assche29c17322013-10-26 14:33:30 +0200451 */
452static void rport_fast_io_fail_timedout(struct work_struct *work)
453{
454 struct srp_rport *rport = container_of(to_delayed_work(work),
455 struct srp_rport, fast_io_fail_work);
456 struct Scsi_Host *shost = rport_to_shost(rport);
457
458 pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
459 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
460
461 mutex_lock(&rport->mutex);
462 if (rport->state == SRP_RPORT_BLOCKED)
463 __rport_fail_io_fast(rport);
464 mutex_unlock(&rport->mutex);
465}
466
467/**
468 * rport_dev_loss_timedout() - device loss timeout handler
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100469 * @work: Work structure used for scheduling this operation.
Bart Van Assche29c17322013-10-26 14:33:30 +0200470 */
471static void rport_dev_loss_timedout(struct work_struct *work)
472{
473 struct srp_rport *rport = container_of(to_delayed_work(work),
474 struct srp_rport, dev_loss_work);
475 struct Scsi_Host *shost = rport_to_shost(rport);
476 struct srp_internal *i = to_srp_internal(shost->transportt);
477
478 pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
479 dev_name(&rport->dev), dev_name(&shost->shost_gendev));
480
481 mutex_lock(&rport->mutex);
482 WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
483 scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
484 mutex_unlock(&rport->mutex);
485
486 i->f->rport_delete(rport);
487}
488
489static void __srp_start_tl_fail_timers(struct srp_rport *rport)
490{
491 struct Scsi_Host *shost = rport_to_shost(rport);
Bart Van Assche8c64e452013-10-26 14:35:59 +0200492 int delay, fast_io_fail_tmo, dev_loss_tmo;
Bart Van Assche29c17322013-10-26 14:33:30 +0200493
494 lockdep_assert_held(&rport->mutex);
495
Bart Van Assche93079162013-12-11 17:06:14 +0100496 delay = rport->reconnect_delay;
497 fast_io_fail_tmo = rport->fast_io_fail_tmo;
498 dev_loss_tmo = rport->dev_loss_tmo;
499 pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
500 rport->state);
Bart Van Assche29c17322013-10-26 14:33:30 +0200501
Bart Van Assche93079162013-12-11 17:06:14 +0100502 if (rport->state == SRP_RPORT_LOST)
503 return;
504 if (delay > 0)
505 queue_delayed_work(system_long_wq, &rport->reconnect_work,
506 1UL * delay * HZ);
Bart Van Asschecd53eb62014-07-09 15:56:43 +0200507 if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) &&
508 srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
Bart Van Assche93079162013-12-11 17:06:14 +0100509 pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
510 rport->state);
511 scsi_target_block(&shost->shost_gendev);
512 if (fast_io_fail_tmo >= 0)
Bart Van Assche8c64e452013-10-26 14:35:59 +0200513 queue_delayed_work(system_long_wq,
Bart Van Assche93079162013-12-11 17:06:14 +0100514 &rport->fast_io_fail_work,
515 1UL * fast_io_fail_tmo * HZ);
516 if (dev_loss_tmo >= 0)
517 queue_delayed_work(system_long_wq,
518 &rport->dev_loss_work,
519 1UL * dev_loss_tmo * HZ);
Bart Van Assche29c17322013-10-26 14:33:30 +0200520 }
521}
522
523/**
524 * srp_start_tl_fail_timers() - start the transport layer failure timers
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100525 * @rport: SRP target port.
Bart Van Assche29c17322013-10-26 14:33:30 +0200526 *
527 * Start the transport layer fast I/O failure and device loss timers. Do not
528 * modify a timer that was already started.
529 */
530void srp_start_tl_fail_timers(struct srp_rport *rport)
531{
532 mutex_lock(&rport->mutex);
533 __srp_start_tl_fail_timers(rport);
534 mutex_unlock(&rport->mutex);
535}
536EXPORT_SYMBOL(srp_start_tl_fail_timers);
537
538/**
Bart Van Assche29c17322013-10-26 14:33:30 +0200539 * srp_reconnect_rport() - reconnect to an SRP target port
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100540 * @rport: SRP target port.
Bart Van Assche29c17322013-10-26 14:33:30 +0200541 *
542 * Blocks SCSI command queueing before invoking reconnect() such that
543 * queuecommand() won't be invoked concurrently with reconnect() from outside
544 * the SCSI EH. This is important since a reconnect() implementation may
545 * reallocate resources needed by queuecommand().
546 *
547 * Notes:
548 * - This function neither waits until outstanding requests have finished nor
549 * tries to abort these. It is the responsibility of the reconnect()
550 * function to finish outstanding commands before reconnecting to the target
551 * port.
552 * - It is the responsibility of the caller to ensure that the resources
553 * reallocated by the reconnect() function won't be used while this function
554 * is in progress. One possible strategy is to invoke this function from
555 * the context of the SCSI EH thread only. Another possible strategy is to
556 * lock the rport mutex inside each SCSI LLD callback that can be invoked by
557 * the SCSI EH (the scsi_host_template.eh_*() functions and also the
558 * scsi_host_template.queuecommand() function).
559 */
560int srp_reconnect_rport(struct srp_rport *rport)
561{
562 struct Scsi_Host *shost = rport_to_shost(rport);
563 struct srp_internal *i = to_srp_internal(shost->transportt);
564 struct scsi_device *sdev;
565 int res;
566
567 pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
568
569 res = mutex_lock_interruptible(&rport->mutex);
570 if (res)
571 goto out;
572 scsi_target_block(&shost->shost_gendev);
Bart Van Asschebe34c622015-05-18 13:22:19 +0200573 srp_wait_for_queuecommand(shost);
Bart Van Assche93079162013-12-11 17:06:14 +0100574 res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
Bart Van Assche29c17322013-10-26 14:33:30 +0200575 pr_debug("%s (state %d): transport.reconnect() returned %d\n",
576 dev_name(&shost->shost_gendev), rport->state, res);
577 if (res == 0) {
578 cancel_delayed_work(&rport->fast_io_fail_work);
579 cancel_delayed_work(&rport->dev_loss_work);
580
Bart Van Assche8c64e452013-10-26 14:35:59 +0200581 rport->failed_reconnects = 0;
Bart Van Assche29c17322013-10-26 14:33:30 +0200582 srp_rport_set_state(rport, SRP_RPORT_RUNNING);
583 scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
584 /*
585 * If the SCSI error handler has offlined one or more devices,
586 * invoking scsi_target_unblock() won't change the state of
587 * these devices into running so do that explicitly.
588 */
589 spin_lock_irq(shost->host_lock);
590 __shost_for_each_device(sdev, shost)
591 if (sdev->sdev_state == SDEV_OFFLINE)
592 sdev->sdev_state = SDEV_RUNNING;
593 spin_unlock_irq(shost->host_lock);
594 } else if (rport->state == SRP_RPORT_RUNNING) {
595 /*
Bart Van Assche18cc4e02013-12-11 17:05:22 +0100596 * srp_reconnect_rport() has been invoked with fast_io_fail
597 * and dev_loss off. Mark the port as failed and start the TL
598 * failure timers if these had not yet been started.
Bart Van Assche29c17322013-10-26 14:33:30 +0200599 */
600 __rport_fail_io_fast(rport);
601 scsi_target_unblock(&shost->shost_gendev,
602 SDEV_TRANSPORT_OFFLINE);
603 __srp_start_tl_fail_timers(rport);
604 } else if (rport->state != SRP_RPORT_BLOCKED) {
605 scsi_target_unblock(&shost->shost_gendev,
606 SDEV_TRANSPORT_OFFLINE);
607 }
608 mutex_unlock(&rport->mutex);
609
610out:
611 return res;
612}
613EXPORT_SYMBOL(srp_reconnect_rport);
614
615/**
616 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100617 * @scmd: SCSI command.
Bart Van Assche29c17322013-10-26 14:33:30 +0200618 *
619 * If a timeout occurs while an rport is in the blocked state, ask the SCSI
620 * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
621 * handle the timeout (BLK_EH_NOT_HANDLED).
622 *
623 * Note: This function is called from soft-IRQ context and with the request
624 * queue lock held.
625 */
626static enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
627{
628 struct scsi_device *sdev = scmd->device;
629 struct Scsi_Host *shost = sdev->host;
630 struct srp_internal *i = to_srp_internal(shost->transportt);
631
632 pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
633 return i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
634 BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
635}
636
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900637static void srp_rport_release(struct device *dev)
638{
639 struct srp_rport *rport = dev_to_rport(dev);
640
641 put_device(dev->parent);
642 kfree(rport);
643}
644
645static int scsi_is_srp_rport(const struct device *dev)
646{
647 return dev->release == srp_rport_release;
648}
649
650static int srp_rport_match(struct attribute_container *cont,
651 struct device *dev)
652{
653 struct Scsi_Host *shost;
654 struct srp_internal *i;
655
656 if (!scsi_is_srp_rport(dev))
657 return 0;
658
659 shost = dev_to_shost(dev->parent);
660 if (!shost->transportt)
661 return 0;
662 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
663 return 0;
664
665 i = to_srp_internal(shost->transportt);
666 return &i->rport_attr_cont.ac == cont;
667}
668
669static int srp_host_match(struct attribute_container *cont, struct device *dev)
670{
671 struct Scsi_Host *shost;
672 struct srp_internal *i;
673
674 if (!scsi_is_host_device(dev))
675 return 0;
676
677 shost = dev_to_shost(dev);
678 if (!shost->transportt)
679 return 0;
680 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
681 return 0;
682
683 i = to_srp_internal(shost->transportt);
684 return &i->t.host_attrs.ac == cont;
685}
686
687/**
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200688 * srp_rport_get() - increment rport reference count
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100689 * @rport: SRP target port.
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200690 */
691void srp_rport_get(struct srp_rport *rport)
692{
693 get_device(&rport->dev);
694}
695EXPORT_SYMBOL(srp_rport_get);
696
697/**
698 * srp_rport_put() - decrement rport reference count
Bart Van Assche0c7f8212014-01-13 12:39:35 +0100699 * @rport: SRP target port.
Bart Van Assche9dd69a62013-10-26 14:32:30 +0200700 */
701void srp_rport_put(struct srp_rport *rport)
702{
703 put_device(&rport->dev);
704}
705EXPORT_SYMBOL(srp_rport_put);
706
707/**
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900708 * srp_rport_add - add a SRP remote port to the device hierarchy
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900709 * @shost: scsi host the remote port is connected to.
710 * @ids: The port id for the remote port.
711 *
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800712 * Publishes a port to the rest of the system.
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900713 */
714struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
715 struct srp_rport_identifiers *ids)
716{
717 struct srp_rport *rport;
718 struct device *parent = &shost->shost_gendev;
Bart Van Assche29c17322013-10-26 14:33:30 +0200719 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900720 int id, ret;
721
722 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
723 if (!rport)
724 return ERR_PTR(-ENOMEM);
725
Bart Van Assche29c17322013-10-26 14:33:30 +0200726 mutex_init(&rport->mutex);
727
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900728 device_initialize(&rport->dev);
729
730 rport->dev.parent = get_device(parent);
731 rport->dev.release = srp_rport_release;
732
733 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900734 rport->roles = ids->roles;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900735
Bart Van Assche8c64e452013-10-26 14:35:59 +0200736 if (i->f->reconnect)
737 rport->reconnect_delay = i->f->reconnect_delay ?
738 *i->f->reconnect_delay : 10;
739 INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
Bart Van Assche29c17322013-10-26 14:33:30 +0200740 rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
741 *i->f->fast_io_fail_tmo : 15;
742 rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
743 INIT_DELAYED_WORK(&rport->fast_io_fail_work,
744 rport_fast_io_fail_timedout);
745 INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
746
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900747 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
Kay Sievers71610f52008-12-03 22:41:36 +0100748 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900749
750 transport_setup_device(&rport->dev);
751
752 ret = device_add(&rport->dev);
753 if (ret) {
754 transport_destroy_device(&rport->dev);
755 put_device(&rport->dev);
756 return ERR_PTR(ret);
757 }
758
759 transport_add_device(&rport->dev);
760 transport_configure_device(&rport->dev);
761
762 return rport;
763}
764EXPORT_SYMBOL_GPL(srp_rport_add);
765
766/**
Rob Landleyeb448202007-11-03 13:30:39 -0500767 * srp_rport_del - remove a SRP remote port
768 * @rport: SRP remote port to remove
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900769 *
770 * Removes the specified SRP remote port.
771 */
772void srp_rport_del(struct srp_rport *rport)
773{
774 struct device *dev = &rport->dev;
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900775
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900776 transport_remove_device(dev);
777 device_del(dev);
778 transport_destroy_device(dev);
Bart Van Assche29c17322013-10-26 14:33:30 +0200779
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900780 put_device(dev);
781}
782EXPORT_SYMBOL_GPL(srp_rport_del);
783
784static int do_srp_rport_del(struct device *dev, void *data)
785{
Dave Dillow91183342008-01-03 21:34:49 -0500786 if (scsi_is_srp_rport(dev))
787 srp_rport_del(dev_to_rport(dev));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900788 return 0;
789}
790
791/**
Rob Landleyeb448202007-11-03 13:30:39 -0500792 * srp_remove_host - tear down a Scsi_Host's SRP data structures
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900793 * @shost: Scsi Host that is torn down
794 *
795 * Removes all SRP remote ports for a given Scsi_Host.
796 * Must be called just before scsi_remove_host for SRP HBAs.
797 */
798void srp_remove_host(struct Scsi_Host *shost)
799{
800 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
801}
802EXPORT_SYMBOL_GPL(srp_remove_host);
803
Bart Van Assche93079162013-12-11 17:06:14 +0100804/**
805 * srp_stop_rport_timers - stop the transport layer recovery timers
Bart Van Assche67513602014-03-14 13:51:19 +0100806 * @rport: SRP remote port for which to stop the timers.
Bart Van Assche93079162013-12-11 17:06:14 +0100807 *
808 * Must be called after srp_remove_host() and scsi_remove_host(). The caller
809 * must hold a reference on the rport (rport->dev) and on the SCSI host
810 * (rport->dev.parent).
811 */
812void srp_stop_rport_timers(struct srp_rport *rport)
813{
814 mutex_lock(&rport->mutex);
815 if (rport->state == SRP_RPORT_BLOCKED)
816 __rport_fail_io_fast(rport);
817 srp_rport_set_state(rport, SRP_RPORT_LOST);
818 mutex_unlock(&rport->mutex);
819
820 cancel_delayed_work_sync(&rport->reconnect_work);
821 cancel_delayed_work_sync(&rport->fast_io_fail_work);
822 cancel_delayed_work_sync(&rport->dev_loss_work);
823}
824EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
825
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900826static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
827 int result)
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900828{
829 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900830 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
831}
832
833static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
834{
835 struct srp_internal *i = to_srp_internal(shost->transportt);
836 return i->f->it_nexus_response(shost, nexus, result);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900837}
838
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900839/**
Rob Landleyeb448202007-11-03 13:30:39 -0500840 * srp_attach_transport - instantiate SRP transport template
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900841 * @ft: SRP transport class function template
842 */
843struct scsi_transport_template *
844srp_attach_transport(struct srp_function_template *ft)
845{
846 int count;
847 struct srp_internal *i;
848
849 i = kzalloc(sizeof(*i), GFP_KERNEL);
850 if (!i)
851 return NULL;
852
Bart Van Assche29c17322013-10-26 14:33:30 +0200853 i->t.eh_timed_out = srp_timed_out;
854
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900855 i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900856 i->t.it_nexus_response = srp_it_nexus_response;
857
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900858 i->t.host_size = sizeof(struct srp_host_attrs);
859 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
860 i->t.host_attrs.ac.class = &srp_host_class.class;
861 i->t.host_attrs.ac.match = srp_host_match;
862 i->host_attrs[0] = NULL;
863 transport_container_register(&i->t.host_attrs);
864
865 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
866 i->rport_attr_cont.ac.class = &srp_rport_class.class;
867 i->rport_attr_cont.ac.match = srp_rport_match;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900868
869 count = 0;
Bart Van Assche9134a852011-09-10 16:31:57 +0200870 i->rport_attrs[count++] = &dev_attr_port_id;
871 i->rport_attrs[count++] = &dev_attr_roles;
Bart Van Assche29c17322013-10-26 14:33:30 +0200872 if (ft->has_rport_state) {
873 i->rport_attrs[count++] = &dev_attr_state;
874 i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
875 i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
876 }
Bart Van Assche8c64e452013-10-26 14:35:59 +0200877 if (ft->reconnect) {
878 i->rport_attrs[count++] = &dev_attr_reconnect_delay;
879 i->rport_attrs[count++] = &dev_attr_failed_reconnects;
880 }
Bart Van Asschedc1bdbd2011-09-16 20:41:13 +0200881 if (ft->rport_delete)
882 i->rport_attrs[count++] = &dev_attr_delete;
Bart Van Assche9134a852011-09-10 16:31:57 +0200883 i->rport_attrs[count++] = NULL;
884 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900885
Bart Van Asscheac9be302011-10-21 19:31:07 +0200886 transport_container_register(&i->rport_attr_cont);
887
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900888 i->f = ft;
889
890 return &i->t;
891}
892EXPORT_SYMBOL_GPL(srp_attach_transport);
893
894/**
Rob Landleyeb448202007-11-03 13:30:39 -0500895 * srp_release_transport - release SRP transport template instance
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900896 * @t: transport template instance
897 */
898void srp_release_transport(struct scsi_transport_template *t)
899{
900 struct srp_internal *i = to_srp_internal(t);
901
902 transport_container_unregister(&i->t.host_attrs);
903 transport_container_unregister(&i->rport_attr_cont);
904
905 kfree(i);
906}
907EXPORT_SYMBOL_GPL(srp_release_transport);
908
909static __init int srp_transport_init(void)
910{
911 int ret;
912
913 ret = transport_class_register(&srp_host_class);
914 if (ret)
915 return ret;
916 ret = transport_class_register(&srp_rport_class);
917 if (ret)
918 goto unregister_host_class;
919
920 return 0;
921unregister_host_class:
922 transport_class_unregister(&srp_host_class);
923 return ret;
924}
925
926static void __exit srp_transport_exit(void)
927{
928 transport_class_unregister(&srp_host_class);
929 transport_class_unregister(&srp_rport_class);
930}
931
932MODULE_AUTHOR("FUJITA Tomonori");
933MODULE_DESCRIPTION("SRP Transport Attributes");
934MODULE_LICENSE("GPL");
935
936module_init(srp_transport_init);
937module_exit(srp_transport_exit);