Merge tag 'rpmsg-v5.9' of git://git.kernel.org/pub/scm/linux/kernel/git/andersson...
[linux-2.6-microblaze.git] / drivers / nvme / host / multipath.c
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 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
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
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
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
155 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
156 {
157         struct nvme_ns *ns;
158
159         mutex_lock(&ctrl->scan_lock);
160         down_read(&ctrl->namespaces_rwsem);
161         list_for_each_entry(ns, &ctrl->namespaces, list)
162                 if (nvme_mpath_clear_current_path(ns))
163                         kblockd_schedule_work(&ns->head->requeue_work);
164         up_read(&ctrl->namespaces_rwsem);
165         mutex_unlock(&ctrl->scan_lock);
166 }
167
168 static bool nvme_path_is_disabled(struct nvme_ns *ns)
169 {
170         /*
171          * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
172          * still be able to complete assuming that the controller is connected.
173          * Otherwise it will fail immediately and return to the requeue list.
174          */
175         if (ns->ctrl->state != NVME_CTRL_LIVE &&
176             ns->ctrl->state != NVME_CTRL_DELETING)
177                 return true;
178         if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
179             test_bit(NVME_NS_REMOVING, &ns->flags))
180                 return true;
181         return false;
182 }
183
184 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
185 {
186         int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
187         struct nvme_ns *found = NULL, *fallback = NULL, *ns;
188
189         list_for_each_entry_rcu(ns, &head->list, siblings) {
190                 if (nvme_path_is_disabled(ns))
191                         continue;
192
193                 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
194                         distance = node_distance(node, ns->ctrl->numa_node);
195                 else
196                         distance = LOCAL_DISTANCE;
197
198                 switch (ns->ana_state) {
199                 case NVME_ANA_OPTIMIZED:
200                         if (distance < found_distance) {
201                                 found_distance = distance;
202                                 found = ns;
203                         }
204                         break;
205                 case NVME_ANA_NONOPTIMIZED:
206                         if (distance < fallback_distance) {
207                                 fallback_distance = distance;
208                                 fallback = ns;
209                         }
210                         break;
211                 default:
212                         break;
213                 }
214         }
215
216         if (!found)
217                 found = fallback;
218         if (found)
219                 rcu_assign_pointer(head->current_path[node], found);
220         return found;
221 }
222
223 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
224                 struct nvme_ns *ns)
225 {
226         ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
227                         siblings);
228         if (ns)
229                 return ns;
230         return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
231 }
232
233 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
234                 int node, struct nvme_ns *old)
235 {
236         struct nvme_ns *ns, *found, *fallback = NULL;
237
238         if (list_is_singular(&head->list)) {
239                 if (nvme_path_is_disabled(old))
240                         return NULL;
241                 return old;
242         }
243
244         for (ns = nvme_next_ns(head, old);
245              ns != old;
246              ns = nvme_next_ns(head, ns)) {
247                 if (nvme_path_is_disabled(ns))
248                         continue;
249
250                 if (ns->ana_state == NVME_ANA_OPTIMIZED) {
251                         found = ns;
252                         goto out;
253                 }
254                 if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
255                         fallback = ns;
256         }
257
258         /* No optimized path found, re-check the current path */
259         if (!nvme_path_is_disabled(old) &&
260             old->ana_state == NVME_ANA_OPTIMIZED) {
261                 found = old;
262                 goto out;
263         }
264         if (!fallback)
265                 return NULL;
266         found = fallback;
267 out:
268         rcu_assign_pointer(head->current_path[node], found);
269         return found;
270 }
271
272 static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
273 {
274         return ns->ctrl->state == NVME_CTRL_LIVE &&
275                 ns->ana_state == NVME_ANA_OPTIMIZED;
276 }
277
278 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
279 {
280         int node = numa_node_id();
281         struct nvme_ns *ns;
282
283         ns = srcu_dereference(head->current_path[node], &head->srcu);
284         if (unlikely(!ns))
285                 return __nvme_find_path(head, node);
286
287         if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR)
288                 return nvme_round_robin_path(head, node, ns);
289         if (unlikely(!nvme_path_is_optimized(ns)))
290                 return __nvme_find_path(head, node);
291         return ns;
292 }
293
294 static bool nvme_available_path(struct nvme_ns_head *head)
295 {
296         struct nvme_ns *ns;
297
298         list_for_each_entry_rcu(ns, &head->list, siblings) {
299                 switch (ns->ctrl->state) {
300                 case NVME_CTRL_LIVE:
301                 case NVME_CTRL_RESETTING:
302                 case NVME_CTRL_CONNECTING:
303                         /* fallthru */
304                         return true;
305                 default:
306                         break;
307                 }
308         }
309         return false;
310 }
311
312 blk_qc_t nvme_ns_head_submit_bio(struct bio *bio)
313 {
314         struct nvme_ns_head *head = bio->bi_disk->private_data;
315         struct device *dev = disk_to_dev(head->disk);
316         struct nvme_ns *ns;
317         blk_qc_t ret = BLK_QC_T_NONE;
318         int srcu_idx;
319
320         /*
321          * The namespace might be going away and the bio might be moved to a
322          * different queue via blk_steal_bios(), so we need to use the bio_split
323          * pool from the original queue to allocate the bvecs from.
324          */
325         blk_queue_split(&bio);
326
327         srcu_idx = srcu_read_lock(&head->srcu);
328         ns = nvme_find_path(head);
329         if (likely(ns)) {
330                 bio->bi_disk = ns->disk;
331                 bio->bi_opf |= REQ_NVME_MPATH;
332                 trace_block_bio_remap(bio->bi_disk->queue, bio,
333                                       disk_devt(ns->head->disk),
334                                       bio->bi_iter.bi_sector);
335                 ret = submit_bio_noacct(bio);
336         } else if (nvme_available_path(head)) {
337                 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
338
339                 spin_lock_irq(&head->requeue_lock);
340                 bio_list_add(&head->requeue_list, bio);
341                 spin_unlock_irq(&head->requeue_lock);
342         } else {
343                 dev_warn_ratelimited(dev, "no available path - failing I/O\n");
344
345                 bio->bi_status = BLK_STS_IOERR;
346                 bio_endio(bio);
347         }
348
349         srcu_read_unlock(&head->srcu, srcu_idx);
350         return ret;
351 }
352
353 static void nvme_requeue_work(struct work_struct *work)
354 {
355         struct nvme_ns_head *head =
356                 container_of(work, struct nvme_ns_head, requeue_work);
357         struct bio *bio, *next;
358
359         spin_lock_irq(&head->requeue_lock);
360         next = bio_list_get(&head->requeue_list);
361         spin_unlock_irq(&head->requeue_lock);
362
363         while ((bio = next) != NULL) {
364                 next = bio->bi_next;
365                 bio->bi_next = NULL;
366
367                 /*
368                  * Reset disk to the mpath node and resubmit to select a new
369                  * path.
370                  */
371                 bio->bi_disk = head->disk;
372                 submit_bio_noacct(bio);
373         }
374 }
375
376 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
377 {
378         struct request_queue *q;
379         bool vwc = false;
380
381         mutex_init(&head->lock);
382         bio_list_init(&head->requeue_list);
383         spin_lock_init(&head->requeue_lock);
384         INIT_WORK(&head->requeue_work, nvme_requeue_work);
385
386         /*
387          * Add a multipath node if the subsystems supports multiple controllers.
388          * We also do this for private namespaces as the namespace sharing data could
389          * change after a rescan.
390          */
391         if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || !multipath)
392                 return 0;
393
394         q = blk_alloc_queue(ctrl->numa_node);
395         if (!q)
396                 goto out;
397         blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
398         /* set to a default value for 512 until disk is validated */
399         blk_queue_logical_block_size(q, 512);
400         blk_set_stacking_limits(&q->limits);
401
402         /* we need to propagate up the VMC settings */
403         if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
404                 vwc = true;
405         blk_queue_write_cache(q, vwc, vwc);
406
407         head->disk = alloc_disk(0);
408         if (!head->disk)
409                 goto out_cleanup_queue;
410         head->disk->fops = &nvme_ns_head_ops;
411         head->disk->private_data = head;
412         head->disk->queue = q;
413         head->disk->flags = GENHD_FL_EXT_DEVT;
414         sprintf(head->disk->disk_name, "nvme%dn%d",
415                         ctrl->subsys->instance, head->instance);
416         return 0;
417
418 out_cleanup_queue:
419         blk_cleanup_queue(q);
420 out:
421         return -ENOMEM;
422 }
423
424 static void nvme_mpath_set_live(struct nvme_ns *ns)
425 {
426         struct nvme_ns_head *head = ns->head;
427
428         if (!head->disk)
429                 return;
430
431         if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags))
432                 device_add_disk(&head->subsys->dev, head->disk,
433                                 nvme_ns_id_attr_groups);
434
435         mutex_lock(&head->lock);
436         if (nvme_path_is_optimized(ns)) {
437                 int node, srcu_idx;
438
439                 srcu_idx = srcu_read_lock(&head->srcu);
440                 for_each_node(node)
441                         __nvme_find_path(head, node);
442                 srcu_read_unlock(&head->srcu, srcu_idx);
443         }
444         mutex_unlock(&head->lock);
445
446         synchronize_srcu(&head->srcu);
447         kblockd_schedule_work(&head->requeue_work);
448 }
449
450 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
451                 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
452                         void *))
453 {
454         void *base = ctrl->ana_log_buf;
455         size_t offset = sizeof(struct nvme_ana_rsp_hdr);
456         int error, i;
457
458         lockdep_assert_held(&ctrl->ana_lock);
459
460         for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
461                 struct nvme_ana_group_desc *desc = base + offset;
462                 u32 nr_nsids;
463                 size_t nsid_buf_size;
464
465                 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
466                         return -EINVAL;
467
468                 nr_nsids = le32_to_cpu(desc->nnsids);
469                 nsid_buf_size = nr_nsids * sizeof(__le32);
470
471                 if (WARN_ON_ONCE(desc->grpid == 0))
472                         return -EINVAL;
473                 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
474                         return -EINVAL;
475                 if (WARN_ON_ONCE(desc->state == 0))
476                         return -EINVAL;
477                 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
478                         return -EINVAL;
479
480                 offset += sizeof(*desc);
481                 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
482                         return -EINVAL;
483
484                 error = cb(ctrl, desc, data);
485                 if (error)
486                         return error;
487
488                 offset += nsid_buf_size;
489         }
490
491         return 0;
492 }
493
494 static inline bool nvme_state_is_live(enum nvme_ana_state state)
495 {
496         return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
497 }
498
499 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
500                 struct nvme_ns *ns)
501 {
502         ns->ana_grpid = le32_to_cpu(desc->grpid);
503         ns->ana_state = desc->state;
504         clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
505
506         if (nvme_state_is_live(ns->ana_state))
507                 nvme_mpath_set_live(ns);
508 }
509
510 static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
511                 struct nvme_ana_group_desc *desc, void *data)
512 {
513         u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
514         unsigned *nr_change_groups = data;
515         struct nvme_ns *ns;
516
517         dev_dbg(ctrl->device, "ANA group %d: %s.\n",
518                         le32_to_cpu(desc->grpid),
519                         nvme_ana_state_names[desc->state]);
520
521         if (desc->state == NVME_ANA_CHANGE)
522                 (*nr_change_groups)++;
523
524         if (!nr_nsids)
525                 return 0;
526
527         down_read(&ctrl->namespaces_rwsem);
528         list_for_each_entry(ns, &ctrl->namespaces, list) {
529                 unsigned nsid = le32_to_cpu(desc->nsids[n]);
530
531                 if (ns->head->ns_id < nsid)
532                         continue;
533                 if (ns->head->ns_id == nsid)
534                         nvme_update_ns_ana_state(desc, ns);
535                 if (++n == nr_nsids)
536                         break;
537         }
538         up_read(&ctrl->namespaces_rwsem);
539         return 0;
540 }
541
542 static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
543 {
544         u32 nr_change_groups = 0;
545         int error;
546
547         mutex_lock(&ctrl->ana_lock);
548         error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
549                         ctrl->ana_log_buf, ctrl->ana_log_size, 0);
550         if (error) {
551                 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
552                 goto out_unlock;
553         }
554
555         error = nvme_parse_ana_log(ctrl, &nr_change_groups,
556                         nvme_update_ana_state);
557         if (error)
558                 goto out_unlock;
559
560         /*
561          * In theory we should have an ANATT timer per group as they might enter
562          * the change state at different times.  But that is a lot of overhead
563          * just to protect against a target that keeps entering new changes
564          * states while never finishing previous ones.  But we'll still
565          * eventually time out once all groups are in change state, so this
566          * isn't a big deal.
567          *
568          * We also double the ANATT value to provide some slack for transports
569          * or AEN processing overhead.
570          */
571         if (nr_change_groups)
572                 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
573         else
574                 del_timer_sync(&ctrl->anatt_timer);
575 out_unlock:
576         mutex_unlock(&ctrl->ana_lock);
577         return error;
578 }
579
580 static void nvme_ana_work(struct work_struct *work)
581 {
582         struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
583
584         if (ctrl->state != NVME_CTRL_LIVE)
585                 return;
586
587         nvme_read_ana_log(ctrl);
588 }
589
590 static void nvme_anatt_timeout(struct timer_list *t)
591 {
592         struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
593
594         dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
595         nvme_reset_ctrl(ctrl);
596 }
597
598 void nvme_mpath_stop(struct nvme_ctrl *ctrl)
599 {
600         if (!nvme_ctrl_use_ana(ctrl))
601                 return;
602         del_timer_sync(&ctrl->anatt_timer);
603         cancel_work_sync(&ctrl->ana_work);
604 }
605
606 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store)  \
607         struct device_attribute subsys_attr_##_name =   \
608                 __ATTR(_name, _mode, _show, _store)
609
610 static const char *nvme_iopolicy_names[] = {
611         [NVME_IOPOLICY_NUMA]    = "numa",
612         [NVME_IOPOLICY_RR]      = "round-robin",
613 };
614
615 static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
616                 struct device_attribute *attr, char *buf)
617 {
618         struct nvme_subsystem *subsys =
619                 container_of(dev, struct nvme_subsystem, dev);
620
621         return sprintf(buf, "%s\n",
622                         nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
623 }
624
625 static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
626                 struct device_attribute *attr, const char *buf, size_t count)
627 {
628         struct nvme_subsystem *subsys =
629                 container_of(dev, struct nvme_subsystem, dev);
630         int i;
631
632         for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
633                 if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
634                         WRITE_ONCE(subsys->iopolicy, i);
635                         return count;
636                 }
637         }
638
639         return -EINVAL;
640 }
641 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
642                       nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
643
644 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
645                 char *buf)
646 {
647         return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
648 }
649 DEVICE_ATTR_RO(ana_grpid);
650
651 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
652                 char *buf)
653 {
654         struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
655
656         return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
657 }
658 DEVICE_ATTR_RO(ana_state);
659
660 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
661                 struct nvme_ana_group_desc *desc, void *data)
662 {
663         struct nvme_ana_group_desc *dst = data;
664
665         if (desc->grpid != dst->grpid)
666                 return 0;
667
668         *dst = *desc;
669         return -ENXIO; /* just break out of the loop */
670 }
671
672 void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
673 {
674         if (nvme_ctrl_use_ana(ns->ctrl)) {
675                 struct nvme_ana_group_desc desc = {
676                         .grpid = id->anagrpid,
677                         .state = 0,
678                 };
679
680                 mutex_lock(&ns->ctrl->ana_lock);
681                 ns->ana_grpid = le32_to_cpu(id->anagrpid);
682                 nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
683                 mutex_unlock(&ns->ctrl->ana_lock);
684                 if (desc.state) {
685                         /* found the group desc: update */
686                         nvme_update_ns_ana_state(&desc, ns);
687                 }
688         } else {
689                 ns->ana_state = NVME_ANA_OPTIMIZED; 
690                 nvme_mpath_set_live(ns);
691         }
692
693         if (bdi_cap_stable_pages_required(ns->queue->backing_dev_info)) {
694                 struct gendisk *disk = ns->head->disk;
695
696                 if (disk)
697                         disk->queue->backing_dev_info->capabilities |=
698                                         BDI_CAP_STABLE_WRITES;
699         }
700 }
701
702 void nvme_mpath_remove_disk(struct nvme_ns_head *head)
703 {
704         if (!head->disk)
705                 return;
706         if (head->disk->flags & GENHD_FL_UP)
707                 del_gendisk(head->disk);
708         blk_set_queue_dying(head->disk->queue);
709         /* make sure all pending bios are cleaned up */
710         kblockd_schedule_work(&head->requeue_work);
711         flush_work(&head->requeue_work);
712         blk_cleanup_queue(head->disk->queue);
713         if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
714                 /*
715                  * if device_add_disk wasn't called, prevent
716                  * disk release to put a bogus reference on the
717                  * request queue
718                  */
719                 head->disk->queue = NULL;
720         }
721         put_disk(head->disk);
722 }
723
724 int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
725 {
726         int error;
727
728         /* check if multipath is enabled and we have the capability */
729         if (!multipath || !ctrl->subsys ||
730             !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA))
731                 return 0;
732
733         ctrl->anacap = id->anacap;
734         ctrl->anatt = id->anatt;
735         ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
736         ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
737
738         mutex_init(&ctrl->ana_lock);
739         timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
740         ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
741                 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc);
742         ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32);
743
744         if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) {
745                 dev_err(ctrl->device,
746                         "ANA log page size (%zd) larger than MDTS (%d).\n",
747                         ctrl->ana_log_size,
748                         ctrl->max_hw_sectors << SECTOR_SHIFT);
749                 dev_err(ctrl->device, "disabling ANA support.\n");
750                 return 0;
751         }
752
753         INIT_WORK(&ctrl->ana_work, nvme_ana_work);
754         kfree(ctrl->ana_log_buf);
755         ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
756         if (!ctrl->ana_log_buf) {
757                 error = -ENOMEM;
758                 goto out;
759         }
760
761         error = nvme_read_ana_log(ctrl);
762         if (error)
763                 goto out_free_ana_log_buf;
764         return 0;
765 out_free_ana_log_buf:
766         kfree(ctrl->ana_log_buf);
767         ctrl->ana_log_buf = NULL;
768 out:
769         return error;
770 }
771
772 void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
773 {
774         kfree(ctrl->ana_log_buf);
775         ctrl->ana_log_buf = NULL;
776 }
777