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