nvmet: Do not require SGL for PCI target controller commands
authorDamien Le Moal <dlemoal@kernel.org>
Sat, 4 Jan 2025 04:59:44 +0000 (13:59 +0900)
committerKeith Busch <kbusch@kernel.org>
Sat, 11 Jan 2025 03:30:48 +0000 (19:30 -0800)
Support for SGL is optional for the PCI transport. Modify
nvmet_req_init() to not require the NVME_CMD_SGL_METABUF command flag to
be set if the target controller transport type is NVMF_TRTYPE_PCI.
In addition to this, the NVMe base specification v2.1 mandate that all
admin commands use PRP, that is, have CDW0.PSDT cleared to 0. Modify
nvmet_parse_admin_cmd() to check this.

Finally, modify nvmet_check_transfer_len() and
nvmet_check_data_len_lte() to return the appropriate error status
depending on the command using SGL or PRP. Since for fabrics
nvmet_req_init() checks that a command uses SGL, always, this change
affects only PCI target controllers.

Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Tested-by: Rick Wertenbroek <rick.wertenbroek@gmail.com>
Tested-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Signed-off-by: Keith Busch <kbusch@kernel.org>
drivers/nvme/target/admin-cmd.c
drivers/nvme/target/core.c

index c91864c185fc0db44b7a23263d1a142720e7f58c..0c5127a1d191057ac4318a58c6d12b758d28172b 100644 (file)
@@ -1478,6 +1478,11 @@ u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
        if (unlikely(ret))
                return ret;
 
+       /* For PCI controllers, admin commands shall not use SGL. */
+       if (nvmet_is_pci_ctrl(req->sq->ctrl) && !req->sq->qid &&
+           cmd->common.flags & NVME_CMD_SGL_ALL)
+               return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
+
        if (nvmet_is_passthru_req(req))
                return nvmet_parse_passthru_admin_cmd(req);
 
index 3a92e3a81b46fb857adb202e3c75a611bb9bfff6..43c9888eea906799809773ef48401722ffae5bc0 100644 (file)
@@ -1122,12 +1122,15 @@ bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
        /*
         * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
         * contains an address of a single contiguous physical buffer that is
-        * byte aligned.
+        * byte aligned. For PCI controllers, this is optional so not enforced.
         */
        if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
-               req->error_loc = offsetof(struct nvme_common_command, flags);
-               status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
-               goto fail;
+               if (!req->sq->ctrl || !nvmet_is_pci_ctrl(req->sq->ctrl)) {
+                       req->error_loc =
+                               offsetof(struct nvme_common_command, flags);
+                       status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
+                       goto fail;
+               }
        }
 
        if (unlikely(!req->sq->ctrl))
@@ -1182,8 +1185,14 @@ EXPORT_SYMBOL_GPL(nvmet_req_transfer_len);
 bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len)
 {
        if (unlikely(len != req->transfer_len)) {
+               u16 status;
+
                req->error_loc = offsetof(struct nvme_common_command, dptr);
-               nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR);
+               if (req->cmd->common.flags & NVME_CMD_SGL_ALL)
+                       status = NVME_SC_SGL_INVALID_DATA;
+               else
+                       status = NVME_SC_INVALID_FIELD;
+               nvmet_req_complete(req, status | NVME_STATUS_DNR);
                return false;
        }
 
@@ -1194,8 +1203,14 @@ EXPORT_SYMBOL_GPL(nvmet_check_transfer_len);
 bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len)
 {
        if (unlikely(data_len > req->transfer_len)) {
+               u16 status;
+
                req->error_loc = offsetof(struct nvme_common_command, dptr);
-               nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR);
+               if (req->cmd->common.flags & NVME_CMD_SGL_ALL)
+                       status = NVME_SC_SGL_INVALID_DATA;
+               else
+                       status = NVME_SC_INVALID_FIELD;
+               nvmet_req_complete(req, status | NVME_STATUS_DNR);
                return false;
        }