1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017-2018 Christoph Hellwig.
6 #include <linux/backing-dev.h>
7 #include <linux/moduleparam.h>
8 #include <linux/vmalloc.h>
9 #include <trace/events/block.h>
12 bool multipath = true;
13 module_param(multipath, bool, 0444);
14 MODULE_PARM_DESC(multipath,
15 "turn on native support for multiple controllers per subsystem");
17 static const char *nvme_iopolicy_names[] = {
18 [NVME_IOPOLICY_NUMA] = "numa",
19 [NVME_IOPOLICY_RR] = "round-robin",
22 static int iopolicy = NVME_IOPOLICY_NUMA;
24 static int nvme_set_iopolicy(const char *val, const struct kernel_param *kp)
28 if (!strncmp(val, "numa", 4))
29 iopolicy = NVME_IOPOLICY_NUMA;
30 else if (!strncmp(val, "round-robin", 11))
31 iopolicy = NVME_IOPOLICY_RR;
38 static int nvme_get_iopolicy(char *buf, const struct kernel_param *kp)
40 return sprintf(buf, "%s\n", nvme_iopolicy_names[iopolicy]);
43 module_param_call(iopolicy, nvme_set_iopolicy, nvme_get_iopolicy,
45 MODULE_PARM_DESC(iopolicy,
46 "Default multipath I/O policy; 'numa' (default) or 'round-robin'");
48 void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys)
50 subsys->iopolicy = iopolicy;
53 void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
55 struct nvme_ns_head *h;
57 lockdep_assert_held(&subsys->lock);
58 list_for_each_entry(h, &subsys->nsheads, entry)
60 blk_mq_unfreeze_queue(h->disk->queue);
63 void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
65 struct nvme_ns_head *h;
67 lockdep_assert_held(&subsys->lock);
68 list_for_each_entry(h, &subsys->nsheads, entry)
70 blk_mq_freeze_queue_wait(h->disk->queue);
73 void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
75 struct nvme_ns_head *h;
77 lockdep_assert_held(&subsys->lock);
78 list_for_each_entry(h, &subsys->nsheads, entry)
80 blk_freeze_queue_start(h->disk->queue);
83 void nvme_failover_req(struct request *req)
85 struct nvme_ns *ns = req->q->queuedata;
86 u16 status = nvme_req(req)->status & 0x7ff;
90 nvme_mpath_clear_current_path(ns);
93 * If we got back an ANA error, we know the controller is alive but not
94 * ready to serve this namespace. Kick of a re-read of the ANA
95 * information page, and just try any other available path for now.
97 if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) {
98 set_bit(NVME_NS_ANA_PENDING, &ns->flags);
99 queue_work(nvme_wq, &ns->ctrl->ana_work);
102 spin_lock_irqsave(&ns->head->requeue_lock, flags);
103 for (bio = req->bio; bio; bio = bio->bi_next) {
104 bio_set_dev(bio, ns->head->disk->part0);
105 if (bio->bi_opf & REQ_POLLED) {
106 bio->bi_opf &= ~REQ_POLLED;
107 bio->bi_cookie = BLK_QC_T_NONE;
110 blk_steal_bios(&ns->head->requeue_list, req);
111 spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
113 blk_mq_end_request(req, 0);
114 kblockd_schedule_work(&ns->head->requeue_work);
117 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
121 down_read(&ctrl->namespaces_rwsem);
122 list_for_each_entry(ns, &ctrl->namespaces, list) {
125 kblockd_schedule_work(&ns->head->requeue_work);
126 if (ctrl->state == NVME_CTRL_LIVE)
127 disk_uevent(ns->head->disk, KOBJ_CHANGE);
129 up_read(&ctrl->namespaces_rwsem);
132 static const char *nvme_ana_state_names[] = {
133 [0] = "invalid state",
134 [NVME_ANA_OPTIMIZED] = "optimized",
135 [NVME_ANA_NONOPTIMIZED] = "non-optimized",
136 [NVME_ANA_INACCESSIBLE] = "inaccessible",
137 [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss",
138 [NVME_ANA_CHANGE] = "change",
141 bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
143 struct nvme_ns_head *head = ns->head;
144 bool changed = false;
150 for_each_node(node) {
151 if (ns == rcu_access_pointer(head->current_path[node])) {
152 rcu_assign_pointer(head->current_path[node], NULL);
160 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
164 down_read(&ctrl->namespaces_rwsem);
165 list_for_each_entry(ns, &ctrl->namespaces, list) {
166 nvme_mpath_clear_current_path(ns);
167 kblockd_schedule_work(&ns->head->requeue_work);
169 up_read(&ctrl->namespaces_rwsem);
172 void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
174 struct nvme_ns_head *head = ns->head;
175 sector_t capacity = get_capacity(head->disk);
178 list_for_each_entry_rcu(ns, &head->list, siblings) {
179 if (capacity != get_capacity(ns->disk))
180 clear_bit(NVME_NS_READY, &ns->flags);
184 rcu_assign_pointer(head->current_path[node], NULL);
187 static bool nvme_path_is_disabled(struct nvme_ns *ns)
190 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
191 * still be able to complete assuming that the controller is connected.
192 * Otherwise it will fail immediately and return to the requeue list.
194 if (ns->ctrl->state != NVME_CTRL_LIVE &&
195 ns->ctrl->state != NVME_CTRL_DELETING)
197 if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
198 !test_bit(NVME_NS_READY, &ns->flags))
203 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
205 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
206 struct nvme_ns *found = NULL, *fallback = NULL, *ns;
208 list_for_each_entry_rcu(ns, &head->list, siblings) {
209 if (nvme_path_is_disabled(ns))
212 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
213 distance = node_distance(node, ns->ctrl->numa_node);
215 distance = LOCAL_DISTANCE;
217 switch (ns->ana_state) {
218 case NVME_ANA_OPTIMIZED:
219 if (distance < found_distance) {
220 found_distance = distance;
224 case NVME_ANA_NONOPTIMIZED:
225 if (distance < fallback_distance) {
226 fallback_distance = distance;
238 rcu_assign_pointer(head->current_path[node], found);
242 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
245 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
249 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
252 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
253 int node, struct nvme_ns *old)
255 struct nvme_ns *ns, *found = NULL;
257 if (list_is_singular(&head->list)) {
258 if (nvme_path_is_disabled(old))
263 for (ns = nvme_next_ns(head, old);
265 ns = nvme_next_ns(head, ns)) {
266 if (nvme_path_is_disabled(ns))
269 if (ns->ana_state == NVME_ANA_OPTIMIZED) {
273 if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
278 * The loop above skips the current path for round-robin semantics.
279 * Fall back to the current path if either:
280 * - no other optimized path found and current is optimized,
281 * - no other usable path found and current is usable.
283 if (!nvme_path_is_disabled(old) &&
284 (old->ana_state == NVME_ANA_OPTIMIZED ||
285 (!found && old->ana_state == NVME_ANA_NONOPTIMIZED)))
291 rcu_assign_pointer(head->current_path[node], found);
295 static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
297 return ns->ctrl->state == NVME_CTRL_LIVE &&
298 ns->ana_state == NVME_ANA_OPTIMIZED;
301 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
303 int node = numa_node_id();
306 ns = srcu_dereference(head->current_path[node], &head->srcu);
308 return __nvme_find_path(head, node);
310 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR)
311 return nvme_round_robin_path(head, node, ns);
312 if (unlikely(!nvme_path_is_optimized(ns)))
313 return __nvme_find_path(head, node);
317 static bool nvme_available_path(struct nvme_ns_head *head)
321 list_for_each_entry_rcu(ns, &head->list, siblings) {
322 if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags))
324 switch (ns->ctrl->state) {
326 case NVME_CTRL_RESETTING:
327 case NVME_CTRL_CONNECTING:
337 static void nvme_ns_head_submit_bio(struct bio *bio)
339 struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data;
340 struct device *dev = disk_to_dev(head->disk);
345 * The namespace might be going away and the bio might be moved to a
346 * different queue via blk_steal_bios(), so we need to use the bio_split
347 * pool from the original queue to allocate the bvecs from.
349 blk_queue_split(&bio);
351 srcu_idx = srcu_read_lock(&head->srcu);
352 ns = nvme_find_path(head);
354 bio_set_dev(bio, ns->disk->part0);
355 bio->bi_opf |= REQ_NVME_MPATH;
356 trace_block_bio_remap(bio, disk_devt(ns->head->disk),
357 bio->bi_iter.bi_sector);
358 submit_bio_noacct(bio);
359 } else if (nvme_available_path(head)) {
360 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
362 spin_lock_irq(&head->requeue_lock);
363 bio_list_add(&head->requeue_list, bio);
364 spin_unlock_irq(&head->requeue_lock);
366 dev_warn_ratelimited(dev, "no available path - failing I/O\n");
371 srcu_read_unlock(&head->srcu, srcu_idx);
374 static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
376 if (!nvme_tryget_ns_head(bdev->bd_disk->private_data))
381 static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
383 nvme_put_ns_head(disk->private_data);
386 #ifdef CONFIG_BLK_DEV_ZONED
387 static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector,
388 unsigned int nr_zones, report_zones_cb cb, void *data)
390 struct nvme_ns_head *head = disk->private_data;
392 int srcu_idx, ret = -EWOULDBLOCK;
394 srcu_idx = srcu_read_lock(&head->srcu);
395 ns = nvme_find_path(head);
397 ret = nvme_ns_report_zones(ns, sector, nr_zones, cb, data);
398 srcu_read_unlock(&head->srcu, srcu_idx);
402 #define nvme_ns_head_report_zones NULL
403 #endif /* CONFIG_BLK_DEV_ZONED */
405 const struct block_device_operations nvme_ns_head_ops = {
406 .owner = THIS_MODULE,
407 .submit_bio = nvme_ns_head_submit_bio,
408 .open = nvme_ns_head_open,
409 .release = nvme_ns_head_release,
410 .ioctl = nvme_ns_head_ioctl,
411 .compat_ioctl = blkdev_compat_ptr_ioctl,
412 .getgeo = nvme_getgeo,
413 .report_zones = nvme_ns_head_report_zones,
414 .pr_ops = &nvme_pr_ops,
417 static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev)
419 return container_of(cdev, struct nvme_ns_head, cdev);
422 static int nvme_ns_head_chr_open(struct inode *inode, struct file *file)
424 if (!nvme_tryget_ns_head(cdev_to_ns_head(inode->i_cdev)))
429 static int nvme_ns_head_chr_release(struct inode *inode, struct file *file)
431 nvme_put_ns_head(cdev_to_ns_head(inode->i_cdev));
435 static const struct file_operations nvme_ns_head_chr_fops = {
436 .owner = THIS_MODULE,
437 .open = nvme_ns_head_chr_open,
438 .release = nvme_ns_head_chr_release,
439 .unlocked_ioctl = nvme_ns_head_chr_ioctl,
440 .compat_ioctl = compat_ptr_ioctl,
441 .uring_cmd = nvme_ns_head_chr_uring_cmd,
444 static int nvme_add_ns_head_cdev(struct nvme_ns_head *head)
448 head->cdev_device.parent = &head->subsys->dev;
449 ret = dev_set_name(&head->cdev_device, "ng%dn%d",
450 head->subsys->instance, head->instance);
453 ret = nvme_cdev_add(&head->cdev, &head->cdev_device,
454 &nvme_ns_head_chr_fops, THIS_MODULE);
458 static void nvme_requeue_work(struct work_struct *work)
460 struct nvme_ns_head *head =
461 container_of(work, struct nvme_ns_head, requeue_work);
462 struct bio *bio, *next;
464 spin_lock_irq(&head->requeue_lock);
465 next = bio_list_get(&head->requeue_list);
466 spin_unlock_irq(&head->requeue_lock);
468 while ((bio = next) != NULL) {
472 submit_bio_noacct(bio);
476 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
480 mutex_init(&head->lock);
481 bio_list_init(&head->requeue_list);
482 spin_lock_init(&head->requeue_lock);
483 INIT_WORK(&head->requeue_work, nvme_requeue_work);
486 * Add a multipath node if the subsystems supports multiple controllers.
487 * We also do this for private namespaces as the namespace sharing flag
488 * could change after a rescan.
490 if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) ||
491 !nvme_is_unique_nsid(ctrl, head) || !multipath)
494 head->disk = blk_alloc_disk(ctrl->numa_node);
497 head->disk->fops = &nvme_ns_head_ops;
498 head->disk->private_data = head;
499 sprintf(head->disk->disk_name, "nvme%dn%d",
500 ctrl->subsys->instance, head->instance);
502 blk_queue_flag_set(QUEUE_FLAG_NONROT, head->disk->queue);
503 blk_queue_flag_set(QUEUE_FLAG_NOWAIT, head->disk->queue);
505 * This assumes all controllers that refer to a namespace either
506 * support poll queues or not. That is not a strict guarantee,
507 * but if the assumption is wrong the effect is only suboptimal
508 * performance but not correctness problem.
510 if (ctrl->tagset->nr_maps > HCTX_TYPE_POLL &&
511 ctrl->tagset->map[HCTX_TYPE_POLL].nr_queues)
512 blk_queue_flag_set(QUEUE_FLAG_POLL, head->disk->queue);
514 /* set to a default value of 512 until the disk is validated */
515 blk_queue_logical_block_size(head->disk->queue, 512);
516 blk_set_stacking_limits(&head->disk->queue->limits);
518 /* we need to propagate up the VMC settings */
519 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
521 blk_queue_write_cache(head->disk->queue, vwc, vwc);
525 static void nvme_mpath_set_live(struct nvme_ns *ns)
527 struct nvme_ns_head *head = ns->head;
534 * test_and_set_bit() is used because it is protecting against two nvme
535 * paths simultaneously calling device_add_disk() on the same namespace
538 if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
539 rc = device_add_disk(&head->subsys->dev, head->disk,
540 nvme_ns_id_attr_groups);
542 clear_bit(NVME_NSHEAD_DISK_LIVE, &ns->flags);
545 nvme_add_ns_head_cdev(head);
548 mutex_lock(&head->lock);
549 if (nvme_path_is_optimized(ns)) {
552 srcu_idx = srcu_read_lock(&head->srcu);
554 __nvme_find_path(head, node);
555 srcu_read_unlock(&head->srcu, srcu_idx);
557 mutex_unlock(&head->lock);
559 synchronize_srcu(&head->srcu);
560 kblockd_schedule_work(&head->requeue_work);
563 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
564 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
567 void *base = ctrl->ana_log_buf;
568 size_t offset = sizeof(struct nvme_ana_rsp_hdr);
571 lockdep_assert_held(&ctrl->ana_lock);
573 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
574 struct nvme_ana_group_desc *desc = base + offset;
576 size_t nsid_buf_size;
578 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
581 nr_nsids = le32_to_cpu(desc->nnsids);
582 nsid_buf_size = flex_array_size(desc, nsids, nr_nsids);
584 if (WARN_ON_ONCE(desc->grpid == 0))
586 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
588 if (WARN_ON_ONCE(desc->state == 0))
590 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
593 offset += sizeof(*desc);
594 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
597 error = cb(ctrl, desc, data);
601 offset += nsid_buf_size;
607 static inline bool nvme_state_is_live(enum nvme_ana_state state)
609 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
612 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
615 ns->ana_grpid = le32_to_cpu(desc->grpid);
616 ns->ana_state = desc->state;
617 clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
619 * nvme_mpath_set_live() will trigger I/O to the multipath path device
620 * and in turn to this path device. However we cannot accept this I/O
621 * if the controller is not live. This may deadlock if called from
622 * nvme_mpath_init_identify() and the ctrl will never complete
623 * initialization, preventing I/O from completing. For this case we
624 * will reprocess the ANA log page in nvme_mpath_update() once the
625 * controller is ready.
627 if (nvme_state_is_live(ns->ana_state) &&
628 ns->ctrl->state == NVME_CTRL_LIVE)
629 nvme_mpath_set_live(ns);
632 static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
633 struct nvme_ana_group_desc *desc, void *data)
635 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
636 unsigned *nr_change_groups = data;
639 dev_dbg(ctrl->device, "ANA group %d: %s.\n",
640 le32_to_cpu(desc->grpid),
641 nvme_ana_state_names[desc->state]);
643 if (desc->state == NVME_ANA_CHANGE)
644 (*nr_change_groups)++;
649 down_read(&ctrl->namespaces_rwsem);
650 list_for_each_entry(ns, &ctrl->namespaces, list) {
653 nsid = le32_to_cpu(desc->nsids[n]);
654 if (ns->head->ns_id < nsid)
656 if (ns->head->ns_id == nsid)
657 nvme_update_ns_ana_state(desc, ns);
660 if (ns->head->ns_id > nsid)
663 up_read(&ctrl->namespaces_rwsem);
667 static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
669 u32 nr_change_groups = 0;
672 mutex_lock(&ctrl->ana_lock);
673 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
674 ctrl->ana_log_buf, ctrl->ana_log_size, 0);
676 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
680 error = nvme_parse_ana_log(ctrl, &nr_change_groups,
681 nvme_update_ana_state);
686 * In theory we should have an ANATT timer per group as they might enter
687 * the change state at different times. But that is a lot of overhead
688 * just to protect against a target that keeps entering new changes
689 * states while never finishing previous ones. But we'll still
690 * eventually time out once all groups are in change state, so this
693 * We also double the ANATT value to provide some slack for transports
694 * or AEN processing overhead.
696 if (nr_change_groups)
697 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
699 del_timer_sync(&ctrl->anatt_timer);
701 mutex_unlock(&ctrl->ana_lock);
705 static void nvme_ana_work(struct work_struct *work)
707 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
709 if (ctrl->state != NVME_CTRL_LIVE)
712 nvme_read_ana_log(ctrl);
715 void nvme_mpath_update(struct nvme_ctrl *ctrl)
717 u32 nr_change_groups = 0;
719 if (!ctrl->ana_log_buf)
722 mutex_lock(&ctrl->ana_lock);
723 nvme_parse_ana_log(ctrl, &nr_change_groups, nvme_update_ana_state);
724 mutex_unlock(&ctrl->ana_lock);
727 static void nvme_anatt_timeout(struct timer_list *t)
729 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
731 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
732 nvme_reset_ctrl(ctrl);
735 void nvme_mpath_stop(struct nvme_ctrl *ctrl)
737 if (!nvme_ctrl_use_ana(ctrl))
739 del_timer_sync(&ctrl->anatt_timer);
740 cancel_work_sync(&ctrl->ana_work);
743 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \
744 struct device_attribute subsys_attr_##_name = \
745 __ATTR(_name, _mode, _show, _store)
747 static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
748 struct device_attribute *attr, char *buf)
750 struct nvme_subsystem *subsys =
751 container_of(dev, struct nvme_subsystem, dev);
753 return sysfs_emit(buf, "%s\n",
754 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
757 static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
758 struct device_attribute *attr, const char *buf, size_t count)
760 struct nvme_subsystem *subsys =
761 container_of(dev, struct nvme_subsystem, dev);
764 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
765 if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
766 WRITE_ONCE(subsys->iopolicy, i);
773 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
774 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
776 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
779 return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
781 DEVICE_ATTR_RO(ana_grpid);
783 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
786 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
788 return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
790 DEVICE_ATTR_RO(ana_state);
792 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
793 struct nvme_ana_group_desc *desc, void *data)
795 struct nvme_ana_group_desc *dst = data;
797 if (desc->grpid != dst->grpid)
801 return -ENXIO; /* just break out of the loop */
804 void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid)
806 if (nvme_ctrl_use_ana(ns->ctrl)) {
807 struct nvme_ana_group_desc desc = {
812 mutex_lock(&ns->ctrl->ana_lock);
813 ns->ana_grpid = le32_to_cpu(anagrpid);
814 nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
815 mutex_unlock(&ns->ctrl->ana_lock);
817 /* found the group desc: update */
818 nvme_update_ns_ana_state(&desc, ns);
820 /* group desc not found: trigger a re-read */
821 set_bit(NVME_NS_ANA_PENDING, &ns->flags);
822 queue_work(nvme_wq, &ns->ctrl->ana_work);
825 ns->ana_state = NVME_ANA_OPTIMIZED;
826 nvme_mpath_set_live(ns);
829 if (blk_queue_stable_writes(ns->queue) && ns->head->disk)
830 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES,
831 ns->head->disk->queue);
832 #ifdef CONFIG_BLK_DEV_ZONED
833 if (blk_queue_is_zoned(ns->queue) && ns->head->disk)
834 ns->head->disk->nr_zones = ns->disk->nr_zones;
838 void nvme_mpath_shutdown_disk(struct nvme_ns_head *head)
842 kblockd_schedule_work(&head->requeue_work);
843 if (test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
844 nvme_cdev_del(&head->cdev, &head->cdev_device);
845 del_gendisk(head->disk);
849 void nvme_mpath_remove_disk(struct nvme_ns_head *head)
853 blk_mark_disk_dead(head->disk);
854 /* make sure all pending bios are cleaned up */
855 kblockd_schedule_work(&head->requeue_work);
856 flush_work(&head->requeue_work);
857 put_disk(head->disk);
860 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
862 mutex_init(&ctrl->ana_lock);
863 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
864 INIT_WORK(&ctrl->ana_work, nvme_ana_work);
867 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
869 size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT;
873 /* check if multipath is enabled and we have the capability */
874 if (!multipath || !ctrl->subsys ||
875 !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA))
878 if (!ctrl->max_namespaces ||
879 ctrl->max_namespaces > le32_to_cpu(id->nn)) {
880 dev_err(ctrl->device,
881 "Invalid MNAN value %u\n", ctrl->max_namespaces);
885 ctrl->anacap = id->anacap;
886 ctrl->anatt = id->anatt;
887 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
888 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
890 ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
891 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
892 ctrl->max_namespaces * sizeof(__le32);
893 if (ana_log_size > max_transfer_size) {
894 dev_err(ctrl->device,
895 "ANA log page size (%zd) larger than MDTS (%zd).\n",
896 ana_log_size, max_transfer_size);
897 dev_err(ctrl->device, "disabling ANA support.\n");
900 if (ana_log_size > ctrl->ana_log_size) {
901 nvme_mpath_stop(ctrl);
902 nvme_mpath_uninit(ctrl);
903 ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL);
904 if (!ctrl->ana_log_buf)
907 ctrl->ana_log_size = ana_log_size;
908 error = nvme_read_ana_log(ctrl);
914 nvme_mpath_uninit(ctrl);
918 void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
920 kvfree(ctrl->ana_log_buf);
921 ctrl->ana_log_buf = NULL;
922 ctrl->ana_log_size = 0;