event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->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(WLVIF_FLAG_PSM, &wlvif->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(WLVIF_FLAG_PSM, &wlvif->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 wl12xx_vif *wlvif,
  141. struct event_mailbox *mbox)
  142. {
  143. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  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. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  158. if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
  159. if (!wlvif->sta.ba_rx_bitmap)
  160. return;
  161. ieee80211_stop_rx_ba_session(vif, wlvif->sta.ba_rx_bitmap,
  162. vif->bss_conf.bssid);
  163. } else {
  164. u8 hlid;
  165. struct wl1271_link *lnk;
  166. for_each_set_bit(hlid, wlvif->ap.sta_hlid_map,
  167. WL12XX_MAX_LINKS) {
  168. lnk = &wl->links[hlid];
  169. if (!lnk->ba_bitmap)
  170. continue;
  171. ieee80211_stop_rx_ba_session(vif,
  172. lnk->ba_bitmap,
  173. lnk->addr);
  174. }
  175. }
  176. }
  177. static void wl12xx_event_soft_gemini_sense(struct wl1271 *wl,
  178. u8 enable)
  179. {
  180. struct ieee80211_vif *vif;
  181. struct wl12xx_vif *wlvif;
  182. if (enable) {
  183. /* disable dynamic PS when requested by the firmware */
  184. wl12xx_for_each_wlvif_sta(wl, wlvif) {
  185. vif = wl12xx_wlvif_to_vif(wlvif);
  186. ieee80211_disable_dyn_ps(vif);
  187. }
  188. set_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags);
  189. } else {
  190. clear_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags);
  191. wl12xx_for_each_wlvif_sta(wl, wlvif) {
  192. vif = wl12xx_wlvif_to_vif(wlvif);
  193. ieee80211_enable_dyn_ps(vif);
  194. wl1271_recalc_rx_streaming(wl, wlvif);
  195. }
  196. }
  197. }
  198. static void wl1271_event_mbox_dump(struct event_mailbox *mbox)
  199. {
  200. wl1271_debug(DEBUG_EVENT, "MBOX DUMP:");
  201. wl1271_debug(DEBUG_EVENT, "\tvector: 0x%x", mbox->events_vector);
  202. wl1271_debug(DEBUG_EVENT, "\tmask: 0x%x", mbox->events_mask);
  203. }
  204. static int wl1271_event_process(struct wl1271 *wl, struct event_mailbox *mbox)
  205. {
  206. struct ieee80211_vif *vif;
  207. struct wl12xx_vif *wlvif;
  208. int ret;
  209. u32 vector;
  210. bool beacon_loss = false;
  211. bool disconnect_sta = false;
  212. unsigned long sta_bitmap = 0;
  213. wl1271_event_mbox_dump(mbox);
  214. vector = le32_to_cpu(mbox->events_vector);
  215. vector &= ~(le32_to_cpu(mbox->events_mask));
  216. wl1271_debug(DEBUG_EVENT, "vector: 0x%x", vector);
  217. if (vector & SCAN_COMPLETE_EVENT_ID) {
  218. wl1271_debug(DEBUG_EVENT, "status: 0x%x",
  219. mbox->scheduled_scan_status);
  220. wl1271_scan_stm(wl, wl->scan_vif);
  221. }
  222. if (vector & PERIODIC_SCAN_REPORT_EVENT_ID) {
  223. wl1271_debug(DEBUG_EVENT, "PERIODIC_SCAN_REPORT_EVENT "
  224. "(status 0x%0x)", mbox->scheduled_scan_status);
  225. wl1271_scan_sched_scan_results(wl);
  226. }
  227. if (vector & PERIODIC_SCAN_COMPLETE_EVENT_ID) {
  228. wl1271_debug(DEBUG_EVENT, "PERIODIC_SCAN_COMPLETE_EVENT "
  229. "(status 0x%0x)", mbox->scheduled_scan_status);
  230. if (wl->sched_scanning) {
  231. wl1271_scan_sched_scan_stop(wl);
  232. ieee80211_sched_scan_stopped(wl->hw);
  233. }
  234. }
  235. if (vector & SOFT_GEMINI_SENSE_EVENT_ID)
  236. wl12xx_event_soft_gemini_sense(wl,
  237. mbox->soft_gemini_sense_info);
  238. /*
  239. * The BSS_LOSE_EVENT_ID is only needed while psm (and hence beacon
  240. * filtering) is enabled. Without PSM, the stack will receive all
  241. * beacons and can detect beacon loss by itself.
  242. *
  243. * As there's possibility that the driver disables PSM before receiving
  244. * BSS_LOSE_EVENT, beacon loss has to be reported to the stack.
  245. *
  246. */
  247. if (vector & BSS_LOSE_EVENT_ID) {
  248. /* TODO: check for multi-role */
  249. wl1271_info("Beacon loss detected.");
  250. /* indicate to the stack, that beacons have been lost */
  251. beacon_loss = true;
  252. }
  253. if (vector & PS_REPORT_EVENT_ID) {
  254. wl1271_debug(DEBUG_EVENT, "PS_REPORT_EVENT");
  255. wl12xx_for_each_wlvif_sta(wl, wlvif) {
  256. ret = wl1271_event_ps_report(wl, wlvif,
  257. mbox, &beacon_loss);
  258. if (ret < 0)
  259. return ret;
  260. }
  261. }
  262. if (vector & PSPOLL_DELIVERY_FAILURE_EVENT_ID)
  263. wl12xx_for_each_wlvif_sta(wl, wlvif) {
  264. wl1271_event_pspoll_delivery_fail(wl, wlvif);
  265. }
  266. if (vector & RSSI_SNR_TRIGGER_0_EVENT_ID) {
  267. /* TODO: check actual multi-role support */
  268. wl1271_debug(DEBUG_EVENT, "RSSI_SNR_TRIGGER_0_EVENT");
  269. wl12xx_for_each_wlvif_sta(wl, wlvif) {
  270. wl1271_event_rssi_trigger(wl, wlvif, mbox);
  271. }
  272. }
  273. if (vector & BA_SESSION_RX_CONSTRAINT_EVENT_ID) {
  274. u8 role_id = mbox->role_id;
  275. wl1271_debug(DEBUG_EVENT, "BA_SESSION_RX_CONSTRAINT_EVENT_ID. "
  276. "ba_allowed = 0x%x, role_id=%d",
  277. mbox->rx_ba_allowed, role_id);
  278. wl12xx_for_each_wlvif(wl, wlvif) {
  279. if (role_id != 0xff && role_id != wlvif->role_id)
  280. continue;
  281. wlvif->ba_allowed = !!mbox->rx_ba_allowed;
  282. if (!wlvif->ba_allowed)
  283. wl1271_stop_ba_event(wl, wlvif);
  284. }
  285. }
  286. if (vector & CHANNEL_SWITCH_COMPLETE_EVENT_ID) {
  287. wl1271_debug(DEBUG_EVENT, "CHANNEL_SWITCH_COMPLETE_EVENT_ID. "
  288. "status = 0x%x",
  289. mbox->channel_switch_status);
  290. /*
  291. * That event uses for two cases:
  292. * 1) channel switch complete with status=0
  293. * 2) channel switch failed status=1
  294. */
  295. if (test_and_clear_bit(WL1271_FLAG_CS_PROGRESS, &wl->flags)) {
  296. /* TODO: configure only the relevant vif */
  297. wl12xx_for_each_wlvif_sta(wl, wlvif) {
  298. struct ieee80211_vif *vif =
  299. wl12xx_wlvif_to_vif(wlvif);
  300. bool success = mbox->channel_switch_status ?
  301. false : true;
  302. ieee80211_chswitch_done(vif, success);
  303. }
  304. }
  305. }
  306. if ((vector & DUMMY_PACKET_EVENT_ID)) {
  307. wl1271_debug(DEBUG_EVENT, "DUMMY_PACKET_ID_EVENT_ID");
  308. wl1271_tx_dummy_packet(wl);
  309. }
  310. /*
  311. * "TX retries exceeded" has a different meaning according to mode.
  312. * In AP mode the offending station is disconnected.
  313. */
  314. if (vector & MAX_TX_RETRY_EVENT_ID) {
  315. wl1271_debug(DEBUG_EVENT, "MAX_TX_RETRY_EVENT_ID");
  316. sta_bitmap |= le16_to_cpu(mbox->sta_tx_retry_exceeded);
  317. disconnect_sta = true;
  318. }
  319. if (vector & INACTIVE_STA_EVENT_ID) {
  320. wl1271_debug(DEBUG_EVENT, "INACTIVE_STA_EVENT_ID");
  321. sta_bitmap |= le16_to_cpu(mbox->sta_aging_status);
  322. disconnect_sta = true;
  323. }
  324. if (disconnect_sta) {
  325. u32 num_packets = wl->conf.tx.max_tx_retries;
  326. struct ieee80211_sta *sta;
  327. const u8 *addr;
  328. int h;
  329. for_each_set_bit(h, &sta_bitmap, WL12XX_MAX_LINKS) {
  330. bool found = false;
  331. /* find the ap vif connected to this sta */
  332. wl12xx_for_each_wlvif_ap(wl, wlvif) {
  333. if (!test_bit(h, wlvif->ap.sta_hlid_map))
  334. continue;
  335. found = true;
  336. break;
  337. }
  338. if (!found)
  339. continue;
  340. vif = wl12xx_wlvif_to_vif(wlvif);
  341. addr = wl->links[h].addr;
  342. rcu_read_lock();
  343. sta = ieee80211_find_sta(vif, addr);
  344. if (sta) {
  345. wl1271_debug(DEBUG_EVENT, "remove sta %d", h);
  346. ieee80211_report_low_ack(sta, num_packets);
  347. }
  348. rcu_read_unlock();
  349. }
  350. }
  351. if (beacon_loss)
  352. wl12xx_for_each_wlvif_sta(wl, wlvif) {
  353. vif = wl12xx_wlvif_to_vif(wlvif);
  354. ieee80211_connection_loss(vif);
  355. }
  356. return 0;
  357. }
  358. int wl1271_event_unmask(struct wl1271 *wl)
  359. {
  360. int ret;
  361. ret = wl1271_acx_event_mbox_mask(wl, ~(wl->event_mask));
  362. if (ret < 0)
  363. return ret;
  364. return 0;
  365. }
  366. void wl1271_event_mbox_config(struct wl1271 *wl)
  367. {
  368. wl->mbox_ptr[0] = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
  369. wl->mbox_ptr[1] = wl->mbox_ptr[0] + sizeof(struct event_mailbox);
  370. wl1271_debug(DEBUG_EVENT, "MBOX ptrs: 0x%x 0x%x",
  371. wl->mbox_ptr[0], wl->mbox_ptr[1]);
  372. }
  373. int wl1271_event_handle(struct wl1271 *wl, u8 mbox_num)
  374. {
  375. struct event_mailbox mbox;
  376. int ret;
  377. wl1271_debug(DEBUG_EVENT, "EVENT on mbox %d", mbox_num);
  378. if (mbox_num > 1)
  379. return -EINVAL;
  380. /* first we read the mbox descriptor */
  381. wl1271_read(wl, wl->mbox_ptr[mbox_num], &mbox,
  382. sizeof(struct event_mailbox), false);
  383. /* process the descriptor */
  384. ret = wl1271_event_process(wl, &mbox);
  385. if (ret < 0)
  386. return ret;
  387. /* then we let the firmware know it can go on...*/
  388. wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_EVENT_ACK);
  389. return 0;
  390. }