nvmet: Cleanup discovery execute handlers
[linux-2.6-block.git] / drivers / nvme / target / discovery.c
CommitLineData
77141dc6 1// SPDX-License-Identifier: GPL-2.0
a07b4970
CH
2/*
3 * Discovery service for the NVMe over Fabrics target.
4 * Copyright (C) 2016 Intel Corporation. All rights reserved.
a07b4970
CH
5 */
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7#include <linux/slab.h>
8#include <generated/utsrelease.h>
9#include "nvmet.h"
10
11struct nvmet_subsys *nvmet_disc_subsys;
12
03198c4d 13static u64 nvmet_genctr;
a07b4970 14
b662a078
JS
15static void __nvmet_disc_changed(struct nvmet_port *port,
16 struct nvmet_ctrl *ctrl)
17{
18 if (ctrl->port != port)
19 return;
20
21 if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_DISC_CHANGE))
22 return;
23
24 nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
25 NVME_AER_NOTICE_DISC_CHANGED, NVME_LOG_DISC);
26}
27
28void nvmet_port_disc_changed(struct nvmet_port *port,
29 struct nvmet_subsys *subsys)
30{
31 struct nvmet_ctrl *ctrl;
32
6f53e73b 33 lockdep_assert_held(&nvmet_config_sem);
b662a078
JS
34 nvmet_genctr++;
35
6f53e73b 36 mutex_lock(&nvmet_disc_subsys->lock);
b662a078
JS
37 list_for_each_entry(ctrl, &nvmet_disc_subsys->ctrls, subsys_entry) {
38 if (subsys && !nvmet_host_allowed(subsys, ctrl->hostnqn))
39 continue;
40
41 __nvmet_disc_changed(port, ctrl);
42 }
6f53e73b 43 mutex_unlock(&nvmet_disc_subsys->lock);
9d09dd8d
JS
44
45 /* If transport can signal change, notify transport */
46 if (port->tr_ops && port->tr_ops->discovery_chg)
47 port->tr_ops->discovery_chg(port);
b662a078
JS
48}
49
50static void __nvmet_subsys_disc_changed(struct nvmet_port *port,
51 struct nvmet_subsys *subsys,
52 struct nvmet_host *host)
53{
54 struct nvmet_ctrl *ctrl;
55
6f53e73b 56 mutex_lock(&nvmet_disc_subsys->lock);
b662a078
JS
57 list_for_each_entry(ctrl, &nvmet_disc_subsys->ctrls, subsys_entry) {
58 if (host && strcmp(nvmet_host_name(host), ctrl->hostnqn))
59 continue;
60
61 __nvmet_disc_changed(port, ctrl);
62 }
6f53e73b 63 mutex_unlock(&nvmet_disc_subsys->lock);
b662a078
JS
64}
65
66void nvmet_subsys_disc_changed(struct nvmet_subsys *subsys,
67 struct nvmet_host *host)
68{
69 struct nvmet_port *port;
70 struct nvmet_subsys_link *s;
71
72 nvmet_genctr++;
73
74 list_for_each_entry(port, nvmet_ports, global_entry)
75 list_for_each_entry(s, &port->subsystems, entry) {
76 if (s->subsys != subsys)
77 continue;
78 __nvmet_subsys_disc_changed(port, subsys, host);
79 }
80}
81
a07b4970
CH
82void nvmet_referral_enable(struct nvmet_port *parent, struct nvmet_port *port)
83{
84 down_write(&nvmet_config_sem);
85 if (list_empty(&port->entry)) {
86 list_add_tail(&port->entry, &parent->referrals);
87 port->enabled = true;
b662a078 88 nvmet_port_disc_changed(parent, NULL);
a07b4970
CH
89 }
90 up_write(&nvmet_config_sem);
91}
92
b662a078 93void nvmet_referral_disable(struct nvmet_port *parent, struct nvmet_port *port)
a07b4970
CH
94{
95 down_write(&nvmet_config_sem);
96 if (!list_empty(&port->entry)) {
97 port->enabled = false;
98 list_del_init(&port->entry);
b662a078 99 nvmet_port_disc_changed(parent, NULL);
a07b4970
CH
100 }
101 up_write(&nvmet_config_sem);
102}
103
104static void nvmet_format_discovery_entry(struct nvmf_disc_rsp_page_hdr *hdr,
4c652685
SG
105 struct nvmet_port *port, char *subsys_nqn, char *traddr,
106 u8 type, u32 numrec)
a07b4970
CH
107{
108 struct nvmf_disc_rsp_page_entry *e = &hdr->entries[numrec];
109
110 e->trtype = port->disc_addr.trtype;
111 e->adrfam = port->disc_addr.adrfam;
112 e->treq = port->disc_addr.treq;
113 e->portid = port->disc_addr.portid;
114 /* we support only dynamic controllers */
115 e->cntlid = cpu_to_le16(NVME_CNTLID_DYNAMIC);
7aa1f427 116 e->asqsz = cpu_to_le16(NVME_AQ_DEPTH);
a446c084 117 e->subtype = type;
a07b4970 118 memcpy(e->trsvcid, port->disc_addr.trsvcid, NVMF_TRSVCID_SIZE);
4c652685 119 memcpy(e->traddr, traddr, NVMF_TRADDR_SIZE);
a07b4970 120 memcpy(e->tsas.common, port->disc_addr.tsas.common, NVMF_TSAS_SIZE);
6038aa53 121 strncpy(e->subnqn, subsys_nqn, NVMF_NQN_SIZE);
a07b4970
CH
122}
123
4c652685
SG
124/*
125 * nvmet_set_disc_traddr - set a correct discovery log entry traddr
126 *
127 * IP based transports (e.g RDMA) can listen on "any" ipv4/ipv6 addresses
128 * (INADDR_ANY or IN6ADDR_ANY_INIT). The discovery log page traddr reply
129 * must not contain that "any" IP address. If the transport implements
130 * .disc_traddr, use it. this callback will set the discovery traddr
131 * from the req->port address in case the port in question listens
132 * "any" IP address.
133 */
134static void nvmet_set_disc_traddr(struct nvmet_req *req, struct nvmet_port *port,
135 char *traddr)
136{
137 if (req->ops->disc_traddr)
138 req->ops->disc_traddr(req, port, traddr);
139 else
140 memcpy(traddr, port->disc_addr.traddr, NVMF_TRADDR_SIZE);
141}
142
d808b7f7
KB
143static size_t discovery_log_entries(struct nvmet_req *req)
144{
145 struct nvmet_ctrl *ctrl = req->sq->ctrl;
146 struct nvmet_subsys_link *p;
147 struct nvmet_port *r;
148 size_t entries = 0;
149
150 list_for_each_entry(p, &req->port->subsystems, entry) {
151 if (!nvmet_host_allowed(p->subsys, ctrl->hostnqn))
152 continue;
153 entries++;
154 }
155 list_for_each_entry(r, &req->port->referrals, entry)
156 entries++;
157 return entries;
158}
159
6f86f2c9 160static void nvmet_execute_disc_get_log_page(struct nvmet_req *req)
a07b4970
CH
161{
162 const int entry_size = sizeof(struct nvmf_disc_rsp_page_entry);
163 struct nvmet_ctrl *ctrl = req->sq->ctrl;
164 struct nvmf_disc_rsp_page_hdr *hdr;
d808b7f7 165 u64 offset = nvmet_get_log_page_offset(req->cmd);
a07b4970 166 size_t data_len = nvmet_get_log_page_len(req->cmd);
d808b7f7 167 size_t alloc_len;
a07b4970
CH
168 struct nvmet_subsys_link *p;
169 struct nvmet_port *r;
170 u32 numrec = 0;
171 u16 status = 0;
d808b7f7
KB
172 void *buffer;
173
6f86f2c9
CH
174 if (req->cmd->get_log_page.lid != NVME_LOG_DISC) {
175 req->error_loc =
176 offsetof(struct nvme_get_log_page_command, lid);
177 status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
178 goto out;
179 }
180
d808b7f7
KB
181 /* Spec requires dword aligned offsets */
182 if (offset & 0x3) {
183 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
184 goto out;
185 }
a07b4970
CH
186
187 /*
188 * Make sure we're passing at least a buffer of response header size.
189 * If host provided data len is less than the header size, only the
190 * number of bytes requested by host will be sent to host.
191 */
d808b7f7
KB
192 down_read(&nvmet_config_sem);
193 alloc_len = sizeof(*hdr) + entry_size * discovery_log_entries(req);
194 buffer = kzalloc(alloc_len, GFP_KERNEL);
195 if (!buffer) {
196 up_read(&nvmet_config_sem);
a07b4970
CH
197 status = NVME_SC_INTERNAL;
198 goto out;
199 }
200
d808b7f7 201 hdr = buffer;
a07b4970 202 list_for_each_entry(p, &req->port->subsystems, entry) {
d808b7f7
KB
203 char traddr[NVMF_TRADDR_SIZE];
204
253928ee 205 if (!nvmet_host_allowed(p->subsys, ctrl->hostnqn))
a07b4970 206 continue;
d808b7f7
KB
207
208 nvmet_set_disc_traddr(req, req->port, traddr);
209 nvmet_format_discovery_entry(hdr, req->port,
210 p->subsys->subsysnqn, traddr,
211 NVME_NQN_NVME, numrec);
a07b4970
CH
212 numrec++;
213 }
214
215 list_for_each_entry(r, &req->port->referrals, entry) {
d808b7f7
KB
216 nvmet_format_discovery_entry(hdr, r,
217 NVME_DISC_SUBSYS_NAME,
218 r->disc_addr.traddr,
219 NVME_NQN_DISC, numrec);
a07b4970
CH
220 numrec++;
221 }
222
223 hdr->genctr = cpu_to_le64(nvmet_genctr);
224 hdr->numrec = cpu_to_le64(numrec);
225 hdr->recfmt = cpu_to_le16(0);
226
b662a078
JS
227 nvmet_clear_aen_bit(req, NVME_AEN_BIT_DISC_CHANGE);
228
a07b4970
CH
229 up_read(&nvmet_config_sem);
230
d808b7f7
KB
231 status = nvmet_copy_to_sgl(req, 0, buffer + offset, data_len);
232 kfree(buffer);
a07b4970
CH
233out:
234 nvmet_req_complete(req, status);
235}
236
6f86f2c9 237static void nvmet_execute_disc_identify(struct nvmet_req *req)
a07b4970
CH
238{
239 struct nvmet_ctrl *ctrl = req->sq->ctrl;
240 struct nvme_id_ctrl *id;
241 u16 status = 0;
242
6f86f2c9
CH
243 if (req->cmd->identify.cns != NVME_ID_CNS_CTRL) {
244 req->error_loc = offsetof(struct nvme_identify, cns);
245 status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
246 goto out;
247 }
248
a07b4970
CH
249 id = kzalloc(sizeof(*id), GFP_KERNEL);
250 if (!id) {
251 status = NVME_SC_INTERNAL;
252 goto out;
253 }
254
255 memset(id->fr, ' ', sizeof(id->fr));
256 strncpy((char *)id->fr, UTS_RELEASE, sizeof(id->fr));
257
258 /* no limit on data transfer sizes for now */
259 id->mdts = 0;
260 id->cntlid = cpu_to_le16(ctrl->cntlid);
261 id->ver = cpu_to_le32(ctrl->subsys->ver);
262 id->lpa = (1 << 2);
263
264 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
265 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
266
267 id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
268 if (ctrl->ops->has_keyed_sgls)
269 id->sgls |= cpu_to_le32(1 << 2);
0d5ee2b2 270 if (req->port->inline_data_size)
a07b4970
CH
271 id->sgls |= cpu_to_le32(1 << 20);
272
b662a078
JS
273 id->oaes = cpu_to_le32(NVMET_DISC_AEN_CFG_OPTIONAL);
274
5eadc9cc 275 strlcpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn));
a07b4970
CH
276
277 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
278
279 kfree(id);
280out:
281 nvmet_req_complete(req, status);
282}
283
6a8ec0ac
JS
284static void nvmet_execute_disc_set_features(struct nvmet_req *req)
285{
b7c8f366 286 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
6a8ec0ac
JS
287 u16 stat;
288
289 switch (cdw10 & 0xff) {
290 case NVME_FEAT_KATO:
291 stat = nvmet_set_feat_kato(req);
292 break;
293 case NVME_FEAT_ASYNC_EVENT:
294 stat = nvmet_set_feat_async_event(req,
295 NVMET_DISC_AEN_CFG_OPTIONAL);
296 break;
297 default:
84faf42b
CK
298 req->error_loc =
299 offsetof(struct nvme_common_command, cdw10);
6a8ec0ac
JS
300 stat = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
301 break;
302 }
303
304 nvmet_req_complete(req, stat);
305}
306
307static void nvmet_execute_disc_get_features(struct nvmet_req *req)
308{
b7c8f366 309 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
6a8ec0ac
JS
310 u16 stat = 0;
311
312 switch (cdw10 & 0xff) {
313 case NVME_FEAT_KATO:
314 nvmet_get_feat_kato(req);
315 break;
316 case NVME_FEAT_ASYNC_EVENT:
317 nvmet_get_feat_async_event(req);
318 break;
319 default:
84faf42b
CK
320 req->error_loc =
321 offsetof(struct nvme_common_command, cdw10);
6a8ec0ac
JS
322 stat = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
323 break;
324 }
325
326 nvmet_req_complete(req, stat);
327}
328
64a0ca88 329u16 nvmet_parse_discovery_cmd(struct nvmet_req *req)
a07b4970
CH
330{
331 struct nvme_command *cmd = req->cmd;
332
a07b4970 333 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
4151dd9a
PP
334 pr_err("got cmd %d while not ready\n",
335 cmd->common.opcode);
84faf42b
CK
336 req->error_loc =
337 offsetof(struct nvme_common_command, opcode);
a07b4970
CH
338 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
339 }
340
341 switch (cmd->common.opcode) {
6a8ec0ac
JS
342 case nvme_admin_set_features:
343 req->execute = nvmet_execute_disc_set_features;
344 req->data_len = 0;
345 return 0;
346 case nvme_admin_get_features:
347 req->execute = nvmet_execute_disc_get_features;
348 req->data_len = 0;
349 return 0;
350 case nvme_admin_async_event:
351 req->execute = nvmet_execute_async_event;
352 req->data_len = 0;
353 return 0;
f9362ac1
JS
354 case nvme_admin_keep_alive:
355 req->execute = nvmet_execute_keep_alive;
356 req->data_len = 0;
357 return 0;
a07b4970
CH
358 case nvme_admin_get_log_page:
359 req->data_len = nvmet_get_log_page_len(cmd);
6f86f2c9
CH
360 req->execute = nvmet_execute_disc_get_log_page;
361 return 0;
a07b4970 362 case nvme_admin_identify:
0add5e8e 363 req->data_len = NVME_IDENTIFY_DATA_SIZE;
6f86f2c9
CH
364 req->execute = nvmet_execute_disc_identify;
365 return 0;
a07b4970 366 default:
48440ab6 367 pr_err("unhandled cmd %d\n", cmd->common.opcode);
84faf42b 368 req->error_loc = offsetof(struct nvme_common_command, opcode);
a07b4970
CH
369 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
370 }
371
a07b4970
CH
372}
373
374int __init nvmet_init_discovery(void)
375{
376 nvmet_disc_subsys =
377 nvmet_subsys_alloc(NVME_DISC_SUBSYS_NAME, NVME_NQN_DISC);
1179d337 378 return PTR_ERR_OR_ZERO(nvmet_disc_subsys);
a07b4970
CH
379}
380
381void nvmet_exit_discovery(void)
382{
383 nvmet_subsys_put(nvmet_disc_subsys);
384}