agg-tx.c 29 KB

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