agg-rx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * HT handling
  3. *
  4. * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
  5. * Copyright 2002-2005, Instant802 Networks, Inc.
  6. * Copyright 2005-2006, Devicescape Software, Inc.
  7. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  8. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  9. * Copyright 2007-2010, Intel Corporation
  10. * Copyright(c) 2015-2017 Intel Deutschland GmbH
  11. * Copyright (C) 2018 Intel Corporation
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. /**
  18. * DOC: RX A-MPDU aggregation
  19. *
  20. * Aggregation on the RX side requires only implementing the
  21. * @ampdu_action callback that is invoked to start/stop any
  22. * block-ack sessions for RX aggregation.
  23. *
  24. * When RX aggregation is started by the peer, the driver is
  25. * notified via @ampdu_action function, with the
  26. * %IEEE80211_AMPDU_RX_START action, and may reject the request
  27. * in which case a negative response is sent to the peer, if it
  28. * accepts it a positive response is sent.
  29. *
  30. * While the session is active, the device/driver are required
  31. * to de-aggregate frames and pass them up one by one to mac80211,
  32. * which will handle the reorder buffer.
  33. *
  34. * When the aggregation session is stopped again by the peer or
  35. * ourselves, the driver's @ampdu_action function will be called
  36. * with the action %IEEE80211_AMPDU_RX_STOP. In this case, the
  37. * call must not fail.
  38. */
  39. #include <linux/ieee80211.h>
  40. #include <linux/slab.h>
  41. #include <linux/export.h>
  42. #include <net/mac80211.h>
  43. #include "ieee80211_i.h"
  44. #include "driver-ops.h"
  45. static void ieee80211_free_tid_rx(struct rcu_head *h)
  46. {
  47. struct tid_ampdu_rx *tid_rx =
  48. container_of(h, struct tid_ampdu_rx, rcu_head);
  49. int i;
  50. for (i = 0; i < tid_rx->buf_size; i++)
  51. __skb_queue_purge(&tid_rx->reorder_buf[i]);
  52. kfree(tid_rx->reorder_buf);
  53. kfree(tid_rx->reorder_time);
  54. kfree(tid_rx);
  55. }
  56. void ___ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
  57. u16 initiator, u16 reason, bool tx)
  58. {
  59. struct ieee80211_local *local = sta->local;
  60. struct tid_ampdu_rx *tid_rx;
  61. struct ieee80211_ampdu_params params = {
  62. .sta = &sta->sta,
  63. .action = IEEE80211_AMPDU_RX_STOP,
  64. .tid = tid,
  65. .amsdu = false,
  66. .timeout = 0,
  67. .ssn = 0,
  68. };
  69. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  70. tid_rx = rcu_dereference_protected(sta->ampdu_mlme.tid_rx[tid],
  71. lockdep_is_held(&sta->ampdu_mlme.mtx));
  72. if (!test_bit(tid, sta->ampdu_mlme.agg_session_valid))
  73. return;
  74. RCU_INIT_POINTER(sta->ampdu_mlme.tid_rx[tid], NULL);
  75. __clear_bit(tid, sta->ampdu_mlme.agg_session_valid);
  76. ht_dbg(sta->sdata,
  77. "Rx BA session stop requested for %pM tid %u %s reason: %d\n",
  78. sta->sta.addr, tid,
  79. initiator == WLAN_BACK_RECIPIENT ? "recipient" : "initiator",
  80. (int)reason);
  81. if (drv_ampdu_action(local, sta->sdata, &params))
  82. sdata_info(sta->sdata,
  83. "HW problem - can not stop rx aggregation for %pM tid %d\n",
  84. sta->sta.addr, tid);
  85. /* check if this is a self generated aggregation halt */
  86. if (initiator == WLAN_BACK_RECIPIENT && tx)
  87. ieee80211_send_delba(sta->sdata, sta->sta.addr,
  88. tid, WLAN_BACK_RECIPIENT, reason);
  89. /*
  90. * return here in case tid_rx is not assigned - which will happen if
  91. * IEEE80211_HW_SUPPORTS_REORDERING_BUFFER is set.
  92. */
  93. if (!tid_rx)
  94. return;
  95. del_timer_sync(&tid_rx->session_timer);
  96. /* make sure ieee80211_sta_reorder_release() doesn't re-arm the timer */
  97. spin_lock_bh(&tid_rx->reorder_lock);
  98. tid_rx->removed = true;
  99. spin_unlock_bh(&tid_rx->reorder_lock);
  100. del_timer_sync(&tid_rx->reorder_timer);
  101. call_rcu(&tid_rx->rcu_head, ieee80211_free_tid_rx);
  102. }
  103. void __ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
  104. u16 initiator, u16 reason, bool tx)
  105. {
  106. mutex_lock(&sta->ampdu_mlme.mtx);
  107. ___ieee80211_stop_rx_ba_session(sta, tid, initiator, reason, tx);
  108. mutex_unlock(&sta->ampdu_mlme.mtx);
  109. }
  110. void ieee80211_stop_rx_ba_session(struct ieee80211_vif *vif, u16 ba_rx_bitmap,
  111. const u8 *addr)
  112. {
  113. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  114. struct sta_info *sta;
  115. int i;
  116. rcu_read_lock();
  117. sta = sta_info_get_bss(sdata, addr);
  118. if (!sta) {
  119. rcu_read_unlock();
  120. return;
  121. }
  122. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  123. if (ba_rx_bitmap & BIT(i))
  124. set_bit(i, sta->ampdu_mlme.tid_rx_stop_requested);
  125. ieee80211_queue_work(&sta->local->hw, &sta->ampdu_mlme.work);
  126. rcu_read_unlock();
  127. }
  128. EXPORT_SYMBOL(ieee80211_stop_rx_ba_session);
  129. /*
  130. * After accepting the AddBA Request we activated a timer,
  131. * resetting it after each frame that arrives from the originator.
  132. */
  133. static void sta_rx_agg_session_timer_expired(struct timer_list *t)
  134. {
  135. struct tid_ampdu_rx *tid_rx = from_timer(tid_rx, t, session_timer);
  136. struct sta_info *sta = tid_rx->sta;
  137. u8 tid = tid_rx->tid;
  138. unsigned long timeout;
  139. timeout = tid_rx->last_rx + TU_TO_JIFFIES(tid_rx->timeout);
  140. if (time_is_after_jiffies(timeout)) {
  141. mod_timer(&tid_rx->session_timer, timeout);
  142. return;
  143. }
  144. ht_dbg(sta->sdata, "RX session timer expired on %pM tid %d\n",
  145. sta->sta.addr, tid);
  146. set_bit(tid, sta->ampdu_mlme.tid_rx_timer_expired);
  147. ieee80211_queue_work(&sta->local->hw, &sta->ampdu_mlme.work);
  148. }
  149. static void sta_rx_agg_reorder_timer_expired(struct timer_list *t)
  150. {
  151. struct tid_ampdu_rx *tid_rx = from_timer(tid_rx, t, reorder_timer);
  152. rcu_read_lock();
  153. ieee80211_release_reorder_timeout(tid_rx->sta, tid_rx->tid);
  154. rcu_read_unlock();
  155. }
  156. static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
  157. u8 dialog_token, u16 status, u16 policy,
  158. u16 buf_size, u16 timeout)
  159. {
  160. struct ieee80211_local *local = sdata->local;
  161. struct sk_buff *skb;
  162. struct ieee80211_mgmt *mgmt;
  163. bool amsdu = ieee80211_hw_check(&local->hw, SUPPORTS_AMSDU_IN_AMPDU);
  164. u16 capab;
  165. skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
  166. if (!skb)
  167. return;
  168. skb_reserve(skb, local->hw.extra_tx_headroom);
  169. mgmt = skb_put_zero(skb, 24);
  170. memcpy(mgmt->da, da, ETH_ALEN);
  171. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  172. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  173. sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  174. sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  175. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  176. else if (sdata->vif.type == NL80211_IFTYPE_STATION)
  177. memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  178. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  179. memcpy(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN);
  180. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  181. IEEE80211_STYPE_ACTION);
  182. skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
  183. mgmt->u.action.category = WLAN_CATEGORY_BACK;
  184. mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
  185. mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
  186. capab = (u16)(amsdu << 0); /* bit 0 A-MSDU support */
  187. capab |= (u16)(policy << 1); /* bit 1 aggregation policy */
  188. capab |= (u16)(tid << 2); /* bit 5:2 TID number */
  189. capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
  190. mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
  191. mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
  192. mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
  193. ieee80211_tx_skb(sdata, skb);
  194. }
  195. void ___ieee80211_start_rx_ba_session(struct sta_info *sta,
  196. u8 dialog_token, u16 timeout,
  197. u16 start_seq_num, u16 ba_policy, u16 tid,
  198. u16 buf_size, bool tx, bool auto_seq)
  199. {
  200. struct ieee80211_local *local = sta->sdata->local;
  201. struct tid_ampdu_rx *tid_agg_rx;
  202. struct ieee80211_ampdu_params params = {
  203. .sta = &sta->sta,
  204. .action = IEEE80211_AMPDU_RX_START,
  205. .tid = tid,
  206. .amsdu = false,
  207. .timeout = timeout,
  208. .ssn = start_seq_num,
  209. };
  210. int i, ret = -EOPNOTSUPP;
  211. u16 status = WLAN_STATUS_REQUEST_DECLINED;
  212. u16 max_buf_size;
  213. if (tid >= IEEE80211_FIRST_TSPEC_TSID) {
  214. ht_dbg(sta->sdata,
  215. "STA %pM requests BA session on unsupported tid %d\n",
  216. sta->sta.addr, tid);
  217. goto end;
  218. }
  219. if (!sta->sta.ht_cap.ht_supported) {
  220. ht_dbg(sta->sdata,
  221. "STA %pM erroneously requests BA session on tid %d w/o QoS\n",
  222. sta->sta.addr, tid);
  223. /* send a response anyway, it's an error case if we get here */
  224. goto end;
  225. }
  226. if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
  227. ht_dbg(sta->sdata,
  228. "Suspend in progress - Denying ADDBA request (%pM tid %d)\n",
  229. sta->sta.addr, tid);
  230. goto end;
  231. }
  232. if (sta->sta.he_cap.has_he)
  233. max_buf_size = IEEE80211_MAX_AMPDU_BUF;
  234. else
  235. max_buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
  236. /* sanity check for incoming parameters:
  237. * check if configuration can support the BA policy
  238. * and if buffer size does not exceeds max value */
  239. /* XXX: check own ht delayed BA capability?? */
  240. if (((ba_policy != 1) &&
  241. (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA))) ||
  242. (buf_size > max_buf_size)) {
  243. status = WLAN_STATUS_INVALID_QOS_PARAM;
  244. ht_dbg_ratelimited(sta->sdata,
  245. "AddBA Req with bad params from %pM on tid %u. policy %d, buffer size %d\n",
  246. sta->sta.addr, tid, ba_policy, buf_size);
  247. goto end;
  248. }
  249. /* determine default buffer size */
  250. if (buf_size == 0)
  251. buf_size = max_buf_size;
  252. /* make sure the size doesn't exceed the maximum supported by the hw */
  253. if (buf_size > sta->sta.max_rx_aggregation_subframes)
  254. buf_size = sta->sta.max_rx_aggregation_subframes;
  255. params.buf_size = buf_size;
  256. ht_dbg(sta->sdata, "AddBA Req buf_size=%d for %pM\n",
  257. buf_size, sta->sta.addr);
  258. /* examine state machine */
  259. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  260. if (test_bit(tid, sta->ampdu_mlme.agg_session_valid)) {
  261. if (sta->ampdu_mlme.tid_rx_token[tid] == dialog_token) {
  262. struct tid_ampdu_rx *tid_rx;
  263. ht_dbg_ratelimited(sta->sdata,
  264. "updated AddBA Req from %pM on tid %u\n",
  265. sta->sta.addr, tid);
  266. /* We have no API to update the timeout value in the
  267. * driver so reject the timeout update if the timeout
  268. * changed. If if did not change, i.e., no real update,
  269. * just reply with success.
  270. */
  271. rcu_read_lock();
  272. tid_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  273. if (tid_rx && tid_rx->timeout == timeout)
  274. status = WLAN_STATUS_SUCCESS;
  275. else
  276. status = WLAN_STATUS_REQUEST_DECLINED;
  277. rcu_read_unlock();
  278. goto end;
  279. }
  280. ht_dbg_ratelimited(sta->sdata,
  281. "unexpected AddBA Req from %pM on tid %u\n",
  282. sta->sta.addr, tid);
  283. /* delete existing Rx BA session on the same tid */
  284. ___ieee80211_stop_rx_ba_session(sta, tid, WLAN_BACK_RECIPIENT,
  285. WLAN_STATUS_UNSPECIFIED_QOS,
  286. false);
  287. }
  288. if (ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER)) {
  289. ret = drv_ampdu_action(local, sta->sdata, &params);
  290. ht_dbg(sta->sdata,
  291. "Rx A-MPDU request on %pM tid %d result %d\n",
  292. sta->sta.addr, tid, ret);
  293. if (!ret)
  294. status = WLAN_STATUS_SUCCESS;
  295. goto end;
  296. }
  297. /* prepare A-MPDU MLME for Rx aggregation */
  298. tid_agg_rx = kzalloc(sizeof(*tid_agg_rx), GFP_KERNEL);
  299. if (!tid_agg_rx)
  300. goto end;
  301. spin_lock_init(&tid_agg_rx->reorder_lock);
  302. /* rx timer */
  303. timer_setup(&tid_agg_rx->session_timer,
  304. sta_rx_agg_session_timer_expired, TIMER_DEFERRABLE);
  305. /* rx reorder timer */
  306. timer_setup(&tid_agg_rx->reorder_timer,
  307. sta_rx_agg_reorder_timer_expired, 0);
  308. /* prepare reordering buffer */
  309. tid_agg_rx->reorder_buf =
  310. kcalloc(buf_size, sizeof(struct sk_buff_head), GFP_KERNEL);
  311. tid_agg_rx->reorder_time =
  312. kcalloc(buf_size, sizeof(unsigned long), GFP_KERNEL);
  313. if (!tid_agg_rx->reorder_buf || !tid_agg_rx->reorder_time) {
  314. kfree(tid_agg_rx->reorder_buf);
  315. kfree(tid_agg_rx->reorder_time);
  316. kfree(tid_agg_rx);
  317. goto end;
  318. }
  319. for (i = 0; i < buf_size; i++)
  320. __skb_queue_head_init(&tid_agg_rx->reorder_buf[i]);
  321. ret = drv_ampdu_action(local, sta->sdata, &params);
  322. ht_dbg(sta->sdata, "Rx A-MPDU request on %pM tid %d result %d\n",
  323. sta->sta.addr, tid, ret);
  324. if (ret) {
  325. kfree(tid_agg_rx->reorder_buf);
  326. kfree(tid_agg_rx->reorder_time);
  327. kfree(tid_agg_rx);
  328. goto end;
  329. }
  330. /* update data */
  331. tid_agg_rx->ssn = start_seq_num;
  332. tid_agg_rx->head_seq_num = start_seq_num;
  333. tid_agg_rx->buf_size = buf_size;
  334. tid_agg_rx->timeout = timeout;
  335. tid_agg_rx->stored_mpdu_num = 0;
  336. tid_agg_rx->auto_seq = auto_seq;
  337. tid_agg_rx->started = false;
  338. tid_agg_rx->reorder_buf_filtered = 0;
  339. tid_agg_rx->tid = tid;
  340. tid_agg_rx->sta = sta;
  341. status = WLAN_STATUS_SUCCESS;
  342. /* activate it for RX */
  343. rcu_assign_pointer(sta->ampdu_mlme.tid_rx[tid], tid_agg_rx);
  344. if (timeout) {
  345. mod_timer(&tid_agg_rx->session_timer, TU_TO_EXP_TIME(timeout));
  346. tid_agg_rx->last_rx = jiffies;
  347. }
  348. end:
  349. if (status == WLAN_STATUS_SUCCESS) {
  350. __set_bit(tid, sta->ampdu_mlme.agg_session_valid);
  351. __clear_bit(tid, sta->ampdu_mlme.unexpected_agg);
  352. sta->ampdu_mlme.tid_rx_token[tid] = dialog_token;
  353. }
  354. if (tx)
  355. ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
  356. dialog_token, status, 1, buf_size,
  357. timeout);
  358. }
  359. static void __ieee80211_start_rx_ba_session(struct sta_info *sta,
  360. u8 dialog_token, u16 timeout,
  361. u16 start_seq_num, u16 ba_policy,
  362. u16 tid, u16 buf_size, bool tx,
  363. bool auto_seq)
  364. {
  365. mutex_lock(&sta->ampdu_mlme.mtx);
  366. ___ieee80211_start_rx_ba_session(sta, dialog_token, timeout,
  367. start_seq_num, ba_policy, tid,
  368. buf_size, tx, auto_seq);
  369. mutex_unlock(&sta->ampdu_mlme.mtx);
  370. }
  371. void ieee80211_process_addba_request(struct ieee80211_local *local,
  372. struct sta_info *sta,
  373. struct ieee80211_mgmt *mgmt,
  374. size_t len)
  375. {
  376. u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num;
  377. u8 dialog_token;
  378. /* extract session parameters from addba request frame */
  379. dialog_token = mgmt->u.action.u.addba_req.dialog_token;
  380. timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
  381. start_seq_num =
  382. le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
  383. capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
  384. ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
  385. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  386. buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
  387. __ieee80211_start_rx_ba_session(sta, dialog_token, timeout,
  388. start_seq_num, ba_policy, tid,
  389. buf_size, true, false);
  390. }
  391. void ieee80211_manage_rx_ba_offl(struct ieee80211_vif *vif,
  392. const u8 *addr, unsigned int tid)
  393. {
  394. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  395. struct ieee80211_local *local = sdata->local;
  396. struct sta_info *sta;
  397. rcu_read_lock();
  398. sta = sta_info_get_bss(sdata, addr);
  399. if (!sta)
  400. goto unlock;
  401. set_bit(tid, sta->ampdu_mlme.tid_rx_manage_offl);
  402. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  403. unlock:
  404. rcu_read_unlock();
  405. }
  406. EXPORT_SYMBOL(ieee80211_manage_rx_ba_offl);
  407. void ieee80211_rx_ba_timer_expired(struct ieee80211_vif *vif,
  408. const u8 *addr, unsigned int tid)
  409. {
  410. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  411. struct ieee80211_local *local = sdata->local;
  412. struct sta_info *sta;
  413. rcu_read_lock();
  414. sta = sta_info_get_bss(sdata, addr);
  415. if (!sta)
  416. goto unlock;
  417. set_bit(tid, sta->ampdu_mlme.tid_rx_timer_expired);
  418. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  419. unlock:
  420. rcu_read_unlock();
  421. }
  422. EXPORT_SYMBOL(ieee80211_rx_ba_timer_expired);