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