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