xref: /openbmc/linux/drivers/nvme/target/passthru.c (revision 6c71a0574249f5e5a45fe055ab5f837023d5eeca)
1c1fef73fSLogan Gunthorpe // SPDX-License-Identifier: GPL-2.0
2c1fef73fSLogan Gunthorpe /*
3c1fef73fSLogan Gunthorpe  * NVMe Over Fabrics Target Passthrough command implementation.
4c1fef73fSLogan Gunthorpe  *
5c1fef73fSLogan Gunthorpe  * Copyright (c) 2017-2018 Western Digital Corporation or its
6c1fef73fSLogan Gunthorpe  * affiliates.
7c1fef73fSLogan Gunthorpe  * Copyright (c) 2019-2020, Eideticom Inc.
8c1fef73fSLogan Gunthorpe  *
9c1fef73fSLogan Gunthorpe  */
10c1fef73fSLogan Gunthorpe #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11c1fef73fSLogan Gunthorpe #include <linux/module.h>
12c1fef73fSLogan Gunthorpe 
13c1fef73fSLogan Gunthorpe #include "../host/nvme.h"
14c1fef73fSLogan Gunthorpe #include "nvmet.h"
15c1fef73fSLogan Gunthorpe 
16c1fef73fSLogan Gunthorpe MODULE_IMPORT_NS(NVME_TARGET_PASSTHRU);
17c1fef73fSLogan Gunthorpe 
18ba76af67SLogan Gunthorpe /*
19ba76af67SLogan Gunthorpe  * xarray to maintain one passthru subsystem per nvme controller.
20ba76af67SLogan Gunthorpe  */
21ba76af67SLogan Gunthorpe static DEFINE_XARRAY(passthru_subsystems);
22ba76af67SLogan Gunthorpe 
nvmet_passthrough_override_cap(struct nvmet_ctrl * ctrl)2377d651a6SAdam Manzanares void nvmet_passthrough_override_cap(struct nvmet_ctrl *ctrl)
2477d651a6SAdam Manzanares {
2577d651a6SAdam Manzanares 	/*
2677d651a6SAdam Manzanares 	 * Multiple command set support can only be declared if the underlying
2777d651a6SAdam Manzanares 	 * controller actually supports it.
2877d651a6SAdam Manzanares 	 */
2977d651a6SAdam Manzanares 	if (!nvme_multi_css(ctrl->subsys->passthru_ctrl))
3077d651a6SAdam Manzanares 		ctrl->cap &= ~(1ULL << 43);
3177d651a6SAdam Manzanares }
3277d651a6SAdam Manzanares 
nvmet_passthru_override_id_descs(struct nvmet_req * req)3334ad6151SAlan Adamson static u16 nvmet_passthru_override_id_descs(struct nvmet_req *req)
3434ad6151SAlan Adamson {
3534ad6151SAlan Adamson 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
3634ad6151SAlan Adamson 	u16 status = NVME_SC_SUCCESS;
3734ad6151SAlan Adamson 	int pos, len;
3834ad6151SAlan Adamson 	bool csi_seen = false;
3934ad6151SAlan Adamson 	void *data;
4034ad6151SAlan Adamson 	u8 csi;
4134ad6151SAlan Adamson 
4234ad6151SAlan Adamson 	if (!ctrl->subsys->clear_ids)
4334ad6151SAlan Adamson 		return status;
4434ad6151SAlan Adamson 
4534ad6151SAlan Adamson 	data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
4634ad6151SAlan Adamson 	if (!data)
4734ad6151SAlan Adamson 		return NVME_SC_INTERNAL;
4834ad6151SAlan Adamson 
4934ad6151SAlan Adamson 	status = nvmet_copy_from_sgl(req, 0, data, NVME_IDENTIFY_DATA_SIZE);
5034ad6151SAlan Adamson 	if (status)
5134ad6151SAlan Adamson 		goto out_free;
5234ad6151SAlan Adamson 
5334ad6151SAlan Adamson 	for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
5434ad6151SAlan Adamson 		struct nvme_ns_id_desc *cur = data + pos;
5534ad6151SAlan Adamson 
5634ad6151SAlan Adamson 		if (cur->nidl == 0)
5734ad6151SAlan Adamson 			break;
5834ad6151SAlan Adamson 		if (cur->nidt == NVME_NIDT_CSI) {
5934ad6151SAlan Adamson 			memcpy(&csi, cur + 1, NVME_NIDT_CSI_LEN);
6034ad6151SAlan Adamson 			csi_seen = true;
6134ad6151SAlan Adamson 			break;
6234ad6151SAlan Adamson 		}
6334ad6151SAlan Adamson 		len = sizeof(struct nvme_ns_id_desc) + cur->nidl;
6434ad6151SAlan Adamson 	}
6534ad6151SAlan Adamson 
6634ad6151SAlan Adamson 	memset(data, 0, NVME_IDENTIFY_DATA_SIZE);
6734ad6151SAlan Adamson 	if (csi_seen) {
6834ad6151SAlan Adamson 		struct nvme_ns_id_desc *cur = data;
6934ad6151SAlan Adamson 
7034ad6151SAlan Adamson 		cur->nidt = NVME_NIDT_CSI;
7134ad6151SAlan Adamson 		cur->nidl = NVME_NIDT_CSI_LEN;
7234ad6151SAlan Adamson 		memcpy(cur + 1, &csi, NVME_NIDT_CSI_LEN);
7334ad6151SAlan Adamson 	}
7434ad6151SAlan Adamson 	status = nvmet_copy_to_sgl(req, 0, data, NVME_IDENTIFY_DATA_SIZE);
7534ad6151SAlan Adamson out_free:
7634ad6151SAlan Adamson 	kfree(data);
7734ad6151SAlan Adamson 	return status;
7834ad6151SAlan Adamson }
7934ad6151SAlan Adamson 
nvmet_passthru_override_id_ctrl(struct nvmet_req * req)80c1fef73fSLogan Gunthorpe static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
81c1fef73fSLogan Gunthorpe {
82c1fef73fSLogan Gunthorpe 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
83c1fef73fSLogan Gunthorpe 	struct nvme_ctrl *pctrl = ctrl->subsys->passthru_ctrl;
84c1fef73fSLogan Gunthorpe 	u16 status = NVME_SC_SUCCESS;
85c1fef73fSLogan Gunthorpe 	struct nvme_id_ctrl *id;
865f7136dbSMatthew Wilcox (Oracle) 	unsigned int max_hw_sectors;
87c1fef73fSLogan Gunthorpe 	int page_shift;
88c1fef73fSLogan Gunthorpe 
89c1fef73fSLogan Gunthorpe 	id = kzalloc(sizeof(*id), GFP_KERNEL);
90c1fef73fSLogan Gunthorpe 	if (!id)
91c1fef73fSLogan Gunthorpe 		return NVME_SC_INTERNAL;
92c1fef73fSLogan Gunthorpe 
93c1fef73fSLogan Gunthorpe 	status = nvmet_copy_from_sgl(req, 0, id, sizeof(*id));
94c1fef73fSLogan Gunthorpe 	if (status)
95c1fef73fSLogan Gunthorpe 		goto out_free;
96c1fef73fSLogan Gunthorpe 
97c1fef73fSLogan Gunthorpe 	id->cntlid = cpu_to_le16(ctrl->cntlid);
98c1fef73fSLogan Gunthorpe 	id->ver = cpu_to_le32(ctrl->subsys->ver);
99c1fef73fSLogan Gunthorpe 
100c1fef73fSLogan Gunthorpe 	/*
101c1fef73fSLogan Gunthorpe 	 * The passthru NVMe driver may have a limit on the number of segments
102c1fef73fSLogan Gunthorpe 	 * which depends on the host's memory fragementation. To solve this,
103c1fef73fSLogan Gunthorpe 	 * ensure mdts is limited to the pages equal to the number of segments.
104c1fef73fSLogan Gunthorpe 	 */
1059bcf156fSDamien Le Moal 	max_hw_sectors = min_not_zero(pctrl->max_segments << PAGE_SECTORS_SHIFT,
106c1fef73fSLogan Gunthorpe 				      pctrl->max_hw_sectors);
107c1fef73fSLogan Gunthorpe 
108df06047dSLogan Gunthorpe 	/*
109df06047dSLogan Gunthorpe 	 * nvmet_passthru_map_sg is limitted to using a single bio so limit
110a8affc03SChristoph Hellwig 	 * the mdts based on BIO_MAX_VECS as well
111df06047dSLogan Gunthorpe 	 */
1129bcf156fSDamien Le Moal 	max_hw_sectors = min_not_zero(BIO_MAX_VECS << PAGE_SECTORS_SHIFT,
113df06047dSLogan Gunthorpe 				      max_hw_sectors);
114df06047dSLogan Gunthorpe 
115c1fef73fSLogan Gunthorpe 	page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12;
116c1fef73fSLogan Gunthorpe 
117c1fef73fSLogan Gunthorpe 	id->mdts = ilog2(max_hw_sectors) + 9 - page_shift;
118c1fef73fSLogan Gunthorpe 
119c1fef73fSLogan Gunthorpe 	id->acl = 3;
120c1fef73fSLogan Gunthorpe 	/*
121c1fef73fSLogan Gunthorpe 	 * We export aerl limit for the fabrics controller, update this when
122c1fef73fSLogan Gunthorpe 	 * passthru based aerl support is added.
123c1fef73fSLogan Gunthorpe 	 */
124c1fef73fSLogan Gunthorpe 	id->aerl = NVMET_ASYNC_EVENTS - 1;
125c1fef73fSLogan Gunthorpe 
126c1fef73fSLogan Gunthorpe 	/* emulate kas as most of the PCIe ctrl don't have a support for kas */
127c1fef73fSLogan Gunthorpe 	id->kas = cpu_to_le16(NVMET_KAS);
128c1fef73fSLogan Gunthorpe 
129c1fef73fSLogan Gunthorpe 	/* don't support host memory buffer */
130c1fef73fSLogan Gunthorpe 	id->hmpre = 0;
131c1fef73fSLogan Gunthorpe 	id->hmmin = 0;
132c1fef73fSLogan Gunthorpe 
133c1fef73fSLogan Gunthorpe 	id->sqes = min_t(__u8, ((0x6 << 4) | 0x6), id->sqes);
134c1fef73fSLogan Gunthorpe 	id->cqes = min_t(__u8, ((0x4 << 4) | 0x4), id->cqes);
135c1fef73fSLogan Gunthorpe 	id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
136c1fef73fSLogan Gunthorpe 
137c1fef73fSLogan Gunthorpe 	/* don't support fuse commands */
138c1fef73fSLogan Gunthorpe 	id->fuses = 0;
139c1fef73fSLogan Gunthorpe 
140c1fef73fSLogan Gunthorpe 	id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
141c1fef73fSLogan Gunthorpe 	if (ctrl->ops->flags & NVMF_KEYED_SGLS)
142c1fef73fSLogan Gunthorpe 		id->sgls |= cpu_to_le32(1 << 2);
143c1fef73fSLogan Gunthorpe 	if (req->port->inline_data_size)
144c1fef73fSLogan Gunthorpe 		id->sgls |= cpu_to_le32(1 << 20);
145c1fef73fSLogan Gunthorpe 
146c1fef73fSLogan Gunthorpe 	/*
147cbf84dbfSJulia Lawall 	 * When passthru controller is setup using nvme-loop transport it will
148c1fef73fSLogan Gunthorpe 	 * export the passthru ctrl subsysnqn (PCIe NVMe ctrl) and will fail in
149c1fef73fSLogan Gunthorpe 	 * the nvme/host/core.c in the nvme_init_subsystem()->nvme_active_ctrl()
150c1fef73fSLogan Gunthorpe 	 * code path with duplicate ctr subsynqn. In order to prevent that we
151c1fef73fSLogan Gunthorpe 	 * mask the passthru-ctrl subsysnqn with the target ctrl subsysnqn.
152c1fef73fSLogan Gunthorpe 	 */
153c1fef73fSLogan Gunthorpe 	memcpy(id->subnqn, ctrl->subsysnqn, sizeof(id->subnqn));
154c1fef73fSLogan Gunthorpe 
155c1fef73fSLogan Gunthorpe 	/* use fabric id-ctrl values */
156c1fef73fSLogan Gunthorpe 	id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) +
157c1fef73fSLogan Gunthorpe 				req->port->inline_data_size) / 16);
158c1fef73fSLogan Gunthorpe 	id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
159c1fef73fSLogan Gunthorpe 
160c1fef73fSLogan Gunthorpe 	id->msdbd = ctrl->ops->msdbd;
161c1fef73fSLogan Gunthorpe 
162c1fef73fSLogan Gunthorpe 	/* Support multipath connections with fabrics */
163c1fef73fSLogan Gunthorpe 	id->cmic |= 1 << 1;
164c1fef73fSLogan Gunthorpe 
165c1fef73fSLogan Gunthorpe 	/* Disable reservations, see nvmet_parse_passthru_io_cmd() */
166c1fef73fSLogan Gunthorpe 	id->oncs &= cpu_to_le16(~NVME_CTRL_ONCS_RESERVATIONS);
167c1fef73fSLogan Gunthorpe 
168c1fef73fSLogan Gunthorpe 	status = nvmet_copy_to_sgl(req, 0, id, sizeof(struct nvme_id_ctrl));
169c1fef73fSLogan Gunthorpe 
170c1fef73fSLogan Gunthorpe out_free:
171c1fef73fSLogan Gunthorpe 	kfree(id);
172c1fef73fSLogan Gunthorpe 	return status;
173c1fef73fSLogan Gunthorpe }
174c1fef73fSLogan Gunthorpe 
nvmet_passthru_override_id_ns(struct nvmet_req * req)175c1fef73fSLogan Gunthorpe static u16 nvmet_passthru_override_id_ns(struct nvmet_req *req)
176c1fef73fSLogan Gunthorpe {
177c1fef73fSLogan Gunthorpe 	u16 status = NVME_SC_SUCCESS;
178c1fef73fSLogan Gunthorpe 	struct nvme_id_ns *id;
179c1fef73fSLogan Gunthorpe 	int i;
180c1fef73fSLogan Gunthorpe 
181c1fef73fSLogan Gunthorpe 	id = kzalloc(sizeof(*id), GFP_KERNEL);
182c1fef73fSLogan Gunthorpe 	if (!id)
183c1fef73fSLogan Gunthorpe 		return NVME_SC_INTERNAL;
184c1fef73fSLogan Gunthorpe 
185c1fef73fSLogan Gunthorpe 	status = nvmet_copy_from_sgl(req, 0, id, sizeof(struct nvme_id_ns));
186c1fef73fSLogan Gunthorpe 	if (status)
187c1fef73fSLogan Gunthorpe 		goto out_free;
188c1fef73fSLogan Gunthorpe 
189c1fef73fSLogan Gunthorpe 	for (i = 0; i < (id->nlbaf + 1); i++)
190c1fef73fSLogan Gunthorpe 		if (id->lbaf[i].ms)
191c1fef73fSLogan Gunthorpe 			memset(&id->lbaf[i], 0, sizeof(id->lbaf[i]));
192c1fef73fSLogan Gunthorpe 
193c1fef73fSLogan Gunthorpe 	id->flbas = id->flbas & ~(1 << 4);
194c1fef73fSLogan Gunthorpe 
195c1fef73fSLogan Gunthorpe 	/*
196c1fef73fSLogan Gunthorpe 	 * Presently the NVMEof target code does not support sending
197c1fef73fSLogan Gunthorpe 	 * metadata, so we must disable it here. This should be updated
198c1fef73fSLogan Gunthorpe 	 * once target starts supporting metadata.
199c1fef73fSLogan Gunthorpe 	 */
200c1fef73fSLogan Gunthorpe 	id->mc = 0;
201c1fef73fSLogan Gunthorpe 
20234ad6151SAlan Adamson 	if (req->sq->ctrl->subsys->clear_ids) {
20334ad6151SAlan Adamson 		memset(id->nguid, 0, NVME_NIDT_NGUID_LEN);
20434ad6151SAlan Adamson 		memset(id->eui64, 0, NVME_NIDT_EUI64_LEN);
20534ad6151SAlan Adamson 	}
20634ad6151SAlan Adamson 
207c1fef73fSLogan Gunthorpe 	status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
208c1fef73fSLogan Gunthorpe 
209c1fef73fSLogan Gunthorpe out_free:
210c1fef73fSLogan Gunthorpe 	kfree(id);
211c1fef73fSLogan Gunthorpe 	return status;
212c1fef73fSLogan Gunthorpe }
213c1fef73fSLogan Gunthorpe 
nvmet_passthru_execute_cmd_work(struct work_struct * w)214c1fef73fSLogan Gunthorpe static void nvmet_passthru_execute_cmd_work(struct work_struct *w)
215c1fef73fSLogan Gunthorpe {
216c1fef73fSLogan Gunthorpe 	struct nvmet_req *req = container_of(w, struct nvmet_req, p.work);
217c1fef73fSLogan Gunthorpe 	struct request *rq = req->p.rq;
218bc8fb906SKeith Busch 	struct nvme_ctrl *ctrl = nvme_req(rq)->ctrl;
21962281b9eSChristoph Hellwig 	struct nvme_ns *ns = rq->q->queuedata;
220bc8fb906SKeith Busch 	u32 effects;
221ae5e6886SKeith Busch 	int status;
222c1fef73fSLogan Gunthorpe 
22362281b9eSChristoph Hellwig 	effects = nvme_passthru_start(ctrl, ns, req->cmd->common.opcode);
22462281b9eSChristoph Hellwig 	status = nvme_execute_rq(rq, false);
225c1fef73fSLogan Gunthorpe 	if (status == NVME_SC_SUCCESS &&
226c1fef73fSLogan Gunthorpe 	    req->cmd->common.opcode == nvme_admin_identify) {
227c1fef73fSLogan Gunthorpe 		switch (req->cmd->identify.cns) {
228c1fef73fSLogan Gunthorpe 		case NVME_ID_CNS_CTRL:
229*9a3eb481SDaniel Wagner 			status = nvmet_passthru_override_id_ctrl(req);
230c1fef73fSLogan Gunthorpe 			break;
231c1fef73fSLogan Gunthorpe 		case NVME_ID_CNS_NS:
232*9a3eb481SDaniel Wagner 			status = nvmet_passthru_override_id_ns(req);
233c1fef73fSLogan Gunthorpe 			break;
23434ad6151SAlan Adamson 		case NVME_ID_CNS_NS_DESC_LIST:
235*9a3eb481SDaniel Wagner 			status = nvmet_passthru_override_id_descs(req);
23634ad6151SAlan Adamson 			break;
237c1fef73fSLogan Gunthorpe 		}
238ae5e6886SKeith Busch 	} else if (status < 0)
239ae5e6886SKeith Busch 		status = NVME_SC_INTERNAL;
240c1fef73fSLogan Gunthorpe 
241c1fef73fSLogan Gunthorpe 	req->cqe->result = nvme_req(rq)->result;
242c1fef73fSLogan Gunthorpe 	nvmet_req_complete(req, status);
2437ee51cf6SChaitanya Kulkarni 	blk_mq_free_request(rq);
244bc8fb906SKeith Busch 
245bc8fb906SKeith Busch 	if (effects)
24631a59782Smin15.li 		nvme_passthru_end(ctrl, ns, effects, req->cmd, status);
247c1fef73fSLogan Gunthorpe }
248c1fef73fSLogan Gunthorpe 
nvmet_passthru_req_done(struct request * rq,blk_status_t blk_status)249de671d61SJens Axboe static enum rq_end_io_ret nvmet_passthru_req_done(struct request *rq,
250c1fef73fSLogan Gunthorpe 						  blk_status_t blk_status)
251c1fef73fSLogan Gunthorpe {
252c1fef73fSLogan Gunthorpe 	struct nvmet_req *req = rq->end_io_data;
253c1fef73fSLogan Gunthorpe 
254c1fef73fSLogan Gunthorpe 	req->cqe->result = nvme_req(rq)->result;
255c1fef73fSLogan Gunthorpe 	nvmet_req_complete(req, nvme_req(rq)->status);
2567ee51cf6SChaitanya Kulkarni 	blk_mq_free_request(rq);
257de671d61SJens Axboe 	return RQ_END_IO_NONE;
258c1fef73fSLogan Gunthorpe }
259c1fef73fSLogan Gunthorpe 
nvmet_passthru_map_sg(struct nvmet_req * req,struct request * rq)260c1fef73fSLogan Gunthorpe static int nvmet_passthru_map_sg(struct nvmet_req *req, struct request *rq)
261c1fef73fSLogan Gunthorpe {
262c1fef73fSLogan Gunthorpe 	struct scatterlist *sg;
263c1fef73fSLogan Gunthorpe 	struct bio *bio;
264a4fe2d3aSChaitanya Kulkarni 	int i;
265c1fef73fSLogan Gunthorpe 
266a8affc03SChristoph Hellwig 	if (req->sg_cnt > BIO_MAX_VECS)
2675e063101SLogan Gunthorpe 		return -EINVAL;
2685e063101SLogan Gunthorpe 
269ab96de5dSChaitanya Kulkarni 	if (nvmet_use_inline_bvec(req)) {
270dab3902bSChaitanya Kulkarni 		bio = &req->p.inline_bio;
27149add496SChristoph Hellwig 		bio_init(bio, NULL, req->inline_bvec,
27249add496SChristoph Hellwig 			 ARRAY_SIZE(req->inline_bvec), req_op(rq));
273dab3902bSChaitanya Kulkarni 	} else {
27407888c66SChristoph Hellwig 		bio = bio_alloc(NULL, bio_max_segs(req->sg_cnt), req_op(rq),
27507888c66SChristoph Hellwig 				GFP_KERNEL);
276c1fef73fSLogan Gunthorpe 		bio->bi_end_io = bio_put;
277dab3902bSChaitanya Kulkarni 	}
278c1fef73fSLogan Gunthorpe 
279c1fef73fSLogan Gunthorpe 	for_each_sg(req->sg, sg, req->sg_cnt, i) {
280c1fef73fSLogan Gunthorpe 		if (bio_add_pc_page(rq->q, bio, sg_page(sg), sg->length,
281c1fef73fSLogan Gunthorpe 				    sg->offset) < sg->length) {
2829a01b58cSChaitanya Kulkarni 			nvmet_req_bio_put(req, bio);
283c1fef73fSLogan Gunthorpe 			return -EINVAL;
284c1fef73fSLogan Gunthorpe 		}
285c1fef73fSLogan Gunthorpe 	}
286c1fef73fSLogan Gunthorpe 
287a4fe2d3aSChaitanya Kulkarni 	blk_rq_bio_prep(rq, bio, req->sg_cnt);
288c1fef73fSLogan Gunthorpe 
289c1fef73fSLogan Gunthorpe 	return 0;
290c1fef73fSLogan Gunthorpe }
291c1fef73fSLogan Gunthorpe 
nvmet_passthru_execute_cmd(struct nvmet_req * req)292c1fef73fSLogan Gunthorpe static void nvmet_passthru_execute_cmd(struct nvmet_req *req)
293c1fef73fSLogan Gunthorpe {
294ab7a2737SChristoph Hellwig 	struct nvme_ctrl *ctrl = nvmet_req_subsys(req)->passthru_ctrl;
295c1fef73fSLogan Gunthorpe 	struct request_queue *q = ctrl->admin_q;
296c1fef73fSLogan Gunthorpe 	struct nvme_ns *ns = NULL;
297c1fef73fSLogan Gunthorpe 	struct request *rq = NULL;
29847e9730cSChaitanya Kulkarni 	unsigned int timeout;
299c1fef73fSLogan Gunthorpe 	u32 effects;
300c1fef73fSLogan Gunthorpe 	u16 status;
301c1fef73fSLogan Gunthorpe 	int ret;
302c1fef73fSLogan Gunthorpe 
303c1fef73fSLogan Gunthorpe 	if (likely(req->sq->qid != 0)) {
304c1fef73fSLogan Gunthorpe 		u32 nsid = le32_to_cpu(req->cmd->common.nsid);
305c1fef73fSLogan Gunthorpe 
306c1fef73fSLogan Gunthorpe 		ns = nvme_find_get_ns(ctrl, nsid);
307c1fef73fSLogan Gunthorpe 		if (unlikely(!ns)) {
308c1fef73fSLogan Gunthorpe 			pr_err("failed to get passthru ns nsid:%u\n", nsid);
309c1fef73fSLogan Gunthorpe 			status = NVME_SC_INVALID_NS | NVME_SC_DNR;
3104db69a3dSChaitanya Kulkarni 			goto out;
311c1fef73fSLogan Gunthorpe 		}
312c1fef73fSLogan Gunthorpe 
313c1fef73fSLogan Gunthorpe 		q = ns->queue;
31420c2c3bbSChaitanya Kulkarni 		timeout = nvmet_req_subsys(req)->io_timeout;
315a2f6a2b8SChaitanya Kulkarni 	} else {
31620c2c3bbSChaitanya Kulkarni 		timeout = nvmet_req_subsys(req)->admin_timeout;
317c1fef73fSLogan Gunthorpe 	}
318c1fef73fSLogan Gunthorpe 
319e559398fSChristoph Hellwig 	rq = blk_mq_alloc_request(q, nvme_req_op(req->cmd), 0);
320c1fef73fSLogan Gunthorpe 	if (IS_ERR(rq)) {
321c1fef73fSLogan Gunthorpe 		status = NVME_SC_INTERNAL;
3224db69a3dSChaitanya Kulkarni 		goto out_put_ns;
323c1fef73fSLogan Gunthorpe 	}
324e559398fSChristoph Hellwig 	nvme_init_request(rq, req->cmd);
325c1fef73fSLogan Gunthorpe 
326a2f6a2b8SChaitanya Kulkarni 	if (timeout)
327a2f6a2b8SChaitanya Kulkarni 		rq->timeout = timeout;
328a2f6a2b8SChaitanya Kulkarni 
329c1fef73fSLogan Gunthorpe 	if (req->sg_cnt) {
330c1fef73fSLogan Gunthorpe 		ret = nvmet_passthru_map_sg(req, rq);
331c1fef73fSLogan Gunthorpe 		if (unlikely(ret)) {
332c1fef73fSLogan Gunthorpe 			status = NVME_SC_INTERNAL;
333a2138fd4SChaitanya Kulkarni 			goto out_put_req;
334c1fef73fSLogan Gunthorpe 		}
335c1fef73fSLogan Gunthorpe 	}
336c1fef73fSLogan Gunthorpe 
337c1fef73fSLogan Gunthorpe 	/*
3382a459f69SChristoph Hellwig 	 * If a command needs post-execution fixups, or there are any
3392a459f69SChristoph Hellwig 	 * non-trivial effects, make sure to execute the command synchronously
3402a459f69SChristoph Hellwig 	 * in a workqueue so that nvme_passthru_end gets called.
341c1fef73fSLogan Gunthorpe 	 */
342c1fef73fSLogan Gunthorpe 	effects = nvme_command_effects(ctrl, ns, req->cmd->common.opcode);
3432a459f69SChristoph Hellwig 	if (req->p.use_workqueue ||
3442a459f69SChristoph Hellwig 	    (effects & ~(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC))) {
345c1fef73fSLogan Gunthorpe 		INIT_WORK(&req->p.work, nvmet_passthru_execute_cmd_work);
346c1fef73fSLogan Gunthorpe 		req->p.rq = rq;
3478832cf92SSagi Grimberg 		queue_work(nvmet_wq, &req->p.work);
348c1fef73fSLogan Gunthorpe 	} else {
349e2e53086SChristoph Hellwig 		rq->end_io = nvmet_passthru_req_done;
350c1fef73fSLogan Gunthorpe 		rq->end_io_data = req;
351e2e53086SChristoph Hellwig 		blk_execute_rq_nowait(rq, false);
352c1fef73fSLogan Gunthorpe 	}
353c1fef73fSLogan Gunthorpe 
354c1fef73fSLogan Gunthorpe 	if (ns)
355c1fef73fSLogan Gunthorpe 		nvme_put_ns(ns);
356c1fef73fSLogan Gunthorpe 
357c1fef73fSLogan Gunthorpe 	return;
358c1fef73fSLogan Gunthorpe 
359a2138fd4SChaitanya Kulkarni out_put_req:
3607ee51cf6SChaitanya Kulkarni 	blk_mq_free_request(rq);
3614db69a3dSChaitanya Kulkarni out_put_ns:
362c1fef73fSLogan Gunthorpe 	if (ns)
363c1fef73fSLogan Gunthorpe 		nvme_put_ns(ns);
3644db69a3dSChaitanya Kulkarni out:
365c1fef73fSLogan Gunthorpe 	nvmet_req_complete(req, status);
366c1fef73fSLogan Gunthorpe }
367c1fef73fSLogan Gunthorpe 
368c1fef73fSLogan Gunthorpe /*
369c1fef73fSLogan Gunthorpe  * We need to emulate set host behaviour to ensure that any requested
370c1fef73fSLogan Gunthorpe  * behaviour of the target's host matches the requested behaviour
371c1fef73fSLogan Gunthorpe  * of the device's host and fail otherwise.
372c1fef73fSLogan Gunthorpe  */
nvmet_passthru_set_host_behaviour(struct nvmet_req * req)373c1fef73fSLogan Gunthorpe static void nvmet_passthru_set_host_behaviour(struct nvmet_req *req)
374c1fef73fSLogan Gunthorpe {
375ab7a2737SChristoph Hellwig 	struct nvme_ctrl *ctrl = nvmet_req_subsys(req)->passthru_ctrl;
376c1fef73fSLogan Gunthorpe 	struct nvme_feat_host_behavior *host;
377c1fef73fSLogan Gunthorpe 	u16 status = NVME_SC_INTERNAL;
378c1fef73fSLogan Gunthorpe 	int ret;
379c1fef73fSLogan Gunthorpe 
380c1fef73fSLogan Gunthorpe 	host = kzalloc(sizeof(*host) * 2, GFP_KERNEL);
381c1fef73fSLogan Gunthorpe 	if (!host)
382c1fef73fSLogan Gunthorpe 		goto out_complete_req;
383c1fef73fSLogan Gunthorpe 
384c1fef73fSLogan Gunthorpe 	ret = nvme_get_features(ctrl, NVME_FEAT_HOST_BEHAVIOR, 0,
385c1fef73fSLogan Gunthorpe 				host, sizeof(*host), NULL);
386c1fef73fSLogan Gunthorpe 	if (ret)
387c1fef73fSLogan Gunthorpe 		goto out_free_host;
388c1fef73fSLogan Gunthorpe 
389c1fef73fSLogan Gunthorpe 	status = nvmet_copy_from_sgl(req, 0, &host[1], sizeof(*host));
390c1fef73fSLogan Gunthorpe 	if (status)
391c1fef73fSLogan Gunthorpe 		goto out_free_host;
392c1fef73fSLogan Gunthorpe 
393c1fef73fSLogan Gunthorpe 	if (memcmp(&host[0], &host[1], sizeof(host[0]))) {
394c1fef73fSLogan Gunthorpe 		pr_warn("target host has requested different behaviour from the local host\n");
395c1fef73fSLogan Gunthorpe 		status = NVME_SC_INTERNAL;
396c1fef73fSLogan Gunthorpe 	}
397c1fef73fSLogan Gunthorpe 
398c1fef73fSLogan Gunthorpe out_free_host:
399c1fef73fSLogan Gunthorpe 	kfree(host);
400c1fef73fSLogan Gunthorpe out_complete_req:
401c1fef73fSLogan Gunthorpe 	nvmet_req_complete(req, status);
402c1fef73fSLogan Gunthorpe }
403c1fef73fSLogan Gunthorpe 
nvmet_setup_passthru_command(struct nvmet_req * req)404c1fef73fSLogan Gunthorpe static u16 nvmet_setup_passthru_command(struct nvmet_req *req)
405c1fef73fSLogan Gunthorpe {
406c1fef73fSLogan Gunthorpe 	req->p.use_workqueue = false;
407c1fef73fSLogan Gunthorpe 	req->execute = nvmet_passthru_execute_cmd;
408c1fef73fSLogan Gunthorpe 	return NVME_SC_SUCCESS;
409c1fef73fSLogan Gunthorpe }
410c1fef73fSLogan Gunthorpe 
nvmet_parse_passthru_io_cmd(struct nvmet_req * req)411c1fef73fSLogan Gunthorpe u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req)
412c1fef73fSLogan Gunthorpe {
4130ceeab96SLogan Gunthorpe 	/* Reject any commands with non-sgl flags set (ie. fused commands) */
4140ceeab96SLogan Gunthorpe 	if (req->cmd->common.flags & ~NVME_CMD_SGL_ALL)
4150ceeab96SLogan Gunthorpe 		return NVME_SC_INVALID_FIELD;
4160ceeab96SLogan Gunthorpe 
417c1fef73fSLogan Gunthorpe 	switch (req->cmd->common.opcode) {
418c1fef73fSLogan Gunthorpe 	case nvme_cmd_resv_register:
419c1fef73fSLogan Gunthorpe 	case nvme_cmd_resv_report:
420c1fef73fSLogan Gunthorpe 	case nvme_cmd_resv_acquire:
421c1fef73fSLogan Gunthorpe 	case nvme_cmd_resv_release:
422c1fef73fSLogan Gunthorpe 		/*
423c1fef73fSLogan Gunthorpe 		 * Reservations cannot be supported properly because the
424c1fef73fSLogan Gunthorpe 		 * underlying device has no way of differentiating different
425c1fef73fSLogan Gunthorpe 		 * hosts that connect via fabrics. This could potentially be
426c1fef73fSLogan Gunthorpe 		 * emulated in the future if regular targets grow support for
427c1fef73fSLogan Gunthorpe 		 * this feature.
428c1fef73fSLogan Gunthorpe 		 */
429c1fef73fSLogan Gunthorpe 		return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
430c1fef73fSLogan Gunthorpe 	}
431c1fef73fSLogan Gunthorpe 
432c1fef73fSLogan Gunthorpe 	return nvmet_setup_passthru_command(req);
433c1fef73fSLogan Gunthorpe }
434c1fef73fSLogan Gunthorpe 
435c1fef73fSLogan Gunthorpe /*
436c1fef73fSLogan Gunthorpe  * Only features that are emulated or specifically allowed in the list  are
437c1fef73fSLogan Gunthorpe  * passed down to the controller. This function implements the allow list for
438c1fef73fSLogan Gunthorpe  * both get and set features.
439c1fef73fSLogan Gunthorpe  */
nvmet_passthru_get_set_features(struct nvmet_req * req)440c1fef73fSLogan Gunthorpe static u16 nvmet_passthru_get_set_features(struct nvmet_req *req)
441c1fef73fSLogan Gunthorpe {
442c1fef73fSLogan Gunthorpe 	switch (le32_to_cpu(req->cmd->features.fid)) {
443c1fef73fSLogan Gunthorpe 	case NVME_FEAT_ARBITRATION:
444c1fef73fSLogan Gunthorpe 	case NVME_FEAT_POWER_MGMT:
445c1fef73fSLogan Gunthorpe 	case NVME_FEAT_LBA_RANGE:
446c1fef73fSLogan Gunthorpe 	case NVME_FEAT_TEMP_THRESH:
447c1fef73fSLogan Gunthorpe 	case NVME_FEAT_ERR_RECOVERY:
448c1fef73fSLogan Gunthorpe 	case NVME_FEAT_VOLATILE_WC:
449c1fef73fSLogan Gunthorpe 	case NVME_FEAT_WRITE_ATOMIC:
450c1fef73fSLogan Gunthorpe 	case NVME_FEAT_AUTO_PST:
451c1fef73fSLogan Gunthorpe 	case NVME_FEAT_TIMESTAMP:
452c1fef73fSLogan Gunthorpe 	case NVME_FEAT_HCTM:
453c1fef73fSLogan Gunthorpe 	case NVME_FEAT_NOPSC:
454c1fef73fSLogan Gunthorpe 	case NVME_FEAT_RRL:
455c1fef73fSLogan Gunthorpe 	case NVME_FEAT_PLM_CONFIG:
456c1fef73fSLogan Gunthorpe 	case NVME_FEAT_PLM_WINDOW:
457c1fef73fSLogan Gunthorpe 	case NVME_FEAT_HOST_BEHAVIOR:
458c1fef73fSLogan Gunthorpe 	case NVME_FEAT_SANITIZE:
459c1fef73fSLogan Gunthorpe 	case NVME_FEAT_VENDOR_START ... NVME_FEAT_VENDOR_END:
460c1fef73fSLogan Gunthorpe 		return nvmet_setup_passthru_command(req);
461c1fef73fSLogan Gunthorpe 
462c1fef73fSLogan Gunthorpe 	case NVME_FEAT_ASYNC_EVENT:
463c1fef73fSLogan Gunthorpe 		/* There is no support for forwarding ASYNC events */
464c1fef73fSLogan Gunthorpe 	case NVME_FEAT_IRQ_COALESCE:
465c1fef73fSLogan Gunthorpe 	case NVME_FEAT_IRQ_CONFIG:
466c1fef73fSLogan Gunthorpe 		/* The IRQ settings will not apply to the target controller */
467c1fef73fSLogan Gunthorpe 	case NVME_FEAT_HOST_MEM_BUF:
468c1fef73fSLogan Gunthorpe 		/*
469c1fef73fSLogan Gunthorpe 		 * Any HMB that's set will not be passed through and will
470c1fef73fSLogan Gunthorpe 		 * not work as expected
471c1fef73fSLogan Gunthorpe 		 */
472c1fef73fSLogan Gunthorpe 	case NVME_FEAT_SW_PROGRESS:
473c1fef73fSLogan Gunthorpe 		/*
474c1fef73fSLogan Gunthorpe 		 * The Pre-Boot Software Load Count doesn't make much
475c1fef73fSLogan Gunthorpe 		 * sense for a target to export
476c1fef73fSLogan Gunthorpe 		 */
477c1fef73fSLogan Gunthorpe 	case NVME_FEAT_RESV_MASK:
478c1fef73fSLogan Gunthorpe 	case NVME_FEAT_RESV_PERSIST:
479c1fef73fSLogan Gunthorpe 		/* No reservations, see nvmet_parse_passthru_io_cmd() */
480c1fef73fSLogan Gunthorpe 	default:
481c1fef73fSLogan Gunthorpe 		return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
482c1fef73fSLogan Gunthorpe 	}
483c1fef73fSLogan Gunthorpe }
484c1fef73fSLogan Gunthorpe 
nvmet_parse_passthru_admin_cmd(struct nvmet_req * req)485c1fef73fSLogan Gunthorpe u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req)
486c1fef73fSLogan Gunthorpe {
4870ceeab96SLogan Gunthorpe 	/* Reject any commands with non-sgl flags set (ie. fused commands) */
4880ceeab96SLogan Gunthorpe 	if (req->cmd->common.flags & ~NVME_CMD_SGL_ALL)
4890ceeab96SLogan Gunthorpe 		return NVME_SC_INVALID_FIELD;
4900ceeab96SLogan Gunthorpe 
491c1fef73fSLogan Gunthorpe 	/*
492c1fef73fSLogan Gunthorpe 	 * Passthru all vendor specific commands
493c1fef73fSLogan Gunthorpe 	 */
494c1fef73fSLogan Gunthorpe 	if (req->cmd->common.opcode >= nvme_admin_vendor_start)
495c1fef73fSLogan Gunthorpe 		return nvmet_setup_passthru_command(req);
496c1fef73fSLogan Gunthorpe 
497c1fef73fSLogan Gunthorpe 	switch (req->cmd->common.opcode) {
498c1fef73fSLogan Gunthorpe 	case nvme_admin_async_event:
499c1fef73fSLogan Gunthorpe 		req->execute = nvmet_execute_async_event;
500c1fef73fSLogan Gunthorpe 		return NVME_SC_SUCCESS;
501c1fef73fSLogan Gunthorpe 	case nvme_admin_keep_alive:
502c1fef73fSLogan Gunthorpe 		/*
503c1fef73fSLogan Gunthorpe 		 * Most PCIe ctrls don't support keep alive cmd, we route keep
504c1fef73fSLogan Gunthorpe 		 * alive to the non-passthru mode. In future please change this
505c1fef73fSLogan Gunthorpe 		 * code when PCIe ctrls with keep alive support available.
506c1fef73fSLogan Gunthorpe 		 */
507c1fef73fSLogan Gunthorpe 		req->execute = nvmet_execute_keep_alive;
508c1fef73fSLogan Gunthorpe 		return NVME_SC_SUCCESS;
509c1fef73fSLogan Gunthorpe 	case nvme_admin_set_features:
510c1fef73fSLogan Gunthorpe 		switch (le32_to_cpu(req->cmd->features.fid)) {
511c1fef73fSLogan Gunthorpe 		case NVME_FEAT_ASYNC_EVENT:
512c1fef73fSLogan Gunthorpe 		case NVME_FEAT_KATO:
513c1fef73fSLogan Gunthorpe 		case NVME_FEAT_NUM_QUEUES:
514c1fef73fSLogan Gunthorpe 		case NVME_FEAT_HOST_ID:
515c1fef73fSLogan Gunthorpe 			req->execute = nvmet_execute_set_features;
516c1fef73fSLogan Gunthorpe 			return NVME_SC_SUCCESS;
517c1fef73fSLogan Gunthorpe 		case NVME_FEAT_HOST_BEHAVIOR:
518c1fef73fSLogan Gunthorpe 			req->execute = nvmet_passthru_set_host_behaviour;
519c1fef73fSLogan Gunthorpe 			return NVME_SC_SUCCESS;
520c1fef73fSLogan Gunthorpe 		default:
521c1fef73fSLogan Gunthorpe 			return nvmet_passthru_get_set_features(req);
522c1fef73fSLogan Gunthorpe 		}
523c1fef73fSLogan Gunthorpe 		break;
524c1fef73fSLogan Gunthorpe 	case nvme_admin_get_features:
525c1fef73fSLogan Gunthorpe 		switch (le32_to_cpu(req->cmd->features.fid)) {
526c1fef73fSLogan Gunthorpe 		case NVME_FEAT_ASYNC_EVENT:
527c1fef73fSLogan Gunthorpe 		case NVME_FEAT_KATO:
528c1fef73fSLogan Gunthorpe 		case NVME_FEAT_NUM_QUEUES:
529c1fef73fSLogan Gunthorpe 		case NVME_FEAT_HOST_ID:
530c1fef73fSLogan Gunthorpe 			req->execute = nvmet_execute_get_features;
531c1fef73fSLogan Gunthorpe 			return NVME_SC_SUCCESS;
532c1fef73fSLogan Gunthorpe 		default:
533c1fef73fSLogan Gunthorpe 			return nvmet_passthru_get_set_features(req);
534c1fef73fSLogan Gunthorpe 		}
535c1fef73fSLogan Gunthorpe 		break;
536c1fef73fSLogan Gunthorpe 	case nvme_admin_identify:
537c1fef73fSLogan Gunthorpe 		switch (req->cmd->identify.cns) {
538c1fef73fSLogan Gunthorpe 		case NVME_ID_CNS_CTRL:
539c1fef73fSLogan Gunthorpe 			req->execute = nvmet_passthru_execute_cmd;
540c1fef73fSLogan Gunthorpe 			req->p.use_workqueue = true;
541c1fef73fSLogan Gunthorpe 			return NVME_SC_SUCCESS;
5425b3356d9SChaitanya Kulkarni 		case NVME_ID_CNS_CS_CTRL:
5435b3356d9SChaitanya Kulkarni 			switch (req->cmd->identify.csi) {
5445b3356d9SChaitanya Kulkarni 			case NVME_CSI_ZNS:
5455b3356d9SChaitanya Kulkarni 				req->execute = nvmet_passthru_execute_cmd;
5465b3356d9SChaitanya Kulkarni 				req->p.use_workqueue = true;
5475b3356d9SChaitanya Kulkarni 				return NVME_SC_SUCCESS;
5485b3356d9SChaitanya Kulkarni 			}
5495b3356d9SChaitanya Kulkarni 			return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
550c1fef73fSLogan Gunthorpe 		case NVME_ID_CNS_NS:
551c1fef73fSLogan Gunthorpe 			req->execute = nvmet_passthru_execute_cmd;
552c1fef73fSLogan Gunthorpe 			req->p.use_workqueue = true;
553c1fef73fSLogan Gunthorpe 			return NVME_SC_SUCCESS;
5545b3356d9SChaitanya Kulkarni 		case NVME_ID_CNS_CS_NS:
5555b3356d9SChaitanya Kulkarni 			switch (req->cmd->identify.csi) {
5565b3356d9SChaitanya Kulkarni 			case NVME_CSI_ZNS:
5575b3356d9SChaitanya Kulkarni 				req->execute = nvmet_passthru_execute_cmd;
5585b3356d9SChaitanya Kulkarni 				req->p.use_workqueue = true;
5595b3356d9SChaitanya Kulkarni 				return NVME_SC_SUCCESS;
5605b3356d9SChaitanya Kulkarni 			}
5615b3356d9SChaitanya Kulkarni 			return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
562c1fef73fSLogan Gunthorpe 		default:
563c1fef73fSLogan Gunthorpe 			return nvmet_setup_passthru_command(req);
564c1fef73fSLogan Gunthorpe 		}
565c1fef73fSLogan Gunthorpe 	case nvme_admin_get_log_page:
566c1fef73fSLogan Gunthorpe 		return nvmet_setup_passthru_command(req);
567c1fef73fSLogan Gunthorpe 	default:
568c1fef73fSLogan Gunthorpe 		/* Reject commands not in the allowlist above */
56907116ea5SChaitanya Kulkarni 		return nvmet_report_invalid_opcode(req);
570c1fef73fSLogan Gunthorpe 	}
571c1fef73fSLogan Gunthorpe }
572ba76af67SLogan Gunthorpe 
nvmet_passthru_ctrl_enable(struct nvmet_subsys * subsys)573ba76af67SLogan Gunthorpe int nvmet_passthru_ctrl_enable(struct nvmet_subsys *subsys)
574ba76af67SLogan Gunthorpe {
575ba76af67SLogan Gunthorpe 	struct nvme_ctrl *ctrl;
576b2702aaaSChaitanya Kulkarni 	struct file *file;
577ba76af67SLogan Gunthorpe 	int ret = -EINVAL;
578ba76af67SLogan Gunthorpe 	void *old;
579ba76af67SLogan Gunthorpe 
580ba76af67SLogan Gunthorpe 	mutex_lock(&subsys->lock);
581ba76af67SLogan Gunthorpe 	if (!subsys->passthru_ctrl_path)
582ba76af67SLogan Gunthorpe 		goto out_unlock;
583ba76af67SLogan Gunthorpe 	if (subsys->passthru_ctrl)
584ba76af67SLogan Gunthorpe 		goto out_unlock;
585ba76af67SLogan Gunthorpe 
586ba76af67SLogan Gunthorpe 	if (subsys->nr_namespaces) {
587ba76af67SLogan Gunthorpe 		pr_info("cannot enable both passthru and regular namespaces for a single subsystem");
588ba76af67SLogan Gunthorpe 		goto out_unlock;
589ba76af67SLogan Gunthorpe 	}
590ba76af67SLogan Gunthorpe 
591b2702aaaSChaitanya Kulkarni 	file = filp_open(subsys->passthru_ctrl_path, O_RDWR, 0);
592b2702aaaSChaitanya Kulkarni 	if (IS_ERR(file)) {
593b2702aaaSChaitanya Kulkarni 		ret = PTR_ERR(file);
594b2702aaaSChaitanya Kulkarni 		goto out_unlock;
595b2702aaaSChaitanya Kulkarni 	}
596b2702aaaSChaitanya Kulkarni 
597b2702aaaSChaitanya Kulkarni 	ctrl = nvme_ctrl_from_file(file);
598b2702aaaSChaitanya Kulkarni 	if (!ctrl) {
599ba76af67SLogan Gunthorpe 		pr_err("failed to open nvme controller %s\n",
600ba76af67SLogan Gunthorpe 		       subsys->passthru_ctrl_path);
601ba76af67SLogan Gunthorpe 
602b2702aaaSChaitanya Kulkarni 		goto out_put_file;
603ba76af67SLogan Gunthorpe 	}
604ba76af67SLogan Gunthorpe 
605ba76af67SLogan Gunthorpe 	old = xa_cmpxchg(&passthru_subsystems, ctrl->cntlid, NULL,
606ba76af67SLogan Gunthorpe 			 subsys, GFP_KERNEL);
607ba76af67SLogan Gunthorpe 	if (xa_is_err(old)) {
608ba76af67SLogan Gunthorpe 		ret = xa_err(old);
609b2702aaaSChaitanya Kulkarni 		goto out_put_file;
610ba76af67SLogan Gunthorpe 	}
611ba76af67SLogan Gunthorpe 
612ba76af67SLogan Gunthorpe 	if (old)
613b2702aaaSChaitanya Kulkarni 		goto out_put_file;
614ba76af67SLogan Gunthorpe 
615ba76af67SLogan Gunthorpe 	subsys->passthru_ctrl = ctrl;
616ba76af67SLogan Gunthorpe 	subsys->ver = ctrl->vs;
617ba76af67SLogan Gunthorpe 
618ba76af67SLogan Gunthorpe 	if (subsys->ver < NVME_VS(1, 2, 1)) {
619ba76af67SLogan Gunthorpe 		pr_warn("nvme controller version is too old: %llu.%llu.%llu, advertising 1.2.1\n",
620ba76af67SLogan Gunthorpe 			NVME_MAJOR(subsys->ver), NVME_MINOR(subsys->ver),
621ba76af67SLogan Gunthorpe 			NVME_TERTIARY(subsys->ver));
622ba76af67SLogan Gunthorpe 		subsys->ver = NVME_VS(1, 2, 1);
623ba76af67SLogan Gunthorpe 	}
624b2702aaaSChaitanya Kulkarni 	nvme_get_ctrl(ctrl);
6253a6b0761SChristoph Hellwig 	__module_get(subsys->passthru_ctrl->ops->module);
626b2702aaaSChaitanya Kulkarni 	ret = 0;
627ba76af67SLogan Gunthorpe 
628b2702aaaSChaitanya Kulkarni out_put_file:
629b2702aaaSChaitanya Kulkarni 	filp_close(file, NULL);
630ba76af67SLogan Gunthorpe out_unlock:
631ba76af67SLogan Gunthorpe 	mutex_unlock(&subsys->lock);
632ba76af67SLogan Gunthorpe 	return ret;
633ba76af67SLogan Gunthorpe }
634ba76af67SLogan Gunthorpe 
__nvmet_passthru_ctrl_disable(struct nvmet_subsys * subsys)635ba76af67SLogan Gunthorpe static void __nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
636ba76af67SLogan Gunthorpe {
637ba76af67SLogan Gunthorpe 	if (subsys->passthru_ctrl) {
638ba76af67SLogan Gunthorpe 		xa_erase(&passthru_subsystems, subsys->passthru_ctrl->cntlid);
6393a6b0761SChristoph Hellwig 		module_put(subsys->passthru_ctrl->ops->module);
640ba76af67SLogan Gunthorpe 		nvme_put_ctrl(subsys->passthru_ctrl);
641ba76af67SLogan Gunthorpe 	}
642ba76af67SLogan Gunthorpe 	subsys->passthru_ctrl = NULL;
643ba76af67SLogan Gunthorpe 	subsys->ver = NVMET_DEFAULT_VS;
644ba76af67SLogan Gunthorpe }
645ba76af67SLogan Gunthorpe 
nvmet_passthru_ctrl_disable(struct nvmet_subsys * subsys)646ba76af67SLogan Gunthorpe void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
647ba76af67SLogan Gunthorpe {
648ba76af67SLogan Gunthorpe 	mutex_lock(&subsys->lock);
649ba76af67SLogan Gunthorpe 	__nvmet_passthru_ctrl_disable(subsys);
650ba76af67SLogan Gunthorpe 	mutex_unlock(&subsys->lock);
651ba76af67SLogan Gunthorpe }
652ba76af67SLogan Gunthorpe 
nvmet_passthru_subsys_free(struct nvmet_subsys * subsys)653ba76af67SLogan Gunthorpe void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys)
654ba76af67SLogan Gunthorpe {
655ba76af67SLogan Gunthorpe 	mutex_lock(&subsys->lock);
656ba76af67SLogan Gunthorpe 	__nvmet_passthru_ctrl_disable(subsys);
657ba76af67SLogan Gunthorpe 	mutex_unlock(&subsys->lock);
658ba76af67SLogan Gunthorpe 	kfree(subsys->passthru_ctrl_path);
659ba76af67SLogan Gunthorpe }
660