Merge tag 's390-4.20-1' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[sfrench/cifs-2.6.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 /*
34  * Supported optional AENs:
35  */
36 #define NVMET_AEN_CFG_OPTIONAL \
37         (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE)
38
39 /*
40  * Plus mandatory SMART AENs (we'll never send them, but allow enabling them):
41  */
42 #define NVMET_AEN_CFG_ALL \
43         (NVME_SMART_CRIT_SPARE | NVME_SMART_CRIT_TEMPERATURE | \
44          NVME_SMART_CRIT_RELIABILITY | NVME_SMART_CRIT_MEDIA | \
45          NVME_SMART_CRIT_VOLATILE_MEMORY | NVMET_AEN_CFG_OPTIONAL)
46
47 /* Helper Macros when NVMe error is NVME_SC_CONNECT_INVALID_PARAM
48  * The 16 bit shift is to set IATTR bit to 1, which means offending
49  * offset starts in the data section of connect()
50  */
51 #define IPO_IATTR_CONNECT_DATA(x)       \
52         (cpu_to_le32((1 << 16) | (offsetof(struct nvmf_connect_data, x))))
53 #define IPO_IATTR_CONNECT_SQE(x)        \
54         (cpu_to_le32(offsetof(struct nvmf_connect_command, x)))
55
56 struct nvmet_ns {
57         struct list_head        dev_link;
58         struct percpu_ref       ref;
59         struct block_device     *bdev;
60         struct file             *file;
61         bool                    readonly;
62         u32                     nsid;
63         u32                     blksize_shift;
64         loff_t                  size;
65         u8                      nguid[16];
66         uuid_t                  uuid;
67         u32                     anagrpid;
68
69         bool                    buffered_io;
70         bool                    enabled;
71         struct nvmet_subsys     *subsys;
72         const char              *device_path;
73
74         struct config_group     device_group;
75         struct config_group     group;
76
77         struct completion       disable_done;
78         mempool_t               *bvec_pool;
79         struct kmem_cache       *bvec_cache;
80 };
81
82 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
83 {
84         return container_of(to_config_group(item), struct nvmet_ns, group);
85 }
86
87 struct nvmet_cq {
88         u16                     qid;
89         u16                     size;
90 };
91
92 struct nvmet_sq {
93         struct nvmet_ctrl       *ctrl;
94         struct percpu_ref       ref;
95         u16                     qid;
96         u16                     size;
97         u32                     sqhd;
98         struct completion       free_done;
99         struct completion       confirm_done;
100 };
101
102 struct nvmet_ana_group {
103         struct config_group     group;
104         struct nvmet_port       *port;
105         u32                     grpid;
106 };
107
108 static inline struct nvmet_ana_group *to_ana_group(struct config_item *item)
109 {
110         return container_of(to_config_group(item), struct nvmet_ana_group,
111                         group);
112 }
113
114 /**
115  * struct nvmet_port -  Common structure to keep port
116  *                              information for the target.
117  * @entry:              Entry into referrals or transport list.
118  * @disc_addr:          Address information is stored in a format defined
119  *                              for a discovery log page entry.
120  * @group:              ConfigFS group for this element's folder.
121  * @priv:               Private data for the transport.
122  */
123 struct nvmet_port {
124         struct list_head                entry;
125         struct nvmf_disc_rsp_page_entry disc_addr;
126         struct config_group             group;
127         struct config_group             subsys_group;
128         struct list_head                subsystems;
129         struct config_group             referrals_group;
130         struct list_head                referrals;
131         struct config_group             ana_groups_group;
132         struct nvmet_ana_group          ana_default_group;
133         enum nvme_ana_state             *ana_state;
134         void                            *priv;
135         bool                            enabled;
136         int                             inline_data_size;
137 };
138
139 static inline struct nvmet_port *to_nvmet_port(struct config_item *item)
140 {
141         return container_of(to_config_group(item), struct nvmet_port,
142                         group);
143 }
144
145 static inline struct nvmet_port *ana_groups_to_port(
146                 struct config_item *item)
147 {
148         return container_of(to_config_group(item), struct nvmet_port,
149                         ana_groups_group);
150 }
151
152 struct nvmet_ctrl {
153         struct nvmet_subsys     *subsys;
154         struct nvmet_cq         **cqs;
155         struct nvmet_sq         **sqs;
156
157         struct mutex            lock;
158         u64                     cap;
159         u32                     cc;
160         u32                     csts;
161
162         uuid_t                  hostid;
163         u16                     cntlid;
164         u32                     kato;
165
166         struct nvmet_port       *port;
167
168         u32                     aen_enabled;
169         unsigned long           aen_masked;
170         struct nvmet_req        *async_event_cmds[NVMET_ASYNC_EVENTS];
171         unsigned int            nr_async_event_cmds;
172         struct list_head        async_events;
173         struct work_struct      async_event_work;
174
175         struct list_head        subsys_entry;
176         struct kref             ref;
177         struct delayed_work     ka_work;
178         struct work_struct      fatal_err_work;
179
180         const struct nvmet_fabrics_ops *ops;
181
182         __le32                  *changed_ns_list;
183         u32                     nr_changed_ns;
184
185         char                    subsysnqn[NVMF_NQN_FIELD_LEN];
186         char                    hostnqn[NVMF_NQN_FIELD_LEN];
187 };
188
189 struct nvmet_subsys {
190         enum nvme_subsys_type   type;
191
192         struct mutex            lock;
193         struct kref             ref;
194
195         struct list_head        namespaces;
196         unsigned int            nr_namespaces;
197         unsigned int            max_nsid;
198
199         struct list_head        ctrls;
200
201         struct list_head        hosts;
202         bool                    allow_any_host;
203
204         u16                     max_qid;
205
206         u64                     ver;
207         u64                     serial;
208         char                    *subsysnqn;
209
210         struct config_group     group;
211
212         struct config_group     namespaces_group;
213         struct config_group     allowed_hosts_group;
214 };
215
216 static inline struct nvmet_subsys *to_subsys(struct config_item *item)
217 {
218         return container_of(to_config_group(item), struct nvmet_subsys, group);
219 }
220
221 static inline struct nvmet_subsys *namespaces_to_subsys(
222                 struct config_item *item)
223 {
224         return container_of(to_config_group(item), struct nvmet_subsys,
225                         namespaces_group);
226 }
227
228 struct nvmet_host {
229         struct config_group     group;
230 };
231
232 static inline struct nvmet_host *to_host(struct config_item *item)
233 {
234         return container_of(to_config_group(item), struct nvmet_host, group);
235 }
236
237 static inline char *nvmet_host_name(struct nvmet_host *host)
238 {
239         return config_item_name(&host->group.cg_item);
240 }
241
242 struct nvmet_host_link {
243         struct list_head        entry;
244         struct nvmet_host       *host;
245 };
246
247 struct nvmet_subsys_link {
248         struct list_head        entry;
249         struct nvmet_subsys     *subsys;
250 };
251
252 struct nvmet_req;
253 struct nvmet_fabrics_ops {
254         struct module *owner;
255         unsigned int type;
256         unsigned int msdbd;
257         bool has_keyed_sgls : 1;
258         void (*queue_response)(struct nvmet_req *req);
259         int (*add_port)(struct nvmet_port *port);
260         void (*remove_port)(struct nvmet_port *port);
261         void (*delete_ctrl)(struct nvmet_ctrl *ctrl);
262         void (*disc_traddr)(struct nvmet_req *req,
263                         struct nvmet_port *port, char *traddr);
264 };
265
266 #define NVMET_MAX_INLINE_BIOVEC 8
267 #define NVMET_MAX_INLINE_DATA_LEN NVMET_MAX_INLINE_BIOVEC * PAGE_SIZE
268
269 struct nvmet_req {
270         struct nvme_command     *cmd;
271         struct nvme_completion  *rsp;
272         struct nvmet_sq         *sq;
273         struct nvmet_cq         *cq;
274         struct nvmet_ns         *ns;
275         struct scatterlist      *sg;
276         struct bio_vec          inline_bvec[NVMET_MAX_INLINE_BIOVEC];
277         union {
278                 struct {
279                         struct bio      inline_bio;
280                 } b;
281                 struct {
282                         bool                    mpool_alloc;
283                         struct kiocb            iocb;
284                         struct bio_vec          *bvec;
285                         struct work_struct      work;
286                 } f;
287         };
288         int                     sg_cnt;
289         /* data length as parsed from the command: */
290         size_t                  data_len;
291         /* data length as parsed from the SGL descriptor: */
292         size_t                  transfer_len;
293
294         struct nvmet_port       *port;
295
296         void (*execute)(struct nvmet_req *req);
297         const struct nvmet_fabrics_ops *ops;
298 };
299
300 extern struct workqueue_struct *buffered_io_wq;
301
302 static inline void nvmet_set_status(struct nvmet_req *req, u16 status)
303 {
304         req->rsp->status = cpu_to_le16(status << 1);
305 }
306
307 static inline void nvmet_set_result(struct nvmet_req *req, u32 result)
308 {
309         req->rsp->result.u32 = cpu_to_le32(result);
310 }
311
312 /*
313  * NVMe command writes actually are DMA reads for us on the target side.
314  */
315 static inline enum dma_data_direction
316 nvmet_data_dir(struct nvmet_req *req)
317 {
318         return nvme_is_write(req->cmd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
319 }
320
321 struct nvmet_async_event {
322         struct list_head        entry;
323         u8                      event_type;
324         u8                      event_info;
325         u8                      log_page;
326 };
327
328 u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
329 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
330 u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
331 u16 nvmet_parse_admin_cmd(struct nvmet_req *req);
332 u16 nvmet_parse_discovery_cmd(struct nvmet_req *req);
333 u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req);
334
335 bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
336                 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
337 void nvmet_req_uninit(struct nvmet_req *req);
338 void nvmet_req_execute(struct nvmet_req *req);
339 void nvmet_req_complete(struct nvmet_req *req, u16 status);
340
341 void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
342                 u16 size);
343 void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, u16 qid,
344                 u16 size);
345 void nvmet_sq_destroy(struct nvmet_sq *sq);
346 int nvmet_sq_init(struct nvmet_sq *sq);
347
348 void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
349
350 void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
351 u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
352                 struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp);
353 u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
354                 struct nvmet_req *req, struct nvmet_ctrl **ret);
355 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
356 u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd);
357
358 struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
359                 enum nvme_subsys_type type);
360 void nvmet_subsys_put(struct nvmet_subsys *subsys);
361 void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
362
363 struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
364 void nvmet_put_namespace(struct nvmet_ns *ns);
365 int nvmet_ns_enable(struct nvmet_ns *ns);
366 void nvmet_ns_disable(struct nvmet_ns *ns);
367 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
368 void nvmet_ns_free(struct nvmet_ns *ns);
369
370 void nvmet_send_ana_event(struct nvmet_subsys *subsys,
371                 struct nvmet_port *port);
372 void nvmet_port_send_ana_event(struct nvmet_port *port);
373
374 int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
375 void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
376
377 int nvmet_enable_port(struct nvmet_port *port);
378 void nvmet_disable_port(struct nvmet_port *port);
379
380 void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port);
381 void nvmet_referral_disable(struct nvmet_port *port);
382
383 u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
384                 size_t len);
385 u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
386                 size_t len);
387 u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
388
389 u32 nvmet_get_log_page_len(struct nvme_command *cmd);
390
391 #define NVMET_QUEUE_SIZE        1024
392 #define NVMET_NR_QUEUES         128
393 #define NVMET_MAX_CMD           NVMET_QUEUE_SIZE
394
395 /*
396  * Nice round number that makes a list of nsids fit into a page.
397  * Should become tunable at some point in the future.
398  */
399 #define NVMET_MAX_NAMESPACES    1024
400
401 /*
402  * 0 is not a valid ANA group ID, so we start numbering at 1.
403  *
404  * ANA Group 1 exists without manual intervention, has namespaces assigned to it
405  * by default, and is available in an optimized state through all ports.
406  */
407 #define NVMET_MAX_ANAGRPS       128
408 #define NVMET_DEFAULT_ANA_GRPID 1
409
410 #define NVMET_KAS               10
411 #define NVMET_DISC_KATO         120
412
413 int __init nvmet_init_configfs(void);
414 void __exit nvmet_exit_configfs(void);
415
416 int __init nvmet_init_discovery(void);
417 void nvmet_exit_discovery(void);
418
419 extern struct nvmet_subsys *nvmet_disc_subsys;
420 extern u64 nvmet_genctr;
421 extern struct rw_semaphore nvmet_config_sem;
422
423 extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
424 extern u64 nvmet_ana_chgcnt;
425 extern struct rw_semaphore nvmet_ana_sem;
426
427 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
428                 const char *hostnqn);
429
430 int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
431 int nvmet_file_ns_enable(struct nvmet_ns *ns);
432 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
433 void nvmet_file_ns_disable(struct nvmet_ns *ns);
434 u16 nvmet_bdev_flush(struct nvmet_req *req);
435 u16 nvmet_file_flush(struct nvmet_req *req);
436 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
437
438 static inline u32 nvmet_rw_len(struct nvmet_req *req)
439 {
440         return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
441                         req->ns->blksize_shift;
442 }
443 #endif /* _NVMET_H */