agg-tx.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. * HT handling
  3. *
  4. * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
  5. * Copyright 2002-2005, Instant802 Networks, Inc.
  6. * Copyright 2005-2006, Devicescape Software, Inc.
  7. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  8. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  9. * Copyright 2007-2010, Intel Corporation
  10. * Copyright(c) 2015-2017 Intel Deutschland GmbH
  11. * Copyright (C) 2018 Intel Corporation
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #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. spin_unlock_bh(&sta->lock);
  315. ht_dbg(sta->sdata, "Tx BA session stop requested for %pM tid %u\n",
  316. sta->sta.addr, tid);
  317. del_timer_sync(&tid_tx->addba_resp_timer);
  318. del_timer_sync(&tid_tx->session_timer);
  319. /*
  320. * After this packets are no longer handed right through
  321. * to the driver but are put onto tid_tx->pending instead,
  322. * with locking to ensure proper access.
  323. */
  324. clear_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
  325. /*
  326. * There might be a few packets being processed right now (on
  327. * another CPU) that have already gotten past the aggregation
  328. * check when it was still OPERATIONAL and consequently have
  329. * IEEE80211_TX_CTL_AMPDU set. In that case, this code might
  330. * call into the driver at the same time or even before the
  331. * TX paths calls into it, which could confuse the driver.
  332. *
  333. * Wait for all currently running TX paths to finish before
  334. * telling the driver. New packets will not go through since
  335. * the aggregation session is no longer OPERATIONAL.
  336. */
  337. if (!local->in_reconfig)
  338. synchronize_net();
  339. tid_tx->stop_initiator = reason == AGG_STOP_PEER_REQUEST ?
  340. WLAN_BACK_RECIPIENT :
  341. WLAN_BACK_INITIATOR;
  342. tid_tx->tx_stop = reason == AGG_STOP_LOCAL_REQUEST;
  343. ret = drv_ampdu_action(local, sta->sdata, &params);
  344. /* HW shall not deny going back to legacy */
  345. if (WARN_ON(ret)) {
  346. /*
  347. * We may have pending packets get stuck in this case...
  348. * Not bothering with a workaround for now.
  349. */
  350. }
  351. /*
  352. * In the case of AGG_STOP_DESTROY_STA, the driver won't
  353. * necessarily call ieee80211_stop_tx_ba_cb(), so this may
  354. * seem like we can leave the tid_tx data pending forever.
  355. * This is true, in a way, but "forever" is only until the
  356. * station struct is actually destroyed. In the meantime,
  357. * leaving it around ensures that we don't transmit packets
  358. * to the driver on this TID which might confuse it.
  359. */
  360. return 0;
  361. }
  362. /*
  363. * After sending add Block Ack request we activated a timer until
  364. * add Block Ack response will arrive from the recipient.
  365. * If this timer expires sta_addba_resp_timer_expired will be executed.
  366. */
  367. static void sta_addba_resp_timer_expired(struct timer_list *t)
  368. {
  369. struct tid_ampdu_tx *tid_tx = from_timer(tid_tx, t, addba_resp_timer);
  370. struct sta_info *sta = tid_tx->sta;
  371. u8 tid = tid_tx->tid;
  372. /* check if the TID waits for addBA response */
  373. if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state)) {
  374. ht_dbg(sta->sdata,
  375. "timer expired on %pM tid %d not expecting addBA response\n",
  376. sta->sta.addr, tid);
  377. return;
  378. }
  379. ht_dbg(sta->sdata, "addBA response timer expired on %pM tid %d\n",
  380. sta->sta.addr, tid);
  381. ieee80211_stop_tx_ba_session(&sta->sta, tid);
  382. }
  383. void ieee80211_tx_ba_session_handle_start(struct sta_info *sta, int tid)
  384. {
  385. struct tid_ampdu_tx *tid_tx;
  386. struct ieee80211_local *local = sta->local;
  387. struct ieee80211_sub_if_data *sdata = sta->sdata;
  388. struct ieee80211_ampdu_params params = {
  389. .sta = &sta->sta,
  390. .action = IEEE80211_AMPDU_TX_START,
  391. .tid = tid,
  392. .buf_size = 0,
  393. .amsdu = false,
  394. .timeout = 0,
  395. };
  396. int ret;
  397. u16 buf_size;
  398. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  399. /*
  400. * Start queuing up packets for this aggregation session.
  401. * We're going to release them once the driver is OK with
  402. * that.
  403. */
  404. clear_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
  405. ieee80211_agg_stop_txq(sta, tid);
  406. /*
  407. * Make sure no packets are being processed. This ensures that
  408. * we have a valid starting sequence number and that in-flight
  409. * packets have been flushed out and no packets for this TID
  410. * will go into the driver during the ampdu_action call.
  411. */
  412. synchronize_net();
  413. params.ssn = sta->tid_seq[tid] >> 4;
  414. ret = drv_ampdu_action(local, sdata, &params);
  415. if (ret) {
  416. ht_dbg(sdata,
  417. "BA request denied - HW unavailable for %pM tid %d\n",
  418. sta->sta.addr, tid);
  419. spin_lock_bh(&sta->lock);
  420. ieee80211_agg_splice_packets(sdata, tid_tx, tid);
  421. ieee80211_assign_tid_tx(sta, tid, NULL);
  422. ieee80211_agg_splice_finish(sdata, tid);
  423. spin_unlock_bh(&sta->lock);
  424. ieee80211_agg_start_txq(sta, tid, false);
  425. kfree_rcu(tid_tx, rcu_head);
  426. return;
  427. }
  428. /* activate the timer for the recipient's addBA response */
  429. mod_timer(&tid_tx->addba_resp_timer, jiffies + ADDBA_RESP_INTERVAL);
  430. ht_dbg(sdata, "activated addBA response timer on %pM tid %d\n",
  431. sta->sta.addr, tid);
  432. spin_lock_bh(&sta->lock);
  433. sta->ampdu_mlme.last_addba_req_time[tid] = jiffies;
  434. sta->ampdu_mlme.addba_req_num[tid]++;
  435. spin_unlock_bh(&sta->lock);
  436. if (sta->sta.he_cap.has_he) {
  437. buf_size = local->hw.max_tx_aggregation_subframes;
  438. } else {
  439. /*
  440. * We really should use what the driver told us it will
  441. * transmit as the maximum, but certain APs (e.g. the
  442. * LinkSys WRT120N with FW v1.0.07 build 002 Jun 18 2012)
  443. * will crash when we use a lower number.
  444. */
  445. buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
  446. }
  447. /* send AddBA request */
  448. ieee80211_send_addba_request(sdata, sta->sta.addr, tid,
  449. tid_tx->dialog_token, params.ssn,
  450. buf_size, tid_tx->timeout);
  451. }
  452. /*
  453. * After accepting the AddBA Response we activated a timer,
  454. * resetting it after each frame that we send.
  455. */
  456. static void sta_tx_agg_session_timer_expired(struct timer_list *t)
  457. {
  458. struct tid_ampdu_tx *tid_tx = from_timer(tid_tx, t, session_timer);
  459. struct sta_info *sta = tid_tx->sta;
  460. u8 tid = tid_tx->tid;
  461. unsigned long timeout;
  462. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  463. return;
  464. }
  465. timeout = tid_tx->last_tx + TU_TO_JIFFIES(tid_tx->timeout);
  466. if (time_is_after_jiffies(timeout)) {
  467. mod_timer(&tid_tx->session_timer, timeout);
  468. return;
  469. }
  470. ht_dbg(sta->sdata, "tx session timer expired on %pM tid %d\n",
  471. sta->sta.addr, tid);
  472. ieee80211_stop_tx_ba_session(&sta->sta, tid);
  473. }
  474. int ieee80211_start_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid,
  475. u16 timeout)
  476. {
  477. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  478. struct ieee80211_sub_if_data *sdata = sta->sdata;
  479. struct ieee80211_local *local = sdata->local;
  480. struct tid_ampdu_tx *tid_tx;
  481. int ret = 0;
  482. trace_api_start_tx_ba_session(pubsta, tid);
  483. if (WARN(sta->reserved_tid == tid,
  484. "Requested to start BA session on reserved tid=%d", tid))
  485. return -EINVAL;
  486. if (!pubsta->ht_cap.ht_supported)
  487. return -EINVAL;
  488. if (WARN_ON_ONCE(!local->ops->ampdu_action))
  489. return -EINVAL;
  490. if ((tid >= IEEE80211_NUM_TIDS) ||
  491. !ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) ||
  492. ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW))
  493. return -EINVAL;
  494. if (WARN_ON(tid >= IEEE80211_FIRST_TSPEC_TSID))
  495. return -EINVAL;
  496. ht_dbg(sdata, "Open BA session requested for %pM tid %u\n",
  497. pubsta->addr, tid);
  498. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  499. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  500. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  501. sdata->vif.type != NL80211_IFTYPE_AP &&
  502. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  503. return -EINVAL;
  504. if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
  505. ht_dbg(sdata,
  506. "BA sessions blocked - Denying BA session request %pM tid %d\n",
  507. sta->sta.addr, tid);
  508. return -EINVAL;
  509. }
  510. /*
  511. * 802.11n-2009 11.5.1.1: If the initiating STA is an HT STA, is a
  512. * member of an IBSS, and has no other existing Block Ack agreement
  513. * with the recipient STA, then the initiating STA shall transmit a
  514. * Probe Request frame to the recipient STA and shall not transmit an
  515. * ADDBA Request frame unless it receives a Probe Response frame
  516. * from the recipient within dot11ADDBAFailureTimeout.
  517. *
  518. * The probe request mechanism for ADDBA is currently not implemented,
  519. * but we only build up Block Ack session with HT STAs. This information
  520. * is set when we receive a bss info from a probe response or a beacon.
  521. */
  522. if (sta->sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  523. !sta->sta.ht_cap.ht_supported) {
  524. ht_dbg(sdata,
  525. "BA request denied - IBSS STA %pM does not advertise HT support\n",
  526. pubsta->addr);
  527. return -EINVAL;
  528. }
  529. spin_lock_bh(&sta->lock);
  530. /* we have tried too many times, receiver does not want A-MPDU */
  531. if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
  532. ret = -EBUSY;
  533. goto err_unlock_sta;
  534. }
  535. /*
  536. * if we have tried more than HT_AGG_BURST_RETRIES times we
  537. * will spread our requests in time to avoid stalling connection
  538. * for too long
  539. */
  540. if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_BURST_RETRIES &&
  541. time_before(jiffies, sta->ampdu_mlme.last_addba_req_time[tid] +
  542. HT_AGG_RETRIES_PERIOD)) {
  543. ht_dbg(sdata,
  544. "BA request denied - %d failed requests on %pM tid %u\n",
  545. sta->ampdu_mlme.addba_req_num[tid], sta->sta.addr, tid);
  546. ret = -EBUSY;
  547. goto err_unlock_sta;
  548. }
  549. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  550. /* check if the TID is not in aggregation flow already */
  551. if (tid_tx || sta->ampdu_mlme.tid_start_tx[tid]) {
  552. ht_dbg(sdata,
  553. "BA request denied - session is not idle on %pM tid %u\n",
  554. sta->sta.addr, tid);
  555. ret = -EAGAIN;
  556. goto err_unlock_sta;
  557. }
  558. /* prepare A-MPDU MLME for Tx aggregation */
  559. tid_tx = kzalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
  560. if (!tid_tx) {
  561. ret = -ENOMEM;
  562. goto err_unlock_sta;
  563. }
  564. skb_queue_head_init(&tid_tx->pending);
  565. __set_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
  566. tid_tx->timeout = timeout;
  567. tid_tx->sta = sta;
  568. tid_tx->tid = tid;
  569. /* response timer */
  570. timer_setup(&tid_tx->addba_resp_timer, sta_addba_resp_timer_expired, 0);
  571. /* tx timer */
  572. timer_setup(&tid_tx->session_timer,
  573. sta_tx_agg_session_timer_expired, TIMER_DEFERRABLE);
  574. /* assign a dialog token */
  575. sta->ampdu_mlme.dialog_token_allocator++;
  576. tid_tx->dialog_token = sta->ampdu_mlme.dialog_token_allocator;
  577. /*
  578. * Finally, assign it to the start array; the work item will
  579. * collect it and move it to the normal array.
  580. */
  581. sta->ampdu_mlme.tid_start_tx[tid] = tid_tx;
  582. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  583. /* this flow continues off the work */
  584. err_unlock_sta:
  585. spin_unlock_bh(&sta->lock);
  586. return ret;
  587. }
  588. EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
  589. static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
  590. struct sta_info *sta, u16 tid)
  591. {
  592. struct tid_ampdu_tx *tid_tx;
  593. struct ieee80211_ampdu_params params = {
  594. .sta = &sta->sta,
  595. .action = IEEE80211_AMPDU_TX_OPERATIONAL,
  596. .tid = tid,
  597. .timeout = 0,
  598. .ssn = 0,
  599. };
  600. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  601. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  602. params.buf_size = tid_tx->buf_size;
  603. params.amsdu = tid_tx->amsdu;
  604. ht_dbg(sta->sdata, "Aggregation is on for %pM tid %d\n",
  605. sta->sta.addr, tid);
  606. drv_ampdu_action(local, sta->sdata, &params);
  607. /*
  608. * synchronize with TX path, while splicing the TX path
  609. * should block so it won't put more packets onto pending.
  610. */
  611. spin_lock_bh(&sta->lock);
  612. ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
  613. /*
  614. * Now mark as operational. This will be visible
  615. * in the TX path, and lets it go lock-free in
  616. * the common case.
  617. */
  618. set_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
  619. ieee80211_agg_splice_finish(sta->sdata, tid);
  620. spin_unlock_bh(&sta->lock);
  621. ieee80211_agg_start_txq(sta, tid, true);
  622. }
  623. void ieee80211_start_tx_ba_cb(struct sta_info *sta, int tid,
  624. struct tid_ampdu_tx *tid_tx)
  625. {
  626. struct ieee80211_sub_if_data *sdata = sta->sdata;
  627. struct ieee80211_local *local = sdata->local;
  628. if (WARN_ON(test_and_set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state)))
  629. return;
  630. if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state))
  631. ieee80211_agg_tx_operational(local, sta, tid);
  632. }
  633. static struct tid_ampdu_tx *
  634. ieee80211_lookup_tid_tx(struct ieee80211_sub_if_data *sdata,
  635. const u8 *ra, u16 tid, struct sta_info **sta)
  636. {
  637. struct tid_ampdu_tx *tid_tx;
  638. if (tid >= IEEE80211_NUM_TIDS) {
  639. ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
  640. tid, IEEE80211_NUM_TIDS);
  641. return NULL;
  642. }
  643. *sta = sta_info_get_bss(sdata, ra);
  644. if (!*sta) {
  645. ht_dbg(sdata, "Could not find station: %pM\n", ra);
  646. return NULL;
  647. }
  648. tid_tx = rcu_dereference((*sta)->ampdu_mlme.tid_tx[tid]);
  649. if (WARN_ON(!tid_tx))
  650. ht_dbg(sdata, "addBA was not requested!\n");
  651. return tid_tx;
  652. }
  653. void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  654. const u8 *ra, u16 tid)
  655. {
  656. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  657. struct ieee80211_local *local = sdata->local;
  658. struct sta_info *sta;
  659. struct tid_ampdu_tx *tid_tx;
  660. trace_api_start_tx_ba_cb(sdata, ra, tid);
  661. rcu_read_lock();
  662. tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
  663. if (!tid_tx)
  664. goto out;
  665. set_bit(HT_AGG_STATE_START_CB, &tid_tx->state);
  666. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  667. out:
  668. rcu_read_unlock();
  669. }
  670. EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
  671. int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
  672. enum ieee80211_agg_stop_reason reason)
  673. {
  674. int ret;
  675. mutex_lock(&sta->ampdu_mlme.mtx);
  676. ret = ___ieee80211_stop_tx_ba_session(sta, tid, reason);
  677. mutex_unlock(&sta->ampdu_mlme.mtx);
  678. return ret;
  679. }
  680. int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
  681. {
  682. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  683. struct ieee80211_sub_if_data *sdata = sta->sdata;
  684. struct ieee80211_local *local = sdata->local;
  685. struct tid_ampdu_tx *tid_tx;
  686. int ret = 0;
  687. trace_api_stop_tx_ba_session(pubsta, tid);
  688. if (!local->ops->ampdu_action)
  689. return -EINVAL;
  690. if (tid >= IEEE80211_NUM_TIDS)
  691. return -EINVAL;
  692. spin_lock_bh(&sta->lock);
  693. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  694. if (!tid_tx) {
  695. ret = -ENOENT;
  696. goto unlock;
  697. }
  698. WARN(sta->reserved_tid == tid,
  699. "Requested to stop BA session on reserved tid=%d", tid);
  700. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  701. /* already in progress stopping it */
  702. ret = 0;
  703. goto unlock;
  704. }
  705. set_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state);
  706. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  707. unlock:
  708. spin_unlock_bh(&sta->lock);
  709. return ret;
  710. }
  711. EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
  712. void ieee80211_stop_tx_ba_cb(struct sta_info *sta, int tid,
  713. struct tid_ampdu_tx *tid_tx)
  714. {
  715. struct ieee80211_sub_if_data *sdata = sta->sdata;
  716. bool send_delba = false;
  717. ht_dbg(sdata, "Stopping Tx BA session for %pM tid %d\n",
  718. sta->sta.addr, tid);
  719. spin_lock_bh(&sta->lock);
  720. if (!test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  721. ht_dbg(sdata,
  722. "unexpected callback to A-MPDU stop for %pM tid %d\n",
  723. sta->sta.addr, tid);
  724. goto unlock_sta;
  725. }
  726. if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop)
  727. send_delba = true;
  728. ieee80211_remove_tid_tx(sta, tid);
  729. unlock_sta:
  730. spin_unlock_bh(&sta->lock);
  731. if (send_delba)
  732. ieee80211_send_delba(sdata, sta->sta.addr, tid,
  733. WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
  734. }
  735. void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  736. const u8 *ra, u16 tid)
  737. {
  738. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  739. struct ieee80211_local *local = sdata->local;
  740. struct sta_info *sta;
  741. struct tid_ampdu_tx *tid_tx;
  742. trace_api_stop_tx_ba_cb(sdata, ra, tid);
  743. rcu_read_lock();
  744. tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
  745. if (!tid_tx)
  746. goto out;
  747. set_bit(HT_AGG_STATE_STOP_CB, &tid_tx->state);
  748. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  749. out:
  750. rcu_read_unlock();
  751. }
  752. EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
  753. void ieee80211_process_addba_resp(struct ieee80211_local *local,
  754. struct sta_info *sta,
  755. struct ieee80211_mgmt *mgmt,
  756. size_t len)
  757. {
  758. struct tid_ampdu_tx *tid_tx;
  759. struct ieee80211_txq *txq;
  760. u16 capab, tid, buf_size;
  761. bool amsdu;
  762. capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
  763. amsdu = capab & IEEE80211_ADDBA_PARAM_AMSDU_MASK;
  764. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  765. buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
  766. buf_size = min(buf_size, local->hw.max_tx_aggregation_subframes);
  767. txq = sta->sta.txq[tid];
  768. if (!amsdu && txq)
  769. set_bit(IEEE80211_TXQ_NO_AMSDU, &to_txq_info(txq)->flags);
  770. mutex_lock(&sta->ampdu_mlme.mtx);
  771. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  772. if (!tid_tx)
  773. goto out;
  774. if (mgmt->u.action.u.addba_resp.dialog_token != tid_tx->dialog_token) {
  775. ht_dbg(sta->sdata, "wrong addBA response token, %pM tid %d\n",
  776. sta->sta.addr, tid);
  777. goto out;
  778. }
  779. del_timer_sync(&tid_tx->addba_resp_timer);
  780. ht_dbg(sta->sdata, "switched off addBA timer for %pM tid %d\n",
  781. sta->sta.addr, tid);
  782. /*
  783. * addba_resp_timer may have fired before we got here, and
  784. * caused WANT_STOP to be set. If the stop then was already
  785. * processed further, STOPPING might be set.
  786. */
  787. if (test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state) ||
  788. test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  789. ht_dbg(sta->sdata,
  790. "got addBA resp for %pM tid %d but we already gave up\n",
  791. sta->sta.addr, tid);
  792. goto out;
  793. }
  794. /*
  795. * IEEE 802.11-2007 7.3.1.14:
  796. * In an ADDBA Response frame, when the Status Code field
  797. * is set to 0, the Buffer Size subfield is set to a value
  798. * of at least 1.
  799. */
  800. if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
  801. == WLAN_STATUS_SUCCESS && buf_size) {
  802. if (test_and_set_bit(HT_AGG_STATE_RESPONSE_RECEIVED,
  803. &tid_tx->state)) {
  804. /* ignore duplicate response */
  805. goto out;
  806. }
  807. tid_tx->buf_size = buf_size;
  808. tid_tx->amsdu = amsdu;
  809. if (test_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state))
  810. ieee80211_agg_tx_operational(local, sta, tid);
  811. sta->ampdu_mlme.addba_req_num[tid] = 0;
  812. tid_tx->timeout =
  813. le16_to_cpu(mgmt->u.action.u.addba_resp.timeout);
  814. if (tid_tx->timeout) {
  815. mod_timer(&tid_tx->session_timer,
  816. TU_TO_EXP_TIME(tid_tx->timeout));
  817. tid_tx->last_tx = jiffies;
  818. }
  819. } else {
  820. ___ieee80211_stop_tx_ba_session(sta, tid, AGG_STOP_DECLINED);
  821. }
  822. out:
  823. mutex_unlock(&sta->ampdu_mlme.mtx);
  824. }