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