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