1 // SPDX-License-Identifier: GPL-2.0
3 * NVMe Over Fabrics Target Passthrough command implementation.
5 * Copyright (c) 2017-2018 Western Digital Corporation or its
7 * Copyright (c) 2019-2020, Eideticom Inc.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/module.h>
13 #include "../host/nvme.h"
16 MODULE_IMPORT_NS(NVME_TARGET_PASSTHRU);
19 * xarray to maintain one passthru subsystem per nvme controller.
21 static DEFINE_XARRAY(passthru_subsystems);
23 static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
25 struct nvmet_ctrl *ctrl = req->sq->ctrl;
26 struct nvme_ctrl *pctrl = ctrl->subsys->passthru_ctrl;
27 u16 status = NVME_SC_SUCCESS;
28 struct nvme_id_ctrl *id;
32 id = kzalloc(sizeof(*id), GFP_KERNEL);
34 return NVME_SC_INTERNAL;
36 status = nvmet_copy_from_sgl(req, 0, id, sizeof(*id));
40 id->cntlid = cpu_to_le16(ctrl->cntlid);
41 id->ver = cpu_to_le32(ctrl->subsys->ver);
44 * The passthru NVMe driver may have a limit on the number of segments
45 * which depends on the host's memory fragementation. To solve this,
46 * ensure mdts is limited to the pages equal to the number of segments.
48 max_hw_sectors = min_not_zero(pctrl->max_segments << (PAGE_SHIFT - 9),
49 pctrl->max_hw_sectors);
52 * nvmet_passthru_map_sg is limitted to using a single bio so limit
53 * the mdts based on BIO_MAX_PAGES as well
55 max_hw_sectors = min_not_zero(BIO_MAX_PAGES << (PAGE_SHIFT - 9),
58 page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12;
60 id->mdts = ilog2(max_hw_sectors) + 9 - page_shift;
64 * We export aerl limit for the fabrics controller, update this when
65 * passthru based aerl support is added.
67 id->aerl = NVMET_ASYNC_EVENTS - 1;
69 /* emulate kas as most of the PCIe ctrl don't have a support for kas */
70 id->kas = cpu_to_le16(NVMET_KAS);
72 /* don't support host memory buffer */
76 id->sqes = min_t(__u8, ((0x6 << 4) | 0x6), id->sqes);
77 id->cqes = min_t(__u8, ((0x4 << 4) | 0x4), id->cqes);
78 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
80 /* don't support fuse commands */
83 id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
84 if (ctrl->ops->flags & NVMF_KEYED_SGLS)
85 id->sgls |= cpu_to_le32(1 << 2);
86 if (req->port->inline_data_size)
87 id->sgls |= cpu_to_le32(1 << 20);
90 * When passsthru controller is setup using nvme-loop transport it will
91 * export the passthru ctrl subsysnqn (PCIe NVMe ctrl) and will fail in
92 * the nvme/host/core.c in the nvme_init_subsystem()->nvme_active_ctrl()
93 * code path with duplicate ctr subsynqn. In order to prevent that we
94 * mask the passthru-ctrl subsysnqn with the target ctrl subsysnqn.
96 memcpy(id->subnqn, ctrl->subsysnqn, sizeof(id->subnqn));
98 /* use fabric id-ctrl values */
99 id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) +
100 req->port->inline_data_size) / 16);
101 id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
103 id->msdbd = ctrl->ops->msdbd;
105 /* Support multipath connections with fabrics */
108 /* Disable reservations, see nvmet_parse_passthru_io_cmd() */
109 id->oncs &= cpu_to_le16(~NVME_CTRL_ONCS_RESERVATIONS);
111 status = nvmet_copy_to_sgl(req, 0, id, sizeof(struct nvme_id_ctrl));
118 static u16 nvmet_passthru_override_id_ns(struct nvmet_req *req)
120 u16 status = NVME_SC_SUCCESS;
121 struct nvme_id_ns *id;
124 id = kzalloc(sizeof(*id), GFP_KERNEL);
126 return NVME_SC_INTERNAL;
128 status = nvmet_copy_from_sgl(req, 0, id, sizeof(struct nvme_id_ns));
132 for (i = 0; i < (id->nlbaf + 1); i++)
134 memset(&id->lbaf[i], 0, sizeof(id->lbaf[i]));
136 id->flbas = id->flbas & ~(1 << 4);
139 * Presently the NVMEof target code does not support sending
140 * metadata, so we must disable it here. This should be updated
141 * once target starts supporting metadata.
145 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
152 static void nvmet_passthru_execute_cmd_work(struct work_struct *w)
154 struct nvmet_req *req = container_of(w, struct nvmet_req, p.work);
155 struct request *rq = req->p.rq;
158 nvme_execute_passthru_rq(rq);
160 status = nvme_req(rq)->status;
161 if (status == NVME_SC_SUCCESS &&
162 req->cmd->common.opcode == nvme_admin_identify) {
163 switch (req->cmd->identify.cns) {
164 case NVME_ID_CNS_CTRL:
165 nvmet_passthru_override_id_ctrl(req);
168 nvmet_passthru_override_id_ns(req);
173 req->cqe->result = nvme_req(rq)->result;
174 nvmet_req_complete(req, status);
175 blk_mq_free_request(rq);
178 static void nvmet_passthru_req_done(struct request *rq,
179 blk_status_t blk_status)
181 struct nvmet_req *req = rq->end_io_data;
183 req->cqe->result = nvme_req(rq)->result;
184 nvmet_req_complete(req, nvme_req(rq)->status);
185 blk_mq_free_request(rq);
188 static int nvmet_passthru_map_sg(struct nvmet_req *req, struct request *rq)
190 struct scatterlist *sg;
195 if (req->sg_cnt > BIO_MAX_PAGES)
198 if (req->cmd->common.opcode == nvme_cmd_flush)
200 else if (nvme_is_write(req->cmd))
201 op_flags = REQ_SYNC | REQ_IDLE;
203 bio = bio_alloc(GFP_KERNEL, req->sg_cnt);
204 bio->bi_end_io = bio_put;
205 bio->bi_opf = req_op(rq) | op_flags;
207 for_each_sg(req->sg, sg, req->sg_cnt, i) {
208 if (bio_add_pc_page(rq->q, bio, sg_page(sg), sg->length,
209 sg->offset) < sg->length) {
215 ret = blk_rq_append_bio(rq, &bio);
224 static void nvmet_passthru_execute_cmd(struct nvmet_req *req)
226 struct nvme_ctrl *ctrl = nvmet_req_passthru_ctrl(req);
227 struct request_queue *q = ctrl->admin_q;
228 struct nvme_ns *ns = NULL;
229 struct request *rq = NULL;
234 if (likely(req->sq->qid != 0)) {
235 u32 nsid = le32_to_cpu(req->cmd->common.nsid);
237 ns = nvme_find_get_ns(ctrl, nsid);
239 pr_err("failed to get passthru ns nsid:%u\n", nsid);
240 status = NVME_SC_INVALID_NS | NVME_SC_DNR;
247 rq = nvme_alloc_request(q, req->cmd, 0);
249 status = NVME_SC_INTERNAL;
254 ret = nvmet_passthru_map_sg(req, rq);
256 status = NVME_SC_INTERNAL;
262 * If there are effects for the command we are about to execute, or
263 * an end_req function we need to use nvme_execute_passthru_rq()
264 * synchronously in a work item seeing the end_req function and
265 * nvme_passthru_end() can't be called in the request done callback
266 * which is typically in interrupt context.
268 effects = nvme_command_effects(ctrl, ns, req->cmd->common.opcode);
269 if (req->p.use_workqueue || effects) {
270 INIT_WORK(&req->p.work, nvmet_passthru_execute_cmd_work);
272 schedule_work(&req->p.work);
274 rq->end_io_data = req;
275 blk_execute_rq_nowait(rq->q, ns ? ns->disk : NULL, rq, 0,
276 nvmet_passthru_req_done);
285 blk_mq_free_request(rq);
290 nvmet_req_complete(req, status);
294 * We need to emulate set host behaviour to ensure that any requested
295 * behaviour of the target's host matches the requested behaviour
296 * of the device's host and fail otherwise.
298 static void nvmet_passthru_set_host_behaviour(struct nvmet_req *req)
300 struct nvme_ctrl *ctrl = nvmet_req_passthru_ctrl(req);
301 struct nvme_feat_host_behavior *host;
302 u16 status = NVME_SC_INTERNAL;
305 host = kzalloc(sizeof(*host) * 2, GFP_KERNEL);
307 goto out_complete_req;
309 ret = nvme_get_features(ctrl, NVME_FEAT_HOST_BEHAVIOR, 0,
310 host, sizeof(*host), NULL);
314 status = nvmet_copy_from_sgl(req, 0, &host[1], sizeof(*host));
318 if (memcmp(&host[0], &host[1], sizeof(host[0]))) {
319 pr_warn("target host has requested different behaviour from the local host\n");
320 status = NVME_SC_INTERNAL;
326 nvmet_req_complete(req, status);
329 static u16 nvmet_setup_passthru_command(struct nvmet_req *req)
331 req->p.use_workqueue = false;
332 req->execute = nvmet_passthru_execute_cmd;
333 return NVME_SC_SUCCESS;
336 u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req)
338 /* Reject any commands with non-sgl flags set (ie. fused commands) */
339 if (req->cmd->common.flags & ~NVME_CMD_SGL_ALL)
340 return NVME_SC_INVALID_FIELD;
342 switch (req->cmd->common.opcode) {
343 case nvme_cmd_resv_register:
344 case nvme_cmd_resv_report:
345 case nvme_cmd_resv_acquire:
346 case nvme_cmd_resv_release:
348 * Reservations cannot be supported properly because the
349 * underlying device has no way of differentiating different
350 * hosts that connect via fabrics. This could potentially be
351 * emulated in the future if regular targets grow support for
354 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
357 return nvmet_setup_passthru_command(req);
361 * Only features that are emulated or specifically allowed in the list are
362 * passed down to the controller. This function implements the allow list for
363 * both get and set features.
365 static u16 nvmet_passthru_get_set_features(struct nvmet_req *req)
367 switch (le32_to_cpu(req->cmd->features.fid)) {
368 case NVME_FEAT_ARBITRATION:
369 case NVME_FEAT_POWER_MGMT:
370 case NVME_FEAT_LBA_RANGE:
371 case NVME_FEAT_TEMP_THRESH:
372 case NVME_FEAT_ERR_RECOVERY:
373 case NVME_FEAT_VOLATILE_WC:
374 case NVME_FEAT_WRITE_ATOMIC:
375 case NVME_FEAT_AUTO_PST:
376 case NVME_FEAT_TIMESTAMP:
378 case NVME_FEAT_NOPSC:
380 case NVME_FEAT_PLM_CONFIG:
381 case NVME_FEAT_PLM_WINDOW:
382 case NVME_FEAT_HOST_BEHAVIOR:
383 case NVME_FEAT_SANITIZE:
384 case NVME_FEAT_VENDOR_START ... NVME_FEAT_VENDOR_END:
385 return nvmet_setup_passthru_command(req);
387 case NVME_FEAT_ASYNC_EVENT:
388 /* There is no support for forwarding ASYNC events */
389 case NVME_FEAT_IRQ_COALESCE:
390 case NVME_FEAT_IRQ_CONFIG:
391 /* The IRQ settings will not apply to the target controller */
392 case NVME_FEAT_HOST_MEM_BUF:
394 * Any HMB that's set will not be passed through and will
395 * not work as expected
397 case NVME_FEAT_SW_PROGRESS:
399 * The Pre-Boot Software Load Count doesn't make much
400 * sense for a target to export
402 case NVME_FEAT_RESV_MASK:
403 case NVME_FEAT_RESV_PERSIST:
404 /* No reservations, see nvmet_parse_passthru_io_cmd() */
406 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
410 u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req)
412 /* Reject any commands with non-sgl flags set (ie. fused commands) */
413 if (req->cmd->common.flags & ~NVME_CMD_SGL_ALL)
414 return NVME_SC_INVALID_FIELD;
417 * Passthru all vendor specific commands
419 if (req->cmd->common.opcode >= nvme_admin_vendor_start)
420 return nvmet_setup_passthru_command(req);
422 switch (req->cmd->common.opcode) {
423 case nvme_admin_async_event:
424 req->execute = nvmet_execute_async_event;
425 return NVME_SC_SUCCESS;
426 case nvme_admin_keep_alive:
428 * Most PCIe ctrls don't support keep alive cmd, we route keep
429 * alive to the non-passthru mode. In future please change this
430 * code when PCIe ctrls with keep alive support available.
432 req->execute = nvmet_execute_keep_alive;
433 return NVME_SC_SUCCESS;
434 case nvme_admin_set_features:
435 switch (le32_to_cpu(req->cmd->features.fid)) {
436 case NVME_FEAT_ASYNC_EVENT:
438 case NVME_FEAT_NUM_QUEUES:
439 case NVME_FEAT_HOST_ID:
440 req->execute = nvmet_execute_set_features;
441 return NVME_SC_SUCCESS;
442 case NVME_FEAT_HOST_BEHAVIOR:
443 req->execute = nvmet_passthru_set_host_behaviour;
444 return NVME_SC_SUCCESS;
446 return nvmet_passthru_get_set_features(req);
449 case nvme_admin_get_features:
450 switch (le32_to_cpu(req->cmd->features.fid)) {
451 case NVME_FEAT_ASYNC_EVENT:
453 case NVME_FEAT_NUM_QUEUES:
454 case NVME_FEAT_HOST_ID:
455 req->execute = nvmet_execute_get_features;
456 return NVME_SC_SUCCESS;
458 return nvmet_passthru_get_set_features(req);
461 case nvme_admin_identify:
462 switch (req->cmd->identify.cns) {
463 case NVME_ID_CNS_CTRL:
464 req->execute = nvmet_passthru_execute_cmd;
465 req->p.use_workqueue = true;
466 return NVME_SC_SUCCESS;
467 case NVME_ID_CNS_CS_CTRL:
468 switch (req->cmd->identify.csi) {
470 req->execute = nvmet_passthru_execute_cmd;
471 req->p.use_workqueue = true;
472 return NVME_SC_SUCCESS;
474 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
476 req->execute = nvmet_passthru_execute_cmd;
477 req->p.use_workqueue = true;
478 return NVME_SC_SUCCESS;
479 case NVME_ID_CNS_CS_NS:
480 switch (req->cmd->identify.csi) {
482 req->execute = nvmet_passthru_execute_cmd;
483 req->p.use_workqueue = true;
484 return NVME_SC_SUCCESS;
486 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
488 return nvmet_setup_passthru_command(req);
490 case nvme_admin_get_log_page:
491 return nvmet_setup_passthru_command(req);
493 /* Reject commands not in the allowlist above */
494 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
498 int nvmet_passthru_ctrl_enable(struct nvmet_subsys *subsys)
500 struct nvme_ctrl *ctrl;
505 mutex_lock(&subsys->lock);
506 if (!subsys->passthru_ctrl_path)
508 if (subsys->passthru_ctrl)
511 if (subsys->nr_namespaces) {
512 pr_info("cannot enable both passthru and regular namespaces for a single subsystem");
516 file = filp_open(subsys->passthru_ctrl_path, O_RDWR, 0);
522 ctrl = nvme_ctrl_from_file(file);
524 pr_err("failed to open nvme controller %s\n",
525 subsys->passthru_ctrl_path);
530 old = xa_cmpxchg(&passthru_subsystems, ctrl->cntlid, NULL,
532 if (xa_is_err(old)) {
540 subsys->passthru_ctrl = ctrl;
541 subsys->ver = ctrl->vs;
543 if (subsys->ver < NVME_VS(1, 2, 1)) {
544 pr_warn("nvme controller version is too old: %llu.%llu.%llu, advertising 1.2.1\n",
545 NVME_MAJOR(subsys->ver), NVME_MINOR(subsys->ver),
546 NVME_TERTIARY(subsys->ver));
547 subsys->ver = NVME_VS(1, 2, 1);
550 __module_get(subsys->passthru_ctrl->ops->module);
554 filp_close(file, NULL);
556 mutex_unlock(&subsys->lock);
560 static void __nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
562 if (subsys->passthru_ctrl) {
563 xa_erase(&passthru_subsystems, subsys->passthru_ctrl->cntlid);
564 module_put(subsys->passthru_ctrl->ops->module);
565 nvme_put_ctrl(subsys->passthru_ctrl);
567 subsys->passthru_ctrl = NULL;
568 subsys->ver = NVMET_DEFAULT_VS;
571 void nvmet_passthru_ctrl_disable(struct nvmet_subsys *subsys)
573 mutex_lock(&subsys->lock);
574 __nvmet_passthru_ctrl_disable(subsys);
575 mutex_unlock(&subsys->lock);
578 void nvmet_passthru_subsys_free(struct nvmet_subsys *subsys)
580 mutex_lock(&subsys->lock);
581 __nvmet_passthru_ctrl_disable(subsys);
582 mutex_unlock(&subsys->lock);
583 kfree(subsys->passthru_ctrl_path);