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