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