1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Common code for the NVMe target. 4 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 5 */ 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 #include <linux/hex.h> 8 #include <linux/module.h> 9 #include <linux/random.h> 10 #include <linux/rculist.h> 11 #include <linux/pci-p2pdma.h> 12 #include <linux/scatterlist.h> 13 14 #include <generated/utsrelease.h> 15 16 #define CREATE_TRACE_POINTS 17 #include "trace.h" 18 19 #include "nvmet.h" 20 #include "debugfs.h" 21 22 struct kmem_cache *nvmet_bvec_cache; 23 struct workqueue_struct *buffered_io_wq; 24 struct workqueue_struct *zbd_wq; 25 static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX]; 26 static DEFINE_IDA(cntlid_ida); 27 28 struct workqueue_struct *nvmet_wq; 29 EXPORT_SYMBOL_GPL(nvmet_wq); 30 struct workqueue_struct *nvmet_aen_wq; 31 EXPORT_SYMBOL_GPL(nvmet_aen_wq); 32 33 /* 34 * This read/write semaphore is used to synchronize access to configuration 35 * information on a target system that will result in discovery log page 36 * information change for at least one host. 37 * The full list of resources to protected by this semaphore is: 38 * 39 * - subsystems list 40 * - per-subsystem allowed hosts list 41 * - allow_any_host subsystem attribute 42 * - nvmet_genctr 43 * - the nvmet_transports array 44 * 45 * When updating any of those lists/structures write lock should be obtained, 46 * while when reading (populating discovery log page or checking host-subsystem 47 * link) read lock is obtained to allow concurrent reads. 48 */ 49 DECLARE_RWSEM(nvmet_config_sem); 50 51 u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1]; 52 u64 nvmet_ana_chgcnt; 53 DECLARE_RWSEM(nvmet_ana_sem); 54 55 inline u16 errno_to_nvme_status(struct nvmet_req *req, int errno) 56 { 57 switch (errno) { 58 case 0: 59 return NVME_SC_SUCCESS; 60 case -ENOSPC: 61 req->error_loc = offsetof(struct nvme_rw_command, length); 62 return NVME_SC_CAP_EXCEEDED | NVME_STATUS_DNR; 63 case -EREMOTEIO: 64 req->error_loc = offsetof(struct nvme_rw_command, slba); 65 return NVME_SC_LBA_RANGE | NVME_STATUS_DNR; 66 case -EOPNOTSUPP: 67 req->error_loc = offsetof(struct nvme_common_command, opcode); 68 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR; 69 case -ENODATA: 70 req->error_loc = offsetof(struct nvme_rw_command, nsid); 71 return NVME_SC_ACCESS_DENIED; 72 case -EIO: 73 fallthrough; 74 default: 75 req->error_loc = offsetof(struct nvme_common_command, opcode); 76 return NVME_SC_INTERNAL | NVME_STATUS_DNR; 77 } 78 } 79 80 u16 nvmet_report_invalid_opcode(struct nvmet_req *req) 81 { 82 pr_debug("unhandled cmd %d on qid %d\n", req->cmd->common.opcode, 83 req->sq->qid); 84 85 req->error_loc = offsetof(struct nvme_common_command, opcode); 86 return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR; 87 } 88 89 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port, 90 const char *subsysnqn); 91 92 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf, 93 size_t len) 94 { 95 if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len) { 96 req->error_loc = offsetof(struct nvme_common_command, dptr); 97 return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR; 98 } 99 return 0; 100 } 101 102 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len) 103 { 104 if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len) { 105 req->error_loc = offsetof(struct nvme_common_command, dptr); 106 return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR; 107 } 108 return 0; 109 } 110 111 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len) 112 { 113 if (sg_zero_buffer(req->sg, req->sg_cnt, len, off) != len) { 114 req->error_loc = offsetof(struct nvme_common_command, dptr); 115 return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR; 116 } 117 return 0; 118 } 119 120 static u32 nvmet_max_nsid(struct nvmet_subsys *subsys) 121 { 122 struct nvmet_ns *cur; 123 unsigned long idx; 124 u32 nsid = 0; 125 126 nvmet_for_each_enabled_ns(&subsys->namespaces, idx, cur) 127 nsid = cur->nsid; 128 129 return nsid; 130 } 131 132 static u32 nvmet_async_event_result(struct nvmet_async_event *aen) 133 { 134 return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16); 135 } 136 137 static void nvmet_async_events_failall(struct nvmet_ctrl *ctrl) 138 { 139 struct nvmet_req *req; 140 141 mutex_lock(&ctrl->lock); 142 while (ctrl->nr_async_event_cmds) { 143 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds]; 144 mutex_unlock(&ctrl->lock); 145 nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_STATUS_DNR); 146 mutex_lock(&ctrl->lock); 147 } 148 mutex_unlock(&ctrl->lock); 149 } 150 151 static void nvmet_async_events_process(struct nvmet_ctrl *ctrl) 152 { 153 struct nvmet_async_event *aen; 154 struct nvmet_req *req; 155 156 mutex_lock(&ctrl->lock); 157 while (ctrl->nr_async_event_cmds && !list_empty(&ctrl->async_events)) { 158 aen = list_first_entry(&ctrl->async_events, 159 struct nvmet_async_event, entry); 160 req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds]; 161 nvmet_set_result(req, nvmet_async_event_result(aen)); 162 163 list_del(&aen->entry); 164 kfree(aen); 165 166 mutex_unlock(&ctrl->lock); 167 trace_nvmet_async_event(ctrl, req->cqe->result.u32); 168 nvmet_req_complete(req, 0); 169 mutex_lock(&ctrl->lock); 170 } 171 mutex_unlock(&ctrl->lock); 172 } 173 174 static void nvmet_async_events_free(struct nvmet_ctrl *ctrl) 175 { 176 struct nvmet_async_event *aen, *tmp; 177 178 mutex_lock(&ctrl->lock); 179 list_for_each_entry_safe(aen, tmp, &ctrl->async_events, entry) { 180 list_del(&aen->entry); 181 kfree(aen); 182 } 183 mutex_unlock(&ctrl->lock); 184 } 185 186 static void nvmet_async_event_work(struct work_struct *work) 187 { 188 struct nvmet_ctrl *ctrl = 189 container_of(work, struct nvmet_ctrl, async_event_work); 190 191 nvmet_async_events_process(ctrl); 192 } 193 194 void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type, 195 u8 event_info, u8 log_page) 196 { 197 struct nvmet_async_event *aen; 198 199 aen = kmalloc_obj(*aen); 200 if (!aen) 201 return; 202 203 aen->event_type = event_type; 204 aen->event_info = event_info; 205 aen->log_page = log_page; 206 207 mutex_lock(&ctrl->lock); 208 list_add_tail(&aen->entry, &ctrl->async_events); 209 mutex_unlock(&ctrl->lock); 210 211 queue_work(nvmet_aen_wq, &ctrl->async_event_work); 212 } 213 214 static void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid) 215 { 216 u32 i; 217 218 mutex_lock(&ctrl->lock); 219 if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES) 220 goto out_unlock; 221 222 for (i = 0; i < ctrl->nr_changed_ns; i++) { 223 if (ctrl->changed_ns_list[i] == nsid) 224 goto out_unlock; 225 } 226 227 if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) { 228 ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff); 229 ctrl->nr_changed_ns = U32_MAX; 230 goto out_unlock; 231 } 232 233 ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid; 234 out_unlock: 235 mutex_unlock(&ctrl->lock); 236 } 237 238 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid) 239 { 240 struct nvmet_ctrl *ctrl; 241 242 lockdep_assert_held(&subsys->lock); 243 244 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { 245 nvmet_add_to_changed_ns_log(ctrl, cpu_to_le32(nsid)); 246 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_NS_ATTR)) 247 continue; 248 nvmet_add_async_event(ctrl, NVME_AER_NOTICE, 249 NVME_AER_NOTICE_NS_CHANGED, 250 NVME_LOG_CHANGED_NS); 251 } 252 } 253 254 void nvmet_send_ana_event(struct nvmet_subsys *subsys, 255 struct nvmet_port *port) 256 { 257 struct nvmet_ctrl *ctrl; 258 259 mutex_lock(&subsys->lock); 260 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { 261 if (port && ctrl->port != port) 262 continue; 263 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_ANA_CHANGE)) 264 continue; 265 nvmet_add_async_event(ctrl, NVME_AER_NOTICE, 266 NVME_AER_NOTICE_ANA, NVME_LOG_ANA); 267 } 268 mutex_unlock(&subsys->lock); 269 } 270 271 void nvmet_port_send_ana_event(struct nvmet_port *port) 272 { 273 struct nvmet_subsys_link *p; 274 275 down_read(&nvmet_config_sem); 276 list_for_each_entry(p, &port->subsystems, entry) 277 nvmet_send_ana_event(p->subsys, port); 278 up_read(&nvmet_config_sem); 279 } 280 281 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops) 282 { 283 int ret = 0; 284 285 down_write(&nvmet_config_sem); 286 if (nvmet_transports[ops->type]) 287 ret = -EINVAL; 288 else 289 nvmet_transports[ops->type] = ops; 290 up_write(&nvmet_config_sem); 291 292 return ret; 293 } 294 EXPORT_SYMBOL_GPL(nvmet_register_transport); 295 296 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops) 297 { 298 down_write(&nvmet_config_sem); 299 nvmet_transports[ops->type] = NULL; 300 up_write(&nvmet_config_sem); 301 } 302 EXPORT_SYMBOL_GPL(nvmet_unregister_transport); 303 304 void nvmet_port_del_ctrls(struct nvmet_port *port, struct nvmet_subsys *subsys) 305 { 306 struct nvmet_ctrl *ctrl; 307 308 mutex_lock(&subsys->lock); 309 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { 310 if (ctrl->port == port) 311 ctrl->ops->delete_ctrl(ctrl); 312 } 313 mutex_unlock(&subsys->lock); 314 } 315 316 int nvmet_enable_port(struct nvmet_port *port) 317 { 318 const struct nvmet_fabrics_ops *ops; 319 int ret; 320 321 lockdep_assert_held(&nvmet_config_sem); 322 323 if (port->disc_addr.trtype == NVMF_TRTYPE_MAX) 324 return -EINVAL; 325 326 ops = nvmet_transports[port->disc_addr.trtype]; 327 if (!ops) { 328 up_write(&nvmet_config_sem); 329 request_module("nvmet-transport-%d", port->disc_addr.trtype); 330 down_write(&nvmet_config_sem); 331 ops = nvmet_transports[port->disc_addr.trtype]; 332 if (!ops) { 333 pr_err("transport type %d not supported\n", 334 port->disc_addr.trtype); 335 return -EINVAL; 336 } 337 } 338 339 if (!try_module_get(ops->owner)) 340 return -EINVAL; 341 342 /* 343 * If the user requested PI support and the transport isn't pi capable, 344 * don't enable the port. 345 */ 346 if (port->pi_enable && !(ops->flags & NVMF_METADATA_SUPPORTED)) { 347 pr_err("T10-PI is not supported by transport type %d\n", 348 port->disc_addr.trtype); 349 ret = -EINVAL; 350 goto out_put; 351 } 352 353 ret = ops->add_port(port); 354 if (ret) 355 goto out_put; 356 357 /* If the transport didn't set inline_data_size, then disable it. */ 358 if (port->inline_data_size < 0) 359 port->inline_data_size = 0; 360 361 /* 362 * If the transport didn't set the max_queue_size properly, then clamp 363 * it to the target limits. Also set default values in case the 364 * transport didn't set it at all. 365 */ 366 if (port->max_queue_size < 0) 367 port->max_queue_size = NVMET_MAX_QUEUE_SIZE; 368 else 369 port->max_queue_size = clamp_t(int, port->max_queue_size, 370 NVMET_MIN_QUEUE_SIZE, 371 NVMET_MAX_QUEUE_SIZE); 372 373 /* 374 * If the transport didn't set the mdts properly, then clamp it to the 375 * target limits. Also set default values in case the transport didn't 376 * set it at all. 377 */ 378 if (port->mdts < 0 || port->mdts > NVMET_MAX_MDTS) 379 port->mdts = 0; 380 381 port->enabled = true; 382 port->tr_ops = ops; 383 return 0; 384 385 out_put: 386 module_put(ops->owner); 387 return ret; 388 } 389 390 void nvmet_disable_port(struct nvmet_port *port) 391 { 392 const struct nvmet_fabrics_ops *ops; 393 394 lockdep_assert_held(&nvmet_config_sem); 395 396 if (!port->enabled) 397 return; 398 399 ops = port->tr_ops; 400 port->enabled = false; 401 ops->remove_port(port); 402 port->tr_ops = NULL; 403 module_put(ops->owner); 404 } 405 406 static void nvmet_keep_alive_timer(struct work_struct *work) 407 { 408 struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work), 409 struct nvmet_ctrl, ka_work); 410 bool reset_tbkas = ctrl->reset_tbkas; 411 412 ctrl->reset_tbkas = false; 413 if (reset_tbkas) { 414 pr_debug("ctrl %d reschedule traffic based keep-alive timer\n", 415 ctrl->cntlid); 416 queue_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ); 417 return; 418 } 419 420 pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n", 421 ctrl->cntlid, ctrl->kato); 422 423 nvmet_ctrl_fatal_error(ctrl); 424 } 425 426 void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl) 427 { 428 if (unlikely(ctrl->kato == 0)) 429 return; 430 431 pr_debug("ctrl %d start keep-alive timer for %d secs\n", 432 ctrl->cntlid, ctrl->kato); 433 434 queue_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ); 435 } 436 437 void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl) 438 { 439 if (unlikely(ctrl->kato == 0)) 440 return; 441 442 pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid); 443 444 cancel_delayed_work_sync(&ctrl->ka_work); 445 } 446 447 u16 nvmet_req_find_ns(struct nvmet_req *req) 448 { 449 u32 nsid = le32_to_cpu(req->cmd->common.nsid); 450 struct nvmet_subsys *subsys = nvmet_req_subsys(req); 451 u16 status = NVME_SC_SUCCESS; 452 453 rcu_read_lock(); 454 req->ns = xa_load(&subsys->namespaces, nsid); 455 if (unlikely(!req->ns) || 456 !test_bit(NVMET_NS_IO_LIVE, &req->ns->flags) || 457 !percpu_ref_tryget_live_rcu(&req->ns->ref)) { 458 req->error_loc = offsetof(struct nvme_common_command, nsid); 459 if (!req->ns) { /* ns doesn't exist! */ 460 status = NVME_SC_INVALID_NS | NVME_STATUS_DNR; 461 goto unlock; 462 } 463 464 /* ns exists but it's disabled */ 465 req->ns = NULL; 466 status = NVME_SC_INTERNAL_PATH_ERROR; 467 } 468 unlock: 469 rcu_read_unlock(); 470 471 return status; 472 } 473 474 static void nvmet_destroy_namespace(struct percpu_ref *ref) 475 { 476 struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref); 477 478 complete(&ns->disable_done); 479 } 480 481 void nvmet_put_namespace(struct nvmet_ns *ns) 482 { 483 percpu_ref_put(&ns->ref); 484 } 485 486 static void nvmet_ns_dev_disable(struct nvmet_ns *ns) 487 { 488 nvmet_bdev_ns_disable(ns); 489 nvmet_file_ns_disable(ns); 490 } 491 492 static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns) 493 { 494 int ret; 495 struct pci_dev *p2p_dev; 496 497 if (!ns->use_p2pmem) 498 return 0; 499 500 if (!ns->bdev) { 501 pr_err("peer-to-peer DMA is not supported by non-block device namespaces\n"); 502 return -EINVAL; 503 } 504 505 if (!blk_queue_pci_p2pdma(ns->bdev->bd_disk->queue)) { 506 pr_err("peer-to-peer DMA is not supported by the driver of %s\n", 507 ns->device_path); 508 return -EINVAL; 509 } 510 511 if (ns->p2p_dev) { 512 ret = pci_p2pdma_distance(ns->p2p_dev, nvmet_ns_dev(ns), true); 513 if (ret < 0) 514 return -EINVAL; 515 } else { 516 /* 517 * Right now we just check that there is p2pmem available so 518 * we can report an error to the user right away if there 519 * is not. We'll find the actual device to use once we 520 * setup the controller when the port's device is available. 521 */ 522 523 p2p_dev = pci_p2pmem_find(nvmet_ns_dev(ns)); 524 if (!p2p_dev) { 525 pr_err("no peer-to-peer memory is available for %s\n", 526 ns->device_path); 527 return -EINVAL; 528 } 529 530 pci_dev_put(p2p_dev); 531 } 532 533 return 0; 534 } 535 536 static void nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl *ctrl, 537 struct nvmet_ns *ns) 538 { 539 struct device *clients[2]; 540 struct pci_dev *p2p_dev; 541 int ret; 542 543 lockdep_assert_held(&ctrl->subsys->lock); 544 545 if (!ctrl->p2p_client || !ns->use_p2pmem) 546 return; 547 548 if (ns->p2p_dev) { 549 ret = pci_p2pdma_distance(ns->p2p_dev, ctrl->p2p_client, true); 550 if (ret < 0) 551 return; 552 553 p2p_dev = pci_dev_get(ns->p2p_dev); 554 } else { 555 clients[0] = ctrl->p2p_client; 556 clients[1] = nvmet_ns_dev(ns); 557 558 p2p_dev = pci_p2pmem_find_many(clients, ARRAY_SIZE(clients)); 559 if (!p2p_dev) { 560 pr_err("no peer-to-peer memory is available that's supported by %s and %s\n", 561 dev_name(ctrl->p2p_client), ns->device_path); 562 return; 563 } 564 } 565 566 ret = radix_tree_insert(&ctrl->p2p_ns_map, ns->nsid, p2p_dev); 567 if (ret < 0) 568 pci_dev_put(p2p_dev); 569 570 pr_info("using p2pmem on %s for nsid %u\n", pci_name(p2p_dev), 571 ns->nsid); 572 } 573 574 bool nvmet_ns_revalidate(struct nvmet_ns *ns) 575 { 576 loff_t oldsize = ns->size; 577 578 if (ns->bdev) 579 nvmet_bdev_ns_revalidate(ns); 580 else 581 nvmet_file_ns_revalidate(ns); 582 583 return oldsize != ns->size; 584 } 585 586 int nvmet_ns_enable(struct nvmet_ns *ns) 587 { 588 struct nvmet_subsys *subsys = ns->subsys; 589 struct nvmet_ctrl *ctrl; 590 int ret; 591 592 mutex_lock(&subsys->lock); 593 ret = 0; 594 595 if (nvmet_is_passthru_subsys(subsys)) { 596 pr_info("cannot enable both passthru and regular namespaces for a single subsystem"); 597 goto out_unlock; 598 } 599 600 if (ns->enabled) 601 goto out_unlock; 602 603 if (!ns->device_path) { 604 ret = -EINVAL; 605 goto out_unlock; 606 } 607 608 ret = nvmet_bdev_ns_enable(ns); 609 if (ret == -ENOTBLK) 610 ret = nvmet_file_ns_enable(ns); 611 if (ret) 612 goto out_unlock; 613 614 ret = nvmet_p2pmem_ns_enable(ns); 615 if (ret) 616 goto out_dev_disable; 617 618 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) 619 nvmet_p2pmem_ns_add_p2p(ctrl, ns); 620 621 if (ns->pr.enable) { 622 ret = nvmet_pr_init_ns(ns); 623 if (ret) 624 goto out_dev_put; 625 } 626 627 ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace, 0, GFP_KERNEL); 628 if (ret) 629 goto out_pr_exit; 630 631 nvmet_ns_changed(subsys, ns->nsid); 632 ns->enabled = true; 633 xa_set_mark(&subsys->namespaces, ns->nsid, NVMET_NS_ENABLED); 634 nvmet_debugfs_ns_setup(ns); 635 set_bit(NVMET_NS_IO_LIVE, &ns->flags); 636 ret = 0; 637 out_unlock: 638 mutex_unlock(&subsys->lock); 639 return ret; 640 out_pr_exit: 641 if (ns->pr.enable) 642 nvmet_pr_exit_ns(ns); 643 out_dev_put: 644 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) 645 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid)); 646 out_dev_disable: 647 nvmet_ns_dev_disable(ns); 648 goto out_unlock; 649 } 650 651 void nvmet_ns_disable(struct nvmet_ns *ns) 652 { 653 struct nvmet_subsys *subsys = ns->subsys; 654 struct nvmet_ctrl *ctrl; 655 656 if (!test_and_clear_bit(NVMET_NS_IO_LIVE, &ns->flags)) 657 return; 658 659 mutex_lock(&subsys->lock); 660 661 xa_clear_mark(&subsys->namespaces, ns->nsid, NVMET_NS_ENABLED); 662 nvmet_debugfs_ns_free(ns); 663 664 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) 665 pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid)); 666 667 mutex_unlock(&subsys->lock); 668 669 /* 670 * Now that we removed the namespaces from the lookup list, we 671 * can kill the per_cpu ref and wait for any remaining references 672 * to be dropped. Rely on that wait for anyone only using the 673 * namespace under rcu_read_lock() too: the ref can't drop to zero 674 * before an RCU grace period has passed since the kill. This only 675 * holds as long as nothing switches the ref to atomic mode before 676 * the kill; bring back synchronize_rcu() here if that changes. 677 * Note that we can't use call_rcu here as we need to ensure the 678 * namespaces have been fully destroyed before unloading the module. 679 */ 680 percpu_ref_kill(&ns->ref); 681 wait_for_completion(&ns->disable_done); 682 percpu_ref_exit(&ns->ref); 683 684 if (ns->pr.enable) 685 nvmet_pr_exit_ns(ns); 686 687 mutex_lock(&subsys->lock); 688 nvmet_ns_changed(subsys, ns->nsid); 689 nvmet_ns_dev_disable(ns); 690 ns->enabled = false; 691 mutex_unlock(&subsys->lock); 692 } 693 694 void nvmet_ns_free(struct nvmet_ns *ns) 695 { 696 struct nvmet_subsys *subsys = ns->subsys; 697 698 nvmet_ns_disable(ns); 699 700 mutex_lock(&subsys->lock); 701 702 xa_erase(&subsys->namespaces, ns->nsid); 703 if (ns->nsid == subsys->max_nsid) 704 subsys->max_nsid = nvmet_max_nsid(subsys); 705 706 subsys->nr_namespaces--; 707 mutex_unlock(&subsys->lock); 708 709 down_write(&nvmet_ana_sem); 710 nvmet_ana_group_enabled[ns->anagrpid]--; 711 up_write(&nvmet_ana_sem); 712 713 kfree(ns->device_path); 714 kfree(ns); 715 } 716 717 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid) 718 { 719 struct nvmet_ns *ns; 720 721 mutex_lock(&subsys->lock); 722 723 if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES) 724 goto out_unlock; 725 726 ns = kzalloc_obj(*ns); 727 if (!ns) 728 goto out_unlock; 729 730 init_completion(&ns->disable_done); 731 732 ns->nsid = nsid; 733 ns->subsys = subsys; 734 735 if (ns->nsid > subsys->max_nsid) 736 subsys->max_nsid = nsid; 737 738 if (xa_insert(&subsys->namespaces, ns->nsid, ns, GFP_KERNEL)) 739 goto out_exit; 740 741 subsys->nr_namespaces++; 742 743 mutex_unlock(&subsys->lock); 744 745 down_write(&nvmet_ana_sem); 746 ns->anagrpid = NVMET_DEFAULT_ANA_GRPID; 747 nvmet_ana_group_enabled[ns->anagrpid]++; 748 up_write(&nvmet_ana_sem); 749 750 uuid_gen(&ns->uuid); 751 ns->buffered_io = false; 752 ns->csi = NVME_CSI_NVM; 753 754 return ns; 755 out_exit: 756 subsys->max_nsid = nvmet_max_nsid(subsys); 757 kfree(ns); 758 out_unlock: 759 mutex_unlock(&subsys->lock); 760 return NULL; 761 } 762 763 static void nvmet_update_sq_head(struct nvmet_req *req) 764 { 765 if (req->sq->size) { 766 u32 old_sqhd, new_sqhd; 767 768 old_sqhd = READ_ONCE(req->sq->sqhd); 769 do { 770 new_sqhd = (old_sqhd + 1) % req->sq->size; 771 } while (!try_cmpxchg(&req->sq->sqhd, &old_sqhd, new_sqhd)); 772 } 773 req->cqe->sq_head = cpu_to_le16(req->sq->sqhd & 0x0000FFFF); 774 } 775 776 static void nvmet_set_error(struct nvmet_req *req, u16 status) 777 { 778 struct nvmet_ctrl *ctrl = req->sq->ctrl; 779 struct nvme_error_slot *new_error_slot; 780 unsigned long flags; 781 782 req->cqe->status = cpu_to_le16(status << 1); 783 784 if (!ctrl || req->error_loc == NVMET_NO_ERROR_LOC) 785 return; 786 787 spin_lock_irqsave(&ctrl->error_lock, flags); 788 ctrl->err_counter++; 789 new_error_slot = 790 &ctrl->slots[ctrl->err_counter % NVMET_ERROR_LOG_SLOTS]; 791 792 new_error_slot->error_count = cpu_to_le64(ctrl->err_counter); 793 new_error_slot->sqid = cpu_to_le16(req->sq->qid); 794 new_error_slot->cmdid = cpu_to_le16(req->cmd->common.command_id); 795 new_error_slot->status_field = cpu_to_le16(status << 1); 796 new_error_slot->param_error_location = cpu_to_le16(req->error_loc); 797 new_error_slot->lba = cpu_to_le64(req->error_slba); 798 new_error_slot->nsid = req->cmd->common.nsid; 799 spin_unlock_irqrestore(&ctrl->error_lock, flags); 800 801 /* set the more bit for this request */ 802 req->cqe->status |= cpu_to_le16(1 << 14); 803 } 804 805 static void __nvmet_req_complete(struct nvmet_req *req, u16 status) 806 { 807 struct nvmet_ns *ns = req->ns; 808 struct nvmet_pr_per_ctrl_ref *pc_ref = req->pc_ref; 809 struct nvmet_passthru *passthru = nvmet_passthru_ref_held(req); 810 811 if (!req->sq->sqhd_disabled) 812 nvmet_update_sq_head(req); 813 req->cqe->sq_id = cpu_to_le16(req->sq->qid); 814 req->cqe->command_id = req->cmd->common.command_id; 815 816 if (unlikely(status)) 817 nvmet_set_error(req, status); 818 819 trace_nvmet_req_complete(req); 820 821 req->ops->queue_response(req); 822 823 if (pc_ref) 824 nvmet_pr_put_ns_pc_ref(pc_ref); 825 if (ns) 826 nvmet_put_namespace(ns); 827 828 if (passthru) 829 nvmet_put_passthru_ref(passthru); 830 } 831 832 void nvmet_req_complete(struct nvmet_req *req, u16 status) 833 { 834 struct nvmet_sq *sq = req->sq; 835 836 __nvmet_req_complete(req, status); 837 percpu_ref_put(&sq->ref); 838 } 839 EXPORT_SYMBOL_GPL(nvmet_req_complete); 840 841 void nvmet_cq_init(struct nvmet_cq *cq) 842 { 843 refcount_set(&cq->ref, 1); 844 } 845 EXPORT_SYMBOL_GPL(nvmet_cq_init); 846 847 bool nvmet_cq_get(struct nvmet_cq *cq) 848 { 849 return refcount_inc_not_zero(&cq->ref); 850 } 851 EXPORT_SYMBOL_GPL(nvmet_cq_get); 852 853 void nvmet_cq_put(struct nvmet_cq *cq) 854 { 855 if (refcount_dec_and_test(&cq->ref)) 856 nvmet_cq_destroy(cq); 857 } 858 EXPORT_SYMBOL_GPL(nvmet_cq_put); 859 860 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, 861 u16 qid, u16 size) 862 { 863 cq->qid = qid; 864 cq->size = size; 865 866 ctrl->cqs[qid] = cq; 867 } 868 869 void nvmet_cq_destroy(struct nvmet_cq *cq) 870 { 871 struct nvmet_ctrl *ctrl = cq->ctrl; 872 873 if (ctrl) { 874 ctrl->cqs[cq->qid] = NULL; 875 nvmet_ctrl_put(cq->ctrl); 876 cq->ctrl = NULL; 877 } 878 } 879 880 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, 881 u16 qid, u16 size) 882 { 883 sq->sqhd = 0; 884 sq->qid = qid; 885 sq->size = size; 886 887 ctrl->sqs[qid] = sq; 888 } 889 890 static void nvmet_confirm_sq(struct percpu_ref *ref) 891 { 892 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref); 893 894 complete(&sq->confirm_done); 895 } 896 897 u16 nvmet_check_cqid(struct nvmet_ctrl *ctrl, u16 cqid, bool create) 898 { 899 if (!ctrl->cqs) 900 return NVME_SC_INTERNAL | NVME_STATUS_DNR; 901 902 if (cqid > ctrl->max_qid) 903 return NVME_SC_QID_INVALID | NVME_STATUS_DNR; 904 905 if ((create && ctrl->cqs[cqid]) || (!create && !ctrl->cqs[cqid])) 906 return NVME_SC_QID_INVALID | NVME_STATUS_DNR; 907 908 return NVME_SC_SUCCESS; 909 } 910 911 u16 nvmet_check_io_cqid(struct nvmet_ctrl *ctrl, u16 cqid, bool create) 912 { 913 if (!cqid) 914 return NVME_SC_QID_INVALID | NVME_STATUS_DNR; 915 return nvmet_check_cqid(ctrl, cqid, create); 916 } 917 918 bool nvmet_cq_in_use(struct nvmet_cq *cq) 919 { 920 return refcount_read(&cq->ref) > 1; 921 } 922 EXPORT_SYMBOL_GPL(nvmet_cq_in_use); 923 924 u16 nvmet_cq_create(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, 925 u16 qid, u16 size) 926 { 927 u16 status; 928 929 status = nvmet_check_cqid(ctrl, qid, true); 930 if (status != NVME_SC_SUCCESS) 931 return status; 932 933 if (!kref_get_unless_zero(&ctrl->ref)) 934 return NVME_SC_INTERNAL | NVME_STATUS_DNR; 935 cq->ctrl = ctrl; 936 937 nvmet_cq_init(cq); 938 nvmet_cq_setup(ctrl, cq, qid, size); 939 940 return NVME_SC_SUCCESS; 941 } 942 EXPORT_SYMBOL_GPL(nvmet_cq_create); 943 944 u16 nvmet_check_sqid(struct nvmet_ctrl *ctrl, u16 sqid, 945 bool create) 946 { 947 if (!ctrl->sqs) 948 return NVME_SC_INTERNAL | NVME_STATUS_DNR; 949 950 if (sqid > ctrl->max_qid) 951 return NVME_SC_QID_INVALID | NVME_STATUS_DNR; 952 953 if ((create && ctrl->sqs[sqid]) || 954 (!create && !ctrl->sqs[sqid])) 955 return NVME_SC_QID_INVALID | NVME_STATUS_DNR; 956 957 return NVME_SC_SUCCESS; 958 } 959 960 u16 nvmet_sq_create(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, 961 struct nvmet_cq *cq, u16 sqid, u16 size) 962 { 963 u16 status; 964 int ret; 965 966 if (!kref_get_unless_zero(&ctrl->ref)) 967 return NVME_SC_INTERNAL | NVME_STATUS_DNR; 968 969 status = nvmet_check_sqid(ctrl, sqid, true); 970 if (status != NVME_SC_SUCCESS) 971 goto ctrl_put; 972 973 ret = nvmet_sq_init(sq, cq); 974 if (ret) { 975 status = NVME_SC_INTERNAL | NVME_STATUS_DNR; 976 goto ctrl_put; 977 } 978 979 nvmet_sq_setup(ctrl, sq, sqid, size); 980 sq->ctrl = ctrl; 981 982 return NVME_SC_SUCCESS; 983 984 ctrl_put: 985 nvmet_ctrl_put(ctrl); 986 return status; 987 } 988 EXPORT_SYMBOL_GPL(nvmet_sq_create); 989 990 void nvmet_sq_destroy(struct nvmet_sq *sq) 991 { 992 struct nvmet_ctrl *ctrl = sq->ctrl; 993 994 /* 995 * If this is the admin queue, complete all AERs so that our 996 * queue doesn't have outstanding requests on it. 997 */ 998 if (ctrl && ctrl->sqs && ctrl->sqs[0] == sq) 999 nvmet_async_events_failall(ctrl); 1000 percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq); 1001 wait_for_completion(&sq->confirm_done); 1002 wait_for_completion(&sq->free_done); 1003 percpu_ref_exit(&sq->ref); 1004 nvmet_auth_sq_destroy(sq); 1005 nvmet_cq_put(sq->cq); 1006 1007 /* 1008 * we must reference the ctrl again after waiting for inflight IO 1009 * to complete. Because admin connect may have sneaked in after we 1010 * store sq->ctrl locally, but before we killed the percpu_ref. the 1011 * admin connect allocates and assigns sq->ctrl, which now needs a 1012 * final ref put, as this ctrl is going away. 1013 */ 1014 ctrl = sq->ctrl; 1015 1016 if (ctrl) { 1017 /* 1018 * The teardown flow may take some time, and the host may not 1019 * send us keep-alive during this period, hence reset the 1020 * traffic based keep-alive timer so we don't trigger a 1021 * controller teardown as a result of a keep-alive expiration. 1022 */ 1023 ctrl->reset_tbkas = true; 1024 sq->ctrl->sqs[sq->qid] = NULL; 1025 nvmet_ctrl_put(ctrl); 1026 sq->ctrl = NULL; /* allows reusing the queue later */ 1027 } 1028 } 1029 EXPORT_SYMBOL_GPL(nvmet_sq_destroy); 1030 1031 static void nvmet_sq_free(struct percpu_ref *ref) 1032 { 1033 struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref); 1034 1035 complete(&sq->free_done); 1036 } 1037 1038 int nvmet_sq_init(struct nvmet_sq *sq, struct nvmet_cq *cq) 1039 { 1040 int ret; 1041 1042 if (!nvmet_cq_get(cq)) 1043 return -EINVAL; 1044 1045 ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL); 1046 if (ret) { 1047 pr_err("percpu_ref init failed!\n"); 1048 nvmet_cq_put(cq); 1049 return ret; 1050 } 1051 init_completion(&sq->free_done); 1052 init_completion(&sq->confirm_done); 1053 nvmet_auth_sq_init(sq); 1054 sq->cq = cq; 1055 1056 return 0; 1057 } 1058 EXPORT_SYMBOL_GPL(nvmet_sq_init); 1059 1060 static inline u16 nvmet_check_ana_state(struct nvmet_port *port, 1061 struct nvmet_ns *ns) 1062 { 1063 enum nvme_ana_state state = port->ana_state[ns->anagrpid]; 1064 1065 if (unlikely(state == NVME_ANA_INACCESSIBLE)) 1066 return NVME_SC_ANA_INACCESSIBLE; 1067 if (unlikely(state == NVME_ANA_PERSISTENT_LOSS)) 1068 return NVME_SC_ANA_PERSISTENT_LOSS; 1069 if (unlikely(state == NVME_ANA_CHANGE)) 1070 return NVME_SC_ANA_TRANSITION; 1071 return 0; 1072 } 1073 1074 static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req) 1075 { 1076 if (unlikely(req->ns->readonly)) { 1077 switch (req->cmd->common.opcode) { 1078 case nvme_cmd_read: 1079 case nvme_cmd_flush: 1080 break; 1081 default: 1082 return NVME_SC_NS_WRITE_PROTECTED; 1083 } 1084 } 1085 1086 return 0; 1087 } 1088 1089 static u32 nvmet_io_cmd_transfer_len(struct nvmet_req *req) 1090 { 1091 struct nvme_command *cmd = req->cmd; 1092 u32 metadata_len = 0; 1093 1094 if (nvme_is_fabrics(cmd)) 1095 return nvmet_fabrics_io_cmd_data_len(req); 1096 1097 if (!req->ns) 1098 return 0; 1099 1100 switch (req->cmd->common.opcode) { 1101 case nvme_cmd_read: 1102 case nvme_cmd_write: 1103 case nvme_cmd_zone_append: 1104 if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns)) 1105 metadata_len = nvmet_rw_metadata_len(req); 1106 return nvmet_rw_data_len(req) + metadata_len; 1107 case nvme_cmd_dsm: 1108 return nvmet_dsm_len(req); 1109 case nvme_cmd_zone_mgmt_recv: 1110 /* The handler rejects a NUMD whose byte count would wrap. */ 1111 if (le32_to_cpu(req->cmd->zmr.numd) >= U32_MAX / 4) 1112 return 0; 1113 return (le32_to_cpu(req->cmd->zmr.numd) + 1) << 2; 1114 default: 1115 return 0; 1116 } 1117 } 1118 1119 static u16 nvmet_parse_io_cmd(struct nvmet_req *req) 1120 { 1121 struct nvme_command *cmd = req->cmd; 1122 u16 ret; 1123 1124 if (nvme_is_fabrics(cmd)) 1125 return nvmet_parse_fabrics_io_cmd(req); 1126 1127 ret = nvmet_check_ctrl_status(req); 1128 if (unlikely(ret)) 1129 return ret; 1130 1131 if (nvmet_is_passthru_req(req)) 1132 return nvmet_parse_passthru_io_cmd(req); 1133 1134 ret = nvmet_req_find_ns(req); 1135 if (unlikely(ret)) 1136 return ret; 1137 1138 ret = nvmet_check_ana_state(req->port, req->ns); 1139 if (unlikely(ret)) { 1140 req->error_loc = offsetof(struct nvme_common_command, nsid); 1141 return ret; 1142 } 1143 ret = nvmet_io_cmd_check_access(req); 1144 if (unlikely(ret)) { 1145 req->error_loc = offsetof(struct nvme_common_command, nsid); 1146 return ret; 1147 } 1148 1149 if (req->ns->pr.enable) { 1150 ret = nvmet_parse_pr_cmd(req); 1151 if (!ret) 1152 return ret; 1153 } 1154 1155 switch (req->ns->csi) { 1156 case NVME_CSI_NVM: 1157 if (req->ns->file) 1158 ret = nvmet_file_parse_io_cmd(req); 1159 else 1160 ret = nvmet_bdev_parse_io_cmd(req); 1161 break; 1162 case NVME_CSI_ZNS: 1163 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) 1164 ret = nvmet_bdev_zns_parse_io_cmd(req); 1165 else 1166 ret = NVME_SC_INVALID_IO_CMD_SET; 1167 break; 1168 default: 1169 ret = NVME_SC_INVALID_IO_CMD_SET; 1170 } 1171 if (ret) 1172 return ret; 1173 1174 if (req->ns->pr.enable) { 1175 ret = nvmet_pr_check_cmd_access(req); 1176 if (ret) 1177 return ret; 1178 1179 ret = nvmet_pr_get_ns_pc_ref(req); 1180 } 1181 return ret; 1182 } 1183 1184 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_sq *sq, 1185 const struct nvmet_fabrics_ops *ops) 1186 { 1187 u8 flags = req->cmd->common.flags; 1188 u16 status; 1189 1190 req->cq = sq->cq; 1191 req->sq = sq; 1192 req->ops = ops; 1193 req->sg = NULL; 1194 req->metadata_sg = NULL; 1195 req->sg_cnt = 0; 1196 req->metadata_sg_cnt = 0; 1197 req->transfer_len = 0; 1198 req->metadata_len = 0; 1199 req->cqe->result.u64 = 0; 1200 req->cqe->status = 0; 1201 req->cqe->sq_head = 0; 1202 req->ns = NULL; 1203 req->error_loc = NVMET_NO_ERROR_LOC; 1204 req->error_slba = 0; 1205 req->pc_ref = NULL; 1206 req->p.ref_held = false; 1207 1208 /* no support for fused commands yet */ 1209 if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) { 1210 req->error_loc = offsetof(struct nvme_common_command, flags); 1211 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1212 goto fail; 1213 } 1214 1215 /* 1216 * For fabrics, PSDT field shall describe metadata pointer (MPTR) that 1217 * contains an address of a single contiguous physical buffer that is 1218 * byte aligned. For PCI controllers, this is optional so not enforced. 1219 */ 1220 if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) { 1221 if (!req->sq->ctrl || !nvmet_is_pci_ctrl(req->sq->ctrl)) { 1222 req->error_loc = 1223 offsetof(struct nvme_common_command, flags); 1224 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1225 goto fail; 1226 } 1227 } 1228 1229 if (unlikely(!req->sq->ctrl)) 1230 /* will return an error for any non-connect command: */ 1231 status = nvmet_parse_connect_cmd(req); 1232 else if (likely(req->sq->qid != 0)) 1233 status = nvmet_parse_io_cmd(req); 1234 else 1235 status = nvmet_parse_admin_cmd(req); 1236 1237 if (status) 1238 goto fail; 1239 1240 trace_nvmet_req_init(req, req->cmd); 1241 1242 if (unlikely(!percpu_ref_tryget_live(&sq->ref))) { 1243 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1244 goto fail; 1245 } 1246 1247 if (sq->ctrl && sq->ctrl->tbkas) 1248 sq->ctrl->reset_tbkas = true; 1249 1250 return true; 1251 1252 fail: 1253 __nvmet_req_complete(req, status); 1254 return false; 1255 } 1256 EXPORT_SYMBOL_GPL(nvmet_req_init); 1257 1258 void nvmet_req_uninit(struct nvmet_req *req) 1259 { 1260 percpu_ref_put(&req->sq->ref); 1261 if (req->pc_ref) 1262 nvmet_pr_put_ns_pc_ref(req->pc_ref); 1263 if (req->ns) 1264 nvmet_put_namespace(req->ns); 1265 } 1266 EXPORT_SYMBOL_GPL(nvmet_req_uninit); 1267 1268 size_t nvmet_req_transfer_len(struct nvmet_req *req) 1269 { 1270 if (likely(req->sq->qid != 0)) 1271 return nvmet_io_cmd_transfer_len(req); 1272 if (unlikely(!req->sq->ctrl)) 1273 return nvmet_connect_cmd_data_len(req); 1274 return nvmet_admin_cmd_data_len(req); 1275 } 1276 EXPORT_SYMBOL_GPL(nvmet_req_transfer_len); 1277 1278 bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len) 1279 { 1280 if (unlikely(len != req->transfer_len)) { 1281 u16 status; 1282 1283 req->error_loc = offsetof(struct nvme_common_command, dptr); 1284 if (req->cmd->common.flags & NVME_CMD_SGL_ALL) 1285 status = NVME_SC_SGL_INVALID_DATA; 1286 else 1287 status = NVME_SC_INVALID_FIELD; 1288 nvmet_req_complete(req, status | NVME_STATUS_DNR); 1289 return false; 1290 } 1291 1292 return true; 1293 } 1294 EXPORT_SYMBOL_GPL(nvmet_check_transfer_len); 1295 1296 bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len) 1297 { 1298 if (unlikely(data_len > req->transfer_len)) { 1299 u16 status; 1300 1301 req->error_loc = offsetof(struct nvme_common_command, dptr); 1302 if (req->cmd->common.flags & NVME_CMD_SGL_ALL) 1303 status = NVME_SC_SGL_INVALID_DATA; 1304 else 1305 status = NVME_SC_INVALID_FIELD; 1306 nvmet_req_complete(req, status | NVME_STATUS_DNR); 1307 return false; 1308 } 1309 1310 return true; 1311 } 1312 1313 static unsigned int nvmet_data_transfer_len(struct nvmet_req *req) 1314 { 1315 return req->transfer_len - req->metadata_len; 1316 } 1317 1318 static int nvmet_req_alloc_p2pmem_sgls(struct pci_dev *p2p_dev, 1319 struct nvmet_req *req) 1320 { 1321 req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt, 1322 nvmet_data_transfer_len(req)); 1323 if (!req->sg) 1324 goto out_err; 1325 1326 if (req->metadata_len) { 1327 req->metadata_sg = pci_p2pmem_alloc_sgl(p2p_dev, 1328 &req->metadata_sg_cnt, req->metadata_len); 1329 if (!req->metadata_sg) 1330 goto out_free_sg; 1331 } 1332 1333 req->p2p_dev = p2p_dev; 1334 1335 return 0; 1336 out_free_sg: 1337 pci_p2pmem_free_sgl(p2p_dev, req->sg); 1338 out_err: 1339 return -ENOMEM; 1340 } 1341 1342 static struct pci_dev *nvmet_req_find_p2p_dev(struct nvmet_req *req) 1343 { 1344 if (!IS_ENABLED(CONFIG_PCI_P2PDMA) || 1345 !req->sq->ctrl || !req->sq->qid || !req->ns) 1346 return NULL; 1347 return radix_tree_lookup(&req->sq->ctrl->p2p_ns_map, req->ns->nsid); 1348 } 1349 1350 int nvmet_req_alloc_sgls(struct nvmet_req *req) 1351 { 1352 struct pci_dev *p2p_dev = nvmet_req_find_p2p_dev(req); 1353 1354 if (p2p_dev && !nvmet_req_alloc_p2pmem_sgls(p2p_dev, req)) 1355 return 0; 1356 1357 req->sg = sgl_alloc(nvmet_data_transfer_len(req), GFP_KERNEL, 1358 &req->sg_cnt); 1359 if (unlikely(!req->sg)) 1360 goto out; 1361 1362 if (req->metadata_len) { 1363 req->metadata_sg = sgl_alloc(req->metadata_len, GFP_KERNEL, 1364 &req->metadata_sg_cnt); 1365 if (unlikely(!req->metadata_sg)) 1366 goto out_free; 1367 } 1368 1369 return 0; 1370 out_free: 1371 nvmet_req_free_sgls(req); 1372 out: 1373 return -ENOMEM; 1374 } 1375 EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgls); 1376 1377 void nvmet_req_free_sgls(struct nvmet_req *req) 1378 { 1379 if (req->p2p_dev) { 1380 pci_p2pmem_free_sgl(req->p2p_dev, req->sg); 1381 if (req->metadata_sg) 1382 pci_p2pmem_free_sgl(req->p2p_dev, req->metadata_sg); 1383 req->p2p_dev = NULL; 1384 } else { 1385 sgl_free(req->sg); 1386 if (req->metadata_sg) 1387 sgl_free(req->metadata_sg); 1388 } 1389 1390 req->sg = NULL; 1391 req->metadata_sg = NULL; 1392 req->sg_cnt = 0; 1393 req->metadata_sg_cnt = 0; 1394 } 1395 EXPORT_SYMBOL_GPL(nvmet_req_free_sgls); 1396 1397 static inline bool nvmet_css_supported(u8 cc_css) 1398 { 1399 switch (cc_css << NVME_CC_CSS_SHIFT) { 1400 case NVME_CC_CSS_NVM: 1401 case NVME_CC_CSS_CSI: 1402 return true; 1403 default: 1404 return false; 1405 } 1406 } 1407 1408 static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl) 1409 { 1410 lockdep_assert_held(&ctrl->lock); 1411 1412 /* 1413 * Only I/O controllers should verify iosqes,iocqes. 1414 * Strictly speaking, the spec says a discovery controller 1415 * should verify iosqes,iocqes are zeroed, however that 1416 * would break backwards compatibility, so don't enforce it. 1417 */ 1418 if (!nvmet_is_disc_subsys(ctrl->subsys) && 1419 (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES || 1420 nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES)) { 1421 ctrl->csts = NVME_CSTS_CFS; 1422 return; 1423 } 1424 1425 if (nvmet_cc_mps(ctrl->cc) != 0 || 1426 nvmet_cc_ams(ctrl->cc) != 0 || 1427 !nvmet_css_supported(nvmet_cc_css(ctrl->cc))) { 1428 ctrl->csts = NVME_CSTS_CFS; 1429 return; 1430 } 1431 1432 ctrl->csts = NVME_CSTS_RDY; 1433 1434 /* 1435 * Controllers that are not yet enabled should not really enforce the 1436 * keep alive timeout, but we still want to track a timeout and cleanup 1437 * in case a host died before it enabled the controller. Hence, simply 1438 * reset the keep alive timer when the controller is enabled. 1439 */ 1440 if (ctrl->kato) 1441 mod_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ); 1442 } 1443 1444 static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl) 1445 { 1446 lockdep_assert_held(&ctrl->lock); 1447 1448 /* XXX: tear down queues? */ 1449 ctrl->csts &= ~NVME_CSTS_RDY; 1450 ctrl->cc = 0; 1451 } 1452 1453 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new) 1454 { 1455 u32 old; 1456 1457 mutex_lock(&ctrl->lock); 1458 old = ctrl->cc; 1459 ctrl->cc = new; 1460 1461 if (nvmet_cc_en(new) && !nvmet_cc_en(old)) 1462 nvmet_start_ctrl(ctrl); 1463 if (!nvmet_cc_en(new) && nvmet_cc_en(old)) 1464 nvmet_clear_ctrl(ctrl); 1465 if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) { 1466 nvmet_clear_ctrl(ctrl); 1467 ctrl->csts |= NVME_CSTS_SHST_CMPLT; 1468 } 1469 if (!nvmet_cc_shn(new) && nvmet_cc_shn(old)) 1470 ctrl->csts &= ~NVME_CSTS_SHST_CMPLT; 1471 mutex_unlock(&ctrl->lock); 1472 } 1473 EXPORT_SYMBOL_GPL(nvmet_update_cc); 1474 1475 static void nvmet_init_cap(struct nvmet_ctrl *ctrl) 1476 { 1477 /* command sets supported: NVMe command set: */ 1478 ctrl->cap = (1ULL << 37); 1479 /* Controller supports one or more I/O Command Sets */ 1480 ctrl->cap |= (1ULL << 43); 1481 /* CC.EN timeout in 500msec units: */ 1482 ctrl->cap |= (15ULL << 24); 1483 /* maximum queue entries supported: */ 1484 if (ctrl->ops->get_max_queue_size) 1485 ctrl->cap |= min_t(u16, ctrl->ops->get_max_queue_size(ctrl), 1486 ctrl->port->max_queue_size) - 1; 1487 else 1488 ctrl->cap |= ctrl->port->max_queue_size - 1; 1489 1490 if (nvmet_is_passthru_subsys(ctrl->subsys)) 1491 nvmet_passthrough_override_cap(ctrl); 1492 } 1493 1494 struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn, 1495 const char *hostnqn, u16 cntlid, 1496 struct nvmet_req *req) 1497 { 1498 struct nvmet_ctrl *ctrl = NULL; 1499 struct nvmet_subsys *subsys; 1500 1501 subsys = nvmet_find_get_subsys(req->port, subsysnqn); 1502 if (!subsys) { 1503 pr_warn("connect request for invalid subsystem %s!\n", 1504 subsysnqn); 1505 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn); 1506 goto out; 1507 } 1508 1509 mutex_lock(&subsys->lock); 1510 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { 1511 if (ctrl->cntlid == cntlid) { 1512 if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) { 1513 pr_warn("hostnqn mismatch.\n"); 1514 continue; 1515 } 1516 if (!kref_get_unless_zero(&ctrl->ref)) 1517 continue; 1518 1519 /* ctrl found */ 1520 goto found; 1521 } 1522 } 1523 1524 ctrl = NULL; /* ctrl not found */ 1525 pr_warn("could not find controller %d for subsys %s / host %s\n", 1526 cntlid, subsysnqn, hostnqn); 1527 req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid); 1528 1529 found: 1530 mutex_unlock(&subsys->lock); 1531 nvmet_subsys_put(subsys); 1532 out: 1533 return ctrl; 1534 } 1535 1536 u16 nvmet_check_ctrl_status(struct nvmet_req *req) 1537 { 1538 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) { 1539 pr_err("got cmd %d while CC.EN == 0 on qid = %d\n", 1540 req->cmd->common.opcode, req->sq->qid); 1541 return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR; 1542 } 1543 1544 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) { 1545 pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n", 1546 req->cmd->common.opcode, req->sq->qid); 1547 return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR; 1548 } 1549 1550 if (unlikely(!nvmet_check_auth_status(req))) { 1551 pr_warn("qid %d not authenticated\n", req->sq->qid); 1552 return NVME_SC_AUTH_REQUIRED | NVME_STATUS_DNR; 1553 } 1554 return 0; 1555 } 1556 1557 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn) 1558 { 1559 struct nvmet_host_link *p; 1560 1561 lockdep_assert_held(&nvmet_config_sem); 1562 1563 if (subsys->allow_any_host) 1564 return true; 1565 1566 if (nvmet_is_disc_subsys(subsys)) /* allow all access to disc subsys */ 1567 return true; 1568 1569 list_for_each_entry(p, &subsys->hosts, entry) { 1570 if (!strcmp(nvmet_host_name(p->host), hostnqn)) 1571 return true; 1572 } 1573 1574 return false; 1575 } 1576 1577 static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl, 1578 struct device *p2p_client) 1579 { 1580 struct nvmet_ns *ns; 1581 unsigned long idx; 1582 1583 lockdep_assert_held(&ctrl->subsys->lock); 1584 1585 if (!p2p_client) 1586 return; 1587 1588 ctrl->p2p_client = get_device(p2p_client); 1589 1590 nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) 1591 nvmet_p2pmem_ns_add_p2p(ctrl, ns); 1592 } 1593 1594 static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl) 1595 { 1596 struct radix_tree_iter iter; 1597 void __rcu **slot; 1598 1599 lockdep_assert_held(&ctrl->subsys->lock); 1600 1601 radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0) 1602 pci_dev_put(radix_tree_deref_slot(slot)); 1603 1604 put_device(ctrl->p2p_client); 1605 } 1606 1607 static void nvmet_fatal_error_handler(struct work_struct *work) 1608 { 1609 struct nvmet_ctrl *ctrl = 1610 container_of(work, struct nvmet_ctrl, fatal_err_work); 1611 1612 pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid); 1613 ctrl->ops->delete_ctrl(ctrl); 1614 } 1615 1616 struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args) 1617 { 1618 struct nvmet_subsys *subsys; 1619 struct nvmet_ctrl *ctrl; 1620 u32 kato = args->kato; 1621 u8 dhchap_status; 1622 int ret; 1623 1624 if (kato > NVMET_MAX_KATO * MSEC_PER_SEC) { 1625 args->status = NVME_SC_KA_TIMEOUT_INVALID | NVME_STATUS_DNR; 1626 args->error_loc = offsetof(struct nvmf_connect_command, kato); 1627 return NULL; 1628 } 1629 1630 args->status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR; 1631 subsys = nvmet_find_get_subsys(args->port, args->subsysnqn); 1632 if (!subsys) { 1633 pr_warn("connect request for invalid subsystem %s!\n", 1634 args->subsysnqn); 1635 args->result = IPO_IATTR_CONNECT_DATA(subsysnqn); 1636 args->error_loc = offsetof(struct nvme_common_command, dptr); 1637 return NULL; 1638 } 1639 1640 down_read(&nvmet_config_sem); 1641 if (!nvmet_host_allowed(subsys, args->hostnqn)) { 1642 pr_info("connect by host %s for subsystem %s not allowed\n", 1643 args->hostnqn, args->subsysnqn); 1644 args->result = IPO_IATTR_CONNECT_DATA(hostnqn); 1645 up_read(&nvmet_config_sem); 1646 args->status = NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR; 1647 args->error_loc = offsetof(struct nvme_common_command, dptr); 1648 goto out_put_subsystem; 1649 } 1650 up_read(&nvmet_config_sem); 1651 1652 args->status = NVME_SC_INTERNAL; 1653 ctrl = kzalloc_obj(*ctrl); 1654 if (!ctrl) 1655 goto out_put_subsystem; 1656 mutex_init(&ctrl->lock); 1657 1658 ctrl->port = args->port; 1659 ctrl->ops = args->ops; 1660 1661 #ifdef CONFIG_NVME_TARGET_PASSTHRU 1662 /* By default, set loop targets to clear IDS by default */ 1663 if (ctrl->port->disc_addr.trtype == NVMF_TRTYPE_LOOP) { 1664 mutex_lock(&subsys->lock); 1665 subsys->passthru = nvmet_subsys_passthru(subsys); 1666 if (!subsys->passthru) { 1667 mutex_unlock(&subsys->lock); 1668 goto out_free_ctrl; 1669 } 1670 subsys->passthru->clear_ids = 1; 1671 mutex_unlock(&subsys->lock); 1672 } 1673 #endif 1674 1675 INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work); 1676 INIT_LIST_HEAD(&ctrl->async_events); 1677 INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL); 1678 INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler); 1679 INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer); 1680 1681 memcpy(ctrl->hostnqn, args->hostnqn, NVMF_NQN_SIZE); 1682 if (args->hostid) 1683 uuid_copy(&ctrl->hostid, args->hostid); 1684 1685 kref_init(&ctrl->ref); 1686 ctrl->subsys = subsys; 1687 ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support; 1688 nvmet_init_cap(ctrl); 1689 WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL); 1690 1691 ctrl->changed_ns_list = kmalloc_objs(__le32, 1692 NVME_MAX_CHANGED_NAMESPACES); 1693 if (!ctrl->changed_ns_list) 1694 goto out_free_ctrl; 1695 1696 /* 1697 * Discovery controllers may use some arbitrary high value 1698 * in order to cleanup stale discovery sessions 1699 */ 1700 if (nvmet_is_disc_subsys(ctrl->subsys) && !kato) 1701 kato = NVMET_DISC_KATO_MS; 1702 1703 /* keep-alive timeout in seconds */ 1704 ctrl->kato = DIV_ROUND_UP_ULL(kato, MSEC_PER_SEC); 1705 1706 ctrl->err_counter = 0; 1707 spin_lock_init(&ctrl->error_lock); 1708 1709 down_read(&nvmet_config_sem); 1710 mutex_lock(&subsys->lock); 1711 1712 ctrl->max_qid = subsys->max_qid; 1713 ctrl->tbkas = subsys->tbkas; 1714 1715 ctrl->sqs = kzalloc_objs(struct nvmet_sq *, ctrl->max_qid + 1); 1716 if (!ctrl->sqs) 1717 goto out_free_changed_ns_list; 1718 1719 ctrl->cqs = kzalloc_objs(struct nvmet_cq *, ctrl->max_qid + 1); 1720 if (!ctrl->cqs) 1721 goto out_free_sqs; 1722 1723 ret = ida_alloc_range(&cntlid_ida, 1724 subsys->cntlid_min, subsys->cntlid_max, 1725 GFP_KERNEL); 1726 if (ret < 0) { 1727 args->status = NVME_SC_CONNECT_CTRL_BUSY | NVME_STATUS_DNR; 1728 goto out_free_cqs; 1729 } 1730 ctrl->cntlid = ret; 1731 1732 ret = nvmet_ctrl_init_pr(ctrl); 1733 if (ret) 1734 goto init_pr_fail; 1735 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls); 1736 nvmet_setup_p2p_ns_map(ctrl, args->p2p_client); 1737 nvmet_debugfs_ctrl_setup(ctrl); 1738 mutex_unlock(&subsys->lock); 1739 up_read(&nvmet_config_sem); 1740 1741 nvmet_start_keep_alive_timer(ctrl); 1742 1743 dhchap_status = nvmet_setup_auth(ctrl, args->sq, false); 1744 if (dhchap_status) { 1745 pr_err("Failed to setup authentication, dhchap status %u\n", 1746 dhchap_status); 1747 nvmet_ctrl_put(ctrl); 1748 if (dhchap_status == NVME_AUTH_DHCHAP_FAILURE_FAILED) 1749 args->status = 1750 NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR; 1751 else 1752 args->status = NVME_SC_INTERNAL; 1753 return NULL; 1754 } 1755 1756 args->status = NVME_SC_SUCCESS; 1757 1758 pr_info("Created %s controller %d for subsystem %s for NQN %s%s%s%s.\n", 1759 nvmet_is_disc_subsys(ctrl->subsys) ? "discovery" : "nvm", 1760 ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn, 1761 ctrl->pi_support ? " T10-PI is enabled" : "", 1762 nvmet_has_auth(ctrl, args->sq) ? " with DH-HMAC-CHAP" : "", 1763 nvmet_queue_tls_keyid(args->sq) ? ", TLS" : ""); 1764 1765 return ctrl; 1766 1767 init_pr_fail: 1768 ida_free(&cntlid_ida, ctrl->cntlid); 1769 out_free_cqs: 1770 kfree(ctrl->cqs); 1771 out_free_sqs: 1772 kfree(ctrl->sqs); 1773 out_free_changed_ns_list: 1774 mutex_unlock(&subsys->lock); 1775 up_read(&nvmet_config_sem); 1776 kfree(ctrl->changed_ns_list); 1777 out_free_ctrl: 1778 kfree(ctrl); 1779 out_put_subsystem: 1780 nvmet_subsys_put(subsys); 1781 return NULL; 1782 } 1783 EXPORT_SYMBOL_GPL(nvmet_alloc_ctrl); 1784 1785 static void nvmet_ctrl_free(struct kref *ref) 1786 { 1787 struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref); 1788 struct nvmet_subsys *subsys = ctrl->subsys; 1789 1790 mutex_lock(&subsys->lock); 1791 nvmet_ctrl_destroy_pr(ctrl); 1792 nvmet_release_p2p_ns_map(ctrl); 1793 list_del(&ctrl->subsys_entry); 1794 mutex_unlock(&subsys->lock); 1795 1796 nvmet_stop_keep_alive_timer(ctrl); 1797 1798 cancel_work_sync(&ctrl->async_event_work); 1799 cancel_work_sync(&ctrl->fatal_err_work); 1800 1801 nvmet_destroy_auth(ctrl); 1802 1803 nvmet_debugfs_ctrl_free(ctrl); 1804 1805 ida_free(&cntlid_ida, ctrl->cntlid); 1806 1807 nvmet_async_events_free(ctrl); 1808 kfree(ctrl->sqs); 1809 kfree(ctrl->cqs); 1810 kfree(ctrl->changed_ns_list); 1811 kfree(ctrl); 1812 1813 nvmet_subsys_put(subsys); 1814 } 1815 1816 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl) 1817 { 1818 kref_put(&ctrl->ref, nvmet_ctrl_free); 1819 } 1820 EXPORT_SYMBOL_GPL(nvmet_ctrl_put); 1821 1822 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl) 1823 { 1824 mutex_lock(&ctrl->lock); 1825 if (!(ctrl->csts & NVME_CSTS_CFS)) { 1826 ctrl->csts |= NVME_CSTS_CFS; 1827 queue_work(nvmet_wq, &ctrl->fatal_err_work); 1828 } 1829 mutex_unlock(&ctrl->lock); 1830 } 1831 EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error); 1832 1833 ssize_t nvmet_ctrl_host_traddr(struct nvmet_ctrl *ctrl, 1834 char *traddr, size_t traddr_len) 1835 { 1836 if (!ctrl->ops->host_traddr) 1837 return -EOPNOTSUPP; 1838 /* They all take the address from the admin queue */ 1839 if (!ctrl->sqs[0]) 1840 return -ENOTCONN; 1841 return ctrl->ops->host_traddr(ctrl, traddr, traddr_len); 1842 } 1843 1844 static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port, 1845 const char *subsysnqn) 1846 { 1847 struct nvmet_subsys_link *p; 1848 1849 if (!port) 1850 return NULL; 1851 1852 if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) { 1853 if (!kref_get_unless_zero(&nvmet_disc_subsys->ref)) 1854 return NULL; 1855 return nvmet_disc_subsys; 1856 } 1857 1858 down_read(&nvmet_config_sem); 1859 if (!strncmp(nvmet_disc_subsys->subsysnqn, subsysnqn, 1860 NVMF_NQN_SIZE)) { 1861 if (kref_get_unless_zero(&nvmet_disc_subsys->ref)) { 1862 up_read(&nvmet_config_sem); 1863 return nvmet_disc_subsys; 1864 } 1865 } 1866 list_for_each_entry(p, &port->subsystems, entry) { 1867 if (!strncmp(p->subsys->subsysnqn, subsysnqn, 1868 NVMF_NQN_SIZE)) { 1869 if (!kref_get_unless_zero(&p->subsys->ref)) 1870 break; 1871 up_read(&nvmet_config_sem); 1872 return p->subsys; 1873 } 1874 } 1875 up_read(&nvmet_config_sem); 1876 return NULL; 1877 } 1878 1879 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, 1880 enum nvme_subsys_type type) 1881 { 1882 struct nvmet_subsys *subsys; 1883 char serial[NVMET_SN_MAX_SIZE / 2]; 1884 int ret; 1885 1886 subsys = kzalloc_obj(*subsys); 1887 if (!subsys) 1888 return ERR_PTR(-ENOMEM); 1889 1890 subsys->ver = NVMET_DEFAULT_VS; 1891 /* generate a random serial number as our controllers are ephemeral: */ 1892 get_random_bytes(&serial, sizeof(serial)); 1893 bin2hex(subsys->serial, &serial, sizeof(serial)); 1894 1895 subsys->model_number = kstrdup(NVMET_DEFAULT_CTRL_MODEL, GFP_KERNEL); 1896 if (!subsys->model_number) { 1897 ret = -ENOMEM; 1898 goto free_subsys; 1899 } 1900 1901 subsys->ieee_oui = 0; 1902 subsys->tbkas = true; 1903 1904 subsys->firmware_rev = kstrndup(UTS_RELEASE, NVMET_FR_MAX_SIZE, GFP_KERNEL); 1905 if (!subsys->firmware_rev) { 1906 ret = -ENOMEM; 1907 goto free_mn; 1908 } 1909 1910 switch (type) { 1911 case NVME_NQN_NVME: 1912 subsys->max_qid = NVMET_NR_QUEUES; 1913 break; 1914 case NVME_NQN_DISC: 1915 case NVME_NQN_CURR: 1916 subsys->max_qid = 0; 1917 break; 1918 default: 1919 pr_err("%s: Unknown Subsystem type - %d\n", __func__, type); 1920 ret = -EINVAL; 1921 goto free_fr; 1922 } 1923 subsys->type = type; 1924 subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE, 1925 GFP_KERNEL); 1926 if (!subsys->subsysnqn) { 1927 ret = -ENOMEM; 1928 goto free_fr; 1929 } 1930 subsys->cntlid_min = NVME_CNTLID_MIN; 1931 subsys->cntlid_max = NVME_CNTLID_MAX; 1932 kref_init(&subsys->ref); 1933 1934 mutex_init(&subsys->lock); 1935 xa_init(&subsys->namespaces); 1936 INIT_LIST_HEAD(&subsys->ctrls); 1937 INIT_LIST_HEAD(&subsys->hosts); 1938 1939 ret = nvmet_debugfs_subsys_setup(subsys); 1940 if (ret) 1941 goto free_subsysnqn; 1942 1943 return subsys; 1944 1945 free_subsysnqn: 1946 kfree(subsys->subsysnqn); 1947 free_fr: 1948 kfree(subsys->firmware_rev); 1949 free_mn: 1950 kfree(subsys->model_number); 1951 free_subsys: 1952 kfree(subsys); 1953 return ERR_PTR(ret); 1954 } 1955 1956 static void nvmet_subsys_free(struct kref *ref) 1957 { 1958 struct nvmet_subsys *subsys = 1959 container_of(ref, struct nvmet_subsys, ref); 1960 1961 WARN_ON_ONCE(!list_empty(&subsys->ctrls)); 1962 WARN_ON_ONCE(!list_empty(&subsys->hosts)); 1963 WARN_ON_ONCE(!xa_empty(&subsys->namespaces)); 1964 1965 nvmet_debugfs_subsys_free(subsys); 1966 1967 xa_destroy(&subsys->namespaces); 1968 nvmet_passthru_subsys_free(subsys); 1969 1970 kfree(subsys->subsysnqn); 1971 kfree(subsys->model_number); 1972 kfree(subsys->firmware_rev); 1973 kfree(subsys); 1974 } 1975 1976 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys) 1977 { 1978 struct nvmet_ctrl *ctrl; 1979 1980 mutex_lock(&subsys->lock); 1981 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) 1982 ctrl->ops->delete_ctrl(ctrl); 1983 mutex_unlock(&subsys->lock); 1984 } 1985 1986 void nvmet_subsys_put(struct nvmet_subsys *subsys) 1987 { 1988 kref_put(&subsys->ref, nvmet_subsys_free); 1989 } 1990 1991 static int __init nvmet_init(void) 1992 { 1993 int error = -ENOMEM; 1994 1995 nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1; 1996 1997 nvmet_bvec_cache = kmem_cache_create("nvmet-bvec", 1998 NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec), 0, 1999 SLAB_HWCACHE_ALIGN, NULL); 2000 if (!nvmet_bvec_cache) 2001 return -ENOMEM; 2002 2003 zbd_wq = alloc_workqueue("nvmet-zbd-wq", WQ_MEM_RECLAIM | WQ_PERCPU, 2004 0); 2005 if (!zbd_wq) 2006 goto out_destroy_bvec_cache; 2007 2008 buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq", 2009 WQ_MEM_RECLAIM | WQ_PERCPU, 0); 2010 if (!buffered_io_wq) 2011 goto out_free_zbd_work_queue; 2012 2013 nvmet_wq = alloc_workqueue("nvmet-wq", 2014 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_SYSFS, 0); 2015 if (!nvmet_wq) 2016 goto out_free_buffered_work_queue; 2017 2018 nvmet_aen_wq = alloc_workqueue("nvmet-aen-wq", 2019 WQ_MEM_RECLAIM | WQ_UNBOUND, 0); 2020 if (!nvmet_aen_wq) 2021 goto out_free_nvmet_work_queue; 2022 2023 error = nvmet_init_debugfs(); 2024 if (error) 2025 goto out_free_nvmet_aen_work_queue; 2026 2027 error = nvmet_init_discovery(); 2028 if (error) 2029 goto out_exit_debugfs; 2030 2031 error = nvmet_init_configfs(); 2032 if (error) 2033 goto out_exit_discovery; 2034 2035 return 0; 2036 2037 out_exit_discovery: 2038 nvmet_exit_discovery(); 2039 out_exit_debugfs: 2040 nvmet_exit_debugfs(); 2041 out_free_nvmet_aen_work_queue: 2042 destroy_workqueue(nvmet_aen_wq); 2043 out_free_nvmet_work_queue: 2044 destroy_workqueue(nvmet_wq); 2045 out_free_buffered_work_queue: 2046 destroy_workqueue(buffered_io_wq); 2047 out_free_zbd_work_queue: 2048 destroy_workqueue(zbd_wq); 2049 out_destroy_bvec_cache: 2050 kmem_cache_destroy(nvmet_bvec_cache); 2051 return error; 2052 } 2053 2054 static void __exit nvmet_exit(void) 2055 { 2056 nvmet_exit_configfs(); 2057 nvmet_exit_discovery(); 2058 nvmet_exit_debugfs(); 2059 ida_destroy(&cntlid_ida); 2060 destroy_workqueue(nvmet_aen_wq); 2061 destroy_workqueue(nvmet_wq); 2062 destroy_workqueue(buffered_io_wq); 2063 destroy_workqueue(zbd_wq); 2064 kmem_cache_destroy(nvmet_bvec_cache); 2065 2066 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024); 2067 BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024); 2068 } 2069 2070 module_init(nvmet_init); 2071 module_exit(nvmet_exit); 2072 2073 MODULE_DESCRIPTION("NVMe target core framework"); 2074 MODULE_LICENSE("GPL v2"); 2075