agg-tx.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. *
  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 = skb_put_zero(skb, 24);
  74. memcpy(mgmt->da, da, ETH_ALEN);
  75. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  76. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  77. sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  78. sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  79. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  80. else if (sdata->vif.type == NL80211_IFTYPE_STATION)
  81. memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  82. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  83. memcpy(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN);
  84. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  85. IEEE80211_STYPE_ACTION);
  86. skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
  87. mgmt->u.action.category = WLAN_CATEGORY_BACK;
  88. mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
  89. mgmt->u.action.u.addba_req.dialog_token = dialog_token;
  90. capab = (u16)(1 << 0); /* bit 0 A-MSDU support */
  91. capab |= (u16)(1 << 1); /* bit 1 aggregation policy */
  92. capab |= (u16)(tid << 2); /* bit 5:2 TID number */
  93. capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
  94. mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
  95. mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
  96. mgmt->u.action.u.addba_req.start_seq_num =
  97. cpu_to_le16(start_seq_num << 4);
  98. ieee80211_tx_skb(sdata, skb);
  99. }
  100. void ieee80211_send_bar(struct ieee80211_vif *vif, u8 *ra, u16 tid, u16 ssn)
  101. {
  102. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  103. struct ieee80211_local *local = sdata->local;
  104. struct sk_buff *skb;
  105. struct ieee80211_bar *bar;
  106. u16 bar_control = 0;
  107. skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
  108. if (!skb)
  109. return;
  110. skb_reserve(skb, local->hw.extra_tx_headroom);
  111. bar = skb_put_zero(skb, sizeof(*bar));
  112. bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  113. IEEE80211_STYPE_BACK_REQ);
  114. memcpy(bar->ra, ra, ETH_ALEN);
  115. memcpy(bar->ta, sdata->vif.addr, ETH_ALEN);
  116. bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
  117. bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
  118. bar_control |= (u16)(tid << IEEE80211_BAR_CTRL_TID_INFO_SHIFT);
  119. bar->control = cpu_to_le16(bar_control);
  120. bar->start_seq_num = cpu_to_le16(ssn);
  121. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  122. IEEE80211_TX_CTL_REQ_TX_STATUS;
  123. ieee80211_tx_skb_tid(sdata, skb, tid);
  124. }
  125. EXPORT_SYMBOL(ieee80211_send_bar);
  126. void ieee80211_assign_tid_tx(struct sta_info *sta, int tid,
  127. struct tid_ampdu_tx *tid_tx)
  128. {
  129. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  130. lockdep_assert_held(&sta->lock);
  131. rcu_assign_pointer(sta->ampdu_mlme.tid_tx[tid], tid_tx);
  132. }
  133. /*
  134. * When multiple aggregation sessions on multiple stations
  135. * are being created/destroyed simultaneously, we need to
  136. * refcount the global queue stop caused by that in order
  137. * to not get into a situation where one of the aggregation
  138. * setup or teardown re-enables queues before the other is
  139. * ready to handle that.
  140. *
  141. * These two functions take care of this issue by keeping
  142. * a global "agg_queue_stop" refcount.
  143. */
  144. static void __acquires(agg_queue)
  145. ieee80211_stop_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
  146. {
  147. int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
  148. /* we do refcounting here, so don't use the queue reason refcounting */
  149. if (atomic_inc_return(&sdata->local->agg_queue_stop[queue]) == 1)
  150. ieee80211_stop_queue_by_reason(
  151. &sdata->local->hw, queue,
  152. IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
  153. false);
  154. __acquire(agg_queue);
  155. }
  156. static void __releases(agg_queue)
  157. ieee80211_wake_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
  158. {
  159. int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
  160. if (atomic_dec_return(&sdata->local->agg_queue_stop[queue]) == 0)
  161. ieee80211_wake_queue_by_reason(
  162. &sdata->local->hw, queue,
  163. IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
  164. false);
  165. __release(agg_queue);
  166. }
  167. static void
  168. ieee80211_agg_stop_txq(struct sta_info *sta, int tid)
  169. {
  170. struct ieee80211_txq *txq = sta->sta.txq[tid];
  171. struct ieee80211_sub_if_data *sdata;
  172. struct fq *fq;
  173. struct txq_info *txqi;
  174. if (!txq)
  175. return;
  176. txqi = to_txq_info(txq);
  177. sdata = vif_to_sdata(txq->vif);
  178. fq = &sdata->local->fq;
  179. /* Lock here to protect against further seqno updates on dequeue */
  180. spin_lock_bh(&fq->lock);
  181. set_bit(IEEE80211_TXQ_STOP, &txqi->flags);
  182. spin_unlock_bh(&fq->lock);
  183. }
  184. static void
  185. ieee80211_agg_start_txq(struct sta_info *sta, int tid, bool enable)
  186. {
  187. struct ieee80211_txq *txq = sta->sta.txq[tid];
  188. struct txq_info *txqi;
  189. if (!txq)
  190. return;
  191. txqi = to_txq_info(txq);
  192. if (enable)
  193. set_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
  194. else
  195. clear_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
  196. clear_bit(IEEE80211_TXQ_STOP, &txqi->flags);
  197. drv_wake_tx_queue(sta->sdata->local, txqi);
  198. }
  199. /*
  200. * splice packets from the STA's pending to the local pending,
  201. * requires a call to ieee80211_agg_splice_finish later
  202. */
  203. static void __acquires(agg_queue)
  204. ieee80211_agg_splice_packets(struct ieee80211_sub_if_data *sdata,
  205. struct tid_ampdu_tx *tid_tx, u16 tid)
  206. {
  207. struct ieee80211_local *local = sdata->local;
  208. int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
  209. unsigned long flags;
  210. ieee80211_stop_queue_agg(sdata, tid);
  211. if (WARN(!tid_tx,
  212. "TID %d gone but expected when splicing aggregates from the pending queue\n",
  213. tid))
  214. return;
  215. if (!skb_queue_empty(&tid_tx->pending)) {
  216. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  217. /* copy over remaining packets */
  218. skb_queue_splice_tail_init(&tid_tx->pending,
  219. &local->pending[queue]);
  220. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  221. }
  222. }
  223. static void __releases(agg_queue)
  224. ieee80211_agg_splice_finish(struct ieee80211_sub_if_data *sdata, u16 tid)
  225. {
  226. ieee80211_wake_queue_agg(sdata, tid);
  227. }
  228. static void ieee80211_remove_tid_tx(struct sta_info *sta, int tid)
  229. {
  230. struct tid_ampdu_tx *tid_tx;
  231. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  232. lockdep_assert_held(&sta->lock);
  233. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  234. /*
  235. * When we get here, the TX path will not be lockless any more wrt.
  236. * aggregation, since the OPERATIONAL bit has long been cleared.
  237. * Thus it will block on getting the lock, if it occurs. So if we
  238. * stop the queue now, we will not get any more packets, and any
  239. * that might be being processed will wait for us here, thereby
  240. * guaranteeing that no packets go to the tid_tx pending queue any
  241. * more.
  242. */
  243. ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
  244. /* future packets must not find the tid_tx struct any more */
  245. ieee80211_assign_tid_tx(sta, tid, NULL);
  246. ieee80211_agg_splice_finish(sta->sdata, tid);
  247. ieee80211_agg_start_txq(sta, tid, false);
  248. kfree_rcu(tid_tx, rcu_head);
  249. }
  250. int ___ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
  251. enum ieee80211_agg_stop_reason reason)
  252. {
  253. struct ieee80211_local *local = sta->local;
  254. struct tid_ampdu_tx *tid_tx;
  255. struct ieee80211_ampdu_params params = {
  256. .sta = &sta->sta,
  257. .tid = tid,
  258. .buf_size = 0,
  259. .amsdu = false,
  260. .timeout = 0,
  261. .ssn = 0,
  262. };
  263. int ret;
  264. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  265. switch (reason) {
  266. case AGG_STOP_DECLINED:
  267. case AGG_STOP_LOCAL_REQUEST:
  268. case AGG_STOP_PEER_REQUEST:
  269. params.action = IEEE80211_AMPDU_TX_STOP_CONT;
  270. break;
  271. case AGG_STOP_DESTROY_STA:
  272. params.action = IEEE80211_AMPDU_TX_STOP_FLUSH;
  273. break;
  274. default:
  275. WARN_ON_ONCE(1);
  276. return -EINVAL;
  277. }
  278. spin_lock_bh(&sta->lock);
  279. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  280. if (!tid_tx) {
  281. spin_unlock_bh(&sta->lock);
  282. return -ENOENT;
  283. }
  284. /*
  285. * if we're already stopping ignore any new requests to stop
  286. * unless we're destroying it in which case notify the driver
  287. */
  288. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  289. spin_unlock_bh(&sta->lock);
  290. if (reason != AGG_STOP_DESTROY_STA)
  291. return -EALREADY;
  292. params.action = IEEE80211_AMPDU_TX_STOP_FLUSH_CONT;
  293. ret = drv_ampdu_action(local, sta->sdata, &params);
  294. WARN_ON_ONCE(ret);
  295. return 0;
  296. }
  297. if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
  298. /* not even started yet! */
  299. ieee80211_assign_tid_tx(sta, tid, NULL);
  300. spin_unlock_bh(&sta->lock);
  301. kfree_rcu(tid_tx, rcu_head);
  302. return 0;
  303. }
  304. set_bit(HT_AGG_STATE_STOPPING, &tid_tx->state);
  305. spin_unlock_bh(&sta->lock);
  306. ht_dbg(sta->sdata, "Tx BA session stop requested for %pM tid %u\n",
  307. sta->sta.addr, tid);
  308. del_timer_sync(&tid_tx->addba_resp_timer);
  309. del_timer_sync(&tid_tx->session_timer);
  310. /*
  311. * After this packets are no longer handed right through
  312. * to the driver but are put onto tid_tx->pending instead,
  313. * with locking to ensure proper access.
  314. */
  315. clear_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
  316. /*
  317. * There might be a few packets being processed right now (on
  318. * another CPU) that have already gotten past the aggregation
  319. * check when it was still OPERATIONAL and consequently have
  320. * IEEE80211_TX_CTL_AMPDU set. In that case, this code might
  321. * call into the driver at the same time or even before the
  322. * TX paths calls into it, which could confuse the driver.
  323. *
  324. * Wait for all currently running TX paths to finish before
  325. * telling the driver. New packets will not go through since
  326. * the aggregation session is no longer OPERATIONAL.
  327. */
  328. synchronize_net();
  329. tid_tx->stop_initiator = reason == AGG_STOP_PEER_REQUEST ?
  330. WLAN_BACK_RECIPIENT :
  331. WLAN_BACK_INITIATOR;
  332. tid_tx->tx_stop = reason == AGG_STOP_LOCAL_REQUEST;
  333. ret = drv_ampdu_action(local, sta->sdata, &params);
  334. /* HW shall not deny going back to legacy */
  335. if (WARN_ON(ret)) {
  336. /*
  337. * We may have pending packets get stuck in this case...
  338. * Not bothering with a workaround for now.
  339. */
  340. }
  341. /*
  342. * In the case of AGG_STOP_DESTROY_STA, the driver won't
  343. * necessarily call ieee80211_stop_tx_ba_cb(), so this may
  344. * seem like we can leave the tid_tx data pending forever.
  345. * This is true, in a way, but "forever" is only until the
  346. * station struct is actually destroyed. In the meantime,
  347. * leaving it around ensures that we don't transmit packets
  348. * to the driver on this TID which might confuse it.
  349. */
  350. return 0;
  351. }
  352. /*
  353. * After sending add Block Ack request we activated a timer until
  354. * add Block Ack response will arrive from the recipient.
  355. * If this timer expires sta_addba_resp_timer_expired will be executed.
  356. */
  357. static void sta_addba_resp_timer_expired(unsigned long data)
  358. {
  359. /* not an elegant detour, but there is no choice as the timer passes
  360. * only one argument, and both sta_info and TID are needed, so init
  361. * flow in sta_info_create gives the TID as data, while the timer_to_id
  362. * array gives the sta through container_of */
  363. u16 tid = *(u8 *)data;
  364. struct sta_info *sta = container_of((void *)data,
  365. struct sta_info, timer_to_tid[tid]);
  366. struct tid_ampdu_tx *tid_tx;
  367. /* check if the TID waits for addBA response */
  368. rcu_read_lock();
  369. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  370. if (!tid_tx ||
  371. test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state)) {
  372. rcu_read_unlock();
  373. ht_dbg(sta->sdata,
  374. "timer expired on %pM tid %d but we are not (or no longer) expecting addBA response there\n",
  375. sta->sta.addr, tid);
  376. return;
  377. }
  378. ht_dbg(sta->sdata, "addBA response timer expired on %pM tid %d\n",
  379. sta->sta.addr, tid);
  380. ieee80211_stop_tx_ba_session(&sta->sta, tid);
  381. rcu_read_unlock();
  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. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  398. /*
  399. * Start queuing up packets for this aggregation session.
  400. * We're going to release them once the driver is OK with
  401. * that.
  402. */
  403. clear_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
  404. ieee80211_agg_stop_txq(sta, tid);
  405. /*
  406. * Make sure no packets are being processed. This ensures that
  407. * we have a valid starting sequence number and that in-flight
  408. * packets have been flushed out and no packets for this TID
  409. * will go into the driver during the ampdu_action call.
  410. */
  411. synchronize_net();
  412. params.ssn = sta->tid_seq[tid] >> 4;
  413. ret = drv_ampdu_action(local, sdata, &params);
  414. if (ret) {
  415. ht_dbg(sdata,
  416. "BA request denied - HW unavailable for %pM tid %d\n",
  417. sta->sta.addr, tid);
  418. spin_lock_bh(&sta->lock);
  419. ieee80211_agg_splice_packets(sdata, tid_tx, tid);
  420. ieee80211_assign_tid_tx(sta, tid, NULL);
  421. ieee80211_agg_splice_finish(sdata, tid);
  422. spin_unlock_bh(&sta->lock);
  423. ieee80211_agg_start_txq(sta, tid, false);
  424. kfree_rcu(tid_tx, rcu_head);
  425. return;
  426. }
  427. /* activate the timer for the recipient's addBA response */
  428. mod_timer(&tid_tx->addba_resp_timer, jiffies + ADDBA_RESP_INTERVAL);
  429. ht_dbg(sdata, "activated addBA response timer on %pM tid %d\n",
  430. sta->sta.addr, tid);
  431. spin_lock_bh(&sta->lock);
  432. sta->ampdu_mlme.last_addba_req_time[tid] = jiffies;
  433. sta->ampdu_mlme.addba_req_num[tid]++;
  434. spin_unlock_bh(&sta->lock);
  435. /* send AddBA request */
  436. ieee80211_send_addba_request(sdata, sta->sta.addr, tid,
  437. tid_tx->dialog_token, params.ssn,
  438. IEEE80211_MAX_AMPDU_BUF,
  439. tid_tx->timeout);
  440. }
  441. /*
  442. * After accepting the AddBA Response we activated a timer,
  443. * resetting it after each frame that we send.
  444. */
  445. static void sta_tx_agg_session_timer_expired(unsigned long data)
  446. {
  447. /* not an elegant detour, but there is no choice as the timer passes
  448. * only one argument, and various sta_info are needed here, so init
  449. * flow in sta_info_create gives the TID as data, while the timer_to_id
  450. * array gives the sta through container_of */
  451. u8 *ptid = (u8 *)data;
  452. u8 *timer_to_id = ptid - *ptid;
  453. struct sta_info *sta = container_of(timer_to_id, struct sta_info,
  454. timer_to_tid[0]);
  455. struct tid_ampdu_tx *tid_tx;
  456. unsigned long timeout;
  457. rcu_read_lock();
  458. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[*ptid]);
  459. if (!tid_tx || test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  460. rcu_read_unlock();
  461. return;
  462. }
  463. timeout = tid_tx->last_tx + TU_TO_JIFFIES(tid_tx->timeout);
  464. if (time_is_after_jiffies(timeout)) {
  465. mod_timer(&tid_tx->session_timer, timeout);
  466. rcu_read_unlock();
  467. return;
  468. }
  469. rcu_read_unlock();
  470. ht_dbg(sta->sdata, "tx session timer expired on %pM tid %d\n",
  471. sta->sta.addr, (u16)*ptid);
  472. ieee80211_stop_tx_ba_session(&sta->sta, *ptid);
  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 - 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. setup_timer(&tid_tx->addba_resp_timer,
  569. sta_addba_resp_timer_expired,
  570. (unsigned long)&sta->timer_to_tid[tid]);
  571. /* tx timer */
  572. setup_deferrable_timer(&tid_tx->session_timer,
  573. sta_tx_agg_session_timer_expired,
  574. (unsigned long)&sta->timer_to_tid[tid]);
  575. /* assign a dialog token */
  576. sta->ampdu_mlme.dialog_token_allocator++;
  577. tid_tx->dialog_token = sta->ampdu_mlme.dialog_token_allocator;
  578. /*
  579. * Finally, assign it to the start array; the work item will
  580. * collect it and move it to the normal array.
  581. */
  582. sta->ampdu_mlme.tid_start_tx[tid] = tid_tx;
  583. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  584. /* this flow continues off the work */
  585. err_unlock_sta:
  586. spin_unlock_bh(&sta->lock);
  587. return ret;
  588. }
  589. EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
  590. static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
  591. struct sta_info *sta, u16 tid)
  592. {
  593. struct tid_ampdu_tx *tid_tx;
  594. struct ieee80211_ampdu_params params = {
  595. .sta = &sta->sta,
  596. .action = IEEE80211_AMPDU_TX_OPERATIONAL,
  597. .tid = tid,
  598. .timeout = 0,
  599. .ssn = 0,
  600. };
  601. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  602. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  603. params.buf_size = tid_tx->buf_size;
  604. params.amsdu = tid_tx->amsdu;
  605. ht_dbg(sta->sdata, "Aggregation is on for %pM tid %d\n",
  606. sta->sta.addr, tid);
  607. drv_ampdu_action(local, sta->sdata, &params);
  608. /*
  609. * synchronize with TX path, while splicing the TX path
  610. * should block so it won't put more packets onto pending.
  611. */
  612. spin_lock_bh(&sta->lock);
  613. ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
  614. /*
  615. * Now mark as operational. This will be visible
  616. * in the TX path, and lets it go lock-free in
  617. * the common case.
  618. */
  619. set_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
  620. ieee80211_agg_splice_finish(sta->sdata, tid);
  621. spin_unlock_bh(&sta->lock);
  622. ieee80211_agg_start_txq(sta, tid, true);
  623. }
  624. void ieee80211_start_tx_ba_cb(struct sta_info *sta, int tid,
  625. struct tid_ampdu_tx *tid_tx)
  626. {
  627. struct ieee80211_sub_if_data *sdata = sta->sdata;
  628. struct ieee80211_local *local = sdata->local;
  629. if (WARN_ON(test_and_set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state)))
  630. return;
  631. if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state))
  632. ieee80211_agg_tx_operational(local, sta, tid);
  633. }
  634. static struct tid_ampdu_tx *
  635. ieee80211_lookup_tid_tx(struct ieee80211_sub_if_data *sdata,
  636. const u8 *ra, u16 tid, struct sta_info **sta)
  637. {
  638. struct tid_ampdu_tx *tid_tx;
  639. if (tid >= IEEE80211_NUM_TIDS) {
  640. ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
  641. tid, IEEE80211_NUM_TIDS);
  642. return NULL;
  643. }
  644. *sta = sta_info_get_bss(sdata, ra);
  645. if (!*sta) {
  646. ht_dbg(sdata, "Could not find station: %pM\n", ra);
  647. return NULL;
  648. }
  649. tid_tx = rcu_dereference((*sta)->ampdu_mlme.tid_tx[tid]);
  650. if (WARN_ON(!tid_tx))
  651. ht_dbg(sdata, "addBA was not requested!\n");
  652. return tid_tx;
  653. }
  654. void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  655. const u8 *ra, u16 tid)
  656. {
  657. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  658. struct ieee80211_local *local = sdata->local;
  659. struct sta_info *sta;
  660. struct tid_ampdu_tx *tid_tx;
  661. trace_api_start_tx_ba_cb(sdata, ra, tid);
  662. rcu_read_lock();
  663. tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
  664. if (!tid_tx)
  665. goto out;
  666. set_bit(HT_AGG_STATE_START_CB, &tid_tx->state);
  667. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  668. out:
  669. rcu_read_unlock();
  670. }
  671. EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
  672. int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
  673. enum ieee80211_agg_stop_reason reason)
  674. {
  675. int ret;
  676. mutex_lock(&sta->ampdu_mlme.mtx);
  677. ret = ___ieee80211_stop_tx_ba_session(sta, tid, reason);
  678. mutex_unlock(&sta->ampdu_mlme.mtx);
  679. return ret;
  680. }
  681. int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
  682. {
  683. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  684. struct ieee80211_sub_if_data *sdata = sta->sdata;
  685. struct ieee80211_local *local = sdata->local;
  686. struct tid_ampdu_tx *tid_tx;
  687. int ret = 0;
  688. trace_api_stop_tx_ba_session(pubsta, tid);
  689. if (!local->ops->ampdu_action)
  690. return -EINVAL;
  691. if (tid >= IEEE80211_NUM_TIDS)
  692. return -EINVAL;
  693. spin_lock_bh(&sta->lock);
  694. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  695. if (!tid_tx) {
  696. ret = -ENOENT;
  697. goto unlock;
  698. }
  699. WARN(sta->reserved_tid == tid,
  700. "Requested to stop BA session on reserved tid=%d", tid);
  701. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  702. /* already in progress stopping it */
  703. ret = 0;
  704. goto unlock;
  705. }
  706. set_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state);
  707. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  708. unlock:
  709. spin_unlock_bh(&sta->lock);
  710. return ret;
  711. }
  712. EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
  713. void ieee80211_stop_tx_ba_cb(struct sta_info *sta, int tid,
  714. struct tid_ampdu_tx *tid_tx)
  715. {
  716. struct ieee80211_sub_if_data *sdata = sta->sdata;
  717. bool send_delba = false;
  718. ht_dbg(sdata, "Stopping Tx BA session for %pM tid %d\n",
  719. sta->sta.addr, tid);
  720. spin_lock_bh(&sta->lock);
  721. if (!test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  722. ht_dbg(sdata,
  723. "unexpected callback to A-MPDU stop for %pM tid %d\n",
  724. sta->sta.addr, tid);
  725. goto unlock_sta;
  726. }
  727. if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop)
  728. send_delba = true;
  729. ieee80211_remove_tid_tx(sta, tid);
  730. unlock_sta:
  731. spin_unlock_bh(&sta->lock);
  732. if (send_delba)
  733. ieee80211_send_delba(sdata, sta->sta.addr, tid,
  734. WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
  735. }
  736. void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  737. const u8 *ra, u16 tid)
  738. {
  739. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  740. struct ieee80211_local *local = sdata->local;
  741. struct sta_info *sta;
  742. struct tid_ampdu_tx *tid_tx;
  743. trace_api_stop_tx_ba_cb(sdata, ra, tid);
  744. rcu_read_lock();
  745. tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
  746. if (!tid_tx)
  747. goto out;
  748. set_bit(HT_AGG_STATE_STOP_CB, &tid_tx->state);
  749. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  750. out:
  751. rcu_read_unlock();
  752. }
  753. EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
  754. void ieee80211_process_addba_resp(struct ieee80211_local *local,
  755. struct sta_info *sta,
  756. struct ieee80211_mgmt *mgmt,
  757. size_t len)
  758. {
  759. struct tid_ampdu_tx *tid_tx;
  760. struct ieee80211_txq *txq;
  761. u16 capab, tid;
  762. u8 buf_size;
  763. bool amsdu;
  764. capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
  765. amsdu = capab & IEEE80211_ADDBA_PARAM_AMSDU_MASK;
  766. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  767. buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
  768. buf_size = min(buf_size, local->hw.max_tx_aggregation_subframes);
  769. txq = sta->sta.txq[tid];
  770. if (!amsdu && txq)
  771. set_bit(IEEE80211_TXQ_NO_AMSDU, &to_txq_info(txq)->flags);
  772. mutex_lock(&sta->ampdu_mlme.mtx);
  773. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  774. if (!tid_tx)
  775. goto out;
  776. if (mgmt->u.action.u.addba_resp.dialog_token != tid_tx->dialog_token) {
  777. ht_dbg(sta->sdata, "wrong addBA response token, %pM tid %d\n",
  778. sta->sta.addr, tid);
  779. goto out;
  780. }
  781. del_timer_sync(&tid_tx->addba_resp_timer);
  782. ht_dbg(sta->sdata, "switched off addBA timer for %pM tid %d\n",
  783. sta->sta.addr, tid);
  784. /*
  785. * addba_resp_timer may have fired before we got here, and
  786. * caused WANT_STOP to be set. If the stop then was already
  787. * processed further, STOPPING might be set.
  788. */
  789. if (test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state) ||
  790. test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  791. ht_dbg(sta->sdata,
  792. "got addBA resp for %pM tid %d but we already gave up\n",
  793. sta->sta.addr, tid);
  794. goto out;
  795. }
  796. /*
  797. * IEEE 802.11-2007 7.3.1.14:
  798. * In an ADDBA Response frame, when the Status Code field
  799. * is set to 0, the Buffer Size subfield is set to a value
  800. * of at least 1.
  801. */
  802. if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
  803. == WLAN_STATUS_SUCCESS && buf_size) {
  804. if (test_and_set_bit(HT_AGG_STATE_RESPONSE_RECEIVED,
  805. &tid_tx->state)) {
  806. /* ignore duplicate response */
  807. goto out;
  808. }
  809. tid_tx->buf_size = buf_size;
  810. tid_tx->amsdu = amsdu;
  811. if (test_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state))
  812. ieee80211_agg_tx_operational(local, sta, tid);
  813. sta->ampdu_mlme.addba_req_num[tid] = 0;
  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. }