• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
nvme_mpath_unfreeze(struct nvme_subsystem * subsys)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 
nvme_mpath_wait_freeze(struct nvme_subsystem * subsys)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 
nvme_mpath_start_freeze(struct nvme_subsystem * subsys)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  */
nvme_set_disk_name(char * disk_name,struct nvme_ns * ns,struct nvme_ctrl * ctrl,int * flags)53 void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
54 			struct nvme_ctrl *ctrl, int *flags)
55 {
56 	if (!multipath) {
57 		sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
58 	} else if (ns->head->disk) {
59 		sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
60 				ctrl->instance, ns->head->instance);
61 		*flags = GENHD_FL_HIDDEN;
62 	} else {
63 		sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
64 				ns->head->instance);
65 	}
66 }
67 
nvme_failover_req(struct request * req)68 bool nvme_failover_req(struct request *req)
69 {
70 	struct nvme_ns *ns = req->q->queuedata;
71 	u16 status = nvme_req(req)->status;
72 	unsigned long flags;
73 
74 	switch (status & 0x7ff) {
75 	case NVME_SC_ANA_TRANSITION:
76 	case NVME_SC_ANA_INACCESSIBLE:
77 	case NVME_SC_ANA_PERSISTENT_LOSS:
78 		/*
79 		 * If we got back an ANA error we know the controller is alive,
80 		 * but not ready to serve this namespaces.  The spec suggests
81 		 * we should update our general state here, but due to the fact
82 		 * that the admin and I/O queues are not serialized that is
83 		 * fundamentally racy.  So instead just clear the current path,
84 		 * mark the the path as pending and kick of a re-read of the ANA
85 		 * log page ASAP.
86 		 */
87 		nvme_mpath_clear_current_path(ns);
88 		if (ns->ctrl->ana_log_buf) {
89 			set_bit(NVME_NS_ANA_PENDING, &ns->flags);
90 			queue_work(nvme_wq, &ns->ctrl->ana_work);
91 		}
92 		break;
93 	case NVME_SC_HOST_PATH_ERROR:
94 	case NVME_SC_HOST_ABORTED_CMD:
95 		/*
96 		 * Temporary transport disruption in talking to the controller.
97 		 * Try to send on a new path.
98 		 */
99 		nvme_mpath_clear_current_path(ns);
100 		break;
101 	default:
102 		/* This was a non-ANA error so follow the normal error path. */
103 		return false;
104 	}
105 
106 	spin_lock_irqsave(&ns->head->requeue_lock, flags);
107 	blk_steal_bios(&ns->head->requeue_list, req);
108 	spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
109 	blk_mq_end_request(req, 0);
110 
111 	kblockd_schedule_work(&ns->head->requeue_work);
112 	return true;
113 }
114 
nvme_kick_requeue_lists(struct nvme_ctrl * ctrl)115 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
116 {
117 	struct nvme_ns *ns;
118 
119 	down_read(&ctrl->namespaces_rwsem);
120 	list_for_each_entry(ns, &ctrl->namespaces, list) {
121 		if (ns->head->disk)
122 			kblockd_schedule_work(&ns->head->requeue_work);
123 	}
124 	up_read(&ctrl->namespaces_rwsem);
125 }
126 
127 static const char *nvme_ana_state_names[] = {
128 	[0]				= "invalid state",
129 	[NVME_ANA_OPTIMIZED]		= "optimized",
130 	[NVME_ANA_NONOPTIMIZED]		= "non-optimized",
131 	[NVME_ANA_INACCESSIBLE]		= "inaccessible",
132 	[NVME_ANA_PERSISTENT_LOSS]	= "persistent-loss",
133 	[NVME_ANA_CHANGE]		= "change",
134 };
135 
nvme_mpath_clear_current_path(struct nvme_ns * ns)136 bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
137 {
138 	struct nvme_ns_head *head = ns->head;
139 	bool changed = false;
140 	int node;
141 
142 	if (!head)
143 		goto out;
144 
145 	for_each_node(node) {
146 		if (ns == rcu_access_pointer(head->current_path[node])) {
147 			rcu_assign_pointer(head->current_path[node], NULL);
148 			changed = true;
149 		}
150 	}
151 out:
152 	return changed;
153 }
154 
nvme_mpath_clear_ctrl_paths(struct nvme_ctrl * ctrl)155 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
156 {
157 	struct nvme_ns *ns;
158 
159 	down_read(&ctrl->namespaces_rwsem);
160 	list_for_each_entry(ns, &ctrl->namespaces, list) {
161 		nvme_mpath_clear_current_path(ns);
162 		kblockd_schedule_work(&ns->head->requeue_work);
163 	}
164 	up_read(&ctrl->namespaces_rwsem);
165 }
166 
nvme_path_is_disabled(struct nvme_ns * ns)167 static bool nvme_path_is_disabled(struct nvme_ns *ns)
168 {
169 	return ns->ctrl->state != NVME_CTRL_LIVE ||
170 		test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
171 		test_bit(NVME_NS_REMOVING, &ns->flags);
172 }
173 
__nvme_find_path(struct nvme_ns_head * head,int node)174 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
175 {
176 	int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
177 	struct nvme_ns *found = NULL, *fallback = NULL, *ns;
178 
179 	list_for_each_entry_rcu(ns, &head->list, siblings) {
180 		if (nvme_path_is_disabled(ns))
181 			continue;
182 
183 		if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
184 			distance = node_distance(node, ns->ctrl->numa_node);
185 		else
186 			distance = LOCAL_DISTANCE;
187 
188 		switch (ns->ana_state) {
189 		case NVME_ANA_OPTIMIZED:
190 			if (distance < found_distance) {
191 				found_distance = distance;
192 				found = ns;
193 			}
194 			break;
195 		case NVME_ANA_NONOPTIMIZED:
196 			if (distance < fallback_distance) {
197 				fallback_distance = distance;
198 				fallback = ns;
199 			}
200 			break;
201 		default:
202 			break;
203 		}
204 	}
205 
206 	if (!found)
207 		found = fallback;
208 	if (found)
209 		rcu_assign_pointer(head->current_path[node], found);
210 	return found;
211 }
212 
nvme_next_ns(struct nvme_ns_head * head,struct nvme_ns * ns)213 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
214 		struct nvme_ns *ns)
215 {
216 	ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
217 			siblings);
218 	if (ns)
219 		return ns;
220 	return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
221 }
222 
nvme_round_robin_path(struct nvme_ns_head * head,int node,struct nvme_ns * old)223 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
224 		int node, struct nvme_ns *old)
225 {
226 	struct nvme_ns *ns, *found, *fallback = NULL;
227 
228 	if (list_is_singular(&head->list)) {
229 		if (nvme_path_is_disabled(old))
230 			return NULL;
231 		return old;
232 	}
233 
234 	for (ns = nvme_next_ns(head, old);
235 	     ns && ns != old;
236 	     ns = nvme_next_ns(head, ns)) {
237 		if (nvme_path_is_disabled(ns))
238 			continue;
239 
240 		if (ns->ana_state == NVME_ANA_OPTIMIZED) {
241 			found = ns;
242 			goto out;
243 		}
244 		if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
245 			fallback = ns;
246 	}
247 
248 	/*
249 	 * The loop above skips the current path for round-robin semantics.
250 	 * Fall back to the current path if either:
251 	 *  - no other optimized path found and current is optimized,
252 	 *  - no other usable path found and current is usable.
253 	 */
254 	if (!nvme_path_is_disabled(old) &&
255 	    (old->ana_state == NVME_ANA_OPTIMIZED ||
256 	     (!fallback && old->ana_state == NVME_ANA_NONOPTIMIZED)))
257 		return old;
258 
259 	if (!fallback)
260 		return NULL;
261 	found = fallback;
262 out:
263 	rcu_assign_pointer(head->current_path[node], found);
264 	return found;
265 }
266 
nvme_path_is_optimized(struct nvme_ns * ns)267 static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
268 {
269 	return ns->ctrl->state == NVME_CTRL_LIVE &&
270 		ns->ana_state == NVME_ANA_OPTIMIZED;
271 }
272 
nvme_find_path(struct nvme_ns_head * head)273 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
274 {
275 	int node = numa_node_id();
276 	struct nvme_ns *ns;
277 
278 	ns = srcu_dereference(head->current_path[node], &head->srcu);
279 	if (unlikely(!ns))
280 		return __nvme_find_path(head, node);
281 
282 	if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR)
283 		return nvme_round_robin_path(head, node, ns);
284 	if (unlikely(!nvme_path_is_optimized(ns)))
285 		return __nvme_find_path(head, node);
286 	return ns;
287 }
288 
nvme_available_path(struct nvme_ns_head * head)289 static bool nvme_available_path(struct nvme_ns_head *head)
290 {
291 	struct nvme_ns *ns;
292 
293 	list_for_each_entry_rcu(ns, &head->list, siblings) {
294 		switch (ns->ctrl->state) {
295 		case NVME_CTRL_LIVE:
296 		case NVME_CTRL_RESETTING:
297 		case NVME_CTRL_CONNECTING:
298 			/* fallthru */
299 			return true;
300 		default:
301 			break;
302 		}
303 	}
304 	return false;
305 }
306 
nvme_ns_head_make_request(struct request_queue * q,struct bio * bio)307 static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
308 		struct bio *bio)
309 {
310 	struct nvme_ns_head *head = q->queuedata;
311 	struct device *dev = disk_to_dev(head->disk);
312 	struct nvme_ns *ns;
313 	blk_qc_t ret = BLK_QC_T_NONE;
314 	int srcu_idx;
315 
316 	/*
317 	 * The namespace might be going away and the bio might
318 	 * be moved to a different queue via blk_steal_bios(),
319 	 * so we need to use the bio_split pool from the original
320 	 * queue to allocate the bvecs from.
321 	 */
322 	blk_queue_split(q, &bio);
323 
324 	srcu_idx = srcu_read_lock(&head->srcu);
325 	ns = nvme_find_path(head);
326 	if (likely(ns)) {
327 		bio->bi_disk = ns->disk;
328 		bio->bi_opf |= REQ_NVME_MPATH;
329 		trace_block_bio_remap(bio->bi_disk->queue, bio,
330 				      disk_devt(ns->head->disk),
331 				      bio->bi_iter.bi_sector);
332 		ret = generic_make_request(bio);
333 	} else if (nvme_available_path(head)) {
334 		dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
335 
336 		spin_lock_irq(&head->requeue_lock);
337 		bio_list_add(&head->requeue_list, bio);
338 		spin_unlock_irq(&head->requeue_lock);
339 	} else {
340 		dev_warn_ratelimited(dev, "no available path - failing I/O\n");
341 
342 		bio->bi_status = BLK_STS_IOERR;
343 		bio_endio(bio);
344 	}
345 
346 	srcu_read_unlock(&head->srcu, srcu_idx);
347 	return ret;
348 }
349 
nvme_requeue_work(struct work_struct * work)350 static void nvme_requeue_work(struct work_struct *work)
351 {
352 	struct nvme_ns_head *head =
353 		container_of(work, struct nvme_ns_head, requeue_work);
354 	struct bio *bio, *next;
355 
356 	spin_lock_irq(&head->requeue_lock);
357 	next = bio_list_get(&head->requeue_list);
358 	spin_unlock_irq(&head->requeue_lock);
359 
360 	while ((bio = next) != NULL) {
361 		next = bio->bi_next;
362 		bio->bi_next = NULL;
363 
364 		/*
365 		 * Reset disk to the mpath node and resubmit to select a new
366 		 * path.
367 		 */
368 		bio->bi_disk = head->disk;
369 		generic_make_request(bio);
370 	}
371 }
372 
nvme_mpath_alloc_disk(struct nvme_ctrl * ctrl,struct nvme_ns_head * head)373 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
374 {
375 	struct request_queue *q;
376 	bool vwc = false;
377 
378 	mutex_init(&head->lock);
379 	bio_list_init(&head->requeue_list);
380 	spin_lock_init(&head->requeue_lock);
381 	INIT_WORK(&head->requeue_work, nvme_requeue_work);
382 
383 	/*
384 	 * Add a multipath node if the subsystems supports multiple controllers.
385 	 * We also do this for private namespaces as the namespace sharing data could
386 	 * change after a rescan.
387 	 */
388 	if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
389 		return 0;
390 
391 	q = blk_alloc_queue_node(GFP_KERNEL, ctrl->numa_node);
392 	if (!q)
393 		goto out;
394 	q->queuedata = head;
395 	blk_queue_make_request(q, nvme_ns_head_make_request);
396 	blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
397 	/* set to a default value for 512 until disk is validated */
398 	blk_queue_logical_block_size(q, 512);
399 	blk_set_stacking_limits(&q->limits);
400 
401 	/* we need to propagate up the VMC settings */
402 	if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
403 		vwc = true;
404 	blk_queue_write_cache(q, vwc, vwc);
405 
406 	head->disk = alloc_disk(0);
407 	if (!head->disk)
408 		goto out_cleanup_queue;
409 	head->disk->fops = &nvme_ns_head_ops;
410 	head->disk->private_data = head;
411 	head->disk->queue = q;
412 	head->disk->flags = GENHD_FL_EXT_DEVT;
413 	sprintf(head->disk->disk_name, "nvme%dn%d",
414 			ctrl->subsys->instance, head->instance);
415 	return 0;
416 
417 out_cleanup_queue:
418 	blk_cleanup_queue(q);
419 out:
420 	return -ENOMEM;
421 }
422 
nvme_mpath_set_live(struct nvme_ns * ns)423 static void nvme_mpath_set_live(struct nvme_ns *ns)
424 {
425 	struct nvme_ns_head *head = ns->head;
426 
427 	if (!head->disk)
428 		return;
429 
430 	if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags))
431 		device_add_disk(&head->subsys->dev, head->disk,
432 				nvme_ns_id_attr_groups);
433 
434 	mutex_lock(&head->lock);
435 	if (nvme_path_is_optimized(ns)) {
436 		int node, srcu_idx;
437 
438 		srcu_idx = srcu_read_lock(&head->srcu);
439 		for_each_node(node)
440 			__nvme_find_path(head, node);
441 		srcu_read_unlock(&head->srcu, srcu_idx);
442 	}
443 	mutex_unlock(&head->lock);
444 
445 	synchronize_srcu(&head->srcu);
446 	kblockd_schedule_work(&head->requeue_work);
447 }
448 
nvme_parse_ana_log(struct nvme_ctrl * ctrl,void * data,int (* cb)(struct nvme_ctrl * ctrl,struct nvme_ana_group_desc *,void *))449 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
450 		int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
451 			void *))
452 {
453 	void *base = ctrl->ana_log_buf;
454 	size_t offset = sizeof(struct nvme_ana_rsp_hdr);
455 	int error, i;
456 
457 	lockdep_assert_held(&ctrl->ana_lock);
458 
459 	for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
460 		struct nvme_ana_group_desc *desc = base + offset;
461 		u32 nr_nsids;
462 		size_t nsid_buf_size;
463 
464 		if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
465 			return -EINVAL;
466 
467 		nr_nsids = le32_to_cpu(desc->nnsids);
468 		nsid_buf_size = nr_nsids * sizeof(__le32);
469 
470 		if (WARN_ON_ONCE(desc->grpid == 0))
471 			return -EINVAL;
472 		if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
473 			return -EINVAL;
474 		if (WARN_ON_ONCE(desc->state == 0))
475 			return -EINVAL;
476 		if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
477 			return -EINVAL;
478 
479 		offset += sizeof(*desc);
480 		if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
481 			return -EINVAL;
482 
483 		error = cb(ctrl, desc, data);
484 		if (error)
485 			return error;
486 
487 		offset += nsid_buf_size;
488 	}
489 
490 	return 0;
491 }
492 
nvme_state_is_live(enum nvme_ana_state state)493 static inline bool nvme_state_is_live(enum nvme_ana_state state)
494 {
495 	return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
496 }
497 
nvme_update_ns_ana_state(struct nvme_ana_group_desc * desc,struct nvme_ns * ns)498 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
499 		struct nvme_ns *ns)
500 {
501 	ns->ana_grpid = le32_to_cpu(desc->grpid);
502 	ns->ana_state = desc->state;
503 	clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
504 	/*
505 	 * nvme_mpath_set_live() will trigger I/O to the multipath path device
506 	 * and in turn to this path device.  However we cannot accept this I/O
507 	 * if the controller is not live.  This may deadlock if called from
508 	 * nvme_mpath_init_identify() and the ctrl will never complete
509 	 * initialization, preventing I/O from completing.  For this case we
510 	 * will reprocess the ANA log page in nvme_mpath_update() once the
511 	 * controller is ready.
512 	 */
513 	if (nvme_state_is_live(ns->ana_state) &&
514 	    ns->ctrl->state == NVME_CTRL_LIVE)
515 		nvme_mpath_set_live(ns);
516 }
517 
nvme_update_ana_state(struct nvme_ctrl * ctrl,struct nvme_ana_group_desc * desc,void * data)518 static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
519 		struct nvme_ana_group_desc *desc, void *data)
520 {
521 	u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
522 	unsigned *nr_change_groups = data;
523 	struct nvme_ns *ns;
524 
525 	dev_dbg(ctrl->device, "ANA group %d: %s.\n",
526 			le32_to_cpu(desc->grpid),
527 			nvme_ana_state_names[desc->state]);
528 
529 	if (desc->state == NVME_ANA_CHANGE)
530 		(*nr_change_groups)++;
531 
532 	if (!nr_nsids)
533 		return 0;
534 
535 	down_read(&ctrl->namespaces_rwsem);
536 	list_for_each_entry(ns, &ctrl->namespaces, list) {
537 		unsigned nsid;
538 again:
539 		nsid = le32_to_cpu(desc->nsids[n]);
540 		if (ns->head->ns_id < nsid)
541 			continue;
542 		if (ns->head->ns_id == nsid)
543 			nvme_update_ns_ana_state(desc, ns);
544 		if (++n == nr_nsids)
545 			break;
546 		if (ns->head->ns_id > nsid)
547 			goto again;
548 	}
549 	up_read(&ctrl->namespaces_rwsem);
550 	return 0;
551 }
552 
nvme_read_ana_log(struct nvme_ctrl * ctrl)553 static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
554 {
555 	u32 nr_change_groups = 0;
556 	int error;
557 
558 	mutex_lock(&ctrl->ana_lock);
559 	error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0,
560 			ctrl->ana_log_buf, ctrl->ana_log_size, 0);
561 	if (error) {
562 		dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
563 		goto out_unlock;
564 	}
565 
566 	error = nvme_parse_ana_log(ctrl, &nr_change_groups,
567 			nvme_update_ana_state);
568 	if (error)
569 		goto out_unlock;
570 
571 	/*
572 	 * In theory we should have an ANATT timer per group as they might enter
573 	 * the change state at different times.  But that is a lot of overhead
574 	 * just to protect against a target that keeps entering new changes
575 	 * states while never finishing previous ones.  But we'll still
576 	 * eventually time out once all groups are in change state, so this
577 	 * isn't a big deal.
578 	 *
579 	 * We also double the ANATT value to provide some slack for transports
580 	 * or AEN processing overhead.
581 	 */
582 	if (nr_change_groups)
583 		mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
584 	else
585 		del_timer_sync(&ctrl->anatt_timer);
586 out_unlock:
587 	mutex_unlock(&ctrl->ana_lock);
588 	return error;
589 }
590 
nvme_ana_work(struct work_struct * work)591 static void nvme_ana_work(struct work_struct *work)
592 {
593 	struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
594 
595 	nvme_read_ana_log(ctrl);
596 }
597 
nvme_mpath_update(struct nvme_ctrl * ctrl)598 void nvme_mpath_update(struct nvme_ctrl *ctrl)
599 {
600 	u32 nr_change_groups = 0;
601 
602 	if (!ctrl->ana_log_buf)
603 		return;
604 
605 	mutex_lock(&ctrl->ana_lock);
606 	nvme_parse_ana_log(ctrl, &nr_change_groups, nvme_update_ana_state);
607 	mutex_unlock(&ctrl->ana_lock);
608 }
609 
nvme_anatt_timeout(struct timer_list * t)610 static void nvme_anatt_timeout(struct timer_list *t)
611 {
612 	struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
613 
614 	dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
615 	nvme_reset_ctrl(ctrl);
616 }
617 
nvme_mpath_stop(struct nvme_ctrl * ctrl)618 void nvme_mpath_stop(struct nvme_ctrl *ctrl)
619 {
620 	if (!nvme_ctrl_use_ana(ctrl))
621 		return;
622 	del_timer_sync(&ctrl->anatt_timer);
623 	cancel_work_sync(&ctrl->ana_work);
624 }
625 
626 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store)  \
627 	struct device_attribute subsys_attr_##_name =	\
628 		__ATTR(_name, _mode, _show, _store)
629 
630 static const char *nvme_iopolicy_names[] = {
631 	[NVME_IOPOLICY_NUMA]	= "numa",
632 	[NVME_IOPOLICY_RR]	= "round-robin",
633 };
634 
nvme_subsys_iopolicy_show(struct device * dev,struct device_attribute * attr,char * buf)635 static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
636 		struct device_attribute *attr, char *buf)
637 {
638 	struct nvme_subsystem *subsys =
639 		container_of(dev, struct nvme_subsystem, dev);
640 
641 	return sprintf(buf, "%s\n",
642 			nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
643 }
644 
nvme_subsys_iopolicy_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)645 static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
646 		struct device_attribute *attr, const char *buf, size_t count)
647 {
648 	struct nvme_subsystem *subsys =
649 		container_of(dev, struct nvme_subsystem, dev);
650 	int i;
651 
652 	for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
653 		if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
654 			WRITE_ONCE(subsys->iopolicy, i);
655 			return count;
656 		}
657 	}
658 
659 	return -EINVAL;
660 }
661 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
662 		      nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
663 
ana_grpid_show(struct device * dev,struct device_attribute * attr,char * buf)664 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
665 		char *buf)
666 {
667 	return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
668 }
669 DEVICE_ATTR_RO(ana_grpid);
670 
ana_state_show(struct device * dev,struct device_attribute * attr,char * buf)671 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
672 		char *buf)
673 {
674 	struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
675 
676 	return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
677 }
678 DEVICE_ATTR_RO(ana_state);
679 
nvme_lookup_ana_group_desc(struct nvme_ctrl * ctrl,struct nvme_ana_group_desc * desc,void * data)680 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
681 		struct nvme_ana_group_desc *desc, void *data)
682 {
683 	struct nvme_ana_group_desc *dst = data;
684 
685 	if (desc->grpid != dst->grpid)
686 		return 0;
687 
688 	*dst = *desc;
689 	return -ENXIO; /* just break out of the loop */
690 }
691 
nvme_mpath_add_disk(struct nvme_ns * ns,struct nvme_id_ns * id)692 void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
693 {
694 	if (nvme_ctrl_use_ana(ns->ctrl)) {
695 		struct nvme_ana_group_desc desc = {
696 			.grpid = id->anagrpid,
697 			.state = 0,
698 		};
699 
700 		mutex_lock(&ns->ctrl->ana_lock);
701 		ns->ana_grpid = le32_to_cpu(id->anagrpid);
702 		nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
703 		mutex_unlock(&ns->ctrl->ana_lock);
704 		if (desc.state) {
705 			/* found the group desc: update */
706 			nvme_update_ns_ana_state(&desc, ns);
707 		} else {
708 			/* group desc not found: trigger a re-read */
709 			set_bit(NVME_NS_ANA_PENDING, &ns->flags);
710 			queue_work(nvme_wq, &ns->ctrl->ana_work);
711 		}
712 	} else {
713 		ns->ana_state = NVME_ANA_OPTIMIZED;
714 		nvme_mpath_set_live(ns);
715 	}
716 
717 	if (bdi_cap_stable_pages_required(ns->queue->backing_dev_info)) {
718 		struct gendisk *disk = ns->head->disk;
719 
720 		if (disk)
721 			disk->queue->backing_dev_info->capabilities |=
722 					BDI_CAP_STABLE_WRITES;
723 	}
724 }
725 
nvme_mpath_remove_disk(struct nvme_ns_head * head)726 void nvme_mpath_remove_disk(struct nvme_ns_head *head)
727 {
728 	if (!head->disk)
729 		return;
730 	if (head->disk->flags & GENHD_FL_UP)
731 		del_gendisk(head->disk);
732 	blk_set_queue_dying(head->disk->queue);
733 	/* make sure all pending bios are cleaned up */
734 	kblockd_schedule_work(&head->requeue_work);
735 	flush_work(&head->requeue_work);
736 	blk_cleanup_queue(head->disk->queue);
737 	if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
738 		/*
739 		 * if device_add_disk wasn't called, prevent
740 		 * disk release to put a bogus reference on the
741 		 * request queue
742 		 */
743 		head->disk->queue = NULL;
744 	}
745 	put_disk(head->disk);
746 }
747 
nvme_mpath_init_ctrl(struct nvme_ctrl * ctrl)748 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
749 {
750 	mutex_init(&ctrl->ana_lock);
751 	timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
752 	INIT_WORK(&ctrl->ana_work, nvme_ana_work);
753 }
754 
nvme_mpath_init_identify(struct nvme_ctrl * ctrl,struct nvme_id_ctrl * id)755 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
756 {
757 	size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT;
758 	size_t ana_log_size;
759 	int error = 0;
760 
761 	/* check if multipath is enabled and we have the capability */
762 	if (!multipath || !ctrl->subsys || !(ctrl->subsys->cmic & (1 << 3)))
763 		return 0;
764 
765 	ctrl->anacap = id->anacap;
766 	ctrl->anatt = id->anatt;
767 	ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
768 	ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
769 
770 	ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
771 		ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
772 		ctrl->max_namespaces * sizeof(__le32);
773 	if (ana_log_size > max_transfer_size) {
774 		dev_err(ctrl->device,
775 			"ANA log page size (%zd) larger than MDTS (%zd).\n",
776 			ana_log_size, max_transfer_size);
777 		dev_err(ctrl->device, "disabling ANA support.\n");
778 		goto out_uninit;
779 	}
780 	if (ana_log_size > ctrl->ana_log_size) {
781 		nvme_mpath_stop(ctrl);
782 		kfree(ctrl->ana_log_buf);
783 		ctrl->ana_log_buf = kmalloc(ana_log_size, GFP_KERNEL);
784 		if (!ctrl->ana_log_buf)
785 			return -ENOMEM;
786 	}
787 	ctrl->ana_log_size = ana_log_size;
788 	error = nvme_read_ana_log(ctrl);
789 	if (error)
790 		goto out_uninit;
791 	return 0;
792 
793 out_uninit:
794 	nvme_mpath_uninit(ctrl);
795 	return error;
796 }
797 
nvme_mpath_uninit(struct nvme_ctrl * ctrl)798 void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
799 {
800 	kfree(ctrl->ana_log_buf);
801 	ctrl->ana_log_buf = NULL;
802 }
803 
804