Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
34/*
35 * Supported optional AENs:
36 */
37#define NVMET_AEN_CFG_OPTIONAL \
38 NVME_AEN_CFG_NS_ATTR
39
40/*
41 * Plus mandatory SMART AENs (we'll never send them, but allow enabling them):
42 */
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)
47
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()
51 */
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)))
56
57struct nvmet_ns {
58 struct list_head dev_link;
59 struct percpu_ref ref;
60 struct block_device *bdev;
61 struct file *file;
62 u32 nsid;
63 u32 blksize_shift;
64 loff_t size;
65 u8 nguid[16];
66 uuid_t uuid;
67
68 bool enabled;
69 struct nvmet_subsys *subsys;
70 const char *device_path;
71
72 struct config_group device_group;
73 struct config_group group;
74
75 struct completion disable_done;
76 mempool_t *bvec_pool;
77 struct kmem_cache *bvec_cache;
78};
79
80static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
81{
82 return container_of(to_config_group(item), struct nvmet_ns, group);
83}
84
85struct nvmet_cq {
86 u16 qid;
87 u16 size;
88};
89
90struct nvmet_sq {
91 struct nvmet_ctrl *ctrl;
92 struct percpu_ref ref;
93 u16 qid;
94 u16 size;
95 u32 sqhd;
96 struct completion free_done;
97 struct completion confirm_done;
98};
99
100/**
101 * struct nvmet_port - Common structure to keep port
102 * information for the target.
103 * @entry: Entry into referrals or transport list.
104 * @disc_addr: Address information is stored in a format defined
105 * for a discovery log page entry.
106 * @group: ConfigFS group for this element's folder.
107 * @priv: Private data for the transport.
108 */
109struct nvmet_port {
110 struct list_head entry;
111 struct nvmf_disc_rsp_page_entry disc_addr;
112 struct config_group group;
113 struct config_group subsys_group;
114 struct list_head subsystems;
115 struct config_group referrals_group;
116 struct list_head referrals;
117 void *priv;
118 bool enabled;
119};
120
121static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
122{
123 return container_of(to_config_group(item), struct nvmet_port,
124 group);
125}
126
127struct nvmet_ctrl {
128 struct nvmet_subsys *subsys;
129 struct nvmet_cq **cqs;
130 struct nvmet_sq **sqs;
131
132 struct mutex lock;
133 u64 cap;
134 u32 cc;
135 u32 csts;
136
137 uuid_t hostid;
138 u16 cntlid;
139 u32 kato;
140
141 u32 aen_enabled;
142 unsigned long aen_masked;
143 struct nvmet_req *async_event_cmds[NVMET_ASYNC_EVENTS];
144 unsigned int nr_async_event_cmds;
145 struct list_head async_events;
146 struct work_struct async_event_work;
147
148 struct list_head subsys_entry;
149 struct kref ref;
150 struct delayed_work ka_work;
151 struct work_struct fatal_err_work;
152
153 const struct nvmet_fabrics_ops *ops;
154
155 __le32 *changed_ns_list;
156 u32 nr_changed_ns;
157
158 char subsysnqn[NVMF_NQN_FIELD_LEN];
159 char hostnqn[NVMF_NQN_FIELD_LEN];
160};
161
162struct nvmet_subsys {
163 enum nvme_subsys_type type;
164
165 struct mutex lock;
166 struct kref ref;
167
168 struct list_head namespaces;
169 unsigned int max_nsid;
170
171 struct list_head ctrls;
172
173 struct list_head hosts;
174 bool allow_any_host;
175
176 u16 max_qid;
177
178 u64 ver;
179 u64 serial;
180 char *subsysnqn;
181
182 struct config_group group;
183
184 struct config_group namespaces_group;
185 struct config_group allowed_hosts_group;
186};
187
188static inline struct nvmet_subsys *to_subsys(struct config_item *item)
189{
190 return container_of(to_config_group(item), struct nvmet_subsys, group);
191}
192
193static inline struct nvmet_subsys *namespaces_to_subsys(
194 struct config_item *item)
195{
196 return container_of(to_config_group(item), struct nvmet_subsys,
197 namespaces_group);
198}
199
200struct nvmet_host {
201 struct config_group group;
202};
203
204static inline struct nvmet_host *to_host(struct config_item *item)
205{
206 return container_of(to_config_group(item), struct nvmet_host, group);
207}
208
209static inline char *nvmet_host_name(struct nvmet_host *host)
210{
211 return config_item_name(&host->group.cg_item);
212}
213
214struct nvmet_host_link {
215 struct list_head entry;
216 struct nvmet_host *host;
217};
218
219struct nvmet_subsys_link {
220 struct list_head entry;
221 struct nvmet_subsys *subsys;
222};
223
224struct nvmet_req;
225struct nvmet_fabrics_ops {
226 struct module *owner;
227 unsigned int type;
228 unsigned int sqe_inline_size;
229 unsigned int msdbd;
230 bool has_keyed_sgls : 1;
231 void (*queue_response)(struct nvmet_req *req);
232 int (*add_port)(struct nvmet_port *port);
233 void (*remove_port)(struct nvmet_port *port);
234 void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
235 void (*disc_traddr)(struct nvmet_req *req,
236 struct nvmet_port *port, char *traddr);
237};
238
239#define NVMET_MAX_INLINE_BIOVEC 8
240
241struct nvmet_req {
242 struct nvme_command *cmd;
243 struct nvme_completion *rsp;
244 struct nvmet_sq *sq;
245 struct nvmet_cq *cq;
246 struct nvmet_ns *ns;
247 struct scatterlist *sg;
248 struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC];
249 union {
250 struct {
251 struct bio inline_bio;
252 } b;
253 struct {
254 bool mpool_alloc;
255 struct kiocb iocb;
256 struct bio_vec *bvec;
257 struct work_struct work;
258 } f;
259 };
260 int sg_cnt;
261 /* data length as parsed from the command: */
262 size_t data_len;
263 /* data length as parsed from the SGL descriptor: */
264 size_t transfer_len;
265
266 struct nvmet_port *port;
267
268 void (*execute)(struct nvmet_req *req);
269 const struct nvmet_fabrics_ops *ops;
270};
271
272static inline void nvmet_set_status(struct nvmet_req *req, u16 status)
273{
274 req->rsp->status = cpu_to_le16(status << 1);
275}
276
277static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
278{
279 req->rsp->result.u32 = cpu_to_le32(result);
280}
281
282/*
283 * NVMe command writes actually are DMA reads for us on the target side.
284 */
285static inline enum dma_data_direction
286nvmet_data_dir(struct nvmet_req *req)
287{
288 return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
289}
290
291struct nvmet_async_event {
292 struct list_head entry;
293 u8 event_type;
294 u8 event_info;
295 u8 log_page;
296};
297
298u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
299u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
300u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
301u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
302u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
303u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req);
304
305bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
306 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
307void nvmet_req_uninit(struct nvmet_req *req);
308void nvmet_req_execute(struct nvmet_req *req);
309void nvmet_req_complete(struct nvmet_req *req, u16 status);
310
311void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
312 u16 size);
313void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
314 u16 size);
315void nvmet_sq_destroy(struct nvmet_sq *sq);
316int nvmet_sq_init(struct nvmet_sq *sq);
317
318void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
319
320void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
321u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
322 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
323u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
324 struct nvmet_req *req, struct nvmet_ctrl **ret);
325void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
326u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd);
327
328struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
329 enum nvme_subsys_type type);
330void nvmet_subsys_put(struct nvmet_subsys *subsys);
331void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
332
333struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
334void nvmet_put_namespace(struct nvmet_ns *ns);
335int nvmet_ns_enable(struct nvmet_ns *ns);
336void nvmet_ns_disable(struct nvmet_ns *ns);
337struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
338void nvmet_ns_free(struct nvmet_ns *ns);
339
340int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
341void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
342
343int nvmet_enable_port(struct nvmet_port *port);
344void nvmet_disable_port(struct nvmet_port *port);
345
346void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
347void nvmet_referral_disable(struct nvmet_port *port);
348
349u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
350 size_t len);
351u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
352 size_t len);
353u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
354
355u32 nvmet_get_log_page_len(struct nvme_command *cmd);
356
357#define NVMET_QUEUE_SIZE 1024
358#define NVMET_NR_QUEUES 128
359#define NVMET_MAX_CMD NVMET_QUEUE_SIZE
360#define NVMET_KAS 10
361#define NVMET_DISC_KATO 120
362
363int __init nvmet_init_configfs(void);
364void __exit nvmet_exit_configfs(void);
365
366int __init nvmet_init_discovery(void);
367void nvmet_exit_discovery(void);
368
369extern struct nvmet_subsys *nvmet_disc_subsys;
370extern u64 nvmet_genctr;
371extern struct rw_semaphore nvmet_config_sem;
372
373bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
374 const char *hostnqn);
375
376int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
377int nvmet_file_ns_enable(struct nvmet_ns *ns);
378void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
379void nvmet_file_ns_disable(struct nvmet_ns *ns);
380
381static inline u32 nvmet_rw_len(struct nvmet_req *req)
382{
383 return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
384 req->ns->blksize_shift;
385}
386#endif /* _NVMET_H */