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