event.c 11 KB

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