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