11n_rxreorder.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * Marvell Wireless LAN device driver: 802.11n RX Re-ordering
  3. *
  4. * Copyright (C) 2011-2014, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include "decl.h"
  20. #include "ioctl.h"
  21. #include "util.h"
  22. #include "fw.h"
  23. #include "main.h"
  24. #include "wmm.h"
  25. #include "11n.h"
  26. #include "11n_rxreorder.h"
  27. /* This function will dispatch amsdu packet and forward it to kernel/upper
  28. * layer.
  29. */
  30. static int mwifiex_11n_dispatch_amsdu_pkt(struct mwifiex_private *priv,
  31. struct sk_buff *skb)
  32. {
  33. struct rxpd *local_rx_pd = (struct rxpd *)(skb->data);
  34. int ret;
  35. if (le16_to_cpu(local_rx_pd->rx_pkt_type) == PKT_TYPE_AMSDU) {
  36. struct sk_buff_head list;
  37. struct sk_buff *rx_skb;
  38. __skb_queue_head_init(&list);
  39. skb_pull(skb, le16_to_cpu(local_rx_pd->rx_pkt_offset));
  40. skb_trim(skb, le16_to_cpu(local_rx_pd->rx_pkt_length));
  41. ieee80211_amsdu_to_8023s(skb, &list, priv->curr_addr,
  42. priv->wdev.iftype, 0, false);
  43. while (!skb_queue_empty(&list)) {
  44. rx_skb = __skb_dequeue(&list);
  45. ret = mwifiex_recv_packet(priv, rx_skb);
  46. if (ret == -1)
  47. mwifiex_dbg(priv->adapter, ERROR,
  48. "Rx of A-MSDU failed");
  49. }
  50. return 0;
  51. }
  52. return -1;
  53. }
  54. /* This function will process the rx packet and forward it to kernel/upper
  55. * layer.
  56. */
  57. static int mwifiex_11n_dispatch_pkt(struct mwifiex_private *priv, void *payload)
  58. {
  59. int ret = mwifiex_11n_dispatch_amsdu_pkt(priv, payload);
  60. if (!ret)
  61. return 0;
  62. if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
  63. return mwifiex_handle_uap_rx_forward(priv, payload);
  64. return mwifiex_process_rx_packet(priv, payload);
  65. }
  66. /*
  67. * This function dispatches all packets in the Rx reorder table until the
  68. * start window.
  69. *
  70. * There could be holes in the buffer, which are skipped by the function.
  71. * Since the buffer is linear, the function uses rotation to simulate
  72. * circular buffer.
  73. */
  74. static void
  75. mwifiex_11n_dispatch_pkt_until_start_win(struct mwifiex_private *priv,
  76. struct mwifiex_rx_reorder_tbl *tbl,
  77. int start_win)
  78. {
  79. int pkt_to_send, i;
  80. void *rx_tmp_ptr;
  81. unsigned long flags;
  82. pkt_to_send = (start_win > tbl->start_win) ?
  83. min((start_win - tbl->start_win), tbl->win_size) :
  84. tbl->win_size;
  85. for (i = 0; i < pkt_to_send; ++i) {
  86. spin_lock_irqsave(&priv->rx_pkt_lock, flags);
  87. rx_tmp_ptr = NULL;
  88. if (tbl->rx_reorder_ptr[i]) {
  89. rx_tmp_ptr = tbl->rx_reorder_ptr[i];
  90. tbl->rx_reorder_ptr[i] = NULL;
  91. }
  92. spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
  93. if (rx_tmp_ptr)
  94. mwifiex_11n_dispatch_pkt(priv, rx_tmp_ptr);
  95. }
  96. spin_lock_irqsave(&priv->rx_pkt_lock, flags);
  97. /*
  98. * We don't have a circular buffer, hence use rotation to simulate
  99. * circular buffer
  100. */
  101. for (i = 0; i < tbl->win_size - pkt_to_send; ++i) {
  102. tbl->rx_reorder_ptr[i] = tbl->rx_reorder_ptr[pkt_to_send + i];
  103. tbl->rx_reorder_ptr[pkt_to_send + i] = NULL;
  104. }
  105. tbl->start_win = start_win;
  106. spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
  107. }
  108. /*
  109. * This function dispatches all packets in the Rx reorder table until
  110. * a hole is found.
  111. *
  112. * The start window is adjusted automatically when a hole is located.
  113. * Since the buffer is linear, the function uses rotation to simulate
  114. * circular buffer.
  115. */
  116. static void
  117. mwifiex_11n_scan_and_dispatch(struct mwifiex_private *priv,
  118. struct mwifiex_rx_reorder_tbl *tbl)
  119. {
  120. int i, j, xchg;
  121. void *rx_tmp_ptr;
  122. unsigned long flags;
  123. for (i = 0; i < tbl->win_size; ++i) {
  124. spin_lock_irqsave(&priv->rx_pkt_lock, flags);
  125. if (!tbl->rx_reorder_ptr[i]) {
  126. spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
  127. break;
  128. }
  129. rx_tmp_ptr = tbl->rx_reorder_ptr[i];
  130. tbl->rx_reorder_ptr[i] = NULL;
  131. spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
  132. mwifiex_11n_dispatch_pkt(priv, rx_tmp_ptr);
  133. }
  134. spin_lock_irqsave(&priv->rx_pkt_lock, flags);
  135. /*
  136. * We don't have a circular buffer, hence use rotation to simulate
  137. * circular buffer
  138. */
  139. if (i > 0) {
  140. xchg = tbl->win_size - i;
  141. for (j = 0; j < xchg; ++j) {
  142. tbl->rx_reorder_ptr[j] = tbl->rx_reorder_ptr[i + j];
  143. tbl->rx_reorder_ptr[i + j] = NULL;
  144. }
  145. }
  146. tbl->start_win = (tbl->start_win + i) & (MAX_TID_VALUE - 1);
  147. spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
  148. }
  149. /*
  150. * This function deletes the Rx reorder table and frees the memory.
  151. *
  152. * The function stops the associated timer and dispatches all the
  153. * pending packets in the Rx reorder table before deletion.
  154. */
  155. static void
  156. mwifiex_del_rx_reorder_entry(struct mwifiex_private *priv,
  157. struct mwifiex_rx_reorder_tbl *tbl)
  158. {
  159. unsigned long flags;
  160. int start_win;
  161. if (!tbl)
  162. return;
  163. spin_lock_irqsave(&priv->adapter->rx_proc_lock, flags);
  164. priv->adapter->rx_locked = true;
  165. if (priv->adapter->rx_processing) {
  166. spin_unlock_irqrestore(&priv->adapter->rx_proc_lock, flags);
  167. flush_workqueue(priv->adapter->rx_workqueue);
  168. } else {
  169. spin_unlock_irqrestore(&priv->adapter->rx_proc_lock, flags);
  170. }
  171. start_win = (tbl->start_win + tbl->win_size) & (MAX_TID_VALUE - 1);
  172. mwifiex_11n_dispatch_pkt_until_start_win(priv, tbl, start_win);
  173. del_timer_sync(&tbl->timer_context.timer);
  174. tbl->timer_context.timer_is_set = false;
  175. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  176. list_del(&tbl->list);
  177. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
  178. kfree(tbl->rx_reorder_ptr);
  179. kfree(tbl);
  180. spin_lock_irqsave(&priv->adapter->rx_proc_lock, flags);
  181. priv->adapter->rx_locked = false;
  182. spin_unlock_irqrestore(&priv->adapter->rx_proc_lock, flags);
  183. }
  184. /*
  185. * This function returns the pointer to an entry in Rx reordering
  186. * table which matches the given TA/TID pair.
  187. */
  188. struct mwifiex_rx_reorder_tbl *
  189. mwifiex_11n_get_rx_reorder_tbl(struct mwifiex_private *priv, int tid, u8 *ta)
  190. {
  191. struct mwifiex_rx_reorder_tbl *tbl;
  192. unsigned long flags;
  193. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  194. list_for_each_entry(tbl, &priv->rx_reorder_tbl_ptr, list) {
  195. if (!memcmp(tbl->ta, ta, ETH_ALEN) && tbl->tid == tid) {
  196. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock,
  197. flags);
  198. return tbl;
  199. }
  200. }
  201. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
  202. return NULL;
  203. }
  204. /* This function retrieves the pointer to an entry in Rx reordering
  205. * table which matches the given TA and deletes it.
  206. */
  207. void mwifiex_11n_del_rx_reorder_tbl_by_ta(struct mwifiex_private *priv, u8 *ta)
  208. {
  209. struct mwifiex_rx_reorder_tbl *tbl, *tmp;
  210. unsigned long flags;
  211. if (!ta)
  212. return;
  213. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  214. list_for_each_entry_safe(tbl, tmp, &priv->rx_reorder_tbl_ptr, list) {
  215. if (!memcmp(tbl->ta, ta, ETH_ALEN)) {
  216. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock,
  217. flags);
  218. mwifiex_del_rx_reorder_entry(priv, tbl);
  219. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  220. }
  221. }
  222. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
  223. return;
  224. }
  225. /*
  226. * This function finds the last sequence number used in the packets
  227. * buffered in Rx reordering table.
  228. */
  229. static int
  230. mwifiex_11n_find_last_seq_num(struct reorder_tmr_cnxt *ctx)
  231. {
  232. struct mwifiex_rx_reorder_tbl *rx_reorder_tbl_ptr = ctx->ptr;
  233. struct mwifiex_private *priv = ctx->priv;
  234. unsigned long flags;
  235. int i;
  236. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  237. for (i = rx_reorder_tbl_ptr->win_size - 1; i >= 0; --i) {
  238. if (rx_reorder_tbl_ptr->rx_reorder_ptr[i]) {
  239. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock,
  240. flags);
  241. return i;
  242. }
  243. }
  244. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
  245. return -1;
  246. }
  247. /*
  248. * This function flushes all the packets in Rx reordering table.
  249. *
  250. * The function checks if any packets are currently buffered in the
  251. * table or not. In case there are packets available, it dispatches
  252. * them and then dumps the Rx reordering table.
  253. */
  254. static void
  255. mwifiex_flush_data(unsigned long context)
  256. {
  257. struct reorder_tmr_cnxt *ctx =
  258. (struct reorder_tmr_cnxt *) context;
  259. int start_win, seq_num;
  260. ctx->timer_is_set = false;
  261. seq_num = mwifiex_11n_find_last_seq_num(ctx);
  262. if (seq_num < 0)
  263. return;
  264. mwifiex_dbg(ctx->priv->adapter, INFO, "info: flush data %d\n", seq_num);
  265. start_win = (ctx->ptr->start_win + seq_num + 1) & (MAX_TID_VALUE - 1);
  266. mwifiex_11n_dispatch_pkt_until_start_win(ctx->priv, ctx->ptr,
  267. start_win);
  268. }
  269. /*
  270. * This function creates an entry in Rx reordering table for the
  271. * given TA/TID.
  272. *
  273. * The function also initializes the entry with sequence number, window
  274. * size as well as initializes the timer.
  275. *
  276. * If the received TA/TID pair is already present, all the packets are
  277. * dispatched and the window size is moved until the SSN.
  278. */
  279. static void
  280. mwifiex_11n_create_rx_reorder_tbl(struct mwifiex_private *priv, u8 *ta,
  281. int tid, int win_size, int seq_num)
  282. {
  283. int i;
  284. struct mwifiex_rx_reorder_tbl *tbl, *new_node;
  285. u16 last_seq = 0;
  286. unsigned long flags;
  287. struct mwifiex_sta_node *node;
  288. /*
  289. * If we get a TID, ta pair which is already present dispatch all the
  290. * the packets and move the window size until the ssn
  291. */
  292. tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid, ta);
  293. if (tbl) {
  294. mwifiex_11n_dispatch_pkt_until_start_win(priv, tbl, seq_num);
  295. return;
  296. }
  297. /* if !tbl then create one */
  298. new_node = kzalloc(sizeof(struct mwifiex_rx_reorder_tbl), GFP_KERNEL);
  299. if (!new_node)
  300. return;
  301. INIT_LIST_HEAD(&new_node->list);
  302. new_node->tid = tid;
  303. memcpy(new_node->ta, ta, ETH_ALEN);
  304. new_node->start_win = seq_num;
  305. new_node->init_win = seq_num;
  306. new_node->flags = 0;
  307. spin_lock_irqsave(&priv->sta_list_spinlock, flags);
  308. if (mwifiex_queuing_ra_based(priv)) {
  309. if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP) {
  310. node = mwifiex_get_sta_entry(priv, ta);
  311. if (node)
  312. last_seq = node->rx_seq[tid];
  313. }
  314. } else {
  315. node = mwifiex_get_sta_entry(priv, ta);
  316. if (node)
  317. last_seq = node->rx_seq[tid];
  318. else
  319. last_seq = priv->rx_seq[tid];
  320. }
  321. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  322. mwifiex_dbg(priv->adapter, INFO,
  323. "info: last_seq=%d start_win=%d\n",
  324. last_seq, new_node->start_win);
  325. if (last_seq != MWIFIEX_DEF_11N_RX_SEQ_NUM &&
  326. last_seq >= new_node->start_win) {
  327. new_node->start_win = last_seq + 1;
  328. new_node->flags |= RXREOR_INIT_WINDOW_SHIFT;
  329. }
  330. new_node->win_size = win_size;
  331. new_node->rx_reorder_ptr = kzalloc(sizeof(void *) * win_size,
  332. GFP_KERNEL);
  333. if (!new_node->rx_reorder_ptr) {
  334. kfree((u8 *) new_node);
  335. mwifiex_dbg(priv->adapter, ERROR,
  336. "%s: failed to alloc reorder_ptr\n", __func__);
  337. return;
  338. }
  339. new_node->timer_context.ptr = new_node;
  340. new_node->timer_context.priv = priv;
  341. new_node->timer_context.timer_is_set = false;
  342. setup_timer(&new_node->timer_context.timer, mwifiex_flush_data,
  343. (unsigned long)&new_node->timer_context);
  344. for (i = 0; i < win_size; ++i)
  345. new_node->rx_reorder_ptr[i] = NULL;
  346. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  347. list_add_tail(&new_node->list, &priv->rx_reorder_tbl_ptr);
  348. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
  349. }
  350. static void
  351. mwifiex_11n_rxreorder_timer_restart(struct mwifiex_rx_reorder_tbl *tbl)
  352. {
  353. u32 min_flush_time;
  354. if (tbl->win_size >= MWIFIEX_BA_WIN_SIZE_32)
  355. min_flush_time = MIN_FLUSH_TIMER_15_MS;
  356. else
  357. min_flush_time = MIN_FLUSH_TIMER_MS;
  358. mod_timer(&tbl->timer_context.timer,
  359. jiffies + msecs_to_jiffies(min_flush_time * tbl->win_size));
  360. tbl->timer_context.timer_is_set = true;
  361. }
  362. /*
  363. * This function prepares command for adding a BA request.
  364. *
  365. * Preparation includes -
  366. * - Setting command ID and proper size
  367. * - Setting add BA request buffer
  368. * - Ensuring correct endian-ness
  369. */
  370. int mwifiex_cmd_11n_addba_req(struct host_cmd_ds_command *cmd, void *data_buf)
  371. {
  372. struct host_cmd_ds_11n_addba_req *add_ba_req = &cmd->params.add_ba_req;
  373. cmd->command = cpu_to_le16(HostCmd_CMD_11N_ADDBA_REQ);
  374. cmd->size = cpu_to_le16(sizeof(*add_ba_req) + S_DS_GEN);
  375. memcpy(add_ba_req, data_buf, sizeof(*add_ba_req));
  376. return 0;
  377. }
  378. /*
  379. * This function prepares command for adding a BA response.
  380. *
  381. * Preparation includes -
  382. * - Setting command ID and proper size
  383. * - Setting add BA response buffer
  384. * - Ensuring correct endian-ness
  385. */
  386. int mwifiex_cmd_11n_addba_rsp_gen(struct mwifiex_private *priv,
  387. struct host_cmd_ds_command *cmd,
  388. struct host_cmd_ds_11n_addba_req
  389. *cmd_addba_req)
  390. {
  391. struct host_cmd_ds_11n_addba_rsp *add_ba_rsp = &cmd->params.add_ba_rsp;
  392. struct mwifiex_sta_node *sta_ptr;
  393. u32 rx_win_size = priv->add_ba_param.rx_win_size;
  394. u8 tid;
  395. int win_size;
  396. unsigned long flags;
  397. uint16_t block_ack_param_set;
  398. if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
  399. ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
  400. priv->adapter->is_hw_11ac_capable &&
  401. memcmp(priv->cfg_bssid, cmd_addba_req->peer_mac_addr, ETH_ALEN)) {
  402. spin_lock_irqsave(&priv->sta_list_spinlock, flags);
  403. sta_ptr = mwifiex_get_sta_entry(priv,
  404. cmd_addba_req->peer_mac_addr);
  405. if (!sta_ptr) {
  406. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  407. mwifiex_dbg(priv->adapter, ERROR,
  408. "BA setup with unknown TDLS peer %pM!\n",
  409. cmd_addba_req->peer_mac_addr);
  410. return -1;
  411. }
  412. if (sta_ptr->is_11ac_enabled)
  413. rx_win_size = MWIFIEX_11AC_STA_AMPDU_DEF_RXWINSIZE;
  414. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  415. }
  416. cmd->command = cpu_to_le16(HostCmd_CMD_11N_ADDBA_RSP);
  417. cmd->size = cpu_to_le16(sizeof(*add_ba_rsp) + S_DS_GEN);
  418. memcpy(add_ba_rsp->peer_mac_addr, cmd_addba_req->peer_mac_addr,
  419. ETH_ALEN);
  420. add_ba_rsp->dialog_token = cmd_addba_req->dialog_token;
  421. add_ba_rsp->block_ack_tmo = cmd_addba_req->block_ack_tmo;
  422. add_ba_rsp->ssn = cmd_addba_req->ssn;
  423. block_ack_param_set = le16_to_cpu(cmd_addba_req->block_ack_param_set);
  424. tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
  425. >> BLOCKACKPARAM_TID_POS;
  426. add_ba_rsp->status_code = cpu_to_le16(ADDBA_RSP_STATUS_ACCEPT);
  427. block_ack_param_set &= ~IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK;
  428. /* If we don't support AMSDU inside AMPDU, reset the bit */
  429. if (!priv->add_ba_param.rx_amsdu ||
  430. (priv->aggr_prio_tbl[tid].amsdu == BA_STREAM_NOT_ALLOWED))
  431. block_ack_param_set &= ~BLOCKACKPARAM_AMSDU_SUPP_MASK;
  432. block_ack_param_set |= rx_win_size << BLOCKACKPARAM_WINSIZE_POS;
  433. add_ba_rsp->block_ack_param_set = cpu_to_le16(block_ack_param_set);
  434. win_size = (le16_to_cpu(add_ba_rsp->block_ack_param_set)
  435. & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK)
  436. >> BLOCKACKPARAM_WINSIZE_POS;
  437. cmd_addba_req->block_ack_param_set = cpu_to_le16(block_ack_param_set);
  438. mwifiex_11n_create_rx_reorder_tbl(priv, cmd_addba_req->peer_mac_addr,
  439. tid, win_size,
  440. le16_to_cpu(cmd_addba_req->ssn));
  441. return 0;
  442. }
  443. /*
  444. * This function prepares command for deleting a BA request.
  445. *
  446. * Preparation includes -
  447. * - Setting command ID and proper size
  448. * - Setting del BA request buffer
  449. * - Ensuring correct endian-ness
  450. */
  451. int mwifiex_cmd_11n_delba(struct host_cmd_ds_command *cmd, void *data_buf)
  452. {
  453. struct host_cmd_ds_11n_delba *del_ba = &cmd->params.del_ba;
  454. cmd->command = cpu_to_le16(HostCmd_CMD_11N_DELBA);
  455. cmd->size = cpu_to_le16(sizeof(*del_ba) + S_DS_GEN);
  456. memcpy(del_ba, data_buf, sizeof(*del_ba));
  457. return 0;
  458. }
  459. /*
  460. * This function identifies if Rx reordering is needed for a received packet.
  461. *
  462. * In case reordering is required, the function will do the reordering
  463. * before sending it to kernel.
  464. *
  465. * The Rx reorder table is checked first with the received TID/TA pair. If
  466. * not found, the received packet is dispatched immediately. But if found,
  467. * the packet is reordered and all the packets in the updated Rx reordering
  468. * table is dispatched until a hole is found.
  469. *
  470. * For sequence number less than the starting window, the packet is dropped.
  471. */
  472. int mwifiex_11n_rx_reorder_pkt(struct mwifiex_private *priv,
  473. u16 seq_num, u16 tid,
  474. u8 *ta, u8 pkt_type, void *payload)
  475. {
  476. struct mwifiex_rx_reorder_tbl *tbl;
  477. int prev_start_win, start_win, end_win, win_size;
  478. u16 pkt_index;
  479. bool init_window_shift = false;
  480. int ret = 0;
  481. tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid, ta);
  482. if (!tbl) {
  483. if (pkt_type != PKT_TYPE_BAR)
  484. mwifiex_11n_dispatch_pkt(priv, payload);
  485. return ret;
  486. }
  487. if ((pkt_type == PKT_TYPE_AMSDU) && !tbl->amsdu) {
  488. mwifiex_11n_dispatch_pkt(priv, payload);
  489. return ret;
  490. }
  491. start_win = tbl->start_win;
  492. prev_start_win = start_win;
  493. win_size = tbl->win_size;
  494. end_win = ((start_win + win_size) - 1) & (MAX_TID_VALUE - 1);
  495. if (tbl->flags & RXREOR_INIT_WINDOW_SHIFT) {
  496. init_window_shift = true;
  497. tbl->flags &= ~RXREOR_INIT_WINDOW_SHIFT;
  498. }
  499. if (tbl->flags & RXREOR_FORCE_NO_DROP) {
  500. mwifiex_dbg(priv->adapter, INFO,
  501. "RXREOR_FORCE_NO_DROP when HS is activated\n");
  502. tbl->flags &= ~RXREOR_FORCE_NO_DROP;
  503. } else if (init_window_shift && seq_num < start_win &&
  504. seq_num >= tbl->init_win) {
  505. mwifiex_dbg(priv->adapter, INFO,
  506. "Sender TID sequence number reset %d->%d for SSN %d\n",
  507. start_win, seq_num, tbl->init_win);
  508. tbl->start_win = start_win = seq_num;
  509. end_win = ((start_win + win_size) - 1) & (MAX_TID_VALUE - 1);
  510. } else {
  511. /*
  512. * If seq_num is less then starting win then ignore and drop
  513. * the packet
  514. */
  515. if ((start_win + TWOPOW11) > (MAX_TID_VALUE - 1)) {
  516. if (seq_num >= ((start_win + TWOPOW11) &
  517. (MAX_TID_VALUE - 1)) &&
  518. seq_num < start_win) {
  519. ret = -1;
  520. goto done;
  521. }
  522. } else if ((seq_num < start_win) ||
  523. (seq_num >= (start_win + TWOPOW11))) {
  524. ret = -1;
  525. goto done;
  526. }
  527. }
  528. /*
  529. * If this packet is a BAR we adjust seq_num as
  530. * WinStart = seq_num
  531. */
  532. if (pkt_type == PKT_TYPE_BAR)
  533. seq_num = ((seq_num + win_size) - 1) & (MAX_TID_VALUE - 1);
  534. if (((end_win < start_win) &&
  535. (seq_num < start_win) && (seq_num > end_win)) ||
  536. ((end_win > start_win) && ((seq_num > end_win) ||
  537. (seq_num < start_win)))) {
  538. end_win = seq_num;
  539. if (((seq_num - win_size) + 1) >= 0)
  540. start_win = (end_win - win_size) + 1;
  541. else
  542. start_win = (MAX_TID_VALUE - (win_size - seq_num)) + 1;
  543. mwifiex_11n_dispatch_pkt_until_start_win(priv, tbl, start_win);
  544. }
  545. if (pkt_type != PKT_TYPE_BAR) {
  546. if (seq_num >= start_win)
  547. pkt_index = seq_num - start_win;
  548. else
  549. pkt_index = (seq_num+MAX_TID_VALUE) - start_win;
  550. if (tbl->rx_reorder_ptr[pkt_index]) {
  551. ret = -1;
  552. goto done;
  553. }
  554. tbl->rx_reorder_ptr[pkt_index] = payload;
  555. }
  556. /*
  557. * Dispatch all packets sequentially from start_win until a
  558. * hole is found and adjust the start_win appropriately
  559. */
  560. mwifiex_11n_scan_and_dispatch(priv, tbl);
  561. done:
  562. if (!tbl->timer_context.timer_is_set ||
  563. prev_start_win != tbl->start_win)
  564. mwifiex_11n_rxreorder_timer_restart(tbl);
  565. return ret;
  566. }
  567. /*
  568. * This function deletes an entry for a given TID/TA pair.
  569. *
  570. * The TID/TA are taken from del BA event body.
  571. */
  572. void
  573. mwifiex_del_ba_tbl(struct mwifiex_private *priv, int tid, u8 *peer_mac,
  574. u8 type, int initiator)
  575. {
  576. struct mwifiex_rx_reorder_tbl *tbl;
  577. struct mwifiex_tx_ba_stream_tbl *ptx_tbl;
  578. struct mwifiex_ra_list_tbl *ra_list;
  579. u8 cleanup_rx_reorder_tbl;
  580. unsigned long flags;
  581. int tid_down;
  582. if (type == TYPE_DELBA_RECEIVE)
  583. cleanup_rx_reorder_tbl = (initiator) ? true : false;
  584. else
  585. cleanup_rx_reorder_tbl = (initiator) ? false : true;
  586. mwifiex_dbg(priv->adapter, EVENT, "event: DELBA: %pM tid=%d initiator=%d\n",
  587. peer_mac, tid, initiator);
  588. if (cleanup_rx_reorder_tbl) {
  589. tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid,
  590. peer_mac);
  591. if (!tbl) {
  592. mwifiex_dbg(priv->adapter, EVENT,
  593. "event: TID, TA not found in table\n");
  594. return;
  595. }
  596. mwifiex_del_rx_reorder_entry(priv, tbl);
  597. } else {
  598. ptx_tbl = mwifiex_get_ba_tbl(priv, tid, peer_mac);
  599. if (!ptx_tbl) {
  600. mwifiex_dbg(priv->adapter, EVENT,
  601. "event: TID, RA not found in table\n");
  602. return;
  603. }
  604. tid_down = mwifiex_wmm_downgrade_tid(priv, tid);
  605. ra_list = mwifiex_wmm_get_ralist_node(priv, tid_down, peer_mac);
  606. if (ra_list) {
  607. ra_list->amsdu_in_ampdu = false;
  608. ra_list->ba_status = BA_SETUP_NONE;
  609. }
  610. spin_lock_irqsave(&priv->tx_ba_stream_tbl_lock, flags);
  611. mwifiex_11n_delete_tx_ba_stream_tbl_entry(priv, ptx_tbl);
  612. spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock, flags);
  613. }
  614. }
  615. /*
  616. * This function handles the command response of an add BA response.
  617. *
  618. * Handling includes changing the header fields into CPU format and
  619. * creating the stream, provided the add BA is accepted.
  620. */
  621. int mwifiex_ret_11n_addba_resp(struct mwifiex_private *priv,
  622. struct host_cmd_ds_command *resp)
  623. {
  624. struct host_cmd_ds_11n_addba_rsp *add_ba_rsp = &resp->params.add_ba_rsp;
  625. int tid, win_size;
  626. struct mwifiex_rx_reorder_tbl *tbl;
  627. uint16_t block_ack_param_set;
  628. block_ack_param_set = le16_to_cpu(add_ba_rsp->block_ack_param_set);
  629. tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
  630. >> BLOCKACKPARAM_TID_POS;
  631. /*
  632. * Check if we had rejected the ADDBA, if yes then do not create
  633. * the stream
  634. */
  635. if (le16_to_cpu(add_ba_rsp->status_code) != BA_RESULT_SUCCESS) {
  636. mwifiex_dbg(priv->adapter, ERROR, "ADDBA RSP: failed %pM tid=%d)\n",
  637. add_ba_rsp->peer_mac_addr, tid);
  638. tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid,
  639. add_ba_rsp->peer_mac_addr);
  640. if (tbl)
  641. mwifiex_del_rx_reorder_entry(priv, tbl);
  642. return 0;
  643. }
  644. win_size = (block_ack_param_set & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK)
  645. >> BLOCKACKPARAM_WINSIZE_POS;
  646. tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid,
  647. add_ba_rsp->peer_mac_addr);
  648. if (tbl) {
  649. if ((block_ack_param_set & BLOCKACKPARAM_AMSDU_SUPP_MASK) &&
  650. priv->add_ba_param.rx_amsdu &&
  651. (priv->aggr_prio_tbl[tid].amsdu != BA_STREAM_NOT_ALLOWED))
  652. tbl->amsdu = true;
  653. else
  654. tbl->amsdu = false;
  655. }
  656. mwifiex_dbg(priv->adapter, CMD,
  657. "cmd: ADDBA RSP: %pM tid=%d ssn=%d win_size=%d\n",
  658. add_ba_rsp->peer_mac_addr, tid, add_ba_rsp->ssn, win_size);
  659. return 0;
  660. }
  661. /*
  662. * This function handles BA stream timeout event by preparing and sending
  663. * a command to the firmware.
  664. */
  665. void mwifiex_11n_ba_stream_timeout(struct mwifiex_private *priv,
  666. struct host_cmd_ds_11n_batimeout *event)
  667. {
  668. struct host_cmd_ds_11n_delba delba;
  669. memset(&delba, 0, sizeof(struct host_cmd_ds_11n_delba));
  670. memcpy(delba.peer_mac_addr, event->peer_mac_addr, ETH_ALEN);
  671. delba.del_ba_param_set |=
  672. cpu_to_le16((u16) event->tid << DELBA_TID_POS);
  673. delba.del_ba_param_set |= cpu_to_le16(
  674. (u16) event->origninator << DELBA_INITIATOR_POS);
  675. delba.reason_code = cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
  676. mwifiex_send_cmd(priv, HostCmd_CMD_11N_DELBA, 0, 0, &delba, false);
  677. }
  678. /*
  679. * This function cleans up the Rx reorder table by deleting all the entries
  680. * and re-initializing.
  681. */
  682. void mwifiex_11n_cleanup_reorder_tbl(struct mwifiex_private *priv)
  683. {
  684. struct mwifiex_rx_reorder_tbl *del_tbl_ptr, *tmp_node;
  685. unsigned long flags;
  686. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  687. list_for_each_entry_safe(del_tbl_ptr, tmp_node,
  688. &priv->rx_reorder_tbl_ptr, list) {
  689. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
  690. mwifiex_del_rx_reorder_entry(priv, del_tbl_ptr);
  691. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  692. }
  693. INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
  694. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
  695. mwifiex_reset_11n_rx_seq_num(priv);
  696. }
  697. /*
  698. * This function updates all rx_reorder_tbl's flags.
  699. */
  700. void mwifiex_update_rxreor_flags(struct mwifiex_adapter *adapter, u8 flags)
  701. {
  702. struct mwifiex_private *priv;
  703. struct mwifiex_rx_reorder_tbl *tbl;
  704. unsigned long lock_flags;
  705. int i;
  706. for (i = 0; i < adapter->priv_num; i++) {
  707. priv = adapter->priv[i];
  708. if (!priv)
  709. continue;
  710. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, lock_flags);
  711. if (list_empty(&priv->rx_reorder_tbl_ptr)) {
  712. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock,
  713. lock_flags);
  714. continue;
  715. }
  716. list_for_each_entry(tbl, &priv->rx_reorder_tbl_ptr, list)
  717. tbl->flags = flags;
  718. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, lock_flags);
  719. }
  720. return;
  721. }
  722. /* This function update all the rx_win_size based on coex flag
  723. */
  724. static void mwifiex_update_ampdu_rxwinsize(struct mwifiex_adapter *adapter,
  725. bool coex_flag)
  726. {
  727. u8 i;
  728. u32 rx_win_size;
  729. struct mwifiex_private *priv;
  730. dev_dbg(adapter->dev, "Update rxwinsize %d\n", coex_flag);
  731. for (i = 0; i < adapter->priv_num; i++) {
  732. if (!adapter->priv[i])
  733. continue;
  734. priv = adapter->priv[i];
  735. rx_win_size = priv->add_ba_param.rx_win_size;
  736. if (coex_flag) {
  737. if (priv->bss_type == MWIFIEX_BSS_TYPE_STA)
  738. priv->add_ba_param.rx_win_size =
  739. MWIFIEX_STA_COEX_AMPDU_DEF_RXWINSIZE;
  740. if (priv->bss_type == MWIFIEX_BSS_TYPE_P2P)
  741. priv->add_ba_param.rx_win_size =
  742. MWIFIEX_STA_COEX_AMPDU_DEF_RXWINSIZE;
  743. if (priv->bss_type == MWIFIEX_BSS_TYPE_UAP)
  744. priv->add_ba_param.rx_win_size =
  745. MWIFIEX_UAP_COEX_AMPDU_DEF_RXWINSIZE;
  746. } else {
  747. if (priv->bss_type == MWIFIEX_BSS_TYPE_STA)
  748. priv->add_ba_param.rx_win_size =
  749. MWIFIEX_STA_AMPDU_DEF_RXWINSIZE;
  750. if (priv->bss_type == MWIFIEX_BSS_TYPE_P2P)
  751. priv->add_ba_param.rx_win_size =
  752. MWIFIEX_STA_AMPDU_DEF_RXWINSIZE;
  753. if (priv->bss_type == MWIFIEX_BSS_TYPE_UAP)
  754. priv->add_ba_param.rx_win_size =
  755. MWIFIEX_UAP_AMPDU_DEF_RXWINSIZE;
  756. }
  757. if (adapter->coex_win_size && adapter->coex_rx_win_size)
  758. priv->add_ba_param.rx_win_size =
  759. adapter->coex_rx_win_size;
  760. if (rx_win_size != priv->add_ba_param.rx_win_size) {
  761. if (!priv->media_connected)
  762. continue;
  763. for (i = 0; i < MAX_NUM_TID; i++)
  764. mwifiex_11n_delba(priv, i);
  765. }
  766. }
  767. }
  768. /* This function check coex for RX BA
  769. */
  770. void mwifiex_coex_ampdu_rxwinsize(struct mwifiex_adapter *adapter)
  771. {
  772. u8 i;
  773. struct mwifiex_private *priv;
  774. u8 count = 0;
  775. for (i = 0; i < adapter->priv_num; i++) {
  776. if (adapter->priv[i]) {
  777. priv = adapter->priv[i];
  778. if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) {
  779. if (priv->media_connected)
  780. count++;
  781. }
  782. if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
  783. if (priv->bss_started)
  784. count++;
  785. }
  786. }
  787. if (count >= MWIFIEX_BSS_COEX_COUNT)
  788. break;
  789. }
  790. if (count >= MWIFIEX_BSS_COEX_COUNT)
  791. mwifiex_update_ampdu_rxwinsize(adapter, true);
  792. else
  793. mwifiex_update_ampdu_rxwinsize(adapter, false);
  794. }