pm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Mac80211 power management API for ST-Ericsson CW1200 drivers
  4. *
  5. * Copyright (c) 2011, ST-Ericsson
  6. * Author: Dmitry Tarnyagin <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/if_ether.h>
  10. #include "cw1200.h"
  11. #include "pm.h"
  12. #include "sta.h"
  13. #include "bh.h"
  14. #include "hwbus.h"
  15. #define CW1200_BEACON_SKIPPING_MULTIPLIER 3
  16. struct cw1200_udp_port_filter {
  17. struct wsm_udp_port_filter_hdr hdr;
  18. /* Up to 4 filters are allowed. */
  19. struct wsm_udp_port_filter filters[WSM_MAX_FILTER_ELEMENTS];
  20. } __packed;
  21. struct cw1200_ether_type_filter {
  22. struct wsm_ether_type_filter_hdr hdr;
  23. /* Up to 4 filters are allowed. */
  24. struct wsm_ether_type_filter filters[WSM_MAX_FILTER_ELEMENTS];
  25. } __packed;
  26. static struct cw1200_udp_port_filter cw1200_udp_port_filter_on = {
  27. .hdr.num = 2,
  28. .filters = {
  29. [0] = {
  30. .action = WSM_FILTER_ACTION_FILTER_OUT,
  31. .type = WSM_FILTER_PORT_TYPE_DST,
  32. .port = __cpu_to_le16(67), /* DHCP Bootps */
  33. },
  34. [1] = {
  35. .action = WSM_FILTER_ACTION_FILTER_OUT,
  36. .type = WSM_FILTER_PORT_TYPE_DST,
  37. .port = __cpu_to_le16(68), /* DHCP Bootpc */
  38. },
  39. }
  40. };
  41. static struct wsm_udp_port_filter_hdr cw1200_udp_port_filter_off = {
  42. .num = 0,
  43. };
  44. #ifndef ETH_P_WAPI
  45. #define ETH_P_WAPI 0x88B4
  46. #endif
  47. static struct cw1200_ether_type_filter cw1200_ether_type_filter_on = {
  48. .hdr.num = 4,
  49. .filters = {
  50. [0] = {
  51. .action = WSM_FILTER_ACTION_FILTER_IN,
  52. .type = __cpu_to_le16(ETH_P_IP),
  53. },
  54. [1] = {
  55. .action = WSM_FILTER_ACTION_FILTER_IN,
  56. .type = __cpu_to_le16(ETH_P_PAE),
  57. },
  58. [2] = {
  59. .action = WSM_FILTER_ACTION_FILTER_IN,
  60. .type = __cpu_to_le16(ETH_P_WAPI),
  61. },
  62. [3] = {
  63. .action = WSM_FILTER_ACTION_FILTER_IN,
  64. .type = __cpu_to_le16(ETH_P_ARP),
  65. },
  66. },
  67. };
  68. static struct wsm_ether_type_filter_hdr cw1200_ether_type_filter_off = {
  69. .num = 0,
  70. };
  71. /* private */
  72. struct cw1200_suspend_state {
  73. unsigned long bss_loss_tmo;
  74. unsigned long join_tmo;
  75. unsigned long direct_probe;
  76. unsigned long link_id_gc;
  77. bool beacon_skipping;
  78. u8 prev_ps_mode;
  79. };
  80. static void cw1200_pm_stay_awake_tmo(struct timer_list *unused)
  81. {
  82. /* XXX what's the point of this ? */
  83. }
  84. int cw1200_pm_init(struct cw1200_pm_state *pm,
  85. struct cw1200_common *priv)
  86. {
  87. spin_lock_init(&pm->lock);
  88. timer_setup(&pm->stay_awake, cw1200_pm_stay_awake_tmo, 0);
  89. return 0;
  90. }
  91. void cw1200_pm_deinit(struct cw1200_pm_state *pm)
  92. {
  93. del_timer_sync(&pm->stay_awake);
  94. }
  95. void cw1200_pm_stay_awake(struct cw1200_pm_state *pm,
  96. unsigned long tmo)
  97. {
  98. long cur_tmo;
  99. spin_lock_bh(&pm->lock);
  100. cur_tmo = pm->stay_awake.expires - jiffies;
  101. if (!timer_pending(&pm->stay_awake) || cur_tmo < (long)tmo)
  102. mod_timer(&pm->stay_awake, jiffies + tmo);
  103. spin_unlock_bh(&pm->lock);
  104. }
  105. static long cw1200_suspend_work(struct delayed_work *work)
  106. {
  107. int ret = cancel_delayed_work(work);
  108. long tmo;
  109. if (ret > 0) {
  110. /* Timer is pending */
  111. tmo = work->timer.expires - jiffies;
  112. if (tmo < 0)
  113. tmo = 0;
  114. } else {
  115. tmo = -1;
  116. }
  117. return tmo;
  118. }
  119. static int cw1200_resume_work(struct cw1200_common *priv,
  120. struct delayed_work *work,
  121. unsigned long tmo)
  122. {
  123. if ((long)tmo < 0)
  124. return 1;
  125. return queue_delayed_work(priv->workqueue, work, tmo);
  126. }
  127. int cw1200_can_suspend(struct cw1200_common *priv)
  128. {
  129. if (atomic_read(&priv->bh_rx)) {
  130. wiphy_dbg(priv->hw->wiphy, "Suspend interrupted.\n");
  131. return 0;
  132. }
  133. return 1;
  134. }
  135. EXPORT_SYMBOL_GPL(cw1200_can_suspend);
  136. int cw1200_wow_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  137. {
  138. struct cw1200_common *priv = hw->priv;
  139. struct cw1200_pm_state *pm_state = &priv->pm_state;
  140. struct cw1200_suspend_state *state;
  141. int ret;
  142. spin_lock_bh(&pm_state->lock);
  143. ret = timer_pending(&pm_state->stay_awake);
  144. spin_unlock_bh(&pm_state->lock);
  145. if (ret)
  146. return -EAGAIN;
  147. /* Do not suspend when datapath is not idle */
  148. if (priv->tx_queue_stats.num_queued)
  149. return -EBUSY;
  150. /* Make sure there is no configuration requests in progress. */
  151. if (!mutex_trylock(&priv->conf_mutex))
  152. return -EBUSY;
  153. /* Ensure pending operations are done.
  154. * Note also that wow_suspend must return in ~2.5sec, before
  155. * watchdog is triggered.
  156. */
  157. if (priv->channel_switch_in_progress)
  158. goto revert1;
  159. /* Do not suspend when join is pending */
  160. if (priv->join_pending)
  161. goto revert1;
  162. /* Do not suspend when scanning */
  163. if (down_trylock(&priv->scan.lock))
  164. goto revert1;
  165. /* Lock TX. */
  166. wsm_lock_tx_async(priv);
  167. /* Wait to avoid possible race with bh code.
  168. * But do not wait too long...
  169. */
  170. if (wait_event_timeout(priv->bh_evt_wq,
  171. !priv->hw_bufs_used, HZ / 10) <= 0)
  172. goto revert2;
  173. /* Set UDP filter */
  174. wsm_set_udp_port_filter(priv, &cw1200_udp_port_filter_on.hdr);
  175. /* Set ethernet frame type filter */
  176. wsm_set_ether_type_filter(priv, &cw1200_ether_type_filter_on.hdr);
  177. /* Allocate state */
  178. state = kzalloc(sizeof(struct cw1200_suspend_state), GFP_KERNEL);
  179. if (!state)
  180. goto revert3;
  181. /* Change to legacy PS while going to suspend */
  182. if (!priv->vif->p2p &&
  183. priv->join_status == CW1200_JOIN_STATUS_STA &&
  184. priv->powersave_mode.mode != WSM_PSM_PS) {
  185. state->prev_ps_mode = priv->powersave_mode.mode;
  186. priv->powersave_mode.mode = WSM_PSM_PS;
  187. cw1200_set_pm(priv, &priv->powersave_mode);
  188. if (wait_event_interruptible_timeout(priv->ps_mode_switch_done,
  189. !priv->ps_mode_switch_in_progress, 1*HZ) <= 0) {
  190. goto revert4;
  191. }
  192. }
  193. /* Store delayed work states. */
  194. state->bss_loss_tmo =
  195. cw1200_suspend_work(&priv->bss_loss_work);
  196. state->join_tmo =
  197. cw1200_suspend_work(&priv->join_timeout);
  198. state->direct_probe =
  199. cw1200_suspend_work(&priv->scan.probe_work);
  200. state->link_id_gc =
  201. cw1200_suspend_work(&priv->link_id_gc_work);
  202. cancel_delayed_work_sync(&priv->clear_recent_scan_work);
  203. atomic_set(&priv->recent_scan, 0);
  204. /* Enable beacon skipping */
  205. if (priv->join_status == CW1200_JOIN_STATUS_STA &&
  206. priv->join_dtim_period &&
  207. !priv->has_multicast_subscription) {
  208. state->beacon_skipping = true;
  209. wsm_set_beacon_wakeup_period(priv,
  210. priv->join_dtim_period,
  211. CW1200_BEACON_SKIPPING_MULTIPLIER * priv->join_dtim_period);
  212. }
  213. /* Stop serving thread */
  214. if (cw1200_bh_suspend(priv))
  215. goto revert5;
  216. ret = timer_pending(&priv->mcast_timeout);
  217. if (ret)
  218. goto revert6;
  219. /* Store suspend state */
  220. pm_state->suspend_state = state;
  221. /* Enable IRQ wake */
  222. ret = priv->hwbus_ops->power_mgmt(priv->hwbus_priv, true);
  223. if (ret) {
  224. wiphy_err(priv->hw->wiphy,
  225. "PM request failed: %d. WoW is disabled.\n", ret);
  226. cw1200_wow_resume(hw);
  227. return -EBUSY;
  228. }
  229. /* Force resume if event is coming from the device. */
  230. if (atomic_read(&priv->bh_rx)) {
  231. cw1200_wow_resume(hw);
  232. return -EAGAIN;
  233. }
  234. return 0;
  235. revert6:
  236. WARN_ON(cw1200_bh_resume(priv));
  237. revert5:
  238. cw1200_resume_work(priv, &priv->bss_loss_work,
  239. state->bss_loss_tmo);
  240. cw1200_resume_work(priv, &priv->join_timeout,
  241. state->join_tmo);
  242. cw1200_resume_work(priv, &priv->scan.probe_work,
  243. state->direct_probe);
  244. cw1200_resume_work(priv, &priv->link_id_gc_work,
  245. state->link_id_gc);
  246. revert4:
  247. kfree(state);
  248. revert3:
  249. wsm_set_udp_port_filter(priv, &cw1200_udp_port_filter_off);
  250. wsm_set_ether_type_filter(priv, &cw1200_ether_type_filter_off);
  251. revert2:
  252. wsm_unlock_tx(priv);
  253. up(&priv->scan.lock);
  254. revert1:
  255. mutex_unlock(&priv->conf_mutex);
  256. return -EBUSY;
  257. }
  258. int cw1200_wow_resume(struct ieee80211_hw *hw)
  259. {
  260. struct cw1200_common *priv = hw->priv;
  261. struct cw1200_pm_state *pm_state = &priv->pm_state;
  262. struct cw1200_suspend_state *state;
  263. state = pm_state->suspend_state;
  264. pm_state->suspend_state = NULL;
  265. /* Disable IRQ wake */
  266. priv->hwbus_ops->power_mgmt(priv->hwbus_priv, false);
  267. /* Scan.lock must be released before BH is resumed other way
  268. * in case when BSS_LOST command arrived the processing of the
  269. * command will be delayed.
  270. */
  271. up(&priv->scan.lock);
  272. /* Resume BH thread */
  273. WARN_ON(cw1200_bh_resume(priv));
  274. /* Restores previous PS mode */
  275. if (!priv->vif->p2p && priv->join_status == CW1200_JOIN_STATUS_STA) {
  276. priv->powersave_mode.mode = state->prev_ps_mode;
  277. cw1200_set_pm(priv, &priv->powersave_mode);
  278. }
  279. if (state->beacon_skipping) {
  280. wsm_set_beacon_wakeup_period(priv, priv->beacon_int *
  281. priv->join_dtim_period >
  282. MAX_BEACON_SKIP_TIME_MS ? 1 :
  283. priv->join_dtim_period, 0);
  284. state->beacon_skipping = false;
  285. }
  286. /* Resume delayed work */
  287. cw1200_resume_work(priv, &priv->bss_loss_work,
  288. state->bss_loss_tmo);
  289. cw1200_resume_work(priv, &priv->join_timeout,
  290. state->join_tmo);
  291. cw1200_resume_work(priv, &priv->scan.probe_work,
  292. state->direct_probe);
  293. cw1200_resume_work(priv, &priv->link_id_gc_work,
  294. state->link_id_gc);
  295. /* Remove UDP port filter */
  296. wsm_set_udp_port_filter(priv, &cw1200_udp_port_filter_off);
  297. /* Remove ethernet frame type filter */
  298. wsm_set_ether_type_filter(priv, &cw1200_ether_type_filter_off);
  299. /* Unlock datapath */
  300. wsm_unlock_tx(priv);
  301. /* Unlock configuration mutex */
  302. mutex_unlock(&priv->conf_mutex);
  303. /* Free memory */
  304. kfree(state);
  305. return 0;
  306. }