agg-tx.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. if (WARN_ON(tid >= IEEE80211_FIRST_TSPEC_TSID))
  497. return -EINVAL;
  498. ht_dbg(sdata, "Open BA session requested for %pM tid %u\n",
  499. pubsta->addr, tid);
  500. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  501. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  502. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  503. sdata->vif.type != NL80211_IFTYPE_AP &&
  504. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  505. return -EINVAL;
  506. if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
  507. ht_dbg(sdata,
  508. "BA sessions blocked - Denying BA session request %pM tid %d\n",
  509. sta->sta.addr, tid);
  510. return -EINVAL;
  511. }
  512. /*
  513. * 802.11n-2009 11.5.1.1: If the initiating STA is an HT STA, is a
  514. * member of an IBSS, and has no other existing Block Ack agreement
  515. * with the recipient STA, then the initiating STA shall transmit a
  516. * Probe Request frame to the recipient STA and shall not transmit an
  517. * ADDBA Request frame unless it receives a Probe Response frame
  518. * from the recipient within dot11ADDBAFailureTimeout.
  519. *
  520. * The probe request mechanism for ADDBA is currently not implemented,
  521. * but we only build up Block Ack session with HT STAs. This information
  522. * is set when we receive a bss info from a probe response or a beacon.
  523. */
  524. if (sta->sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  525. !sta->sta.ht_cap.ht_supported) {
  526. ht_dbg(sdata,
  527. "BA request denied - IBSS STA %pM does not advertise HT support\n",
  528. pubsta->addr);
  529. return -EINVAL;
  530. }
  531. spin_lock_bh(&sta->lock);
  532. /* we have tried too many times, receiver does not want A-MPDU */
  533. if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
  534. ret = -EBUSY;
  535. goto err_unlock_sta;
  536. }
  537. /*
  538. * if we have tried more than HT_AGG_BURST_RETRIES times we
  539. * will spread our requests in time to avoid stalling connection
  540. * for too long
  541. */
  542. if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_BURST_RETRIES &&
  543. time_before(jiffies, sta->ampdu_mlme.last_addba_req_time[tid] +
  544. HT_AGG_RETRIES_PERIOD)) {
  545. ht_dbg(sdata,
  546. "BA request denied - waiting a grace period after %d failed requests on %pM tid %u\n",
  547. sta->ampdu_mlme.addba_req_num[tid], sta->sta.addr, tid);
  548. ret = -EBUSY;
  549. goto err_unlock_sta;
  550. }
  551. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  552. /* check if the TID is not in aggregation flow already */
  553. if (tid_tx || sta->ampdu_mlme.tid_start_tx[tid]) {
  554. ht_dbg(sdata,
  555. "BA request denied - session is not idle on %pM tid %u\n",
  556. sta->sta.addr, tid);
  557. ret = -EAGAIN;
  558. goto err_unlock_sta;
  559. }
  560. /* prepare A-MPDU MLME for Tx aggregation */
  561. tid_tx = kzalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
  562. if (!tid_tx) {
  563. ret = -ENOMEM;
  564. goto err_unlock_sta;
  565. }
  566. skb_queue_head_init(&tid_tx->pending);
  567. __set_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
  568. tid_tx->timeout = timeout;
  569. /* response timer */
  570. tid_tx->addba_resp_timer.function = sta_addba_resp_timer_expired;
  571. tid_tx->addba_resp_timer.data = (unsigned long)&sta->timer_to_tid[tid];
  572. init_timer(&tid_tx->addba_resp_timer);
  573. /* tx timer */
  574. tid_tx->session_timer.function = sta_tx_agg_session_timer_expired;
  575. tid_tx->session_timer.data = (unsigned long)&sta->timer_to_tid[tid];
  576. init_timer_deferrable(&tid_tx->session_timer);
  577. /* assign a dialog token */
  578. sta->ampdu_mlme.dialog_token_allocator++;
  579. tid_tx->dialog_token = sta->ampdu_mlme.dialog_token_allocator;
  580. /*
  581. * Finally, assign it to the start array; the work item will
  582. * collect it and move it to the normal array.
  583. */
  584. sta->ampdu_mlme.tid_start_tx[tid] = tid_tx;
  585. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  586. /* this flow continues off the work */
  587. err_unlock_sta:
  588. spin_unlock_bh(&sta->lock);
  589. return ret;
  590. }
  591. EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
  592. static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
  593. struct sta_info *sta, u16 tid)
  594. {
  595. struct tid_ampdu_tx *tid_tx;
  596. struct ieee80211_ampdu_params params = {
  597. .sta = &sta->sta,
  598. .action = IEEE80211_AMPDU_TX_OPERATIONAL,
  599. .tid = tid,
  600. .timeout = 0,
  601. .ssn = 0,
  602. };
  603. lockdep_assert_held(&sta->ampdu_mlme.mtx);
  604. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  605. params.buf_size = tid_tx->buf_size;
  606. params.amsdu = tid_tx->amsdu;
  607. ht_dbg(sta->sdata, "Aggregation is on for %pM tid %d\n",
  608. sta->sta.addr, tid);
  609. drv_ampdu_action(local, sta->sdata, &params);
  610. /*
  611. * synchronize with TX path, while splicing the TX path
  612. * should block so it won't put more packets onto pending.
  613. */
  614. spin_lock_bh(&sta->lock);
  615. ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
  616. /*
  617. * Now mark as operational. This will be visible
  618. * in the TX path, and lets it go lock-free in
  619. * the common case.
  620. */
  621. set_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
  622. ieee80211_agg_splice_finish(sta->sdata, tid);
  623. spin_unlock_bh(&sta->lock);
  624. ieee80211_agg_start_txq(sta, tid, true);
  625. }
  626. void ieee80211_start_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u16 tid)
  627. {
  628. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  629. struct ieee80211_local *local = sdata->local;
  630. struct sta_info *sta;
  631. struct tid_ampdu_tx *tid_tx;
  632. trace_api_start_tx_ba_cb(sdata, ra, tid);
  633. if (tid >= IEEE80211_NUM_TIDS) {
  634. ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
  635. tid, IEEE80211_NUM_TIDS);
  636. return;
  637. }
  638. mutex_lock(&local->sta_mtx);
  639. sta = sta_info_get_bss(sdata, ra);
  640. if (!sta) {
  641. mutex_unlock(&local->sta_mtx);
  642. ht_dbg(sdata, "Could not find station: %pM\n", ra);
  643. return;
  644. }
  645. mutex_lock(&sta->ampdu_mlme.mtx);
  646. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  647. if (WARN_ON(!tid_tx)) {
  648. ht_dbg(sdata, "addBA was not requested!\n");
  649. goto unlock;
  650. }
  651. if (WARN_ON(test_and_set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state)))
  652. goto unlock;
  653. if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state))
  654. ieee80211_agg_tx_operational(local, sta, tid);
  655. unlock:
  656. mutex_unlock(&sta->ampdu_mlme.mtx);
  657. mutex_unlock(&local->sta_mtx);
  658. }
  659. void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  660. const u8 *ra, u16 tid)
  661. {
  662. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  663. struct ieee80211_local *local = sdata->local;
  664. struct ieee80211_ra_tid *ra_tid;
  665. struct sk_buff *skb = dev_alloc_skb(0);
  666. if (unlikely(!skb))
  667. return;
  668. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  669. memcpy(&ra_tid->ra, ra, ETH_ALEN);
  670. ra_tid->tid = tid;
  671. skb->pkt_type = IEEE80211_SDATA_QUEUE_AGG_START;
  672. skb_queue_tail(&sdata->skb_queue, skb);
  673. ieee80211_queue_work(&local->hw, &sdata->work);
  674. }
  675. EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
  676. int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
  677. enum ieee80211_agg_stop_reason reason)
  678. {
  679. int ret;
  680. mutex_lock(&sta->ampdu_mlme.mtx);
  681. ret = ___ieee80211_stop_tx_ba_session(sta, tid, reason);
  682. mutex_unlock(&sta->ampdu_mlme.mtx);
  683. return ret;
  684. }
  685. int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
  686. {
  687. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  688. struct ieee80211_sub_if_data *sdata = sta->sdata;
  689. struct ieee80211_local *local = sdata->local;
  690. struct tid_ampdu_tx *tid_tx;
  691. int ret = 0;
  692. trace_api_stop_tx_ba_session(pubsta, tid);
  693. if (!local->ops->ampdu_action)
  694. return -EINVAL;
  695. if (tid >= IEEE80211_NUM_TIDS)
  696. return -EINVAL;
  697. spin_lock_bh(&sta->lock);
  698. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  699. if (!tid_tx) {
  700. ret = -ENOENT;
  701. goto unlock;
  702. }
  703. WARN(sta->reserved_tid == tid,
  704. "Requested to stop BA session on reserved tid=%d", tid);
  705. if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  706. /* already in progress stopping it */
  707. ret = 0;
  708. goto unlock;
  709. }
  710. set_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state);
  711. ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
  712. unlock:
  713. spin_unlock_bh(&sta->lock);
  714. return ret;
  715. }
  716. EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
  717. void ieee80211_stop_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u8 tid)
  718. {
  719. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  720. struct ieee80211_local *local = sdata->local;
  721. struct sta_info *sta;
  722. struct tid_ampdu_tx *tid_tx;
  723. bool send_delba = false;
  724. trace_api_stop_tx_ba_cb(sdata, ra, tid);
  725. if (tid >= IEEE80211_NUM_TIDS) {
  726. ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
  727. tid, IEEE80211_NUM_TIDS);
  728. return;
  729. }
  730. ht_dbg(sdata, "Stopping Tx BA session for %pM tid %d\n", ra, tid);
  731. mutex_lock(&local->sta_mtx);
  732. sta = sta_info_get_bss(sdata, ra);
  733. if (!sta) {
  734. ht_dbg(sdata, "Could not find station: %pM\n", ra);
  735. goto unlock;
  736. }
  737. mutex_lock(&sta->ampdu_mlme.mtx);
  738. spin_lock_bh(&sta->lock);
  739. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  740. if (!tid_tx || !test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  741. ht_dbg(sdata,
  742. "unexpected callback to A-MPDU stop for %pM tid %d\n",
  743. sta->sta.addr, tid);
  744. goto unlock_sta;
  745. }
  746. if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop)
  747. send_delba = true;
  748. ieee80211_remove_tid_tx(sta, tid);
  749. unlock_sta:
  750. spin_unlock_bh(&sta->lock);
  751. if (send_delba)
  752. ieee80211_send_delba(sdata, ra, tid,
  753. WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
  754. mutex_unlock(&sta->ampdu_mlme.mtx);
  755. unlock:
  756. mutex_unlock(&local->sta_mtx);
  757. }
  758. void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
  759. const u8 *ra, u16 tid)
  760. {
  761. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  762. struct ieee80211_local *local = sdata->local;
  763. struct ieee80211_ra_tid *ra_tid;
  764. struct sk_buff *skb = dev_alloc_skb(0);
  765. if (unlikely(!skb))
  766. return;
  767. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  768. memcpy(&ra_tid->ra, ra, ETH_ALEN);
  769. ra_tid->tid = tid;
  770. skb->pkt_type = IEEE80211_SDATA_QUEUE_AGG_STOP;
  771. skb_queue_tail(&sdata->skb_queue, skb);
  772. ieee80211_queue_work(&local->hw, &sdata->work);
  773. }
  774. EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
  775. void ieee80211_process_addba_resp(struct ieee80211_local *local,
  776. struct sta_info *sta,
  777. struct ieee80211_mgmt *mgmt,
  778. size_t len)
  779. {
  780. struct tid_ampdu_tx *tid_tx;
  781. struct ieee80211_txq *txq;
  782. u16 capab, tid;
  783. u8 buf_size;
  784. bool amsdu;
  785. capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
  786. amsdu = capab & IEEE80211_ADDBA_PARAM_AMSDU_MASK;
  787. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  788. buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
  789. buf_size = min(buf_size, local->hw.max_tx_aggregation_subframes);
  790. txq = sta->sta.txq[tid];
  791. if (!amsdu && txq)
  792. set_bit(IEEE80211_TXQ_NO_AMSDU, &to_txq_info(txq)->flags);
  793. mutex_lock(&sta->ampdu_mlme.mtx);
  794. tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
  795. if (!tid_tx)
  796. goto out;
  797. if (mgmt->u.action.u.addba_resp.dialog_token != tid_tx->dialog_token) {
  798. ht_dbg(sta->sdata, "wrong addBA response token, %pM tid %d\n",
  799. sta->sta.addr, tid);
  800. goto out;
  801. }
  802. del_timer_sync(&tid_tx->addba_resp_timer);
  803. ht_dbg(sta->sdata, "switched off addBA timer for %pM tid %d\n",
  804. sta->sta.addr, tid);
  805. /*
  806. * addba_resp_timer may have fired before we got here, and
  807. * caused WANT_STOP to be set. If the stop then was already
  808. * processed further, STOPPING might be set.
  809. */
  810. if (test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state) ||
  811. test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
  812. ht_dbg(sta->sdata,
  813. "got addBA resp for %pM tid %d but we already gave up\n",
  814. sta->sta.addr, tid);
  815. goto out;
  816. }
  817. /*
  818. * IEEE 802.11-2007 7.3.1.14:
  819. * In an ADDBA Response frame, when the Status Code field
  820. * is set to 0, the Buffer Size subfield is set to a value
  821. * of at least 1.
  822. */
  823. if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
  824. == WLAN_STATUS_SUCCESS && buf_size) {
  825. if (test_and_set_bit(HT_AGG_STATE_RESPONSE_RECEIVED,
  826. &tid_tx->state)) {
  827. /* ignore duplicate response */
  828. goto out;
  829. }
  830. tid_tx->buf_size = buf_size;
  831. tid_tx->amsdu = amsdu;
  832. if (test_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state))
  833. ieee80211_agg_tx_operational(local, sta, tid);
  834. sta->ampdu_mlme.addba_req_num[tid] = 0;
  835. if (tid_tx->timeout) {
  836. mod_timer(&tid_tx->session_timer,
  837. TU_TO_EXP_TIME(tid_tx->timeout));
  838. tid_tx->last_tx = jiffies;
  839. }
  840. } else {
  841. ___ieee80211_stop_tx_ba_session(sta, tid, AGG_STOP_DECLINED);
  842. }
  843. out:
  844. mutex_unlock(&sta->ampdu_mlme.mtx);
  845. }