agg-tx.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/ieee80211.h>
  17. #include <linux/slab.h>
  18. #include <linux/export.h>
  19. #include <net/mac80211.h>
  20. #include "ieee80211_i.h"
  21. #include "driver-ops.h"
  22. #include "wme.h"
  23. /**
  24. * DOC: TX A-MPDU aggregation
  25. *
  26. * Aggregation on the TX side requires setting the hardware flag
  27. * %IEEE80211_HW_AMPDU_AGGREGATION. The driver will then be handed
  28. * packets with a flag indicating A-MPDU aggregation. The driver
  29. * or device is responsible for actually aggregating the frames,
  30. * as well as deciding how many and which to aggregate.
  31. *
  32. * When TX aggregation is started by some subsystem (usually the rate
  33. * control algorithm would be appropriate) by calling the
  34. * ieee80211_start_tx_ba_session() function, the driver will be
  35. * notified via its @ampdu_action function, with the
  36. * %IEEE80211_AMPDU_TX_START action.
  37. *
  38. * In response to that, the driver is later required to call the
  39. * ieee80211_start_tx_ba_cb_irqsafe() function, which will really
  40. * start the aggregation session after the peer has also responded.
  41. * If the peer responds negatively, the session will be stopped
  42. * again right away. Note that it is possible for the aggregation
  43. * session to be stopped before the driver has indicated that it
  44. * is done setting it up, in which case it must not indicate the
  45. * setup completion.
  46. *
  47. * Also note that, since we also need to wait for a response from
  48. * the peer, the driver is notified of the completion of the
  49. * handshake by the %IEEE80211_AMPDU_TX_OPERATIONAL action to the
  50. * @ampdu_action callback.
  51. *
  52. * Similarly, when the aggregation session is stopped by the peer
  53. * or something calling ieee80211_stop_tx_ba_session(), the driver's
  54. * @ampdu_action function will be called with the action
  55. * %IEEE80211_AMPDU_TX_STOP. In this case, the call must not fail,
  56. * and the driver must later call ieee80211_stop_tx_ba_cb_irqsafe().
  57. * Note that the sta can get destroyed before the BA tear down is
  58. * complete.
  59. */
  60. static void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata,
  61. const u8 *da, u16 tid,
  62. u8 dialog_token, u16 start_seq_num,
  63. u16 agg_size, u16 timeout)
  64. {
  65. struct ieee80211_local *local = sdata->local;
  66. struct sk_buff *skb;
  67. struct ieee80211_mgmt *mgmt;
  68. u16 capab;
  69. skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
  70. if (!skb)
  71. return;
  72. skb_reserve(skb, local->hw.extra_tx_headroom);
  73. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  74. memset(mgmt, 0, 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 = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
  113. memset(bar, 0, sizeof(*bar));
  114. bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  115. IEEE80211_STYPE_BACK_REQ);
  116. memcpy(bar->ra, ra, ETH_ALEN);
  117. memcpy(bar->ta, sdata->vif.addr, ETH_ALEN);
  118. bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
  119. bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
  120. bar_control |= (u16)(tid << IEEE80211_BAR_CTRL_TID_INFO_SHIFT);
  121. bar->control = cpu_to_le16(bar_control);
  122. bar->start_seq_num = cpu_to_le16(ssn);
  123. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  124. IEEE80211_TX_CTL_REQ_TX_STATUS;
  125. ieee80211_tx_skb_tid(sdata, skb, tid);
  126. }
  127. EXPORT_SYMBOL(ieee80211_send_bar);
  128. void ieee80211_assign_tid_tx(struct sta_info *sta, int tid,
  129. struct tid_ampdu_tx *tid_tx)
  130. {
  131. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  132. lockdep_assert_held(&sta->lock);
  133. rcu_assign_pointer(sta->ampdu_mlme.tid_tx[tid], tid_tx);
  134. }
  135. /*
  136. * When multiple aggregation sessions on multiple stations
  137. * are being created/destroyed simultaneously, we need to
  138. * refcount the global queue stop caused by that in order
  139. * to not get into a situation where one of the aggregation
  140. * setup or teardown re-enables queues before the other is
  141. * ready to handle that.
  142. *
  143. * These two functions take care of this issue by keeping
  144. * a global "agg_queue_stop" refcount.
  145. */
  146. static void __acquires(agg_queue)
  147. ieee80211_stop_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
  148. {
  149. int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
  150. /* we do refcounting here, so don't use the queue reason refcounting */
  151. if (atomic_inc_return(&sdata->local->agg_queue_stop[queue]) == 1)
  152. ieee80211_stop_queue_by_reason(
  153. &sdata->local->hw, queue,
  154. IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
  155. false);
  156. __acquire(agg_queue);
  157. }
  158. static void __releases(agg_queue)
  159. ieee80211_wake_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
  160. {
  161. int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
  162. if (atomic_dec_return(&sdata->local->agg_queue_stop[queue]) == 0)
  163. ieee80211_wake_queue_by_reason(
  164. &sdata->local->hw, queue,
  165. IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
  166. false);
  167. __release(agg_queue);
  168. }
  169. static void
  170. ieee80211_agg_stop_txq(struct sta_info *sta, int tid)
  171. {
  172. struct ieee80211_txq *txq = sta->sta.txq[tid];
  173. struct ieee80211_sub_if_data *sdata;
  174. struct fq *fq;
  175. struct txq_info *txqi;
  176. if (!txq)
  177. return;
  178. txqi = to_txq_info(txq);
  179. sdata = vif_to_sdata(txq->vif);
  180. fq = &sdata->local->fq;
  181. /* Lock here to protect against further seqno updates on dequeue */
  182. spin_lock_bh(&fq->lock);
  183. set_bit(IEEE80211_TXQ_STOP, &txqi->flags);
  184. spin_unlock_bh(&fq->lock);
  185. }
  186. static void
  187. ieee80211_agg_start_txq(struct sta_info *sta, int tid, bool enable)
  188. {
  189. struct ieee80211_txq *txq = sta->sta.txq[tid];
  190. struct txq_info *txqi;
  191. if (!txq)
  192. return;
  193. txqi = to_txq_info(txq);
  194. if (enable)
  195. set_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
  196. else
  197. clear_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
  198. clear_bit(IEEE80211_TXQ_STOP, &txqi->flags);
  199. drv_wake_tx_queue(sta->sdata->local, txqi);
  200. }
  201. /*
  202. * splice packets from the STA's pending to the local pending,
  203. * requires a call to ieee80211_agg_splice_finish later
  204. */
  205. static void __acquires(agg_queue)
  206. ieee80211_agg_splice_packets(struct ieee80211_sub_if_data *sdata,
  207. struct tid_ampdu_tx *tid_tx, u16 tid)
  208. {
  209. struct ieee80211_local *local = sdata->local;
  210. int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
  211. unsigned long flags;
  212. ieee80211_stop_queue_agg(sdata, tid);
  213. if (WARN(!tid_tx,
  214. "TID %d gone but expected when splicing aggregates from the pending queue\n",
  215. tid))
  216. return;
  217. if (!skb_queue_empty(&tid_tx->pending)) {
  218. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  219. /* copy over remaining packets */
  220. skb_queue_splice_tail_init(&tid_tx->pending,
  221. &local->pending[queue]);
  222. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  223. }
  224. }
  225. static void __releases(agg_queue)
  226. ieee80211_agg_splice_finish(struct ieee80211_sub_if_data *sdata, u16 tid)
  227. {
  228. ieee80211_wake_queue_agg(sdata, tid);
  229. }
  230. static void ieee80211_remove_tid_tx(struct sta_info *sta, int tid)
  231. {
  232. struct tid_ampdu_tx *tid_tx;
  233. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  234. lockdep_assert_held(&sta->lock);
  235. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  236. /*
  237. * When we get here, the TX path will not be lockless any more wrt.
  238. * aggregation, since the OPERATIONAL bit has long been cleared.
  239. * Thus it will block on getting the lock, if it occurs. So if we
  240. * stop the queue now, we will not get any more packets, and any
  241. * that might be being processed will wait for us here, thereby
  242. * guaranteeing that no packets go to the tid_tx pending queue any
  243. * more.
  244. */
  245. ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
  246. /* future packets must not find the tid_tx struct any more */
  247. ieee80211_assign_tid_tx(sta, tid, NULL);
  248. ieee80211_agg_splice_finish(sta->sdata, tid);
  249. ieee80211_agg_start_txq(sta, tid, false);
  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. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  282. if (!tid_tx) {
  283. spin_unlock_bh(&sta->lock);
  284. return -ENOENT;
  285. }
  286. /*
  287. * if we're already stopping ignore any new requests to stop
  288. * unless we're destroying it in which case notify the driver
  289. */
  290. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  291. spin_unlock_bh(&sta->lock);
  292. if (reason != AGG_STOP_DESTROY_STA)
  293. return -EALREADY;
  294. params.action = IEEE80211_AMPDU_TX_STOP_FLUSH_CONT;
  295. ret = drv_ampdu_action(local, sta->sdata, &params);
  296. WARN_ON_ONCE(ret);
  297. return 0;
  298. }
  299. if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
  300. /* not even started yet! */
  301. ieee80211_assign_tid_tx(sta, tid, NULL);
  302. spin_unlock_bh(&sta->lock);
  303. kfree_rcu(tid_tx, rcu_head);
  304. return 0;
  305. }
  306. set_bit(HT_AGG_STATE_STOPPING, &tid_tx->state);
  307. spin_unlock_bh(&sta->lock);
  308. ht_dbg(sta->sdata, "Tx BA session stop requested for %pM tid %u\n",
  309. sta->sta.addr, tid);
  310. del_timer_sync(&tid_tx->addba_resp_timer);
  311. del_timer_sync(&tid_tx->session_timer);
  312. /*
  313. * After this packets are no longer handed right through
  314. * to the driver but are put onto tid_tx->pending instead,
  315. * with locking to ensure proper access.
  316. */
  317. clear_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
  318. /*
  319. * There might be a few packets being processed right now (on
  320. * another CPU) that have already gotten past the aggregation
  321. * check when it was still OPERATIONAL and consequently have
  322. * IEEE80211_TX_CTL_AMPDU set. In that case, this code might
  323. * call into the driver at the same time or even before the
  324. * TX paths calls into it, which could confuse the driver.
  325. *
  326. * Wait for all currently running TX paths to finish before
  327. * telling the driver. New packets will not go through since
  328. * the aggregation session is no longer OPERATIONAL.
  329. */
  330. synchronize_net();
  331. tid_tx->stop_initiator = reason == AGG_STOP_PEER_REQUEST ?
  332. WLAN_BACK_RECIPIENT :
  333. WLAN_BACK_INITIATOR;
  334. tid_tx->tx_stop = reason == AGG_STOP_LOCAL_REQUEST;
  335. ret = drv_ampdu_action(local, sta->sdata, &params);
  336. /* HW shall not deny going back to legacy */
  337. if (WARN_ON(ret)) {
  338. /*
  339. * We may have pending packets get stuck in this case...
  340. * Not bothering with a workaround for now.
  341. */
  342. }
  343. /*
  344. * In the case of AGG_STOP_DESTROY_STA, the driver won't
  345. * necessarily call ieee80211_stop_tx_ba_cb(), so this may
  346. * seem like we can leave the tid_tx data pending forever.
  347. * This is true, in a way, but "forever" is only until the
  348. * station struct is actually destroyed. In the meantime,
  349. * leaving it around ensures that we don't transmit packets
  350. * to the driver on this TID which might confuse it.
  351. */
  352. return 0;
  353. }
  354. /*
  355. * After sending add Block Ack request we activated a timer until
  356. * add Block Ack response will arrive from the recipient.
  357. * If this timer expires sta_addba_resp_timer_expired will be executed.
  358. */
  359. static void sta_addba_resp_timer_expired(unsigned long data)
  360. {
  361. /* not an elegant detour, but there is no choice as the timer passes
  362. * only one argument, and both sta_info and TID are needed, so init
  363. * flow in sta_info_create gives the TID as data, while the timer_to_id
  364. * array gives the sta through container_of */
  365. u16 tid = *(u8 *)data;
  366. struct sta_info *sta = container_of((void *)data,
  367. struct sta_info, timer_to_tid[tid]);
  368. struct tid_ampdu_tx *tid_tx;
  369. /* check if the TID waits for addBA response */
  370. rcu_read_lock();
  371. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  372. if (!tid_tx ||
  373. test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state)) {
  374. rcu_read_unlock();
  375. ht_dbg(sta->sdata,
  376. "timer expired on %pM tid %d but we are not (or no longer) expecting addBA response there\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. rcu_read_unlock();
  384. }
  385. void ieee80211_tx_ba_session_handle_start(struct sta_info *sta, int tid)
  386. {
  387. struct tid_ampdu_tx *tid_tx;
  388. struct ieee80211_local *local = sta->local;
  389. struct ieee80211_sub_if_data *sdata = sta->sdata;
  390. struct ieee80211_ampdu_params params = {
  391. .sta = &sta->sta,
  392. .action = IEEE80211_AMPDU_TX_START,
  393. .tid = tid,
  394. .buf_size = 0,
  395. .amsdu = false,
  396. .timeout = 0,
  397. };
  398. int ret;
  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. /* send AddBA request */
  438. ieee80211_send_addba_request(sdata, sta->sta.addr, tid,
  439. tid_tx->dialog_token, params.ssn,
  440. IEEE80211_MAX_AMPDU_BUF,
  441. tid_tx->timeout);
  442. }
  443. /*
  444. * After accepting the AddBA Response we activated a timer,
  445. * resetting it after each frame that we send.
  446. */
  447. static void sta_tx_agg_session_timer_expired(unsigned long data)
  448. {
  449. /* not an elegant detour, but there is no choice as the timer passes
  450. * only one argument, and various sta_info are needed here, so init
  451. * flow in sta_info_create gives the TID as data, while the timer_to_id
  452. * array gives the sta through container_of */
  453. u8 *ptid = (u8 *)data;
  454. u8 *timer_to_id = ptid - *ptid;
  455. struct sta_info *sta = container_of(timer_to_id, struct sta_info,
  456. timer_to_tid[0]);
  457. struct tid_ampdu_tx *tid_tx;
  458. unsigned long timeout;
  459. rcu_read_lock();
  460. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[*ptid]);
  461. if (!tid_tx || test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  462. rcu_read_unlock();
  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. rcu_read_unlock();
  469. return;
  470. }
  471. rcu_read_unlock();
  472. ht_dbg(sta->sdata, "tx session timer expired on %pM tid %d\n",
  473. sta->sta.addr, (u16)*ptid);
  474. ieee80211_stop_tx_ba_session(&sta->sta, *ptid);
  475. }
  476. int ieee80211_start_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid,
  477. u16 timeout)
  478. {
  479. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  480. struct ieee80211_sub_if_data *sdata = sta->sdata;
  481. struct ieee80211_local *local = sdata->local;
  482. struct tid_ampdu_tx *tid_tx;
  483. int ret = 0;
  484. trace_api_start_tx_ba_session(pubsta, tid);
  485. if (WARN(sta->reserved_tid == tid,
  486. "Requested to start BA session on reserved tid=%d", tid))
  487. return -EINVAL;
  488. if (!pubsta->ht_cap.ht_supported)
  489. return -EINVAL;
  490. if (WARN_ON_ONCE(!local->ops->ampdu_action))
  491. return -EINVAL;
  492. if ((tid >= IEEE80211_NUM_TIDS) ||
  493. !ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) ||
  494. ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW))
  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 - waiting a grace period after %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. /* response timer */
  568. tid_tx->addba_resp_timer.function = sta_addba_resp_timer_expired;
  569. tid_tx->addba_resp_timer.data = (unsigned long)&sta->timer_to_tid[tid];
  570. init_timer(&tid_tx->addba_resp_timer);
  571. /* tx timer */
  572. tid_tx->session_timer.function = sta_tx_agg_session_timer_expired;
  573. tid_tx->session_timer.data = (unsigned long)&sta->timer_to_tid[tid];
  574. init_timer_deferrable(&tid_tx->session_timer);
  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 ieee80211_vif *vif, u8 *ra, u16 tid)
  625. {
  626. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  627. struct ieee80211_local *local = sdata->local;
  628. struct sta_info *sta;
  629. struct tid_ampdu_tx *tid_tx;
  630. trace_api_start_tx_ba_cb(sdata, ra, tid);
  631. if (tid >= IEEE80211_NUM_TIDS) {
  632. ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
  633. tid, IEEE80211_NUM_TIDS);
  634. return;
  635. }
  636. mutex_lock(&local->sta_mtx);
  637. sta = sta_info_get_bss(sdata, ra);
  638. if (!sta) {
  639. mutex_unlock(&local->sta_mtx);
  640. ht_dbg(sdata, "Could not find station: %pM\n", ra);
  641. return;
  642. }
  643. mutex_lock(&sta->ampdu_mlme.mtx);
  644. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  645. if (WARN_ON(!tid_tx)) {
  646. ht_dbg(sdata, "addBA was not requested!\n");
  647. goto unlock;
  648. }
  649. if (WARN_ON(test_and_set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state)))
  650. goto unlock;
  651. if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state))
  652. ieee80211_agg_tx_operational(local, sta, tid);
  653. unlock:
  654. mutex_unlock(&sta->ampdu_mlme.mtx);
  655. mutex_unlock(&local->sta_mtx);
  656. }
  657. void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  658. const u8 *ra, u16 tid)
  659. {
  660. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  661. struct ieee80211_local *local = sdata->local;
  662. struct ieee80211_ra_tid *ra_tid;
  663. struct sk_buff *skb = dev_alloc_skb(0);
  664. if (unlikely(!skb))
  665. return;
  666. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  667. memcpy(&ra_tid->ra, ra, ETH_ALEN);
  668. ra_tid->tid = tid;
  669. skb->pkt_type = IEEE80211_SDATA_QUEUE_AGG_START;
  670. skb_queue_tail(&sdata->skb_queue, skb);
  671. ieee80211_queue_work(&local->hw, &sdata->work);
  672. }
  673. EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
  674. int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
  675. enum ieee80211_agg_stop_reason reason)
  676. {
  677. int ret;
  678. mutex_lock(&sta->ampdu_mlme.mtx);
  679. ret = ___ieee80211_stop_tx_ba_session(sta, tid, reason);
  680. mutex_unlock(&sta->ampdu_mlme.mtx);
  681. return ret;
  682. }
  683. int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
  684. {
  685. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  686. struct ieee80211_sub_if_data *sdata = sta->sdata;
  687. struct ieee80211_local *local = sdata->local;
  688. struct tid_ampdu_tx *tid_tx;
  689. int ret = 0;
  690. trace_api_stop_tx_ba_session(pubsta, tid);
  691. if (!local->ops->ampdu_action)
  692. return -EINVAL;
  693. if (tid >= IEEE80211_NUM_TIDS)
  694. return -EINVAL;
  695. spin_lock_bh(&sta->lock);
  696. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  697. if (!tid_tx) {
  698. ret = -ENOENT;
  699. goto unlock;
  700. }
  701. WARN(sta->reserved_tid == tid,
  702. "Requested to stop BA session on reserved tid=%d", tid);
  703. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  704. /* already in progress stopping it */
  705. ret = 0;
  706. goto unlock;
  707. }
  708. set_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state);
  709. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  710. unlock:
  711. spin_unlock_bh(&sta->lock);
  712. return ret;
  713. }
  714. EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
  715. void ieee80211_stop_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u8 tid)
  716. {
  717. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  718. struct ieee80211_local *local = sdata->local;
  719. struct sta_info *sta;
  720. struct tid_ampdu_tx *tid_tx;
  721. bool send_delba = false;
  722. trace_api_stop_tx_ba_cb(sdata, ra, tid);
  723. if (tid >= IEEE80211_NUM_TIDS) {
  724. ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
  725. tid, IEEE80211_NUM_TIDS);
  726. return;
  727. }
  728. ht_dbg(sdata, "Stopping Tx BA session for %pM tid %d\n", ra, tid);
  729. mutex_lock(&local->sta_mtx);
  730. sta = sta_info_get_bss(sdata, ra);
  731. if (!sta) {
  732. ht_dbg(sdata, "Could not find station: %pM\n", ra);
  733. goto unlock;
  734. }
  735. mutex_lock(&sta->ampdu_mlme.mtx);
  736. spin_lock_bh(&sta->lock);
  737. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  738. if (!tid_tx || !test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  739. ht_dbg(sdata,
  740. "unexpected callback to A-MPDU stop for %pM tid %d\n",
  741. sta->sta.addr, tid);
  742. goto unlock_sta;
  743. }
  744. if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop)
  745. send_delba = true;
  746. ieee80211_remove_tid_tx(sta, tid);
  747. unlock_sta:
  748. spin_unlock_bh(&sta->lock);
  749. if (send_delba)
  750. ieee80211_send_delba(sdata, ra, tid,
  751. WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
  752. mutex_unlock(&sta->ampdu_mlme.mtx);
  753. unlock:
  754. mutex_unlock(&local->sta_mtx);
  755. }
  756. void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  757. const u8 *ra, u16 tid)
  758. {
  759. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  760. struct ieee80211_local *local = sdata->local;
  761. struct ieee80211_ra_tid *ra_tid;
  762. struct sk_buff *skb = dev_alloc_skb(0);
  763. if (unlikely(!skb))
  764. return;
  765. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  766. memcpy(&ra_tid->ra, ra, ETH_ALEN);
  767. ra_tid->tid = tid;
  768. skb->pkt_type = IEEE80211_SDATA_QUEUE_AGG_STOP;
  769. skb_queue_tail(&sdata->skb_queue, skb);
  770. ieee80211_queue_work(&local->hw, &sdata->work);
  771. }
  772. EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
  773. void ieee80211_process_addba_resp(struct ieee80211_local *local,
  774. struct sta_info *sta,
  775. struct ieee80211_mgmt *mgmt,
  776. size_t len)
  777. {
  778. struct tid_ampdu_tx *tid_tx;
  779. struct ieee80211_txq *txq;
  780. u16 capab, tid;
  781. u8 buf_size;
  782. bool amsdu;
  783. capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
  784. amsdu = capab & IEEE80211_ADDBA_PARAM_AMSDU_MASK;
  785. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  786. buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
  787. buf_size = min(buf_size, local->hw.max_tx_aggregation_subframes);
  788. txq = sta->sta.txq[tid];
  789. if (!amsdu && txq)
  790. set_bit(IEEE80211_TXQ_NO_AMSDU, &to_txq_info(txq)->flags);
  791. mutex_lock(&sta->ampdu_mlme.mtx);
  792. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  793. if (!tid_tx)
  794. goto out;
  795. if (mgmt->u.action.u.addba_resp.dialog_token != tid_tx->dialog_token) {
  796. ht_dbg(sta->sdata, "wrong addBA response token, %pM tid %d\n",
  797. sta->sta.addr, tid);
  798. goto out;
  799. }
  800. del_timer_sync(&tid_tx->addba_resp_timer);
  801. ht_dbg(sta->sdata, "switched off addBA timer for %pM tid %d\n",
  802. sta->sta.addr, tid);
  803. /*
  804. * addba_resp_timer may have fired before we got here, and
  805. * caused WANT_STOP to be set. If the stop then was already
  806. * processed further, STOPPING might be set.
  807. */
  808. if (test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state) ||
  809. test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  810. ht_dbg(sta->sdata,
  811. "got addBA resp for %pM tid %d but we already gave up\n",
  812. sta->sta.addr, tid);
  813. goto out;
  814. }
  815. /*
  816. * IEEE 802.11-2007 7.3.1.14:
  817. * In an ADDBA Response frame, when the Status Code field
  818. * is set to 0, the Buffer Size subfield is set to a value
  819. * of at least 1.
  820. */
  821. if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
  822. == WLAN_STATUS_SUCCESS && buf_size) {
  823. if (test_and_set_bit(HT_AGG_STATE_RESPONSE_RECEIVED,
  824. &tid_tx->state)) {
  825. /* ignore duplicate response */
  826. goto out;
  827. }
  828. tid_tx->buf_size = buf_size;
  829. tid_tx->amsdu = amsdu;
  830. if (test_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state))
  831. ieee80211_agg_tx_operational(local, sta, tid);
  832. sta->ampdu_mlme.addba_req_num[tid] = 0;
  833. if (tid_tx->timeout) {
  834. mod_timer(&tid_tx->session_timer,
  835. TU_TO_EXP_TIME(tid_tx->timeout));
  836. tid_tx->last_tx = jiffies;
  837. }
  838. } else {
  839. ___ieee80211_stop_tx_ba_session(sta, tid, AGG_STOP_DECLINED);
  840. }
  841. out:
  842. mutex_unlock(&sta->ampdu_mlme.mtx);
  843. }