blob: e0f064dcbd0211ec188cde84de322ffc7880d40a [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (c) 2017-2018 Christoph Hellwig.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5
6#include <linux/moduleparam.h>
7#include <trace/events/block.h>
8#include "nvme.h"
9
10static bool multipath = true;
11module_param(multipath, bool, 0444);
12MODULE_PARM_DESC(multipath,
13 "turn on native support for multiple controllers per subsystem");
14
David Brazdil0f672f62019-12-10 10:32:29 +000015void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016{
David Brazdil0f672f62019-12-10 10:32:29 +000017 struct nvme_ns_head *h;
18
19 lockdep_assert_held(&subsys->lock);
20 list_for_each_entry(h, &subsys->nsheads, entry)
21 if (h->disk)
22 blk_mq_unfreeze_queue(h->disk->queue);
23}
24
25void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
26{
27 struct nvme_ns_head *h;
28
29 lockdep_assert_held(&subsys->lock);
30 list_for_each_entry(h, &subsys->nsheads, entry)
31 if (h->disk)
32 blk_mq_freeze_queue_wait(h->disk->queue);
33}
34
35void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
36{
37 struct nvme_ns_head *h;
38
39 lockdep_assert_held(&subsys->lock);
40 list_for_each_entry(h, &subsys->nsheads, entry)
41 if (h->disk)
42 blk_freeze_queue_start(h->disk->queue);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043}
44
45/*
46 * If multipathing is enabled we need to always use the subsystem instance
47 * number for numbering our devices to avoid conflicts between subsystems that
48 * have multiple controllers and thus use the multipath-aware subsystem node
49 * and those that have a single controller and use the controller node
50 * directly.
51 */
52void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
53 struct nvme_ctrl *ctrl, int *flags)
54{
55 if (!multipath) {
56 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
57 } else if (ns->head->disk) {
58 sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
David Brazdil0f672f62019-12-10 10:32:29 +000059 ctrl->instance, ns->head->instance);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060 *flags = GENHD_FL_HIDDEN;
61 } else {
62 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
63 ns->head->instance);
64 }
65}
66
67void nvme_failover_req(struct request *req)
68{
69 struct nvme_ns *ns = req->q->queuedata;
70 u16 status = nvme_req(req)->status;
71 unsigned long flags;
72
73 spin_lock_irqsave(&ns->head->requeue_lock, flags);
74 blk_steal_bios(&ns->head->requeue_list, req);
75 spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
76 blk_mq_end_request(req, 0);
77
78 switch (status & 0x7ff) {
79 case NVME_SC_ANA_TRANSITION:
80 case NVME_SC_ANA_INACCESSIBLE:
81 case NVME_SC_ANA_PERSISTENT_LOSS:
82 /*
83 * If we got back an ANA error we know the controller is alive,
84 * but not ready to serve this namespaces. The spec suggests
85 * we should update our general state here, but due to the fact
86 * that the admin and I/O queues are not serialized that is
87 * fundamentally racy. So instead just clear the current path,
88 * mark the the path as pending and kick of a re-read of the ANA
89 * log page ASAP.
90 */
91 nvme_mpath_clear_current_path(ns);
92 if (ns->ctrl->ana_log_buf) {
93 set_bit(NVME_NS_ANA_PENDING, &ns->flags);
94 queue_work(nvme_wq, &ns->ctrl->ana_work);
95 }
96 break;
97 case NVME_SC_HOST_PATH_ERROR:
98 /*
99 * Temporary transport disruption in talking to the controller.
100 * Try to send on a new path.
101 */
102 nvme_mpath_clear_current_path(ns);
103 break;
104 default:
105 /*
106 * Reset the controller for any non-ANA error as we don't know
107 * what caused the error.
108 */
109 nvme_reset_ctrl(ns->ctrl);
110 break;
111 }
112
113 kblockd_schedule_work(&ns->head->requeue_work);
114}
115
116void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
117{
118 struct nvme_ns *ns;
119
120 down_read(&ctrl->namespaces_rwsem);
121 list_for_each_entry(ns, &ctrl->namespaces, list) {
122 if (ns->head->disk)
123 kblockd_schedule_work(&ns->head->requeue_work);
124 }
125 up_read(&ctrl->namespaces_rwsem);
126}
127
128static const char *nvme_ana_state_names[] = {
129 [0] = "invalid state",
130 [NVME_ANA_OPTIMIZED] = "optimized",
131 [NVME_ANA_NONOPTIMIZED] = "non-optimized",
132 [NVME_ANA_INACCESSIBLE] = "inaccessible",
133 [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss",
134 [NVME_ANA_CHANGE] = "change",
135};
136
David Brazdil0f672f62019-12-10 10:32:29 +0000137bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138{
David Brazdil0f672f62019-12-10 10:32:29 +0000139 struct nvme_ns_head *head = ns->head;
140 bool changed = false;
141 int node;
142
143 if (!head)
144 goto out;
145
146 for_each_node(node) {
147 if (ns == rcu_access_pointer(head->current_path[node])) {
148 rcu_assign_pointer(head->current_path[node], NULL);
149 changed = true;
150 }
151 }
152out:
153 return changed;
154}
155
156void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
157{
158 struct nvme_ns *ns;
159
160 mutex_lock(&ctrl->scan_lock);
161 down_read(&ctrl->namespaces_rwsem);
162 list_for_each_entry(ns, &ctrl->namespaces, list)
163 if (nvme_mpath_clear_current_path(ns))
164 kblockd_schedule_work(&ns->head->requeue_work);
165 up_read(&ctrl->namespaces_rwsem);
166 mutex_unlock(&ctrl->scan_lock);
167}
168
169static bool nvme_path_is_disabled(struct nvme_ns *ns)
170{
171 return ns->ctrl->state != NVME_CTRL_LIVE ||
172 test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
173 test_bit(NVME_NS_REMOVING, &ns->flags);
174}
175
176static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
177{
178 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
179 struct nvme_ns *found = NULL, *fallback = NULL, *ns;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000180
181 list_for_each_entry_rcu(ns, &head->list, siblings) {
David Brazdil0f672f62019-12-10 10:32:29 +0000182 if (nvme_path_is_disabled(ns))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000183 continue;
David Brazdil0f672f62019-12-10 10:32:29 +0000184
185 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
186 distance = node_distance(node, ns->ctrl->numa_node);
187 else
188 distance = LOCAL_DISTANCE;
189
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000190 switch (ns->ana_state) {
191 case NVME_ANA_OPTIMIZED:
David Brazdil0f672f62019-12-10 10:32:29 +0000192 if (distance < found_distance) {
193 found_distance = distance;
194 found = ns;
195 }
196 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000197 case NVME_ANA_NONOPTIMIZED:
David Brazdil0f672f62019-12-10 10:32:29 +0000198 if (distance < fallback_distance) {
199 fallback_distance = distance;
200 fallback = ns;
201 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000202 break;
203 default:
204 break;
205 }
206 }
207
David Brazdil0f672f62019-12-10 10:32:29 +0000208 if (!found)
209 found = fallback;
210 if (found)
211 rcu_assign_pointer(head->current_path[node], found);
212 return found;
213}
214
215static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
216 struct nvme_ns *ns)
217{
218 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
219 siblings);
220 if (ns)
221 return ns;
222 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
223}
224
225static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
226 int node, struct nvme_ns *old)
227{
228 struct nvme_ns *ns, *found, *fallback = NULL;
229
230 if (list_is_singular(&head->list)) {
231 if (nvme_path_is_disabled(old))
232 return NULL;
233 return old;
234 }
235
236 for (ns = nvme_next_ns(head, old);
237 ns != old;
238 ns = nvme_next_ns(head, ns)) {
239 if (nvme_path_is_disabled(ns))
240 continue;
241
242 if (ns->ana_state == NVME_ANA_OPTIMIZED) {
243 found = ns;
244 goto out;
245 }
246 if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
247 fallback = ns;
248 }
249
250 if (!fallback)
251 return NULL;
252 found = fallback;
253out:
254 rcu_assign_pointer(head->current_path[node], found);
255 return found;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000256}
257
258static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
259{
260 return ns->ctrl->state == NVME_CTRL_LIVE &&
261 ns->ana_state == NVME_ANA_OPTIMIZED;
262}
263
264inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
265{
David Brazdil0f672f62019-12-10 10:32:29 +0000266 int node = numa_node_id();
267 struct nvme_ns *ns;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000268
David Brazdil0f672f62019-12-10 10:32:29 +0000269 ns = srcu_dereference(head->current_path[node], &head->srcu);
270 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR && ns)
271 ns = nvme_round_robin_path(head, node, ns);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000272 if (unlikely(!ns || !nvme_path_is_optimized(ns)))
David Brazdil0f672f62019-12-10 10:32:29 +0000273 ns = __nvme_find_path(head, node);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000274 return ns;
275}
276
David Brazdil0f672f62019-12-10 10:32:29 +0000277static bool nvme_available_path(struct nvme_ns_head *head)
278{
279 struct nvme_ns *ns;
280
281 list_for_each_entry_rcu(ns, &head->list, siblings) {
282 switch (ns->ctrl->state) {
283 case NVME_CTRL_LIVE:
284 case NVME_CTRL_RESETTING:
285 case NVME_CTRL_CONNECTING:
286 /* fallthru */
287 return true;
288 default:
289 break;
290 }
291 }
292 return false;
293}
294
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
296 struct bio *bio)
297{
298 struct nvme_ns_head *head = q->queuedata;
299 struct device *dev = disk_to_dev(head->disk);
300 struct nvme_ns *ns;
301 blk_qc_t ret = BLK_QC_T_NONE;
302 int srcu_idx;
303
David Brazdil0f672f62019-12-10 10:32:29 +0000304 /*
305 * The namespace might be going away and the bio might
306 * be moved to a different queue via blk_steal_bios(),
307 * so we need to use the bio_split pool from the original
308 * queue to allocate the bvecs from.
309 */
310 blk_queue_split(q, &bio);
311
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000312 srcu_idx = srcu_read_lock(&head->srcu);
313 ns = nvme_find_path(head);
314 if (likely(ns)) {
315 bio->bi_disk = ns->disk;
316 bio->bi_opf |= REQ_NVME_MPATH;
317 trace_block_bio_remap(bio->bi_disk->queue, bio,
318 disk_devt(ns->head->disk),
319 bio->bi_iter.bi_sector);
320 ret = direct_make_request(bio);
David Brazdil0f672f62019-12-10 10:32:29 +0000321 } else if (nvme_available_path(head)) {
322 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000323
324 spin_lock_irq(&head->requeue_lock);
325 bio_list_add(&head->requeue_list, bio);
326 spin_unlock_irq(&head->requeue_lock);
327 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000328 dev_warn_ratelimited(dev, "no available path - failing I/O\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000329
330 bio->bi_status = BLK_STS_IOERR;
331 bio_endio(bio);
332 }
333
334 srcu_read_unlock(&head->srcu, srcu_idx);
335 return ret;
336}
337
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000338static void nvme_requeue_work(struct work_struct *work)
339{
340 struct nvme_ns_head *head =
341 container_of(work, struct nvme_ns_head, requeue_work);
342 struct bio *bio, *next;
343
344 spin_lock_irq(&head->requeue_lock);
345 next = bio_list_get(&head->requeue_list);
346 spin_unlock_irq(&head->requeue_lock);
347
348 while ((bio = next) != NULL) {
349 next = bio->bi_next;
350 bio->bi_next = NULL;
351
352 /*
353 * Reset disk to the mpath node and resubmit to select a new
354 * path.
355 */
356 bio->bi_disk = head->disk;
357 generic_make_request(bio);
358 }
359}
360
361int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
362{
363 struct request_queue *q;
364 bool vwc = false;
365
366 mutex_init(&head->lock);
367 bio_list_init(&head->requeue_list);
368 spin_lock_init(&head->requeue_lock);
369 INIT_WORK(&head->requeue_work, nvme_requeue_work);
370
371 /*
372 * Add a multipath node if the subsystems supports multiple controllers.
373 * We also do this for private namespaces as the namespace sharing data could
374 * change after a rescan.
375 */
376 if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
377 return 0;
378
David Brazdil0f672f62019-12-10 10:32:29 +0000379 q = blk_alloc_queue_node(GFP_KERNEL, ctrl->numa_node);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000380 if (!q)
381 goto out;
382 q->queuedata = head;
383 blk_queue_make_request(q, nvme_ns_head_make_request);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000384 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
385 /* set to a default value for 512 until disk is validated */
386 blk_queue_logical_block_size(q, 512);
387 blk_set_stacking_limits(&q->limits);
388
389 /* we need to propagate up the VMC settings */
390 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
391 vwc = true;
392 blk_queue_write_cache(q, vwc, vwc);
393
394 head->disk = alloc_disk(0);
395 if (!head->disk)
396 goto out_cleanup_queue;
397 head->disk->fops = &nvme_ns_head_ops;
398 head->disk->private_data = head;
399 head->disk->queue = q;
400 head->disk->flags = GENHD_FL_EXT_DEVT;
401 sprintf(head->disk->disk_name, "nvme%dn%d",
402 ctrl->subsys->instance, head->instance);
403 return 0;
404
405out_cleanup_queue:
406 blk_cleanup_queue(q);
407out:
408 return -ENOMEM;
409}
410
411static void nvme_mpath_set_live(struct nvme_ns *ns)
412{
413 struct nvme_ns_head *head = ns->head;
414
415 lockdep_assert_held(&ns->head->lock);
416
417 if (!head->disk)
418 return;
419
David Brazdil0f672f62019-12-10 10:32:29 +0000420 if (!(head->disk->flags & GENHD_FL_UP))
421 device_add_disk(&head->subsys->dev, head->disk,
422 nvme_ns_id_attr_groups);
423
424 if (nvme_path_is_optimized(ns)) {
425 int node, srcu_idx;
426
427 srcu_idx = srcu_read_lock(&head->srcu);
428 for_each_node(node)
429 __nvme_find_path(head, node);
430 srcu_read_unlock(&head->srcu, srcu_idx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000431 }
432
David Brazdil0f672f62019-12-10 10:32:29 +0000433 synchronize_srcu(&ns->head->srcu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000434 kblockd_schedule_work(&ns->head->requeue_work);
435}
436
437static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
438 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
439 void *))
440{
441 void *base = ctrl->ana_log_buf;
442 size_t offset = sizeof(struct nvme_ana_rsp_hdr);
443 int error, i;
444
445 lockdep_assert_held(&ctrl->ana_lock);
446
447 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
448 struct nvme_ana_group_desc *desc = base + offset;
449 u32 nr_nsids = le32_to_cpu(desc->nnsids);
450 size_t nsid_buf_size = nr_nsids * sizeof(__le32);
451
452 if (WARN_ON_ONCE(desc->grpid == 0))
453 return -EINVAL;
454 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
455 return -EINVAL;
456 if (WARN_ON_ONCE(desc->state == 0))
457 return -EINVAL;
458 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
459 return -EINVAL;
460
461 offset += sizeof(*desc);
462 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
463 return -EINVAL;
464
465 error = cb(ctrl, desc, data);
466 if (error)
467 return error;
468
469 offset += nsid_buf_size;
470 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
471 return -EINVAL;
472 }
473
474 return 0;
475}
476
477static inline bool nvme_state_is_live(enum nvme_ana_state state)
478{
479 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
480}
481
482static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
483 struct nvme_ns *ns)
484{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000485 mutex_lock(&ns->head->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000486 ns->ana_grpid = le32_to_cpu(desc->grpid);
487 ns->ana_state = desc->state;
488 clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
489
David Brazdil0f672f62019-12-10 10:32:29 +0000490 if (nvme_state_is_live(ns->ana_state))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491 nvme_mpath_set_live(ns);
492 mutex_unlock(&ns->head->lock);
493}
494
495static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
496 struct nvme_ana_group_desc *desc, void *data)
497{
498 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
499 unsigned *nr_change_groups = data;
500 struct nvme_ns *ns;
501
David Brazdil0f672f62019-12-10 10:32:29 +0000502 dev_dbg(ctrl->device, "ANA group %d: %s.\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503 le32_to_cpu(desc->grpid),
504 nvme_ana_state_names[desc->state]);
505
506 if (desc->state == NVME_ANA_CHANGE)
507 (*nr_change_groups)++;
508
509 if (!nr_nsids)
510 return 0;
511
512 down_write(&ctrl->namespaces_rwsem);
513 list_for_each_entry(ns, &ctrl->namespaces, list) {
David Brazdil0f672f62019-12-10 10:32:29 +0000514 unsigned nsid = le32_to_cpu(desc->nsids[n]);
515
516 if (ns->head->ns_id < nsid)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517 continue;
David Brazdil0f672f62019-12-10 10:32:29 +0000518 if (ns->head->ns_id == nsid)
519 nvme_update_ns_ana_state(desc, ns);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000520 if (++n == nr_nsids)
521 break;
522 }
523 up_write(&ctrl->namespaces_rwsem);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000524 return 0;
525}
526
David Brazdil0f672f62019-12-10 10:32:29 +0000527static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000528{
529 u32 nr_change_groups = 0;
530 int error;
531
532 mutex_lock(&ctrl->ana_lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000533 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534 ctrl->ana_log_buf, ctrl->ana_log_size, 0);
535 if (error) {
536 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
537 goto out_unlock;
538 }
539
540 error = nvme_parse_ana_log(ctrl, &nr_change_groups,
541 nvme_update_ana_state);
542 if (error)
543 goto out_unlock;
544
545 /*
546 * In theory we should have an ANATT timer per group as they might enter
547 * the change state at different times. But that is a lot of overhead
548 * just to protect against a target that keeps entering new changes
549 * states while never finishing previous ones. But we'll still
550 * eventually time out once all groups are in change state, so this
551 * isn't a big deal.
552 *
553 * We also double the ANATT value to provide some slack for transports
554 * or AEN processing overhead.
555 */
556 if (nr_change_groups)
557 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
558 else
559 del_timer_sync(&ctrl->anatt_timer);
560out_unlock:
561 mutex_unlock(&ctrl->ana_lock);
562 return error;
563}
564
565static void nvme_ana_work(struct work_struct *work)
566{
567 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
568
David Brazdil0f672f62019-12-10 10:32:29 +0000569 nvme_read_ana_log(ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000570}
571
572static void nvme_anatt_timeout(struct timer_list *t)
573{
574 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
575
576 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
577 nvme_reset_ctrl(ctrl);
578}
579
580void nvme_mpath_stop(struct nvme_ctrl *ctrl)
581{
582 if (!nvme_ctrl_use_ana(ctrl))
583 return;
584 del_timer_sync(&ctrl->anatt_timer);
585 cancel_work_sync(&ctrl->ana_work);
586}
587
David Brazdil0f672f62019-12-10 10:32:29 +0000588#define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \
589 struct device_attribute subsys_attr_##_name = \
590 __ATTR(_name, _mode, _show, _store)
591
592static const char *nvme_iopolicy_names[] = {
593 [NVME_IOPOLICY_NUMA] = "numa",
594 [NVME_IOPOLICY_RR] = "round-robin",
595};
596
597static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
598 struct device_attribute *attr, char *buf)
599{
600 struct nvme_subsystem *subsys =
601 container_of(dev, struct nvme_subsystem, dev);
602
603 return sprintf(buf, "%s\n",
604 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
605}
606
607static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
608 struct device_attribute *attr, const char *buf, size_t count)
609{
610 struct nvme_subsystem *subsys =
611 container_of(dev, struct nvme_subsystem, dev);
612 int i;
613
614 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
615 if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
616 WRITE_ONCE(subsys->iopolicy, i);
617 return count;
618 }
619 }
620
621 return -EINVAL;
622}
623SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
624 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
625
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000626static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
627 char *buf)
628{
629 return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
630}
631DEVICE_ATTR_RO(ana_grpid);
632
633static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
634 char *buf)
635{
636 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
637
638 return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
639}
640DEVICE_ATTR_RO(ana_state);
641
642static int nvme_set_ns_ana_state(struct nvme_ctrl *ctrl,
643 struct nvme_ana_group_desc *desc, void *data)
644{
645 struct nvme_ns *ns = data;
646
647 if (ns->ana_grpid == le32_to_cpu(desc->grpid)) {
648 nvme_update_ns_ana_state(desc, ns);
649 return -ENXIO; /* just break out of the loop */
650 }
651
652 return 0;
653}
654
655void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
656{
657 if (nvme_ctrl_use_ana(ns->ctrl)) {
658 mutex_lock(&ns->ctrl->ana_lock);
659 ns->ana_grpid = le32_to_cpu(id->anagrpid);
660 nvme_parse_ana_log(ns->ctrl, ns, nvme_set_ns_ana_state);
661 mutex_unlock(&ns->ctrl->ana_lock);
662 } else {
663 mutex_lock(&ns->head->lock);
664 ns->ana_state = NVME_ANA_OPTIMIZED;
665 nvme_mpath_set_live(ns);
666 mutex_unlock(&ns->head->lock);
667 }
668}
669
670void nvme_mpath_remove_disk(struct nvme_ns_head *head)
671{
672 if (!head->disk)
673 return;
David Brazdil0f672f62019-12-10 10:32:29 +0000674 if (head->disk->flags & GENHD_FL_UP)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000675 del_gendisk(head->disk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000676 blk_set_queue_dying(head->disk->queue);
677 /* make sure all pending bios are cleaned up */
678 kblockd_schedule_work(&head->requeue_work);
679 flush_work(&head->requeue_work);
680 blk_cleanup_queue(head->disk->queue);
681 put_disk(head->disk);
682}
683
684int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
685{
686 int error;
687
David Brazdil0f672f62019-12-10 10:32:29 +0000688 /* check if multipath is enabled and we have the capability */
689 if (!multipath || !ctrl->subsys || !(ctrl->subsys->cmic & (1 << 3)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000690 return 0;
691
692 ctrl->anacap = id->anacap;
693 ctrl->anatt = id->anatt;
694 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
695 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
696
697 mutex_init(&ctrl->ana_lock);
698 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
699 ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
700 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc);
David Brazdil0f672f62019-12-10 10:32:29 +0000701 ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000702
703 if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) {
704 dev_err(ctrl->device,
705 "ANA log page size (%zd) larger than MDTS (%d).\n",
706 ctrl->ana_log_size,
707 ctrl->max_hw_sectors << SECTOR_SHIFT);
708 dev_err(ctrl->device, "disabling ANA support.\n");
709 return 0;
710 }
711
712 INIT_WORK(&ctrl->ana_work, nvme_ana_work);
713 ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
714 if (!ctrl->ana_log_buf) {
715 error = -ENOMEM;
716 goto out;
717 }
718
David Brazdil0f672f62019-12-10 10:32:29 +0000719 error = nvme_read_ana_log(ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000720 if (error)
721 goto out_free_ana_log_buf;
722 return 0;
723out_free_ana_log_buf:
724 kfree(ctrl->ana_log_buf);
David Brazdil0f672f62019-12-10 10:32:29 +0000725 ctrl->ana_log_buf = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000726out:
727 return error;
728}
729
730void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
731{
732 kfree(ctrl->ana_log_buf);
David Brazdil0f672f62019-12-10 10:32:29 +0000733 ctrl->ana_log_buf = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000734}
735