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