Commit | Line | Data |
---|---|---|
0e086994 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
09345f65 FT |
2 | /* |
3 | * SCSI RDMA (SRP) transport class | |
4 | * | |
5 | * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org> | |
09345f65 FT |
6 | */ |
7 | #include <linux/init.h> | |
8 | #include <linux/module.h> | |
9 | #include <linux/jiffies.h> | |
10 | #include <linux/err.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/string.h> | |
13 | ||
14 | #include <scsi/scsi.h> | |
29c17324 | 15 | #include <scsi/scsi_cmnd.h> |
09345f65 FT |
16 | #include <scsi/scsi_device.h> |
17 | #include <scsi/scsi_host.h> | |
18 | #include <scsi/scsi_transport.h> | |
19 | #include <scsi/scsi_transport_srp.h> | |
29c17324 | 20 | #include "scsi_priv.h" |
09345f65 FT |
21 | |
22 | struct srp_host_attrs { | |
23 | atomic_t next_port_id; | |
24 | }; | |
25 | #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data) | |
26 | ||
27 | #define SRP_HOST_ATTRS 0 | |
8c64e453 | 28 | #define SRP_RPORT_ATTRS 8 |
09345f65 FT |
29 | |
30 | struct srp_internal { | |
31 | struct scsi_transport_template t; | |
32 | struct srp_function_template *f; | |
33 | ||
ee959b00 | 34 | struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1]; |
09345f65 | 35 | |
ee959b00 | 36 | struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1]; |
09345f65 FT |
37 | struct transport_container rport_attr_cont; |
38 | }; | |
39 | ||
c9ddf734 BVA |
40 | static int scsi_is_srp_rport(const struct device *dev); |
41 | ||
09345f65 FT |
42 | #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t) |
43 | ||
44 | #define dev_to_rport(d) container_of(d, struct srp_rport, dev) | |
ee959b00 | 45 | #define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent) |
29c17324 BVA |
46 | static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r) |
47 | { | |
48 | return dev_to_shost(r->dev.parent); | |
49 | } | |
50 | ||
c9ddf734 BVA |
51 | static int find_child_rport(struct device *dev, void *data) |
52 | { | |
53 | struct device **child = data; | |
54 | ||
55 | if (scsi_is_srp_rport(dev)) { | |
56 | WARN_ON_ONCE(*child); | |
57 | *child = dev; | |
58 | } | |
59 | return 0; | |
60 | } | |
61 | ||
e68ca752 BVA |
62 | static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost) |
63 | { | |
c9ddf734 BVA |
64 | struct device *child = NULL; |
65 | ||
66 | WARN_ON_ONCE(device_for_each_child(&shost->shost_gendev, &child, | |
67 | find_child_rport) < 0); | |
68 | return child ? dev_to_rport(child) : NULL; | |
e68ca752 BVA |
69 | } |
70 | ||
29c17324 BVA |
71 | /** |
72 | * srp_tmo_valid() - check timeout combination validity | |
0c7f8218 BVA |
73 | * @reconnect_delay: Reconnect delay in seconds. |
74 | * @fast_io_fail_tmo: Fast I/O fail timeout in seconds. | |
75 | * @dev_loss_tmo: Device loss timeout in seconds. | |
29c17324 BVA |
76 | * |
77 | * The combination of the timeout parameters must be such that SCSI commands | |
78 | * are finished in a reasonable time. Hence do not allow the fast I/O fail | |
18cc4e02 BVA |
79 | * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to |
80 | * exceed that limit if failing I/O fast has been disabled. Furthermore, these | |
29c17324 | 81 | * parameters must be such that multipath can detect failed paths timely. |
8c64e453 | 82 | * Hence do not allow all three parameters to be disabled simultaneously. |
29c17324 | 83 | */ |
85aa93be | 84 | int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, long dev_loss_tmo) |
29c17324 | 85 | { |
8c64e453 BVA |
86 | if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0) |
87 | return -EINVAL; | |
88 | if (reconnect_delay == 0) | |
29c17324 BVA |
89 | return -EINVAL; |
90 | if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT) | |
91 | return -EINVAL; | |
18cc4e02 BVA |
92 | if (fast_io_fail_tmo < 0 && |
93 | dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT) | |
94 | return -EINVAL; | |
29c17324 BVA |
95 | if (dev_loss_tmo >= LONG_MAX / HZ) |
96 | return -EINVAL; | |
97 | if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 && | |
98 | fast_io_fail_tmo >= dev_loss_tmo) | |
99 | return -EINVAL; | |
100 | return 0; | |
101 | } | |
102 | EXPORT_SYMBOL_GPL(srp_tmo_valid); | |
09345f65 FT |
103 | |
104 | static int srp_host_setup(struct transport_container *tc, struct device *dev, | |
ee959b00 | 105 | struct device *cdev) |
09345f65 FT |
106 | { |
107 | struct Scsi_Host *shost = dev_to_shost(dev); | |
108 | struct srp_host_attrs *srp_host = to_srp_host_attrs(shost); | |
109 | ||
110 | atomic_set(&srp_host->next_port_id, 0); | |
111 | return 0; | |
112 | } | |
113 | ||
114 | static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup, | |
115 | NULL, NULL); | |
116 | ||
117 | static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports", | |
118 | NULL, NULL, NULL); | |
119 | ||
09345f65 | 120 | static ssize_t |
ee959b00 TJ |
121 | show_srp_rport_id(struct device *dev, struct device_attribute *attr, |
122 | char *buf) | |
09345f65 | 123 | { |
ee959b00 | 124 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
df441cc0 | 125 | return sprintf(buf, "%16phC\n", rport->port_id); |
09345f65 FT |
126 | } |
127 | ||
ee959b00 | 128 | static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL); |
09345f65 | 129 | |
aebd5e47 FT |
130 | static const struct { |
131 | u32 value; | |
132 | char *name; | |
133 | } srp_rport_role_names[] = { | |
134 | {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"}, | |
135 | {SRP_RPORT_ROLE_TARGET, "SRP Target"}, | |
136 | }; | |
137 | ||
138 | static ssize_t | |
ee959b00 TJ |
139 | show_srp_rport_roles(struct device *dev, struct device_attribute *attr, |
140 | char *buf) | |
aebd5e47 | 141 | { |
ee959b00 | 142 | struct srp_rport *rport = transport_class_to_srp_rport(dev); |
aebd5e47 FT |
143 | int i; |
144 | char *name = NULL; | |
145 | ||
146 | for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++) | |
147 | if (srp_rport_role_names[i].value == rport->roles) { | |
148 | name = srp_rport_role_names[i].name; | |
149 | break; | |
150 | } | |
151 | return sprintf(buf, "%s\n", name ? : "unknown"); | |
152 | } | |
153 | ||
ee959b00 | 154 | static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL); |
aebd5e47 | 155 | |
dc1bdbd9 BVA |
156 | static ssize_t store_srp_rport_delete(struct device *dev, |
157 | struct device_attribute *attr, | |
158 | const char *buf, size_t count) | |
159 | { | |
160 | struct srp_rport *rport = transport_class_to_srp_rport(dev); | |
161 | struct Scsi_Host *shost = dev_to_shost(dev); | |
162 | struct srp_internal *i = to_srp_internal(shost->transportt); | |
163 | ||
164 | if (i->f->rport_delete) { | |
165 | i->f->rport_delete(rport); | |
166 | return count; | |
167 | } else { | |
168 | return -ENOSYS; | |
169 | } | |
170 | } | |
171 | ||
172 | static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete); | |
173 | ||
29c17324 BVA |
174 | static ssize_t show_srp_rport_state(struct device *dev, |
175 | struct device_attribute *attr, | |
176 | char *buf) | |
177 | { | |
178 | static const char *const state_name[] = { | |
179 | [SRP_RPORT_RUNNING] = "running", | |
180 | [SRP_RPORT_BLOCKED] = "blocked", | |
181 | [SRP_RPORT_FAIL_FAST] = "fail-fast", | |
182 | [SRP_RPORT_LOST] = "lost", | |
183 | }; | |
184 | struct srp_rport *rport = transport_class_to_srp_rport(dev); | |
185 | enum srp_rport_state state = rport->state; | |
186 | ||
187 | return sprintf(buf, "%s\n", | |
188 | (unsigned)state < ARRAY_SIZE(state_name) ? | |
189 | state_name[state] : "???"); | |
190 | } | |
191 | ||
192 | static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL); | |
193 | ||
194 | static ssize_t srp_show_tmo(char *buf, int tmo) | |
195 | { | |
196 | return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n"); | |
197 | } | |
198 | ||
3fdf70ac | 199 | int srp_parse_tmo(int *tmo, const char *buf) |
29c17324 BVA |
200 | { |
201 | int res = 0; | |
202 | ||
203 | if (strncmp(buf, "off", 3) != 0) | |
204 | res = kstrtoint(buf, 0, tmo); | |
205 | else | |
206 | *tmo = -1; | |
207 | ||
208 | return res; | |
209 | } | |
3fdf70ac | 210 | EXPORT_SYMBOL(srp_parse_tmo); |
29c17324 | 211 | |
8c64e453 BVA |
212 | static ssize_t show_reconnect_delay(struct device *dev, |
213 | struct device_attribute *attr, char *buf) | |
214 | { | |
215 | struct srp_rport *rport = transport_class_to_srp_rport(dev); | |
216 | ||
217 | return srp_show_tmo(buf, rport->reconnect_delay); | |
218 | } | |
219 | ||
220 | static ssize_t store_reconnect_delay(struct device *dev, | |
221 | struct device_attribute *attr, | |
222 | const char *buf, const size_t count) | |
223 | { | |
224 | struct srp_rport *rport = transport_class_to_srp_rport(dev); | |
225 | int res, delay; | |
226 | ||
227 | res = srp_parse_tmo(&delay, buf); | |
228 | if (res) | |
229 | goto out; | |
230 | res = srp_tmo_valid(delay, rport->fast_io_fail_tmo, | |
231 | rport->dev_loss_tmo); | |
232 | if (res) | |
233 | goto out; | |
234 | ||
235 | if (rport->reconnect_delay <= 0 && delay > 0 && | |
236 | rport->state != SRP_RPORT_RUNNING) { | |
237 | queue_delayed_work(system_long_wq, &rport->reconnect_work, | |
238 | delay * HZ); | |
239 | } else if (delay <= 0) { | |
240 | cancel_delayed_work(&rport->reconnect_work); | |
241 | } | |
242 | rport->reconnect_delay = delay; | |
243 | res = count; | |
244 | ||
245 | out: | |
246 | return res; | |
247 | } | |
248 | ||
249 | static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay, | |
250 | store_reconnect_delay); | |
251 | ||
252 | static ssize_t show_failed_reconnects(struct device *dev, | |
253 | struct device_attribute *attr, char *buf) | |
254 | { | |
255 | struct srp_rport *rport = transport_class_to_srp_rport(dev); | |
256 | ||
257 | return sprintf(buf, "%d\n", rport->failed_reconnects); | |
258 | } | |
259 | ||
260 | static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL); | |
261 | ||
29c17324 BVA |
262 | static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev, |
263 | struct device_attribute *attr, | |
264 | char *buf) | |
265 | { | |
266 | struct srp_rport *rport = transport_class_to_srp_rport(dev); | |
267 | ||
268 | return srp_show_tmo(buf, rport->fast_io_fail_tmo); | |
269 | } | |
270 | ||
271 | static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev, | |
272 | struct device_attribute *attr, | |
273 | const char *buf, size_t count) | |
274 | { | |
275 | struct srp_rport *rport = transport_class_to_srp_rport(dev); | |
276 | int res; | |
277 | int fast_io_fail_tmo; | |
278 | ||
279 | res = srp_parse_tmo(&fast_io_fail_tmo, buf); | |
280 | if (res) | |
281 | goto out; | |
8c64e453 BVA |
282 | res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo, |
283 | rport->dev_loss_tmo); | |
29c17324 BVA |
284 | if (res) |
285 | goto out; | |
286 | rport->fast_io_fail_tmo = fast_io_fail_tmo; | |
287 | res = count; | |
288 | ||
289 | out: | |
290 | return res; | |
291 | } | |
292 | ||
293 | static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR, | |
294 | show_srp_rport_fast_io_fail_tmo, | |
295 | store_srp_rport_fast_io_fail_tmo); | |
296 | ||
297 | static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev, | |
298 | struct device_attribute *attr, | |
299 | char *buf) | |
300 | { | |
301 | struct srp_rport *rport = transport_class_to_srp_rport(dev); | |
302 | ||
303 | return srp_show_tmo(buf, rport->dev_loss_tmo); | |
304 | } | |
305 | ||
306 | static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev, | |
307 | struct device_attribute *attr, | |
308 | const char *buf, size_t count) | |
309 | { | |
310 | struct srp_rport *rport = transport_class_to_srp_rport(dev); | |
311 | int res; | |
312 | int dev_loss_tmo; | |
313 | ||
314 | res = srp_parse_tmo(&dev_loss_tmo, buf); | |
315 | if (res) | |
316 | goto out; | |
8c64e453 BVA |
317 | res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo, |
318 | dev_loss_tmo); | |
29c17324 BVA |
319 | if (res) |
320 | goto out; | |
321 | rport->dev_loss_tmo = dev_loss_tmo; | |
322 | res = count; | |
323 | ||
324 | out: | |
325 | return res; | |
326 | } | |
327 | ||
328 | static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR, | |
329 | show_srp_rport_dev_loss_tmo, | |
330 | store_srp_rport_dev_loss_tmo); | |
331 | ||
332 | static int srp_rport_set_state(struct srp_rport *rport, | |
333 | enum srp_rport_state new_state) | |
334 | { | |
335 | enum srp_rport_state old_state = rport->state; | |
336 | ||
337 | lockdep_assert_held(&rport->mutex); | |
338 | ||
339 | switch (new_state) { | |
340 | case SRP_RPORT_RUNNING: | |
341 | switch (old_state) { | |
342 | case SRP_RPORT_LOST: | |
343 | goto invalid; | |
344 | default: | |
345 | break; | |
346 | } | |
347 | break; | |
348 | case SRP_RPORT_BLOCKED: | |
349 | switch (old_state) { | |
350 | case SRP_RPORT_RUNNING: | |
351 | break; | |
352 | default: | |
353 | goto invalid; | |
354 | } | |
355 | break; | |
356 | case SRP_RPORT_FAIL_FAST: | |
357 | switch (old_state) { | |
358 | case SRP_RPORT_LOST: | |
359 | goto invalid; | |
360 | default: | |
361 | break; | |
362 | } | |
363 | break; | |
364 | case SRP_RPORT_LOST: | |
365 | break; | |
366 | } | |
367 | rport->state = new_state; | |
368 | return 0; | |
369 | ||
370 | invalid: | |
371 | return -EINVAL; | |
372 | } | |
373 | ||
8c64e453 BVA |
374 | /** |
375 | * srp_reconnect_work() - reconnect and schedule a new attempt if necessary | |
0c7f8218 | 376 | * @work: Work structure used for scheduling this operation. |
8c64e453 BVA |
377 | */ |
378 | static void srp_reconnect_work(struct work_struct *work) | |
379 | { | |
380 | struct srp_rport *rport = container_of(to_delayed_work(work), | |
381 | struct srp_rport, reconnect_work); | |
382 | struct Scsi_Host *shost = rport_to_shost(rport); | |
383 | int delay, res; | |
384 | ||
385 | res = srp_reconnect_rport(rport); | |
386 | if (res != 0) { | |
387 | shost_printk(KERN_ERR, shost, | |
388 | "reconnect attempt %d failed (%d)\n", | |
389 | ++rport->failed_reconnects, res); | |
390 | delay = rport->reconnect_delay * | |
391 | min(100, max(1, rport->failed_reconnects - 10)); | |
392 | if (delay > 0) | |
393 | queue_delayed_work(system_long_wq, | |
394 | &rport->reconnect_work, delay * HZ); | |
395 | } | |
396 | } | |
397 | ||
bf1a28f9 HR |
398 | /* |
399 | * scsi_target_block() must have been called before this function is | |
400 | * called to guarantee that no .queuecommand() calls are in progress. | |
401 | */ | |
29c17324 BVA |
402 | static void __rport_fail_io_fast(struct srp_rport *rport) |
403 | { | |
404 | struct Scsi_Host *shost = rport_to_shost(rport); | |
405 | struct srp_internal *i; | |
406 | ||
407 | lockdep_assert_held(&rport->mutex); | |
408 | ||
409 | if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST)) | |
410 | return; | |
bf1a28f9 | 411 | |
29c17324 BVA |
412 | scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE); |
413 | ||
414 | /* Involve the LLD if possible to terminate all I/O on the rport. */ | |
415 | i = to_srp_internal(shost->transportt); | |
669f0441 | 416 | if (i->f->terminate_rport_io) |
29c17324 BVA |
417 | i->f->terminate_rport_io(rport); |
418 | } | |
419 | ||
420 | /** | |
421 | * rport_fast_io_fail_timedout() - fast I/O failure timeout handler | |
0c7f8218 | 422 | * @work: Work structure used for scheduling this operation. |
29c17324 BVA |
423 | */ |
424 | static void rport_fast_io_fail_timedout(struct work_struct *work) | |
425 | { | |
426 | struct srp_rport *rport = container_of(to_delayed_work(work), | |
427 | struct srp_rport, fast_io_fail_work); | |
428 | struct Scsi_Host *shost = rport_to_shost(rport); | |
429 | ||
430 | pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n", | |
431 | dev_name(&rport->dev), dev_name(&shost->shost_gendev)); | |
432 | ||
433 | mutex_lock(&rport->mutex); | |
434 | if (rport->state == SRP_RPORT_BLOCKED) | |
435 | __rport_fail_io_fast(rport); | |
436 | mutex_unlock(&rport->mutex); | |
437 | } | |
438 | ||
439 | /** | |
440 | * rport_dev_loss_timedout() - device loss timeout handler | |
0c7f8218 | 441 | * @work: Work structure used for scheduling this operation. |
29c17324 BVA |
442 | */ |
443 | static void rport_dev_loss_timedout(struct work_struct *work) | |
444 | { | |
445 | struct srp_rport *rport = container_of(to_delayed_work(work), | |
446 | struct srp_rport, dev_loss_work); | |
447 | struct Scsi_Host *shost = rport_to_shost(rport); | |
448 | struct srp_internal *i = to_srp_internal(shost->transportt); | |
449 | ||
450 | pr_info("dev_loss_tmo expired for SRP %s / %s.\n", | |
451 | dev_name(&rport->dev), dev_name(&shost->shost_gendev)); | |
452 | ||
453 | mutex_lock(&rport->mutex); | |
454 | WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0); | |
455 | scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE); | |
456 | mutex_unlock(&rport->mutex); | |
457 | ||
458 | i->f->rport_delete(rport); | |
459 | } | |
460 | ||
461 | static void __srp_start_tl_fail_timers(struct srp_rport *rport) | |
462 | { | |
463 | struct Scsi_Host *shost = rport_to_shost(rport); | |
8c64e453 | 464 | int delay, fast_io_fail_tmo, dev_loss_tmo; |
29c17324 BVA |
465 | |
466 | lockdep_assert_held(&rport->mutex); | |
467 | ||
93079162 BVA |
468 | delay = rport->reconnect_delay; |
469 | fast_io_fail_tmo = rport->fast_io_fail_tmo; | |
470 | dev_loss_tmo = rport->dev_loss_tmo; | |
471 | pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev), | |
472 | rport->state); | |
29c17324 | 473 | |
93079162 BVA |
474 | if (rport->state == SRP_RPORT_LOST) |
475 | return; | |
476 | if (delay > 0) | |
477 | queue_delayed_work(system_long_wq, &rport->reconnect_work, | |
478 | 1UL * delay * HZ); | |
cd53eb68 BVA |
479 | if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) && |
480 | srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) { | |
93079162 BVA |
481 | pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev), |
482 | rport->state); | |
483 | scsi_target_block(&shost->shost_gendev); | |
484 | if (fast_io_fail_tmo >= 0) | |
8c64e453 | 485 | queue_delayed_work(system_long_wq, |
93079162 BVA |
486 | &rport->fast_io_fail_work, |
487 | 1UL * fast_io_fail_tmo * HZ); | |
488 | if (dev_loss_tmo >= 0) | |
489 | queue_delayed_work(system_long_wq, | |
490 | &rport->dev_loss_work, | |
491 | 1UL * dev_loss_tmo * HZ); | |
29c17324 BVA |
492 | } |
493 | } | |
494 | ||
495 | /** | |
496 | * srp_start_tl_fail_timers() - start the transport layer failure timers | |
0c7f8218 | 497 | * @rport: SRP target port. |
29c17324 BVA |
498 | * |
499 | * Start the transport layer fast I/O failure and device loss timers. Do not | |
500 | * modify a timer that was already started. | |
501 | */ | |
502 | void srp_start_tl_fail_timers(struct srp_rport *rport) | |
503 | { | |
504 | mutex_lock(&rport->mutex); | |
505 | __srp_start_tl_fail_timers(rport); | |
506 | mutex_unlock(&rport->mutex); | |
507 | } | |
508 | EXPORT_SYMBOL(srp_start_tl_fail_timers); | |
509 | ||
29c17324 BVA |
510 | /** |
511 | * srp_reconnect_rport() - reconnect to an SRP target port | |
0c7f8218 | 512 | * @rport: SRP target port. |
29c17324 BVA |
513 | * |
514 | * Blocks SCSI command queueing before invoking reconnect() such that | |
515 | * queuecommand() won't be invoked concurrently with reconnect() from outside | |
516 | * the SCSI EH. This is important since a reconnect() implementation may | |
517 | * reallocate resources needed by queuecommand(). | |
518 | * | |
519 | * Notes: | |
520 | * - This function neither waits until outstanding requests have finished nor | |
521 | * tries to abort these. It is the responsibility of the reconnect() | |
522 | * function to finish outstanding commands before reconnecting to the target | |
523 | * port. | |
524 | * - It is the responsibility of the caller to ensure that the resources | |
525 | * reallocated by the reconnect() function won't be used while this function | |
526 | * is in progress. One possible strategy is to invoke this function from | |
527 | * the context of the SCSI EH thread only. Another possible strategy is to | |
528 | * lock the rport mutex inside each SCSI LLD callback that can be invoked by | |
529 | * the SCSI EH (the scsi_host_template.eh_*() functions and also the | |
530 | * scsi_host_template.queuecommand() function). | |
531 | */ | |
532 | int srp_reconnect_rport(struct srp_rport *rport) | |
533 | { | |
534 | struct Scsi_Host *shost = rport_to_shost(rport); | |
535 | struct srp_internal *i = to_srp_internal(shost->transportt); | |
536 | struct scsi_device *sdev; | |
537 | int res; | |
538 | ||
539 | pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev)); | |
540 | ||
541 | res = mutex_lock_interruptible(&rport->mutex); | |
542 | if (res) | |
543 | goto out; | |
544 | scsi_target_block(&shost->shost_gendev); | |
93079162 | 545 | res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV; |
29c17324 BVA |
546 | pr_debug("%s (state %d): transport.reconnect() returned %d\n", |
547 | dev_name(&shost->shost_gendev), rport->state, res); | |
548 | if (res == 0) { | |
549 | cancel_delayed_work(&rport->fast_io_fail_work); | |
550 | cancel_delayed_work(&rport->dev_loss_work); | |
551 | ||
8c64e453 | 552 | rport->failed_reconnects = 0; |
29c17324 BVA |
553 | srp_rport_set_state(rport, SRP_RPORT_RUNNING); |
554 | scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING); | |
555 | /* | |
556 | * If the SCSI error handler has offlined one or more devices, | |
557 | * invoking scsi_target_unblock() won't change the state of | |
558 | * these devices into running so do that explicitly. | |
559 | */ | |
0db6ca8a BVA |
560 | shost_for_each_device(sdev, shost) { |
561 | mutex_lock(&sdev->state_mutex); | |
a817e73f | 562 | if (sdev->sdev_state == SDEV_OFFLINE) |
29c17324 | 563 | sdev->sdev_state = SDEV_RUNNING; |
0db6ca8a BVA |
564 | mutex_unlock(&sdev->state_mutex); |
565 | } | |
29c17324 BVA |
566 | } else if (rport->state == SRP_RPORT_RUNNING) { |
567 | /* | |
18cc4e02 BVA |
568 | * srp_reconnect_rport() has been invoked with fast_io_fail |
569 | * and dev_loss off. Mark the port as failed and start the TL | |
570 | * failure timers if these had not yet been started. | |
29c17324 BVA |
571 | */ |
572 | __rport_fail_io_fast(rport); | |
29c17324 BVA |
573 | __srp_start_tl_fail_timers(rport); |
574 | } else if (rport->state != SRP_RPORT_BLOCKED) { | |
575 | scsi_target_unblock(&shost->shost_gendev, | |
576 | SDEV_TRANSPORT_OFFLINE); | |
577 | } | |
578 | mutex_unlock(&rport->mutex); | |
579 | ||
580 | out: | |
581 | return res; | |
582 | } | |
583 | EXPORT_SYMBOL(srp_reconnect_rport); | |
584 | ||
585 | /** | |
586 | * srp_timed_out() - SRP transport intercept of the SCSI timeout EH | |
0c7f8218 | 587 | * @scmd: SCSI command. |
29c17324 BVA |
588 | * |
589 | * If a timeout occurs while an rport is in the blocked state, ask the SCSI | |
590 | * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core | |
6600593c | 591 | * handle the timeout (BLK_EH_DONE). |
29c17324 BVA |
592 | * |
593 | * Note: This function is called from soft-IRQ context and with the request | |
594 | * queue lock held. | |
595 | */ | |
b6a05c82 | 596 | enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd) |
29c17324 BVA |
597 | { |
598 | struct scsi_device *sdev = scmd->device; | |
599 | struct Scsi_Host *shost = sdev->host; | |
600 | struct srp_internal *i = to_srp_internal(shost->transportt); | |
e68ca752 | 601 | struct srp_rport *rport = shost_to_rport(shost); |
29c17324 BVA |
602 | |
603 | pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev)); | |
c9ddf734 BVA |
604 | return rport && rport->fast_io_fail_tmo < 0 && |
605 | rport->dev_loss_tmo < 0 && | |
e68ca752 | 606 | i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ? |
6600593c | 607 | BLK_EH_RESET_TIMER : BLK_EH_DONE; |
29c17324 | 608 | } |
b6a05c82 | 609 | EXPORT_SYMBOL(srp_timed_out); |
29c17324 | 610 | |
09345f65 FT |
611 | static void srp_rport_release(struct device *dev) |
612 | { | |
613 | struct srp_rport *rport = dev_to_rport(dev); | |
614 | ||
615 | put_device(dev->parent); | |
616 | kfree(rport); | |
617 | } | |
618 | ||
619 | static int scsi_is_srp_rport(const struct device *dev) | |
620 | { | |
621 | return dev->release == srp_rport_release; | |
622 | } | |
623 | ||
624 | static int srp_rport_match(struct attribute_container *cont, | |
625 | struct device *dev) | |
626 | { | |
627 | struct Scsi_Host *shost; | |
628 | struct srp_internal *i; | |
629 | ||
630 | if (!scsi_is_srp_rport(dev)) | |
631 | return 0; | |
632 | ||
633 | shost = dev_to_shost(dev->parent); | |
634 | if (!shost->transportt) | |
635 | return 0; | |
636 | if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) | |
637 | return 0; | |
638 | ||
639 | i = to_srp_internal(shost->transportt); | |
640 | return &i->rport_attr_cont.ac == cont; | |
641 | } | |
642 | ||
643 | static int srp_host_match(struct attribute_container *cont, struct device *dev) | |
644 | { | |
645 | struct Scsi_Host *shost; | |
646 | struct srp_internal *i; | |
647 | ||
648 | if (!scsi_is_host_device(dev)) | |
649 | return 0; | |
650 | ||
651 | shost = dev_to_shost(dev); | |
652 | if (!shost->transportt) | |
653 | return 0; | |
654 | if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) | |
655 | return 0; | |
656 | ||
657 | i = to_srp_internal(shost->transportt); | |
658 | return &i->t.host_attrs.ac == cont; | |
659 | } | |
660 | ||
9dd69a60 BVA |
661 | /** |
662 | * srp_rport_get() - increment rport reference count | |
0c7f8218 | 663 | * @rport: SRP target port. |
9dd69a60 BVA |
664 | */ |
665 | void srp_rport_get(struct srp_rport *rport) | |
666 | { | |
667 | get_device(&rport->dev); | |
668 | } | |
669 | EXPORT_SYMBOL(srp_rport_get); | |
670 | ||
671 | /** | |
672 | * srp_rport_put() - decrement rport reference count | |
0c7f8218 | 673 | * @rport: SRP target port. |
9dd69a60 BVA |
674 | */ |
675 | void srp_rport_put(struct srp_rport *rport) | |
676 | { | |
677 | put_device(&rport->dev); | |
678 | } | |
679 | EXPORT_SYMBOL(srp_rport_put); | |
680 | ||
09345f65 FT |
681 | /** |
682 | * srp_rport_add - add a SRP remote port to the device hierarchy | |
09345f65 FT |
683 | * @shost: scsi host the remote port is connected to. |
684 | * @ids: The port id for the remote port. | |
685 | * | |
dc8875e1 | 686 | * Publishes a port to the rest of the system. |
09345f65 FT |
687 | */ |
688 | struct srp_rport *srp_rport_add(struct Scsi_Host *shost, | |
689 | struct srp_rport_identifiers *ids) | |
690 | { | |
691 | struct srp_rport *rport; | |
692 | struct device *parent = &shost->shost_gendev; | |
29c17324 | 693 | struct srp_internal *i = to_srp_internal(shost->transportt); |
09345f65 FT |
694 | int id, ret; |
695 | ||
696 | rport = kzalloc(sizeof(*rport), GFP_KERNEL); | |
697 | if (!rport) | |
698 | return ERR_PTR(-ENOMEM); | |
699 | ||
29c17324 BVA |
700 | mutex_init(&rport->mutex); |
701 | ||
09345f65 FT |
702 | device_initialize(&rport->dev); |
703 | ||
704 | rport->dev.parent = get_device(parent); | |
705 | rport->dev.release = srp_rport_release; | |
706 | ||
707 | memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id)); | |
aebd5e47 | 708 | rport->roles = ids->roles; |
09345f65 | 709 | |
8c64e453 BVA |
710 | if (i->f->reconnect) |
711 | rport->reconnect_delay = i->f->reconnect_delay ? | |
712 | *i->f->reconnect_delay : 10; | |
713 | INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work); | |
29c17324 BVA |
714 | rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ? |
715 | *i->f->fast_io_fail_tmo : 15; | |
716 | rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60; | |
717 | INIT_DELAYED_WORK(&rport->fast_io_fail_work, | |
718 | rport_fast_io_fail_timedout); | |
719 | INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout); | |
720 | ||
09345f65 | 721 | id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id); |
71610f55 | 722 | dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id); |
09345f65 FT |
723 | |
724 | transport_setup_device(&rport->dev); | |
725 | ||
726 | ret = device_add(&rport->dev); | |
727 | if (ret) { | |
728 | transport_destroy_device(&rport->dev); | |
729 | put_device(&rport->dev); | |
730 | return ERR_PTR(ret); | |
731 | } | |
732 | ||
733 | transport_add_device(&rport->dev); | |
734 | transport_configure_device(&rport->dev); | |
735 | ||
736 | return rport; | |
737 | } | |
738 | EXPORT_SYMBOL_GPL(srp_rport_add); | |
739 | ||
740 | /** | |
eb44820c RL |
741 | * srp_rport_del - remove a SRP remote port |
742 | * @rport: SRP remote port to remove | |
09345f65 FT |
743 | * |
744 | * Removes the specified SRP remote port. | |
745 | */ | |
746 | void srp_rport_del(struct srp_rport *rport) | |
747 | { | |
748 | struct device *dev = &rport->dev; | |
62fe8826 | 749 | |
09345f65 FT |
750 | transport_remove_device(dev); |
751 | device_del(dev); | |
752 | transport_destroy_device(dev); | |
29c17324 | 753 | |
09345f65 FT |
754 | put_device(dev); |
755 | } | |
756 | EXPORT_SYMBOL_GPL(srp_rport_del); | |
757 | ||
758 | static int do_srp_rport_del(struct device *dev, void *data) | |
759 | { | |
91183344 DD |
760 | if (scsi_is_srp_rport(dev)) |
761 | srp_rport_del(dev_to_rport(dev)); | |
09345f65 FT |
762 | return 0; |
763 | } | |
764 | ||
765 | /** | |
eb44820c | 766 | * srp_remove_host - tear down a Scsi_Host's SRP data structures |
09345f65 FT |
767 | * @shost: Scsi Host that is torn down |
768 | * | |
769 | * Removes all SRP remote ports for a given Scsi_Host. | |
770 | * Must be called just before scsi_remove_host for SRP HBAs. | |
771 | */ | |
772 | void srp_remove_host(struct Scsi_Host *shost) | |
773 | { | |
774 | device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del); | |
775 | } | |
776 | EXPORT_SYMBOL_GPL(srp_remove_host); | |
777 | ||
93079162 BVA |
778 | /** |
779 | * srp_stop_rport_timers - stop the transport layer recovery timers | |
67513605 | 780 | * @rport: SRP remote port for which to stop the timers. |
93079162 BVA |
781 | * |
782 | * Must be called after srp_remove_host() and scsi_remove_host(). The caller | |
783 | * must hold a reference on the rport (rport->dev) and on the SCSI host | |
784 | * (rport->dev.parent). | |
785 | */ | |
786 | void srp_stop_rport_timers(struct srp_rport *rport) | |
787 | { | |
788 | mutex_lock(&rport->mutex); | |
789 | if (rport->state == SRP_RPORT_BLOCKED) | |
790 | __rport_fail_io_fast(rport); | |
791 | srp_rport_set_state(rport, SRP_RPORT_LOST); | |
792 | mutex_unlock(&rport->mutex); | |
793 | ||
794 | cancel_delayed_work_sync(&rport->reconnect_work); | |
795 | cancel_delayed_work_sync(&rport->fast_io_fail_work); | |
796 | cancel_delayed_work_sync(&rport->dev_loss_work); | |
797 | } | |
798 | EXPORT_SYMBOL_GPL(srp_stop_rport_timers); | |
799 | ||
09345f65 | 800 | /** |
eb44820c | 801 | * srp_attach_transport - instantiate SRP transport template |
09345f65 FT |
802 | * @ft: SRP transport class function template |
803 | */ | |
804 | struct scsi_transport_template * | |
805 | srp_attach_transport(struct srp_function_template *ft) | |
806 | { | |
807 | int count; | |
808 | struct srp_internal *i; | |
809 | ||
810 | i = kzalloc(sizeof(*i), GFP_KERNEL); | |
811 | if (!i) | |
812 | return NULL; | |
813 | ||
814 | i->t.host_size = sizeof(struct srp_host_attrs); | |
815 | i->t.host_attrs.ac.attrs = &i->host_attrs[0]; | |
816 | i->t.host_attrs.ac.class = &srp_host_class.class; | |
817 | i->t.host_attrs.ac.match = srp_host_match; | |
818 | i->host_attrs[0] = NULL; | |
819 | transport_container_register(&i->t.host_attrs); | |
820 | ||
821 | i->rport_attr_cont.ac.attrs = &i->rport_attrs[0]; | |
822 | i->rport_attr_cont.ac.class = &srp_rport_class.class; | |
823 | i->rport_attr_cont.ac.match = srp_rport_match; | |
09345f65 FT |
824 | |
825 | count = 0; | |
9134a855 BVA |
826 | i->rport_attrs[count++] = &dev_attr_port_id; |
827 | i->rport_attrs[count++] = &dev_attr_roles; | |
29c17324 BVA |
828 | if (ft->has_rport_state) { |
829 | i->rport_attrs[count++] = &dev_attr_state; | |
830 | i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo; | |
831 | i->rport_attrs[count++] = &dev_attr_dev_loss_tmo; | |
832 | } | |
8c64e453 BVA |
833 | if (ft->reconnect) { |
834 | i->rport_attrs[count++] = &dev_attr_reconnect_delay; | |
835 | i->rport_attrs[count++] = &dev_attr_failed_reconnects; | |
836 | } | |
dc1bdbd9 BVA |
837 | if (ft->rport_delete) |
838 | i->rport_attrs[count++] = &dev_attr_delete; | |
9134a855 BVA |
839 | i->rport_attrs[count++] = NULL; |
840 | BUG_ON(count > ARRAY_SIZE(i->rport_attrs)); | |
09345f65 | 841 | |
ac9be30e BVA |
842 | transport_container_register(&i->rport_attr_cont); |
843 | ||
09345f65 FT |
844 | i->f = ft; |
845 | ||
846 | return &i->t; | |
847 | } | |
848 | EXPORT_SYMBOL_GPL(srp_attach_transport); | |
849 | ||
850 | /** | |
eb44820c | 851 | * srp_release_transport - release SRP transport template instance |
09345f65 FT |
852 | * @t: transport template instance |
853 | */ | |
854 | void srp_release_transport(struct scsi_transport_template *t) | |
855 | { | |
856 | struct srp_internal *i = to_srp_internal(t); | |
857 | ||
858 | transport_container_unregister(&i->t.host_attrs); | |
859 | transport_container_unregister(&i->rport_attr_cont); | |
860 | ||
861 | kfree(i); | |
862 | } | |
863 | EXPORT_SYMBOL_GPL(srp_release_transport); | |
864 | ||
865 | static __init int srp_transport_init(void) | |
866 | { | |
867 | int ret; | |
868 | ||
869 | ret = transport_class_register(&srp_host_class); | |
870 | if (ret) | |
871 | return ret; | |
872 | ret = transport_class_register(&srp_rport_class); | |
873 | if (ret) | |
874 | goto unregister_host_class; | |
875 | ||
876 | return 0; | |
877 | unregister_host_class: | |
878 | transport_class_unregister(&srp_host_class); | |
879 | return ret; | |
880 | } | |
881 | ||
882 | static void __exit srp_transport_exit(void) | |
883 | { | |
884 | transport_class_unregister(&srp_host_class); | |
885 | transport_class_unregister(&srp_rport_class); | |
886 | } | |
887 | ||
888 | MODULE_AUTHOR("FUJITA Tomonori"); | |
889 | MODULE_DESCRIPTION("SRP Transport Attributes"); | |
890 | MODULE_LICENSE("GPL"); | |
891 | ||
892 | module_init(srp_transport_init); | |
893 | module_exit(srp_transport_exit); |