agg-tx.c 30 KB

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