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