ht.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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-2008, Intel Corporation
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/ieee80211.h>
  16. #include <net/wireless.h>
  17. #include <net/mac80211.h>
  18. #include "ieee80211_i.h"
  19. #include "sta_info.h"
  20. #include "wme.h"
  21. int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
  22. struct ieee80211_ht_info *ht_info)
  23. {
  24. if (ht_info == NULL)
  25. return -EINVAL;
  26. memset(ht_info, 0, sizeof(*ht_info));
  27. if (ht_cap_ie) {
  28. u8 ampdu_info = ht_cap_ie->ampdu_params_info;
  29. ht_info->ht_supported = 1;
  30. ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
  31. ht_info->ampdu_factor =
  32. ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
  33. ht_info->ampdu_density =
  34. (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
  35. memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
  36. } else
  37. ht_info->ht_supported = 0;
  38. return 0;
  39. }
  40. int ieee80211_ht_addt_info_ie_to_ht_bss_info(
  41. struct ieee80211_ht_addt_info *ht_add_info_ie,
  42. struct ieee80211_ht_bss_info *bss_info)
  43. {
  44. if (bss_info == NULL)
  45. return -EINVAL;
  46. memset(bss_info, 0, sizeof(*bss_info));
  47. if (ht_add_info_ie) {
  48. u16 op_mode;
  49. op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
  50. bss_info->primary_channel = ht_add_info_ie->control_chan;
  51. bss_info->bss_cap = ht_add_info_ie->ht_param;
  52. bss_info->bss_op_mode = (u8)(op_mode & 0xff);
  53. }
  54. return 0;
  55. }
  56. static void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata,
  57. const u8 *da, u16 tid,
  58. u8 dialog_token, u16 start_seq_num,
  59. u16 agg_size, u16 timeout)
  60. {
  61. struct ieee80211_local *local = sdata->local;
  62. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  63. struct sk_buff *skb;
  64. struct ieee80211_mgmt *mgmt;
  65. u16 capab;
  66. skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
  67. if (!skb) {
  68. printk(KERN_ERR "%s: failed to allocate buffer "
  69. "for addba request frame\n", sdata->dev->name);
  70. return;
  71. }
  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->dev->dev_addr, ETH_ALEN);
  77. if (sdata->vif.type == NL80211_IFTYPE_AP)
  78. memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
  79. else
  80. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  81. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  82. IEEE80211_STYPE_ACTION);
  83. skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
  84. mgmt->u.action.category = WLAN_CATEGORY_BACK;
  85. mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
  86. mgmt->u.action.u.addba_req.dialog_token = dialog_token;
  87. capab = (u16)(1 << 1); /* bit 1 aggregation policy */
  88. capab |= (u16)(tid << 2); /* bit 5:2 TID number */
  89. capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
  90. mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
  91. mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
  92. mgmt->u.action.u.addba_req.start_seq_num =
  93. cpu_to_le16(start_seq_num << 4);
  94. ieee80211_tx_skb(sdata, skb, 0);
  95. }
  96. static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
  97. u8 dialog_token, u16 status, u16 policy,
  98. u16 buf_size, u16 timeout)
  99. {
  100. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  101. struct ieee80211_local *local = sdata->local;
  102. struct sk_buff *skb;
  103. struct ieee80211_mgmt *mgmt;
  104. u16 capab;
  105. skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
  106. if (!skb) {
  107. printk(KERN_DEBUG "%s: failed to allocate buffer "
  108. "for addba resp frame\n", sdata->dev->name);
  109. return;
  110. }
  111. skb_reserve(skb, local->hw.extra_tx_headroom);
  112. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  113. memset(mgmt, 0, 24);
  114. memcpy(mgmt->da, da, ETH_ALEN);
  115. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  116. if (sdata->vif.type == NL80211_IFTYPE_AP)
  117. memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
  118. else
  119. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  120. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  121. IEEE80211_STYPE_ACTION);
  122. skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
  123. mgmt->u.action.category = WLAN_CATEGORY_BACK;
  124. mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
  125. mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
  126. capab = (u16)(policy << 1); /* bit 1 aggregation policy */
  127. capab |= (u16)(tid << 2); /* bit 5:2 TID number */
  128. capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
  129. mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
  130. mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
  131. mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
  132. ieee80211_tx_skb(sdata, skb, 0);
  133. }
  134. static void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata,
  135. const u8 *da, u16 tid,
  136. u16 initiator, u16 reason_code)
  137. {
  138. struct ieee80211_local *local = sdata->local;
  139. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  140. struct sk_buff *skb;
  141. struct ieee80211_mgmt *mgmt;
  142. u16 params;
  143. skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
  144. if (!skb) {
  145. printk(KERN_ERR "%s: failed to allocate buffer "
  146. "for delba frame\n", sdata->dev->name);
  147. return;
  148. }
  149. skb_reserve(skb, local->hw.extra_tx_headroom);
  150. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  151. memset(mgmt, 0, 24);
  152. memcpy(mgmt->da, da, ETH_ALEN);
  153. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  154. if (sdata->vif.type == NL80211_IFTYPE_AP)
  155. memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
  156. else
  157. memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
  158. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  159. IEEE80211_STYPE_ACTION);
  160. skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
  161. mgmt->u.action.category = WLAN_CATEGORY_BACK;
  162. mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
  163. params = (u16)(initiator << 11); /* bit 11 initiator */
  164. params |= (u16)(tid << 12); /* bit 15:12 TID number */
  165. mgmt->u.action.u.delba.params = cpu_to_le16(params);
  166. mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
  167. ieee80211_tx_skb(sdata, skb, 0);
  168. }
  169. void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
  170. {
  171. struct ieee80211_local *local = sdata->local;
  172. struct sk_buff *skb;
  173. struct ieee80211_bar *bar;
  174. u16 bar_control = 0;
  175. skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
  176. if (!skb) {
  177. printk(KERN_ERR "%s: failed to allocate buffer for "
  178. "bar frame\n", sdata->dev->name);
  179. return;
  180. }
  181. skb_reserve(skb, local->hw.extra_tx_headroom);
  182. bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
  183. memset(bar, 0, sizeof(*bar));
  184. bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  185. IEEE80211_STYPE_BACK_REQ);
  186. memcpy(bar->ra, ra, ETH_ALEN);
  187. memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
  188. bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
  189. bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
  190. bar_control |= (u16)(tid << 12);
  191. bar->control = cpu_to_le16(bar_control);
  192. bar->start_seq_num = cpu_to_le16(ssn);
  193. ieee80211_tx_skb(sdata, skb, 0);
  194. }
  195. void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
  196. u16 initiator, u16 reason)
  197. {
  198. struct ieee80211_local *local = sdata->local;
  199. struct ieee80211_hw *hw = &local->hw;
  200. struct sta_info *sta;
  201. int ret, i;
  202. DECLARE_MAC_BUF(mac);
  203. rcu_read_lock();
  204. sta = sta_info_get(local, ra);
  205. if (!sta) {
  206. rcu_read_unlock();
  207. return;
  208. }
  209. /* check if TID is in operational state */
  210. spin_lock_bh(&sta->lock);
  211. if (sta->ampdu_mlme.tid_state_rx[tid]
  212. != HT_AGG_STATE_OPERATIONAL) {
  213. spin_unlock_bh(&sta->lock);
  214. rcu_read_unlock();
  215. return;
  216. }
  217. sta->ampdu_mlme.tid_state_rx[tid] =
  218. HT_AGG_STATE_REQ_STOP_BA_MSK |
  219. (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
  220. spin_unlock_bh(&sta->lock);
  221. /* stop HW Rx aggregation. ampdu_action existence
  222. * already verified in session init so we add the BUG_ON */
  223. BUG_ON(!local->ops->ampdu_action);
  224. #ifdef CONFIG_MAC80211_HT_DEBUG
  225. printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
  226. print_mac(mac, ra), tid);
  227. #endif /* CONFIG_MAC80211_HT_DEBUG */
  228. ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
  229. &sta->sta, tid, NULL);
  230. if (ret)
  231. printk(KERN_DEBUG "HW problem - can not stop rx "
  232. "aggregation for tid %d\n", tid);
  233. /* shutdown timer has not expired */
  234. if (initiator != WLAN_BACK_TIMER)
  235. del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
  236. /* check if this is a self generated aggregation halt */
  237. if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
  238. ieee80211_send_delba(sdata, ra, tid, 0, reason);
  239. /* free the reordering buffer */
  240. for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
  241. if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
  242. /* release the reordered frames */
  243. dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
  244. sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
  245. sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
  246. }
  247. }
  248. /* free resources */
  249. kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
  250. kfree(sta->ampdu_mlme.tid_rx[tid]);
  251. sta->ampdu_mlme.tid_rx[tid] = NULL;
  252. sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
  253. rcu_read_unlock();
  254. }
  255. /*
  256. * After sending add Block Ack request we activated a timer until
  257. * add Block Ack response will arrive from the recipient.
  258. * If this timer expires sta_addba_resp_timer_expired will be executed.
  259. */
  260. static void sta_addba_resp_timer_expired(unsigned long data)
  261. {
  262. /* not an elegant detour, but there is no choice as the timer passes
  263. * only one argument, and both sta_info and TID are needed, so init
  264. * flow in sta_info_create gives the TID as data, while the timer_to_id
  265. * array gives the sta through container_of */
  266. u16 tid = *(u8 *)data;
  267. struct sta_info *temp_sta = container_of((void *)data,
  268. struct sta_info, timer_to_tid[tid]);
  269. struct ieee80211_local *local = temp_sta->local;
  270. struct ieee80211_hw *hw = &local->hw;
  271. struct sta_info *sta;
  272. u8 *state;
  273. rcu_read_lock();
  274. sta = sta_info_get(local, temp_sta->sta.addr);
  275. if (!sta) {
  276. rcu_read_unlock();
  277. return;
  278. }
  279. state = &sta->ampdu_mlme.tid_state_tx[tid];
  280. /* check if the TID waits for addBA response */
  281. spin_lock_bh(&sta->lock);
  282. if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
  283. spin_unlock_bh(&sta->lock);
  284. *state = HT_AGG_STATE_IDLE;
  285. #ifdef CONFIG_MAC80211_HT_DEBUG
  286. printk(KERN_DEBUG "timer expired on tid %d but we are not "
  287. "expecting addBA response there", tid);
  288. #endif
  289. goto timer_expired_exit;
  290. }
  291. #ifdef CONFIG_MAC80211_HT_DEBUG
  292. printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
  293. #endif
  294. /* go through the state check in stop_BA_session */
  295. *state = HT_AGG_STATE_OPERATIONAL;
  296. spin_unlock_bh(&sta->lock);
  297. ieee80211_stop_tx_ba_session(hw, temp_sta->sta.addr, tid,
  298. WLAN_BACK_INITIATOR);
  299. timer_expired_exit:
  300. rcu_read_unlock();
  301. }
  302. void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
  303. {
  304. struct ieee80211_local *local = sdata->local;
  305. int i;
  306. for (i = 0; i < STA_TID_NUM; i++) {
  307. ieee80211_stop_tx_ba_session(&local->hw, addr, i,
  308. WLAN_BACK_INITIATOR);
  309. ieee80211_sta_stop_rx_ba_session(sdata, addr, i,
  310. WLAN_BACK_RECIPIENT,
  311. WLAN_REASON_QSTA_LEAVE_QBSS);
  312. }
  313. }
  314. int ieee80211_start_tx_ba_session(struct ieee80211_hw *hw, u8 *ra, u16 tid)
  315. {
  316. struct ieee80211_local *local = hw_to_local(hw);
  317. struct sta_info *sta;
  318. struct ieee80211_sub_if_data *sdata;
  319. u16 start_seq_num;
  320. u8 *state;
  321. int ret;
  322. DECLARE_MAC_BUF(mac);
  323. if (tid >= STA_TID_NUM)
  324. return -EINVAL;
  325. #ifdef CONFIG_MAC80211_HT_DEBUG
  326. printk(KERN_DEBUG "Open BA session requested for %s tid %u\n",
  327. print_mac(mac, ra), tid);
  328. #endif /* CONFIG_MAC80211_HT_DEBUG */
  329. rcu_read_lock();
  330. sta = sta_info_get(local, ra);
  331. if (!sta) {
  332. #ifdef CONFIG_MAC80211_HT_DEBUG
  333. printk(KERN_DEBUG "Could not find the station\n");
  334. #endif
  335. ret = -ENOENT;
  336. goto exit;
  337. }
  338. spin_lock_bh(&sta->lock);
  339. /* we have tried too many times, receiver does not want A-MPDU */
  340. if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
  341. ret = -EBUSY;
  342. goto err_unlock_sta;
  343. }
  344. state = &sta->ampdu_mlme.tid_state_tx[tid];
  345. /* check if the TID is not in aggregation flow already */
  346. if (*state != HT_AGG_STATE_IDLE) {
  347. #ifdef CONFIG_MAC80211_HT_DEBUG
  348. printk(KERN_DEBUG "BA request denied - session is not "
  349. "idle on tid %u\n", tid);
  350. #endif /* CONFIG_MAC80211_HT_DEBUG */
  351. ret = -EAGAIN;
  352. goto err_unlock_sta;
  353. }
  354. /* prepare A-MPDU MLME for Tx aggregation */
  355. sta->ampdu_mlme.tid_tx[tid] =
  356. kmalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
  357. if (!sta->ampdu_mlme.tid_tx[tid]) {
  358. #ifdef CONFIG_MAC80211_HT_DEBUG
  359. if (net_ratelimit())
  360. printk(KERN_ERR "allocate tx mlme to tid %d failed\n",
  361. tid);
  362. #endif
  363. ret = -ENOMEM;
  364. goto err_unlock_sta;
  365. }
  366. /* Tx timer */
  367. sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.function =
  368. sta_addba_resp_timer_expired;
  369. sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.data =
  370. (unsigned long)&sta->timer_to_tid[tid];
  371. init_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
  372. /* create a new queue for this aggregation */
  373. ret = ieee80211_ht_agg_queue_add(local, sta, tid);
  374. /* case no queue is available to aggregation
  375. * don't switch to aggregation */
  376. if (ret) {
  377. #ifdef CONFIG_MAC80211_HT_DEBUG
  378. printk(KERN_DEBUG "BA request denied - queue unavailable for"
  379. " tid %d\n", tid);
  380. #endif /* CONFIG_MAC80211_HT_DEBUG */
  381. goto err_unlock_queue;
  382. }
  383. sdata = sta->sdata;
  384. /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
  385. * call back right away, it must see that the flow has begun */
  386. *state |= HT_ADDBA_REQUESTED_MSK;
  387. /* This is slightly racy because the queue isn't stopped */
  388. start_seq_num = sta->tid_seq[tid];
  389. if (local->ops->ampdu_action)
  390. ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_START,
  391. &sta->sta, tid, &start_seq_num);
  392. if (ret) {
  393. /* No need to requeue the packets in the agg queue, since we
  394. * held the tx lock: no packet could be enqueued to the newly
  395. * allocated queue */
  396. ieee80211_ht_agg_queue_remove(local, sta, tid, 0);
  397. #ifdef CONFIG_MAC80211_HT_DEBUG
  398. printk(KERN_DEBUG "BA request denied - HW unavailable for"
  399. " tid %d\n", tid);
  400. #endif /* CONFIG_MAC80211_HT_DEBUG */
  401. *state = HT_AGG_STATE_IDLE;
  402. goto err_unlock_queue;
  403. }
  404. /* Will put all the packets in the new SW queue */
  405. ieee80211_requeue(local, ieee802_1d_to_ac[tid]);
  406. spin_unlock_bh(&sta->lock);
  407. /* send an addBA request */
  408. sta->ampdu_mlme.dialog_token_allocator++;
  409. sta->ampdu_mlme.tid_tx[tid]->dialog_token =
  410. sta->ampdu_mlme.dialog_token_allocator;
  411. sta->ampdu_mlme.tid_tx[tid]->ssn = start_seq_num;
  412. ieee80211_send_addba_request(sta->sdata, ra, tid,
  413. sta->ampdu_mlme.tid_tx[tid]->dialog_token,
  414. sta->ampdu_mlme.tid_tx[tid]->ssn,
  415. 0x40, 5000);
  416. /* activate the timer for the recipient's addBA response */
  417. sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.expires =
  418. jiffies + ADDBA_RESP_INTERVAL;
  419. add_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
  420. #ifdef CONFIG_MAC80211_HT_DEBUG
  421. printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
  422. #endif
  423. goto exit;
  424. err_unlock_queue:
  425. kfree(sta->ampdu_mlme.tid_tx[tid]);
  426. sta->ampdu_mlme.tid_tx[tid] = NULL;
  427. ret = -EBUSY;
  428. err_unlock_sta:
  429. spin_unlock_bh(&sta->lock);
  430. exit:
  431. rcu_read_unlock();
  432. return ret;
  433. }
  434. EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
  435. int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
  436. u8 *ra, u16 tid,
  437. enum ieee80211_back_parties initiator)
  438. {
  439. struct ieee80211_local *local = hw_to_local(hw);
  440. struct sta_info *sta;
  441. u8 *state;
  442. int ret = 0;
  443. DECLARE_MAC_BUF(mac);
  444. if (tid >= STA_TID_NUM)
  445. return -EINVAL;
  446. rcu_read_lock();
  447. sta = sta_info_get(local, ra);
  448. if (!sta) {
  449. rcu_read_unlock();
  450. return -ENOENT;
  451. }
  452. /* check if the TID is in aggregation */
  453. state = &sta->ampdu_mlme.tid_state_tx[tid];
  454. spin_lock_bh(&sta->lock);
  455. if (*state != HT_AGG_STATE_OPERATIONAL) {
  456. ret = -ENOENT;
  457. goto stop_BA_exit;
  458. }
  459. #ifdef CONFIG_MAC80211_HT_DEBUG
  460. printk(KERN_DEBUG "Tx BA session stop requested for %s tid %u\n",
  461. print_mac(mac, ra), tid);
  462. #endif /* CONFIG_MAC80211_HT_DEBUG */
  463. ieee80211_stop_queue(hw, sta->tid_to_tx_q[tid]);
  464. *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
  465. (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
  466. if (local->ops->ampdu_action)
  467. ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_STOP,
  468. &sta->sta, tid, NULL);
  469. /* case HW denied going back to legacy */
  470. if (ret) {
  471. WARN_ON(ret != -EBUSY);
  472. *state = HT_AGG_STATE_OPERATIONAL;
  473. ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
  474. goto stop_BA_exit;
  475. }
  476. stop_BA_exit:
  477. spin_unlock_bh(&sta->lock);
  478. rcu_read_unlock();
  479. return ret;
  480. }
  481. EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
  482. void ieee80211_start_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u16 tid)
  483. {
  484. struct ieee80211_local *local = hw_to_local(hw);
  485. struct sta_info *sta;
  486. u8 *state;
  487. DECLARE_MAC_BUF(mac);
  488. if (tid >= STA_TID_NUM) {
  489. #ifdef CONFIG_MAC80211_HT_DEBUG
  490. printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
  491. tid, STA_TID_NUM);
  492. #endif
  493. return;
  494. }
  495. rcu_read_lock();
  496. sta = sta_info_get(local, ra);
  497. if (!sta) {
  498. rcu_read_unlock();
  499. #ifdef CONFIG_MAC80211_HT_DEBUG
  500. printk(KERN_DEBUG "Could not find station: %s\n",
  501. print_mac(mac, ra));
  502. #endif
  503. return;
  504. }
  505. state = &sta->ampdu_mlme.tid_state_tx[tid];
  506. spin_lock_bh(&sta->lock);
  507. if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
  508. #ifdef CONFIG_MAC80211_HT_DEBUG
  509. printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
  510. *state);
  511. #endif
  512. spin_unlock_bh(&sta->lock);
  513. rcu_read_unlock();
  514. return;
  515. }
  516. WARN_ON_ONCE(*state & HT_ADDBA_DRV_READY_MSK);
  517. *state |= HT_ADDBA_DRV_READY_MSK;
  518. if (*state == HT_AGG_STATE_OPERATIONAL) {
  519. #ifdef CONFIG_MAC80211_HT_DEBUG
  520. printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
  521. #endif
  522. ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
  523. }
  524. spin_unlock_bh(&sta->lock);
  525. rcu_read_unlock();
  526. }
  527. EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
  528. void ieee80211_stop_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u8 tid)
  529. {
  530. struct ieee80211_local *local = hw_to_local(hw);
  531. struct sta_info *sta;
  532. u8 *state;
  533. int agg_queue;
  534. DECLARE_MAC_BUF(mac);
  535. if (tid >= STA_TID_NUM) {
  536. #ifdef CONFIG_MAC80211_HT_DEBUG
  537. printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
  538. tid, STA_TID_NUM);
  539. #endif
  540. return;
  541. }
  542. #ifdef CONFIG_MAC80211_HT_DEBUG
  543. printk(KERN_DEBUG "Stopping Tx BA session for %s tid %d\n",
  544. print_mac(mac, ra), tid);
  545. #endif /* CONFIG_MAC80211_HT_DEBUG */
  546. rcu_read_lock();
  547. sta = sta_info_get(local, ra);
  548. if (!sta) {
  549. #ifdef CONFIG_MAC80211_HT_DEBUG
  550. printk(KERN_DEBUG "Could not find station: %s\n",
  551. print_mac(mac, ra));
  552. #endif
  553. rcu_read_unlock();
  554. return;
  555. }
  556. state = &sta->ampdu_mlme.tid_state_tx[tid];
  557. /* NOTE: no need to use sta->lock in this state check, as
  558. * ieee80211_stop_tx_ba_session will let only one stop call to
  559. * pass through per sta/tid
  560. */
  561. if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
  562. #ifdef CONFIG_MAC80211_HT_DEBUG
  563. printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
  564. #endif
  565. rcu_read_unlock();
  566. return;
  567. }
  568. if (*state & HT_AGG_STATE_INITIATOR_MSK)
  569. ieee80211_send_delba(sta->sdata, ra, tid,
  570. WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
  571. agg_queue = sta->tid_to_tx_q[tid];
  572. ieee80211_ht_agg_queue_remove(local, sta, tid, 1);
  573. /* We just requeued the all the frames that were in the
  574. * removed queue, and since we might miss a softirq we do
  575. * netif_schedule_queue. ieee80211_wake_queue is not used
  576. * here as this queue is not necessarily stopped
  577. */
  578. netif_schedule_queue(netdev_get_tx_queue(local->mdev, agg_queue));
  579. spin_lock_bh(&sta->lock);
  580. *state = HT_AGG_STATE_IDLE;
  581. sta->ampdu_mlme.addba_req_num[tid] = 0;
  582. kfree(sta->ampdu_mlme.tid_tx[tid]);
  583. sta->ampdu_mlme.tid_tx[tid] = NULL;
  584. spin_unlock_bh(&sta->lock);
  585. rcu_read_unlock();
  586. }
  587. EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
  588. void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
  589. const u8 *ra, u16 tid)
  590. {
  591. struct ieee80211_local *local = hw_to_local(hw);
  592. struct ieee80211_ra_tid *ra_tid;
  593. struct sk_buff *skb = dev_alloc_skb(0);
  594. if (unlikely(!skb)) {
  595. #ifdef CONFIG_MAC80211_HT_DEBUG
  596. if (net_ratelimit())
  597. printk(KERN_WARNING "%s: Not enough memory, "
  598. "dropping start BA session", skb->dev->name);
  599. #endif
  600. return;
  601. }
  602. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  603. memcpy(&ra_tid->ra, ra, ETH_ALEN);
  604. ra_tid->tid = tid;
  605. skb->pkt_type = IEEE80211_ADDBA_MSG;
  606. skb_queue_tail(&local->skb_queue, skb);
  607. tasklet_schedule(&local->tasklet);
  608. }
  609. EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
  610. void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
  611. const u8 *ra, u16 tid)
  612. {
  613. struct ieee80211_local *local = hw_to_local(hw);
  614. struct ieee80211_ra_tid *ra_tid;
  615. struct sk_buff *skb = dev_alloc_skb(0);
  616. if (unlikely(!skb)) {
  617. #ifdef CONFIG_MAC80211_HT_DEBUG
  618. if (net_ratelimit())
  619. printk(KERN_WARNING "%s: Not enough memory, "
  620. "dropping stop BA session", skb->dev->name);
  621. #endif
  622. return;
  623. }
  624. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  625. memcpy(&ra_tid->ra, ra, ETH_ALEN);
  626. ra_tid->tid = tid;
  627. skb->pkt_type = IEEE80211_DELBA_MSG;
  628. skb_queue_tail(&local->skb_queue, skb);
  629. tasklet_schedule(&local->tasklet);
  630. }
  631. EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
  632. /*
  633. * After accepting the AddBA Request we activated a timer,
  634. * resetting it after each frame that arrives from the originator.
  635. * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
  636. */
  637. static void sta_rx_agg_session_timer_expired(unsigned long data)
  638. {
  639. /* not an elegant detour, but there is no choice as the timer passes
  640. * only one argument, and various sta_info are needed here, so init
  641. * flow in sta_info_create gives the TID as data, while the timer_to_id
  642. * array gives the sta through container_of */
  643. u8 *ptid = (u8 *)data;
  644. u8 *timer_to_id = ptid - *ptid;
  645. struct sta_info *sta = container_of(timer_to_id, struct sta_info,
  646. timer_to_tid[0]);
  647. #ifdef CONFIG_MAC80211_HT_DEBUG
  648. printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
  649. #endif
  650. ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
  651. (u16)*ptid, WLAN_BACK_TIMER,
  652. WLAN_REASON_QSTA_TIMEOUT);
  653. }
  654. void ieee80211_process_addba_request(struct ieee80211_local *local,
  655. struct sta_info *sta,
  656. struct ieee80211_mgmt *mgmt,
  657. size_t len)
  658. {
  659. struct ieee80211_hw *hw = &local->hw;
  660. struct ieee80211_conf *conf = &hw->conf;
  661. struct tid_ampdu_rx *tid_agg_rx;
  662. u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
  663. u8 dialog_token;
  664. int ret = -EOPNOTSUPP;
  665. DECLARE_MAC_BUF(mac);
  666. /* extract session parameters from addba request frame */
  667. dialog_token = mgmt->u.action.u.addba_req.dialog_token;
  668. timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
  669. start_seq_num =
  670. le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
  671. capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
  672. ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
  673. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  674. buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
  675. status = WLAN_STATUS_REQUEST_DECLINED;
  676. /* sanity check for incoming parameters:
  677. * check if configuration can support the BA policy
  678. * and if buffer size does not exceeds max value */
  679. if (((ba_policy != 1)
  680. && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
  681. || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
  682. status = WLAN_STATUS_INVALID_QOS_PARAM;
  683. #ifdef CONFIG_MAC80211_HT_DEBUG
  684. if (net_ratelimit())
  685. printk(KERN_DEBUG "AddBA Req with bad params from "
  686. "%s on tid %u. policy %d, buffer size %d\n",
  687. print_mac(mac, mgmt->sa), tid, ba_policy,
  688. buf_size);
  689. #endif /* CONFIG_MAC80211_HT_DEBUG */
  690. goto end_no_lock;
  691. }
  692. /* determine default buffer size */
  693. if (buf_size == 0) {
  694. struct ieee80211_supported_band *sband;
  695. sband = local->hw.wiphy->bands[conf->channel->band];
  696. buf_size = IEEE80211_MIN_AMPDU_BUF;
  697. buf_size = buf_size << sband->ht_info.ampdu_factor;
  698. }
  699. /* examine state machine */
  700. spin_lock_bh(&sta->lock);
  701. if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
  702. #ifdef CONFIG_MAC80211_HT_DEBUG
  703. if (net_ratelimit())
  704. printk(KERN_DEBUG "unexpected AddBA Req from "
  705. "%s on tid %u\n",
  706. print_mac(mac, mgmt->sa), tid);
  707. #endif /* CONFIG_MAC80211_HT_DEBUG */
  708. goto end;
  709. }
  710. /* prepare A-MPDU MLME for Rx aggregation */
  711. sta->ampdu_mlme.tid_rx[tid] =
  712. kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
  713. if (!sta->ampdu_mlme.tid_rx[tid]) {
  714. #ifdef CONFIG_MAC80211_HT_DEBUG
  715. if (net_ratelimit())
  716. printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
  717. tid);
  718. #endif
  719. goto end;
  720. }
  721. /* rx timer */
  722. sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
  723. sta_rx_agg_session_timer_expired;
  724. sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
  725. (unsigned long)&sta->timer_to_tid[tid];
  726. init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
  727. tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
  728. /* prepare reordering buffer */
  729. tid_agg_rx->reorder_buf =
  730. kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
  731. if (!tid_agg_rx->reorder_buf) {
  732. #ifdef CONFIG_MAC80211_HT_DEBUG
  733. if (net_ratelimit())
  734. printk(KERN_ERR "can not allocate reordering buffer "
  735. "to tid %d\n", tid);
  736. #endif
  737. kfree(sta->ampdu_mlme.tid_rx[tid]);
  738. goto end;
  739. }
  740. memset(tid_agg_rx->reorder_buf, 0,
  741. buf_size * sizeof(struct sk_buff *));
  742. if (local->ops->ampdu_action)
  743. ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
  744. &sta->sta, tid, &start_seq_num);
  745. #ifdef CONFIG_MAC80211_HT_DEBUG
  746. printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
  747. #endif /* CONFIG_MAC80211_HT_DEBUG */
  748. if (ret) {
  749. kfree(tid_agg_rx->reorder_buf);
  750. kfree(tid_agg_rx);
  751. sta->ampdu_mlme.tid_rx[tid] = NULL;
  752. goto end;
  753. }
  754. /* change state and send addba resp */
  755. sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
  756. tid_agg_rx->dialog_token = dialog_token;
  757. tid_agg_rx->ssn = start_seq_num;
  758. tid_agg_rx->head_seq_num = start_seq_num;
  759. tid_agg_rx->buf_size = buf_size;
  760. tid_agg_rx->timeout = timeout;
  761. tid_agg_rx->stored_mpdu_num = 0;
  762. status = WLAN_STATUS_SUCCESS;
  763. end:
  764. spin_unlock_bh(&sta->lock);
  765. end_no_lock:
  766. ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
  767. dialog_token, status, 1, buf_size, timeout);
  768. }
  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 ieee80211_hw *hw = &local->hw;
  775. u16 capab;
  776. u16 tid;
  777. u8 *state;
  778. capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
  779. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  780. state = &sta->ampdu_mlme.tid_state_tx[tid];
  781. spin_lock_bh(&sta->lock);
  782. if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
  783. spin_unlock_bh(&sta->lock);
  784. return;
  785. }
  786. if (mgmt->u.action.u.addba_resp.dialog_token !=
  787. sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
  788. spin_unlock_bh(&sta->lock);
  789. #ifdef CONFIG_MAC80211_HT_DEBUG
  790. printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
  791. #endif /* CONFIG_MAC80211_HT_DEBUG */
  792. return;
  793. }
  794. del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
  795. #ifdef CONFIG_MAC80211_HT_DEBUG
  796. printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
  797. #endif /* CONFIG_MAC80211_HT_DEBUG */
  798. if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
  799. == WLAN_STATUS_SUCCESS) {
  800. *state |= HT_ADDBA_RECEIVED_MSK;
  801. sta->ampdu_mlme.addba_req_num[tid] = 0;
  802. if (*state == HT_AGG_STATE_OPERATIONAL)
  803. ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
  804. spin_unlock_bh(&sta->lock);
  805. } else {
  806. sta->ampdu_mlme.addba_req_num[tid]++;
  807. /* this will allow the state check in stop_BA_session */
  808. *state = HT_AGG_STATE_OPERATIONAL;
  809. spin_unlock_bh(&sta->lock);
  810. ieee80211_stop_tx_ba_session(hw, sta->sta.addr, tid,
  811. WLAN_BACK_INITIATOR);
  812. }
  813. }
  814. void ieee80211_process_delba(struct ieee80211_sub_if_data *sdata,
  815. struct sta_info *sta,
  816. struct ieee80211_mgmt *mgmt, size_t len)
  817. {
  818. struct ieee80211_local *local = sdata->local;
  819. u16 tid, params;
  820. u16 initiator;
  821. DECLARE_MAC_BUF(mac);
  822. params = le16_to_cpu(mgmt->u.action.u.delba.params);
  823. tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
  824. initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
  825. #ifdef CONFIG_MAC80211_HT_DEBUG
  826. if (net_ratelimit())
  827. printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
  828. print_mac(mac, mgmt->sa),
  829. initiator ? "initiator" : "recipient", tid,
  830. mgmt->u.action.u.delba.reason_code);
  831. #endif /* CONFIG_MAC80211_HT_DEBUG */
  832. if (initiator == WLAN_BACK_INITIATOR)
  833. ieee80211_sta_stop_rx_ba_session(sdata, sta->sta.addr, tid,
  834. WLAN_BACK_INITIATOR, 0);
  835. else { /* WLAN_BACK_RECIPIENT */
  836. spin_lock_bh(&sta->lock);
  837. sta->ampdu_mlme.tid_state_tx[tid] =
  838. HT_AGG_STATE_OPERATIONAL;
  839. spin_unlock_bh(&sta->lock);
  840. ieee80211_stop_tx_ba_session(&local->hw, sta->sta.addr, tid,
  841. WLAN_BACK_RECIPIENT);
  842. }
  843. }