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> 29a07b4970SChristoph Hellwig 30a07b4970SChristoph Hellwig #define NVMET_ASYNC_EVENTS 4 31a07b4970SChristoph Hellwig #define NVMET_ERROR_LOG_SLOTS 128 32a07b4970SChristoph Hellwig 33a07b4970SChristoph Hellwig /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM 34a07b4970SChristoph Hellwig * The 16 bit shift is to set IATTR bit to 1, which means offending 35a07b4970SChristoph Hellwig * offset starts in the data section of connect() 36a07b4970SChristoph Hellwig */ 37a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_DATA(x) \ 38a07b4970SChristoph Hellwig (cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x)))) 39a07b4970SChristoph Hellwig #define IPO_IATTR_CONNECT_SQE(x) \ 40a07b4970SChristoph Hellwig (cpu_to_le32(offsetof(struct nvmf_connect_command, x))) 41a07b4970SChristoph Hellwig 42a07b4970SChristoph Hellwig struct nvmet_ns { 43a07b4970SChristoph Hellwig struct list_head dev_link; 44a07b4970SChristoph Hellwig struct percpu_ref ref; 45a07b4970SChristoph Hellwig struct block_device *bdev; 46a07b4970SChristoph Hellwig u32 nsid; 47a07b4970SChristoph Hellwig u32 blksize_shift; 48a07b4970SChristoph Hellwig loff_t size; 49a07b4970SChristoph Hellwig u8 nguid[16]; 50637dc0f3SJohannes Thumshirn uuid_t uuid; 51a07b4970SChristoph Hellwig 52e4fcf07cSSolganik Alexander bool enabled; 53a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 54a07b4970SChristoph Hellwig const char *device_path; 55a07b4970SChristoph Hellwig 56a07b4970SChristoph Hellwig struct config_group device_group; 57a07b4970SChristoph Hellwig struct config_group group; 58a07b4970SChristoph Hellwig 59a07b4970SChristoph Hellwig struct completion disable_done; 60a07b4970SChristoph Hellwig }; 61a07b4970SChristoph Hellwig 62a07b4970SChristoph Hellwig static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item) 63a07b4970SChristoph Hellwig { 64a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_ns, group); 65a07b4970SChristoph Hellwig } 66a07b4970SChristoph Hellwig 67a07b4970SChristoph Hellwig struct nvmet_cq { 68a07b4970SChristoph Hellwig u16 qid; 69a07b4970SChristoph Hellwig u16 size; 70a07b4970SChristoph Hellwig }; 71a07b4970SChristoph Hellwig 72a07b4970SChristoph Hellwig struct nvmet_sq { 73a07b4970SChristoph Hellwig struct nvmet_ctrl *ctrl; 74a07b4970SChristoph Hellwig struct percpu_ref ref; 75a07b4970SChristoph Hellwig u16 qid; 76a07b4970SChristoph Hellwig u16 size; 77a07b4970SChristoph Hellwig struct completion free_done; 78427242ceSSagi Grimberg struct completion confirm_done; 79a07b4970SChristoph Hellwig }; 80a07b4970SChristoph Hellwig 81a07b4970SChristoph Hellwig /** 82a07b4970SChristoph Hellwig * struct nvmet_port - Common structure to keep port 83a07b4970SChristoph Hellwig * information for the target. 84a07b4970SChristoph Hellwig * @entry: List head for holding a list of these elements. 85a07b4970SChristoph Hellwig * @disc_addr: Address information is stored in a format defined 86a07b4970SChristoph Hellwig * for a discovery log page entry. 87a07b4970SChristoph Hellwig * @group: ConfigFS group for this element's folder. 88a07b4970SChristoph Hellwig * @priv: Private data for the transport. 89a07b4970SChristoph Hellwig */ 90a07b4970SChristoph Hellwig struct nvmet_port { 91a07b4970SChristoph Hellwig struct list_head entry; 92a07b4970SChristoph Hellwig struct nvmf_disc_rsp_page_entry disc_addr; 93a07b4970SChristoph Hellwig struct config_group group; 94a07b4970SChristoph Hellwig struct config_group subsys_group; 95a07b4970SChristoph Hellwig struct list_head subsystems; 96a07b4970SChristoph Hellwig struct config_group referrals_group; 97a07b4970SChristoph Hellwig struct list_head referrals; 98a07b4970SChristoph Hellwig void *priv; 99a07b4970SChristoph Hellwig bool enabled; 100a07b4970SChristoph Hellwig }; 101a07b4970SChristoph Hellwig 102a07b4970SChristoph Hellwig static inline struct nvmet_port *to_nvmet_port(struct config_item *item) 103a07b4970SChristoph Hellwig { 104a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_port, 105a07b4970SChristoph Hellwig group); 106a07b4970SChristoph Hellwig } 107a07b4970SChristoph Hellwig 108a07b4970SChristoph Hellwig struct nvmet_ctrl { 109a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 110a07b4970SChristoph Hellwig struct nvmet_cq **cqs; 111a07b4970SChristoph Hellwig struct nvmet_sq **sqs; 112a07b4970SChristoph Hellwig 113a07b4970SChristoph Hellwig struct mutex lock; 114a07b4970SChristoph Hellwig u64 cap; 115a07b4970SChristoph Hellwig u32 cc; 116a07b4970SChristoph Hellwig u32 csts; 117a07b4970SChristoph Hellwig 118*28dd5cf7SOmri Mann uuid_t hostid; 119a07b4970SChristoph Hellwig u16 cntlid; 120a07b4970SChristoph Hellwig u32 kato; 121a07b4970SChristoph Hellwig 122a07b4970SChristoph Hellwig struct nvmet_req *async_event_cmds[NVMET_ASYNC_EVENTS]; 123a07b4970SChristoph Hellwig unsigned int nr_async_event_cmds; 124a07b4970SChristoph Hellwig struct list_head async_events; 125a07b4970SChristoph Hellwig struct work_struct async_event_work; 126a07b4970SChristoph Hellwig 127a07b4970SChristoph Hellwig struct list_head subsys_entry; 128a07b4970SChristoph Hellwig struct kref ref; 129a07b4970SChristoph Hellwig struct delayed_work ka_work; 130a07b4970SChristoph Hellwig struct work_struct fatal_err_work; 131a07b4970SChristoph Hellwig 132a07b4970SChristoph Hellwig struct nvmet_fabrics_ops *ops; 133a07b4970SChristoph Hellwig 134a07b4970SChristoph Hellwig char subsysnqn[NVMF_NQN_FIELD_LEN]; 135a07b4970SChristoph Hellwig char hostnqn[NVMF_NQN_FIELD_LEN]; 136a07b4970SChristoph Hellwig }; 137a07b4970SChristoph Hellwig 138a07b4970SChristoph Hellwig struct nvmet_subsys { 139a07b4970SChristoph Hellwig enum nvme_subsys_type type; 140a07b4970SChristoph Hellwig 141a07b4970SChristoph Hellwig struct mutex lock; 142a07b4970SChristoph Hellwig struct kref ref; 143a07b4970SChristoph Hellwig 144a07b4970SChristoph Hellwig struct list_head namespaces; 145a07b4970SChristoph Hellwig unsigned int max_nsid; 146a07b4970SChristoph Hellwig 147a07b4970SChristoph Hellwig struct list_head ctrls; 148a07b4970SChristoph Hellwig 149a07b4970SChristoph Hellwig struct list_head hosts; 150a07b4970SChristoph Hellwig bool allow_any_host; 151a07b4970SChristoph Hellwig 152a07b4970SChristoph Hellwig u16 max_qid; 153a07b4970SChristoph Hellwig 154a07b4970SChristoph Hellwig u64 ver; 1552e7f5d2aSJohannes Thumshirn u64 serial; 156a07b4970SChristoph Hellwig char *subsysnqn; 157a07b4970SChristoph Hellwig 158a07b4970SChristoph Hellwig struct config_group group; 159a07b4970SChristoph Hellwig 160a07b4970SChristoph Hellwig struct config_group namespaces_group; 161a07b4970SChristoph Hellwig struct config_group allowed_hosts_group; 162a07b4970SChristoph Hellwig }; 163a07b4970SChristoph Hellwig 164a07b4970SChristoph Hellwig static inline struct nvmet_subsys *to_subsys(struct config_item *item) 165a07b4970SChristoph Hellwig { 166a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_subsys, group); 167a07b4970SChristoph Hellwig } 168a07b4970SChristoph Hellwig 169a07b4970SChristoph Hellwig static inline struct nvmet_subsys *namespaces_to_subsys( 170a07b4970SChristoph Hellwig struct config_item *item) 171a07b4970SChristoph Hellwig { 172a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_subsys, 173a07b4970SChristoph Hellwig namespaces_group); 174a07b4970SChristoph Hellwig } 175a07b4970SChristoph Hellwig 176a07b4970SChristoph Hellwig struct nvmet_host { 177a07b4970SChristoph Hellwig struct config_group group; 178a07b4970SChristoph Hellwig }; 179a07b4970SChristoph Hellwig 180a07b4970SChristoph Hellwig static inline struct nvmet_host *to_host(struct config_item *item) 181a07b4970SChristoph Hellwig { 182a07b4970SChristoph Hellwig return container_of(to_config_group(item), struct nvmet_host, group); 183a07b4970SChristoph Hellwig } 184a07b4970SChristoph Hellwig 185a07b4970SChristoph Hellwig static inline char *nvmet_host_name(struct nvmet_host *host) 186a07b4970SChristoph Hellwig { 187a07b4970SChristoph Hellwig return config_item_name(&host->group.cg_item); 188a07b4970SChristoph Hellwig } 189a07b4970SChristoph Hellwig 190a07b4970SChristoph Hellwig struct nvmet_host_link { 191a07b4970SChristoph Hellwig struct list_head entry; 192a07b4970SChristoph Hellwig struct nvmet_host *host; 193a07b4970SChristoph Hellwig }; 194a07b4970SChristoph Hellwig 195a07b4970SChristoph Hellwig struct nvmet_subsys_link { 196a07b4970SChristoph Hellwig struct list_head entry; 197a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 198a07b4970SChristoph Hellwig }; 199a07b4970SChristoph Hellwig 200a07b4970SChristoph Hellwig struct nvmet_req; 201a07b4970SChristoph Hellwig struct nvmet_fabrics_ops { 202a07b4970SChristoph Hellwig struct module *owner; 203a07b4970SChristoph Hellwig unsigned int type; 204a07b4970SChristoph Hellwig unsigned int sqe_inline_size; 205a07b4970SChristoph Hellwig unsigned int msdbd; 206a07b4970SChristoph Hellwig bool has_keyed_sgls : 1; 207a07b4970SChristoph Hellwig void (*queue_response)(struct nvmet_req *req); 208a07b4970SChristoph Hellwig int (*add_port)(struct nvmet_port *port); 209a07b4970SChristoph Hellwig void (*remove_port)(struct nvmet_port *port); 210a07b4970SChristoph Hellwig void (*delete_ctrl)(struct nvmet_ctrl *ctrl); 211a07b4970SChristoph Hellwig }; 212a07b4970SChristoph Hellwig 213a07b4970SChristoph Hellwig #define NVMET_MAX_INLINE_BIOVEC 8 214a07b4970SChristoph Hellwig 215a07b4970SChristoph Hellwig struct nvmet_req { 216a07b4970SChristoph Hellwig struct nvme_command *cmd; 217a07b4970SChristoph Hellwig struct nvme_completion *rsp; 218a07b4970SChristoph Hellwig struct nvmet_sq *sq; 219a07b4970SChristoph Hellwig struct nvmet_cq *cq; 220a07b4970SChristoph Hellwig struct nvmet_ns *ns; 221a07b4970SChristoph Hellwig struct scatterlist *sg; 222a07b4970SChristoph Hellwig struct bio inline_bio; 223a07b4970SChristoph Hellwig struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC]; 224a07b4970SChristoph Hellwig int sg_cnt; 225a07b4970SChristoph Hellwig size_t data_len; 226a07b4970SChristoph Hellwig 227a07b4970SChristoph Hellwig struct nvmet_port *port; 228a07b4970SChristoph Hellwig 229a07b4970SChristoph Hellwig void (*execute)(struct nvmet_req *req); 230a07b4970SChristoph Hellwig struct nvmet_fabrics_ops *ops; 231a07b4970SChristoph Hellwig }; 232a07b4970SChristoph Hellwig 233a07b4970SChristoph Hellwig static inline void nvmet_set_status(struct nvmet_req *req, u16 status) 234a07b4970SChristoph Hellwig { 235a07b4970SChristoph Hellwig req->rsp->status = cpu_to_le16(status << 1); 236a07b4970SChristoph Hellwig } 237a07b4970SChristoph Hellwig 238a07b4970SChristoph Hellwig static inline void nvmet_set_result(struct nvmet_req *req, u32 result) 239a07b4970SChristoph Hellwig { 240d49187e9SChristoph Hellwig req->rsp->result.u32 = cpu_to_le32(result); 241a07b4970SChristoph Hellwig } 242a07b4970SChristoph Hellwig 243a07b4970SChristoph Hellwig /* 244a07b4970SChristoph Hellwig * NVMe command writes actually are DMA reads for us on the target side. 245a07b4970SChristoph Hellwig */ 246a07b4970SChristoph Hellwig static inline enum dma_data_direction 247a07b4970SChristoph Hellwig nvmet_data_dir(struct nvmet_req *req) 248a07b4970SChristoph Hellwig { 249a07b4970SChristoph Hellwig return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 250a07b4970SChristoph Hellwig } 251a07b4970SChristoph Hellwig 252a07b4970SChristoph Hellwig struct nvmet_async_event { 253a07b4970SChristoph Hellwig struct list_head entry; 254a07b4970SChristoph Hellwig u8 event_type; 255a07b4970SChristoph Hellwig u8 event_info; 256a07b4970SChristoph Hellwig u8 log_page; 257a07b4970SChristoph Hellwig }; 258a07b4970SChristoph Hellwig 25964a0ca88SParav Pandit u16 nvmet_parse_connect_cmd(struct nvmet_req *req); 26064a0ca88SParav Pandit u16 nvmet_parse_io_cmd(struct nvmet_req *req); 26164a0ca88SParav Pandit u16 nvmet_parse_admin_cmd(struct nvmet_req *req); 26264a0ca88SParav Pandit u16 nvmet_parse_discovery_cmd(struct nvmet_req *req); 26364a0ca88SParav Pandit u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req); 264a07b4970SChristoph Hellwig 265a07b4970SChristoph Hellwig bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq, 266a07b4970SChristoph Hellwig struct nvmet_sq *sq, struct nvmet_fabrics_ops *ops); 267549f01aeSVijay Immanuel void nvmet_req_uninit(struct nvmet_req *req); 268a07b4970SChristoph Hellwig void nvmet_req_complete(struct nvmet_req *req, u16 status); 269a07b4970SChristoph Hellwig 270a07b4970SChristoph Hellwig void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid, 271a07b4970SChristoph Hellwig u16 size); 272a07b4970SChristoph Hellwig void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid, 273a07b4970SChristoph Hellwig u16 size); 274a07b4970SChristoph Hellwig void nvmet_sq_destroy(struct nvmet_sq *sq); 275a07b4970SChristoph Hellwig int nvmet_sq_init(struct nvmet_sq *sq); 276a07b4970SChristoph Hellwig 277a07b4970SChristoph Hellwig void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl); 278a07b4970SChristoph Hellwig 279a07b4970SChristoph Hellwig void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new); 280a07b4970SChristoph Hellwig u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn, 281a07b4970SChristoph Hellwig struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp); 282a07b4970SChristoph Hellwig u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid, 283a07b4970SChristoph Hellwig struct nvmet_req *req, struct nvmet_ctrl **ret); 284a07b4970SChristoph Hellwig void nvmet_ctrl_put(struct nvmet_ctrl *ctrl); 28564a0ca88SParav Pandit u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd); 286a07b4970SChristoph Hellwig 287a07b4970SChristoph Hellwig struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, 288a07b4970SChristoph Hellwig enum nvme_subsys_type type); 289a07b4970SChristoph Hellwig void nvmet_subsys_put(struct nvmet_subsys *subsys); 290344770b0SSagi Grimberg void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys); 291a07b4970SChristoph Hellwig 292a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid); 293a07b4970SChristoph Hellwig void nvmet_put_namespace(struct nvmet_ns *ns); 294a07b4970SChristoph Hellwig int nvmet_ns_enable(struct nvmet_ns *ns); 295a07b4970SChristoph Hellwig void nvmet_ns_disable(struct nvmet_ns *ns); 296a07b4970SChristoph Hellwig struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid); 297a07b4970SChristoph Hellwig void nvmet_ns_free(struct nvmet_ns *ns); 298a07b4970SChristoph Hellwig 299a07b4970SChristoph Hellwig int nvmet_register_transport(struct nvmet_fabrics_ops *ops); 300a07b4970SChristoph Hellwig void nvmet_unregister_transport(struct nvmet_fabrics_ops *ops); 301a07b4970SChristoph Hellwig 302a07b4970SChristoph Hellwig int nvmet_enable_port(struct nvmet_port *port); 303a07b4970SChristoph Hellwig void nvmet_disable_port(struct nvmet_port *port); 304a07b4970SChristoph Hellwig 305a07b4970SChristoph Hellwig void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port); 306a07b4970SChristoph Hellwig void nvmet_referral_disable(struct nvmet_port *port); 307a07b4970SChristoph Hellwig 308a07b4970SChristoph Hellwig u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf, 309a07b4970SChristoph Hellwig size_t len); 310a07b4970SChristoph Hellwig u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, 311a07b4970SChristoph Hellwig size_t len); 312a07b4970SChristoph Hellwig 313a07b4970SChristoph Hellwig u32 nvmet_get_log_page_len(struct nvme_command *cmd); 314a07b4970SChristoph Hellwig 315a07b4970SChristoph Hellwig #define NVMET_QUEUE_SIZE 1024 316a07b4970SChristoph Hellwig #define NVMET_NR_QUEUES 64 317a07b4970SChristoph Hellwig #define NVMET_MAX_CMD NVMET_QUEUE_SIZE 318a07b4970SChristoph Hellwig #define NVMET_KAS 10 319a07b4970SChristoph Hellwig #define NVMET_DISC_KATO 120 320a07b4970SChristoph Hellwig 321a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void); 322a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void); 323a07b4970SChristoph Hellwig 324a07b4970SChristoph Hellwig int __init nvmet_init_discovery(void); 325a07b4970SChristoph Hellwig void nvmet_exit_discovery(void); 326a07b4970SChristoph Hellwig 327a07b4970SChristoph Hellwig extern struct nvmet_subsys *nvmet_disc_subsys; 328a07b4970SChristoph Hellwig extern u64 nvmet_genctr; 329a07b4970SChristoph Hellwig extern struct rw_semaphore nvmet_config_sem; 330a07b4970SChristoph Hellwig 331a07b4970SChristoph Hellwig bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys, 332a07b4970SChristoph Hellwig const char *hostnqn); 333a07b4970SChristoph Hellwig 334a07b4970SChristoph Hellwig #endif /* _NVMET_H */ 335