pm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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(unsigned long arg)
  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. setup_timer(&pm->stay_awake, cw1200_pm_stay_awake_tmo,
  92. (unsigned long)pm);
  93. return 0;
  94. }
  95. void cw1200_pm_deinit(struct cw1200_pm_state *pm)
  96. {
  97. del_timer_sync(&pm->stay_awake);
  98. }
  99. void cw1200_pm_stay_awake(struct cw1200_pm_state *pm,
  100. unsigned long tmo)
  101. {
  102. long cur_tmo;
  103. spin_lock_bh(&pm->lock);
  104. cur_tmo = pm->stay_awake.expires - jiffies;
  105. if (!timer_pending(&pm->stay_awake) || cur_tmo < (long)tmo)
  106. mod_timer(&pm->stay_awake, jiffies + tmo);
  107. spin_unlock_bh(&pm->lock);
  108. }
  109. static long cw1200_suspend_work(struct delayed_work *work)
  110. {
  111. int ret = cancel_delayed_work(work);
  112. long tmo;
  113. if (ret > 0) {
  114. /* Timer is pending */
  115. tmo = work->timer.expires - jiffies;
  116. if (tmo < 0)
  117. tmo = 0;
  118. } else {
  119. tmo = -1;
  120. }
  121. return tmo;
  122. }
  123. static int cw1200_resume_work(struct cw1200_common *priv,
  124. struct delayed_work *work,
  125. unsigned long tmo)
  126. {
  127. if ((long)tmo < 0)
  128. return 1;
  129. return queue_delayed_work(priv->workqueue, work, tmo);
  130. }
  131. int cw1200_can_suspend(struct cw1200_common *priv)
  132. {
  133. if (atomic_read(&priv->bh_rx)) {
  134. wiphy_dbg(priv->hw->wiphy, "Suspend interrupted.\n");
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. EXPORT_SYMBOL_GPL(cw1200_can_suspend);
  140. int cw1200_wow_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  141. {
  142. struct cw1200_common *priv = hw->priv;
  143. struct cw1200_pm_state *pm_state = &priv->pm_state;
  144. struct cw1200_suspend_state *state;
  145. int ret;
  146. spin_lock_bh(&pm_state->lock);
  147. ret = timer_pending(&pm_state->stay_awake);
  148. spin_unlock_bh(&pm_state->lock);
  149. if (ret)
  150. return -EAGAIN;
  151. /* Do not suspend when datapath is not idle */
  152. if (priv->tx_queue_stats.num_queued)
  153. return -EBUSY;
  154. /* Make sure there is no configuration requests in progress. */
  155. if (!mutex_trylock(&priv->conf_mutex))
  156. return -EBUSY;
  157. /* Ensure pending operations are done.
  158. * Note also that wow_suspend must return in ~2.5sec, before
  159. * watchdog is triggered.
  160. */
  161. if (priv->channel_switch_in_progress)
  162. goto revert1;
  163. /* Do not suspend when join is pending */
  164. if (priv->join_pending)
  165. goto revert1;
  166. /* Do not suspend when scanning */
  167. if (down_trylock(&priv->scan.lock))
  168. goto revert1;
  169. /* Lock TX. */
  170. wsm_lock_tx_async(priv);
  171. /* Wait to avoid possible race with bh code.
  172. * But do not wait too long...
  173. */
  174. if (wait_event_timeout(priv->bh_evt_wq,
  175. !priv->hw_bufs_used, HZ / 10) <= 0)
  176. goto revert2;
  177. /* Set UDP filter */
  178. wsm_set_udp_port_filter(priv, &cw1200_udp_port_filter_on.hdr);
  179. /* Set ethernet frame type filter */
  180. wsm_set_ether_type_filter(priv, &cw1200_ether_type_filter_on.hdr);
  181. /* Allocate state */
  182. state = kzalloc(sizeof(struct cw1200_suspend_state), GFP_KERNEL);
  183. if (!state)
  184. goto revert3;
  185. /* Change to legacy PS while going to suspend */
  186. if (!priv->vif->p2p &&
  187. priv->join_status == CW1200_JOIN_STATUS_STA &&
  188. priv->powersave_mode.mode != WSM_PSM_PS) {
  189. state->prev_ps_mode = priv->powersave_mode.mode;
  190. priv->powersave_mode.mode = WSM_PSM_PS;
  191. cw1200_set_pm(priv, &priv->powersave_mode);
  192. if (wait_event_interruptible_timeout(priv->ps_mode_switch_done,
  193. !priv->ps_mode_switch_in_progress, 1*HZ) <= 0) {
  194. goto revert4;
  195. }
  196. }
  197. /* Store delayed work states. */
  198. state->bss_loss_tmo =
  199. cw1200_suspend_work(&priv->bss_loss_work);
  200. state->join_tmo =
  201. cw1200_suspend_work(&priv->join_timeout);
  202. state->direct_probe =
  203. cw1200_suspend_work(&priv->scan.probe_work);
  204. state->link_id_gc =
  205. cw1200_suspend_work(&priv->link_id_gc_work);
  206. cancel_delayed_work_sync(&priv->clear_recent_scan_work);
  207. atomic_set(&priv->recent_scan, 0);
  208. /* Enable beacon skipping */
  209. if (priv->join_status == CW1200_JOIN_STATUS_STA &&
  210. priv->join_dtim_period &&
  211. !priv->has_multicast_subscription) {
  212. state->beacon_skipping = true;
  213. wsm_set_beacon_wakeup_period(priv,
  214. priv->join_dtim_period,
  215. CW1200_BEACON_SKIPPING_MULTIPLIER * priv->join_dtim_period);
  216. }
  217. /* Stop serving thread */
  218. if (cw1200_bh_suspend(priv))
  219. goto revert5;
  220. ret = timer_pending(&priv->mcast_timeout);
  221. if (ret)
  222. goto revert6;
  223. /* Store suspend state */
  224. pm_state->suspend_state = state;
  225. /* Enable IRQ wake */
  226. ret = priv->hwbus_ops->power_mgmt(priv->hwbus_priv, true);
  227. if (ret) {
  228. wiphy_err(priv->hw->wiphy,
  229. "PM request failed: %d. WoW is disabled.\n", ret);
  230. cw1200_wow_resume(hw);
  231. return -EBUSY;
  232. }
  233. /* Force resume if event is coming from the device. */
  234. if (atomic_read(&priv->bh_rx)) {
  235. cw1200_wow_resume(hw);
  236. return -EAGAIN;
  237. }
  238. return 0;
  239. revert6:
  240. WARN_ON(cw1200_bh_resume(priv));
  241. revert5:
  242. cw1200_resume_work(priv, &priv->bss_loss_work,
  243. state->bss_loss_tmo);
  244. cw1200_resume_work(priv, &priv->join_timeout,
  245. state->join_tmo);
  246. cw1200_resume_work(priv, &priv->scan.probe_work,
  247. state->direct_probe);
  248. cw1200_resume_work(priv, &priv->link_id_gc_work,
  249. state->link_id_gc);
  250. revert4:
  251. kfree(state);
  252. revert3:
  253. wsm_set_udp_port_filter(priv, &cw1200_udp_port_filter_off);
  254. wsm_set_ether_type_filter(priv, &cw1200_ether_type_filter_off);
  255. revert2:
  256. wsm_unlock_tx(priv);
  257. up(&priv->scan.lock);
  258. revert1:
  259. mutex_unlock(&priv->conf_mutex);
  260. return -EBUSY;
  261. }
  262. int cw1200_wow_resume(struct ieee80211_hw *hw)
  263. {
  264. struct cw1200_common *priv = hw->priv;
  265. struct cw1200_pm_state *pm_state = &priv->pm_state;
  266. struct cw1200_suspend_state *state;
  267. state = pm_state->suspend_state;
  268. pm_state->suspend_state = NULL;
  269. /* Disable IRQ wake */
  270. priv->hwbus_ops->power_mgmt(priv->hwbus_priv, false);
  271. /* Scan.lock must be released before BH is resumed other way
  272. * in case when BSS_LOST command arrived the processing of the
  273. * command will be delayed.
  274. */
  275. up(&priv->scan.lock);
  276. /* Resume BH thread */
  277. WARN_ON(cw1200_bh_resume(priv));
  278. /* Restores previous PS mode */
  279. if (!priv->vif->p2p && priv->join_status == CW1200_JOIN_STATUS_STA) {
  280. priv->powersave_mode.mode = state->prev_ps_mode;
  281. cw1200_set_pm(priv, &priv->powersave_mode);
  282. }
  283. if (state->beacon_skipping) {
  284. wsm_set_beacon_wakeup_period(priv, priv->beacon_int *
  285. priv->join_dtim_period >
  286. MAX_BEACON_SKIP_TIME_MS ? 1 :
  287. priv->join_dtim_period, 0);
  288. state->beacon_skipping = false;
  289. }
  290. /* Resume delayed work */
  291. cw1200_resume_work(priv, &priv->bss_loss_work,
  292. state->bss_loss_tmo);
  293. cw1200_resume_work(priv, &priv->join_timeout,
  294. state->join_tmo);
  295. cw1200_resume_work(priv, &priv->scan.probe_work,
  296. state->direct_probe);
  297. cw1200_resume_work(priv, &priv->link_id_gc_work,
  298. state->link_id_gc);
  299. /* Remove UDP port filter */
  300. wsm_set_udp_port_filter(priv, &cw1200_udp_port_filter_off);
  301. /* Remove ethernet frame type filter */
  302. wsm_set_ether_type_filter(priv, &cw1200_ether_type_filter_off);
  303. /* Unlock datapath */
  304. wsm_unlock_tx(priv);
  305. /* Unlock configuration mutex */
  306. mutex_unlock(&priv->conf_mutex);
  307. /* Free memory */
  308. kfree(state);
  309. return 0;
  310. }