xref: /openbmc/linux/drivers/nvme/host/multipath.c (revision 0352f880)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2017-2018 Christoph Hellwig.
4  */
5 
6 #include <linux/backing-dev.h>
7 #include <linux/moduleparam.h>
8 #include <linux/vmalloc.h>
9 #include <trace/events/block.h>
10 #include "nvme.h"
11 
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");
16 
17 static const char *nvme_iopolicy_names[] = {
18 	[NVME_IOPOLICY_NUMA]	= "numa",
19 	[NVME_IOPOLICY_RR]	= "round-robin",
20 };
21 
22 static int iopolicy = NVME_IOPOLICY_NUMA;
23 
24 static int nvme_set_iopolicy(const char *val, const struct kernel_param *kp)
25 {
26 	if (!val)
27 		return -EINVAL;
28 	if (!strncmp(val, "numa", 4))
29 		iopolicy = NVME_IOPOLICY_NUMA;
30 	else if (!strncmp(val, "round-robin", 11))
31 		iopolicy = NVME_IOPOLICY_RR;
32 	else
33 		return -EINVAL;
34 
35 	return 0;
36 }
37 
38 static int nvme_get_iopolicy(char *buf, const struct kernel_param *kp)
39 {
40 	return sprintf(buf, "%s\n", nvme_iopolicy_names[iopolicy]);
41 }
42 
43 module_param_call(iopolicy, nvme_set_iopolicy, nvme_get_iopolicy,
44 	&iopolicy, 0644);
45 MODULE_PARM_DESC(iopolicy,
46 	"Default multipath I/O policy; 'numa' (default) or 'round-robin'");
47 
48 void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys)
49 {
50 	subsys->iopolicy = iopolicy;
51 }
52 
53 void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
54 {
55 	struct nvme_ns_head *h;
56 
57 	lockdep_assert_held(&subsys->lock);
58 	list_for_each_entry(h, &subsys->nsheads, entry)
59 		if (h->disk)
60 			blk_mq_unfreeze_queue(h->disk->queue);
61 }
62 
63 void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
64 {
65 	struct nvme_ns_head *h;
66 
67 	lockdep_assert_held(&subsys->lock);
68 	list_for_each_entry(h, &subsys->nsheads, entry)
69 		if (h->disk)
70 			blk_mq_freeze_queue_wait(h->disk->queue);
71 }
72 
73 void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
74 {
75 	struct nvme_ns_head *h;
76 
77 	lockdep_assert_held(&subsys->lock);
78 	list_for_each_entry(h, &subsys->nsheads, entry)
79 		if (h->disk)
80 			blk_freeze_queue_start(h->disk->queue);
81 }
82 
83 void nvme_failover_req(struct request *req)
84 {
85 	struct nvme_ns *ns = req->q->queuedata;
86 	u16 status = nvme_req(req)->status & 0x7ff;
87 	unsigned long flags;
88 	struct bio *bio;
89 
90 	nvme_mpath_clear_current_path(ns);
91 
92 	/*
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.
96 	 */
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);
100 	}
101 
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;
108 		}
109 	}
110 	blk_steal_bios(&ns->head->requeue_list, req);
111 	spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
112 
113 	blk_mq_end_request(req, 0);
114 	kblockd_schedule_work(&ns->head->requeue_work);
115 }
116 
117 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
118 {
119 	struct nvme_ns *ns;
120 
121 	down_read(&ctrl->namespaces_rwsem);
122 	list_for_each_entry(ns, &ctrl->namespaces, list) {
123 		if (!ns->head->disk)
124 			continue;
125 		kblockd_schedule_work(&ns->head->requeue_work);
126 		if (ctrl->state == NVME_CTRL_LIVE)
127 			disk_uevent(ns->head->disk, KOBJ_CHANGE);
128 	}
129 	up_read(&ctrl->namespaces_rwsem);
130 }
131 
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",
139 };
140 
141 bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
142 {
143 	struct nvme_ns_head *head = ns->head;
144 	bool changed = false;
145 	int node;
146 
147 	if (!head)
148 		goto out;
149 
150 	for_each_node(node) {
151 		if (ns == rcu_access_pointer(head->current_path[node])) {
152 			rcu_assign_pointer(head->current_path[node], NULL);
153 			changed = true;
154 		}
155 	}
156 out:
157 	return changed;
158 }
159 
160 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
161 {
162 	struct nvme_ns *ns;
163 
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);
168 	}
169 	up_read(&ctrl->namespaces_rwsem);
170 }
171 
172 void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
173 {
174 	struct nvme_ns_head *head = ns->head;
175 	sector_t capacity = get_capacity(head->disk);
176 	int node;
177 
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);
181 	}
182 
183 	for_each_node(node)
184 		rcu_assign_pointer(head->current_path[node], NULL);
185 	kblockd_schedule_work(&head->requeue_work);
186 }
187 
188 static bool nvme_path_is_disabled(struct nvme_ns *ns)
189 {
190 	/*
191 	 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
192 	 * still be able to complete assuming that the controller is connected.
193 	 * Otherwise it will fail immediately and return to the requeue list.
194 	 */
195 	if (ns->ctrl->state != NVME_CTRL_LIVE &&
196 	    ns->ctrl->state != NVME_CTRL_DELETING)
197 		return true;
198 	if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
199 	    !test_bit(NVME_NS_READY, &ns->flags))
200 		return true;
201 	return false;
202 }
203 
204 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
205 {
206 	int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
207 	struct nvme_ns *found = NULL, *fallback = NULL, *ns;
208 
209 	list_for_each_entry_rcu(ns, &head->list, siblings) {
210 		if (nvme_path_is_disabled(ns))
211 			continue;
212 
213 		if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
214 			distance = node_distance(node, ns->ctrl->numa_node);
215 		else
216 			distance = LOCAL_DISTANCE;
217 
218 		switch (ns->ana_state) {
219 		case NVME_ANA_OPTIMIZED:
220 			if (distance < found_distance) {
221 				found_distance = distance;
222 				found = ns;
223 			}
224 			break;
225 		case NVME_ANA_NONOPTIMIZED:
226 			if (distance < fallback_distance) {
227 				fallback_distance = distance;
228 				fallback = ns;
229 			}
230 			break;
231 		default:
232 			break;
233 		}
234 	}
235 
236 	if (!found)
237 		found = fallback;
238 	if (found)
239 		rcu_assign_pointer(head->current_path[node], found);
240 	return found;
241 }
242 
243 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
244 		struct nvme_ns *ns)
245 {
246 	ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
247 			siblings);
248 	if (ns)
249 		return ns;
250 	return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
251 }
252 
253 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
254 		int node, struct nvme_ns *old)
255 {
256 	struct nvme_ns *ns, *found = NULL;
257 
258 	if (list_is_singular(&head->list)) {
259 		if (nvme_path_is_disabled(old))
260 			return NULL;
261 		return old;
262 	}
263 
264 	for (ns = nvme_next_ns(head, old);
265 	     ns && ns != old;
266 	     ns = nvme_next_ns(head, ns)) {
267 		if (nvme_path_is_disabled(ns))
268 			continue;
269 
270 		if (ns->ana_state == NVME_ANA_OPTIMIZED) {
271 			found = ns;
272 			goto out;
273 		}
274 		if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
275 			found = ns;
276 	}
277 
278 	/*
279 	 * The loop above skips the current path for round-robin semantics.
280 	 * Fall back to the current path if either:
281 	 *  - no other optimized path found and current is optimized,
282 	 *  - no other usable path found and current is usable.
283 	 */
284 	if (!nvme_path_is_disabled(old) &&
285 	    (old->ana_state == NVME_ANA_OPTIMIZED ||
286 	     (!found && old->ana_state == NVME_ANA_NONOPTIMIZED)))
287 		return old;
288 
289 	if (!found)
290 		return NULL;
291 out:
292 	rcu_assign_pointer(head->current_path[node], found);
293 	return found;
294 }
295 
296 static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
297 {
298 	return ns->ctrl->state == NVME_CTRL_LIVE &&
299 		ns->ana_state == NVME_ANA_OPTIMIZED;
300 }
301 
302 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
303 {
304 	int node = numa_node_id();
305 	struct nvme_ns *ns;
306 
307 	ns = srcu_dereference(head->current_path[node], &head->srcu);
308 	if (unlikely(!ns))
309 		return __nvme_find_path(head, node);
310 
311 	if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR)
312 		return nvme_round_robin_path(head, node, ns);
313 	if (unlikely(!nvme_path_is_optimized(ns)))
314 		return __nvme_find_path(head, node);
315 	return ns;
316 }
317 
318 static bool nvme_available_path(struct nvme_ns_head *head)
319 {
320 	struct nvme_ns *ns;
321 
322 	list_for_each_entry_rcu(ns, &head->list, siblings) {
323 		if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags))
324 			continue;
325 		switch (ns->ctrl->state) {
326 		case NVME_CTRL_LIVE:
327 		case NVME_CTRL_RESETTING:
328 		case NVME_CTRL_CONNECTING:
329 			/* fallthru */
330 			return true;
331 		default:
332 			break;
333 		}
334 	}
335 	return false;
336 }
337 
338 static void nvme_ns_head_submit_bio(struct bio *bio)
339 {
340 	struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data;
341 	struct device *dev = disk_to_dev(head->disk);
342 	struct nvme_ns *ns;
343 	int srcu_idx;
344 
345 	/*
346 	 * The namespace might be going away and the bio might be moved to a
347 	 * different queue via blk_steal_bios(), so we need to use the bio_split
348 	 * pool from the original queue to allocate the bvecs from.
349 	 */
350 	bio = bio_split_to_limits(bio);
351 
352 	srcu_idx = srcu_read_lock(&head->srcu);
353 	ns = nvme_find_path(head);
354 	if (likely(ns)) {
355 		bio_set_dev(bio, ns->disk->part0);
356 		bio->bi_opf |= REQ_NVME_MPATH;
357 		trace_block_bio_remap(bio, disk_devt(ns->head->disk),
358 				      bio->bi_iter.bi_sector);
359 		submit_bio_noacct(bio);
360 	} else if (nvme_available_path(head)) {
361 		dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
362 
363 		spin_lock_irq(&head->requeue_lock);
364 		bio_list_add(&head->requeue_list, bio);
365 		spin_unlock_irq(&head->requeue_lock);
366 	} else {
367 		dev_warn_ratelimited(dev, "no available path - failing I/O\n");
368 
369 		bio_io_error(bio);
370 	}
371 
372 	srcu_read_unlock(&head->srcu, srcu_idx);
373 }
374 
375 static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
376 {
377 	if (!nvme_tryget_ns_head(bdev->bd_disk->private_data))
378 		return -ENXIO;
379 	return 0;
380 }
381 
382 static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
383 {
384 	nvme_put_ns_head(disk->private_data);
385 }
386 
387 #ifdef CONFIG_BLK_DEV_ZONED
388 static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector,
389 		unsigned int nr_zones, report_zones_cb cb, void *data)
390 {
391 	struct nvme_ns_head *head = disk->private_data;
392 	struct nvme_ns *ns;
393 	int srcu_idx, ret = -EWOULDBLOCK;
394 
395 	srcu_idx = srcu_read_lock(&head->srcu);
396 	ns = nvme_find_path(head);
397 	if (ns)
398 		ret = nvme_ns_report_zones(ns, sector, nr_zones, cb, data);
399 	srcu_read_unlock(&head->srcu, srcu_idx);
400 	return ret;
401 }
402 #else
403 #define nvme_ns_head_report_zones	NULL
404 #endif /* CONFIG_BLK_DEV_ZONED */
405 
406 const struct block_device_operations nvme_ns_head_ops = {
407 	.owner		= THIS_MODULE,
408 	.submit_bio	= nvme_ns_head_submit_bio,
409 	.open		= nvme_ns_head_open,
410 	.release	= nvme_ns_head_release,
411 	.ioctl		= nvme_ns_head_ioctl,
412 	.compat_ioctl	= blkdev_compat_ptr_ioctl,
413 	.getgeo		= nvme_getgeo,
414 	.report_zones	= nvme_ns_head_report_zones,
415 	.pr_ops		= &nvme_pr_ops,
416 };
417 
418 static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev)
419 {
420 	return container_of(cdev, struct nvme_ns_head, cdev);
421 }
422 
423 static int nvme_ns_head_chr_open(struct inode *inode, struct file *file)
424 {
425 	if (!nvme_tryget_ns_head(cdev_to_ns_head(inode->i_cdev)))
426 		return -ENXIO;
427 	return 0;
428 }
429 
430 static int nvme_ns_head_chr_release(struct inode *inode, struct file *file)
431 {
432 	nvme_put_ns_head(cdev_to_ns_head(inode->i_cdev));
433 	return 0;
434 }
435 
436 static const struct file_operations nvme_ns_head_chr_fops = {
437 	.owner		= THIS_MODULE,
438 	.open		= nvme_ns_head_chr_open,
439 	.release	= nvme_ns_head_chr_release,
440 	.unlocked_ioctl	= nvme_ns_head_chr_ioctl,
441 	.compat_ioctl	= compat_ptr_ioctl,
442 	.uring_cmd	= nvme_ns_head_chr_uring_cmd,
443 	.uring_cmd_iopoll = nvme_ns_head_chr_uring_cmd_iopoll,
444 };
445 
446 static int nvme_add_ns_head_cdev(struct nvme_ns_head *head)
447 {
448 	int ret;
449 
450 	head->cdev_device.parent = &head->subsys->dev;
451 	ret = dev_set_name(&head->cdev_device, "ng%dn%d",
452 			   head->subsys->instance, head->instance);
453 	if (ret)
454 		return ret;
455 	ret = nvme_cdev_add(&head->cdev, &head->cdev_device,
456 			    &nvme_ns_head_chr_fops, THIS_MODULE);
457 	return ret;
458 }
459 
460 static void nvme_requeue_work(struct work_struct *work)
461 {
462 	struct nvme_ns_head *head =
463 		container_of(work, struct nvme_ns_head, requeue_work);
464 	struct bio *bio, *next;
465 
466 	spin_lock_irq(&head->requeue_lock);
467 	next = bio_list_get(&head->requeue_list);
468 	spin_unlock_irq(&head->requeue_lock);
469 
470 	while ((bio = next) != NULL) {
471 		next = bio->bi_next;
472 		bio->bi_next = NULL;
473 
474 		submit_bio_noacct(bio);
475 	}
476 }
477 
478 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
479 {
480 	bool vwc = false;
481 
482 	mutex_init(&head->lock);
483 	bio_list_init(&head->requeue_list);
484 	spin_lock_init(&head->requeue_lock);
485 	INIT_WORK(&head->requeue_work, nvme_requeue_work);
486 
487 	/*
488 	 * Add a multipath node if the subsystems supports multiple controllers.
489 	 * We also do this for private namespaces as the namespace sharing flag
490 	 * could change after a rescan.
491 	 */
492 	if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) ||
493 	    !nvme_is_unique_nsid(ctrl, head) || !multipath)
494 		return 0;
495 
496 	head->disk = blk_alloc_disk(ctrl->numa_node);
497 	if (!head->disk)
498 		return -ENOMEM;
499 	head->disk->fops = &nvme_ns_head_ops;
500 	head->disk->private_data = head;
501 	sprintf(head->disk->disk_name, "nvme%dn%d",
502 			ctrl->subsys->instance, head->instance);
503 
504 	blk_queue_flag_set(QUEUE_FLAG_NONROT, head->disk->queue);
505 	blk_queue_flag_set(QUEUE_FLAG_NOWAIT, head->disk->queue);
506 	/*
507 	 * This assumes all controllers that refer to a namespace either
508 	 * support poll queues or not.  That is not a strict guarantee,
509 	 * but if the assumption is wrong the effect is only suboptimal
510 	 * performance but not correctness problem.
511 	 */
512 	if (ctrl->tagset->nr_maps > HCTX_TYPE_POLL &&
513 	    ctrl->tagset->map[HCTX_TYPE_POLL].nr_queues)
514 		blk_queue_flag_set(QUEUE_FLAG_POLL, head->disk->queue);
515 
516 	/* set to a default value of 512 until the disk is validated */
517 	blk_queue_logical_block_size(head->disk->queue, 512);
518 	blk_set_stacking_limits(&head->disk->queue->limits);
519 	blk_queue_dma_alignment(head->disk->queue, 3);
520 
521 	/* we need to propagate up the VMC settings */
522 	if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
523 		vwc = true;
524 	blk_queue_write_cache(head->disk->queue, vwc, vwc);
525 	return 0;
526 }
527 
528 static void nvme_mpath_set_live(struct nvme_ns *ns)
529 {
530 	struct nvme_ns_head *head = ns->head;
531 	int rc;
532 
533 	if (!head->disk)
534 		return;
535 
536 	/*
537 	 * test_and_set_bit() is used because it is protecting against two nvme
538 	 * paths simultaneously calling device_add_disk() on the same namespace
539 	 * head.
540 	 */
541 	if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
542 		rc = device_add_disk(&head->subsys->dev, head->disk,
543 				     nvme_ns_id_attr_groups);
544 		if (rc) {
545 			clear_bit(NVME_NSHEAD_DISK_LIVE, &ns->flags);
546 			return;
547 		}
548 		nvme_add_ns_head_cdev(head);
549 	}
550 
551 	mutex_lock(&head->lock);
552 	if (nvme_path_is_optimized(ns)) {
553 		int node, srcu_idx;
554 
555 		srcu_idx = srcu_read_lock(&head->srcu);
556 		for_each_node(node)
557 			__nvme_find_path(head, node);
558 		srcu_read_unlock(&head->srcu, srcu_idx);
559 	}
560 	mutex_unlock(&head->lock);
561 
562 	synchronize_srcu(&head->srcu);
563 	kblockd_schedule_work(&head->requeue_work);
564 }
565 
566 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
567 		int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
568 			void *))
569 {
570 	void *base = ctrl->ana_log_buf;
571 	size_t offset = sizeof(struct nvme_ana_rsp_hdr);
572 	int error, i;
573 
574 	lockdep_assert_held(&ctrl->ana_lock);
575 
576 	for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
577 		struct nvme_ana_group_desc *desc = base + offset;
578 		u32 nr_nsids;
579 		size_t nsid_buf_size;
580 
581 		if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
582 			return -EINVAL;
583 
584 		nr_nsids = le32_to_cpu(desc->nnsids);
585 		nsid_buf_size = flex_array_size(desc, nsids, nr_nsids);
586 
587 		if (WARN_ON_ONCE(desc->grpid == 0))
588 			return -EINVAL;
589 		if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
590 			return -EINVAL;
591 		if (WARN_ON_ONCE(desc->state == 0))
592 			return -EINVAL;
593 		if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
594 			return -EINVAL;
595 
596 		offset += sizeof(*desc);
597 		if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
598 			return -EINVAL;
599 
600 		error = cb(ctrl, desc, data);
601 		if (error)
602 			return error;
603 
604 		offset += nsid_buf_size;
605 	}
606 
607 	return 0;
608 }
609 
610 static inline bool nvme_state_is_live(enum nvme_ana_state state)
611 {
612 	return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
613 }
614 
615 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
616 		struct nvme_ns *ns)
617 {
618 	ns->ana_grpid = le32_to_cpu(desc->grpid);
619 	ns->ana_state = desc->state;
620 	clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
621 	/*
622 	 * nvme_mpath_set_live() will trigger I/O to the multipath path device
623 	 * and in turn to this path device.  However we cannot accept this I/O
624 	 * if the controller is not live.  This may deadlock if called from
625 	 * nvme_mpath_init_identify() and the ctrl will never complete
626 	 * initialization, preventing I/O from completing.  For this case we
627 	 * will reprocess the ANA log page in nvme_mpath_update() once the
628 	 * controller is ready.
629 	 */
630 	if (nvme_state_is_live(ns->ana_state) &&
631 	    ns->ctrl->state == NVME_CTRL_LIVE)
632 		nvme_mpath_set_live(ns);
633 }
634 
635 static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
636 		struct nvme_ana_group_desc *desc, void *data)
637 {
638 	u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
639 	unsigned *nr_change_groups = data;
640 	struct nvme_ns *ns;
641 
642 	dev_dbg(ctrl->device, "ANA group %d: %s.\n",
643 			le32_to_cpu(desc->grpid),
644 			nvme_ana_state_names[desc->state]);
645 
646 	if (desc->state == NVME_ANA_CHANGE)
647 		(*nr_change_groups)++;
648 
649 	if (!nr_nsids)
650 		return 0;
651 
652 	down_read(&ctrl->namespaces_rwsem);
653 	list_for_each_entry(ns, &ctrl->namespaces, list) {
654 		unsigned nsid;
655 again:
656 		nsid = le32_to_cpu(desc->nsids[n]);
657 		if (ns->head->ns_id < nsid)
658 			continue;
659 		if (ns->head->ns_id == nsid)
660 			nvme_update_ns_ana_state(desc, ns);
661 		if (++n == nr_nsids)
662 			break;
663 		if (ns->head->ns_id > nsid)
664 			goto again;
665 	}
666 	up_read(&ctrl->namespaces_rwsem);
667 	return 0;
668 }
669 
670 static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
671 {
672 	u32 nr_change_groups = 0;
673 	int error;
674 
675 	mutex_lock(&ctrl->ana_lock);
676 	error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
677 			ctrl->ana_log_buf, ctrl->ana_log_size, 0);
678 	if (error) {
679 		dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
680 		goto out_unlock;
681 	}
682 
683 	error = nvme_parse_ana_log(ctrl, &nr_change_groups,
684 			nvme_update_ana_state);
685 	if (error)
686 		goto out_unlock;
687 
688 	/*
689 	 * In theory we should have an ANATT timer per group as they might enter
690 	 * the change state at different times.  But that is a lot of overhead
691 	 * just to protect against a target that keeps entering new changes
692 	 * states while never finishing previous ones.  But we'll still
693 	 * eventually time out once all groups are in change state, so this
694 	 * isn't a big deal.
695 	 *
696 	 * We also double the ANATT value to provide some slack for transports
697 	 * or AEN processing overhead.
698 	 */
699 	if (nr_change_groups)
700 		mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
701 	else
702 		del_timer_sync(&ctrl->anatt_timer);
703 out_unlock:
704 	mutex_unlock(&ctrl->ana_lock);
705 	return error;
706 }
707 
708 static void nvme_ana_work(struct work_struct *work)
709 {
710 	struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
711 
712 	if (ctrl->state != NVME_CTRL_LIVE)
713 		return;
714 
715 	nvme_read_ana_log(ctrl);
716 }
717 
718 void nvme_mpath_update(struct nvme_ctrl *ctrl)
719 {
720 	u32 nr_change_groups = 0;
721 
722 	if (!ctrl->ana_log_buf)
723 		return;
724 
725 	mutex_lock(&ctrl->ana_lock);
726 	nvme_parse_ana_log(ctrl, &nr_change_groups, nvme_update_ana_state);
727 	mutex_unlock(&ctrl->ana_lock);
728 }
729 
730 static void nvme_anatt_timeout(struct timer_list *t)
731 {
732 	struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
733 
734 	dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
735 	nvme_reset_ctrl(ctrl);
736 }
737 
738 void nvme_mpath_stop(struct nvme_ctrl *ctrl)
739 {
740 	if (!nvme_ctrl_use_ana(ctrl))
741 		return;
742 	del_timer_sync(&ctrl->anatt_timer);
743 	cancel_work_sync(&ctrl->ana_work);
744 }
745 
746 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store)  \
747 	struct device_attribute subsys_attr_##_name =	\
748 		__ATTR(_name, _mode, _show, _store)
749 
750 static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
751 		struct device_attribute *attr, char *buf)
752 {
753 	struct nvme_subsystem *subsys =
754 		container_of(dev, struct nvme_subsystem, dev);
755 
756 	return sysfs_emit(buf, "%s\n",
757 			  nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
758 }
759 
760 static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
761 		struct device_attribute *attr, const char *buf, size_t count)
762 {
763 	struct nvme_subsystem *subsys =
764 		container_of(dev, struct nvme_subsystem, dev);
765 	int i;
766 
767 	for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
768 		if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
769 			WRITE_ONCE(subsys->iopolicy, i);
770 			return count;
771 		}
772 	}
773 
774 	return -EINVAL;
775 }
776 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
777 		      nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
778 
779 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
780 		char *buf)
781 {
782 	return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
783 }
784 DEVICE_ATTR_RO(ana_grpid);
785 
786 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
787 		char *buf)
788 {
789 	struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
790 
791 	return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
792 }
793 DEVICE_ATTR_RO(ana_state);
794 
795 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
796 		struct nvme_ana_group_desc *desc, void *data)
797 {
798 	struct nvme_ana_group_desc *dst = data;
799 
800 	if (desc->grpid != dst->grpid)
801 		return 0;
802 
803 	*dst = *desc;
804 	return -ENXIO; /* just break out of the loop */
805 }
806 
807 void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid)
808 {
809 	if (nvme_ctrl_use_ana(ns->ctrl)) {
810 		struct nvme_ana_group_desc desc = {
811 			.grpid = anagrpid,
812 			.state = 0,
813 		};
814 
815 		mutex_lock(&ns->ctrl->ana_lock);
816 		ns->ana_grpid = le32_to_cpu(anagrpid);
817 		nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
818 		mutex_unlock(&ns->ctrl->ana_lock);
819 		if (desc.state) {
820 			/* found the group desc: update */
821 			nvme_update_ns_ana_state(&desc, ns);
822 		} else {
823 			/* group desc not found: trigger a re-read */
824 			set_bit(NVME_NS_ANA_PENDING, &ns->flags);
825 			queue_work(nvme_wq, &ns->ctrl->ana_work);
826 		}
827 	} else {
828 		ns->ana_state = NVME_ANA_OPTIMIZED;
829 		nvme_mpath_set_live(ns);
830 	}
831 
832 	if (blk_queue_stable_writes(ns->queue) && ns->head->disk)
833 		blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES,
834 				   ns->head->disk->queue);
835 #ifdef CONFIG_BLK_DEV_ZONED
836 	if (blk_queue_is_zoned(ns->queue) && ns->head->disk)
837 		ns->head->disk->nr_zones = ns->disk->nr_zones;
838 #endif
839 }
840 
841 void nvme_mpath_shutdown_disk(struct nvme_ns_head *head)
842 {
843 	if (!head->disk)
844 		return;
845 	kblockd_schedule_work(&head->requeue_work);
846 	if (test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
847 		nvme_cdev_del(&head->cdev, &head->cdev_device);
848 		del_gendisk(head->disk);
849 	}
850 }
851 
852 void nvme_mpath_remove_disk(struct nvme_ns_head *head)
853 {
854 	if (!head->disk)
855 		return;
856 	blk_mark_disk_dead(head->disk);
857 	/* make sure all pending bios are cleaned up */
858 	kblockd_schedule_work(&head->requeue_work);
859 	flush_work(&head->requeue_work);
860 	put_disk(head->disk);
861 }
862 
863 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
864 {
865 	mutex_init(&ctrl->ana_lock);
866 	timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
867 	INIT_WORK(&ctrl->ana_work, nvme_ana_work);
868 }
869 
870 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
871 {
872 	size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT;
873 	size_t ana_log_size;
874 	int error = 0;
875 
876 	/* check if multipath is enabled and we have the capability */
877 	if (!multipath || !ctrl->subsys ||
878 	    !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA))
879 		return 0;
880 
881 	if (!ctrl->max_namespaces ||
882 	    ctrl->max_namespaces > le32_to_cpu(id->nn)) {
883 		dev_err(ctrl->device,
884 			"Invalid MNAN value %u\n", ctrl->max_namespaces);
885 		return -EINVAL;
886 	}
887 
888 	ctrl->anacap = id->anacap;
889 	ctrl->anatt = id->anatt;
890 	ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
891 	ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
892 
893 	ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
894 		ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
895 		ctrl->max_namespaces * sizeof(__le32);
896 	if (ana_log_size > max_transfer_size) {
897 		dev_err(ctrl->device,
898 			"ANA log page size (%zd) larger than MDTS (%zd).\n",
899 			ana_log_size, max_transfer_size);
900 		dev_err(ctrl->device, "disabling ANA support.\n");
901 		goto out_uninit;
902 	}
903 	if (ana_log_size > ctrl->ana_log_size) {
904 		nvme_mpath_stop(ctrl);
905 		nvme_mpath_uninit(ctrl);
906 		ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL);
907 		if (!ctrl->ana_log_buf)
908 			return -ENOMEM;
909 	}
910 	ctrl->ana_log_size = ana_log_size;
911 	error = nvme_read_ana_log(ctrl);
912 	if (error)
913 		goto out_uninit;
914 	return 0;
915 
916 out_uninit:
917 	nvme_mpath_uninit(ctrl);
918 	return error;
919 }
920 
921 void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
922 {
923 	kvfree(ctrl->ana_log_buf);
924 	ctrl->ana_log_buf = NULL;
925 	ctrl->ana_log_size = 0;
926 }
927