Skip to content

Commit 68a5c91

Browse files
damien-lemoalkeithbusch
authored andcommitted
nvmet: pci-epf: Correctly initialize CSTS when enabling the controller
The function nvmet_pci_epf_poll_cc_work() sets the NVME_CSTS_RDY bit of the controller status register (CSTS) when nvmet_pci_epf_enable_ctrl() returns success. However, since this function can be called several times (e.g. if the host reboots), instead of setting the bit in ctrl->csts, initialize this field to only have NVME_CSTS_RDY set. Conversely, if nvmet_pci_epf_enable_ctrl() fails, make sure to clear all bits from ctrl->csts. To simplify nvmet_pci_epf_poll_cc_work(), initialize ctrl->csts to NVME_CSTS_RDY directly inside nvmet_pci_epf_enable_ctrl() and clear this field in that function as well in case of a failure. To be consistent, move clearing the NVME_CSTS_RDY bit from ctrl->csts when the controller is being disabled from nvmet_pci_epf_poll_cc_work() into nvmet_pci_epf_disable_ctrl(). Fixes: 0faa0fe ("nvmet: New NVMe PCI endpoint function target driver") Signed-off-by: Damien Le Moal <dlemoal@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Sagi Grimberg <sagi@grimberg.me> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent 3988ac1 commit 68a5c91

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

drivers/nvme/target/pci-epf.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,14 +1822,14 @@ static int nvmet_pci_epf_enable_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
18221822
if (ctrl->io_sqes < sizeof(struct nvme_command)) {
18231823
dev_err(ctrl->dev, "Unsupported I/O SQES %zu (need %zu)\n",
18241824
ctrl->io_sqes, sizeof(struct nvme_command));
1825-
return -EINVAL;
1825+
goto err;
18261826
}
18271827

18281828
ctrl->io_cqes = 1UL << nvmet_cc_iocqes(ctrl->cc);
18291829
if (ctrl->io_cqes < sizeof(struct nvme_completion)) {
18301830
dev_err(ctrl->dev, "Unsupported I/O CQES %zu (need %zu)\n",
18311831
ctrl->io_sqes, sizeof(struct nvme_completion));
1832-
return -EINVAL;
1832+
goto err;
18331833
}
18341834

18351835
/* Create the admin queue. */
@@ -1844,7 +1844,7 @@ static int nvmet_pci_epf_enable_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
18441844
qsize, pci_addr, 0);
18451845
if (status != NVME_SC_SUCCESS) {
18461846
dev_err(ctrl->dev, "Failed to create admin completion queue\n");
1847-
return -EINVAL;
1847+
goto err;
18481848
}
18491849

18501850
qsize = aqa & 0x00000fff;
@@ -1854,17 +1854,22 @@ static int nvmet_pci_epf_enable_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
18541854
if (status != NVME_SC_SUCCESS) {
18551855
dev_err(ctrl->dev, "Failed to create admin submission queue\n");
18561856
nvmet_pci_epf_delete_cq(ctrl->tctrl, 0);
1857-
return -EINVAL;
1857+
goto err;
18581858
}
18591859

18601860
ctrl->sq_ab = NVMET_PCI_EPF_SQ_AB;
18611861
ctrl->irq_vector_threshold = NVMET_PCI_EPF_IV_THRESHOLD;
18621862
ctrl->enabled = true;
1863+
ctrl->csts = NVME_CSTS_RDY;
18631864

18641865
/* Start polling the controller SQs. */
18651866
schedule_delayed_work(&ctrl->poll_sqs, 0);
18661867

18671868
return 0;
1869+
1870+
err:
1871+
ctrl->csts = 0;
1872+
return -EINVAL;
18681873
}
18691874

18701875
static void nvmet_pci_epf_disable_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
@@ -1889,6 +1894,8 @@ static void nvmet_pci_epf_disable_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
18891894
/* Delete the admin queue last. */
18901895
nvmet_pci_epf_delete_sq(ctrl->tctrl, 0);
18911896
nvmet_pci_epf_delete_cq(ctrl->tctrl, 0);
1897+
1898+
ctrl->csts &= ~NVME_CSTS_RDY;
18921899
}
18931900

18941901
static void nvmet_pci_epf_poll_cc_work(struct work_struct *work)
@@ -1909,13 +1916,10 @@ static void nvmet_pci_epf_poll_cc_work(struct work_struct *work)
19091916
ret = nvmet_pci_epf_enable_ctrl(ctrl);
19101917
if (ret)
19111918
return;
1912-
ctrl->csts |= NVME_CSTS_RDY;
19131919
}
19141920

1915-
if (!nvmet_cc_en(new_cc) && nvmet_cc_en(old_cc)) {
1921+
if (!nvmet_cc_en(new_cc) && nvmet_cc_en(old_cc))
19161922
nvmet_pci_epf_disable_ctrl(ctrl);
1917-
ctrl->csts &= ~NVME_CSTS_RDY;
1918-
}
19191923

19201924
if (nvmet_cc_shn(new_cc) && !nvmet_cc_shn(old_cc)) {
19211925
nvmet_pci_epf_disable_ctrl(ctrl);

0 commit comments

Comments
 (0)