177141dc6SChristoph Hellwig /* SPDX-License-Identifier: GPL-2.0 */ 2a07b4970SChristoph Hellwig /* 3a07b4970SChristoph Hellwig * Copyright (c) 2015-2016 HGST, a Western Digital Company. 4a07b4970SChristoph Hellwig */ 5a07b4970SChristoph Hellwig 6a07b4970SChristoph Hellwig #ifndef _NVMET_H 7a07b4970SChristoph Hellwig #define _NVMET_H 8a07b4970SChristoph Hellwig 9a07b4970SChristoph Hellwig #include <linux/dma-mapping.h> 10a07b4970SChristoph Hellwig #include <linux/types.h> 11a07b4970SChristoph Hellwig #include <linux/device.h> 12a07b4970SChristoph Hellwig #include <linux/kref.h> 13a07b4970SChristoph Hellwig #include <linux/percpu-refcount.h> 14a07b4970SChristoph Hellwig #include <linux/list.h> 15a07b4970SChristoph Hellwig #include <linux/mutex.h> 168e412263SChristoph Hellwig #include <linux/uuid.h> 17a07b4970SChristoph Hellwig #include <linux/nvme.h> 18a07b4970SChristoph Hellwig #include <linux/configfs.h> 19a07b4970SChristoph Hellwig #include <linux/rcupdate.h> 20a07b4970SChristoph Hellwig #include <linux/blkdev.h> 21c6925093SLogan Gunthorpe #include <linux/radix-tree.h> 22d2d1c454SIsrael Rukshin #include <linux/t10-pi.h> 23a07b4970SChristoph Hellwig 24ba76af67SLogan Gunthorpe #define NVMET_DEFAULT_VS NVME_VS(1, 3, 0) 25ba76af67SLogan Gunthorpe 26a07b4970SChristoph Hellwig #define NVMET_ASYNC_EVENTS 4 27a07b4970SChristoph Hellwig #define NVMET_ERROR_LOG_SLOTS 128 285698b805SChaitanya Kulkarni #define NVMET_NO_ERROR_LOC ((u16)-1) 29013b7ebeSMark Ruijter #define NVMET_DEFAULT_CTRL_MODEL "Linux" 30a07b4970SChristoph Hellwig 31c86b8f7bSChristoph Hellwig /* 32c86b8f7bSChristoph Hellwig * Supported optional AENs: 33c86b8f7bSChristoph Hellwig */ 34c86b8f7bSChristoph Hellwig #define NVMET_AEN_CFG_OPTIONAL \ 3562ac0d32SChristoph Hellwig (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE) 36f301c2b1SJay Sternberg #define NVMET_DISC_AEN_CFG_OPTIONAL \ 37f301c2b1SJay Sternberg (NVME_AEN_CFG_DISC_CHANGE) 38c86b8f7bSChristoph Hellwig 39c86b8f7bSChristoph Hellwig /* 40c86b8f7bSChristoph Hellwig * Plus mandatory SMART AENs (we'll never send them, but allow enabling them): 41c86b8f7bSChristoph Hellwig */ 42c86b8f7bSChristoph Hellwig #define NVMET_AEN_CFG_ALL \ 43c86b8f7bSChristoph Hellwig (NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \ 44c86b8f7bSChristoph Hellwig NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \ 45c86b8f7bSChristoph Hellwig NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL) 46c86b8f7bSChristoph Hellwig 47a07b4970SChristoph Hellwig /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM 48a07b4970SChristoph Hellwig * The 16 bit shift is to set IATTR bit to 1, which means offending 49a07b4970SChristoph Hellwig * offset starts in the data section of connect() 50a07b4970SChristoph Hellwig */ 51a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_DATA(x) \ 52a07b4970SChristoph Hellwig (cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x)))) 53a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_SQE(x) \ 54a07b4970SChristoph Hellwig (cpu_to_le32(offsetof(struct nvmf_connect_command, x))) 55a07b4970SChristoph Hellwig 56a07b4970SChristoph Hellwig struct nvmet_ns { 57a07b4970SChristoph Hellwig struct percpu_ref ref; 58a07b4970SChristoph Hellwig struct block_device *bdev; 59d5eff33eSChaitanya Kulkarni struct file *file; 60dedf0be5SChaitanya Kulkarni bool readonly; 61a07b4970SChristoph Hellwig u32 nsid; 62a07b4970SChristoph Hellwig u32 blksize_shift; 63a07b4970SChristoph Hellwig loff_t size; 64a07b4970SChristoph Hellwig u8 nguid[16]; 65637dc0f3SJohannes Thumshirn uuid_t uuid; 6672efd25dSChristoph Hellwig u32 anagrpid; 67a07b4970SChristoph Hellwig 6855eb942eSChaitanya Kulkarni bool buffered_io; 69e4fcf07cSSolganik Alexander bool enabled; 70a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 71a07b4970SChristoph Hellwig const char *device_path; 72a07b4970SChristoph Hellwig 73a07b4970SChristoph Hellwig struct config_group device_group; 74a07b4970SChristoph Hellwig struct config_group group; 75a07b4970SChristoph Hellwig 76a07b4970SChristoph Hellwig struct completion disable_done; 77d5eff33eSChaitanya Kulkarni mempool_t *bvec_pool; 78d5eff33eSChaitanya Kulkarni struct kmem_cache *bvec_cache; 79c6925093SLogan Gunthorpe 80c6925093SLogan Gunthorpe int use_p2pmem; 81c6925093SLogan Gunthorpe struct pci_dev *p2p_dev; 82d2d1c454SIsrael Rukshin int pi_type; 83d2d1c454SIsrael Rukshin int metadata_size; 84a07b4970SChristoph Hellwig }; 85a07b4970SChristoph Hellwig 86a07b4970SChristoph Hellwig static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item) 87a07b4970SChristoph Hellwig { 88a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_ns, group); 89a07b4970SChristoph Hellwig } 90a07b4970SChristoph Hellwig 91c6925093SLogan Gunthorpe static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns) 92c6925093SLogan Gunthorpe { 93c6925093SLogan Gunthorpe return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL; 94c6925093SLogan Gunthorpe } 95c6925093SLogan Gunthorpe 96a07b4970SChristoph Hellwig struct nvmet_cq { 97a07b4970SChristoph Hellwig u16 qid; 98a07b4970SChristoph Hellwig u16 size; 99a07b4970SChristoph Hellwig }; 100a07b4970SChristoph Hellwig 101a07b4970SChristoph Hellwig struct nvmet_sq { 102a07b4970SChristoph Hellwig struct nvmet_ctrl *ctrl; 103a07b4970SChristoph Hellwig struct percpu_ref ref; 104a07b4970SChristoph Hellwig u16 qid; 105a07b4970SChristoph Hellwig u16 size; 106f9cf2a64SJames Smart u32 sqhd; 107e6a622fdSSagi Grimberg bool sqhd_disabled; 108a07b4970SChristoph Hellwig struct completion free_done; 109427242ceSSagi Grimberg struct completion confirm_done; 110a07b4970SChristoph Hellwig }; 111a07b4970SChristoph Hellwig 11262ac0d32SChristoph Hellwig struct nvmet_ana_group { 11362ac0d32SChristoph Hellwig struct config_group group; 11462ac0d32SChristoph Hellwig struct nvmet_port *port; 11562ac0d32SChristoph Hellwig u32 grpid; 11662ac0d32SChristoph Hellwig }; 11762ac0d32SChristoph Hellwig 11862ac0d32SChristoph Hellwig static inline struct nvmet_ana_group *to_ana_group(struct config_item *item) 11962ac0d32SChristoph Hellwig { 12062ac0d32SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_ana_group, 12162ac0d32SChristoph Hellwig group); 12262ac0d32SChristoph Hellwig } 12362ac0d32SChristoph Hellwig 124a07b4970SChristoph Hellwig /** 125a07b4970SChristoph Hellwig * struct nvmet_port - Common structure to keep port 126a07b4970SChristoph Hellwig * information for the target. 127fe4a9791SChristoph Hellwig * @entry: Entry into referrals or transport list. 128a07b4970SChristoph Hellwig * @disc_addr: Address information is stored in a format defined 129a07b4970SChristoph Hellwig * for a discovery log page entry. 130a07b4970SChristoph Hellwig * @group: ConfigFS group for this element's folder. 131a07b4970SChristoph Hellwig * @priv: Private data for the transport. 132a07b4970SChristoph Hellwig */ 133a07b4970SChristoph Hellwig struct nvmet_port { 134a07b4970SChristoph Hellwig struct list_head entry; 135a07b4970SChristoph Hellwig struct nvmf_disc_rsp_page_entry disc_addr; 136a07b4970SChristoph Hellwig struct config_group group; 137a07b4970SChristoph Hellwig struct config_group subsys_group; 138a07b4970SChristoph Hellwig struct list_head subsystems; 139a07b4970SChristoph Hellwig struct config_group referrals_group; 140a07b4970SChristoph Hellwig struct list_head referrals; 141b662a078SJay Sternberg struct list_head global_entry; 14262ac0d32SChristoph Hellwig struct config_group ana_groups_group; 14362ac0d32SChristoph Hellwig struct nvmet_ana_group ana_default_group; 14472efd25dSChristoph Hellwig enum nvme_ana_state *ana_state; 145a07b4970SChristoph Hellwig void *priv; 146a07b4970SChristoph Hellwig bool enabled; 1470d5ee2b2SSteve Wise int inline_data_size; 1489d09dd8dSJames Smart const struct nvmet_fabrics_ops *tr_ops; 149ea52ac1cSIsrael Rukshin bool pi_enable; 150a07b4970SChristoph Hellwig }; 151a07b4970SChristoph Hellwig 152a07b4970SChristoph Hellwig static inline struct nvmet_port *to_nvmet_port(struct config_item *item) 153a07b4970SChristoph Hellwig { 154a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_port, 155a07b4970SChristoph Hellwig group); 156a07b4970SChristoph Hellwig } 157a07b4970SChristoph Hellwig 15862ac0d32SChristoph Hellwig static inline struct nvmet_port *ana_groups_to_port( 15962ac0d32SChristoph Hellwig struct config_item *item) 16062ac0d32SChristoph Hellwig { 16162ac0d32SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_port, 16262ac0d32SChristoph Hellwig ana_groups_group); 16362ac0d32SChristoph Hellwig } 16462ac0d32SChristoph Hellwig 165a07b4970SChristoph Hellwig struct nvmet_ctrl { 166a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 167a07b4970SChristoph Hellwig struct nvmet_sq **sqs; 168a07b4970SChristoph Hellwig 169c09305aeSSagi Grimberg bool cmd_seen; 170c09305aeSSagi Grimberg 171a07b4970SChristoph Hellwig struct mutex lock; 172a07b4970SChristoph Hellwig u64 cap; 173a07b4970SChristoph Hellwig u32 cc; 174a07b4970SChristoph Hellwig u32 csts; 175a07b4970SChristoph Hellwig 17628dd5cf7SOmri Mann uuid_t hostid; 177a07b4970SChristoph Hellwig u16 cntlid; 178a07b4970SChristoph Hellwig u32 kato; 179a07b4970SChristoph Hellwig 1804ee43280SChristoph Hellwig struct nvmet_port *port; 1814ee43280SChristoph Hellwig 182c86b8f7bSChristoph Hellwig u32 aen_enabled; 18355fdd6b6SChristoph Hellwig unsigned long aen_masked; 184a07b4970SChristoph Hellwig struct nvmet_req *async_event_cmds[NVMET_ASYNC_EVENTS]; 185a07b4970SChristoph Hellwig unsigned int nr_async_event_cmds; 186a07b4970SChristoph Hellwig struct list_head async_events; 187a07b4970SChristoph Hellwig struct work_struct async_event_work; 188a07b4970SChristoph Hellwig 189a07b4970SChristoph Hellwig struct list_head subsys_entry; 190a07b4970SChristoph Hellwig struct kref ref; 191a07b4970SChristoph Hellwig struct delayed_work ka_work; 192a07b4970SChristoph Hellwig struct work_struct fatal_err_work; 193a07b4970SChristoph Hellwig 194e929f06dSChristoph Hellwig const struct nvmet_fabrics_ops *ops; 195a07b4970SChristoph Hellwig 196c16734eaSChristoph Hellwig __le32 *changed_ns_list; 197c16734eaSChristoph Hellwig u32 nr_changed_ns; 198c16734eaSChristoph Hellwig 199a07b4970SChristoph Hellwig char subsysnqn[NVMF_NQN_FIELD_LEN]; 200a07b4970SChristoph Hellwig char hostnqn[NVMF_NQN_FIELD_LEN]; 201c6925093SLogan Gunthorpe 202c6925093SLogan Gunthorpe struct device *p2p_client; 203c6925093SLogan Gunthorpe struct radix_tree_root p2p_ns_map; 204e4a97625SChaitanya Kulkarni 205e4a97625SChaitanya Kulkarni spinlock_t error_lock; 206e4a97625SChaitanya Kulkarni u64 err_counter; 207e4a97625SChaitanya Kulkarni struct nvme_error_slot slots[NVMET_ERROR_LOG_SLOTS]; 208ea52ac1cSIsrael Rukshin bool pi_support; 209a07b4970SChristoph Hellwig }; 210a07b4970SChristoph Hellwig 211013b7ebeSMark Ruijter struct nvmet_subsys_model { 212013b7ebeSMark Ruijter struct rcu_head rcuhead; 213013b7ebeSMark Ruijter char number[]; 214013b7ebeSMark Ruijter }; 215013b7ebeSMark Ruijter 216a07b4970SChristoph Hellwig struct nvmet_subsys { 217a07b4970SChristoph Hellwig enum nvme_subsys_type type; 218a07b4970SChristoph Hellwig 219a07b4970SChristoph Hellwig struct mutex lock; 220a07b4970SChristoph Hellwig struct kref ref; 221a07b4970SChristoph Hellwig 2227774e77eSChaitanya Kulkarni struct xarray namespaces; 223793c7cfcSChristoph Hellwig unsigned int nr_namespaces; 224a07b4970SChristoph Hellwig unsigned int max_nsid; 22594a39d61SChaitanya Kulkarni u16 cntlid_min; 22694a39d61SChaitanya Kulkarni u16 cntlid_max; 227a07b4970SChristoph Hellwig 228a07b4970SChristoph Hellwig struct list_head ctrls; 229a07b4970SChristoph Hellwig 230a07b4970SChristoph Hellwig struct list_head hosts; 231a07b4970SChristoph Hellwig bool allow_any_host; 232a07b4970SChristoph Hellwig 233a07b4970SChristoph Hellwig u16 max_qid; 234a07b4970SChristoph Hellwig 235a07b4970SChristoph Hellwig u64 ver; 2362e7f5d2aSJohannes Thumshirn u64 serial; 237a07b4970SChristoph Hellwig char *subsysnqn; 238ea52ac1cSIsrael Rukshin bool pi_support; 239a07b4970SChristoph Hellwig 240a07b4970SChristoph Hellwig struct config_group group; 241a07b4970SChristoph Hellwig 242a07b4970SChristoph Hellwig struct config_group namespaces_group; 243a07b4970SChristoph Hellwig struct config_group allowed_hosts_group; 244013b7ebeSMark Ruijter 245013b7ebeSMark Ruijter struct nvmet_subsys_model __rcu *model; 246c1fef73fSLogan Gunthorpe 247c1fef73fSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU 248c1fef73fSLogan Gunthorpe struct nvme_ctrl *passthru_ctrl; 249ba76af67SLogan Gunthorpe char *passthru_ctrl_path; 250cae5b01aSLogan Gunthorpe struct config_group passthru_group; 251a2f6a2b8SChaitanya Kulkarni unsigned int admin_timeout; 25247e9730cSChaitanya Kulkarni unsigned int io_timeout; 253c1fef73fSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */ 254a07b4970SChristoph Hellwig }; 255a07b4970SChristoph Hellwig 256a07b4970SChristoph Hellwig static inline struct nvmet_subsys *to_subsys(struct config_item *item) 257a07b4970SChristoph Hellwig { 258a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_subsys, group); 259a07b4970SChristoph Hellwig } 260a07b4970SChristoph Hellwig 261a07b4970SChristoph Hellwig static inline struct nvmet_subsys *namespaces_to_subsys( 262a07b4970SChristoph Hellwig struct config_item *item) 263a07b4970SChristoph Hellwig { 264a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_subsys, 265a07b4970SChristoph Hellwig namespaces_group); 266a07b4970SChristoph Hellwig } 267a07b4970SChristoph Hellwig 268a07b4970SChristoph Hellwig struct nvmet_host { 269a07b4970SChristoph Hellwig struct config_group group; 270a07b4970SChristoph Hellwig }; 271a07b4970SChristoph Hellwig 272a07b4970SChristoph Hellwig static inline struct nvmet_host *to_host(struct config_item *item) 273a07b4970SChristoph Hellwig { 274a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_host, group); 275a07b4970SChristoph Hellwig } 276a07b4970SChristoph Hellwig 277a07b4970SChristoph Hellwig static inline char *nvmet_host_name(struct nvmet_host *host) 278a07b4970SChristoph Hellwig { 279a07b4970SChristoph Hellwig return config_item_name(&host->group.cg_item); 280a07b4970SChristoph Hellwig } 281a07b4970SChristoph Hellwig 282a07b4970SChristoph Hellwig struct nvmet_host_link { 283a07b4970SChristoph Hellwig struct list_head entry; 284a07b4970SChristoph Hellwig struct nvmet_host *host; 285a07b4970SChristoph Hellwig }; 286a07b4970SChristoph Hellwig 287a07b4970SChristoph Hellwig struct nvmet_subsys_link { 288a07b4970SChristoph Hellwig struct list_head entry; 289a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 290a07b4970SChristoph Hellwig }; 291a07b4970SChristoph Hellwig 292a07b4970SChristoph Hellwig struct nvmet_req; 293a07b4970SChristoph Hellwig struct nvmet_fabrics_ops { 294a07b4970SChristoph Hellwig struct module *owner; 295a07b4970SChristoph Hellwig unsigned int type; 296a07b4970SChristoph Hellwig unsigned int msdbd; 2976fa350f7SMax Gurtovoy unsigned int flags; 2986fa350f7SMax Gurtovoy #define NVMF_KEYED_SGLS (1 << 0) 2996fa350f7SMax Gurtovoy #define NVMF_METADATA_SUPPORTED (1 << 1) 300a07b4970SChristoph Hellwig void (*queue_response)(struct nvmet_req *req); 301a07b4970SChristoph Hellwig int (*add_port)(struct nvmet_port *port); 302a07b4970SChristoph Hellwig void (*remove_port)(struct nvmet_port *port); 303a07b4970SChristoph Hellwig void (*delete_ctrl)(struct nvmet_ctrl *ctrl); 3044c652685SSagi Grimberg void (*disc_traddr)(struct nvmet_req *req, 3054c652685SSagi Grimberg struct nvmet_port *port, char *traddr); 3061672ddb8SSagi Grimberg u16 (*install_queue)(struct nvmet_sq *nvme_sq); 3079d09dd8dSJames Smart void (*discovery_chg)(struct nvmet_port *port); 30802cb00e2SMax Gurtovoy u8 (*get_mdts)(const struct nvmet_ctrl *ctrl); 309a07b4970SChristoph Hellwig }; 310a07b4970SChristoph Hellwig 311a07b4970SChristoph Hellwig #define NVMET_MAX_INLINE_BIOVEC 8 31273383adfSSagi Grimberg #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE 313a07b4970SChristoph Hellwig 314a07b4970SChristoph Hellwig struct nvmet_req { 315a07b4970SChristoph Hellwig struct nvme_command *cmd; 316fc6c9730SMax Gurtovoy struct nvme_completion *cqe; 317a07b4970SChristoph Hellwig struct nvmet_sq *sq; 318a07b4970SChristoph Hellwig struct nvmet_cq *cq; 319a07b4970SChristoph Hellwig struct nvmet_ns *ns; 320a07b4970SChristoph Hellwig struct scatterlist *sg; 321c6e3f133SIsrael Rukshin struct scatterlist *metadata_sg; 322a07b4970SChristoph Hellwig struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC]; 323d5eff33eSChaitanya Kulkarni union { 324d5eff33eSChaitanya Kulkarni struct { 325d5eff33eSChaitanya Kulkarni struct bio inline_bio; 326d5eff33eSChaitanya Kulkarni } b; 327d5eff33eSChaitanya Kulkarni struct { 328d5eff33eSChaitanya Kulkarni bool mpool_alloc; 329d5eff33eSChaitanya Kulkarni struct kiocb iocb; 330d5eff33eSChaitanya Kulkarni struct bio_vec *bvec; 331d5eff33eSChaitanya Kulkarni struct work_struct work; 332d5eff33eSChaitanya Kulkarni } f; 333c1fef73fSLogan Gunthorpe struct { 334dab3902bSChaitanya Kulkarni struct bio inline_bio; 335c1fef73fSLogan Gunthorpe struct request *rq; 336c1fef73fSLogan Gunthorpe struct work_struct work; 337c1fef73fSLogan Gunthorpe bool use_workqueue; 338c1fef73fSLogan Gunthorpe } p; 339d5eff33eSChaitanya Kulkarni }; 340a07b4970SChristoph Hellwig int sg_cnt; 341c6e3f133SIsrael Rukshin int metadata_sg_cnt; 3425e62d5c9SChristoph Hellwig /* data length as parsed from the SGL descriptor: */ 3435e62d5c9SChristoph Hellwig size_t transfer_len; 344c6e3f133SIsrael Rukshin size_t metadata_len; 345a07b4970SChristoph Hellwig 346a07b4970SChristoph Hellwig struct nvmet_port *port; 347a07b4970SChristoph Hellwig 348a07b4970SChristoph Hellwig void (*execute)(struct nvmet_req *req); 349e929f06dSChristoph Hellwig const struct nvmet_fabrics_ops *ops; 350c6925093SLogan Gunthorpe 351c6925093SLogan Gunthorpe struct pci_dev *p2p_dev; 352c6925093SLogan Gunthorpe struct device *p2p_client; 353e4a97625SChaitanya Kulkarni u16 error_loc; 354e4a97625SChaitanya Kulkarni u64 error_slba; 355a07b4970SChristoph Hellwig }; 356a07b4970SChristoph Hellwig 35755eb942eSChaitanya Kulkarni extern struct workqueue_struct *buffered_io_wq; 35855eb942eSChaitanya Kulkarni 359a07b4970SChristoph Hellwig static inline void nvmet_set_result(struct nvmet_req *req, u32 result) 360a07b4970SChristoph Hellwig { 361fc6c9730SMax Gurtovoy req->cqe->result.u32 = cpu_to_le32(result); 362a07b4970SChristoph Hellwig } 363a07b4970SChristoph Hellwig 364a07b4970SChristoph Hellwig /* 365a07b4970SChristoph Hellwig * NVMe command writes actually are DMA reads for us on the target side. 366a07b4970SChristoph Hellwig */ 367a07b4970SChristoph Hellwig static inline enum dma_data_direction 368a07b4970SChristoph Hellwig nvmet_data_dir(struct nvmet_req *req) 369a07b4970SChristoph Hellwig { 370a07b4970SChristoph Hellwig return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 371a07b4970SChristoph Hellwig } 372a07b4970SChristoph Hellwig 373a07b4970SChristoph Hellwig struct nvmet_async_event { 374a07b4970SChristoph Hellwig struct list_head entry; 375a07b4970SChristoph Hellwig u8 event_type; 376a07b4970SChristoph Hellwig u8 event_info; 377a07b4970SChristoph Hellwig u8 log_page; 378a07b4970SChristoph Hellwig }; 379a07b4970SChristoph Hellwig 3807114ddebSJay Sternberg static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn) 3816c8312adSJay Sternberg { 382b7c8f366SChaitanya Kulkarni int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15; 3836c8312adSJay Sternberg 3846c8312adSJay Sternberg if (!rae) 3857114ddebSJay Sternberg clear_bit(bn, &req->sq->ctrl->aen_masked); 3866c8312adSJay Sternberg } 3876c8312adSJay Sternberg 3887114ddebSJay Sternberg static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn) 3896c8312adSJay Sternberg { 3907114ddebSJay Sternberg if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn))) 3916c8312adSJay Sternberg return true; 3927114ddebSJay Sternberg return test_and_set_bit(bn, &ctrl->aen_masked); 3936c8312adSJay Sternberg } 3946c8312adSJay Sternberg 39590107455SJay Sternberg void nvmet_get_feat_kato(struct nvmet_req *req); 39690107455SJay Sternberg void nvmet_get_feat_async_event(struct nvmet_req *req); 39790107455SJay Sternberg u16 nvmet_set_feat_kato(struct nvmet_req *req); 39890107455SJay Sternberg u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask); 39990107455SJay Sternberg void nvmet_execute_async_event(struct nvmet_req *req); 4004e683c48SAmit Engel void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl); 4014e683c48SAmit Engel void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl); 40290107455SJay Sternberg 40364a0ca88SParav Pandit u16 nvmet_parse_connect_cmd(struct nvmet_req *req); 4049d05a96eSBart Van Assche void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id); 405d5eff33eSChaitanya Kulkarni u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req); 406d5eff33eSChaitanya Kulkarni u16 nvmet_file_parse_io_cmd(struct nvmet_req *req); 40764a0ca88SParav Pandit u16 nvmet_parse_admin_cmd(struct nvmet_req *req); 40864a0ca88SParav Pandit u16 nvmet_parse_discovery_cmd(struct nvmet_req *req); 40964a0ca88SParav Pandit u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req); 410a07b4970SChristoph Hellwig 411a07b4970SChristoph Hellwig bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq, 412e929f06dSChristoph Hellwig struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops); 413549f01aeSVijay Immanuel void nvmet_req_uninit(struct nvmet_req *req); 414136cc1ffSIsrael Rukshin bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len); 415b716e688SSagi Grimberg bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len); 416a07b4970SChristoph Hellwig void nvmet_req_complete(struct nvmet_req *req, u16 status); 417c6e3f133SIsrael Rukshin int nvmet_req_alloc_sgls(struct nvmet_req *req); 418c6e3f133SIsrael Rukshin void nvmet_req_free_sgls(struct nvmet_req *req); 419a07b4970SChristoph Hellwig 420c1fef73fSLogan Gunthorpe void nvmet_execute_set_features(struct nvmet_req *req); 421c1fef73fSLogan Gunthorpe void nvmet_execute_get_features(struct nvmet_req *req); 422f9362ac1SJay Sternberg void nvmet_execute_keep_alive(struct nvmet_req *req); 423f9362ac1SJay Sternberg 424a07b4970SChristoph Hellwig void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid, 425a07b4970SChristoph Hellwig u16 size); 426a07b4970SChristoph Hellwig void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid, 427a07b4970SChristoph Hellwig u16 size); 428a07b4970SChristoph Hellwig void nvmet_sq_destroy(struct nvmet_sq *sq); 429a07b4970SChristoph Hellwig int nvmet_sq_init(struct nvmet_sq *sq); 430a07b4970SChristoph Hellwig 431a07b4970SChristoph Hellwig void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl); 432a07b4970SChristoph Hellwig 433a07b4970SChristoph Hellwig void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new); 434a07b4970SChristoph Hellwig u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn, 435a07b4970SChristoph Hellwig struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp); 436a07b4970SChristoph Hellwig u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid, 437a07b4970SChristoph Hellwig struct nvmet_req *req, struct nvmet_ctrl **ret); 438a07b4970SChristoph Hellwig void nvmet_ctrl_put(struct nvmet_ctrl *ctrl); 43964a0ca88SParav Pandit u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd); 440a07b4970SChristoph Hellwig 441a07b4970SChristoph Hellwig struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, 442a07b4970SChristoph Hellwig enum nvme_subsys_type type); 443a07b4970SChristoph Hellwig void nvmet_subsys_put(struct nvmet_subsys *subsys); 444344770b0SSagi Grimberg void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys); 445a07b4970SChristoph Hellwig 446a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid); 447a07b4970SChristoph Hellwig void nvmet_put_namespace(struct nvmet_ns *ns); 448a07b4970SChristoph Hellwig int nvmet_ns_enable(struct nvmet_ns *ns); 449a07b4970SChristoph Hellwig void nvmet_ns_disable(struct nvmet_ns *ns); 450a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid); 451a07b4970SChristoph Hellwig void nvmet_ns_free(struct nvmet_ns *ns); 452a07b4970SChristoph Hellwig 45362ac0d32SChristoph Hellwig void nvmet_send_ana_event(struct nvmet_subsys *subsys, 45462ac0d32SChristoph Hellwig struct nvmet_port *port); 45562ac0d32SChristoph Hellwig void nvmet_port_send_ana_event(struct nvmet_port *port); 45662ac0d32SChristoph Hellwig 457e929f06dSChristoph Hellwig int nvmet_register_transport(const struct nvmet_fabrics_ops *ops); 458e929f06dSChristoph Hellwig void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops); 459a07b4970SChristoph Hellwig 4603aed8673SLogan Gunthorpe void nvmet_port_del_ctrls(struct nvmet_port *port, 4613aed8673SLogan Gunthorpe struct nvmet_subsys *subsys); 4623aed8673SLogan Gunthorpe 463a07b4970SChristoph Hellwig int nvmet_enable_port(struct nvmet_port *port); 464a07b4970SChristoph Hellwig void nvmet_disable_port(struct nvmet_port *port); 465a07b4970SChristoph Hellwig 466a07b4970SChristoph Hellwig void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port); 467b662a078SJay Sternberg void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port); 468a07b4970SChristoph Hellwig 469a07b4970SChristoph Hellwig u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf, 470a07b4970SChristoph Hellwig size_t len); 471a07b4970SChristoph Hellwig u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, 472a07b4970SChristoph Hellwig size_t len); 473c7759fffSChristoph Hellwig u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len); 474a07b4970SChristoph Hellwig 475a07b4970SChristoph Hellwig u32 nvmet_get_log_page_len(struct nvme_command *cmd); 476d808b7f7SKeith Busch u64 nvmet_get_log_page_offset(struct nvme_command *cmd); 477a07b4970SChristoph Hellwig 478b662a078SJay Sternberg extern struct list_head *nvmet_ports; 479b662a078SJay Sternberg void nvmet_port_disc_changed(struct nvmet_port *port, 480b662a078SJay Sternberg struct nvmet_subsys *subsys); 481b662a078SJay Sternberg void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys, 482b662a078SJay Sternberg struct nvmet_host *host); 483b662a078SJay Sternberg void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type, 484b662a078SJay Sternberg u8 event_info, u8 log_page); 485b662a078SJay Sternberg 486a07b4970SChristoph Hellwig #define NVMET_QUEUE_SIZE 1024 4873375e29cSJames Smart #define NVMET_NR_QUEUES 128 488a07b4970SChristoph Hellwig #define NVMET_MAX_CMD NVMET_QUEUE_SIZE 489793c7cfcSChristoph Hellwig 490793c7cfcSChristoph Hellwig /* 491793c7cfcSChristoph Hellwig * Nice round number that makes a list of nsids fit into a page. 492793c7cfcSChristoph Hellwig * Should become tunable at some point in the future. 493793c7cfcSChristoph Hellwig */ 494793c7cfcSChristoph Hellwig #define NVMET_MAX_NAMESPACES 1024 495793c7cfcSChristoph Hellwig 49672efd25dSChristoph Hellwig /* 49772efd25dSChristoph Hellwig * 0 is not a valid ANA group ID, so we start numbering at 1. 49872efd25dSChristoph Hellwig * 49972efd25dSChristoph Hellwig * ANA Group 1 exists without manual intervention, has namespaces assigned to it 50072efd25dSChristoph Hellwig * by default, and is available in an optimized state through all ports. 50172efd25dSChristoph Hellwig */ 50262ac0d32SChristoph Hellwig #define NVMET_MAX_ANAGRPS 128 50372efd25dSChristoph Hellwig #define NVMET_DEFAULT_ANA_GRPID 1 50472efd25dSChristoph Hellwig 505a07b4970SChristoph Hellwig #define NVMET_KAS 10 506f9362ac1SJay Sternberg #define NVMET_DISC_KATO_MS 120000 507a07b4970SChristoph Hellwig 508a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void); 509a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void); 510a07b4970SChristoph Hellwig 511a07b4970SChristoph Hellwig int __init nvmet_init_discovery(void); 512a07b4970SChristoph Hellwig void nvmet_exit_discovery(void); 513a07b4970SChristoph Hellwig 514a07b4970SChristoph Hellwig extern struct nvmet_subsys *nvmet_disc_subsys; 515a07b4970SChristoph Hellwig extern struct rw_semaphore nvmet_config_sem; 516a07b4970SChristoph Hellwig 51772efd25dSChristoph Hellwig extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1]; 51872efd25dSChristoph Hellwig extern u64 nvmet_ana_chgcnt; 51972efd25dSChristoph Hellwig extern struct rw_semaphore nvmet_ana_sem; 52072efd25dSChristoph Hellwig 521253928eeSSagi Grimberg bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn); 522a07b4970SChristoph Hellwig 523d5eff33eSChaitanya Kulkarni int nvmet_bdev_ns_enable(struct nvmet_ns *ns); 524d5eff33eSChaitanya Kulkarni int nvmet_file_ns_enable(struct nvmet_ns *ns); 525d5eff33eSChaitanya Kulkarni void nvmet_bdev_ns_disable(struct nvmet_ns *ns); 526d5eff33eSChaitanya Kulkarni void nvmet_file_ns_disable(struct nvmet_ns *ns); 527dedf0be5SChaitanya Kulkarni u16 nvmet_bdev_flush(struct nvmet_req *req); 528dedf0be5SChaitanya Kulkarni u16 nvmet_file_flush(struct nvmet_req *req); 529dedf0be5SChaitanya Kulkarni void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid); 530e8cd1ff1SAnthony Iliopoulos void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns); 531e8cd1ff1SAnthony Iliopoulos int nvmet_file_ns_revalidate(struct nvmet_ns *ns); 532463c5fabSChaitanya Kulkarni void nvmet_ns_revalidate(struct nvmet_ns *ns); 533d5eff33eSChaitanya Kulkarni 53426af180cSIsrael Rukshin static inline u32 nvmet_rw_data_len(struct nvmet_req *req) 535d5eff33eSChaitanya Kulkarni { 536d5eff33eSChaitanya Kulkarni return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) << 537d5eff33eSChaitanya Kulkarni req->ns->blksize_shift; 538d5eff33eSChaitanya Kulkarni } 539c6aa3542SChaitanya Kulkarni 540ea52ac1cSIsrael Rukshin static inline u32 nvmet_rw_metadata_len(struct nvmet_req *req) 541ea52ac1cSIsrael Rukshin { 542ea52ac1cSIsrael Rukshin if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) 543ea52ac1cSIsrael Rukshin return 0; 544ea52ac1cSIsrael Rukshin return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) * 545ea52ac1cSIsrael Rukshin req->ns->metadata_size; 546ea52ac1cSIsrael Rukshin } 547ea52ac1cSIsrael Rukshin 54859ef0eaaSChristoph Hellwig static inline u32 nvmet_dsm_len(struct nvmet_req *req) 54959ef0eaaSChristoph Hellwig { 55059ef0eaaSChristoph Hellwig return (le32_to_cpu(req->cmd->dsm.nr) + 1) * 55159ef0eaaSChristoph Hellwig sizeof(struct nvme_dsm_range); 55259ef0eaaSChristoph Hellwig } 55359ef0eaaSChristoph Hellwig 554c1fef73fSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU 555ba76af67SLogan Gunthorpe void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys); 556ba76af67SLogan Gunthorpe int nvmet_passthru_ctrl_enable(struct nvmet_subsys *subsys); 557ba76af67SLogan Gunthorpe void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys); 558c1fef73fSLogan Gunthorpe u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req); 559c1fef73fSLogan Gunthorpe u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req); 560c1fef73fSLogan Gunthorpe static inline struct nvme_ctrl *nvmet_passthru_ctrl(struct nvmet_subsys *subsys) 561c1fef73fSLogan Gunthorpe { 562c1fef73fSLogan Gunthorpe return subsys->passthru_ctrl; 563c1fef73fSLogan Gunthorpe } 564c1fef73fSLogan Gunthorpe #else /* CONFIG_NVME_TARGET_PASSTHRU */ 565ba76af67SLogan Gunthorpe static inline void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys) 566ba76af67SLogan Gunthorpe { 567ba76af67SLogan Gunthorpe } 568ba76af67SLogan Gunthorpe static inline void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys) 569ba76af67SLogan Gunthorpe { 570ba76af67SLogan Gunthorpe } 571c1fef73fSLogan Gunthorpe static inline u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req) 572c1fef73fSLogan Gunthorpe { 573c1fef73fSLogan Gunthorpe return 0; 574c1fef73fSLogan Gunthorpe } 575c1fef73fSLogan Gunthorpe static inline u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req) 576c1fef73fSLogan Gunthorpe { 577c1fef73fSLogan Gunthorpe return 0; 578c1fef73fSLogan Gunthorpe } 579c1fef73fSLogan Gunthorpe static inline struct nvme_ctrl *nvmet_passthru_ctrl(struct nvmet_subsys *subsys) 580c1fef73fSLogan Gunthorpe { 581c1fef73fSLogan Gunthorpe return NULL; 582c1fef73fSLogan Gunthorpe } 583c1fef73fSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */ 584c1fef73fSLogan Gunthorpe 585c1fef73fSLogan Gunthorpe static inline struct nvme_ctrl * 586c1fef73fSLogan Gunthorpe nvmet_req_passthru_ctrl(struct nvmet_req *req) 587c1fef73fSLogan Gunthorpe { 588c1fef73fSLogan Gunthorpe return nvmet_passthru_ctrl(req->sq->ctrl->subsys); 589c1fef73fSLogan Gunthorpe } 590c1fef73fSLogan Gunthorpe 591c6aa3542SChaitanya Kulkarni u16 errno_to_nvme_status(struct nvmet_req *req, int errno); 5929d05a96eSBart Van Assche 5939d05a96eSBart Van Assche /* Convert a 32-bit number to a 16-bit 0's based number */ 5949d05a96eSBart Van Assche static inline __le16 to0based(u32 a) 5959d05a96eSBart Van Assche { 5969d05a96eSBart Van Assche return cpu_to_le16(max(1U, min(1U << 16, a)) - 1); 5979d05a96eSBart Van Assche } 5989d05a96eSBart Van Assche 599ea52ac1cSIsrael Rukshin static inline bool nvmet_ns_has_pi(struct nvmet_ns *ns) 600ea52ac1cSIsrael Rukshin { 601ea52ac1cSIsrael Rukshin if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) 602ea52ac1cSIsrael Rukshin return false; 603ea52ac1cSIsrael Rukshin return ns->pi_type && ns->metadata_size == sizeof(struct t10_pi_tuple); 604ea52ac1cSIsrael Rukshin } 605ea52ac1cSIsrael Rukshin 606*193fcf37SChaitanya Kulkarni static inline __le64 nvmet_sect_to_lba(struct nvmet_ns *ns, sector_t sect) 607*193fcf37SChaitanya Kulkarni { 608*193fcf37SChaitanya Kulkarni return cpu_to_le64(sect >> (ns->blksize_shift - SECTOR_SHIFT)); 609*193fcf37SChaitanya Kulkarni } 610*193fcf37SChaitanya Kulkarni 611*193fcf37SChaitanya Kulkarni static inline sector_t nvmet_lba_to_sect(struct nvmet_ns *ns, __le64 lba) 612*193fcf37SChaitanya Kulkarni { 613*193fcf37SChaitanya Kulkarni return le64_to_cpu(lba) << (ns->blksize_shift - SECTOR_SHIFT); 614*193fcf37SChaitanya Kulkarni } 615*193fcf37SChaitanya Kulkarni 616a07b4970SChristoph Hellwig #endif /* _NVMET_H */ 617