event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include "wl12xx.h"
  24. #include "reg.h"
  25. #include "io.h"
  26. #include "event.h"
  27. #include "ps.h"
  28. #include "scan.h"
  29. #include "wl12xx_80211.h"
  30. void wl1271_pspoll_work(struct work_struct *work)
  31. {
  32. struct ieee80211_vif *vif;
  33. struct wl12xx_vif *wlvif;
  34. struct delayed_work *dwork;
  35. struct wl1271 *wl;
  36. int ret;
  37. dwork = container_of(work, struct delayed_work, work);
  38. wlvif = container_of(dwork, struct wl12xx_vif, pspoll_work);
  39. vif = container_of((void *)wlvif, struct ieee80211_vif, drv_priv);
  40. wl = wlvif->wl;
  41. wl1271_debug(DEBUG_EVENT, "pspoll work");
  42. mutex_lock(&wl->mutex);
  43. if (unlikely(wl->state == WL1271_STATE_OFF))
  44. goto out;
  45. if (!test_and_clear_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags))
  46. goto out;
  47. if (!test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
  48. goto out;
  49. /*
  50. * if we end up here, then we were in powersave when the pspoll
  51. * delivery failure occurred, and no-one changed state since, so
  52. * we should go back to powersave.
  53. */
  54. ret = wl1271_ps_elp_wakeup(wl);
  55. if (ret < 0)
  56. goto out;
  57. wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE,
  58. wlvif->basic_rate, true);
  59. wl1271_ps_elp_sleep(wl);
  60. out:
  61. mutex_unlock(&wl->mutex);
  62. };
  63. static void wl1271_event_pspoll_delivery_fail(struct wl1271 *wl,
  64. struct wl12xx_vif *wlvif)
  65. {
  66. int delay = wl->conf.conn.ps_poll_recovery_period;
  67. int ret;
  68. wlvif->ps_poll_failures++;
  69. if (wlvif->ps_poll_failures == 1)
  70. wl1271_info("AP with dysfunctional ps-poll, "
  71. "trying to work around it.");
  72. /* force active mode receive data from the AP */
  73. if (test_bit(WL1271_FLAG_PSM, &wl->flags)) {
  74. ret = wl1271_ps_set_mode(wl, wlvif, STATION_ACTIVE_MODE,
  75. wlvif->basic_rate, true);
  76. if (ret < 0)
  77. return;
  78. set_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags);
  79. ieee80211_queue_delayed_work(wl->hw, &wlvif->pspoll_work,
  80. msecs_to_jiffies(delay));
  81. }
  82. /*
  83. * If already in active mode, lets we should be getting data from
  84. * the AP right away. If we enter PSM too fast after this, and data
  85. * remains on the AP, we will get another event like this, and we'll
  86. * go into active once more.
  87. */
  88. }
  89. static int wl1271_event_ps_report(struct wl1271 *wl,
  90. struct wl12xx_vif *wlvif,
  91. struct event_mailbox *mbox,
  92. bool *beacon_loss)
  93. {
  94. int ret = 0;
  95. u32 total_retries = wl->conf.conn.psm_entry_retries;
  96. wl1271_debug(DEBUG_EVENT, "ps_status: 0x%x", mbox->ps_status);
  97. switch (mbox->ps_status) {
  98. case EVENT_ENTER_POWER_SAVE_FAIL:
  99. wl1271_debug(DEBUG_PSM, "PSM entry failed");
  100. if (!test_bit(WL1271_FLAG_PSM, &wl->flags)) {
  101. /* remain in active mode */
  102. wlvif->psm_entry_retry = 0;
  103. break;
  104. }
  105. if (wlvif->psm_entry_retry < total_retries) {
  106. wlvif->psm_entry_retry++;
  107. ret = wl1271_ps_set_mode(wl, wlvif,
  108. STATION_POWER_SAVE_MODE,
  109. wlvif->basic_rate, true);
  110. } else {
  111. wl1271_info("No ack to nullfunc from AP.");
  112. wlvif->psm_entry_retry = 0;
  113. *beacon_loss = true;
  114. }
  115. break;
  116. case EVENT_ENTER_POWER_SAVE_SUCCESS:
  117. wlvif->psm_entry_retry = 0;
  118. /* enable beacon filtering */
  119. ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
  120. if (ret < 0)
  121. break;
  122. /*
  123. * BET has only a minor effect in 5GHz and masks
  124. * channel switch IEs, so we only enable BET on 2.4GHz
  125. */
  126. if (wl->band == IEEE80211_BAND_2GHZ)
  127. /* enable beacon early termination */
  128. ret = wl1271_acx_bet_enable(wl, wlvif, true);
  129. if (wlvif->ps_compl) {
  130. complete(wlvif->ps_compl);
  131. wlvif->ps_compl = NULL;
  132. }
  133. break;
  134. default:
  135. break;
  136. }
  137. return ret;
  138. }
  139. static void wl1271_event_rssi_trigger(struct wl1271 *wl,
  140. struct ieee80211_vif *vif,
  141. struct event_mailbox *mbox)
  142. {
  143. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  144. enum nl80211_cqm_rssi_threshold_event event;
  145. s8 metric = mbox->rssi_snr_trigger_metric[0];
  146. wl1271_debug(DEBUG_EVENT, "RSSI trigger metric: %d", metric);
  147. if (metric <= wlvif->rssi_thold)
  148. event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
  149. else
  150. event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
  151. if (event != wlvif->last_rssi_event)
  152. ieee80211_cqm_rssi_notify(vif, event, GFP_KERNEL);
  153. wlvif->last_rssi_event = event;
  154. }
  155. static void wl1271_stop_ba_event(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  156. {
  157. if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
  158. if (!wlvif->sta.ba_rx_bitmap)
  159. return;
  160. ieee80211_stop_rx_ba_session(wl->vif, wlvif->sta.ba_rx_bitmap,
  161. wl->vif->bss_conf.bssid);
  162. } else {
  163. int i;
  164. struct wl1271_link *lnk;
  165. for (i = WL1271_AP_STA_HLID_START; i < AP_MAX_LINKS; i++) {
  166. lnk = &wl->links[i];
  167. if (!wl1271_is_active_sta(wl, i) || !lnk->ba_bitmap)
  168. continue;
  169. ieee80211_stop_rx_ba_session(wl->vif,
  170. lnk->ba_bitmap,
  171. lnk->addr);
  172. }
  173. }
  174. }
  175. static void wl12xx_event_soft_gemini_sense(struct wl1271 *wl,
  176. u8 enable)
  177. {
  178. if (enable) {
  179. /* disable dynamic PS when requested by the firmware */
  180. ieee80211_disable_dyn_ps(wl->vif);
  181. set_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags);
  182. } else {
  183. ieee80211_enable_dyn_ps(wl->vif);
  184. clear_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags);
  185. wl1271_recalc_rx_streaming(wl);
  186. }
  187. }
  188. static void wl1271_event_mbox_dump(struct event_mailbox *mbox)
  189. {
  190. wl1271_debug(DEBUG_EVENT, "MBOX DUMP:");
  191. wl1271_debug(DEBUG_EVENT, "\tvector: 0x%x", mbox->events_vector);
  192. wl1271_debug(DEBUG_EVENT, "\tmask: 0x%x", mbox->events_mask);
  193. }
  194. static int wl1271_event_process(struct wl1271 *wl, struct event_mailbox *mbox)
  195. {
  196. struct ieee80211_vif *vif = wl->vif; /* TODO: get as param */
  197. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  198. int ret;
  199. u32 vector;
  200. bool beacon_loss = false;
  201. bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
  202. bool disconnect_sta = false;
  203. unsigned long sta_bitmap = 0;
  204. wl1271_event_mbox_dump(mbox);
  205. vector = le32_to_cpu(mbox->events_vector);
  206. vector &= ~(le32_to_cpu(mbox->events_mask));
  207. wl1271_debug(DEBUG_EVENT, "vector: 0x%x", vector);
  208. if (vector & SCAN_COMPLETE_EVENT_ID) {
  209. wl1271_debug(DEBUG_EVENT, "status: 0x%x",
  210. mbox->scheduled_scan_status);
  211. wl1271_scan_stm(wl, wl->scan_vif);
  212. }
  213. if (vector & PERIODIC_SCAN_REPORT_EVENT_ID) {
  214. wl1271_debug(DEBUG_EVENT, "PERIODIC_SCAN_REPORT_EVENT "
  215. "(status 0x%0x)", mbox->scheduled_scan_status);
  216. wl1271_scan_sched_scan_results(wl);
  217. }
  218. if (vector & PERIODIC_SCAN_COMPLETE_EVENT_ID) {
  219. wl1271_debug(DEBUG_EVENT, "PERIODIC_SCAN_COMPLETE_EVENT "
  220. "(status 0x%0x)", mbox->scheduled_scan_status);
  221. if (wl->sched_scanning) {
  222. wl1271_scan_sched_scan_stop(wl);
  223. ieee80211_sched_scan_stopped(wl->hw);
  224. }
  225. }
  226. if (vector & SOFT_GEMINI_SENSE_EVENT_ID &&
  227. wlvif->bss_type == BSS_TYPE_STA_BSS)
  228. wl12xx_event_soft_gemini_sense(wl,
  229. mbox->soft_gemini_sense_info);
  230. /*
  231. * The BSS_LOSE_EVENT_ID is only needed while psm (and hence beacon
  232. * filtering) is enabled. Without PSM, the stack will receive all
  233. * beacons and can detect beacon loss by itself.
  234. *
  235. * As there's possibility that the driver disables PSM before receiving
  236. * BSS_LOSE_EVENT, beacon loss has to be reported to the stack.
  237. *
  238. */
  239. if ((vector & BSS_LOSE_EVENT_ID) && !is_ap) {
  240. wl1271_info("Beacon loss detected.");
  241. /* indicate to the stack, that beacons have been lost */
  242. beacon_loss = true;
  243. }
  244. if ((vector & PS_REPORT_EVENT_ID) && !is_ap) {
  245. wl1271_debug(DEBUG_EVENT, "PS_REPORT_EVENT");
  246. ret = wl1271_event_ps_report(wl, wlvif, mbox, &beacon_loss);
  247. if (ret < 0)
  248. return ret;
  249. }
  250. if ((vector & PSPOLL_DELIVERY_FAILURE_EVENT_ID) && !is_ap)
  251. wl1271_event_pspoll_delivery_fail(wl, wlvif);
  252. if (vector & RSSI_SNR_TRIGGER_0_EVENT_ID) {
  253. wl1271_debug(DEBUG_EVENT, "RSSI_SNR_TRIGGER_0_EVENT");
  254. if (wl->vif)
  255. wl1271_event_rssi_trigger(wl, vif, mbox);
  256. }
  257. if ((vector & BA_SESSION_RX_CONSTRAINT_EVENT_ID)) {
  258. wl1271_debug(DEBUG_EVENT, "BA_SESSION_RX_CONSTRAINT_EVENT_ID. "
  259. "ba_allowed = 0x%x", mbox->rx_ba_allowed);
  260. wlvif->ba_allowed = !!mbox->rx_ba_allowed;
  261. if (wl->vif && !wlvif->ba_allowed)
  262. wl1271_stop_ba_event(wl, wlvif);
  263. }
  264. if ((vector & CHANNEL_SWITCH_COMPLETE_EVENT_ID) && !is_ap) {
  265. wl1271_debug(DEBUG_EVENT, "CHANNEL_SWITCH_COMPLETE_EVENT_ID. "
  266. "status = 0x%x",
  267. mbox->channel_switch_status);
  268. /*
  269. * That event uses for two cases:
  270. * 1) channel switch complete with status=0
  271. * 2) channel switch failed status=1
  272. */
  273. if (test_and_clear_bit(WL1271_FLAG_CS_PROGRESS, &wl->flags) &&
  274. (wl->vif))
  275. ieee80211_chswitch_done(wl->vif,
  276. mbox->channel_switch_status ? false : true);
  277. }
  278. if ((vector & DUMMY_PACKET_EVENT_ID)) {
  279. wl1271_debug(DEBUG_EVENT, "DUMMY_PACKET_ID_EVENT_ID");
  280. if (wl->vif)
  281. wl1271_tx_dummy_packet(wl);
  282. }
  283. /*
  284. * "TX retries exceeded" has a different meaning according to mode.
  285. * In AP mode the offending station is disconnected.
  286. */
  287. if ((vector & MAX_TX_RETRY_EVENT_ID) && is_ap) {
  288. wl1271_debug(DEBUG_EVENT, "MAX_TX_RETRY_EVENT_ID");
  289. sta_bitmap |= le16_to_cpu(mbox->sta_tx_retry_exceeded);
  290. disconnect_sta = true;
  291. }
  292. if ((vector & INACTIVE_STA_EVENT_ID) && is_ap) {
  293. wl1271_debug(DEBUG_EVENT, "INACTIVE_STA_EVENT_ID");
  294. sta_bitmap |= le16_to_cpu(mbox->sta_aging_status);
  295. disconnect_sta = true;
  296. }
  297. if (is_ap && disconnect_sta) {
  298. u32 num_packets = wl->conf.tx.max_tx_retries;
  299. struct ieee80211_sta *sta;
  300. const u8 *addr;
  301. int h;
  302. for (h = find_first_bit(&sta_bitmap, AP_MAX_LINKS);
  303. h < AP_MAX_LINKS;
  304. h = find_next_bit(&sta_bitmap, AP_MAX_LINKS, h+1)) {
  305. if (!wl1271_is_active_sta(wl, h))
  306. continue;
  307. addr = wl->links[h].addr;
  308. rcu_read_lock();
  309. sta = ieee80211_find_sta(wl->vif, addr);
  310. if (sta) {
  311. wl1271_debug(DEBUG_EVENT, "remove sta %d", h);
  312. ieee80211_report_low_ack(sta, num_packets);
  313. }
  314. rcu_read_unlock();
  315. }
  316. }
  317. if (wl->vif && beacon_loss)
  318. ieee80211_connection_loss(wl->vif);
  319. return 0;
  320. }
  321. int wl1271_event_unmask(struct wl1271 *wl)
  322. {
  323. int ret;
  324. ret = wl1271_acx_event_mbox_mask(wl, ~(wl->event_mask));
  325. if (ret < 0)
  326. return ret;
  327. return 0;
  328. }
  329. void wl1271_event_mbox_config(struct wl1271 *wl)
  330. {
  331. wl->mbox_ptr[0] = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
  332. wl->mbox_ptr[1] = wl->mbox_ptr[0] + sizeof(struct event_mailbox);
  333. wl1271_debug(DEBUG_EVENT, "MBOX ptrs: 0x%x 0x%x",
  334. wl->mbox_ptr[0], wl->mbox_ptr[1]);
  335. }
  336. int wl1271_event_handle(struct wl1271 *wl, u8 mbox_num)
  337. {
  338. struct event_mailbox mbox;
  339. int ret;
  340. wl1271_debug(DEBUG_EVENT, "EVENT on mbox %d", mbox_num);
  341. if (mbox_num > 1)
  342. return -EINVAL;
  343. /* first we read the mbox descriptor */
  344. wl1271_read(wl, wl->mbox_ptr[mbox_num], &mbox,
  345. sizeof(struct event_mailbox), false);
  346. /* process the descriptor */
  347. ret = wl1271_event_process(wl, &mbox);
  348. if (ret < 0)
  349. return ret;
  350. /* then we let the firmware know it can go on...*/
  351. wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_EVENT_ACK);
  352. return 0;
  353. }