agg-tx.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HT handling
  4. *
  5. * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
  6. * Copyright 2002-2005, Instant802 Networks, Inc.
  7. * Copyright 2005-2006, Devicescape Software, Inc.
  8. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  9. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  10. * Copyright 2007-2010, Intel Corporation
  11. * Copyright(c) 2015-2017 Intel Deutschland GmbH
  12. * Copyright (C) 2018 - 2024 Intel Corporation
  13. */
  14. #include <linux/ieee80211.h>
  15. #include <linux/slab.h>
  16. #include <linux/export.h>
  17. #include <net/mac80211.h>
  18. #include "ieee80211_i.h"
  19. #include "driver-ops.h"
  20. #include "wme.h"
  21. /**
  22. * DOC: TX A-MPDU aggregation
  23. *
  24. * Aggregation on the TX side requires setting the hardware flag
  25. * %IEEE80211_HW_AMPDU_AGGREGATION. The driver will then be handed
  26. * packets with a flag indicating A-MPDU aggregation. The driver
  27. * or device is responsible for actually aggregating the frames,
  28. * as well as deciding how many and which to aggregate.
  29. *
  30. * When TX aggregation is started by some subsystem (usually the rate
  31. * control algorithm would be appropriate) by calling the
  32. * ieee80211_start_tx_ba_session() function, the driver will be
  33. * notified via its @ampdu_action function, with the
  34. * %IEEE80211_AMPDU_TX_START action.
  35. *
  36. * In response to that, the driver is later required to call the
  37. * ieee80211_start_tx_ba_cb_irqsafe() function, which will really
  38. * start the aggregation session after the peer has also responded.
  39. * If the peer responds negatively, the session will be stopped
  40. * again right away. Note that it is possible for the aggregation
  41. * session to be stopped before the driver has indicated that it
  42. * is done setting it up, in which case it must not indicate the
  43. * setup completion.
  44. *
  45. * Also note that, since we also need to wait for a response from
  46. * the peer, the driver is notified of the completion of the
  47. * handshake by the %IEEE80211_AMPDU_TX_OPERATIONAL action to the
  48. * @ampdu_action callback.
  49. *
  50. * Similarly, when the aggregation session is stopped by the peer
  51. * or something calling ieee80211_stop_tx_ba_session(), the driver's
  52. * @ampdu_action function will be called with the action
  53. * %IEEE80211_AMPDU_TX_STOP. In this case, the call must not fail,
  54. * and the driver must later call ieee80211_stop_tx_ba_cb_irqsafe().
  55. * Note that the sta can get destroyed before the BA tear down is
  56. * complete.
  57. */
  58. static void ieee80211_send_addba_request(struct sta_info *sta, u16 tid,
  59. u8 dialog_token, u16 start_seq_num,
  60. u16 agg_size, u16 timeout)
  61. {
  62. struct ieee80211_sub_if_data *sdata = sta->sdata;
  63. struct ieee80211_local *local = sdata->local;
  64. struct sk_buff *skb;
  65. struct ieee80211_mgmt *mgmt;
  66. u16 capab;
  67. skb = dev_alloc_skb(sizeof(*mgmt) +
  68. 2 + sizeof(struct ieee80211_addba_ext_ie) +
  69. local->hw.extra_tx_headroom);
  70. if (!skb)
  71. return;
  72. skb_reserve(skb, local->hw.extra_tx_headroom);
  73. mgmt = ieee80211_mgmt_ba(skb, sta->sta.addr, sdata);
  74. skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
  75. mgmt->u.action.category = WLAN_CATEGORY_BACK;
  76. mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
  77. mgmt->u.action.u.addba_req.dialog_token = dialog_token;
  78. capab = IEEE80211_ADDBA_PARAM_AMSDU_MASK;
  79. capab |= IEEE80211_ADDBA_PARAM_POLICY_MASK;
  80. capab |= u16_encode_bits(tid, IEEE80211_ADDBA_PARAM_TID_MASK);
  81. capab |= u16_encode_bits(agg_size, IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK);
  82. mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
  83. mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
  84. mgmt->u.action.u.addba_req.start_seq_num =
  85. cpu_to_le16(start_seq_num << 4);
  86. if (sta->sta.deflink.he_cap.has_he)
  87. ieee80211_add_addbaext(skb, 0, agg_size);
  88. ieee80211_tx_skb_tid(sdata, skb, tid, -1);
  89. }
  90. void ieee80211_send_bar(struct ieee80211_vif *vif, u8 *ra, u16 tid, u16 ssn)
  91. {
  92. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  93. struct ieee80211_local *local = sdata->local;
  94. struct sk_buff *skb;
  95. struct ieee80211_bar *bar;
  96. u16 bar_control = 0;
  97. skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
  98. if (!skb)
  99. return;
  100. skb_reserve(skb, local->hw.extra_tx_headroom);
  101. bar = skb_put_zero(skb, sizeof(*bar));
  102. bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  103. IEEE80211_STYPE_BACK_REQ);
  104. memcpy(bar->ra, ra, ETH_ALEN);
  105. memcpy(bar->ta, sdata->vif.addr, ETH_ALEN);
  106. bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
  107. bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
  108. bar_control |= (u16)(tid << IEEE80211_BAR_CTRL_TID_INFO_SHIFT);
  109. bar->control = cpu_to_le16(bar_control);
  110. bar->start_seq_num = cpu_to_le16(ssn);
  111. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  112. IEEE80211_TX_CTL_REQ_TX_STATUS;
  113. ieee80211_tx_skb_tid(sdata, skb, tid, -1);
  114. }
  115. EXPORT_SYMBOL(ieee80211_send_bar);
  116. void ieee80211_assign_tid_tx(struct sta_info *sta, int tid,
  117. struct tid_ampdu_tx *tid_tx)
  118. {
  119. lockdep_assert_wiphy(sta->local->hw.wiphy);
  120. lockdep_assert_held(&sta->lock);
  121. rcu_assign_pointer(sta->ampdu_mlme.tid_tx[tid], tid_tx);
  122. }
  123. /*
  124. * When multiple aggregation sessions on multiple stations
  125. * are being created/destroyed simultaneously, we need to
  126. * refcount the global queue stop caused by that in order
  127. * to not get into a situation where one of the aggregation
  128. * setup or teardown re-enables queues before the other is
  129. * ready to handle that.
  130. *
  131. * These two functions take care of this issue by keeping
  132. * a global "agg_queue_stop" refcount.
  133. */
  134. static void __acquires(agg_queue)
  135. ieee80211_stop_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
  136. {
  137. int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
  138. /* we do refcounting here, so don't use the queue reason refcounting */
  139. if (atomic_inc_return(&sdata->local->agg_queue_stop[queue]) == 1)
  140. ieee80211_stop_queue_by_reason(
  141. &sdata->local->hw, queue,
  142. IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
  143. false);
  144. __acquire(agg_queue);
  145. }
  146. static void __releases(agg_queue)
  147. ieee80211_wake_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
  148. {
  149. int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
  150. if (atomic_dec_return(&sdata->local->agg_queue_stop[queue]) == 0)
  151. ieee80211_wake_queue_by_reason(
  152. &sdata->local->hw, queue,
  153. IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
  154. false);
  155. __release(agg_queue);
  156. }
  157. static void
  158. ieee80211_agg_stop_txq(struct sta_info *sta, int tid)
  159. {
  160. struct ieee80211_txq *txq = sta->sta.txq[tid];
  161. struct ieee80211_sub_if_data *sdata;
  162. struct fq *fq;
  163. struct txq_info *txqi;
  164. if (!txq)
  165. return;
  166. txqi = to_txq_info(txq);
  167. sdata = vif_to_sdata(txq->vif);
  168. fq = &sdata->local->fq;
  169. /* Lock here to protect against further seqno updates on dequeue */
  170. spin_lock_bh(&fq->lock);
  171. set_bit(IEEE80211_TXQ_STOP, &txqi->flags);
  172. spin_unlock_bh(&fq->lock);
  173. }
  174. static void
  175. ieee80211_agg_start_txq(struct sta_info *sta, int tid, bool enable)
  176. {
  177. struct ieee80211_txq *txq = sta->sta.txq[tid];
  178. struct txq_info *txqi;
  179. lockdep_assert_wiphy(sta->local->hw.wiphy);
  180. if (!txq)
  181. return;
  182. txqi = to_txq_info(txq);
  183. if (enable)
  184. set_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
  185. else
  186. clear_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
  187. clear_bit(IEEE80211_TXQ_STOP, &txqi->flags);
  188. local_bh_disable();
  189. rcu_read_lock();
  190. schedule_and_wake_txq(sta->sdata->local, txqi);
  191. rcu_read_unlock();
  192. local_bh_enable();
  193. }
  194. /*
  195. * splice packets from the STA's pending to the local pending,
  196. * requires a call to ieee80211_agg_splice_finish later
  197. */
  198. static void __acquires(agg_queue)
  199. ieee80211_agg_splice_packets(struct ieee80211_sub_if_data *sdata,
  200. struct tid_ampdu_tx *tid_tx, u16 tid)
  201. {
  202. struct ieee80211_local *local = sdata->local;
  203. int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
  204. unsigned long flags;
  205. ieee80211_stop_queue_agg(sdata, tid);
  206. if (WARN(!tid_tx,
  207. "TID %d gone but expected when splicing aggregates from the pending queue\n",
  208. tid))
  209. return;
  210. if (!skb_queue_empty(&tid_tx->pending)) {
  211. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  212. /* copy over remaining packets */
  213. skb_queue_splice_tail_init(&tid_tx->pending,
  214. &local->pending[queue]);
  215. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  216. }
  217. }
  218. static void __releases(agg_queue)
  219. ieee80211_agg_splice_finish(struct ieee80211_sub_if_data *sdata, u16 tid)
  220. {
  221. ieee80211_wake_queue_agg(sdata, tid);
  222. }
  223. static void ieee80211_remove_tid_tx(struct sta_info *sta, int tid)
  224. {
  225. struct tid_ampdu_tx *tid_tx;
  226. lockdep_assert_wiphy(sta->local->hw.wiphy);
  227. lockdep_assert_held(&sta->lock);
  228. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  229. /*
  230. * When we get here, the TX path will not be lockless any more wrt.
  231. * aggregation, since the OPERATIONAL bit has long been cleared.
  232. * Thus it will block on getting the lock, if it occurs. So if we
  233. * stop the queue now, we will not get any more packets, and any
  234. * that might be being processed will wait for us here, thereby
  235. * guaranteeing that no packets go to the tid_tx pending queue any
  236. * more.
  237. */
  238. ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
  239. /* future packets must not find the tid_tx struct any more */
  240. ieee80211_assign_tid_tx(sta, tid, NULL);
  241. ieee80211_agg_splice_finish(sta->sdata, tid);
  242. kfree_rcu(tid_tx, rcu_head);
  243. }
  244. int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
  245. enum ieee80211_agg_stop_reason reason)
  246. {
  247. struct ieee80211_local *local = sta->local;
  248. struct tid_ampdu_tx *tid_tx;
  249. struct ieee80211_ampdu_params params = {
  250. .sta = &sta->sta,
  251. .tid = tid,
  252. .buf_size = 0,
  253. .amsdu = false,
  254. .timeout = 0,
  255. .ssn = 0,
  256. };
  257. int ret;
  258. lockdep_assert_wiphy(sta->local->hw.wiphy);
  259. switch (reason) {
  260. case AGG_STOP_DECLINED:
  261. case AGG_STOP_LOCAL_REQUEST:
  262. case AGG_STOP_PEER_REQUEST:
  263. params.action = IEEE80211_AMPDU_TX_STOP_CONT;
  264. break;
  265. case AGG_STOP_DESTROY_STA:
  266. params.action = IEEE80211_AMPDU_TX_STOP_FLUSH;
  267. break;
  268. default:
  269. WARN_ON_ONCE(1);
  270. return -EINVAL;
  271. }
  272. spin_lock_bh(&sta->lock);
  273. /* free struct pending for start, if present */
  274. tid_tx = sta->ampdu_mlme.tid_start_tx[tid];
  275. kfree(tid_tx);
  276. sta->ampdu_mlme.tid_start_tx[tid] = NULL;
  277. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  278. if (!tid_tx) {
  279. spin_unlock_bh(&sta->lock);
  280. return -ENOENT;
  281. }
  282. /*
  283. * if we're already stopping ignore any new requests to stop
  284. * unless we're destroying it in which case notify the driver
  285. */
  286. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  287. spin_unlock_bh(&sta->lock);
  288. if (reason != AGG_STOP_DESTROY_STA)
  289. return -EALREADY;
  290. params.action = IEEE80211_AMPDU_TX_STOP_FLUSH_CONT;
  291. ret = drv_ampdu_action(local, sta->sdata, &params);
  292. WARN_ON_ONCE(ret);
  293. return 0;
  294. }
  295. if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
  296. /* not even started yet! */
  297. ieee80211_assign_tid_tx(sta, tid, NULL);
  298. spin_unlock_bh(&sta->lock);
  299. kfree_rcu(tid_tx, rcu_head);
  300. return 0;
  301. }
  302. set_bit(HT_AGG_STATE_STOPPING, &tid_tx->state);
  303. ieee80211_agg_stop_txq(sta, tid);
  304. spin_unlock_bh(&sta->lock);
  305. ht_dbg(sta->sdata, "Tx BA session stop requested for %pM tid %u\n",
  306. sta->sta.addr, tid);
  307. timer_delete_sync(&tid_tx->addba_resp_timer);
  308. timer_delete_sync(&tid_tx->session_timer);
  309. /*
  310. * After this packets are no longer handed right through
  311. * to the driver but are put onto tid_tx->pending instead,
  312. * with locking to ensure proper access.
  313. */
  314. clear_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
  315. /*
  316. * There might be a few packets being processed right now (on
  317. * another CPU) that have already gotten past the aggregation
  318. * check when it was still OPERATIONAL and consequently have
  319. * IEEE80211_TX_CTL_AMPDU set. In that case, this code might
  320. * call into the driver at the same time or even before the
  321. * TX paths calls into it, which could confuse the driver.
  322. *
  323. * Wait for all currently running TX paths to finish before
  324. * telling the driver. New packets will not go through since
  325. * the aggregation session is no longer OPERATIONAL.
  326. */
  327. if (!local->in_reconfig)
  328. synchronize_net();
  329. tid_tx->stop_initiator = reason == AGG_STOP_PEER_REQUEST ?
  330. WLAN_BACK_RECIPIENT :
  331. WLAN_BACK_INITIATOR;
  332. tid_tx->tx_stop = reason == AGG_STOP_LOCAL_REQUEST;
  333. ret = drv_ampdu_action(local, sta->sdata, &params);
  334. /* HW shall not deny going back to legacy */
  335. if (WARN_ON(ret)) {
  336. /*
  337. * We may have pending packets get stuck in this case...
  338. * Not bothering with a workaround for now.
  339. */
  340. }
  341. /*
  342. * In the case of AGG_STOP_DESTROY_STA, the driver won't
  343. * necessarily call ieee80211_stop_tx_ba_cb(), so this may
  344. * seem like we can leave the tid_tx data pending forever.
  345. * This is true, in a way, but "forever" is only until the
  346. * station struct is actually destroyed. In the meantime,
  347. * leaving it around ensures that we don't transmit packets
  348. * to the driver on this TID which might confuse it.
  349. */
  350. return 0;
  351. }
  352. /*
  353. * After sending add Block Ack request we activated a timer until
  354. * add Block Ack response will arrive from the recipient.
  355. * If this timer expires sta_addba_resp_timer_expired will be executed.
  356. */
  357. static void sta_addba_resp_timer_expired(struct timer_list *t)
  358. {
  359. struct tid_ampdu_tx *tid_tx = timer_container_of(tid_tx, t,
  360. addba_resp_timer);
  361. struct sta_info *sta = tid_tx->sta;
  362. u8 tid = tid_tx->tid;
  363. /* check if the TID waits for addBA response */
  364. if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state)) {
  365. ht_dbg(sta->sdata,
  366. "timer expired on %pM tid %d not expecting addBA response\n",
  367. sta->sta.addr, tid);
  368. return;
  369. }
  370. ht_dbg(sta->sdata, "addBA response timer expired on %pM tid %d\n",
  371. sta->sta.addr, tid);
  372. ieee80211_stop_tx_ba_session(&sta->sta, tid);
  373. }
  374. static void ieee80211_send_addba_with_timeout(struct sta_info *sta,
  375. struct tid_ampdu_tx *tid_tx)
  376. {
  377. struct ieee80211_sub_if_data *sdata = sta->sdata;
  378. struct ieee80211_local *local = sta->local;
  379. u8 tid = tid_tx->tid;
  380. u16 buf_size;
  381. if (WARN_ON_ONCE(test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state) ||
  382. test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state)))
  383. return;
  384. lockdep_assert_wiphy(sta->local->hw.wiphy);
  385. /* activate the timer for the recipient's addBA response */
  386. mod_timer(&tid_tx->addba_resp_timer, jiffies + ADDBA_RESP_INTERVAL);
  387. ht_dbg(sdata, "activated addBA response timer on %pM tid %d\n",
  388. sta->sta.addr, tid);
  389. spin_lock_bh(&sta->lock);
  390. sta->ampdu_mlme.last_addba_req_time[tid] = jiffies;
  391. sta->ampdu_mlme.addba_req_num[tid]++;
  392. spin_unlock_bh(&sta->lock);
  393. if (sta->sta.valid_links ||
  394. sta->sta.deflink.eht_cap.has_eht ||
  395. ieee80211_hw_check(&local->hw, STRICT)) {
  396. buf_size = local->hw.max_tx_aggregation_subframes;
  397. } else if (sta->sta.deflink.he_cap.has_he) {
  398. buf_size = min_t(u16, local->hw.max_tx_aggregation_subframes,
  399. IEEE80211_MAX_AMPDU_BUF_HE);
  400. } else {
  401. /*
  402. * We really should use what the driver told us it will
  403. * transmit as the maximum, but certain APs (e.g. the
  404. * LinkSys WRT120N with FW v1.0.07 build 002 Jun 18 2012)
  405. * will crash when we use a lower number.
  406. */
  407. buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
  408. }
  409. /* send AddBA request */
  410. ieee80211_send_addba_request(sta, tid, tid_tx->dialog_token,
  411. tid_tx->ssn, buf_size, tid_tx->timeout);
  412. WARN_ON(test_and_set_bit(HT_AGG_STATE_SENT_ADDBA, &tid_tx->state));
  413. }
  414. void ieee80211_tx_ba_session_handle_start(struct sta_info *sta, int tid)
  415. {
  416. struct tid_ampdu_tx *tid_tx;
  417. struct ieee80211_local *local = sta->local;
  418. struct ieee80211_sub_if_data *sdata = sta->sdata;
  419. struct ieee80211_ampdu_params params = {
  420. .sta = &sta->sta,
  421. .action = IEEE80211_AMPDU_TX_START,
  422. .tid = tid,
  423. .buf_size = 0,
  424. .amsdu = false,
  425. .timeout = 0,
  426. };
  427. int ret;
  428. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  429. /*
  430. * Start queuing up packets for this aggregation session.
  431. * We're going to release them once the driver is OK with
  432. * that.
  433. */
  434. clear_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
  435. /*
  436. * Make sure no packets are being processed. This ensures that
  437. * we have a valid starting sequence number and that in-flight
  438. * packets have been flushed out and no packets for this TID
  439. * will go into the driver during the ampdu_action call.
  440. */
  441. synchronize_net();
  442. params.ssn = sta->tid_seq[tid] >> 4;
  443. ret = drv_ampdu_action(local, sdata, &params);
  444. tid_tx->ssn = params.ssn;
  445. if (ret == IEEE80211_AMPDU_TX_START_DELAY_ADDBA) {
  446. return;
  447. } else if (ret == IEEE80211_AMPDU_TX_START_IMMEDIATE) {
  448. /*
  449. * We didn't send the request yet, so don't need to check
  450. * here if we already got a response, just mark as driver
  451. * ready immediately.
  452. */
  453. set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state);
  454. } else if (ret) {
  455. ht_dbg(sdata,
  456. "BA request denied - HW unavailable for %pM tid %d\n",
  457. sta->sta.addr, tid);
  458. spin_lock_bh(&sta->lock);
  459. ieee80211_agg_splice_packets(sdata, tid_tx, tid);
  460. ieee80211_assign_tid_tx(sta, tid, NULL);
  461. ieee80211_agg_splice_finish(sdata, tid);
  462. spin_unlock_bh(&sta->lock);
  463. ieee80211_agg_start_txq(sta, tid, false);
  464. kfree_rcu(tid_tx, rcu_head);
  465. return;
  466. }
  467. ieee80211_send_addba_with_timeout(sta, tid_tx);
  468. }
  469. void ieee80211_refresh_tx_agg_session_timer(struct ieee80211_sta *pubsta,
  470. u16 tid)
  471. {
  472. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  473. struct tid_ampdu_tx *tid_tx;
  474. if (WARN_ON_ONCE(tid >= IEEE80211_NUM_TIDS))
  475. return;
  476. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  477. if (!tid_tx)
  478. return;
  479. tid_tx->last_tx = jiffies;
  480. }
  481. EXPORT_SYMBOL(ieee80211_refresh_tx_agg_session_timer);
  482. /*
  483. * After accepting the AddBA Response we activated a timer,
  484. * resetting it after each frame that we send.
  485. */
  486. static void sta_tx_agg_session_timer_expired(struct timer_list *t)
  487. {
  488. struct tid_ampdu_tx *tid_tx = timer_container_of(tid_tx, t,
  489. session_timer);
  490. struct sta_info *sta = tid_tx->sta;
  491. u8 tid = tid_tx->tid;
  492. unsigned long timeout;
  493. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  494. return;
  495. }
  496. timeout = tid_tx->last_tx + TU_TO_JIFFIES(tid_tx->timeout);
  497. if (time_is_after_jiffies(timeout)) {
  498. mod_timer(&tid_tx->session_timer, timeout);
  499. return;
  500. }
  501. ht_dbg(sta->sdata, "tx session timer expired on %pM tid %d\n",
  502. sta->sta.addr, tid);
  503. ieee80211_stop_tx_ba_session(&sta->sta, tid);
  504. }
  505. int ieee80211_start_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid,
  506. u16 timeout)
  507. {
  508. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  509. struct ieee80211_sub_if_data *sdata = sta->sdata;
  510. struct ieee80211_local *local = sdata->local;
  511. struct tid_ampdu_tx *tid_tx;
  512. int ret = 0;
  513. trace_api_start_tx_ba_session(pubsta, tid);
  514. if (WARN(sta->reserved_tid == tid,
  515. "Requested to start BA session on reserved tid=%d", tid))
  516. return -EINVAL;
  517. if (!pubsta->valid_links &&
  518. !pubsta->deflink.ht_cap.ht_supported &&
  519. !pubsta->deflink.vht_cap.vht_supported &&
  520. !pubsta->deflink.he_cap.has_he &&
  521. !pubsta->deflink.eht_cap.has_eht &&
  522. !pubsta->deflink.s1g_cap.s1g)
  523. return -EINVAL;
  524. if (WARN_ON_ONCE(!local->ops->ampdu_action))
  525. return -EINVAL;
  526. if ((tid >= IEEE80211_NUM_TIDS) ||
  527. !ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) ||
  528. ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW))
  529. return -EINVAL;
  530. if (WARN_ON(tid >= IEEE80211_FIRST_TSPEC_TSID))
  531. return -EINVAL;
  532. ht_dbg(sdata, "Open BA session requested for %pM tid %u\n",
  533. pubsta->addr, tid);
  534. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  535. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  536. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  537. sdata->vif.type != NL80211_IFTYPE_AP &&
  538. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  539. return -EINVAL;
  540. if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
  541. ht_dbg(sdata,
  542. "BA sessions blocked - Denying BA session request %pM tid %d\n",
  543. sta->sta.addr, tid);
  544. return -EINVAL;
  545. }
  546. if (test_sta_flag(sta, WLAN_STA_MFP) &&
  547. !test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
  548. ht_dbg(sdata,
  549. "MFP STA not authorized - deny BA session request %pM tid %d\n",
  550. sta->sta.addr, tid);
  551. return -EINVAL;
  552. }
  553. /*
  554. * 802.11n-2009 11.5.1.1: If the initiating STA is an HT STA, is a
  555. * member of an IBSS, and has no other existing Block Ack agreement
  556. * with the recipient STA, then the initiating STA shall transmit a
  557. * Probe Request frame to the recipient STA and shall not transmit an
  558. * ADDBA Request frame unless it receives a Probe Response frame
  559. * from the recipient within dot11ADDBAFailureTimeout.
  560. *
  561. * The probe request mechanism for ADDBA is currently not implemented,
  562. * but we only build up Block Ack session with HT STAs. This information
  563. * is set when we receive a bss info from a probe response or a beacon.
  564. */
  565. if (sta->sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  566. !sta->sta.deflink.ht_cap.ht_supported) {
  567. ht_dbg(sdata,
  568. "BA request denied - IBSS STA %pM does not advertise HT support\n",
  569. pubsta->addr);
  570. return -EINVAL;
  571. }
  572. spin_lock_bh(&sta->lock);
  573. /* we have tried too many times, receiver does not want A-MPDU */
  574. if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
  575. ret = -EBUSY;
  576. goto err_unlock_sta;
  577. }
  578. /*
  579. * if we have tried more than HT_AGG_BURST_RETRIES times we
  580. * will spread our requests in time to avoid stalling connection
  581. * for too long
  582. */
  583. if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_BURST_RETRIES &&
  584. time_before(jiffies, sta->ampdu_mlme.last_addba_req_time[tid] +
  585. HT_AGG_RETRIES_PERIOD)) {
  586. ht_dbg(sdata,
  587. "BA request denied - %d failed requests on %pM tid %u\n",
  588. sta->ampdu_mlme.addba_req_num[tid], sta->sta.addr, tid);
  589. ret = -EBUSY;
  590. goto err_unlock_sta;
  591. }
  592. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  593. /* check if the TID is not in aggregation flow already */
  594. if (tid_tx || sta->ampdu_mlme.tid_start_tx[tid]) {
  595. ht_dbg(sdata,
  596. "BA request denied - session is not idle on %pM tid %u\n",
  597. sta->sta.addr, tid);
  598. ret = -EAGAIN;
  599. goto err_unlock_sta;
  600. }
  601. /* prepare A-MPDU MLME for Tx aggregation */
  602. tid_tx = kzalloc_obj(struct tid_ampdu_tx, GFP_ATOMIC);
  603. if (!tid_tx) {
  604. ret = -ENOMEM;
  605. goto err_unlock_sta;
  606. }
  607. skb_queue_head_init(&tid_tx->pending);
  608. __set_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
  609. tid_tx->timeout = timeout;
  610. tid_tx->sta = sta;
  611. tid_tx->tid = tid;
  612. /* response timer */
  613. timer_setup(&tid_tx->addba_resp_timer, sta_addba_resp_timer_expired, 0);
  614. /* tx timer */
  615. timer_setup(&tid_tx->session_timer,
  616. sta_tx_agg_session_timer_expired, TIMER_DEFERRABLE);
  617. /* assign a dialog token */
  618. sta->ampdu_mlme.dialog_token_allocator++;
  619. tid_tx->dialog_token = sta->ampdu_mlme.dialog_token_allocator;
  620. /*
  621. * Finally, assign it to the start array; the work item will
  622. * collect it and move it to the normal array.
  623. */
  624. sta->ampdu_mlme.tid_start_tx[tid] = tid_tx;
  625. wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
  626. /* this flow continues off the work */
  627. err_unlock_sta:
  628. spin_unlock_bh(&sta->lock);
  629. return ret;
  630. }
  631. EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
  632. static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
  633. struct sta_info *sta, u16 tid)
  634. {
  635. struct tid_ampdu_tx *tid_tx;
  636. struct ieee80211_ampdu_params params = {
  637. .sta = &sta->sta,
  638. .action = IEEE80211_AMPDU_TX_OPERATIONAL,
  639. .tid = tid,
  640. .timeout = 0,
  641. .ssn = 0,
  642. };
  643. lockdep_assert_wiphy(sta->local->hw.wiphy);
  644. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  645. params.buf_size = tid_tx->buf_size;
  646. params.amsdu = tid_tx->amsdu;
  647. ht_dbg(sta->sdata, "Aggregation is on for %pM tid %d\n",
  648. sta->sta.addr, tid);
  649. drv_ampdu_action(local, sta->sdata, &params);
  650. /*
  651. * synchronize with TX path, while splicing the TX path
  652. * should block so it won't put more packets onto pending.
  653. */
  654. spin_lock_bh(&sta->lock);
  655. ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
  656. /*
  657. * Now mark as operational. This will be visible
  658. * in the TX path, and lets it go lock-free in
  659. * the common case.
  660. */
  661. set_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
  662. ieee80211_agg_splice_finish(sta->sdata, tid);
  663. spin_unlock_bh(&sta->lock);
  664. ieee80211_agg_start_txq(sta, tid, true);
  665. }
  666. void ieee80211_start_tx_ba_cb(struct sta_info *sta, int tid,
  667. struct tid_ampdu_tx *tid_tx)
  668. {
  669. struct ieee80211_sub_if_data *sdata = sta->sdata;
  670. struct ieee80211_local *local = sdata->local;
  671. lockdep_assert_wiphy(sta->local->hw.wiphy);
  672. if (WARN_ON(test_and_set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state)))
  673. return;
  674. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state) ||
  675. test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state))
  676. return;
  677. if (!test_bit(HT_AGG_STATE_SENT_ADDBA, &tid_tx->state)) {
  678. ieee80211_send_addba_with_timeout(sta, tid_tx);
  679. /* RESPONSE_RECEIVED state would trigger the flow again */
  680. return;
  681. }
  682. if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state))
  683. ieee80211_agg_tx_operational(local, sta, tid);
  684. }
  685. static struct tid_ampdu_tx *
  686. ieee80211_lookup_tid_tx(struct ieee80211_sub_if_data *sdata,
  687. const u8 *ra, u16 tid, struct sta_info **sta)
  688. {
  689. struct tid_ampdu_tx *tid_tx;
  690. if (tid >= IEEE80211_NUM_TIDS) {
  691. ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
  692. tid, IEEE80211_NUM_TIDS);
  693. return NULL;
  694. }
  695. *sta = sta_info_get_bss(sdata, ra);
  696. if (!*sta) {
  697. ht_dbg(sdata, "Could not find station: %pM\n", ra);
  698. return NULL;
  699. }
  700. tid_tx = rcu_dereference((*sta)->ampdu_mlme.tid_tx[tid]);
  701. if (WARN_ON(!tid_tx))
  702. ht_dbg(sdata, "addBA was not requested!\n");
  703. return tid_tx;
  704. }
  705. void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  706. const u8 *ra, u16 tid)
  707. {
  708. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  709. struct ieee80211_local *local = sdata->local;
  710. struct sta_info *sta;
  711. struct tid_ampdu_tx *tid_tx;
  712. trace_api_start_tx_ba_cb(sdata, ra, tid);
  713. rcu_read_lock();
  714. tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
  715. if (!tid_tx)
  716. goto out;
  717. set_bit(HT_AGG_STATE_START_CB, &tid_tx->state);
  718. wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
  719. out:
  720. rcu_read_unlock();
  721. }
  722. EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
  723. int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
  724. {
  725. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  726. struct ieee80211_sub_if_data *sdata = sta->sdata;
  727. struct ieee80211_local *local = sdata->local;
  728. struct tid_ampdu_tx *tid_tx;
  729. int ret = 0;
  730. trace_api_stop_tx_ba_session(pubsta, tid);
  731. if (!local->ops->ampdu_action)
  732. return -EINVAL;
  733. if (tid >= IEEE80211_NUM_TIDS)
  734. return -EINVAL;
  735. spin_lock_bh(&sta->lock);
  736. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  737. if (!tid_tx) {
  738. ret = -ENOENT;
  739. goto unlock;
  740. }
  741. WARN(sta->reserved_tid == tid,
  742. "Requested to stop BA session on reserved tid=%d", tid);
  743. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  744. /* already in progress stopping it */
  745. ret = 0;
  746. goto unlock;
  747. }
  748. set_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state);
  749. wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
  750. unlock:
  751. spin_unlock_bh(&sta->lock);
  752. return ret;
  753. }
  754. EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
  755. void ieee80211_stop_tx_ba_cb(struct sta_info *sta, int tid,
  756. struct tid_ampdu_tx *tid_tx)
  757. {
  758. struct ieee80211_sub_if_data *sdata = sta->sdata;
  759. bool send_delba = false;
  760. bool start_txq = false;
  761. ht_dbg(sdata, "Stopping Tx BA session for %pM tid %d\n",
  762. sta->sta.addr, tid);
  763. spin_lock_bh(&sta->lock);
  764. if (!test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  765. ht_dbg(sdata,
  766. "unexpected callback to A-MPDU stop for %pM tid %d\n",
  767. sta->sta.addr, tid);
  768. goto unlock_sta;
  769. }
  770. if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop)
  771. send_delba = true;
  772. ieee80211_remove_tid_tx(sta, tid);
  773. start_txq = true;
  774. unlock_sta:
  775. spin_unlock_bh(&sta->lock);
  776. if (start_txq)
  777. ieee80211_agg_start_txq(sta, tid, false);
  778. if (send_delba)
  779. ieee80211_send_delba(sdata, sta->sta.addr, tid,
  780. WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
  781. }
  782. void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  783. const u8 *ra, u16 tid)
  784. {
  785. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  786. struct ieee80211_local *local = sdata->local;
  787. struct sta_info *sta;
  788. struct tid_ampdu_tx *tid_tx;
  789. trace_api_stop_tx_ba_cb(sdata, ra, tid);
  790. rcu_read_lock();
  791. tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
  792. if (!tid_tx)
  793. goto out;
  794. set_bit(HT_AGG_STATE_STOP_CB, &tid_tx->state);
  795. wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
  796. out:
  797. rcu_read_unlock();
  798. }
  799. EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
  800. void ieee80211_process_addba_resp(struct ieee80211_local *local,
  801. struct sta_info *sta,
  802. struct ieee80211_mgmt *mgmt,
  803. size_t len)
  804. {
  805. struct tid_ampdu_tx *tid_tx;
  806. struct ieee80211_txq *txq;
  807. u16 capab, tid, buf_size;
  808. bool amsdu;
  809. lockdep_assert_wiphy(sta->local->hw.wiphy);
  810. capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
  811. amsdu = capab & IEEE80211_ADDBA_PARAM_AMSDU_MASK;
  812. tid = u16_get_bits(capab, IEEE80211_ADDBA_PARAM_TID_MASK);
  813. buf_size = u16_get_bits(capab, IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK);
  814. ieee80211_retrieve_addba_ext_data(sta,
  815. mgmt->u.action.u.addba_resp.variable,
  816. len - offsetof(typeof(*mgmt),
  817. u.action.u.addba_resp.variable),
  818. &buf_size);
  819. buf_size = min(buf_size, local->hw.max_tx_aggregation_subframes);
  820. txq = sta->sta.txq[tid];
  821. if (!amsdu && txq)
  822. set_bit(IEEE80211_TXQ_NO_AMSDU, &to_txq_info(txq)->flags);
  823. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  824. if (!tid_tx)
  825. return;
  826. if (mgmt->u.action.u.addba_resp.dialog_token != tid_tx->dialog_token) {
  827. ht_dbg(sta->sdata, "wrong addBA response token, %pM tid %d\n",
  828. sta->sta.addr, tid);
  829. return;
  830. }
  831. timer_delete_sync(&tid_tx->addba_resp_timer);
  832. ht_dbg(sta->sdata, "switched off addBA timer for %pM tid %d\n",
  833. sta->sta.addr, tid);
  834. /*
  835. * addba_resp_timer may have fired before we got here, and
  836. * caused WANT_STOP to be set. If the stop then was already
  837. * processed further, STOPPING might be set.
  838. */
  839. if (test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state) ||
  840. test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  841. ht_dbg(sta->sdata,
  842. "got addBA resp for %pM tid %d but we already gave up\n",
  843. sta->sta.addr, tid);
  844. return;
  845. }
  846. /*
  847. * IEEE 802.11-2007 7.3.1.14:
  848. * In an ADDBA Response frame, when the Status Code field
  849. * is set to 0, the Buffer Size subfield is set to a value
  850. * of at least 1.
  851. */
  852. if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
  853. == WLAN_STATUS_SUCCESS && buf_size) {
  854. if (test_and_set_bit(HT_AGG_STATE_RESPONSE_RECEIVED,
  855. &tid_tx->state)) {
  856. /* ignore duplicate response */
  857. return;
  858. }
  859. tid_tx->buf_size = buf_size;
  860. tid_tx->amsdu = amsdu;
  861. if (test_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state))
  862. ieee80211_agg_tx_operational(local, sta, tid);
  863. sta->ampdu_mlme.addba_req_num[tid] = 0;
  864. tid_tx->timeout =
  865. le16_to_cpu(mgmt->u.action.u.addba_resp.timeout);
  866. if (tid_tx->timeout) {
  867. mod_timer(&tid_tx->session_timer,
  868. TU_TO_EXP_TIME(tid_tx->timeout));
  869. tid_tx->last_tx = jiffies;
  870. }
  871. } else {
  872. __ieee80211_stop_tx_ba_session(sta, tid, AGG_STOP_DECLINED);
  873. }
  874. }