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 <trace/events/block.h>
11 static bool multipath = true;
12 module_param(multipath, bool, 0444);
13 MODULE_PARM_DESC(multipath,
14 "turn on native support for multiple controllers per subsystem");
16 void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
18 struct nvme_ns_head *h;
20 lockdep_assert_held(&subsys->lock);
21 list_for_each_entry(h, &subsys->nsheads, entry)
23 blk_mq_unfreeze_queue(h->disk->queue);
26 void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
28 struct nvme_ns_head *h;
30 lockdep_assert_held(&subsys->lock);
31 list_for_each_entry(h, &subsys->nsheads, entry)
33 blk_mq_freeze_queue_wait(h->disk->queue);
36 void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
38 struct nvme_ns_head *h;
40 lockdep_assert_held(&subsys->lock);
41 list_for_each_entry(h, &subsys->nsheads, entry)
43 blk_freeze_queue_start(h->disk->queue);
47 * If multipathing is enabled we need to always use the subsystem instance
48 * number for numbering our devices to avoid conflicts between subsystems that
49 * have multiple controllers and thus use the multipath-aware subsystem node
50 * and those that have a single controller and use the controller node
53 bool nvme_mpath_set_disk_name(struct nvme_ns *ns, char *disk_name, int *flags)
57 if (!ns->head->disk) {
58 sprintf(disk_name, "nvme%dn%d", ns->ctrl->subsys->instance,
62 sprintf(disk_name, "nvme%dc%dn%d", ns->ctrl->subsys->instance,
63 ns->ctrl->instance, ns->head->instance);
64 *flags = GENHD_FL_HIDDEN;
68 void nvme_failover_req(struct request *req)
70 struct nvme_ns *ns = req->q->queuedata;
71 u16 status = nvme_req(req)->status & 0x7ff;
75 nvme_mpath_clear_current_path(ns);
78 * If we got back an ANA error, we know the controller is alive but not
79 * ready to serve this namespace. Kick of a re-read of the ANA
80 * information page, and just try any other available path for now.
82 if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) {
83 set_bit(NVME_NS_ANA_PENDING, &ns->flags);
84 queue_work(nvme_wq, &ns->ctrl->ana_work);
87 spin_lock_irqsave(&ns->head->requeue_lock, flags);
88 for (bio = req->bio; bio; bio = bio->bi_next)
89 bio_set_dev(bio, ns->head->disk->part0);
90 blk_steal_bios(&ns->head->requeue_list, req);
91 spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
93 blk_mq_end_request(req, 0);
94 kblockd_schedule_work(&ns->head->requeue_work);
97 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
101 down_read(&ctrl->namespaces_rwsem);
102 list_for_each_entry(ns, &ctrl->namespaces, list) {
104 kblockd_schedule_work(&ns->head->requeue_work);
106 up_read(&ctrl->namespaces_rwsem);
109 static const char *nvme_ana_state_names[] = {
110 [0] = "invalid state",
111 [NVME_ANA_OPTIMIZED] = "optimized",
112 [NVME_ANA_NONOPTIMIZED] = "non-optimized",
113 [NVME_ANA_INACCESSIBLE] = "inaccessible",
114 [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss",
115 [NVME_ANA_CHANGE] = "change",
118 bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
120 struct nvme_ns_head *head = ns->head;
121 bool changed = false;
127 for_each_node(node) {
128 if (ns == rcu_access_pointer(head->current_path[node])) {
129 rcu_assign_pointer(head->current_path[node], NULL);
137 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
141 down_read(&ctrl->namespaces_rwsem);
142 list_for_each_entry(ns, &ctrl->namespaces, list) {
143 nvme_mpath_clear_current_path(ns);
144 kblockd_schedule_work(&ns->head->requeue_work);
146 up_read(&ctrl->namespaces_rwsem);
149 void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
151 struct nvme_ns_head *head = ns->head;
152 sector_t capacity = get_capacity(head->disk);
155 list_for_each_entry_rcu(ns, &head->list, siblings) {
156 if (capacity != get_capacity(ns->disk))
157 clear_bit(NVME_NS_READY, &ns->flags);
161 rcu_assign_pointer(head->current_path[node], NULL);
164 static bool nvme_path_is_disabled(struct nvme_ns *ns)
167 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
168 * still be able to complete assuming that the controller is connected.
169 * Otherwise it will fail immediately and return to the requeue list.
171 if (ns->ctrl->state != NVME_CTRL_LIVE &&
172 ns->ctrl->state != NVME_CTRL_DELETING)
174 if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
175 !test_bit(NVME_NS_READY, &ns->flags))
180 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
182 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
183 struct nvme_ns *found = NULL, *fallback = NULL, *ns;
185 list_for_each_entry_rcu(ns, &head->list, siblings) {
186 if (nvme_path_is_disabled(ns))
189 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
190 distance = node_distance(node, ns->ctrl->numa_node);
192 distance = LOCAL_DISTANCE;
194 switch (ns->ana_state) {
195 case NVME_ANA_OPTIMIZED:
196 if (distance < found_distance) {
197 found_distance = distance;
201 case NVME_ANA_NONOPTIMIZED:
202 if (distance < fallback_distance) {
203 fallback_distance = distance;
215 rcu_assign_pointer(head->current_path[node], found);
219 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
222 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
226 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
229 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
230 int node, struct nvme_ns *old)
232 struct nvme_ns *ns, *found = NULL;
234 if (list_is_singular(&head->list)) {
235 if (nvme_path_is_disabled(old))
240 for (ns = nvme_next_ns(head, old);
242 ns = nvme_next_ns(head, ns)) {
243 if (nvme_path_is_disabled(ns))
246 if (ns->ana_state == NVME_ANA_OPTIMIZED) {
250 if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
255 * The loop above skips the current path for round-robin semantics.
256 * Fall back to the current path if either:
257 * - no other optimized path found and current is optimized,
258 * - no other usable path found and current is usable.
260 if (!nvme_path_is_disabled(old) &&
261 (old->ana_state == NVME_ANA_OPTIMIZED ||
262 (!found && old->ana_state == NVME_ANA_NONOPTIMIZED)))
268 rcu_assign_pointer(head->current_path[node], found);
272 static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
274 return ns->ctrl->state == NVME_CTRL_LIVE &&
275 ns->ana_state == NVME_ANA_OPTIMIZED;
278 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
280 int node = numa_node_id();
283 ns = srcu_dereference(head->current_path[node], &head->srcu);
285 return __nvme_find_path(head, node);
287 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR)
288 return nvme_round_robin_path(head, node, ns);
289 if (unlikely(!nvme_path_is_optimized(ns)))
290 return __nvme_find_path(head, node);
294 static bool nvme_available_path(struct nvme_ns_head *head)
298 list_for_each_entry_rcu(ns, &head->list, siblings) {
299 if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags))
301 switch (ns->ctrl->state) {
303 case NVME_CTRL_RESETTING:
304 case NVME_CTRL_CONNECTING:
314 static blk_qc_t nvme_ns_head_submit_bio(struct bio *bio)
316 struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data;
317 struct device *dev = disk_to_dev(head->disk);
319 blk_qc_t ret = BLK_QC_T_NONE;
323 * The namespace might be going away and the bio might be moved to a
324 * different queue via blk_steal_bios(), so we need to use the bio_split
325 * pool from the original queue to allocate the bvecs from.
327 blk_queue_split(&bio);
329 srcu_idx = srcu_read_lock(&head->srcu);
330 ns = nvme_find_path(head);
332 bio_set_dev(bio, ns->disk->part0);
333 bio->bi_opf |= REQ_NVME_MPATH;
334 trace_block_bio_remap(bio, disk_devt(ns->head->disk),
335 bio->bi_iter.bi_sector);
336 ret = submit_bio_noacct(bio);
337 } else if (nvme_available_path(head)) {
338 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
340 spin_lock_irq(&head->requeue_lock);
341 bio_list_add(&head->requeue_list, bio);
342 spin_unlock_irq(&head->requeue_lock);
344 dev_warn_ratelimited(dev, "no available path - failing I/O\n");
346 bio->bi_status = BLK_STS_IOERR;
350 srcu_read_unlock(&head->srcu, srcu_idx);
354 static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
356 if (!nvme_tryget_ns_head(bdev->bd_disk->private_data))
361 static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
363 nvme_put_ns_head(disk->private_data);
366 #ifdef CONFIG_BLK_DEV_ZONED
367 static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector,
368 unsigned int nr_zones, report_zones_cb cb, void *data)
370 struct nvme_ns_head *head = disk->private_data;
372 int srcu_idx, ret = -EWOULDBLOCK;
374 srcu_idx = srcu_read_lock(&head->srcu);
375 ns = nvme_find_path(head);
377 ret = nvme_ns_report_zones(ns, sector, nr_zones, cb, data);
378 srcu_read_unlock(&head->srcu, srcu_idx);
382 #define nvme_ns_head_report_zones NULL
383 #endif /* CONFIG_BLK_DEV_ZONED */
385 const struct block_device_operations nvme_ns_head_ops = {
386 .owner = THIS_MODULE,
387 .submit_bio = nvme_ns_head_submit_bio,
388 .open = nvme_ns_head_open,
389 .release = nvme_ns_head_release,
390 .ioctl = nvme_ns_head_ioctl,
391 .getgeo = nvme_getgeo,
392 .report_zones = nvme_ns_head_report_zones,
393 .pr_ops = &nvme_pr_ops,
396 static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev)
398 return container_of(cdev, struct nvme_ns_head, cdev);
401 static int nvme_ns_head_chr_open(struct inode *inode, struct file *file)
403 if (!nvme_tryget_ns_head(cdev_to_ns_head(inode->i_cdev)))
408 static int nvme_ns_head_chr_release(struct inode *inode, struct file *file)
410 nvme_put_ns_head(cdev_to_ns_head(inode->i_cdev));
414 static const struct file_operations nvme_ns_head_chr_fops = {
415 .owner = THIS_MODULE,
416 .open = nvme_ns_head_chr_open,
417 .release = nvme_ns_head_chr_release,
418 .unlocked_ioctl = nvme_ns_head_chr_ioctl,
419 .compat_ioctl = compat_ptr_ioctl,
422 static int nvme_add_ns_head_cdev(struct nvme_ns_head *head)
426 head->cdev_device.parent = &head->subsys->dev;
427 ret = dev_set_name(&head->cdev_device, "ng%dn%d",
428 head->subsys->instance, head->instance);
431 ret = nvme_cdev_add(&head->cdev, &head->cdev_device,
432 &nvme_ns_head_chr_fops, THIS_MODULE);
436 static void nvme_requeue_work(struct work_struct *work)
438 struct nvme_ns_head *head =
439 container_of(work, struct nvme_ns_head, requeue_work);
440 struct bio *bio, *next;
442 spin_lock_irq(&head->requeue_lock);
443 next = bio_list_get(&head->requeue_list);
444 spin_unlock_irq(&head->requeue_lock);
446 while ((bio = next) != NULL) {
450 submit_bio_noacct(bio);
454 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
458 mutex_init(&head->lock);
459 bio_list_init(&head->requeue_list);
460 spin_lock_init(&head->requeue_lock);
461 INIT_WORK(&head->requeue_work, nvme_requeue_work);
464 * Add a multipath node if the subsystems supports multiple controllers.
465 * We also do this for private namespaces as the namespace sharing data could
466 * change after a rescan.
468 if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || !multipath)
471 head->disk = blk_alloc_disk(ctrl->numa_node);
474 head->disk->fops = &nvme_ns_head_ops;
475 head->disk->private_data = head;
476 sprintf(head->disk->disk_name, "nvme%dn%d",
477 ctrl->subsys->instance, head->instance);
479 blk_queue_flag_set(QUEUE_FLAG_NONROT, head->disk->queue);
480 blk_queue_flag_set(QUEUE_FLAG_NOWAIT, head->disk->queue);
482 /* set to a default value of 512 until the disk is validated */
483 blk_queue_logical_block_size(head->disk->queue, 512);
484 blk_set_stacking_limits(&head->disk->queue->limits);
486 /* we need to propagate up the VMC settings */
487 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
489 blk_queue_write_cache(head->disk->queue, vwc, vwc);
493 static void nvme_mpath_set_live(struct nvme_ns *ns)
495 struct nvme_ns_head *head = ns->head;
500 if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
501 device_add_disk(&head->subsys->dev, head->disk,
502 nvme_ns_id_attr_groups);
503 nvme_add_ns_head_cdev(head);
506 mutex_lock(&head->lock);
507 if (nvme_path_is_optimized(ns)) {
510 srcu_idx = srcu_read_lock(&head->srcu);
512 __nvme_find_path(head, node);
513 srcu_read_unlock(&head->srcu, srcu_idx);
515 mutex_unlock(&head->lock);
517 synchronize_srcu(&head->srcu);
518 kblockd_schedule_work(&head->requeue_work);
521 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
522 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
525 void *base = ctrl->ana_log_buf;
526 size_t offset = sizeof(struct nvme_ana_rsp_hdr);
529 lockdep_assert_held(&ctrl->ana_lock);
531 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
532 struct nvme_ana_group_desc *desc = base + offset;
534 size_t nsid_buf_size;
536 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
539 nr_nsids = le32_to_cpu(desc->nnsids);
540 nsid_buf_size = nr_nsids * sizeof(__le32);
542 if (WARN_ON_ONCE(desc->grpid == 0))
544 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
546 if (WARN_ON_ONCE(desc->state == 0))
548 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
551 offset += sizeof(*desc);
552 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
555 error = cb(ctrl, desc, data);
559 offset += nsid_buf_size;
565 static inline bool nvme_state_is_live(enum nvme_ana_state state)
567 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
570 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
573 ns->ana_grpid = le32_to_cpu(desc->grpid);
574 ns->ana_state = desc->state;
575 clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
577 if (nvme_state_is_live(ns->ana_state))
578 nvme_mpath_set_live(ns);
581 static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
582 struct nvme_ana_group_desc *desc, void *data)
584 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
585 unsigned *nr_change_groups = data;
588 dev_dbg(ctrl->device, "ANA group %d: %s.\n",
589 le32_to_cpu(desc->grpid),
590 nvme_ana_state_names[desc->state]);
592 if (desc->state == NVME_ANA_CHANGE)
593 (*nr_change_groups)++;
598 down_read(&ctrl->namespaces_rwsem);
599 list_for_each_entry(ns, &ctrl->namespaces, list) {
602 nsid = le32_to_cpu(desc->nsids[n]);
603 if (ns->head->ns_id < nsid)
605 if (ns->head->ns_id == nsid)
606 nvme_update_ns_ana_state(desc, ns);
609 if (ns->head->ns_id > nsid)
612 up_read(&ctrl->namespaces_rwsem);
616 static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
618 u32 nr_change_groups = 0;
621 mutex_lock(&ctrl->ana_lock);
622 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
623 ctrl->ana_log_buf, ctrl->ana_log_size, 0);
625 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
629 error = nvme_parse_ana_log(ctrl, &nr_change_groups,
630 nvme_update_ana_state);
635 * In theory we should have an ANATT timer per group as they might enter
636 * the change state at different times. But that is a lot of overhead
637 * just to protect against a target that keeps entering new changes
638 * states while never finishing previous ones. But we'll still
639 * eventually time out once all groups are in change state, so this
642 * We also double the ANATT value to provide some slack for transports
643 * or AEN processing overhead.
645 if (nr_change_groups)
646 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
648 del_timer_sync(&ctrl->anatt_timer);
650 mutex_unlock(&ctrl->ana_lock);
654 static void nvme_ana_work(struct work_struct *work)
656 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
658 if (ctrl->state != NVME_CTRL_LIVE)
661 nvme_read_ana_log(ctrl);
664 static void nvme_anatt_timeout(struct timer_list *t)
666 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
668 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
669 nvme_reset_ctrl(ctrl);
672 void nvme_mpath_stop(struct nvme_ctrl *ctrl)
674 if (!nvme_ctrl_use_ana(ctrl))
676 del_timer_sync(&ctrl->anatt_timer);
677 cancel_work_sync(&ctrl->ana_work);
680 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \
681 struct device_attribute subsys_attr_##_name = \
682 __ATTR(_name, _mode, _show, _store)
684 static const char *nvme_iopolicy_names[] = {
685 [NVME_IOPOLICY_NUMA] = "numa",
686 [NVME_IOPOLICY_RR] = "round-robin",
689 static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
690 struct device_attribute *attr, char *buf)
692 struct nvme_subsystem *subsys =
693 container_of(dev, struct nvme_subsystem, dev);
695 return sysfs_emit(buf, "%s\n",
696 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
699 static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
700 struct device_attribute *attr, const char *buf, size_t count)
702 struct nvme_subsystem *subsys =
703 container_of(dev, struct nvme_subsystem, dev);
706 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
707 if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
708 WRITE_ONCE(subsys->iopolicy, i);
715 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
716 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
718 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
721 return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
723 DEVICE_ATTR_RO(ana_grpid);
725 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
728 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
730 return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
732 DEVICE_ATTR_RO(ana_state);
734 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
735 struct nvme_ana_group_desc *desc, void *data)
737 struct nvme_ana_group_desc *dst = data;
739 if (desc->grpid != dst->grpid)
743 return -ENXIO; /* just break out of the loop */
746 void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
748 if (nvme_ctrl_use_ana(ns->ctrl)) {
749 struct nvme_ana_group_desc desc = {
750 .grpid = id->anagrpid,
754 mutex_lock(&ns->ctrl->ana_lock);
755 ns->ana_grpid = le32_to_cpu(id->anagrpid);
756 nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
757 mutex_unlock(&ns->ctrl->ana_lock);
759 /* found the group desc: update */
760 nvme_update_ns_ana_state(&desc, ns);
762 /* group desc not found: trigger a re-read */
763 set_bit(NVME_NS_ANA_PENDING, &ns->flags);
764 queue_work(nvme_wq, &ns->ctrl->ana_work);
767 ns->ana_state = NVME_ANA_OPTIMIZED;
768 nvme_mpath_set_live(ns);
771 if (blk_queue_stable_writes(ns->queue) && ns->head->disk)
772 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES,
773 ns->head->disk->queue);
774 #ifdef CONFIG_BLK_DEV_ZONED
775 if (blk_queue_is_zoned(ns->queue) && ns->head->disk)
776 ns->head->disk->queue->nr_zones = ns->queue->nr_zones;
780 void nvme_mpath_shutdown_disk(struct nvme_ns_head *head)
784 kblockd_schedule_work(&head->requeue_work);
785 if (test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
786 nvme_cdev_del(&head->cdev, &head->cdev_device);
787 del_gendisk(head->disk);
791 void nvme_mpath_remove_disk(struct nvme_ns_head *head)
795 blk_set_queue_dying(head->disk->queue);
796 /* make sure all pending bios are cleaned up */
797 kblockd_schedule_work(&head->requeue_work);
798 flush_work(&head->requeue_work);
799 blk_cleanup_disk(head->disk);
802 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
804 mutex_init(&ctrl->ana_lock);
805 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
806 INIT_WORK(&ctrl->ana_work, nvme_ana_work);
809 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
811 size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT;
815 /* check if multipath is enabled and we have the capability */
816 if (!multipath || !ctrl->subsys ||
817 !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA))
820 if (!ctrl->max_namespaces ||
821 ctrl->max_namespaces > le32_to_cpu(id->nn)) {
822 dev_err(ctrl->device,
823 "Invalid MNAN value %u\n", ctrl->max_namespaces);
827 ctrl->anacap = id->anacap;
828 ctrl->anatt = id->anatt;
829 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
830 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
832 ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
833 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
834 ctrl->max_namespaces * sizeof(__le32);
835 if (ana_log_size > max_transfer_size) {
836 dev_err(ctrl->device,
837 "ANA log page size (%zd) larger than MDTS (%zd).\n",
838 ana_log_size, max_transfer_size);
839 dev_err(ctrl->device, "disabling ANA support.\n");
842 if (ana_log_size > ctrl->ana_log_size) {
843 nvme_mpath_stop(ctrl);
844 kfree(ctrl->ana_log_buf);
845 ctrl->ana_log_buf = kmalloc(ana_log_size, GFP_KERNEL);
846 if (!ctrl->ana_log_buf)
849 ctrl->ana_log_size = ana_log_size;
850 error = nvme_read_ana_log(ctrl);
856 nvme_mpath_uninit(ctrl);
860 void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
862 kfree(ctrl->ana_log_buf);
863 ctrl->ana_log_buf = NULL;