agg-tx.c 30 KB

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