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