1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 4 */ 5 6 #ifndef _NVMET_H 7 #define _NVMET_H 8 9 #include <linux/dma-mapping.h> 10 #include <linux/jiffies.h> 11 #include <linux/types.h> 12 #include <linux/device.h> 13 #include <linux/kref.h> 14 #include <linux/percpu-refcount.h> 15 #include <linux/list.h> 16 #include <linux/mutex.h> 17 #include <linux/uuid.h> 18 #include <linux/nvme.h> 19 #include <linux/configfs.h> 20 #include <linux/rcupdate.h> 21 #include <linux/blkdev.h> 22 #include <linux/radix-tree.h> 23 #include <linux/t10-pi.h> 24 #include <linux/kfifo.h> 25 26 #define NVMET_DEFAULT_VS NVME_VS(2, 1, 0) 27 28 #define NVMET_NS_ENABLED XA_MARK_1 29 #define NVMET_ASYNC_EVENTS 4 30 #define NVMET_ERROR_LOG_SLOTS 128 31 #define NVMET_NO_ERROR_LOC ((u16)-1) 32 #define NVMET_DEFAULT_CTRL_MODEL "Linux" 33 #define NVMET_MN_MAX_SIZE 40 34 #define NVMET_SN_MAX_SIZE 20 35 #define NVMET_FR_MAX_SIZE 8 36 #define NVMET_PR_LOG_QUEUE_SIZE 64 37 38 #define nvmet_for_each_ns(xa, index, entry) \ 39 xa_for_each(xa, index, entry) 40 41 #define nvmet_for_each_enabled_ns(xa, index, entry) \ 42 xa_for_each_marked(xa, index, entry, NVMET_NS_ENABLED) 43 44 /* 45 * Supported optional AENs: 46 */ 47 #define NVMET_AEN_CFG_OPTIONAL \ 48 (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE) 49 #define NVMET_DISC_AEN_CFG_OPTIONAL \ 50 (NVME_AEN_CFG_DISC_CHANGE) 51 52 /* 53 * Plus mandatory SMART AENs (we'll never send them, but allow enabling them): 54 */ 55 #define NVMET_AEN_CFG_ALL \ 56 (NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \ 57 NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \ 58 NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL) 59 60 /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM 61 * The 16 bit shift is to set IATTR bit to 1, which means offending 62 * offset starts in the data section of connect() 63 */ 64 #define IPO_IATTR_CONNECT_DATA(x) \ 65 (cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x)))) 66 #define IPO_IATTR_CONNECT_SQE(x) \ 67 (cpu_to_le32(offsetof(struct nvmf_connect_command, x))) 68 69 struct nvmet_pr_registrant { 70 u64 rkey; 71 uuid_t hostid; 72 enum nvme_pr_type rtype; 73 struct list_head entry; 74 struct rcu_head rcu; 75 }; 76 77 struct nvmet_pr { 78 bool enable; 79 unsigned long notify_mask; 80 atomic_t generation; 81 struct nvmet_pr_registrant __rcu *holder; 82 /* 83 * During the execution of the reservation command, mutual 84 * exclusion is required throughout the process. However, 85 * while waiting asynchronously for the 'per controller 86 * percpu_ref' to complete before the 'preempt and abort' 87 * command finishes, a semaphore is needed to ensure mutual 88 * exclusion instead of a mutex. 89 */ 90 struct semaphore pr_sem; 91 struct list_head registrant_list; 92 }; 93 94 struct nvmet_pr_per_ctrl_ref { 95 struct percpu_ref ref; 96 struct completion free_done; 97 struct completion confirm_done; 98 uuid_t hostid; 99 }; 100 101 struct nvmet_ns { 102 struct percpu_ref ref; 103 struct file *bdev_file; 104 struct block_device *bdev; 105 struct file *file; 106 bool readonly; 107 u32 nsid; 108 u32 blksize_shift; 109 loff_t size; 110 u8 nguid[16]; 111 uuid_t uuid; 112 u32 anagrpid; 113 114 bool buffered_io; 115 bool enabled; 116 #define NVMET_NS_IO_LIVE 0 117 unsigned long flags; 118 struct nvmet_subsys *subsys; 119 const char *device_path; 120 121 struct config_group device_group; 122 struct config_group group; 123 124 struct completion disable_done; 125 mempool_t *bvec_pool; 126 127 struct pci_dev *p2p_dev; 128 int use_p2pmem; 129 int pi_type; 130 int metadata_size; 131 u8 csi; 132 struct nvmet_pr pr; 133 struct xarray pr_per_ctrl_refs; 134 #ifdef CONFIG_NVME_TARGET_DEBUGFS 135 struct dentry *debugfs_dir; 136 #endif 137 }; 138 139 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item) 140 { 141 return container_of(to_config_group(item), struct nvmet_ns, group); 142 } 143 144 static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns) 145 { 146 return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL; 147 } 148 149 struct nvmet_cq { 150 struct nvmet_ctrl *ctrl; 151 u16 qid; 152 u16 size; 153 refcount_t ref; 154 }; 155 156 struct nvmet_sq { 157 struct nvmet_ctrl *ctrl; 158 struct percpu_ref ref; 159 struct nvmet_cq *cq; 160 u16 qid; 161 u16 size; 162 u32 sqhd; 163 bool sqhd_disabled; 164 #ifdef CONFIG_NVME_TARGET_AUTH 165 struct mutex auth_lock; 166 bool authenticated; 167 struct delayed_work auth_expired_work; 168 bool auth_expiry_active; 169 u16 dhchap_tid; 170 u8 sc_c; 171 u8 dhchap_status; 172 u8 dhchap_step; 173 u8 *dhchap_c1; 174 u8 *dhchap_c2; 175 u32 dhchap_s1; 176 u32 dhchap_s2; 177 u8 *dhchap_skey; 178 int dhchap_skey_len; 179 #endif 180 #ifdef CONFIG_NVME_TARGET_TCP_TLS 181 struct key *tls_key; 182 #endif 183 struct completion free_done; 184 struct completion confirm_done; 185 }; 186 187 struct nvmet_ana_group { 188 struct config_group group; 189 struct nvmet_port *port; 190 u32 grpid; 191 }; 192 193 static inline struct nvmet_ana_group *to_ana_group(struct config_item *item) 194 { 195 return container_of(to_config_group(item), struct nvmet_ana_group, 196 group); 197 } 198 199 /** 200 * struct nvmet_port - Common structure to keep port 201 * information for the target. 202 * @entry: Entry into referrals or transport list. 203 * @disc_addr: Address information is stored in a format defined 204 * for a discovery log page entry. 205 * @group: ConfigFS group for this element's folder. 206 * @priv: Private data for the transport. 207 */ 208 struct nvmet_port { 209 struct list_head entry; 210 struct nvmf_disc_rsp_page_entry disc_addr; 211 struct config_group group; 212 struct config_group subsys_group; 213 struct list_head subsystems; 214 struct config_group referrals_group; 215 struct list_head referrals; 216 struct list_head global_entry; 217 struct config_group ana_groups_group; 218 struct nvmet_ana_group ana_default_group; 219 struct key *keyring; 220 void *priv; 221 bool enabled; 222 int inline_data_size; 223 int max_queue_size; 224 int mdts; 225 const struct nvmet_fabrics_ops *tr_ops; 226 bool pi_enable; 227 enum nvme_ana_state ana_state[]; 228 }; 229 230 static inline struct nvmet_port *to_nvmet_port(struct config_item *item) 231 { 232 return container_of(to_config_group(item), struct nvmet_port, 233 group); 234 } 235 236 static inline struct nvmet_port *ana_groups_to_port( 237 struct config_item *item) 238 { 239 return container_of(to_config_group(item), struct nvmet_port, 240 ana_groups_group); 241 } 242 243 static inline u8 nvmet_port_disc_addr_treq_secure_channel(struct nvmet_port *port) 244 { 245 return (port->disc_addr.treq & NVME_TREQ_SECURE_CHANNEL_MASK); 246 } 247 248 static inline bool nvmet_port_secure_channel_required(struct nvmet_port *port) 249 { 250 return nvmet_port_disc_addr_treq_secure_channel(port) == NVMF_TREQ_REQUIRED; 251 } 252 253 struct nvmet_pr_log_mgr { 254 struct mutex lock; 255 u64 lost_count; 256 u64 counter; 257 DECLARE_KFIFO(log_queue, struct nvme_pr_log, NVMET_PR_LOG_QUEUE_SIZE); 258 }; 259 260 struct nvmet_ctrl { 261 struct nvmet_subsys *subsys; 262 struct nvmet_sq **sqs; 263 struct nvmet_cq **cqs; 264 265 void *drvdata; 266 267 bool reset_tbkas; 268 269 struct mutex lock; 270 u64 cap; 271 u32 cc; 272 u32 csts; 273 274 uuid_t hostid; 275 u16 cntlid; 276 u16 max_qid; 277 u32 kato; 278 bool tbkas; 279 280 struct nvmet_port *port; 281 282 u32 aen_enabled; 283 unsigned long aen_masked; 284 struct nvmet_req *async_event_cmds[NVMET_ASYNC_EVENTS]; 285 unsigned int nr_async_event_cmds; 286 struct list_head async_events; 287 struct work_struct async_event_work; 288 289 struct list_head subsys_entry; 290 struct kref ref; 291 struct delayed_work ka_work; 292 struct work_struct fatal_err_work; 293 294 const struct nvmet_fabrics_ops *ops; 295 296 __le32 *changed_ns_list; 297 u32 nr_changed_ns; 298 299 char hostnqn[NVMF_NQN_FIELD_LEN]; 300 301 struct device *p2p_client; 302 struct radix_tree_root p2p_ns_map; 303 #ifdef CONFIG_NVME_TARGET_DEBUGFS 304 struct dentry *debugfs_dir; 305 #endif 306 spinlock_t error_lock; 307 u64 err_counter; 308 struct nvme_error_slot slots[NVMET_ERROR_LOG_SLOTS]; 309 bool pi_support; 310 bool concat; 311 #ifdef CONFIG_NVME_TARGET_AUTH 312 struct nvme_dhchap_key *host_key; 313 struct nvme_dhchap_key *ctrl_key; 314 u8 shash_id; 315 struct crypto_kpp *dh_tfm; 316 u8 dh_gid; 317 u8 *dh_key; 318 size_t dh_keysize; 319 #endif 320 #ifdef CONFIG_NVME_TARGET_TCP_TLS 321 struct key *tls_key; 322 #endif 323 struct nvmet_pr_log_mgr pr_log_mgr; 324 }; 325 326 struct nvmet_passthru { 327 struct percpu_ref ref; 328 struct completion disable_done; 329 #define NVMET_PASSTHRU_ENABLED 0 330 unsigned long flags; 331 332 struct nvme_ctrl *ctrl; 333 char *ctrl_path; 334 unsigned int admin_timeout; 335 unsigned int io_timeout; 336 unsigned int clear_ids; 337 }; 338 339 struct nvmet_subsys { 340 enum nvme_subsys_type type; 341 342 struct mutex lock; 343 struct kref ref; 344 345 struct xarray namespaces; 346 unsigned int nr_namespaces; 347 u32 max_nsid; 348 u16 cntlid_min; 349 u16 cntlid_max; 350 351 struct list_head ctrls; 352 353 struct list_head hosts; 354 bool allow_any_host; 355 #ifdef CONFIG_NVME_TARGET_DEBUGFS 356 struct dentry *debugfs_dir; 357 #endif 358 u16 max_qid; 359 360 u64 ver; 361 char serial[NVMET_SN_MAX_SIZE]; 362 bool subsys_discovered; 363 char *subsysnqn; 364 bool pi_support; 365 bool tbkas; 366 367 struct config_group group; 368 369 struct config_group namespaces_group; 370 struct config_group allowed_hosts_group; 371 372 u16 vendor_id; 373 u16 subsys_vendor_id; 374 char *model_number; 375 u32 ieee_oui; 376 char *firmware_rev; 377 378 #ifdef CONFIG_NVME_TARGET_PASSTHRU 379 struct nvmet_passthru *passthru; 380 struct config_group passthru_group; 381 #endif /* CONFIG_NVME_TARGET_PASSTHRU */ 382 383 #ifdef CONFIG_BLK_DEV_ZONED 384 u8 zasl; 385 #endif /* CONFIG_BLK_DEV_ZONED */ 386 }; 387 388 static inline struct nvmet_subsys *to_subsys(struct config_item *item) 389 { 390 return container_of(to_config_group(item), struct nvmet_subsys, group); 391 } 392 393 static inline struct nvmet_subsys *namespaces_to_subsys( 394 struct config_item *item) 395 { 396 return container_of(to_config_group(item), struct nvmet_subsys, 397 namespaces_group); 398 } 399 400 struct nvmet_host { 401 struct config_group group; 402 u8 *dhchap_secret; 403 u8 *dhchap_ctrl_secret; 404 int dhchap_key_offset; 405 int dhchap_ctrl_key_offset; 406 u8 dhchap_key_hash; 407 u8 dhchap_ctrl_key_hash; 408 u8 dhchap_hash_id; 409 u8 dhchap_dhgroup_id; 410 }; 411 412 static inline struct nvmet_host *to_host(struct config_item *item) 413 { 414 return container_of(to_config_group(item), struct nvmet_host, group); 415 } 416 417 static inline char *nvmet_host_name(struct nvmet_host *host) 418 { 419 return config_item_name(&host->group.cg_item); 420 } 421 422 struct nvmet_host_link { 423 struct list_head entry; 424 struct nvmet_host *host; 425 }; 426 427 struct nvmet_subsys_link { 428 struct list_head entry; 429 struct nvmet_subsys *subsys; 430 }; 431 432 struct nvmet_req; 433 struct nvmet_fabrics_ops { 434 struct module *owner; 435 unsigned int type; 436 unsigned int msdbd; 437 unsigned int flags; 438 #define NVMF_KEYED_SGLS (1 << 0) 439 #define NVMF_METADATA_SUPPORTED (1 << 1) 440 void (*queue_response)(struct nvmet_req *req); 441 int (*add_port)(struct nvmet_port *port); 442 void (*remove_port)(struct nvmet_port *port); 443 void (*delete_ctrl)(struct nvmet_ctrl *ctrl); 444 void (*disc_traddr)(struct nvmet_req *req, 445 struct nvmet_port *port, char *traddr); 446 ssize_t (*host_traddr)(struct nvmet_ctrl *ctrl, 447 char *traddr, size_t traddr_len); 448 u16 (*install_queue)(struct nvmet_sq *nvme_sq); 449 void (*discovery_chg)(struct nvmet_port *port); 450 u8 (*get_mdts)(const struct nvmet_ctrl *ctrl); 451 u16 (*get_max_queue_size)(const struct nvmet_ctrl *ctrl); 452 453 /* Operations mandatory for PCI target controllers */ 454 u16 (*create_sq)(struct nvmet_ctrl *ctrl, u16 sqid, u16 cqid, u16 flags, 455 u16 qsize, u64 prp1); 456 u16 (*delete_sq)(struct nvmet_ctrl *ctrl, u16 sqid); 457 u16 (*create_cq)(struct nvmet_ctrl *ctrl, u16 cqid, u16 flags, 458 u16 qsize, u64 prp1, u16 irq_vector); 459 u16 (*delete_cq)(struct nvmet_ctrl *ctrl, u16 cqid); 460 u16 (*set_feature)(const struct nvmet_ctrl *ctrl, u8 feat, 461 void *feat_data); 462 u16 (*get_feature)(const struct nvmet_ctrl *ctrl, u8 feat, 463 void *feat_data); 464 }; 465 466 #define NVMET_MAX_INLINE_BIOVEC 8 467 #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE 468 469 struct nvmet_req { 470 struct nvme_command *cmd; 471 struct nvme_completion *cqe; 472 struct nvmet_sq *sq; 473 struct nvmet_cq *cq; 474 struct nvmet_ns *ns; 475 struct scatterlist *sg; 476 struct scatterlist *metadata_sg; 477 struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC]; 478 union { 479 struct { 480 struct bio inline_bio; 481 } b; 482 struct { 483 bool mpool_alloc; 484 struct kiocb iocb; 485 struct bio_vec *bvec; 486 struct work_struct work; 487 } f; 488 struct { 489 struct bio inline_bio; 490 struct request *rq; 491 struct work_struct work; 492 bool use_workqueue; 493 bool ref_held; 494 } p; 495 #ifdef CONFIG_BLK_DEV_ZONED 496 struct { 497 struct bio inline_bio; 498 struct work_struct zmgmt_work; 499 } z; 500 #endif /* CONFIG_BLK_DEV_ZONED */ 501 struct { 502 struct work_struct abort_work; 503 } r; 504 }; 505 int sg_cnt; 506 int metadata_sg_cnt; 507 /* data length as parsed from the SGL descriptor: */ 508 size_t transfer_len; 509 size_t metadata_len; 510 511 struct nvmet_port *port; 512 513 void (*execute)(struct nvmet_req *req); 514 const struct nvmet_fabrics_ops *ops; 515 516 struct pci_dev *p2p_dev; 517 struct device *p2p_client; 518 u16 error_loc; 519 u64 error_slba; 520 struct nvmet_pr_per_ctrl_ref *pc_ref; 521 }; 522 523 #define NVMET_MAX_MPOOL_BVEC 16 524 extern struct kmem_cache *nvmet_bvec_cache; 525 extern struct workqueue_struct *buffered_io_wq; 526 extern struct workqueue_struct *zbd_wq; 527 extern struct workqueue_struct *nvmet_wq; 528 extern struct workqueue_struct *nvmet_aen_wq; 529 530 static inline void nvmet_set_result(struct nvmet_req *req, u32 result) 531 { 532 req->cqe->result.u32 = cpu_to_le32(result); 533 } 534 535 /* 536 * NVMe command writes actually are DMA reads for us on the target side. 537 */ 538 static inline enum dma_data_direction 539 nvmet_data_dir(struct nvmet_req *req) 540 { 541 return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 542 } 543 544 struct nvmet_async_event { 545 struct list_head entry; 546 u8 event_type; 547 u8 event_info; 548 u8 log_page; 549 }; 550 551 static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn) 552 { 553 int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15; 554 555 if (!rae) 556 clear_bit(bn, &req->sq->ctrl->aen_masked); 557 } 558 559 static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn) 560 { 561 if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn))) 562 return true; 563 return test_and_set_bit(bn, &ctrl->aen_masked); 564 } 565 566 void nvmet_get_feat_kato(struct nvmet_req *req); 567 void nvmet_get_feat_async_event(struct nvmet_req *req); 568 u16 nvmet_set_feat_kato(struct nvmet_req *req); 569 u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask); 570 void nvmet_execute_async_event(struct nvmet_req *req); 571 void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl); 572 void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl); 573 574 u16 nvmet_parse_connect_cmd(struct nvmet_req *req); 575 u32 nvmet_connect_cmd_data_len(struct nvmet_req *req); 576 void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id); 577 void nvmet_bdev_set_nvm_limits(struct block_device *bdev, 578 struct nvme_id_ns_nvm *id); 579 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req); 580 u16 nvmet_file_parse_io_cmd(struct nvmet_req *req); 581 u16 nvmet_bdev_zns_parse_io_cmd(struct nvmet_req *req); 582 u32 nvmet_admin_cmd_data_len(struct nvmet_req *req); 583 u16 nvmet_parse_admin_cmd(struct nvmet_req *req); 584 u32 nvmet_discovery_cmd_data_len(struct nvmet_req *req); 585 u16 nvmet_parse_discovery_cmd(struct nvmet_req *req); 586 u16 nvmet_parse_fabrics_admin_cmd(struct nvmet_req *req); 587 u32 nvmet_fabrics_admin_cmd_data_len(struct nvmet_req *req); 588 u16 nvmet_parse_fabrics_io_cmd(struct nvmet_req *req); 589 u32 nvmet_fabrics_io_cmd_data_len(struct nvmet_req *req); 590 591 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_sq *sq, 592 const struct nvmet_fabrics_ops *ops); 593 void nvmet_req_uninit(struct nvmet_req *req); 594 size_t nvmet_req_transfer_len(struct nvmet_req *req); 595 bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len); 596 bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len); 597 void nvmet_req_complete(struct nvmet_req *req, u16 status); 598 int nvmet_req_alloc_sgls(struct nvmet_req *req); 599 void nvmet_req_free_sgls(struct nvmet_req *req); 600 601 void nvmet_execute_set_features(struct nvmet_req *req); 602 void nvmet_execute_get_features(struct nvmet_req *req); 603 void nvmet_execute_keep_alive(struct nvmet_req *req); 604 605 u16 nvmet_check_cqid(struct nvmet_ctrl *ctrl, u16 cqid, bool create); 606 u16 nvmet_check_io_cqid(struct nvmet_ctrl *ctrl, u16 cqid, bool create); 607 void nvmet_cq_init(struct nvmet_cq *cq); 608 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid, 609 u16 size); 610 u16 nvmet_cq_create(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid, 611 u16 size); 612 void nvmet_cq_destroy(struct nvmet_cq *cq); 613 bool nvmet_cq_get(struct nvmet_cq *cq); 614 void nvmet_cq_put(struct nvmet_cq *cq); 615 bool nvmet_cq_in_use(struct nvmet_cq *cq); 616 u16 nvmet_check_sqid(struct nvmet_ctrl *ctrl, u16 sqid, bool create); 617 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid, 618 u16 size); 619 u16 nvmet_sq_create(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, 620 struct nvmet_cq *cq, u16 qid, u16 size); 621 void nvmet_sq_destroy(struct nvmet_sq *sq); 622 int nvmet_sq_init(struct nvmet_sq *sq, struct nvmet_cq *cq); 623 624 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl); 625 626 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new); 627 628 struct nvmet_alloc_ctrl_args { 629 struct nvmet_port *port; 630 struct nvmet_sq *sq; 631 char *subsysnqn; 632 char *hostnqn; 633 uuid_t *hostid; 634 const struct nvmet_fabrics_ops *ops; 635 struct device *p2p_client; 636 u32 kato; 637 __le32 result; 638 u16 error_loc; 639 u16 status; 640 }; 641 642 struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args); 643 struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn, 644 const char *hostnqn, u16 cntlid, 645 struct nvmet_req *req); 646 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl); 647 u16 nvmet_check_ctrl_status(struct nvmet_req *req); 648 ssize_t nvmet_ctrl_host_traddr(struct nvmet_ctrl *ctrl, 649 char *traddr, size_t traddr_len); 650 651 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, 652 enum nvme_subsys_type type); 653 void nvmet_subsys_put(struct nvmet_subsys *subsys); 654 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys); 655 656 u16 nvmet_req_find_ns(struct nvmet_req *req); 657 void nvmet_put_namespace(struct nvmet_ns *ns); 658 int nvmet_ns_enable(struct nvmet_ns *ns); 659 void nvmet_ns_disable(struct nvmet_ns *ns); 660 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid); 661 void nvmet_ns_free(struct nvmet_ns *ns); 662 663 void nvmet_send_ana_event(struct nvmet_subsys *subsys, 664 struct nvmet_port *port); 665 void nvmet_port_send_ana_event(struct nvmet_port *port); 666 667 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops); 668 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops); 669 670 void nvmet_port_del_ctrls(struct nvmet_port *port, 671 struct nvmet_subsys *subsys); 672 673 int nvmet_enable_port(struct nvmet_port *port); 674 void nvmet_disable_port(struct nvmet_port *port); 675 676 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port); 677 void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port); 678 679 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf, 680 size_t len); 681 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, 682 size_t len); 683 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len); 684 685 u32 nvmet_get_log_page_len(struct nvme_command *cmd); 686 u64 nvmet_get_log_page_offset(struct nvme_command *cmd); 687 688 extern struct list_head *nvmet_ports; 689 void nvmet_port_disc_changed(struct nvmet_port *port, 690 struct nvmet_subsys *subsys); 691 void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys, 692 struct nvmet_host *host); 693 void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type, 694 u8 event_info, u8 log_page); 695 696 #define NVMET_MIN_QUEUE_SIZE 16 697 #define NVMET_MAX_QUEUE_SIZE 1024 698 #define NVMET_NR_QUEUES 128 699 #define NVMET_MAX_CMD(ctrl) (NVME_CAP_MQES(ctrl->cap) + 1) 700 #define NVMET_MAX_MDTS 255 701 702 /* 703 * Nice round number that makes a list of nsids fit into a page. 704 * Should become tunable at some point in the future. 705 */ 706 #define NVMET_MAX_NAMESPACES 1024 707 708 /* 709 * 0 is not a valid ANA group ID, so we start numbering at 1. 710 * 711 * ANA Group 1 exists without manual intervention, has namespaces assigned to it 712 * by default, and is available in an optimized state through all ports. 713 */ 714 #define NVMET_MAX_ANAGRPS 128 715 #define NVMET_DEFAULT_ANA_GRPID 1 716 717 #define NVMET_KAS 10 718 #define NVMET_DISC_KATO_MS 120000 719 720 /* 721 * Largest keep-alive timeout in seconds that can be reported back in 722 * milliseconds and converted to jiffies without overflowing. 723 */ 724 #define NVMET_MAX_KATO \ 725 min3(U32_MAX / MSEC_PER_SEC, U32_MAX / HZ, MAX_JIFFY_OFFSET / HZ) 726 727 int __init nvmet_init_configfs(void); 728 void __exit nvmet_exit_configfs(void); 729 730 int __init nvmet_init_discovery(void); 731 void nvmet_exit_discovery(void); 732 733 extern struct nvmet_subsys *nvmet_disc_subsys; 734 extern struct rw_semaphore nvmet_config_sem; 735 736 extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1]; 737 extern u64 nvmet_ana_chgcnt; 738 extern struct rw_semaphore nvmet_ana_sem; 739 740 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn); 741 742 int nvmet_bdev_ns_enable(struct nvmet_ns *ns); 743 int nvmet_file_ns_enable(struct nvmet_ns *ns); 744 void nvmet_bdev_ns_disable(struct nvmet_ns *ns); 745 void nvmet_file_ns_disable(struct nvmet_ns *ns); 746 u16 nvmet_bdev_flush(struct nvmet_req *req); 747 u16 nvmet_file_flush(struct nvmet_req *req); 748 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid); 749 void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns); 750 void nvmet_file_ns_revalidate(struct nvmet_ns *ns); 751 bool nvmet_ns_revalidate(struct nvmet_ns *ns); 752 u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts); 753 754 bool nvmet_bdev_zns_enable(struct nvmet_ns *ns); 755 void nvmet_execute_identify_ctrl_zns(struct nvmet_req *req); 756 void nvmet_execute_identify_ns_zns(struct nvmet_req *req); 757 void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req); 758 void nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req *req); 759 void nvmet_bdev_execute_zone_append(struct nvmet_req *req); 760 761 static inline u32 nvmet_rw_data_len(struct nvmet_req *req) 762 { 763 return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) << 764 req->ns->blksize_shift; 765 } 766 767 static inline u32 nvmet_rw_metadata_len(struct nvmet_req *req) 768 { 769 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) 770 return 0; 771 return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) * 772 req->ns->metadata_size; 773 } 774 775 static inline u32 nvmet_dsm_len(struct nvmet_req *req) 776 { 777 return (le32_to_cpu(req->cmd->dsm.nr) + 1) * 778 sizeof(struct nvme_dsm_range); 779 } 780 781 static inline struct nvmet_subsys *nvmet_req_subsys(struct nvmet_req *req) 782 { 783 return req->sq->ctrl->subsys; 784 } 785 786 static inline struct nvmet_ctrl *nvmet_req_ctrl(struct nvmet_req *req) 787 { 788 return req->sq->ctrl; 789 } 790 791 static inline bool nvmet_is_disc_subsys(struct nvmet_subsys *subsys) 792 { 793 return subsys->type != NVME_NQN_NVME; 794 } 795 796 static inline bool nvmet_is_pci_ctrl(struct nvmet_ctrl *ctrl) 797 { 798 return ctrl->port->disc_addr.trtype == NVMF_TRTYPE_PCI; 799 } 800 801 /* Limit MDTS according to port config or transport capability */ 802 static inline u8 nvmet_ctrl_mdts(struct nvmet_req *req) 803 { 804 struct nvmet_ctrl *ctrl = req->sq->ctrl; 805 u8 mdts = req->port->mdts; 806 807 if (!ctrl->ops->get_mdts) 808 return mdts; 809 return min_not_zero(ctrl->ops->get_mdts(ctrl), mdts); 810 } 811 812 #ifdef CONFIG_NVME_TARGET_PASSTHRU 813 void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys); 814 int nvmet_passthru_ctrl_enable(struct nvmet_subsys *subsys); 815 void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys); 816 u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req); 817 u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req); 818 static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys) 819 { 820 return subsys->passthru && 821 test_bit(NVMET_PASSTHRU_ENABLED, &subsys->passthru->flags); 822 } 823 824 static inline struct nvmet_passthru *nvmet_subsys_passthru( 825 struct nvmet_subsys *subsys) 826 { 827 lockdep_assert_held(&subsys->lock); 828 829 if (!subsys->passthru) { 830 subsys->passthru = kzalloc_obj(*subsys->passthru); 831 if (!subsys->passthru) 832 return NULL; 833 } 834 return subsys->passthru; 835 } 836 837 static inline struct nvmet_passthru *nvmet_passthru_ref_held( 838 struct nvmet_req *req) 839 { 840 if (req->p.ref_held) 841 return nvmet_req_subsys(req)->passthru; 842 843 return NULL; 844 } 845 846 static inline bool nvmet_get_passthru_ref(struct nvmet_req *req) 847 { 848 if (!percpu_ref_tryget_live(&nvmet_req_subsys(req)->passthru->ref)) 849 return false; 850 851 req->p.ref_held = true; 852 return true; 853 } 854 855 static inline void nvmet_put_passthru_ref(struct nvmet_passthru *passthru) 856 { 857 percpu_ref_put(&passthru->ref); 858 } 859 #else /* CONFIG_NVME_TARGET_PASSTHRU */ 860 static inline void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys) 861 { 862 } 863 static inline void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys) 864 { 865 } 866 static inline u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req) 867 { 868 return 0; 869 } 870 static inline u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req) 871 { 872 return 0; 873 } 874 static inline bool nvmet_is_passthru_subsys(struct nvmet_subsys *subsys) 875 { 876 return NULL; 877 } 878 static inline struct nvmet_passthru *nvmet_subsys_passthru( 879 struct nvmet_subsys *subsys) 880 { 881 return NULL; 882 } 883 static inline struct nvmet_passthru *nvmet_passthru_ref_held( 884 struct nvmet_req *req) 885 { 886 return NULL; 887 } 888 static inline bool nvmet_get_passthru_ref(struct nvmet_req *req) 889 { 890 return false; 891 } 892 static inline void nvmet_put_passthru_ref(struct nvmet_passthru *passthru) 893 { 894 } 895 #endif /* CONFIG_NVME_TARGET_PASSTHRU */ 896 897 static inline bool nvmet_is_passthru_req(struct nvmet_req *req) 898 { 899 return nvmet_is_passthru_subsys(nvmet_req_subsys(req)); 900 } 901 902 void nvmet_passthrough_override_cap(struct nvmet_ctrl *ctrl); 903 904 u16 errno_to_nvme_status(struct nvmet_req *req, int errno); 905 u16 nvmet_report_invalid_opcode(struct nvmet_req *req); 906 907 static inline bool nvmet_cc_en(u32 cc) 908 { 909 return (cc & NVME_CC_ENABLE) >> NVME_CC_EN_SHIFT; 910 } 911 912 static inline u8 nvmet_cc_css(u32 cc) 913 { 914 return (cc & NVME_CC_CSS_MASK) >> NVME_CC_CSS_SHIFT; 915 } 916 917 static inline u8 nvmet_cc_mps(u32 cc) 918 { 919 return (cc & NVME_CC_MPS_MASK) >> NVME_CC_MPS_SHIFT; 920 } 921 922 static inline u8 nvmet_cc_ams(u32 cc) 923 { 924 return (cc & NVME_CC_AMS_MASK) >> NVME_CC_AMS_SHIFT; 925 } 926 927 static inline u8 nvmet_cc_shn(u32 cc) 928 { 929 return (cc & NVME_CC_SHN_MASK) >> NVME_CC_SHN_SHIFT; 930 } 931 932 static inline u8 nvmet_cc_iosqes(u32 cc) 933 { 934 return (cc & NVME_CC_IOSQES_MASK) >> NVME_CC_IOSQES_SHIFT; 935 } 936 937 static inline u8 nvmet_cc_iocqes(u32 cc) 938 { 939 return (cc & NVME_CC_IOCQES_MASK) >> NVME_CC_IOCQES_SHIFT; 940 } 941 942 /* Convert a 32-bit number to a 16-bit 0's based number */ 943 static inline __le16 to0based(u32 a) 944 { 945 return cpu_to_le16(clamp(a, 1U, 1U << 16) - 1); 946 } 947 948 static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns) 949 { 950 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) 951 return false; 952 return ns->pi_type && ns->metadata_size == sizeof(struct t10_pi_tuple); 953 } 954 955 static inline __le64 nvmet_sect_to_lba(struct nvmet_ns *ns, sector_t sect) 956 { 957 return cpu_to_le64(sect >> (ns->blksize_shift - SECTOR_SHIFT)); 958 } 959 960 static inline sector_t nvmet_lba_to_sect(struct nvmet_ns *ns, __le64 lba) 961 { 962 return le64_to_cpu(lba) << (ns->blksize_shift - SECTOR_SHIFT); 963 } 964 965 static inline bool nvmet_use_inline_bvec(struct nvmet_req *req) 966 { 967 return req->transfer_len <= NVMET_MAX_INLINE_DATA_LEN && 968 req->sg_cnt <= NVMET_MAX_INLINE_BIOVEC; 969 } 970 971 static inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio) 972 { 973 if (bio != &req->b.inline_bio) 974 bio_put(bio); 975 else 976 bio_uninit(bio); 977 } 978 979 #ifdef CONFIG_NVME_TARGET_TCP_TLS 980 static inline key_serial_t nvmet_queue_tls_keyid(struct nvmet_sq *sq) 981 { 982 /* A PCI endpoint controller is created without a queue */ 983 return sq && sq->tls_key ? key_serial(sq->tls_key) : 0; 984 } 985 static inline void nvmet_sq_put_tls_key(struct nvmet_sq *sq) 986 { 987 if (sq->tls_key) { 988 key_put(sq->tls_key); 989 sq->tls_key = NULL; 990 } 991 } 992 #else 993 static inline key_serial_t nvmet_queue_tls_keyid(struct nvmet_sq *sq) { return 0; } 994 static inline void nvmet_sq_put_tls_key(struct nvmet_sq *sq) {} 995 #endif 996 #ifdef CONFIG_NVME_TARGET_AUTH 997 u32 nvmet_auth_send_data_len(struct nvmet_req *req); 998 void nvmet_execute_auth_send(struct nvmet_req *req); 999 u32 nvmet_auth_receive_data_len(struct nvmet_req *req); 1000 void nvmet_execute_auth_receive(struct nvmet_req *req); 1001 int nvmet_auth_set_key(struct nvmet_host *host, const char *secret, 1002 bool set_ctrl); 1003 int nvmet_auth_set_host_hash(struct nvmet_host *host, const char *hash); 1004 u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset); 1005 void nvmet_auth_sq_init(struct nvmet_sq *sq); 1006 void nvmet_destroy_auth(struct nvmet_ctrl *ctrl); 1007 void nvmet_auth_sq_free(struct nvmet_sq *sq); 1008 void nvmet_auth_sq_destroy(struct nvmet_sq *sq); 1009 int nvmet_setup_dhgroup(struct nvmet_ctrl *ctrl, u8 dhgroup_id); 1010 bool nvmet_check_auth_status(struct nvmet_req *req); 1011 int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response, 1012 unsigned int hash_len); 1013 int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response, 1014 unsigned int hash_len); 1015 static inline bool nvmet_has_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq) 1016 { 1017 return ctrl->host_key != NULL && !nvmet_queue_tls_keyid(sq); 1018 } 1019 int nvmet_auth_ctrl_exponential(struct nvmet_req *req, 1020 u8 *buf, int buf_size); 1021 int nvmet_auth_ctrl_sesskey(struct nvmet_req *req, 1022 const u8 *pkey, int pkey_size); 1023 void nvmet_auth_insert_psk(struct nvmet_sq *sq); 1024 #else 1025 static inline u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, 1026 struct nvmet_sq *sq, bool reset) 1027 { 1028 return 0; 1029 } 1030 static inline void nvmet_auth_sq_init(struct nvmet_sq *sq) 1031 { 1032 } 1033 static inline void nvmet_destroy_auth(struct nvmet_ctrl *ctrl) {}; 1034 static inline void nvmet_auth_sq_free(struct nvmet_sq *sq) {}; 1035 static inline void nvmet_auth_sq_destroy(struct nvmet_sq *sq) {}; 1036 static inline bool nvmet_check_auth_status(struct nvmet_req *req) 1037 { 1038 return true; 1039 } 1040 static inline bool nvmet_has_auth(struct nvmet_ctrl *ctrl, 1041 struct nvmet_sq *sq) 1042 { 1043 return false; 1044 } 1045 static inline const char *nvmet_dhchap_dhgroup_name(u8 dhgid) { return NULL; } 1046 static inline void nvmet_auth_insert_psk(struct nvmet_sq *sq) {}; 1047 #endif 1048 1049 int nvmet_pr_init_ns(struct nvmet_ns *ns); 1050 u16 nvmet_parse_pr_cmd(struct nvmet_req *req); 1051 u16 nvmet_pr_check_cmd_access(struct nvmet_req *req); 1052 int nvmet_ctrl_init_pr(struct nvmet_ctrl *ctrl); 1053 void nvmet_ctrl_destroy_pr(struct nvmet_ctrl *ctrl); 1054 void nvmet_pr_exit_ns(struct nvmet_ns *ns); 1055 void nvmet_execute_get_log_page_resv(struct nvmet_req *req); 1056 u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask); 1057 u16 nvmet_get_feat_resv_notif_mask(struct nvmet_req *req); 1058 u16 nvmet_pr_get_ns_pc_ref(struct nvmet_req *req); 1059 static inline void nvmet_pr_put_ns_pc_ref(struct nvmet_pr_per_ctrl_ref *pc_ref) 1060 { 1061 percpu_ref_put(&pc_ref->ref); 1062 } 1063 1064 /* 1065 * Data for the get_feature() and set_feature() operations of PCI target 1066 * controllers. 1067 */ 1068 struct nvmet_feat_irq_coalesce { 1069 u8 thr; 1070 u8 time; 1071 }; 1072 1073 struct nvmet_feat_irq_config { 1074 u16 iv; 1075 bool cd; 1076 }; 1077 1078 struct nvmet_feat_arbitration { 1079 u8 hpw; 1080 u8 mpw; 1081 u8 lpw; 1082 u8 ab; 1083 }; 1084 1085 #endif /* _NVMET_H */ 1086