blob: 838ee58d80cda8c5404e394a00ff7ea195b55a42 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * Copyright (c) 2017-2018 Christoph Hellwig.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 */
13
14#include <linux/moduleparam.h>
15#include <trace/events/block.h>
16#include "nvme.h"
17
18static bool multipath = true;
19module_param(multipath, bool, 0444);
20MODULE_PARM_DESC(multipath,
21 "turn on native support for multiple controllers per subsystem");
22
23void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
24{
25 struct nvme_ns_head *h;
26
27 lockdep_assert_held(&subsys->lock);
28 list_for_each_entry(h, &subsys->nsheads, entry)
29 if (h->disk)
30 blk_mq_unfreeze_queue(h->disk->queue);
31}
32
33void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
34{
35 struct nvme_ns_head *h;
36
37 lockdep_assert_held(&subsys->lock);
38 list_for_each_entry(h, &subsys->nsheads, entry)
39 if (h->disk)
40 blk_mq_freeze_queue_wait(h->disk->queue);
41}
42
43void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
44{
45 struct nvme_ns_head *h;
46
47 lockdep_assert_held(&subsys->lock);
48 list_for_each_entry(h, &subsys->nsheads, entry)
49 if (h->disk)
50 blk_freeze_queue_start(h->disk->queue);
51}
52
53/*
54 * If multipathing is enabled we need to always use the subsystem instance
55 * number for numbering our devices to avoid conflicts between subsystems that
56 * have multiple controllers and thus use the multipath-aware subsystem node
57 * and those that have a single controller and use the controller node
58 * directly.
59 */
60void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
61 struct nvme_ctrl *ctrl, int *flags)
62{
63 if (!multipath) {
64 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
65 } else if (ns->head->disk) {
66 sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
67 ctrl->cntlid, ns->head->instance);
68 *flags = GENHD_FL_HIDDEN;
69 } else {
70 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
71 ns->head->instance);
72 }
73}
74
75void nvme_failover_req(struct request *req)
76{
77 struct nvme_ns *ns = req->q->queuedata;
78 u16 status = nvme_req(req)->status;
79 unsigned long flags;
80
81 spin_lock_irqsave(&ns->head->requeue_lock, flags);
82 blk_steal_bios(&ns->head->requeue_list, req);
83 spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
84 blk_mq_end_request(req, 0);
85
86 switch (status & 0x7ff) {
87 case NVME_SC_ANA_TRANSITION:
88 case NVME_SC_ANA_INACCESSIBLE:
89 case NVME_SC_ANA_PERSISTENT_LOSS:
90 /*
91 * If we got back an ANA error we know the controller is alive,
92 * but not ready to serve this namespaces. The spec suggests
93 * we should update our general state here, but due to the fact
94 * that the admin and I/O queues are not serialized that is
95 * fundamentally racy. So instead just clear the current path,
96 * mark the the path as pending and kick of a re-read of the ANA
97 * log page ASAP.
98 */
99 nvme_mpath_clear_current_path(ns);
100 if (ns->ctrl->ana_log_buf) {
101 set_bit(NVME_NS_ANA_PENDING, &ns->flags);
102 queue_work(nvme_wq, &ns->ctrl->ana_work);
103 }
104 break;
105 case NVME_SC_HOST_PATH_ERROR:
106 /*
107 * Temporary transport disruption in talking to the controller.
108 * Try to send on a new path.
109 */
110 nvme_mpath_clear_current_path(ns);
111 break;
112 default:
113 /*
114 * Reset the controller for any non-ANA error as we don't know
115 * what caused the error.
116 */
117 nvme_reset_ctrl(ns->ctrl);
118 break;
119 }
120
121 kblockd_schedule_work(&ns->head->requeue_work);
122}
123
124void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
125{
126 struct nvme_ns *ns;
127
128 down_read(&ctrl->namespaces_rwsem);
129 list_for_each_entry(ns, &ctrl->namespaces, list) {
130 if (ns->head->disk)
131 kblockd_schedule_work(&ns->head->requeue_work);
132 }
133 up_read(&ctrl->namespaces_rwsem);
134}
135
136static const char *nvme_ana_state_names[] = {
137 [0] = "invalid state",
138 [NVME_ANA_OPTIMIZED] = "optimized",
139 [NVME_ANA_NONOPTIMIZED] = "non-optimized",
140 [NVME_ANA_INACCESSIBLE] = "inaccessible",
141 [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss",
142 [NVME_ANA_CHANGE] = "change",
143};
144
145static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head)
146{
147 struct nvme_ns *ns, *fallback = NULL;
148
149 list_for_each_entry_rcu(ns, &head->list, siblings) {
150 if (ns->ctrl->state != NVME_CTRL_LIVE ||
151 test_bit(NVME_NS_ANA_PENDING, &ns->flags))
152 continue;
153 switch (ns->ana_state) {
154 case NVME_ANA_OPTIMIZED:
155 rcu_assign_pointer(head->current_path, ns);
156 return ns;
157 case NVME_ANA_NONOPTIMIZED:
158 fallback = ns;
159 break;
160 default:
161 break;
162 }
163 }
164
165 if (fallback)
166 rcu_assign_pointer(head->current_path, fallback);
167 return fallback;
168}
169
170static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
171{
172 return ns->ctrl->state == NVME_CTRL_LIVE &&
173 ns->ana_state == NVME_ANA_OPTIMIZED;
174}
175
176inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
177{
178 struct nvme_ns *ns = srcu_dereference(head->current_path, &head->srcu);
179
180 if (unlikely(!ns || !nvme_path_is_optimized(ns)))
181 ns = __nvme_find_path(head);
182 return ns;
183}
184
185static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
186 struct bio *bio)
187{
188 struct nvme_ns_head *head = q->queuedata;
189 struct device *dev = disk_to_dev(head->disk);
190 struct nvme_ns *ns;
191 blk_qc_t ret = BLK_QC_T_NONE;
192 int srcu_idx;
193
194 srcu_idx = srcu_read_lock(&head->srcu);
195 ns = nvme_find_path(head);
196 if (likely(ns)) {
197 bio->bi_disk = ns->disk;
198 bio->bi_opf |= REQ_NVME_MPATH;
199 trace_block_bio_remap(bio->bi_disk->queue, bio,
200 disk_devt(ns->head->disk),
201 bio->bi_iter.bi_sector);
202 ret = direct_make_request(bio);
203 } else if (!list_empty_careful(&head->list)) {
204 dev_warn_ratelimited(dev, "no path available - requeuing I/O\n");
205
206 spin_lock_irq(&head->requeue_lock);
207 bio_list_add(&head->requeue_list, bio);
208 spin_unlock_irq(&head->requeue_lock);
209 } else {
210 dev_warn_ratelimited(dev, "no path - failing I/O\n");
211
212 bio->bi_status = BLK_STS_IOERR;
213 bio_endio(bio);
214 }
215
216 srcu_read_unlock(&head->srcu, srcu_idx);
217 return ret;
218}
219
220static bool nvme_ns_head_poll(struct request_queue *q, blk_qc_t qc)
221{
222 struct nvme_ns_head *head = q->queuedata;
223 struct nvme_ns *ns;
224 bool found = false;
225 int srcu_idx;
226
227 srcu_idx = srcu_read_lock(&head->srcu);
228 ns = srcu_dereference(head->current_path, &head->srcu);
229 if (likely(ns && nvme_path_is_optimized(ns)))
230 found = ns->queue->poll_fn(q, qc);
231 srcu_read_unlock(&head->srcu, srcu_idx);
232 return found;
233}
234
235static void nvme_requeue_work(struct work_struct *work)
236{
237 struct nvme_ns_head *head =
238 container_of(work, struct nvme_ns_head, requeue_work);
239 struct bio *bio, *next;
240
241 spin_lock_irq(&head->requeue_lock);
242 next = bio_list_get(&head->requeue_list);
243 spin_unlock_irq(&head->requeue_lock);
244
245 while ((bio = next) != NULL) {
246 next = bio->bi_next;
247 bio->bi_next = NULL;
248
249 /*
250 * Reset disk to the mpath node and resubmit to select a new
251 * path.
252 */
253 bio->bi_disk = head->disk;
254 generic_make_request(bio);
255 }
256}
257
258int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
259{
260 struct request_queue *q;
261 bool vwc = false;
262
263 mutex_init(&head->lock);
264 bio_list_init(&head->requeue_list);
265 spin_lock_init(&head->requeue_lock);
266 INIT_WORK(&head->requeue_work, nvme_requeue_work);
267
268 /*
269 * Add a multipath node if the subsystems supports multiple controllers.
270 * We also do this for private namespaces as the namespace sharing data could
271 * change after a rescan.
272 */
273 if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
274 return 0;
275
276 q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE, NULL);
277 if (!q)
278 goto out;
279 q->queuedata = head;
280 blk_queue_make_request(q, nvme_ns_head_make_request);
281 q->poll_fn = nvme_ns_head_poll;
282 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
283 /* set to a default value for 512 until disk is validated */
284 blk_queue_logical_block_size(q, 512);
285 blk_set_stacking_limits(&q->limits);
286
287 /* we need to propagate up the VMC settings */
288 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
289 vwc = true;
290 blk_queue_write_cache(q, vwc, vwc);
291
292 head->disk = alloc_disk(0);
293 if (!head->disk)
294 goto out_cleanup_queue;
295 head->disk->fops = &nvme_ns_head_ops;
296 head->disk->private_data = head;
297 head->disk->queue = q;
298 head->disk->flags = GENHD_FL_EXT_DEVT;
299 sprintf(head->disk->disk_name, "nvme%dn%d",
300 ctrl->subsys->instance, head->instance);
301 return 0;
302
303out_cleanup_queue:
304 blk_cleanup_queue(q);
305out:
306 return -ENOMEM;
307}
308
309static void nvme_mpath_set_live(struct nvme_ns *ns)
310{
311 struct nvme_ns_head *head = ns->head;
312
313 lockdep_assert_held(&ns->head->lock);
314
315 if (!head->disk)
316 return;
317
318 if (!(head->disk->flags & GENHD_FL_UP)) {
319 device_add_disk(&head->subsys->dev, head->disk);
320 if (sysfs_create_group(&disk_to_dev(head->disk)->kobj,
321 &nvme_ns_id_attr_group))
322 dev_warn(&head->subsys->dev,
323 "failed to create id group.\n");
324 }
325
326 synchronize_srcu(&ns->head->srcu);
327 kblockd_schedule_work(&ns->head->requeue_work);
328}
329
330static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
331 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
332 void *))
333{
334 void *base = ctrl->ana_log_buf;
335 size_t offset = sizeof(struct nvme_ana_rsp_hdr);
336 int error, i;
337
338 lockdep_assert_held(&ctrl->ana_lock);
339
340 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
341 struct nvme_ana_group_desc *desc = base + offset;
342 u32 nr_nsids = le32_to_cpu(desc->nnsids);
343 size_t nsid_buf_size = nr_nsids * sizeof(__le32);
344
345 if (WARN_ON_ONCE(desc->grpid == 0))
346 return -EINVAL;
347 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
348 return -EINVAL;
349 if (WARN_ON_ONCE(desc->state == 0))
350 return -EINVAL;
351 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
352 return -EINVAL;
353
354 offset += sizeof(*desc);
355 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
356 return -EINVAL;
357
358 error = cb(ctrl, desc, data);
359 if (error)
360 return error;
361
362 offset += nsid_buf_size;
363 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
364 return -EINVAL;
365 }
366
367 return 0;
368}
369
370static inline bool nvme_state_is_live(enum nvme_ana_state state)
371{
372 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
373}
374
375static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
376 struct nvme_ns *ns)
377{
378 mutex_lock(&ns->head->lock);
379 ns->ana_grpid = le32_to_cpu(desc->grpid);
380 ns->ana_state = desc->state;
381 clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
382
383 if (nvme_state_is_live(ns->ana_state))
384 nvme_mpath_set_live(ns);
385 mutex_unlock(&ns->head->lock);
386}
387
388static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
389 struct nvme_ana_group_desc *desc, void *data)
390{
391 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
392 unsigned *nr_change_groups = data;
393 struct nvme_ns *ns;
394
395 dev_info(ctrl->device, "ANA group %d: %s.\n",
396 le32_to_cpu(desc->grpid),
397 nvme_ana_state_names[desc->state]);
398
399 if (desc->state == NVME_ANA_CHANGE)
400 (*nr_change_groups)++;
401
402 if (!nr_nsids)
403 return 0;
404
405 down_write(&ctrl->namespaces_rwsem);
406 list_for_each_entry(ns, &ctrl->namespaces, list) {
407 unsigned nsid = le32_to_cpu(desc->nsids[n]);
408
409 if (ns->head->ns_id < nsid)
410 continue;
411 if (ns->head->ns_id == nsid)
412 nvme_update_ns_ana_state(desc, ns);
413 if (++n == nr_nsids)
414 break;
415 }
416 up_write(&ctrl->namespaces_rwsem);
417 return 0;
418}
419
420static int nvme_read_ana_log(struct nvme_ctrl *ctrl, bool groups_only)
421{
422 u32 nr_change_groups = 0;
423 int error;
424
425 mutex_lock(&ctrl->ana_lock);
426 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA,
427 groups_only ? NVME_ANA_LOG_RGO : 0,
428 ctrl->ana_log_buf, ctrl->ana_log_size, 0);
429 if (error) {
430 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
431 goto out_unlock;
432 }
433
434 error = nvme_parse_ana_log(ctrl, &nr_change_groups,
435 nvme_update_ana_state);
436 if (error)
437 goto out_unlock;
438
439 /*
440 * In theory we should have an ANATT timer per group as they might enter
441 * the change state at different times. But that is a lot of overhead
442 * just to protect against a target that keeps entering new changes
443 * states while never finishing previous ones. But we'll still
444 * eventually time out once all groups are in change state, so this
445 * isn't a big deal.
446 *
447 * We also double the ANATT value to provide some slack for transports
448 * or AEN processing overhead.
449 */
450 if (nr_change_groups)
451 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
452 else
453 del_timer_sync(&ctrl->anatt_timer);
454out_unlock:
455 mutex_unlock(&ctrl->ana_lock);
456 return error;
457}
458
459static void nvme_ana_work(struct work_struct *work)
460{
461 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
462
463 nvme_read_ana_log(ctrl, false);
464}
465
466static void nvme_anatt_timeout(struct timer_list *t)
467{
468 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
469
470 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
471 nvme_reset_ctrl(ctrl);
472}
473
474void nvme_mpath_stop(struct nvme_ctrl *ctrl)
475{
476 if (!nvme_ctrl_use_ana(ctrl))
477 return;
478 del_timer_sync(&ctrl->anatt_timer);
479 cancel_work_sync(&ctrl->ana_work);
480}
481
482static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
483 char *buf)
484{
485 return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
486}
487DEVICE_ATTR_RO(ana_grpid);
488
489static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
490 char *buf)
491{
492 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
493
494 return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
495}
496DEVICE_ATTR_RO(ana_state);
497
498static int nvme_set_ns_ana_state(struct nvme_ctrl *ctrl,
499 struct nvme_ana_group_desc *desc, void *data)
500{
501 struct nvme_ns *ns = data;
502
503 if (ns->ana_grpid == le32_to_cpu(desc->grpid)) {
504 nvme_update_ns_ana_state(desc, ns);
505 return -ENXIO; /* just break out of the loop */
506 }
507
508 return 0;
509}
510
511void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
512{
513 if (nvme_ctrl_use_ana(ns->ctrl)) {
514 mutex_lock(&ns->ctrl->ana_lock);
515 ns->ana_grpid = le32_to_cpu(id->anagrpid);
516 nvme_parse_ana_log(ns->ctrl, ns, nvme_set_ns_ana_state);
517 mutex_unlock(&ns->ctrl->ana_lock);
518 } else {
519 mutex_lock(&ns->head->lock);
520 ns->ana_state = NVME_ANA_OPTIMIZED;
521 nvme_mpath_set_live(ns);
522 mutex_unlock(&ns->head->lock);
523 }
524}
525
526void nvme_mpath_remove_disk(struct nvme_ns_head *head)
527{
528 if (!head->disk)
529 return;
530 if (head->disk->flags & GENHD_FL_UP) {
531 sysfs_remove_group(&disk_to_dev(head->disk)->kobj,
532 &nvme_ns_id_attr_group);
533 del_gendisk(head->disk);
534 }
535 blk_set_queue_dying(head->disk->queue);
536 /* make sure all pending bios are cleaned up */
537 kblockd_schedule_work(&head->requeue_work);
538 flush_work(&head->requeue_work);
539 blk_cleanup_queue(head->disk->queue);
540 put_disk(head->disk);
541}
542
543int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
544{
545 int error;
546
547 /* check if multipath is enabled and we have the capability */
548 if (!multipath || !ctrl->subsys || !(ctrl->subsys->cmic & (1 << 3)))
549 return 0;
550
551 ctrl->anacap = id->anacap;
552 ctrl->anatt = id->anatt;
553 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
554 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
555
556 mutex_init(&ctrl->ana_lock);
557 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
558 ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
559 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc);
560 ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32);
561
562 if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) {
563 dev_err(ctrl->device,
564 "ANA log page size (%zd) larger than MDTS (%d).\n",
565 ctrl->ana_log_size,
566 ctrl->max_hw_sectors << SECTOR_SHIFT);
567 dev_err(ctrl->device, "disabling ANA support.\n");
568 return 0;
569 }
570
571 INIT_WORK(&ctrl->ana_work, nvme_ana_work);
572 ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
573 if (!ctrl->ana_log_buf) {
574 error = -ENOMEM;
575 goto out;
576 }
577
578 error = nvme_read_ana_log(ctrl, false);
579 if (error)
580 goto out_free_ana_log_buf;
581 return 0;
582out_free_ana_log_buf:
583 kfree(ctrl->ana_log_buf);
584 ctrl->ana_log_buf = NULL;
585out:
586 return error;
587}
588
589void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
590{
591 kfree(ctrl->ana_log_buf);
592 ctrl->ana_log_buf = NULL;
593}
594