blob: 74bad4e3d3778523b429977291ab791cf70d0eb6 [file] [log] [blame]
Christoph Hellwigbc50ad72019-02-18 09:36:29 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwig32acab32017-11-02 12:59:30 +01002/*
Christoph Hellwig0d0b6602018-05-14 08:48:54 +02003 * Copyright (c) 2017-2018 Christoph Hellwig.
Christoph Hellwig32acab32017-11-02 12:59:30 +01004 */
5
Keith Buschb2ce4d92020-04-09 09:09:04 -07006#include <linux/backing-dev.h>
Christoph Hellwig32acab32017-11-02 12:59:30 +01007#include <linux/moduleparam.h>
Hannes Reinecke2796b562018-06-07 10:38:47 +02008#include <trace/events/block.h>
Christoph Hellwig32acab32017-11-02 12:59:30 +01009#include "nvme.h"
10
11static bool multipath = true;
Keith Busch5cadde82018-04-26 14:24:29 -060012module_param(multipath, bool, 0444);
Christoph Hellwig32acab32017-11-02 12:59:30 +010013MODULE_PARM_DESC(multipath,
14 "turn on native support for multiple controllers per subsystem");
15
Sagi Grimbergb9156da2019-07-31 11:00:26 -070016void 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
26void 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
36void 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
Keith Buscha785dbc2018-04-26 14:22:41 -060046/*
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 */
53void 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,
Hannes Reinecke8a03b272019-05-03 15:37:35 +020060 ctrl->instance, ns->head->instance);
Keith Buscha785dbc2018-04-26 14:22:41 -060061 *flags = GENHD_FL_HIDDEN;
62 } else {
63 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
64 ns->head->instance);
65 }
66}
67
John Meneghini764e9332020-02-20 10:05:38 +090068bool nvme_failover_req(struct request *req)
Christoph Hellwig32acab32017-11-02 12:59:30 +010069{
70 struct nvme_ns *ns = req->q->queuedata;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +020071 u16 status = nvme_req(req)->status;
Christoph Hellwig32acab32017-11-02 12:59:30 +010072 unsigned long flags;
73
Christoph Hellwig0d0b6602018-05-14 08:48:54 +020074 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;
James Smart783f4a42018-09-27 16:58:54 -070093 case NVME_SC_HOST_PATH_ERROR:
Max Gurtovoy2dc39472019-10-13 19:57:35 +030094 case NVME_SC_HOST_ABORTED_CMD:
James Smart783f4a42018-09-27 16:58:54 -070095 /*
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;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200101 default:
John Meneghini764e9332020-02-20 10:05:38 +0900102 /* This was a non-ANA error so follow the normal error path. */
103 return false;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200104 }
105
John Meneghini764e9332020-02-20 10:05:38 +0900106 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
Christoph Hellwig32acab32017-11-02 12:59:30 +0100111 kblockd_schedule_work(&ns->head->requeue_work);
John Meneghini764e9332020-02-20 10:05:38 +0900112 return true;
Christoph Hellwig32acab32017-11-02 12:59:30 +0100113}
114
Christoph Hellwig32acab32017-11-02 12:59:30 +0100115void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
116{
117 struct nvme_ns *ns;
118
Jianchao Wang765cc0312018-02-12 20:54:46 +0800119 down_read(&ctrl->namespaces_rwsem);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100120 list_for_each_entry(ns, &ctrl->namespaces, list) {
121 if (ns->head->disk)
122 kblockd_schedule_work(&ns->head->requeue_work);
123 }
Jianchao Wang765cc0312018-02-12 20:54:46 +0800124 up_read(&ctrl->namespaces_rwsem);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100125}
126
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200127static 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
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700136bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
Christoph Hellwig32acab32017-11-02 12:59:30 +0100137{
Christoph Hellwigf3334442018-09-11 09:51:29 +0200138 struct nvme_ns_head *head = ns->head;
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700139 bool changed = false;
Christoph Hellwigf3334442018-09-11 09:51:29 +0200140 int node;
141
142 if (!head)
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700143 goto out;
Christoph Hellwigf3334442018-09-11 09:51:29 +0200144
145 for_each_node(node) {
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700146 if (ns == rcu_access_pointer(head->current_path[node])) {
Christoph Hellwigf3334442018-09-11 09:51:29 +0200147 rcu_assign_pointer(head->current_path[node], NULL);
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700148 changed = true;
149 }
Christoph Hellwigf3334442018-09-11 09:51:29 +0200150 }
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700151out:
152 return changed;
153}
154
155void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
156{
157 struct nvme_ns *ns;
158
159 mutex_lock(&ctrl->scan_lock);
Anton Eidelman763303a2019-11-01 17:27:55 -0700160 down_read(&ctrl->namespaces_rwsem);
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700161 list_for_each_entry(ns, &ctrl->namespaces, list)
162 if (nvme_mpath_clear_current_path(ns))
163 kblockd_schedule_work(&ns->head->requeue_work);
Anton Eidelman763303a2019-11-01 17:27:55 -0700164 up_read(&ctrl->namespaces_rwsem);
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700165 mutex_unlock(&ctrl->scan_lock);
Christoph Hellwigf3334442018-09-11 09:51:29 +0200166}
167
Hannes Reineckeca7ae5c2019-07-04 08:10:46 +0200168static bool nvme_path_is_disabled(struct nvme_ns *ns)
169{
170 return ns->ctrl->state != NVME_CTRL_LIVE ||
Hannes Reinecke04e70bd2019-07-04 08:10:47 +0200171 test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
172 test_bit(NVME_NS_REMOVING, &ns->flags);
Hannes Reineckeca7ae5c2019-07-04 08:10:46 +0200173}
174
Christoph Hellwigf3334442018-09-11 09:51:29 +0200175static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
176{
177 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
178 struct nvme_ns *found = NULL, *fallback = NULL, *ns;
Christoph Hellwig32acab32017-11-02 12:59:30 +0100179
180 list_for_each_entry_rcu(ns, &head->list, siblings) {
Hannes Reineckeca7ae5c2019-07-04 08:10:46 +0200181 if (nvme_path_is_disabled(ns))
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200182 continue;
Christoph Hellwigf3334442018-09-11 09:51:29 +0200183
Hannes Reinecke75c10e72019-02-18 11:43:26 +0100184 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
185 distance = node_distance(node, ns->ctrl->numa_node);
186 else
187 distance = LOCAL_DISTANCE;
Christoph Hellwigf3334442018-09-11 09:51:29 +0200188
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200189 switch (ns->ana_state) {
190 case NVME_ANA_OPTIMIZED:
Christoph Hellwigf3334442018-09-11 09:51:29 +0200191 if (distance < found_distance) {
192 found_distance = distance;
193 found = ns;
194 }
195 break;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200196 case NVME_ANA_NONOPTIMIZED:
Christoph Hellwigf3334442018-09-11 09:51:29 +0200197 if (distance < fallback_distance) {
198 fallback_distance = distance;
199 fallback = ns;
200 }
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200201 break;
202 default:
203 break;
Christoph Hellwig32acab32017-11-02 12:59:30 +0100204 }
205 }
206
Christoph Hellwigf3334442018-09-11 09:51:29 +0200207 if (!found)
208 found = fallback;
209 if (found)
210 rcu_assign_pointer(head->current_path[node], found);
211 return found;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200212}
213
Hannes Reinecke75c10e72019-02-18 11:43:26 +0100214static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
215 struct nvme_ns *ns)
216{
217 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
218 siblings);
219 if (ns)
220 return ns;
221 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
222}
223
224static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
225 int node, struct nvme_ns *old)
226{
227 struct nvme_ns *ns, *found, *fallback = NULL;
228
Hannes Reinecke2032d072019-07-04 08:10:46 +0200229 if (list_is_singular(&head->list)) {
230 if (nvme_path_is_disabled(old))
231 return NULL;
Hannes Reinecke75c10e72019-02-18 11:43:26 +0100232 return old;
Hannes Reinecke2032d072019-07-04 08:10:46 +0200233 }
Hannes Reinecke75c10e72019-02-18 11:43:26 +0100234
235 for (ns = nvme_next_ns(head, old);
236 ns != old;
237 ns = nvme_next_ns(head, ns)) {
Hannes Reineckeca7ae5c2019-07-04 08:10:46 +0200238 if (nvme_path_is_disabled(ns))
Hannes Reinecke75c10e72019-02-18 11:43:26 +0100239 continue;
240
241 if (ns->ana_state == NVME_ANA_OPTIMIZED) {
242 found = ns;
243 goto out;
244 }
245 if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
246 fallback = ns;
247 }
248
249 if (!fallback)
250 return NULL;
251 found = fallback;
252out:
253 rcu_assign_pointer(head->current_path[node], found);
254 return found;
255}
256
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200257static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
258{
259 return ns->ctrl->state == NVME_CTRL_LIVE &&
260 ns->ana_state == NVME_ANA_OPTIMIZED;
Christoph Hellwig32acab32017-11-02 12:59:30 +0100261}
262
263inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
264{
Christoph Hellwigf3334442018-09-11 09:51:29 +0200265 int node = numa_node_id();
266 struct nvme_ns *ns;
Christoph Hellwig32acab32017-11-02 12:59:30 +0100267
Christoph Hellwigf3334442018-09-11 09:51:29 +0200268 ns = srcu_dereference(head->current_path[node], &head->srcu);
Hannes Reinecke75c10e72019-02-18 11:43:26 +0100269 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR && ns)
270 ns = nvme_round_robin_path(head, node, ns);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200271 if (unlikely(!ns || !nvme_path_is_optimized(ns)))
Christoph Hellwigf3334442018-09-11 09:51:29 +0200272 ns = __nvme_find_path(head, node);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100273 return ns;
274}
275
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700276static bool nvme_available_path(struct nvme_ns_head *head)
277{
278 struct nvme_ns *ns;
279
280 list_for_each_entry_rcu(ns, &head->list, siblings) {
281 switch (ns->ctrl->state) {
282 case NVME_CTRL_LIVE:
283 case NVME_CTRL_RESETTING:
284 case NVME_CTRL_CONNECTING:
285 /* fallthru */
286 return true;
287 default:
288 break;
289 }
290 }
291 return false;
292}
293
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200294blk_qc_t nvme_ns_head_submit_bio(struct bio *bio)
Christoph Hellwig32acab32017-11-02 12:59:30 +0100295{
Christoph Hellwig7890b972020-03-29 19:41:38 +0200296 struct nvme_ns_head *head = bio->bi_disk->private_data;
Christoph Hellwig32acab32017-11-02 12:59:30 +0100297 struct device *dev = disk_to_dev(head->disk);
298 struct nvme_ns *ns;
299 blk_qc_t ret = BLK_QC_T_NONE;
300 int srcu_idx;
301
Hannes Reinecke525aa5a2019-04-30 18:57:09 +0200302 /*
Christoph Hellwigf695ca32020-07-01 10:59:39 +0200303 * The namespace might be going away and the bio might be moved to a
304 * different queue via blk_steal_bios(), so we need to use the bio_split
305 * pool from the original queue to allocate the bvecs from.
Hannes Reinecke525aa5a2019-04-30 18:57:09 +0200306 */
Christoph Hellwigf695ca32020-07-01 10:59:39 +0200307 blk_queue_split(&bio);
Hannes Reinecke525aa5a2019-04-30 18:57:09 +0200308
Christoph Hellwig32acab32017-11-02 12:59:30 +0100309 srcu_idx = srcu_read_lock(&head->srcu);
310 ns = nvme_find_path(head);
311 if (likely(ns)) {
312 bio->bi_disk = ns->disk;
313 bio->bi_opf |= REQ_NVME_MPATH;
Hannes Reinecke2796b562018-06-07 10:38:47 +0200314 trace_block_bio_remap(bio->bi_disk->queue, bio,
315 disk_devt(ns->head->disk),
316 bio->bi_iter.bi_sector);
Christoph Hellwig5a6c35f2020-07-01 10:59:47 +0200317 ret = submit_bio_noacct(bio);
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700318 } else if (nvme_available_path(head)) {
319 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
Christoph Hellwig32acab32017-11-02 12:59:30 +0100320
321 spin_lock_irq(&head->requeue_lock);
322 bio_list_add(&head->requeue_list, bio);
323 spin_unlock_irq(&head->requeue_lock);
324 } else {
Sagi Grimberg0157ec82019-07-25 11:56:57 -0700325 dev_warn_ratelimited(dev, "no available path - failing I/O\n");
Christoph Hellwig32acab32017-11-02 12:59:30 +0100326
327 bio->bi_status = BLK_STS_IOERR;
328 bio_endio(bio);
329 }
330
331 srcu_read_unlock(&head->srcu, srcu_idx);
332 return ret;
333}
334
Christoph Hellwig32acab32017-11-02 12:59:30 +0100335static void nvme_requeue_work(struct work_struct *work)
336{
337 struct nvme_ns_head *head =
338 container_of(work, struct nvme_ns_head, requeue_work);
339 struct bio *bio, *next;
340
341 spin_lock_irq(&head->requeue_lock);
342 next = bio_list_get(&head->requeue_list);
343 spin_unlock_irq(&head->requeue_lock);
344
345 while ((bio = next) != NULL) {
346 next = bio->bi_next;
347 bio->bi_next = NULL;
348
349 /*
350 * Reset disk to the mpath node and resubmit to select a new
351 * path.
352 */
353 bio->bi_disk = head->disk;
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200354 submit_bio_noacct(bio);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100355 }
356}
357
358int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
359{
360 struct request_queue *q;
361 bool vwc = false;
362
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200363 mutex_init(&head->lock);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100364 bio_list_init(&head->requeue_list);
365 spin_lock_init(&head->requeue_lock);
366 INIT_WORK(&head->requeue_work, nvme_requeue_work);
367
368 /*
369 * Add a multipath node if the subsystems supports multiple controllers.
370 * We also do this for private namespaces as the namespace sharing data could
371 * change after a rescan.
372 */
Keith Busch92decf12020-04-03 10:53:46 -0700373 if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || !multipath)
Christoph Hellwig32acab32017-11-02 12:59:30 +0100374 return 0;
375
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200376 q = blk_alloc_queue(ctrl->numa_node);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100377 if (!q)
378 goto out;
Bart Van Assche8b904b52018-03-07 17:10:10 -0800379 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100380 /* set to a default value for 512 until disk is validated */
381 blk_queue_logical_block_size(q, 512);
Sagi Grimberg8f676b852018-11-02 11:22:13 -0700382 blk_set_stacking_limits(&q->limits);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100383
384 /* we need to propagate up the VMC settings */
385 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
386 vwc = true;
387 blk_queue_write_cache(q, vwc, vwc);
388
389 head->disk = alloc_disk(0);
390 if (!head->disk)
391 goto out_cleanup_queue;
392 head->disk->fops = &nvme_ns_head_ops;
393 head->disk->private_data = head;
394 head->disk->queue = q;
395 head->disk->flags = GENHD_FL_EXT_DEVT;
396 sprintf(head->disk->disk_name, "nvme%dn%d",
397 ctrl->subsys->instance, head->instance);
398 return 0;
399
400out_cleanup_queue:
401 blk_cleanup_queue(q);
402out:
403 return -ENOMEM;
404}
405
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200406static void nvme_mpath_set_live(struct nvme_ns *ns)
Christoph Hellwig32acab32017-11-02 12:59:30 +0100407{
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200408 struct nvme_ns_head *head = ns->head;
409
Christoph Hellwig32acab32017-11-02 12:59:30 +0100410 if (!head->disk)
411 return;
Baegjae Sung9bd82b12018-02-28 16:06:04 +0900412
Anton Eidelmand8a22f82020-06-24 01:53:11 -0700413 if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags))
Hannes Reinecke33b14f672018-09-28 08:17:20 +0200414 device_add_disk(&head->subsys->dev, head->disk,
415 nvme_ns_id_attr_groups);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200416
Anton Eidelmand8a22f82020-06-24 01:53:11 -0700417 mutex_lock(&head->lock);
Keith Busch886fabf2018-10-05 09:49:37 -0600418 if (nvme_path_is_optimized(ns)) {
419 int node, srcu_idx;
420
421 srcu_idx = srcu_read_lock(&head->srcu);
422 for_each_node(node)
423 __nvme_find_path(head, node);
424 srcu_read_unlock(&head->srcu, srcu_idx);
425 }
Sagi Grimberge1644712020-06-24 01:53:10 -0700426 mutex_unlock(&head->lock);
Keith Busch886fabf2018-10-05 09:49:37 -0600427
Sagi Grimberge1644712020-06-24 01:53:10 -0700428 synchronize_srcu(&head->srcu);
429 kblockd_schedule_work(&head->requeue_work);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200430}
431
432static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
433 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
434 void *))
435{
436 void *base = ctrl->ana_log_buf;
437 size_t offset = sizeof(struct nvme_ana_rsp_hdr);
438 int error, i;
439
440 lockdep_assert_held(&ctrl->ana_lock);
441
442 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
443 struct nvme_ana_group_desc *desc = base + offset;
Prabhath Sajeepa64fab722019-10-28 16:56:48 -0600444 u32 nr_nsids;
445 size_t nsid_buf_size;
446
447 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
448 return -EINVAL;
449
450 nr_nsids = le32_to_cpu(desc->nnsids);
451 nsid_buf_size = nr_nsids * sizeof(__le32);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200452
453 if (WARN_ON_ONCE(desc->grpid == 0))
454 return -EINVAL;
455 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
456 return -EINVAL;
457 if (WARN_ON_ONCE(desc->state == 0))
458 return -EINVAL;
459 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
460 return -EINVAL;
461
462 offset += sizeof(*desc);
463 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
464 return -EINVAL;
465
466 error = cb(ctrl, desc, data);
467 if (error)
468 return error;
469
470 offset += nsid_buf_size;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200471 }
472
473 return 0;
474}
475
476static inline bool nvme_state_is_live(enum nvme_ana_state state)
477{
478 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
479}
480
481static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
482 struct nvme_ns *ns)
483{
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200484 ns->ana_grpid = le32_to_cpu(desc->grpid);
485 ns->ana_state = desc->state;
486 clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
487
Martin Georgecc2278c2019-03-27 09:52:56 +0100488 if (nvme_state_is_live(ns->ana_state))
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200489 nvme_mpath_set_live(ns);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200490}
491
492static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
493 struct nvme_ana_group_desc *desc, void *data)
494{
495 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
496 unsigned *nr_change_groups = data;
497 struct nvme_ns *ns;
498
Hannes Reinecke592b6e72019-04-28 20:24:42 -0700499 dev_dbg(ctrl->device, "ANA group %d: %s.\n",
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200500 le32_to_cpu(desc->grpid),
501 nvme_ana_state_names[desc->state]);
502
503 if (desc->state == NVME_ANA_CHANGE)
504 (*nr_change_groups)++;
505
506 if (!nr_nsids)
507 return 0;
508
Sagi Grimberg657f1972020-04-02 09:34:54 -0700509 down_read(&ctrl->namespaces_rwsem);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200510 list_for_each_entry(ns, &ctrl->namespaces, list) {
Anton Eidelmane01f91d2019-08-16 13:00:10 -0700511 unsigned nsid = le32_to_cpu(desc->nsids[n]);
512
513 if (ns->head->ns_id < nsid)
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200514 continue;
Anton Eidelmane01f91d2019-08-16 13:00:10 -0700515 if (ns->head->ns_id == nsid)
516 nvme_update_ns_ana_state(desc, ns);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200517 if (++n == nr_nsids)
518 break;
519 }
Sagi Grimberg657f1972020-04-02 09:34:54 -0700520 up_read(&ctrl->namespaces_rwsem);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200521 return 0;
522}
523
Anton Eidelman86cccfb2019-10-18 11:32:51 -0700524static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200525{
526 u32 nr_change_groups = 0;
527 int error;
528
529 mutex_lock(&ctrl->ana_lock);
Keith Buschbe93e872020-06-29 12:06:40 -0700530 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200531 ctrl->ana_log_buf, ctrl->ana_log_size, 0);
532 if (error) {
533 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
534 goto out_unlock;
535 }
536
537 error = nvme_parse_ana_log(ctrl, &nr_change_groups,
538 nvme_update_ana_state);
539 if (error)
540 goto out_unlock;
541
542 /*
543 * In theory we should have an ANATT timer per group as they might enter
544 * the change state at different times. But that is a lot of overhead
545 * just to protect against a target that keeps entering new changes
546 * states while never finishing previous ones. But we'll still
547 * eventually time out once all groups are in change state, so this
548 * isn't a big deal.
549 *
550 * We also double the ANATT value to provide some slack for transports
551 * or AEN processing overhead.
552 */
553 if (nr_change_groups)
554 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
555 else
556 del_timer_sync(&ctrl->anatt_timer);
557out_unlock:
558 mutex_unlock(&ctrl->ana_lock);
559 return error;
560}
561
562static void nvme_ana_work(struct work_struct *work)
563{
564 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
565
Anton Eidelman86cccfb2019-10-18 11:32:51 -0700566 nvme_read_ana_log(ctrl);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200567}
568
569static void nvme_anatt_timeout(struct timer_list *t)
570{
571 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
572
573 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
574 nvme_reset_ctrl(ctrl);
575}
576
577void nvme_mpath_stop(struct nvme_ctrl *ctrl)
578{
579 if (!nvme_ctrl_use_ana(ctrl))
580 return;
581 del_timer_sync(&ctrl->anatt_timer);
582 cancel_work_sync(&ctrl->ana_work);
583}
584
Hannes Reinecke75c10e72019-02-18 11:43:26 +0100585#define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \
586 struct device_attribute subsys_attr_##_name = \
587 __ATTR(_name, _mode, _show, _store)
588
589static const char *nvme_iopolicy_names[] = {
590 [NVME_IOPOLICY_NUMA] = "numa",
591 [NVME_IOPOLICY_RR] = "round-robin",
592};
593
594static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
596{
597 struct nvme_subsystem *subsys =
598 container_of(dev, struct nvme_subsystem, dev);
599
600 return sprintf(buf, "%s\n",
601 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
602}
603
604static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
605 struct device_attribute *attr, const char *buf, size_t count)
606{
607 struct nvme_subsystem *subsys =
608 container_of(dev, struct nvme_subsystem, dev);
609 int i;
610
611 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
612 if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
613 WRITE_ONCE(subsys->iopolicy, i);
614 return count;
615 }
616 }
617
618 return -EINVAL;
619}
620SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
621 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
622
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200623static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
624 char *buf)
625{
626 return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
627}
628DEVICE_ATTR_RO(ana_grpid);
629
630static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
631 char *buf)
632{
633 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
634
635 return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
636}
637DEVICE_ATTR_RO(ana_state);
638
Anton Eidelman489dd102020-06-24 01:53:09 -0700639static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200640 struct nvme_ana_group_desc *desc, void *data)
641{
Anton Eidelman489dd102020-06-24 01:53:09 -0700642 struct nvme_ana_group_desc *dst = data;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200643
Anton Eidelman489dd102020-06-24 01:53:09 -0700644 if (desc->grpid != dst->grpid)
645 return 0;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200646
Anton Eidelman489dd102020-06-24 01:53:09 -0700647 *dst = *desc;
648 return -ENXIO; /* just break out of the loop */
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200649}
650
651void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
652{
653 if (nvme_ctrl_use_ana(ns->ctrl)) {
Anton Eidelman489dd102020-06-24 01:53:09 -0700654 struct nvme_ana_group_desc desc = {
655 .grpid = id->anagrpid,
656 .state = 0,
657 };
658
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200659 mutex_lock(&ns->ctrl->ana_lock);
660 ns->ana_grpid = le32_to_cpu(id->anagrpid);
Anton Eidelman489dd102020-06-24 01:53:09 -0700661 nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200662 mutex_unlock(&ns->ctrl->ana_lock);
Anton Eidelman489dd102020-06-24 01:53:09 -0700663 if (desc.state) {
664 /* found the group desc: update */
665 nvme_update_ns_ana_state(&desc, ns);
666 }
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200667 } else {
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200668 ns->ana_state = NVME_ANA_OPTIMIZED;
669 nvme_mpath_set_live(ns);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200670 }
Keith Buschb2ce4d92020-04-09 09:09:04 -0700671
672 if (bdi_cap_stable_pages_required(ns->queue->backing_dev_info)) {
Christoph Hellwig72d44712020-06-29 16:30:19 +0200673 struct gendisk *disk = ns->head->disk;
Keith Buschb2ce4d92020-04-09 09:09:04 -0700674
Christoph Hellwig72d44712020-06-29 16:30:19 +0200675 if (disk)
676 disk->queue->backing_dev_info->capabilities |=
677 BDI_CAP_STABLE_WRITES;
Keith Buschb2ce4d92020-04-09 09:09:04 -0700678 }
Christoph Hellwig32acab32017-11-02 12:59:30 +0100679}
680
681void nvme_mpath_remove_disk(struct nvme_ns_head *head)
682{
683 if (!head->disk)
684 return;
Hannes Reinecke33b14f672018-09-28 08:17:20 +0200685 if (head->disk->flags & GENHD_FL_UP)
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200686 del_gendisk(head->disk);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100687 blk_set_queue_dying(head->disk->queue);
688 /* make sure all pending bios are cleaned up */
689 kblockd_schedule_work(&head->requeue_work);
690 flush_work(&head->requeue_work);
691 blk_cleanup_queue(head->disk->queue);
Sagi Grimbergc3124462020-06-24 01:53:12 -0700692 if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
693 /*
694 * if device_add_disk wasn't called, prevent
695 * disk release to put a bogus reference on the
696 * request queue
697 */
698 head->disk->queue = NULL;
699 }
Christoph Hellwig32acab32017-11-02 12:59:30 +0100700 put_disk(head->disk);
701}
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200702
703int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
704{
705 int error;
706
Marta Rybczynska66b20ac2019-07-23 07:41:20 +0200707 /* check if multipath is enabled and we have the capability */
Keith Busch92decf12020-04-03 10:53:46 -0700708 if (!multipath || !ctrl->subsys ||
709 !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA))
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200710 return 0;
711
712 ctrl->anacap = id->anacap;
713 ctrl->anatt = id->anatt;
714 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
715 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
716
717 mutex_init(&ctrl->ana_lock);
718 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
719 ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
720 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc);
Hannes Reinecke78a61cd2019-01-09 09:45:15 +0100721 ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200722
723 if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) {
724 dev_err(ctrl->device,
725 "ANA log page size (%zd) larger than MDTS (%d).\n",
726 ctrl->ana_log_size,
727 ctrl->max_hw_sectors << SECTOR_SHIFT);
728 dev_err(ctrl->device, "disabling ANA support.\n");
729 return 0;
730 }
731
732 INIT_WORK(&ctrl->ana_work, nvme_ana_work);
Logan Gunthorpe3b783092020-02-20 13:29:53 -0700733 kfree(ctrl->ana_log_buf);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200734 ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
Susobhan Deybb830ad2018-09-25 12:29:15 -0700735 if (!ctrl->ana_log_buf) {
736 error = -ENOMEM;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200737 goto out;
Susobhan Deybb830ad2018-09-25 12:29:15 -0700738 }
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200739
Anton Eidelman86cccfb2019-10-18 11:32:51 -0700740 error = nvme_read_ana_log(ctrl);
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200741 if (error)
742 goto out_free_ana_log_buf;
743 return 0;
744out_free_ana_log_buf:
745 kfree(ctrl->ana_log_buf);
Hannes Reineckec7055fd2019-01-08 12:46:58 +0100746 ctrl->ana_log_buf = NULL;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200747out:
Susobhan Deybb830ad2018-09-25 12:29:15 -0700748 return error;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200749}
750
751void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
752{
753 kfree(ctrl->ana_log_buf);
Hannes Reineckec7055fd2019-01-08 12:46:58 +0100754 ctrl->ana_log_buf = NULL;
Christoph Hellwig0d0b6602018-05-14 08:48:54 +0200755}
756