• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Datapath implementation for XRadio drivers
3  *
4  * Copyright (c) 2013
5  * Xradio Technology Co., Ltd. <www.xradiotech.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <net/mac80211.h>
13 #include <linux/etherdevice.h>
14 #include <linux/skbuff.h>
15 
16 #include "xradio.h"
17 #include "wsm.h"
18 #include "bh.h"
19 #include "ap.h"
20 #include "sta.h"
21 #include "sbus.h"
22 
23 #ifndef SUPPORT_HT40
24 
25 #define B_RATE_INDEX   0     /* 11b rate for important short frames in 2.4G. */
26 #define AG_RATE_INDEX  6     /* 11a/g rate for important short frames in 5G. */
27 #define XRADIO_INVALID_RATE_ID (0xFF)
28 
29 #endif
30 
31 /* rate should fall quickly to avoid dropping frames by aps.*/
32 #ifdef ENHANCE_ANTI_INTERFERE
33 #define HIGH_RATE_MAX_RETRY  9
34 #else
35 #define HIGH_RATE_MAX_RETRY  7
36 #endif
37 
38 #ifdef CONFIG_XRADIO_TESTMODE
39 #include "nl80211_testmode_msg_copy.h"
40 #endif /* CONFIG_XRADIO_TESTMODE */
41 #ifdef TES_P2P_0002_ROC_RESTART
42 #include <linux/time.h>
43 #endif
44 static const struct ieee80211_rate *xradio_get_tx_rate(
45 							const struct xradio_common *hw_priv,
46 							const struct ieee80211_tx_rate *rate);
47 
48 #ifdef SUPPORT_HT40
49 
50 u32 TxedHtofdmRateMap[4][8] = { {0x0} };
51 u32 TxedLegacyRateMap[2][8] = { {0x0} };
52 
53 u32 RxedHtofdmRateMap[4][8] = { {0x0} };
54 u32 RxedLegacyRateMap[2][8] = { {0x0} };
55 
56 u8 LegacyRxedRateLut[2][14] = {
57 	{
58 		0, 1, 2, 3, 0, 0, 4, 5, 6, 7, 8, 9, 10, 11
59 	},
60 	{
61 		0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7
62 	}
63 };
64 
65 #else
66 
67 u32 TxedRateIdx_Map[24] = { 0 };
68 u32 RxedRateIdx_Map[24] = { 0 };
69 
70 #endif
71 
72 /* ******************************************************************** */
73 /* TX policy cache implementation					*/
74 
tx_policy_dump(struct tx_policy * policy)75 static void tx_policy_dump(struct tx_policy *policy)
76 {
77 	txrx_printk(XRADIO_DBG_MSG, "[TX policy] "
78 		    "%.1X%.1X%.1X%.1X%.1X%.1X%.1X%.1X"
79 		    "%.1X%.1X%.1X%.1X%.1X%.1X%.1X%.1X"
80 		    "%.1X%.1X%.1X%.1X%.1X%.1X%.1X%.1X: %d\n",
81 		    policy->raw[0] & 0x0F, policy->raw[0] >> 4,
82 		    policy->raw[1] & 0x0F, policy->raw[1] >> 4,
83 		    policy->raw[2] & 0x0F, policy->raw[2] >> 4,
84 		    policy->raw[3] & 0x0F, policy->raw[3] >> 4,
85 		    policy->raw[4] & 0x0F, policy->raw[4] >> 4,
86 		    policy->raw[5] & 0x0F, policy->raw[5] >> 4,
87 		    policy->raw[6] & 0x0F, policy->raw[6] >> 4,
88 		    policy->raw[7] & 0x0F, policy->raw[7] >> 4,
89 		    policy->raw[8] & 0x0F, policy->raw[8] >> 4,
90 		    policy->raw[9] & 0x0F, policy->raw[9] >> 4,
91 		    policy->raw[10] & 0x0F, policy->raw[10] >> 4,
92 		    policy->raw[11] & 0x0F, policy->raw[11] >> 4,
93 		    policy->defined);
94 }
95 
xradio_check_go_neg_conf_success(struct xradio_common * hw_priv,u8 * action)96 static void xradio_check_go_neg_conf_success(struct xradio_common *hw_priv,
97 					     u8 *action)
98 {
99 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
100 	if (action[2] == 0x50 && action[3] == 0x6F && action[4] == 0x9A &&
101 	    action[5] == 0x09 && action[6] == 0x02) {
102 		if (action[17] == 0) {
103 			hw_priv->is_go_thru_go_neg = true;
104 		} else {
105 			hw_priv->is_go_thru_go_neg = false;
106 		}
107 	}
108 }
109 
110 #ifdef TES_P2P_0002_ROC_RESTART
111 /*
112  * TES_P2P_0002 WorkAround:
113  * P2P GO Neg Process and P2P FIND may be collision.
114  * When P2P Device is waiting for GO NEG CFM in 30ms,
115  * P2P FIND may end with p2p listen, and then goes to p2p search.
116  * Then xradio scan will occupy phy on other channel in 3+ seconds.
117  * P2P Device will not be able to receive the GO NEG CFM.
118  * We extend the roc period to remaind phy to receive
119  * GO NEG CFM as WorkAround.
120  */
121 
122 s32 TES_P2P_0002_roc_dur;
123 s32 TES_P2P_0002_roc_sec;
124 s32 TES_P2P_0002_roc_usec;
125 u32 TES_P2P_0002_packet_id;
126 u32 TES_P2P_0002_state = TES_P2P_0002_STATE_IDLE;
127 
xradio_frame_monitor(struct xradio_common * hw_priv,struct sk_buff * skb,bool tx)128 static void xradio_frame_monitor(struct xradio_common *hw_priv,
129 				 struct sk_buff *skb, bool tx)
130 {
131 	struct ieee80211_hdr *frame = (struct ieee80211_hdr *)skb->data;
132 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
133 
134 	u8 *action = (u8 *) &mgmt->u.action.category;
135 	u8 *category_code = &(action[0]);
136 	u8 *action_code = &(action[1]);
137 	u8 *oui = &(action[2]);
138 	u8 *subtype = &(action[5]);
139 	u8 *oui_subtype = &(action[6]);
140 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
141 
142 	if (ieee80211_is_action(frame->frame_control) &&
143 	   *category_code == WLAN_CATEGORY_PUBLIC &&
144 	   *action_code == 0x09) {
145 		if ((oui[0] == 0x50) && (oui[1] == 0x6F) &&
146 		   (oui[2] == 0x9A) && (*subtype == 0x09)) {
147 			/* w, GO Negotiation Response */
148 			if (*oui_subtype == 0x01) {
149 				if ((TES_P2P_0002_state == TES_P2P_0002_STATE_IDLE) &&
150 				   (tx == true)) { /* w, p2p atturbute:status, id=0 */
151 					u8 *go_neg_resp_res = &(action[17]);
152 					if (*go_neg_resp_res == 0x0) {
153 						TES_P2P_0002_state = TES_P2P_0002_STATE_SEND_RESP;
154 						txrx_printk(XRADIO_DBG_NIY,
155 							    "[ROC_RESTART_STATE_SEND_RESP]\n");
156 					}
157 				}
158 			/* w, GO Negotiation Confirmation */
159 			} else if (*oui_subtype == 0x02) {
160 				if (tx == false) {
161 					TES_P2P_0002_state = TES_P2P_0002_STATE_IDLE;
162 					txrx_printk(XRADIO_DBG_NIY, "[ROC_RESTART_STATE_IDLE]"
163 						    "[GO Negotiation Confirmation]\n");
164 				}
165 			/* w, Provision Discovery Response */
166 			} else if (*oui_subtype == 0x08) {
167 				if (tx == false) {
168 					TES_P2P_0002_state = TES_P2P_0002_STATE_IDLE;
169 					txrx_printk(XRADIO_DBG_NIY, "[ROC_RESTART_STATE_IDLE]"
170 						    "[Provision Discovery Response]\n");
171 				}
172 			}
173 		}
174 	}
175 }
176 #endif
177 
xradio_check_prov_desc_req(struct xradio_common * hw_priv,u8 * action)178 static void xradio_check_prov_desc_req(struct xradio_common *hw_priv,
179 						u8 *action)
180 {
181 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
182 	if (action[2] == 0x50 && action[3] == 0x6F && action[4] == 0x9A &&
183 	    action[5] == 0x09 && action[6] == 0x07) {
184 		hw_priv->is_go_thru_go_neg = false;
185 	}
186 }
187 
188 #ifdef AP_HT_COMPAT_FIX
189 #define AP_COMPAT_THRESHOLD  2000
190 #define AP_COMPAT_MIN_CNT    200
191 u8 ap_compat_bssid[ETH_ALEN] = { 0 };
192 
193 #ifdef SUPPORT_HT40
194 
xradio_apcompat_detect(struct xradio_vif * priv,u16 rxedRateEntry)195 static int xradio_apcompat_detect(struct xradio_vif *priv, u16 rxedRateEntry)
196 
197 #else
198 
199 static int xradio_apcompat_detect(struct xradio_vif *priv, u8 rx_rate)
200 
201 #endif
202 {
203 
204 #ifdef SUPPORT_HT40
205 
206 	if ((GET_RATE_ENTRY_MODEM(rxedRateEntry) == FW_RATE_MODEM_LEGACY)
207 		&& (GET_RATE_ENTRY_RATEINDEX(rxedRateEntry) < 6)) {
208 
209 #else
210 
211 	if (rx_rate < AG_RATE_INDEX) {
212 
213 #endif
214 		priv->ht_compat_cnt++;
215 
216 #ifdef SUPPORT_HT40
217 
218 		txrx_printk(XRADIO_DBG_MSG, "%s:rate=%d.\n",
219 			__func__, GET_RATE_ENTRY_RATEINDEX(rxedRateEntry));
220 
221 #else
222 
223 		txrx_printk(XRADIO_DBG_MSG, "%s:rate=%d.\n", __func__, rx_rate);
224 
225 #endif
226 
227 	} else {
228 		priv->ht_compat_det |= 1;
229 		priv->ht_compat_cnt = 0;
230 		txrx_printk(XRADIO_DBG_NIY, "%s:HT compat detect\n", __func__);
231 		return 0;
232 	}
233 
234 	/* Enhance compatibility with some illegal APs.*/
235 	if (priv->ht_compat_cnt  > AP_COMPAT_THRESHOLD ||
236 		(priv->ht_compat_cnt > AP_COMPAT_MIN_CNT &&
237 		 priv->bssid[0] == 0xC8 &&
238 		 priv->bssid[1] == 0x3A &&
239 		 priv->bssid[2] == 0x35)) {
240 		struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
241 		memcpy(ap_compat_bssid, priv->bssid, ETH_ALEN);
242 		wsm_send_disassoc_to_self(hw_priv, priv);
243 		txrx_printk(XRADIO_DBG_WARN, "%s:SSID=%s, BSSID=" \
244 			    "%02x:%02x:%02x:%02x:%02x:%02x\n", __func__, priv->ssid,
245 			    ap_compat_bssid[0], ap_compat_bssid[1],
246 			    ap_compat_bssid[2], ap_compat_bssid[3],
247 			    ap_compat_bssid[4], ap_compat_bssid[5]);
248 		return 1;
249 	}
250 	return 0;
251 }
252 
253 static void xradio_remove_ht_ie(struct xradio_vif *priv, struct sk_buff *skb)
254 {
255 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
256 	u8 *ies        = NULL;
257 	size_t ies_len = 0;
258 	u8 *ht_ie      = NULL;
259 
260 	if (!mgmt || memcmp(ap_compat_bssid, mgmt->bssid, ETH_ALEN))
261 		return;
262 
263 	if (ieee80211_is_probe_resp(mgmt->frame_control))
264 		ies = mgmt->u.probe_resp.variable;
265 	else if (ieee80211_is_beacon(mgmt->frame_control))
266 		ies = mgmt->u.beacon.variable;
267 	else if (ieee80211_is_assoc_resp(mgmt->frame_control))
268 		ies = mgmt->u.assoc_resp.variable;
269 	else if (ieee80211_is_assoc_req(mgmt->frame_control))
270 		ies = mgmt->u.assoc_req.variable;
271 	else
272 		return;
273 
274 	ies_len = skb->len - (ies - (u8 *)(skb->data));
275 	ht_ie   = (u8 *)xradio_get_ie(ies, ies_len, WLAN_EID_HT_CAPABILITY);
276 	if (ht_ie) {
277 		u8 ht_len   = *(ht_ie + 1) + 2;
278 		u8 move_len = (ies + ies_len) - (ht_ie + ht_len);
279 		memmove(ht_ie, (ht_ie + ht_len), move_len);
280 		skb_trim(skb, skb->len - ht_len);
281 		ies_len = skb->len - (ies - (u8 *)(skb->data));
282 		ht_ie = (u8 *)xradio_get_ie(ies, ies_len, WLAN_EID_HT_INFORMATION);
283 		if (ht_ie) {
284 			ht_len   = *(ht_ie + 1) + 2;
285 			move_len = (ies + ies_len) - (ht_ie + ht_len);
286 			memmove(ht_ie, (ht_ie + ht_len), move_len);
287 			skb_trim(skb, skb->len - ht_len);
288 		}
289 	}
290 	txrx_printk(XRADIO_DBG_WARN, "%s: BSSID=%02x:%02x:%02x:%02x:%02x:%02x\n",
291 		    __func__,
292 		    mgmt->bssid[0], mgmt->bssid[1],
293 		    mgmt->bssid[2], mgmt->bssid[3],
294 		    mgmt->bssid[4], mgmt->bssid[5]);
295 }
296 #endif /*AP_HT_COMPAT_FIX*/
297 
298 #ifdef SUPPORT_HT40
299 
300 static inline u8 xradio_rate_to_entry_idx(u8 flags, u8 hw_value)
301 {
302 	u8 entryIndex = 0xF;
303 	if (flags & IEEE80211_TX_RC_MCS)
304 		entryIndex = hw_value - MCS_RATES_OFF;
305 	else
306 		entryIndex = hw_value;
307 	return entryIndex;
308 }
309 
310 #endif
311 
312 static void tx_policy_build(struct xradio_vif *priv,
313 	/* [out] */ struct tx_policy *policy,
314 	struct ieee80211_tx_rate *rates, size_t count)
315 {
316 	int i, j;
317 	struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
318 	struct ieee80211_rate *tmp_rate = NULL;
319 	unsigned limit = hw_priv->short_frame_max_tx_count;
320 	unsigned max_rates_cnt = count;
321 	unsigned total = 0;
322 	u8 lowest_rate_idx = 0;
323 	SYS_BUG(rates[0].idx < 0);
324 	memset(policy, 0, sizeof(*policy));
325 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
326 
327 	txrx_printk(XRADIO_DBG_MSG, "============================");
328 
329 #ifdef SUPPORT_HT40
330 
331 	/* calculate the tx policy count and max retry times */
332 	total = rates[0].count;
333 	for (i = 1; i < count; ++i) {
334 		if (rates[i].idx < 0 || i >= limit) {
335 			count = i;
336 			break;
337 		} else {
338 			total += rates[i].count;
339 		}
340 	}
341 
342 #else
343 
344 	/*
345 	 * minstrel is buggy a little bit, so distille
346 	 * incoming rates first.
347 	 */
348 	/* Sort rates in descending order. */
349 	total = rates[0].count;
350 	for (i = 1; i < count; ++i) {
351 		if (rates[i].idx > rates[i-1].idx) {
352 			rates[i].idx = rates[i-1].idx > 0 ? (rates[i-1].idx - 1) : -1;
353 		}
354 		if (rates[i].idx < 0 || i >= limit) {
355 			count = i;
356 			break;
357 		} else {
358 			total += rates[i].count;
359 		}
360 	}
361 #endif
362 
363 	/*
364 	 * Add lowest rate to the end.
365 	 * TODO: it's better to do this in rate control of mac80211.
366 	 */
367 	if (unlikely(!(rates[0].flags & IEEE80211_TX_RC_MCS)) &&
368 		hw_priv->channel->band == NL80211_BAND_2GHZ) {
369 		u32 rateset = (priv->oper_rates | priv->base_rates) & ~0xf;
370 		if (rateset)
371 			lowest_rate_idx = __ffs(rateset);
372 		else
373 			lowest_rate_idx = 0xff;
374 		txrx_printk(XRADIO_DBG_MSG, "rateset=0x%x, lowest_rate_idx=%d\n",
375 					rateset, lowest_rate_idx);
376 	}
377 	if (count < max_rates_cnt && rates[count-1].idx > lowest_rate_idx) {
378 		rates[count].idx   = lowest_rate_idx;
379 		rates[count].count = rates[0].count;
380 		rates[count].flags = rates[0].flags;
381 		rates[count].flags &= (~IEEE80211_TX_RC_40_MHZ_WIDTH);
382 		rates[count].flags &= (~IEEE80211_TX_RC_SHORT_GI);
383 		total += rates[count].count;
384 		count++;
385 	}
386 
387 	/*
388 	 * Adjust tx count to limit, rates should fall quickly
389 	 * and lower rates should be more retry, because reorder
390 	 * buffer of reciever will be timeout and clear probably.
391 	 */
392 	if (count < 2) {
393 		rates[0].count = limit;
394 		total = limit;
395 	} else {
396 		u8 end_retry = 0;  /* the retry should be add to last rate. */
397 		if (limit > HIGH_RATE_MAX_RETRY) {
398 			end_retry = limit - HIGH_RATE_MAX_RETRY;
399 			limit     = HIGH_RATE_MAX_RETRY;
400 		}
401 		/* i<100 to avoid dead loop */
402 		for (i = 0; (limit != total) && (i < 100); ++i) {
403 			j = i % count;
404 			if (limit < total) {
405 				total += (rates[j].count > 1 ? -1 : 0);
406 				rates[j].count += (rates[j].count > 1 ? -1 : 0);
407 			} else {
408 				j = count - 1 - j;
409 				if (rates[j].count > 0) {
410 					total++;
411 					rates[j].count++;
412 				}
413 			}
414 		}
415 		if (end_retry) {
416 			rates[count-1].count += end_retry;
417 			limit += end_retry;
418 		}
419 	}
420 #ifdef SUPPORT_HT40
421 
422 	/* Eliminate duplicates. */
423 	total = rates[0].count;
424 	for (i = 0, j = 1; j < count; ++j) {
425 		if ((rates[j].idx == rates[i].idx) &&
426 			(rates[j].flags == rates[i].flags)) {
427 			rates[i].count += rates[j].count;
428 		} else {
429 			++i;
430 			if (i != j)
431 				rates[i] = rates[j];
432 		}
433 		total += rates[j].count;
434 	}
435 	for (j = i + 1; j < count; j++) {
436 		rates[j].idx = -1;
437 		rates[j].count = 0;
438 	}
439 	count = i + 1;
440 
441 #else
442 
443 	/* Eliminate duplicates. */
444 	total = rates[0].count;
445 	for (i = 0, j = 1; j < count; ++j) {
446 		if (rates[j].idx < 0 || rates[j].idx > rates[i].idx)
447 			break;
448 		if (rates[j].idx == rates[i].idx) {
449 			rates[i].count += rates[j].count;
450 		} else {
451 			++i;
452 			if (i != j)
453 				rates[i] = rates[j];
454 		}
455 		total += rates[j].count;
456 	}
457 	count = i + 1;
458 #endif
459 
460 	/*
461 	 * Re-fill policy trying to keep every requested rate and with
462 	 * respect to the global max tx retransmission count.
463 	 */
464 	if (limit < count)
465 		limit = count;
466 	if (total > limit) {
467 		for (i = 0; i < count; ++i) {
468 			int left = count - i - 1;
469 			if (rates[i].count > limit - left)
470 				rates[i].count = limit - left;
471 			limit -= rates[i].count;
472 		}
473 	}
474 
475 	/*
476 	 * HACK!!! Device has problems (at least) switching from
477 	 * 54Mbps CTS to 1Mbps. This switch takes enormous amount
478 	 * of time (100-200 ms), leading to valuable throughput drop.
479 	 * As a workaround, additional g-rates are injected to the
480 	 * policy.
481 	 */
482 	if (count == 2 && !(rates[0].flags & IEEE80211_TX_RC_MCS) &&
483 			rates[0].idx > 4 && rates[0].count > 2 &&
484 			rates[1].idx < 2) {
485 		/* ">> 1" is an equivalent of "/ 2", but faster */
486 		int mid_rate = (rates[0].idx + 4) >> 1;
487 
488 		/* Decrease number of retries for the initial rate */
489 		rates[0].count -= 2;
490 
491 		if (mid_rate != 4) {
492 			/* Keep fallback rate at 1Mbps. */
493 			rates[3] = rates[1];
494 
495 			/* Inject 1 transmission on lowest g-rate */
496 			rates[2].idx = 4;
497 			rates[2].count = 1;
498 			rates[2].flags = rates[1].flags;
499 
500 			/* Inject 1 transmission on mid-rate */
501 			rates[1].idx = mid_rate;
502 			rates[1].count = 1;
503 
504 			/* Fallback to 1 Mbps is a really bad thing,
505 			 * so let's try to increase probability of
506 			 * successful transmission on the lowest g rate
507 			 * even more */
508 			if (rates[0].count >= 3) {
509 				--rates[0].count;
510 				++rates[2].count;
511 			}
512 
513 			/* Adjust amount of rates defined */
514 			count += 2;
515 		} else {
516 			/* Keep fallback rate at 1Mbps. */
517 			rates[2] = rates[1];
518 
519 			/* Inject 2 transmissions on lowest g-rate */
520 			rates[1].idx = 4;
521 			rates[1].count = 2;
522 
523 			/* Adjust amount of rates defined */
524 			count += 1;
525 		}
526 	}
527 #ifdef SUPPORT_HT40
528 
529 	/* set rate_entrys */
530 	for (i = 0; i < count; ++i) {
531 		register u8 entry_idx = 0;
532 		register u8 modem_type = RATE_MODEM_LEGACY;
533 		register u8 bandwidth = RATE_BANDWIDTH_20M;
534 		register u8 flag = rates[i].flags;
535 		if (rates[i].idx < 0) {
536 			policy->rate_entrys[i] = XRADIO_INVALID_RATE_ENTRY;
537 			continue;
538 		}
539 
540 		/* set rate index and retry counter. */
541 		tmp_rate  = (struct ieee80211_rate *)xradio_get_tx_rate(hw_priv, &rates[i]);
542 
543 		entry_idx = xradio_rate_to_entry_idx(flag, tmp_rate->hw_value);
544 
545 		policy->rate_entrys[i] = ((entry_idx & 0xf) << RATEINDEX_SHIFT)
546 					| (rates[i].count & 0xf);
547 
548 		/* set modem_type and bandwidth. */
549 		if (flag & IEEE80211_TX_RC_MCS)
550 			modem_type = RATE_MODEM_HTOFDM;
551 
552 		if (flag & IEEE80211_TX_RC_40_MHZ_WIDTH)
553 			bandwidth = RATE_BANDWIDTH_40M;
554 
555 		policy->rate_entrys[i] |= ((modem_type << MODEMTYPE_SHIFT) |
556 					(bandwidth  << BANDWIDTH_SHIFT));
557 
558 		/* set short preamble in DSSS */
559 		policy->rate_entrys[i] |= MAKE_PREAMBLE_S(flag
560 					& IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
561 
562 		/* set short GI */
563 		policy->rate_entrys[i] |= MAKE_SGI(flag
564 					& IEEE80211_TX_RC_SHORT_GI);
565 
566 		/*
567 		 * set green field mode.
568 		 * there is a conflict between sgi and green field in one stream,
569 		 * and we prefer to use sgi instead of green field
570 		 */
571 		if (priv->association_mode.PhyModeCfg.GF_Enable
572 			&& (modem_type == RATE_MODEM_HTOFDM)) {
573 			if (!(GET_RATE_ENTRY_FLAGS(policy->rate_entrys[i]) & FW_RATE_F_SGI))
574 				policy->rate_entrys[i] |= MAKE_GF(1);
575 		}
576 
577 		/* set STBC, but unsurported in xradio yet. */
578 		policy->rate_entrys[i] |= MAKE_STBC(0);
579 
580 		policy->defined++;
581 		policy->retry_count += (rates[i].count & 0xf);
582 
583 		txrx_printk(XRADIO_DBG_NIY, "[TX policy] rate_entrys=0x%04x",
584 						policy->rate_entrys[i]);
585 	}
586 
587 #else
588 
589 	tmp_rate = (struct ieee80211_rate *)xradio_get_tx_rate(hw_priv, &rates[0]);
590 	if (tmp_rate)
591 		policy->defined = tmp_rate->hw_value + 1;
592 
593 #ifdef ENHANCE_ANTI_INTERFERE
594 	/*
595 	 * We need to check if 11b rate be supported.
596 	 * We only add 11bg rate when no rate below 5.5Mbps is set,
597 	 * and last rate has enough reties. Add we need check fw version too.
598 	 */
599 	if (WSM_CAPS_11N_TO_11BG(hw_priv->wsm_caps) && !priv->vif->p2p &&
600 		((priv->oper_rates | priv->base_rates) & 0xf) == 0xf &&
601 		(rates[0].flags & IEEE80211_TX_RC_MCS) &&
602 		rates[count-1].idx == 0 && rates[count-1].count >= 3) {
603 		u8 mcs0_cnt = rates[count-1].count;
604 		u8 cnt1 = (mcs0_cnt>>2);
605 		u8 cnt2 = ((mcs0_cnt - (cnt1<<1))>>1);
606 		if (count <= 1) {
607 			mcs0_cnt -= (cnt1 + cnt1 + cnt2);
608 			rates[count-1].count = 0;
609 			policy->tbl[0] |= ((cnt1&0xf)<<(3<<2));
610 			policy->tbl[0] |= ((cnt2&0xf)<<(2<<2));
611 			policy->tbl[0] |= ((cnt1&0xf)<<(1<<2));
612 			policy->tbl[0] |= ((mcs0_cnt&0xf)<<(0<<2));
613 			policy->retry_count = cnt1 + cnt2 + cnt1 + mcs0_cnt;
614 			txrx_printk(XRADIO_DBG_MSG,
615 				"[TX policy]to11b=%d(f=%d), %d, 11=%d, 5.5=%d, 2=%d, 1=%d\n",
616 				rates[count-1].idx, rates[0].flags,
617 				rates[count-1].count, cnt1, cnt2, cnt1, mcs0_cnt);
618 		} else {
619 			mcs0_cnt -= (cnt1 + cnt1 + cnt2);
620 			rates[count-1].count = cnt1;
621 			policy->tbl[0] |= ((cnt1&0xf)<<(3<<2));
622 			policy->tbl[0] |= ((cnt2&0xf)<<(2<<2));
623 			policy->tbl[0] |= ((mcs0_cnt&0xf)<<(0<<2));
624 			policy->retry_count = cnt1 + cnt2 + mcs0_cnt;
625 			txrx_printk(XRADIO_DBG_MSG,
626 				"[TX policy]to11b=%d(f=%d), %d, 11=%d, 5.5=%d, 2=%d, 1=%d\n",
627 				rates[count-1].idx, rates[0].flags,
628 				rates[count-1].count, cnt1, cnt2, 0, mcs0_cnt);
629 		}
630 	} else {
631 		txrx_printk(XRADIO_DBG_MSG,
632 			"[TX policy]WSM_CAPS to11b=%d, p2p=%d, MSC=%d, rates=0x%08x, count(%d)=%d\n",
633 			!!WSM_CAPS_11N_TO_11BG(hw_priv->wsm_caps), priv->vif->p2p,
634 			!!(rates[0].flags & IEEE80211_TX_RC_MCS),
635 			(priv->oper_rates | priv->base_rates),
636 			count-1, rates[count-1].count);
637 	}
638 #endif
639 
640 	for (i = 0; i < count; ++i) {
641 		register unsigned rateid, off, shift, retries;
642 
643 		tmp_rate = (struct ieee80211_rate *)xradio_get_tx_rate(hw_priv, &rates[i]);
644 		if (tmp_rate) {
645 			rateid = tmp_rate->hw_value;
646 		} else {
647 			break;
648 		}
649 		off = rateid >> 3;		/* eq. rateid / 8 */
650 		shift = (rateid & 0x07) << 2;	/* eq. (rateid % 8) * 4 */
651 
652 		retries = rates[i].count;
653 		if (unlikely(retries > 0x0F))
654 			rates[i].count = retries = 0x0F;
655 		policy->tbl[off] |= __cpu_to_le32(retries << shift);
656 		policy->retry_count += retries;
657 		txrx_printk(XRADIO_DBG_MSG, "[TX policy] %d.%dMps=%d",
658 			    tmp_rate->bitrate/10, tmp_rate->bitrate%10, retries);
659 	}
660 
661 #endif
662 
663 	txrx_printk(XRADIO_DBG_MSG, "[TX policy] Dst Policy (%zu): " \
664 		"%d:%d, %d:%d, %d:%d, %d:%d, %d:%d\n",
665 		count,
666 		rates[0].idx, rates[0].count,
667 		rates[1].idx, rates[1].count,
668 		rates[2].idx, rates[2].count,
669 		rates[3].idx, rates[3].count,
670 		rates[4].idx, rates[4].count);
671 }
672 
673 #ifdef SUPPORT_HT40
674 
675 static inline bool tx_policy_is_equal(const struct tx_policy *wanted,
676 					const struct tx_policy *cached)
677 {
678 
679 	size_t count = wanted->defined << 1;
680 
681 	if (wanted->defined != cached->defined)
682 		return false;
683 
684 	if (count) {
685 		if (memcmp(wanted->raw, cached->raw, count))
686 			return false;
687 	}
688 
689 	return true;
690 }
691 
692 #else
693 
694 static inline bool tx_policy_is_equal(const struct tx_policy *wanted,
695 					const struct tx_policy *cached)
696 {
697 	size_t count = wanted->defined >> 1;
698 
699 	if (wanted->defined > cached->defined)
700 		return false;
701 	if (count) {
702 		if (memcmp(wanted->raw, cached->raw, count))
703 			return false;
704 	}
705 	if (wanted->defined & 1) {
706 		if ((wanted->raw[count] & 0x0F) != (cached->raw[count] & 0x0F))
707 			return false;
708 	}
709 	return true;
710 }
711 
712 #endif
713 
714 static int tx_policy_find(struct tx_policy_cache *cache,
715 				const struct tx_policy *wanted)
716 {
717 	/* O(n) complexity. Not so good, but there's only 8 entries in
718 	 * the cache.
719 	 * Also lru helps to reduce search time. */
720 	struct tx_policy_cache_entry *it;
721 	/* Search for policy in "used" list */
722 	list_for_each_entry(it, &cache->used, link) {
723 		if (tx_policy_is_equal(wanted, &it->policy))
724 			return it - cache->cache;
725 	}
726 	/* Then - in "free list" */
727 	list_for_each_entry(it, &cache->free, link) {
728 		if (tx_policy_is_equal(wanted, &it->policy))
729 			return it - cache->cache;
730 	}
731 	return -1;
732 }
733 
734 static inline void tx_policy_use(struct tx_policy_cache *cache,
735 				 struct tx_policy_cache_entry *entry)
736 {
737 	++entry->policy.usage_count;
738 	list_move(&entry->link, &cache->used);
739 }
740 
741 static inline int tx_policy_release(struct tx_policy_cache *cache,
742 				    struct tx_policy_cache_entry *entry)
743 {
744 	int ret = --entry->policy.usage_count;
745 	if (!ret)
746 		list_move(&entry->link, &cache->free);
747 	return ret;
748 }
749 
750 /* ******************************************************************** */
751 /* External TX policy cache API						*/
752 
753 void tx_policy_init(struct xradio_common *hw_priv)
754 {
755 	struct tx_policy_cache *cache = &hw_priv->tx_policy_cache;
756 	int i;
757 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
758 
759 	memset(cache, 0, sizeof(*cache));
760 
761 	spin_lock_init(&cache->lock);
762 	INIT_LIST_HEAD(&cache->used);
763 	INIT_LIST_HEAD(&cache->free);
764 
765 	for (i = 0; i < TX_POLICY_CACHE_SIZE; ++i)
766 		list_add(&cache->cache[i].link, &cache->free);
767 }
768 
769 static int tx_policy_get(struct xradio_vif *priv,
770 		  struct ieee80211_tx_rate *rates,
771 		  u8 use_bg_rate, bool *renew)
772 {
773 	int idx;
774 	struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
775 	struct tx_policy_cache *cache = &hw_priv->tx_policy_cache;
776 	struct tx_policy wanted;
777 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
778 
779 	if (use_bg_rate) {
780 
781 #ifdef SUPPORT_HT40
782 
783 		memset(&wanted, 0, sizeof(wanted));
784 		wanted.retry_count = (hw_priv->short_frame_max_tx_count&0xf);
785 		wanted.defined = 1;
786 
787 		/* set rate index and retry counter. */
788 		wanted.rate_entrys[0] = (((use_bg_rate & 0xf)<<4)
789 					| (wanted.retry_count & 0xf));
790 
791 		/* set modem_type and bandwidth. */
792 		wanted.rate_entrys[0] |=
793 			((RATE_MODEM_LEGACY  << MODEMTYPE_SHIFT)
794 			| (RATE_BANDWIDTH_20M << BANDWIDTH_SHIFT));
795 
796 		/* set short preamble in DSSS */
797 		wanted.rate_entrys[0] |= MAKE_PREAMBLE_S(rates[0].flags
798 					& IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
799 
800 		txrx_printk(XRADIO_DBG_NIY, "[TX policy] robust rate=0x%04x\n",
801 					wanted.rate_entrys[0]);
802 
803 #else
804 		u8 rate  = (u8)(use_bg_rate & 0x3f);
805 		u8 shitf = ((rate&0x7)<<2);
806 		u8 off   = (rate>>3);
807 		memset(&wanted, 0, sizeof(wanted));
808 		wanted.defined = rate + 1;
809 		wanted.retry_count = (hw_priv->short_frame_max_tx_count&0xf);
810 		wanted.tbl[off] = wanted.retry_count<<shitf;
811 		txrx_printk(XRADIO_DBG_NIY, "[TX policy] robust rate=%d\n", rate);
812 
813 #endif
814 
815 	} else
816 		tx_policy_build(priv, &wanted, rates, IEEE80211_TX_MAX_RATES);
817 
818 	/* use rate policy instead of minstel policy in debug mode*/
819 #ifdef CONFIG_XRADIO_DEBUGFS
820 	if (rates_dbg_en & 0x2) {
821 		memset(&wanted, 0, sizeof(wanted));
822 
823 #ifdef SUPPORT_HT40
824 
825 		wanted.defined = Ratecnt_dbg;
826 		wanted.retry_count = (hw_priv->short_frame_max_tx_count&0xf);
827 		memcpy(&wanted.rate_entrys[0], &rates_debug[0],
828 						sizeof(wanted.rate_entrys));
829 
830 #else
831 
832 		wanted.defined = maxRate_dbg + 1;
833 		wanted.retry_count = (hw_priv->short_frame_max_tx_count&0xf);
834 		memcpy(&wanted.tbl[0], &rates_debug[0], sizeof(wanted.tbl));
835 
836 #endif
837 	}
838 #endif
839 
840 	spin_lock_bh(&cache->lock);
841 	idx = tx_policy_find(cache, &wanted);
842 	if (idx >= 0) {
843 		txrx_printk(XRADIO_DBG_MSG, "[TX policy] Used TX policy: %d\n",
844 					idx);
845 		*renew = false;
846 	} else if (WARN_ON_ONCE(list_empty(&cache->free))) {
847 		spin_unlock_bh(&cache->lock);
848 		txrx_printk(XRADIO_DBG_ERROR, "[TX policy] no policy cache\n");
849 		return XRADIO_INVALID_RATE_ID;
850 	} else {
851 		struct tx_policy_cache_entry *entry;
852 
853 		/* If policy is not found create a new one
854 		 * using the oldest entry in "free" list */
855 		*renew = true;
856 		entry = list_entry(cache->free.prev,
857 			struct tx_policy_cache_entry, link);
858 		entry->policy = wanted;
859 		idx = entry - cache->cache;
860 		txrx_printk(XRADIO_DBG_MSG, "[TX policy] New TX policy: %d\n",
861 					idx);
862 		tx_policy_dump(&entry->policy);
863 	}
864 	tx_policy_use(cache, &cache->cache[idx]);
865 	if (unlikely(list_empty(&cache->free)) &&
866 		!cache->queue_locked) {
867 		/* Lock TX queues. */
868 		DBG_INT_ADD(policy_lock_cnt);
869 		txrx_printk(XRADIO_DBG_WARN, "[TX policy] policy cache used up\n");
870 		xradio_tx_queues_lock(hw_priv);
871 		cache->queue_locked = true;
872 	}
873 	spin_unlock_bh(&cache->lock);
874 
875 	/*force to upload retry limit when using debug rate policy */
876 #ifdef CONFIG_XRADIO_DEBUGFS
877 	if (retry_dbg & 0x2) {
878 		retry_dbg &= ~0x2;
879 		/* retry dgb need to be applied to policy. */
880 		*renew = true;
881 		cache->cache[idx].policy.uploaded = 0;
882 	}
883 #endif
884 
885 	return idx;
886 }
887 
888 static void tx_policy_put(struct xradio_common *hw_priv, int idx)
889 {
890 	int usage;
891 	/*int locked;*/
892 	struct tx_policy_cache *cache = &hw_priv->tx_policy_cache;
893 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
894 
895 	spin_lock_bh(&cache->lock);
896 	/*locked = list_empty(&cache->free);*/
897 	usage = tx_policy_release(cache, &cache->cache[idx]);
898 	if (unlikely(cache->queue_locked) && !list_empty(&cache->free)) {
899 		/* Unlock TX queues. */
900 		xradio_tx_queues_unlock(hw_priv);
901 		cache->queue_locked = false;
902 	}
903 	spin_unlock_bh(&cache->lock);
904 }
905 
906 /*
907 bool tx_policy_cache_full(struct xradio_common *hw_priv)
908 {
909 	bool ret;
910 	struct tx_policy_cache *cache = &hw_priv->tx_policy_cache;
911 	spin_lock_bh(&cache->lock);
912 	ret = list_empty(&cache->free);
913 	spin_unlock_bh(&cache->lock);
914 	return ret;
915 }
916 */
917 extern u32 policy_upload;
918 extern u32 policy_num;
919 static int tx_policy_upload(struct xradio_common *hw_priv)
920 {
921 	struct tx_policy_cache *cache = &hw_priv->tx_policy_cache;
922 	int i;
923 	struct wsm_set_tx_rate_retry_policy arg = {
924 		.hdr = {
925 			.numTxRatePolicies = 0,
926 		}
927 	};
928 	int if_id = 0;
929 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
930 
931 	spin_lock_bh(&cache->lock);
932 	/* Upload only modified entries. */
933 	for (i = 0; i < TX_POLICY_CACHE_SIZE; ++i) {
934 		struct tx_policy *src = &cache->cache[i].policy;
935 		if (src->retry_count && !src->uploaded) {
936 			struct wsm_set_tx_rate_retry_policy_policy *dst =
937 				&arg.tbl[arg.hdr.numTxRatePolicies];
938 			dst->policyIndex = i;
939 			dst->shortRetryCount = hw_priv->short_frame_max_tx_count-1;
940 			/* only RTS need use longRetryCount, should be short_frame. */
941 			dst->longRetryCount = hw_priv->short_frame_max_tx_count-1;
942 
943 			/* BIT(2) - Terminate retries when Tx rate retry policy
944 			 *          finishes.
945 			 * BIT(3) - Count initial frame transmission as part of
946 			 *          rate retry counting but not as a retry
947 			 *          attempt */
948 			dst->policyFlags = BIT(2) | BIT(3);
949 
950 #ifdef SUPPORT_HT40
951 
952 			memcpy(dst->rate_entrys, src->rate_entrys,
953 					sizeof(dst->rate_entrys));
954 
955 #else
956 
957 			memcpy(dst->rateCountIndices, src->tbl,
958 					sizeof(dst->rateCountIndices));
959 
960 #endif
961 
962 			src->uploaded = 1;
963 			++arg.hdr.numTxRatePolicies;
964 		}
965 	}
966 	spin_unlock_bh(&cache->lock);
967 	atomic_set(&hw_priv->upload_count, 0);
968 
969 	xradio_debug_tx_cache_miss(hw_priv);
970 	txrx_printk(XRADIO_DBG_MSG, "[TX policy] Upload %d policies\n",
971 				arg.hdr.numTxRatePolicies);
972 #ifdef CONFIG_XRADIO_DEBUGFS
973 
974 #ifndef SUPPORT_HT40
975 
976 	if (arg.tbl[0].policyIndex == 7)
977 		txrx_printk(XRADIO_DBG_MSG, "rate:0x%08x, 0x%08x, 0x%08x\n",
978 								arg.tbl[0].rateCountIndices[2],
979 								arg.tbl[0].rateCountIndices[1],
980 								arg.tbl[0].rateCountIndices[0]);
981 
982 #endif
983 	policy_upload++;
984 	policy_num += arg.hdr.numTxRatePolicies;
985 #endif
986 	/*TODO: COMBO*/
987 	return wsm_set_tx_rate_retry_policy(hw_priv, &arg, if_id);
988 }
989 
990 void tx_policy_upload_work(struct work_struct *work)
991 {
992 	struct xradio_common *hw_priv =
993 		container_of(work, struct xradio_common, tx_policy_upload_work);
994 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
995 
996 	SYS_WARN(tx_policy_upload(hw_priv));
997 	wsm_unlock_tx(hw_priv);
998 }
999 
1000 /* ******************************************************************** */
1001 /* xradio TX implementation						*/
1002 
1003 struct xradio_txinfo {
1004 	struct sk_buff *skb;
1005 	unsigned queue;
1006 	struct ieee80211_tx_info *tx_info;
1007 	const struct ieee80211_rate *rate;
1008 	struct ieee80211_hdr *hdr;
1009 	size_t hdrlen;
1010 	const u8 *da;
1011 	struct xradio_sta_priv *sta_priv;
1012 	struct xradio_txpriv txpriv;
1013 };
1014 
1015 u32 xradio_rate_mask_to_wsm(struct xradio_common *hw_priv, u32 rates)
1016 {
1017 	u32 ret = 0;
1018 	int i;
1019 	u32 n_bitrates =
1020 		  hw_priv->hw->wiphy->bands[hw_priv->channel->band]->n_bitrates;
1021 	struct ieee80211_rate *bitrates =
1022 		  hw_priv->hw->wiphy->bands[hw_priv->channel->band]->bitrates;
1023 
1024 	for (i = 0; i < n_bitrates; ++i) {
1025 		if (rates & BIT(i))
1026 			ret |= BIT(bitrates[i].hw_value);
1027 	}
1028 	return ret;
1029 }
1030 
1031 static const struct ieee80211_rate *
1032 xradio_get_tx_rate(const struct xradio_common *hw_priv,
1033 		   const struct ieee80211_tx_rate *rate)
1034 {
1035 	if (rate->idx < 0)
1036 		return NULL;
1037 	if (rate->flags & IEEE80211_TX_RC_MCS)
1038 		return &hw_priv->mcs_rates[rate->idx];
1039 	return &hw_priv->hw->wiphy->bands[hw_priv->channel->band]->
1040 		bitrates[rate->idx];
1041 }
1042 
1043 #ifdef SUPPORT_HT40
1044 
1045 u16 xradio_get_rate_entry(const struct xradio_common *hw_priv,
1046 			u8 Bandwidth, u16 FormatFlag, u8 hw_value)
1047 {
1048 	u16 rate_entry = XRADIO_INVALID_RATE_ENTRY;
1049 	u8  modem_type = 0;
1050 	s8  rate_idx = hw_value;
1051 	if (rate_idx <= MAX_RATES_IDX) {
1052 		if (rate_idx >= MCS_RATES_OFF) {
1053 			rate_idx  -= MCS_RATES_OFF;
1054 			modem_type = RATE_MODEM_HTOFDM;
1055 		}
1056 		rate_entry = rate_idx << RATEINDEX_SHIFT;
1057 		rate_entry |= (modem_type & MODEMTYPE_MASK) << MODEMTYPE_SHIFT;
1058 		rate_entry |= (Bandwidth  & BANDWIDTH_MASK) << BANDWIDTH_SHIFT;
1059 		rate_entry |= (FormatFlag & RATE_F_MASK);
1060 	}
1061 	return rate_entry;
1062 }
1063 
1064 #else
1065 
1066 static inline s8 xradio_get_rate_idx(const struct xradio_common *hw_priv,
1067 									 u8 flag, u16 hw_value)
1068 {
1069 	s16 ret = (s16)hw_value;
1070 	if (flag & IEEE80211_TX_RC_MCS) {  /* 11n */
1071 		if (hw_value <= hw_priv->mcs_rates[7].hw_value &&
1072 			 hw_value >= hw_priv->mcs_rates[0].hw_value)
1073 			ret -= hw_priv->mcs_rates[0].hw_value;
1074 		else
1075 			ret = -1;
1076 	} else {  /* 11b/g */
1077 		if (hw_value > 5 && hw_value < hw_priv->mcs_rates[0].hw_value) {
1078 			ret -= hw_priv->hw->wiphy-> \
1079 			       bands[hw_priv->channel->band]->bitrates[0].hw_value;
1080 			if (hw_priv->hw->wiphy-> \
1081 			   bands[hw_priv->channel->band]->bitrates[0].hw_value < 5) /* 11a*/
1082 				ret -= 2;
1083 		} else if (hw_value < 4) {
1084 			ret -= hw_priv->hw->wiphy-> \
1085 			       bands[hw_priv->channel->band]->bitrates[0].hw_value;
1086 		} else {
1087 			ret = -1;
1088 		}
1089 	}
1090 	return (s8)ret;
1091 }
1092 
1093 #endif
1094 
1095 static int
1096 xradio_tx_h_calc_link_ids(struct xradio_vif *priv,
1097 			  struct xradio_txinfo *t)
1098 {
1099 #ifndef P2P_MULTIVIF
1100 	struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
1101 #endif
1102 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1103 
1104 #ifndef P2P_MULTIVIF
1105 	if ((t->tx_info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1106 			(hw_priv->roc_if_id == priv->if_id))
1107 		t->txpriv.offchannel_if_id = 2;
1108 	else
1109 		t->txpriv.offchannel_if_id = 0;
1110 #endif
1111 
1112 	if (likely(t->sta_priv && t->sta_priv->link_id))
1113 		t->txpriv.raw_link_id =
1114 				t->txpriv.link_id =
1115 				t->sta_priv->link_id;
1116 	else if (priv->mode != NL80211_IFTYPE_AP)
1117 		t->txpriv.raw_link_id =
1118 				t->txpriv.link_id = 0;
1119 	else if (is_multicast_ether_addr(t->da)) {
1120 		if (priv->enable_beacon) {
1121 			t->txpriv.raw_link_id = 0;
1122 			t->txpriv.link_id = priv->link_id_after_dtim;
1123 		} else {
1124 			t->txpriv.raw_link_id = 0;
1125 			t->txpriv.link_id = 0;
1126 		}
1127 	} else {
1128 		t->txpriv.link_id =
1129 			xradio_find_link_id(priv, t->da);
1130 		/* Do not assign valid link id for deauth/disassoc frame being
1131 		transmitted to an unassociated STA */
1132 		if (!(t->txpriv.link_id) &&
1133 			(ieee80211_is_deauth(t->hdr->frame_control) ||
1134 			ieee80211_is_disassoc(t->hdr->frame_control))) {
1135 					t->txpriv.link_id = 0;
1136 		} else {
1137 			if (!t->txpriv.link_id)
1138 				t->txpriv.link_id = xradio_alloc_link_id(priv, t->da);
1139 			if (!t->txpriv.link_id) {
1140 				txrx_printk(XRADIO_DBG_ERROR,
1141 					    "%s: No more link IDs available.\n", __func__);
1142 				return -ENOENT;
1143 			}
1144 		}
1145 		t->txpriv.raw_link_id = t->txpriv.link_id;
1146 	}
1147 	if (t->txpriv.raw_link_id)
1148 		priv->link_id_db[t->txpriv.raw_link_id - 1].timestamp =
1149 				jiffies;
1150 
1151 #if 0//defined(CONFIG_XRADIO_USE_EXTENSIONS)
1152 	if (t->tx_info->control.sta &&
1153 			(t->tx_info->control.sta->uapsd_queues & BIT(t->queue)))
1154 		t->txpriv.link_id = priv->link_id_uapsd;
1155 #endif /* CONFIG_XRADIO_USE_EXTENSIONS */
1156 	return 0;
1157 }
1158 
1159 static void
1160 xradio_tx_h_pm(struct xradio_vif *priv,
1161 	       struct xradio_txinfo *t)
1162 {
1163 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1164 
1165 	if (unlikely(ieee80211_is_auth(t->hdr->frame_control))) {
1166 		u32 mask = ~BIT(t->txpriv.raw_link_id);
1167 		spin_lock_bh(&priv->ps_state_lock);
1168 		priv->sta_asleep_mask &= mask;
1169 		priv->pspoll_mask &= mask;
1170 		spin_unlock_bh(&priv->ps_state_lock);
1171 	}
1172 }
1173 
1174 static void
1175 xradio_tx_h_calc_tid(struct xradio_vif *priv,
1176 		     struct xradio_txinfo *t)
1177 {
1178 	if (ieee80211_is_data_qos(t->hdr->frame_control)) {
1179 		u8 *qos = ieee80211_get_qos_ctl(t->hdr);
1180 		t->txpriv.tid = qos[0] & IEEE80211_QOS_CTL_TID_MASK;
1181 	} else if (ieee80211_is_data(t->hdr->frame_control)) {
1182 		t->txpriv.tid = 0;
1183 	}
1184 }
1185 
1186 /* IV/ICV injection. */
1187 /* TODO: Quite unoptimal. It's better co modify mac80211
1188  * to reserve space for IV */
1189 static int
1190 xradio_tx_h_crypt(struct xradio_vif *priv,
1191 		  struct xradio_txinfo *t)
1192 {
1193 	size_t iv_len;
1194 	size_t icv_len;
1195 	u8 *icv;
1196 	u8 *newhdr;
1197 	int is_multi_mfp = 0;
1198 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1199 
1200 	if (ieee80211_is_mgmt(t->hdr->frame_control) &&
1201 		 is_multicast_ether_addr(t->hdr->addr1) &&
1202 		 ieee80211_is_robust_mgmt_frame(t->skb))
1203 		 is_multi_mfp = 1;
1204 
1205 	if (!t->tx_info->control.hw_key ||
1206 	    (!(t->hdr->frame_control &
1207 	     __cpu_to_le32(IEEE80211_FCTL_PROTECTED)) && (!is_multi_mfp)))
1208 		return 0;
1209 
1210 	iv_len = t->tx_info->control.hw_key->iv_len;
1211 	icv_len = t->tx_info->control.hw_key->icv_len;
1212 #ifdef AP_ARP_COMPAT_FIX
1213 	t->txpriv.iv_len = iv_len;
1214 #endif
1215 	if (t->tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
1216 		icv_len += 8; /* MIC */
1217 
1218 	if ((skb_headroom(t->skb) + skb_tailroom(t->skb) <
1219 			 iv_len + icv_len + WSM_TX_EXTRA_HEADROOM) ||
1220 			(skb_headroom(t->skb) <
1221 			 iv_len + WSM_TX_EXTRA_HEADROOM)) {
1222 		txrx_printk(XRADIO_DBG_ERROR,
1223 			"Bug: no space allocated for crypto headers.\n"
1224 			"headroom: %d, tailroom: %d, "
1225 			"req_headroom: %zu, req_tailroom: %zu\n"
1226 			"Please fix it in xradio_get_skb().\n",
1227 			skb_headroom(t->skb), skb_tailroom(t->skb),
1228 			iv_len + WSM_TX_EXTRA_HEADROOM, icv_len);
1229 		return -ENOMEM;
1230 	} else if (skb_tailroom(t->skb) < icv_len) {
1231 		size_t offset = icv_len - skb_tailroom(t->skb);
1232 		u8 *p;
1233 		txrx_printk(XRADIO_DBG_WARN,
1234 			"Slowpath: tailroom is not big enough. "
1235 			"Req: %zu, got: %d.\n",
1236 			icv_len, skb_tailroom(t->skb));
1237 
1238 		p = skb_push(t->skb, offset);
1239 		memmove(p, &p[offset], t->skb->len - offset);
1240 		skb_trim(t->skb, t->skb->len - offset);
1241 	}
1242 
1243 	newhdr = skb_push(t->skb, iv_len);
1244 	memmove(newhdr, newhdr + iv_len, t->hdrlen);
1245 	t->hdr = (struct ieee80211_hdr *) newhdr;
1246 	t->hdrlen += iv_len;
1247 	icv = skb_put(t->skb, icv_len);
1248 
1249 	if (t->tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1250 		struct ieee80211_mmie *mmie = (struct ieee80211_mmie *) icv;
1251 		memset(mmie, 0, sizeof(struct ieee80211_mmie));
1252 		mmie->element_id = WLAN_EID_MMIE;
1253 		mmie->length = sizeof(*mmie) - 2;
1254 	}
1255 
1256 	return 0;
1257 }
1258 #ifdef SUPPORT_HT40
1259 
1260 static int xradio_tx_h_align(struct xradio_vif *priv, struct xradio_txinfo *t)
1261 
1262 #else
1263 
1264 static int
1265 xradio_tx_h_align(struct xradio_vif *priv, struct xradio_txinfo *t,
1266 		  u8 *flags)
1267 
1268 #endif
1269 {
1270 	size_t offset = (size_t)t->skb->data & 3;
1271 	u8 *newhdr;
1272 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1273 
1274 	if (!offset)
1275 		return 0;
1276 
1277 	if (skb_headroom(t->skb) < offset) {
1278 		txrx_printk(XRADIO_DBG_ERROR,
1279 			"Bug: no space allocated "
1280 			"for DMA alignment.\n"
1281 			"headroom: %d\n",
1282 			skb_headroom(t->skb));
1283 		return -ENOMEM;
1284 	}
1285     /* offset = 1or3 process */
1286 	if (offset & 1) {
1287 		newhdr = skb_push(t->skb, offset);
1288 		memmove(newhdr, newhdr + offset, t->skb->len-offset);
1289 		skb_trim(t->skb, t->skb->len-offset);
1290 		t->hdr = (struct ieee80211_hdr *) newhdr;
1291 		xradio_debug_tx_align(priv);
1292 		return 0;
1293 	}
1294 	/* offset=2 process */
1295 	skb_push(t->skb, offset);
1296 	t->hdrlen += offset;
1297 	t->txpriv.offset += offset;
1298 #ifndef SUPPORT_HT40
1299 	*flags |= WSM_TX_2BYTES_SHIFT;
1300 #endif
1301 	xradio_debug_tx_align(priv);
1302 	return 0;
1303 }
1304 
1305 static int
1306 xradio_tx_h_action(struct xradio_vif *priv, struct xradio_txinfo *t)
1307 {
1308 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)t->hdr;
1309 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1310 
1311 	if (ieee80211_is_action(t->hdr->frame_control) &&
1312 			mgmt->u.action.category == WLAN_CATEGORY_BACK)
1313 		return 1;
1314 	else
1315 		return 0;
1316 }
1317 
1318 /* Add WSM header */
1319 static struct wsm_tx *
1320 xradio_tx_h_wsm(struct xradio_vif *priv, struct xradio_txinfo *t)
1321 {
1322 	struct wsm_tx *wsm;
1323 	struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
1324 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1325 
1326 	if (skb_headroom(t->skb) < sizeof(struct wsm_tx)) {
1327 		txrx_printk(XRADIO_DBG_ERROR,
1328 			"Bug: no space allocated "
1329 			"for WSM header.\n"
1330 			"headroom: %d\n",
1331 			skb_headroom(t->skb));
1332 		return NULL;
1333 	}
1334 
1335 	wsm = (struct wsm_tx *)skb_push(t->skb, sizeof(struct wsm_tx));
1336 	t->txpriv.offset += sizeof(struct wsm_tx);
1337 	memset(wsm, 0, sizeof(*wsm));
1338 
1339 #ifdef SUPPORT_HT40
1340 
1341 	wsm->DataOffset = t->txpriv.offset;
1342 
1343 #endif
1344 
1345 	wsm->hdr.len = __cpu_to_le16(t->skb->len);
1346 	wsm->hdr.id  = __cpu_to_le16(0x0004);
1347 	wsm->queueId = (t->txpriv.raw_link_id << 2) | wsm_queue_id_to_wsm(t->queue);
1348 	if (wsm->hdr.len > hw_priv->wsm_caps.sizeInpChBuf) {
1349 		txrx_printk(XRADIO_DBG_ERROR, "%s, msg length too big=%d\n",
1350 			    __func__, wsm->hdr.len);
1351 		skb_pull(t->skb, sizeof(struct wsm_tx));  //skb revert.
1352 		wsm = NULL;
1353 	}
1354 
1355 	return wsm;
1356 }
1357 
1358 /* BT Coex specific handling */
1359 
1360 static void xradio_tx_h_bt(struct xradio_vif *priv, struct xradio_txinfo *t,
1361 						   struct wsm_tx *wsm)
1362 {
1363 	u8 priority = 0;
1364 	struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
1365 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1366 
1367 	if (!hw_priv->is_BT_Present)
1368 		return;
1369 
1370 	if (unlikely(ieee80211_is_nullfunc(t->hdr->frame_control)))
1371 		priority = WSM_EPTA_PRIORITY_MGT;
1372 	else if (ieee80211_is_data(t->hdr->frame_control)) {
1373 		/* Skip LLC SNAP header (+6) */
1374 		u8 *payload = &t->skb->data[t->hdrlen];
1375 		u16 *ethertype = (u16 *) &payload[6];
1376 		if (unlikely(*ethertype == __be16_to_cpu(ETH_P_PAE)))
1377 			priority = WSM_EPTA_PRIORITY_EAPOL;
1378 	} else if (unlikely(ieee80211_is_assoc_req(t->hdr->frame_control) ||
1379 		ieee80211_is_reassoc_req(t->hdr->frame_control))) {
1380 		struct ieee80211_mgmt *mgt_frame =
1381 				(struct ieee80211_mgmt *)t->hdr;
1382 
1383 		if (mgt_frame->u.assoc_req.listen_interval <
1384 						priv->listen_interval) {
1385 			txrx_printk(XRADIO_DBG_MSG,
1386 				"Modified Listen Interval to %d from %d\n",
1387 				priv->listen_interval,
1388 				mgt_frame->u.assoc_req.listen_interval);
1389 			/* Replace listen interval derieved from
1390 			 * the one read from SDD */
1391 			mgt_frame->u.assoc_req.listen_interval =
1392 				priv->listen_interval;
1393 		}
1394 	}
1395 
1396 	if (likely(!priority)) {
1397 		if (ieee80211_is_action(t->hdr->frame_control))
1398 			priority = WSM_EPTA_PRIORITY_ACTION;
1399 		else if (ieee80211_is_mgmt(t->hdr->frame_control))
1400 			priority = WSM_EPTA_PRIORITY_MGT;
1401 		else if (wsm->queueId == WSM_QUEUE_VOICE)
1402 			priority = WSM_EPTA_PRIORITY_VOICE;
1403 		else if (wsm->queueId == WSM_QUEUE_VIDEO)
1404 			priority = WSM_EPTA_PRIORITY_VIDEO;
1405 		else
1406 			priority = WSM_EPTA_PRIORITY_DATA;
1407 	}
1408 
1409 	txrx_printk(XRADIO_DBG_MSG, "[TX] EPTA priority %d.\n",
1410 		priority);
1411 
1412 #ifdef SUPPORT_HT40
1413 
1414 	wsm->EptaPriority = priority;
1415 
1416 #else
1417 
1418 	wsm->flags |= priority << 1;
1419 
1420 #endif
1421 
1422 }
1423 
1424 static int
1425 xradio_tx_h_rate_policy(struct xradio_vif *priv,
1426 						struct xradio_txinfo *t,
1427 						struct wsm_tx *wsm)
1428 {
1429 	bool tx_policy_renew = false;
1430 	struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
1431 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1432 
1433 	/*use debug policy for data frames only*/
1434 #ifdef CONFIG_XRADIO_DEBUGFS
1435 	if ((rates_dbg_en & 0x1) && ieee80211_is_data(t->hdr->frame_control)) {
1436 		rates_dbg_en |= 0x02;
1437 	}
1438 #endif
1439 
1440 	t->txpriv.rate_id = tx_policy_get(priv,
1441 		t->tx_info->control.rates, t->txpriv.use_bg_rate,
1442 		&tx_policy_renew);
1443 	if (t->txpriv.rate_id == XRADIO_INVALID_RATE_ID)
1444 		return -EFAULT;
1445 
1446 #ifdef SUPPORT_HT40
1447 
1448 	wsm->TxPolicyIndex = t->txpriv.rate_id;
1449 
1450 	wsm->TxRateEntry  =
1451 	hw_priv->tx_policy_cache.cache[t->txpriv.rate_id].policy.rate_entrys[0];
1452 
1453 #else
1454 
1455 	wsm->flags |= t->txpriv.rate_id << 4;
1456 	t->rate = xradio_get_tx_rate(hw_priv, &t->tx_info->control.rates[0]);
1457 	if (t->txpriv.use_bg_rate)
1458 		wsm->maxTxRate = (u8)(t->txpriv.use_bg_rate & 0x3f);
1459 	else
1460 		wsm->maxTxRate = t->rate->hw_value;
1461 
1462 #endif
1463 
1464 #ifdef SUPPORT_HT40
1465 
1466 	/* set the first TxRateEntry and
1467 	   clear the dataframe flag of rates_dbg_en */
1468 #ifdef CONFIG_XRADIO_DEBUGFS
1469 	if (rates_dbg_en & 0x02) {
1470 		wsm->TxRateEntry = rates_debug[0];
1471 		rates_dbg_en  &= ~0x2;
1472 	}
1473 #endif
1474 
1475 #else
1476 
1477 	/*set the maxTxRate and clear the dataframe flag of rates_dbg_en */
1478 #ifdef CONFIG_XRADIO_DEBUGFS
1479 	if (rates_dbg_en & 0x02) {
1480 		wsm->maxTxRate = maxRate_dbg;
1481 		rates_dbg_en  &= ~0x2;
1482 	}
1483 #endif
1484 
1485 	if (t->rate->flags & IEEE80211_TX_RC_MCS) {
1486 		if (priv->association_mode.greenfieldMode)
1487 			wsm->htTxParameters |=
1488 				__cpu_to_le32(WSM_HT_TX_GREENFIELD);
1489 		else
1490 			wsm->htTxParameters |=
1491 				__cpu_to_le32(WSM_HT_TX_MIXED);
1492 	}
1493 
1494 #endif
1495 
1496 	if (tx_policy_renew) {
1497 		txrx_printk(XRADIO_DBG_MSG, "[TX] TX policy renew.\n");
1498 		/* It's not so optimal to stop TX queues every now and then.
1499 		 * Maybe it's better to reimplement task scheduling with
1500 		 * a counter. */
1501 		/* xradio_tx_queues_lock(priv); */
1502 		/* Definetly better. TODO. */
1503 		if (atomic_add_return(1, &hw_priv->upload_count) == 1) {
1504 			wsm_lock_tx_async(hw_priv);
1505 			if (queue_work(hw_priv->workqueue,
1506 				  &hw_priv->tx_policy_upload_work) <= 0) {
1507 				atomic_set(&hw_priv->upload_count, 0);
1508 				wsm_unlock_tx(hw_priv);
1509 			}
1510 		}
1511 	}
1512 	return 0;
1513 }
1514 
1515 static bool
1516 xradio_tx_h_pm_state(struct xradio_vif *priv, struct xradio_txinfo *t)
1517 {
1518 	int was_buffered = 1;
1519 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1520 
1521 	if (t->txpriv.link_id == priv->link_id_after_dtim &&
1522 			!priv->buffered_multicasts) {
1523 		priv->buffered_multicasts = true;
1524 		if (priv->sta_asleep_mask)
1525 			queue_work(priv->hw_priv->workqueue,
1526 				&priv->multicast_start_work);
1527 	}
1528 
1529 	if (t->txpriv.raw_link_id && t->txpriv.tid < XRADIO_MAX_TID)
1530 		was_buffered = priv->link_id_db[t->txpriv.raw_link_id - 1]
1531 				.buffered[t->txpriv.tid]++;
1532 
1533 	return !was_buffered;
1534 }
1535 
1536 static void
1537 xradio_tx_h_ba_stat(struct xradio_vif *priv,
1538 		    struct xradio_txinfo *t)
1539 {
1540 	struct xradio_common *hw_priv = priv->hw_priv;
1541 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1542 
1543 	if (priv->join_status != XRADIO_JOIN_STATUS_STA)
1544 		return;
1545 	if (!xradio_is_ht(&hw_priv->ht_info))
1546 		return;
1547 	if (!priv->setbssparams_done)
1548 		return;
1549 	if (!ieee80211_is_data(t->hdr->frame_control))
1550 		return;
1551 
1552 	spin_lock_bh(&hw_priv->ba_lock);
1553 	hw_priv->ba_acc += t->skb->len - t->hdrlen;
1554 	if (!(hw_priv->ba_cnt_rx || hw_priv->ba_cnt)) {
1555 		mod_timer(&hw_priv->ba_timer,
1556 			jiffies + XRADIO_BLOCK_ACK_INTERVAL);
1557 	}
1558 	hw_priv->ba_cnt++;
1559 	spin_unlock_bh(&hw_priv->ba_lock);
1560 }
1561 
1562 static int
1563 xradio_tx_h_skb_pad(struct xradio_common *priv,
1564 		    struct wsm_tx *wsm,
1565 		    struct sk_buff *skb)
1566 {
1567 	size_t len = __le16_to_cpu(wsm->hdr.len);
1568 	size_t padded_len = priv->sbus_ops->align_size(priv->sbus_priv, len);
1569 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1570 
1571 	if (SYS_WARN(skb_padto(skb, padded_len) != 0)) {
1572 		return -EINVAL;
1573 	}
1574 	return 0;
1575 }
1576 
1577 /* ******************************************************************** */
1578 #if (defined(CONFIG_XRADIO_DEBUG))
1579 u16  txparse_flags;
1580 u16  rxparse_flags;
1581 #endif
1582 
1583 #if PERF_INFO_TEST
1584 struct timespec64 mac_start_time;
1585 #endif
1586 
1587 void xradio_tx(struct ieee80211_hw *dev, struct ieee80211_tx_control *control, struct sk_buff *skb)
1588 {
1589 	struct xradio_common *hw_priv = dev->priv;
1590 	struct xradio_txinfo t = {
1591 		.skb = skb,
1592 		.queue = skb_get_queue_mapping(skb),
1593 		.tx_info = IEEE80211_SKB_CB(skb),
1594 		.hdr = (struct ieee80211_hdr *)skb->data,
1595 		.txpriv.tid = XRADIO_MAX_TID,
1596 		.txpriv.rate_id = XRADIO_INVALID_RATE_ID,
1597 #ifdef P2P_MULTIVIF
1598 		.txpriv.raw_if_id = 0,
1599 #endif
1600 		.txpriv.use_bg_rate = 0,
1601 #ifdef AP_ARP_COMPAT_FIX
1602 		.txpriv.iv_len = 0,
1603 #endif
1604 	};
1605 	struct ieee80211_sta *sta = NULL;
1606 	struct wsm_tx *wsm;
1607 	bool tid_update = 0;
1608 #if PERF_INFO_TEST
1609 	int date_len = skb->len;
1610 #endif
1611 
1612 #ifndef SUPPORT_HT40
1613 
1614 	u8 flags = 0;
1615 
1616 #endif
1617 
1618 	int ret = 0;
1619 	struct xradio_vif *priv;
1620 	struct ieee80211_hdr *frame = (struct ieee80211_hdr *)skb->data;
1621 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1622 
1623 	PERF_INFO_GETTIME(&mac_start_time);
1624 	if (!skb->data)
1625 		SYS_BUG(1);
1626 
1627 #ifdef HW_RESTART
1628 	if (hw_priv->hw_restart) {
1629 		txrx_printk(XRADIO_DBG_WARN, "%s, hw in reset.\n", __func__);
1630 		ret = __LINE__;
1631 		goto drop;
1632 	}
1633 #endif
1634 
1635 	if (!(t.tx_info->control.vif)) {
1636 		ret = __LINE__;
1637 		goto drop;
1638 	}
1639 	priv = xrwl_get_vif_from_ieee80211(t.tx_info->control.vif);
1640 	if (!priv) {
1641 		ret = __LINE__;
1642 		goto drop;
1643 	}
1644 
1645 	if (atomic_read(&priv->enabled) == 0) {
1646 		ret = __LINE__;
1647 		goto drop;
1648 	}
1649 
1650 #ifdef CONFIG_XRADIO_DEBUGFS
1651 	/* parse frame for debug. */
1652 	if (txparse_flags)
1653 		xradio_parse_frame(skb->data, 0, txparse_flags, priv->if_id);
1654 #endif
1655 
1656 	/*
1657 	 * dhcp and 8021 frames are important, use b/g rate and delay scan.
1658 	 * it can make sense, such as accelerate connect.
1659 	 */
1660 	if (ieee80211_is_auth(frame->frame_control)) {
1661 		hw_priv->scan_delay_time[priv->if_id] = jiffies;
1662 		hw_priv->scan_delay_status[priv->if_id] = XRADIO_SCAN_DELAY;
1663 	} else if (ieee80211_is_data_present(frame->frame_control)) {
1664 		u8 *llc = skb->data+ieee80211_hdrlen(frame->frame_control);
1665 		if (is_dhcp(llc) || is_8021x(llc)) {
1666 			t.txpriv.use_bg_rate =
1667 			hw_priv->hw->wiphy-> \
1668 			   bands[hw_priv->channel->band]->bitrates[0].hw_value;
1669 			if (priv->vif->p2p)
1670 				t.txpriv.use_bg_rate = AG_RATE_INDEX;
1671 			t.txpriv.use_bg_rate |= 0x80;
1672 		}
1673 		if (t.txpriv.use_bg_rate) {
1674 			hw_priv->scan_delay_time[priv->if_id] = jiffies;
1675 			hw_priv->scan_delay_status[priv->if_id] = XRADIO_SCAN_DELAY;
1676 		}
1677 	} else if (ieee80211_is_deauth(frame->frame_control) ||
1678 		   ieee80211_is_disassoc(frame->frame_control)) {
1679 		hw_priv->scan_delay_status[priv->if_id] = XRADIO_SCAN_ALLOW;
1680 	}
1681 
1682 #ifdef AP_HT_COMPAT_FIX
1683 	if (ieee80211_is_assoc_req(frame->frame_control) &&
1684 		priv->if_id == 0 && !(priv->ht_compat_det & 0x10)) {
1685 		xradio_remove_ht_ie(priv, skb);
1686 	}
1687 #endif
1688 
1689 #ifdef CONFIG_XRADIO_TESTMODE
1690 	spin_lock_bh(&hw_priv->tsm_lock);
1691 	if (hw_priv->start_stop_tsm.start) {
1692 		if (hw_priv->tsm_info.ac == t.queue)
1693 			hw_priv->tsm_stats.txed_msdu_count++;
1694 	}
1695 	spin_unlock_bh(&hw_priv->tsm_lock);
1696 #endif /*CONFIG_XRADIO_TESTMODE*/
1697 
1698 #ifdef TES_P2P_0002_ROC_RESTART
1699 	xradio_frame_monitor(hw_priv, skb, true);
1700 #endif
1701 
1702 	if (ieee80211_is_action(frame->frame_control) &&
1703 		mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
1704 		u8 *action = (u8 *)&mgmt->u.action.category;
1705 		xradio_check_go_neg_conf_success(hw_priv, action);
1706 		xradio_check_prov_desc_req(hw_priv, action);
1707 	}
1708 
1709 	t.txpriv.if_id = priv->if_id;
1710 	t.hdrlen = ieee80211_hdrlen(t.hdr->frame_control);
1711 	t.da = ieee80211_get_DA(t.hdr);
1712 	if (control->sta != NULL) {
1713 		t.sta_priv = (struct xradio_sta_priv *)control->sta->drv_priv;
1714 	}
1715 	if (SYS_WARN(t.queue >= 4)) {
1716 		ret = __LINE__;
1717 		goto drop;
1718 	}
1719 
1720 	/*
1721 	spin_lock_bh(&hw_priv->tx_queue[t.queue].lock);
1722 	if ((priv->if_id == 0) &&
1723 		(hw_priv->tx_queue[t.queue].num_queued_vif[0] >=
1724 			hw_priv->vif0_throttle)) {
1725 		spin_unlock_bh(&hw_priv->tx_queue[t.queue].lock);
1726 		ret = __LINE__;
1727 		goto drop;
1728 	} else if ((priv->if_id == 1) &&
1729 		(hw_priv->tx_queue[t.queue].num_queued_vif[1] >=
1730 			hw_priv->vif1_throttle)) {
1731 		spin_unlock_bh(&hw_priv->tx_queue[t.queue].lock);
1732 		ret = __LINE__;
1733 		goto drop;
1734 	}
1735 	spin_unlock_bh(&hw_priv->tx_queue[t.queue].lock);
1736 	*/
1737 
1738 	ret = xradio_tx_h_calc_link_ids(priv, &t);
1739 	if (ret) {
1740 		ret = __LINE__;
1741 		goto drop;
1742 	}
1743 
1744 	txrx_printk(XRADIO_DBG_MSG, "[TX] TX %d bytes (if_id: %d, "
1745 			"queue: %d, link_id: %d (%d)). priv %p\n",
1746 			skb->len, priv->if_id, t.queue, t.txpriv.link_id,
1747 			t.txpriv.raw_link_id, priv);
1748 
1749 	xradio_tx_h_pm(priv, &t);
1750 	xradio_tx_h_calc_tid(priv, &t);
1751 	ret = xradio_tx_h_crypt(priv, &t);
1752 	if (ret) {
1753 		ret = __LINE__;
1754 		goto drop;
1755 	}
1756 
1757 #ifdef SUPPORT_HT40
1758 
1759 	ret = xradio_tx_h_align(priv, &t);
1760 
1761 #else
1762 
1763 	ret = xradio_tx_h_align(priv, &t, &flags);
1764 
1765 #endif
1766 
1767 	if (ret) {
1768 		ret = __LINE__;
1769 		goto drop;
1770 	}
1771 	ret = xradio_tx_h_action(priv, &t);
1772 	if (ret) {
1773 		ret = __LINE__;
1774 		goto drop;
1775 	}
1776 	wsm = xradio_tx_h_wsm(priv, &t);
1777 	if (!wsm) {
1778 		ret = __LINE__;
1779 		goto drop;
1780 	}
1781 
1782 #ifdef SUPPORT_HT40
1783 
1784 #ifdef CONFIG_XRADIO_TESTMODE
1785 	wsm->ExpireTimeSetting = WSM_TX_FLAG_EXPIRY_TIME;
1786 #endif /*CONFIG_XRADIO_TESTMODE*/
1787 
1788 #else
1789 
1790 #ifdef CONFIG_XRADIO_TESTMODE
1791 	flags |= WSM_TX_FLAG_EXPIRY_TIME;
1792 #endif /*CONFIG_XRADIO_TESTMODE*/
1793 	wsm->flags |= flags;
1794 
1795 #endif
1796 	xradio_tx_h_bt(priv, &t, wsm);
1797 	ret = xradio_tx_h_rate_policy(priv, &t, wsm);
1798 	if (ret) {
1799 		ret = __LINE__;
1800 		goto drop;
1801 	}
1802 
1803 	ret = xradio_tx_h_skb_pad(hw_priv, wsm, skb);
1804 	if (ret) {
1805 		ret = __LINE__;
1806 		goto drop;
1807 	}
1808 
1809 	rcu_read_lock();
1810 	//sta = rcu_dereference(t.tx_info->control.sta);
1811 
1812 	xradio_tx_h_ba_stat(priv, &t);
1813 	spin_lock_bh(&priv->ps_state_lock);
1814 	tid_update = xradio_tx_h_pm_state(priv, &t);
1815 	SYS_BUG(xradio_queue_put(&hw_priv->tx_queue[t.queue],
1816 			t.skb, &t.txpriv));
1817 #ifdef ROC_DEBUG
1818 	txrx_printk(XRADIO_DBG_ERROR, "QPUT %x, %pM, if_id - %d\n",
1819 		t.hdr->frame_control, t.da, priv->if_id);
1820 #endif
1821 	spin_unlock_bh(&priv->ps_state_lock);
1822 
1823 	/* To improve tcp tx in linux4.9
1824 	 * skb_orphan will tell tcp that driver has processed this skb,
1825 	 * so tcp can send other skb to driver.
1826 	 * If this is a retransmitted frame by umac, driver do not skb_orphan it again.
1827 	 */
1828 	if (!(t.tx_info->flags & IEEE80211_TX_INTFL_RETRANSMISSION))
1829 		skb_orphan(skb);
1830 
1831 #if defined(CONFIG_XRADIO_USE_EXTENSIONS)
1832 	if (tid_update && sta)
1833 		mac80211_sta_set_buffered(sta,
1834 				t.txpriv.tid, true);
1835 #endif /* CONFIG_XRADIO_USE_EXTENSIONS */
1836 
1837 	rcu_read_unlock();
1838 
1839 	xradio_proc_wakeup(hw_priv);
1840 	PERF_INFO_STAMP(&mac_start_time, &mac_tx, date_len);
1841 
1842 	return;
1843 
1844 drop:
1845 	txrx_printk(XRADIO_DBG_WARN, "drop=%d, fctl=0x%04x.\n",
1846 		    ret, frame->frame_control);
1847 	if (!(t.tx_info->flags & IEEE80211_TX_INTFL_RETRANSMISSION))
1848 		skb_orphan(skb);
1849 	xradio_skb_post_gc(hw_priv, skb, &t.txpriv);
1850 	return;
1851 }
1852 
1853 /* ******************************************************************** */
1854 
1855 static int xradio_handle_pspoll(struct xradio_vif *priv,
1856 				struct sk_buff *skb)
1857 {
1858 	struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
1859 	struct ieee80211_sta *sta;
1860 	struct ieee80211_pspoll *pspoll =
1861 		(struct ieee80211_pspoll *) skb->data;
1862 	int link_id = 0;
1863 	u32 pspoll_mask = 0;
1864 	int drop = 1;
1865 	int i;
1866 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
1867 
1868 	if (priv->join_status != XRADIO_JOIN_STATUS_AP)
1869 		goto done;
1870 	if (memcmp(priv->vif->addr, pspoll->bssid, ETH_ALEN))
1871 		goto done;
1872 
1873 	rcu_read_lock();
1874 	sta = mac80211_find_sta(priv->vif, pspoll->ta);
1875 	if (sta) {
1876 		struct xradio_sta_priv *sta_priv;
1877 		sta_priv = (struct xradio_sta_priv *)&sta->drv_priv;
1878 		link_id = sta_priv->link_id;
1879 		pspoll_mask = BIT(sta_priv->link_id);
1880 	}
1881 	rcu_read_unlock();
1882 	if (!link_id)
1883 		goto done;
1884 
1885 	priv->pspoll_mask |= pspoll_mask;
1886 	drop = 0;
1887 
1888 	/* Do not report pspols if data for given link id is
1889 	 * queued already. */
1890 	for (i = 0; i < 4; ++i) {
1891 		if (xradio_queue_get_num_queued(priv,
1892 				&hw_priv->tx_queue[i],
1893 				pspoll_mask)) {
1894 			xradio_proc_wakeup(hw_priv);
1895 			drop = 1;
1896 			break;
1897 		}
1898 	}
1899 	txrx_printk(XRADIO_DBG_NIY, "[RX] PSPOLL: %s\n", drop ? "local" : "fwd");
1900 done:
1901 	return drop;
1902 }
1903 
1904 /* ******************************************************************** */
1905 extern u32 tx_retrylimit;
1906 extern u32 tx_over_limit;
1907 extern u32 tx_lower_limit;
1908 extern int retry_mis;
1909 
1910 #ifdef SUPPORT_HT40
1911 
1912 void xradio_get_ieee80211_tx_rate(struct xradio_common *hw_priv,
1913 	u16 RateEntry, struct ieee80211_tx_rate *rates)
1914 {
1915 	if (GET_RATE_ENTRY_RATEINDEX(RateEntry) == FW_RATE_USE_DEFAULT) {
1916 		rates->idx	= -1;
1917 		rates->count	= 0;
1918 		rates->flags	= 0;
1919 
1920 		return;
1921 	}
1922 
1923 	if (GET_RATE_ENTRY_MODEM(RateEntry) == FW_RATE_MODEM_HTOFDM) {
1924 		rates->idx	= GET_RATE_ENTRY_RATEINDEX(RateEntry);
1925 		rates->count	= GET_RATE_ENTRY_MAXRETRY(RateEntry);
1926 		rates->flags	= IEEE80211_TX_RC_MCS;
1927 
1928 		if (GET_RATE_ENTRY_BANDWIDTH(RateEntry) == FW_RATE_BW_40M)
1929 			rates->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
1930 
1931 		if (GET_RATE_ENTRY_FLAGS(RateEntry) & FW_RATE_F_GF)
1932 			rates->flags |= IEEE80211_TX_RC_GREEN_FIELD;
1933 
1934 		if (GET_RATE_ENTRY_FLAGS(RateEntry) & FW_RATE_F_SGI)
1935 			rates->flags |= IEEE80211_TX_RC_SHORT_GI;
1936 	} else {
1937 		rates->idx = LegacyRxedRateLut[hw_priv->channel->band] \
1938 					[GET_RATE_ENTRY_RATEINDEX(RateEntry)];
1939 		rates->count = GET_RATE_ENTRY_MAXRETRY(RateEntry);
1940 
1941 		if (GET_RATE_ENTRY_FLAGS(RateEntry) & FW_RATE_F_SPRE)
1942 			rates->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
1943 	}
1944 
1945 	return;
1946 }
1947 
1948 #endif
1949 
1950 void xradio_tx_confirm_cb(struct xradio_common *hw_priv,
1951 			  struct wsm_tx_confirm *arg)
1952 {
1953 	u8 queue_id = xradio_queue_get_queue_id(arg->packetID);
1954 	struct xradio_queue *queue = &hw_priv->tx_queue[queue_id];
1955 	struct sk_buff *skb;
1956 	const struct xradio_txpriv *txpriv;
1957 	struct xradio_vif *priv;
1958 	u32    feedback_retry = 0;
1959 
1960 #ifdef SUPPORT_HT40
1961 
1962 	if (arg->status) {
1963 		txrx_printk(XRADIO_DBG_WARN, "status=%d, retry=%d, lastRate=0x%04x\n",
1964 			    arg->status, arg->ackFailures, arg->txedRateEntry);
1965 	} else {
1966 		txrx_printk(XRADIO_DBG_MSG, "status=%d, retry=%d, lastRate=0x%04x\n",
1967 			    arg->status, arg->ackFailures, arg->txedRateEntry);
1968 	}
1969 
1970 
1971 #else
1972 
1973 	if (arg->status) {
1974 		txrx_printk(XRADIO_DBG_NIY, "status=%d, retry=%d, lastRate=%d\n",
1975 			    arg->status, arg->ackFailures, arg->txedRate);
1976 	} else {
1977 		txrx_printk(XRADIO_DBG_MSG, "status=%d, retry=%d, lastRate=%d\n",
1978 			    arg->status, arg->ackFailures, arg->txedRate);
1979 	}
1980 
1981 #endif
1982 
1983 #ifdef TES_P2P_0002_ROC_RESTART
1984 	if ((TES_P2P_0002_state == TES_P2P_0002_STATE_GET_PKTID) &&
1985 		(arg->packetID == TES_P2P_0002_packet_id)) {
1986 		if (arg->status == 0x00) {
1987 
1988 			struct timespec64 TES_P2P_0002_tmval;
1989 			s32 TES_P2P_0002_roc_time;
1990 			s32 TES_P2P_0002_now_sec;
1991 			s32 TES_P2P_0002_now_usec;
1992 			bool TES_P2P_0002_roc_rst_need;
1993 
1994 			xr_do_gettimeofday(&TES_P2P_0002_tmval);
1995 			TES_P2P_0002_roc_rst_need	= false;
1996 			TES_P2P_0002_now_sec = (s32)(TES_P2P_0002_tmval.tv_sec);
1997 			TES_P2P_0002_now_usec = (s32)(TES_P2P_0002_tmval.tv_nsec / 1000);
1998 			TES_P2P_0002_roc_time = TES_P2P_0002_roc_dur -
1999 			    (((TES_P2P_0002_now_sec - TES_P2P_0002_roc_sec) * 1000) +
2000 			    ((TES_P2P_0002_now_usec - TES_P2P_0002_roc_usec) / 1000));
2001 
2002 			/* tx rsp to rx cfm will need more than 60ms */
2003 			if (TES_P2P_0002_roc_time < 100) {
2004 				TES_P2P_0002_roc_time = 100;
2005 				TES_P2P_0002_roc_rst_need = true;
2006 			}
2007 
2008 			if (TES_P2P_0002_roc_rst_need == true) {
2009 				txrx_printk(XRADIO_DBG_WARN,
2010 					    "[ROC RESTART ACTIVE ON][Confirm CallBack]");
2011 				cancel_delayed_work_sync(&hw_priv->rem_chan_timeout);
2012 				if (atomic_read(&hw_priv->remain_on_channel)) {
2013 					queue_delayed_work(hw_priv->spare_workqueue,
2014 							   &hw_priv->rem_chan_timeout,
2015 							   (TES_P2P_0002_roc_time) * HZ / 1000);
2016 				}
2017 			}
2018 		}
2019 		TES_P2P_0002_state = TES_P2P_0002_STATE_IDLE;
2020 		txrx_printk(XRADIO_DBG_NIY,
2021 			    "[ROC_RESTART_STATE_IDLE][Confirm CallBack]");
2022 	}
2023 #endif
2024 
2025 	if (unlikely(xradio_itp_tx_running(hw_priv)))
2026 		return;
2027 
2028 	priv = xrwl_hwpriv_to_vifpriv(hw_priv, arg->if_id);
2029 	if (unlikely(!priv))
2030 		return;
2031 	if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED)) {
2032 		/* STA is stopped. */
2033 		spin_unlock(&priv->vif_lock);
2034 		return;
2035 	}
2036 
2037 	if (SYS_WARN(queue_id >= 4)) {
2038 		spin_unlock(&priv->vif_lock);
2039 		return;
2040 	}
2041 
2042 #ifdef CONFIG_XRADIO_TESTMODE
2043 	spin_lock_bh(&hw_priv->tsm_lock);
2044 	if ((arg->status == WSM_STATUS_RETRY_EXCEEDED) ||
2045 	    (arg->status == WSM_STATUS_TX_LIFETIME_EXCEEDED)) {
2046 		hw_priv->tsm_stats.msdu_discarded_count++;
2047 	} else if ((hw_priv->start_stop_tsm.start) &&
2048 		(arg->status == WSM_STATUS_SUCCESS)) {
2049 		if (queue_id == hw_priv->tsm_info.ac) {
2050 			struct timespec64 tmval;
2051 			xr_do_gettimeofday(&tmval);
2052 			u16 pkt_delay =
2053 				hw_priv->start_stop_tsm.packetization_delay;
2054 			if (hw_priv->tsm_info.sta_roamed &&
2055 			    !hw_priv->tsm_info.use_rx_roaming) {
2056 				hw_priv->tsm_info.roam_delay = tmval.tv_nsec / 1000 -
2057 				hw_priv->tsm_info.txconf_timestamp_vo;
2058 				if (hw_priv->tsm_info.roam_delay > pkt_delay)
2059 					hw_priv->tsm_info.roam_delay -= pkt_delay;
2060 				txrx_printk(XRADIO_DBG_MSG, "[TX] txConf"
2061 				"Roaming: roam_delay = %u\n",
2062 				hw_priv->tsm_info.roam_delay);
2063 				hw_priv->tsm_info.sta_roamed = 0;
2064 			}
2065 			hw_priv->tsm_info.txconf_timestamp_vo = tmval.tv_nsec / 1000;
2066 		}
2067 	}
2068 	spin_unlock_bh(&hw_priv->tsm_lock);
2069 #endif /*CONFIG_XRADIO_TESTMODE*/
2070 	if ((arg->status == WSM_REQUEUE) &&
2071 	    (arg->flags & WSM_TX_STATUS_REQUEUE)) {
2072 		/* "Requeue" means "implicit suspend" */
2073 		struct wsm_suspend_resume suspend = {
2074 			.link_id = arg->link_id,
2075 			.stop = 1,
2076 			.multicast = !arg->link_id,
2077 			.if_id = arg->if_id,
2078 		};
2079 		xradio_suspend_resume(priv, &suspend);
2080 		txrx_printk(XRADIO_DBG_NIY, "Requeue for link_id %d (try %d)."
2081 			" STAs asleep: 0x%.8X\n",
2082 			arg->link_id,
2083 			xradio_queue_get_generation(arg->packetID) + 1,
2084 			priv->sta_asleep_mask);
2085 #ifdef CONFIG_XRADIO_TESTMODE
2086 		SYS_WARN(xradio_queue_requeue(hw_priv, queue,
2087 				arg->packetID, true));
2088 #else
2089 		SYS_WARN(xradio_queue_requeue(queue,
2090 				arg->packetID, true));
2091 #endif
2092 		spin_lock_bh(&priv->ps_state_lock);
2093 		if (!arg->link_id) {
2094 			priv->buffered_multicasts = true;
2095 			if (priv->sta_asleep_mask) {
2096 				queue_work(hw_priv->workqueue,
2097 					&priv->multicast_start_work);
2098 			}
2099 		}
2100 		spin_unlock_bh(&priv->ps_state_lock);
2101 		spin_unlock(&priv->vif_lock);
2102 	} else if (!xradio_queue_get_skb(
2103 			queue, arg->packetID, &skb, &txpriv)) {
2104 		struct ieee80211_tx_info *tx = IEEE80211_SKB_CB(skb);
2105 		struct ieee80211_hdr *frame =
2106 		    (struct ieee80211_hdr *)&skb->data[txpriv->offset];
2107 		int tx_count = arg->ackFailures;
2108 		u8 ht_flags = 0;
2109 
2110 		/*
2111 		 * reset if_0 in firmware when STA-unjoined,
2112 		 * fix the errors when switch APs in combo mode.
2113 		 */
2114 		if (unlikely(ieee80211_is_disassoc(frame->frame_control) ||
2115 			  ieee80211_is_deauth(frame->frame_control))) {
2116 			if (priv->join_status == XRADIO_JOIN_STATUS_STA) {
2117 				wsm_send_deauth_to_self(hw_priv, priv);
2118 				/* Shedule unjoin work */
2119 				txrx_printk(XRADIO_DBG_WARN,
2120 					    "Issue unjoin command(TX) by self.\n");
2121 				if (cancel_delayed_work(&priv->unjoin_delayed_work)) {
2122 					wsm_lock_tx_async(hw_priv);
2123 					if (queue_work(hw_priv->workqueue, &priv->unjoin_work) <= 0)
2124 						wsm_unlock_tx(hw_priv);
2125 				}
2126 			}
2127 		}
2128 
2129 #ifdef ROC_DEBUG
2130 #ifndef P2P_MULTIVIF
2131 		if (txpriv->offchannel_if_id)
2132 			txrx_printk(XRADIO_DBG_ERROR, "TX CONFIRM %x - %d - %d\n",
2133 				skb->data[txpriv->offset],
2134 				txpriv->offchannel_if_id, arg->status);
2135 #else
2136 		if (txpriv->if_id)
2137 			txrx_printk(XRADIO_DBG_ERROR, "TX CONFIRM %x - %d - %d\n",
2138 				skb->data[txpriv->offset],
2139 				txpriv->raw_if_id, arg->status);
2140 #endif
2141 #endif
2142 
2143 #ifdef SUPPORT_HT40
2144 
2145 		if (priv->association_mode.PhyModeCfg.GF_Enable)
2146 			ht_flags |= IEEE80211_TX_RC_GREEN_FIELD;
2147 
2148 #else
2149 
2150 		if (priv->association_mode.greenfieldMode)
2151 			ht_flags |= IEEE80211_TX_RC_GREEN_FIELD;
2152 
2153 #endif
2154 
2155 		/* bss loss confirm. */
2156 		if (unlikely(priv->bss_loss_status == XRADIO_BSS_LOSS_CONFIRMING &&
2157 		    priv->bss_loss_confirm_id == arg->packetID)) {
2158 			spin_lock(&priv->bss_loss_lock);
2159 			priv->bss_loss_status = arg->status ? XRADIO_BSS_LOSS_CONFIRMED :
2160 						    XRADIO_BSS_LOSS_NONE;
2161 			spin_unlock(&priv->bss_loss_lock);
2162 		}
2163 
2164 /*when less ap can't reply arp request accidentally,
2165 *then disconnect actively,
2166 *and wait system trigger reconnect again.
2167 */
2168 #ifdef AP_ARP_COMPAT_FIX
2169 		if (likely(!arg->status) &&
2170 			(priv->join_status == XRADIO_JOIN_STATUS_STA) &&
2171 			(ieee80211_is_data(frame->frame_control))) {
2172 				u8 machdrlen = ieee80211_hdrlen(frame->frame_control);
2173 				u8 *llc_data = (u8 *)frame + machdrlen + txpriv->iv_len;
2174 				if (is_SNAP(llc_data) && is_arp(llc_data)) {
2175 					u8 *arp_hdr = llc_data + LLC_LEN;
2176 					u16 *arp_type = (u16 *)(arp_hdr + ARP_TYPE_OFFSET);
2177 					if (*arp_type == cpu_to_be16(ARP_REQUEST))
2178 						priv->arp_compat_cnt++;
2179 					if (priv->arp_compat_cnt > 10) {
2180 						txrx_printk(XRADIO_DBG_ERROR,
2181 							"ap don't reply arp resp count=%d\n",
2182 							priv->arp_compat_cnt);
2183 						priv->arp_compat_cnt = 0;
2184 						wsm_send_disassoc_to_self(hw_priv, priv);
2185 					}
2186 				}
2187 		}
2188 #endif
2189 
2190 		if (likely(!arg->status)) {
2191 			tx->flags |= IEEE80211_TX_STAT_ACK;
2192 			priv->cqm_tx_failure_count = 0;
2193 			++tx_count;
2194 #ifdef SUPPORT_HT40
2195 
2196 			if (((arg->txedRateEntry >> MODEMTYPE_SHIFT)
2197 				& MODEMTYPE_MASK) == RATE_MODEM_HTOFDM) {
2198 
2199 				if (((arg->txedRateEntry >> BANDWIDTH_SHIFT)
2200 					& BANDWIDTH_MASK)
2201 					== RATE_BANDWIDTH_20M) {
2202 					if (arg->txedRateEntry & RATE_F_SGI) {
2203 						u8 index = (arg->txedRateEntry
2204 							>> RATEINDEX_SHIFT)
2205 							& RATEINDEX_MASK;
2206 
2207 						TxedHtofdmRateMap[0][index]++;
2208 					} else {
2209 						u8 index = (arg->txedRateEntry
2210 							>> RATEINDEX_SHIFT)
2211 							& RATEINDEX_MASK;
2212 
2213 						TxedHtofdmRateMap[1][index]++;
2214 					}
2215 				} else {
2216 					if (arg->txedRateEntry & RATE_F_SGI) {
2217 						u8 index = (arg->txedRateEntry
2218 							>> RATEINDEX_SHIFT)
2219 							& RATEINDEX_MASK;
2220 
2221 						TxedHtofdmRateMap[2][index]++;
2222 					} else {
2223 						u8 index = (arg->txedRateEntry
2224 							>> RATEINDEX_SHIFT)
2225 							& RATEINDEX_MASK;
2226 
2227 						TxedHtofdmRateMap[3][index]++;
2228 					}
2229 				}
2230 			} else {
2231 				u8 index =
2232 				(arg->txedRateEntry >> RATEINDEX_SHIFT)
2233 				& RATEINDEX_MASK;
2234 
2235 				if (index < 6) {
2236 					if (arg->txedRateEntry
2237 						& RATE_F_PREAMBLE_S) {
2238 						TxedLegacyRateMap[0] \
2239 								[index*2+1]++;
2240 					} else {
2241 						TxedLegacyRateMap[0] \
2242 								[index*2]++;
2243 					}
2244 				} else {
2245 					TxedLegacyRateMap[1][index-6]++;
2246 				}
2247 			}
2248 
2249 #else
2250 
2251 			if (arg->txedRate < 24)
2252 				TxedRateIdx_Map[arg->txedRate]++;
2253 			else
2254 				SYS_WARN(1);
2255 
2256 #endif
2257 
2258 			xradio_debug_txed(priv);
2259 			if (arg->flags & WSM_TX_STATUS_AGGREGATION) {
2260 				/* Do not report aggregation to mac80211:
2261 				 * it confuses minstrel a lot. */
2262 				/* tx->flags |= IEEE80211_TX_STAT_AMPDU; */
2263 				xradio_debug_txed_agg(priv);
2264 			}
2265 		} else {
2266 			/* TODO: Update TX failure counters */
2267 			if (unlikely(priv->cqm_tx_failure_thold &&
2268 			     (++priv->cqm_tx_failure_count >
2269 			      priv->cqm_tx_failure_thold))) {
2270 				priv->cqm_tx_failure_thold = 0;
2271 				queue_work(hw_priv->workqueue,
2272 						&priv->tx_failure_work);
2273 			}
2274 			if (tx_count)
2275 				++tx_count;
2276 		}
2277 		spin_unlock(&priv->vif_lock);
2278 
2279 		tx->status.ampdu_len = 1;
2280 		tx->status.ampdu_ack_len = 1;
2281 
2282 #if 0
2283 		tx_count = arg->ackFailures+1;
2284 		for (i = 0; i < IEEE80211_TX_MAX_RATES; ++i) {
2285 			if (tx->status.rates[i].count >= tx_count) {
2286 				tx->status.rates[i].count = tx_count;
2287 				if (likely(!arg->status)) {
2288 					s8 txed_idx = xradio_get_rate_idx(hw_priv,
2289 							 tx->status.rates[i].flags,
2290 							 arg->txedRate);
2291 					if (tx->status.rates[i].idx != txed_idx) {
2292 						if (i < (IEEE80211_TX_MAX_RATES-1)) {
2293 							i++;
2294 							tx->status.rates[i].idx   = txed_idx;
2295 							tx->status.rates[i].count = 1;
2296 						} else if (txed_idx >= 0) {
2297 							tx->status.rates[i].idx   = txed_idx;
2298 							tx->status.rates[i].count = 1;
2299 						}
2300 					}
2301 				}
2302 				break;
2303 			}
2304 			tx_count -= tx->status.rates[i].count;
2305 			if (tx->status.rates[i].flags & IEEE80211_TX_RC_MCS)
2306 				tx->status.rates[i].flags |= ht_flags;
2307 		}
2308 
2309 		for (++i; i < IEEE80211_TX_MAX_RATES; ++i) {
2310 			tx->status.rates[i].count = 0;
2311 			tx->status.rates[i].idx = -1;
2312 		}
2313 
2314 #else
2315 
2316 #ifdef SUPPORT_HT40
2317 
2318 		if (txpriv->use_bg_rate) {
2319 			tx->status.rates[0].count = arg->ackFailures+1;
2320 			tx->status.rates[0].idx = 0;
2321 			tx->status.rates[1].idx = -1;
2322 			tx->status.rates[2].idx = -1;
2323 			tx->status.rates[3].idx = -1;
2324 			//tx->status.rates[4].idx = -1;
2325 		} else {
2326 			u8 RateTryIdx = 0x0;
2327 			u8 BreakIdx = 0x0;
2328 			bool IsFind = false;
2329 			bool IsBreak = false;
2330 
2331 			for (RateTryIdx = 0; RateTryIdx < IEEE80211_TX_MAX_RATES; RateTryIdx++) {
2332 
2333 				if (!IsBreak)
2334 					BreakIdx = RateTryIdx;
2335 
2336 				if (GET_RATE_ENTRY_RATEINDEX(arg->RateTry[RateTryIdx]) == FW_RATE_USE_DEFAULT)
2337 					IsBreak = true;
2338 
2339 				if (IsBreak) {
2340 					tx->status.rates[RateTryIdx].idx = -1;
2341 					tx->status.rates[RateTryIdx].count = 0;
2342 					tx->status.rates[RateTryIdx].flags = 0;
2343 				} else {
2344 					xradio_get_ieee80211_tx_rate(hw_priv,
2345 						arg->RateTry[RateTryIdx], &tx->status.rates[RateTryIdx]);
2346 					if ((!arg->status) && (!IsFind)) {
2347 						if ((arg->RateTry[RateTryIdx]&0xFFF0) == (arg->txedRateEntry&0xFFF0)) {
2348 							tx->status.rates[RateTryIdx].count++;
2349 							IsFind = true;
2350 						}
2351 					}
2352 				}
2353 			}
2354 
2355 			if ((!arg->status) && (!IsFind)) {
2356 				if (!IsBreak)
2357 					BreakIdx = 4;
2358 
2359 				xradio_get_ieee80211_tx_rate(hw_priv, arg->txedRateEntry, &tx->status.rates[BreakIdx]);
2360 				tx->status.rates[BreakIdx].count = 1;
2361 			}
2362 		}
2363 
2364 #else
2365 		txrx_printk(XRADIO_DBG_MSG,
2366 			    "feedback:%08x, %08x, %08x.\n",
2367 			     arg->rate_try[2], arg->rate_try[1], arg->rate_try[0]);
2368 		if (txpriv->use_bg_rate) {   /* bg rates */
2369 			tx->status.rates[0].count = arg->ackFailures+1;
2370 			tx->status.rates[0].idx   = 0;
2371 			tx->status.rates[1].idx   = -1;
2372 			tx->status.rates[2].idx   = -1;
2373 			tx->status.rates[3].idx   = -1;
2374 			//tx->status.rates[4].idx   = -1;
2375 		} else {
2376 			int i;
2377 			int j;
2378 			s8  txed_idx;
2379 			register u8 rate_num = 0, shift = 0, retries = 0;
2380 			u8  flag = tx->status.rates[0].flags;
2381 
2382 			/* get retry rate idx. */
2383 			for (i = 2; i >= 0; i--) {
2384 				if (arg->rate_try[i]) {
2385 					for (j = 7; j >= 0; j--) {
2386 						shift   = j<<2;
2387 						retries = (arg->rate_try[i]>>shift) & 0xf;
2388 						if (retries) {
2389 							feedback_retry += retries;
2390 							txed_idx = xradio_get_rate_idx(hw_priv, flag,
2391 										       ((i<<3) + j));
2392 							txrx_printk(XRADIO_DBG_MSG,
2393 								    "rate_num=%d, hw=%d, idx=%d, "
2394 								    "retries=%d, flag=%d",
2395 								    rate_num, ((i<<3)+j),
2396 								    txed_idx, retries, flag);
2397 							if (likely(txed_idx >= 0)) {
2398 								tx->status.rates[rate_num].idx   = txed_idx;
2399 								tx->status.rates[rate_num].count = retries;
2400 								if (tx->status.rates[rate_num].flags &
2401 									IEEE80211_TX_RC_MCS)
2402 									tx->status.rates[rate_num].flags |=
2403 									    ht_flags;
2404 								rate_num++;
2405 								if (rate_num >= IEEE80211_TX_MAX_RATES) {
2406 									i = -1;
2407 									break;
2408 								}
2409 							}
2410 						}
2411 					}
2412 				}
2413 			}
2414 
2415 			/* If there is 11b rates in 11n mode, put it into MCS0 */
2416 			if ((arg->rate_try[0]&0xffff) && (flag & IEEE80211_TX_RC_MCS)) {
2417 				int br_retrys = 0;
2418 				for (i = 0; i < 16; i += 4)
2419 					br_retrys += ((arg->rate_try[0]>>i)&0xf);
2420 				if (rate_num > 0 && tx->status.rates[rate_num-1].idx == 0) {
2421 					tx->status.rates[rate_num-1].count += br_retrys;
2422 				} else if (rate_num < IEEE80211_TX_MAX_RATES) {
2423 					tx->status.rates[rate_num].idx   = 0;
2424 					tx->status.rates[rate_num].count += br_retrys;
2425 					rate_num++;
2426 				}
2427 			}
2428 
2429 			/* clear other rate. */
2430 			for (i = rate_num; i < IEEE80211_TX_MAX_RATES; ++i) {
2431 				tx->status.rates[i].count = 0;
2432 				tx->status.rates[i].idx = -1;
2433 			}
2434 			/* get successful rate idx. */
2435 			if (!arg->status) {
2436 				txed_idx = xradio_get_rate_idx(hw_priv, flag, arg->txedRate);
2437 				if (rate_num == 0) {
2438 					tx->status.rates[0].idx = txed_idx;
2439 					tx->status.rates[0].count = 1;
2440 				} else if (rate_num <= IEEE80211_TX_MAX_RATES) {
2441 					--rate_num;
2442 					if (txed_idx == tx->status.rates[rate_num].idx) {
2443 						tx->status.rates[rate_num].count += 1;
2444 					} else if (rate_num < (IEEE80211_TX_MAX_RATES-1)) {
2445 						++rate_num;
2446 						tx->status.rates[rate_num].idx   = txed_idx;
2447 						tx->status.rates[rate_num].count = 1;
2448 					} else if (txed_idx >= 0) {
2449 						tx->status.rates[rate_num].idx   = txed_idx;
2450 						tx->status.rates[rate_num].count = 1;
2451 					}
2452 				}
2453 			}
2454 		}
2455 
2456 #endif
2457 
2458 #endif
2459 
2460 #ifdef CONFIG_XRADIO_DEBUGFS
2461 		if (arg->status == WSM_STATUS_RETRY_EXCEEDED) {
2462 			tx_retrylimit++;
2463 			retry_mis += ((s32)hw_priv->short_frame_max_tx_count -
2464 				       arg->ackFailures-1);
2465 			if (arg->ackFailures != (hw_priv->short_frame_max_tx_count-1)) {
2466 				if (arg->ackFailures < (hw_priv->short_frame_max_tx_count-1))
2467 					tx_lower_limit++;
2468 				else
2469 					tx_over_limit++;
2470 				txrx_printk(XRADIO_DBG_NIY,
2471 					    "retry_err, ackFailures=%d, feedbk_retry=%d.\n",
2472 					    arg->ackFailures, feedback_retry);
2473 			}
2474 		} else if (feedback_retry > hw_priv->short_frame_max_tx_count-1) {
2475 			tx_over_limit++;
2476 			txrx_printk(XRADIO_DBG_WARN,
2477 				    "status=%d, ackFailures=%d, feedbk_retry=%d.\n",
2478 				    arg->status, arg->ackFailures, feedback_retry);
2479 		}
2480 #endif
2481 
2482 		txrx_printk(XRADIO_DBG_MSG, "[TX policy] Ack: " \
2483 		"%d:%d, %d:%d, %d:%d, %d:%d\n",
2484 		tx->status.rates[0].idx, tx->status.rates[0].count,
2485 		tx->status.rates[1].idx, tx->status.rates[1].count,
2486 		tx->status.rates[2].idx, tx->status.rates[2].count,
2487 		tx->status.rates[3].idx, tx->status.rates[3].count);
2488 		//tx->status.rates[4].idx, tx->status.rates[4].count);
2489 
2490 #ifdef CONFIG_XRADIO_TESTMODE
2491 		xradio_queue_remove(hw_priv, queue, arg->packetID);
2492 #else
2493 		xradio_queue_remove(queue, arg->packetID);
2494 #endif /*CONFIG_XRADIO_TESTMODE*/
2495 	} else {
2496 		spin_unlock(&priv->vif_lock);
2497 		txrx_printk(XRADIO_DBG_WARN,
2498 			"%s xradio_queue_get_skb failed.\n", __func__);
2499 	}
2500 }
2501 
2502 #if defined(CONFIG_XRADIO_USE_EXTENSIONS)
2503 static void xradio_set_skb_eosp(struct sk_buff *skb)
2504 {
2505 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2506 	info->flags |= IEEE80211_TX_STATUS_EOSP;
2507 }
2508 #endif
2509 
2510 static void xradio_notify_buffered_tx(struct xradio_vif *priv,
2511 			       struct sk_buff *skb, int link_id, int tid)
2512 {
2513 #if defined(CONFIG_XRADIO_USE_EXTENSIONS)
2514 	struct ieee80211_sta *sta;
2515 	struct ieee80211_hdr *hdr;
2516 	u8 *buffered;
2517 	u8 still_buffered = 0;
2518 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
2519 
2520 	if (link_id && tid < XRADIO_MAX_TID) {
2521 		buffered = priv->link_id_db
2522 				[link_id - 1].buffered;
2523 
2524 		spin_lock_bh(&priv->ps_state_lock);
2525 		if (!SYS_WARN(!buffered[tid]))
2526 			still_buffered = --buffered[tid];
2527 		spin_unlock_bh(&priv->ps_state_lock);
2528 
2529 		if (!still_buffered && tid < XRADIO_MAX_TID) {
2530 			hdr = (struct ieee80211_hdr *) skb->data;
2531 			rcu_read_lock();
2532 			sta = mac80211_find_sta(priv->vif, hdr->addr1);
2533 			if (sta) {
2534 				mac80211_sta_set_buffered(sta, tid, false);
2535 				xradio_set_skb_eosp(skb);
2536 			}
2537 			rcu_read_unlock();
2538 		}
2539 	}
2540 #endif /* CONFIG_XRADIO_USE_EXTENSIONS */
2541 }
2542 
2543 void xradio_skb_dtor(struct xradio_common *hw_priv,
2544 		     struct sk_buff *skb,
2545 		     const struct xradio_txpriv *txpriv)
2546 {
2547 	struct xradio_vif *priv =
2548 		__xrwl_hwpriv_to_vifpriv(hw_priv, txpriv->if_id);
2549 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
2550 
2551 	skb_pull(skb, txpriv->offset);
2552 	if (priv && txpriv->rate_id != XRADIO_INVALID_RATE_ID) {
2553 		xradio_notify_buffered_tx(priv, skb,
2554 				txpriv->raw_link_id, txpriv->tid);
2555 		tx_policy_put(hw_priv, txpriv->rate_id);
2556 	}
2557 	if (likely(!xradio_is_itp(hw_priv))) {
2558 		down(&hw_priv->dtor_lock);
2559 		mac80211_tx_status(hw_priv->hw, skb);
2560 		up(&hw_priv->dtor_lock);
2561 	}
2562 }
2563 #ifdef CONFIG_XRADIO_TESTMODE
2564 /* TODO It should be removed before official delivery */
2565 static void frame_hexdump(char *prefix, u8 *data, int len)
2566 {
2567 	int i;
2568 
2569 	txrx_printk(XRADIO_DBG_MSG, "%s hexdump:\n", prefix);
2570 	for (i = 0; i < len; i++) {
2571 		if (i + 10 < len) {
2572 			txrx_printk(XRADIO_DBG_MSG, "%.1X %.1X %.1X %.1X" \
2573 				"%.1X %.1X %.1X %.1X %.1X %.1X",
2574 				data[i], data[i+1], data[i+2],
2575 				data[i+3], data[i+4], data[i+5],
2576 				data[i+6], data[i+7], data[i+8],
2577 				data[i+9]);
2578 			i += 9;
2579 		} else {
2580 			txrx_printk(XRADIO_DBG_MSG, "%.1X ", data[i]);
2581 		}
2582 	}
2583 }
2584 /**
2585  * c1200_tunnel_send_testmode_data - Send test frame to the driver
2586  *
2587  * @priv: pointer to xradio private structure
2588  * @skb: skb with frame
2589  *
2590  * Returns: 0 on success or non zero value on failure
2591  */
2592 static int xradio_tunnel_send_testmode_data(struct xradio_common *hw_priv,
2593 					    struct sk_buff *skb)
2594 {
2595 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
2596 	if (xradio_tesmode_event(hw_priv->hw->wiphy, XR_MSG_EVENT_FRAME_DATA,
2597 				 skb->data, skb->len, GFP_ATOMIC))
2598 		return -EINVAL;
2599 
2600 	return 0;
2601 }
2602 
2603 /**
2604  * xradio_frame_test_detection - Detection frame_test
2605  *
2606  * @priv: pointer to xradio vif structure
2607  * @frame: ieee80211 header
2608  * @skb: skb with frame
2609  *
2610  * Returns: 1 - frame test detected, 0 - not detected
2611  */
2612 static int xradio_frame_test_detection(struct xradio_vif *priv,
2613 				       struct ieee80211_hdr *frame,
2614 				       struct sk_buff *skb)
2615 {
2616 	struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
2617 	int hdrlen = ieee80211_hdrlen(frame->frame_control);
2618 	int detected = 0;
2619 	int ret;
2620 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
2621 
2622 	if (hdrlen + hw_priv->test_frame.len <= skb->len &&
2623 	    memcmp(skb->data + hdrlen, hw_priv->test_frame.data,
2624 		   hw_priv->test_frame.len) == 0) {
2625 		detected = 1;
2626 		txrx_printk(XRADIO_DBG_MSG, "TEST FRAME detected");
2627 		frame_hexdump("TEST FRAME original:", skb->data, skb->len);
2628 		ret = ieee80211_data_to_8023(skb, hw_priv->mac_addr,
2629 				priv->mode);
2630 		if (!ret) {
2631 			frame_hexdump("FRAME 802.3:", skb->data, skb->len);
2632 			ret = xradio_tunnel_send_testmode_data(hw_priv, skb);
2633 		}
2634 		if (ret)
2635 			txrx_printk(XRADIO_DBG_ERROR, "Send TESTFRAME failed(%d)", ret);
2636 	}
2637 	return detected;
2638 }
2639 #endif /* CONFIG_XRADIO_TESTMODE */
2640 
2641 
2642 static void
2643 xradio_rx_h_ba_stat(struct xradio_vif *priv,
2644 		    size_t hdrlen, size_t skb_len)
2645 {
2646 	struct xradio_common *hw_priv = priv->hw_priv;
2647 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
2648 
2649 	if (priv->join_status != XRADIO_JOIN_STATUS_STA)
2650 		return;
2651 	if (!xradio_is_ht(&hw_priv->ht_info))
2652 		return;
2653 	if (!priv->setbssparams_done)
2654 		return;
2655 
2656 	spin_lock_bh(&hw_priv->ba_lock);
2657 	hw_priv->ba_acc_rx += skb_len - hdrlen;
2658 	if (!(hw_priv->ba_cnt_rx || hw_priv->ba_cnt)) {
2659 		mod_timer(&hw_priv->ba_timer,
2660 			jiffies + XRADIO_BLOCK_ACK_INTERVAL);
2661 	}
2662 	hw_priv->ba_cnt_rx++;
2663 	spin_unlock_bh(&hw_priv->ba_lock);
2664 }
2665 
2666 #if 0
2667 u8 nettest_bssid[] = {0x00, 0x02, 0x03, 0x04, 0x05, 0x06};
2668 u8 save_rate_ie;
2669 #endif
2670 #if PERF_INFO_TEST
2671 struct timespec64 upper_rx_time;
2672 size_t upper_rx_size;
2673 #endif
2674 
2675 void xradio_rx_cb(struct xradio_vif *priv,
2676 		  struct wsm_rx *arg,
2677 		  struct sk_buff **skb_p)
2678 {
2679 	struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
2680 	struct sk_buff *skb = *skb_p;
2681 	struct ieee80211_rx_status *hdr = IEEE80211_SKB_RXCB(skb);
2682 	struct ieee80211_hdr *frame = (struct ieee80211_hdr *)skb->data;
2683 #if defined(CONFIG_XRADIO_USE_EXTENSIONS)
2684 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
2685 #endif
2686 	struct xradio_link_entry *entry = NULL;
2687 	unsigned long grace_period = 0;
2688 	bool early_data = false;
2689 	size_t hdrlen = 0;
2690 	u8   parse_iv_len = 0;
2691 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
2692 
2693 	hdr->flag = 0;
2694 
2695 	if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED)) {
2696 		/* STA is stopped. */
2697 		goto drop;
2698 	}
2699 
2700 #ifdef TES_P2P_0002_ROC_RESTART
2701 	xradio_frame_monitor(hw_priv, skb, false);
2702 #endif
2703 
2704 #if defined(CONFIG_XRADIO_USE_EXTENSIONS)
2705 	if ((ieee80211_is_action(frame->frame_control))
2706 	    && (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC)) {
2707 		u8 *action = (u8 *)&mgmt->u.action.category;
2708 		xradio_check_go_neg_conf_success(hw_priv, action);
2709 	}
2710 #endif
2711 
2712 #ifdef CONFIG_XRADIO_TESTMODE
2713 	spin_lock_bh(&hw_priv->tsm_lock);
2714 	if (hw_priv->start_stop_tsm.start) {
2715 		unsigned queue_id = skb_get_queue_mapping(skb);
2716 		if (queue_id == 0) {
2717 			struct timespec64 tmval;
2718 			xr_do_gettimeofday(&tmval);
2719 			if (hw_priv->tsm_info.sta_roamed &&
2720 			    hw_priv->tsm_info.use_rx_roaming) {
2721 				hw_priv->tsm_info.roam_delay = tmval.tv_nsec / 1000 -
2722 					hw_priv->tsm_info.rx_timestamp_vo;
2723 				txrx_printk(XRADIO_DBG_NIY, "[RX] RxInd Roaming:"
2724 				"roam_delay = %u\n", hw_priv->tsm_info.roam_delay);
2725 				hw_priv->tsm_info.sta_roamed = 0;
2726 			}
2727 			hw_priv->tsm_info.rx_timestamp_vo = tmval.tv_nsec / 1000;
2728 		}
2729 	}
2730 	spin_unlock_bh(&hw_priv->tsm_lock);
2731 #endif /*CONFIG_XRADIO_TESTMODE*/
2732 	if (arg->link_id && (arg->link_id != XRADIO_LINK_ID_UNMAPPED)
2733 			&& (arg->link_id <= XRADIO_MAX_STA_IN_AP_MODE)) {
2734 		entry =	&priv->link_id_db[arg->link_id - 1];
2735 		if (entry->status == XRADIO_LINK_SOFT &&
2736 				ieee80211_is_data(frame->frame_control))
2737 			early_data = true;
2738 		entry->timestamp = jiffies;
2739 	}
2740 #if defined(CONFIG_XRADIO_USE_EXTENSIONS)
2741 	else if ((arg->link_id == XRADIO_LINK_ID_UNMAPPED)
2742 			&& (priv->vif->p2p == WSM_START_MODE_P2P_GO)
2743 			&& ieee80211_is_action(frame->frame_control)
2744 			&& (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC)) {
2745 		txrx_printk(XRADIO_DBG_NIY, "[RX] Going to MAP&RESET link ID\n");
2746 
2747 #if defined (DEBUG_P2P_SETTING_CRASH)
2748 		printk(KERN_ERR "[BUG12]link ID is unmapped, go to MAP&RESET link ID\n");
2749 #endif
2750 		if (work_pending(&priv->linkid_reset_work))
2751 			SYS_WARN(1);
2752 
2753 		memcpy(&priv->action_frame_sa[0],
2754 				ieee80211_get_SA(frame), ETH_ALEN);
2755 		priv->action_linkid = 0;
2756 		schedule_work(&priv->linkid_reset_work);
2757 	}
2758 
2759 	if (arg->link_id && (arg->link_id != XRADIO_LINK_ID_UNMAPPED)
2760 			&& (priv->vif->p2p == WSM_START_MODE_P2P_GO)
2761 			&& ieee80211_is_action(frame->frame_control)
2762 			&& (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC)) {
2763 		/* Link ID already exists for the ACTION frame.
2764 		 * Reset and Remap */
2765 		if (work_pending(&priv->linkid_reset_work))
2766 			SYS_WARN(1);
2767 		memcpy(&priv->action_frame_sa[0],
2768 				ieee80211_get_SA(frame), ETH_ALEN);
2769 		priv->action_linkid = arg->link_id;
2770 		schedule_work(&priv->linkid_reset_work);
2771 	}
2772 #endif
2773 	if (unlikely(arg->status)) {
2774 		if (arg->status == WSM_STATUS_MICFAILURE) {
2775 			txrx_printk(XRADIO_DBG_WARN, "[RX] IF=%d, MIC failure.\n",
2776 				    priv->if_id);
2777 			hdr->flag |= RX_FLAG_MMIC_ERROR;
2778 		} else if (arg->status == WSM_STATUS_NO_KEY_FOUND) {
2779 #ifdef MONITOR_MODE
2780 			if (hw_priv->monitor_if_id == -1) {
2781 #endif
2782 				txrx_printk(XRADIO_DBG_WARN, "[RX] IF=%d, No key found.\n",
2783 				    priv->if_id);
2784 				goto drop;
2785 #ifdef MONITOR_MODE
2786 			}
2787 #endif
2788 		} else {
2789 			txrx_printk(XRADIO_DBG_WARN, "[RX] IF=%d, Receive failure: %d.\n",
2790 				priv->if_id, arg->status);
2791 			goto drop;
2792 		}
2793 	}
2794 
2795 	if (skb->len < sizeof(struct ieee80211_pspoll)) {
2796 		txrx_printk(XRADIO_DBG_WARN, "Mailformed SDU rx'ed. "
2797 			    "Size is lesser than IEEE header.\n");
2798 		goto drop;
2799 	}
2800 
2801 	if (unlikely(ieee80211_is_pspoll(frame->frame_control)))
2802 		if (xradio_handle_pspoll(priv, skb))
2803 			goto drop;
2804 
2805 	hdr->mactime = 0; /* Not supported by WSM */
2806 	hdr->band = (arg->channelNumber > 14) ?
2807 			NL80211_BAND_5GHZ : NL80211_BAND_2GHZ;
2808 	hdr->freq = ieee80211_channel_to_frequency(
2809 			arg->channelNumber,
2810 			hdr->band);
2811 
2812 #ifdef AP_HT_COMPAT_FIX
2813 
2814 #ifdef SUPPORT_HT40
2815 
2816 	if (!priv->ht_compat_det && priv->htcap &&
2817 		ieee80211_is_data_qos(frame->frame_control)) {
2818 		if (xradio_apcompat_detect(priv, arg->rxedRateEntry))
2819 			goto drop;
2820 	}
2821 
2822 #else
2823 
2824 	if (!priv->ht_compat_det && priv->htcap &&
2825 		ieee80211_is_data_qos(frame->frame_control)) {
2826 		if (xradio_apcompat_detect(priv, arg->rxedRate))
2827 			goto drop;
2828 	}
2829 
2830 #endif
2831 
2832 #endif
2833 
2834 #ifdef SUPPORT_HT40
2835 
2836 	if (((arg->rxedRateEntry >> MODEMTYPE_SHIFT) & MODEMTYPE_MASK) ==
2837 		RATE_MODEM_HTOFDM) {
2838 		if (((arg->rxedRateEntry >> BANDWIDTH_SHIFT) & BANDWIDTH_MASK) ==
2839 			RATE_BANDWIDTH_20M) {
2840 			if (arg->rxedRateEntry & RATE_F_SGI) {
2841 				u8 index = (arg->rxedRateEntry >> RATEINDEX_SHIFT) & RATEINDEX_MASK;
2842 				RxedHtofdmRateMap[0][index]++;
2843 			} else {
2844 				u8 index = (arg->rxedRateEntry >> RATEINDEX_SHIFT) & RATEINDEX_MASK;
2845 				RxedHtofdmRateMap[1][index]++;
2846 			}
2847 		} else {
2848 			if (arg->rxedRateEntry & RATE_F_SGI) {
2849 				u8 index = (arg->rxedRateEntry >> RATEINDEX_SHIFT) & RATEINDEX_MASK;
2850 				RxedHtofdmRateMap[2][index]++;
2851 			} else {
2852 				u8 index = (arg->rxedRateEntry >> RATEINDEX_SHIFT) & RATEINDEX_MASK;
2853 				RxedHtofdmRateMap[3][index]++;
2854 			}
2855 		}
2856 	} else {
2857 		u8 index = (arg->rxedRateEntry >> RATEINDEX_SHIFT) & RATEINDEX_MASK;
2858 		if (index < 6) {
2859 			if (arg->rxedRateEntry & RATE_F_PREAMBLE_S)
2860 				RxedLegacyRateMap[0][index*2+1]++;
2861 			else
2862 				RxedLegacyRateMap[0][index*2]++;
2863 
2864 		} else {
2865 			RxedLegacyRateMap[1][index-6]++;
2866 		}
2867 	}
2868 
2869 	if (GET_RATE_ENTRY_MODEM(arg->rxedRateEntry) == FW_RATE_MODEM_HTOFDM) {
2870 		hdr->encoding = RX_ENC_HT;
2871 		hdr->rate_idx = GET_RATE_ENTRY_RATEINDEX(arg->rxedRateEntry);
2872 
2873 
2874 		if (GET_RATE_ENTRY_FLAGS(arg->rxedRateEntry)&FW_RATE_F_SGI)
2875 			hdr->flag |= RX_ENC_FLAG_SHORT_GI;
2876 
2877 	} else {
2878 		hdr->rate_idx = LegacyRxedRateLut[hdr->band][GET_RATE_ENTRY_RATEINDEX(arg->rxedRateEntry)];
2879 
2880 		if (GET_RATE_ENTRY_FLAGS(arg->rxedRateEntry)&FW_RATE_F_SPRE)
2881 			hdr->flag |= RX_ENC_FLAG_SHORTPRE;
2882 
2883 	}
2884 
2885 #else
2886 
2887 	if (arg->rxedRate < 24)
2888 		RxedRateIdx_Map[arg->rxedRate]++;
2889 	else
2890 		SYS_WARN(1);
2891 
2892 	if (arg->rxedRate >= 14) {
2893 		hdr->encoding = RX_ENC_HT;
2894 		hdr->rate_idx = arg->rxedRate - 14;
2895 	} else if (arg->rxedRate >= 4) {
2896 		if (hdr->band == NL80211_BAND_5GHZ)
2897 			hdr->rate_idx = arg->rxedRate - 6;
2898 		else
2899 			hdr->rate_idx = arg->rxedRate - 2;
2900 	} else {
2901 		hdr->rate_idx = arg->rxedRate;
2902 	}
2903 
2904 #endif
2905 
2906 	hdr->signal = (s8)arg->rcpiRssi;
2907 	hdr->antenna = 0;
2908 
2909 	hdrlen = ieee80211_hdrlen(frame->frame_control);
2910 
2911 	if (WSM_RX_STATUS_ENCRYPTION(arg->flags)) {
2912 		size_t iv_len = 0, icv_len = 0;
2913 
2914 		hdr->flag |= RX_FLAG_DECRYPTED;
2915 
2916 		/* Oops... There is no fast way to ask mac80211 about
2917 		 * IV/ICV lengths. Even defineas are not exposed.*/
2918 		switch (WSM_RX_STATUS_ENCRYPTION(arg->flags)) {
2919 		case WSM_RX_STATUS_WEP:
2920 			iv_len = 4 /* WEP_IV_LEN */;
2921 			icv_len = 4 /* WEP_ICV_LEN */;
2922 			break;
2923 		case WSM_RX_STATUS_TKIP:
2924 			iv_len = 8 /* TKIP_IV_LEN */;
2925 			icv_len = 4 /* TKIP_ICV_LEN */
2926 				+ 8 /*MICHAEL_MIC_LEN*/;
2927 			break;
2928 		case WSM_RX_STATUS_AES:
2929 			iv_len = 8 /* CCMP_HDR_LEN */;
2930 			icv_len = 8 /* CCMP_MIC_LEN */;
2931 			break;
2932 		case WSM_RX_STATUS_WAPI:
2933 			iv_len = 18 /* WAPI_HDR_LEN */;
2934 			icv_len = 16 /* WAPI_MIC_LEN */;
2935 			hdr->flag |= RX_FLAG_IV_STRIPPED;
2936 			break;
2937 		default:
2938 			SYS_WARN("Unknown encryption type");
2939 			goto drop;
2940 		}
2941 
2942 		/* Firmware strips ICV in case of MIC failure. */
2943 		if (arg->status == WSM_STATUS_MICFAILURE) {
2944 			icv_len = 0;
2945 			hdr->flag |= RX_FLAG_IV_STRIPPED;
2946 		}
2947 
2948 		if (skb->len < hdrlen + iv_len + icv_len) {
2949 			txrx_printk(XRADIO_DBG_WARN, "Mailformed SDU rx'ed. "
2950 				"Size is lesser than crypto headers.\n");
2951 			goto drop;
2952 		}
2953 
2954 		if (WSM_RX_STATUS_ENCRYPTION(arg->flags) ==
2955 		    WSM_RX_STATUS_TKIP) {
2956 			/* Remove TKIP MIC 8 bytes*/
2957 			memmove(skb->data + skb->len-icv_len,
2958 				skb->data + skb->len-icv_len+8, 4);
2959 			skb_trim(skb, skb->len - 8);
2960 			hdr->flag |= RX_FLAG_MMIC_STRIPPED;
2961 		} else if (unlikely(WSM_RX_STATUS_ENCRYPTION(arg->flags) ==
2962 			   WSM_RX_STATUS_WAPI)) {
2963 			/* Protocols not defined in mac80211 should be
2964 			   stripped/crypted in driver/firmware */
2965 			/* Remove IV, ICV and MIC */
2966 			skb_trim(skb, skb->len - icv_len);
2967 			memmove(skb->data + iv_len, skb->data, hdrlen);
2968 			skb_pull(skb, iv_len);
2969 		}
2970 		parse_iv_len = iv_len;
2971 	}
2972 
2973 	xradio_debug_rxed(priv);
2974 	if (arg->flags & WSM_RX_STATUS_AGGREGATE)
2975 		xradio_debug_rxed_agg(priv);
2976 
2977 #if 0
2978 	/*for nettest*/
2979 	if (ieee80211_is_probe_resp(frame->frame_control) &&
2980 		!arg->status &&
2981 		!memcmp(ieee80211_get_SA(frame), nettest_bssid, ETH_ALEN)) {
2982 		const u8 *supp_rate_ie;
2983 		u8 *ies = ((struct ieee80211_mgmt *)
2984 			   (skb->data))->u.probe_resp.variable;
2985 		size_t ies_len = skb->len - (ies - (u8 *)(skb->data));
2986 
2987 		supp_rate_ie = xradio_get_ie(ies, ies_len, WLAN_EID_SUPP_RATES);
2988 		save_rate_ie = supp_rate_ie[2];
2989 		txrx_printk(XRADIO_DBG_WARN, "[netest]: save_rate_ie=%2x\n",
2990 			    save_rate_ie);
2991 	}
2992 
2993 	if (ieee80211_is_assoc_resp(frame->frame_control) &&
2994 		!arg->status &&
2995 		!memcmp(ieee80211_get_SA(frame), nettest_bssid, ETH_ALEN)) {
2996 		u8 *supp_rate_ie2;
2997 		size_t ies_len;
2998 		u8 *ies = ((struct ieee80211_mgmt *)
2999 			   (skb->data))->u.assoc_resp.variable;
3000 		ies_len = skb->len - (ies - (u8 *)(skb->data));
3001 		supp_rate_ie2 = xradio_get_ie(ies, ies_len, WLAN_EID_SUPP_RATES);
3002 
3003 		if ((supp_rate_ie2[1] == 1) && (supp_rate_ie2[2] == 0x80)) {
3004 			supp_rate_ie2[2] = save_rate_ie;
3005 			txrx_printk(XRADIO_DBG_WARN,
3006 				    "[netest]: rate_ie modified=%2x\n",
3007 				    supp_rate_ie2[2]);
3008 		}
3009 	}
3010 	/*for test*/
3011 #endif
3012 
3013 	if (ieee80211_is_beacon(frame->frame_control) &&
3014 		!arg->status &&
3015 		!memcmp(ieee80211_get_SA(frame), priv->join_bssid, ETH_ALEN)) {
3016 		const u8 *tim_ie;
3017 		u8 *ies;
3018 		size_t ies_len;
3019 		priv->disable_beacon_filter = false;
3020 		queue_work(hw_priv->workqueue, &priv->update_filtering_work);
3021 		ies = ((struct ieee80211_mgmt *)
3022 			  (skb->data))->u.beacon.variable;
3023 		ies_len = skb->len - (ies - (u8 *)(skb->data));
3024 
3025 		tim_ie = xradio_get_ie(ies, ies_len, WLAN_EID_TIM);
3026 		if (tim_ie) {
3027 			struct ieee80211_tim_ie *tim =
3028 				(struct ieee80211_tim_ie *)&tim_ie[2];
3029 
3030 			if (priv->join_dtim_period != tim->dtim_period) {
3031 				priv->join_dtim_period = tim->dtim_period;
3032 				queue_work(hw_priv->workqueue,
3033 					&priv->set_beacon_wakeup_period_work);
3034 			}
3035 		}
3036 		if (unlikely(priv->disable_beacon_filter)) {
3037 			priv->disable_beacon_filter = false;
3038 			queue_work(hw_priv->workqueue,
3039 				&priv->update_filtering_work);
3040 		}
3041 	}
3042 #ifdef AP_HT_CAP_UPDATE
3043     if (priv->mode == NL80211_IFTYPE_AP           &&
3044 	priv->join_status == XRADIO_JOIN_STATUS_AP &&
3045 	ieee80211_is_beacon(frame->frame_control) &&
3046 	((priv->ht_info&HT_INFO_MASK) != 0x0011)  &&
3047 	!arg->status) {
3048 	u8 *ies;
3049 	size_t ies_len;
3050 	const u8 *ht_cap;
3051 	ies = ((struct ieee80211_mgmt *)(skb->data))->u.beacon.variable;
3052 	ies_len = skb->len - (ies - (u8 *)(skb->data));
3053 	ht_cap = xradio_get_ie(ies, ies_len, WLAN_EID_HT_CAPABILITY);
3054 	if (!ht_cap) {
3055 	    priv->ht_info |= 0x0011;
3056 	    queue_work(hw_priv->workqueue, &priv->ht_info_update_work);
3057 	}
3058     }
3059 #endif
3060 
3061 #ifdef AP_HT_COMPAT_FIX
3062 	if (ieee80211_is_mgmt(frame->frame_control) &&
3063 		priv->if_id == 0 && !(priv->ht_compat_det & 0x10)) {
3064 		xradio_remove_ht_ie(priv, skb);
3065 	}
3066 #endif
3067 
3068 #ifdef ROAM_OFFLOAD
3069 	if ((ieee80211_is_beacon(frame->frame_control) ||
3070 		 ieee80211_is_probe_resp(frame->frame_control)) &&
3071 			!arg->status) {
3072 		if (hw_priv->auto_scanning &&
3073 			!atomic_read(&hw_priv->scan.in_progress))
3074 			hw_priv->frame_rcvd = 1;
3075 
3076 		if (!memcmp(ieee80211_get_SA(frame), priv->join_bssid, ETH_ALEN)) {
3077 			if (hw_priv->beacon)
3078 				dev_kfree_skb(hw_priv->beacon);
3079 			hw_priv->beacon = skb_copy(skb, GFP_ATOMIC);
3080 			if (!hw_priv->beacon)
3081 				txrx_printk(XRADIO_DBG_ERROR,
3082 					    "sched_scan: own beacon storing failed\n");
3083 		}
3084 	}
3085 #endif /*ROAM_OFFLOAD*/
3086 
3087 	/*scanResult.timestamp to adapt to Framework(WiFi) on Android5.0 or advanced version.*/
3088 	if ((ieee80211_is_beacon(mgmt->frame_control) ||
3089 		ieee80211_is_probe_resp(mgmt->frame_control))
3090 		&& !arg->status) {
3091 		struct timespec64 ts;
3092 		u64 tv_nsec;
3093 		xr_get_monotonic_boottime(&ts);
3094 		tv_nsec = ts.tv_nsec;
3095 		do_div(tv_nsec, 1000);
3096 		if (ieee80211_is_beacon(mgmt->frame_control)) {
3097 			mgmt->u.beacon.timestamp =
3098 				((u64)ts.tv_sec * 1000000 + tv_nsec);
3099 		} else if (ieee80211_is_probe_resp(mgmt->frame_control)) {
3100 			mgmt->u.probe_resp.timestamp =
3101 				((u64)ts.tv_sec * 1000000 + tv_nsec);
3102 		}
3103 	}
3104 
3105 	/* don't delay scan before next connect */
3106 	if (ieee80211_is_deauth(frame->frame_control) ||
3107 	    ieee80211_is_disassoc(frame->frame_control))
3108 		hw_priv->scan_delay_status[priv->if_id] = XRADIO_SCAN_ALLOW;
3109 
3110 	/* Stay awake for 1sec. after frame is received to give
3111 	 * userspace chance to react and acquire appropriate
3112 	 * wakelock. */
3113 	if (ieee80211_is_auth(frame->frame_control))
3114 		grace_period = 10 * HZ;
3115 	else if (ieee80211_is_deauth(frame->frame_control))
3116 		grace_period = 5 * HZ;
3117 #ifndef CONFIG_XRADIO_SUSPEND_POWER_OFF
3118 	else
3119 		grace_period = 0.2 * HZ;
3120 #endif
3121 
3122 	if (ieee80211_is_data(frame->frame_control))
3123 		xradio_rx_h_ba_stat(priv, hdrlen, skb->len);
3124 #ifdef CONFIG_PM
3125 	xradio_pm_stay_awake(&hw_priv->pm_state, grace_period);
3126 #endif
3127 #ifdef CONFIG_XRADIO_TESTMODE
3128 	if (hw_priv->test_frame.len > 0 &&
3129 		priv->mode == NL80211_IFTYPE_STATION) {
3130 		if (xradio_frame_test_detection(priv, frame, skb) == 1) {
3131 			consume_skb(skb);
3132 			*skb_p = NULL;
3133 			return;
3134 		}
3135 	}
3136 #endif /* CONFIG_XRADIO_TESTMODE */
3137 
3138 #ifdef AP_ARP_COMPAT_FIX
3139 	if (priv->join_status == XRADIO_JOIN_STATUS_STA) {
3140 		u16 fctl = frame->frame_control;
3141 		if (ieee80211_is_data(fctl)) {
3142 			u8 machdrlen = ieee80211_hdrlen(fctl);
3143 			u8 *llc_data = (u8 *)frame + machdrlen + parse_iv_len;
3144 			if (is_SNAP(llc_data) && is_arp(llc_data)) {
3145 				u8 *arp_hdr = llc_data + LLC_LEN;
3146 				u16 *arp_type = (u16 *)(arp_hdr + ARP_TYPE_OFFSET);
3147 				if (*arp_type == cpu_to_be16(ARP_RESPONSE)) {
3148 					priv->arp_compat_cnt = 0;
3149 				}
3150 			}
3151 		}
3152 	}
3153 #endif
3154 
3155 #ifdef CONFIG_XRADIO_DEBUGFS
3156 	/* parsse frame here for debug. */
3157 	if (rxparse_flags)
3158 		xradio_parse_frame(skb->data, parse_iv_len,
3159 				   rxparse_flags|PF_RX, priv->if_id);
3160 #endif
3161 
3162 	/* Some aps change channel to inform station by sending beacon with WLAN_EID_DS_PARAMS ie,
3163 	 *then station needs to reconnect to ap.
3164 	*/
3165 	if (ieee80211_is_beacon(frame->frame_control) &&
3166 		!arg->status &&
3167 		(priv->join_status == XRADIO_JOIN_STATUS_STA) &&
3168 		!memcmp(ieee80211_get_SA(frame), priv->join_bssid, ETH_ALEN)) {
3169 		const u8 *ds_ie;
3170 		u8 *ies;
3171 		size_t ies_len;
3172 		int ds_ie_partms_chan = 0;
3173 		ies = ((struct ieee80211_mgmt *)
3174 			  (skb->data))->u.beacon.variable;
3175 		ies_len = skb->len - (ies - (u8 *)(skb->data));
3176 
3177 		ds_ie = xradio_get_ie(ies, ies_len, WLAN_EID_DS_PARAMS);
3178 		if (ds_ie && (ds_ie[1] == 1)) {
3179 			ds_ie_partms_chan = ds_ie[2];
3180 			if (ds_ie_partms_chan != hw_priv->join_chan) {
3181 				txrx_printk(XRADIO_DBG_WARN, "***ap changes channel by beacon with ds ie, "
3182 					"then station reconnects to ap, %d -> %d\n",
3183 					hw_priv->join_chan, ds_ie_partms_chan);
3184 				wsm_send_disassoc_to_self(hw_priv, priv);
3185 			}
3186 		}
3187 	}
3188 
3189 #if PERF_INFO_TEST
3190 	upper_rx_size = skb->len;
3191 #endif
3192 	PERF_INFO_GETTIME(&upper_rx_time);
3193 	/* Try to  a packet for the case dev_alloc_skb failed in bh.*/
3194 	if (unlikely(xradio_itp_rxed(hw_priv, skb)))
3195 		consume_skb(skb);
3196 	else if (unlikely(early_data)) {
3197 		spin_lock_bh(&priv->ps_state_lock);
3198 		/* Double-check status with lock held */
3199 		if (entry->status == XRADIO_LINK_SOFT) {
3200 			skb_queue_tail(&entry->rx_queue, skb);
3201 			txrx_printk(XRADIO_DBG_WARN, "***skb_queue_tail\n");
3202 		} else
3203 			mac80211_rx_irqsafe(priv->hw, skb);
3204 		spin_unlock_bh(&priv->ps_state_lock);
3205 	} else {
3206 		mac80211_rx_irqsafe(priv->hw, skb);
3207 	}
3208 	*skb_p = NULL;
3209 	PERF_INFO_STAMP(&upper_rx_time, &mac_rx, upper_rx_size);
3210 
3211 	return;
3212 
3213 drop:
3214 	/* TODO: update failure counters */
3215 	return;
3216 }
3217 
3218 /* ******************************************************************** */
3219 /* Security								*/
3220 
3221 int xradio_alloc_key(struct xradio_common *hw_priv)
3222 {
3223 	int idx;
3224 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
3225 
3226 	idx = ffs(~hw_priv->key_map) - 1;
3227 	if (idx < 0 || idx > WSM_KEY_MAX_INDEX)
3228 		return -1;
3229 
3230 	hw_priv->key_map |= BIT(idx);
3231 	hw_priv->keys[idx].entryIndex = idx;
3232 	txrx_printk(XRADIO_DBG_NIY, "%s, idx=%d\n", __func__, idx);
3233 	return idx;
3234 }
3235 
3236 void xradio_free_key(struct xradio_common *hw_priv, int idx)
3237 {
3238 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
3239 
3240 	SYS_BUG(!(hw_priv->key_map & BIT(idx)));
3241 	memset(&hw_priv->keys[idx], 0, sizeof(hw_priv->keys[idx]));
3242 	hw_priv->key_map &= ~BIT(idx);
3243 	txrx_printk(XRADIO_DBG_NIY, "%s, idx=%d\n", __func__, idx);
3244 }
3245 
3246 void xradio_free_keys(struct xradio_common *hw_priv)
3247 {
3248 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
3249 
3250 	memset(&hw_priv->keys, 0, sizeof(hw_priv->keys));
3251 	hw_priv->key_map = 0;
3252 }
3253 
3254 int xradio_upload_keys(struct xradio_vif *priv)
3255 {
3256 	struct xradio_common *hw_priv = xrwl_vifpriv_to_hwpriv(priv);
3257 	int idx, ret = 0;
3258 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
3259 
3260 	for (idx = 0; idx <= WSM_KEY_MAX_IDX; ++idx)
3261 		if (hw_priv->key_map & BIT(idx)) {
3262 			ret = wsm_add_key(hw_priv, &hw_priv->keys[idx], priv->if_id);
3263 			if (ret < 0)
3264 				break;
3265 		}
3266 	return ret;
3267 }
3268 
3269 #if defined(CONFIG_XRADIO_USE_EXTENSIONS)
3270 /* Workaround for WFD test case 6.1.10 */
3271 void xradio_link_id_reset(struct work_struct *work)
3272 {
3273 	struct xradio_vif *priv =
3274 		container_of(work, struct xradio_vif, linkid_reset_work);
3275 	struct xradio_common *hw_priv = priv->hw_priv;
3276 	int temp_linkid;
3277 	txrx_printk(XRADIO_DBG_TRC, "%s\n", __func__);
3278 
3279 	if (!priv->action_linkid) {
3280 		/* In GO mode we can receive ACTION frames without a linkID */
3281 		temp_linkid = xradio_alloc_link_id(priv,
3282 				&priv->action_frame_sa[0]);
3283 		SYS_WARN(!temp_linkid);
3284 		if (temp_linkid) {
3285 			/* Make sure we execute the WQ */
3286 			flush_workqueue(hw_priv->workqueue);
3287 			/* Release the link ID */
3288 			spin_lock_bh(&priv->ps_state_lock);
3289 			priv->link_id_db[temp_linkid - 1].prev_status =
3290 				priv->link_id_db[temp_linkid - 1].status;
3291 			priv->link_id_db[temp_linkid - 1].status =
3292 				XRADIO_LINK_RESET;
3293 			spin_unlock_bh(&priv->ps_state_lock);
3294 			wsm_lock_tx_async(hw_priv);
3295 			if (queue_work(hw_priv->workqueue,
3296 				       &priv->link_id_work) <= 0)
3297 				wsm_unlock_tx(hw_priv);
3298 		}
3299 	} else {
3300 		spin_lock_bh(&priv->ps_state_lock);
3301 		priv->link_id_db[priv->action_linkid - 1].prev_status =
3302 			priv->link_id_db[priv->action_linkid - 1].status;
3303 		priv->link_id_db[priv->action_linkid - 1].status =
3304 			XRADIO_LINK_RESET_REMAP;
3305 		spin_unlock_bh(&priv->ps_state_lock);
3306 		wsm_lock_tx_async(hw_priv);
3307 		if (queue_work(hw_priv->workqueue, &priv->link_id_work) <= 0)
3308 				wsm_unlock_tx(hw_priv);
3309 		flush_workqueue(hw_priv->workqueue);
3310 	}
3311 }
3312 #endif
3313