0005-suspend.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. From 393a9f9995f490d4b17ef4713aa370111b67efeb Mon Sep 17 00:00:00 2001
  2. From: kitakar5525 <34676735+kitakar5525@users.noreply.github.com>
  3. Date: Sat, 28 Sep 2019 17:48:21 +0200
  4. Subject: [PATCH] nvme: Backport changes for suspend
  5. Backported commits are:
  6. - torvalds/linux@4eaefe8c621c6195c91044396ed8060c179f7aae
  7. (nvme-pci: Allow PCI bus-level PM to be used if ASPM is disabled)
  8. - torvalds/linux@accd2dd72c8f087441d725dd916688171519e4e6
  9. (PCI/ASPM: Add pcie_aspm_enabled())
  10. - torvalds/linux@d916b1be94b6dc8d293abed2451f3062f6af7551
  11. (nvme-pci: use host managed power state for suspend)
  12. - torvalds/linux@1a87ee657c530bb2f3e39e4ac184d48f5f959cda
  13. (nvme: export get and set features)
  14. - torvalds/linux@d6135c3a1ec0cddda7b8b8e1b5b4abeeafd98289
  15. (nvme-pci: Sync queues on reset)
  16. Patchset: suspend
  17. ---
  18. drivers/nvme/host/core.c | 36 ++++++++++++--
  19. drivers/nvme/host/nvme.h | 7 +++
  20. drivers/nvme/host/pci.c | 103 +++++++++++++++++++++++++++++++++++++--
  21. drivers/pci/pcie/aspm.c | 20 ++++++++
  22. include/linux/pci.h | 2 +
  23. 5 files changed, 162 insertions(+), 6 deletions(-)
  24. diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
  25. index e64310f2296f..2a46419c5820 100644
  26. --- a/drivers/nvme/host/core.c
  27. +++ b/drivers/nvme/host/core.c
  28. @@ -1065,15 +1065,15 @@ static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
  29. return id;
  30. }
  31. -static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
  32. - void *buffer, size_t buflen, u32 *result)
  33. +static int nvme_features(struct nvme_ctrl *dev, u8 op, unsigned int fid,
  34. + unsigned int dword11, void *buffer, size_t buflen, u32 *result)
  35. {
  36. union nvme_result res = { 0 };
  37. struct nvme_command c;
  38. int ret;
  39. memset(&c, 0, sizeof(c));
  40. - c.features.opcode = nvme_admin_set_features;
  41. + c.features.opcode = op;
  42. c.features.fid = cpu_to_le32(fid);
  43. c.features.dword11 = cpu_to_le32(dword11);
  44. @@ -1084,6 +1084,24 @@ static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword
  45. return ret;
  46. }
  47. +int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid,
  48. + unsigned int dword11, void *buffer, size_t buflen,
  49. + u32 *result)
  50. +{
  51. + return nvme_features(dev, nvme_admin_set_features, fid, dword11, buffer,
  52. + buflen, result);
  53. +}
  54. +EXPORT_SYMBOL_GPL(nvme_set_features);
  55. +
  56. +int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid,
  57. + unsigned int dword11, void *buffer, size_t buflen,
  58. + u32 *result)
  59. +{
  60. + return nvme_features(dev, nvme_admin_get_features, fid, dword11, buffer,
  61. + buflen, result);
  62. +}
  63. +EXPORT_SYMBOL_GPL(nvme_get_features);
  64. +
  65. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
  66. {
  67. u32 q_count = (*count - 1) | ((*count - 1) << 16);
  68. @@ -3630,6 +3648,18 @@ static void nvme_free_ctrl(struct device *dev)
  69. nvme_put_subsystem(subsys);
  70. }
  71. +
  72. +void nvme_sync_queues(struct nvme_ctrl *ctrl)
  73. +{
  74. + struct nvme_ns *ns;
  75. +
  76. + down_read(&ctrl->namespaces_rwsem);
  77. + list_for_each_entry(ns, &ctrl->namespaces, list)
  78. + blk_sync_queue(ns->queue);
  79. + up_read(&ctrl->namespaces_rwsem);
  80. +}
  81. +EXPORT_SYMBOL_GPL(nvme_sync_queues);
  82. +
  83. /*
  84. * Initialize a NVMe controller structures. This needs to be called during
  85. * earliest initialization so that we have the initialized structured around
  86. diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
  87. index 276975506709..29e062d29c04 100644
  88. --- a/drivers/nvme/host/nvme.h
  89. +++ b/drivers/nvme/host/nvme.h
  90. @@ -437,6 +437,7 @@ void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
  91. void nvme_stop_queues(struct nvme_ctrl *ctrl);
  92. void nvme_start_queues(struct nvme_ctrl *ctrl);
  93. void nvme_kill_queues(struct nvme_ctrl *ctrl);
  94. +void nvme_sync_queues(struct nvme_ctrl *ctrl);
  95. void nvme_unfreeze(struct nvme_ctrl *ctrl);
  96. void nvme_wait_freeze(struct nvme_ctrl *ctrl);
  97. void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout);
  98. @@ -454,6 +455,12 @@ int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  99. union nvme_result *result, void *buffer, unsigned bufflen,
  100. unsigned timeout, int qid, int at_head,
  101. blk_mq_req_flags_t flags);
  102. +int nvme_set_features(struct nvme_ctrl *dev, unsigned int fid,
  103. + unsigned int dword11, void *buffer, size_t buflen,
  104. + u32 *result);
  105. +int nvme_get_features(struct nvme_ctrl *dev, unsigned int fid,
  106. + unsigned int dword11, void *buffer, size_t buflen,
  107. + u32 *result);
  108. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count);
  109. void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
  110. int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
  111. diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
  112. index d7cf3202cdd3..bdafe953c9a9 100644
  113. --- a/drivers/nvme/host/pci.c
  114. +++ b/drivers/nvme/host/pci.c
  115. @@ -26,6 +26,7 @@
  116. #include <linux/mutex.h>
  117. #include <linux/once.h>
  118. #include <linux/pci.h>
  119. +#include <linux/suspend.h>
  120. #include <linux/t10-pi.h>
  121. #include <linux/types.h>
  122. #include <linux/io-64-nonatomic-lo-hi.h>
  123. @@ -106,6 +107,7 @@ struct nvme_dev {
  124. u32 cmbloc;
  125. struct nvme_ctrl ctrl;
  126. struct completion ioq_wait;
  127. + u32 last_ps;
  128. mempool_t *iod_mempool;
  129. @@ -2286,6 +2288,7 @@ static void nvme_reset_work(struct work_struct *work)
  130. */
  131. if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
  132. nvme_dev_disable(dev, false);
  133. + nvme_sync_queues(&dev->ctrl);
  134. mutex_lock(&dev->shutdown_lock);
  135. result = nvme_pci_enable(dev);
  136. @@ -2624,16 +2627,101 @@ static void nvme_remove(struct pci_dev *pdev)
  137. }
  138. #ifdef CONFIG_PM_SLEEP
  139. +static int nvme_get_power_state(struct nvme_ctrl *ctrl, u32 *ps)
  140. +{
  141. + return nvme_get_features(ctrl, NVME_FEAT_POWER_MGMT, 0, NULL, 0, ps);
  142. +}
  143. +
  144. +static int nvme_set_power_state(struct nvme_ctrl *ctrl, u32 ps)
  145. +{
  146. + return nvme_set_features(ctrl, NVME_FEAT_POWER_MGMT, ps, NULL, 0, NULL);
  147. +}
  148. +
  149. +static int nvme_resume(struct device *dev)
  150. +{
  151. + struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
  152. + struct nvme_ctrl *ctrl = &ndev->ctrl;
  153. +
  154. + if (ndev->last_ps == U32_MAX ||
  155. + nvme_set_power_state(ctrl, ndev->last_ps) != 0)
  156. + nvme_reset_ctrl(ctrl);
  157. + return 0;
  158. +}
  159. +
  160. static int nvme_suspend(struct device *dev)
  161. {
  162. struct pci_dev *pdev = to_pci_dev(dev);
  163. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  164. + struct nvme_ctrl *ctrl = &ndev->ctrl;
  165. + int ret = -EBUSY;
  166. +
  167. + ndev->last_ps = U32_MAX;
  168. +
  169. + /*
  170. + * The platform does not remove power for a kernel managed suspend so
  171. + * use host managed nvme power settings for lowest idle power if
  172. + * possible. This should have quicker resume latency than a full device
  173. + * shutdown. But if the firmware is involved after the suspend or the
  174. + * device does not support any non-default power states, shut down the
  175. + * device fully.
  176. + *
  177. + * If ASPM is not enabled for the device, shut down the device and allow
  178. + * the PCI bus layer to put it into D3 in order to take the PCIe link
  179. + * down, so as to allow the platform to achieve its minimum low-power
  180. + * state (which may not be possible if the link is up).
  181. + */
  182. + if (pm_suspend_via_firmware() || !ctrl->npss ||
  183. + !pcie_aspm_enabled(pdev)) {
  184. + nvme_dev_disable(ndev, true);
  185. + return 0;
  186. + }
  187. +
  188. + nvme_start_freeze(ctrl);
  189. + nvme_wait_freeze(ctrl);
  190. + nvme_sync_queues(ctrl);
  191. +
  192. + if (ctrl->state != NVME_CTRL_LIVE &&
  193. + ctrl->state != NVME_CTRL_ADMIN_ONLY)
  194. + goto unfreeze;
  195. +
  196. + ret = nvme_get_power_state(ctrl, &ndev->last_ps);
  197. + if (ret < 0)
  198. + goto unfreeze;
  199. +
  200. + ret = nvme_set_power_state(ctrl, ctrl->npss);
  201. + if (ret < 0)
  202. + goto unfreeze;
  203. +
  204. + if (ret) {
  205. + /*
  206. + * Clearing npss forces a controller reset on resume. The
  207. + * correct value will be resdicovered then.
  208. + */
  209. + nvme_dev_disable(ndev, true);
  210. + ctrl->npss = 0;
  211. + ret = 0;
  212. + goto unfreeze;
  213. + }
  214. + /*
  215. + * A saved state prevents pci pm from generically controlling the
  216. + * device's power. If we're using protocol specific settings, we don't
  217. + * want pci interfering.
  218. + */
  219. + pci_save_state(pdev);
  220. +unfreeze:
  221. + nvme_unfreeze(ctrl);
  222. + return ret;
  223. +}
  224. +
  225. +static int nvme_simple_suspend(struct device *dev)
  226. +{
  227. + struct nvme_dev *ndev = pci_get_drvdata(to_pci_dev(dev));
  228. nvme_dev_disable(ndev, true);
  229. return 0;
  230. }
  231. -static int nvme_resume(struct device *dev)
  232. +static int nvme_simple_resume(struct device *dev)
  233. {
  234. struct pci_dev *pdev = to_pci_dev(dev);
  235. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  236. @@ -2641,9 +2729,16 @@ static int nvme_resume(struct device *dev)
  237. nvme_reset_ctrl(&ndev->ctrl);
  238. return 0;
  239. }
  240. -#endif
  241. -static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
  242. +const struct dev_pm_ops nvme_dev_pm_ops = {
  243. + .suspend = nvme_suspend,
  244. + .resume = nvme_resume,
  245. + .freeze = nvme_simple_suspend,
  246. + .thaw = nvme_simple_resume,
  247. + .poweroff = nvme_simple_suspend,
  248. + .restore = nvme_simple_resume,
  249. +};
  250. +#endif /* CONFIG_PM_SLEEP */
  251. static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
  252. pci_channel_state_t state)
  253. @@ -2748,9 +2843,11 @@ static struct pci_driver nvme_driver = {
  254. .probe = nvme_probe,
  255. .remove = nvme_remove,
  256. .shutdown = nvme_shutdown,
  257. +#ifdef CONFIG_PM_SLEEP
  258. .driver = {
  259. .pm = &nvme_dev_pm_ops,
  260. },
  261. +#endif
  262. .sriov_configure = pci_sriov_configure_simple,
  263. .err_handler = &nvme_err_handler,
  264. };
  265. diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
  266. index 279f9f0197b0..cb474338f39d 100644
  267. --- a/drivers/pci/pcie/aspm.c
  268. +++ b/drivers/pci/pcie/aspm.c
  269. @@ -1171,6 +1171,26 @@ static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp)
  270. module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
  271. NULL, 0644);
  272. +/**
  273. + * pcie_aspm_enabled - Check if PCIe ASPM has been enabled for a device.
  274. + * @pdev: Target device.
  275. + */
  276. +bool pcie_aspm_enabled(struct pci_dev *pdev)
  277. +{
  278. + struct pci_dev *bridge = pci_upstream_bridge(pdev);
  279. + bool ret;
  280. +
  281. + if (!bridge)
  282. + return false;
  283. +
  284. + mutex_lock(&aspm_lock);
  285. + ret = bridge->link_state ? !!bridge->link_state->aspm_enabled : false;
  286. + mutex_unlock(&aspm_lock);
  287. +
  288. + return ret;
  289. +}
  290. +EXPORT_SYMBOL_GPL(pcie_aspm_enabled);
  291. +
  292. #ifdef CONFIG_PCIEASPM_DEBUG
  293. static ssize_t link_state_show(struct device *dev,
  294. struct device_attribute *attr,
  295. diff --git a/include/linux/pci.h b/include/linux/pci.h
  296. index 2fda9893962d..ec6c48ecd7d5 100644
  297. --- a/include/linux/pci.h
  298. +++ b/include/linux/pci.h
  299. @@ -1488,8 +1488,10 @@ extern bool pcie_ports_native;
  300. #ifdef CONFIG_PCIEASPM
  301. bool pcie_aspm_support_enabled(void);
  302. +bool pcie_aspm_enabled(struct pci_dev *pdev);
  303. #else
  304. static inline bool pcie_aspm_support_enabled(void) { return false; }
  305. +static inline bool pcie_aspm_enabled(struct pci_dev *pdev) { return false; }
  306. #endif
  307. #ifdef CONFIG_PCIEAER
  308. --
  309. 2.33.0