xref: /linux/drivers/net/wireless/nxp/nxpwifi/cfg80211.c (revision 0af298e62e5cff4ebf9bec47b8711177a7e2f115)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * nxpwifi: cfg80211 support
4  *
5  * Copyright 2011-2024 NXP
6  */
7 
8 #include "cfg80211.h"
9 #include "main.h"
10 #include "cmdevt.h"
11 #include "11n.h"
12 #include "wmm.h"
13 
14 static const struct ieee80211_iface_limit nxpwifi_ap_sta_limits[] = {
15 	{
16 		.max = NXPWIFI_MAX_BSS_NUM,
17 		.types = BIT(NL80211_IFTYPE_STATION) |
18 			 BIT(NL80211_IFTYPE_AP) |
19 			 BIT(NL80211_IFTYPE_MONITOR),
20 	},
21 };
22 
23 static const struct ieee80211_iface_combination
24 nxpwifi_iface_comb_ap_sta = {
25 	.limits = nxpwifi_ap_sta_limits,
26 	.num_different_channels = 1,
27 	.n_limits = ARRAY_SIZE(nxpwifi_ap_sta_limits),
28 	.max_interfaces = NXPWIFI_MAX_BSS_NUM,
29 	.beacon_int_infra_match = true,
30 	.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
31 				BIT(NL80211_CHAN_WIDTH_20) |
32 				BIT(NL80211_CHAN_WIDTH_40),
33 };
34 
35 static const struct ieee80211_iface_combination
36 nxpwifi_iface_comb_ap_sta_vht = {
37 	.limits = nxpwifi_ap_sta_limits,
38 	.num_different_channels = 1,
39 	.n_limits = ARRAY_SIZE(nxpwifi_ap_sta_limits),
40 	.max_interfaces = NXPWIFI_MAX_BSS_NUM,
41 	.beacon_int_infra_match = true,
42 	.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
43 				BIT(NL80211_CHAN_WIDTH_20) |
44 				BIT(NL80211_CHAN_WIDTH_40) |
45 				BIT(NL80211_CHAN_WIDTH_80),
46 };
47 
48 /* Map nl80211 channel types to secondary channel offsets */
49 u8 nxpwifi_chan_type_to_sec_chan_offset(enum nl80211_channel_type chan_type)
50 {
51 	switch (chan_type) {
52 	case NL80211_CHAN_NO_HT:
53 	case NL80211_CHAN_HT20:
54 		return IEEE80211_HT_PARAM_CHA_SEC_NONE;
55 	case NL80211_CHAN_HT40PLUS:
56 		return IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
57 	case NL80211_CHAN_HT40MINUS:
58 		return IEEE80211_HT_PARAM_CHA_SEC_BELOW;
59 	default:
60 		return IEEE80211_HT_PARAM_CHA_SEC_NONE;
61 	}
62 }
63 
64 /* Map IEEE HT secondary‑channel type to nl80211 channel type */
65 u8 nxpwifi_get_chan_type(struct nxpwifi_private *priv)
66 {
67 	struct nxpwifi_channel_band channel_band;
68 	int ret;
69 
70 	ret = nxpwifi_get_chan_info(priv, &channel_band);
71 
72 	if (!ret) {
73 		switch (channel_band.band_config.chan_width) {
74 		case CHAN_BW_20MHZ:
75 			if (IS_11N_ENABLED(priv))
76 				return NL80211_CHAN_HT20;
77 			else
78 				return NL80211_CHAN_NO_HT;
79 		case CHAN_BW_40MHZ:
80 			if (channel_band.band_config.chan2_offset ==
81 			    IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
82 				return NL80211_CHAN_HT40PLUS;
83 			else
84 				return NL80211_CHAN_HT40MINUS;
85 		default:
86 			return NL80211_CHAN_HT20;
87 		}
88 	}
89 
90 	return NL80211_CHAN_HT20;
91 }
92 
93 /* Retrieve the driver private data from the wiphy */
94 static void *nxpwifi_cfg80211_get_adapter(struct wiphy *wiphy)
95 {
96 	return (void *)(*(unsigned long *)wiphy_priv(wiphy));
97 }
98 
99 /* cfg80211 operation handler to delete a network key. */
100 static int
101 nxpwifi_cfg80211_del_key(struct wiphy *wiphy, struct wireless_dev *wdev,
102 			 int link_id, u8 key_index,
103 			 enum nl80211_key_type type,
104 			 const u8 *mac_addr)
105 {
106 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
107 	static const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
108 	const u8 *peer_mac =
109 		(type == NL80211_KEYTYPE_PAIRWISE) ? mac_addr : bc_mac;
110 	int ret;
111 
112 	ret = nxpwifi_set_encode(priv, NULL, NULL, 0, key_index, peer_mac, 1);
113 	if (ret)
114 		nxpwifi_dbg(priv->adapter, ERROR,
115 			    "crypto keys deleted failed %d\n", ret);
116 	else
117 		nxpwifi_dbg(priv->adapter, INFO, "info: crypto keys deleted\n");
118 
119 	return ret;
120 }
121 
122 /* Build an skb containing a management frame */
123 static void
124 nxpwifi_form_mgmt_frame(struct sk_buff *skb, const u8 *buf, size_t len)
125 {
126 	u8 addr[ETH_ALEN];
127 	u16 pkt_len;
128 	u32 tx_control = 0, pkt_type = PKT_TYPE_MGMT;
129 
130 	eth_broadcast_addr(addr);
131 	pkt_len = len + ETH_ALEN;
132 
133 	skb_reserve(skb, NXPWIFI_MIN_DATA_HEADER_LEN +
134 		    NXPWIFI_MGMT_FRAME_HEADER_SIZE + sizeof(pkt_len));
135 	memcpy(skb_push(skb, sizeof(pkt_len)), &pkt_len, sizeof(pkt_len));
136 
137 	memcpy(skb_push(skb, sizeof(tx_control)),
138 	       &tx_control, sizeof(tx_control));
139 
140 	memcpy(skb_push(skb, sizeof(pkt_type)), &pkt_type, sizeof(pkt_type));
141 
142 	/* Add packet data and address4 */
143 	skb_put_data(skb, buf, sizeof(struct ieee80211_hdr_3addr));
144 	skb_put_data(skb, addr, ETH_ALEN);
145 	skb_put_data(skb, buf + sizeof(struct ieee80211_hdr_3addr),
146 		     len - sizeof(struct ieee80211_hdr_3addr));
147 
148 	skb->priority = TC_PRIO_BESTEFFORT;
149 	__net_timestamp(skb);
150 }
151 
152 /* cfg80211 operation handler to transmit a management frame. */
153 static int
154 nxpwifi_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
155 			 struct cfg80211_mgmt_tx_params *params, u64 cookie)
156 {
157 	const u8 *buf = params->buf;
158 	size_t len = params->len;
159 	struct sk_buff *skb;
160 	u16 pkt_len;
161 	const struct ieee80211_mgmt *mgmt;
162 	struct nxpwifi_txinfo *tx_info;
163 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
164 
165 	if (!buf || !len) {
166 		nxpwifi_dbg(priv->adapter, ERROR, "invalid buffer and length\n");
167 		return -EINVAL;
168 	}
169 
170 	mgmt = (const struct ieee80211_mgmt *)buf;
171 	if (GET_BSS_ROLE(priv) != NXPWIFI_BSS_ROLE_STA &&
172 	    ieee80211_is_probe_resp(mgmt->frame_control)) {
173 		/* Offloaded probe responses; skip TX in AP/GO mode */
174 		nxpwifi_dbg(priv->adapter, INFO,
175 			    "info: skip to send probe resp in AP or GO mode\n");
176 		return 0;
177 	}
178 
179 	if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP) {
180 		if (ieee80211_is_auth(mgmt->frame_control))
181 			nxpwifi_dbg(priv->adapter, MSG,
182 				    "auth: send auth to %pM\n", mgmt->da);
183 		if (ieee80211_is_deauth(mgmt->frame_control))
184 			nxpwifi_dbg(priv->adapter, MSG,
185 				    "auth: send deauth to %pM\n", mgmt->da);
186 		if (ieee80211_is_disassoc(mgmt->frame_control))
187 			nxpwifi_dbg(priv->adapter, MSG,
188 				    "assoc: send disassoc to %pM\n", mgmt->da);
189 		if (ieee80211_is_assoc_resp(mgmt->frame_control))
190 			nxpwifi_dbg(priv->adapter, MSG,
191 				    "assoc: send assoc resp to %pM\n",
192 				    mgmt->da);
193 		if (ieee80211_is_reassoc_resp(mgmt->frame_control))
194 			nxpwifi_dbg(priv->adapter, MSG,
195 				    "assoc: send reassoc resp to %pM\n",
196 				    mgmt->da);
197 	}
198 
199 	pkt_len = len + ETH_ALEN;
200 	skb = dev_alloc_skb(NXPWIFI_MIN_DATA_HEADER_LEN +
201 			    NXPWIFI_MGMT_FRAME_HEADER_SIZE +
202 			    pkt_len + sizeof(pkt_len));
203 
204 	if (!skb) {
205 		nxpwifi_dbg(priv->adapter, ERROR,
206 			    "allocate skb failed for management frame\n");
207 		return -ENOMEM;
208 	}
209 
210 	tx_info = NXPWIFI_SKB_TXCB(skb);
211 	memset(tx_info, 0, sizeof(*tx_info));
212 	tx_info->bss_num = priv->bss_num;
213 	tx_info->bss_type = priv->bss_type;
214 	tx_info->pkt_len = pkt_len;
215 
216 	nxpwifi_form_mgmt_frame(skb, buf, len);
217 
218 	if (ieee80211_is_action(mgmt->frame_control))
219 		skb = nxpwifi_clone_skb_for_tx_status(priv,
220 						      skb,
221 				NXPWIFI_BUF_FLAG_ACTION_TX_STATUS, &cookie);
222 	else
223 		cfg80211_mgmt_tx_status(wdev, cookie, buf, len, true,
224 					GFP_ATOMIC);
225 
226 	nxpwifi_queue_tx_pkt(priv, skb);
227 
228 	nxpwifi_dbg(priv->adapter, INFO, "info: management frame transmitted\n");
229 	return 0;
230 }
231 
232 /* cfg80211 operation handler to register a mgmt frame. */
233 static void
234 nxpwifi_cfg80211_update_mgmt_frame_registrations(struct wiphy *wiphy,
235 						 struct wireless_dev *wdev,
236 						 struct mgmt_frame_regs *upd)
237 {
238 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
239 	u32 mask = upd->interface_stypes;
240 
241 	if (mask != priv->mgmt_frame_mask) {
242 		priv->mgmt_frame_mask = mask;
243 		if (priv->host_mlme_reg &&
244 		    GET_BSS_ROLE(priv) != NXPWIFI_BSS_ROLE_UAP)
245 			priv->mgmt_frame_mask |= HOST_MLME_MGMT_MASK;
246 
247 		nxpwifi_mgmt_frame_reg(priv, priv->mgmt_frame_mask);
248 
249 		nxpwifi_dbg(priv->adapter, INFO, "info: mgmt frame registered\n");
250 	}
251 }
252 
253 /* cfg80211 operation handler to remain on channel. */
254 static int
255 nxpwifi_cfg80211_remain_on_channel(struct wiphy *wiphy,
256 				   struct wireless_dev *wdev,
257 				   struct ieee80211_channel *chan,
258 				   unsigned int duration, u64 cookie,
259 				   const u8 *rx_addr)
260 {
261 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
262 	struct nxpwifi_adapter *adapter = priv->adapter;
263 	int ret;
264 
265 	if (!chan) {
266 		nxpwifi_dbg(adapter, ERROR, "Invalid parameter for ROC\n");
267 		return -EINVAL;
268 	}
269 
270 	if (priv->roc_cfg.cookie) {
271 		nxpwifi_dbg(adapter, INFO,
272 			    "info: ongoing ROC, cookie = 0x%llx\n",
273 			    priv->roc_cfg.cookie);
274 		return -EBUSY;
275 	}
276 
277 	ret = nxpwifi_remain_on_chan_cfg(priv, HOST_ACT_GEN_SET, chan,
278 					 duration);
279 
280 	if (!ret) {
281 		priv->roc_cfg.cookie = cookie;
282 		priv->roc_cfg.chan = *chan;
283 
284 		cfg80211_ready_on_channel(wdev, cookie, chan,
285 					  duration, GFP_ATOMIC);
286 
287 		nxpwifi_dbg(adapter, INFO,
288 			    "info: ROC, cookie = 0x%llx\n", cookie);
289 	}
290 
291 	return ret;
292 }
293 
294 /* cfg80211 operation handler to cancel remain on channel. */
295 static int
296 nxpwifi_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
297 					  struct wireless_dev *wdev, u64 cookie)
298 {
299 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
300 	int ret;
301 
302 	if (cookie != priv->roc_cfg.cookie)
303 		return -ENOENT;
304 
305 	ret = nxpwifi_remain_on_chan_cfg(priv, HOST_ACT_GEN_REMOVE,
306 					 &priv->roc_cfg.chan, 0);
307 
308 	if (!ret) {
309 		cfg80211_remain_on_channel_expired(wdev, cookie,
310 						   &priv->roc_cfg.chan,
311 						   GFP_ATOMIC);
312 
313 		memset(&priv->roc_cfg, 0, sizeof(struct nxpwifi_roc_cfg));
314 
315 		nxpwifi_dbg(priv->adapter, INFO,
316 			    "info: cancel ROC, cookie = 0x%llx\n", cookie);
317 	}
318 
319 	return ret;
320 }
321 
322 /* cfg80211 operation handler to set Tx power. */
323 static int
324 nxpwifi_cfg80211_set_tx_power(struct wiphy *wiphy,
325 			      struct wireless_dev *wdev,
326 			      int radio_idx,
327 			      enum nl80211_tx_power_setting type,
328 			      int mbm)
329 {
330 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
331 	struct nxpwifi_private *priv;
332 	struct nxpwifi_power_cfg power_cfg;
333 	int dbm = MBM_TO_DBM(mbm);
334 
335 	switch (type) {
336 	case NL80211_TX_POWER_FIXED:
337 		power_cfg.is_power_auto = 0;
338 		power_cfg.is_power_fixed = 1;
339 		power_cfg.power_level = dbm;
340 		break;
341 	case NL80211_TX_POWER_LIMITED:
342 		power_cfg.is_power_auto = 0;
343 		power_cfg.is_power_fixed = 0;
344 		power_cfg.power_level = dbm;
345 		break;
346 	case NL80211_TX_POWER_AUTOMATIC:
347 		power_cfg.is_power_auto = 1;
348 		break;
349 	}
350 
351 	priv = nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_ANY);
352 
353 	return nxpwifi_set_tx_power(priv, &power_cfg);
354 }
355 
356 /* cfg80211 operation handler to get Tx power. */
357 static int
358 nxpwifi_cfg80211_get_tx_power(struct wiphy *wiphy,
359 			      struct wireless_dev *wdev,
360 			      int radio_idx,
361 			      unsigned int link_id,
362 			      int *dbm)
363 {
364 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
365 	struct nxpwifi_private *priv = nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_ANY);
366 	int ret = nxpwifi_get_tx_pwr(priv);
367 
368 	if (ret < 0)
369 		return ret;
370 
371 	*dbm = priv->tx_power_level;
372 
373 	return 0;
374 }
375 
376 /*
377  * cfg80211 handler for setting IEEE 802.11 Power Save mode.
378  *
379  * The 'timeout' parameter is not supported and is ignored.
380  */
381 static int
382 nxpwifi_cfg80211_set_power_mgmt(struct wiphy *wiphy,
383 				struct net_device *dev,
384 				bool enabled, int timeout)
385 {
386 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
387 	u32 ps_mode;
388 
389 	if (timeout)
390 		nxpwifi_dbg(priv->adapter, INFO,
391 			    "info: ignore timeout value for IEEE Power Save\n");
392 
393 	ps_mode = enabled;
394 
395 	return nxpwifi_drv_set_power(priv, &ps_mode);
396 }
397 
398 /*
399  * cfg80211 handler for setting the default WEP key.
400  *
401  * Ignored if WEP is not enabled.
402  */
403 static int
404 nxpwifi_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
405 				 int link_id, u8 key_index, bool unicast,
406 				 bool multicast)
407 {
408 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(netdev);
409 	int ret = 0;
410 
411 	/* Return if WEP key not configured */
412 	if (!priv->sec_info.wep_enabled)
413 		return 0;
414 
415 	if (priv->bss_type == NXPWIFI_BSS_TYPE_UAP) {
416 		priv->wep_key_curr_index = key_index;
417 	} else {
418 		ret = nxpwifi_set_encode(priv, NULL, NULL, 0, key_index,
419 					 NULL, 0);
420 		if (ret)
421 			nxpwifi_dbg(priv->adapter, ERROR,
422 				    "failed to set default Tx key index\n");
423 	}
424 
425 	return ret;
426 }
427 
428 /* cfg80211 handler for adding an 802.11 encryption key. */
429 static int
430 nxpwifi_cfg80211_add_key(struct wiphy *wiphy, struct wireless_dev *wdev,
431 			 int link_id, u8 key_index,
432 			 enum nl80211_key_type type,
433 			 const u8 *mac_addr, struct key_params *params)
434 {
435 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
436 	struct nxpwifi_wep_key *wep_key;
437 	u8 bc_mac[ETH_ALEN];
438 	const u8 *peer_mac;
439 	int ret;
440 
441 	eth_broadcast_addr(bc_mac);
442 	peer_mac = (type == NL80211_KEYTYPE_PAIRWISE) ? mac_addr : bc_mac;
443 
444 	if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP &&
445 	    (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
446 	     params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
447 		if (params->key && params->key_len) {
448 			wep_key = &priv->wep_key[key_index];
449 			memset(wep_key, 0, sizeof(struct nxpwifi_wep_key));
450 			memcpy(wep_key->key_material, params->key,
451 			       params->key_len);
452 			wep_key->key_index = key_index;
453 			wep_key->key_length = params->key_len;
454 			priv->sec_info.wep_enabled = 1;
455 		}
456 		return 0;
457 	}
458 
459 	ret = nxpwifi_set_encode(priv, params, params->key, params->key_len,
460 				 key_index, peer_mac, 0);
461 	if (ret)
462 		nxpwifi_dbg(priv->adapter, ERROR,
463 			    "failed to add crypto keys\n");
464 
465 	return ret;
466 }
467 
468 /* cfg80211 handler for setting the default management key. */
469 static int
470 nxpwifi_cfg80211_set_default_mgmt_key(struct wiphy *wiphy,
471 				      struct wireless_dev *wdev,
472 				      int link_id,
473 				      u8 key_index)
474 {
475 	return 0;
476 }
477 
478 /*
479  * Sends regulatory domain information to the firmware.
480  *
481  * Includes:
482  *   - Country code
483  *   - Sub-band definitions (first channel, channel count, max TX power)
484  */
485 int nxpwifi_send_domain_info_cmd_fw(struct wiphy *wiphy, enum nl80211_band band)
486 {
487 	u8 no_of_triplet = 0;
488 	struct ieee80211_country_ie_triplet *t;
489 	u8 no_of_parsed_chan = 0;
490 	u8 first_chan = 0, next_chan = 0, max_pwr = 0;
491 	u8 i, flag = 0;
492 	struct ieee80211_supported_band *sband;
493 	struct ieee80211_channel *ch;
494 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
495 	struct nxpwifi_private *priv;
496 	struct nxpwifi_802_11d_domain_reg *domain_info = &adapter->domain_reg;
497 	int ret;
498 
499 	domain_info->dfs_region = adapter->dfs_region;
500 
501 	/* Set country code */
502 	domain_info->country_code[0] = adapter->country_code[0];
503 	domain_info->country_code[1] = adapter->country_code[1];
504 	domain_info->country_code[2] = ' ';
505 
506 	if (!wiphy->bands[band]) {
507 		nxpwifi_dbg(adapter, ERROR,
508 			    "11D: setting domain info in FW\n");
509 		return -EINVAL;
510 	}
511 
512 	sband = wiphy->bands[band];
513 
514 	for (i = 0; i < sband->n_channels ; i++) {
515 		ch = &sband->channels[i];
516 		if (ch->flags & IEEE80211_CHAN_DISABLED)
517 			continue;
518 
519 		if (!flag) {
520 			flag = 1;
521 			first_chan = (u32)ch->hw_value;
522 			next_chan = first_chan;
523 			max_pwr = ch->max_power;
524 			no_of_parsed_chan = 1;
525 			continue;
526 		}
527 
528 		if (ch->hw_value == next_chan + 1 &&
529 		    ch->max_power == max_pwr) {
530 			next_chan++;
531 			no_of_parsed_chan++;
532 		} else {
533 			t = &domain_info->triplet[no_of_triplet];
534 			t->chans.first_channel = first_chan;
535 			t->chans.num_channels = no_of_parsed_chan;
536 			t->chans.max_power = max_pwr;
537 			no_of_triplet++;
538 			first_chan = (u32)ch->hw_value;
539 			next_chan = first_chan;
540 			max_pwr = ch->max_power;
541 			no_of_parsed_chan = 1;
542 		}
543 	}
544 
545 	if (flag) {
546 		t = &domain_info->triplet[no_of_triplet];
547 		t->chans.first_channel = first_chan;
548 		t->chans.num_channels = no_of_parsed_chan;
549 		t->chans.max_power = max_pwr;
550 		no_of_triplet++;
551 	}
552 
553 	domain_info->no_of_triplet = no_of_triplet;
554 
555 	priv = nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_ANY);
556 
557 	ret = nxpwifi_apply_regdomain(priv);
558 
559 	if (ret)
560 		nxpwifi_dbg(adapter, INFO,
561 			    "11D: failed to set domain info in FW\n");
562 
563 	return ret;
564 }
565 
566 static void nxpwifi_reg_apply_radar_flags(struct wiphy *wiphy)
567 {
568 	struct ieee80211_supported_band *sband;
569 	struct ieee80211_channel *chan;
570 	unsigned int i;
571 
572 	if (!wiphy->bands[NL80211_BAND_5GHZ])
573 		return;
574 	sband = wiphy->bands[NL80211_BAND_5GHZ];
575 
576 	for (i = 0; i < sband->n_channels; i++) {
577 		chan = &sband->channels[i];
578 		if ((!(chan->flags & IEEE80211_CHAN_DISABLED)) &&
579 		    (chan->flags & IEEE80211_CHAN_RADAR))
580 			chan->flags |= IEEE80211_CHAN_NO_IR;
581 	}
582 }
583 
584 /*
585  * cfg80211 regulatory domain change callback.
586  *
587  * Invoked when the regulatory domain is updated by:
588  *   - the driver
589  *   - the system core
590  *   - the user
591  *   - a received Country IE
592  */
593 static void nxpwifi_reg_notifier(struct wiphy *wiphy,
594 				 struct regulatory_request *request)
595 {
596 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
597 
598 	nxpwifi_dbg(adapter, INFO,
599 		    "info: cfg80211 regulatory domain callback for %c%c\n",
600 		    request->alpha2[0], request->alpha2[1]);
601 	nxpwifi_reg_apply_radar_flags(wiphy);
602 
603 	switch (request->initiator) {
604 	case NL80211_REGDOM_SET_BY_DRIVER:
605 	case NL80211_REGDOM_SET_BY_CORE:
606 	case NL80211_REGDOM_SET_BY_USER:
607 	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
608 		break;
609 	default:
610 		nxpwifi_dbg(adapter, ERROR,
611 			    "unknown regdom initiator: %d\n",
612 			    request->initiator);
613 		return;
614 	}
615 
616 	/* Skip world/unchanged regulatory domains. */
617 	if (strncmp(request->alpha2, "00", 2) &&
618 	    strncmp(request->alpha2, adapter->country_code,
619 		    sizeof(request->alpha2))) {
620 		memcpy(adapter->country_code, request->alpha2,
621 		       sizeof(request->alpha2));
622 		adapter->dfs_region = request->dfs_region;
623 		nxpwifi_send_domain_info_cmd_fw(wiphy, NL80211_BAND_2GHZ);
624 		if (adapter->fw_bands & BAND_A)
625 			nxpwifi_send_domain_info_cmd_fw(wiphy,
626 							NL80211_BAND_5GHZ);
627 	}
628 }
629 
630 /*
631  * cfg80211 op: set wiphy parameters.
632  * Updates RTS/fragmentation thresholds and retry limits based on 'changed'
633  * flags.
634  */
635 static int
636 nxpwifi_cfg80211_set_wiphy_params(struct wiphy *wiphy, int radio_idx, u32 changed)
637 {
638 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
639 	struct nxpwifi_private *priv;
640 	struct nxpwifi_uap_bss_param *bss_cfg;
641 	int ret = 0;
642 
643 	priv = nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_ANY);
644 
645 	switch (priv->bss_role) {
646 	case NXPWIFI_BSS_ROLE_UAP:
647 		bss_cfg = kzalloc_obj(*bss_cfg);
648 		if (!bss_cfg) {
649 			ret = -ENOMEM;
650 			break;
651 		}
652 
653 		nxpwifi_set_sys_config_invalid_data(bss_cfg);
654 
655 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
656 			bss_cfg->rts_threshold = wiphy->rts_threshold;
657 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
658 			bss_cfg->frag_threshold = wiphy->frag_threshold;
659 		if (changed & WIPHY_PARAM_RETRY_LONG)
660 			bss_cfg->retry_limit = wiphy->retry_long;
661 
662 		ret = nxpwifi_set_uap_sys_cfg(priv, bss_cfg);
663 
664 		kfree(bss_cfg);
665 		if (ret)
666 			nxpwifi_dbg(adapter, ERROR,
667 				    "Failed to set wiphy phy params\n");
668 		break;
669 
670 	case NXPWIFI_BSS_ROLE_STA:
671 		if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
672 			ret = nxpwifi_set_rts(priv,
673 					      wiphy->rts_threshold);
674 			if (ret)
675 				break;
676 		}
677 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
678 			ret = nxpwifi_set_frag(priv,
679 					       wiphy->frag_threshold);
680 		break;
681 	}
682 
683 	return ret;
684 }
685 
686 static int nxpwifi_deinit_priv_params(struct nxpwifi_private *priv)
687 {
688 	struct nxpwifi_adapter *adapter = priv->adapter;
689 	int ret = 0;
690 
691 	if (priv->mgmt_frame_mask) {
692 		priv->mgmt_frame_mask = 0;
693 		ret = nxpwifi_mgmt_frame_reg(priv, priv->mgmt_frame_mask);
694 
695 		if (ret) {
696 			nxpwifi_dbg(adapter, ERROR,
697 				    "could not unregister mgmt frame rx\n");
698 			return ret;
699 		}
700 		priv->host_mlme_reg = false;
701 
702 	}
703 
704 	nxpwifi_deauthenticate(priv, NULL);
705 
706 	atomic_set(&adapter->iface_changing, 1);
707 	flush_workqueue(adapter->workqueue);
708 	flush_workqueue(adapter->rx_workqueue);
709 	nxpwifi_free_priv(priv);
710 	priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
711 	priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
712 	priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
713 
714 	return ret;
715 }
716 
717 static int
718 nxpwifi_init_new_priv_params(struct nxpwifi_private *priv,
719 			     struct net_device *dev,
720 			     enum nl80211_iftype type)
721 {
722 	struct nxpwifi_adapter *adapter = priv->adapter;
723 	int ret;
724 
725 	nxpwifi_init_priv(priv);
726 
727 	priv->bss_mode = type;
728 	priv->wdev.iftype = type;
729 
730 	nxpwifi_init_priv_params(priv, priv->netdev);
731 	priv->bss_started = 0;
732 
733 	switch (type) {
734 	case NL80211_IFTYPE_STATION:
735 		priv->bss_role = NXPWIFI_BSS_ROLE_STA;
736 		break;
737 	case NL80211_IFTYPE_AP:
738 		priv->bss_role = NXPWIFI_BSS_ROLE_UAP;
739 		break;
740 	default:
741 		nxpwifi_dbg(adapter, ERROR,
742 			    "%s: changing to %d not supported\n",
743 			    dev->name, type);
744 		return -EOPNOTSUPP;
745 	}
746 
747 	ret = nxpwifi_get_unused_bss_num(adapter, priv->bss_type,
748 					 &priv->bss_num);
749 
750 	if (ret) {
751 		nxpwifi_dbg(adapter, ERROR,
752 			    "%s: no unused bss_num for type %d\n",
753 			    dev->name, priv->bss_type);
754 		return ret;
755 	}
756 
757 	flush_workqueue(adapter->workqueue);
758 	atomic_set(&adapter->iface_changing, 0);
759 
760 	nxpwifi_set_mac_address(priv, dev, false, NULL);
761 
762 	return 0;
763 }
764 
765 static bool
766 is_vif_type_change_allowed(struct nxpwifi_adapter *adapter,
767 			   enum nl80211_iftype old_iftype,
768 			   enum nl80211_iftype new_iftype)
769 {
770 	switch (old_iftype) {
771 	case NL80211_IFTYPE_STATION:
772 		switch (new_iftype) {
773 		case NL80211_IFTYPE_AP:
774 			return adapter->curr_iface_comb.uap_intf !=
775 			       adapter->iface_limit.uap_intf;
776 		default:
777 			return false;
778 		}
779 
780 	case NL80211_IFTYPE_AP:
781 		switch (new_iftype) {
782 		case NL80211_IFTYPE_STATION:
783 			return adapter->curr_iface_comb.sta_intf !=
784 			       adapter->iface_limit.sta_intf;
785 		default:
786 			return false;
787 		}
788 
789 	default:
790 		break;
791 	}
792 
793 	return false;
794 }
795 
796 static void
797 update_vif_type_counter(struct nxpwifi_adapter *adapter,
798 			enum nl80211_iftype iftype,
799 			int change)
800 {
801 	switch (iftype) {
802 	case NL80211_IFTYPE_UNSPECIFIED:
803 	case NL80211_IFTYPE_STATION:
804 		adapter->curr_iface_comb.sta_intf += change;
805 		break;
806 	case NL80211_IFTYPE_AP:
807 		adapter->curr_iface_comb.uap_intf += change;
808 		break;
809 	case NL80211_IFTYPE_MONITOR:
810 		break;
811 	default:
812 		nxpwifi_dbg(adapter, ERROR,
813 			    "%s: Unsupported iftype passed: %d\n",
814 			    __func__, iftype);
815 		break;
816 	}
817 }
818 
819 static int
820 nxpwifi_change_vif_to_sta(struct net_device *dev,
821 			  enum nl80211_iftype curr_iftype,
822 			  enum nl80211_iftype type,
823 			  struct vif_params *params)
824 {
825 	struct nxpwifi_private *priv;
826 	struct nxpwifi_adapter *adapter;
827 	int ret;
828 
829 	priv = nxpwifi_netdev_get_priv(dev);
830 
831 	if (!priv)
832 		return -EINVAL;
833 
834 	adapter = priv->adapter;
835 
836 	nxpwifi_dbg(adapter, INFO,
837 		    "%s: changing role to station\n", dev->name);
838 
839 	ret = nxpwifi_deinit_priv_params(priv);
840 	if (ret)
841 		goto done;
842 	ret = nxpwifi_init_new_priv_params(priv, dev, type);
843 	if (ret)
844 		goto done;
845 
846 	update_vif_type_counter(adapter, curr_iftype, -1);
847 	update_vif_type_counter(adapter, type, 1);
848 	dev->ieee80211_ptr->iftype = type;
849 
850 	ret = nxpwifi_set_bss_mode(priv);
851 
852 	if (ret)
853 		goto done;
854 
855 	ret = nxpwifi_sta_init_cmd(priv, false, false);
856 
857 done:
858 	return ret;
859 }
860 
861 static int
862 nxpwifi_change_vif_to_ap(struct net_device *dev,
863 			 enum nl80211_iftype curr_iftype,
864 			 enum nl80211_iftype type,
865 			 struct vif_params *params)
866 {
867 	struct nxpwifi_private *priv;
868 	struct nxpwifi_adapter *adapter;
869 	int ret;
870 
871 	priv = nxpwifi_netdev_get_priv(dev);
872 
873 	if (!priv)
874 		return -EINVAL;
875 
876 	adapter = priv->adapter;
877 
878 	nxpwifi_dbg(adapter, INFO,
879 		    "%s: changing role to AP\n", dev->name);
880 
881 	ret = nxpwifi_deinit_priv_params(priv);
882 	if (ret)
883 		goto done;
884 
885 	ret = nxpwifi_init_new_priv_params(priv, dev, type);
886 	if (ret)
887 		goto done;
888 
889 	update_vif_type_counter(adapter, curr_iftype, -1);
890 	update_vif_type_counter(adapter, type, 1);
891 	dev->ieee80211_ptr->iftype = type;
892 
893 	ret = nxpwifi_set_bss_mode(priv);
894 
895 	if (ret)
896 		goto done;
897 
898 	ret = nxpwifi_sta_init_cmd(priv, false, false);
899 
900 done:
901 	return ret;
902 }
903 
904 /* cfg80211 operation handler to change interface type. */
905 static int
906 nxpwifi_cfg80211_change_virtual_intf(struct wiphy *wiphy,
907 				     struct net_device *dev,
908 				     enum nl80211_iftype type,
909 				     struct vif_params *params)
910 {
911 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
912 	enum nl80211_iftype curr_iftype = dev->ieee80211_ptr->iftype;
913 
914 	if (priv->scan_request) {
915 		nxpwifi_dbg(priv->adapter, ERROR,
916 			    "change virtual interface: scan in process\n");
917 		return -EBUSY;
918 	}
919 
920 	if (type == NL80211_IFTYPE_UNSPECIFIED) {
921 		nxpwifi_dbg(priv->adapter, INFO,
922 			    "%s: no new type specified, keeping old type %d\n",
923 			    dev->name, curr_iftype);
924 		return 0;
925 	}
926 
927 	if (curr_iftype == type) {
928 		nxpwifi_dbg(priv->adapter, INFO,
929 			    "%s: interface already is of type %d\n",
930 			    dev->name, curr_iftype);
931 		return 0;
932 	}
933 
934 	if (!is_vif_type_change_allowed(priv->adapter, curr_iftype, type)) {
935 		nxpwifi_dbg(priv->adapter, ERROR,
936 			    "%s: change from type %d to %d is not allowed\n",
937 			    dev->name, curr_iftype, type);
938 		return -EOPNOTSUPP;
939 	}
940 
941 	switch (curr_iftype) {
942 	case NL80211_IFTYPE_STATION:
943 		switch (type) {
944 		case NL80211_IFTYPE_AP:
945 			return nxpwifi_change_vif_to_ap(dev, curr_iftype, type,
946 							params);
947 		default:
948 			goto errnotsupp;
949 		}
950 
951 	case NL80211_IFTYPE_AP:
952 		switch (type) {
953 		case NL80211_IFTYPE_STATION:
954 			return nxpwifi_change_vif_to_sta(dev, curr_iftype,
955 							 type, params);
956 		default:
957 			goto errnotsupp;
958 		}
959 
960 	default:
961 		goto errnotsupp;
962 	}
963 
964 errnotsupp:
965 	nxpwifi_dbg(priv->adapter, ERROR,
966 		    "unsupported interface type transition: %d to %d\n",
967 		    curr_iftype, type);
968 	return -EOPNOTSUPP;
969 }
970 
971 #define RATE_FORMAT_LG  0
972 #define RATE_FORMAT_HT  1
973 #define RATE_FORMAT_VHT 2
974 #define RATE_FORMAT_HE  3
975 
976 static void
977 nxpwifi_parse_htinfo(struct nxpwifi_private *priv, u8 rateinfo, u8 htinfo,
978 		     struct rate_info *rate)
979 {
980 	struct nxpwifi_adapter *adapter = priv->adapter;
981 	u8 rate_format;
982 	u8 he_dcm;
983 	u8 stbc;
984 	u8 gi;
985 	u8 bw;
986 	/* Bitrates in multiples of 100kb/s. */
987 	static const int legacy_rates[] = {
988 		[0] = 10,
989 		[1] = 20,
990 		[2] = 55,
991 		[3] = 110,
992 		[4] = 60, /* NXPWIFI_RATE_INDEX_OFDM0 */
993 		[5] = 60,
994 		[6] = 90,
995 		[7] = 120,
996 		[8] = 180,
997 		[9] = 240,
998 		[10] = 360,
999 		[11] = 480,
1000 		[12] = 540,
1001 	};
1002 
1003 	rate_format = htinfo & 0x3;
1004 
1005 	switch (rate_format) {
1006 	case RATE_FORMAT_LG:
1007 		if (rateinfo < ARRAY_SIZE(legacy_rates))
1008 			rate->legacy = legacy_rates[rateinfo];
1009 		break;
1010 	case RATE_FORMAT_HT:
1011 		rate->mcs = rateinfo;
1012 		rate->flags |= RATE_INFO_FLAGS_MCS;
1013 		break;
1014 	case RATE_FORMAT_VHT:
1015 		rate->mcs = rateinfo & 0xF;
1016 		rate->flags |= RATE_INFO_FLAGS_VHT_MCS;
1017 		break;
1018 	case RATE_FORMAT_HE:
1019 		rate->mcs = rateinfo & 0xF;
1020 		rate->flags |= RATE_INFO_FLAGS_HE_MCS;
1021 		he_dcm = 0; /* ToDo: ext_rate_info */
1022 		gi = (htinfo & BIT(4)) >> 4 |
1023 		     (htinfo & BIT(7)) >> 6;
1024 		stbc = (htinfo & BIT(5)) >> 5;
1025 		if (gi > 3) {
1026 			nxpwifi_dbg(adapter, ERROR, "Invalid gi value\n");
1027 			break;
1028 		}
1029 		if (gi == 3 && stbc && he_dcm) {
1030 			gi = 0;
1031 			stbc = 0;
1032 			he_dcm = 0;
1033 		}
1034 		if (gi > 0)
1035 			gi -= 1;
1036 		rate->he_gi = gi;
1037 		rate->he_dcm = he_dcm;
1038 		break;
1039 	}
1040 
1041 	bw = (htinfo & 0xC) >> 2;
1042 
1043 	switch (bw) {
1044 	case 0:
1045 		rate->bw = RATE_INFO_BW_20;
1046 		break;
1047 	case 1:
1048 		rate->bw = RATE_INFO_BW_40;
1049 		break;
1050 	case 2:
1051 		rate->bw = RATE_INFO_BW_80;
1052 		break;
1053 	case 3:
1054 		rate->bw = RATE_INFO_BW_160;
1055 		break;
1056 	}
1057 
1058 	if (rate_format != RATE_FORMAT_HE && (htinfo & BIT(4)))
1059 		rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1060 
1061 	if ((rateinfo >> 4) == 1)
1062 		rate->nss = 2;
1063 	else
1064 		rate->nss = 1;
1065 }
1066 
1067 /*
1068  * Dump station statistics into station_info.
1069  * Includes bytes/packets counters, signal level, and TX/RX rates.
1070  */
1071 static int
1072 nxpwifi_dump_station_info(struct nxpwifi_private *priv,
1073 			  struct nxpwifi_sta_node *node,
1074 			  struct station_info *sinfo)
1075 {
1076 	u32 rate;
1077 	int ret;
1078 
1079 	sinfo->filled = BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
1080 			BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
1081 			BIT_ULL(NL80211_STA_INFO_RX_PACKETS) |
1082 			BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
1083 			BIT_ULL(NL80211_STA_INFO_TX_BITRATE) |
1084 			BIT_ULL(NL80211_STA_INFO_SIGNAL) |
1085 			BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
1086 
1087 	if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP) {
1088 		if (!node)
1089 			return -ENOENT;
1090 
1091 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
1092 				BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1093 		sinfo->inactive_time =
1094 			jiffies_to_msecs(jiffies - node->stats.last_rx);
1095 
1096 		sinfo->signal = node->stats.rssi;
1097 		sinfo->signal_avg = node->stats.rssi;
1098 		sinfo->rx_bytes = node->stats.rx_bytes;
1099 		sinfo->tx_bytes = node->stats.tx_bytes;
1100 		sinfo->rx_packets = node->stats.rx_packets;
1101 		sinfo->tx_packets = node->stats.tx_packets;
1102 		sinfo->tx_failed = node->stats.tx_failed;
1103 
1104 		nxpwifi_parse_htinfo(priv, priv->tx_rate,
1105 				     node->stats.last_tx_htinfo,
1106 				     &sinfo->txrate);
1107 		sinfo->txrate.legacy = node->stats.last_tx_rate * 5;
1108 
1109 		return 0;
1110 	}
1111 
1112 	/* Get signal information from the firmware */
1113 	ret = nxpwifi_get_rssi_info(priv);
1114 	if (ret) {
1115 		nxpwifi_dbg(priv->adapter, ERROR,
1116 			    "failed to get signal information\n");
1117 		goto done;
1118 	}
1119 
1120 	ret = nxpwifi_drv_get_data_rate(priv, &rate);
1121 	if (ret) {
1122 		nxpwifi_dbg(priv->adapter, ERROR,
1123 			    "getting data rate error\n");
1124 		goto done;
1125 	}
1126 
1127 	/* Retrieve DTIM period value from firmware. */
1128 	nxpwifi_get_802_11_snmp_mib(priv, DTIM_PERIOD_I, &priv->dtim_period);
1129 
1130 	nxpwifi_parse_htinfo(priv, priv->tx_rate, priv->tx_htinfo,
1131 			     &sinfo->txrate);
1132 
1133 	sinfo->signal_avg = priv->bcn_rssi_avg;
1134 	sinfo->rx_bytes = priv->stats.rx_bytes;
1135 	sinfo->tx_bytes = priv->stats.tx_bytes;
1136 	sinfo->rx_packets = priv->stats.rx_packets;
1137 	sinfo->tx_packets = priv->stats.tx_packets;
1138 	sinfo->signal = priv->bcn_rssi_avg;
1139 	/* Convert bitrate from 500 kb/s units to 100 kb/s units. */
1140 	sinfo->txrate.legacy = rate * 5;
1141 
1142 	sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
1143 	nxpwifi_parse_htinfo(priv, priv->rxpd_rate, priv->rxpd_htinfo,
1144 			     &sinfo->rxrate);
1145 
1146 	if (priv->bss_mode == NL80211_IFTYPE_STATION) {
1147 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BSS_PARAM);
1148 		sinfo->bss_param.flags = 0;
1149 		if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1150 						WLAN_CAPABILITY_SHORT_PREAMBLE)
1151 			sinfo->bss_param.flags |=
1152 					BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1153 		if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1154 						WLAN_CAPABILITY_SHORT_SLOT_TIME)
1155 			sinfo->bss_param.flags |=
1156 					BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1157 		sinfo->bss_param.dtim_period = priv->dtim_period;
1158 		sinfo->bss_param.beacon_interval =
1159 			priv->curr_bss_params.bss_descriptor.beacon_period;
1160 	}
1161 
1162 done:
1163 	return ret;
1164 }
1165 
1166 /*
1167  * cfg80211 op: get station information.
1168  * Works only when connected and fills station_info with current stats.
1169  */
1170 static int
1171 nxpwifi_cfg80211_get_station(struct wiphy *wiphy, struct wireless_dev *wdev,
1172 			     const u8 *mac, struct station_info *sinfo)
1173 {
1174 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
1175 	struct nxpwifi_sta_node *node;
1176 
1177 	if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_STA) {
1178 		if (!priv->media_connected ||
1179 		    memcmp(mac, priv->cfg_bssid, ETH_ALEN))
1180 			return -ENOENT;
1181 		node = NULL;
1182 	} else {
1183 		rcu_read_lock();
1184 		node = nxpwifi_get_sta_entry(priv, mac);
1185 		rcu_read_unlock();
1186 	}
1187 
1188 	return nxpwifi_dump_station_info(priv, node, sinfo);
1189 }
1190 
1191 /* cfg80211 operation handler to dump station information. */
1192 static int
1193 nxpwifi_cfg80211_dump_station(struct wiphy *wiphy, struct wireless_dev *wdev,
1194 			      int idx, u8 *mac, struct station_info *sinfo)
1195 {
1196 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
1197 	struct nxpwifi_sta_node *node;
1198 	struct nxpwifi_sta_node *found = NULL;
1199 	int i;
1200 
1201 	if ((GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_STA) &&
1202 	    priv->media_connected && idx == 0) {
1203 		ether_addr_copy(mac, priv->cfg_bssid);
1204 		return nxpwifi_dump_station_info(priv, NULL, sinfo);
1205 	} else if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP) {
1206 		nxpwifi_ap_get_sta_list(priv);
1207 
1208 		i = 0;
1209 		rcu_read_lock();
1210 		list_for_each_entry_rcu(node, &priv->sta_list, list) {
1211 			if (i++ != idx)
1212 				continue;
1213 			found = node;
1214 			break;
1215 		}
1216 		rcu_read_unlock();
1217 
1218 		if (found) {
1219 			ether_addr_copy(mac, node->mac_addr);
1220 			return nxpwifi_dump_station_info(priv, node, sinfo);
1221 		}
1222 	}
1223 
1224 	return -ENOENT;
1225 }
1226 
1227 static int
1228 nxpwifi_cfg80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
1229 			     int idx, struct survey_info *survey)
1230 {
1231 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
1232 	struct nxpwifi_chan_stats *pchan_stats = priv->adapter->chan_stats;
1233 	enum nl80211_band band;
1234 	u8 chan_num;
1235 
1236 	nxpwifi_dbg(priv->adapter, DUMP, "dump_survey idx=%d\n", idx);
1237 
1238 	memset(survey, 0, sizeof(struct survey_info));
1239 
1240 	if ((GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_STA) &&
1241 	    priv->media_connected && idx == 0) {
1242 		u8 curr_bss_band = priv->curr_bss_params.band;
1243 		u32 chan = priv->curr_bss_params.bss_descriptor.channel;
1244 
1245 		band = nxpwifi_band_to_radio_type(curr_bss_band);
1246 		survey->channel = ieee80211_get_channel
1247 				  (wiphy,
1248 				   ieee80211_channel_to_frequency(chan, band));
1249 
1250 		if (priv->bcn_nf_last) {
1251 			survey->filled = SURVEY_INFO_NOISE_DBM;
1252 			survey->noise = priv->bcn_nf_last;
1253 		}
1254 		return 0;
1255 	}
1256 
1257 	if (idx >= priv->adapter->num_in_chan_stats)
1258 		return -ENOENT;
1259 
1260 	if (!pchan_stats[idx].cca_scan_dur)
1261 		return 0;
1262 
1263 	band = pchan_stats[idx].bandcfg;
1264 	chan_num = pchan_stats[idx].chan_num;
1265 	survey->channel = ieee80211_get_channel
1266 			  (wiphy,
1267 			   ieee80211_channel_to_frequency(chan_num, band));
1268 	survey->filled = SURVEY_INFO_NOISE_DBM |
1269 			 SURVEY_INFO_TIME |
1270 			 SURVEY_INFO_TIME_BUSY;
1271 	survey->noise = pchan_stats[idx].noise;
1272 	survey->time = pchan_stats[idx].cca_scan_dur;
1273 	survey->time_busy = pchan_stats[idx].cca_busy_dur;
1274 
1275 	return 0;
1276 }
1277 
1278 /* Supported rates to be advertised to the cfg80211 */
1279 static struct ieee80211_rate nxpwifi_rates[] = {
1280 	{.bitrate = 10, .hw_value = 2, },
1281 	{.bitrate = 20, .hw_value = 4, },
1282 	{.bitrate = 55, .hw_value = 11, },
1283 	{.bitrate = 110, .hw_value = 22, },
1284 	{.bitrate = 60, .hw_value = 12, },
1285 	{.bitrate = 90, .hw_value = 18, },
1286 	{.bitrate = 120, .hw_value = 24, },
1287 	{.bitrate = 180, .hw_value = 36, },
1288 	{.bitrate = 240, .hw_value = 48, },
1289 	{.bitrate = 360, .hw_value = 72, },
1290 	{.bitrate = 480, .hw_value = 96, },
1291 	{.bitrate = 540, .hw_value = 108, },
1292 };
1293 
1294 /* Channel definitions to be advertised to cfg80211 */
1295 static struct ieee80211_channel nxpwifi_channels_2ghz[] = {
1296 	{.center_freq = 2412, .hw_value = 1, },
1297 	{.center_freq = 2417, .hw_value = 2, },
1298 	{.center_freq = 2422, .hw_value = 3, },
1299 	{.center_freq = 2427, .hw_value = 4, },
1300 	{.center_freq = 2432, .hw_value = 5, },
1301 	{.center_freq = 2437, .hw_value = 6, },
1302 	{.center_freq = 2442, .hw_value = 7, },
1303 	{.center_freq = 2447, .hw_value = 8, },
1304 	{.center_freq = 2452, .hw_value = 9, },
1305 	{.center_freq = 2457, .hw_value = 10, },
1306 	{.center_freq = 2462, .hw_value = 11, },
1307 	{.center_freq = 2467, .hw_value = 12, },
1308 	{.center_freq = 2472, .hw_value = 13, },
1309 	{.center_freq = 2484, .hw_value = 14, },
1310 };
1311 
1312 static struct ieee80211_supported_band nxpwifi_band_2ghz = {
1313 	.band = NL80211_BAND_2GHZ,
1314 	.channels = nxpwifi_channels_2ghz,
1315 	.n_channels = ARRAY_SIZE(nxpwifi_channels_2ghz),
1316 	.bitrates = nxpwifi_rates,
1317 	.n_bitrates = ARRAY_SIZE(nxpwifi_rates),
1318 };
1319 
1320 static struct ieee80211_channel nxpwifi_channels_5ghz[] = {
1321 	{.center_freq = 5040, .hw_value = 8, },
1322 	{.center_freq = 5060, .hw_value = 12, },
1323 	{.center_freq = 5080, .hw_value = 16, },
1324 	{.center_freq = 5170, .hw_value = 34, },
1325 	{.center_freq = 5190, .hw_value = 38, },
1326 	{.center_freq = 5210, .hw_value = 42, },
1327 	{.center_freq = 5230, .hw_value = 46, },
1328 	{.center_freq = 5180, .hw_value = 36, },
1329 	{.center_freq = 5200, .hw_value = 40, },
1330 	{.center_freq = 5220, .hw_value = 44, },
1331 	{.center_freq = 5240, .hw_value = 48, },
1332 	{.center_freq = 5260, .hw_value = 52, },
1333 	{.center_freq = 5280, .hw_value = 56, },
1334 	{.center_freq = 5300, .hw_value = 60, },
1335 	{.center_freq = 5320, .hw_value = 64, },
1336 	{.center_freq = 5500, .hw_value = 100, },
1337 	{.center_freq = 5520, .hw_value = 104, },
1338 	{.center_freq = 5540, .hw_value = 108, },
1339 	{.center_freq = 5560, .hw_value = 112, },
1340 	{.center_freq = 5580, .hw_value = 116, },
1341 	{.center_freq = 5600, .hw_value = 120, },
1342 	{.center_freq = 5620, .hw_value = 124, },
1343 	{.center_freq = 5640, .hw_value = 128, },
1344 	{.center_freq = 5660, .hw_value = 132, },
1345 	{.center_freq = 5680, .hw_value = 136, },
1346 	{.center_freq = 5700, .hw_value = 140, },
1347 	{.center_freq = 5745, .hw_value = 149, },
1348 	{.center_freq = 5765, .hw_value = 153, },
1349 	{.center_freq = 5785, .hw_value = 157, },
1350 	{.center_freq = 5805, .hw_value = 161, },
1351 	{.center_freq = 5825, .hw_value = 165, },
1352 };
1353 
1354 static struct ieee80211_supported_band nxpwifi_band_5ghz = {
1355 	.band = NL80211_BAND_5GHZ,
1356 	.channels = nxpwifi_channels_5ghz,
1357 	.n_channels = ARRAY_SIZE(nxpwifi_channels_5ghz),
1358 	.bitrates = nxpwifi_rates + 4,
1359 	.n_bitrates = ARRAY_SIZE(nxpwifi_rates) - 4,
1360 };
1361 
1362 /* Supported crypto cipher suits to be advertised to cfg80211 */
1363 static const u32 nxpwifi_cipher_suites[] = {
1364 	WLAN_CIPHER_SUITE_WEP40,
1365 	WLAN_CIPHER_SUITE_WEP104,
1366 	WLAN_CIPHER_SUITE_TKIP,
1367 	WLAN_CIPHER_SUITE_CCMP,
1368 	WLAN_CIPHER_SUITE_SMS4,
1369 	WLAN_CIPHER_SUITE_AES_CMAC,
1370 	WLAN_CIPHER_SUITE_GCMP_256,
1371 	WLAN_CIPHER_SUITE_CCMP_256,
1372 	WLAN_CIPHER_SUITE_BIP_GMAC_256,
1373 	WLAN_CIPHER_SUITE_BIP_CMAC_256,
1374 };
1375 
1376 /* Supported mgmt frame types to be advertised to cfg80211 */
1377 static const struct ieee80211_txrx_stypes
1378 nxpwifi_mgmt_stypes[NUM_NL80211_IFTYPES] = {
1379 	[NL80211_IFTYPE_STATION] = {
1380 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1381 		      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1382 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1383 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1384 	},
1385 	[NL80211_IFTYPE_AP] = {
1386 		.tx = 0xffff,
1387 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
1388 		      BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
1389 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
1390 		      BIT(IEEE80211_STYPE_DISASSOC >> 4) |
1391 		      BIT(IEEE80211_STYPE_AUTH >> 4) |
1392 		      BIT(IEEE80211_STYPE_DEAUTH >> 4) |
1393 		      BIT(IEEE80211_STYPE_ACTION >> 4),
1394 	},
1395 };
1396 
1397 /*
1398  * cfg80211 op: set bitrate mask.
1399  * Converts cfg80211 bitrate selections into firmware bitmap format.
1400  */
1401 static int
1402 nxpwifi_cfg80211_set_bitrate_mask(struct wiphy *wiphy,
1403 				  struct net_device *dev,
1404 				  unsigned int link_id,
1405 				  const u8 *peer,
1406 				  const struct cfg80211_bitrate_mask *mask)
1407 {
1408 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
1409 	u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
1410 	enum nl80211_band band;
1411 	struct nxpwifi_adapter *adapter = priv->adapter;
1412 
1413 	if (!priv->media_connected) {
1414 		nxpwifi_dbg(adapter, ERROR,
1415 			    "Can not set Tx data rate in disconnected state\n");
1416 		return -EINVAL;
1417 	}
1418 
1419 	band = nxpwifi_band_to_radio_type(priv->curr_bss_params.band);
1420 
1421 	memset(bitmap_rates, 0, sizeof(bitmap_rates));
1422 
1423 	/* Fill HR/DSSS legacy rates (2.4 GHz only). */
1424 	if (band == NL80211_BAND_2GHZ)
1425 		bitmap_rates[0] = mask->control[band].legacy & 0x000f;
1426 
1427 	/* Fill OFDM legacy rates. */
1428 	if (band == NL80211_BAND_2GHZ)
1429 		bitmap_rates[1] = (mask->control[band].legacy & 0x0ff0) >> 4;
1430 	else
1431 		bitmap_rates[1] = mask->control[band].legacy;
1432 
1433 	/* Fill HT MCS bitmap (1x1 or 2x2 depending on hardware). */
1434 	bitmap_rates[2] = mask->control[band].ht_mcs[0];
1435 	if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1436 		bitmap_rates[2] |= mask->control[band].ht_mcs[1] << 8;
1437 
1438 	/* Fill VHT MCS bitmap if supported by firmware. */
1439 	if (adapter->fw_api_ver == NXPWIFI_FW_V15) {
1440 		bitmap_rates[10] = mask->control[band].vht_mcs[0];
1441 		if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1442 			bitmap_rates[11] = mask->control[band].vht_mcs[1];
1443 	}
1444 
1445 	return nxpwifi_set_tx_rate(priv, bitmap_rates);
1446 }
1447 
1448 /*
1449  * cfg80211 op: configure connection-quality monitoring.
1450  * Subscribes or unsubscribes HIGH_RSSI and LOW_RSSI events to firmware.
1451  */
1452 static int nxpwifi_cfg80211_set_cqm_rssi_config(struct wiphy *wiphy,
1453 						struct net_device *dev,
1454 						s32 rssi_thold, u32 rssi_hyst)
1455 {
1456 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
1457 	struct nxpwifi_ds_misc_subsc_evt subsc_evt;
1458 	int ret = 0;
1459 
1460 	priv->cqm_rssi_thold = rssi_thold;
1461 	priv->cqm_rssi_hyst = rssi_hyst;
1462 
1463 	memset(&subsc_evt, 0x00, sizeof(struct nxpwifi_ds_misc_subsc_evt));
1464 	subsc_evt.events = BITMASK_BCN_RSSI_LOW | BITMASK_BCN_RSSI_HIGH;
1465 
1466 	/* Subscribe/unsubscribe low and high rssi events */
1467 	if (rssi_thold && rssi_hyst) {
1468 		subsc_evt.action = HOST_ACT_BITWISE_SET;
1469 		subsc_evt.bcn_l_rssi_cfg.abs_value = abs(rssi_thold);
1470 		subsc_evt.bcn_h_rssi_cfg.abs_value = abs(rssi_thold);
1471 		subsc_evt.bcn_l_rssi_cfg.evt_freq = 1;
1472 		subsc_evt.bcn_h_rssi_cfg.evt_freq = 1;
1473 		ret = nxpwifi_802_11_subscribe_event(priv, &subsc_evt);
1474 	} else {
1475 		subsc_evt.action = HOST_ACT_BITWISE_CLR;
1476 		ret = nxpwifi_802_11_subscribe_event(priv, &subsc_evt);
1477 	}
1478 
1479 	return ret;
1480 }
1481 
1482 /*
1483  * cfg80211 operation handler for change_beacon.
1484  * Function retrieves and sets modified management IEs to FW.
1485  */
1486 int nxpwifi_cfg80211_change_beacon(struct wiphy *wiphy,
1487 				   struct net_device *dev,
1488 				   struct cfg80211_ap_update *params)
1489 {
1490 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
1491 	struct nxpwifi_adapter *adapter = priv->adapter;
1492 	struct cfg80211_beacon_data *data = &params->beacon;
1493 	int ret;
1494 
1495 	nxpwifi_cancel_scan(adapter);
1496 
1497 	if (GET_BSS_ROLE(priv) != NXPWIFI_BSS_ROLE_UAP) {
1498 		nxpwifi_dbg(priv->adapter, ERROR,
1499 			    "%s: bss_type mismatched\n", __func__);
1500 		return -EINVAL;
1501 	}
1502 
1503 	ret = nxpwifi_set_mgmt_ies(priv, data);
1504 	if (ret)
1505 		nxpwifi_dbg(priv->adapter, ERROR,
1506 			    "%s: setting mgmt ies failed\n", __func__);
1507 
1508 	return ret;
1509 }
1510 
1511 /*
1512  * cfg80211 operation handler for del_station.
1513  * Function deauthenticates station which value is provided in mac parameter.
1514  * If mac is NULL/broadcast, all stations in associated station list are
1515  * deauthenticated. If bss is not started or there are no stations in
1516  * associated stations list, no action is taken.
1517  */
1518 static int
1519 nxpwifi_cfg80211_del_station(struct wiphy *wiphy, struct wireless_dev *wdev,
1520 			     struct station_del_parameters *params)
1521 {
1522 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
1523 	struct nxpwifi_sta_node *sta_node;
1524 	u8 deauth_mac[ETH_ALEN];
1525 	int ret = 0;
1526 
1527 	if (!priv->bss_started && priv->wdev.links[0].cac_started) {
1528 		nxpwifi_dbg(priv->adapter, INFO, "%s: abort CAC!\n", __func__);
1529 		nxpwifi_abort_cac(priv);
1530 	}
1531 
1532 	if (list_empty(&priv->sta_list) || !priv->bss_started)
1533 		return 0;
1534 
1535 	if (!params->mac || is_broadcast_ether_addr(params->mac))
1536 		return 0;
1537 
1538 	nxpwifi_dbg(priv->adapter, INFO, "%s: mac address %pM\n",
1539 		    __func__, params->mac);
1540 
1541 	eth_zero_addr(deauth_mac);
1542 
1543 	rcu_read_lock();
1544 	sta_node = nxpwifi_get_sta_entry(priv, params->mac);
1545 	if (sta_node)
1546 		ether_addr_copy(deauth_mac, params->mac);
1547 	rcu_read_unlock();
1548 
1549 	if (is_valid_ether_addr(deauth_mac)) {
1550 		ret = nxpwifi_uap_sta_deauth(priv, deauth_mac);
1551 		nxpwifi_del_sta_entry(priv, deauth_mac);
1552 	}
1553 	return ret;
1554 }
1555 
1556 static int
1557 nxpwifi_cfg80211_set_antenna(struct wiphy *wiphy, int radio_idx, u32 tx_ant, u32 rx_ant)
1558 {
1559 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
1560 	struct nxpwifi_private *priv = nxpwifi_get_priv(adapter,
1561 							NXPWIFI_BSS_ROLE_ANY);
1562 	struct nxpwifi_ds_ant_cfg ant_cfg;
1563 
1564 	if (!tx_ant || !rx_ant)
1565 		return -EOPNOTSUPP;
1566 
1567 	if (adapter->hw_dev_mcs_support != HT_STREAM_2X2) {
1568 		/*
1569 		 * Not a MIMO chip. User should provide specific antenna number
1570 		 * for Tx/Rx path or enable all antennas for diversity
1571 		 */
1572 		if (tx_ant != rx_ant)
1573 			return -EOPNOTSUPP;
1574 
1575 		if ((tx_ant & (tx_ant - 1)) &&
1576 		    (tx_ant != BIT(adapter->number_of_antenna) - 1))
1577 			return -EOPNOTSUPP;
1578 
1579 		if ((tx_ant == BIT(adapter->number_of_antenna) - 1) &&
1580 		    priv->adapter->number_of_antenna > 1) {
1581 			tx_ant = RF_ANTENNA_AUTO;
1582 			rx_ant = RF_ANTENNA_AUTO;
1583 		}
1584 	} else {
1585 		struct ieee80211_sta_ht_cap *ht_info;
1586 		int rx_mcs_supp;
1587 		enum nl80211_band band;
1588 
1589 		if ((tx_ant == 0x1 && rx_ant == 0x1)) {
1590 			adapter->user_dev_mcs_support = HT_STREAM_1X1;
1591 			if (adapter->is_hw_11ac_capable)
1592 				adapter->usr_dot_11ac_mcs_support =
1593 						NXPWIFI_11AC_MCS_MAP_1X1;
1594 		} else {
1595 			adapter->user_dev_mcs_support = HT_STREAM_2X2;
1596 			if (adapter->is_hw_11ac_capable)
1597 				adapter->usr_dot_11ac_mcs_support =
1598 						NXPWIFI_11AC_MCS_MAP_2X2;
1599 		}
1600 
1601 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
1602 			if (!adapter->wiphy->bands[band])
1603 				continue;
1604 
1605 			ht_info = &adapter->wiphy->bands[band]->ht_cap;
1606 			rx_mcs_supp =
1607 				GET_RXMCSSUPP(adapter->user_dev_mcs_support);
1608 			memset(&ht_info->mcs, 0, adapter->number_of_antenna);
1609 			memset(&ht_info->mcs, 0xff, rx_mcs_supp);
1610 		}
1611 	}
1612 
1613 	ant_cfg.tx_ant = tx_ant;
1614 	ant_cfg.rx_ant = rx_ant;
1615 
1616 	return nxpwifi_set_rf_antenna(priv, &ant_cfg);
1617 }
1618 
1619 static int
1620 nxpwifi_cfg80211_get_antenna(struct wiphy *wiphy, int radio_idx, u32 *tx_ant, u32 *rx_ant)
1621 {
1622 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
1623 	struct nxpwifi_private *priv = nxpwifi_get_priv(adapter,
1624 							NXPWIFI_BSS_ROLE_ANY);
1625 	int ret;
1626 
1627 	ret = nxpwifi_get_rf_antenna(priv, tx_ant, rx_ant);
1628 
1629 	return ret;
1630 }
1631 
1632 /*
1633  * cfg80211 op: stop AP.
1634  * Stops the BSS running on the uAP interface.
1635  */
1636 static int nxpwifi_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *dev,
1637 				    unsigned int link_id)
1638 {
1639 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
1640 	int ret;
1641 
1642 	nxpwifi_abort_cac(priv);
1643 
1644 	if (nxpwifi_del_mgmt_ies(priv))
1645 		nxpwifi_dbg(priv->adapter, ERROR,
1646 			    "Failed to delete mgmt IEs!\n");
1647 
1648 	priv->ap_11n_enabled = 0;
1649 	memset(&priv->bss_cfg, 0, sizeof(priv->bss_cfg));
1650 
1651 	ret = nxpwifi_ap_stop_bss(priv);
1652 	if (ret) {
1653 		nxpwifi_dbg(priv->adapter, ERROR,
1654 			    "Failed to stop the BSS\n");
1655 		goto done;
1656 	}
1657 
1658 	ret = nxpwifi_ap_sys_reset(priv);
1659 	if (ret) {
1660 		nxpwifi_dbg(priv->adapter, ERROR,
1661 			    "Failed to reset BSS\n");
1662 		goto done;
1663 	}
1664 
1665 	netif_carrier_off(priv->netdev);
1666 	nxpwifi_stop_net_dev_queue(priv->netdev, priv->adapter);
1667 
1668 	if (atomic_dec_and_test(&priv->adapter->uap_count)) {
1669 		priv->adapter->chandef_valid = false;
1670 		memset(&priv->adapter->chandef, 0, sizeof(priv->adapter->chandef));
1671 	}
1672 
1673 done:
1674 	return ret;
1675 }
1676 
1677 /*
1678  * cfg80211 op: start AP.
1679  * Applies beacon/DTIM/SSID/security settings to the uAP configuration and
1680  * starts the BSS.
1681  */
1682 static int nxpwifi_cfg80211_start_ap(struct wiphy *wiphy,
1683 				     struct net_device *dev,
1684 				     struct cfg80211_ap_settings *params)
1685 {
1686 	struct nxpwifi_uap_bss_param *bss_cfg;
1687 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
1688 	struct nxpwifi_adapter *adapter = priv->adapter;
1689 	struct cfg80211_chan_def use_chandef;
1690 	bool is_first_uap = false;
1691 	int ret;
1692 
1693 	/*
1694 	 * Adapter is the HW channel owner (single PHY).
1695 	 * All UAP interfaces on the same adapter must share
1696 	 * the same RF channel.
1697 	 */
1698 	use_chandef = params->chandef;
1699 
1700 	if (adapter->chandef_valid) {
1701 		if (!cfg80211_chandef_identical(&adapter->chandef,
1702 						&params->chandef)) {
1703 			nxpwifi_dbg(adapter, INFO,
1704 				    "UAP already running on channel %d, ignore requested channel %d\n",
1705 				    adapter->chandef.chan->hw_value,
1706 				    params->chandef.chan->hw_value);
1707 		}
1708 		use_chandef = adapter->chandef;
1709 	} else	{
1710 		is_first_uap = true;
1711 	}
1712 
1713 	if (GET_BSS_ROLE(priv) != NXPWIFI_BSS_ROLE_UAP)
1714 		return -EINVAL;
1715 
1716 	if (!nxpwifi_is_channel_setting_allowable(priv, params->chandef.chan))
1717 		return -EOPNOTSUPP;
1718 
1719 	bss_cfg = kzalloc_obj(*bss_cfg);
1720 	if (!bss_cfg)
1721 		return -ENOMEM;
1722 
1723 	nxpwifi_set_sys_config_invalid_data(bss_cfg);
1724 
1725 	memcpy(bss_cfg->mac_addr, priv->curr_addr, ETH_ALEN);
1726 
1727 	if (params->beacon_interval)
1728 		bss_cfg->beacon_period = params->beacon_interval;
1729 	if (params->dtim_period)
1730 		bss_cfg->dtim_period = params->dtim_period;
1731 
1732 	if (params->ssid && params->ssid_len) {
1733 		memcpy(bss_cfg->ssid.ssid, params->ssid, params->ssid_len);
1734 		bss_cfg->ssid.ssid_len = params->ssid_len;
1735 	}
1736 	if (params->inactivity_timeout > 0) {
1737 		/* sta_ao_timer/ps_sta_ao_timer is in unit of 100ms */
1738 		bss_cfg->sta_ao_timer = 10 * params->inactivity_timeout;
1739 		bss_cfg->ps_sta_ao_timer = 10 * params->inactivity_timeout;
1740 	}
1741 
1742 	/* Default: SSID is visible */
1743 	bss_cfg->bcast_ssid_ctl = NXPWIFI_BCAST_SSID_VISIBLE;
1744 
1745 	switch (params->hidden_ssid) {
1746 	case NL80211_HIDDEN_SSID_NOT_IN_USE:
1747 		bss_cfg->bcast_ssid_ctl = NXPWIFI_BCAST_SSID_VISIBLE;
1748 		break;
1749 	case NL80211_HIDDEN_SSID_ZERO_LEN:
1750 		bss_cfg->bcast_ssid_ctl = NXPWIFI_BCAST_SSID_HIDE_LEN_ZERO;
1751 		break;
1752 	case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
1753 		bss_cfg->bcast_ssid_ctl = NXPWIFI_BCAST_SSID_HIDE_LEN_RETAIN;
1754 		break;
1755 	}
1756 
1757 	nxpwifi_uap_set_channel(priv, bss_cfg, use_chandef);
1758 	nxpwifi_set_uap_rates(bss_cfg, params);
1759 
1760 	ret = nxpwifi_set_secure_params(priv, bss_cfg, params);
1761 	if (ret) {
1762 		nxpwifi_dbg(adapter, ERROR,
1763 			    "Failed to parse security parameters!\n");
1764 		goto done;
1765 	}
1766 
1767 	nxpwifi_set_ht_params(priv, bss_cfg, params);
1768 
1769 	if (adapter->is_hw_11ac_capable) {
1770 		nxpwifi_set_vht_params(priv, bss_cfg, params);
1771 		nxpwifi_set_vht_width(priv, use_chandef.width,
1772 				      priv->ap_11ac_enabled);
1773 	}
1774 
1775 	if (priv->ap_11ac_enabled)
1776 		nxpwifi_set_11ac_ba_params(priv);
1777 	else
1778 		nxpwifi_set_ba_params(priv);
1779 
1780 	if (adapter->is_hw_11ax_capable) {
1781 		priv->ap_11ax_enabled =
1782 			nxpwifi_check_11ax_capability(priv, bss_cfg, params);
1783 		if (priv->ap_11ax_enabled)
1784 			nxpwifi_set_11ax_status(priv, bss_cfg, params);
1785 	}
1786 
1787 	nxpwifi_set_wmm_params(priv, bss_cfg, params);
1788 
1789 	if (nxpwifi_is_11h_active(priv))
1790 		nxpwifi_set_tpc_params(priv, bss_cfg, params);
1791 
1792 	if (nxpwifi_is_11h_active(priv) &&
1793 	    !cfg80211_chandef_dfs_required(wiphy, &params->chandef,
1794 					   priv->bss_mode)) {
1795 		nxpwifi_dbg(priv->adapter, INFO,
1796 			    "Disable 11h extensions in FW\n");
1797 		ret = nxpwifi_11h_activate(priv, false);
1798 		if (ret) {
1799 			nxpwifi_dbg(priv->adapter, ERROR,
1800 				    "Failed to disable 11h extensions!!");
1801 			goto done;
1802 		}
1803 		priv->state_11h.is_11h_active = false;
1804 	}
1805 
1806 	nxpwifi_config_uap_11d(priv, &params->beacon);
1807 
1808 	ret = nxpwifi_set_mgmt_ies(priv, &params->beacon);
1809 	if (ret)
1810 		goto done;
1811 
1812 	ret = nxpwifi_config_start_uap(priv, bss_cfg);
1813 	if (ret) {
1814 		nxpwifi_dbg(priv->adapter, ERROR,
1815 			    "Failed to start AP\n");
1816 		goto done;
1817 	}
1818 
1819 	/* First UAP records adapter-level HW channel */
1820 	if (is_first_uap) {
1821 		adapter->chandef = use_chandef;
1822 		adapter->chandef_valid = true;
1823 	}
1824 	atomic_inc(&adapter->uap_count);
1825 	netif_carrier_on(priv->netdev);
1826 	nxpwifi_wake_up_net_dev_queue(priv->netdev, priv->adapter);
1827 
1828 	memcpy(&priv->bss_cfg, bss_cfg, sizeof(priv->bss_cfg));
1829 
1830 done:
1831 	kfree(bss_cfg);
1832 	return ret;
1833 }
1834 
1835 /*
1836  * cfg80211 op: handle scan request.
1837  * Issues a firmware scan using the requested parameters and reports the
1838  * results.
1839  */
1840 static int
1841 nxpwifi_cfg80211_scan(struct wiphy *wiphy,
1842 		      struct cfg80211_scan_request *request)
1843 {
1844 	struct net_device *dev = request->wdev->netdev;
1845 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
1846 	int i, offset, ret;
1847 	struct ieee80211_channel *chan;
1848 	struct element *ie;
1849 	struct nxpwifi_user_scan_cfg *user_scan_cfg;
1850 	u8 mac_addr[ETH_ALEN];
1851 
1852 	nxpwifi_dbg(priv->adapter, CMD,
1853 		    "info: received scan request on %s\n", dev->name);
1854 
1855 	/*
1856 	 * Block scan requests during active scanning or scan cleanup.
1857 	 * Prevents new scans when the interface is disabled or teardown is in
1858 	 * progress.
1859 	 */
1860 	if (priv->scan_request || priv->scan_aborting) {
1861 		nxpwifi_dbg(priv->adapter, WARN,
1862 			    "cmd: Scan already in process..\n");
1863 		return -EBUSY;
1864 	}
1865 
1866 	if (!priv->wdev.connected && priv->scan_block)
1867 		priv->scan_block = false;
1868 
1869 	if (!nxpwifi_stop_bg_scan(priv))
1870 		cfg80211_sched_scan_stopped_locked(priv->wdev.wiphy, 0);
1871 
1872 	user_scan_cfg = kzalloc_obj(*user_scan_cfg);
1873 	if (!user_scan_cfg)
1874 		return -ENOMEM;
1875 
1876 	priv->scan_request = request;
1877 
1878 	if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1879 		get_random_mask_addr(mac_addr, request->mac_addr,
1880 				     request->mac_addr_mask);
1881 		ether_addr_copy(request->mac_addr, mac_addr);
1882 		ether_addr_copy(user_scan_cfg->random_mac, mac_addr);
1883 	}
1884 
1885 	user_scan_cfg->num_ssids = request->n_ssids;
1886 	user_scan_cfg->ssid_list = request->ssids;
1887 
1888 	if (request->ie && request->ie_len) {
1889 		offset = 0;
1890 		for (i = 0; i < NXPWIFI_MAX_VSIE_NUM; i++) {
1891 			if (priv->vs_ie[i].mask != NXPWIFI_VSIE_MASK_CLEAR)
1892 				continue;
1893 			priv->vs_ie[i].mask = NXPWIFI_VSIE_MASK_SCAN;
1894 			ie = (struct element *)(request->ie + offset);
1895 			memcpy(&priv->vs_ie[i].ie, ie,
1896 			       sizeof(*ie) + ie->datalen);
1897 			offset += sizeof(*ie) + ie->datalen;
1898 
1899 			if (offset >= request->ie_len)
1900 				break;
1901 		}
1902 	}
1903 
1904 	for (i = 0; i < min_t(u32, request->n_channels,
1905 			      NXPWIFI_USER_SCAN_CHAN_MAX); i++) {
1906 		chan = request->channels[i];
1907 		user_scan_cfg->chan_list[i].chan_number = chan->hw_value;
1908 		user_scan_cfg->chan_list[i].radio_type = chan->band;
1909 
1910 		if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
1911 			user_scan_cfg->chan_list[i].scan_type =
1912 						NXPWIFI_SCAN_TYPE_PASSIVE;
1913 		else
1914 			user_scan_cfg->chan_list[i].scan_type =
1915 						NXPWIFI_SCAN_TYPE_ACTIVE;
1916 
1917 		user_scan_cfg->chan_list[i].scan_time = 0;
1918 	}
1919 
1920 	if (priv->adapter->scan_chan_gap_enabled &&
1921 	    nxpwifi_is_any_intf_active(priv))
1922 		user_scan_cfg->scan_chan_gap =
1923 					      priv->adapter->scan_chan_gap_time;
1924 
1925 	ret = nxpwifi_scan_networks(priv, user_scan_cfg);
1926 	kfree(user_scan_cfg);
1927 	if (ret) {
1928 		nxpwifi_dbg(priv->adapter, ERROR,
1929 			    "scan failed: %d\n", ret);
1930 		priv->scan_aborting = false;
1931 		priv->scan_request = NULL;
1932 		return ret;
1933 	}
1934 
1935 	if (request->ie && request->ie_len) {
1936 		for (i = 0; i < NXPWIFI_MAX_VSIE_NUM; i++) {
1937 			if (priv->vs_ie[i].mask == NXPWIFI_VSIE_MASK_SCAN) {
1938 				priv->vs_ie[i].mask = NXPWIFI_VSIE_MASK_CLEAR;
1939 				memset(&priv->vs_ie[i].ie, 0,
1940 				       NXPWIFI_MAX_VSIE_LEN);
1941 			}
1942 		}
1943 	}
1944 	return 0;
1945 }
1946 
1947 /*
1948  * cfg80211 sched_scan_start handler.
1949  *
1950  * Send a bgscan configuration request to the firmware based on the
1951  * scheduled scan parameters. On success, the firmware later issues a
1952  * BGSCAN_REPORT event, after which the driver should query the firmware
1953  * for scan results.
1954  */
1955 static int
1956 nxpwifi_cfg80211_sched_scan_start(struct wiphy *wiphy,
1957 				  struct net_device *dev,
1958 				  struct cfg80211_sched_scan_request *request)
1959 {
1960 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
1961 	int i, offset;
1962 	struct ieee80211_channel *chan;
1963 	struct nxpwifi_bg_scan_cfg *bgscan_cfg;
1964 	struct element *ie;
1965 	int ret;
1966 
1967 	if (!request || (!request->n_ssids && !request->n_match_sets)) {
1968 		wiphy_err(wiphy, "%s : Invalid Sched_scan parameters",
1969 			  __func__);
1970 		return -EINVAL;
1971 	}
1972 
1973 	wiphy_info(wiphy, "sched_scan start : n_ssids=%d n_match_sets=%d ",
1974 		   request->n_ssids, request->n_match_sets);
1975 	wiphy_info(wiphy, "n_channels=%d interval=%d ie_len=%d\n",
1976 		   request->n_channels, request->scan_plans->interval,
1977 		   (int)request->ie_len);
1978 
1979 	bgscan_cfg = kzalloc_obj(*bgscan_cfg);
1980 	if (!bgscan_cfg)
1981 		return -ENOMEM;
1982 
1983 	if (priv->scan_request || priv->scan_aborting)
1984 		bgscan_cfg->start_later = true;
1985 
1986 	bgscan_cfg->num_ssids = request->n_match_sets;
1987 	bgscan_cfg->ssid_list = request->match_sets;
1988 
1989 	if (request->ie && request->ie_len) {
1990 		offset = 0;
1991 		for (i = 0; i < NXPWIFI_MAX_VSIE_NUM; i++) {
1992 			if (priv->vs_ie[i].mask != NXPWIFI_VSIE_MASK_CLEAR)
1993 				continue;
1994 			priv->vs_ie[i].mask = NXPWIFI_VSIE_MASK_BGSCAN;
1995 			ie = (struct element *)(request->ie + offset);
1996 			memcpy(&priv->vs_ie[i].ie, ie,
1997 			       sizeof(*ie) + ie->datalen);
1998 			offset += sizeof(*ie) + ie->datalen;
1999 
2000 			if (offset >= request->ie_len)
2001 				break;
2002 		}
2003 	}
2004 
2005 	for (i = 0; i < min_t(u32, request->n_channels,
2006 			      NXPWIFI_BG_SCAN_CHAN_MAX); i++) {
2007 		chan = request->channels[i];
2008 		bgscan_cfg->chan_list[i].chan_number = chan->hw_value;
2009 		bgscan_cfg->chan_list[i].radio_type = chan->band;
2010 
2011 		if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2012 			bgscan_cfg->chan_list[i].scan_type =
2013 						NXPWIFI_SCAN_TYPE_PASSIVE;
2014 		else
2015 			bgscan_cfg->chan_list[i].scan_type =
2016 						NXPWIFI_SCAN_TYPE_ACTIVE;
2017 
2018 		bgscan_cfg->chan_list[i].scan_time = 0;
2019 	}
2020 
2021 	bgscan_cfg->chan_per_scan = min_t(u32, request->n_channels,
2022 					  NXPWIFI_BG_SCAN_CHAN_MAX);
2023 
2024 	/* Minimum scan cycle duration: 15 seconds */
2025 	bgscan_cfg->scan_interval = (request->scan_plans->interval >
2026 				     NXPWIFI_BGSCAN_INTERVAL) ?
2027 				request->scan_plans->interval :
2028 				NXPWIFI_BGSCAN_INTERVAL;
2029 
2030 	bgscan_cfg->repeat_count = NXPWIFI_BGSCAN_REPEAT_COUNT;
2031 	bgscan_cfg->report_condition = NXPWIFI_BGSCAN_SSID_MATCH |
2032 				NXPWIFI_BGSCAN_WAIT_ALL_CHAN_DONE;
2033 	bgscan_cfg->bss_type = NXPWIFI_BSS_MODE_INFRA;
2034 	bgscan_cfg->action = NXPWIFI_BGSCAN_ACT_SET;
2035 	bgscan_cfg->enable = true;
2036 	if (request->min_rssi_thold != NL80211_SCAN_RSSI_THOLD_OFF) {
2037 		bgscan_cfg->report_condition |= NXPWIFI_BGSCAN_SSID_RSSI_MATCH;
2038 		bgscan_cfg->rssi_threshold = request->min_rssi_thold;
2039 	}
2040 
2041 	ret = nxpwifi_bg_scan_config(priv, bgscan_cfg);
2042 
2043 	if (!ret)
2044 		priv->sched_scanning = true;
2045 
2046 	kfree(bgscan_cfg);
2047 	return ret;
2048 }
2049 
2050 /*
2051  * cfg80211 sched_scan_stop handler.
2052  *
2053  * Send a bgscan configuration command to disable the previous
2054  * background scan settings in the firmware.
2055  */
2056 static int nxpwifi_cfg80211_sched_scan_stop(struct wiphy *wiphy,
2057 					    struct net_device *dev, u64 reqid)
2058 {
2059 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
2060 
2061 	wiphy_info(wiphy, "sched scan stop!");
2062 	return nxpwifi_stop_bg_scan(priv);
2063 }
2064 
2065 /*
2066  * Set default cfg80211 HT capabilities.
2067  */
2068 static void
2069 nxpwifi_setup_ht_caps(struct nxpwifi_private *priv,
2070 		      struct ieee80211_sta_ht_cap *ht_info)
2071 {
2072 	int rx_mcs_supp;
2073 	struct ieee80211_mcs_info mcs_set;
2074 	u8 *mcs = (u8 *)&mcs_set;
2075 	struct nxpwifi_adapter *adapter = priv->adapter;
2076 
2077 	ht_info->ht_supported = true;
2078 	ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2079 	ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2080 
2081 	memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
2082 
2083 	/* Fill HT capability information */
2084 	if (ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2085 		ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2086 	else
2087 		ht_info->cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2088 
2089 	if (ISSUPP_SHORTGI20(adapter->hw_dot_11n_dev_cap))
2090 		ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
2091 	else
2092 		ht_info->cap &= ~IEEE80211_HT_CAP_SGI_20;
2093 
2094 	if (ISSUPP_SHORTGI40(adapter->hw_dot_11n_dev_cap))
2095 		ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
2096 	else
2097 		ht_info->cap &= ~IEEE80211_HT_CAP_SGI_40;
2098 
2099 	if (adapter->user_dev_mcs_support == HT_STREAM_2X2)
2100 		ht_info->cap |= 2 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2101 	else
2102 		ht_info->cap |= 1 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2103 
2104 	if (ISSUPP_TXSTBC(adapter->hw_dot_11n_dev_cap))
2105 		ht_info->cap |= IEEE80211_HT_CAP_TX_STBC;
2106 	else
2107 		ht_info->cap &= ~IEEE80211_HT_CAP_TX_STBC;
2108 
2109 	if (ISSUPP_GREENFIELD(adapter->hw_dot_11n_dev_cap))
2110 		ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
2111 	else
2112 		ht_info->cap &= ~IEEE80211_HT_CAP_GRN_FLD;
2113 
2114 	if (ISENABLED_40MHZ_INTOLERANT(adapter->hw_dot_11n_dev_cap))
2115 		ht_info->cap |= IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2116 	else
2117 		ht_info->cap &= ~IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2118 
2119 	if (ISSUPP_RXLDPC(adapter->hw_dot_11n_dev_cap))
2120 		ht_info->cap |= IEEE80211_HT_CAP_LDPC_CODING;
2121 	else
2122 		ht_info->cap &= ~IEEE80211_HT_CAP_LDPC_CODING;
2123 
2124 	ht_info->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
2125 	ht_info->cap |= IEEE80211_HT_CAP_SM_PS;
2126 
2127 	rx_mcs_supp = GET_RXMCSSUPP(adapter->user_dev_mcs_support);
2128 	/* Set MCS for 1x1/2x2 */
2129 	memset(mcs, 0xff, rx_mcs_supp);
2130 	/* Clear all the other values */
2131 	memset(&mcs[rx_mcs_supp], 0,
2132 	       sizeof(struct ieee80211_mcs_info) - rx_mcs_supp);
2133 	if (priv->bss_mode == NL80211_IFTYPE_STATION ||
2134 	    ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2135 		/* Set MCS32 for infra mode or ad-hoc mode with 40MHz support */
2136 		SETHT_MCS32(mcs_set.rx_mask);
2137 
2138 	memcpy((u8 *)&ht_info->mcs, mcs, sizeof(struct ieee80211_mcs_info));
2139 
2140 	ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2141 }
2142 
2143 static void
2144 nxpwifi_setup_vht_caps(struct nxpwifi_private *priv,
2145 		       struct ieee80211_sta_vht_cap *vht_info)
2146 {
2147 	struct nxpwifi_adapter *adapter = priv->adapter;
2148 
2149 	vht_info->vht_supported = true;
2150 
2151 	vht_info->cap = adapter->hw_dot_11ac_dev_cap;
2152 	/* Update MCS support for VHT */
2153 	vht_info->vht_mcs.rx_mcs_map =
2154 		cpu_to_le16(adapter->hw_dot_11ac_mcs_support & 0xFFFF);
2155 	vht_info->vht_mcs.rx_highest = 0;
2156 	vht_info->vht_mcs.tx_mcs_map =
2157 		cpu_to_le16(adapter->hw_dot_11ac_mcs_support >> 16);
2158 	vht_info->vht_mcs.tx_highest = 0;
2159 }
2160 
2161 /*
2162  * 5 GHz HE capability masks for UAP mode.
2163  *
2164  * MAC: TWT requester/respondor, broadcast TWT, OMI control.
2165  *
2166  * PHY: 40/80 MHz width, puncturing, LDPC, NDP 4xLTF, STBC,
2167  *      Doppler, DCM, SU BF/BFe, STS, sounding dims, extended
2168  *      range, PPE present, 4xLTF 0.8us GI, Rx 1024-QAM.
2169  */
2170 #define UAP_HE_MAC_CAP0_MASK (IEEE80211_HE_MAC_CAP0_TWT_REQ | \
2171 			      IEEE80211_HE_MAC_CAP0_TWT_RES)
2172 
2173 #define UAP_HE_MAC_CAP1_MASK 0
2174 #define UAP_HE_MAC_CAP2_MASK IEEE80211_HE_MAC_CAP2_BCAST_TWT
2175 #define UAP_HE_MAC_CAP3_MASK IEEE80211_HE_MAC_CAP3_OMI_CONTROL
2176 #define UAP_HE_MAC_CAP4_MASK 0
2177 #define UAP_HE_MAC_CAP5_MASK 0
2178 
2179 #define UAP_HE_PHY_CAP0_MASK IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G
2180 #define UAP_HE_PHY_CAP1_MASK (IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD | \
2181 			      IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_80MHZ_ONLY_SECOND_20MHZ | \
2182 			      IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_80MHZ_ONLY_SECOND_40MHZ)
2183 #define UAP_HE_PHY_CAP2_MASK (IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US | \
2184 			      IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ | \
2185 			      IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ | \
2186 			      IEEE80211_HE_PHY_CAP2_DOPPLER_TX | \
2187 			      IEEE80211_HE_PHY_CAP2_DOPPLER_RX)
2188 #define UAP_HE_PHY_CAP3_MASK (IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_BPSK | \
2189 			      IEEE80211_HE_PHY_CAP3_DCM_MAX_TX_NSS_1 | \
2190 			      IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_BPSK | \
2191 			      IEEE80211_HE_PHY_CAP3_DCM_MAX_RX_NSS_1 | \
2192 			      IEEE80211_HE_PHY_CAP3_SU_BEAMFORMER)
2193 #define UAP_HE_PHY_CAP4_MASK (IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE | \
2194 			      IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_8)
2195 #define UAP_HE_PHY_CAP5_MASK IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_2
2196 #define UAP_HE_PHY_CAP6_MASK (IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE | \
2197 			      IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
2198 #define UAP_HE_PHY_CAP7_MASK (IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI | \
2199 			      IEEE80211_HE_PHY_CAP7_MAX_NC_1)
2200 #define UAP_HE_PHY_CAP8_MASK 0
2201 #define UAP_HE_PHY_CAP9_MASK IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU
2202 #define UAP_HE_PHY_CAP10_MASK 0
2203 
2204 /*
2205  * 2.4 GHz HE capability masks for UAP mode.
2206  *
2207  * MAC: HTC HE, OMI control (no UL OFDMA).
2208  * PHY: 40 MHz, LDPC, NDP 4xLTF, STBC, Doppler, DCM,
2209  *      SU BF/BFe, STS/sounding dims, extended range,
2210  *      PPE present, 4xLTF 0.8us GI, Rx 1024-QAM.
2211  */
2212 #define UAP_HE_2G_MAC_CAP0_MASK 0x00
2213 #define UAP_HE_2G_MAC_CAP1_MASK 0x00
2214 #define UAP_HE_2G_MAC_CAP2_MASK 0x00
2215 #define UAP_HE_2G_MAC_CAP3_MASK IEEE80211_HE_MAC_CAP3_OMI_CONTROL
2216 #define UAP_HE_2G_MAC_CAP4_MASK 0x00
2217 #define UAP_HE_2G_MAC_CAP5_MASK 0x00
2218 
2219 #define UAP_HE_2G_PHY_CAP0_MASK IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G
2220 #define UAP_HE_2G_PHY_CAP1_MASK IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD
2221 #define UAP_HE_2G_PHY_CAP2_MASK (IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US | \
2222 				 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ | \
2223 				 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ | \
2224 				 IEEE80211_HE_PHY_CAP2_DOPPLER_TX | \
2225 				 IEEE80211_HE_PHY_CAP2_DOPPLER_RX)
2226 #define UAP_HE_2G_PHY_CAP3_MASK (IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_BPSK | \
2227 				 IEEE80211_HE_PHY_CAP3_DCM_MAX_TX_NSS_1 | \
2228 				 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_BPSK | \
2229 				 IEEE80211_HE_PHY_CAP3_DCM_MAX_RX_NSS_1 | \
2230 				 IEEE80211_HE_PHY_CAP3_SU_BEAMFORMER)
2231 #define UAP_HE_2G_PHY_CAP4_MASK (IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE | \
2232 				 IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_8)
2233 #define UAP_HE_2G_PHY_CAP5_MASK IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_2
2234 #define UAP_HE_2G_PHY_CAP6_MASK (IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE | \
2235 				 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
2236 #define UAP_HE_2G_PHY_CAP7_MASK (IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI | \
2237 				 IEEE80211_HE_PHY_CAP7_MAX_NC_1)
2238 #define UAP_HE_2G_PHY_CAP8_MASK 0x00
2239 #define UAP_HE_2G_PHY_CAP9_MASK IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU
2240 #define UAP_HE_2G_PHY_CAP10_MASK 0x00
2241 #define HE_CAP_FIX_SIZE 22
2242 
2243 static void
2244 nxpwifi_update_11ax_ie(u8 band,
2245 		       struct nxpwifi_11ax_he_cap_cfg *he_cap_cfg)
2246 {
2247 	if (band == BAND_A) {
2248 		he_cap_cfg->cap_elem.mac_cap_info[0] &= UAP_HE_MAC_CAP0_MASK;
2249 		he_cap_cfg->cap_elem.mac_cap_info[1] &= UAP_HE_MAC_CAP1_MASK;
2250 		he_cap_cfg->cap_elem.mac_cap_info[2] &= UAP_HE_MAC_CAP2_MASK;
2251 		he_cap_cfg->cap_elem.mac_cap_info[3] &= UAP_HE_MAC_CAP3_MASK;
2252 		he_cap_cfg->cap_elem.mac_cap_info[4] &= UAP_HE_MAC_CAP4_MASK;
2253 		he_cap_cfg->cap_elem.mac_cap_info[5] &= UAP_HE_MAC_CAP5_MASK;
2254 		he_cap_cfg->cap_elem.phy_cap_info[0] &= UAP_HE_PHY_CAP0_MASK;
2255 		he_cap_cfg->cap_elem.phy_cap_info[1] &= UAP_HE_PHY_CAP1_MASK;
2256 		he_cap_cfg->cap_elem.phy_cap_info[2] &= UAP_HE_PHY_CAP2_MASK;
2257 		he_cap_cfg->cap_elem.phy_cap_info[3] &= UAP_HE_PHY_CAP3_MASK;
2258 		he_cap_cfg->cap_elem.phy_cap_info[4] &= UAP_HE_PHY_CAP4_MASK;
2259 		he_cap_cfg->cap_elem.phy_cap_info[5] &= UAP_HE_PHY_CAP5_MASK;
2260 		he_cap_cfg->cap_elem.phy_cap_info[6] &= UAP_HE_PHY_CAP6_MASK;
2261 		he_cap_cfg->cap_elem.phy_cap_info[7] &= UAP_HE_PHY_CAP7_MASK;
2262 		he_cap_cfg->cap_elem.phy_cap_info[8] &= UAP_HE_PHY_CAP8_MASK;
2263 		he_cap_cfg->cap_elem.phy_cap_info[9] &= UAP_HE_PHY_CAP9_MASK;
2264 		he_cap_cfg->cap_elem.phy_cap_info[10] &= UAP_HE_PHY_CAP10_MASK;
2265 	} else {
2266 		he_cap_cfg->cap_elem.mac_cap_info[0] &= UAP_HE_2G_MAC_CAP0_MASK;
2267 		he_cap_cfg->cap_elem.mac_cap_info[1] &= UAP_HE_2G_MAC_CAP1_MASK;
2268 		he_cap_cfg->cap_elem.mac_cap_info[2] &= UAP_HE_2G_MAC_CAP2_MASK;
2269 		he_cap_cfg->cap_elem.mac_cap_info[3] &= UAP_HE_2G_MAC_CAP3_MASK;
2270 		he_cap_cfg->cap_elem.mac_cap_info[4] &= UAP_HE_2G_MAC_CAP4_MASK;
2271 		he_cap_cfg->cap_elem.mac_cap_info[5] &= UAP_HE_2G_MAC_CAP5_MASK;
2272 		he_cap_cfg->cap_elem.phy_cap_info[0] &= UAP_HE_2G_PHY_CAP0_MASK;
2273 		he_cap_cfg->cap_elem.phy_cap_info[1] &= UAP_HE_2G_PHY_CAP1_MASK;
2274 		he_cap_cfg->cap_elem.phy_cap_info[2] &= UAP_HE_2G_PHY_CAP2_MASK;
2275 		he_cap_cfg->cap_elem.phy_cap_info[3] &= UAP_HE_2G_PHY_CAP3_MASK;
2276 		he_cap_cfg->cap_elem.phy_cap_info[4] &= UAP_HE_2G_PHY_CAP4_MASK;
2277 		he_cap_cfg->cap_elem.phy_cap_info[5] &= UAP_HE_2G_PHY_CAP5_MASK;
2278 		he_cap_cfg->cap_elem.phy_cap_info[6] &= UAP_HE_2G_PHY_CAP6_MASK;
2279 		he_cap_cfg->cap_elem.phy_cap_info[7] &= UAP_HE_2G_PHY_CAP7_MASK;
2280 		he_cap_cfg->cap_elem.phy_cap_info[8] &= UAP_HE_2G_PHY_CAP8_MASK;
2281 		he_cap_cfg->cap_elem.phy_cap_info[9] &= UAP_HE_2G_PHY_CAP9_MASK;
2282 		he_cap_cfg->cap_elem.phy_cap_info[10] &= UAP_HE_2G_PHY_CAP10_MASK;
2283 	}
2284 }
2285 
2286 static void
2287 nxpwifi_setup_he_caps(struct nxpwifi_private *priv,
2288 		      struct ieee80211_supported_band *band)
2289 {
2290 	struct nxpwifi_adapter *adapter = priv->adapter;
2291 	struct ieee80211_sband_iftype_data *iftype_data;
2292 	struct nxpwifi_11ax_he_cap_cfg he_cap_cfg;
2293 	u8 hw_he_cap_len;
2294 	u8 extra_mcs_size;
2295 	int ppe_threshold_len;
2296 
2297 	if (band->band == NL80211_BAND_5GHZ) {
2298 		hw_he_cap_len = adapter->hw_he_cap_len;
2299 		memcpy(&he_cap_cfg, adapter->hw_he_cap, hw_he_cap_len);
2300 		nxpwifi_update_11ax_ie(BAND_A, &he_cap_cfg);
2301 	} else {
2302 		hw_he_cap_len = adapter->hw_2g_he_cap_len;
2303 		memcpy(&he_cap_cfg, adapter->hw_2g_he_cap, hw_he_cap_len);
2304 		nxpwifi_update_11ax_ie(BAND_G, &he_cap_cfg);
2305 	}
2306 
2307 	if (!hw_he_cap_len)
2308 		return;
2309 
2310 	iftype_data = kmalloc_obj(*iftype_data);
2311 	if (!iftype_data)
2312 		return;
2313 	memset(iftype_data, 0, sizeof(*iftype_data));
2314 
2315 	iftype_data->types_mask =
2316 		BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP);
2317 	iftype_data->he_cap.has_he = true;
2318 
2319 	memcpy(iftype_data->he_cap.he_cap_elem.mac_cap_info,
2320 	       he_cap_cfg.cap_elem.mac_cap_info,
2321 	       sizeof(he_cap_cfg.cap_elem.mac_cap_info));
2322 	memcpy(iftype_data->he_cap.he_cap_elem.phy_cap_info,
2323 	       he_cap_cfg.cap_elem.phy_cap_info,
2324 	       sizeof(he_cap_cfg.cap_elem.phy_cap_info));
2325 	memset(&iftype_data->he_cap.he_mcs_nss_supp,
2326 	       0xff,
2327 	       sizeof(iftype_data->he_cap.he_mcs_nss_supp));
2328 	memcpy(&iftype_data->he_cap.he_mcs_nss_supp,
2329 	       he_cap_cfg.he_txrx_mcs_support,
2330 	       sizeof(he_cap_cfg.he_txrx_mcs_support));
2331 
2332 	extra_mcs_size = 0;
2333 	/* Add 160 MHz MCS/NSS if supported */
2334 	if (he_cap_cfg.cap_elem.phy_cap_info[0] & BIT(3))
2335 		extra_mcs_size += 4;
2336 	/* Add 80+80 MHz MCS/NSS if supported */
2337 	if (he_cap_cfg.cap_elem.phy_cap_info[0] & BIT(4))
2338 		extra_mcs_size += 4;
2339 	if (extra_mcs_size)
2340 		memcpy((u8 *)&iftype_data->he_cap.he_mcs_nss_supp.rx_mcs_160,
2341 		       he_cap_cfg.val, extra_mcs_size);
2342 
2343 	/* Parse PPE thresholds when present */
2344 	ppe_threshold_len = he_cap_cfg.len - HE_CAP_FIX_SIZE - extra_mcs_size;
2345 	if (he_cap_cfg.cap_elem.phy_cap_info[6] & BIT(7) && ppe_threshold_len) {
2346 		memcpy(iftype_data->he_cap.ppe_thres,
2347 		       &he_cap_cfg.val[extra_mcs_size],
2348 		       ppe_threshold_len);
2349 	} else {
2350 		iftype_data->he_cap.he_cap_elem.phy_cap_info[6] &= BIT(7);
2351 	}
2352 
2353 	_ieee80211_set_sband_iftype_data(band, iftype_data, 1);
2354 }
2355 
2356 /* create a new virtual interface with the given name and name assign type */
2357 struct wireless_dev *nxpwifi_add_virtual_intf(struct wiphy *wiphy,
2358 					      const char *name,
2359 					      unsigned char name_assign_type,
2360 					      enum nl80211_iftype type,
2361 					      struct vif_params *params)
2362 {
2363 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
2364 	struct nxpwifi_private *priv;
2365 	struct net_device *dev;
2366 	void *mdev_priv;
2367 	int ret;
2368 
2369 	if (!adapter)
2370 		return ERR_PTR(-EFAULT);
2371 
2372 	switch (type) {
2373 	case NL80211_IFTYPE_UNSPECIFIED:
2374 	case NL80211_IFTYPE_STATION:
2375 		if (adapter->curr_iface_comb.sta_intf ==
2376 		    adapter->iface_limit.sta_intf) {
2377 			nxpwifi_dbg(adapter, ERROR,
2378 				    "cannot create multiple sta ifaces\n");
2379 			return ERR_PTR(-EINVAL);
2380 		}
2381 
2382 		priv = nxpwifi_get_unused_priv_by_bss_type
2383 		       (adapter, NXPWIFI_BSS_TYPE_STA);
2384 		if (!priv) {
2385 			nxpwifi_dbg(adapter, ERROR,
2386 				    "could not get free private struct\n");
2387 			return ERR_PTR(-EFAULT);
2388 		}
2389 
2390 		priv->wdev.wiphy = wiphy;
2391 		priv->wdev.iftype = NL80211_IFTYPE_STATION;
2392 
2393 		if (type == NL80211_IFTYPE_UNSPECIFIED)
2394 			priv->bss_mode = NL80211_IFTYPE_STATION;
2395 		else
2396 			priv->bss_mode = type;
2397 
2398 		priv->bss_type = NXPWIFI_BSS_TYPE_STA;
2399 		priv->frame_type = NXPWIFI_DATA_FRAME_TYPE_ETH_II;
2400 		priv->bss_priority = 0;
2401 		priv->bss_role = NXPWIFI_BSS_ROLE_STA;
2402 
2403 		break;
2404 	case NL80211_IFTYPE_AP:
2405 		if (adapter->curr_iface_comb.uap_intf ==
2406 		    adapter->iface_limit.uap_intf) {
2407 			nxpwifi_dbg(adapter, ERROR,
2408 				    "cannot create multiple AP ifaces\n");
2409 			return ERR_PTR(-EINVAL);
2410 		}
2411 
2412 		priv = nxpwifi_get_unused_priv_by_bss_type
2413 		       (adapter, NXPWIFI_BSS_TYPE_UAP);
2414 		if (!priv) {
2415 			nxpwifi_dbg(adapter, ERROR,
2416 				    "could not get free private struct\n");
2417 			return ERR_PTR(-EFAULT);
2418 		}
2419 
2420 		priv->wdev.wiphy = wiphy;
2421 		priv->wdev.iftype = NL80211_IFTYPE_AP;
2422 
2423 		priv->bss_type = NXPWIFI_BSS_TYPE_UAP;
2424 		priv->frame_type = NXPWIFI_DATA_FRAME_TYPE_ETH_II;
2425 		priv->bss_priority = 0;
2426 		priv->bss_role = NXPWIFI_BSS_ROLE_UAP;
2427 		priv->bss_started = 0;
2428 		priv->bss_mode = type;
2429 
2430 		break;
2431 	case NL80211_IFTYPE_MONITOR:
2432 		priv = nxpwifi_get_unused_priv_by_bss_type
2433 			(adapter, NXPWIFI_BSS_TYPE_UAP);
2434 		if (!priv) {
2435 			nxpwifi_dbg(adapter, ERROR,
2436 				    "could not get free private struct\n");
2437 			return ERR_PTR(-EFAULT);
2438 		}
2439 		priv->wdev.wiphy = wiphy;
2440 		priv->wdev.iftype = NL80211_IFTYPE_MONITOR;
2441 
2442 		priv->bss_type = NXPWIFI_BSS_TYPE_UAP;
2443 		priv->frame_type = NXPWIFI_DATA_FRAME_TYPE_ETH_II;
2444 		priv->bss_priority = 0;
2445 		priv->bss_started = 0;
2446 		priv->bss_mode = type;
2447 
2448 		break;
2449 	default:
2450 		nxpwifi_dbg(adapter, ERROR, "type not supported\n");
2451 		return ERR_PTR(-EINVAL);
2452 	}
2453 
2454 	dev = alloc_netdev_mqs(sizeof(struct nxpwifi_private *), name,
2455 			       name_assign_type, ether_setup,
2456 			       IEEE80211_NUM_ACS, 1);
2457 	if (!dev) {
2458 		nxpwifi_dbg(adapter, ERROR,
2459 			    "no memory available for netdevice\n");
2460 		ret = -ENOMEM;
2461 		goto err_alloc_netdev;
2462 	}
2463 
2464 	nxpwifi_init_priv_params(priv, dev);
2465 
2466 	priv->netdev = dev;
2467 
2468 	nxpwifi_set_mac_address(priv, dev, false, NULL);
2469 
2470 	if (type != NL80211_IFTYPE_MONITOR) {
2471 		ret = nxpwifi_set_bss_mode(priv);
2472 
2473 		if (ret) {
2474 			nxpwifi_dbg(adapter, ERROR,
2475 				    "%s: err_set_bss_mode\n", __func__);
2476 			goto err_set_bss_mode;
2477 		}
2478 	}
2479 
2480 	ret = nxpwifi_sta_init_cmd(priv, false, false);
2481 	if (ret)
2482 		goto err_sta_init;
2483 
2484 	dev_net_set(dev, wiphy_net(wiphy));
2485 	dev->ieee80211_ptr = &priv->wdev;
2486 	dev->ieee80211_ptr->iftype = priv->bss_mode;
2487 	SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
2488 
2489 	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
2490 	dev->watchdog_timeo = NXPWIFI_DEFAULT_WATCHDOG_TIMEOUT;
2491 	dev->needed_headroom = NXPWIFI_MIN_DATA_HEADER_LEN;
2492 	dev->ethtool_ops = &nxpwifi_ethtool_ops;
2493 
2494 	mdev_priv = netdev_priv(dev);
2495 	*((unsigned long *)mdev_priv) = (unsigned long)priv;
2496 
2497 	if (type == NL80211_IFTYPE_MONITOR)
2498 		dev->type = ARPHRD_IEEE80211_RADIOTAP;
2499 
2500 	SET_NETDEV_DEV(dev, adapter->dev);
2501 
2502 	wiphy_work_init(&priv->reset_conn_state_work, nxpwifi_reset_conn_state_work);
2503 
2504 	wiphy_delayed_work_init(&priv->dfs_cac_work, nxpwifi_dfs_cac_work);
2505 
2506 	wiphy_delayed_work_init(&priv->dfs_chan_sw_work, nxpwifi_dfs_chan_sw_work);
2507 
2508 	/* Register network device */
2509 	if (cfg80211_register_netdevice(dev)) {
2510 		nxpwifi_dbg(adapter, ERROR, "cannot register network device\n");
2511 		ret = -EFAULT;
2512 		goto err_reg_netdev;
2513 	}
2514 
2515 	nxpwifi_dbg(adapter, INFO,
2516 		    "info: %s: NXP 802.11 Adapter\n", dev->name);
2517 
2518 #ifdef CONFIG_DEBUG_FS
2519 	nxpwifi_dev_debugfs_init(priv);
2520 #endif
2521 
2522 	update_vif_type_counter(adapter, type, 1);
2523 
2524 	return &priv->wdev;
2525 
2526 err_reg_netdev:
2527 	free_netdev(dev);
2528 	priv->netdev = NULL;
2529 err_sta_init:
2530 err_set_bss_mode:
2531 err_alloc_netdev:
2532 	memset(&priv->wdev, 0, sizeof(priv->wdev));
2533 	priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
2534 	priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
2535 	return ERR_PTR(ret);
2536 }
2537 EXPORT_SYMBOL_GPL(nxpwifi_add_virtual_intf);
2538 
2539 /* del_virtual_intf: remove the virtual interface determined by dev */
2540 int nxpwifi_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
2541 {
2542 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
2543 	struct nxpwifi_adapter *adapter = priv->adapter;
2544 	struct sk_buff *skb, *tmp;
2545 
2546 #ifdef CONFIG_DEBUG_FS
2547 	nxpwifi_dev_debugfs_remove(priv);
2548 #endif
2549 	if (priv->bss_mode == NL80211_IFTYPE_MONITOR) {
2550 		struct nxpwifi_802_11_net_monitor netmon_cfg;
2551 
2552 		memset(&netmon_cfg, 0, sizeof(struct nxpwifi_802_11_net_monitor));
2553 		nxpwifi_config_monitor_mode(priv, &netmon_cfg);
2554 	}
2555 
2556 	if (priv->sched_scanning)
2557 		priv->sched_scanning = false;
2558 
2559 	nxpwifi_stop_net_dev_queue(priv->netdev, adapter);
2560 
2561 	skb_queue_walk_safe(&priv->bypass_txq, skb, tmp) {
2562 		skb_unlink(skb, &priv->bypass_txq);
2563 		nxpwifi_write_data_complete(priv->adapter, skb, 0, -1);
2564 	}
2565 
2566 	netif_carrier_off(priv->netdev);
2567 
2568 	if (wdev->netdev->reg_state == NETREG_REGISTERED)
2569 		cfg80211_unregister_netdevice(wdev->netdev);
2570 
2571 	/* Clear the priv in adapter */
2572 	priv->netdev = NULL;
2573 
2574 	update_vif_type_counter(adapter, priv->bss_mode, -1);
2575 
2576 	priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
2577 
2578 	if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_STA ||
2579 	    GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP)
2580 		kfree(priv->hist_data);
2581 
2582 	return 0;
2583 }
2584 EXPORT_SYMBOL_GPL(nxpwifi_del_virtual_intf);
2585 
2586 static bool
2587 nxpwifi_is_pattern_supported(struct cfg80211_pkt_pattern *pat, s8 *byte_seq,
2588 			     u8 max_byte_seq)
2589 {
2590 	int j, k, valid_byte_cnt = 0;
2591 	bool dont_care_byte = false;
2592 
2593 	for (j = 0; j < DIV_ROUND_UP(pat->pattern_len, 8); j++) {
2594 		for (k = 0; k < 8; k++) {
2595 			if (pat->mask[j] & 1 << k) {
2596 				memcpy(byte_seq + valid_byte_cnt,
2597 				       &pat->pattern[j * 8 + k], 1);
2598 				valid_byte_cnt++;
2599 				if (dont_care_byte)
2600 					return false;
2601 			} else {
2602 				if (valid_byte_cnt)
2603 					dont_care_byte = true;
2604 			}
2605 
2606 			/* wildcard bytes record as the offset before the valid byte */
2607 			if (!valid_byte_cnt && !dont_care_byte)
2608 				pat->pkt_offset++;
2609 
2610 			if (valid_byte_cnt > max_byte_seq)
2611 				return false;
2612 		}
2613 	}
2614 
2615 	byte_seq[max_byte_seq] = valid_byte_cnt;
2616 
2617 	return true;
2618 }
2619 
2620 #ifdef CONFIG_PM
2621 static void nxpwifi_set_auto_arp_mef_entry(struct nxpwifi_private *priv,
2622 					   struct nxpwifi_mef_entry *mef_entry)
2623 {
2624 	int i, filt_num = 0, num_ipv4 = 0;
2625 	struct in_device *in_dev;
2626 	struct in_ifaddr *ifa;
2627 	__be32 ips[NXPWIFI_MAX_SUPPORTED_IPADDR];
2628 	struct nxpwifi_adapter *adapter = priv->adapter;
2629 
2630 	mef_entry->mode = MEF_MODE_HOST_SLEEP;
2631 	mef_entry->action = MEF_ACTION_AUTO_ARP;
2632 
2633 	/* Enable ARP offload feature */
2634 	memset(ips, 0, sizeof(ips));
2635 	for (i = 0; i < adapter->priv_num; i++) {
2636 		if (adapter->priv[i]->netdev) {
2637 			in_dev = __in_dev_get_rtnl(adapter->priv[i]->netdev);
2638 			if (!in_dev)
2639 				continue;
2640 			ifa = rtnl_dereference(in_dev->ifa_list);
2641 			if (!ifa || !ifa->ifa_local)
2642 				continue;
2643 			ips[i] = ifa->ifa_local;
2644 			num_ipv4++;
2645 		}
2646 	}
2647 
2648 	for (i = 0; i < num_ipv4; i++) {
2649 		if (!ips[i])
2650 			continue;
2651 		mef_entry->filter[filt_num].repeat = 1;
2652 		memcpy(mef_entry->filter[filt_num].byte_seq,
2653 		       (u8 *)&ips[i], sizeof(ips[i]));
2654 		mef_entry->filter[filt_num].byte_seq[NXPWIFI_MEF_MAX_BYTESEQ] =
2655 			sizeof(ips[i]);
2656 		mef_entry->filter[filt_num].offset = 46;
2657 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
2658 		if (filt_num) {
2659 			mef_entry->filter[filt_num].filt_action =
2660 				TYPE_OR;
2661 		}
2662 		filt_num++;
2663 	}
2664 
2665 	mef_entry->filter[filt_num].repeat = 1;
2666 	mef_entry->filter[filt_num].byte_seq[0] = 0x08;
2667 	mef_entry->filter[filt_num].byte_seq[1] = 0x06;
2668 	mef_entry->filter[filt_num].byte_seq[NXPWIFI_MEF_MAX_BYTESEQ] = 2;
2669 	mef_entry->filter[filt_num].offset = 20;
2670 	mef_entry->filter[filt_num].filt_type = TYPE_EQ;
2671 	mef_entry->filter[filt_num].filt_action = TYPE_AND;
2672 }
2673 
2674 static int nxpwifi_set_wowlan_mef_entry(struct nxpwifi_private *priv,
2675 					struct nxpwifi_ds_mef_cfg *mef_cfg,
2676 					struct nxpwifi_mef_entry *mef_entry,
2677 					struct cfg80211_wowlan *wowlan)
2678 {
2679 	int i, filt_num = 0, ret = 0;
2680 	bool first_pat = true;
2681 	u8 byte_seq[NXPWIFI_MEF_MAX_BYTESEQ + 1];
2682 
2683 	mef_entry->mode = MEF_MODE_HOST_SLEEP;
2684 	mef_entry->action = MEF_ACTION_ALLOW_AND_WAKEUP_HOST;
2685 
2686 	for (i = 0; i < wowlan->n_patterns; i++) {
2687 		memset(byte_seq, 0, sizeof(byte_seq));
2688 		if (!nxpwifi_is_pattern_supported
2689 		     (&wowlan->patterns[i], byte_seq,
2690 		      NXPWIFI_MEF_MAX_BYTESEQ)) {
2691 			nxpwifi_dbg(priv->adapter, ERROR,
2692 				    "Pattern not supported\n");
2693 			return -EOPNOTSUPP;
2694 		}
2695 
2696 		if (!wowlan->patterns[i].pkt_offset) {
2697 			if (is_unicast_ether_addr(byte_seq) &&
2698 			    byte_seq[NXPWIFI_MEF_MAX_BYTESEQ] == 1) {
2699 				mef_cfg->criteria |= NXPWIFI_CRITERIA_UNICAST;
2700 				continue;
2701 			} else if (is_broadcast_ether_addr(byte_seq)) {
2702 				mef_cfg->criteria |= NXPWIFI_CRITERIA_BROADCAST;
2703 				continue;
2704 			} else if ((!memcmp(byte_seq, "\x33\x33", 2) &&
2705 				    (byte_seq[NXPWIFI_MEF_MAX_BYTESEQ] == 2)) ||
2706 				   (!memcmp(byte_seq, "\x01\x00\x5e", 3) &&
2707 				    (byte_seq[NXPWIFI_MEF_MAX_BYTESEQ] == 3))) {
2708 				mef_cfg->criteria |= NXPWIFI_CRITERIA_MULTICAST;
2709 				continue;
2710 			}
2711 		}
2712 		mef_entry->filter[filt_num].repeat = 1;
2713 		mef_entry->filter[filt_num].offset =
2714 			wowlan->patterns[i].pkt_offset;
2715 		memcpy(mef_entry->filter[filt_num].byte_seq, byte_seq,
2716 		       sizeof(byte_seq));
2717 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
2718 
2719 		if (first_pat) {
2720 			first_pat = false;
2721 			nxpwifi_dbg(priv->adapter, INFO, "Wake on patterns\n");
2722 		} else {
2723 			mef_entry->filter[filt_num].filt_action = TYPE_AND;
2724 		}
2725 
2726 		filt_num++;
2727 	}
2728 
2729 	if (wowlan->magic_pkt) {
2730 		mef_cfg->criteria |= NXPWIFI_CRITERIA_UNICAST;
2731 		mef_entry->filter[filt_num].repeat = 16;
2732 		memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
2733 		       ETH_ALEN);
2734 		mef_entry->filter[filt_num].byte_seq[NXPWIFI_MEF_MAX_BYTESEQ] =
2735 			ETH_ALEN;
2736 		mef_entry->filter[filt_num].offset = 28;
2737 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
2738 		if (filt_num)
2739 			mef_entry->filter[filt_num].filt_action = TYPE_OR;
2740 
2741 		filt_num++;
2742 		mef_entry->filter[filt_num].repeat = 16;
2743 		memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
2744 		       ETH_ALEN);
2745 		mef_entry->filter[filt_num].byte_seq[NXPWIFI_MEF_MAX_BYTESEQ] =
2746 			ETH_ALEN;
2747 		mef_entry->filter[filt_num].offset = 56;
2748 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
2749 		mef_entry->filter[filt_num].filt_action = TYPE_OR;
2750 		nxpwifi_dbg(priv->adapter, INFO, "Wake on magic packet\n");
2751 	}
2752 	return ret;
2753 }
2754 
2755 static int nxpwifi_set_mef_filter(struct nxpwifi_private *priv,
2756 				  struct cfg80211_wowlan *wowlan)
2757 {
2758 	int ret = 0, num_entries = 1;
2759 	struct nxpwifi_ds_mef_cfg mef_cfg;
2760 	struct nxpwifi_mef_entry *mef_entry;
2761 
2762 	if (wowlan->n_patterns || wowlan->magic_pkt)
2763 		num_entries++;
2764 
2765 	mef_entry = kzalloc_objs(*mef_entry, num_entries);
2766 	if (!mef_entry)
2767 		return -ENOMEM;
2768 
2769 	memset(&mef_cfg, 0, sizeof(mef_cfg));
2770 	mef_cfg.criteria |= NXPWIFI_CRITERIA_BROADCAST |
2771 		NXPWIFI_CRITERIA_UNICAST;
2772 	mef_cfg.num_entries = num_entries;
2773 	mef_cfg.mef_entry = mef_entry;
2774 
2775 	nxpwifi_set_auto_arp_mef_entry(priv, &mef_entry[0]);
2776 
2777 	if (wowlan->n_patterns || wowlan->magic_pkt) {
2778 		ret = nxpwifi_set_wowlan_mef_entry(priv, &mef_cfg,
2779 						   &mef_entry[1], wowlan);
2780 		if (ret)
2781 			goto done;
2782 	}
2783 
2784 	if (!mef_cfg.criteria)
2785 		mef_cfg.criteria = NXPWIFI_CRITERIA_BROADCAST |
2786 			NXPWIFI_CRITERIA_UNICAST |
2787 			NXPWIFI_CRITERIA_MULTICAST;
2788 
2789 	ret = nxpwifi_mef_cfg(priv, &mef_cfg);
2790 
2791 done:
2792 	kfree(mef_entry);
2793 	return ret;
2794 }
2795 
2796 static int nxpwifi_cfg80211_suspend(struct wiphy *wiphy,
2797 				    struct cfg80211_wowlan *wowlan)
2798 {
2799 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
2800 	struct nxpwifi_ds_hs_cfg hs_cfg;
2801 	int i, ret = 0, retry_num = 10;
2802 	struct nxpwifi_private *priv;
2803 	struct nxpwifi_private *sta_priv =
2804 			nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_STA);
2805 
2806 	adapter->wowlan_enabled = false;
2807 
2808 	sta_priv->scan_aborting = true;
2809 	for (i = 0; i < adapter->priv_num; i++) {
2810 		priv = adapter->priv[i];
2811 		nxpwifi_abort_cac(priv);
2812 	}
2813 
2814 	nxpwifi_cancel_all_pending_cmd(adapter);
2815 
2816 	for (i = 0; i < adapter->priv_num; i++) {
2817 		priv = adapter->priv[i];
2818 		if (priv->netdev)
2819 			netif_device_detach(priv->netdev);
2820 	}
2821 
2822 	for (i = 0; i < retry_num; i++) {
2823 		if (!nxpwifi_wmm_lists_empty(adapter) ||
2824 		    !nxpwifi_bypass_txlist_empty(adapter) ||
2825 		    !skb_queue_empty(&adapter->tx_data_q))
2826 			usleep_range(10000, 15000);
2827 		else
2828 			break;
2829 	}
2830 
2831 	if (!wowlan) {
2832 		nxpwifi_dbg(adapter, INFO,
2833 			    "None of the WOWLAN triggers enabled\n");
2834 		ret = 0;
2835 		goto done;
2836 	}
2837 
2838 	if (!sta_priv->media_connected && !wowlan->nd_config) {
2839 		nxpwifi_dbg(adapter, ERROR,
2840 			    "Can not configure WOWLAN in disconnected state\n");
2841 		ret = 0;
2842 		goto done;
2843 	}
2844 
2845 	ret = nxpwifi_set_mef_filter(sta_priv, wowlan);
2846 	if (ret) {
2847 		nxpwifi_dbg(adapter, ERROR, "Failed to set MEF filter\n");
2848 		goto done;
2849 	}
2850 
2851 	memset(&hs_cfg, 0, sizeof(hs_cfg));
2852 	hs_cfg.conditions = le32_to_cpu(adapter->hs_cfg.conditions);
2853 
2854 	if (wowlan->nd_config) {
2855 		nxpwifi_dbg(adapter, INFO, "Wake on net detect\n");
2856 		hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
2857 		nxpwifi_cfg80211_sched_scan_start(wiphy, sta_priv->netdev,
2858 						  wowlan->nd_config);
2859 	}
2860 
2861 	if (wowlan->disconnect) {
2862 		hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
2863 		nxpwifi_dbg(sta_priv->adapter, INFO, "Wake on device disconnect\n");
2864 	}
2865 
2866 	hs_cfg.is_invoke_hostcmd = false;
2867 	hs_cfg.gpio = adapter->hs_cfg.gpio;
2868 	hs_cfg.gap = adapter->hs_cfg.gap;
2869 	ret = nxpwifi_set_hs_params(sta_priv, HOST_ACT_GEN_SET,
2870 				    NXPWIFI_SYNC_CMD, &hs_cfg);
2871 	if (ret)
2872 		nxpwifi_dbg(adapter, ERROR, "Failed to set HS params\n");
2873 
2874 	adapter->wowlan_enabled = true;
2875 
2876 done:
2877 	sta_priv->scan_aborting = false;
2878 	return ret;
2879 }
2880 
2881 static int nxpwifi_cfg80211_resume(struct wiphy *wiphy)
2882 {
2883 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
2884 	struct nxpwifi_private *priv;
2885 	struct nxpwifi_ds_wakeup_reason wakeup_reason;
2886 	struct cfg80211_wowlan_wakeup wakeup_report;
2887 	int i;
2888 	bool report_wakeup_reason = true;
2889 
2890 	for (i = 0; i < adapter->priv_num; i++) {
2891 		priv = adapter->priv[i];
2892 		if (priv->netdev)
2893 			netif_device_attach(priv->netdev);
2894 	}
2895 
2896 	if (!wiphy->wowlan_config)
2897 		goto done;
2898 
2899 	priv = nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_STA);
2900 	nxpwifi_get_wakeup_reason(priv, HOST_ACT_GEN_GET, NXPWIFI_SYNC_CMD,
2901 				  &wakeup_reason);
2902 	memset(&wakeup_report, 0, sizeof(struct cfg80211_wowlan_wakeup));
2903 
2904 	wakeup_report.pattern_idx = -1;
2905 
2906 	switch (wakeup_reason.hs_wakeup_reason) {
2907 	case NO_HSWAKEUP_REASON:
2908 		break;
2909 	case BCAST_DATA_MATCHED:
2910 		break;
2911 	case MCAST_DATA_MATCHED:
2912 		break;
2913 	case UCAST_DATA_MATCHED:
2914 		break;
2915 	case MASKTABLE_EVENT_MATCHED:
2916 		break;
2917 	case NON_MASKABLE_EVENT_MATCHED:
2918 		if (wiphy->wowlan_config->disconnect)
2919 			wakeup_report.disconnect = true;
2920 		if (wiphy->wowlan_config->nd_config)
2921 			wakeup_report.net_detect = adapter->nd_info;
2922 		break;
2923 	case NON_MASKABLE_CONDITION_MATCHED:
2924 		break;
2925 	case MAGIC_PATTERN_MATCHED:
2926 		if (wiphy->wowlan_config->magic_pkt)
2927 			wakeup_report.magic_pkt = true;
2928 		if (wiphy->wowlan_config->n_patterns)
2929 			wakeup_report.pattern_idx = 1;
2930 		break;
2931 	case GTK_REKEY_FAILURE:
2932 		if (wiphy->wowlan_config->gtk_rekey_failure)
2933 			wakeup_report.gtk_rekey_failure = true;
2934 		break;
2935 	default:
2936 		report_wakeup_reason = false;
2937 		break;
2938 	}
2939 
2940 	if (report_wakeup_reason)
2941 		cfg80211_report_wowlan_wakeup(&priv->wdev, &wakeup_report,
2942 					      GFP_KERNEL);
2943 
2944 done:
2945 	if (adapter->nd_info) {
2946 		for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
2947 			kfree(adapter->nd_info->matches[i]);
2948 		kfree(adapter->nd_info);
2949 		adapter->nd_info = NULL;
2950 	}
2951 
2952 	return 0;
2953 }
2954 
2955 static void nxpwifi_cfg80211_set_wakeup(struct wiphy *wiphy,
2956 					bool enabled)
2957 {
2958 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
2959 
2960 	device_set_wakeup_enable(adapter->dev, enabled);
2961 }
2962 #endif
2963 
2964 static int nxpwifi_get_coalesce_pkt_type(u8 *byte_seq)
2965 {
2966 	if ((byte_seq[0] & 0x01) &&
2967 	    byte_seq[NXPWIFI_COALESCE_MAX_BYTESEQ] == 1)
2968 		return PACKET_TYPE_UNICAST;
2969 	else if (is_broadcast_ether_addr(byte_seq))
2970 		return PACKET_TYPE_BROADCAST;
2971 	else if ((!memcmp(byte_seq, "\x33\x33", 2) &&
2972 		  byte_seq[NXPWIFI_COALESCE_MAX_BYTESEQ] == 2) ||
2973 		 (!memcmp(byte_seq, "\x01\x00\x5e", 3) &&
2974 		  byte_seq[NXPWIFI_COALESCE_MAX_BYTESEQ] == 3))
2975 		return PACKET_TYPE_MULTICAST;
2976 
2977 	return 0;
2978 }
2979 
2980 static int
2981 nxpwifi_fill_coalesce_rule_info(struct nxpwifi_private *priv,
2982 				struct cfg80211_coalesce_rules *crule,
2983 				struct nxpwifi_coalesce_rule *mrule)
2984 {
2985 	u8 byte_seq[NXPWIFI_COALESCE_MAX_BYTESEQ + 1];
2986 	struct filt_field_param *param;
2987 	int i;
2988 
2989 	mrule->max_coalescing_delay = crule->delay;
2990 
2991 	param = mrule->params;
2992 
2993 	for (i = 0; i < crule->n_patterns; i++) {
2994 		memset(byte_seq, 0, sizeof(byte_seq));
2995 		if (!nxpwifi_is_pattern_supported(&crule->patterns[i],
2996 						  byte_seq,
2997 						NXPWIFI_COALESCE_MAX_BYTESEQ)) {
2998 			nxpwifi_dbg(priv->adapter, ERROR,
2999 				    "Pattern not supported\n");
3000 			return -EOPNOTSUPP;
3001 		}
3002 
3003 		if (!crule->patterns[i].pkt_offset) {
3004 			u8 pkt_type;
3005 
3006 			pkt_type = nxpwifi_get_coalesce_pkt_type(byte_seq);
3007 			if (pkt_type && mrule->pkt_type) {
3008 				nxpwifi_dbg(priv->adapter, ERROR,
3009 					    "Multiple packet types not allowed\n");
3010 				return -EOPNOTSUPP;
3011 			} else if (pkt_type) {
3012 				mrule->pkt_type = pkt_type;
3013 				continue;
3014 			}
3015 		}
3016 
3017 		if (crule->condition == NL80211_COALESCE_CONDITION_MATCH)
3018 			param->operation = RECV_FILTER_MATCH_TYPE_EQ;
3019 		else
3020 			param->operation = RECV_FILTER_MATCH_TYPE_NE;
3021 
3022 		param->operand_len = byte_seq[NXPWIFI_COALESCE_MAX_BYTESEQ];
3023 		memcpy(param->operand_byte_stream, byte_seq,
3024 		       param->operand_len);
3025 		param->offset = crule->patterns[i].pkt_offset;
3026 		param++;
3027 
3028 		mrule->num_of_fields++;
3029 	}
3030 
3031 	if (!mrule->pkt_type) {
3032 		nxpwifi_dbg(priv->adapter, ERROR,
3033 			    "Packet type can not be determined\n");
3034 		return -EOPNOTSUPP;
3035 	}
3036 
3037 	return 0;
3038 }
3039 
3040 static int nxpwifi_cfg80211_set_coalesce(struct wiphy *wiphy,
3041 					 struct cfg80211_coalesce *coalesce)
3042 {
3043 	struct nxpwifi_adapter *adapter = nxpwifi_cfg80211_get_adapter(wiphy);
3044 	int i, ret;
3045 	struct nxpwifi_ds_coalesce_cfg coalesce_cfg;
3046 	struct nxpwifi_private *priv =
3047 			nxpwifi_get_priv(adapter, NXPWIFI_BSS_ROLE_STA);
3048 
3049 	memset(&coalesce_cfg, 0, sizeof(coalesce_cfg));
3050 
3051 	if (!coalesce)
3052 		return nxpwifi_coalesce_cfg(priv, &coalesce_cfg);
3053 
3054 	coalesce_cfg.num_of_rules = coalesce->n_rules;
3055 	for (i = 0; i < coalesce->n_rules; i++) {
3056 		ret = nxpwifi_fill_coalesce_rule_info(priv, &coalesce->rules[i],
3057 						      &coalesce_cfg.rule[i]);
3058 		if (ret) {
3059 			nxpwifi_dbg(adapter, ERROR,
3060 				    "Recheck the patterns provided for rule %d\n",
3061 				    i + 1);
3062 			return ret;
3063 		}
3064 	}
3065 
3066 	return nxpwifi_coalesce_cfg(priv, &coalesce_cfg);
3067 }
3068 
3069 static int
3070 nxpwifi_cfg80211_uap_add_station(struct nxpwifi_private *priv, const u8 *mac,
3071 				 struct station_parameters *params)
3072 {
3073 	struct nxpwifi_sta_info add_sta;
3074 	int ret;
3075 
3076 	memcpy(add_sta.peer_mac, mac, ETH_ALEN);
3077 	add_sta.params = params;
3078 
3079 	ret = nxpwifi_add_new_station(priv, &add_sta);
3080 
3081 	return ret;
3082 }
3083 
3084 static int
3085 nxpwifi_cfg80211_add_station(struct wiphy *wiphy, struct wireless_dev *wdev,
3086 			     const u8 *mac, struct station_parameters *params)
3087 {
3088 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
3089 	int ret = -EOPNOTSUPP;
3090 
3091 	if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP)
3092 		ret = nxpwifi_cfg80211_uap_add_station(priv, mac, params);
3093 
3094 	return ret;
3095 }
3096 
3097 static int
3098 nxpwifi_cfg80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
3099 				struct cfg80211_csa_settings *params)
3100 {
3101 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
3102 	int chsw_msec;
3103 	int ret;
3104 
3105 	if (priv->adapter->scan_processing) {
3106 		nxpwifi_dbg(priv->adapter, ERROR,
3107 			    "radar detection: scan in process...\n");
3108 		return -EBUSY;
3109 	}
3110 
3111 	if (priv->wdev.links[0].cac_started)
3112 		return -EBUSY;
3113 
3114 	if (cfg80211_chandef_identical(&params->chandef,
3115 				       &priv->dfs_chandef))
3116 		return -EINVAL;
3117 
3118 	if (params->block_tx) {
3119 		netif_carrier_off(priv->netdev);
3120 		nxpwifi_stop_net_dev_queue(priv->netdev, priv->adapter);
3121 		priv->uap_stop_tx = true;
3122 	}
3123 
3124 	ret = nxpwifi_del_mgmt_ies(priv);
3125 	if (ret)
3126 		nxpwifi_dbg(priv->adapter, ERROR,
3127 			    "Failed to delete mgmt IEs!\n");
3128 
3129 	ret = nxpwifi_set_mgmt_ies(priv, &params->beacon_csa);
3130 	if (ret) {
3131 		nxpwifi_dbg(priv->adapter, ERROR,
3132 			    "%s: setting mgmt ies failed\n", __func__);
3133 		goto done;
3134 	}
3135 
3136 	memcpy(&priv->dfs_chandef, &params->chandef, sizeof(priv->dfs_chandef));
3137 	memcpy(&priv->ap_update_info.beacon, &params->beacon_after,
3138 	       sizeof(priv->ap_update_info.beacon));
3139 
3140 	chsw_msec = max(params->count * priv->bss_cfg.beacon_period, 100);
3141 
3142 	nxpwifi_queue_delayed_wiphy_work(priv->adapter,
3143 					 &priv->dfs_chan_sw_work,
3144 					 msecs_to_jiffies(chsw_msec));
3145 
3146 done:
3147 	return ret;
3148 }
3149 
3150 static int nxpwifi_cfg80211_get_channel(struct wiphy *wiphy,
3151 					struct wireless_dev *wdev,
3152 					unsigned int link_id,
3153 					struct cfg80211_chan_def *chandef)
3154 {
3155 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
3156 	struct nxpwifi_bssdescriptor *curr_bss;
3157 	struct ieee80211_channel *chan;
3158 	enum nl80211_channel_type chan_type;
3159 	enum nl80211_band band;
3160 	int freq;
3161 	int ret = -ENODATA;
3162 
3163 	if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP &&
3164 	    cfg80211_chandef_valid(&priv->bss_chandef)) {
3165 		*chandef = priv->bss_chandef;
3166 		ret = 0;
3167 	} else if (priv->media_connected) {
3168 		curr_bss = &priv->curr_bss_params.bss_descriptor;
3169 		band = nxpwifi_band_to_radio_type(priv->curr_bss_params.band);
3170 		freq = ieee80211_channel_to_frequency(curr_bss->channel, band);
3171 		chan = ieee80211_get_channel(wiphy, freq);
3172 
3173 		if (priv->ht_param_present) {
3174 			chan_type = nxpwifi_get_chan_type(priv);
3175 			cfg80211_chandef_create(chandef, chan, chan_type);
3176 		} else {
3177 			cfg80211_chandef_create(chandef, chan,
3178 						NL80211_CHAN_NO_HT);
3179 		}
3180 		ret = 0;
3181 	}
3182 
3183 	return ret;
3184 }
3185 
3186 #ifdef CONFIG_NL80211_TESTMODE
3187 
3188 enum nxpwifi_tm_attr {
3189 	__NXPWIFI_TM_ATTR_INVALID	= 0,
3190 	NXPWIFI_TM_ATTR_CMD		= 1,
3191 	NXPWIFI_TM_ATTR_DATA		= 2,
3192 
3193 	/* keep last */
3194 	__NXPWIFI_TM_ATTR_AFTER_LAST,
3195 	NXPWIFI_TM_ATTR_MAX		= __NXPWIFI_TM_ATTR_AFTER_LAST - 1,
3196 };
3197 
3198 static const struct nla_policy nxpwifi_tm_policy[NXPWIFI_TM_ATTR_MAX + 1] = {
3199 	[NXPWIFI_TM_ATTR_CMD]		= { .type = NLA_U32 },
3200 	[NXPWIFI_TM_ATTR_DATA]		= { .type = NLA_BINARY,
3201 					    .len = NXPWIFI_SIZE_OF_CMD_BUFFER },
3202 };
3203 
3204 enum nxpwifi_tm_command {
3205 	NXPWIFI_TM_CMD_HOSTCMD	= 0,
3206 };
3207 
3208 static int nxpwifi_tm_cmd(struct wiphy *wiphy, struct wireless_dev *wdev,
3209 			  void *data, int len)
3210 {
3211 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(wdev->netdev);
3212 	struct nxpwifi_ds_misc_cmd *hostcmd;
3213 	struct nlattr *tb[NXPWIFI_TM_ATTR_MAX + 1];
3214 	struct sk_buff *skb;
3215 	int err;
3216 
3217 	if (!priv)
3218 		return -EINVAL;
3219 
3220 	err = nla_parse_deprecated(tb, NXPWIFI_TM_ATTR_MAX, data, len,
3221 				   nxpwifi_tm_policy, NULL);
3222 	if (err)
3223 		return err;
3224 
3225 	if (!tb[NXPWIFI_TM_ATTR_CMD])
3226 		return -EINVAL;
3227 
3228 	switch (nla_get_u32(tb[NXPWIFI_TM_ATTR_CMD])) {
3229 	case NXPWIFI_TM_CMD_HOSTCMD:
3230 		if (!tb[NXPWIFI_TM_ATTR_DATA])
3231 			return -EINVAL;
3232 
3233 		hostcmd = kzalloc_obj(*hostcmd);
3234 		if (!hostcmd)
3235 			return -ENOMEM;
3236 
3237 		hostcmd->len = nla_len(tb[NXPWIFI_TM_ATTR_DATA]);
3238 		memcpy(hostcmd->cmd, nla_data(tb[NXPWIFI_TM_ATTR_DATA]),
3239 		       hostcmd->len);
3240 
3241 		if (nxpwifi_hostcmd(priv, hostcmd)) {
3242 			nxpwifi_dbg(priv->adapter, ERROR, "Failed to process hostcmd\n");
3243 			kfree(hostcmd);
3244 			return -EFAULT;
3245 		}
3246 
3247 		/* process hostcmd response*/
3248 		skb = cfg80211_testmode_alloc_reply_skb(wiphy, hostcmd->len);
3249 		if (!skb) {
3250 			kfree(hostcmd);
3251 			return -ENOMEM;
3252 		}
3253 		err = nla_put(skb, NXPWIFI_TM_ATTR_DATA,
3254 			      hostcmd->len, hostcmd->cmd);
3255 		if (err) {
3256 			kfree(hostcmd);
3257 			kfree_skb(skb);
3258 			return -EMSGSIZE;
3259 		}
3260 
3261 		err = cfg80211_testmode_reply(skb);
3262 		kfree(hostcmd);
3263 		return err;
3264 	default:
3265 		return -EOPNOTSUPP;
3266 	}
3267 }
3268 #endif
3269 
3270 static int
3271 nxpwifi_cfg80211_start_radar_detection(struct wiphy *wiphy,
3272 				       struct net_device *dev,
3273 				       struct cfg80211_chan_def *chandef,
3274 				       u32 cac_time_ms, int link_id)
3275 {
3276 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
3277 	struct nxpwifi_radar_params radar_params;
3278 	int ret;
3279 
3280 	if (priv->adapter->scan_processing) {
3281 		nxpwifi_dbg(priv->adapter, ERROR,
3282 			    "radar detection: scan already in process...\n");
3283 		return -EBUSY;
3284 	}
3285 
3286 	if (!nxpwifi_is_11h_active(priv)) {
3287 		nxpwifi_dbg(priv->adapter, INFO,
3288 			    "Enable 11h extensions in FW\n");
3289 		if (nxpwifi_11h_activate(priv, true)) {
3290 			nxpwifi_dbg(priv->adapter, ERROR,
3291 				    "Failed to activate 11h extensions!!");
3292 			return -EPERM;
3293 		}
3294 		priv->state_11h.is_11h_active = true;
3295 	}
3296 
3297 	memset(&radar_params, 0, sizeof(struct nxpwifi_radar_params));
3298 	radar_params.chandef = chandef;
3299 	radar_params.cac_time_ms = cac_time_ms;
3300 
3301 	memcpy(&priv->dfs_chandef, chandef, sizeof(priv->dfs_chandef));
3302 
3303 	ret = nxpwifi_chan_report_request(priv, &radar_params);
3304 	if (!ret)
3305 		nxpwifi_queue_delayed_wiphy_work(priv->adapter,
3306 						 &priv->dfs_cac_work,
3307 						 msecs_to_jiffies(cac_time_ms));
3308 
3309 	return ret;
3310 }
3311 
3312 static int
3313 nxpwifi_cfg80211_change_station(struct wiphy *wiphy, struct wireless_dev *wdev,
3314 				const u8 *mac,
3315 				struct station_parameters *params)
3316 {
3317 	return 0;
3318 }
3319 
3320 static int
3321 nxpwifi_cfg80211_authenticate(struct wiphy *wiphy,
3322 			      struct net_device *dev,
3323 			      struct cfg80211_auth_request *req)
3324 {
3325 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
3326 	struct nxpwifi_adapter *adapter = priv->adapter;
3327 	struct sk_buff *skb;
3328 	u16 pkt_len, auth_alg;
3329 	size_t frame_len;
3330 	int ret;
3331 	struct ieee80211_mgmt *mgmt;
3332 	struct nxpwifi_txinfo *tx_info;
3333 	u8 trans = 1, status_code = 0;
3334 	u8 *varptr = NULL;
3335 
3336 	if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP) {
3337 		nxpwifi_dbg(adapter, ERROR, "Interface role is AP\n");
3338 		return -EINVAL;
3339 	}
3340 
3341 	if (priv->wdev.iftype != NL80211_IFTYPE_STATION) {
3342 		nxpwifi_dbg(adapter, ERROR,
3343 			    "Interface type is not correct (type %d)\n",
3344 			    priv->wdev.iftype);
3345 		return -EINVAL;
3346 	}
3347 
3348 	if (!nxpwifi_is_channel_setting_allowable(priv, req->bss->channel))
3349 		return -EOPNOTSUPP;
3350 
3351 	if (priv->auth_alg != WLAN_AUTH_SAE &&
3352 	    (priv->auth_flag & HOST_MLME_AUTH_PENDING)) {
3353 		nxpwifi_dbg(adapter, ERROR, "Pending auth on going\n");
3354 		return -EBUSY;
3355 	}
3356 
3357 	if (!priv->host_mlme_reg) {
3358 		priv->host_mlme_reg = true;
3359 		priv->mgmt_frame_mask |= HOST_MLME_MGMT_MASK;
3360 		nxpwifi_mgmt_frame_reg(priv, priv->mgmt_frame_mask);
3361 	}
3362 
3363 	switch (req->auth_type) {
3364 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
3365 		auth_alg = WLAN_AUTH_OPEN;
3366 		break;
3367 	case NL80211_AUTHTYPE_SHARED_KEY:
3368 		auth_alg = WLAN_AUTH_SHARED_KEY;
3369 		break;
3370 	case NL80211_AUTHTYPE_FT:
3371 		auth_alg = WLAN_AUTH_FT;
3372 		break;
3373 	case NL80211_AUTHTYPE_NETWORK_EAP:
3374 		auth_alg = WLAN_AUTH_LEAP;
3375 		break;
3376 	case NL80211_AUTHTYPE_SAE:
3377 		auth_alg = WLAN_AUTH_SAE;
3378 		break;
3379 	default:
3380 		nxpwifi_dbg(adapter, ERROR,
3381 			    "unsupported auth type=%d\n", req->auth_type);
3382 		return -EOPNOTSUPP;
3383 	}
3384 
3385 	if (!(priv->auth_flag & HOST_MLME_AUTH_PENDING)) {
3386 		ret = nxpwifi_remain_on_chan_cfg(priv, HOST_ACT_GEN_SET,
3387 						 req->bss->channel,
3388 						 AUTH_TX_DEFAULT_WAIT_TIME);
3389 
3390 		if (!ret) {
3391 			priv->roc_cfg.cookie =
3392 				nxpwifi_roc_cookie(adapter);
3393 			priv->roc_cfg.chan = *req->bss->channel;
3394 		} else {
3395 			return -EPERM;
3396 		}
3397 	}
3398 
3399 	priv->sec_info.authentication_mode = auth_alg;
3400 
3401 	nxpwifi_cancel_scan(adapter);
3402 
3403 	frame_len = req->ie_len + req->auth_data_len +
3404 		sizeof(struct ieee80211_hdr_3addr) + NXPWIFI_AUTH_BODY_LEN;
3405 
3406 	if (req->auth_data_len >= 4)
3407 		frame_len -= 4;
3408 
3409 	/* nxpwifi_form_mgmt_frame() inserts address4, so the frame handed to
3410 	 * the firmware is ETH_ALEN longer than the one built here.
3411 	 */
3412 	if (frame_len > U16_MAX - ETH_ALEN) {
3413 		nxpwifi_dbg(adapter, ERROR,
3414 			    "auth frame too long: %zu bytes\n", frame_len);
3415 		return -EINVAL;
3416 	}
3417 	pkt_len = frame_len + ETH_ALEN;
3418 
3419 	mgmt = kzalloc(frame_len, GFP_KERNEL);
3420 	if (!mgmt)
3421 		return -ENOMEM;
3422 
3423 	skb = dev_alloc_skb(NXPWIFI_MIN_DATA_HEADER_LEN +
3424 			    NXPWIFI_MGMT_FRAME_HEADER_SIZE +
3425 			    pkt_len + sizeof(pkt_len));
3426 	if (!skb) {
3427 		nxpwifi_dbg(adapter, ERROR,
3428 			    "allocate skb failed for management frame\n");
3429 		kfree(mgmt);
3430 		return -ENOMEM;
3431 	}
3432 
3433 	tx_info = NXPWIFI_SKB_TXCB(skb);
3434 	memset(tx_info, 0, sizeof(*tx_info));
3435 	tx_info->bss_num = priv->bss_num;
3436 	tx_info->bss_type = priv->bss_type;
3437 	tx_info->pkt_len = pkt_len;
3438 
3439 	memcpy(mgmt->da, req->bss->bssid, ETH_ALEN);
3440 	memcpy(mgmt->sa, priv->curr_addr, ETH_ALEN);
3441 	memcpy(mgmt->bssid, req->bss->bssid, ETH_ALEN);
3442 	mgmt->frame_control =
3443 		cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_AUTH);
3444 
3445 	if (req->auth_data_len >= 4) {
3446 		if (req->auth_type == NL80211_AUTHTYPE_SAE) {
3447 			__le16 *pos = (__le16 *)req->auth_data;
3448 
3449 			trans = le16_to_cpu(pos[0]);
3450 			status_code = le16_to_cpu(pos[1]);
3451 		}
3452 		memcpy((u8 *)(&mgmt->u.auth.variable), req->auth_data + 4,
3453 		       req->auth_data_len - 4);
3454 		varptr = (u8 *)&mgmt->u.auth.variable +
3455 			 (req->auth_data_len - 4);
3456 	}
3457 
3458 	mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
3459 	mgmt->u.auth.auth_transaction = cpu_to_le16(trans);
3460 	mgmt->u.auth.status_code = cpu_to_le16(status_code);
3461 
3462 	if (req->ie && req->ie_len) {
3463 		if (!varptr)
3464 			varptr = (u8 *)&mgmt->u.auth.variable;
3465 		memcpy((u8 *)varptr, req->ie, req->ie_len);
3466 	}
3467 
3468 	nxpwifi_form_mgmt_frame(skb, (const u8 *)mgmt, frame_len);
3469 	kfree(mgmt);
3470 	priv->auth_flag = HOST_MLME_AUTH_PENDING;
3471 	priv->auth_alg = auth_alg;
3472 	skb->priority = WMM_HIGHEST_PRIORITY;
3473 	__net_timestamp(skb);
3474 
3475 	nxpwifi_dbg(adapter, MSG,
3476 		    "auth: send authentication to %pM\n", req->bss->bssid);
3477 
3478 	nxpwifi_queue_tx_pkt(priv, skb);
3479 
3480 	return 0;
3481 }
3482 
3483 static int
3484 nxpwifi_cfg80211_associate(struct wiphy *wiphy, struct net_device *dev,
3485 			   struct cfg80211_assoc_request *req)
3486 {
3487 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
3488 	struct nxpwifi_adapter *adapter = priv->adapter;
3489 	int ret;
3490 	struct cfg80211_ssid req_ssid;
3491 	const u8 *ssid_ie;
3492 
3493 	if (GET_BSS_ROLE(priv) != NXPWIFI_BSS_ROLE_STA) {
3494 		nxpwifi_dbg(adapter, ERROR,
3495 			    "%s: reject infra assoc request in non-STA role\n",
3496 			    dev->name);
3497 		return -EINVAL;
3498 	}
3499 
3500 	if (test_bit(NXPWIFI_SURPRISE_REMOVED, &adapter->work_flags) ||
3501 	    test_bit(NXPWIFI_IS_CMD_TIMEDOUT, &adapter->work_flags)) {
3502 		nxpwifi_dbg(adapter, ERROR,
3503 			    "%s: Ignore association.\t"
3504 			    "Card removed or FW in bad state\n",
3505 			    dev->name);
3506 		return -EPERM;
3507 	}
3508 
3509 	if (priv->auth_alg == WLAN_AUTH_SAE)
3510 		priv->auth_flag = HOST_MLME_AUTH_DONE;
3511 
3512 	if (priv->auth_flag && !(priv->auth_flag & HOST_MLME_AUTH_DONE))
3513 		return -EBUSY;
3514 
3515 	if (priv->roc_cfg.cookie) {
3516 		ret = nxpwifi_remain_on_chan_cfg(priv, HOST_ACT_GEN_REMOVE,
3517 						 &priv->roc_cfg.chan, 0);
3518 		if (!ret)
3519 			memset(&priv->roc_cfg, 0,
3520 			       sizeof(struct nxpwifi_roc_cfg));
3521 		else
3522 			return ret;
3523 	}
3524 
3525 	if (!nxpwifi_stop_bg_scan(priv))
3526 		cfg80211_sched_scan_stopped_locked(priv->wdev.wiphy, 0);
3527 
3528 	memset(&req_ssid, 0, sizeof(struct cfg80211_ssid));
3529 	rcu_read_lock();
3530 	ssid_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3531 
3532 	if (!ssid_ie)
3533 		goto ssid_err;
3534 
3535 	req_ssid.ssid_len = ssid_ie[1];
3536 	if (req_ssid.ssid_len > IEEE80211_MAX_SSID_LEN) {
3537 		nxpwifi_dbg(adapter, ERROR, "invalid SSID - aborting\n");
3538 		goto ssid_err;
3539 	}
3540 
3541 	memcpy(req_ssid.ssid, ssid_ie + 2, req_ssid.ssid_len);
3542 	if (!req_ssid.ssid_len || req_ssid.ssid[0] < 0x20) {
3543 		nxpwifi_dbg(adapter, ERROR, "invalid SSID - aborting\n");
3544 		goto ssid_err;
3545 	}
3546 	rcu_read_unlock();
3547 
3548 	/*
3549 	 * As this is new association, clear locally stored
3550 	 * keys and security related flags
3551 	 */
3552 	priv->sec_info.wpa_enabled = false;
3553 	priv->sec_info.wpa2_enabled = false;
3554 	priv->wep_key_curr_index = 0;
3555 	priv->sec_info.encryption_mode = 0;
3556 	priv->sec_info.is_authtype_auto = 0;
3557 	ret = nxpwifi_set_encode(priv, NULL, NULL, 0, 0, NULL, 1);
3558 
3559 	if (req->crypto.n_ciphers_pairwise)
3560 		priv->sec_info.encryption_mode =
3561 			req->crypto.ciphers_pairwise[0];
3562 
3563 	if (req->crypto.cipher_group)
3564 		priv->sec_info.encryption_mode = req->crypto.cipher_group;
3565 
3566 	if (req->ie)
3567 		ret = nxpwifi_set_gen_ie(priv, req->ie, req->ie_len);
3568 
3569 	memcpy(priv->cfg_bssid, req->bss->bssid, ETH_ALEN);
3570 
3571 	nxpwifi_dbg(adapter, MSG,
3572 		    "assoc: send association to %pM\n", req->bss->bssid);
3573 
3574 	cfg80211_ref_bss(adapter->wiphy, req->bss);
3575 
3576 	ret = nxpwifi_bss_start(priv, req->bss, &req_ssid);
3577 
3578 	if (ret) {
3579 		priv->auth_flag = 0;
3580 		priv->auth_alg = WLAN_AUTH_NONE;
3581 		eth_zero_addr(priv->cfg_bssid);
3582 	}
3583 
3584 	if (ret >= 0) {
3585 		if (priv->assoc_rsp_size) {
3586 			priv->req_bss = req->bss;
3587 			adapter->assoc_resp_received = true;
3588 			nxpwifi_queue_wiphy_work(adapter,
3589 						 &adapter->host_mlme_work);
3590 		}
3591 		ret = 0;
3592 	}
3593 
3594 	cfg80211_put_bss(priv->adapter->wiphy, req->bss);
3595 
3596 	return ret;
3597 
3598 ssid_err:
3599 
3600 	rcu_read_unlock();
3601 	return -EINVAL;
3602 }
3603 
3604 static int
3605 nxpwifi_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
3606 			    u16 reason_code)
3607 {
3608 	struct nxpwifi_private *priv = nxpwifi_netdev_get_priv(dev);
3609 	int ret;
3610 
3611 	if (!nxpwifi_stop_bg_scan(priv))
3612 		cfg80211_sched_scan_stopped_locked(priv->wdev.wiphy, 0);
3613 
3614 	ret = nxpwifi_deauthenticate(priv, NULL);
3615 	if (!ret) {
3616 		eth_zero_addr(priv->cfg_bssid);
3617 		priv->hs2_enabled = false;
3618 	}
3619 
3620 	return ret;
3621 }
3622 
3623 static int
3624 nxpwifi_cfg80211_deauthenticate(struct wiphy *wiphy,
3625 				struct net_device *dev,
3626 				struct cfg80211_deauth_request *req)
3627 {
3628 	return nxpwifi_cfg80211_disconnect(wiphy, dev, req->reason_code);
3629 }
3630 
3631 static int
3632 nxpwifi_cfg80211_disassociate(struct wiphy *wiphy,
3633 			      struct net_device *dev,
3634 			      struct cfg80211_disassoc_request *req)
3635 {
3636 	return nxpwifi_cfg80211_disconnect(wiphy, dev, req->reason_code);
3637 }
3638 
3639 static int
3640 nxpwifi_cfg80211_probe_peer(struct wiphy *wiphy,
3641 			      struct net_device *dev, const u8 *peer,
3642 			      u64 cookie)
3643 {
3644 	/*
3645 	 * hostapd looks for NL80211_CMD_PROBE_CLIENT support; otherwise,
3646 	 * it requires monitor-mode support (which mwifiex doesn't support).
3647 	 * Provide fake probe_peer support to work around this.
3648 	 */
3649 	return -EOPNOTSUPP;
3650 }
3651 
3652 /* station cfg80211 operations */
3653 static const struct cfg80211_ops nxpwifi_cfg80211_ops = {
3654 	.add_virtual_intf = nxpwifi_add_virtual_intf,
3655 	.del_virtual_intf = nxpwifi_del_virtual_intf,
3656 	.change_virtual_intf = nxpwifi_cfg80211_change_virtual_intf,
3657 	.scan = nxpwifi_cfg80211_scan,
3658 	.auth = nxpwifi_cfg80211_authenticate,
3659 	.assoc = nxpwifi_cfg80211_associate,
3660 	.deauth = nxpwifi_cfg80211_deauthenticate,
3661 	.disassoc = nxpwifi_cfg80211_disassociate,
3662 	.probe_peer = nxpwifi_cfg80211_probe_peer,
3663 	.get_station = nxpwifi_cfg80211_get_station,
3664 	.dump_station = nxpwifi_cfg80211_dump_station,
3665 	.dump_survey = nxpwifi_cfg80211_dump_survey,
3666 	.set_wiphy_params = nxpwifi_cfg80211_set_wiphy_params,
3667 	.add_key = nxpwifi_cfg80211_add_key,
3668 	.del_key = nxpwifi_cfg80211_del_key,
3669 	.set_default_mgmt_key = nxpwifi_cfg80211_set_default_mgmt_key,
3670 	.mgmt_tx = nxpwifi_cfg80211_mgmt_tx,
3671 	.update_mgmt_frame_registrations =
3672 		nxpwifi_cfg80211_update_mgmt_frame_registrations,
3673 	.remain_on_channel = nxpwifi_cfg80211_remain_on_channel,
3674 	.cancel_remain_on_channel = nxpwifi_cfg80211_cancel_remain_on_channel,
3675 	.set_default_key = nxpwifi_cfg80211_set_default_key,
3676 	.set_power_mgmt = nxpwifi_cfg80211_set_power_mgmt,
3677 	.set_tx_power = nxpwifi_cfg80211_set_tx_power,
3678 	.get_tx_power = nxpwifi_cfg80211_get_tx_power,
3679 	.set_bitrate_mask = nxpwifi_cfg80211_set_bitrate_mask,
3680 	.start_ap = nxpwifi_cfg80211_start_ap,
3681 	.stop_ap = nxpwifi_cfg80211_stop_ap,
3682 	.change_beacon = nxpwifi_cfg80211_change_beacon,
3683 	.set_cqm_rssi_config = nxpwifi_cfg80211_set_cqm_rssi_config,
3684 	.set_antenna = nxpwifi_cfg80211_set_antenna,
3685 	.get_antenna = nxpwifi_cfg80211_get_antenna,
3686 	.del_station = nxpwifi_cfg80211_del_station,
3687 	.sched_scan_start = nxpwifi_cfg80211_sched_scan_start,
3688 	.sched_scan_stop = nxpwifi_cfg80211_sched_scan_stop,
3689 	.change_station = nxpwifi_cfg80211_change_station,
3690 #ifdef CONFIG_PM
3691 	.suspend = nxpwifi_cfg80211_suspend,
3692 	.resume = nxpwifi_cfg80211_resume,
3693 	.set_wakeup = nxpwifi_cfg80211_set_wakeup,
3694 #endif
3695 	.set_coalesce = nxpwifi_cfg80211_set_coalesce,
3696 	.add_station = nxpwifi_cfg80211_add_station,
3697 	CFG80211_TESTMODE_CMD(nxpwifi_tm_cmd)
3698 	.get_channel = nxpwifi_cfg80211_get_channel,
3699 	.start_radar_detection = nxpwifi_cfg80211_start_radar_detection,
3700 	.channel_switch = nxpwifi_cfg80211_channel_switch,
3701 };
3702 
3703 #ifdef CONFIG_PM
3704 static const struct wiphy_wowlan_support nxpwifi_wowlan_support = {
3705 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
3706 		WIPHY_WOWLAN_NET_DETECT | WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
3707 		WIPHY_WOWLAN_GTK_REKEY_FAILURE,
3708 	.n_patterns = NXPWIFI_MEF_MAX_FILTERS,
3709 	.pattern_min_len = 1,
3710 	.pattern_max_len = NXPWIFI_MAX_PATTERN_LEN,
3711 	.max_pkt_offset = NXPWIFI_MAX_OFFSET_LEN,
3712 	.max_nd_match_sets = NXPWIFI_MAX_ND_MATCH_SETS,
3713 };
3714 
3715 static const struct wiphy_wowlan_support nxpwifi_wowlan_support_no_gtk = {
3716 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
3717 		 WIPHY_WOWLAN_NET_DETECT,
3718 	.n_patterns = NXPWIFI_MEF_MAX_FILTERS,
3719 	.pattern_min_len = 1,
3720 	.pattern_max_len = NXPWIFI_MAX_PATTERN_LEN,
3721 	.max_pkt_offset = NXPWIFI_MAX_OFFSET_LEN,
3722 	.max_nd_match_sets = NXPWIFI_MAX_ND_MATCH_SETS,
3723 };
3724 #endif
3725 
3726 static const struct wiphy_coalesce_support nxpwifi_coalesce_support = {
3727 	.n_rules = NXPWIFI_COALESCE_MAX_RULES,
3728 	.max_delay = NXPWIFI_MAX_COALESCING_DELAY,
3729 	.n_patterns = NXPWIFI_COALESCE_MAX_FILTERS,
3730 	.pattern_min_len = 1,
3731 	.pattern_max_len = NXPWIFI_MAX_PATTERN_LEN,
3732 	.max_pkt_offset = NXPWIFI_MAX_OFFSET_LEN,
3733 };
3734 
3735 int nxpwifi_init_channel_scan_gap(struct nxpwifi_adapter *adapter)
3736 {
3737 	u32 n_channels_bg, n_channels_a = 0;
3738 
3739 	n_channels_bg = nxpwifi_band_2ghz.n_channels;
3740 
3741 	if (adapter->fw_bands & BAND_A)
3742 		n_channels_a = nxpwifi_band_5ghz.n_channels;
3743 
3744 	/*
3745 	 * allocate twice the number total channels, since the driver issues an
3746 	 * additional active scan request for hidden SSIDs on passive channels.
3747 	 */
3748 	adapter->num_in_chan_stats = 2 * (n_channels_bg + n_channels_a);
3749 	adapter->chan_stats = kzalloc_objs(*adapter->chan_stats,
3750 					   adapter->num_in_chan_stats);
3751 
3752 	if (!adapter->chan_stats)
3753 		return -ENOMEM;
3754 
3755 	return 0;
3756 }
3757 
3758 /*
3759  * Register the device with cfg80211.
3760  *
3761  * Create and initialize the wiphy, fill in defaults and handlers,
3762  * then register it with the cfg80211 subsystem.
3763  */
3764 int nxpwifi_register_cfg80211(struct nxpwifi_adapter *adapter)
3765 {
3766 	int ret;
3767 	void *wdev_priv;
3768 	struct wiphy *wiphy;
3769 	struct nxpwifi_private *priv = adapter->priv[NXPWIFI_BSS_TYPE_STA];
3770 	struct ieee80211_sta_ht_cap *ht_cap;
3771 	struct ieee80211_sta_vht_cap *vht_cap;
3772 	u8 *country_code;
3773 	u32 thr, retry;
3774 
3775 	/* create a new wiphy for use with cfg80211 */
3776 	wiphy = wiphy_new(&nxpwifi_cfg80211_ops,
3777 			  sizeof(struct nxpwifi_adapter *));
3778 	if (!wiphy) {
3779 		nxpwifi_dbg(adapter, ERROR,
3780 			    "%s: creating new wiphy\n", __func__);
3781 		return -ENOMEM;
3782 	}
3783 
3784 	wiphy->max_scan_ssids = NXPWIFI_MAX_SSID_LIST_LENGTH;
3785 	wiphy->max_scan_ie_len = NXPWIFI_MAX_VSIE_LEN;
3786 
3787 	wiphy->mgmt_stypes = nxpwifi_mgmt_stypes;
3788 	wiphy->max_remain_on_channel_duration = 5000;
3789 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
3790 				 BIT(NL80211_IFTYPE_AP) |
3791 				 BIT(NL80211_IFTYPE_MONITOR);
3792 
3793 	wiphy->max_num_akm_suites = CFG80211_MAX_NUM_AKM_SUITES;
3794 
3795 	wiphy->bands[NL80211_BAND_2GHZ] =
3796 		devm_kmemdup(adapter->dev, &nxpwifi_band_2ghz,
3797 			     sizeof(nxpwifi_band_2ghz), GFP_KERNEL);
3798 	if (!wiphy->bands[NL80211_BAND_2GHZ]) {
3799 		ret = -ENOMEM;
3800 		goto err;
3801 	}
3802 
3803 	if (adapter->fw_bands & BAND_A) {
3804 		wiphy->bands[NL80211_BAND_5GHZ] =
3805 			devm_kmemdup(adapter->dev, &nxpwifi_band_5ghz,
3806 				     sizeof(nxpwifi_band_5ghz), GFP_KERNEL);
3807 		if (!wiphy->bands[NL80211_BAND_5GHZ]) {
3808 			ret = -ENOMEM;
3809 			goto err;
3810 		}
3811 	} else {
3812 		wiphy->bands[NL80211_BAND_5GHZ] = NULL;
3813 	}
3814 
3815 	ht_cap = &wiphy->bands[NL80211_BAND_2GHZ]->ht_cap;
3816 	nxpwifi_setup_ht_caps(priv, ht_cap);
3817 
3818 	if (adapter->is_hw_11ac_capable) {
3819 		vht_cap = &wiphy->bands[NL80211_BAND_2GHZ]->vht_cap;
3820 		nxpwifi_setup_vht_caps(priv, vht_cap);
3821 	}
3822 
3823 	if (adapter->is_hw_11ax_capable)
3824 		nxpwifi_setup_he_caps(priv, wiphy->bands[NL80211_BAND_2GHZ]);
3825 
3826 	if (adapter->fw_bands & BAND_A) {
3827 		ht_cap = &wiphy->bands[NL80211_BAND_5GHZ]->ht_cap;
3828 		nxpwifi_setup_ht_caps(priv, ht_cap);
3829 
3830 		if (adapter->is_hw_11ac_capable) {
3831 			vht_cap = &wiphy->bands[NL80211_BAND_5GHZ]->vht_cap;
3832 			nxpwifi_setup_vht_caps(priv, vht_cap);
3833 		}
3834 
3835 		if (adapter->is_hw_11ax_capable)
3836 			nxpwifi_setup_he_caps(priv, wiphy->bands[NL80211_BAND_5GHZ]);
3837 	}
3838 
3839 	if (adapter->is_hw_11ac_capable)
3840 		wiphy->iface_combinations = &nxpwifi_iface_comb_ap_sta_vht;
3841 	else
3842 		wiphy->iface_combinations = &nxpwifi_iface_comb_ap_sta;
3843 	wiphy->n_iface_combinations = 1;
3844 
3845 	wiphy->max_ap_assoc_sta = adapter->max_sta_conn;
3846 
3847 	/* Initialize cipher suits */
3848 	wiphy->cipher_suites = nxpwifi_cipher_suites;
3849 	wiphy->n_cipher_suites = ARRAY_SIZE(nxpwifi_cipher_suites);
3850 
3851 	if (adapter->regd) {
3852 		wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
3853 					   REGULATORY_DISABLE_BEACON_HINTS |
3854 					   REGULATORY_COUNTRY_IE_IGNORE;
3855 		wiphy_apply_custom_regulatory(wiphy, adapter->regd);
3856 	}
3857 
3858 	ether_addr_copy(wiphy->perm_addr, adapter->perm_addr);
3859 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
3860 	wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
3861 			WIPHY_FLAG_AP_UAPSD |
3862 			WIPHY_FLAG_REPORTS_OBSS |
3863 			WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3864 			WIPHY_FLAG_HAS_CHANNEL_SWITCH |
3865 			WIPHY_FLAG_NETNS_OK |
3866 			WIPHY_FLAG_PS_ON_BY_DEFAULT;
3867 	wiphy->max_num_csa_counters = NXPWIFI_MAX_CSA_COUNTERS;
3868 
3869 #ifdef CONFIG_PM
3870 	if (ISSUPP_FIRMWARE_SUPPLICANT(priv->adapter->fw_cap_info))
3871 		wiphy->wowlan = &nxpwifi_wowlan_support;
3872 	else
3873 		wiphy->wowlan = &nxpwifi_wowlan_support_no_gtk;
3874 #endif
3875 
3876 	wiphy->coalesce = &nxpwifi_coalesce_support;
3877 
3878 	wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
3879 				    NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2;
3880 
3881 	wiphy->max_sched_scan_reqs = 1;
3882 	wiphy->max_sched_scan_ssids = NXPWIFI_MAX_SSID_LIST_LENGTH;
3883 	wiphy->max_sched_scan_ie_len = NXPWIFI_MAX_VSIE_LEN;
3884 	wiphy->max_match_sets = NXPWIFI_MAX_SSID_LIST_LENGTH;
3885 
3886 	wiphy->available_antennas_tx = BIT(adapter->number_of_antenna) - 1;
3887 	wiphy->available_antennas_rx = BIT(adapter->number_of_antenna) - 1;
3888 
3889 	wiphy->features |= NL80211_FEATURE_SAE |
3890 			   NL80211_FEATURE_INACTIVITY_TIMER |
3891 			   NL80211_FEATURE_LOW_PRIORITY_SCAN |
3892 			   NL80211_FEATURE_NEED_OBSS_SCAN;
3893 
3894 	if (ISSUPP_RANDOM_MAC(adapter->fw_cap_info))
3895 		wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
3896 				   NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
3897 				   NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
3898 
3899 	if (adapter->fw_api_ver == NXPWIFI_FW_V15)
3900 		wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
3901 
3902 	/* Reserve space for nxpwifi specific private data for BSS */
3903 	wiphy->bss_priv_size = sizeof(struct nxpwifi_bss_priv);
3904 
3905 	wiphy->reg_notifier = nxpwifi_reg_notifier;
3906 
3907 	/* Set struct nxpwifi_adapter pointer in wiphy_priv */
3908 	wdev_priv = wiphy_priv(wiphy);
3909 	*(unsigned long *)wdev_priv = (unsigned long)adapter;
3910 
3911 	set_wiphy_dev(wiphy, priv->adapter->dev);
3912 
3913 	ret = wiphy_register(wiphy);
3914 	if (ret < 0) {
3915 		nxpwifi_dbg(adapter, ERROR,
3916 			    "%s: wiphy_register failed: %d\n", __func__, ret);
3917 		goto err;
3918 	}
3919 
3920 	if (!adapter->regd) {
3921 		if (adapter->region_code == 0x00) {
3922 			nxpwifi_dbg(adapter, WARN,
3923 				    "Ignore world regulatory domain\n");
3924 		} else {
3925 			wiphy->regulatory_flags |=
3926 				REGULATORY_DISABLE_BEACON_HINTS |
3927 				REGULATORY_COUNTRY_IE_IGNORE;
3928 			country_code =
3929 				nxpwifi_11d_code_2_region(adapter->region_code);
3930 			if (country_code &&
3931 			    regulatory_hint(wiphy, country_code))
3932 				nxpwifi_dbg(priv->adapter, ERROR,
3933 					    "regulatory_hint() failed\n");
3934 		}
3935 	}
3936 
3937 	nxpwifi_get_802_11_snmp_mib(priv, FRAG_THRESH_I, &thr);
3938 	wiphy->frag_threshold = thr;
3939 	nxpwifi_get_802_11_snmp_mib(priv, RTS_THRESH_I, &thr);
3940 	wiphy->rts_threshold = thr;
3941 	nxpwifi_get_802_11_snmp_mib(priv, SHORT_RETRY_LIM_I, &retry);
3942 	wiphy->retry_short = (u8)retry;
3943 	nxpwifi_get_802_11_snmp_mib(priv, LONG_RETRY_LIM_I, &retry);
3944 	wiphy->retry_long = (u8)retry;
3945 
3946 	adapter->wiphy = wiphy;
3947 	return ret;
3948 
3949 err:
3950 	wiphy_free(wiphy);
3951 	return ret;
3952 }
3953