event.c 12 KB

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