agg-tx.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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 txq_info *txqi;
  174. if (!txq)
  175. return;
  176. txqi = to_txq_info(txq);
  177. /* Lock here to protect against further seqno updates on dequeue */
  178. spin_lock_bh(&txqi->queue.lock);
  179. set_bit(IEEE80211_TXQ_STOP, &txqi->flags);
  180. spin_unlock_bh(&txqi->queue.lock);
  181. }
  182. static void
  183. ieee80211_agg_start_txq(struct sta_info *sta, int tid, bool enable)
  184. {
  185. struct ieee80211_txq *txq = sta->sta.txq[tid];
  186. struct txq_info *txqi;
  187. if (!txq)
  188. return;
  189. txqi = to_txq_info(txq);
  190. if (enable)
  191. set_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
  192. else
  193. clear_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
  194. clear_bit(IEEE80211_TXQ_STOP, &txqi->flags);
  195. drv_wake_tx_queue(sta->sdata->local, txqi);
  196. }
  197. /*
  198. * splice packets from the STA's pending to the local pending,
  199. * requires a call to ieee80211_agg_splice_finish later
  200. */
  201. static void __acquires(agg_queue)
  202. ieee80211_agg_splice_packets(struct ieee80211_sub_if_data *sdata,
  203. struct tid_ampdu_tx *tid_tx, u16 tid)
  204. {
  205. struct ieee80211_local *local = sdata->local;
  206. int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
  207. unsigned long flags;
  208. ieee80211_stop_queue_agg(sdata, tid);
  209. if (WARN(!tid_tx,
  210. "TID %d gone but expected when splicing aggregates from the pending queue\n",
  211. tid))
  212. return;
  213. if (!skb_queue_empty(&tid_tx->pending)) {
  214. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  215. /* copy over remaining packets */
  216. skb_queue_splice_tail_init(&tid_tx->pending,
  217. &local->pending[queue]);
  218. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  219. }
  220. }
  221. static void __releases(agg_queue)
  222. ieee80211_agg_splice_finish(struct ieee80211_sub_if_data *sdata, u16 tid)
  223. {
  224. ieee80211_wake_queue_agg(sdata, tid);
  225. }
  226. static void ieee80211_remove_tid_tx(struct sta_info *sta, int tid)
  227. {
  228. struct tid_ampdu_tx *tid_tx;
  229. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  230. lockdep_assert_held(&sta->lock);
  231. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  232. /*
  233. * When we get here, the TX path will not be lockless any more wrt.
  234. * aggregation, since the OPERATIONAL bit has long been cleared.
  235. * Thus it will block on getting the lock, if it occurs. So if we
  236. * stop the queue now, we will not get any more packets, and any
  237. * that might be being processed will wait for us here, thereby
  238. * guaranteeing that no packets go to the tid_tx pending queue any
  239. * more.
  240. */
  241. ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
  242. /* future packets must not find the tid_tx struct any more */
  243. ieee80211_assign_tid_tx(sta, tid, NULL);
  244. ieee80211_agg_splice_finish(sta->sdata, tid);
  245. ieee80211_agg_start_txq(sta, tid, false);
  246. kfree_rcu(tid_tx, rcu_head);
  247. }
  248. int ___ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
  249. enum ieee80211_agg_stop_reason reason)
  250. {
  251. struct ieee80211_local *local = sta->local;
  252. struct tid_ampdu_tx *tid_tx;
  253. struct ieee80211_ampdu_params params = {
  254. .sta = &sta->sta,
  255. .tid = tid,
  256. .buf_size = 0,
  257. .amsdu = false,
  258. .timeout = 0,
  259. .ssn = 0,
  260. };
  261. int ret;
  262. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  263. switch (reason) {
  264. case AGG_STOP_DECLINED:
  265. case AGG_STOP_LOCAL_REQUEST:
  266. case AGG_STOP_PEER_REQUEST:
  267. params.action = IEEE80211_AMPDU_TX_STOP_CONT;
  268. break;
  269. case AGG_STOP_DESTROY_STA:
  270. params.action = IEEE80211_AMPDU_TX_STOP_FLUSH;
  271. break;
  272. default:
  273. WARN_ON_ONCE(1);
  274. return -EINVAL;
  275. }
  276. spin_lock_bh(&sta->lock);
  277. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  278. if (!tid_tx) {
  279. spin_unlock_bh(&sta->lock);
  280. return -ENOENT;
  281. }
  282. /*
  283. * if we're already stopping ignore any new requests to stop
  284. * unless we're destroying it in which case notify the driver
  285. */
  286. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  287. spin_unlock_bh(&sta->lock);
  288. if (reason != AGG_STOP_DESTROY_STA)
  289. return -EALREADY;
  290. params.action = IEEE80211_AMPDU_TX_STOP_FLUSH_CONT;
  291. ret = drv_ampdu_action(local, sta->sdata, &params);
  292. WARN_ON_ONCE(ret);
  293. return 0;
  294. }
  295. if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
  296. /* not even started yet! */
  297. ieee80211_assign_tid_tx(sta, tid, NULL);
  298. spin_unlock_bh(&sta->lock);
  299. kfree_rcu(tid_tx, rcu_head);
  300. return 0;
  301. }
  302. set_bit(HT_AGG_STATE_STOPPING, &tid_tx->state);
  303. spin_unlock_bh(&sta->lock);
  304. ht_dbg(sta->sdata, "Tx BA session stop requested for %pM tid %u\n",
  305. sta->sta.addr, tid);
  306. del_timer_sync(&tid_tx->addba_resp_timer);
  307. del_timer_sync(&tid_tx->session_timer);
  308. /*
  309. * After this packets are no longer handed right through
  310. * to the driver but are put onto tid_tx->pending instead,
  311. * with locking to ensure proper access.
  312. */
  313. clear_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
  314. /*
  315. * There might be a few packets being processed right now (on
  316. * another CPU) that have already gotten past the aggregation
  317. * check when it was still OPERATIONAL and consequently have
  318. * IEEE80211_TX_CTL_AMPDU set. In that case, this code might
  319. * call into the driver at the same time or even before the
  320. * TX paths calls into it, which could confuse the driver.
  321. *
  322. * Wait for all currently running TX paths to finish before
  323. * telling the driver. New packets will not go through since
  324. * the aggregation session is no longer OPERATIONAL.
  325. */
  326. synchronize_net();
  327. tid_tx->stop_initiator = reason == AGG_STOP_PEER_REQUEST ?
  328. WLAN_BACK_RECIPIENT :
  329. WLAN_BACK_INITIATOR;
  330. tid_tx->tx_stop = reason == AGG_STOP_LOCAL_REQUEST;
  331. ret = drv_ampdu_action(local, sta->sdata, &params);
  332. /* HW shall not deny going back to legacy */
  333. if (WARN_ON(ret)) {
  334. /*
  335. * We may have pending packets get stuck in this case...
  336. * Not bothering with a workaround for now.
  337. */
  338. }
  339. /*
  340. * In the case of AGG_STOP_DESTROY_STA, the driver won't
  341. * necessarily call ieee80211_stop_tx_ba_cb(), so this may
  342. * seem like we can leave the tid_tx data pending forever.
  343. * This is true, in a way, but "forever" is only until the
  344. * station struct is actually destroyed. In the meantime,
  345. * leaving it around ensures that we don't transmit packets
  346. * to the driver on this TID which might confuse it.
  347. */
  348. return 0;
  349. }
  350. /*
  351. * After sending add Block Ack request we activated a timer until
  352. * add Block Ack response will arrive from the recipient.
  353. * If this timer expires sta_addba_resp_timer_expired will be executed.
  354. */
  355. static void sta_addba_resp_timer_expired(unsigned long data)
  356. {
  357. /* not an elegant detour, but there is no choice as the timer passes
  358. * only one argument, and both sta_info and TID are needed, so init
  359. * flow in sta_info_create gives the TID as data, while the timer_to_id
  360. * array gives the sta through container_of */
  361. u16 tid = *(u8 *)data;
  362. struct sta_info *sta = container_of((void *)data,
  363. struct sta_info, timer_to_tid[tid]);
  364. struct tid_ampdu_tx *tid_tx;
  365. /* check if the TID waits for addBA response */
  366. rcu_read_lock();
  367. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  368. if (!tid_tx ||
  369. test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state)) {
  370. rcu_read_unlock();
  371. ht_dbg(sta->sdata,
  372. "timer expired on %pM tid %d but we are not (or no longer) expecting addBA response there\n",
  373. sta->sta.addr, tid);
  374. return;
  375. }
  376. ht_dbg(sta->sdata, "addBA response timer expired on %pM tid %d\n",
  377. sta->sta.addr, tid);
  378. ieee80211_stop_tx_ba_session(&sta->sta, tid);
  379. rcu_read_unlock();
  380. }
  381. void ieee80211_tx_ba_session_handle_start(struct sta_info *sta, int tid)
  382. {
  383. struct tid_ampdu_tx *tid_tx;
  384. struct ieee80211_local *local = sta->local;
  385. struct ieee80211_sub_if_data *sdata = sta->sdata;
  386. struct ieee80211_ampdu_params params = {
  387. .sta = &sta->sta,
  388. .action = IEEE80211_AMPDU_TX_START,
  389. .tid = tid,
  390. .buf_size = 0,
  391. .amsdu = false,
  392. .timeout = 0,
  393. };
  394. int ret;
  395. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  396. /*
  397. * Start queuing up packets for this aggregation session.
  398. * We're going to release them once the driver is OK with
  399. * that.
  400. */
  401. clear_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
  402. ieee80211_agg_stop_txq(sta, tid);
  403. /*
  404. * Make sure no packets are being processed. This ensures that
  405. * we have a valid starting sequence number and that in-flight
  406. * packets have been flushed out and no packets for this TID
  407. * will go into the driver during the ampdu_action call.
  408. */
  409. synchronize_net();
  410. params.ssn = sta->tid_seq[tid] >> 4;
  411. ret = drv_ampdu_action(local, sdata, &params);
  412. if (ret) {
  413. ht_dbg(sdata,
  414. "BA request denied - HW unavailable for %pM tid %d\n",
  415. sta->sta.addr, tid);
  416. spin_lock_bh(&sta->lock);
  417. ieee80211_agg_splice_packets(sdata, tid_tx, tid);
  418. ieee80211_assign_tid_tx(sta, tid, NULL);
  419. ieee80211_agg_splice_finish(sdata, tid);
  420. spin_unlock_bh(&sta->lock);
  421. ieee80211_agg_start_txq(sta, tid, false);
  422. kfree_rcu(tid_tx, rcu_head);
  423. return;
  424. }
  425. /* activate the timer for the recipient's addBA response */
  426. mod_timer(&tid_tx->addba_resp_timer, jiffies + ADDBA_RESP_INTERVAL);
  427. ht_dbg(sdata, "activated addBA response timer on %pM tid %d\n",
  428. sta->sta.addr, tid);
  429. spin_lock_bh(&sta->lock);
  430. sta->ampdu_mlme.last_addba_req_time[tid] = jiffies;
  431. sta->ampdu_mlme.addba_req_num[tid]++;
  432. spin_unlock_bh(&sta->lock);
  433. /* send AddBA request */
  434. ieee80211_send_addba_request(sdata, sta->sta.addr, tid,
  435. tid_tx->dialog_token, params.ssn,
  436. IEEE80211_MAX_AMPDU_BUF,
  437. tid_tx->timeout);
  438. }
  439. /*
  440. * After accepting the AddBA Response we activated a timer,
  441. * resetting it after each frame that we send.
  442. */
  443. static void sta_tx_agg_session_timer_expired(unsigned long data)
  444. {
  445. /* not an elegant detour, but there is no choice as the timer passes
  446. * only one argument, and various sta_info are needed here, so init
  447. * flow in sta_info_create gives the TID as data, while the timer_to_id
  448. * array gives the sta through container_of */
  449. u8 *ptid = (u8 *)data;
  450. u8 *timer_to_id = ptid - *ptid;
  451. struct sta_info *sta = container_of(timer_to_id, struct sta_info,
  452. timer_to_tid[0]);
  453. struct tid_ampdu_tx *tid_tx;
  454. unsigned long timeout;
  455. rcu_read_lock();
  456. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[*ptid]);
  457. if (!tid_tx || test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  458. rcu_read_unlock();
  459. return;
  460. }
  461. timeout = tid_tx->last_tx + TU_TO_JIFFIES(tid_tx->timeout);
  462. if (time_is_after_jiffies(timeout)) {
  463. mod_timer(&tid_tx->session_timer, timeout);
  464. rcu_read_unlock();
  465. return;
  466. }
  467. rcu_read_unlock();
  468. ht_dbg(sta->sdata, "tx session timer expired on %pM tid %d\n",
  469. sta->sta.addr, (u16)*ptid);
  470. ieee80211_stop_tx_ba_session(&sta->sta, *ptid);
  471. }
  472. int ieee80211_start_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid,
  473. u16 timeout)
  474. {
  475. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  476. struct ieee80211_sub_if_data *sdata = sta->sdata;
  477. struct ieee80211_local *local = sdata->local;
  478. struct tid_ampdu_tx *tid_tx;
  479. int ret = 0;
  480. trace_api_start_tx_ba_session(pubsta, tid);
  481. if (WARN(sta->reserved_tid == tid,
  482. "Requested to start BA session on reserved tid=%d", tid))
  483. return -EINVAL;
  484. if (!pubsta->ht_cap.ht_supported)
  485. return -EINVAL;
  486. if (WARN_ON_ONCE(!local->ops->ampdu_action))
  487. return -EINVAL;
  488. if ((tid >= IEEE80211_NUM_TIDS) ||
  489. !ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) ||
  490. ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW))
  491. return -EINVAL;
  492. ht_dbg(sdata, "Open BA session requested for %pM tid %u\n",
  493. pubsta->addr, tid);
  494. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  495. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  496. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  497. sdata->vif.type != NL80211_IFTYPE_AP &&
  498. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  499. return -EINVAL;
  500. if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
  501. ht_dbg(sdata,
  502. "BA sessions blocked - Denying BA session request %pM tid %d\n",
  503. sta->sta.addr, tid);
  504. return -EINVAL;
  505. }
  506. /*
  507. * 802.11n-2009 11.5.1.1: If the initiating STA is an HT STA, is a
  508. * member of an IBSS, and has no other existing Block Ack agreement
  509. * with the recipient STA, then the initiating STA shall transmit a
  510. * Probe Request frame to the recipient STA and shall not transmit an
  511. * ADDBA Request frame unless it receives a Probe Response frame
  512. * from the recipient within dot11ADDBAFailureTimeout.
  513. *
  514. * The probe request mechanism for ADDBA is currently not implemented,
  515. * but we only build up Block Ack session with HT STAs. This information
  516. * is set when we receive a bss info from a probe response or a beacon.
  517. */
  518. if (sta->sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  519. !sta->sta.ht_cap.ht_supported) {
  520. ht_dbg(sdata,
  521. "BA request denied - IBSS STA %pM does not advertise HT support\n",
  522. pubsta->addr);
  523. return -EINVAL;
  524. }
  525. spin_lock_bh(&sta->lock);
  526. /* we have tried too many times, receiver does not want A-MPDU */
  527. if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
  528. ret = -EBUSY;
  529. goto err_unlock_sta;
  530. }
  531. /*
  532. * if we have tried more than HT_AGG_BURST_RETRIES times we
  533. * will spread our requests in time to avoid stalling connection
  534. * for too long
  535. */
  536. if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_BURST_RETRIES &&
  537. time_before(jiffies, sta->ampdu_mlme.last_addba_req_time[tid] +
  538. HT_AGG_RETRIES_PERIOD)) {
  539. ht_dbg(sdata,
  540. "BA request denied - waiting a grace period after %d failed requests on %pM tid %u\n",
  541. sta->ampdu_mlme.addba_req_num[tid], sta->sta.addr, tid);
  542. ret = -EBUSY;
  543. goto err_unlock_sta;
  544. }
  545. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  546. /* check if the TID is not in aggregation flow already */
  547. if (tid_tx || sta->ampdu_mlme.tid_start_tx[tid]) {
  548. ht_dbg(sdata,
  549. "BA request denied - session is not idle on %pM tid %u\n",
  550. sta->sta.addr, tid);
  551. ret = -EAGAIN;
  552. goto err_unlock_sta;
  553. }
  554. /* prepare A-MPDU MLME for Tx aggregation */
  555. tid_tx = kzalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
  556. if (!tid_tx) {
  557. ret = -ENOMEM;
  558. goto err_unlock_sta;
  559. }
  560. skb_queue_head_init(&tid_tx->pending);
  561. __set_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
  562. tid_tx->timeout = timeout;
  563. /* response timer */
  564. tid_tx->addba_resp_timer.function = sta_addba_resp_timer_expired;
  565. tid_tx->addba_resp_timer.data = (unsigned long)&sta->timer_to_tid[tid];
  566. init_timer(&tid_tx->addba_resp_timer);
  567. /* tx timer */
  568. tid_tx->session_timer.function = sta_tx_agg_session_timer_expired;
  569. tid_tx->session_timer.data = (unsigned long)&sta->timer_to_tid[tid];
  570. init_timer_deferrable(&tid_tx->session_timer);
  571. /* assign a dialog token */
  572. sta->ampdu_mlme.dialog_token_allocator++;
  573. tid_tx->dialog_token = sta->ampdu_mlme.dialog_token_allocator;
  574. /*
  575. * Finally, assign it to the start array; the work item will
  576. * collect it and move it to the normal array.
  577. */
  578. sta->ampdu_mlme.tid_start_tx[tid] = tid_tx;
  579. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  580. /* this flow continues off the work */
  581. err_unlock_sta:
  582. spin_unlock_bh(&sta->lock);
  583. return ret;
  584. }
  585. EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
  586. static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
  587. struct sta_info *sta, u16 tid)
  588. {
  589. struct tid_ampdu_tx *tid_tx;
  590. struct ieee80211_ampdu_params params = {
  591. .sta = &sta->sta,
  592. .action = IEEE80211_AMPDU_TX_OPERATIONAL,
  593. .tid = tid,
  594. .timeout = 0,
  595. .ssn = 0,
  596. };
  597. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  598. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  599. params.buf_size = tid_tx->buf_size;
  600. params.amsdu = tid_tx->amsdu;
  601. ht_dbg(sta->sdata, "Aggregation is on for %pM tid %d\n",
  602. sta->sta.addr, tid);
  603. drv_ampdu_action(local, sta->sdata, &params);
  604. /*
  605. * synchronize with TX path, while splicing the TX path
  606. * should block so it won't put more packets onto pending.
  607. */
  608. spin_lock_bh(&sta->lock);
  609. ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
  610. /*
  611. * Now mark as operational. This will be visible
  612. * in the TX path, and lets it go lock-free in
  613. * the common case.
  614. */
  615. set_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
  616. ieee80211_agg_splice_finish(sta->sdata, tid);
  617. spin_unlock_bh(&sta->lock);
  618. ieee80211_agg_start_txq(sta, tid, true);
  619. }
  620. void ieee80211_start_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u16 tid)
  621. {
  622. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  623. struct ieee80211_local *local = sdata->local;
  624. struct sta_info *sta;
  625. struct tid_ampdu_tx *tid_tx;
  626. trace_api_start_tx_ba_cb(sdata, ra, tid);
  627. if (tid >= IEEE80211_NUM_TIDS) {
  628. ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
  629. tid, IEEE80211_NUM_TIDS);
  630. return;
  631. }
  632. mutex_lock(&local->sta_mtx);
  633. sta = sta_info_get_bss(sdata, ra);
  634. if (!sta) {
  635. mutex_unlock(&local->sta_mtx);
  636. ht_dbg(sdata, "Could not find station: %pM\n", ra);
  637. return;
  638. }
  639. mutex_lock(&sta->ampdu_mlme.mtx);
  640. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  641. if (WARN_ON(!tid_tx)) {
  642. ht_dbg(sdata, "addBA was not requested!\n");
  643. goto unlock;
  644. }
  645. if (WARN_ON(test_and_set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state)))
  646. goto unlock;
  647. if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state))
  648. ieee80211_agg_tx_operational(local, sta, tid);
  649. unlock:
  650. mutex_unlock(&sta->ampdu_mlme.mtx);
  651. mutex_unlock(&local->sta_mtx);
  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 ieee80211_ra_tid *ra_tid;
  659. struct sk_buff *skb = dev_alloc_skb(0);
  660. if (unlikely(!skb))
  661. return;
  662. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  663. memcpy(&ra_tid->ra, ra, ETH_ALEN);
  664. ra_tid->tid = tid;
  665. skb->pkt_type = IEEE80211_SDATA_QUEUE_AGG_START;
  666. skb_queue_tail(&sdata->skb_queue, skb);
  667. ieee80211_queue_work(&local->hw, &sdata->work);
  668. }
  669. EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
  670. int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
  671. enum ieee80211_agg_stop_reason reason)
  672. {
  673. int ret;
  674. mutex_lock(&sta->ampdu_mlme.mtx);
  675. ret = ___ieee80211_stop_tx_ba_session(sta, tid, reason);
  676. mutex_unlock(&sta->ampdu_mlme.mtx);
  677. return ret;
  678. }
  679. int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
  680. {
  681. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  682. struct ieee80211_sub_if_data *sdata = sta->sdata;
  683. struct ieee80211_local *local = sdata->local;
  684. struct tid_ampdu_tx *tid_tx;
  685. int ret = 0;
  686. trace_api_stop_tx_ba_session(pubsta, tid);
  687. if (!local->ops->ampdu_action)
  688. return -EINVAL;
  689. if (tid >= IEEE80211_NUM_TIDS)
  690. return -EINVAL;
  691. spin_lock_bh(&sta->lock);
  692. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  693. if (!tid_tx) {
  694. ret = -ENOENT;
  695. goto unlock;
  696. }
  697. WARN(sta->reserved_tid == tid,
  698. "Requested to stop BA session on reserved tid=%d", tid);
  699. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  700. /* already in progress stopping it */
  701. ret = 0;
  702. goto unlock;
  703. }
  704. set_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state);
  705. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  706. unlock:
  707. spin_unlock_bh(&sta->lock);
  708. return ret;
  709. }
  710. EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
  711. void ieee80211_stop_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u8 tid)
  712. {
  713. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  714. struct ieee80211_local *local = sdata->local;
  715. struct sta_info *sta;
  716. struct tid_ampdu_tx *tid_tx;
  717. bool send_delba = false;
  718. trace_api_stop_tx_ba_cb(sdata, ra, tid);
  719. if (tid >= IEEE80211_NUM_TIDS) {
  720. ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
  721. tid, IEEE80211_NUM_TIDS);
  722. return;
  723. }
  724. ht_dbg(sdata, "Stopping Tx BA session for %pM tid %d\n", ra, tid);
  725. mutex_lock(&local->sta_mtx);
  726. sta = sta_info_get_bss(sdata, ra);
  727. if (!sta) {
  728. ht_dbg(sdata, "Could not find station: %pM\n", ra);
  729. goto unlock;
  730. }
  731. mutex_lock(&sta->ampdu_mlme.mtx);
  732. spin_lock_bh(&sta->lock);
  733. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  734. if (!tid_tx || !test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  735. ht_dbg(sdata,
  736. "unexpected callback to A-MPDU stop for %pM tid %d\n",
  737. sta->sta.addr, tid);
  738. goto unlock_sta;
  739. }
  740. if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop)
  741. send_delba = true;
  742. ieee80211_remove_tid_tx(sta, tid);
  743. unlock_sta:
  744. spin_unlock_bh(&sta->lock);
  745. if (send_delba)
  746. ieee80211_send_delba(sdata, ra, tid,
  747. WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
  748. mutex_unlock(&sta->ampdu_mlme.mtx);
  749. unlock:
  750. mutex_unlock(&local->sta_mtx);
  751. }
  752. void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  753. const u8 *ra, u16 tid)
  754. {
  755. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  756. struct ieee80211_local *local = sdata->local;
  757. struct ieee80211_ra_tid *ra_tid;
  758. struct sk_buff *skb = dev_alloc_skb(0);
  759. if (unlikely(!skb))
  760. return;
  761. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  762. memcpy(&ra_tid->ra, ra, ETH_ALEN);
  763. ra_tid->tid = tid;
  764. skb->pkt_type = IEEE80211_SDATA_QUEUE_AGG_STOP;
  765. skb_queue_tail(&sdata->skb_queue, skb);
  766. ieee80211_queue_work(&local->hw, &sdata->work);
  767. }
  768. EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
  769. void ieee80211_process_addba_resp(struct ieee80211_local *local,
  770. struct sta_info *sta,
  771. struct ieee80211_mgmt *mgmt,
  772. size_t len)
  773. {
  774. struct tid_ampdu_tx *tid_tx;
  775. u16 capab, tid;
  776. u8 buf_size;
  777. bool amsdu;
  778. capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
  779. amsdu = capab & IEEE80211_ADDBA_PARAM_AMSDU_MASK;
  780. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  781. buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
  782. buf_size = min(buf_size, local->hw.max_tx_aggregation_subframes);
  783. mutex_lock(&sta->ampdu_mlme.mtx);
  784. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  785. if (!tid_tx)
  786. goto out;
  787. if (mgmt->u.action.u.addba_resp.dialog_token != tid_tx->dialog_token) {
  788. ht_dbg(sta->sdata, "wrong addBA response token, %pM tid %d\n",
  789. sta->sta.addr, tid);
  790. goto out;
  791. }
  792. del_timer_sync(&tid_tx->addba_resp_timer);
  793. ht_dbg(sta->sdata, "switched off addBA timer for %pM tid %d\n",
  794. sta->sta.addr, tid);
  795. /*
  796. * addba_resp_timer may have fired before we got here, and
  797. * caused WANT_STOP to be set. If the stop then was already
  798. * processed further, STOPPING might be set.
  799. */
  800. if (test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state) ||
  801. test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  802. ht_dbg(sta->sdata,
  803. "got addBA resp for %pM tid %d but we already gave up\n",
  804. sta->sta.addr, tid);
  805. goto out;
  806. }
  807. /*
  808. * IEEE 802.11-2007 7.3.1.14:
  809. * In an ADDBA Response frame, when the Status Code field
  810. * is set to 0, the Buffer Size subfield is set to a value
  811. * of at least 1.
  812. */
  813. if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
  814. == WLAN_STATUS_SUCCESS && buf_size) {
  815. if (test_and_set_bit(HT_AGG_STATE_RESPONSE_RECEIVED,
  816. &tid_tx->state)) {
  817. /* ignore duplicate response */
  818. goto out;
  819. }
  820. tid_tx->buf_size = buf_size;
  821. tid_tx->amsdu = amsdu;
  822. if (test_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state))
  823. ieee80211_agg_tx_operational(local, sta, tid);
  824. sta->ampdu_mlme.addba_req_num[tid] = 0;
  825. if (tid_tx->timeout) {
  826. mod_timer(&tid_tx->session_timer,
  827. TU_TO_EXP_TIME(tid_tx->timeout));
  828. tid_tx->last_tx = jiffies;
  829. }
  830. } else {
  831. ___ieee80211_stop_tx_ba_session(sta, tid, AGG_STOP_DECLINED);
  832. }
  833. out:
  834. mutex_unlock(&sta->ampdu_mlme.mtx);
  835. }