11n_rxreorder.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. dev_err(priv->adapter->dev,
  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. dev_dbg(ctx->priv->adapter->dev, "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. dev_dbg(priv->adapter->dev,
  310. "info: AP/ADHOC:last_seq=%d start_win=%d\n",
  311. last_seq, new_node->start_win);
  312. if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP) {
  313. node = mwifiex_get_sta_entry(priv, ta);
  314. if (node)
  315. last_seq = node->rx_seq[tid];
  316. }
  317. } else {
  318. node = mwifiex_get_sta_entry(priv, ta);
  319. if (node)
  320. last_seq = node->rx_seq[tid];
  321. else
  322. last_seq = priv->rx_seq[tid];
  323. }
  324. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  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. dev_err(priv->adapter->dev,
  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. init_timer(&new_node->timer_context.timer);
  343. new_node->timer_context.timer.function = mwifiex_flush_data;
  344. new_node->timer_context.timer.data =
  345. (unsigned long) &new_node->timer_context;
  346. for (i = 0; i < win_size; ++i)
  347. new_node->rx_reorder_ptr[i] = NULL;
  348. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  349. list_add_tail(&new_node->list, &priv->rx_reorder_tbl_ptr);
  350. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
  351. }
  352. static void
  353. mwifiex_11n_rxreorder_timer_restart(struct mwifiex_rx_reorder_tbl *tbl)
  354. {
  355. u32 min_flush_time;
  356. if (tbl->win_size >= MWIFIEX_BA_WIN_SIZE_32)
  357. min_flush_time = MIN_FLUSH_TIMER_15_MS;
  358. else
  359. min_flush_time = MIN_FLUSH_TIMER_MS;
  360. mod_timer(&tbl->timer_context.timer,
  361. jiffies + msecs_to_jiffies(min_flush_time * tbl->win_size));
  362. tbl->timer_context.timer_is_set = true;
  363. }
  364. /*
  365. * This function prepares command for adding a BA request.
  366. *
  367. * Preparation includes -
  368. * - Setting command ID and proper size
  369. * - Setting add BA request buffer
  370. * - Ensuring correct endian-ness
  371. */
  372. int mwifiex_cmd_11n_addba_req(struct host_cmd_ds_command *cmd, void *data_buf)
  373. {
  374. struct host_cmd_ds_11n_addba_req *add_ba_req = &cmd->params.add_ba_req;
  375. cmd->command = cpu_to_le16(HostCmd_CMD_11N_ADDBA_REQ);
  376. cmd->size = cpu_to_le16(sizeof(*add_ba_req) + S_DS_GEN);
  377. memcpy(add_ba_req, data_buf, sizeof(*add_ba_req));
  378. return 0;
  379. }
  380. /*
  381. * This function prepares command for adding a BA response.
  382. *
  383. * Preparation includes -
  384. * - Setting command ID and proper size
  385. * - Setting add BA response buffer
  386. * - Ensuring correct endian-ness
  387. */
  388. int mwifiex_cmd_11n_addba_rsp_gen(struct mwifiex_private *priv,
  389. struct host_cmd_ds_command *cmd,
  390. struct host_cmd_ds_11n_addba_req
  391. *cmd_addba_req)
  392. {
  393. struct host_cmd_ds_11n_addba_rsp *add_ba_rsp = &cmd->params.add_ba_rsp;
  394. struct mwifiex_sta_node *sta_ptr;
  395. u32 rx_win_size = priv->add_ba_param.rx_win_size;
  396. u8 tid;
  397. int win_size;
  398. unsigned long flags;
  399. uint16_t block_ack_param_set;
  400. if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
  401. ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
  402. priv->adapter->is_hw_11ac_capable &&
  403. memcmp(priv->cfg_bssid, cmd_addba_req->peer_mac_addr, ETH_ALEN)) {
  404. spin_lock_irqsave(&priv->sta_list_spinlock, flags);
  405. sta_ptr = mwifiex_get_sta_entry(priv,
  406. cmd_addba_req->peer_mac_addr);
  407. if (!sta_ptr) {
  408. dev_warn(priv->adapter->dev,
  409. "BA setup with unknown TDLS peer %pM!\n",
  410. cmd_addba_req->peer_mac_addr);
  411. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  412. return -1;
  413. }
  414. if (sta_ptr->is_11ac_enabled)
  415. rx_win_size = MWIFIEX_11AC_STA_AMPDU_DEF_RXWINSIZE;
  416. spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
  417. }
  418. cmd->command = cpu_to_le16(HostCmd_CMD_11N_ADDBA_RSP);
  419. cmd->size = cpu_to_le16(sizeof(*add_ba_rsp) + S_DS_GEN);
  420. memcpy(add_ba_rsp->peer_mac_addr, cmd_addba_req->peer_mac_addr,
  421. ETH_ALEN);
  422. add_ba_rsp->dialog_token = cmd_addba_req->dialog_token;
  423. add_ba_rsp->block_ack_tmo = cmd_addba_req->block_ack_tmo;
  424. add_ba_rsp->ssn = cmd_addba_req->ssn;
  425. block_ack_param_set = le16_to_cpu(cmd_addba_req->block_ack_param_set);
  426. tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
  427. >> BLOCKACKPARAM_TID_POS;
  428. add_ba_rsp->status_code = cpu_to_le16(ADDBA_RSP_STATUS_ACCEPT);
  429. block_ack_param_set &= ~IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK;
  430. /* If we don't support AMSDU inside AMPDU, reset the bit */
  431. if (!priv->add_ba_param.rx_amsdu ||
  432. (priv->aggr_prio_tbl[tid].amsdu == BA_STREAM_NOT_ALLOWED))
  433. block_ack_param_set &= ~BLOCKACKPARAM_AMSDU_SUPP_MASK;
  434. block_ack_param_set |= rx_win_size << BLOCKACKPARAM_WINSIZE_POS;
  435. add_ba_rsp->block_ack_param_set = cpu_to_le16(block_ack_param_set);
  436. win_size = (le16_to_cpu(add_ba_rsp->block_ack_param_set)
  437. & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK)
  438. >> BLOCKACKPARAM_WINSIZE_POS;
  439. cmd_addba_req->block_ack_param_set = cpu_to_le16(block_ack_param_set);
  440. mwifiex_11n_create_rx_reorder_tbl(priv, cmd_addba_req->peer_mac_addr,
  441. tid, win_size,
  442. le16_to_cpu(cmd_addba_req->ssn));
  443. return 0;
  444. }
  445. /*
  446. * This function prepares command for deleting a BA request.
  447. *
  448. * Preparation includes -
  449. * - Setting command ID and proper size
  450. * - Setting del BA request buffer
  451. * - Ensuring correct endian-ness
  452. */
  453. int mwifiex_cmd_11n_delba(struct host_cmd_ds_command *cmd, void *data_buf)
  454. {
  455. struct host_cmd_ds_11n_delba *del_ba = &cmd->params.del_ba;
  456. cmd->command = cpu_to_le16(HostCmd_CMD_11N_DELBA);
  457. cmd->size = cpu_to_le16(sizeof(*del_ba) + S_DS_GEN);
  458. memcpy(del_ba, data_buf, sizeof(*del_ba));
  459. return 0;
  460. }
  461. /*
  462. * This function identifies if Rx reordering is needed for a received packet.
  463. *
  464. * In case reordering is required, the function will do the reordering
  465. * before sending it to kernel.
  466. *
  467. * The Rx reorder table is checked first with the received TID/TA pair. If
  468. * not found, the received packet is dispatched immediately. But if found,
  469. * the packet is reordered and all the packets in the updated Rx reordering
  470. * table is dispatched until a hole is found.
  471. *
  472. * For sequence number less than the starting window, the packet is dropped.
  473. */
  474. int mwifiex_11n_rx_reorder_pkt(struct mwifiex_private *priv,
  475. u16 seq_num, u16 tid,
  476. u8 *ta, u8 pkt_type, void *payload)
  477. {
  478. struct mwifiex_rx_reorder_tbl *tbl;
  479. int prev_start_win, start_win, end_win, win_size;
  480. u16 pkt_index;
  481. bool init_window_shift = false;
  482. int ret = 0;
  483. tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid, ta);
  484. if (!tbl) {
  485. if (pkt_type != PKT_TYPE_BAR)
  486. mwifiex_11n_dispatch_pkt(priv, payload);
  487. return ret;
  488. }
  489. if ((pkt_type == PKT_TYPE_AMSDU) && !tbl->amsdu) {
  490. mwifiex_11n_dispatch_pkt(priv, payload);
  491. return ret;
  492. }
  493. start_win = tbl->start_win;
  494. prev_start_win = start_win;
  495. win_size = tbl->win_size;
  496. end_win = ((start_win + win_size) - 1) & (MAX_TID_VALUE - 1);
  497. if (tbl->flags & RXREOR_INIT_WINDOW_SHIFT) {
  498. init_window_shift = true;
  499. tbl->flags &= ~RXREOR_INIT_WINDOW_SHIFT;
  500. }
  501. if (tbl->flags & RXREOR_FORCE_NO_DROP) {
  502. dev_dbg(priv->adapter->dev,
  503. "RXREOR_FORCE_NO_DROP when HS is activated\n");
  504. tbl->flags &= ~RXREOR_FORCE_NO_DROP;
  505. } else if (init_window_shift && seq_num < start_win &&
  506. seq_num >= tbl->init_win) {
  507. dev_dbg(priv->adapter->dev,
  508. "Sender TID sequence number reset %d->%d for SSN %d\n",
  509. start_win, seq_num, tbl->init_win);
  510. tbl->start_win = start_win = seq_num;
  511. end_win = ((start_win + win_size) - 1) & (MAX_TID_VALUE - 1);
  512. } else {
  513. /*
  514. * If seq_num is less then starting win then ignore and drop
  515. * the packet
  516. */
  517. if ((start_win + TWOPOW11) > (MAX_TID_VALUE - 1)) {
  518. if (seq_num >= ((start_win + TWOPOW11) &
  519. (MAX_TID_VALUE - 1)) &&
  520. seq_num < start_win) {
  521. ret = -1;
  522. goto done;
  523. }
  524. } else if ((seq_num < start_win) ||
  525. (seq_num >= (start_win + TWOPOW11))) {
  526. ret = -1;
  527. goto done;
  528. }
  529. }
  530. /*
  531. * If this packet is a BAR we adjust seq_num as
  532. * WinStart = seq_num
  533. */
  534. if (pkt_type == PKT_TYPE_BAR)
  535. seq_num = ((seq_num + win_size) - 1) & (MAX_TID_VALUE - 1);
  536. if (((end_win < start_win) &&
  537. (seq_num < start_win) && (seq_num > end_win)) ||
  538. ((end_win > start_win) && ((seq_num > end_win) ||
  539. (seq_num < start_win)))) {
  540. end_win = seq_num;
  541. if (((seq_num - win_size) + 1) >= 0)
  542. start_win = (end_win - win_size) + 1;
  543. else
  544. start_win = (MAX_TID_VALUE - (win_size - seq_num)) + 1;
  545. mwifiex_11n_dispatch_pkt_until_start_win(priv, tbl, start_win);
  546. }
  547. if (pkt_type != PKT_TYPE_BAR) {
  548. if (seq_num >= start_win)
  549. pkt_index = seq_num - start_win;
  550. else
  551. pkt_index = (seq_num+MAX_TID_VALUE) - start_win;
  552. if (tbl->rx_reorder_ptr[pkt_index]) {
  553. ret = -1;
  554. goto done;
  555. }
  556. tbl->rx_reorder_ptr[pkt_index] = payload;
  557. }
  558. /*
  559. * Dispatch all packets sequentially from start_win until a
  560. * hole is found and adjust the start_win appropriately
  561. */
  562. mwifiex_11n_scan_and_dispatch(priv, tbl);
  563. done:
  564. if (!tbl->timer_context.timer_is_set ||
  565. prev_start_win != tbl->start_win)
  566. mwifiex_11n_rxreorder_timer_restart(tbl);
  567. return ret;
  568. }
  569. /*
  570. * This function deletes an entry for a given TID/TA pair.
  571. *
  572. * The TID/TA are taken from del BA event body.
  573. */
  574. void
  575. mwifiex_del_ba_tbl(struct mwifiex_private *priv, int tid, u8 *peer_mac,
  576. u8 type, int initiator)
  577. {
  578. struct mwifiex_rx_reorder_tbl *tbl;
  579. struct mwifiex_tx_ba_stream_tbl *ptx_tbl;
  580. u8 cleanup_rx_reorder_tbl;
  581. unsigned long flags;
  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. dev_dbg(priv->adapter->dev, "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. dev_dbg(priv->adapter->dev,
  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. dev_dbg(priv->adapter->dev,
  601. "event: TID, RA not found in table\n");
  602. return;
  603. }
  604. spin_lock_irqsave(&priv->tx_ba_stream_tbl_lock, flags);
  605. mwifiex_11n_delete_tx_ba_stream_tbl_entry(priv, ptx_tbl);
  606. spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock, flags);
  607. }
  608. }
  609. /*
  610. * This function handles the command response of an add BA response.
  611. *
  612. * Handling includes changing the header fields into CPU format and
  613. * creating the stream, provided the add BA is accepted.
  614. */
  615. int mwifiex_ret_11n_addba_resp(struct mwifiex_private *priv,
  616. struct host_cmd_ds_command *resp)
  617. {
  618. struct host_cmd_ds_11n_addba_rsp *add_ba_rsp = &resp->params.add_ba_rsp;
  619. int tid, win_size;
  620. struct mwifiex_rx_reorder_tbl *tbl;
  621. uint16_t block_ack_param_set;
  622. block_ack_param_set = le16_to_cpu(add_ba_rsp->block_ack_param_set);
  623. tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
  624. >> BLOCKACKPARAM_TID_POS;
  625. /*
  626. * Check if we had rejected the ADDBA, if yes then do not create
  627. * the stream
  628. */
  629. if (le16_to_cpu(add_ba_rsp->status_code) != BA_RESULT_SUCCESS) {
  630. dev_err(priv->adapter->dev, "ADDBA RSP: failed %pM tid=%d)\n",
  631. add_ba_rsp->peer_mac_addr, tid);
  632. tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid,
  633. add_ba_rsp->peer_mac_addr);
  634. if (tbl)
  635. mwifiex_del_rx_reorder_entry(priv, tbl);
  636. return 0;
  637. }
  638. win_size = (block_ack_param_set & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK)
  639. >> BLOCKACKPARAM_WINSIZE_POS;
  640. tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid,
  641. add_ba_rsp->peer_mac_addr);
  642. if (tbl) {
  643. if ((block_ack_param_set & BLOCKACKPARAM_AMSDU_SUPP_MASK) &&
  644. priv->add_ba_param.rx_amsdu &&
  645. (priv->aggr_prio_tbl[tid].amsdu != BA_STREAM_NOT_ALLOWED))
  646. tbl->amsdu = true;
  647. else
  648. tbl->amsdu = false;
  649. }
  650. dev_dbg(priv->adapter->dev,
  651. "cmd: ADDBA RSP: %pM tid=%d ssn=%d win_size=%d\n",
  652. add_ba_rsp->peer_mac_addr, tid, add_ba_rsp->ssn, win_size);
  653. return 0;
  654. }
  655. /*
  656. * This function handles BA stream timeout event by preparing and sending
  657. * a command to the firmware.
  658. */
  659. void mwifiex_11n_ba_stream_timeout(struct mwifiex_private *priv,
  660. struct host_cmd_ds_11n_batimeout *event)
  661. {
  662. struct host_cmd_ds_11n_delba delba;
  663. memset(&delba, 0, sizeof(struct host_cmd_ds_11n_delba));
  664. memcpy(delba.peer_mac_addr, event->peer_mac_addr, ETH_ALEN);
  665. delba.del_ba_param_set |=
  666. cpu_to_le16((u16) event->tid << DELBA_TID_POS);
  667. delba.del_ba_param_set |= cpu_to_le16(
  668. (u16) event->origninator << DELBA_INITIATOR_POS);
  669. delba.reason_code = cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
  670. mwifiex_send_cmd(priv, HostCmd_CMD_11N_DELBA, 0, 0, &delba, false);
  671. }
  672. /*
  673. * This function cleans up the Rx reorder table by deleting all the entries
  674. * and re-initializing.
  675. */
  676. void mwifiex_11n_cleanup_reorder_tbl(struct mwifiex_private *priv)
  677. {
  678. struct mwifiex_rx_reorder_tbl *del_tbl_ptr, *tmp_node;
  679. unsigned long flags;
  680. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  681. list_for_each_entry_safe(del_tbl_ptr, tmp_node,
  682. &priv->rx_reorder_tbl_ptr, list) {
  683. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
  684. mwifiex_del_rx_reorder_entry(priv, del_tbl_ptr);
  685. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
  686. }
  687. INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
  688. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
  689. mwifiex_reset_11n_rx_seq_num(priv);
  690. }
  691. /*
  692. * This function updates all rx_reorder_tbl's flags.
  693. */
  694. void mwifiex_update_rxreor_flags(struct mwifiex_adapter *adapter, u8 flags)
  695. {
  696. struct mwifiex_private *priv;
  697. struct mwifiex_rx_reorder_tbl *tbl;
  698. unsigned long lock_flags;
  699. int i;
  700. for (i = 0; i < adapter->priv_num; i++) {
  701. priv = adapter->priv[i];
  702. if (!priv)
  703. continue;
  704. spin_lock_irqsave(&priv->rx_reorder_tbl_lock, lock_flags);
  705. if (list_empty(&priv->rx_reorder_tbl_ptr)) {
  706. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock,
  707. lock_flags);
  708. continue;
  709. }
  710. list_for_each_entry(tbl, &priv->rx_reorder_tbl_ptr, list)
  711. tbl->flags = flags;
  712. spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, lock_flags);
  713. }
  714. return;
  715. }