pm.c 9.0 KB

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