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> 22a07b4970SChristoph Hellwig 23a07b4970SChristoph Hellwig #define NVMET_ASYNC_EVENTS 4 24a07b4970SChristoph Hellwig #define NVMET_ERROR_LOG_SLOTS 128 255698b805SChaitanya Kulkarni #define NVMET_NO_ERROR_LOC ((u16)-1) 26a07b4970SChristoph Hellwig 27c86b8f7bSChristoph Hellwig /* 28c86b8f7bSChristoph Hellwig * Supported optional AENs: 29c86b8f7bSChristoph Hellwig */ 30c86b8f7bSChristoph Hellwig #define NVMET_AEN_CFG_OPTIONAL \ 3162ac0d32SChristoph Hellwig (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE) 32f301c2b1SJay Sternberg #define NVMET_DISC_AEN_CFG_OPTIONAL \ 33f301c2b1SJay Sternberg (NVME_AEN_CFG_DISC_CHANGE) 34c86b8f7bSChristoph Hellwig 35c86b8f7bSChristoph Hellwig /* 36c86b8f7bSChristoph Hellwig * Plus mandatory SMART AENs (we'll never send them, but allow enabling them): 37c86b8f7bSChristoph Hellwig */ 38c86b8f7bSChristoph Hellwig #define NVMET_AEN_CFG_ALL \ 39c86b8f7bSChristoph Hellwig (NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \ 40c86b8f7bSChristoph Hellwig NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \ 41c86b8f7bSChristoph Hellwig NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL) 42c86b8f7bSChristoph Hellwig 43a07b4970SChristoph Hellwig /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM 44a07b4970SChristoph Hellwig * The 16 bit shift is to set IATTR bit to 1, which means offending 45a07b4970SChristoph Hellwig * offset starts in the data section of connect() 46a07b4970SChristoph Hellwig */ 47a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_DATA(x) \ 48a07b4970SChristoph Hellwig (cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x)))) 49a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_SQE(x) \ 50a07b4970SChristoph Hellwig (cpu_to_le32(offsetof(struct nvmf_connect_command, x))) 51a07b4970SChristoph Hellwig 52a07b4970SChristoph Hellwig struct nvmet_ns { 53a07b4970SChristoph Hellwig struct list_head dev_link; 54a07b4970SChristoph Hellwig struct percpu_ref ref; 55a07b4970SChristoph Hellwig struct block_device *bdev; 56d5eff33eSChaitanya Kulkarni struct file *file; 57dedf0be5SChaitanya Kulkarni bool readonly; 58a07b4970SChristoph Hellwig u32 nsid; 59a07b4970SChristoph Hellwig u32 blksize_shift; 60a07b4970SChristoph Hellwig loff_t size; 61a07b4970SChristoph Hellwig u8 nguid[16]; 62637dc0f3SJohannes Thumshirn uuid_t uuid; 6372efd25dSChristoph Hellwig u32 anagrpid; 64a07b4970SChristoph Hellwig 6555eb942eSChaitanya Kulkarni bool buffered_io; 66e4fcf07cSSolganik Alexander bool enabled; 67a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 68a07b4970SChristoph Hellwig const char *device_path; 69a07b4970SChristoph Hellwig 70a07b4970SChristoph Hellwig struct config_group device_group; 71a07b4970SChristoph Hellwig struct config_group group; 72a07b4970SChristoph Hellwig 73a07b4970SChristoph Hellwig struct completion disable_done; 74d5eff33eSChaitanya Kulkarni mempool_t *bvec_pool; 75d5eff33eSChaitanya Kulkarni struct kmem_cache *bvec_cache; 76c6925093SLogan Gunthorpe 77c6925093SLogan Gunthorpe int use_p2pmem; 78c6925093SLogan Gunthorpe struct pci_dev *p2p_dev; 79a07b4970SChristoph Hellwig }; 80a07b4970SChristoph Hellwig 81a07b4970SChristoph Hellwig static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item) 82a07b4970SChristoph Hellwig { 83a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_ns, group); 84a07b4970SChristoph Hellwig } 85a07b4970SChristoph Hellwig 86c6925093SLogan Gunthorpe static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns) 87c6925093SLogan Gunthorpe { 88c6925093SLogan Gunthorpe return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL; 89c6925093SLogan Gunthorpe } 90c6925093SLogan Gunthorpe 91a07b4970SChristoph Hellwig struct nvmet_cq { 92a07b4970SChristoph Hellwig u16 qid; 93a07b4970SChristoph Hellwig u16 size; 94a07b4970SChristoph Hellwig }; 95a07b4970SChristoph Hellwig 96a07b4970SChristoph Hellwig struct nvmet_sq { 97a07b4970SChristoph Hellwig struct nvmet_ctrl *ctrl; 98a07b4970SChristoph Hellwig struct percpu_ref ref; 99a07b4970SChristoph Hellwig u16 qid; 100a07b4970SChristoph Hellwig u16 size; 101f9cf2a64SJames Smart u32 sqhd; 102e6a622fdSSagi Grimberg bool sqhd_disabled; 103a07b4970SChristoph Hellwig struct completion free_done; 104427242ceSSagi Grimberg struct completion confirm_done; 105a07b4970SChristoph Hellwig }; 106a07b4970SChristoph Hellwig 10762ac0d32SChristoph Hellwig struct nvmet_ana_group { 10862ac0d32SChristoph Hellwig struct config_group group; 10962ac0d32SChristoph Hellwig struct nvmet_port *port; 11062ac0d32SChristoph Hellwig u32 grpid; 11162ac0d32SChristoph Hellwig }; 11262ac0d32SChristoph Hellwig 11362ac0d32SChristoph Hellwig static inline struct nvmet_ana_group *to_ana_group(struct config_item *item) 11462ac0d32SChristoph Hellwig { 11562ac0d32SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_ana_group, 11662ac0d32SChristoph Hellwig group); 11762ac0d32SChristoph Hellwig } 11862ac0d32SChristoph Hellwig 119a07b4970SChristoph Hellwig /** 120a07b4970SChristoph Hellwig * struct nvmet_port - Common structure to keep port 121a07b4970SChristoph Hellwig * information for the target. 122fe4a9791SChristoph Hellwig * @entry: Entry into referrals or transport list. 123a07b4970SChristoph Hellwig * @disc_addr: Address information is stored in a format defined 124a07b4970SChristoph Hellwig * for a discovery log page entry. 125a07b4970SChristoph Hellwig * @group: ConfigFS group for this element's folder. 126a07b4970SChristoph Hellwig * @priv: Private data for the transport. 127a07b4970SChristoph Hellwig */ 128a07b4970SChristoph Hellwig struct nvmet_port { 129a07b4970SChristoph Hellwig struct list_head entry; 130a07b4970SChristoph Hellwig struct nvmf_disc_rsp_page_entry disc_addr; 131a07b4970SChristoph Hellwig struct config_group group; 132a07b4970SChristoph Hellwig struct config_group subsys_group; 133a07b4970SChristoph Hellwig struct list_head subsystems; 134a07b4970SChristoph Hellwig struct config_group referrals_group; 135a07b4970SChristoph Hellwig struct list_head referrals; 136b662a078SJay Sternberg struct list_head global_entry; 13762ac0d32SChristoph Hellwig struct config_group ana_groups_group; 13862ac0d32SChristoph Hellwig struct nvmet_ana_group ana_default_group; 13972efd25dSChristoph Hellwig enum nvme_ana_state *ana_state; 140a07b4970SChristoph Hellwig void *priv; 141a07b4970SChristoph Hellwig bool enabled; 1420d5ee2b2SSteve Wise int inline_data_size; 1439d09dd8dSJames Smart const struct nvmet_fabrics_ops *tr_ops; 144a07b4970SChristoph Hellwig }; 145a07b4970SChristoph Hellwig 146a07b4970SChristoph Hellwig static inline struct nvmet_port *to_nvmet_port(struct config_item *item) 147a07b4970SChristoph Hellwig { 148a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_port, 149a07b4970SChristoph Hellwig group); 150a07b4970SChristoph Hellwig } 151a07b4970SChristoph Hellwig 15262ac0d32SChristoph Hellwig static inline struct nvmet_port *ana_groups_to_port( 15362ac0d32SChristoph Hellwig struct config_item *item) 15462ac0d32SChristoph Hellwig { 15562ac0d32SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_port, 15662ac0d32SChristoph Hellwig ana_groups_group); 15762ac0d32SChristoph Hellwig } 15862ac0d32SChristoph Hellwig 159a07b4970SChristoph Hellwig struct nvmet_ctrl { 160a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 161a07b4970SChristoph Hellwig struct nvmet_cq **cqs; 162a07b4970SChristoph Hellwig struct nvmet_sq **sqs; 163a07b4970SChristoph Hellwig 164c09305aeSSagi Grimberg bool cmd_seen; 165c09305aeSSagi Grimberg 166a07b4970SChristoph Hellwig struct mutex lock; 167a07b4970SChristoph Hellwig u64 cap; 168a07b4970SChristoph Hellwig u32 cc; 169a07b4970SChristoph Hellwig u32 csts; 170a07b4970SChristoph Hellwig 17128dd5cf7SOmri Mann uuid_t hostid; 172a07b4970SChristoph Hellwig u16 cntlid; 173a07b4970SChristoph Hellwig u32 kato; 174a07b4970SChristoph Hellwig 1754ee43280SChristoph Hellwig struct nvmet_port *port; 1764ee43280SChristoph Hellwig 177c86b8f7bSChristoph Hellwig u32 aen_enabled; 17855fdd6b6SChristoph Hellwig unsigned long aen_masked; 179a07b4970SChristoph Hellwig struct nvmet_req *async_event_cmds[NVMET_ASYNC_EVENTS]; 180a07b4970SChristoph Hellwig unsigned int nr_async_event_cmds; 181a07b4970SChristoph Hellwig struct list_head async_events; 182a07b4970SChristoph Hellwig struct work_struct async_event_work; 183a07b4970SChristoph Hellwig 184a07b4970SChristoph Hellwig struct list_head subsys_entry; 185a07b4970SChristoph Hellwig struct kref ref; 186a07b4970SChristoph Hellwig struct delayed_work ka_work; 187a07b4970SChristoph Hellwig struct work_struct fatal_err_work; 188a07b4970SChristoph Hellwig 189e929f06dSChristoph Hellwig const struct nvmet_fabrics_ops *ops; 190a07b4970SChristoph Hellwig 191c16734eaSChristoph Hellwig __le32 *changed_ns_list; 192c16734eaSChristoph Hellwig u32 nr_changed_ns; 193c16734eaSChristoph Hellwig 194a07b4970SChristoph Hellwig char subsysnqn[NVMF_NQN_FIELD_LEN]; 195a07b4970SChristoph Hellwig char hostnqn[NVMF_NQN_FIELD_LEN]; 196c6925093SLogan Gunthorpe 197c6925093SLogan Gunthorpe struct device *p2p_client; 198c6925093SLogan Gunthorpe struct radix_tree_root p2p_ns_map; 199e4a97625SChaitanya Kulkarni 200e4a97625SChaitanya Kulkarni spinlock_t error_lock; 201e4a97625SChaitanya Kulkarni u64 err_counter; 202e4a97625SChaitanya Kulkarni struct nvme_error_slot slots[NVMET_ERROR_LOG_SLOTS]; 203a07b4970SChristoph Hellwig }; 204a07b4970SChristoph Hellwig 205a07b4970SChristoph Hellwig struct nvmet_subsys { 206a07b4970SChristoph Hellwig enum nvme_subsys_type type; 207a07b4970SChristoph Hellwig 208a07b4970SChristoph Hellwig struct mutex lock; 209a07b4970SChristoph Hellwig struct kref ref; 210a07b4970SChristoph Hellwig 211a07b4970SChristoph Hellwig struct list_head namespaces; 212793c7cfcSChristoph Hellwig unsigned int nr_namespaces; 213a07b4970SChristoph Hellwig unsigned int max_nsid; 214*94a39d61SChaitanya Kulkarni u16 cntlid_min; 215*94a39d61SChaitanya Kulkarni u16 cntlid_max; 216a07b4970SChristoph Hellwig 217a07b4970SChristoph Hellwig struct list_head ctrls; 218a07b4970SChristoph Hellwig 219a07b4970SChristoph Hellwig struct list_head hosts; 220a07b4970SChristoph Hellwig bool allow_any_host; 221a07b4970SChristoph Hellwig 222a07b4970SChristoph Hellwig u16 max_qid; 223a07b4970SChristoph Hellwig 224a07b4970SChristoph Hellwig u64 ver; 2252e7f5d2aSJohannes Thumshirn u64 serial; 226a07b4970SChristoph Hellwig char *subsysnqn; 227a07b4970SChristoph Hellwig 228a07b4970SChristoph Hellwig struct config_group group; 229a07b4970SChristoph Hellwig 230a07b4970SChristoph Hellwig struct config_group namespaces_group; 231a07b4970SChristoph Hellwig struct config_group allowed_hosts_group; 232a07b4970SChristoph Hellwig }; 233a07b4970SChristoph Hellwig 234a07b4970SChristoph Hellwig static inline struct nvmet_subsys *to_subsys(struct config_item *item) 235a07b4970SChristoph Hellwig { 236a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_subsys, group); 237a07b4970SChristoph Hellwig } 238a07b4970SChristoph Hellwig 239a07b4970SChristoph Hellwig static inline struct nvmet_subsys *namespaces_to_subsys( 240a07b4970SChristoph Hellwig struct config_item *item) 241a07b4970SChristoph Hellwig { 242a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_subsys, 243a07b4970SChristoph Hellwig namespaces_group); 244a07b4970SChristoph Hellwig } 245a07b4970SChristoph Hellwig 246a07b4970SChristoph Hellwig struct nvmet_host { 247a07b4970SChristoph Hellwig struct config_group group; 248a07b4970SChristoph Hellwig }; 249a07b4970SChristoph Hellwig 250a07b4970SChristoph Hellwig static inline struct nvmet_host *to_host(struct config_item *item) 251a07b4970SChristoph Hellwig { 252a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_host, group); 253a07b4970SChristoph Hellwig } 254a07b4970SChristoph Hellwig 255a07b4970SChristoph Hellwig static inline char *nvmet_host_name(struct nvmet_host *host) 256a07b4970SChristoph Hellwig { 257a07b4970SChristoph Hellwig return config_item_name(&host->group.cg_item); 258a07b4970SChristoph Hellwig } 259a07b4970SChristoph Hellwig 260a07b4970SChristoph Hellwig struct nvmet_host_link { 261a07b4970SChristoph Hellwig struct list_head entry; 262a07b4970SChristoph Hellwig struct nvmet_host *host; 263a07b4970SChristoph Hellwig }; 264a07b4970SChristoph Hellwig 265a07b4970SChristoph Hellwig struct nvmet_subsys_link { 266a07b4970SChristoph Hellwig struct list_head entry; 267a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 268a07b4970SChristoph Hellwig }; 269a07b4970SChristoph Hellwig 270a07b4970SChristoph Hellwig struct nvmet_req; 271a07b4970SChristoph Hellwig struct nvmet_fabrics_ops { 272a07b4970SChristoph Hellwig struct module *owner; 273a07b4970SChristoph Hellwig unsigned int type; 274a07b4970SChristoph Hellwig unsigned int msdbd; 275a07b4970SChristoph Hellwig bool has_keyed_sgls : 1; 276a07b4970SChristoph Hellwig void (*queue_response)(struct nvmet_req *req); 277a07b4970SChristoph Hellwig int (*add_port)(struct nvmet_port *port); 278a07b4970SChristoph Hellwig void (*remove_port)(struct nvmet_port *port); 279a07b4970SChristoph Hellwig void (*delete_ctrl)(struct nvmet_ctrl *ctrl); 2804c652685SSagi Grimberg void (*disc_traddr)(struct nvmet_req *req, 2814c652685SSagi Grimberg struct nvmet_port *port, char *traddr); 2821672ddb8SSagi Grimberg u16 (*install_queue)(struct nvmet_sq *nvme_sq); 2839d09dd8dSJames Smart void (*discovery_chg)(struct nvmet_port *port); 284a07b4970SChristoph Hellwig }; 285a07b4970SChristoph Hellwig 286a07b4970SChristoph Hellwig #define NVMET_MAX_INLINE_BIOVEC 8 28773383adfSSagi Grimberg #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE 288a07b4970SChristoph Hellwig 289a07b4970SChristoph Hellwig struct nvmet_req { 290a07b4970SChristoph Hellwig struct nvme_command *cmd; 291fc6c9730SMax Gurtovoy struct nvme_completion *cqe; 292a07b4970SChristoph Hellwig struct nvmet_sq *sq; 293a07b4970SChristoph Hellwig struct nvmet_cq *cq; 294a07b4970SChristoph Hellwig struct nvmet_ns *ns; 295a07b4970SChristoph Hellwig struct scatterlist *sg; 296a07b4970SChristoph Hellwig struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC]; 297d5eff33eSChaitanya Kulkarni union { 298d5eff33eSChaitanya Kulkarni struct { 299d5eff33eSChaitanya Kulkarni struct bio inline_bio; 300d5eff33eSChaitanya Kulkarni } b; 301d5eff33eSChaitanya Kulkarni struct { 302d5eff33eSChaitanya Kulkarni bool mpool_alloc; 303d5eff33eSChaitanya Kulkarni struct kiocb iocb; 304d5eff33eSChaitanya Kulkarni struct bio_vec *bvec; 305d5eff33eSChaitanya Kulkarni struct work_struct work; 306d5eff33eSChaitanya Kulkarni } f; 307d5eff33eSChaitanya Kulkarni }; 308a07b4970SChristoph Hellwig int sg_cnt; 3095e62d5c9SChristoph Hellwig /* data length as parsed from the SGL descriptor: */ 3105e62d5c9SChristoph Hellwig size_t transfer_len; 311a07b4970SChristoph Hellwig 312a07b4970SChristoph Hellwig struct nvmet_port *port; 313a07b4970SChristoph Hellwig 314a07b4970SChristoph Hellwig void (*execute)(struct nvmet_req *req); 315e929f06dSChristoph Hellwig const struct nvmet_fabrics_ops *ops; 316c6925093SLogan Gunthorpe 317c6925093SLogan Gunthorpe struct pci_dev *p2p_dev; 318c6925093SLogan Gunthorpe struct device *p2p_client; 319e4a97625SChaitanya Kulkarni u16 error_loc; 320e4a97625SChaitanya Kulkarni u64 error_slba; 321a07b4970SChristoph Hellwig }; 322a07b4970SChristoph Hellwig 32355eb942eSChaitanya Kulkarni extern struct workqueue_struct *buffered_io_wq; 32455eb942eSChaitanya Kulkarni 325a07b4970SChristoph Hellwig static inline void nvmet_set_result(struct nvmet_req *req, u32 result) 326a07b4970SChristoph Hellwig { 327fc6c9730SMax Gurtovoy req->cqe->result.u32 = cpu_to_le32(result); 328a07b4970SChristoph Hellwig } 329a07b4970SChristoph Hellwig 330a07b4970SChristoph Hellwig /* 331a07b4970SChristoph Hellwig * NVMe command writes actually are DMA reads for us on the target side. 332a07b4970SChristoph Hellwig */ 333a07b4970SChristoph Hellwig static inline enum dma_data_direction 334a07b4970SChristoph Hellwig nvmet_data_dir(struct nvmet_req *req) 335a07b4970SChristoph Hellwig { 336a07b4970SChristoph Hellwig return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 337a07b4970SChristoph Hellwig } 338a07b4970SChristoph Hellwig 339a07b4970SChristoph Hellwig struct nvmet_async_event { 340a07b4970SChristoph Hellwig struct list_head entry; 341a07b4970SChristoph Hellwig u8 event_type; 342a07b4970SChristoph Hellwig u8 event_info; 343a07b4970SChristoph Hellwig u8 log_page; 344a07b4970SChristoph Hellwig }; 345a07b4970SChristoph Hellwig 3467114ddebSJay Sternberg static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn) 3476c8312adSJay Sternberg { 348b7c8f366SChaitanya Kulkarni int rae = le32_to_cpu(req->cmd->common.cdw10) & 1 << 15; 3496c8312adSJay Sternberg 3506c8312adSJay Sternberg if (!rae) 3517114ddebSJay Sternberg clear_bit(bn, &req->sq->ctrl->aen_masked); 3526c8312adSJay Sternberg } 3536c8312adSJay Sternberg 3547114ddebSJay Sternberg static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn) 3556c8312adSJay Sternberg { 3567114ddebSJay Sternberg if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn))) 3576c8312adSJay Sternberg return true; 3587114ddebSJay Sternberg return test_and_set_bit(bn, &ctrl->aen_masked); 3596c8312adSJay Sternberg } 3606c8312adSJay Sternberg 36190107455SJay Sternberg void nvmet_get_feat_kato(struct nvmet_req *req); 36290107455SJay Sternberg void nvmet_get_feat_async_event(struct nvmet_req *req); 36390107455SJay Sternberg u16 nvmet_set_feat_kato(struct nvmet_req *req); 36490107455SJay Sternberg u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask); 36590107455SJay Sternberg void nvmet_execute_async_event(struct nvmet_req *req); 36690107455SJay Sternberg 36764a0ca88SParav Pandit u16 nvmet_parse_connect_cmd(struct nvmet_req *req); 3689d05a96eSBart Van Assche void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id); 369d5eff33eSChaitanya Kulkarni u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req); 370d5eff33eSChaitanya Kulkarni u16 nvmet_file_parse_io_cmd(struct nvmet_req *req); 37164a0ca88SParav Pandit u16 nvmet_parse_admin_cmd(struct nvmet_req *req); 37264a0ca88SParav Pandit u16 nvmet_parse_discovery_cmd(struct nvmet_req *req); 37364a0ca88SParav Pandit u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req); 374a07b4970SChristoph Hellwig 375a07b4970SChristoph Hellwig bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq, 376e929f06dSChristoph Hellwig struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops); 377549f01aeSVijay Immanuel void nvmet_req_uninit(struct nvmet_req *req); 378e9061c39SChristoph Hellwig bool nvmet_check_data_len(struct nvmet_req *req, size_t data_len); 379b716e688SSagi Grimberg bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len); 380a07b4970SChristoph Hellwig void nvmet_req_complete(struct nvmet_req *req, u16 status); 3815b2322e4SLogan Gunthorpe int nvmet_req_alloc_sgl(struct nvmet_req *req); 3825b2322e4SLogan Gunthorpe void nvmet_req_free_sgl(struct nvmet_req *req); 383a07b4970SChristoph Hellwig 384f9362ac1SJay Sternberg void nvmet_execute_keep_alive(struct nvmet_req *req); 385f9362ac1SJay Sternberg 386a07b4970SChristoph Hellwig void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid, 387a07b4970SChristoph Hellwig u16 size); 388a07b4970SChristoph Hellwig void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid, 389a07b4970SChristoph Hellwig u16 size); 390a07b4970SChristoph Hellwig void nvmet_sq_destroy(struct nvmet_sq *sq); 391a07b4970SChristoph Hellwig int nvmet_sq_init(struct nvmet_sq *sq); 392a07b4970SChristoph Hellwig 393a07b4970SChristoph Hellwig void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl); 394a07b4970SChristoph Hellwig 395a07b4970SChristoph Hellwig void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new); 396a07b4970SChristoph Hellwig u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn, 397a07b4970SChristoph Hellwig struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp); 398a07b4970SChristoph Hellwig u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid, 399a07b4970SChristoph Hellwig struct nvmet_req *req, struct nvmet_ctrl **ret); 400a07b4970SChristoph Hellwig void nvmet_ctrl_put(struct nvmet_ctrl *ctrl); 40164a0ca88SParav Pandit u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd); 402a07b4970SChristoph Hellwig 403a07b4970SChristoph Hellwig struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, 404a07b4970SChristoph Hellwig enum nvme_subsys_type type); 405a07b4970SChristoph Hellwig void nvmet_subsys_put(struct nvmet_subsys *subsys); 406344770b0SSagi Grimberg void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys); 407a07b4970SChristoph Hellwig 408a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid); 409a07b4970SChristoph Hellwig void nvmet_put_namespace(struct nvmet_ns *ns); 410a07b4970SChristoph Hellwig int nvmet_ns_enable(struct nvmet_ns *ns); 411a07b4970SChristoph Hellwig void nvmet_ns_disable(struct nvmet_ns *ns); 412a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid); 413a07b4970SChristoph Hellwig void nvmet_ns_free(struct nvmet_ns *ns); 414a07b4970SChristoph Hellwig 41562ac0d32SChristoph Hellwig void nvmet_send_ana_event(struct nvmet_subsys *subsys, 41662ac0d32SChristoph Hellwig struct nvmet_port *port); 41762ac0d32SChristoph Hellwig void nvmet_port_send_ana_event(struct nvmet_port *port); 41862ac0d32SChristoph Hellwig 419e929f06dSChristoph Hellwig int nvmet_register_transport(const struct nvmet_fabrics_ops *ops); 420e929f06dSChristoph Hellwig void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops); 421a07b4970SChristoph Hellwig 4223aed8673SLogan Gunthorpe void nvmet_port_del_ctrls(struct nvmet_port *port, 4233aed8673SLogan Gunthorpe struct nvmet_subsys *subsys); 4243aed8673SLogan Gunthorpe 425a07b4970SChristoph Hellwig int nvmet_enable_port(struct nvmet_port *port); 426a07b4970SChristoph Hellwig void nvmet_disable_port(struct nvmet_port *port); 427a07b4970SChristoph Hellwig 428a07b4970SChristoph Hellwig void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port); 429b662a078SJay Sternberg void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port); 430a07b4970SChristoph Hellwig 431a07b4970SChristoph Hellwig u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf, 432a07b4970SChristoph Hellwig size_t len); 433a07b4970SChristoph Hellwig u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, 434a07b4970SChristoph Hellwig size_t len); 435c7759fffSChristoph Hellwig u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len); 436a07b4970SChristoph Hellwig 437a07b4970SChristoph Hellwig u32 nvmet_get_log_page_len(struct nvme_command *cmd); 438d808b7f7SKeith Busch u64 nvmet_get_log_page_offset(struct nvme_command *cmd); 439a07b4970SChristoph Hellwig 440b662a078SJay Sternberg extern struct list_head *nvmet_ports; 441b662a078SJay Sternberg void nvmet_port_disc_changed(struct nvmet_port *port, 442b662a078SJay Sternberg struct nvmet_subsys *subsys); 443b662a078SJay Sternberg void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys, 444b662a078SJay Sternberg struct nvmet_host *host); 445b662a078SJay Sternberg void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type, 446b662a078SJay Sternberg u8 event_info, u8 log_page); 447b662a078SJay Sternberg 448a07b4970SChristoph Hellwig #define NVMET_QUEUE_SIZE 1024 4493375e29cSJames Smart #define NVMET_NR_QUEUES 128 450a07b4970SChristoph Hellwig #define NVMET_MAX_CMD NVMET_QUEUE_SIZE 451793c7cfcSChristoph Hellwig 452793c7cfcSChristoph Hellwig /* 453793c7cfcSChristoph Hellwig * Nice round number that makes a list of nsids fit into a page. 454793c7cfcSChristoph Hellwig * Should become tunable at some point in the future. 455793c7cfcSChristoph Hellwig */ 456793c7cfcSChristoph Hellwig #define NVMET_MAX_NAMESPACES 1024 457793c7cfcSChristoph Hellwig 45872efd25dSChristoph Hellwig /* 45972efd25dSChristoph Hellwig * 0 is not a valid ANA group ID, so we start numbering at 1. 46072efd25dSChristoph Hellwig * 46172efd25dSChristoph Hellwig * ANA Group 1 exists without manual intervention, has namespaces assigned to it 46272efd25dSChristoph Hellwig * by default, and is available in an optimized state through all ports. 46372efd25dSChristoph Hellwig */ 46462ac0d32SChristoph Hellwig #define NVMET_MAX_ANAGRPS 128 46572efd25dSChristoph Hellwig #define NVMET_DEFAULT_ANA_GRPID 1 46672efd25dSChristoph Hellwig 467a07b4970SChristoph Hellwig #define NVMET_KAS 10 468f9362ac1SJay Sternberg #define NVMET_DISC_KATO_MS 120000 469a07b4970SChristoph Hellwig 470a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void); 471a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void); 472a07b4970SChristoph Hellwig 473a07b4970SChristoph Hellwig int __init nvmet_init_discovery(void); 474a07b4970SChristoph Hellwig void nvmet_exit_discovery(void); 475a07b4970SChristoph Hellwig 476a07b4970SChristoph Hellwig extern struct nvmet_subsys *nvmet_disc_subsys; 477a07b4970SChristoph Hellwig extern struct rw_semaphore nvmet_config_sem; 478a07b4970SChristoph Hellwig 47972efd25dSChristoph Hellwig extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1]; 48072efd25dSChristoph Hellwig extern u64 nvmet_ana_chgcnt; 48172efd25dSChristoph Hellwig extern struct rw_semaphore nvmet_ana_sem; 48272efd25dSChristoph Hellwig 483253928eeSSagi Grimberg bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn); 484a07b4970SChristoph Hellwig 485d5eff33eSChaitanya Kulkarni int nvmet_bdev_ns_enable(struct nvmet_ns *ns); 486d5eff33eSChaitanya Kulkarni int nvmet_file_ns_enable(struct nvmet_ns *ns); 487d5eff33eSChaitanya Kulkarni void nvmet_bdev_ns_disable(struct nvmet_ns *ns); 488d5eff33eSChaitanya Kulkarni void nvmet_file_ns_disable(struct nvmet_ns *ns); 489dedf0be5SChaitanya Kulkarni u16 nvmet_bdev_flush(struct nvmet_req *req); 490dedf0be5SChaitanya Kulkarni u16 nvmet_file_flush(struct nvmet_req *req); 491dedf0be5SChaitanya Kulkarni void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid); 492d5eff33eSChaitanya Kulkarni 493d5eff33eSChaitanya Kulkarni static inline u32 nvmet_rw_len(struct nvmet_req *req) 494d5eff33eSChaitanya Kulkarni { 495d5eff33eSChaitanya Kulkarni return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) << 496d5eff33eSChaitanya Kulkarni req->ns->blksize_shift; 497d5eff33eSChaitanya Kulkarni } 498c6aa3542SChaitanya Kulkarni 49959ef0eaaSChristoph Hellwig static inline u32 nvmet_dsm_len(struct nvmet_req *req) 50059ef0eaaSChristoph Hellwig { 50159ef0eaaSChristoph Hellwig return (le32_to_cpu(req->cmd->dsm.nr) + 1) * 50259ef0eaaSChristoph Hellwig sizeof(struct nvme_dsm_range); 50359ef0eaaSChristoph Hellwig } 50459ef0eaaSChristoph Hellwig 505c6aa3542SChaitanya Kulkarni u16 errno_to_nvme_status(struct nvmet_req *req, int errno); 5069d05a96eSBart Van Assche 5079d05a96eSBart Van Assche /* Convert a 32-bit number to a 16-bit 0's based number */ 5089d05a96eSBart Van Assche static inline __le16 to0based(u32 a) 5099d05a96eSBart Van Assche { 5109d05a96eSBart Van Assche return cpu_to_le16(max(1U, min(1U << 16, a)) - 1); 5119d05a96eSBart Van Assche } 5129d05a96eSBart Van Assche 513a07b4970SChristoph Hellwig #endif /* _NVMET_H */ 514