1 /*
2  * Marvell Wireless LAN device driver: WMM
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 
28 
29 /* Maximum value FW can accept for driver delay in packet transmission */
30 #define DRV_PKT_DELAY_TO_FW_MAX   512
31 
32 
33 #define WMM_QUEUED_PACKET_LOWER_LIMIT   180
34 
35 #define WMM_QUEUED_PACKET_UPPER_LIMIT   200
36 
37 /* Offset for TOS field in the IP header */
38 #define IPTOS_OFFSET 5
39 
40 static bool disable_tx_amsdu;
41 module_param(disable_tx_amsdu, bool, 0644);
42 
43 /* WMM information IE */
44 static const u8 wmm_info_ie[] = { WLAN_EID_VENDOR_SPECIFIC, 0x07,
45 	0x00, 0x50, 0xf2, 0x02,
46 	0x00, 0x01, 0x00
47 };
48 
49 static const u8 wmm_aci_to_qidx_map[] = { WMM_AC_BE,
50 	WMM_AC_BK,
51 	WMM_AC_VI,
52 	WMM_AC_VO
53 };
54 
55 static u8 tos_to_tid[] = {
56 	/* TID DSCP_P2 DSCP_P1 DSCP_P0 WMM_AC */
57 	0x01,			/* 0 1 0 AC_BK */
58 	0x02,			/* 0 0 0 AC_BK */
59 	0x00,			/* 0 0 1 AC_BE */
60 	0x03,			/* 0 1 1 AC_BE */
61 	0x04,			/* 1 0 0 AC_VI */
62 	0x05,			/* 1 0 1 AC_VI */
63 	0x06,			/* 1 1 0 AC_VO */
64 	0x07			/* 1 1 1 AC_VO */
65 };
66 
67 static u8 ac_to_tid[4][2] = { {1, 2}, {0, 3}, {4, 5}, {6, 7} };
68 
69 /*
70  * This function debug prints the priority parameters for a WMM AC.
71  */
72 static void
73 mwifiex_wmm_ac_debug_print(const struct ieee_types_wmm_ac_parameters *ac_param)
74 {
75 	const char *ac_str[] = { "BK", "BE", "VI", "VO" };
76 
77 	pr_debug("info: WMM AC_%s: ACI=%d, ACM=%d, Aifsn=%d, "
78 		 "EcwMin=%d, EcwMax=%d, TxopLimit=%d\n",
79 		 ac_str[wmm_aci_to_qidx_map[(ac_param->aci_aifsn_bitmap
80 					     & MWIFIEX_ACI) >> 5]],
81 		 (ac_param->aci_aifsn_bitmap & MWIFIEX_ACI) >> 5,
82 		 (ac_param->aci_aifsn_bitmap & MWIFIEX_ACM) >> 4,
83 		 ac_param->aci_aifsn_bitmap & MWIFIEX_AIFSN,
84 		 ac_param->ecw_bitmap & MWIFIEX_ECW_MIN,
85 		 (ac_param->ecw_bitmap & MWIFIEX_ECW_MAX) >> 4,
86 		 le16_to_cpu(ac_param->tx_op_limit));
87 }
88 
89 /*
90  * This function allocates a route address list.
91  *
92  * The function also initializes the list with the provided RA.
93  */
94 static struct mwifiex_ra_list_tbl *
95 mwifiex_wmm_allocate_ralist_node(struct mwifiex_adapter *adapter, const u8 *ra)
96 {
97 	struct mwifiex_ra_list_tbl *ra_list;
98 
99 	ra_list = kzalloc(sizeof(struct mwifiex_ra_list_tbl), GFP_ATOMIC);
100 	if (!ra_list)
101 		return NULL;
102 
103 	INIT_LIST_HEAD(&ra_list->list);
104 	skb_queue_head_init(&ra_list->skb_head);
105 
106 	memcpy(ra_list->ra, ra, ETH_ALEN);
107 
108 	ra_list->total_pkt_count = 0;
109 
110 	mwifiex_dbg(adapter, INFO, "info: allocated ra_list %p\n", ra_list);
111 
112 	return ra_list;
113 }
114 
115 /* This function returns random no between 16 and 32 to be used as threshold
116  * for no of packets after which BA setup is initiated.
117  */
118 static u8 mwifiex_get_random_ba_threshold(void)
119 {
120 	u64 ns;
121 	/* setup ba_packet_threshold here random number between
122 	 * [BA_SETUP_PACKET_OFFSET,
123 	 * BA_SETUP_PACKET_OFFSET+BA_SETUP_MAX_PACKET_THRESHOLD-1]
124 	 */
125 	ns = ktime_get_ns();
126 	ns += (ns >> 32) + (ns >> 16);
127 
128 	return ((u8)ns % BA_SETUP_MAX_PACKET_THRESHOLD) + BA_SETUP_PACKET_OFFSET;
129 }
130 
131 /*
132  * This function allocates and adds a RA list for all TIDs
133  * with the given RA.
134  */
135 void mwifiex_ralist_add(struct mwifiex_private *priv, const u8 *ra)
136 {
137 	int i;
138 	struct mwifiex_ra_list_tbl *ra_list;
139 	struct mwifiex_adapter *adapter = priv->adapter;
140 	struct mwifiex_sta_node *node;
141 	unsigned long flags;
142 
143 
144 	for (i = 0; i < MAX_NUM_TID; ++i) {
145 		ra_list = mwifiex_wmm_allocate_ralist_node(adapter, ra);
146 		mwifiex_dbg(adapter, INFO,
147 			    "info: created ra_list %p\n", ra_list);
148 
149 		if (!ra_list)
150 			break;
151 
152 		ra_list->is_11n_enabled = 0;
153 		ra_list->tdls_link = false;
154 		ra_list->ba_status = BA_SETUP_NONE;
155 		ra_list->amsdu_in_ampdu = false;
156 		if (!mwifiex_queuing_ra_based(priv)) {
157 			if (mwifiex_is_tdls_link_setup
158 				(mwifiex_get_tdls_link_status(priv, ra))) {
159 				ra_list->tdls_link = true;
160 				ra_list->is_11n_enabled =
161 					mwifiex_tdls_peer_11n_enabled(priv, ra);
162 			} else {
163 				ra_list->is_11n_enabled = IS_11N_ENABLED(priv);
164 			}
165 		} else {
166 			spin_lock_irqsave(&priv->sta_list_spinlock, flags);
167 			node = mwifiex_get_sta_entry(priv, ra);
168 			if (node)
169 				ra_list->tx_paused = node->tx_pause;
170 			ra_list->is_11n_enabled =
171 				      mwifiex_is_sta_11n_enabled(priv, node);
172 			if (ra_list->is_11n_enabled)
173 				ra_list->max_amsdu = node->max_amsdu;
174 			spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
175 		}
176 
177 		mwifiex_dbg(adapter, DATA, "data: ralist %p: is_11n_enabled=%d\n",
178 			    ra_list, ra_list->is_11n_enabled);
179 
180 		if (ra_list->is_11n_enabled) {
181 			ra_list->ba_pkt_count = 0;
182 			ra_list->ba_packet_thr =
183 					      mwifiex_get_random_ba_threshold();
184 		}
185 		list_add_tail(&ra_list->list,
186 			      &priv->wmm.tid_tbl_ptr[i].ra_list);
187 	}
188 }
189 
190 /*
191  * This function sets the WMM queue priorities to their default values.
192  */
193 static void mwifiex_wmm_default_queue_priorities(struct mwifiex_private *priv)
194 {
195 	/* Default queue priorities: VO->VI->BE->BK */
196 	priv->wmm.queue_priority[0] = WMM_AC_VO;
197 	priv->wmm.queue_priority[1] = WMM_AC_VI;
198 	priv->wmm.queue_priority[2] = WMM_AC_BE;
199 	priv->wmm.queue_priority[3] = WMM_AC_BK;
200 }
201 
202 /*
203  * This function map ACs to TIDs.
204  */
205 static void
206 mwifiex_wmm_queue_priorities_tid(struct mwifiex_private *priv)
207 {
208 	struct mwifiex_wmm_desc *wmm = &priv->wmm;
209 	u8 *queue_priority = wmm->queue_priority;
210 	int i;
211 
212 	for (i = 0; i < 4; ++i) {
213 		tos_to_tid[7 - (i * 2)] = ac_to_tid[queue_priority[i]][1];
214 		tos_to_tid[6 - (i * 2)] = ac_to_tid[queue_priority[i]][0];
215 	}
216 
217 	for (i = 0; i < MAX_NUM_TID; ++i)
218 		priv->tos_to_tid_inv[tos_to_tid[i]] = (u8)i;
219 
220 	atomic_set(&wmm->highest_queued_prio, HIGH_PRIO_TID);
221 }
222 
223 /*
224  * This function initializes WMM priority queues.
225  */
226 void
227 mwifiex_wmm_setup_queue_priorities(struct mwifiex_private *priv,
228 				   struct ieee_types_wmm_parameter *wmm_ie)
229 {
230 	u16 cw_min, avg_back_off, tmp[4];
231 	u32 i, j, num_ac;
232 	u8 ac_idx;
233 
234 	if (!wmm_ie || !priv->wmm_enabled) {
235 		/* WMM is not enabled, just set the defaults and return */
236 		mwifiex_wmm_default_queue_priorities(priv);
237 		return;
238 	}
239 
240 	mwifiex_dbg(priv->adapter, INFO,
241 		    "info: WMM Parameter IE: version=%d,\t"
242 		    "qos_info Parameter Set Count=%d, Reserved=%#x\n",
243 		    wmm_ie->vend_hdr.version, wmm_ie->qos_info_bitmap &
244 		    IEEE80211_WMM_IE_AP_QOSINFO_PARAM_SET_CNT_MASK,
245 		    wmm_ie->reserved);
246 
247 	for (num_ac = 0; num_ac < ARRAY_SIZE(wmm_ie->ac_params); num_ac++) {
248 		u8 ecw = wmm_ie->ac_params[num_ac].ecw_bitmap;
249 		u8 aci_aifsn = wmm_ie->ac_params[num_ac].aci_aifsn_bitmap;
250 		cw_min = (1 << (ecw & MWIFIEX_ECW_MIN)) - 1;
251 		avg_back_off = (cw_min >> 1) + (aci_aifsn & MWIFIEX_AIFSN);
252 
253 		ac_idx = wmm_aci_to_qidx_map[(aci_aifsn & MWIFIEX_ACI) >> 5];
254 		priv->wmm.queue_priority[ac_idx] = ac_idx;
255 		tmp[ac_idx] = avg_back_off;
256 
257 		mwifiex_dbg(priv->adapter, INFO,
258 			    "info: WMM: CWmax=%d CWmin=%d Avg Back-off=%d\n",
259 			    (1 << ((ecw & MWIFIEX_ECW_MAX) >> 4)) - 1,
260 			    cw_min, avg_back_off);
261 		mwifiex_wmm_ac_debug_print(&wmm_ie->ac_params[num_ac]);
262 	}
263 
264 	/* Bubble sort */
265 	for (i = 0; i < num_ac; i++) {
266 		for (j = 1; j < num_ac - i; j++) {
267 			if (tmp[j - 1] > tmp[j]) {
268 				swap(tmp[j - 1], tmp[j]);
269 				swap(priv->wmm.queue_priority[j - 1],
270 				     priv->wmm.queue_priority[j]);
271 			} else if (tmp[j - 1] == tmp[j]) {
272 				if (priv->wmm.queue_priority[j - 1]
273 				    < priv->wmm.queue_priority[j])
274 					swap(priv->wmm.queue_priority[j - 1],
275 					     priv->wmm.queue_priority[j]);
276 			}
277 		}
278 	}
279 
280 	mwifiex_wmm_queue_priorities_tid(priv);
281 }
282 
283 /*
284  * This function evaluates whether or not an AC is to be downgraded.
285  *
286  * In case the AC is not enabled, the highest AC is returned that is
287  * enabled and does not require admission control.
288  */
289 static enum mwifiex_wmm_ac_e
290 mwifiex_wmm_eval_downgrade_ac(struct mwifiex_private *priv,
291 			      enum mwifiex_wmm_ac_e eval_ac)
292 {
293 	int down_ac;
294 	enum mwifiex_wmm_ac_e ret_ac;
295 	struct mwifiex_wmm_ac_status *ac_status;
296 
297 	ac_status = &priv->wmm.ac_status[eval_ac];
298 
299 	if (!ac_status->disabled)
300 		/* Okay to use this AC, its enabled */
301 		return eval_ac;
302 
303 	/* Setup a default return value of the lowest priority */
304 	ret_ac = WMM_AC_BK;
305 
306 	/*
307 	 *  Find the highest AC that is enabled and does not require
308 	 *  admission control. The spec disallows downgrading to an AC,
309 	 *  which is enabled due to a completed admission control.
310 	 *  Unadmitted traffic is not to be sent on an AC with admitted
311 	 *  traffic.
312 	 */
313 	for (down_ac = WMM_AC_BK; down_ac < eval_ac; down_ac++) {
314 		ac_status = &priv->wmm.ac_status[down_ac];
315 
316 		if (!ac_status->disabled && !ac_status->flow_required)
317 			/* AC is enabled and does not require admission
318 			   control */
319 			ret_ac = (enum mwifiex_wmm_ac_e) down_ac;
320 	}
321 
322 	return ret_ac;
323 }
324 
325 /*
326  * This function downgrades WMM priority queue.
327  */
328 void
329 mwifiex_wmm_setup_ac_downgrade(struct mwifiex_private *priv)
330 {
331 	int ac_val;
332 
333 	mwifiex_dbg(priv->adapter, INFO, "info: WMM: AC Priorities:\t"
334 		    "BK(0), BE(1), VI(2), VO(3)\n");
335 
336 	if (!priv->wmm_enabled) {
337 		/* WMM is not enabled, default priorities */
338 		for (ac_val = WMM_AC_BK; ac_val <= WMM_AC_VO; ac_val++)
339 			priv->wmm.ac_down_graded_vals[ac_val] =
340 						(enum mwifiex_wmm_ac_e) ac_val;
341 	} else {
342 		for (ac_val = WMM_AC_BK; ac_val <= WMM_AC_VO; ac_val++) {
343 			priv->wmm.ac_down_graded_vals[ac_val]
344 				= mwifiex_wmm_eval_downgrade_ac(priv,
345 						(enum mwifiex_wmm_ac_e) ac_val);
346 			mwifiex_dbg(priv->adapter, INFO,
347 				    "info: WMM: AC PRIO %d maps to %d\n",
348 				    ac_val,
349 				    priv->wmm.ac_down_graded_vals[ac_val]);
350 		}
351 	}
352 }
353 
354 /*
355  * This function converts the IP TOS field to an WMM AC
356  * Queue assignment.
357  */
358 static enum mwifiex_wmm_ac_e
359 mwifiex_wmm_convert_tos_to_ac(struct mwifiex_adapter *adapter, u32 tos)
360 {
361 	/* Map of TOS UP values to WMM AC */
362 	const enum mwifiex_wmm_ac_e tos_to_ac[] = { WMM_AC_BE,
363 		WMM_AC_BK,
364 		WMM_AC_BK,
365 		WMM_AC_BE,
366 		WMM_AC_VI,
367 		WMM_AC_VI,
368 		WMM_AC_VO,
369 		WMM_AC_VO
370 	};
371 
372 	if (tos >= ARRAY_SIZE(tos_to_ac))
373 		return WMM_AC_BE;
374 
375 	return tos_to_ac[tos];
376 }
377 
378 /*
379  * This function evaluates a given TID and downgrades it to a lower
380  * TID if the WMM Parameter IE received from the AP indicates that the
381  * AP is disabled (due to call admission control (ACM bit). Mapping
382  * of TID to AC is taken care of internally.
383  */
384 u8 mwifiex_wmm_downgrade_tid(struct mwifiex_private *priv, u32 tid)
385 {
386 	enum mwifiex_wmm_ac_e ac, ac_down;
387 	u8 new_tid;
388 
389 	ac = mwifiex_wmm_convert_tos_to_ac(priv->adapter, tid);
390 	ac_down = priv->wmm.ac_down_graded_vals[ac];
391 
392 	/* Send the index to tid array, picking from the array will be
393 	 * taken care by dequeuing function
394 	 */
395 	new_tid = ac_to_tid[ac_down][tid % 2];
396 
397 	return new_tid;
398 }
399 
400 /*
401  * This function initializes the WMM state information and the
402  * WMM data path queues.
403  */
404 void
405 mwifiex_wmm_init(struct mwifiex_adapter *adapter)
406 {
407 	int i, j;
408 	struct mwifiex_private *priv;
409 
410 	for (j = 0; j < adapter->priv_num; ++j) {
411 		priv = adapter->priv[j];
412 		if (!priv)
413 			continue;
414 
415 		for (i = 0; i < MAX_NUM_TID; ++i) {
416 			if (!disable_tx_amsdu &&
417 			    adapter->tx_buf_size > MWIFIEX_TX_DATA_BUF_SIZE_2K)
418 				priv->aggr_prio_tbl[i].amsdu =
419 							priv->tos_to_tid_inv[i];
420 			else
421 				priv->aggr_prio_tbl[i].amsdu =
422 							BA_STREAM_NOT_ALLOWED;
423 			priv->aggr_prio_tbl[i].ampdu_ap =
424 							priv->tos_to_tid_inv[i];
425 			priv->aggr_prio_tbl[i].ampdu_user =
426 							priv->tos_to_tid_inv[i];
427 		}
428 
429 		priv->aggr_prio_tbl[6].amsdu
430 					= priv->aggr_prio_tbl[6].ampdu_ap
431 					= priv->aggr_prio_tbl[6].ampdu_user
432 					= BA_STREAM_NOT_ALLOWED;
433 
434 		priv->aggr_prio_tbl[7].amsdu = priv->aggr_prio_tbl[7].ampdu_ap
435 					= priv->aggr_prio_tbl[7].ampdu_user
436 					= BA_STREAM_NOT_ALLOWED;
437 
438 		mwifiex_set_ba_params(priv);
439 		mwifiex_reset_11n_rx_seq_num(priv);
440 
441 		priv->wmm.drv_pkt_delay_max = MWIFIEX_WMM_DRV_DELAY_MAX;
442 		atomic_set(&priv->wmm.tx_pkts_queued, 0);
443 		atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
444 	}
445 }
446 
447 int mwifiex_bypass_txlist_empty(struct mwifiex_adapter *adapter)
448 {
449 	struct mwifiex_private *priv;
450 	int i;
451 
452 	for (i = 0; i < adapter->priv_num; i++) {
453 		priv = adapter->priv[i];
454 		if (!priv)
455 			continue;
456 		if (adapter->if_ops.is_port_ready &&
457 		    !adapter->if_ops.is_port_ready(priv))
458 			continue;
459 		if (!skb_queue_empty(&priv->bypass_txq))
460 			return false;
461 	}
462 
463 	return true;
464 }
465 
466 /*
467  * This function checks if WMM Tx queue is empty.
468  */
469 int
470 mwifiex_wmm_lists_empty(struct mwifiex_adapter *adapter)
471 {
472 	int i;
473 	struct mwifiex_private *priv;
474 
475 	for (i = 0; i < adapter->priv_num; ++i) {
476 		priv = adapter->priv[i];
477 		if (!priv)
478 			continue;
479 		if (!priv->port_open &&
480 		    (priv->bss_mode != NL80211_IFTYPE_ADHOC))
481 			continue;
482 		if (adapter->if_ops.is_port_ready &&
483 		    !adapter->if_ops.is_port_ready(priv))
484 			continue;
485 		if (atomic_read(&priv->wmm.tx_pkts_queued))
486 			return false;
487 	}
488 
489 	return true;
490 }
491 
492 /*
493  * This function deletes all packets in an RA list node.
494  *
495  * The packet sent completion callback handler are called with
496  * status failure, after they are dequeued to ensure proper
497  * cleanup. The RA list node itself is freed at the end.
498  */
499 static void
500 mwifiex_wmm_del_pkts_in_ralist_node(struct mwifiex_private *priv,
501 				    struct mwifiex_ra_list_tbl *ra_list)
502 {
503 	struct mwifiex_adapter *adapter = priv->adapter;
504 	struct sk_buff *skb, *tmp;
505 
506 	skb_queue_walk_safe(&ra_list->skb_head, skb, tmp) {
507 		skb_unlink(skb, &ra_list->skb_head);
508 		mwifiex_write_data_complete(adapter, skb, 0, -1);
509 	}
510 }
511 
512 /*
513  * This function deletes all packets in an RA list.
514  *
515  * Each nodes in the RA list are freed individually first, and then
516  * the RA list itself is freed.
517  */
518 static void
519 mwifiex_wmm_del_pkts_in_ralist(struct mwifiex_private *priv,
520 			       struct list_head *ra_list_head)
521 {
522 	struct mwifiex_ra_list_tbl *ra_list;
523 
524 	list_for_each_entry(ra_list, ra_list_head, list)
525 		mwifiex_wmm_del_pkts_in_ralist_node(priv, ra_list);
526 }
527 
528 /*
529  * This function deletes all packets in all RA lists.
530  */
531 static void mwifiex_wmm_cleanup_queues(struct mwifiex_private *priv)
532 {
533 	int i;
534 
535 	for (i = 0; i < MAX_NUM_TID; i++)
536 		mwifiex_wmm_del_pkts_in_ralist(priv, &priv->wmm.tid_tbl_ptr[i].
537 								       ra_list);
538 
539 	atomic_set(&priv->wmm.tx_pkts_queued, 0);
540 	atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
541 }
542 
543 /*
544  * This function deletes all route addresses from all RA lists.
545  */
546 static void mwifiex_wmm_delete_all_ralist(struct mwifiex_private *priv)
547 {
548 	struct mwifiex_ra_list_tbl *ra_list, *tmp_node;
549 	int i;
550 
551 	for (i = 0; i < MAX_NUM_TID; ++i) {
552 		mwifiex_dbg(priv->adapter, INFO,
553 			    "info: ra_list: freeing buf for tid %d\n", i);
554 		list_for_each_entry_safe(ra_list, tmp_node,
555 					 &priv->wmm.tid_tbl_ptr[i].ra_list,
556 					 list) {
557 			list_del(&ra_list->list);
558 			kfree(ra_list);
559 		}
560 
561 		INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[i].ra_list);
562 	}
563 }
564 
565 static int mwifiex_free_ack_frame(int id, void *p, void *data)
566 {
567 	pr_warn("Have pending ack frames!\n");
568 	kfree_skb(p);
569 	return 0;
570 }
571 
572 /*
573  * This function cleans up the Tx and Rx queues.
574  *
575  * Cleanup includes -
576  *      - All packets in RA lists
577  *      - All entries in Rx reorder table
578  *      - All entries in Tx BA stream table
579  *      - MPA buffer (if required)
580  *      - All RA lists
581  */
582 void
583 mwifiex_clean_txrx(struct mwifiex_private *priv)
584 {
585 	unsigned long flags;
586 	struct sk_buff *skb, *tmp;
587 
588 	mwifiex_11n_cleanup_reorder_tbl(priv);
589 	spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
590 
591 	mwifiex_wmm_cleanup_queues(priv);
592 	mwifiex_11n_delete_all_tx_ba_stream_tbl(priv);
593 
594 	if (priv->adapter->if_ops.cleanup_mpa_buf)
595 		priv->adapter->if_ops.cleanup_mpa_buf(priv->adapter);
596 
597 	mwifiex_wmm_delete_all_ralist(priv);
598 	memcpy(tos_to_tid, ac_to_tid, sizeof(tos_to_tid));
599 
600 	if (priv->adapter->if_ops.clean_pcie_ring &&
601 	    !priv->adapter->surprise_removed)
602 		priv->adapter->if_ops.clean_pcie_ring(priv->adapter);
603 	spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
604 
605 	skb_queue_walk_safe(&priv->tdls_txq, skb, tmp) {
606 		skb_unlink(skb, &priv->tdls_txq);
607 		mwifiex_write_data_complete(priv->adapter, skb, 0, -1);
608 	}
609 
610 	skb_queue_walk_safe(&priv->bypass_txq, skb, tmp) {
611 		skb_unlink(skb, &priv->bypass_txq);
612 		mwifiex_write_data_complete(priv->adapter, skb, 0, -1);
613 	}
614 	atomic_set(&priv->adapter->bypass_tx_pending, 0);
615 
616 	idr_for_each(&priv->ack_status_frames, mwifiex_free_ack_frame, NULL);
617 	idr_destroy(&priv->ack_status_frames);
618 }
619 
620 /*
621  * This function retrieves a particular RA list node, matching with the
622  * given TID and RA address.
623  */
624 struct mwifiex_ra_list_tbl *
625 mwifiex_wmm_get_ralist_node(struct mwifiex_private *priv, u8 tid,
626 			    const u8 *ra_addr)
627 {
628 	struct mwifiex_ra_list_tbl *ra_list;
629 
630 	list_for_each_entry(ra_list, &priv->wmm.tid_tbl_ptr[tid].ra_list,
631 			    list) {
632 		if (!memcmp(ra_list->ra, ra_addr, ETH_ALEN))
633 			return ra_list;
634 	}
635 
636 	return NULL;
637 }
638 
639 void mwifiex_update_ralist_tx_pause(struct mwifiex_private *priv, u8 *mac,
640 				    u8 tx_pause)
641 {
642 	struct mwifiex_ra_list_tbl *ra_list;
643 	u32 pkt_cnt = 0, tx_pkts_queued;
644 	unsigned long flags;
645 	int i;
646 
647 	spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
648 
649 	for (i = 0; i < MAX_NUM_TID; ++i) {
650 		ra_list = mwifiex_wmm_get_ralist_node(priv, i, mac);
651 		if (ra_list && ra_list->tx_paused != tx_pause) {
652 			pkt_cnt += ra_list->total_pkt_count;
653 			ra_list->tx_paused = tx_pause;
654 			if (tx_pause)
655 				priv->wmm.pkts_paused[i] +=
656 					ra_list->total_pkt_count;
657 			else
658 				priv->wmm.pkts_paused[i] -=
659 					ra_list->total_pkt_count;
660 		}
661 	}
662 
663 	if (pkt_cnt) {
664 		tx_pkts_queued = atomic_read(&priv->wmm.tx_pkts_queued);
665 		if (tx_pause)
666 			tx_pkts_queued -= pkt_cnt;
667 		else
668 			tx_pkts_queued += pkt_cnt;
669 
670 		atomic_set(&priv->wmm.tx_pkts_queued, tx_pkts_queued);
671 		atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
672 	}
673 	spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
674 }
675 
676 /* This function updates non-tdls peer ralist tx_pause while
677  * tdls channel switching
678  */
679 void mwifiex_update_ralist_tx_pause_in_tdls_cs(struct mwifiex_private *priv,
680 					       u8 *mac, u8 tx_pause)
681 {
682 	struct mwifiex_ra_list_tbl *ra_list;
683 	u32 pkt_cnt = 0, tx_pkts_queued;
684 	unsigned long flags;
685 	int i;
686 
687 	spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
688 
689 	for (i = 0; i < MAX_NUM_TID; ++i) {
690 		list_for_each_entry(ra_list, &priv->wmm.tid_tbl_ptr[i].ra_list,
691 				    list) {
692 			if (!memcmp(ra_list->ra, mac, ETH_ALEN))
693 				continue;
694 
695 			if (ra_list->tx_paused != tx_pause) {
696 				pkt_cnt += ra_list->total_pkt_count;
697 				ra_list->tx_paused = tx_pause;
698 				if (tx_pause)
699 					priv->wmm.pkts_paused[i] +=
700 						ra_list->total_pkt_count;
701 				else
702 					priv->wmm.pkts_paused[i] -=
703 						ra_list->total_pkt_count;
704 			}
705 		}
706 	}
707 
708 	if (pkt_cnt) {
709 		tx_pkts_queued = atomic_read(&priv->wmm.tx_pkts_queued);
710 		if (tx_pause)
711 			tx_pkts_queued -= pkt_cnt;
712 		else
713 			tx_pkts_queued += pkt_cnt;
714 
715 		atomic_set(&priv->wmm.tx_pkts_queued, tx_pkts_queued);
716 		atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
717 	}
718 	spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
719 }
720 
721 /*
722  * This function retrieves an RA list node for a given TID and
723  * RA address pair.
724  *
725  * If no such node is found, a new node is added first and then
726  * retrieved.
727  */
728 struct mwifiex_ra_list_tbl *
729 mwifiex_wmm_get_queue_raptr(struct mwifiex_private *priv, u8 tid,
730 			    const u8 *ra_addr)
731 {
732 	struct mwifiex_ra_list_tbl *ra_list;
733 
734 	ra_list = mwifiex_wmm_get_ralist_node(priv, tid, ra_addr);
735 	if (ra_list)
736 		return ra_list;
737 	mwifiex_ralist_add(priv, ra_addr);
738 
739 	return mwifiex_wmm_get_ralist_node(priv, tid, ra_addr);
740 }
741 
742 /*
743  * This function deletes RA list nodes for given mac for all TIDs.
744  * Function also decrements TX pending count accordingly.
745  */
746 void
747 mwifiex_wmm_del_peer_ra_list(struct mwifiex_private *priv, const u8 *ra_addr)
748 {
749 	struct mwifiex_ra_list_tbl *ra_list;
750 	unsigned long flags;
751 	int i;
752 
753 	spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
754 
755 	for (i = 0; i < MAX_NUM_TID; ++i) {
756 		ra_list = mwifiex_wmm_get_ralist_node(priv, i, ra_addr);
757 
758 		if (!ra_list)
759 			continue;
760 		mwifiex_wmm_del_pkts_in_ralist_node(priv, ra_list);
761 		if (ra_list->tx_paused)
762 			priv->wmm.pkts_paused[i] -= ra_list->total_pkt_count;
763 		else
764 			atomic_sub(ra_list->total_pkt_count,
765 				   &priv->wmm.tx_pkts_queued);
766 		list_del(&ra_list->list);
767 		kfree(ra_list);
768 	}
769 	spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
770 }
771 
772 /*
773  * This function checks if a particular RA list node exists in a given TID
774  * table index.
775  */
776 int
777 mwifiex_is_ralist_valid(struct mwifiex_private *priv,
778 			struct mwifiex_ra_list_tbl *ra_list, int ptr_index)
779 {
780 	struct mwifiex_ra_list_tbl *rlist;
781 
782 	list_for_each_entry(rlist, &priv->wmm.tid_tbl_ptr[ptr_index].ra_list,
783 			    list) {
784 		if (rlist == ra_list)
785 			return true;
786 	}
787 
788 	return false;
789 }
790 
791 /*
792  * This function adds a packet to bypass TX queue.
793  * This is special TX queue for packets which can be sent even when port_open
794  * is false.
795  */
796 void
797 mwifiex_wmm_add_buf_bypass_txqueue(struct mwifiex_private *priv,
798 				   struct sk_buff *skb)
799 {
800 	skb_queue_tail(&priv->bypass_txq, skb);
801 }
802 
803 /*
804  * This function adds a packet to WMM queue.
805  *
806  * In disconnected state the packet is immediately dropped and the
807  * packet send completion callback is called with status failure.
808  *
809  * Otherwise, the correct RA list node is located and the packet
810  * is queued at the list tail.
811  */
812 void
813 mwifiex_wmm_add_buf_txqueue(struct mwifiex_private *priv,
814 			    struct sk_buff *skb)
815 {
816 	struct mwifiex_adapter *adapter = priv->adapter;
817 	u32 tid;
818 	struct mwifiex_ra_list_tbl *ra_list;
819 	u8 ra[ETH_ALEN], tid_down;
820 	unsigned long flags;
821 	struct list_head list_head;
822 	int tdls_status = TDLS_NOT_SETUP;
823 	struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
824 	struct mwifiex_txinfo *tx_info = MWIFIEX_SKB_TXCB(skb);
825 
826 	memcpy(ra, eth_hdr->h_dest, ETH_ALEN);
827 
828 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
829 	    ISSUPP_TDLS_ENABLED(adapter->fw_cap_info)) {
830 		if (ntohs(eth_hdr->h_proto) == ETH_P_TDLS)
831 			mwifiex_dbg(adapter, DATA,
832 				    "TDLS setup packet for %pM.\t"
833 				    "Don't block\n", ra);
834 		else if (memcmp(priv->cfg_bssid, ra, ETH_ALEN))
835 			tdls_status = mwifiex_get_tdls_link_status(priv, ra);
836 	}
837 
838 	if (!priv->media_connected && !mwifiex_is_skb_mgmt_frame(skb)) {
839 		mwifiex_dbg(adapter, DATA, "data: drop packet in disconnect\n");
840 		mwifiex_write_data_complete(adapter, skb, 0, -1);
841 		return;
842 	}
843 
844 	tid = skb->priority;
845 
846 	spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
847 
848 	tid_down = mwifiex_wmm_downgrade_tid(priv, tid);
849 
850 	/* In case of infra as we have already created the list during
851 	   association we just don't have to call get_queue_raptr, we will
852 	   have only 1 raptr for a tid in case of infra */
853 	if (!mwifiex_queuing_ra_based(priv) &&
854 	    !mwifiex_is_skb_mgmt_frame(skb)) {
855 		switch (tdls_status) {
856 		case TDLS_SETUP_COMPLETE:
857 		case TDLS_CHAN_SWITCHING:
858 		case TDLS_IN_BASE_CHAN:
859 		case TDLS_IN_OFF_CHAN:
860 			ra_list = mwifiex_wmm_get_queue_raptr(priv, tid_down,
861 							      ra);
862 			tx_info->flags |= MWIFIEX_BUF_FLAG_TDLS_PKT;
863 			break;
864 		case TDLS_SETUP_INPROGRESS:
865 			skb_queue_tail(&priv->tdls_txq, skb);
866 			spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
867 					       flags);
868 			return;
869 		default:
870 			list_head = priv->wmm.tid_tbl_ptr[tid_down].ra_list;
871 			ra_list = list_first_entry_or_null(&list_head,
872 					struct mwifiex_ra_list_tbl, list);
873 			break;
874 		}
875 	} else {
876 		memcpy(ra, skb->data, ETH_ALEN);
877 		if (ra[0] & 0x01 || mwifiex_is_skb_mgmt_frame(skb))
878 			eth_broadcast_addr(ra);
879 		ra_list = mwifiex_wmm_get_queue_raptr(priv, tid_down, ra);
880 	}
881 
882 	if (!ra_list) {
883 		spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
884 		mwifiex_write_data_complete(adapter, skb, 0, -1);
885 		return;
886 	}
887 
888 	skb_queue_tail(&ra_list->skb_head, skb);
889 
890 	ra_list->ba_pkt_count++;
891 	ra_list->total_pkt_count++;
892 
893 	if (atomic_read(&priv->wmm.highest_queued_prio) <
894 						priv->tos_to_tid_inv[tid_down])
895 		atomic_set(&priv->wmm.highest_queued_prio,
896 			   priv->tos_to_tid_inv[tid_down]);
897 
898 	if (ra_list->tx_paused)
899 		priv->wmm.pkts_paused[tid_down]++;
900 	else
901 		atomic_inc(&priv->wmm.tx_pkts_queued);
902 
903 	spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
904 }
905 
906 /*
907  * This function processes the get WMM status command response from firmware.
908  *
909  * The response may contain multiple TLVs -
910  *      - AC Queue status TLVs
911  *      - Current WMM Parameter IE TLV
912  *      - Admission Control action frame TLVs
913  *
914  * This function parses the TLVs and then calls further specific functions
915  * to process any changes in the queue prioritize or state.
916  */
917 int mwifiex_ret_wmm_get_status(struct mwifiex_private *priv,
918 			       const struct host_cmd_ds_command *resp)
919 {
920 	u8 *curr = (u8 *) &resp->params.get_wmm_status;
921 	uint16_t resp_len = le16_to_cpu(resp->size), tlv_len;
922 	int mask = IEEE80211_WMM_IE_AP_QOSINFO_PARAM_SET_CNT_MASK;
923 	bool valid = true;
924 
925 	struct mwifiex_ie_types_data *tlv_hdr;
926 	struct mwifiex_ie_types_wmm_queue_status *tlv_wmm_qstatus;
927 	struct ieee_types_wmm_parameter *wmm_param_ie = NULL;
928 	struct mwifiex_wmm_ac_status *ac_status;
929 
930 	mwifiex_dbg(priv->adapter, INFO,
931 		    "info: WMM: WMM_GET_STATUS cmdresp received: %d\n",
932 		    resp_len);
933 
934 	while ((resp_len >= sizeof(tlv_hdr->header)) && valid) {
935 		tlv_hdr = (struct mwifiex_ie_types_data *) curr;
936 		tlv_len = le16_to_cpu(tlv_hdr->header.len);
937 
938 		if (resp_len < tlv_len + sizeof(tlv_hdr->header))
939 			break;
940 
941 		switch (le16_to_cpu(tlv_hdr->header.type)) {
942 		case TLV_TYPE_WMMQSTATUS:
943 			tlv_wmm_qstatus =
944 				(struct mwifiex_ie_types_wmm_queue_status *)
945 				tlv_hdr;
946 			mwifiex_dbg(priv->adapter, CMD,
947 				    "info: CMD_RESP: WMM_GET_STATUS:\t"
948 				    "QSTATUS TLV: %d, %d, %d\n",
949 				    tlv_wmm_qstatus->queue_index,
950 				    tlv_wmm_qstatus->flow_required,
951 				    tlv_wmm_qstatus->disabled);
952 
953 			ac_status = &priv->wmm.ac_status[tlv_wmm_qstatus->
954 							 queue_index];
955 			ac_status->disabled = tlv_wmm_qstatus->disabled;
956 			ac_status->flow_required =
957 						tlv_wmm_qstatus->flow_required;
958 			ac_status->flow_created = tlv_wmm_qstatus->flow_created;
959 			break;
960 
961 		case WLAN_EID_VENDOR_SPECIFIC:
962 			/*
963 			 * Point the regular IEEE IE 2 bytes into the Marvell IE
964 			 *   and setup the IEEE IE type and length byte fields
965 			 */
966 
967 			wmm_param_ie =
968 				(struct ieee_types_wmm_parameter *) (curr +
969 								    2);
970 			wmm_param_ie->vend_hdr.len = (u8) tlv_len;
971 			wmm_param_ie->vend_hdr.element_id =
972 						WLAN_EID_VENDOR_SPECIFIC;
973 
974 			mwifiex_dbg(priv->adapter, CMD,
975 				    "info: CMD_RESP: WMM_GET_STATUS:\t"
976 				    "WMM Parameter Set Count: %d\n",
977 				    wmm_param_ie->qos_info_bitmap & mask);
978 
979 			memcpy((u8 *) &priv->curr_bss_params.bss_descriptor.
980 			       wmm_ie, wmm_param_ie,
981 			       wmm_param_ie->vend_hdr.len + 2);
982 
983 			break;
984 
985 		default:
986 			valid = false;
987 			break;
988 		}
989 
990 		curr += (tlv_len + sizeof(tlv_hdr->header));
991 		resp_len -= (tlv_len + sizeof(tlv_hdr->header));
992 	}
993 
994 	mwifiex_wmm_setup_queue_priorities(priv, wmm_param_ie);
995 	mwifiex_wmm_setup_ac_downgrade(priv);
996 
997 	return 0;
998 }
999 
1000 /*
1001  * Callback handler from the command module to allow insertion of a WMM TLV.
1002  *
1003  * If the BSS we are associating to supports WMM, this function adds the
1004  * required WMM Information IE to the association request command buffer in
1005  * the form of a Marvell extended IEEE IE.
1006  */
1007 u32
1008 mwifiex_wmm_process_association_req(struct mwifiex_private *priv,
1009 				    u8 **assoc_buf,
1010 				    struct ieee_types_wmm_parameter *wmm_ie,
1011 				    struct ieee80211_ht_cap *ht_cap)
1012 {
1013 	struct mwifiex_ie_types_wmm_param_set *wmm_tlv;
1014 	u32 ret_len = 0;
1015 
1016 	/* Null checks */
1017 	if (!assoc_buf)
1018 		return 0;
1019 	if (!(*assoc_buf))
1020 		return 0;
1021 
1022 	if (!wmm_ie)
1023 		return 0;
1024 
1025 	mwifiex_dbg(priv->adapter, INFO,
1026 		    "info: WMM: process assoc req: bss->wmm_ie=%#x\n",
1027 		    wmm_ie->vend_hdr.element_id);
1028 
1029 	if ((priv->wmm_required ||
1030 	     (ht_cap && (priv->adapter->config_bands & BAND_GN ||
1031 	     priv->adapter->config_bands & BAND_AN))) &&
1032 	    wmm_ie->vend_hdr.element_id == WLAN_EID_VENDOR_SPECIFIC) {
1033 		wmm_tlv = (struct mwifiex_ie_types_wmm_param_set *) *assoc_buf;
1034 		wmm_tlv->header.type = cpu_to_le16((u16) wmm_info_ie[0]);
1035 		wmm_tlv->header.len = cpu_to_le16((u16) wmm_info_ie[1]);
1036 		memcpy(wmm_tlv->wmm_ie, &wmm_info_ie[2],
1037 		       le16_to_cpu(wmm_tlv->header.len));
1038 		if (wmm_ie->qos_info_bitmap & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD)
1039 			memcpy((u8 *) (wmm_tlv->wmm_ie
1040 				       + le16_to_cpu(wmm_tlv->header.len)
1041 				       - sizeof(priv->wmm_qosinfo)),
1042 			       &priv->wmm_qosinfo, sizeof(priv->wmm_qosinfo));
1043 
1044 		ret_len = sizeof(wmm_tlv->header)
1045 			  + le16_to_cpu(wmm_tlv->header.len);
1046 
1047 		*assoc_buf += ret_len;
1048 	}
1049 
1050 	return ret_len;
1051 }
1052 
1053 /*
1054  * This function computes the time delay in the driver queues for a
1055  * given packet.
1056  *
1057  * When the packet is received at the OS/Driver interface, the current
1058  * time is set in the packet structure. The difference between the present
1059  * time and that received time is computed in this function and limited
1060  * based on pre-compiled limits in the driver.
1061  */
1062 u8
1063 mwifiex_wmm_compute_drv_pkt_delay(struct mwifiex_private *priv,
1064 				  const struct sk_buff *skb)
1065 {
1066 	u32 queue_delay = ktime_to_ms(net_timedelta(skb->tstamp));
1067 	u8 ret_val;
1068 
1069 	/*
1070 	 * Queue delay is passed as a uint8 in units of 2ms (ms shifted
1071 	 *  by 1). Min value (other than 0) is therefore 2ms, max is 510ms.
1072 	 *
1073 	 * Pass max value if queue_delay is beyond the uint8 range
1074 	 */
1075 	ret_val = (u8) (min(queue_delay, priv->wmm.drv_pkt_delay_max) >> 1);
1076 
1077 	mwifiex_dbg(priv->adapter, DATA, "data: WMM: Pkt Delay: %d ms,\t"
1078 		    "%d ms sent to FW\n", queue_delay, ret_val);
1079 
1080 	return ret_val;
1081 }
1082 
1083 /*
1084  * This function retrieves the highest priority RA list table pointer.
1085  */
1086 static struct mwifiex_ra_list_tbl *
1087 mwifiex_wmm_get_highest_priolist_ptr(struct mwifiex_adapter *adapter,
1088 				     struct mwifiex_private **priv, int *tid)
1089 {
1090 	struct mwifiex_private *priv_tmp;
1091 	struct mwifiex_ra_list_tbl *ptr;
1092 	struct mwifiex_tid_tbl *tid_ptr;
1093 	atomic_t *hqp;
1094 	unsigned long flags_ra;
1095 	int i, j;
1096 
1097 	/* check the BSS with highest priority first */
1098 	for (j = adapter->priv_num - 1; j >= 0; --j) {
1099 		/* iterate over BSS with the equal priority */
1100 		list_for_each_entry(adapter->bss_prio_tbl[j].bss_prio_cur,
1101 				    &adapter->bss_prio_tbl[j].bss_prio_head,
1102 				    list) {
1103 
1104 try_again:
1105 			priv_tmp = adapter->bss_prio_tbl[j].bss_prio_cur->priv;
1106 
1107 			if (((priv_tmp->bss_mode != NL80211_IFTYPE_ADHOC) &&
1108 			     !priv_tmp->port_open) ||
1109 			    (atomic_read(&priv_tmp->wmm.tx_pkts_queued) == 0))
1110 				continue;
1111 
1112 			if (adapter->if_ops.is_port_ready &&
1113 			    !adapter->if_ops.is_port_ready(priv_tmp))
1114 				continue;
1115 
1116 			/* iterate over the WMM queues of the BSS */
1117 			hqp = &priv_tmp->wmm.highest_queued_prio;
1118 			for (i = atomic_read(hqp); i >= LOW_PRIO_TID; --i) {
1119 
1120 				spin_lock_irqsave(&priv_tmp->wmm.
1121 						  ra_list_spinlock, flags_ra);
1122 
1123 				tid_ptr = &(priv_tmp)->wmm.
1124 					tid_tbl_ptr[tos_to_tid[i]];
1125 
1126 				/* iterate over receiver addresses */
1127 				list_for_each_entry(ptr, &tid_ptr->ra_list,
1128 						    list) {
1129 
1130 					if (!ptr->tx_paused &&
1131 					    !skb_queue_empty(&ptr->skb_head))
1132 						/* holds both locks */
1133 						goto found;
1134 				}
1135 
1136 				spin_unlock_irqrestore(&priv_tmp->wmm.
1137 						       ra_list_spinlock,
1138 						       flags_ra);
1139 			}
1140 
1141 			if (atomic_read(&priv_tmp->wmm.tx_pkts_queued) != 0) {
1142 				atomic_set(&priv_tmp->wmm.highest_queued_prio,
1143 					   HIGH_PRIO_TID);
1144 				/* Iterate current private once more, since
1145 				 * there still exist packets in data queue
1146 				 */
1147 				goto try_again;
1148 			} else
1149 				atomic_set(&priv_tmp->wmm.highest_queued_prio,
1150 					   NO_PKT_PRIO_TID);
1151 		}
1152 	}
1153 
1154 	return NULL;
1155 
1156 found:
1157 	/* holds ra_list_spinlock */
1158 	if (atomic_read(hqp) > i)
1159 		atomic_set(hqp, i);
1160 	spin_unlock_irqrestore(&priv_tmp->wmm.ra_list_spinlock, flags_ra);
1161 
1162 	*priv = priv_tmp;
1163 	*tid = tos_to_tid[i];
1164 
1165 	return ptr;
1166 }
1167 
1168 /* This functions rotates ra and bss lists so packets are picked round robin.
1169  *
1170  * After a packet is successfully transmitted, rotate the ra list, so the ra
1171  * next to the one transmitted, will come first in the list. This way we pick
1172  * the ra' in a round robin fashion. Same applies to bss nodes of equal
1173  * priority.
1174  *
1175  * Function also increments wmm.packets_out counter.
1176  */
1177 void mwifiex_rotate_priolists(struct mwifiex_private *priv,
1178 				 struct mwifiex_ra_list_tbl *ra,
1179 				 int tid)
1180 {
1181 	struct mwifiex_adapter *adapter = priv->adapter;
1182 	struct mwifiex_bss_prio_tbl *tbl = adapter->bss_prio_tbl;
1183 	struct mwifiex_tid_tbl *tid_ptr = &priv->wmm.tid_tbl_ptr[tid];
1184 	unsigned long flags;
1185 
1186 	spin_lock_irqsave(&tbl[priv->bss_priority].bss_prio_lock, flags);
1187 	/*
1188 	 * dirty trick: we remove 'head' temporarily and reinsert it after
1189 	 * curr bss node. imagine list to stay fixed while head is moved
1190 	 */
1191 	list_move(&tbl[priv->bss_priority].bss_prio_head,
1192 		  &tbl[priv->bss_priority].bss_prio_cur->list);
1193 	spin_unlock_irqrestore(&tbl[priv->bss_priority].bss_prio_lock, flags);
1194 
1195 	spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
1196 	if (mwifiex_is_ralist_valid(priv, ra, tid)) {
1197 		priv->wmm.packets_out[tid]++;
1198 		/* same as above */
1199 		list_move(&tid_ptr->ra_list, &ra->list);
1200 	}
1201 	spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
1202 }
1203 
1204 /*
1205  * This function checks if 11n aggregation is possible.
1206  */
1207 static int
1208 mwifiex_is_11n_aggragation_possible(struct mwifiex_private *priv,
1209 				    struct mwifiex_ra_list_tbl *ptr,
1210 				    int max_buf_size)
1211 {
1212 	int count = 0, total_size = 0;
1213 	struct sk_buff *skb, *tmp;
1214 	int max_amsdu_size;
1215 
1216 	if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP && priv->ap_11n_enabled &&
1217 	    ptr->is_11n_enabled)
1218 		max_amsdu_size = min_t(int, ptr->max_amsdu, max_buf_size);
1219 	else
1220 		max_amsdu_size = max_buf_size;
1221 
1222 	skb_queue_walk_safe(&ptr->skb_head, skb, tmp) {
1223 		total_size += skb->len;
1224 		if (total_size >= max_amsdu_size)
1225 			break;
1226 		if (++count >= MIN_NUM_AMSDU)
1227 			return true;
1228 	}
1229 
1230 	return false;
1231 }
1232 
1233 /*
1234  * This function sends a single packet to firmware for transmission.
1235  */
1236 static void
1237 mwifiex_send_single_packet(struct mwifiex_private *priv,
1238 			   struct mwifiex_ra_list_tbl *ptr, int ptr_index,
1239 			   unsigned long ra_list_flags)
1240 			   __releases(&priv->wmm.ra_list_spinlock)
1241 {
1242 	struct sk_buff *skb, *skb_next;
1243 	struct mwifiex_tx_param tx_param;
1244 	struct mwifiex_adapter *adapter = priv->adapter;
1245 	struct mwifiex_txinfo *tx_info;
1246 
1247 	if (skb_queue_empty(&ptr->skb_head)) {
1248 		spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1249 				       ra_list_flags);
1250 		mwifiex_dbg(adapter, DATA, "data: nothing to send\n");
1251 		return;
1252 	}
1253 
1254 	skb = skb_dequeue(&ptr->skb_head);
1255 
1256 	tx_info = MWIFIEX_SKB_TXCB(skb);
1257 	mwifiex_dbg(adapter, DATA,
1258 		    "data: dequeuing the packet %p %p\n", ptr, skb);
1259 
1260 	ptr->total_pkt_count--;
1261 
1262 	if (!skb_queue_empty(&ptr->skb_head))
1263 		skb_next = skb_peek(&ptr->skb_head);
1264 	else
1265 		skb_next = NULL;
1266 
1267 	spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, ra_list_flags);
1268 
1269 	tx_param.next_pkt_len = ((skb_next) ? skb_next->len +
1270 				sizeof(struct txpd) : 0);
1271 
1272 	if (mwifiex_process_tx(priv, skb, &tx_param) == -EBUSY) {
1273 		/* Queue the packet back at the head */
1274 		spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags);
1275 
1276 		if (!mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1277 			spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1278 					       ra_list_flags);
1279 			mwifiex_write_data_complete(adapter, skb, 0, -1);
1280 			return;
1281 		}
1282 
1283 		skb_queue_tail(&ptr->skb_head, skb);
1284 
1285 		ptr->total_pkt_count++;
1286 		ptr->ba_pkt_count++;
1287 		tx_info->flags |= MWIFIEX_BUF_FLAG_REQUEUED_PKT;
1288 		spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1289 				       ra_list_flags);
1290 	} else {
1291 		mwifiex_rotate_priolists(priv, ptr, ptr_index);
1292 		atomic_dec(&priv->wmm.tx_pkts_queued);
1293 	}
1294 }
1295 
1296 /*
1297  * This function checks if the first packet in the given RA list
1298  * is already processed or not.
1299  */
1300 static int
1301 mwifiex_is_ptr_processed(struct mwifiex_private *priv,
1302 			 struct mwifiex_ra_list_tbl *ptr)
1303 {
1304 	struct sk_buff *skb;
1305 	struct mwifiex_txinfo *tx_info;
1306 
1307 	if (skb_queue_empty(&ptr->skb_head))
1308 		return false;
1309 
1310 	skb = skb_peek(&ptr->skb_head);
1311 
1312 	tx_info = MWIFIEX_SKB_TXCB(skb);
1313 	if (tx_info->flags & MWIFIEX_BUF_FLAG_REQUEUED_PKT)
1314 		return true;
1315 
1316 	return false;
1317 }
1318 
1319 /*
1320  * This function sends a single processed packet to firmware for
1321  * transmission.
1322  */
1323 static void
1324 mwifiex_send_processed_packet(struct mwifiex_private *priv,
1325 			      struct mwifiex_ra_list_tbl *ptr, int ptr_index,
1326 			      unsigned long ra_list_flags)
1327 				__releases(&priv->wmm.ra_list_spinlock)
1328 {
1329 	struct mwifiex_tx_param tx_param;
1330 	struct mwifiex_adapter *adapter = priv->adapter;
1331 	int ret = -1;
1332 	struct sk_buff *skb, *skb_next;
1333 	struct mwifiex_txinfo *tx_info;
1334 
1335 	if (skb_queue_empty(&ptr->skb_head)) {
1336 		spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1337 				       ra_list_flags);
1338 		return;
1339 	}
1340 
1341 	skb = skb_dequeue(&ptr->skb_head);
1342 
1343 	if (adapter->data_sent || adapter->tx_lock_flag) {
1344 		ptr->total_pkt_count--;
1345 		spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1346 				       ra_list_flags);
1347 		skb_queue_tail(&adapter->tx_data_q, skb);
1348 		atomic_dec(&priv->wmm.tx_pkts_queued);
1349 		atomic_inc(&adapter->tx_queued);
1350 		return;
1351 	}
1352 
1353 	if (!skb_queue_empty(&ptr->skb_head))
1354 		skb_next = skb_peek(&ptr->skb_head);
1355 	else
1356 		skb_next = NULL;
1357 
1358 	tx_info = MWIFIEX_SKB_TXCB(skb);
1359 
1360 	spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, ra_list_flags);
1361 
1362 	tx_param.next_pkt_len =
1363 		((skb_next) ? skb_next->len +
1364 		 sizeof(struct txpd) : 0);
1365 	if (adapter->iface_type == MWIFIEX_USB) {
1366 		ret = adapter->if_ops.host_to_card(adapter, priv->usb_port,
1367 						   skb, &tx_param);
1368 	} else {
1369 		ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_DATA,
1370 						   skb, &tx_param);
1371 	}
1372 
1373 	switch (ret) {
1374 	case -EBUSY:
1375 		mwifiex_dbg(adapter, ERROR, "data: -EBUSY is returned\n");
1376 		spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags);
1377 
1378 		if (!mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1379 			spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1380 					       ra_list_flags);
1381 			mwifiex_write_data_complete(adapter, skb, 0, -1);
1382 			return;
1383 		}
1384 
1385 		skb_queue_tail(&ptr->skb_head, skb);
1386 
1387 		tx_info->flags |= MWIFIEX_BUF_FLAG_REQUEUED_PKT;
1388 		spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1389 				       ra_list_flags);
1390 		break;
1391 	case -1:
1392 		mwifiex_dbg(adapter, ERROR, "host_to_card failed: %#x\n", ret);
1393 		adapter->dbg.num_tx_host_to_card_failure++;
1394 		mwifiex_write_data_complete(adapter, skb, 0, ret);
1395 		break;
1396 	case -EINPROGRESS:
1397 		break;
1398 	case 0:
1399 		mwifiex_write_data_complete(adapter, skb, 0, ret);
1400 	default:
1401 		break;
1402 	}
1403 	if (ret != -EBUSY) {
1404 		mwifiex_rotate_priolists(priv, ptr, ptr_index);
1405 		atomic_dec(&priv->wmm.tx_pkts_queued);
1406 		spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags);
1407 		ptr->total_pkt_count--;
1408 		spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1409 				       ra_list_flags);
1410 	}
1411 }
1412 
1413 /*
1414  * This function dequeues a packet from the highest priority list
1415  * and transmits it.
1416  */
1417 static int
1418 mwifiex_dequeue_tx_packet(struct mwifiex_adapter *adapter)
1419 {
1420 	struct mwifiex_ra_list_tbl *ptr;
1421 	struct mwifiex_private *priv = NULL;
1422 	int ptr_index = 0;
1423 	u8 ra[ETH_ALEN];
1424 	int tid_del = 0, tid = 0;
1425 	unsigned long flags;
1426 
1427 	ptr = mwifiex_wmm_get_highest_priolist_ptr(adapter, &priv, &ptr_index);
1428 	if (!ptr)
1429 		return -1;
1430 
1431 	tid = mwifiex_get_tid(ptr);
1432 
1433 	mwifiex_dbg(adapter, DATA, "data: tid=%d\n", tid);
1434 
1435 	spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
1436 	if (!mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1437 		spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
1438 		return -1;
1439 	}
1440 
1441 	if (mwifiex_is_ptr_processed(priv, ptr)) {
1442 		mwifiex_send_processed_packet(priv, ptr, ptr_index, flags);
1443 		/* ra_list_spinlock has been freed in
1444 		   mwifiex_send_processed_packet() */
1445 		return 0;
1446 	}
1447 
1448 	if (!ptr->is_11n_enabled ||
1449 		ptr->ba_status ||
1450 		priv->wps.session_enable) {
1451 		if (ptr->is_11n_enabled &&
1452 			ptr->ba_status &&
1453 			ptr->amsdu_in_ampdu &&
1454 			mwifiex_is_amsdu_allowed(priv, tid) &&
1455 			mwifiex_is_11n_aggragation_possible(priv, ptr,
1456 							adapter->tx_buf_size))
1457 			mwifiex_11n_aggregate_pkt(priv, ptr, ptr_index, flags);
1458 			/* ra_list_spinlock has been freed in
1459 			 * mwifiex_11n_aggregate_pkt()
1460 			 */
1461 		else
1462 			mwifiex_send_single_packet(priv, ptr, ptr_index, flags);
1463 			/* ra_list_spinlock has been freed in
1464 			 * mwifiex_send_single_packet()
1465 			 */
1466 	} else {
1467 		if (mwifiex_is_ampdu_allowed(priv, ptr, tid) &&
1468 		    ptr->ba_pkt_count > ptr->ba_packet_thr) {
1469 			if (mwifiex_space_avail_for_new_ba_stream(adapter)) {
1470 				mwifiex_create_ba_tbl(priv, ptr->ra, tid,
1471 						      BA_SETUP_INPROGRESS);
1472 				mwifiex_send_addba(priv, tid, ptr->ra);
1473 			} else if (mwifiex_find_stream_to_delete
1474 				   (priv, tid, &tid_del, ra)) {
1475 				mwifiex_create_ba_tbl(priv, ptr->ra, tid,
1476 						      BA_SETUP_INPROGRESS);
1477 				mwifiex_send_delba(priv, tid_del, ra, 1);
1478 			}
1479 		}
1480 		if (mwifiex_is_amsdu_allowed(priv, tid) &&
1481 		    mwifiex_is_11n_aggragation_possible(priv, ptr,
1482 							adapter->tx_buf_size))
1483 			mwifiex_11n_aggregate_pkt(priv, ptr, ptr_index, flags);
1484 			/* ra_list_spinlock has been freed in
1485 			   mwifiex_11n_aggregate_pkt() */
1486 		else
1487 			mwifiex_send_single_packet(priv, ptr, ptr_index, flags);
1488 			/* ra_list_spinlock has been freed in
1489 			   mwifiex_send_single_packet() */
1490 	}
1491 	return 0;
1492 }
1493 
1494 void mwifiex_process_bypass_tx(struct mwifiex_adapter *adapter)
1495 {
1496 	struct mwifiex_tx_param tx_param;
1497 	struct sk_buff *skb;
1498 	struct mwifiex_txinfo *tx_info;
1499 	struct mwifiex_private *priv;
1500 	int i;
1501 
1502 	if (adapter->data_sent || adapter->tx_lock_flag)
1503 		return;
1504 
1505 	for (i = 0; i < adapter->priv_num; ++i) {
1506 		priv = adapter->priv[i];
1507 
1508 		if (!priv)
1509 			continue;
1510 
1511 		if (adapter->if_ops.is_port_ready &&
1512 		    !adapter->if_ops.is_port_ready(priv))
1513 			continue;
1514 
1515 		if (skb_queue_empty(&priv->bypass_txq))
1516 			continue;
1517 
1518 		skb = skb_dequeue(&priv->bypass_txq);
1519 		tx_info = MWIFIEX_SKB_TXCB(skb);
1520 
1521 		/* no aggregation for bypass packets */
1522 		tx_param.next_pkt_len = 0;
1523 
1524 		if (mwifiex_process_tx(priv, skb, &tx_param) == -EBUSY) {
1525 			skb_queue_head(&priv->bypass_txq, skb);
1526 			tx_info->flags |= MWIFIEX_BUF_FLAG_REQUEUED_PKT;
1527 		} else {
1528 			atomic_dec(&adapter->bypass_tx_pending);
1529 		}
1530 	}
1531 }
1532 
1533 /*
1534  * This function transmits the highest priority packet awaiting in the
1535  * WMM Queues.
1536  */
1537 void
1538 mwifiex_wmm_process_tx(struct mwifiex_adapter *adapter)
1539 {
1540 	do {
1541 		if (mwifiex_dequeue_tx_packet(adapter))
1542 			break;
1543 		if (adapter->iface_type != MWIFIEX_SDIO) {
1544 			if (adapter->data_sent ||
1545 			    adapter->tx_lock_flag)
1546 				break;
1547 		} else {
1548 			if (atomic_read(&adapter->tx_queued) >=
1549 			    MWIFIEX_MAX_PKTS_TXQ)
1550 				break;
1551 		}
1552 	} while (!mwifiex_wmm_lists_empty(adapter));
1553 }
1554