@@ -1648,16 +1648,17 @@ static int nvmet_pci_epf_process_sq(struct nvmet_pci_epf_ctrl *ctrl,
1648
1648
{
1649
1649
struct nvmet_pci_epf_iod * iod ;
1650
1650
int ret , n = 0 ;
1651
+ u16 head = sq -> head ;
1651
1652
1652
1653
sq -> tail = nvmet_pci_epf_bar_read32 (ctrl , sq -> db );
1653
- while (sq -> head != sq -> tail && (!ctrl -> sq_ab || n < ctrl -> sq_ab )) {
1654
+ while (head != sq -> tail && (!ctrl -> sq_ab || n < ctrl -> sq_ab )) {
1654
1655
iod = nvmet_pci_epf_alloc_iod (sq );
1655
1656
if (!iod )
1656
1657
break ;
1657
1658
1658
1659
/* Get the NVMe command submitted by the host. */
1659
1660
ret = nvmet_pci_epf_transfer (ctrl , & iod -> cmd ,
1660
- sq -> pci_addr + sq -> head * sq -> qes ,
1661
+ sq -> pci_addr + head * sq -> qes ,
1661
1662
sq -> qes , DMA_FROM_DEVICE );
1662
1663
if (ret ) {
1663
1664
/* Not much we can do... */
@@ -1666,12 +1667,13 @@ static int nvmet_pci_epf_process_sq(struct nvmet_pci_epf_ctrl *ctrl,
1666
1667
}
1667
1668
1668
1669
dev_dbg (ctrl -> dev , "SQ[%u]: head %u, tail %u, command %s\n" ,
1669
- sq -> qid , sq -> head , sq -> tail ,
1670
+ sq -> qid , head , sq -> tail ,
1670
1671
nvmet_pci_epf_iod_name (iod ));
1671
1672
1672
- sq -> head ++ ;
1673
- if (sq -> head == sq -> depth )
1674
- sq -> head = 0 ;
1673
+ head ++ ;
1674
+ if (head == sq -> depth )
1675
+ head = 0 ;
1676
+ WRITE_ONCE (sq -> head , head );
1675
1677
n ++ ;
1676
1678
1677
1679
queue_work_on (WORK_CPU_UNBOUND , sq -> iod_wq , & iod -> work );
@@ -1761,8 +1763,17 @@ static void nvmet_pci_epf_cq_work(struct work_struct *work)
1761
1763
if (!iod )
1762
1764
break ;
1763
1765
1764
- /* Post the IOD completion entry. */
1766
+ /*
1767
+ * Post the IOD completion entry. If the IOD request was
1768
+ * executed (req->execute() called), the CQE is already
1769
+ * initialized. However, the IOD may have been failed before
1770
+ * that, leaving the CQE not properly initialized. So always
1771
+ * initialize it here.
1772
+ */
1765
1773
cqe = & iod -> cqe ;
1774
+ cqe -> sq_head = cpu_to_le16 (READ_ONCE (iod -> sq -> head ));
1775
+ cqe -> sq_id = cpu_to_le16 (iod -> sq -> qid );
1776
+ cqe -> command_id = iod -> cmd .common .command_id ;
1766
1777
cqe -> status = cpu_to_le16 ((iod -> status << 1 ) | cq -> phase );
1767
1778
1768
1779
dev_dbg (ctrl -> dev ,
@@ -1800,6 +1811,21 @@ static void nvmet_pci_epf_cq_work(struct work_struct *work)
1800
1811
NVMET_PCI_EPF_CQ_RETRY_INTERVAL );
1801
1812
}
1802
1813
1814
+ static void nvmet_pci_epf_clear_ctrl_config (struct nvmet_pci_epf_ctrl * ctrl )
1815
+ {
1816
+ struct nvmet_ctrl * tctrl = ctrl -> tctrl ;
1817
+
1818
+ /* Initialize controller status. */
1819
+ tctrl -> csts = 0 ;
1820
+ ctrl -> csts = 0 ;
1821
+ nvmet_pci_epf_bar_write32 (ctrl , NVME_REG_CSTS , ctrl -> csts );
1822
+
1823
+ /* Initialize controller configuration and start polling. */
1824
+ tctrl -> cc = 0 ;
1825
+ ctrl -> cc = 0 ;
1826
+ nvmet_pci_epf_bar_write32 (ctrl , NVME_REG_CC , ctrl -> cc );
1827
+ }
1828
+
1803
1829
static int nvmet_pci_epf_enable_ctrl (struct nvmet_pci_epf_ctrl * ctrl )
1804
1830
{
1805
1831
u64 pci_addr , asq , acq ;
@@ -1865,18 +1891,20 @@ static int nvmet_pci_epf_enable_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
1865
1891
return 0 ;
1866
1892
1867
1893
err :
1868
- ctrl -> csts = 0 ;
1894
+ nvmet_pci_epf_clear_ctrl_config ( ctrl ) ;
1869
1895
return - EINVAL ;
1870
1896
}
1871
1897
1872
- static void nvmet_pci_epf_disable_ctrl (struct nvmet_pci_epf_ctrl * ctrl )
1898
+ static void nvmet_pci_epf_disable_ctrl (struct nvmet_pci_epf_ctrl * ctrl ,
1899
+ bool shutdown )
1873
1900
{
1874
1901
int qid ;
1875
1902
1876
1903
if (!ctrl -> enabled )
1877
1904
return ;
1878
1905
1879
- dev_info (ctrl -> dev , "Disabling controller\n" );
1906
+ dev_info (ctrl -> dev , "%s controller\n" ,
1907
+ shutdown ? "Shutting down" : "Disabling" );
1880
1908
1881
1909
ctrl -> enabled = false;
1882
1910
cancel_delayed_work_sync (& ctrl -> poll_sqs );
@@ -1893,6 +1921,11 @@ static void nvmet_pci_epf_disable_ctrl(struct nvmet_pci_epf_ctrl *ctrl)
1893
1921
nvmet_pci_epf_delete_cq (ctrl -> tctrl , 0 );
1894
1922
1895
1923
ctrl -> csts &= ~NVME_CSTS_RDY ;
1924
+ if (shutdown ) {
1925
+ ctrl -> csts |= NVME_CSTS_SHST_CMPLT ;
1926
+ ctrl -> cc &= ~NVME_CC_ENABLE ;
1927
+ nvmet_pci_epf_bar_write32 (ctrl , NVME_REG_CC , ctrl -> cc );
1928
+ }
1896
1929
}
1897
1930
1898
1931
static void nvmet_pci_epf_poll_cc_work (struct work_struct * work )
@@ -1919,12 +1952,10 @@ static void nvmet_pci_epf_poll_cc_work(struct work_struct *work)
1919
1952
}
1920
1953
1921
1954
if (!nvmet_cc_en (new_cc ) && nvmet_cc_en (old_cc ))
1922
- nvmet_pci_epf_disable_ctrl (ctrl );
1955
+ nvmet_pci_epf_disable_ctrl (ctrl , false );
1923
1956
1924
- if (nvmet_cc_shn (new_cc ) && !nvmet_cc_shn (old_cc )) {
1925
- nvmet_pci_epf_disable_ctrl (ctrl );
1926
- ctrl -> csts |= NVME_CSTS_SHST_CMPLT ;
1927
- }
1957
+ if (nvmet_cc_shn (new_cc ) && !nvmet_cc_shn (old_cc ))
1958
+ nvmet_pci_epf_disable_ctrl (ctrl , true);
1928
1959
1929
1960
if (!nvmet_cc_shn (new_cc ) && nvmet_cc_shn (old_cc ))
1930
1961
ctrl -> csts &= ~NVME_CSTS_SHST_CMPLT ;
@@ -1963,16 +1994,10 @@ static void nvmet_pci_epf_init_bar(struct nvmet_pci_epf_ctrl *ctrl)
1963
1994
/* Clear Controller Memory Buffer Supported (CMBS). */
1964
1995
ctrl -> cap &= ~(0x1ULL << 57 );
1965
1996
1966
- /* Controller configuration. */
1967
- ctrl -> cc = tctrl -> cc & (~NVME_CC_ENABLE );
1968
-
1969
- /* Controller status. */
1970
- ctrl -> csts = ctrl -> tctrl -> csts ;
1971
-
1972
1997
nvmet_pci_epf_bar_write64 (ctrl , NVME_REG_CAP , ctrl -> cap );
1973
1998
nvmet_pci_epf_bar_write32 (ctrl , NVME_REG_VS , tctrl -> subsys -> ver );
1974
- nvmet_pci_epf_bar_write32 ( ctrl , NVME_REG_CSTS , ctrl -> csts );
1975
- nvmet_pci_epf_bar_write32 (ctrl , NVME_REG_CC , ctrl -> cc );
1999
+
2000
+ nvmet_pci_epf_clear_ctrl_config (ctrl );
1976
2001
}
1977
2002
1978
2003
static int nvmet_pci_epf_create_ctrl (struct nvmet_pci_epf * nvme_epf ,
@@ -2070,14 +2095,22 @@ static int nvmet_pci_epf_create_ctrl(struct nvmet_pci_epf *nvme_epf,
2070
2095
2071
2096
static void nvmet_pci_epf_start_ctrl (struct nvmet_pci_epf_ctrl * ctrl )
2072
2097
{
2098
+
2099
+ dev_info (ctrl -> dev , "PCI link up\n" );
2100
+ ctrl -> link_up = true;
2101
+
2073
2102
schedule_delayed_work (& ctrl -> poll_cc , NVMET_PCI_EPF_CC_POLL_INTERVAL );
2074
2103
}
2075
2104
2076
2105
static void nvmet_pci_epf_stop_ctrl (struct nvmet_pci_epf_ctrl * ctrl )
2077
2106
{
2107
+ dev_info (ctrl -> dev , "PCI link down\n" );
2108
+ ctrl -> link_up = false;
2109
+
2078
2110
cancel_delayed_work_sync (& ctrl -> poll_cc );
2079
2111
2080
- nvmet_pci_epf_disable_ctrl (ctrl );
2112
+ nvmet_pci_epf_disable_ctrl (ctrl , false);
2113
+ nvmet_pci_epf_clear_ctrl_config (ctrl );
2081
2114
}
2082
2115
2083
2116
static void nvmet_pci_epf_destroy_ctrl (struct nvmet_pci_epf_ctrl * ctrl )
@@ -2300,10 +2333,8 @@ static int nvmet_pci_epf_epc_init(struct pci_epf *epf)
2300
2333
if (ret )
2301
2334
goto out_clear_bar ;
2302
2335
2303
- if (!epc_features -> linkup_notifier ) {
2304
- ctrl -> link_up = true;
2336
+ if (!epc_features -> linkup_notifier )
2305
2337
nvmet_pci_epf_start_ctrl (& nvme_epf -> ctrl );
2306
- }
2307
2338
2308
2339
return 0 ;
2309
2340
@@ -2319,7 +2350,6 @@ static void nvmet_pci_epf_epc_deinit(struct pci_epf *epf)
2319
2350
struct nvmet_pci_epf * nvme_epf = epf_get_drvdata (epf );
2320
2351
struct nvmet_pci_epf_ctrl * ctrl = & nvme_epf -> ctrl ;
2321
2352
2322
- ctrl -> link_up = false;
2323
2353
nvmet_pci_epf_destroy_ctrl (ctrl );
2324
2354
2325
2355
nvmet_pci_epf_deinit_dma (nvme_epf );
@@ -2331,7 +2361,6 @@ static int nvmet_pci_epf_link_up(struct pci_epf *epf)
2331
2361
struct nvmet_pci_epf * nvme_epf = epf_get_drvdata (epf );
2332
2362
struct nvmet_pci_epf_ctrl * ctrl = & nvme_epf -> ctrl ;
2333
2363
2334
- ctrl -> link_up = true;
2335
2364
nvmet_pci_epf_start_ctrl (ctrl );
2336
2365
2337
2366
return 0 ;
@@ -2342,7 +2371,6 @@ static int nvmet_pci_epf_link_down(struct pci_epf *epf)
2342
2371
struct nvmet_pci_epf * nvme_epf = epf_get_drvdata (epf );
2343
2372
struct nvmet_pci_epf_ctrl * ctrl = & nvme_epf -> ctrl ;
2344
2373
2345
- ctrl -> link_up = false;
2346
2374
nvmet_pci_epf_stop_ctrl (ctrl );
2347
2375
2348
2376
return 0 ;
0 commit comments