1a07b4970SChristoph Hellwig /* 2a07b4970SChristoph Hellwig * Copyright (c) 2015-2016 HGST, a Western Digital Company. 3a07b4970SChristoph Hellwig * 4a07b4970SChristoph Hellwig * This program is free software; you can redistribute it and/or modify it 5a07b4970SChristoph Hellwig * under the terms and conditions of the GNU General Public License, 6a07b4970SChristoph Hellwig * version 2, as published by the Free Software Foundation. 7a07b4970SChristoph Hellwig * 8a07b4970SChristoph Hellwig * This program is distributed in the hope it will be useful, but WITHOUT 9a07b4970SChristoph Hellwig * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10a07b4970SChristoph Hellwig * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11a07b4970SChristoph Hellwig * more details. 12a07b4970SChristoph Hellwig */ 13a07b4970SChristoph Hellwig 14a07b4970SChristoph Hellwig #ifndef _NVMET_H 15a07b4970SChristoph Hellwig #define _NVMET_H 16a07b4970SChristoph Hellwig 17a07b4970SChristoph Hellwig #include <linux/dma-mapping.h> 18a07b4970SChristoph Hellwig #include <linux/types.h> 19a07b4970SChristoph Hellwig #include <linux/device.h> 20a07b4970SChristoph Hellwig #include <linux/kref.h> 21a07b4970SChristoph Hellwig #include <linux/percpu-refcount.h> 22a07b4970SChristoph Hellwig #include <linux/list.h> 23a07b4970SChristoph Hellwig #include <linux/mutex.h> 248e412263SChristoph Hellwig #include <linux/uuid.h> 25a07b4970SChristoph Hellwig #include <linux/nvme.h> 26a07b4970SChristoph Hellwig #include <linux/configfs.h> 27a07b4970SChristoph Hellwig #include <linux/rcupdate.h> 28a07b4970SChristoph Hellwig #include <linux/blkdev.h> 29c6925093SLogan Gunthorpe #include <linux/radix-tree.h> 30a07b4970SChristoph Hellwig 31a07b4970SChristoph Hellwig #define NVMET_ASYNC_EVENTS 4 32a07b4970SChristoph Hellwig #define NVMET_ERROR_LOG_SLOTS 128 33a07b4970SChristoph Hellwig 34c86b8f7bSChristoph Hellwig /* 35c86b8f7bSChristoph Hellwig * Supported optional AENs: 36c86b8f7bSChristoph Hellwig */ 37c86b8f7bSChristoph Hellwig #define NVMET_AEN_CFG_OPTIONAL \ 3862ac0d32SChristoph Hellwig (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE) 39*f301c2b1SJay Sternberg #define NVMET_DISC_AEN_CFG_OPTIONAL \ 40*f301c2b1SJay Sternberg (NVME_AEN_CFG_DISC_CHANGE) 41c86b8f7bSChristoph Hellwig 42c86b8f7bSChristoph Hellwig /* 43c86b8f7bSChristoph Hellwig * Plus mandatory SMART AENs (we'll never send them, but allow enabling them): 44c86b8f7bSChristoph Hellwig */ 45c86b8f7bSChristoph Hellwig #define NVMET_AEN_CFG_ALL \ 46c86b8f7bSChristoph Hellwig (NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \ 47c86b8f7bSChristoph Hellwig NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \ 48c86b8f7bSChristoph Hellwig NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL) 49c86b8f7bSChristoph Hellwig 50a07b4970SChristoph Hellwig /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM 51a07b4970SChristoph Hellwig * The 16 bit shift is to set IATTR bit to 1, which means offending 52a07b4970SChristoph Hellwig * offset starts in the data section of connect() 53a07b4970SChristoph Hellwig */ 54a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_DATA(x) \ 55a07b4970SChristoph Hellwig (cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x)))) 56a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_SQE(x) \ 57a07b4970SChristoph Hellwig (cpu_to_le32(offsetof(struct nvmf_connect_command, x))) 58a07b4970SChristoph Hellwig 59a07b4970SChristoph Hellwig struct nvmet_ns { 60a07b4970SChristoph Hellwig struct list_head dev_link; 61a07b4970SChristoph Hellwig struct percpu_ref ref; 62a07b4970SChristoph Hellwig struct block_device *bdev; 63d5eff33eSChaitanya Kulkarni struct file *file; 64dedf0be5SChaitanya Kulkarni bool readonly; 65a07b4970SChristoph Hellwig u32 nsid; 66a07b4970SChristoph Hellwig u32 blksize_shift; 67a07b4970SChristoph Hellwig loff_t size; 68a07b4970SChristoph Hellwig u8 nguid[16]; 69637dc0f3SJohannes Thumshirn uuid_t uuid; 7072efd25dSChristoph Hellwig u32 anagrpid; 71a07b4970SChristoph Hellwig 7255eb942eSChaitanya Kulkarni bool buffered_io; 73e4fcf07cSSolganik Alexander bool enabled; 74a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 75a07b4970SChristoph Hellwig const char *device_path; 76a07b4970SChristoph Hellwig 77a07b4970SChristoph Hellwig struct config_group device_group; 78a07b4970SChristoph Hellwig struct config_group group; 79a07b4970SChristoph Hellwig 80a07b4970SChristoph Hellwig struct completion disable_done; 81d5eff33eSChaitanya Kulkarni mempool_t *bvec_pool; 82d5eff33eSChaitanya Kulkarni struct kmem_cache *bvec_cache; 83c6925093SLogan Gunthorpe 84c6925093SLogan Gunthorpe int use_p2pmem; 85c6925093SLogan Gunthorpe struct pci_dev *p2p_dev; 86a07b4970SChristoph Hellwig }; 87a07b4970SChristoph Hellwig 88a07b4970SChristoph Hellwig static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item) 89a07b4970SChristoph Hellwig { 90a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_ns, group); 91a07b4970SChristoph Hellwig } 92a07b4970SChristoph Hellwig 93c6925093SLogan Gunthorpe static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns) 94c6925093SLogan Gunthorpe { 95c6925093SLogan Gunthorpe return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL; 96c6925093SLogan Gunthorpe } 97c6925093SLogan Gunthorpe 98a07b4970SChristoph Hellwig struct nvmet_cq { 99a07b4970SChristoph Hellwig u16 qid; 100a07b4970SChristoph Hellwig u16 size; 101a07b4970SChristoph Hellwig }; 102a07b4970SChristoph Hellwig 103a07b4970SChristoph Hellwig struct nvmet_sq { 104a07b4970SChristoph Hellwig struct nvmet_ctrl *ctrl; 105a07b4970SChristoph Hellwig struct percpu_ref ref; 106a07b4970SChristoph Hellwig u16 qid; 107a07b4970SChristoph Hellwig u16 size; 108f9cf2a64SJames Smart u32 sqhd; 109a07b4970SChristoph Hellwig struct completion free_done; 110427242ceSSagi Grimberg struct completion confirm_done; 111a07b4970SChristoph Hellwig }; 112a07b4970SChristoph Hellwig 11362ac0d32SChristoph Hellwig struct nvmet_ana_group { 11462ac0d32SChristoph Hellwig struct config_group group; 11562ac0d32SChristoph Hellwig struct nvmet_port *port; 11662ac0d32SChristoph Hellwig u32 grpid; 11762ac0d32SChristoph Hellwig }; 11862ac0d32SChristoph Hellwig 11962ac0d32SChristoph Hellwig static inline struct nvmet_ana_group *to_ana_group(struct config_item *item) 12062ac0d32SChristoph Hellwig { 12162ac0d32SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_ana_group, 12262ac0d32SChristoph Hellwig group); 12362ac0d32SChristoph Hellwig } 12462ac0d32SChristoph Hellwig 125a07b4970SChristoph Hellwig /** 126a07b4970SChristoph Hellwig * struct nvmet_port - Common structure to keep port 127a07b4970SChristoph Hellwig * information for the target. 128fe4a9791SChristoph Hellwig * @entry: Entry into referrals or transport list. 129a07b4970SChristoph Hellwig * @disc_addr: Address information is stored in a format defined 130a07b4970SChristoph Hellwig * for a discovery log page entry. 131a07b4970SChristoph Hellwig * @group: ConfigFS group for this element's folder. 132a07b4970SChristoph Hellwig * @priv: Private data for the transport. 133a07b4970SChristoph Hellwig */ 134a07b4970SChristoph Hellwig struct nvmet_port { 135a07b4970SChristoph Hellwig struct list_head entry; 136a07b4970SChristoph Hellwig struct nvmf_disc_rsp_page_entry disc_addr; 137a07b4970SChristoph Hellwig struct config_group group; 138a07b4970SChristoph Hellwig struct config_group subsys_group; 139a07b4970SChristoph Hellwig struct list_head subsystems; 140a07b4970SChristoph Hellwig struct config_group referrals_group; 141a07b4970SChristoph Hellwig struct list_head referrals; 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; 148a07b4970SChristoph Hellwig }; 149a07b4970SChristoph Hellwig 150a07b4970SChristoph Hellwig static inline struct nvmet_port *to_nvmet_port(struct config_item *item) 151a07b4970SChristoph Hellwig { 152a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_port, 153a07b4970SChristoph Hellwig group); 154a07b4970SChristoph Hellwig } 155a07b4970SChristoph Hellwig 15662ac0d32SChristoph Hellwig static inline struct nvmet_port *ana_groups_to_port( 15762ac0d32SChristoph Hellwig struct config_item *item) 15862ac0d32SChristoph Hellwig { 15962ac0d32SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_port, 16062ac0d32SChristoph Hellwig ana_groups_group); 16162ac0d32SChristoph Hellwig } 16262ac0d32SChristoph Hellwig 163a07b4970SChristoph Hellwig struct nvmet_ctrl { 164a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 165a07b4970SChristoph Hellwig struct nvmet_cq **cqs; 166a07b4970SChristoph Hellwig struct nvmet_sq **sqs; 167a07b4970SChristoph Hellwig 168c09305aeSSagi Grimberg bool cmd_seen; 169c09305aeSSagi Grimberg 170a07b4970SChristoph Hellwig struct mutex lock; 171a07b4970SChristoph Hellwig u64 cap; 172a07b4970SChristoph Hellwig u32 cc; 173a07b4970SChristoph Hellwig u32 csts; 174a07b4970SChristoph Hellwig 17528dd5cf7SOmri Mann uuid_t hostid; 176a07b4970SChristoph Hellwig u16 cntlid; 177a07b4970SChristoph Hellwig u32 kato; 178a07b4970SChristoph Hellwig 1794ee43280SChristoph Hellwig struct nvmet_port *port; 1804ee43280SChristoph Hellwig 181c86b8f7bSChristoph Hellwig u32 aen_enabled; 18255fdd6b6SChristoph Hellwig unsigned long aen_masked; 183a07b4970SChristoph Hellwig struct nvmet_req *async_event_cmds[NVMET_ASYNC_EVENTS]; 184a07b4970SChristoph Hellwig unsigned int nr_async_event_cmds; 185a07b4970SChristoph Hellwig struct list_head async_events; 186a07b4970SChristoph Hellwig struct work_struct async_event_work; 187a07b4970SChristoph Hellwig 188a07b4970SChristoph Hellwig struct list_head subsys_entry; 189a07b4970SChristoph Hellwig struct kref ref; 190a07b4970SChristoph Hellwig struct delayed_work ka_work; 191a07b4970SChristoph Hellwig struct work_struct fatal_err_work; 192a07b4970SChristoph Hellwig 193e929f06dSChristoph Hellwig const struct nvmet_fabrics_ops *ops; 194a07b4970SChristoph Hellwig 195c16734eaSChristoph Hellwig __le32 *changed_ns_list; 196c16734eaSChristoph Hellwig u32 nr_changed_ns; 197c16734eaSChristoph Hellwig 198a07b4970SChristoph Hellwig char subsysnqn[NVMF_NQN_FIELD_LEN]; 199a07b4970SChristoph Hellwig char hostnqn[NVMF_NQN_FIELD_LEN]; 200c6925093SLogan Gunthorpe 201c6925093SLogan Gunthorpe struct device *p2p_client; 202c6925093SLogan Gunthorpe struct radix_tree_root p2p_ns_map; 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; 214a07b4970SChristoph Hellwig 215a07b4970SChristoph Hellwig struct list_head ctrls; 216a07b4970SChristoph Hellwig 217a07b4970SChristoph Hellwig struct list_head hosts; 218a07b4970SChristoph Hellwig bool allow_any_host; 219a07b4970SChristoph Hellwig 220a07b4970SChristoph Hellwig u16 max_qid; 221a07b4970SChristoph Hellwig 222a07b4970SChristoph Hellwig u64 ver; 2232e7f5d2aSJohannes Thumshirn u64 serial; 224a07b4970SChristoph Hellwig char *subsysnqn; 225a07b4970SChristoph Hellwig 226a07b4970SChristoph Hellwig struct config_group group; 227a07b4970SChristoph Hellwig 228a07b4970SChristoph Hellwig struct config_group namespaces_group; 229a07b4970SChristoph Hellwig struct config_group allowed_hosts_group; 230a07b4970SChristoph Hellwig }; 231a07b4970SChristoph Hellwig 232a07b4970SChristoph Hellwig static inline struct nvmet_subsys *to_subsys(struct config_item *item) 233a07b4970SChristoph Hellwig { 234a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_subsys, group); 235a07b4970SChristoph Hellwig } 236a07b4970SChristoph Hellwig 237a07b4970SChristoph Hellwig static inline struct nvmet_subsys *namespaces_to_subsys( 238a07b4970SChristoph Hellwig struct config_item *item) 239a07b4970SChristoph Hellwig { 240a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_subsys, 241a07b4970SChristoph Hellwig namespaces_group); 242a07b4970SChristoph Hellwig } 243a07b4970SChristoph Hellwig 244a07b4970SChristoph Hellwig struct nvmet_host { 245a07b4970SChristoph Hellwig struct config_group group; 246a07b4970SChristoph Hellwig }; 247a07b4970SChristoph Hellwig 248a07b4970SChristoph Hellwig static inline struct nvmet_host *to_host(struct config_item *item) 249a07b4970SChristoph Hellwig { 250a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_host, group); 251a07b4970SChristoph Hellwig } 252a07b4970SChristoph Hellwig 253a07b4970SChristoph Hellwig static inline char *nvmet_host_name(struct nvmet_host *host) 254a07b4970SChristoph Hellwig { 255a07b4970SChristoph Hellwig return config_item_name(&host->group.cg_item); 256a07b4970SChristoph Hellwig } 257a07b4970SChristoph Hellwig 258a07b4970SChristoph Hellwig struct nvmet_host_link { 259a07b4970SChristoph Hellwig struct list_head entry; 260a07b4970SChristoph Hellwig struct nvmet_host *host; 261a07b4970SChristoph Hellwig }; 262a07b4970SChristoph Hellwig 263a07b4970SChristoph Hellwig struct nvmet_subsys_link { 264a07b4970SChristoph Hellwig struct list_head entry; 265a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 266a07b4970SChristoph Hellwig }; 267a07b4970SChristoph Hellwig 268a07b4970SChristoph Hellwig struct nvmet_req; 269a07b4970SChristoph Hellwig struct nvmet_fabrics_ops { 270a07b4970SChristoph Hellwig struct module *owner; 271a07b4970SChristoph Hellwig unsigned int type; 272a07b4970SChristoph Hellwig unsigned int msdbd; 273a07b4970SChristoph Hellwig bool has_keyed_sgls : 1; 274a07b4970SChristoph Hellwig void (*queue_response)(struct nvmet_req *req); 275a07b4970SChristoph Hellwig int (*add_port)(struct nvmet_port *port); 276a07b4970SChristoph Hellwig void (*remove_port)(struct nvmet_port *port); 277a07b4970SChristoph Hellwig void (*delete_ctrl)(struct nvmet_ctrl *ctrl); 2784c652685SSagi Grimberg void (*disc_traddr)(struct nvmet_req *req, 2794c652685SSagi Grimberg struct nvmet_port *port, char *traddr); 280a07b4970SChristoph Hellwig }; 281a07b4970SChristoph Hellwig 282a07b4970SChristoph Hellwig #define NVMET_MAX_INLINE_BIOVEC 8 28373383adfSSagi Grimberg #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE 284a07b4970SChristoph Hellwig 285a07b4970SChristoph Hellwig struct nvmet_req { 286a07b4970SChristoph Hellwig struct nvme_command *cmd; 287a07b4970SChristoph Hellwig struct nvme_completion *rsp; 288a07b4970SChristoph Hellwig struct nvmet_sq *sq; 289a07b4970SChristoph Hellwig struct nvmet_cq *cq; 290a07b4970SChristoph Hellwig struct nvmet_ns *ns; 291a07b4970SChristoph Hellwig struct scatterlist *sg; 292a07b4970SChristoph Hellwig struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC]; 293d5eff33eSChaitanya Kulkarni union { 294d5eff33eSChaitanya Kulkarni struct { 295d5eff33eSChaitanya Kulkarni struct bio inline_bio; 296d5eff33eSChaitanya Kulkarni } b; 297d5eff33eSChaitanya Kulkarni struct { 298d5eff33eSChaitanya Kulkarni bool mpool_alloc; 299d5eff33eSChaitanya Kulkarni struct kiocb iocb; 300d5eff33eSChaitanya Kulkarni struct bio_vec *bvec; 301d5eff33eSChaitanya Kulkarni struct work_struct work; 302d5eff33eSChaitanya Kulkarni } f; 303d5eff33eSChaitanya Kulkarni }; 304a07b4970SChristoph Hellwig int sg_cnt; 3055e62d5c9SChristoph Hellwig /* data length as parsed from the command: */ 306a07b4970SChristoph Hellwig size_t data_len; 3075e62d5c9SChristoph Hellwig /* data length as parsed from the SGL descriptor: */ 3085e62d5c9SChristoph Hellwig size_t transfer_len; 309a07b4970SChristoph Hellwig 310a07b4970SChristoph Hellwig struct nvmet_port *port; 311a07b4970SChristoph Hellwig 312a07b4970SChristoph Hellwig void (*execute)(struct nvmet_req *req); 313e929f06dSChristoph Hellwig const struct nvmet_fabrics_ops *ops; 314c6925093SLogan Gunthorpe 315c6925093SLogan Gunthorpe struct pci_dev *p2p_dev; 316c6925093SLogan Gunthorpe struct device *p2p_client; 317a07b4970SChristoph Hellwig }; 318a07b4970SChristoph Hellwig 31955eb942eSChaitanya Kulkarni extern struct workqueue_struct *buffered_io_wq; 32055eb942eSChaitanya Kulkarni 321a07b4970SChristoph Hellwig static inline void nvmet_set_status(struct nvmet_req *req, u16 status) 322a07b4970SChristoph Hellwig { 323a07b4970SChristoph Hellwig req->rsp->status = cpu_to_le16(status << 1); 324a07b4970SChristoph Hellwig } 325a07b4970SChristoph Hellwig 326a07b4970SChristoph Hellwig static inline void nvmet_set_result(struct nvmet_req *req, u32 result) 327a07b4970SChristoph Hellwig { 328d49187e9SChristoph Hellwig req->rsp->result.u32 = cpu_to_le32(result); 329a07b4970SChristoph Hellwig } 330a07b4970SChristoph Hellwig 331a07b4970SChristoph Hellwig /* 332a07b4970SChristoph Hellwig * NVMe command writes actually are DMA reads for us on the target side. 333a07b4970SChristoph Hellwig */ 334a07b4970SChristoph Hellwig static inline enum dma_data_direction 335a07b4970SChristoph Hellwig nvmet_data_dir(struct nvmet_req *req) 336a07b4970SChristoph Hellwig { 337a07b4970SChristoph Hellwig return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 338a07b4970SChristoph Hellwig } 339a07b4970SChristoph Hellwig 340a07b4970SChristoph Hellwig struct nvmet_async_event { 341a07b4970SChristoph Hellwig struct list_head entry; 342a07b4970SChristoph Hellwig u8 event_type; 343a07b4970SChristoph Hellwig u8 event_info; 344a07b4970SChristoph Hellwig u8 log_page; 345a07b4970SChristoph Hellwig }; 346a07b4970SChristoph Hellwig 3477114ddebSJay Sternberg static inline void nvmet_clear_aen_bit(struct nvmet_req *req, u32 bn) 3486c8312adSJay Sternberg { 3496c8312adSJay Sternberg int rae = le32_to_cpu(req->cmd->common.cdw10[0]) & 1 << 15; 3506c8312adSJay Sternberg 3516c8312adSJay Sternberg if (!rae) 3527114ddebSJay Sternberg clear_bit(bn, &req->sq->ctrl->aen_masked); 3536c8312adSJay Sternberg } 3546c8312adSJay Sternberg 3557114ddebSJay Sternberg static inline bool nvmet_aen_bit_disabled(struct nvmet_ctrl *ctrl, u32 bn) 3566c8312adSJay Sternberg { 3577114ddebSJay Sternberg if (!(READ_ONCE(ctrl->aen_enabled) & (1 << bn))) 3586c8312adSJay Sternberg return true; 3597114ddebSJay Sternberg return test_and_set_bit(bn, &ctrl->aen_masked); 3606c8312adSJay Sternberg } 3616c8312adSJay Sternberg 36290107455SJay Sternberg void nvmet_get_feat_kato(struct nvmet_req *req); 36390107455SJay Sternberg void nvmet_get_feat_async_event(struct nvmet_req *req); 36490107455SJay Sternberg u16 nvmet_set_feat_kato(struct nvmet_req *req); 36590107455SJay Sternberg u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask); 36690107455SJay Sternberg void nvmet_execute_async_event(struct nvmet_req *req); 36790107455SJay Sternberg 36864a0ca88SParav Pandit u16 nvmet_parse_connect_cmd(struct nvmet_req *req); 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); 3785e62d5c9SChristoph Hellwig void nvmet_req_execute(struct nvmet_req *req); 379a07b4970SChristoph Hellwig void nvmet_req_complete(struct nvmet_req *req, u16 status); 3805b2322e4SLogan Gunthorpe int nvmet_req_alloc_sgl(struct nvmet_req *req); 3815b2322e4SLogan Gunthorpe void nvmet_req_free_sgl(struct nvmet_req *req); 382a07b4970SChristoph Hellwig 383f9362ac1SJay Sternberg void nvmet_execute_keep_alive(struct nvmet_req *req); 384f9362ac1SJay Sternberg 385a07b4970SChristoph Hellwig void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid, 386a07b4970SChristoph Hellwig u16 size); 387a07b4970SChristoph Hellwig void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid, 388a07b4970SChristoph Hellwig u16 size); 389a07b4970SChristoph Hellwig void nvmet_sq_destroy(struct nvmet_sq *sq); 390a07b4970SChristoph Hellwig int nvmet_sq_init(struct nvmet_sq *sq); 391a07b4970SChristoph Hellwig 392a07b4970SChristoph Hellwig void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl); 393a07b4970SChristoph Hellwig 394a07b4970SChristoph Hellwig void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new); 395a07b4970SChristoph Hellwig u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn, 396a07b4970SChristoph Hellwig struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp); 397a07b4970SChristoph Hellwig u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid, 398a07b4970SChristoph Hellwig struct nvmet_req *req, struct nvmet_ctrl **ret); 399a07b4970SChristoph Hellwig void nvmet_ctrl_put(struct nvmet_ctrl *ctrl); 40064a0ca88SParav Pandit u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd); 401a07b4970SChristoph Hellwig 402a07b4970SChristoph Hellwig struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, 403a07b4970SChristoph Hellwig enum nvme_subsys_type type); 404a07b4970SChristoph Hellwig void nvmet_subsys_put(struct nvmet_subsys *subsys); 405344770b0SSagi Grimberg void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys); 406a07b4970SChristoph Hellwig 407a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid); 408a07b4970SChristoph Hellwig void nvmet_put_namespace(struct nvmet_ns *ns); 409a07b4970SChristoph Hellwig int nvmet_ns_enable(struct nvmet_ns *ns); 410a07b4970SChristoph Hellwig void nvmet_ns_disable(struct nvmet_ns *ns); 411a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid); 412a07b4970SChristoph Hellwig void nvmet_ns_free(struct nvmet_ns *ns); 413a07b4970SChristoph Hellwig 41462ac0d32SChristoph Hellwig void nvmet_send_ana_event(struct nvmet_subsys *subsys, 41562ac0d32SChristoph Hellwig struct nvmet_port *port); 41662ac0d32SChristoph Hellwig void nvmet_port_send_ana_event(struct nvmet_port *port); 41762ac0d32SChristoph Hellwig 418e929f06dSChristoph Hellwig int nvmet_register_transport(const struct nvmet_fabrics_ops *ops); 419e929f06dSChristoph Hellwig void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops); 420a07b4970SChristoph Hellwig 421a07b4970SChristoph Hellwig int nvmet_enable_port(struct nvmet_port *port); 422a07b4970SChristoph Hellwig void nvmet_disable_port(struct nvmet_port *port); 423a07b4970SChristoph Hellwig 424a07b4970SChristoph Hellwig void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port); 425a07b4970SChristoph Hellwig void nvmet_referral_disable(struct nvmet_port *port); 426a07b4970SChristoph Hellwig 427a07b4970SChristoph Hellwig u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf, 428a07b4970SChristoph Hellwig size_t len); 429a07b4970SChristoph Hellwig u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, 430a07b4970SChristoph Hellwig size_t len); 431c7759fffSChristoph Hellwig u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len); 432a07b4970SChristoph Hellwig 433a07b4970SChristoph Hellwig u32 nvmet_get_log_page_len(struct nvme_command *cmd); 434a07b4970SChristoph Hellwig 435a07b4970SChristoph Hellwig #define NVMET_QUEUE_SIZE 1024 4363375e29cSJames Smart #define NVMET_NR_QUEUES 128 437a07b4970SChristoph Hellwig #define NVMET_MAX_CMD NVMET_QUEUE_SIZE 438793c7cfcSChristoph Hellwig 439793c7cfcSChristoph Hellwig /* 440793c7cfcSChristoph Hellwig * Nice round number that makes a list of nsids fit into a page. 441793c7cfcSChristoph Hellwig * Should become tunable at some point in the future. 442793c7cfcSChristoph Hellwig */ 443793c7cfcSChristoph Hellwig #define NVMET_MAX_NAMESPACES 1024 444793c7cfcSChristoph Hellwig 44572efd25dSChristoph Hellwig /* 44672efd25dSChristoph Hellwig * 0 is not a valid ANA group ID, so we start numbering at 1. 44772efd25dSChristoph Hellwig * 44872efd25dSChristoph Hellwig * ANA Group 1 exists without manual intervention, has namespaces assigned to it 44972efd25dSChristoph Hellwig * by default, and is available in an optimized state through all ports. 45072efd25dSChristoph Hellwig */ 45162ac0d32SChristoph Hellwig #define NVMET_MAX_ANAGRPS 128 45272efd25dSChristoph Hellwig #define NVMET_DEFAULT_ANA_GRPID 1 45372efd25dSChristoph Hellwig 454a07b4970SChristoph Hellwig #define NVMET_KAS 10 455f9362ac1SJay Sternberg #define NVMET_DISC_KATO_MS 120000 456a07b4970SChristoph Hellwig 457a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void); 458a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void); 459a07b4970SChristoph Hellwig 460a07b4970SChristoph Hellwig int __init nvmet_init_discovery(void); 461a07b4970SChristoph Hellwig void nvmet_exit_discovery(void); 462a07b4970SChristoph Hellwig 463a07b4970SChristoph Hellwig extern struct nvmet_subsys *nvmet_disc_subsys; 464a07b4970SChristoph Hellwig extern u64 nvmet_genctr; 465a07b4970SChristoph Hellwig extern struct rw_semaphore nvmet_config_sem; 466a07b4970SChristoph Hellwig 46772efd25dSChristoph Hellwig extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1]; 46872efd25dSChristoph Hellwig extern u64 nvmet_ana_chgcnt; 46972efd25dSChristoph Hellwig extern struct rw_semaphore nvmet_ana_sem; 47072efd25dSChristoph Hellwig 471a07b4970SChristoph Hellwig bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys, 472a07b4970SChristoph Hellwig const char *hostnqn); 473a07b4970SChristoph Hellwig 474d5eff33eSChaitanya Kulkarni int nvmet_bdev_ns_enable(struct nvmet_ns *ns); 475d5eff33eSChaitanya Kulkarni int nvmet_file_ns_enable(struct nvmet_ns *ns); 476d5eff33eSChaitanya Kulkarni void nvmet_bdev_ns_disable(struct nvmet_ns *ns); 477d5eff33eSChaitanya Kulkarni void nvmet_file_ns_disable(struct nvmet_ns *ns); 478dedf0be5SChaitanya Kulkarni u16 nvmet_bdev_flush(struct nvmet_req *req); 479dedf0be5SChaitanya Kulkarni u16 nvmet_file_flush(struct nvmet_req *req); 480dedf0be5SChaitanya Kulkarni void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid); 481d5eff33eSChaitanya Kulkarni 482d5eff33eSChaitanya Kulkarni static inline u32 nvmet_rw_len(struct nvmet_req *req) 483d5eff33eSChaitanya Kulkarni { 484d5eff33eSChaitanya Kulkarni return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) << 485d5eff33eSChaitanya Kulkarni req->ns->blksize_shift; 486d5eff33eSChaitanya Kulkarni } 487a07b4970SChristoph Hellwig #endif /* _NVMET_H */ 488