xref: /linux/drivers/net/wireless/nxp/nxpwifi/wmm.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NXP Wireless LAN device driver: WMM
4  *
5  * Copyright 2011-2024 NXP
6  */
7 
8 #include "cfg.h"
9 #include "util.h"
10 #include "fw.h"
11 #include "main.h"
12 #include "wmm.h"
13 #include "11n.h"
14 
15 /* Maximum value FW can accept for driver delay in packet transmission */
16 #define DRV_PKT_DELAY_TO_FW_MAX   512
17 
18 #define WMM_QUEUED_PACKET_LOWER_LIMIT   180
19 
20 #define WMM_QUEUED_PACKET_UPPER_LIMIT   200
21 
22 /* Offset for TOS field in the IP header */
23 #define IPTOS_OFFSET 5
24 
25 static bool disable_tx_amsdu;
26 
27 /*
28  * This table inverses the tos_to_tid operation to get a priority
29  * which is in sequential order, and can be compared.
30  * Use this to compare the priority of two different TIDs.
31  */
32 static const u8 tos_to_tid_inv[] = {
33 	0x02,  /* from tos_to_tid[2] = 0 */
34 	0x00,  /* from tos_to_tid[0] = 1 */
35 	0x01,  /* from tos_to_tid[1] = 2 */
36 	0x03,
37 	0x04,
38 	0x05,
39 	0x06,
40 	0x07
41 };
42 
43 /* WMM information element */
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 /* Debug prints the priority parameters for a WMM AC. */
70 static void
nxpwifi_wmm_ac_debug_print(const struct ieee80211_wmm_ac_param * ac_param)71 nxpwifi_wmm_ac_debug_print(const struct ieee80211_wmm_ac_param *ac_param)
72 {
73 	static const char * const ac_str[] = { "BK", "BE", "VI", "VO" };
74 
75 	pr_debug("info: WMM AC_%s: ACI=%d, ACM=%d, Aifsn=%d, ",
76 		 ac_str[wmm_aci_to_qidx_map[(ac_param->aci_aifsn
77 					     & NXPWIFI_ACI) >> 5]],
78 		 (ac_param->aci_aifsn & NXPWIFI_ACI) >> 5,
79 		 (ac_param->aci_aifsn & NXPWIFI_ACM) >> 4,
80 		 ac_param->aci_aifsn & NXPWIFI_AIFSN);
81 	pr_debug("EcwMin=%d, EcwMax=%d, TxopLimit=%d\n",
82 		 ac_param->cw & NXPWIFI_ECW_MIN,
83 		 (ac_param->cw & NXPWIFI_ECW_MAX) >> 4,
84 		 le16_to_cpu(ac_param->txop_limit));
85 }
86 
87 /* Allocates a route address list. */
88 static struct nxpwifi_ra_list_tbl *
nxpwifi_wmm_allocate_ralist_node(struct nxpwifi_adapter * adapter,const u8 * ra)89 nxpwifi_wmm_allocate_ralist_node(struct nxpwifi_adapter *adapter, const u8 *ra)
90 {
91 	struct nxpwifi_ra_list_tbl *ra_list;
92 
93 	ra_list = kzalloc_obj(*ra_list, GFP_ATOMIC);
94 	if (!ra_list)
95 		return NULL;
96 
97 	INIT_LIST_HEAD(&ra_list->list);
98 	skb_queue_head_init(&ra_list->skb_head);
99 
100 	memcpy(ra_list->ra, ra, ETH_ALEN);
101 
102 	ra_list->total_pkt_count = 0;
103 
104 	nxpwifi_dbg(adapter, INFO, "info: allocated ra_list %p\n", ra_list);
105 
106 	return ra_list;
107 }
108 
109 /*
110  * Returns random no between 16 and 32 to be used as threshold for no of
111  * packets after which BA setup is initiated.
112  */
nxpwifi_get_random_ba_threshold(void)113 static u8 nxpwifi_get_random_ba_threshold(void)
114 {
115 	u64 ns;
116 	/*
117 	 * setup ba_packet_threshold here random number between
118 	 * [BA_SETUP_PACKET_OFFSET,
119 	 * BA_SETUP_PACKET_OFFSET+BA_SETUP_MAX_PACKET_THRESHOLD-1]
120 	 */
121 	ns = ktime_get_ns();
122 	ns += (ns >> 32) + (ns >> 16);
123 
124 	return ((u8)ns % BA_SETUP_MAX_PACKET_THRESHOLD) + BA_SETUP_PACKET_OFFSET;
125 }
126 
127 /* Allocates and adds a RA list for all TIDs with the given RA. */
nxpwifi_ralist_add(struct nxpwifi_private * priv,const u8 * ra)128 void nxpwifi_ralist_add(struct nxpwifi_private *priv, const u8 *ra)
129 {
130 	int i;
131 	struct nxpwifi_ra_list_tbl *ra_list;
132 	struct nxpwifi_adapter *adapter = priv->adapter;
133 	struct nxpwifi_sta_node *node;
134 
135 	for (i = 0; i < MAX_NUM_TID; ++i) {
136 		ra_list = nxpwifi_wmm_allocate_ralist_node(adapter, ra);
137 		nxpwifi_dbg(adapter, INFO,
138 			    "info: created ra_list %p\n", ra_list);
139 
140 		if (!ra_list)
141 			break;
142 
143 		ra_list->is_11n_enabled = 0;
144 		ra_list->ba_status = BA_SETUP_NONE;
145 		ra_list->amsdu_in_ampdu = false;
146 		if (!nxpwifi_queuing_ra_based(priv)) {
147 			ra_list->is_11n_enabled = IS_11N_ENABLED(priv);
148 		} else {
149 			rcu_read_lock();
150 			node = nxpwifi_get_sta_entry(priv, ra);
151 			if (node)
152 				ra_list->tx_paused = node->tx_pause;
153 			ra_list->is_11n_enabled =
154 				      nxpwifi_is_sta_11n_enabled(priv, node);
155 			if (ra_list->is_11n_enabled)
156 				ra_list->max_amsdu = node->max_amsdu;
157 			rcu_read_unlock();
158 		}
159 
160 		nxpwifi_dbg(adapter, DATA, "data: ralist %p: is_11n_enabled=%d\n",
161 			    ra_list, ra_list->is_11n_enabled);
162 
163 		if (ra_list->is_11n_enabled) {
164 			ra_list->ba_pkt_count = 0;
165 			ra_list->ba_packet_thr =
166 					      nxpwifi_get_random_ba_threshold();
167 		}
168 		list_add_tail(&ra_list->list,
169 			      &priv->wmm.tid_tbl_ptr[i].ra_list);
170 	}
171 }
172 
173 /* Sets the WMM queue priorities to their default values. */
nxpwifi_wmm_default_queue_priorities(struct nxpwifi_private * priv)174 static void nxpwifi_wmm_default_queue_priorities(struct nxpwifi_private *priv)
175 {
176 	/* Default queue priorities: VO->VI->BE->BK */
177 	priv->wmm.queue_priority[0] = WMM_AC_VO;
178 	priv->wmm.queue_priority[1] = WMM_AC_VI;
179 	priv->wmm.queue_priority[2] = WMM_AC_BE;
180 	priv->wmm.queue_priority[3] = WMM_AC_BK;
181 }
182 
183 /* Map ACs to TIDs. */
184 static void
nxpwifi_wmm_queue_priorities_tid(struct nxpwifi_private * priv)185 nxpwifi_wmm_queue_priorities_tid(struct nxpwifi_private *priv)
186 {
187 	struct nxpwifi_wmm_desc *wmm = &priv->wmm;
188 	u8 *queue_priority = wmm->queue_priority;
189 	int i;
190 
191 	for (i = 0; i < 4; ++i) {
192 		tos_to_tid[7 - (i * 2)] = ac_to_tid[queue_priority[i]][1];
193 		tos_to_tid[6 - (i * 2)] = ac_to_tid[queue_priority[i]][0];
194 	}
195 
196 	for (i = 0; i < MAX_NUM_TID; ++i)
197 		priv->tos_to_tid_inv[tos_to_tid[i]] = (u8)i;
198 
199 	atomic_set(&wmm->highest_queued_prio, HIGH_PRIO_TID);
200 }
201 
202 /* Initializes WMM priority queues. */
203 void
nxpwifi_wmm_setup_queue_priorities(struct nxpwifi_private * priv,struct ieee80211_wmm_param_ie * wmm_ie)204 nxpwifi_wmm_setup_queue_priorities(struct nxpwifi_private *priv,
205 				   struct ieee80211_wmm_param_ie *wmm_ie)
206 {
207 	u16 cw_min, avg_back_off, tmp[4];
208 	u32 i, j, num_ac;
209 	u8 ac_idx;
210 
211 	if (!wmm_ie || !priv->wmm_enabled) {
212 		/* WMM is not enabled, just set the defaults and return */
213 		nxpwifi_wmm_default_queue_priorities(priv);
214 		return;
215 	}
216 
217 	nxpwifi_dbg(priv->adapter, INFO,
218 		    "info: WMM Parameter element: version=%d,\t"
219 		    "qos_info Parameter Set Count=%d, Reserved=%#x\n",
220 		    wmm_ie->version, wmm_ie->qos_info &
221 		    IEEE80211_WMM_IE_AP_QOSINFO_PARAM_SET_CNT_MASK,
222 		    wmm_ie->reserved);
223 
224 	for (num_ac = 0; num_ac < ARRAY_SIZE(wmm_ie->ac); num_ac++) {
225 		u8 ecw = wmm_ie->ac[num_ac].cw;
226 		u8 aci_aifsn = wmm_ie->ac[num_ac].aci_aifsn;
227 
228 		cw_min = (1 << (ecw & NXPWIFI_ECW_MIN)) - 1;
229 		avg_back_off = (cw_min >> 1) + (aci_aifsn & NXPWIFI_AIFSN);
230 
231 		ac_idx = wmm_aci_to_qidx_map[(aci_aifsn & NXPWIFI_ACI) >> 5];
232 		priv->wmm.queue_priority[ac_idx] = ac_idx;
233 		tmp[ac_idx] = avg_back_off;
234 
235 		nxpwifi_dbg(priv->adapter, INFO,
236 			    "info: WMM: CWmax=%d CWmin=%d Avg Back-off=%d\n",
237 			    (1 << ((ecw & NXPWIFI_ECW_MAX) >> 4)) - 1,
238 			    cw_min, avg_back_off);
239 		nxpwifi_wmm_ac_debug_print(&wmm_ie->ac[num_ac]);
240 	}
241 
242 	/* Bubble sort */
243 	for (i = 0; i < num_ac; i++) {
244 		for (j = 1; j < num_ac - i; j++) {
245 			if (tmp[j - 1] > tmp[j]) {
246 				swap(tmp[j - 1], tmp[j]);
247 				swap(priv->wmm.queue_priority[j - 1],
248 				     priv->wmm.queue_priority[j]);
249 			} else if (tmp[j - 1] == tmp[j]) {
250 				if (priv->wmm.queue_priority[j - 1]
251 				    < priv->wmm.queue_priority[j])
252 					swap(priv->wmm.queue_priority[j - 1],
253 					     priv->wmm.queue_priority[j]);
254 			}
255 		}
256 	}
257 
258 	nxpwifi_wmm_queue_priorities_tid(priv);
259 }
260 
261 /* Evaluates whether or not an AC is to be downgraded. */
262 static enum nxpwifi_wmm_ac_e
nxpwifi_wmm_eval_downgrade_ac(struct nxpwifi_private * priv,enum nxpwifi_wmm_ac_e eval_ac)263 nxpwifi_wmm_eval_downgrade_ac(struct nxpwifi_private *priv,
264 			      enum nxpwifi_wmm_ac_e eval_ac)
265 {
266 	int down_ac;
267 	enum nxpwifi_wmm_ac_e ret_ac;
268 	struct nxpwifi_wmm_ac_status *ac_status;
269 
270 	ac_status = &priv->wmm.ac_status[eval_ac];
271 
272 	if (!ac_status->disabled)
273 		/* Okay to use this AC, its enabled */
274 		return eval_ac;
275 
276 	/* Setup a default return value of the lowest priority */
277 	ret_ac = WMM_AC_BK;
278 
279 	/*
280 	 * Find the highest AC that is enabled and does not require
281 	 *  admission control. The spec disallows downgrading to an AC,
282 	 *  which is enabled due to a completed admission control.
283 	 *  Unadmitted traffic is not to be sent on an AC with admitted
284 	 *  traffic.
285 	 */
286 	for (down_ac = WMM_AC_BK; down_ac < eval_ac; down_ac++) {
287 		ac_status = &priv->wmm.ac_status[down_ac];
288 
289 		if (!ac_status->disabled && !ac_status->flow_required)
290 			/*
291 			 * AC is enabled and does not require admission
292 			 * control
293 			 */
294 			ret_ac = (enum nxpwifi_wmm_ac_e)down_ac;
295 	}
296 
297 	return ret_ac;
298 }
299 
300 /* Downgrades WMM priority queue. */
301 void
nxpwifi_wmm_setup_ac_downgrade(struct nxpwifi_private * priv)302 nxpwifi_wmm_setup_ac_downgrade(struct nxpwifi_private *priv)
303 {
304 	int ac_val;
305 
306 	nxpwifi_dbg(priv->adapter, INFO, "info: WMM: AC Priorities:\t"
307 		    "BK(0), BE(1), VI(2), VO(3)\n");
308 
309 	if (!priv->wmm_enabled) {
310 		/* WMM is not enabled, default priorities */
311 		for (ac_val = WMM_AC_BK; ac_val <= WMM_AC_VO; ac_val++)
312 			priv->wmm.ac_down_graded_vals[ac_val] =
313 				(enum nxpwifi_wmm_ac_e)ac_val;
314 	} else {
315 		for (ac_val = WMM_AC_BK; ac_val <= WMM_AC_VO; ac_val++) {
316 			priv->wmm.ac_down_graded_vals[ac_val] =
317 				nxpwifi_wmm_eval_downgrade_ac
318 				(priv, (enum nxpwifi_wmm_ac_e)ac_val);
319 			nxpwifi_dbg(priv->adapter, INFO,
320 				    "info: WMM: AC PRIO %d maps to %d\n",
321 				    ac_val,
322 				    priv->wmm.ac_down_graded_vals[ac_val]);
323 		}
324 	}
325 }
326 
327 /* Converts the IP TOS field to an WMM AC Queue assignment. */
328 static enum nxpwifi_wmm_ac_e
nxpwifi_wmm_convert_tos_to_ac(struct nxpwifi_adapter * adapter,u32 tos)329 nxpwifi_wmm_convert_tos_to_ac(struct nxpwifi_adapter *adapter, u32 tos)
330 {
331 	/* Map of TOS UP values to WMM AC */
332 	static const enum nxpwifi_wmm_ac_e tos_to_ac[] = {
333 		WMM_AC_BE,
334 		WMM_AC_BK,
335 		WMM_AC_BK,
336 		WMM_AC_BE,
337 		WMM_AC_VI,
338 		WMM_AC_VI,
339 		WMM_AC_VO,
340 		WMM_AC_VO
341 	};
342 
343 	if (tos >= ARRAY_SIZE(tos_to_ac))
344 		return WMM_AC_BE;
345 
346 	return tos_to_ac[tos];
347 }
348 
349 /*
350  * Evaluates a given TID and downgrades it to a lower TID if the WMM Parameter
351  * element received from the AP indicates that the AP is disabled (due to call
352  * admission control (ACM bit).
353  */
nxpwifi_wmm_downgrade_tid(struct nxpwifi_private * priv,u32 tid)354 u8 nxpwifi_wmm_downgrade_tid(struct nxpwifi_private *priv, u32 tid)
355 {
356 	enum nxpwifi_wmm_ac_e ac, ac_down;
357 	u8 new_tid;
358 
359 	ac = nxpwifi_wmm_convert_tos_to_ac(priv->adapter, tid);
360 	ac_down = priv->wmm.ac_down_graded_vals[ac];
361 
362 	/*
363 	 * Send the index to tid array, picking from the array will be
364 	 * taken care by dequeuing function
365 	 */
366 	new_tid = ac_to_tid[ac_down][tid % 2];
367 
368 	return new_tid;
369 }
370 
371 /* Initializes the WMM state information and the WMM data path queues. */
372 void
nxpwifi_wmm_init(struct nxpwifi_adapter * adapter)373 nxpwifi_wmm_init(struct nxpwifi_adapter *adapter)
374 {
375 	int i, j;
376 	struct nxpwifi_private *priv;
377 
378 	for (j = 0; j < adapter->priv_num; ++j) {
379 		priv = adapter->priv[j];
380 
381 		for (i = 0; i < MAX_NUM_TID; ++i) {
382 			if (!disable_tx_amsdu &&
383 			    adapter->tx_buf_size > NXPWIFI_TX_DATA_BUF_SIZE_2K)
384 				priv->aggr_prio_tbl[i].amsdu =
385 					priv->tos_to_tid_inv[i];
386 			else
387 				priv->aggr_prio_tbl[i].amsdu =
388 					BA_STREAM_NOT_ALLOWED;
389 			priv->aggr_prio_tbl[i].ampdu_ap =
390 				priv->tos_to_tid_inv[i];
391 			priv->aggr_prio_tbl[i].ampdu_user =
392 				priv->tos_to_tid_inv[i];
393 		}
394 
395 		priv->aggr_prio_tbl[6].amsdu =
396 			priv->aggr_prio_tbl[6].ampdu_ap =
397 			priv->aggr_prio_tbl[6].ampdu_user =
398 			BA_STREAM_NOT_ALLOWED;
399 
400 		priv->aggr_prio_tbl[7].amsdu =
401 			priv->aggr_prio_tbl[7].ampdu_ap =
402 			priv->aggr_prio_tbl[7].ampdu_user =
403 			BA_STREAM_NOT_ALLOWED;
404 
405 		nxpwifi_set_ba_params(priv);
406 		nxpwifi_reset_11n_rx_seq_num(priv);
407 
408 		priv->wmm.drv_pkt_delay_max = NXPWIFI_WMM_DRV_DELAY_MAX;
409 		atomic_set(&priv->wmm.tx_pkts_queued, 0);
410 		atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
411 	}
412 }
413 
nxpwifi_bypass_txlist_empty(struct nxpwifi_adapter * adapter)414 bool nxpwifi_bypass_txlist_empty(struct nxpwifi_adapter *adapter)
415 {
416 	struct nxpwifi_private *priv;
417 	int i;
418 
419 	for (i = 0; i < adapter->priv_num; i++) {
420 		priv = adapter->priv[i];
421 		if (!skb_queue_empty(&priv->bypass_txq))
422 			return false;
423 	}
424 
425 	return true;
426 }
427 
428 /* Checks if WMM Tx queue is empty. */
nxpwifi_wmm_lists_empty(struct nxpwifi_adapter * adapter)429 bool nxpwifi_wmm_lists_empty(struct nxpwifi_adapter *adapter)
430 {
431 	int i;
432 	struct nxpwifi_private *priv;
433 
434 	for (i = 0; i < adapter->priv_num; ++i) {
435 		priv = adapter->priv[i];
436 		if (!priv->port_open)
437 			continue;
438 		if (atomic_read(&priv->wmm.tx_pkts_queued))
439 			return false;
440 	}
441 
442 	return true;
443 }
444 
445 /* Deletes all packets in an RA list node. */
446 static void
nxpwifi_wmm_del_pkts_in_ralist_node(struct nxpwifi_private * priv,struct nxpwifi_ra_list_tbl * ra_list)447 nxpwifi_wmm_del_pkts_in_ralist_node(struct nxpwifi_private *priv,
448 				    struct nxpwifi_ra_list_tbl *ra_list)
449 {
450 	struct nxpwifi_adapter *adapter = priv->adapter;
451 	struct sk_buff *skb, *tmp;
452 
453 	skb_queue_walk_safe(&ra_list->skb_head, skb, tmp) {
454 		skb_unlink(skb, &ra_list->skb_head);
455 		nxpwifi_write_data_complete(adapter, skb, 0, -1);
456 	}
457 }
458 
459 /* Deletes all packets in an RA list. */
460 static void
nxpwifi_wmm_del_pkts_in_ralist(struct nxpwifi_private * priv,struct list_head * ra_list_head)461 nxpwifi_wmm_del_pkts_in_ralist(struct nxpwifi_private *priv,
462 			       struct list_head *ra_list_head)
463 {
464 	struct nxpwifi_ra_list_tbl *ra_list;
465 
466 	list_for_each_entry(ra_list, ra_list_head, list)
467 		nxpwifi_wmm_del_pkts_in_ralist_node(priv, ra_list);
468 }
469 
470 /* Deletes all packets in all RA lists. */
nxpwifi_wmm_cleanup_queues(struct nxpwifi_private * priv)471 static void nxpwifi_wmm_cleanup_queues(struct nxpwifi_private *priv)
472 {
473 	int i;
474 
475 	for (i = 0; i < MAX_NUM_TID; i++)
476 		nxpwifi_wmm_del_pkts_in_ralist
477 		(priv, &priv->wmm.tid_tbl_ptr[i].ra_list);
478 
479 	atomic_set(&priv->wmm.tx_pkts_queued, 0);
480 	atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
481 }
482 
483 /* Deletes all route addresses from all RA lists. */
nxpwifi_wmm_delete_all_ralist(struct nxpwifi_private * priv)484 static void nxpwifi_wmm_delete_all_ralist(struct nxpwifi_private *priv)
485 {
486 	struct nxpwifi_ra_list_tbl *ra_list, *tmp_node;
487 	int i;
488 
489 	for (i = 0; i < MAX_NUM_TID; ++i) {
490 		nxpwifi_dbg(priv->adapter, INFO,
491 			    "info: ra_list: freeing buf for tid %d\n", i);
492 		list_for_each_entry_safe(ra_list, tmp_node,
493 					 &priv->wmm.tid_tbl_ptr[i].ra_list,
494 					 list) {
495 			list_del(&ra_list->list);
496 			kfree(ra_list);
497 		}
498 
499 		INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[i].ra_list);
500 	}
501 }
502 
nxpwifi_free_ack_frame(int id,void * p,void * data)503 static int nxpwifi_free_ack_frame(int id, void *p, void *data)
504 {
505 	pr_warn("Have pending ack frames!\n");
506 	kfree_skb(p);
507 	return 0;
508 }
509 
510 /* Cleans up the Tx and Rx queues. */
511 void
nxpwifi_clean_txrx(struct nxpwifi_private * priv)512 nxpwifi_clean_txrx(struct nxpwifi_private *priv)
513 {
514 	struct sk_buff *skb, *tmp;
515 	unsigned long index;
516 	void *entry;
517 
518 	nxpwifi_11n_cleanup_reorder_tbl(priv);
519 	spin_lock_bh(&priv->wmm.ra_list_spinlock);
520 
521 	nxpwifi_wmm_cleanup_queues(priv);
522 	nxpwifi_11n_delete_all_tx_ba_stream_tbl(priv);
523 
524 	if (priv->adapter->if_ops.cleanup_mpa_buf)
525 		priv->adapter->if_ops.cleanup_mpa_buf(priv->adapter);
526 
527 	nxpwifi_wmm_delete_all_ralist(priv);
528 	memcpy(tos_to_tid, ac_to_tid, sizeof(tos_to_tid));
529 
530 	spin_unlock_bh(&priv->wmm.ra_list_spinlock);
531 
532 	skb_queue_walk_safe(&priv->bypass_txq, skb, tmp) {
533 		skb_unlink(skb, &priv->bypass_txq);
534 		nxpwifi_write_data_complete(priv->adapter, skb, 0, -1);
535 	}
536 	atomic_set(&priv->adapter->bypass_tx_pending, 0);
537 
538 	xa_for_each(&priv->ack_status_frames, index, entry) {
539 		nxpwifi_free_ack_frame(index, entry, NULL);
540 		xa_erase(&priv->ack_status_frames, index);
541 	}
542 
543 	xa_destroy(&priv->ack_status_frames);
544 }
545 
546 /* Retrieves a particular RA list node, matching with the given TID and RA address. */
547 struct nxpwifi_ra_list_tbl *
nxpwifi_wmm_get_ralist_node(struct nxpwifi_private * priv,u8 tid,const u8 * ra_addr)548 nxpwifi_wmm_get_ralist_node(struct nxpwifi_private *priv, u8 tid,
549 			    const u8 *ra_addr)
550 {
551 	struct nxpwifi_ra_list_tbl *ra_list;
552 
553 	list_for_each_entry(ra_list, &priv->wmm.tid_tbl_ptr[tid].ra_list,
554 			    list) {
555 		if (!memcmp(ra_list->ra, ra_addr, ETH_ALEN))
556 			return ra_list;
557 	}
558 
559 	return NULL;
560 }
561 
nxpwifi_update_ralist_tx_pause(struct nxpwifi_private * priv,u8 * mac,u8 tx_pause)562 void nxpwifi_update_ralist_tx_pause(struct nxpwifi_private *priv, u8 *mac,
563 				    u8 tx_pause)
564 {
565 	struct nxpwifi_ra_list_tbl *ra_list;
566 	u32 pkt_cnt = 0, tx_pkts_queued;
567 	int i;
568 
569 	spin_lock_bh(&priv->wmm.ra_list_spinlock);
570 
571 	for (i = 0; i < MAX_NUM_TID; ++i) {
572 		ra_list = nxpwifi_wmm_get_ralist_node(priv, i, mac);
573 		if (ra_list && ra_list->tx_paused != tx_pause) {
574 			pkt_cnt += ra_list->total_pkt_count;
575 			ra_list->tx_paused = tx_pause;
576 			if (tx_pause)
577 				priv->wmm.pkts_paused[i] +=
578 					ra_list->total_pkt_count;
579 			else
580 				priv->wmm.pkts_paused[i] -=
581 					ra_list->total_pkt_count;
582 		}
583 	}
584 
585 	if (pkt_cnt) {
586 		tx_pkts_queued = atomic_read(&priv->wmm.tx_pkts_queued);
587 		if (tx_pause)
588 			tx_pkts_queued -= pkt_cnt;
589 		else
590 			tx_pkts_queued += pkt_cnt;
591 
592 		atomic_set(&priv->wmm.tx_pkts_queued, tx_pkts_queued);
593 		atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
594 	}
595 	spin_unlock_bh(&priv->wmm.ra_list_spinlock);
596 }
597 
598 /* Retrieves an RA list node for a given TID and RA address pair. */
599 struct nxpwifi_ra_list_tbl *
nxpwifi_wmm_get_queue_raptr(struct nxpwifi_private * priv,u8 tid,const u8 * ra_addr)600 nxpwifi_wmm_get_queue_raptr(struct nxpwifi_private *priv, u8 tid,
601 			    const u8 *ra_addr)
602 {
603 	struct nxpwifi_ra_list_tbl *ra_list;
604 
605 	ra_list = nxpwifi_wmm_get_ralist_node(priv, tid, ra_addr);
606 	if (ra_list)
607 		return ra_list;
608 	nxpwifi_ralist_add(priv, ra_addr);
609 
610 	return nxpwifi_wmm_get_ralist_node(priv, tid, ra_addr);
611 }
612 
613 /* Deletes RA list nodes for given mac for all TIDs. */
614 void
nxpwifi_wmm_del_peer_ra_list(struct nxpwifi_private * priv,const u8 * ra_addr)615 nxpwifi_wmm_del_peer_ra_list(struct nxpwifi_private *priv, const u8 *ra_addr)
616 {
617 	struct nxpwifi_ra_list_tbl *ra_list;
618 	int i;
619 
620 	spin_lock_bh(&priv->wmm.ra_list_spinlock);
621 
622 	for (i = 0; i < MAX_NUM_TID; ++i) {
623 		ra_list = nxpwifi_wmm_get_ralist_node(priv, i, ra_addr);
624 
625 		if (!ra_list)
626 			continue;
627 		nxpwifi_wmm_del_pkts_in_ralist_node(priv, ra_list);
628 		if (ra_list->tx_paused)
629 			priv->wmm.pkts_paused[i] -= ra_list->total_pkt_count;
630 		else
631 			atomic_sub(ra_list->total_pkt_count,
632 				   &priv->wmm.tx_pkts_queued);
633 		list_del(&ra_list->list);
634 		kfree(ra_list);
635 	}
636 	spin_unlock_bh(&priv->wmm.ra_list_spinlock);
637 }
638 
639 /* Checks if a particular RA list node exists in a given TID table index. */
nxpwifi_is_ralist_valid(struct nxpwifi_private * priv,struct nxpwifi_ra_list_tbl * ra_list,int ptr_index)640 bool nxpwifi_is_ralist_valid(struct nxpwifi_private *priv,
641 			     struct nxpwifi_ra_list_tbl *ra_list, int ptr_index)
642 {
643 	struct nxpwifi_ra_list_tbl *rlist;
644 
645 	list_for_each_entry(rlist, &priv->wmm.tid_tbl_ptr[ptr_index].ra_list,
646 			    list) {
647 		if (rlist == ra_list)
648 			return true;
649 	}
650 
651 	return false;
652 }
653 
654 /* Adds a packet to bypass TX queue. */
655 void
nxpwifi_wmm_add_buf_bypass_txqueue(struct nxpwifi_private * priv,struct sk_buff * skb)656 nxpwifi_wmm_add_buf_bypass_txqueue(struct nxpwifi_private *priv,
657 				   struct sk_buff *skb)
658 {
659 	skb_queue_tail(&priv->bypass_txq, skb);
660 }
661 
662 /* Adds a packet to WMM queue. */
663 void
nxpwifi_wmm_add_buf_txqueue(struct nxpwifi_private * priv,struct sk_buff * skb)664 nxpwifi_wmm_add_buf_txqueue(struct nxpwifi_private *priv,
665 			    struct sk_buff *skb)
666 {
667 	struct nxpwifi_adapter *adapter = priv->adapter;
668 	u32 tid;
669 	struct nxpwifi_ra_list_tbl *ra_list = NULL;
670 	struct list_head list_head;
671 	u8 ra[ETH_ALEN], tid_down;
672 	struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
673 
674 	memcpy(ra, eth_hdr->h_dest, ETH_ALEN);
675 
676 	if (!priv->media_connected && !nxpwifi_is_skb_mgmt_frame(skb)) {
677 		nxpwifi_dbg(adapter, DATA, "data: drop packet in disconnect\n");
678 		nxpwifi_write_data_complete(adapter, skb, 0, -1);
679 		return;
680 	}
681 
682 	tid = skb->priority;
683 
684 	spin_lock_bh(&priv->wmm.ra_list_spinlock);
685 
686 	tid_down = nxpwifi_wmm_downgrade_tid(priv, tid);
687 
688 	/*
689 	 * In case of infra as we have already created the list during
690 	 * association we just don't have to call get_queue_raptr, we will
691 	 * have only 1 raptr for a tid in case of infra
692 	 */
693 	if (!nxpwifi_queuing_ra_based(priv) &&
694 	    !nxpwifi_is_skb_mgmt_frame(skb)) {
695 		list_head = priv->wmm.tid_tbl_ptr[tid_down].ra_list;
696 		ra_list = list_first_entry_or_null(&list_head,
697 						   struct nxpwifi_ra_list_tbl,
698 						   list);
699 	} else {
700 		memcpy(ra, skb->data, ETH_ALEN);
701 		if (is_multicast_ether_addr(ra) ||
702 		    nxpwifi_is_skb_mgmt_frame(skb))
703 			eth_broadcast_addr(ra);
704 		ra_list = nxpwifi_wmm_get_queue_raptr(priv, tid_down, ra);
705 	}
706 
707 	if (!ra_list) {
708 		spin_unlock_bh(&priv->wmm.ra_list_spinlock);
709 		nxpwifi_write_data_complete(adapter, skb, 0, -1);
710 		return;
711 	}
712 
713 	skb_queue_tail(&ra_list->skb_head, skb);
714 
715 	ra_list->ba_pkt_count++;
716 	ra_list->total_pkt_count++;
717 
718 	if (atomic_read(&priv->wmm.highest_queued_prio) <
719 	    priv->tos_to_tid_inv[tid_down])
720 		atomic_set(&priv->wmm.highest_queued_prio,
721 			   priv->tos_to_tid_inv[tid_down]);
722 
723 	if (ra_list->tx_paused)
724 		priv->wmm.pkts_paused[tid_down]++;
725 	else
726 		atomic_inc(&priv->wmm.tx_pkts_queued);
727 
728 	spin_unlock_bh(&priv->wmm.ra_list_spinlock);
729 }
730 
731 /* Processes the get WMM status command response from firmware. */
nxpwifi_ret_wmm_get_status(struct nxpwifi_private * priv,const struct host_cmd_ds_command * resp)732 int nxpwifi_ret_wmm_get_status(struct nxpwifi_private *priv,
733 			       const struct host_cmd_ds_command *resp)
734 {
735 	u8 *curr;
736 	u16 resp_len = le16_to_cpu(resp->size), tlv_len;
737 	bool valid = true;
738 
739 	struct nxpwifi_ie_types_data *tlv_hdr;
740 	struct nxpwifi_ie_types_wmm_queue_status *wmm_qs;
741 	struct ieee80211_wmm_param_ie *wmm_param_ie = NULL;
742 	struct nxpwifi_wmm_ac_status *ac_status;
743 	u32 base;
744 
745 	nxpwifi_dbg(priv->adapter, INFO,
746 		    "info: WMM: WMM_GET_STATUS cmdresp received: %d\n",
747 		    resp_len);
748 
749 	base = offsetofend(struct host_cmd_ds_command, params.get_wmm_status);
750 
751 	if (resp_len < base)
752 		return -EINVAL;
753 
754 	curr = (u8 *)&resp->params.get_wmm_status;
755 	resp_len -= base;
756 
757 	while (resp_len >= sizeof(tlv_hdr->header) && valid) {
758 		tlv_hdr = (struct nxpwifi_ie_types_data *)curr;
759 		tlv_len = le16_to_cpu(tlv_hdr->header.len);
760 
761 		if (resp_len < tlv_len + sizeof(tlv_hdr->header))
762 			break;
763 
764 		switch (le16_to_cpu(tlv_hdr->header.type)) {
765 		case TLV_TYPE_WMMQSTATUS:
766 			if (tlv_len <
767 			    sizeof(struct nxpwifi_ie_types_wmm_queue_status) -
768 				sizeof(tlv_hdr->header))
769 				break;
770 
771 			wmm_qs =
772 			    (struct nxpwifi_ie_types_wmm_queue_status *)tlv_hdr;
773 
774 			if (wmm_qs->queue_index >= IEEE80211_NUM_ACS)
775 				break;
776 
777 			ac_status = &priv->wmm.ac_status[wmm_qs->queue_index];
778 			ac_status->disabled = wmm_qs->disabled;
779 			ac_status->flow_required = wmm_qs->flow_required;
780 			ac_status->flow_created = wmm_qs->flow_created;
781 			break;
782 
783 		case WLAN_EID_VENDOR_SPECIFIC:
784 			/* Need at least OUI(4) + WMM fixed fields */
785 			if (tlv_len + sizeof(tlv_hdr->header) <
786 			    offsetofend(struct ieee80211_wmm_param_ie,
787 					qos_info))
788 				break;
789 
790 			wmm_param_ie =
791 			    (struct ieee80211_wmm_param_ie *)(curr + 2);
792 
793 			if (tlv_len + 2 > sizeof(struct ieee80211_wmm_param_ie))
794 				break;
795 
796 			wmm_param_ie->len = (u8)tlv_len;
797 			wmm_param_ie->element_id = WLAN_EID_VENDOR_SPECIFIC;
798 
799 			memcpy(&priv->curr_bss_params.bss_descriptor.wmm_ie,
800 			       wmm_param_ie, wmm_param_ie->len + 2);
801 			break;
802 
803 		default:
804 			valid = false;
805 			break;
806 		}
807 
808 		curr += sizeof(tlv_hdr->header) + tlv_len;
809 		resp_len -= sizeof(tlv_hdr->header) + tlv_len;
810 	}
811 
812 	nxpwifi_wmm_setup_queue_priorities(priv, wmm_param_ie);
813 	nxpwifi_wmm_setup_ac_downgrade(priv);
814 
815 	return 0;
816 }
817 
818 /*
819  * Callback handler from the command module to allow insertion of a WMM TLV.
820  *
821  * If the BSS we are associating to supports WMM, this function adds the
822  * required WMM Information element to the association request command buffer in
823  * the form of a NXP extended IEEE element.
824  */
825 u32
nxpwifi_wmm_process_association_req(struct nxpwifi_private * priv,u8 ** assoc_buf,struct ieee80211_wmm_param_ie * wmm_ie,struct ieee80211_ht_cap * ht_cap)826 nxpwifi_wmm_process_association_req(struct nxpwifi_private *priv,
827 				    u8 **assoc_buf,
828 				    struct ieee80211_wmm_param_ie *wmm_ie,
829 				    struct ieee80211_ht_cap *ht_cap)
830 {
831 	struct nxpwifi_ie_types_wmm_param_set *wmm_tlv;
832 	u32 ret_len = 0;
833 
834 	/* Null checks */
835 	if (!assoc_buf)
836 		return 0;
837 	if (!(*assoc_buf))
838 		return 0;
839 
840 	if (!wmm_ie)
841 		return 0;
842 
843 	nxpwifi_dbg(priv->adapter, INFO,
844 		    "info: WMM: process assoc req: bss->wmm_ie=%#x\n",
845 		    wmm_ie->element_id);
846 
847 	if ((priv->wmm_required ||
848 	     (ht_cap && (priv->config_bands & BAND_GN ||
849 	     priv->config_bands & BAND_AN))) &&
850 	    wmm_ie->element_id == WLAN_EID_VENDOR_SPECIFIC) {
851 		wmm_tlv = (struct nxpwifi_ie_types_wmm_param_set *)*assoc_buf;
852 		wmm_tlv->header.type = cpu_to_le16((u16)wmm_info_ie[0]);
853 		wmm_tlv->header.len = cpu_to_le16((u16)wmm_info_ie[1]);
854 		memcpy(wmm_tlv->wmm_ie, &wmm_info_ie[2],
855 		       le16_to_cpu(wmm_tlv->header.len));
856 		if (wmm_ie->qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD)
857 			memcpy((u8 *)(wmm_tlv->wmm_ie
858 				      + le16_to_cpu(wmm_tlv->header.len)
859 				      - sizeof(priv->wmm_qosinfo)),
860 			       &priv->wmm_qosinfo, sizeof(priv->wmm_qosinfo));
861 
862 		ret_len = sizeof(wmm_tlv->header)
863 			  + le16_to_cpu(wmm_tlv->header.len);
864 
865 		*assoc_buf += ret_len;
866 	}
867 
868 	return ret_len;
869 }
870 
871 /* Computes the time delay in the driver queues for a given packet. */
872 u8
nxpwifi_wmm_compute_drv_pkt_delay(struct nxpwifi_private * priv,const struct sk_buff * skb)873 nxpwifi_wmm_compute_drv_pkt_delay(struct nxpwifi_private *priv,
874 				  const struct sk_buff *skb)
875 {
876 	u32 queue_delay = ktime_to_ms(net_timedelta(skb->tstamp));
877 	u8 ret_val;
878 
879 	/*
880 	 * Queue delay is passed as a uint8 in units of 2ms (ms shifted
881 	 *  by 1). Min value (other than 0) is therefore 2ms, max is 510ms.
882 	 *
883 	 * Pass max value if queue_delay is beyond the uint8 range
884 	 */
885 	ret_val = (u8)(min(queue_delay, priv->wmm.drv_pkt_delay_max) >> 1);
886 
887 	nxpwifi_dbg(priv->adapter, DATA, "data: WMM: Pkt Delay: %d ms,\t"
888 		    "%d ms sent to FW\n", queue_delay, ret_val);
889 
890 	return ret_val;
891 }
892 
893 /* Retrieves the highest priority RA list table pointer. */
894 static struct nxpwifi_ra_list_tbl *
nxpwifi_wmm_get_highest_priolist_ptr(struct nxpwifi_adapter * adapter,struct nxpwifi_private ** priv,int * tid)895 nxpwifi_wmm_get_highest_priolist_ptr(struct nxpwifi_adapter *adapter,
896 				     struct nxpwifi_private **priv, int *tid)
897 {
898 	struct nxpwifi_private *priv_tmp;
899 	struct nxpwifi_ra_list_tbl *ptr;
900 	struct nxpwifi_tid_tbl *tid_ptr;
901 	atomic_t *hqp;
902 	int i, j;
903 	u8 to_tid;
904 
905 	/* check the BSS with highest priority first */
906 	for (j = adapter->priv_num - 1; j >= 0; --j) {
907 		/* iterate over BSS with the equal priority */
908 		list_for_each_entry(adapter->bss_prio_tbl[j].bss_prio_cur,
909 				    &adapter->bss_prio_tbl[j].bss_prio_head,
910 				    list) {
911 try_again:
912 			priv_tmp = adapter->bss_prio_tbl[j].bss_prio_cur->priv;
913 
914 			if (!priv_tmp->port_open ||
915 			    (atomic_read(&priv_tmp->wmm.tx_pkts_queued) == 0))
916 				continue;
917 
918 			/* iterate over the WMM queues of the BSS */
919 			hqp = &priv_tmp->wmm.highest_queued_prio;
920 			for (i = atomic_read(hqp); i >= LOW_PRIO_TID; --i) {
921 				spin_lock_bh(&priv_tmp->wmm.ra_list_spinlock);
922 
923 				to_tid = tos_to_tid[i];
924 				tid_ptr = &(priv_tmp)->wmm.tid_tbl_ptr[to_tid];
925 
926 				/* iterate over receiver addresses */
927 				list_for_each_entry(ptr, &tid_ptr->ra_list,
928 						    list) {
929 					if (!ptr->tx_paused &&
930 					    !skb_queue_empty(&ptr->skb_head))
931 						/* holds both locks */
932 						goto found;
933 				}
934 
935 				spin_unlock_bh(&priv_tmp->wmm.ra_list_spinlock);
936 			}
937 
938 			if (atomic_read(&priv_tmp->wmm.tx_pkts_queued) != 0) {
939 				atomic_set(&priv_tmp->wmm.highest_queued_prio,
940 					   HIGH_PRIO_TID);
941 				/*
942 				 * Iterate current private once more, since
943 				 * there still exist packets in data queue
944 				 */
945 				goto try_again;
946 			} else {
947 				atomic_set(&priv_tmp->wmm.highest_queued_prio,
948 					   NO_PKT_PRIO_TID);
949 			}
950 		}
951 	}
952 
953 	return NULL;
954 
955 found:
956 	/* holds ra_list_spinlock */
957 	if (atomic_read(hqp) > i)
958 		atomic_set(hqp, i);
959 	spin_unlock_bh(&priv_tmp->wmm.ra_list_spinlock);
960 
961 	*priv = priv_tmp;
962 	*tid = tos_to_tid[i];
963 
964 	return ptr;
965 }
966 
967 /* Rotates ra and bss lists so packets are picked round robin. */
nxpwifi_rotate_priolists(struct nxpwifi_private * priv,struct nxpwifi_ra_list_tbl * ra,int tid)968 void nxpwifi_rotate_priolists(struct nxpwifi_private *priv,
969 			      struct nxpwifi_ra_list_tbl *ra,
970 			      int tid)
971 {
972 	struct nxpwifi_adapter *adapter = priv->adapter;
973 	struct nxpwifi_bss_prio_tbl *tbl = adapter->bss_prio_tbl;
974 	struct nxpwifi_tid_tbl *tid_ptr = &priv->wmm.tid_tbl_ptr[tid];
975 
976 	spin_lock_bh(&tbl[priv->bss_priority].bss_prio_lock);
977 	/*
978 	 * dirty trick: we remove 'head' temporarily and reinsert it after
979 	 * curr bss node. imagine list to stay fixed while head is moved
980 	 */
981 	list_move(&tbl[priv->bss_priority].bss_prio_head,
982 		  &tbl[priv->bss_priority].bss_prio_cur->list);
983 	spin_unlock_bh(&tbl[priv->bss_priority].bss_prio_lock);
984 
985 	spin_lock_bh(&priv->wmm.ra_list_spinlock);
986 	if (nxpwifi_is_ralist_valid(priv, ra, tid)) {
987 		priv->wmm.packets_out[tid]++;
988 		/* same as above */
989 		list_move(&tid_ptr->ra_list, &ra->list);
990 	}
991 	spin_unlock_bh(&priv->wmm.ra_list_spinlock);
992 }
993 
994 /* Checks if 11n aggregation is possible. */
995 static bool
nxpwifi_is_11n_aggragation_possible(struct nxpwifi_private * priv,struct nxpwifi_ra_list_tbl * ptr,int max_buf_size)996 nxpwifi_is_11n_aggragation_possible(struct nxpwifi_private *priv,
997 				    struct nxpwifi_ra_list_tbl *ptr,
998 				    int max_buf_size)
999 {
1000 	int count = 0, total_size = 0;
1001 	struct sk_buff *skb, *tmp;
1002 	int max_amsdu_size;
1003 
1004 	if (priv->bss_role == NXPWIFI_BSS_ROLE_UAP && priv->ap_11n_enabled &&
1005 	    ptr->is_11n_enabled)
1006 		max_amsdu_size = min_t(int, ptr->max_amsdu, max_buf_size);
1007 	else
1008 		max_amsdu_size = max_buf_size;
1009 
1010 	skb_queue_walk_safe(&ptr->skb_head, skb, tmp) {
1011 		total_size += skb->len;
1012 		if (total_size >= max_amsdu_size)
1013 			break;
1014 		if (++count >= MIN_NUM_AMSDU)
1015 			return true;
1016 	}
1017 
1018 	return false;
1019 }
1020 
1021 /* Sends a single packet to firmware for transmission. */
1022 static void
nxpwifi_send_single_packet(struct nxpwifi_private * priv,struct nxpwifi_ra_list_tbl * ptr,int ptr_index)1023 nxpwifi_send_single_packet(struct nxpwifi_private *priv,
1024 			   struct nxpwifi_ra_list_tbl *ptr, int ptr_index)
1025 __releases(&priv->wmm.ra_list_spinlock)
1026 {
1027 	struct sk_buff *skb, *skb_next;
1028 	struct nxpwifi_tx_param tx_param;
1029 	struct nxpwifi_adapter *adapter = priv->adapter;
1030 	struct nxpwifi_txinfo *tx_info;
1031 
1032 	if (skb_queue_empty(&ptr->skb_head)) {
1033 		spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1034 		nxpwifi_dbg(adapter, DATA, "data: nothing to send\n");
1035 		return;
1036 	}
1037 
1038 	skb = skb_dequeue(&ptr->skb_head);
1039 
1040 	tx_info = NXPWIFI_SKB_TXCB(skb);
1041 	nxpwifi_dbg(adapter, DATA,
1042 		    "data: dequeuing the packet %p %p\n", ptr, skb);
1043 
1044 	ptr->total_pkt_count--;
1045 
1046 	if (!skb_queue_empty(&ptr->skb_head))
1047 		skb_next = skb_peek(&ptr->skb_head);
1048 	else
1049 		skb_next = NULL;
1050 
1051 	spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1052 
1053 	tx_param.next_pkt_len = ((skb_next) ? skb_next->len +
1054 				sizeof(struct txpd) : 0);
1055 
1056 	if (nxpwifi_process_tx(priv, skb, &tx_param) == -EBUSY) {
1057 		/* Queue the packet back at the head */
1058 		spin_lock_bh(&priv->wmm.ra_list_spinlock);
1059 
1060 		if (!nxpwifi_is_ralist_valid(priv, ptr, ptr_index)) {
1061 			spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1062 			nxpwifi_write_data_complete(adapter, skb, 0, -1);
1063 			return;
1064 		}
1065 
1066 		skb_queue_tail(&ptr->skb_head, skb);
1067 
1068 		ptr->total_pkt_count++;
1069 		ptr->ba_pkt_count++;
1070 		tx_info->flags |= NXPWIFI_BUF_FLAG_REQUEUED_PKT;
1071 		spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1072 	} else {
1073 		nxpwifi_rotate_priolists(priv, ptr, ptr_index);
1074 		atomic_dec(&priv->wmm.tx_pkts_queued);
1075 	}
1076 }
1077 
1078 /* Checks if the first packet in the given RA list is already processed or not. */
1079 static bool
nxpwifi_is_ptr_processed(struct nxpwifi_private * priv,struct nxpwifi_ra_list_tbl * ptr)1080 nxpwifi_is_ptr_processed(struct nxpwifi_private *priv,
1081 			 struct nxpwifi_ra_list_tbl *ptr)
1082 {
1083 	struct sk_buff *skb;
1084 	struct nxpwifi_txinfo *tx_info;
1085 
1086 	if (skb_queue_empty(&ptr->skb_head))
1087 		return false;
1088 
1089 	skb = skb_peek(&ptr->skb_head);
1090 
1091 	tx_info = NXPWIFI_SKB_TXCB(skb);
1092 	if (tx_info->flags & NXPWIFI_BUF_FLAG_REQUEUED_PKT)
1093 		return true;
1094 
1095 	return false;
1096 }
1097 
1098 /* Sends a single processed packet to firmware for transmission. */
1099 static void
nxpwifi_send_processed_packet(struct nxpwifi_private * priv,struct nxpwifi_ra_list_tbl * ptr,int ptr_index)1100 nxpwifi_send_processed_packet(struct nxpwifi_private *priv,
1101 			      struct nxpwifi_ra_list_tbl *ptr, int ptr_index)
1102 				__releases(&priv->wmm.ra_list_spinlock)
1103 {
1104 	struct nxpwifi_tx_param tx_param;
1105 	struct nxpwifi_adapter *adapter = priv->adapter;
1106 	int ret;
1107 	struct sk_buff *skb, *skb_next;
1108 	struct nxpwifi_txinfo *tx_info;
1109 
1110 	if (skb_queue_empty(&ptr->skb_head)) {
1111 		spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1112 		return;
1113 	}
1114 
1115 	skb = skb_dequeue(&ptr->skb_head);
1116 
1117 	if (adapter->data_sent || adapter->tx_lock_flag) {
1118 		ptr->total_pkt_count--;
1119 		spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1120 		skb_queue_tail(&adapter->tx_data_q, skb);
1121 		atomic_dec(&priv->wmm.tx_pkts_queued);
1122 		atomic_inc(&adapter->tx_queued);
1123 		return;
1124 	}
1125 
1126 	if (!skb_queue_empty(&ptr->skb_head))
1127 		skb_next = skb_peek(&ptr->skb_head);
1128 	else
1129 		skb_next = NULL;
1130 
1131 	tx_info = NXPWIFI_SKB_TXCB(skb);
1132 
1133 	spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1134 
1135 	tx_param.next_pkt_len =
1136 		((skb_next) ? skb_next->len +
1137 		 sizeof(struct txpd) : 0);
1138 
1139 	ret = adapter->if_ops.host_to_card(adapter, NXPWIFI_TYPE_DATA,
1140 					   skb, &tx_param);
1141 
1142 	switch (ret) {
1143 	case -EBUSY:
1144 		nxpwifi_dbg(adapter, ERROR, "data: -EBUSY is returned\n");
1145 		spin_lock_bh(&priv->wmm.ra_list_spinlock);
1146 
1147 		if (!nxpwifi_is_ralist_valid(priv, ptr, ptr_index)) {
1148 			spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1149 			nxpwifi_write_data_complete(adapter, skb, 0, -1);
1150 			return;
1151 		}
1152 
1153 		skb_queue_tail(&ptr->skb_head, skb);
1154 
1155 		tx_info->flags |= NXPWIFI_BUF_FLAG_REQUEUED_PKT;
1156 		spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1157 		break;
1158 	case -EINPROGRESS:
1159 		break;
1160 	case 0:
1161 		nxpwifi_write_data_complete(adapter, skb, 0, ret);
1162 		break;
1163 	default:
1164 		nxpwifi_dbg(adapter, ERROR, "host_to_card failed: %#x\n", ret);
1165 		adapter->dbg.num_tx_host_to_card_failure++;
1166 		nxpwifi_write_data_complete(adapter, skb, 0, ret);
1167 		break;
1168 	}
1169 
1170 	if (ret != -EBUSY) {
1171 		nxpwifi_rotate_priolists(priv, ptr, ptr_index);
1172 		atomic_dec(&priv->wmm.tx_pkts_queued);
1173 		spin_lock_bh(&priv->wmm.ra_list_spinlock);
1174 		ptr->total_pkt_count--;
1175 		spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1176 	}
1177 }
1178 
1179 /* Dequeues a packet from the highest priority list and transmits it. */
1180 static int
nxpwifi_dequeue_tx_packet(struct nxpwifi_adapter * adapter)1181 nxpwifi_dequeue_tx_packet(struct nxpwifi_adapter *adapter)
1182 {
1183 	struct nxpwifi_ra_list_tbl *ptr;
1184 	struct nxpwifi_private *priv = NULL;
1185 	int ptr_index = 0;
1186 	u8 ra[ETH_ALEN];
1187 	int tid_del = 0, tid = 0;
1188 
1189 	ptr = nxpwifi_wmm_get_highest_priolist_ptr(adapter, &priv, &ptr_index);
1190 	if (!ptr)
1191 		return -ENOENT;
1192 
1193 	tid = nxpwifi_get_tid(ptr);
1194 
1195 	nxpwifi_dbg(adapter, DATA, "data: tid=%d\n", tid);
1196 
1197 	spin_lock_bh(&priv->wmm.ra_list_spinlock);
1198 	if (!nxpwifi_is_ralist_valid(priv, ptr, ptr_index)) {
1199 		spin_unlock_bh(&priv->wmm.ra_list_spinlock);
1200 		return -EINVAL;
1201 	}
1202 
1203 	if (nxpwifi_is_ptr_processed(priv, ptr)) {
1204 		nxpwifi_send_processed_packet(priv, ptr, ptr_index);
1205 		/*
1206 		 * ra_list_spinlock has been freed in
1207 		 * nxpwifi_send_processed_packet()
1208 		 */
1209 		return 0;
1210 	}
1211 
1212 	if (!ptr->is_11n_enabled ||
1213 	    ptr->ba_status ||
1214 	    priv->wps.session_enable) {
1215 		if (ptr->is_11n_enabled &&
1216 		    ptr->ba_status &&
1217 		    ptr->amsdu_in_ampdu &&
1218 		    nxpwifi_is_amsdu_allowed(priv, tid) &&
1219 		    nxpwifi_is_11n_aggragation_possible(priv, ptr,
1220 							adapter->tx_buf_size))
1221 			nxpwifi_11n_aggregate_pkt(priv, ptr, ptr_index);
1222 			/*
1223 			 * ra_list_spinlock has been freed in
1224 			 * nxpwifi_11n_aggregate_pkt()
1225 			 */
1226 		else
1227 			nxpwifi_send_single_packet(priv, ptr, ptr_index);
1228 			/*
1229 			 * ra_list_spinlock has been freed in
1230 			 * nxpwifi_send_single_packet()
1231 			 */
1232 	} else {
1233 		if (nxpwifi_is_ampdu_allowed(priv, ptr, tid) &&
1234 		    ptr->ba_pkt_count > ptr->ba_packet_thr) {
1235 			if (nxpwifi_space_avail_for_new_ba_stream(adapter)) {
1236 				nxpwifi_create_ba_tbl(priv, ptr->ra, tid,
1237 						      BA_SETUP_INPROGRESS);
1238 				nxpwifi_send_addba(priv, tid, ptr->ra);
1239 			} else if (nxpwifi_find_stream_to_delete
1240 				   (priv, tid, &tid_del, ra)) {
1241 				nxpwifi_create_ba_tbl(priv, ptr->ra, tid,
1242 						      BA_SETUP_INPROGRESS);
1243 				nxpwifi_send_delba(priv, tid_del, ra, 1);
1244 			}
1245 		}
1246 		if (nxpwifi_is_amsdu_allowed(priv, tid) &&
1247 		    nxpwifi_is_11n_aggragation_possible(priv, ptr,
1248 							adapter->tx_buf_size))
1249 			nxpwifi_11n_aggregate_pkt(priv, ptr, ptr_index);
1250 			/*
1251 			 * ra_list_spinlock has been freed in
1252 			 * nxpwifi_11n_aggregate_pkt()
1253 			 */
1254 		else
1255 			nxpwifi_send_single_packet(priv, ptr, ptr_index);
1256 			/*
1257 			 * ra_list_spinlock has been freed in
1258 			 * nxpwifi_send_single_packet()
1259 			 */
1260 	}
1261 	return 0;
1262 }
1263 
nxpwifi_process_bypass_tx(struct nxpwifi_adapter * adapter)1264 void nxpwifi_process_bypass_tx(struct nxpwifi_adapter *adapter)
1265 {
1266 	struct nxpwifi_tx_param tx_param;
1267 	struct sk_buff *skb;
1268 	struct nxpwifi_txinfo *tx_info;
1269 	struct nxpwifi_private *priv;
1270 	int i;
1271 
1272 	if (adapter->data_sent || adapter->tx_lock_flag)
1273 		return;
1274 
1275 	for (i = 0; i < adapter->priv_num; ++i) {
1276 		priv = adapter->priv[i];
1277 
1278 		if (skb_queue_empty(&priv->bypass_txq))
1279 			continue;
1280 
1281 		skb = skb_dequeue(&priv->bypass_txq);
1282 		tx_info = NXPWIFI_SKB_TXCB(skb);
1283 
1284 		/* no aggregation for bypass packets */
1285 		tx_param.next_pkt_len = 0;
1286 
1287 		if (nxpwifi_process_tx(priv, skb, &tx_param) == -EBUSY) {
1288 			skb_queue_head(&priv->bypass_txq, skb);
1289 			tx_info->flags |= NXPWIFI_BUF_FLAG_REQUEUED_PKT;
1290 		} else {
1291 			atomic_dec(&adapter->bypass_tx_pending);
1292 		}
1293 	}
1294 }
1295 
1296 /* Transmits the highest priority packet awaiting in the WMM Queues. */
1297 void
nxpwifi_wmm_process_tx(struct nxpwifi_adapter * adapter)1298 nxpwifi_wmm_process_tx(struct nxpwifi_adapter *adapter)
1299 {
1300 	do {
1301 		if (nxpwifi_dequeue_tx_packet(adapter))
1302 			break;
1303 		if (adapter->iface_type != NXPWIFI_SDIO) {
1304 			if (adapter->data_sent ||
1305 			    adapter->tx_lock_flag)
1306 				break;
1307 		} else {
1308 			if (atomic_read(&adapter->tx_queued) >=
1309 			    NXPWIFI_MAX_PKTS_TXQ)
1310 				break;
1311 		}
1312 	} while (!nxpwifi_wmm_lists_empty(adapter));
1313 }
1314 
nxpwifi_wmm_init_tos_to_tid_inv(struct nxpwifi_private * priv)1315 void nxpwifi_wmm_init_tos_to_tid_inv(struct nxpwifi_private *priv)
1316 {
1317 	memcpy(priv->tos_to_tid_inv, tos_to_tid_inv, sizeof(priv->tos_to_tid_inv));
1318 }
1319