blob: 480dfe10fad943e269449111f2372e8a876a0f95 [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
Christoph Hellwigc86b8f72018-05-30 15:04:47 +020033
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
Christoph Hellwiga07b4972016-06-21 18:04:20 +020048/* 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;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040061 struct file *file;
Christoph Hellwiga07b4972016-06-21 18:04:20 +020062 u32 nsid;
63 u32 blksize_shift;
64 loff_t size;
65 u8 nguid[16];
Johannes Thumshirn637dc0f2017-06-07 11:45:32 +020066 uuid_t uuid;
Christoph Hellwiga07b4972016-06-21 18:04:20 +020067
Solganik Alexandere4fcf072016-10-30 10:35:15 +020068 bool enabled;
Christoph Hellwiga07b4972016-06-21 18:04:20 +020069 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;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040076 mempool_t *bvec_pool;
77 struct kmem_cache *bvec_cache;
Christoph Hellwiga07b4972016-06-21 18:04:20 +020078};
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
Christoph Hellwiga07b4972016-06-21 18:04:20 +020085struct 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;
James Smartf9cf2a62017-10-18 14:33:59 -070095 u32 sqhd;
Christoph Hellwiga07b4972016-06-21 18:04:20 +020096 struct completion free_done;
Sagi Grimberg427242c2017-03-06 18:46:20 +020097 struct completion confirm_done;
Christoph Hellwiga07b4972016-06-21 18:04:20 +020098};
99
100/**
101 * struct nvmet_port - Common structure to keep port
102 * information for the target.
Christoph Hellwigfe4a9792018-05-26 14:11:25 +0200103 * @entry: Entry into referrals or transport list.
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200104 * @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;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200134 u32 cc;
135 u32 csts;
136
Omri Mann28dd5cf2017-08-30 15:22:59 +0300137 uuid_t hostid;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200138 u16 cntlid;
139 u32 kato;
140
Christoph Hellwigc86b8f72018-05-30 15:04:47 +0200141 u32 aen_enabled;
Christoph Hellwig55fdd6b2018-05-30 15:05:09 +0200142 unsigned long aen_masked;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200143 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
Christoph Hellwige929f062018-03-20 20:41:35 +0100153 const struct nvmet_fabrics_ops *ops;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200154
Christoph Hellwigc16734e2018-05-25 17:16:09 +0200155 __le32 *changed_ns_list;
156 u32 nr_changed_ns;
157
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200158 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;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200172
173 struct list_head hosts;
174 bool allow_any_host;
175
176 u16 max_qid;
177
178 u64 ver;
Johannes Thumshirn2e7f5d22017-07-14 15:36:55 +0200179 u64 serial;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200180 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);
Sagi Grimberg4c652682018-01-24 20:27:10 +0200235 void (*disc_traddr)(struct nvmet_req *req,
236 struct nvmet_port *port, char *traddr);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200237};
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;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200248 struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC];
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400249 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 };
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200260 int sg_cnt;
Christoph Hellwig5e62d5c2017-11-09 14:29:58 +0100261 /* data length as parsed from the command: */
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200262 size_t data_len;
Christoph Hellwig5e62d5c2017-11-09 14:29:58 +0100263 /* data length as parsed from the SGL descriptor: */
264 size_t transfer_len;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200265
266 struct nvmet_port *port;
267
268 void (*execute)(struct nvmet_req *req);
Christoph Hellwige929f062018-03-20 20:41:35 +0100269 const struct nvmet_fabrics_ops *ops;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200270};
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{
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800279 req->rsp->result.u32 = cpu_to_le32(result);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200280}
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
Parav Pandit64a0ca82017-02-27 23:21:33 -0600298u16 nvmet_parse_connect_cmd(struct nvmet_req *req);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400299u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req);
300u16 nvmet_file_parse_io_cmd(struct nvmet_req *req);
Parav Pandit64a0ca82017-02-27 23:21:33 -0600301u16 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);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200304
305bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
Christoph Hellwige929f062018-03-20 20:41:35 +0100306 struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops);
Vijay Immanuel549f01a2017-05-08 16:38:35 -0700307void nvmet_req_uninit(struct nvmet_req *req);
Christoph Hellwig5e62d5c2017-11-09 14:29:58 +0100308void nvmet_req_execute(struct nvmet_req *req);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200309void 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);
Parav Pandit64a0ca82017-02-27 23:21:33 -0600326u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200327
328struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
329 enum nvme_subsys_type type);
330void nvmet_subsys_put(struct nvmet_subsys *subsys);
Sagi Grimberg344770b2016-11-27 22:29:17 +0200331void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200332
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
Christoph Hellwige929f062018-03-20 20:41:35 +0100340int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
341void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200342
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);
Christoph Hellwigc7759ff2018-05-22 11:10:02 +0200353u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200354
355u32 nvmet_get_log_page_len(struct nvme_command *cmd);
356
357#define NVMET_QUEUE_SIZE 1024
James Smart3375e292017-09-11 16:14:50 -0700358#define NVMET_NR_QUEUES 128
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200359#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
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400376int 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}
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200386#endif /* _NVMET_H */