xref: /openbmc/linux/drivers/nvme/target/passthru.c (revision 255490f9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVMe Over Fabrics Target Passthrough command implementation.
4  *
5  * Copyright (c) 2017-2018 Western Digital Corporation or its
6  * affiliates.
7  * Copyright (c) 2019-2020, Eideticom Inc.
8  *
9  */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/module.h>
12 
13 #include "../host/nvme.h"
14 #include "nvmet.h"
15 
16 MODULE_IMPORT_NS(NVME_TARGET_PASSTHRU);
17 
18 /*
19  * xarray to maintain one passthru subsystem per nvme controller.
20  */
21 static DEFINE_XARRAY(passthru_subsystems);
22 
23 void nvmet_passthrough_override_cap(struct nvmet_ctrl *ctrl)
24 {
25 	/*
26 	 * Multiple command set support can only be declared if the underlying
27 	 * controller actually supports it.
28 	 */
29 	if (!nvme_multi_css(ctrl->subsys->passthru_ctrl))
30 		ctrl->cap &= ~(1ULL << 43);
31 }
32 
33 static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
34 {
35 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
36 	struct nvme_ctrl *pctrl = ctrl->subsys->passthru_ctrl;
37 	u16 status = NVME_SC_SUCCESS;
38 	struct nvme_id_ctrl *id;
39 	unsigned int max_hw_sectors;
40 	int page_shift;
41 
42 	id = kzalloc(sizeof(*id), GFP_KERNEL);
43 	if (!id)
44 		return NVME_SC_INTERNAL;
45 
46 	status = nvmet_copy_from_sgl(req, 0, id, sizeof(*id));
47 	if (status)
48 		goto out_free;
49 
50 	id->cntlid = cpu_to_le16(ctrl->cntlid);
51 	id->ver = cpu_to_le32(ctrl->subsys->ver);
52 
53 	/*
54 	 * The passthru NVMe driver may have a limit on the number of segments
55 	 * which depends on the host's memory fragementation. To solve this,
56 	 * ensure mdts is limited to the pages equal to the number of segments.
57 	 */
58 	max_hw_sectors = min_not_zero(pctrl->max_segments << (PAGE_SHIFT - 9),
59 				      pctrl->max_hw_sectors);
60 
61 	/*
62 	 * nvmet_passthru_map_sg is limitted to using a single bio so limit
63 	 * the mdts based on BIO_MAX_VECS as well
64 	 */
65 	max_hw_sectors = min_not_zero(BIO_MAX_VECS << (PAGE_SHIFT - 9),
66 				      max_hw_sectors);
67 
68 	page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12;
69 
70 	id->mdts = ilog2(max_hw_sectors) + 9 - page_shift;
71 
72 	id->acl = 3;
73 	/*
74 	 * We export aerl limit for the fabrics controller, update this when
75 	 * passthru based aerl support is added.
76 	 */
77 	id->aerl = NVMET_ASYNC_EVENTS - 1;
78 
79 	/* emulate kas as most of the PCIe ctrl don't have a support for kas */
80 	id->kas = cpu_to_le16(NVMET_KAS);
81 
82 	/* don't support host memory buffer */
83 	id->hmpre = 0;
84 	id->hmmin = 0;
85 
86 	id->sqes = min_t(__u8, ((0x6 << 4) | 0x6), id->sqes);
87 	id->cqes = min_t(__u8, ((0x4 << 4) | 0x4), id->cqes);
88 	id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
89 
90 	/* don't support fuse commands */
91 	id->fuses = 0;
92 
93 	id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
94 	if (ctrl->ops->flags & NVMF_KEYED_SGLS)
95 		id->sgls |= cpu_to_le32(1 << 2);
96 	if (req->port->inline_data_size)
97 		id->sgls |= cpu_to_le32(1 << 20);
98 
99 	/*
100 	 * When passthru controller is setup using nvme-loop transport it will
101 	 * export the passthru ctrl subsysnqn (PCIe NVMe ctrl) and will fail in
102 	 * the nvme/host/core.c in the nvme_init_subsystem()->nvme_active_ctrl()
103 	 * code path with duplicate ctr subsynqn. In order to prevent that we
104 	 * mask the passthru-ctrl subsysnqn with the target ctrl subsysnqn.
105 	 */
106 	memcpy(id->subnqn, ctrl->subsysnqn, sizeof(id->subnqn));
107 
108 	/* use fabric id-ctrl values */
109 	id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) +
110 				req->port->inline_data_size) / 16);
111 	id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
112 
113 	id->msdbd = ctrl->ops->msdbd;
114 
115 	/* Support multipath connections with fabrics */
116 	id->cmic |= 1 << 1;
117 
118 	/* Disable reservations, see nvmet_parse_passthru_io_cmd() */
119 	id->oncs &= cpu_to_le16(~NVME_CTRL_ONCS_RESERVATIONS);
120 
121 	status = nvmet_copy_to_sgl(req, 0, id, sizeof(struct nvme_id_ctrl));
122 
123 out_free:
124 	kfree(id);
125 	return status;
126 }
127 
128 static u16 nvmet_passthru_override_id_ns(struct nvmet_req *req)
129 {
130 	u16 status = NVME_SC_SUCCESS;
131 	struct nvme_id_ns *id;
132 	int i;
133 
134 	id = kzalloc(sizeof(*id), GFP_KERNEL);
135 	if (!id)
136 		return NVME_SC_INTERNAL;
137 
138 	status = nvmet_copy_from_sgl(req, 0, id, sizeof(struct nvme_id_ns));
139 	if (status)
140 		goto out_free;
141 
142 	for (i = 0; i < (id->nlbaf + 1); i++)
143 		if (id->lbaf[i].ms)
144 			memset(&id->lbaf[i], 0, sizeof(id->lbaf[i]));
145 
146 	id->flbas = id->flbas & ~(1 << 4);
147 
148 	/*
149 	 * Presently the NVMEof target code does not support sending
150 	 * metadata, so we must disable it here. This should be updated
151 	 * once target starts supporting metadata.
152 	 */
153 	id->mc = 0;
154 
155 	status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
156 
157 out_free:
158 	kfree(id);
159 	return status;
160 }
161 
162 static void nvmet_passthru_execute_cmd_work(struct work_struct *w)
163 {
164 	struct nvmet_req *req = container_of(w, struct nvmet_req, p.work);
165 	struct request *rq = req->p.rq;
166 	int status;
167 
168 	status = nvme_execute_passthru_rq(rq);
169 
170 	if (status == NVME_SC_SUCCESS &&
171 	    req->cmd->common.opcode == nvme_admin_identify) {
172 		switch (req->cmd->identify.cns) {
173 		case NVME_ID_CNS_CTRL:
174 			nvmet_passthru_override_id_ctrl(req);
175 			break;
176 		case NVME_ID_CNS_NS:
177 			nvmet_passthru_override_id_ns(req);
178 			break;
179 		}
180 	} else if (status < 0)
181 		status = NVME_SC_INTERNAL;
182 
183 	req->cqe->result = nvme_req(rq)->result;
184 	nvmet_req_complete(req, status);
185 	blk_mq_free_request(rq);
186 }
187 
188 static void nvmet_passthru_req_done(struct request *rq,
189 				    blk_status_t blk_status)
190 {
191 	struct nvmet_req *req = rq->end_io_data;
192 
193 	req->cqe->result = nvme_req(rq)->result;
194 	nvmet_req_complete(req, nvme_req(rq)->status);
195 	blk_mq_free_request(rq);
196 }
197 
198 static int nvmet_passthru_map_sg(struct nvmet_req *req, struct request *rq)
199 {
200 	struct scatterlist *sg;
201 	struct bio *bio;
202 	int i;
203 
204 	if (req->sg_cnt > BIO_MAX_VECS)
205 		return -EINVAL;
206 
207 	if (nvmet_use_inline_bvec(req)) {
208 		bio = &req->p.inline_bio;
209 		bio_init(bio, NULL, req->inline_bvec,
210 			 ARRAY_SIZE(req->inline_bvec), req_op(rq));
211 	} else {
212 		bio = bio_alloc(NULL, bio_max_segs(req->sg_cnt), req_op(rq),
213 				GFP_KERNEL);
214 		bio->bi_end_io = bio_put;
215 	}
216 
217 	for_each_sg(req->sg, sg, req->sg_cnt, i) {
218 		if (bio_add_pc_page(rq->q, bio, sg_page(sg), sg->length,
219 				    sg->offset) < sg->length) {
220 			nvmet_req_bio_put(req, bio);
221 			return -EINVAL;
222 		}
223 	}
224 
225 	blk_rq_bio_prep(rq, bio, req->sg_cnt);
226 
227 	return 0;
228 }
229 
230 static void nvmet_passthru_execute_cmd(struct nvmet_req *req)
231 {
232 	struct nvme_ctrl *ctrl = nvmet_req_subsys(req)->passthru_ctrl;
233 	struct request_queue *q = ctrl->admin_q;
234 	struct nvme_ns *ns = NULL;
235 	struct request *rq = NULL;
236 	unsigned int timeout;
237 	u32 effects;
238 	u16 status;
239 	int ret;
240 
241 	if (likely(req->sq->qid != 0)) {
242 		u32 nsid = le32_to_cpu(req->cmd->common.nsid);
243 
244 		ns = nvme_find_get_ns(ctrl, nsid);
245 		if (unlikely(!ns)) {
246 			pr_err("failed to get passthru ns nsid:%u\n", nsid);
247 			status = NVME_SC_INVALID_NS | NVME_SC_DNR;
248 			goto out;
249 		}
250 
251 		q = ns->queue;
252 		timeout = nvmet_req_subsys(req)->io_timeout;
253 	} else {
254 		timeout = nvmet_req_subsys(req)->admin_timeout;
255 	}
256 
257 	rq = blk_mq_alloc_request(q, nvme_req_op(req->cmd), 0);
258 	if (IS_ERR(rq)) {
259 		status = NVME_SC_INTERNAL;
260 		goto out_put_ns;
261 	}
262 	nvme_init_request(rq, req->cmd);
263 
264 	if (timeout)
265 		rq->timeout = timeout;
266 
267 	if (req->sg_cnt) {
268 		ret = nvmet_passthru_map_sg(req, rq);
269 		if (unlikely(ret)) {
270 			status = NVME_SC_INTERNAL;
271 			goto out_put_req;
272 		}
273 	}
274 
275 	/*
276 	 * If there are effects for the command we are about to execute, or
277 	 * an end_req function we need to use nvme_execute_passthru_rq()
278 	 * synchronously in a work item seeing the end_req function and
279 	 * nvme_passthru_end() can't be called in the request done callback
280 	 * which is typically in interrupt context.
281 	 */
282 	effects = nvme_command_effects(ctrl, ns, req->cmd->common.opcode);
283 	if (req->p.use_workqueue || effects) {
284 		INIT_WORK(&req->p.work, nvmet_passthru_execute_cmd_work);
285 		req->p.rq = rq;
286 		queue_work(nvmet_wq, &req->p.work);
287 	} else {
288 		rq->end_io = nvmet_passthru_req_done;
289 		rq->end_io_data = req;
290 		blk_execute_rq_nowait(rq, false);
291 	}
292 
293 	if (ns)
294 		nvme_put_ns(ns);
295 
296 	return;
297 
298 out_put_req:
299 	blk_mq_free_request(rq);
300 out_put_ns:
301 	if (ns)
302 		nvme_put_ns(ns);
303 out:
304 	nvmet_req_complete(req, status);
305 }
306 
307 /*
308  * We need to emulate set host behaviour to ensure that any requested
309  * behaviour of the target's host matches the requested behaviour
310  * of the device's host and fail otherwise.
311  */
312 static void nvmet_passthru_set_host_behaviour(struct nvmet_req *req)
313 {
314 	struct nvme_ctrl *ctrl = nvmet_req_subsys(req)->passthru_ctrl;
315 	struct nvme_feat_host_behavior *host;
316 	u16 status = NVME_SC_INTERNAL;
317 	int ret;
318 
319 	host = kzalloc(sizeof(*host) * 2, GFP_KERNEL);
320 	if (!host)
321 		goto out_complete_req;
322 
323 	ret = nvme_get_features(ctrl, NVME_FEAT_HOST_BEHAVIOR, 0,
324 				host, sizeof(*host), NULL);
325 	if (ret)
326 		goto out_free_host;
327 
328 	status = nvmet_copy_from_sgl(req, 0, &host[1], sizeof(*host));
329 	if (status)
330 		goto out_free_host;
331 
332 	if (memcmp(&host[0], &host[1], sizeof(host[0]))) {
333 		pr_warn("target host has requested different behaviour from the local host\n");
334 		status = NVME_SC_INTERNAL;
335 	}
336 
337 out_free_host:
338 	kfree(host);
339 out_complete_req:
340 	nvmet_req_complete(req, status);
341 }
342 
343 static u16 nvmet_setup_passthru_command(struct nvmet_req *req)
344 {
345 	req->p.use_workqueue = false;
346 	req->execute = nvmet_passthru_execute_cmd;
347 	return NVME_SC_SUCCESS;
348 }
349 
350 u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req)
351 {
352 	/* Reject any commands with non-sgl flags set (ie. fused commands) */
353 	if (req->cmd->common.flags & ~NVME_CMD_SGL_ALL)
354 		return NVME_SC_INVALID_FIELD;
355 
356 	switch (req->cmd->common.opcode) {
357 	case nvme_cmd_resv_register:
358 	case nvme_cmd_resv_report:
359 	case nvme_cmd_resv_acquire:
360 	case nvme_cmd_resv_release:
361 		/*
362 		 * Reservations cannot be supported properly because the
363 		 * underlying device has no way of differentiating different
364 		 * hosts that connect via fabrics. This could potentially be
365 		 * emulated in the future if regular targets grow support for
366 		 * this feature.
367 		 */
368 		return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
369 	}
370 
371 	return nvmet_setup_passthru_command(req);
372 }
373 
374 /*
375  * Only features that are emulated or specifically allowed in the list  are
376  * passed down to the controller. This function implements the allow list for
377  * both get and set features.
378  */
379 static u16 nvmet_passthru_get_set_features(struct nvmet_req *req)
380 {
381 	switch (le32_to_cpu(req->cmd->features.fid)) {
382 	case NVME_FEAT_ARBITRATION:
383 	case NVME_FEAT_POWER_MGMT:
384 	case NVME_FEAT_LBA_RANGE:
385 	case NVME_FEAT_TEMP_THRESH:
386 	case NVME_FEAT_ERR_RECOVERY:
387 	case NVME_FEAT_VOLATILE_WC:
388 	case NVME_FEAT_WRITE_ATOMIC:
389 	case NVME_FEAT_AUTO_PST:
390 	case NVME_FEAT_TIMESTAMP:
391 	case NVME_FEAT_HCTM:
392 	case NVME_FEAT_NOPSC:
393 	case NVME_FEAT_RRL:
394 	case NVME_FEAT_PLM_CONFIG:
395 	case NVME_FEAT_PLM_WINDOW:
396 	case NVME_FEAT_HOST_BEHAVIOR:
397 	case NVME_FEAT_SANITIZE:
398 	case NVME_FEAT_VENDOR_START ... NVME_FEAT_VENDOR_END:
399 		return nvmet_setup_passthru_command(req);
400 
401 	case NVME_FEAT_ASYNC_EVENT:
402 		/* There is no support for forwarding ASYNC events */
403 	case NVME_FEAT_IRQ_COALESCE:
404 	case NVME_FEAT_IRQ_CONFIG:
405 		/* The IRQ settings will not apply to the target controller */
406 	case NVME_FEAT_HOST_MEM_BUF:
407 		/*
408 		 * Any HMB that's set will not be passed through and will
409 		 * not work as expected
410 		 */
411 	case NVME_FEAT_SW_PROGRESS:
412 		/*
413 		 * The Pre-Boot Software Load Count doesn't make much
414 		 * sense for a target to export
415 		 */
416 	case NVME_FEAT_RESV_MASK:
417 	case NVME_FEAT_RESV_PERSIST:
418 		/* No reservations, see nvmet_parse_passthru_io_cmd() */
419 	default:
420 		return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
421 	}
422 }
423 
424 u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req)
425 {
426 	/* Reject any commands with non-sgl flags set (ie. fused commands) */
427 	if (req->cmd->common.flags & ~NVME_CMD_SGL_ALL)
428 		return NVME_SC_INVALID_FIELD;
429 
430 	/*
431 	 * Passthru all vendor specific commands
432 	 */
433 	if (req->cmd->common.opcode >= nvme_admin_vendor_start)
434 		return nvmet_setup_passthru_command(req);
435 
436 	switch (req->cmd->common.opcode) {
437 	case nvme_admin_async_event:
438 		req->execute = nvmet_execute_async_event;
439 		return NVME_SC_SUCCESS;
440 	case nvme_admin_keep_alive:
441 		/*
442 		 * Most PCIe ctrls don't support keep alive cmd, we route keep
443 		 * alive to the non-passthru mode. In future please change this
444 		 * code when PCIe ctrls with keep alive support available.
445 		 */
446 		req->execute = nvmet_execute_keep_alive;
447 		return NVME_SC_SUCCESS;
448 	case nvme_admin_set_features:
449 		switch (le32_to_cpu(req->cmd->features.fid)) {
450 		case NVME_FEAT_ASYNC_EVENT:
451 		case NVME_FEAT_KATO:
452 		case NVME_FEAT_NUM_QUEUES:
453 		case NVME_FEAT_HOST_ID:
454 			req->execute = nvmet_execute_set_features;
455 			return NVME_SC_SUCCESS;
456 		case NVME_FEAT_HOST_BEHAVIOR:
457 			req->execute = nvmet_passthru_set_host_behaviour;
458 			return NVME_SC_SUCCESS;
459 		default:
460 			return nvmet_passthru_get_set_features(req);
461 		}
462 		break;
463 	case nvme_admin_get_features:
464 		switch (le32_to_cpu(req->cmd->features.fid)) {
465 		case NVME_FEAT_ASYNC_EVENT:
466 		case NVME_FEAT_KATO:
467 		case NVME_FEAT_NUM_QUEUES:
468 		case NVME_FEAT_HOST_ID:
469 			req->execute = nvmet_execute_get_features;
470 			return NVME_SC_SUCCESS;
471 		default:
472 			return nvmet_passthru_get_set_features(req);
473 		}
474 		break;
475 	case nvme_admin_identify:
476 		switch (req->cmd->identify.cns) {
477 		case NVME_ID_CNS_CTRL:
478 			req->execute = nvmet_passthru_execute_cmd;
479 			req->p.use_workqueue = true;
480 			return NVME_SC_SUCCESS;
481 		case NVME_ID_CNS_CS_CTRL:
482 			switch (req->cmd->identify.csi) {
483 			case NVME_CSI_ZNS:
484 				req->execute = nvmet_passthru_execute_cmd;
485 				req->p.use_workqueue = true;
486 				return NVME_SC_SUCCESS;
487 			}
488 			return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
489 		case NVME_ID_CNS_NS:
490 			req->execute = nvmet_passthru_execute_cmd;
491 			req->p.use_workqueue = true;
492 			return NVME_SC_SUCCESS;
493 		case NVME_ID_CNS_CS_NS:
494 			switch (req->cmd->identify.csi) {
495 			case NVME_CSI_ZNS:
496 				req->execute = nvmet_passthru_execute_cmd;
497 				req->p.use_workqueue = true;
498 				return NVME_SC_SUCCESS;
499 			}
500 			return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
501 		default:
502 			return nvmet_setup_passthru_command(req);
503 		}
504 	case nvme_admin_get_log_page:
505 		return nvmet_setup_passthru_command(req);
506 	default:
507 		/* Reject commands not in the allowlist above */
508 		return nvmet_report_invalid_opcode(req);
509 	}
510 }
511 
512 int nvmet_passthru_ctrl_enable(struct nvmet_subsys *subsys)
513 {
514 	struct nvme_ctrl *ctrl;
515 	struct file *file;
516 	int ret = -EINVAL;
517 	void *old;
518 
519 	mutex_lock(&subsys->lock);
520 	if (!subsys->passthru_ctrl_path)
521 		goto out_unlock;
522 	if (subsys->passthru_ctrl)
523 		goto out_unlock;
524 
525 	if (subsys->nr_namespaces) {
526 		pr_info("cannot enable both passthru and regular namespaces for a single subsystem");
527 		goto out_unlock;
528 	}
529 
530 	file = filp_open(subsys->passthru_ctrl_path, O_RDWR, 0);
531 	if (IS_ERR(file)) {
532 		ret = PTR_ERR(file);
533 		goto out_unlock;
534 	}
535 
536 	ctrl = nvme_ctrl_from_file(file);
537 	if (!ctrl) {
538 		pr_err("failed to open nvme controller %s\n",
539 		       subsys->passthru_ctrl_path);
540 
541 		goto out_put_file;
542 	}
543 
544 	old = xa_cmpxchg(&passthru_subsystems, ctrl->cntlid, NULL,
545 			 subsys, GFP_KERNEL);
546 	if (xa_is_err(old)) {
547 		ret = xa_err(old);
548 		goto out_put_file;
549 	}
550 
551 	if (old)
552 		goto out_put_file;
553 
554 	subsys->passthru_ctrl = ctrl;
555 	subsys->ver = ctrl->vs;
556 
557 	if (subsys->ver < NVME_VS(1, 2, 1)) {
558 		pr_warn("nvme controller version is too old: %llu.%llu.%llu, advertising 1.2.1\n",
559 			NVME_MAJOR(subsys->ver), NVME_MINOR(subsys->ver),
560 			NVME_TERTIARY(subsys->ver));
561 		subsys->ver = NVME_VS(1, 2, 1);
562 	}
563 	nvme_get_ctrl(ctrl);
564 	__module_get(subsys->passthru_ctrl->ops->module);
565 	ret = 0;
566 
567 out_put_file:
568 	filp_close(file, NULL);
569 out_unlock:
570 	mutex_unlock(&subsys->lock);
571 	return ret;
572 }
573 
574 static void __nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
575 {
576 	if (subsys->passthru_ctrl) {
577 		xa_erase(&passthru_subsystems, subsys->passthru_ctrl->cntlid);
578 		module_put(subsys->passthru_ctrl->ops->module);
579 		nvme_put_ctrl(subsys->passthru_ctrl);
580 	}
581 	subsys->passthru_ctrl = NULL;
582 	subsys->ver = NVMET_DEFAULT_VS;
583 }
584 
585 void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
586 {
587 	mutex_lock(&subsys->lock);
588 	__nvmet_passthru_ctrl_disable(subsys);
589 	mutex_unlock(&subsys->lock);
590 }
591 
592 void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys)
593 {
594 	mutex_lock(&subsys->lock);
595 	__nvmet_passthru_ctrl_disable(subsys);
596 	mutex_unlock(&subsys->lock);
597 	kfree(subsys->passthru_ctrl_path);
598 }
599