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 <linux/vmalloc.h> 9 #include <trace/events/block.h> 10 #include "nvme.h" 11 12 bool multipath = true; 13 module_param(multipath, bool, 0444); 14 MODULE_PARM_DESC(multipath, 15 "turn on native support for multiple controllers per subsystem"); 16 17 static const char *nvme_iopolicy_names[] = { 18 [NVME_IOPOLICY_NUMA] = "numa", 19 [NVME_IOPOLICY_RR] = "round-robin", 20 }; 21 22 static int iopolicy = NVME_IOPOLICY_NUMA; 23 24 static int nvme_set_iopolicy(const char *val, const struct kernel_param *kp) 25 { 26 if (!val) 27 return -EINVAL; 28 if (!strncmp(val, "numa", 4)) 29 iopolicy = NVME_IOPOLICY_NUMA; 30 else if (!strncmp(val, "round-robin", 11)) 31 iopolicy = NVME_IOPOLICY_RR; 32 else 33 return -EINVAL; 34 35 return 0; 36 } 37 38 static int nvme_get_iopolicy(char *buf, const struct kernel_param *kp) 39 { 40 return sprintf(buf, "%s\n", nvme_iopolicy_names[iopolicy]); 41 } 42 43 module_param_call(iopolicy, nvme_set_iopolicy, nvme_get_iopolicy, 44 &iopolicy, 0644); 45 MODULE_PARM_DESC(iopolicy, 46 "Default multipath I/O policy; 'numa' (default) or 'round-robin'"); 47 48 void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys) 49 { 50 subsys->iopolicy = iopolicy; 51 } 52 53 void nvme_mpath_unfreeze(struct nvme_subsystem *subsys) 54 { 55 struct nvme_ns_head *h; 56 57 lockdep_assert_held(&subsys->lock); 58 list_for_each_entry(h, &subsys->nsheads, entry) 59 if (h->disk) 60 blk_mq_unfreeze_queue(h->disk->queue); 61 } 62 63 void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys) 64 { 65 struct nvme_ns_head *h; 66 67 lockdep_assert_held(&subsys->lock); 68 list_for_each_entry(h, &subsys->nsheads, entry) 69 if (h->disk) 70 blk_mq_freeze_queue_wait(h->disk->queue); 71 } 72 73 void nvme_mpath_start_freeze(struct nvme_subsystem *subsys) 74 { 75 struct nvme_ns_head *h; 76 77 lockdep_assert_held(&subsys->lock); 78 list_for_each_entry(h, &subsys->nsheads, entry) 79 if (h->disk) 80 blk_freeze_queue_start(h->disk->queue); 81 } 82 83 void nvme_failover_req(struct request *req) 84 { 85 struct nvme_ns *ns = req->q->queuedata; 86 u16 status = nvme_req(req)->status & 0x7ff; 87 unsigned long flags; 88 struct bio *bio; 89 90 nvme_mpath_clear_current_path(ns); 91 92 /* 93 * If we got back an ANA error, we know the controller is alive but not 94 * ready to serve this namespace. Kick of a re-read of the ANA 95 * information page, and just try any other available path for now. 96 */ 97 if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) { 98 set_bit(NVME_NS_ANA_PENDING, &ns->flags); 99 queue_work(nvme_wq, &ns->ctrl->ana_work); 100 } 101 102 spin_lock_irqsave(&ns->head->requeue_lock, flags); 103 for (bio = req->bio; bio; bio = bio->bi_next) { 104 bio_set_dev(bio, ns->head->disk->part0); 105 if (bio->bi_opf & REQ_POLLED) { 106 bio->bi_opf &= ~REQ_POLLED; 107 bio->bi_cookie = BLK_QC_T_NONE; 108 } 109 } 110 blk_steal_bios(&ns->head->requeue_list, req); 111 spin_unlock_irqrestore(&ns->head->requeue_lock, flags); 112 113 blk_mq_end_request(req, 0); 114 kblockd_schedule_work(&ns->head->requeue_work); 115 } 116 117 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl) 118 { 119 struct nvme_ns *ns; 120 121 down_read(&ctrl->namespaces_rwsem); 122 list_for_each_entry(ns, &ctrl->namespaces, list) { 123 if (!ns->head->disk) 124 continue; 125 kblockd_schedule_work(&ns->head->requeue_work); 126 if (ctrl->state == NVME_CTRL_LIVE) 127 disk_uevent(ns->head->disk, KOBJ_CHANGE); 128 } 129 up_read(&ctrl->namespaces_rwsem); 130 } 131 132 static const char *nvme_ana_state_names[] = { 133 [0] = "invalid state", 134 [NVME_ANA_OPTIMIZED] = "optimized", 135 [NVME_ANA_NONOPTIMIZED] = "non-optimized", 136 [NVME_ANA_INACCESSIBLE] = "inaccessible", 137 [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss", 138 [NVME_ANA_CHANGE] = "change", 139 }; 140 141 bool nvme_mpath_clear_current_path(struct nvme_ns *ns) 142 { 143 struct nvme_ns_head *head = ns->head; 144 bool changed = false; 145 int node; 146 147 if (!head) 148 goto out; 149 150 for_each_node(node) { 151 if (ns == rcu_access_pointer(head->current_path[node])) { 152 rcu_assign_pointer(head->current_path[node], NULL); 153 changed = true; 154 } 155 } 156 out: 157 return changed; 158 } 159 160 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl) 161 { 162 struct nvme_ns *ns; 163 164 down_read(&ctrl->namespaces_rwsem); 165 list_for_each_entry(ns, &ctrl->namespaces, list) { 166 nvme_mpath_clear_current_path(ns); 167 kblockd_schedule_work(&ns->head->requeue_work); 168 } 169 up_read(&ctrl->namespaces_rwsem); 170 } 171 172 void nvme_mpath_revalidate_paths(struct nvme_ns *ns) 173 { 174 struct nvme_ns_head *head = ns->head; 175 sector_t capacity = get_capacity(head->disk); 176 int node; 177 178 list_for_each_entry_rcu(ns, &head->list, siblings) { 179 if (capacity != get_capacity(ns->disk)) 180 clear_bit(NVME_NS_READY, &ns->flags); 181 } 182 183 for_each_node(node) 184 rcu_assign_pointer(head->current_path[node], NULL); 185 } 186 187 static bool nvme_path_is_disabled(struct nvme_ns *ns) 188 { 189 /* 190 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should 191 * still be able to complete assuming that the controller is connected. 192 * Otherwise it will fail immediately and return to the requeue list. 193 */ 194 if (ns->ctrl->state != NVME_CTRL_LIVE && 195 ns->ctrl->state != NVME_CTRL_DELETING) 196 return true; 197 if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) || 198 !test_bit(NVME_NS_READY, &ns->flags)) 199 return true; 200 return false; 201 } 202 203 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node) 204 { 205 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance; 206 struct nvme_ns *found = NULL, *fallback = NULL, *ns; 207 208 list_for_each_entry_rcu(ns, &head->list, siblings) { 209 if (nvme_path_is_disabled(ns)) 210 continue; 211 212 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA) 213 distance = node_distance(node, ns->ctrl->numa_node); 214 else 215 distance = LOCAL_DISTANCE; 216 217 switch (ns->ana_state) { 218 case NVME_ANA_OPTIMIZED: 219 if (distance < found_distance) { 220 found_distance = distance; 221 found = ns; 222 } 223 break; 224 case NVME_ANA_NONOPTIMIZED: 225 if (distance < fallback_distance) { 226 fallback_distance = distance; 227 fallback = ns; 228 } 229 break; 230 default: 231 break; 232 } 233 } 234 235 if (!found) 236 found = fallback; 237 if (found) 238 rcu_assign_pointer(head->current_path[node], found); 239 return found; 240 } 241 242 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head, 243 struct nvme_ns *ns) 244 { 245 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns, 246 siblings); 247 if (ns) 248 return ns; 249 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings); 250 } 251 252 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head, 253 int node, struct nvme_ns *old) 254 { 255 struct nvme_ns *ns, *found = NULL; 256 257 if (list_is_singular(&head->list)) { 258 if (nvme_path_is_disabled(old)) 259 return NULL; 260 return old; 261 } 262 263 for (ns = nvme_next_ns(head, old); 264 ns && ns != old; 265 ns = nvme_next_ns(head, ns)) { 266 if (nvme_path_is_disabled(ns)) 267 continue; 268 269 if (ns->ana_state == NVME_ANA_OPTIMIZED) { 270 found = ns; 271 goto out; 272 } 273 if (ns->ana_state == NVME_ANA_NONOPTIMIZED) 274 found = ns; 275 } 276 277 /* 278 * The loop above skips the current path for round-robin semantics. 279 * Fall back to the current path if either: 280 * - no other optimized path found and current is optimized, 281 * - no other usable path found and current is usable. 282 */ 283 if (!nvme_path_is_disabled(old) && 284 (old->ana_state == NVME_ANA_OPTIMIZED || 285 (!found && old->ana_state == NVME_ANA_NONOPTIMIZED))) 286 return old; 287 288 if (!found) 289 return NULL; 290 out: 291 rcu_assign_pointer(head->current_path[node], found); 292 return found; 293 } 294 295 static inline bool nvme_path_is_optimized(struct nvme_ns *ns) 296 { 297 return ns->ctrl->state == NVME_CTRL_LIVE && 298 ns->ana_state == NVME_ANA_OPTIMIZED; 299 } 300 301 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head) 302 { 303 int node = numa_node_id(); 304 struct nvme_ns *ns; 305 306 ns = srcu_dereference(head->current_path[node], &head->srcu); 307 if (unlikely(!ns)) 308 return __nvme_find_path(head, node); 309 310 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR) 311 return nvme_round_robin_path(head, node, ns); 312 if (unlikely(!nvme_path_is_optimized(ns))) 313 return __nvme_find_path(head, node); 314 return ns; 315 } 316 317 static bool nvme_available_path(struct nvme_ns_head *head) 318 { 319 struct nvme_ns *ns; 320 321 list_for_each_entry_rcu(ns, &head->list, siblings) { 322 if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags)) 323 continue; 324 switch (ns->ctrl->state) { 325 case NVME_CTRL_LIVE: 326 case NVME_CTRL_RESETTING: 327 case NVME_CTRL_CONNECTING: 328 /* fallthru */ 329 return true; 330 default: 331 break; 332 } 333 } 334 return false; 335 } 336 337 static void nvme_ns_head_submit_bio(struct bio *bio) 338 { 339 struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data; 340 struct device *dev = disk_to_dev(head->disk); 341 struct nvme_ns *ns; 342 int srcu_idx; 343 344 /* 345 * The namespace might be going away and the bio might be moved to a 346 * different queue via blk_steal_bios(), so we need to use the bio_split 347 * pool from the original queue to allocate the bvecs from. 348 */ 349 blk_queue_split(&bio); 350 351 srcu_idx = srcu_read_lock(&head->srcu); 352 ns = nvme_find_path(head); 353 if (likely(ns)) { 354 bio_set_dev(bio, ns->disk->part0); 355 bio->bi_opf |= REQ_NVME_MPATH; 356 trace_block_bio_remap(bio, disk_devt(ns->head->disk), 357 bio->bi_iter.bi_sector); 358 submit_bio_noacct(bio); 359 } else if (nvme_available_path(head)) { 360 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n"); 361 362 spin_lock_irq(&head->requeue_lock); 363 bio_list_add(&head->requeue_list, bio); 364 spin_unlock_irq(&head->requeue_lock); 365 } else { 366 dev_warn_ratelimited(dev, "no available path - failing I/O\n"); 367 368 bio_io_error(bio); 369 } 370 371 srcu_read_unlock(&head->srcu, srcu_idx); 372 } 373 374 static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode) 375 { 376 if (!nvme_tryget_ns_head(bdev->bd_disk->private_data)) 377 return -ENXIO; 378 return 0; 379 } 380 381 static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode) 382 { 383 nvme_put_ns_head(disk->private_data); 384 } 385 386 #ifdef CONFIG_BLK_DEV_ZONED 387 static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector, 388 unsigned int nr_zones, report_zones_cb cb, void *data) 389 { 390 struct nvme_ns_head *head = disk->private_data; 391 struct nvme_ns *ns; 392 int srcu_idx, ret = -EWOULDBLOCK; 393 394 srcu_idx = srcu_read_lock(&head->srcu); 395 ns = nvme_find_path(head); 396 if (ns) 397 ret = nvme_ns_report_zones(ns, sector, nr_zones, cb, data); 398 srcu_read_unlock(&head->srcu, srcu_idx); 399 return ret; 400 } 401 #else 402 #define nvme_ns_head_report_zones NULL 403 #endif /* CONFIG_BLK_DEV_ZONED */ 404 405 const struct block_device_operations nvme_ns_head_ops = { 406 .owner = THIS_MODULE, 407 .submit_bio = nvme_ns_head_submit_bio, 408 .open = nvme_ns_head_open, 409 .release = nvme_ns_head_release, 410 .ioctl = nvme_ns_head_ioctl, 411 .getgeo = nvme_getgeo, 412 .report_zones = nvme_ns_head_report_zones, 413 .pr_ops = &nvme_pr_ops, 414 }; 415 416 static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev) 417 { 418 return container_of(cdev, struct nvme_ns_head, cdev); 419 } 420 421 static int nvme_ns_head_chr_open(struct inode *inode, struct file *file) 422 { 423 if (!nvme_tryget_ns_head(cdev_to_ns_head(inode->i_cdev))) 424 return -ENXIO; 425 return 0; 426 } 427 428 static int nvme_ns_head_chr_release(struct inode *inode, struct file *file) 429 { 430 nvme_put_ns_head(cdev_to_ns_head(inode->i_cdev)); 431 return 0; 432 } 433 434 static const struct file_operations nvme_ns_head_chr_fops = { 435 .owner = THIS_MODULE, 436 .open = nvme_ns_head_chr_open, 437 .release = nvme_ns_head_chr_release, 438 .unlocked_ioctl = nvme_ns_head_chr_ioctl, 439 .compat_ioctl = compat_ptr_ioctl, 440 }; 441 442 static int nvme_add_ns_head_cdev(struct nvme_ns_head *head) 443 { 444 int ret; 445 446 head->cdev_device.parent = &head->subsys->dev; 447 ret = dev_set_name(&head->cdev_device, "ng%dn%d", 448 head->subsys->instance, head->instance); 449 if (ret) 450 return ret; 451 ret = nvme_cdev_add(&head->cdev, &head->cdev_device, 452 &nvme_ns_head_chr_fops, THIS_MODULE); 453 return ret; 454 } 455 456 static void nvme_requeue_work(struct work_struct *work) 457 { 458 struct nvme_ns_head *head = 459 container_of(work, struct nvme_ns_head, requeue_work); 460 struct bio *bio, *next; 461 462 spin_lock_irq(&head->requeue_lock); 463 next = bio_list_get(&head->requeue_list); 464 spin_unlock_irq(&head->requeue_lock); 465 466 while ((bio = next) != NULL) { 467 next = bio->bi_next; 468 bio->bi_next = NULL; 469 470 submit_bio_noacct(bio); 471 } 472 } 473 474 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head) 475 { 476 bool vwc = false; 477 478 mutex_init(&head->lock); 479 bio_list_init(&head->requeue_list); 480 spin_lock_init(&head->requeue_lock); 481 INIT_WORK(&head->requeue_work, nvme_requeue_work); 482 483 /* 484 * Add a multipath node if the subsystems supports multiple controllers. 485 * We also do this for private namespaces as the namespace sharing data could 486 * change after a rescan. 487 */ 488 if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || !multipath) 489 return 0; 490 491 head->disk = blk_alloc_disk(ctrl->numa_node); 492 if (!head->disk) 493 return -ENOMEM; 494 head->disk->fops = &nvme_ns_head_ops; 495 head->disk->private_data = head; 496 sprintf(head->disk->disk_name, "nvme%dn%d", 497 ctrl->subsys->instance, head->instance); 498 499 blk_queue_flag_set(QUEUE_FLAG_NONROT, head->disk->queue); 500 blk_queue_flag_set(QUEUE_FLAG_NOWAIT, head->disk->queue); 501 /* 502 * This assumes all controllers that refer to a namespace either 503 * support poll queues or not. That is not a strict guarantee, 504 * but if the assumption is wrong the effect is only suboptimal 505 * performance but not correctness problem. 506 */ 507 if (ctrl->tagset->nr_maps > HCTX_TYPE_POLL && 508 ctrl->tagset->map[HCTX_TYPE_POLL].nr_queues) 509 blk_queue_flag_set(QUEUE_FLAG_POLL, head->disk->queue); 510 511 /* set to a default value of 512 until the disk is validated */ 512 blk_queue_logical_block_size(head->disk->queue, 512); 513 blk_set_stacking_limits(&head->disk->queue->limits); 514 515 /* we need to propagate up the VMC settings */ 516 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) 517 vwc = true; 518 blk_queue_write_cache(head->disk->queue, vwc, vwc); 519 return 0; 520 } 521 522 static void nvme_mpath_set_live(struct nvme_ns *ns) 523 { 524 struct nvme_ns_head *head = ns->head; 525 int rc; 526 527 if (!head->disk) 528 return; 529 530 /* 531 * test_and_set_bit() is used because it is protecting against two nvme 532 * paths simultaneously calling device_add_disk() on the same namespace 533 * head. 534 */ 535 if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { 536 rc = device_add_disk(&head->subsys->dev, head->disk, 537 nvme_ns_id_attr_groups); 538 if (rc) { 539 clear_bit(NVME_NSHEAD_DISK_LIVE, &ns->flags); 540 return; 541 } 542 nvme_add_ns_head_cdev(head); 543 } 544 545 mutex_lock(&head->lock); 546 if (nvme_path_is_optimized(ns)) { 547 int node, srcu_idx; 548 549 srcu_idx = srcu_read_lock(&head->srcu); 550 for_each_node(node) 551 __nvme_find_path(head, node); 552 srcu_read_unlock(&head->srcu, srcu_idx); 553 } 554 mutex_unlock(&head->lock); 555 556 synchronize_srcu(&head->srcu); 557 kblockd_schedule_work(&head->requeue_work); 558 } 559 560 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data, 561 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *, 562 void *)) 563 { 564 void *base = ctrl->ana_log_buf; 565 size_t offset = sizeof(struct nvme_ana_rsp_hdr); 566 int error, i; 567 568 lockdep_assert_held(&ctrl->ana_lock); 569 570 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) { 571 struct nvme_ana_group_desc *desc = base + offset; 572 u32 nr_nsids; 573 size_t nsid_buf_size; 574 575 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc))) 576 return -EINVAL; 577 578 nr_nsids = le32_to_cpu(desc->nnsids); 579 nsid_buf_size = flex_array_size(desc, nsids, nr_nsids); 580 581 if (WARN_ON_ONCE(desc->grpid == 0)) 582 return -EINVAL; 583 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax)) 584 return -EINVAL; 585 if (WARN_ON_ONCE(desc->state == 0)) 586 return -EINVAL; 587 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE)) 588 return -EINVAL; 589 590 offset += sizeof(*desc); 591 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size)) 592 return -EINVAL; 593 594 error = cb(ctrl, desc, data); 595 if (error) 596 return error; 597 598 offset += nsid_buf_size; 599 } 600 601 return 0; 602 } 603 604 static inline bool nvme_state_is_live(enum nvme_ana_state state) 605 { 606 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED; 607 } 608 609 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc, 610 struct nvme_ns *ns) 611 { 612 ns->ana_grpid = le32_to_cpu(desc->grpid); 613 ns->ana_state = desc->state; 614 clear_bit(NVME_NS_ANA_PENDING, &ns->flags); 615 616 if (nvme_state_is_live(ns->ana_state)) 617 nvme_mpath_set_live(ns); 618 } 619 620 static int nvme_update_ana_state(struct nvme_ctrl *ctrl, 621 struct nvme_ana_group_desc *desc, void *data) 622 { 623 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0; 624 unsigned *nr_change_groups = data; 625 struct nvme_ns *ns; 626 627 dev_dbg(ctrl->device, "ANA group %d: %s.\n", 628 le32_to_cpu(desc->grpid), 629 nvme_ana_state_names[desc->state]); 630 631 if (desc->state == NVME_ANA_CHANGE) 632 (*nr_change_groups)++; 633 634 if (!nr_nsids) 635 return 0; 636 637 down_read(&ctrl->namespaces_rwsem); 638 list_for_each_entry(ns, &ctrl->namespaces, list) { 639 unsigned nsid; 640 again: 641 nsid = le32_to_cpu(desc->nsids[n]); 642 if (ns->head->ns_id < nsid) 643 continue; 644 if (ns->head->ns_id == nsid) 645 nvme_update_ns_ana_state(desc, ns); 646 if (++n == nr_nsids) 647 break; 648 if (ns->head->ns_id > nsid) 649 goto again; 650 } 651 up_read(&ctrl->namespaces_rwsem); 652 return 0; 653 } 654 655 static int nvme_read_ana_log(struct nvme_ctrl *ctrl) 656 { 657 u32 nr_change_groups = 0; 658 int error; 659 660 mutex_lock(&ctrl->ana_lock); 661 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM, 662 ctrl->ana_log_buf, ctrl->ana_log_size, 0); 663 if (error) { 664 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error); 665 goto out_unlock; 666 } 667 668 error = nvme_parse_ana_log(ctrl, &nr_change_groups, 669 nvme_update_ana_state); 670 if (error) 671 goto out_unlock; 672 673 /* 674 * In theory we should have an ANATT timer per group as they might enter 675 * the change state at different times. But that is a lot of overhead 676 * just to protect against a target that keeps entering new changes 677 * states while never finishing previous ones. But we'll still 678 * eventually time out once all groups are in change state, so this 679 * isn't a big deal. 680 * 681 * We also double the ANATT value to provide some slack for transports 682 * or AEN processing overhead. 683 */ 684 if (nr_change_groups) 685 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies); 686 else 687 del_timer_sync(&ctrl->anatt_timer); 688 out_unlock: 689 mutex_unlock(&ctrl->ana_lock); 690 return error; 691 } 692 693 static void nvme_ana_work(struct work_struct *work) 694 { 695 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work); 696 697 if (ctrl->state != NVME_CTRL_LIVE) 698 return; 699 700 nvme_read_ana_log(ctrl); 701 } 702 703 static void nvme_anatt_timeout(struct timer_list *t) 704 { 705 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer); 706 707 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n"); 708 nvme_reset_ctrl(ctrl); 709 } 710 711 void nvme_mpath_stop(struct nvme_ctrl *ctrl) 712 { 713 if (!nvme_ctrl_use_ana(ctrl)) 714 return; 715 del_timer_sync(&ctrl->anatt_timer); 716 cancel_work_sync(&ctrl->ana_work); 717 } 718 719 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \ 720 struct device_attribute subsys_attr_##_name = \ 721 __ATTR(_name, _mode, _show, _store) 722 723 static ssize_t nvme_subsys_iopolicy_show(struct device *dev, 724 struct device_attribute *attr, char *buf) 725 { 726 struct nvme_subsystem *subsys = 727 container_of(dev, struct nvme_subsystem, dev); 728 729 return sysfs_emit(buf, "%s\n", 730 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]); 731 } 732 733 static ssize_t nvme_subsys_iopolicy_store(struct device *dev, 734 struct device_attribute *attr, const char *buf, size_t count) 735 { 736 struct nvme_subsystem *subsys = 737 container_of(dev, struct nvme_subsystem, dev); 738 int i; 739 740 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) { 741 if (sysfs_streq(buf, nvme_iopolicy_names[i])) { 742 WRITE_ONCE(subsys->iopolicy, i); 743 return count; 744 } 745 } 746 747 return -EINVAL; 748 } 749 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR, 750 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store); 751 752 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr, 753 char *buf) 754 { 755 return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid); 756 } 757 DEVICE_ATTR_RO(ana_grpid); 758 759 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr, 760 char *buf) 761 { 762 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 763 764 return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]); 765 } 766 DEVICE_ATTR_RO(ana_state); 767 768 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl, 769 struct nvme_ana_group_desc *desc, void *data) 770 { 771 struct nvme_ana_group_desc *dst = data; 772 773 if (desc->grpid != dst->grpid) 774 return 0; 775 776 *dst = *desc; 777 return -ENXIO; /* just break out of the loop */ 778 } 779 780 void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id) 781 { 782 if (nvme_ctrl_use_ana(ns->ctrl)) { 783 struct nvme_ana_group_desc desc = { 784 .grpid = id->anagrpid, 785 .state = 0, 786 }; 787 788 mutex_lock(&ns->ctrl->ana_lock); 789 ns->ana_grpid = le32_to_cpu(id->anagrpid); 790 nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc); 791 mutex_unlock(&ns->ctrl->ana_lock); 792 if (desc.state) { 793 /* found the group desc: update */ 794 nvme_update_ns_ana_state(&desc, ns); 795 } else { 796 /* group desc not found: trigger a re-read */ 797 set_bit(NVME_NS_ANA_PENDING, &ns->flags); 798 queue_work(nvme_wq, &ns->ctrl->ana_work); 799 } 800 } else { 801 ns->ana_state = NVME_ANA_OPTIMIZED; 802 nvme_mpath_set_live(ns); 803 } 804 805 if (blk_queue_stable_writes(ns->queue) && ns->head->disk) 806 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, 807 ns->head->disk->queue); 808 #ifdef CONFIG_BLK_DEV_ZONED 809 if (blk_queue_is_zoned(ns->queue) && ns->head->disk) 810 ns->head->disk->queue->nr_zones = ns->queue->nr_zones; 811 #endif 812 } 813 814 void nvme_mpath_shutdown_disk(struct nvme_ns_head *head) 815 { 816 if (!head->disk) 817 return; 818 kblockd_schedule_work(&head->requeue_work); 819 if (test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { 820 nvme_cdev_del(&head->cdev, &head->cdev_device); 821 del_gendisk(head->disk); 822 } 823 } 824 825 void nvme_mpath_remove_disk(struct nvme_ns_head *head) 826 { 827 if (!head->disk) 828 return; 829 blk_mark_disk_dead(head->disk); 830 /* make sure all pending bios are cleaned up */ 831 kblockd_schedule_work(&head->requeue_work); 832 flush_work(&head->requeue_work); 833 blk_cleanup_disk(head->disk); 834 } 835 836 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl) 837 { 838 mutex_init(&ctrl->ana_lock); 839 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0); 840 INIT_WORK(&ctrl->ana_work, nvme_ana_work); 841 } 842 843 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) 844 { 845 size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT; 846 size_t ana_log_size; 847 int error = 0; 848 849 /* check if multipath is enabled and we have the capability */ 850 if (!multipath || !ctrl->subsys || 851 !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA)) 852 return 0; 853 854 if (!ctrl->max_namespaces || 855 ctrl->max_namespaces > le32_to_cpu(id->nn)) { 856 dev_err(ctrl->device, 857 "Invalid MNAN value %u\n", ctrl->max_namespaces); 858 return -EINVAL; 859 } 860 861 ctrl->anacap = id->anacap; 862 ctrl->anatt = id->anatt; 863 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid); 864 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax); 865 866 ana_log_size = sizeof(struct nvme_ana_rsp_hdr) + 867 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) + 868 ctrl->max_namespaces * sizeof(__le32); 869 if (ana_log_size > max_transfer_size) { 870 dev_err(ctrl->device, 871 "ANA log page size (%zd) larger than MDTS (%zd).\n", 872 ana_log_size, max_transfer_size); 873 dev_err(ctrl->device, "disabling ANA support.\n"); 874 goto out_uninit; 875 } 876 if (ana_log_size > ctrl->ana_log_size) { 877 nvme_mpath_stop(ctrl); 878 nvme_mpath_uninit(ctrl); 879 ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL); 880 if (!ctrl->ana_log_buf) 881 return -ENOMEM; 882 } 883 ctrl->ana_log_size = ana_log_size; 884 error = nvme_read_ana_log(ctrl); 885 if (error) 886 goto out_uninit; 887 return 0; 888 889 out_uninit: 890 nvme_mpath_uninit(ctrl); 891 return error; 892 } 893 894 void nvme_mpath_uninit(struct nvme_ctrl *ctrl) 895 { 896 kvfree(ctrl->ana_log_buf); 897 ctrl->ana_log_buf = NULL; 898 ctrl->ana_log_size = 0; 899 } 900