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