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