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. init_timer(&pm->stay_awake);
  92. pm->stay_awake.data = (unsigned long)pm;
  93. pm->stay_awake.function = cw1200_pm_stay_awake_tmo;
  94. return 0;
  95. }
  96. void cw1200_pm_deinit(struct cw1200_pm_state *pm)
  97. {
  98. del_timer_sync(&pm->stay_awake);
  99. }
  100. void cw1200_pm_stay_awake(struct cw1200_pm_state *pm,
  101. unsigned long tmo)
  102. {
  103. long cur_tmo;
  104. spin_lock_bh(&pm->lock);
  105. cur_tmo = pm->stay_awake.expires - jiffies;
  106. if (!timer_pending(&pm->stay_awake) || cur_tmo < (long)tmo)
  107. mod_timer(&pm->stay_awake, jiffies + tmo);
  108. spin_unlock_bh(&pm->lock);
  109. }
  110. static long cw1200_suspend_work(struct delayed_work *work)
  111. {
  112. int ret = cancel_delayed_work(work);
  113. long tmo;
  114. if (ret > 0) {
  115. /* Timer is pending */
  116. tmo = work->timer.expires - jiffies;
  117. if (tmo < 0)
  118. tmo = 0;
  119. } else {
  120. tmo = -1;
  121. }
  122. return tmo;
  123. }
  124. static int cw1200_resume_work(struct cw1200_common *priv,
  125. struct delayed_work *work,
  126. unsigned long tmo)
  127. {
  128. if ((long)tmo < 0)
  129. return 1;
  130. return queue_delayed_work(priv->workqueue, work, tmo);
  131. }
  132. int cw1200_can_suspend(struct cw1200_common *priv)
  133. {
  134. if (atomic_read(&priv->bh_rx)) {
  135. wiphy_dbg(priv->hw->wiphy, "Suspend interrupted.\n");
  136. return 0;
  137. }
  138. return 1;
  139. }
  140. EXPORT_SYMBOL_GPL(cw1200_can_suspend);
  141. int cw1200_wow_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  142. {
  143. struct cw1200_common *priv = hw->priv;
  144. struct cw1200_pm_state *pm_state = &priv->pm_state;
  145. struct cw1200_suspend_state *state;
  146. int ret;
  147. spin_lock_bh(&pm_state->lock);
  148. ret = timer_pending(&pm_state->stay_awake);
  149. spin_unlock_bh(&pm_state->lock);
  150. if (ret)
  151. return -EAGAIN;
  152. /* Do not suspend when datapath is not idle */
  153. if (priv->tx_queue_stats.num_queued)
  154. return -EBUSY;
  155. /* Make sure there is no configuration requests in progress. */
  156. if (!mutex_trylock(&priv->conf_mutex))
  157. return -EBUSY;
  158. /* Ensure pending operations are done.
  159. * Note also that wow_suspend must return in ~2.5sec, before
  160. * watchdog is triggered.
  161. */
  162. if (priv->channel_switch_in_progress)
  163. goto revert1;
  164. /* Do not suspend when join is pending */
  165. if (priv->join_pending)
  166. goto revert1;
  167. /* Do not suspend when scanning */
  168. if (down_trylock(&priv->scan.lock))
  169. goto revert1;
  170. /* Lock TX. */
  171. wsm_lock_tx_async(priv);
  172. /* Wait to avoid possible race with bh code.
  173. * But do not wait too long...
  174. */
  175. if (wait_event_timeout(priv->bh_evt_wq,
  176. !priv->hw_bufs_used, HZ / 10) <= 0)
  177. goto revert2;
  178. /* Set UDP filter */
  179. wsm_set_udp_port_filter(priv, &cw1200_udp_port_filter_on.hdr);
  180. /* Set ethernet frame type filter */
  181. wsm_set_ether_type_filter(priv, &cw1200_ether_type_filter_on.hdr);
  182. /* Allocate state */
  183. state = kzalloc(sizeof(struct cw1200_suspend_state), GFP_KERNEL);
  184. if (!state)
  185. goto revert3;
  186. /* Change to legacy PS while going to suspend */
  187. if (!priv->vif->p2p &&
  188. priv->join_status == CW1200_JOIN_STATUS_STA &&
  189. priv->powersave_mode.mode != WSM_PSM_PS) {
  190. state->prev_ps_mode = priv->powersave_mode.mode;
  191. priv->powersave_mode.mode = WSM_PSM_PS;
  192. cw1200_set_pm(priv, &priv->powersave_mode);
  193. if (wait_event_interruptible_timeout(priv->ps_mode_switch_done,
  194. !priv->ps_mode_switch_in_progress, 1*HZ) <= 0) {
  195. goto revert3;
  196. }
  197. }
  198. /* Store delayed work states. */
  199. state->bss_loss_tmo =
  200. cw1200_suspend_work(&priv->bss_loss_work);
  201. state->join_tmo =
  202. cw1200_suspend_work(&priv->join_timeout);
  203. state->direct_probe =
  204. cw1200_suspend_work(&priv->scan.probe_work);
  205. state->link_id_gc =
  206. cw1200_suspend_work(&priv->link_id_gc_work);
  207. cancel_delayed_work_sync(&priv->clear_recent_scan_work);
  208. atomic_set(&priv->recent_scan, 0);
  209. /* Enable beacon skipping */
  210. if (priv->join_status == CW1200_JOIN_STATUS_STA &&
  211. priv->join_dtim_period &&
  212. !priv->has_multicast_subscription) {
  213. state->beacon_skipping = true;
  214. wsm_set_beacon_wakeup_period(priv,
  215. priv->join_dtim_period,
  216. CW1200_BEACON_SKIPPING_MULTIPLIER * priv->join_dtim_period);
  217. }
  218. /* Stop serving thread */
  219. if (cw1200_bh_suspend(priv))
  220. goto revert4;
  221. ret = timer_pending(&priv->mcast_timeout);
  222. if (ret)
  223. goto revert5;
  224. /* Store suspend state */
  225. pm_state->suspend_state = state;
  226. /* Enable IRQ wake */
  227. ret = priv->hwbus_ops->power_mgmt(priv->hwbus_priv, true);
  228. if (ret) {
  229. wiphy_err(priv->hw->wiphy,
  230. "PM request failed: %d. WoW is disabled.\n", ret);
  231. cw1200_wow_resume(hw);
  232. return -EBUSY;
  233. }
  234. /* Force resume if event is coming from the device. */
  235. if (atomic_read(&priv->bh_rx)) {
  236. cw1200_wow_resume(hw);
  237. return -EAGAIN;
  238. }
  239. return 0;
  240. revert5:
  241. WARN_ON(cw1200_bh_resume(priv));
  242. revert4:
  243. cw1200_resume_work(priv, &priv->bss_loss_work,
  244. state->bss_loss_tmo);
  245. cw1200_resume_work(priv, &priv->join_timeout,
  246. state->join_tmo);
  247. cw1200_resume_work(priv, &priv->scan.probe_work,
  248. state->direct_probe);
  249. cw1200_resume_work(priv, &priv->link_id_gc_work,
  250. state->link_id_gc);
  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. }