2 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
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.
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
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 #include <linux/radix-tree.h>
31 #define NVMET_ASYNC_EVENTS 4
32 #define NVMET_ERROR_LOG_SLOTS 128
35 * Supported optional AENs:
37 #define NVMET_AEN_CFG_OPTIONAL \
38 (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE)
41 * Plus mandatory SMART AENs (we'll never send them, but allow enabling them):
43 #define NVMET_AEN_CFG_ALL \
44 (NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \
45 NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \
46 NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL)
48 /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM
49 * The 16 bit shift is to set IATTR bit to 1, which means offending
50 * offset starts in the data section of connect()
52 #define IPO_IATTR_CONNECT_DATA(x) \
53 (cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x))))
54 #define IPO_IATTR_CONNECT_SQE(x) \
55 (cpu_to_le32(offsetof(struct nvmf_connect_command, x)))
58 struct list_head dev_link;
59 struct percpu_ref ref;
60 struct block_device *bdev;
72 struct nvmet_subsys *subsys;
73 const char *device_path;
75 struct config_group device_group;
76 struct config_group group;
78 struct completion disable_done;
80 struct kmem_cache *bvec_cache;
83 struct pci_dev *p2p_dev;
86 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
88 return container_of(to_config_group(item), struct nvmet_ns, group);
91 static inline struct device *nvmet_ns_dev(struct nvmet_ns *ns)
93 return ns->bdev ? disk_to_dev(ns->bdev->bd_disk) : NULL;
102 struct nvmet_ctrl *ctrl;
103 struct percpu_ref ref;
107 struct completion free_done;
108 struct completion confirm_done;
111 struct nvmet_ana_group {
112 struct config_group group;
113 struct nvmet_port *port;
117 static inline struct nvmet_ana_group *to_ana_group(struct config_item *item)
119 return container_of(to_config_group(item), struct nvmet_ana_group,
124 * struct nvmet_port - Common structure to keep port
125 * information for the target.
126 * @entry: Entry into referrals or transport list.
127 * @disc_addr: Address information is stored in a format defined
128 * for a discovery log page entry.
129 * @group: ConfigFS group for this element's folder.
130 * @priv: Private data for the transport.
133 struct list_head entry;
134 struct nvmf_disc_rsp_page_entry disc_addr;
135 struct config_group group;
136 struct config_group subsys_group;
137 struct list_head subsystems;
138 struct config_group referrals_group;
139 struct list_head referrals;
140 struct config_group ana_groups_group;
141 struct nvmet_ana_group ana_default_group;
142 enum nvme_ana_state *ana_state;
145 int inline_data_size;
148 static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
150 return container_of(to_config_group(item), struct nvmet_port,
154 static inline struct nvmet_port *ana_groups_to_port(
155 struct config_item *item)
157 return container_of(to_config_group(item), struct nvmet_port,
162 struct nvmet_subsys *subsys;
163 struct nvmet_cq **cqs;
164 struct nvmet_sq **sqs;
177 struct nvmet_port *port;
180 unsigned long aen_masked;
181 struct nvmet_req *async_event_cmds[NVMET_ASYNC_EVENTS];
182 unsigned int nr_async_event_cmds;
183 struct list_head async_events;
184 struct work_struct async_event_work;
186 struct list_head subsys_entry;
188 struct delayed_work ka_work;
189 struct work_struct fatal_err_work;
191 const struct nvmet_fabrics_ops *ops;
193 __le32 *changed_ns_list;
196 char subsysnqn[NVMF_NQN_FIELD_LEN];
197 char hostnqn[NVMF_NQN_FIELD_LEN];
199 struct device *p2p_client;
200 struct radix_tree_root p2p_ns_map;
203 struct nvmet_subsys {
204 enum nvme_subsys_type type;
209 struct list_head namespaces;
210 unsigned int nr_namespaces;
211 unsigned int max_nsid;
213 struct list_head ctrls;
215 struct list_head hosts;
224 struct config_group group;
226 struct config_group namespaces_group;
227 struct config_group allowed_hosts_group;
230 static inline struct nvmet_subsys *to_subsys(struct config_item *item)
232 return container_of(to_config_group(item), struct nvmet_subsys, group);
235 static inline struct nvmet_subsys *namespaces_to_subsys(
236 struct config_item *item)
238 return container_of(to_config_group(item), struct nvmet_subsys,
243 struct config_group group;
246 static inline struct nvmet_host *to_host(struct config_item *item)
248 return container_of(to_config_group(item), struct nvmet_host, group);
251 static inline char *nvmet_host_name(struct nvmet_host *host)
253 return config_item_name(&host->group.cg_item);
256 struct nvmet_host_link {
257 struct list_head entry;
258 struct nvmet_host *host;
261 struct nvmet_subsys_link {
262 struct list_head entry;
263 struct nvmet_subsys *subsys;
267 struct nvmet_fabrics_ops {
268 struct module *owner;
271 bool has_keyed_sgls : 1;
272 void (*queue_response)(struct nvmet_req *req);
273 int (*add_port)(struct nvmet_port *port);
274 void (*remove_port)(struct nvmet_port *port);
275 void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
276 void (*disc_traddr)(struct nvmet_req *req,
277 struct nvmet_port *port, char *traddr);
280 #define NVMET_MAX_INLINE_BIOVEC 8
281 #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
284 struct nvme_command *cmd;
285 struct nvme_completion *rsp;
289 struct scatterlist *sg;
290 struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC];
293 struct bio inline_bio;
298 struct bio_vec *bvec;
299 struct work_struct work;
303 /* data length as parsed from the command: */
305 /* data length as parsed from the SGL descriptor: */
308 struct nvmet_port *port;
310 void (*execute)(struct nvmet_req *req);
311 const struct nvmet_fabrics_ops *ops;
313 struct pci_dev *p2p_dev;
314 struct device *p2p_client;
317 extern struct workqueue_struct *buffered_io_wq;
319 static inline void nvmet_set_status(struct nvmet_req *req, u16 status)
321 req->rsp->status = cpu_to_le16(status << 1);
324 static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
326 req->rsp->result.u32 = cpu_to_le32(result);
330 * NVMe command writes actually are DMA reads for us on the target side.
332 static inline enum dma_data_direction
333 nvmet_data_dir(struct nvmet_req *req)
335 return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
338 struct nvmet_async_event {
339 struct list_head entry;
345 u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
346 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
347 u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
348 u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
349 u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
350 u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req);
352 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
353 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
354 void nvmet_req_uninit(struct nvmet_req *req);
355 void nvmet_req_execute(struct nvmet_req *req);
356 void nvmet_req_complete(struct nvmet_req *req, u16 status);
357 int nvmet_req_alloc_sgl(struct nvmet_req *req);
358 void nvmet_req_free_sgl(struct nvmet_req *req);
360 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
362 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
364 void nvmet_sq_destroy(struct nvmet_sq *sq);
365 int nvmet_sq_init(struct nvmet_sq *sq);
367 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
369 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
370 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
371 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
372 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
373 struct nvmet_req *req, struct nvmet_ctrl **ret);
374 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
375 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd);
377 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
378 enum nvme_subsys_type type);
379 void nvmet_subsys_put(struct nvmet_subsys *subsys);
380 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
382 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
383 void nvmet_put_namespace(struct nvmet_ns *ns);
384 int nvmet_ns_enable(struct nvmet_ns *ns);
385 void nvmet_ns_disable(struct nvmet_ns *ns);
386 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
387 void nvmet_ns_free(struct nvmet_ns *ns);
389 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
390 struct nvmet_port *port);
391 void nvmet_port_send_ana_event(struct nvmet_port *port);
393 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
394 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
396 int nvmet_enable_port(struct nvmet_port *port);
397 void nvmet_disable_port(struct nvmet_port *port);
399 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
400 void nvmet_referral_disable(struct nvmet_port *port);
402 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
404 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
406 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
408 u32 nvmet_get_log_page_len(struct nvme_command *cmd);
410 #define NVMET_QUEUE_SIZE 1024
411 #define NVMET_NR_QUEUES 128
412 #define NVMET_MAX_CMD NVMET_QUEUE_SIZE
415 * Nice round number that makes a list of nsids fit into a page.
416 * Should become tunable at some point in the future.
418 #define NVMET_MAX_NAMESPACES 1024
421 * 0 is not a valid ANA group ID, so we start numbering at 1.
423 * ANA Group 1 exists without manual intervention, has namespaces assigned to it
424 * by default, and is available in an optimized state through all ports.
426 #define NVMET_MAX_ANAGRPS 128
427 #define NVMET_DEFAULT_ANA_GRPID 1
430 #define NVMET_DISC_KATO 120
432 int __init nvmet_init_configfs(void);
433 void __exit nvmet_exit_configfs(void);
435 int __init nvmet_init_discovery(void);
436 void nvmet_exit_discovery(void);
438 extern struct nvmet_subsys *nvmet_disc_subsys;
439 extern u64 nvmet_genctr;
440 extern struct rw_semaphore nvmet_config_sem;
442 extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
443 extern u64 nvmet_ana_chgcnt;
444 extern struct rw_semaphore nvmet_ana_sem;
446 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
447 const char *hostnqn);
449 int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
450 int nvmet_file_ns_enable(struct nvmet_ns *ns);
451 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
452 void nvmet_file_ns_disable(struct nvmet_ns *ns);
453 u16 nvmet_bdev_flush(struct nvmet_req *req);
454 u16 nvmet_file_flush(struct nvmet_req *req);
455 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
457 static inline u32 nvmet_rw_len(struct nvmet_req *req)
459 return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
460 req->ns->blksize_shift;
462 #endif /* _NVMET_H */