1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017-2018 Christoph Hellwig.
6 #include <linux/moduleparam.h>
7 #include <trace/events/block.h>
10 static bool multipath = true;
11 module_param(multipath, bool, 0444);
12 MODULE_PARM_DESC(multipath,
13 "turn on native support for multiple controllers per subsystem");
16 * If multipathing is enabled we need to always use the subsystem instance
17 * number for numbering our devices to avoid conflicts between subsystems that
18 * have multiple controllers and thus use the multipath-aware subsystem node
19 * and those that have a single controller and use the controller node
22 void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
23 struct nvme_ctrl *ctrl, int *flags)
26 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
27 } else if (ns->head->disk) {
28 sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
29 ctrl->instance, ns->head->instance);
30 *flags = GENHD_FL_HIDDEN;
32 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
37 void nvme_failover_req(struct request *req)
39 struct nvme_ns *ns = req->q->queuedata;
40 u16 status = nvme_req(req)->status;
43 spin_lock_irqsave(&ns->head->requeue_lock, flags);
44 blk_steal_bios(&ns->head->requeue_list, req);
45 spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
46 blk_mq_end_request(req, 0);
48 switch (status & 0x7ff) {
49 case NVME_SC_ANA_TRANSITION:
50 case NVME_SC_ANA_INACCESSIBLE:
51 case NVME_SC_ANA_PERSISTENT_LOSS:
53 * If we got back an ANA error we know the controller is alive,
54 * but not ready to serve this namespaces. The spec suggests
55 * we should update our general state here, but due to the fact
56 * that the admin and I/O queues are not serialized that is
57 * fundamentally racy. So instead just clear the current path,
58 * mark the the path as pending and kick of a re-read of the ANA
61 nvme_mpath_clear_current_path(ns);
62 if (ns->ctrl->ana_log_buf) {
63 set_bit(NVME_NS_ANA_PENDING, &ns->flags);
64 queue_work(nvme_wq, &ns->ctrl->ana_work);
67 case NVME_SC_HOST_PATH_ERROR:
69 * Temporary transport disruption in talking to the controller.
70 * Try to send on a new path.
72 nvme_mpath_clear_current_path(ns);
76 * Reset the controller for any non-ANA error as we don't know
77 * what caused the error.
79 nvme_reset_ctrl(ns->ctrl);
83 kblockd_schedule_work(&ns->head->requeue_work);
86 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
90 down_read(&ctrl->namespaces_rwsem);
91 list_for_each_entry(ns, &ctrl->namespaces, list) {
93 kblockd_schedule_work(&ns->head->requeue_work);
95 up_read(&ctrl->namespaces_rwsem);
98 static const char *nvme_ana_state_names[] = {
99 [0] = "invalid state",
100 [NVME_ANA_OPTIMIZED] = "optimized",
101 [NVME_ANA_NONOPTIMIZED] = "non-optimized",
102 [NVME_ANA_INACCESSIBLE] = "inaccessible",
103 [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss",
104 [NVME_ANA_CHANGE] = "change",
107 void nvme_mpath_clear_current_path(struct nvme_ns *ns)
109 struct nvme_ns_head *head = ns->head;
115 for_each_node(node) {
116 if (ns == rcu_access_pointer(head->current_path[node]))
117 rcu_assign_pointer(head->current_path[node], NULL);
121 static bool nvme_path_is_disabled(struct nvme_ns *ns)
123 return ns->ctrl->state != NVME_CTRL_LIVE ||
124 test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
125 test_bit(NVME_NS_REMOVING, &ns->flags);
128 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
130 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
131 struct nvme_ns *found = NULL, *fallback = NULL, *ns;
133 list_for_each_entry_rcu(ns, &head->list, siblings) {
134 if (nvme_path_is_disabled(ns))
137 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
138 distance = node_distance(node, ns->ctrl->numa_node);
140 distance = LOCAL_DISTANCE;
142 switch (ns->ana_state) {
143 case NVME_ANA_OPTIMIZED:
144 if (distance < found_distance) {
145 found_distance = distance;
149 case NVME_ANA_NONOPTIMIZED:
150 if (distance < fallback_distance) {
151 fallback_distance = distance;
163 rcu_assign_pointer(head->current_path[node], found);
167 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
170 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
174 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
177 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
178 int node, struct nvme_ns *old)
180 struct nvme_ns *ns, *found, *fallback = NULL;
182 if (list_is_singular(&head->list)) {
183 if (nvme_path_is_disabled(old))
188 for (ns = nvme_next_ns(head, old);
190 ns = nvme_next_ns(head, ns)) {
191 if (nvme_path_is_disabled(ns))
194 if (ns->ana_state == NVME_ANA_OPTIMIZED) {
198 if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
206 rcu_assign_pointer(head->current_path[node], found);
210 static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
212 return ns->ctrl->state == NVME_CTRL_LIVE &&
213 ns->ana_state == NVME_ANA_OPTIMIZED;
216 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
218 int node = numa_node_id();
221 ns = srcu_dereference(head->current_path[node], &head->srcu);
222 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR && ns)
223 ns = nvme_round_robin_path(head, node, ns);
224 if (unlikely(!ns || !nvme_path_is_optimized(ns)))
225 ns = __nvme_find_path(head, node);
229 static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
232 struct nvme_ns_head *head = q->queuedata;
233 struct device *dev = disk_to_dev(head->disk);
235 blk_qc_t ret = BLK_QC_T_NONE;
239 * The namespace might be going away and the bio might
240 * be moved to a different queue via blk_steal_bios(),
241 * so we need to use the bio_split pool from the original
242 * queue to allocate the bvecs from.
244 blk_queue_split(q, &bio);
246 srcu_idx = srcu_read_lock(&head->srcu);
247 ns = nvme_find_path(head);
249 bio->bi_disk = ns->disk;
250 bio->bi_opf |= REQ_NVME_MPATH;
251 trace_block_bio_remap(bio->bi_disk->queue, bio,
252 disk_devt(ns->head->disk),
253 bio->bi_iter.bi_sector);
254 ret = direct_make_request(bio);
255 } else if (!list_empty_careful(&head->list)) {
256 dev_warn_ratelimited(dev, "no path available - requeuing I/O\n");
258 spin_lock_irq(&head->requeue_lock);
259 bio_list_add(&head->requeue_list, bio);
260 spin_unlock_irq(&head->requeue_lock);
262 dev_warn_ratelimited(dev, "no path - failing I/O\n");
264 bio->bi_status = BLK_STS_IOERR;
268 srcu_read_unlock(&head->srcu, srcu_idx);
272 static void nvme_requeue_work(struct work_struct *work)
274 struct nvme_ns_head *head =
275 container_of(work, struct nvme_ns_head, requeue_work);
276 struct bio *bio, *next;
278 spin_lock_irq(&head->requeue_lock);
279 next = bio_list_get(&head->requeue_list);
280 spin_unlock_irq(&head->requeue_lock);
282 while ((bio = next) != NULL) {
287 * Reset disk to the mpath node and resubmit to select a new
290 bio->bi_disk = head->disk;
291 generic_make_request(bio);
295 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
297 struct request_queue *q;
300 mutex_init(&head->lock);
301 bio_list_init(&head->requeue_list);
302 spin_lock_init(&head->requeue_lock);
303 INIT_WORK(&head->requeue_work, nvme_requeue_work);
306 * Add a multipath node if the subsystems supports multiple controllers.
307 * We also do this for private namespaces as the namespace sharing data could
308 * change after a rescan.
310 if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
313 q = blk_alloc_queue_node(GFP_KERNEL, ctrl->numa_node);
317 blk_queue_make_request(q, nvme_ns_head_make_request);
318 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
319 /* set to a default value for 512 until disk is validated */
320 blk_queue_logical_block_size(q, 512);
321 blk_set_stacking_limits(&q->limits);
323 /* we need to propagate up the VMC settings */
324 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
326 blk_queue_write_cache(q, vwc, vwc);
328 head->disk = alloc_disk(0);
330 goto out_cleanup_queue;
331 head->disk->fops = &nvme_ns_head_ops;
332 head->disk->private_data = head;
333 head->disk->queue = q;
334 head->disk->flags = GENHD_FL_EXT_DEVT;
335 sprintf(head->disk->disk_name, "nvme%dn%d",
336 ctrl->subsys->instance, head->instance);
340 blk_cleanup_queue(q);
345 static void nvme_mpath_set_live(struct nvme_ns *ns)
347 struct nvme_ns_head *head = ns->head;
349 lockdep_assert_held(&ns->head->lock);
354 if (!(head->disk->flags & GENHD_FL_UP))
355 device_add_disk(&head->subsys->dev, head->disk,
356 nvme_ns_id_attr_groups);
358 if (nvme_path_is_optimized(ns)) {
361 srcu_idx = srcu_read_lock(&head->srcu);
363 __nvme_find_path(head, node);
364 srcu_read_unlock(&head->srcu, srcu_idx);
367 kblockd_schedule_work(&ns->head->requeue_work);
370 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
371 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
374 void *base = ctrl->ana_log_buf;
375 size_t offset = sizeof(struct nvme_ana_rsp_hdr);
378 lockdep_assert_held(&ctrl->ana_lock);
380 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
381 struct nvme_ana_group_desc *desc = base + offset;
382 u32 nr_nsids = le32_to_cpu(desc->nnsids);
383 size_t nsid_buf_size = nr_nsids * sizeof(__le32);
385 if (WARN_ON_ONCE(desc->grpid == 0))
387 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
389 if (WARN_ON_ONCE(desc->state == 0))
391 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
394 offset += sizeof(*desc);
395 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
398 error = cb(ctrl, desc, data);
402 offset += nsid_buf_size;
403 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
410 static inline bool nvme_state_is_live(enum nvme_ana_state state)
412 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
415 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
418 mutex_lock(&ns->head->lock);
419 ns->ana_grpid = le32_to_cpu(desc->grpid);
420 ns->ana_state = desc->state;
421 clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
423 if (nvme_state_is_live(ns->ana_state))
424 nvme_mpath_set_live(ns);
425 mutex_unlock(&ns->head->lock);
428 static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
429 struct nvme_ana_group_desc *desc, void *data)
431 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
432 unsigned *nr_change_groups = data;
435 dev_dbg(ctrl->device, "ANA group %d: %s.\n",
436 le32_to_cpu(desc->grpid),
437 nvme_ana_state_names[desc->state]);
439 if (desc->state == NVME_ANA_CHANGE)
440 (*nr_change_groups)++;
445 down_write(&ctrl->namespaces_rwsem);
446 list_for_each_entry(ns, &ctrl->namespaces, list) {
447 if (ns->head->ns_id != le32_to_cpu(desc->nsids[n]))
449 nvme_update_ns_ana_state(desc, ns);
453 up_write(&ctrl->namespaces_rwsem);
454 WARN_ON_ONCE(n < nr_nsids);
458 static int nvme_read_ana_log(struct nvme_ctrl *ctrl, bool groups_only)
460 u32 nr_change_groups = 0;
463 mutex_lock(&ctrl->ana_lock);
464 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA,
465 groups_only ? NVME_ANA_LOG_RGO : 0,
466 ctrl->ana_log_buf, ctrl->ana_log_size, 0);
468 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
472 error = nvme_parse_ana_log(ctrl, &nr_change_groups,
473 nvme_update_ana_state);
478 * In theory we should have an ANATT timer per group as they might enter
479 * the change state at different times. But that is a lot of overhead
480 * just to protect against a target that keeps entering new changes
481 * states while never finishing previous ones. But we'll still
482 * eventually time out once all groups are in change state, so this
485 * We also double the ANATT value to provide some slack for transports
486 * or AEN processing overhead.
488 if (nr_change_groups)
489 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
491 del_timer_sync(&ctrl->anatt_timer);
493 mutex_unlock(&ctrl->ana_lock);
497 static void nvme_ana_work(struct work_struct *work)
499 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
501 nvme_read_ana_log(ctrl, false);
504 static void nvme_anatt_timeout(struct timer_list *t)
506 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
508 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
509 nvme_reset_ctrl(ctrl);
512 void nvme_mpath_stop(struct nvme_ctrl *ctrl)
514 if (!nvme_ctrl_use_ana(ctrl))
516 del_timer_sync(&ctrl->anatt_timer);
517 cancel_work_sync(&ctrl->ana_work);
520 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \
521 struct device_attribute subsys_attr_##_name = \
522 __ATTR(_name, _mode, _show, _store)
524 static const char *nvme_iopolicy_names[] = {
525 [NVME_IOPOLICY_NUMA] = "numa",
526 [NVME_IOPOLICY_RR] = "round-robin",
529 static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
532 struct nvme_subsystem *subsys =
533 container_of(dev, struct nvme_subsystem, dev);
535 return sprintf(buf, "%s\n",
536 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
539 static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
540 struct device_attribute *attr, const char *buf, size_t count)
542 struct nvme_subsystem *subsys =
543 container_of(dev, struct nvme_subsystem, dev);
546 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
547 if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
548 WRITE_ONCE(subsys->iopolicy, i);
555 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
556 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
558 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
561 return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
563 DEVICE_ATTR_RO(ana_grpid);
565 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
568 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
570 return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
572 DEVICE_ATTR_RO(ana_state);
574 static int nvme_set_ns_ana_state(struct nvme_ctrl *ctrl,
575 struct nvme_ana_group_desc *desc, void *data)
577 struct nvme_ns *ns = data;
579 if (ns->ana_grpid == le32_to_cpu(desc->grpid)) {
580 nvme_update_ns_ana_state(desc, ns);
581 return -ENXIO; /* just break out of the loop */
587 void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
589 if (nvme_ctrl_use_ana(ns->ctrl)) {
590 mutex_lock(&ns->ctrl->ana_lock);
591 ns->ana_grpid = le32_to_cpu(id->anagrpid);
592 nvme_parse_ana_log(ns->ctrl, ns, nvme_set_ns_ana_state);
593 mutex_unlock(&ns->ctrl->ana_lock);
595 mutex_lock(&ns->head->lock);
596 ns->ana_state = NVME_ANA_OPTIMIZED;
597 nvme_mpath_set_live(ns);
598 mutex_unlock(&ns->head->lock);
602 void nvme_mpath_remove_disk(struct nvme_ns_head *head)
606 if (head->disk->flags & GENHD_FL_UP)
607 del_gendisk(head->disk);
608 blk_set_queue_dying(head->disk->queue);
609 /* make sure all pending bios are cleaned up */
610 kblockd_schedule_work(&head->requeue_work);
611 flush_work(&head->requeue_work);
612 blk_cleanup_queue(head->disk->queue);
613 put_disk(head->disk);
616 int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
620 /* check if multipath is enabled and we have the capability */
621 if (!multipath || !ctrl->subsys || !(ctrl->subsys->cmic & (1 << 3)))
624 ctrl->anacap = id->anacap;
625 ctrl->anatt = id->anatt;
626 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
627 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
629 mutex_init(&ctrl->ana_lock);
630 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
631 ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
632 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc);
633 ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32);
635 if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) {
636 dev_err(ctrl->device,
637 "ANA log page size (%zd) larger than MDTS (%d).\n",
639 ctrl->max_hw_sectors << SECTOR_SHIFT);
640 dev_err(ctrl->device, "disabling ANA support.\n");
644 INIT_WORK(&ctrl->ana_work, nvme_ana_work);
645 ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
646 if (!ctrl->ana_log_buf) {
651 error = nvme_read_ana_log(ctrl, true);
653 goto out_free_ana_log_buf;
655 out_free_ana_log_buf:
656 kfree(ctrl->ana_log_buf);
657 ctrl->ana_log_buf = NULL;
662 void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
664 kfree(ctrl->ana_log_buf);
665 ctrl->ana_log_buf = NULL;