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