1 /*
2  * NXP Wireless LAN device driver: CFG80211
3  *
4  * Copyright 2011-2020 NXP
5  *
6  * This software file (the "File") is distributed by NXP
7  * under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19 
20 #include "cfg80211.h"
21 #include "main.h"
22 #include "11n.h"
23 #include "wmm.h"
24 
25 static char *reg_alpha2;
26 module_param(reg_alpha2, charp, 0);
27 
28 static const struct ieee80211_iface_limit mwifiex_ap_sta_limits[] = {
29 	{
30 		.max = MWIFIEX_MAX_BSS_NUM,
31 		.types = BIT(NL80211_IFTYPE_STATION) |
32 				   BIT(NL80211_IFTYPE_P2P_GO) |
33 				   BIT(NL80211_IFTYPE_P2P_CLIENT) |
34 				   BIT(NL80211_IFTYPE_AP),
35 	},
36 };
37 
38 static const struct ieee80211_iface_combination
39 mwifiex_iface_comb_ap_sta = {
40 	.limits = mwifiex_ap_sta_limits,
41 	.num_different_channels = 1,
42 	.n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
43 	.max_interfaces = MWIFIEX_MAX_BSS_NUM,
44 	.beacon_int_infra_match = true,
45 	.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
46 				BIT(NL80211_CHAN_WIDTH_20) |
47 				BIT(NL80211_CHAN_WIDTH_40),
48 };
49 
50 static const struct ieee80211_iface_combination
51 mwifiex_iface_comb_ap_sta_vht = {
52 	.limits = mwifiex_ap_sta_limits,
53 	.num_different_channels = 1,
54 	.n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
55 	.max_interfaces = MWIFIEX_MAX_BSS_NUM,
56 	.beacon_int_infra_match = true,
57 	.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
58 				BIT(NL80211_CHAN_WIDTH_20) |
59 				BIT(NL80211_CHAN_WIDTH_40) |
60 				BIT(NL80211_CHAN_WIDTH_80),
61 };
62 
63 static const struct
64 ieee80211_iface_combination mwifiex_iface_comb_ap_sta_drcs = {
65 	.limits = mwifiex_ap_sta_limits,
66 	.num_different_channels = 2,
67 	.n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
68 	.max_interfaces = MWIFIEX_MAX_BSS_NUM,
69 	.beacon_int_infra_match = true,
70 };
71 
72 /*
73  * This function maps the nl802.11 channel type into driver channel type.
74  *
75  * The mapping is as follows -
76  *      NL80211_CHAN_NO_HT     -> IEEE80211_HT_PARAM_CHA_SEC_NONE
77  *      NL80211_CHAN_HT20      -> IEEE80211_HT_PARAM_CHA_SEC_NONE
78  *      NL80211_CHAN_HT40PLUS  -> IEEE80211_HT_PARAM_CHA_SEC_ABOVE
79  *      NL80211_CHAN_HT40MINUS -> IEEE80211_HT_PARAM_CHA_SEC_BELOW
80  *      Others                 -> IEEE80211_HT_PARAM_CHA_SEC_NONE
81  */
mwifiex_chan_type_to_sec_chan_offset(enum nl80211_channel_type chan_type)82 u8 mwifiex_chan_type_to_sec_chan_offset(enum nl80211_channel_type chan_type)
83 {
84 	switch (chan_type) {
85 	case NL80211_CHAN_NO_HT:
86 	case NL80211_CHAN_HT20:
87 		return IEEE80211_HT_PARAM_CHA_SEC_NONE;
88 	case NL80211_CHAN_HT40PLUS:
89 		return IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
90 	case NL80211_CHAN_HT40MINUS:
91 		return IEEE80211_HT_PARAM_CHA_SEC_BELOW;
92 	default:
93 		return IEEE80211_HT_PARAM_CHA_SEC_NONE;
94 	}
95 }
96 
97 /* This function maps IEEE HT secondary channel type to NL80211 channel type
98  */
mwifiex_get_chan_type(struct mwifiex_private * priv)99 u8 mwifiex_get_chan_type(struct mwifiex_private *priv)
100 {
101 	struct mwifiex_channel_band channel_band;
102 	int ret;
103 
104 	ret = mwifiex_get_chan_info(priv, &channel_band);
105 
106 	if (!ret) {
107 		switch (channel_band.band_config.chan_width) {
108 		case CHAN_BW_20MHZ:
109 			if (IS_11N_ENABLED(priv))
110 				return NL80211_CHAN_HT20;
111 			else
112 				return NL80211_CHAN_NO_HT;
113 		case CHAN_BW_40MHZ:
114 			if (channel_band.band_config.chan2_offset ==
115 			    SEC_CHAN_ABOVE)
116 				return NL80211_CHAN_HT40PLUS;
117 			else
118 				return NL80211_CHAN_HT40MINUS;
119 		default:
120 			return NL80211_CHAN_HT20;
121 		}
122 	}
123 
124 	return NL80211_CHAN_HT20;
125 }
126 
127 /*
128  * This function checks whether WEP is set.
129  */
130 static int
mwifiex_is_alg_wep(u32 cipher)131 mwifiex_is_alg_wep(u32 cipher)
132 {
133 	switch (cipher) {
134 	case WLAN_CIPHER_SUITE_WEP40:
135 	case WLAN_CIPHER_SUITE_WEP104:
136 		return 1;
137 	default:
138 		break;
139 	}
140 
141 	return 0;
142 }
143 
144 /*
145  * This function retrieves the private structure from kernel wiphy structure.
146  */
mwifiex_cfg80211_get_adapter(struct wiphy * wiphy)147 static void *mwifiex_cfg80211_get_adapter(struct wiphy *wiphy)
148 {
149 	return (void *) (*(unsigned long *) wiphy_priv(wiphy));
150 }
151 
152 /*
153  * CFG802.11 operation handler to delete a network key.
154  */
155 static int
mwifiex_cfg80211_del_key(struct wiphy * wiphy,struct net_device * netdev,u8 key_index,bool pairwise,const u8 * mac_addr)156 mwifiex_cfg80211_del_key(struct wiphy *wiphy, struct net_device *netdev,
157 			 u8 key_index, bool pairwise, const u8 *mac_addr)
158 {
159 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
160 	static const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
161 	const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
162 
163 	if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index, peer_mac, 1)) {
164 		mwifiex_dbg(priv->adapter, ERROR, "deleting the crypto keys\n");
165 		return -EFAULT;
166 	}
167 
168 	mwifiex_dbg(priv->adapter, INFO, "info: crypto keys deleted\n");
169 	return 0;
170 }
171 
172 /*
173  * This function forms an skb for management frame.
174  */
175 static int
mwifiex_form_mgmt_frame(struct sk_buff * skb,const u8 * buf,size_t len)176 mwifiex_form_mgmt_frame(struct sk_buff *skb, const u8 *buf, size_t len)
177 {
178 	u8 addr[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
179 	u16 pkt_len;
180 	u32 tx_control = 0, pkt_type = PKT_TYPE_MGMT;
181 
182 	pkt_len = len + ETH_ALEN;
183 
184 	skb_reserve(skb, MWIFIEX_MIN_DATA_HEADER_LEN +
185 		    MWIFIEX_MGMT_FRAME_HEADER_SIZE + sizeof(pkt_len));
186 	memcpy(skb_push(skb, sizeof(pkt_len)), &pkt_len, sizeof(pkt_len));
187 
188 	memcpy(skb_push(skb, sizeof(tx_control)),
189 	       &tx_control, sizeof(tx_control));
190 
191 	memcpy(skb_push(skb, sizeof(pkt_type)), &pkt_type, sizeof(pkt_type));
192 
193 	/* Add packet data and address4 */
194 	skb_put_data(skb, buf, sizeof(struct ieee80211_hdr_3addr));
195 	skb_put_data(skb, addr, ETH_ALEN);
196 	skb_put_data(skb, buf + sizeof(struct ieee80211_hdr_3addr),
197 		     len - sizeof(struct ieee80211_hdr_3addr));
198 
199 	skb->priority = LOW_PRIO_TID;
200 	__net_timestamp(skb);
201 
202 	return 0;
203 }
204 
205 /*
206  * CFG802.11 operation handler to transmit a management frame.
207  */
208 static int
mwifiex_cfg80211_mgmt_tx(struct wiphy * wiphy,struct wireless_dev * wdev,struct cfg80211_mgmt_tx_params * params,u64 * cookie)209 mwifiex_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
210 			 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
211 {
212 	const u8 *buf = params->buf;
213 	size_t len = params->len;
214 	struct sk_buff *skb;
215 	u16 pkt_len;
216 	const struct ieee80211_mgmt *mgmt;
217 	struct mwifiex_txinfo *tx_info;
218 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
219 
220 	if (!buf || !len) {
221 		mwifiex_dbg(priv->adapter, ERROR, "invalid buffer and length\n");
222 		return -EFAULT;
223 	}
224 
225 	mgmt = (const struct ieee80211_mgmt *)buf;
226 	if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA &&
227 	    ieee80211_is_probe_resp(mgmt->frame_control)) {
228 		/* Since we support offload probe resp, we need to skip probe
229 		 * resp in AP or GO mode */
230 		mwifiex_dbg(priv->adapter, INFO,
231 			    "info: skip to send probe resp in AP or GO mode\n");
232 		return 0;
233 	}
234 
235 	pkt_len = len + ETH_ALEN;
236 	skb = dev_alloc_skb(MWIFIEX_MIN_DATA_HEADER_LEN +
237 			    MWIFIEX_MGMT_FRAME_HEADER_SIZE +
238 			    pkt_len + sizeof(pkt_len));
239 
240 	if (!skb) {
241 		mwifiex_dbg(priv->adapter, ERROR,
242 			    "allocate skb failed for management frame\n");
243 		return -ENOMEM;
244 	}
245 
246 	tx_info = MWIFIEX_SKB_TXCB(skb);
247 	memset(tx_info, 0, sizeof(*tx_info));
248 	tx_info->bss_num = priv->bss_num;
249 	tx_info->bss_type = priv->bss_type;
250 	tx_info->pkt_len = pkt_len;
251 
252 	mwifiex_form_mgmt_frame(skb, buf, len);
253 	*cookie = prandom_u32() | 1;
254 
255 	if (ieee80211_is_action(mgmt->frame_control))
256 		skb = mwifiex_clone_skb_for_tx_status(priv,
257 						      skb,
258 				MWIFIEX_BUF_FLAG_ACTION_TX_STATUS, cookie);
259 	else
260 		cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, true,
261 					GFP_ATOMIC);
262 
263 	mwifiex_queue_tx_pkt(priv, skb);
264 
265 	mwifiex_dbg(priv->adapter, INFO, "info: management frame transmitted\n");
266 	return 0;
267 }
268 
269 /*
270  * CFG802.11 operation handler to register a mgmt frame.
271  */
272 static void
mwifiex_cfg80211_update_mgmt_frame_registrations(struct wiphy * wiphy,struct wireless_dev * wdev,struct mgmt_frame_regs * upd)273 mwifiex_cfg80211_update_mgmt_frame_registrations(struct wiphy *wiphy,
274 						 struct wireless_dev *wdev,
275 						 struct mgmt_frame_regs *upd)
276 {
277 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
278 	u32 mask = upd->interface_stypes;
279 
280 	if (mask != priv->mgmt_frame_mask) {
281 		priv->mgmt_frame_mask = mask;
282 		mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
283 				 HostCmd_ACT_GEN_SET, 0,
284 				 &priv->mgmt_frame_mask, false);
285 		mwifiex_dbg(priv->adapter, INFO, "info: mgmt frame registered\n");
286 	}
287 }
288 
289 /*
290  * CFG802.11 operation handler to remain on channel.
291  */
292 static int
mwifiex_cfg80211_remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,struct ieee80211_channel * chan,unsigned int duration,u64 * cookie)293 mwifiex_cfg80211_remain_on_channel(struct wiphy *wiphy,
294 				   struct wireless_dev *wdev,
295 				   struct ieee80211_channel *chan,
296 				   unsigned int duration, u64 *cookie)
297 {
298 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
299 	int ret;
300 
301 	if (!chan || !cookie) {
302 		mwifiex_dbg(priv->adapter, ERROR, "Invalid parameter for ROC\n");
303 		return -EINVAL;
304 	}
305 
306 	if (priv->roc_cfg.cookie) {
307 		mwifiex_dbg(priv->adapter, INFO,
308 			    "info: ongoing ROC, cookie = 0x%llx\n",
309 			    priv->roc_cfg.cookie);
310 		return -EBUSY;
311 	}
312 
313 	ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_SET, chan,
314 					 duration);
315 
316 	if (!ret) {
317 		*cookie = prandom_u32() | 1;
318 		priv->roc_cfg.cookie = *cookie;
319 		priv->roc_cfg.chan = *chan;
320 
321 		cfg80211_ready_on_channel(wdev, *cookie, chan,
322 					  duration, GFP_ATOMIC);
323 
324 		mwifiex_dbg(priv->adapter, INFO,
325 			    "info: ROC, cookie = 0x%llx\n", *cookie);
326 	}
327 
328 	return ret;
329 }
330 
331 /*
332  * CFG802.11 operation handler to cancel remain on channel.
333  */
334 static int
mwifiex_cfg80211_cancel_remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,u64 cookie)335 mwifiex_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
336 					  struct wireless_dev *wdev, u64 cookie)
337 {
338 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
339 	int ret;
340 
341 	if (cookie != priv->roc_cfg.cookie)
342 		return -ENOENT;
343 
344 	ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_REMOVE,
345 					 &priv->roc_cfg.chan, 0);
346 
347 	if (!ret) {
348 		cfg80211_remain_on_channel_expired(wdev, cookie,
349 						   &priv->roc_cfg.chan,
350 						   GFP_ATOMIC);
351 
352 		memset(&priv->roc_cfg, 0, sizeof(struct mwifiex_roc_cfg));
353 
354 		mwifiex_dbg(priv->adapter, INFO,
355 			    "info: cancel ROC, cookie = 0x%llx\n", cookie);
356 	}
357 
358 	return ret;
359 }
360 
361 /*
362  * CFG802.11 operation handler to set Tx power.
363  */
364 static int
mwifiex_cfg80211_set_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,enum nl80211_tx_power_setting type,int mbm)365 mwifiex_cfg80211_set_tx_power(struct wiphy *wiphy,
366 			      struct wireless_dev *wdev,
367 			      enum nl80211_tx_power_setting type,
368 			      int mbm)
369 {
370 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
371 	struct mwifiex_private *priv;
372 	struct mwifiex_power_cfg power_cfg;
373 	int dbm = MBM_TO_DBM(mbm);
374 
375 	switch (type) {
376 	case NL80211_TX_POWER_FIXED:
377 		power_cfg.is_power_auto = 0;
378 		power_cfg.is_power_fixed = 1;
379 		power_cfg.power_level = dbm;
380 		break;
381 	case NL80211_TX_POWER_LIMITED:
382 		power_cfg.is_power_auto = 0;
383 		power_cfg.is_power_fixed = 0;
384 		power_cfg.power_level = dbm;
385 		break;
386 	case NL80211_TX_POWER_AUTOMATIC:
387 		power_cfg.is_power_auto = 1;
388 		break;
389 	}
390 
391 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
392 
393 	return mwifiex_set_tx_power(priv, &power_cfg);
394 }
395 
396 /*
397  * CFG802.11 operation handler to get Tx power.
398  */
399 static int
mwifiex_cfg80211_get_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,int * dbm)400 mwifiex_cfg80211_get_tx_power(struct wiphy *wiphy,
401 			      struct wireless_dev *wdev,
402 			      int *dbm)
403 {
404 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
405 	struct mwifiex_private *priv = mwifiex_get_priv(adapter,
406 							MWIFIEX_BSS_ROLE_ANY);
407 	int ret = mwifiex_send_cmd(priv, HostCmd_CMD_RF_TX_PWR,
408 				   HostCmd_ACT_GEN_GET, 0, NULL, true);
409 
410 	if (ret < 0)
411 		return ret;
412 
413 	/* tx_power_level is set in HostCmd_CMD_RF_TX_PWR command handler */
414 	*dbm = priv->tx_power_level;
415 
416 	return 0;
417 }
418 
419 /*
420  * CFG802.11 operation handler to set Power Save option.
421  *
422  * The timeout value, if provided, is currently ignored.
423  */
424 static int
mwifiex_cfg80211_set_power_mgmt(struct wiphy * wiphy,struct net_device * dev,bool enabled,int timeout)425 mwifiex_cfg80211_set_power_mgmt(struct wiphy *wiphy,
426 				struct net_device *dev,
427 				bool enabled, int timeout)
428 {
429 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
430 	u32 ps_mode;
431 
432 	if (timeout)
433 		mwifiex_dbg(priv->adapter, INFO,
434 			    "info: ignore timeout value for IEEE Power Save\n");
435 
436 	ps_mode = enabled;
437 
438 	return mwifiex_drv_set_power(priv, &ps_mode);
439 }
440 
441 /*
442  * CFG802.11 operation handler to set the default network key.
443  */
444 static int
mwifiex_cfg80211_set_default_key(struct wiphy * wiphy,struct net_device * netdev,u8 key_index,bool unicast,bool multicast)445 mwifiex_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
446 				 u8 key_index, bool unicast,
447 				 bool multicast)
448 {
449 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
450 
451 	/* Return if WEP key not configured */
452 	if (!priv->sec_info.wep_enabled)
453 		return 0;
454 
455 	if (priv->bss_type == MWIFIEX_BSS_TYPE_UAP) {
456 		priv->wep_key_curr_index = key_index;
457 	} else if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index,
458 				      NULL, 0)) {
459 		mwifiex_dbg(priv->adapter, ERROR, "set default Tx key index\n");
460 		return -EFAULT;
461 	}
462 
463 	return 0;
464 }
465 
466 /*
467  * CFG802.11 operation handler to add a network key.
468  */
469 static int
mwifiex_cfg80211_add_key(struct wiphy * wiphy,struct net_device * netdev,u8 key_index,bool pairwise,const u8 * mac_addr,struct key_params * params)470 mwifiex_cfg80211_add_key(struct wiphy *wiphy, struct net_device *netdev,
471 			 u8 key_index, bool pairwise, const u8 *mac_addr,
472 			 struct key_params *params)
473 {
474 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
475 	struct mwifiex_wep_key *wep_key;
476 	static const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
477 	const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
478 
479 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
480 	    (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
481 	     params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
482 		if (params->key && params->key_len) {
483 			wep_key = &priv->wep_key[key_index];
484 			memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
485 			memcpy(wep_key->key_material, params->key,
486 			       params->key_len);
487 			wep_key->key_index = key_index;
488 			wep_key->key_length = params->key_len;
489 			priv->sec_info.wep_enabled = 1;
490 		}
491 		return 0;
492 	}
493 
494 	if (mwifiex_set_encode(priv, params, params->key, params->key_len,
495 			       key_index, peer_mac, 0)) {
496 		mwifiex_dbg(priv->adapter, ERROR, "crypto keys added\n");
497 		return -EFAULT;
498 	}
499 
500 	return 0;
501 }
502 
503 /*
504  * CFG802.11 operation handler to set default mgmt key.
505  */
506 static int
mwifiex_cfg80211_set_default_mgmt_key(struct wiphy * wiphy,struct net_device * netdev,u8 key_index)507 mwifiex_cfg80211_set_default_mgmt_key(struct wiphy *wiphy,
508 				      struct net_device *netdev,
509 				      u8 key_index)
510 {
511 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
512 	struct mwifiex_ds_encrypt_key encrypt_key;
513 
514 	wiphy_dbg(wiphy, "set default mgmt key, key index=%d\n", key_index);
515 
516 	memset(&encrypt_key, 0, sizeof(struct mwifiex_ds_encrypt_key));
517 	encrypt_key.key_len = WLAN_KEY_LEN_CCMP;
518 	encrypt_key.key_index = key_index;
519 	encrypt_key.is_igtk_def_key = true;
520 	eth_broadcast_addr(encrypt_key.mac_addr);
521 
522 	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
523 				HostCmd_ACT_GEN_SET, true, &encrypt_key, true);
524 }
525 
526 /*
527  * This function sends domain information to the firmware.
528  *
529  * The following information are passed to the firmware -
530  *      - Country codes
531  *      - Sub bands (first channel, number of channels, maximum Tx power)
532  */
mwifiex_send_domain_info_cmd_fw(struct wiphy * wiphy)533 int mwifiex_send_domain_info_cmd_fw(struct wiphy *wiphy)
534 {
535 	u8 no_of_triplet = 0;
536 	struct ieee80211_country_ie_triplet *t;
537 	u8 no_of_parsed_chan = 0;
538 	u8 first_chan = 0, next_chan = 0, max_pwr = 0;
539 	u8 i, flag = 0;
540 	enum nl80211_band band;
541 	struct ieee80211_supported_band *sband;
542 	struct ieee80211_channel *ch;
543 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
544 	struct mwifiex_private *priv;
545 	struct mwifiex_802_11d_domain_reg *domain_info = &adapter->domain_reg;
546 
547 	/* Set country code */
548 	domain_info->country_code[0] = adapter->country_code[0];
549 	domain_info->country_code[1] = adapter->country_code[1];
550 	domain_info->country_code[2] = ' ';
551 
552 	band = mwifiex_band_to_radio_type(adapter->config_bands);
553 	if (!wiphy->bands[band]) {
554 		mwifiex_dbg(adapter, ERROR,
555 			    "11D: setting domain info in FW\n");
556 		return -1;
557 	}
558 
559 	sband = wiphy->bands[band];
560 
561 	for (i = 0; i < sband->n_channels ; i++) {
562 		ch = &sband->channels[i];
563 		if (ch->flags & IEEE80211_CHAN_DISABLED)
564 			continue;
565 
566 		if (!flag) {
567 			flag = 1;
568 			first_chan = (u32) ch->hw_value;
569 			next_chan = first_chan;
570 			max_pwr = ch->max_power;
571 			no_of_parsed_chan = 1;
572 			continue;
573 		}
574 
575 		if (ch->hw_value == next_chan + 1 &&
576 		    ch->max_power == max_pwr) {
577 			next_chan++;
578 			no_of_parsed_chan++;
579 		} else {
580 			t = &domain_info->triplet[no_of_triplet];
581 			t->chans.first_channel = first_chan;
582 			t->chans.num_channels = no_of_parsed_chan;
583 			t->chans.max_power = max_pwr;
584 			no_of_triplet++;
585 			first_chan = (u32) ch->hw_value;
586 			next_chan = first_chan;
587 			max_pwr = ch->max_power;
588 			no_of_parsed_chan = 1;
589 		}
590 	}
591 
592 	if (flag) {
593 		t = &domain_info->triplet[no_of_triplet];
594 		t->chans.first_channel = first_chan;
595 		t->chans.num_channels = no_of_parsed_chan;
596 		t->chans.max_power = max_pwr;
597 		no_of_triplet++;
598 	}
599 
600 	domain_info->no_of_triplet = no_of_triplet;
601 
602 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
603 
604 	if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
605 			     HostCmd_ACT_GEN_SET, 0, NULL, false)) {
606 		mwifiex_dbg(adapter, INFO,
607 			    "11D: setting domain info in FW\n");
608 		return -1;
609 	}
610 
611 	return 0;
612 }
613 
mwifiex_reg_apply_radar_flags(struct wiphy * wiphy)614 static void mwifiex_reg_apply_radar_flags(struct wiphy *wiphy)
615 {
616 	struct ieee80211_supported_band *sband;
617 	struct ieee80211_channel *chan;
618 	unsigned int i;
619 
620 	if (!wiphy->bands[NL80211_BAND_5GHZ])
621 		return;
622 	sband = wiphy->bands[NL80211_BAND_5GHZ];
623 
624 	for (i = 0; i < sband->n_channels; i++) {
625 		chan = &sband->channels[i];
626 		if ((!(chan->flags & IEEE80211_CHAN_DISABLED)) &&
627 		    (chan->flags & IEEE80211_CHAN_RADAR))
628 			chan->flags |= IEEE80211_CHAN_NO_IR;
629 	}
630 }
631 
632 /*
633  * CFG802.11 regulatory domain callback function.
634  *
635  * This function is called when the regulatory domain is changed due to the
636  * following reasons -
637  *      - Set by driver
638  *      - Set by system core
639  *      - Set by user
640  *      - Set bt Country IE
641  */
mwifiex_reg_notifier(struct wiphy * wiphy,struct regulatory_request * request)642 static void mwifiex_reg_notifier(struct wiphy *wiphy,
643 				 struct regulatory_request *request)
644 {
645 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
646 	struct mwifiex_private *priv = mwifiex_get_priv(adapter,
647 							MWIFIEX_BSS_ROLE_ANY);
648 	mwifiex_dbg(adapter, INFO,
649 		    "info: cfg80211 regulatory domain callback for %c%c\n",
650 		    request->alpha2[0], request->alpha2[1]);
651 	mwifiex_reg_apply_radar_flags(wiphy);
652 
653 	switch (request->initiator) {
654 	case NL80211_REGDOM_SET_BY_DRIVER:
655 	case NL80211_REGDOM_SET_BY_CORE:
656 	case NL80211_REGDOM_SET_BY_USER:
657 	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
658 		break;
659 	default:
660 		mwifiex_dbg(adapter, ERROR,
661 			    "unknown regdom initiator: %d\n",
662 			    request->initiator);
663 		return;
664 	}
665 
666 	/* Don't send world or same regdom info to firmware */
667 	if (strncmp(request->alpha2, "00", 2) &&
668 	    strncmp(request->alpha2, adapter->country_code,
669 		    sizeof(request->alpha2))) {
670 		memcpy(adapter->country_code, request->alpha2,
671 		       sizeof(request->alpha2));
672 		mwifiex_send_domain_info_cmd_fw(wiphy);
673 		mwifiex_dnld_txpwr_table(priv);
674 	}
675 }
676 
677 /*
678  * This function sets the fragmentation threshold.
679  *
680  * The fragmentation threshold value must lie between MWIFIEX_FRAG_MIN_VALUE
681  * and MWIFIEX_FRAG_MAX_VALUE.
682  */
683 static int
mwifiex_set_frag(struct mwifiex_private * priv,u32 frag_thr)684 mwifiex_set_frag(struct mwifiex_private *priv, u32 frag_thr)
685 {
686 	if (frag_thr < MWIFIEX_FRAG_MIN_VALUE ||
687 	    frag_thr > MWIFIEX_FRAG_MAX_VALUE)
688 		frag_thr = MWIFIEX_FRAG_MAX_VALUE;
689 
690 	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
691 				HostCmd_ACT_GEN_SET, FRAG_THRESH_I,
692 				&frag_thr, true);
693 }
694 
695 /*
696  * This function sets the RTS threshold.
697 
698  * The rts value must lie between MWIFIEX_RTS_MIN_VALUE
699  * and MWIFIEX_RTS_MAX_VALUE.
700  */
701 static int
mwifiex_set_rts(struct mwifiex_private * priv,u32 rts_thr)702 mwifiex_set_rts(struct mwifiex_private *priv, u32 rts_thr)
703 {
704 	if (rts_thr < MWIFIEX_RTS_MIN_VALUE || rts_thr > MWIFIEX_RTS_MAX_VALUE)
705 		rts_thr = MWIFIEX_RTS_MAX_VALUE;
706 
707 	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
708 				HostCmd_ACT_GEN_SET, RTS_THRESH_I,
709 				&rts_thr, true);
710 }
711 
712 /*
713  * CFG802.11 operation handler to set wiphy parameters.
714  *
715  * This function can be used to set the RTS threshold and the
716  * Fragmentation threshold of the driver.
717  */
718 static int
mwifiex_cfg80211_set_wiphy_params(struct wiphy * wiphy,u32 changed)719 mwifiex_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
720 {
721 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
722 	struct mwifiex_private *priv;
723 	struct mwifiex_uap_bss_param *bss_cfg;
724 	int ret;
725 
726 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
727 
728 	switch (priv->bss_role) {
729 	case MWIFIEX_BSS_ROLE_UAP:
730 		if (priv->bss_started) {
731 			mwifiex_dbg(adapter, ERROR,
732 				    "cannot change wiphy params when bss started");
733 			return -EINVAL;
734 		}
735 
736 		bss_cfg = kzalloc(sizeof(*bss_cfg), GFP_KERNEL);
737 		if (!bss_cfg)
738 			return -ENOMEM;
739 
740 		mwifiex_set_sys_config_invalid_data(bss_cfg);
741 
742 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
743 			bss_cfg->rts_threshold = wiphy->rts_threshold;
744 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
745 			bss_cfg->frag_threshold = wiphy->frag_threshold;
746 		if (changed & WIPHY_PARAM_RETRY_LONG)
747 			bss_cfg->retry_limit = wiphy->retry_long;
748 
749 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_UAP_SYS_CONFIG,
750 				       HostCmd_ACT_GEN_SET,
751 				       UAP_BSS_PARAMS_I, bss_cfg,
752 				       false);
753 
754 		kfree(bss_cfg);
755 		if (ret) {
756 			mwifiex_dbg(adapter, ERROR,
757 				    "Failed to set wiphy phy params\n");
758 			return ret;
759 		}
760 		break;
761 
762 	case MWIFIEX_BSS_ROLE_STA:
763 		if (priv->media_connected) {
764 			mwifiex_dbg(adapter, ERROR,
765 				    "cannot change wiphy params when connected");
766 			return -EINVAL;
767 		}
768 		if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
769 			ret = mwifiex_set_rts(priv,
770 					      wiphy->rts_threshold);
771 			if (ret)
772 				return ret;
773 		}
774 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
775 			ret = mwifiex_set_frag(priv,
776 					       wiphy->frag_threshold);
777 			if (ret)
778 				return ret;
779 		}
780 		break;
781 	}
782 
783 	return 0;
784 }
785 
786 static int
mwifiex_cfg80211_deinit_p2p(struct mwifiex_private * priv)787 mwifiex_cfg80211_deinit_p2p(struct mwifiex_private *priv)
788 {
789 	u16 mode = P2P_MODE_DISABLE;
790 
791 	if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
792 			     HostCmd_ACT_GEN_SET, 0, &mode, true))
793 		return -1;
794 
795 	return 0;
796 }
797 
798 /*
799  * This function initializes the functionalities for P2P client.
800  * The P2P client initialization sequence is:
801  * disable -> device -> client
802  */
803 static int
mwifiex_cfg80211_init_p2p_client(struct mwifiex_private * priv)804 mwifiex_cfg80211_init_p2p_client(struct mwifiex_private *priv)
805 {
806 	u16 mode;
807 
808 	if (mwifiex_cfg80211_deinit_p2p(priv))
809 		return -1;
810 
811 	mode = P2P_MODE_DEVICE;
812 	if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
813 			     HostCmd_ACT_GEN_SET, 0, &mode, true))
814 		return -1;
815 
816 	mode = P2P_MODE_CLIENT;
817 	if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
818 			     HostCmd_ACT_GEN_SET, 0, &mode, true))
819 		return -1;
820 
821 	return 0;
822 }
823 
824 /*
825  * This function initializes the functionalities for P2P GO.
826  * The P2P GO initialization sequence is:
827  * disable -> device -> GO
828  */
829 static int
mwifiex_cfg80211_init_p2p_go(struct mwifiex_private * priv)830 mwifiex_cfg80211_init_p2p_go(struct mwifiex_private *priv)
831 {
832 	u16 mode;
833 
834 	if (mwifiex_cfg80211_deinit_p2p(priv))
835 		return -1;
836 
837 	mode = P2P_MODE_DEVICE;
838 	if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
839 			     HostCmd_ACT_GEN_SET, 0, &mode, true))
840 		return -1;
841 
842 	mode = P2P_MODE_GO;
843 	if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
844 			     HostCmd_ACT_GEN_SET, 0, &mode, true))
845 		return -1;
846 
847 	return 0;
848 }
849 
mwifiex_deinit_priv_params(struct mwifiex_private * priv)850 static int mwifiex_deinit_priv_params(struct mwifiex_private *priv)
851 {
852 	struct mwifiex_adapter *adapter = priv->adapter;
853 	unsigned long flags;
854 
855 	priv->mgmt_frame_mask = 0;
856 	if (mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
857 			     HostCmd_ACT_GEN_SET, 0,
858 			     &priv->mgmt_frame_mask, false)) {
859 		mwifiex_dbg(adapter, ERROR,
860 			    "could not unregister mgmt frame rx\n");
861 		return -1;
862 	}
863 
864 	mwifiex_deauthenticate(priv, NULL);
865 
866 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
867 	adapter->main_locked = true;
868 	if (adapter->mwifiex_processing) {
869 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
870 		flush_workqueue(adapter->workqueue);
871 	} else {
872 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
873 	}
874 
875 	spin_lock_bh(&adapter->rx_proc_lock);
876 	adapter->rx_locked = true;
877 	if (adapter->rx_processing) {
878 		spin_unlock_bh(&adapter->rx_proc_lock);
879 		flush_workqueue(adapter->rx_workqueue);
880 	} else {
881 	spin_unlock_bh(&adapter->rx_proc_lock);
882 	}
883 
884 	mwifiex_free_priv(priv);
885 	priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
886 	priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
887 	priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
888 
889 	return 0;
890 }
891 
892 static int
mwifiex_init_new_priv_params(struct mwifiex_private * priv,struct net_device * dev,enum nl80211_iftype type)893 mwifiex_init_new_priv_params(struct mwifiex_private *priv,
894 			     struct net_device *dev,
895 			     enum nl80211_iftype type)
896 {
897 	struct mwifiex_adapter *adapter = priv->adapter;
898 	unsigned long flags;
899 
900 	mwifiex_init_priv(priv);
901 
902 	priv->bss_mode = type;
903 	priv->wdev.iftype = type;
904 
905 	mwifiex_init_priv_params(priv, priv->netdev);
906 	priv->bss_started = 0;
907 
908 	switch (type) {
909 	case NL80211_IFTYPE_STATION:
910 	case NL80211_IFTYPE_ADHOC:
911 		priv->bss_role =  MWIFIEX_BSS_ROLE_STA;
912 		break;
913 	case NL80211_IFTYPE_P2P_CLIENT:
914 		priv->bss_role =  MWIFIEX_BSS_ROLE_STA;
915 		break;
916 	case NL80211_IFTYPE_P2P_GO:
917 		priv->bss_role =  MWIFIEX_BSS_ROLE_UAP;
918 		break;
919 	case NL80211_IFTYPE_AP:
920 		priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
921 		break;
922 	default:
923 		mwifiex_dbg(adapter, ERROR,
924 			    "%s: changing to %d not supported\n",
925 			    dev->name, type);
926 		return -EOPNOTSUPP;
927 	}
928 
929 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
930 	adapter->main_locked = false;
931 	spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
932 
933 	spin_lock_bh(&adapter->rx_proc_lock);
934 	adapter->rx_locked = false;
935 	spin_unlock_bh(&adapter->rx_proc_lock);
936 
937 	mwifiex_set_mac_address(priv, dev, false, NULL);
938 
939 	return 0;
940 }
941 
942 static int
mwifiex_change_vif_to_p2p(struct net_device * dev,enum nl80211_iftype curr_iftype,enum nl80211_iftype type,struct vif_params * params)943 mwifiex_change_vif_to_p2p(struct net_device *dev,
944 			  enum nl80211_iftype curr_iftype,
945 			  enum nl80211_iftype type,
946 			  struct vif_params *params)
947 {
948 	struct mwifiex_private *priv;
949 	struct mwifiex_adapter *adapter;
950 
951 	priv = mwifiex_netdev_get_priv(dev);
952 
953 	if (!priv)
954 		return -1;
955 
956 	adapter = priv->adapter;
957 
958 	if (adapter->curr_iface_comb.p2p_intf ==
959 	    adapter->iface_limit.p2p_intf) {
960 		mwifiex_dbg(adapter, ERROR,
961 			    "cannot create multiple P2P ifaces\n");
962 		return -1;
963 	}
964 
965 	mwifiex_dbg(adapter, INFO,
966 		    "%s: changing role to p2p\n", dev->name);
967 
968 	if (mwifiex_deinit_priv_params(priv))
969 		return -1;
970 	if (mwifiex_init_new_priv_params(priv, dev, type))
971 		return -1;
972 
973 	switch (type) {
974 	case NL80211_IFTYPE_P2P_CLIENT:
975 		if (mwifiex_cfg80211_init_p2p_client(priv))
976 			return -EFAULT;
977 		break;
978 	case NL80211_IFTYPE_P2P_GO:
979 		if (mwifiex_cfg80211_init_p2p_go(priv))
980 			return -EFAULT;
981 		break;
982 	default:
983 		mwifiex_dbg(adapter, ERROR,
984 			    "%s: changing to %d not supported\n",
985 			    dev->name, type);
986 		return -EOPNOTSUPP;
987 	}
988 
989 	if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
990 			     HostCmd_ACT_GEN_SET, 0, NULL, true))
991 		return -1;
992 
993 	if (mwifiex_sta_init_cmd(priv, false, false))
994 		return -1;
995 
996 	switch (curr_iftype) {
997 	case NL80211_IFTYPE_STATION:
998 	case NL80211_IFTYPE_ADHOC:
999 		adapter->curr_iface_comb.sta_intf--;
1000 		break;
1001 	case NL80211_IFTYPE_AP:
1002 		adapter->curr_iface_comb.uap_intf--;
1003 		break;
1004 	default:
1005 		break;
1006 	}
1007 
1008 	adapter->curr_iface_comb.p2p_intf++;
1009 	dev->ieee80211_ptr->iftype = type;
1010 
1011 	return 0;
1012 }
1013 
1014 static int
mwifiex_change_vif_to_sta_adhoc(struct net_device * dev,enum nl80211_iftype curr_iftype,enum nl80211_iftype type,struct vif_params * params)1015 mwifiex_change_vif_to_sta_adhoc(struct net_device *dev,
1016 				enum nl80211_iftype curr_iftype,
1017 				enum nl80211_iftype type,
1018 				struct vif_params *params)
1019 {
1020 	struct mwifiex_private *priv;
1021 	struct mwifiex_adapter *adapter;
1022 
1023 	priv = mwifiex_netdev_get_priv(dev);
1024 
1025 	if (!priv)
1026 		return -1;
1027 
1028 	adapter = priv->adapter;
1029 
1030 	if ((curr_iftype != NL80211_IFTYPE_P2P_CLIENT &&
1031 	     curr_iftype != NL80211_IFTYPE_P2P_GO) &&
1032 	    (adapter->curr_iface_comb.sta_intf ==
1033 	     adapter->iface_limit.sta_intf)) {
1034 		mwifiex_dbg(adapter, ERROR,
1035 			    "cannot create multiple station/adhoc ifaces\n");
1036 		return -1;
1037 	}
1038 
1039 	if (type == NL80211_IFTYPE_STATION)
1040 		mwifiex_dbg(adapter, INFO,
1041 			    "%s: changing role to station\n", dev->name);
1042 	else
1043 		mwifiex_dbg(adapter, INFO,
1044 			    "%s: changing role to adhoc\n", dev->name);
1045 
1046 	if (mwifiex_deinit_priv_params(priv))
1047 		return -1;
1048 	if (mwifiex_init_new_priv_params(priv, dev, type))
1049 		return -1;
1050 	if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1051 			     HostCmd_ACT_GEN_SET, 0, NULL, true))
1052 		return -1;
1053 	if (mwifiex_sta_init_cmd(priv, false, false))
1054 		return -1;
1055 
1056 	switch (curr_iftype) {
1057 	case NL80211_IFTYPE_P2P_CLIENT:
1058 	case NL80211_IFTYPE_P2P_GO:
1059 		adapter->curr_iface_comb.p2p_intf--;
1060 		break;
1061 	case NL80211_IFTYPE_AP:
1062 		adapter->curr_iface_comb.uap_intf--;
1063 		break;
1064 	default:
1065 		break;
1066 	}
1067 
1068 	adapter->curr_iface_comb.sta_intf++;
1069 	dev->ieee80211_ptr->iftype = type;
1070 	return 0;
1071 }
1072 
1073 static int
mwifiex_change_vif_to_ap(struct net_device * dev,enum nl80211_iftype curr_iftype,enum nl80211_iftype type,struct vif_params * params)1074 mwifiex_change_vif_to_ap(struct net_device *dev,
1075 			 enum nl80211_iftype curr_iftype,
1076 			 enum nl80211_iftype type,
1077 			 struct vif_params *params)
1078 {
1079 	struct mwifiex_private *priv;
1080 	struct mwifiex_adapter *adapter;
1081 
1082 	priv = mwifiex_netdev_get_priv(dev);
1083 
1084 	if (!priv)
1085 		return -1;
1086 
1087 	adapter = priv->adapter;
1088 
1089 	if (adapter->curr_iface_comb.uap_intf ==
1090 	    adapter->iface_limit.uap_intf) {
1091 		mwifiex_dbg(adapter, ERROR,
1092 			    "cannot create multiple AP ifaces\n");
1093 		return -1;
1094 	}
1095 
1096 	mwifiex_dbg(adapter, INFO,
1097 		    "%s: changing role to AP\n", dev->name);
1098 
1099 	if (mwifiex_deinit_priv_params(priv))
1100 		return -1;
1101 	if (mwifiex_init_new_priv_params(priv, dev, type))
1102 		return -1;
1103 	if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1104 			     HostCmd_ACT_GEN_SET, 0, NULL, true))
1105 		return -1;
1106 	if (mwifiex_sta_init_cmd(priv, false, false))
1107 		return -1;
1108 
1109 	switch (curr_iftype) {
1110 	case NL80211_IFTYPE_P2P_CLIENT:
1111 	case NL80211_IFTYPE_P2P_GO:
1112 		adapter->curr_iface_comb.p2p_intf--;
1113 		break;
1114 	case NL80211_IFTYPE_STATION:
1115 	case NL80211_IFTYPE_ADHOC:
1116 		adapter->curr_iface_comb.sta_intf--;
1117 		break;
1118 	default:
1119 		break;
1120 	}
1121 
1122 	adapter->curr_iface_comb.uap_intf++;
1123 	dev->ieee80211_ptr->iftype = type;
1124 	return 0;
1125 }
1126 /*
1127  * CFG802.11 operation handler to change interface type.
1128  */
1129 static int
mwifiex_cfg80211_change_virtual_intf(struct wiphy * wiphy,struct net_device * dev,enum nl80211_iftype type,struct vif_params * params)1130 mwifiex_cfg80211_change_virtual_intf(struct wiphy *wiphy,
1131 				     struct net_device *dev,
1132 				     enum nl80211_iftype type,
1133 				     struct vif_params *params)
1134 {
1135 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1136 	enum nl80211_iftype curr_iftype = dev->ieee80211_ptr->iftype;
1137 
1138 	if (priv->scan_request) {
1139 		mwifiex_dbg(priv->adapter, ERROR,
1140 			    "change virtual interface: scan in process\n");
1141 		return -EBUSY;
1142 	}
1143 
1144 	switch (curr_iftype) {
1145 	case NL80211_IFTYPE_ADHOC:
1146 		switch (type) {
1147 		case NL80211_IFTYPE_STATION:
1148 			priv->bss_mode = type;
1149 			priv->sec_info.authentication_mode =
1150 						   NL80211_AUTHTYPE_OPEN_SYSTEM;
1151 			dev->ieee80211_ptr->iftype = type;
1152 			mwifiex_deauthenticate(priv, NULL);
1153 			return mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1154 						HostCmd_ACT_GEN_SET, 0, NULL,
1155 						true);
1156 		case NL80211_IFTYPE_P2P_CLIENT:
1157 		case NL80211_IFTYPE_P2P_GO:
1158 			return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1159 							 type, params);
1160 		case NL80211_IFTYPE_AP:
1161 			return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1162 							params);
1163 		case NL80211_IFTYPE_UNSPECIFIED:
1164 			mwifiex_dbg(priv->adapter, INFO,
1165 				    "%s: kept type as IBSS\n", dev->name);
1166 			fallthrough;
1167 		case NL80211_IFTYPE_ADHOC:	/* This shouldn't happen */
1168 			return 0;
1169 		default:
1170 			mwifiex_dbg(priv->adapter, ERROR,
1171 				    "%s: changing to %d not supported\n",
1172 				    dev->name, type);
1173 			return -EOPNOTSUPP;
1174 		}
1175 		break;
1176 	case NL80211_IFTYPE_STATION:
1177 		switch (type) {
1178 		case NL80211_IFTYPE_ADHOC:
1179 			priv->bss_mode = type;
1180 			priv->sec_info.authentication_mode =
1181 						   NL80211_AUTHTYPE_OPEN_SYSTEM;
1182 			dev->ieee80211_ptr->iftype = type;
1183 			mwifiex_deauthenticate(priv, NULL);
1184 			return mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1185 						HostCmd_ACT_GEN_SET, 0, NULL,
1186 						true);
1187 		case NL80211_IFTYPE_P2P_CLIENT:
1188 		case NL80211_IFTYPE_P2P_GO:
1189 			return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1190 							 type, params);
1191 		case NL80211_IFTYPE_AP:
1192 			return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1193 							params);
1194 		case NL80211_IFTYPE_UNSPECIFIED:
1195 			mwifiex_dbg(priv->adapter, INFO,
1196 				    "%s: kept type as STA\n", dev->name);
1197 			fallthrough;
1198 		case NL80211_IFTYPE_STATION:	/* This shouldn't happen */
1199 			return 0;
1200 		default:
1201 			mwifiex_dbg(priv->adapter, ERROR,
1202 				    "%s: changing to %d not supported\n",
1203 				    dev->name, type);
1204 			return -EOPNOTSUPP;
1205 		}
1206 		break;
1207 	case NL80211_IFTYPE_AP:
1208 		switch (type) {
1209 		case NL80211_IFTYPE_ADHOC:
1210 			return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1211 							       type, params);
1212 			break;
1213 		case NL80211_IFTYPE_P2P_CLIENT:
1214 		case NL80211_IFTYPE_P2P_GO:
1215 			return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1216 							 type, params);
1217 		case NL80211_IFTYPE_UNSPECIFIED:
1218 			mwifiex_dbg(priv->adapter, INFO,
1219 				    "%s: kept type as AP\n", dev->name);
1220 			fallthrough;
1221 		case NL80211_IFTYPE_AP:		/* This shouldn't happen */
1222 			return 0;
1223 		default:
1224 			mwifiex_dbg(priv->adapter, ERROR,
1225 				    "%s: changing to %d not supported\n",
1226 				    dev->name, type);
1227 			return -EOPNOTSUPP;
1228 		}
1229 		break;
1230 	case NL80211_IFTYPE_P2P_CLIENT:
1231 	case NL80211_IFTYPE_P2P_GO:
1232 		switch (type) {
1233 		case NL80211_IFTYPE_STATION:
1234 			if (mwifiex_cfg80211_deinit_p2p(priv))
1235 				return -EFAULT;
1236 			priv->adapter->curr_iface_comb.p2p_intf--;
1237 			priv->adapter->curr_iface_comb.sta_intf++;
1238 			dev->ieee80211_ptr->iftype = type;
1239 			if (mwifiex_deinit_priv_params(priv))
1240 				return -1;
1241 			if (mwifiex_init_new_priv_params(priv, dev, type))
1242 				return -1;
1243 			if (mwifiex_sta_init_cmd(priv, false, false))
1244 				return -1;
1245 			break;
1246 		case NL80211_IFTYPE_ADHOC:
1247 			if (mwifiex_cfg80211_deinit_p2p(priv))
1248 				return -EFAULT;
1249 			return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1250 							       type, params);
1251 			break;
1252 		case NL80211_IFTYPE_AP:
1253 			if (mwifiex_cfg80211_deinit_p2p(priv))
1254 				return -EFAULT;
1255 			return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1256 							params);
1257 		case NL80211_IFTYPE_UNSPECIFIED:
1258 			mwifiex_dbg(priv->adapter, INFO,
1259 				    "%s: kept type as P2P\n", dev->name);
1260 			fallthrough;
1261 		case NL80211_IFTYPE_P2P_CLIENT:
1262 		case NL80211_IFTYPE_P2P_GO:
1263 			return 0;
1264 		default:
1265 			mwifiex_dbg(priv->adapter, ERROR,
1266 				    "%s: changing to %d not supported\n",
1267 				    dev->name, type);
1268 			return -EOPNOTSUPP;
1269 		}
1270 		break;
1271 	default:
1272 		mwifiex_dbg(priv->adapter, ERROR,
1273 			    "%s: unknown iftype: %d\n",
1274 			    dev->name, dev->ieee80211_ptr->iftype);
1275 		return -EOPNOTSUPP;
1276 	}
1277 
1278 
1279 	return 0;
1280 }
1281 
1282 static void
mwifiex_parse_htinfo(struct mwifiex_private * priv,u8 rateinfo,u8 htinfo,struct rate_info * rate)1283 mwifiex_parse_htinfo(struct mwifiex_private *priv, u8 rateinfo, u8 htinfo,
1284 		     struct rate_info *rate)
1285 {
1286 	struct mwifiex_adapter *adapter = priv->adapter;
1287 
1288 	if (adapter->is_hw_11ac_capable) {
1289 		/* bit[1-0]: 00=LG 01=HT 10=VHT */
1290 		if (htinfo & BIT(0)) {
1291 			/* HT */
1292 			rate->mcs = rateinfo;
1293 			rate->flags |= RATE_INFO_FLAGS_MCS;
1294 		}
1295 		if (htinfo & BIT(1)) {
1296 			/* VHT */
1297 			rate->mcs = rateinfo & 0x0F;
1298 			rate->flags |= RATE_INFO_FLAGS_VHT_MCS;
1299 		}
1300 
1301 		if (htinfo & (BIT(1) | BIT(0))) {
1302 			/* HT or VHT */
1303 			switch (htinfo & (BIT(3) | BIT(2))) {
1304 			case 0:
1305 				rate->bw = RATE_INFO_BW_20;
1306 				break;
1307 			case (BIT(2)):
1308 				rate->bw = RATE_INFO_BW_40;
1309 				break;
1310 			case (BIT(3)):
1311 				rate->bw = RATE_INFO_BW_80;
1312 				break;
1313 			case (BIT(3) | BIT(2)):
1314 				rate->bw = RATE_INFO_BW_160;
1315 				break;
1316 			}
1317 
1318 			if (htinfo & BIT(4))
1319 				rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1320 
1321 			if ((rateinfo >> 4) == 1)
1322 				rate->nss = 2;
1323 			else
1324 				rate->nss = 1;
1325 		}
1326 	} else {
1327 		/*
1328 		 * Bit 0 in htinfo indicates that current rate is 11n. Valid
1329 		 * MCS index values for us are 0 to 15.
1330 		 */
1331 		if ((htinfo & BIT(0)) && (rateinfo < 16)) {
1332 			rate->mcs = rateinfo;
1333 			rate->flags |= RATE_INFO_FLAGS_MCS;
1334 			rate->bw = RATE_INFO_BW_20;
1335 			if (htinfo & BIT(1))
1336 				rate->bw = RATE_INFO_BW_40;
1337 			if (htinfo & BIT(2))
1338 				rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1339 		}
1340 	}
1341 
1342 	/* Decode legacy rates for non-HT. */
1343 	if (!(htinfo & (BIT(0) | BIT(1)))) {
1344 		/* Bitrates in multiples of 100kb/s. */
1345 		static const int legacy_rates[] = {
1346 			[0] = 10,
1347 			[1] = 20,
1348 			[2] = 55,
1349 			[3] = 110,
1350 			[4] = 60, /* MWIFIEX_RATE_INDEX_OFDM0 */
1351 			[5] = 60,
1352 			[6] = 90,
1353 			[7] = 120,
1354 			[8] = 180,
1355 			[9] = 240,
1356 			[10] = 360,
1357 			[11] = 480,
1358 			[12] = 540,
1359 		};
1360 		if (rateinfo < ARRAY_SIZE(legacy_rates))
1361 			rate->legacy = legacy_rates[rateinfo];
1362 	}
1363 }
1364 
1365 /*
1366  * This function dumps the station information on a buffer.
1367  *
1368  * The following information are shown -
1369  *      - Total bytes transmitted
1370  *      - Total bytes received
1371  *      - Total packets transmitted
1372  *      - Total packets received
1373  *      - Signal quality level
1374  *      - Transmission rate
1375  */
1376 static int
mwifiex_dump_station_info(struct mwifiex_private * priv,struct mwifiex_sta_node * node,struct station_info * sinfo)1377 mwifiex_dump_station_info(struct mwifiex_private *priv,
1378 			  struct mwifiex_sta_node *node,
1379 			  struct station_info *sinfo)
1380 {
1381 	u32 rate;
1382 
1383 	sinfo->filled = BIT_ULL(NL80211_STA_INFO_RX_BYTES) | BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
1384 			BIT_ULL(NL80211_STA_INFO_RX_PACKETS) | BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
1385 			BIT_ULL(NL80211_STA_INFO_TX_BITRATE) |
1386 			BIT_ULL(NL80211_STA_INFO_SIGNAL) | BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
1387 
1388 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1389 		if (!node)
1390 			return -ENOENT;
1391 
1392 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
1393 				BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1394 		sinfo->inactive_time =
1395 			jiffies_to_msecs(jiffies - node->stats.last_rx);
1396 
1397 		sinfo->signal = node->stats.rssi;
1398 		sinfo->signal_avg = node->stats.rssi;
1399 		sinfo->rx_bytes = node->stats.rx_bytes;
1400 		sinfo->tx_bytes = node->stats.tx_bytes;
1401 		sinfo->rx_packets = node->stats.rx_packets;
1402 		sinfo->tx_packets = node->stats.tx_packets;
1403 		sinfo->tx_failed = node->stats.tx_failed;
1404 
1405 		mwifiex_parse_htinfo(priv, priv->tx_rate,
1406 				     node->stats.last_tx_htinfo,
1407 				     &sinfo->txrate);
1408 		sinfo->txrate.legacy = node->stats.last_tx_rate * 5;
1409 
1410 		return 0;
1411 	}
1412 
1413 	/* Get signal information from the firmware */
1414 	if (mwifiex_send_cmd(priv, HostCmd_CMD_RSSI_INFO,
1415 			     HostCmd_ACT_GEN_GET, 0, NULL, true)) {
1416 		mwifiex_dbg(priv->adapter, ERROR,
1417 			    "failed to get signal information\n");
1418 		return -EFAULT;
1419 	}
1420 
1421 	if (mwifiex_drv_get_data_rate(priv, &rate)) {
1422 		mwifiex_dbg(priv->adapter, ERROR,
1423 			    "getting data rate error\n");
1424 		return -EFAULT;
1425 	}
1426 
1427 	/* Get DTIM period information from firmware */
1428 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
1429 			 HostCmd_ACT_GEN_GET, DTIM_PERIOD_I,
1430 			 &priv->dtim_period, true);
1431 
1432 	mwifiex_parse_htinfo(priv, priv->tx_rate, priv->tx_htinfo,
1433 			     &sinfo->txrate);
1434 
1435 	sinfo->signal_avg = priv->bcn_rssi_avg;
1436 	sinfo->rx_bytes = priv->stats.rx_bytes;
1437 	sinfo->tx_bytes = priv->stats.tx_bytes;
1438 	sinfo->rx_packets = priv->stats.rx_packets;
1439 	sinfo->tx_packets = priv->stats.tx_packets;
1440 	sinfo->signal = priv->bcn_rssi_avg;
1441 	/* bit rate is in 500 kb/s units. Convert it to 100kb/s units */
1442 	sinfo->txrate.legacy = rate * 5;
1443 
1444 	sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
1445 	mwifiex_parse_htinfo(priv, priv->rxpd_rate, priv->rxpd_htinfo,
1446 			     &sinfo->rxrate);
1447 
1448 	if (priv->bss_mode == NL80211_IFTYPE_STATION) {
1449 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BSS_PARAM);
1450 		sinfo->bss_param.flags = 0;
1451 		if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1452 						WLAN_CAPABILITY_SHORT_PREAMBLE)
1453 			sinfo->bss_param.flags |=
1454 					BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1455 		if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1456 						WLAN_CAPABILITY_SHORT_SLOT_TIME)
1457 			sinfo->bss_param.flags |=
1458 					BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1459 		sinfo->bss_param.dtim_period = priv->dtim_period;
1460 		sinfo->bss_param.beacon_interval =
1461 			priv->curr_bss_params.bss_descriptor.beacon_period;
1462 	}
1463 
1464 	return 0;
1465 }
1466 
1467 /*
1468  * CFG802.11 operation handler to get station information.
1469  *
1470  * This function only works in connected mode, and dumps the
1471  * requested station information, if available.
1472  */
1473 static int
mwifiex_cfg80211_get_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_info * sinfo)1474 mwifiex_cfg80211_get_station(struct wiphy *wiphy, struct net_device *dev,
1475 			     const u8 *mac, struct station_info *sinfo)
1476 {
1477 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1478 
1479 	if (!priv->media_connected)
1480 		return -ENOENT;
1481 	if (memcmp(mac, priv->cfg_bssid, ETH_ALEN))
1482 		return -ENOENT;
1483 
1484 	return mwifiex_dump_station_info(priv, NULL, sinfo);
1485 }
1486 
1487 /*
1488  * CFG802.11 operation handler to dump station information.
1489  */
1490 static int
mwifiex_cfg80211_dump_station(struct wiphy * wiphy,struct net_device * dev,int idx,u8 * mac,struct station_info * sinfo)1491 mwifiex_cfg80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
1492 			      int idx, u8 *mac, struct station_info *sinfo)
1493 {
1494 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1495 	struct mwifiex_sta_node *node;
1496 	int i;
1497 
1498 	if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
1499 	    priv->media_connected && idx == 0) {
1500 		ether_addr_copy(mac, priv->cfg_bssid);
1501 		return mwifiex_dump_station_info(priv, NULL, sinfo);
1502 	} else if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1503 		mwifiex_send_cmd(priv, HOST_CMD_APCMD_STA_LIST,
1504 				 HostCmd_ACT_GEN_GET, 0, NULL, true);
1505 
1506 		i = 0;
1507 		list_for_each_entry(node, &priv->sta_list, list) {
1508 			if (i++ != idx)
1509 				continue;
1510 			ether_addr_copy(mac, node->mac_addr);
1511 			return mwifiex_dump_station_info(priv, node, sinfo);
1512 		}
1513 	}
1514 
1515 	return -ENOENT;
1516 }
1517 
1518 static int
mwifiex_cfg80211_dump_survey(struct wiphy * wiphy,struct net_device * dev,int idx,struct survey_info * survey)1519 mwifiex_cfg80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
1520 			     int idx, struct survey_info *survey)
1521 {
1522 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1523 	struct mwifiex_chan_stats *pchan_stats = priv->adapter->chan_stats;
1524 	enum nl80211_band band;
1525 
1526 	mwifiex_dbg(priv->adapter, DUMP, "dump_survey idx=%d\n", idx);
1527 
1528 	memset(survey, 0, sizeof(struct survey_info));
1529 
1530 	if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
1531 	    priv->media_connected && idx == 0) {
1532 			u8 curr_bss_band = priv->curr_bss_params.band;
1533 			u32 chan = priv->curr_bss_params.bss_descriptor.channel;
1534 
1535 			band = mwifiex_band_to_radio_type(curr_bss_band);
1536 			survey->channel = ieee80211_get_channel(wiphy,
1537 				ieee80211_channel_to_frequency(chan, band));
1538 
1539 			if (priv->bcn_nf_last) {
1540 				survey->filled = SURVEY_INFO_NOISE_DBM;
1541 				survey->noise = priv->bcn_nf_last;
1542 			}
1543 			return 0;
1544 	}
1545 
1546 	if (idx >= priv->adapter->num_in_chan_stats)
1547 		return -ENOENT;
1548 
1549 	if (!pchan_stats[idx].cca_scan_dur)
1550 		return 0;
1551 
1552 	band = pchan_stats[idx].bandcfg;
1553 	survey->channel = ieee80211_get_channel(wiphy,
1554 	    ieee80211_channel_to_frequency(pchan_stats[idx].chan_num, band));
1555 	survey->filled = SURVEY_INFO_NOISE_DBM |
1556 			 SURVEY_INFO_TIME |
1557 			 SURVEY_INFO_TIME_BUSY;
1558 	survey->noise = pchan_stats[idx].noise;
1559 	survey->time = pchan_stats[idx].cca_scan_dur;
1560 	survey->time_busy = pchan_stats[idx].cca_busy_dur;
1561 
1562 	return 0;
1563 }
1564 
1565 /* Supported rates to be advertised to the cfg80211 */
1566 static struct ieee80211_rate mwifiex_rates[] = {
1567 	{.bitrate = 10, .hw_value = 2, },
1568 	{.bitrate = 20, .hw_value = 4, },
1569 	{.bitrate = 55, .hw_value = 11, },
1570 	{.bitrate = 110, .hw_value = 22, },
1571 	{.bitrate = 60, .hw_value = 12, },
1572 	{.bitrate = 90, .hw_value = 18, },
1573 	{.bitrate = 120, .hw_value = 24, },
1574 	{.bitrate = 180, .hw_value = 36, },
1575 	{.bitrate = 240, .hw_value = 48, },
1576 	{.bitrate = 360, .hw_value = 72, },
1577 	{.bitrate = 480, .hw_value = 96, },
1578 	{.bitrate = 540, .hw_value = 108, },
1579 };
1580 
1581 /* Channel definitions to be advertised to cfg80211 */
1582 static struct ieee80211_channel mwifiex_channels_2ghz[] = {
1583 	{.center_freq = 2412, .hw_value = 1, },
1584 	{.center_freq = 2417, .hw_value = 2, },
1585 	{.center_freq = 2422, .hw_value = 3, },
1586 	{.center_freq = 2427, .hw_value = 4, },
1587 	{.center_freq = 2432, .hw_value = 5, },
1588 	{.center_freq = 2437, .hw_value = 6, },
1589 	{.center_freq = 2442, .hw_value = 7, },
1590 	{.center_freq = 2447, .hw_value = 8, },
1591 	{.center_freq = 2452, .hw_value = 9, },
1592 	{.center_freq = 2457, .hw_value = 10, },
1593 	{.center_freq = 2462, .hw_value = 11, },
1594 	{.center_freq = 2467, .hw_value = 12, },
1595 	{.center_freq = 2472, .hw_value = 13, },
1596 	{.center_freq = 2484, .hw_value = 14, },
1597 };
1598 
1599 static struct ieee80211_supported_band mwifiex_band_2ghz = {
1600 	.channels = mwifiex_channels_2ghz,
1601 	.n_channels = ARRAY_SIZE(mwifiex_channels_2ghz),
1602 	.bitrates = mwifiex_rates,
1603 	.n_bitrates = ARRAY_SIZE(mwifiex_rates),
1604 };
1605 
1606 static struct ieee80211_channel mwifiex_channels_5ghz[] = {
1607 	{.center_freq = 5040, .hw_value = 8, },
1608 	{.center_freq = 5060, .hw_value = 12, },
1609 	{.center_freq = 5080, .hw_value = 16, },
1610 	{.center_freq = 5170, .hw_value = 34, },
1611 	{.center_freq = 5190, .hw_value = 38, },
1612 	{.center_freq = 5210, .hw_value = 42, },
1613 	{.center_freq = 5230, .hw_value = 46, },
1614 	{.center_freq = 5180, .hw_value = 36, },
1615 	{.center_freq = 5200, .hw_value = 40, },
1616 	{.center_freq = 5220, .hw_value = 44, },
1617 	{.center_freq = 5240, .hw_value = 48, },
1618 	{.center_freq = 5260, .hw_value = 52, },
1619 	{.center_freq = 5280, .hw_value = 56, },
1620 	{.center_freq = 5300, .hw_value = 60, },
1621 	{.center_freq = 5320, .hw_value = 64, },
1622 	{.center_freq = 5500, .hw_value = 100, },
1623 	{.center_freq = 5520, .hw_value = 104, },
1624 	{.center_freq = 5540, .hw_value = 108, },
1625 	{.center_freq = 5560, .hw_value = 112, },
1626 	{.center_freq = 5580, .hw_value = 116, },
1627 	{.center_freq = 5600, .hw_value = 120, },
1628 	{.center_freq = 5620, .hw_value = 124, },
1629 	{.center_freq = 5640, .hw_value = 128, },
1630 	{.center_freq = 5660, .hw_value = 132, },
1631 	{.center_freq = 5680, .hw_value = 136, },
1632 	{.center_freq = 5700, .hw_value = 140, },
1633 	{.center_freq = 5745, .hw_value = 149, },
1634 	{.center_freq = 5765, .hw_value = 153, },
1635 	{.center_freq = 5785, .hw_value = 157, },
1636 	{.center_freq = 5805, .hw_value = 161, },
1637 	{.center_freq = 5825, .hw_value = 165, },
1638 };
1639 
1640 static struct ieee80211_supported_band mwifiex_band_5ghz = {
1641 	.channels = mwifiex_channels_5ghz,
1642 	.n_channels = ARRAY_SIZE(mwifiex_channels_5ghz),
1643 	.bitrates = mwifiex_rates + 4,
1644 	.n_bitrates = ARRAY_SIZE(mwifiex_rates) - 4,
1645 };
1646 
1647 
1648 /* Supported crypto cipher suits to be advertised to cfg80211 */
1649 static const u32 mwifiex_cipher_suites[] = {
1650 	WLAN_CIPHER_SUITE_WEP40,
1651 	WLAN_CIPHER_SUITE_WEP104,
1652 	WLAN_CIPHER_SUITE_TKIP,
1653 	WLAN_CIPHER_SUITE_CCMP,
1654 	WLAN_CIPHER_SUITE_SMS4,
1655 	WLAN_CIPHER_SUITE_AES_CMAC,
1656 };
1657 
1658 /* Supported mgmt frame types to be advertised to cfg80211 */
1659 static const struct ieee80211_txrx_stypes
1660 mwifiex_mgmt_stypes[NUM_NL80211_IFTYPES] = {
1661 	[NL80211_IFTYPE_STATION] = {
1662 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1663 		      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1664 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1665 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1666 	},
1667 	[NL80211_IFTYPE_AP] = {
1668 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1669 		      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1670 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1671 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1672 	},
1673 	[NL80211_IFTYPE_P2P_CLIENT] = {
1674 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1675 		      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1676 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1677 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1678 	},
1679 	[NL80211_IFTYPE_P2P_GO] = {
1680 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1681 		      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1682 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1683 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1684 	},
1685 };
1686 
1687 /*
1688  * CFG802.11 operation handler for setting bit rates.
1689  *
1690  * Function configures data rates to firmware using bitrate mask
1691  * provided by cfg80211.
1692  */
mwifiex_cfg80211_set_bitrate_mask(struct wiphy * wiphy,struct net_device * dev,const u8 * peer,const struct cfg80211_bitrate_mask * mask)1693 static int mwifiex_cfg80211_set_bitrate_mask(struct wiphy *wiphy,
1694 				struct net_device *dev,
1695 				const u8 *peer,
1696 				const struct cfg80211_bitrate_mask *mask)
1697 {
1698 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1699 	u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
1700 	enum nl80211_band band;
1701 	struct mwifiex_adapter *adapter = priv->adapter;
1702 
1703 	if (!priv->media_connected) {
1704 		mwifiex_dbg(adapter, ERROR,
1705 			    "Can not set Tx data rate in disconnected state\n");
1706 		return -EINVAL;
1707 	}
1708 
1709 	band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
1710 
1711 	memset(bitmap_rates, 0, sizeof(bitmap_rates));
1712 
1713 	/* Fill HR/DSSS rates. */
1714 	if (band == NL80211_BAND_2GHZ)
1715 		bitmap_rates[0] = mask->control[band].legacy & 0x000f;
1716 
1717 	/* Fill OFDM rates */
1718 	if (band == NL80211_BAND_2GHZ)
1719 		bitmap_rates[1] = (mask->control[band].legacy & 0x0ff0) >> 4;
1720 	else
1721 		bitmap_rates[1] = mask->control[band].legacy;
1722 
1723 	/* Fill HT MCS rates */
1724 	bitmap_rates[2] = mask->control[band].ht_mcs[0];
1725 	if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1726 		bitmap_rates[2] |= mask->control[band].ht_mcs[1] << 8;
1727 
1728        /* Fill VHT MCS rates */
1729 	if (adapter->fw_api_ver == MWIFIEX_FW_V15) {
1730 		bitmap_rates[10] = mask->control[band].vht_mcs[0];
1731 		if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1732 			bitmap_rates[11] = mask->control[band].vht_mcs[1];
1733 	}
1734 
1735 	return mwifiex_send_cmd(priv, HostCmd_CMD_TX_RATE_CFG,
1736 				HostCmd_ACT_GEN_SET, 0, bitmap_rates, true);
1737 }
1738 
1739 /*
1740  * CFG802.11 operation handler for connection quality monitoring.
1741  *
1742  * This function subscribes/unsubscribes HIGH_RSSI and LOW_RSSI
1743  * events to FW.
1744  */
mwifiex_cfg80211_set_cqm_rssi_config(struct wiphy * wiphy,struct net_device * dev,s32 rssi_thold,u32 rssi_hyst)1745 static int mwifiex_cfg80211_set_cqm_rssi_config(struct wiphy *wiphy,
1746 						struct net_device *dev,
1747 						s32 rssi_thold, u32 rssi_hyst)
1748 {
1749 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1750 	struct mwifiex_ds_misc_subsc_evt subsc_evt;
1751 
1752 	priv->cqm_rssi_thold = rssi_thold;
1753 	priv->cqm_rssi_hyst = rssi_hyst;
1754 
1755 	memset(&subsc_evt, 0x00, sizeof(struct mwifiex_ds_misc_subsc_evt));
1756 	subsc_evt.events = BITMASK_BCN_RSSI_LOW | BITMASK_BCN_RSSI_HIGH;
1757 
1758 	/* Subscribe/unsubscribe low and high rssi events */
1759 	if (rssi_thold && rssi_hyst) {
1760 		subsc_evt.action = HostCmd_ACT_BITWISE_SET;
1761 		subsc_evt.bcn_l_rssi_cfg.abs_value = abs(rssi_thold);
1762 		subsc_evt.bcn_h_rssi_cfg.abs_value = abs(rssi_thold);
1763 		subsc_evt.bcn_l_rssi_cfg.evt_freq = 1;
1764 		subsc_evt.bcn_h_rssi_cfg.evt_freq = 1;
1765 		return mwifiex_send_cmd(priv,
1766 					HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1767 					0, 0, &subsc_evt, true);
1768 	} else {
1769 		subsc_evt.action = HostCmd_ACT_BITWISE_CLR;
1770 		return mwifiex_send_cmd(priv,
1771 					HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1772 					0, 0, &subsc_evt, true);
1773 	}
1774 
1775 	return 0;
1776 }
1777 
1778 /* cfg80211 operation handler for change_beacon.
1779  * Function retrieves and sets modified management IEs to FW.
1780  */
mwifiex_cfg80211_change_beacon(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_beacon_data * data)1781 static int mwifiex_cfg80211_change_beacon(struct wiphy *wiphy,
1782 					  struct net_device *dev,
1783 					  struct cfg80211_beacon_data *data)
1784 {
1785 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1786 	struct mwifiex_adapter *adapter = priv->adapter;
1787 
1788 	mwifiex_cancel_scan(adapter);
1789 
1790 	if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP) {
1791 		mwifiex_dbg(priv->adapter, ERROR,
1792 			    "%s: bss_type mismatched\n", __func__);
1793 		return -EINVAL;
1794 	}
1795 
1796 	if (!priv->bss_started) {
1797 		mwifiex_dbg(priv->adapter, ERROR,
1798 			    "%s: bss not started\n", __func__);
1799 		return -EINVAL;
1800 	}
1801 
1802 	if (mwifiex_set_mgmt_ies(priv, data)) {
1803 		mwifiex_dbg(priv->adapter, ERROR,
1804 			    "%s: setting mgmt ies failed\n", __func__);
1805 		return -EFAULT;
1806 	}
1807 
1808 	return 0;
1809 }
1810 
1811 /* cfg80211 operation handler for del_station.
1812  * Function deauthenticates station which value is provided in mac parameter.
1813  * If mac is NULL/broadcast, all stations in associated station list are
1814  * deauthenticated. If bss is not started or there are no stations in
1815  * associated stations list, no action is taken.
1816  */
1817 static int
mwifiex_cfg80211_del_station(struct wiphy * wiphy,struct net_device * dev,struct station_del_parameters * params)1818 mwifiex_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev,
1819 			     struct station_del_parameters *params)
1820 {
1821 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1822 	struct mwifiex_sta_node *sta_node;
1823 	u8 deauth_mac[ETH_ALEN];
1824 
1825 	if (!priv->bss_started && priv->wdev.cac_started) {
1826 		mwifiex_dbg(priv->adapter, INFO, "%s: abort CAC!\n", __func__);
1827 		mwifiex_abort_cac(priv);
1828 	}
1829 
1830 	if (list_empty(&priv->sta_list) || !priv->bss_started)
1831 		return 0;
1832 
1833 	if (!params->mac || is_broadcast_ether_addr(params->mac))
1834 		return 0;
1835 
1836 	mwifiex_dbg(priv->adapter, INFO, "%s: mac address %pM\n",
1837 		    __func__, params->mac);
1838 
1839 	eth_zero_addr(deauth_mac);
1840 
1841 	spin_lock_bh(&priv->sta_list_spinlock);
1842 	sta_node = mwifiex_get_sta_entry(priv, params->mac);
1843 	if (sta_node)
1844 		ether_addr_copy(deauth_mac, params->mac);
1845 	spin_unlock_bh(&priv->sta_list_spinlock);
1846 
1847 	if (is_valid_ether_addr(deauth_mac)) {
1848 		if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_STA_DEAUTH,
1849 				     HostCmd_ACT_GEN_SET, 0,
1850 				     deauth_mac, true))
1851 			return -1;
1852 	}
1853 
1854 	return 0;
1855 }
1856 
1857 static int
mwifiex_cfg80211_set_antenna(struct wiphy * wiphy,u32 tx_ant,u32 rx_ant)1858 mwifiex_cfg80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
1859 {
1860 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1861 	struct mwifiex_private *priv = mwifiex_get_priv(adapter,
1862 							MWIFIEX_BSS_ROLE_ANY);
1863 	struct mwifiex_ds_ant_cfg ant_cfg;
1864 
1865 	if (!tx_ant || !rx_ant)
1866 		return -EOPNOTSUPP;
1867 
1868 	if (adapter->hw_dev_mcs_support != HT_STREAM_2X2) {
1869 		/* Not a MIMO chip. User should provide specific antenna number
1870 		 * for Tx/Rx path or enable all antennas for diversity
1871 		 */
1872 		if (tx_ant != rx_ant)
1873 			return -EOPNOTSUPP;
1874 
1875 		if ((tx_ant & (tx_ant - 1)) &&
1876 		    (tx_ant != BIT(adapter->number_of_antenna) - 1))
1877 			return -EOPNOTSUPP;
1878 
1879 		if ((tx_ant == BIT(adapter->number_of_antenna) - 1) &&
1880 		    (priv->adapter->number_of_antenna > 1)) {
1881 			tx_ant = RF_ANTENNA_AUTO;
1882 			rx_ant = RF_ANTENNA_AUTO;
1883 		}
1884 	} else {
1885 		struct ieee80211_sta_ht_cap *ht_info;
1886 		int rx_mcs_supp;
1887 		enum nl80211_band band;
1888 
1889 		if ((tx_ant == 0x1 && rx_ant == 0x1)) {
1890 			adapter->user_dev_mcs_support = HT_STREAM_1X1;
1891 			if (adapter->is_hw_11ac_capable)
1892 				adapter->usr_dot_11ac_mcs_support =
1893 						MWIFIEX_11AC_MCS_MAP_1X1;
1894 		} else {
1895 			adapter->user_dev_mcs_support = HT_STREAM_2X2;
1896 			if (adapter->is_hw_11ac_capable)
1897 				adapter->usr_dot_11ac_mcs_support =
1898 						MWIFIEX_11AC_MCS_MAP_2X2;
1899 		}
1900 
1901 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
1902 			if (!adapter->wiphy->bands[band])
1903 				continue;
1904 
1905 			ht_info = &adapter->wiphy->bands[band]->ht_cap;
1906 			rx_mcs_supp =
1907 				GET_RXMCSSUPP(adapter->user_dev_mcs_support);
1908 			memset(&ht_info->mcs, 0, adapter->number_of_antenna);
1909 			memset(&ht_info->mcs, 0xff, rx_mcs_supp);
1910 		}
1911 	}
1912 
1913 	ant_cfg.tx_ant = tx_ant;
1914 	ant_cfg.rx_ant = rx_ant;
1915 
1916 	return mwifiex_send_cmd(priv, HostCmd_CMD_RF_ANTENNA,
1917 				HostCmd_ACT_GEN_SET, 0, &ant_cfg, true);
1918 }
1919 
1920 static int
mwifiex_cfg80211_get_antenna(struct wiphy * wiphy,u32 * tx_ant,u32 * rx_ant)1921 mwifiex_cfg80211_get_antenna(struct wiphy *wiphy, u32 *tx_ant, u32 *rx_ant)
1922 {
1923 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1924 	struct mwifiex_private *priv = mwifiex_get_priv(adapter,
1925 							MWIFIEX_BSS_ROLE_ANY);
1926 	mwifiex_send_cmd(priv, HostCmd_CMD_RF_ANTENNA,
1927 			 HostCmd_ACT_GEN_GET, 0, NULL, true);
1928 
1929 	*tx_ant = priv->tx_ant;
1930 	*rx_ant = priv->rx_ant;
1931 
1932 	return 0;
1933 }
1934 
1935 /* cfg80211 operation handler for stop ap.
1936  * Function stops BSS running at uAP interface.
1937  */
mwifiex_cfg80211_stop_ap(struct wiphy * wiphy,struct net_device * dev)1938 static int mwifiex_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
1939 {
1940 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1941 
1942 	mwifiex_abort_cac(priv);
1943 
1944 	if (mwifiex_del_mgmt_ies(priv))
1945 		mwifiex_dbg(priv->adapter, ERROR,
1946 			    "Failed to delete mgmt IEs!\n");
1947 
1948 	priv->ap_11n_enabled = 0;
1949 	memset(&priv->bss_cfg, 0, sizeof(priv->bss_cfg));
1950 
1951 	if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_BSS_STOP,
1952 			     HostCmd_ACT_GEN_SET, 0, NULL, true)) {
1953 		mwifiex_dbg(priv->adapter, ERROR,
1954 			    "Failed to stop the BSS\n");
1955 		return -1;
1956 	}
1957 
1958 	if (mwifiex_send_cmd(priv, HOST_CMD_APCMD_SYS_RESET,
1959 			     HostCmd_ACT_GEN_SET, 0, NULL, true)) {
1960 		mwifiex_dbg(priv->adapter, ERROR,
1961 			    "Failed to reset BSS\n");
1962 		return -1;
1963 	}
1964 
1965 	if (netif_carrier_ok(priv->netdev))
1966 		netif_carrier_off(priv->netdev);
1967 	mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
1968 
1969 	return 0;
1970 }
1971 
1972 /* cfg80211 operation handler for start_ap.
1973  * Function sets beacon period, DTIM period, SSID and security into
1974  * AP config structure.
1975  * AP is configured with these settings and BSS is started.
1976  */
mwifiex_cfg80211_start_ap(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ap_settings * params)1977 static int mwifiex_cfg80211_start_ap(struct wiphy *wiphy,
1978 				     struct net_device *dev,
1979 				     struct cfg80211_ap_settings *params)
1980 {
1981 	struct mwifiex_uap_bss_param *bss_cfg;
1982 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1983 
1984 	if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP)
1985 		return -1;
1986 
1987 	bss_cfg = kzalloc(sizeof(struct mwifiex_uap_bss_param), GFP_KERNEL);
1988 	if (!bss_cfg)
1989 		return -ENOMEM;
1990 
1991 	mwifiex_set_sys_config_invalid_data(bss_cfg);
1992 
1993 	if (params->beacon_interval)
1994 		bss_cfg->beacon_period = params->beacon_interval;
1995 	if (params->dtim_period)
1996 		bss_cfg->dtim_period = params->dtim_period;
1997 
1998 	if (params->ssid && params->ssid_len) {
1999 		memcpy(bss_cfg->ssid.ssid, params->ssid, params->ssid_len);
2000 		bss_cfg->ssid.ssid_len = params->ssid_len;
2001 	}
2002 	if (params->inactivity_timeout > 0) {
2003 		/* sta_ao_timer/ps_sta_ao_timer is in unit of 100ms */
2004 		bss_cfg->sta_ao_timer = 10 * params->inactivity_timeout;
2005 		bss_cfg->ps_sta_ao_timer = 10 * params->inactivity_timeout;
2006 	}
2007 
2008 	switch (params->hidden_ssid) {
2009 	case NL80211_HIDDEN_SSID_NOT_IN_USE:
2010 		bss_cfg->bcast_ssid_ctl = 1;
2011 		break;
2012 	case NL80211_HIDDEN_SSID_ZERO_LEN:
2013 		bss_cfg->bcast_ssid_ctl = 0;
2014 		break;
2015 	case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
2016 		bss_cfg->bcast_ssid_ctl = 2;
2017 		break;
2018 	default:
2019 		kfree(bss_cfg);
2020 		return -EINVAL;
2021 	}
2022 
2023 	mwifiex_uap_set_channel(priv, bss_cfg, params->chandef);
2024 	mwifiex_set_uap_rates(bss_cfg, params);
2025 
2026 	if (mwifiex_set_secure_params(priv, bss_cfg, params)) {
2027 		mwifiex_dbg(priv->adapter, ERROR,
2028 			    "Failed to parse security parameters!\n");
2029 		goto out;
2030 	}
2031 
2032 	mwifiex_set_ht_params(priv, bss_cfg, params);
2033 
2034 	if (priv->adapter->is_hw_11ac_capable) {
2035 		mwifiex_set_vht_params(priv, bss_cfg, params);
2036 		mwifiex_set_vht_width(priv, params->chandef.width,
2037 				      priv->ap_11ac_enabled);
2038 	}
2039 
2040 	if (priv->ap_11ac_enabled)
2041 		mwifiex_set_11ac_ba_params(priv);
2042 	else
2043 		mwifiex_set_ba_params(priv);
2044 
2045 	mwifiex_set_wmm_params(priv, bss_cfg, params);
2046 
2047 	if (mwifiex_is_11h_active(priv))
2048 		mwifiex_set_tpc_params(priv, bss_cfg, params);
2049 
2050 	if (mwifiex_is_11h_active(priv) &&
2051 	    !cfg80211_chandef_dfs_required(wiphy, &params->chandef,
2052 					   priv->bss_mode)) {
2053 		mwifiex_dbg(priv->adapter, INFO,
2054 			    "Disable 11h extensions in FW\n");
2055 		if (mwifiex_11h_activate(priv, false)) {
2056 			mwifiex_dbg(priv->adapter, ERROR,
2057 				    "Failed to disable 11h extensions!!");
2058 			goto out;
2059 		}
2060 		priv->state_11h.is_11h_active = false;
2061 	}
2062 
2063 	mwifiex_config_uap_11d(priv, &params->beacon);
2064 
2065 	if (mwifiex_config_start_uap(priv, bss_cfg)) {
2066 		mwifiex_dbg(priv->adapter, ERROR,
2067 			    "Failed to start AP\n");
2068 		goto out;
2069 	}
2070 
2071 	if (mwifiex_set_mgmt_ies(priv, &params->beacon))
2072 		goto out;
2073 
2074 	if (!netif_carrier_ok(priv->netdev))
2075 		netif_carrier_on(priv->netdev);
2076 	mwifiex_wake_up_net_dev_queue(priv->netdev, priv->adapter);
2077 
2078 	memcpy(&priv->bss_cfg, bss_cfg, sizeof(priv->bss_cfg));
2079 	kfree(bss_cfg);
2080 	return 0;
2081 
2082 out:
2083 	kfree(bss_cfg);
2084 	return -1;
2085 }
2086 
2087 /*
2088  * CFG802.11 operation handler for disconnection request.
2089  *
2090  * This function does not work when there is already a disconnection
2091  * procedure going on.
2092  */
2093 static int
mwifiex_cfg80211_disconnect(struct wiphy * wiphy,struct net_device * dev,u16 reason_code)2094 mwifiex_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
2095 			    u16 reason_code)
2096 {
2097 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2098 
2099 	if (!mwifiex_stop_bg_scan(priv))
2100 		cfg80211_sched_scan_stopped_locked(priv->wdev.wiphy, 0);
2101 
2102 	if (mwifiex_deauthenticate(priv, NULL))
2103 		return -EFAULT;
2104 
2105 	eth_zero_addr(priv->cfg_bssid);
2106 	priv->hs2_enabled = false;
2107 
2108 	return 0;
2109 }
2110 
2111 /*
2112  * This function informs the CFG802.11 subsystem of a new IBSS.
2113  *
2114  * The following information are sent to the CFG802.11 subsystem
2115  * to register the new IBSS. If we do not register the new IBSS,
2116  * a kernel panic will result.
2117  *      - SSID
2118  *      - SSID length
2119  *      - BSSID
2120  *      - Channel
2121  */
mwifiex_cfg80211_inform_ibss_bss(struct mwifiex_private * priv)2122 static int mwifiex_cfg80211_inform_ibss_bss(struct mwifiex_private *priv)
2123 {
2124 	struct ieee80211_channel *chan;
2125 	struct mwifiex_bss_info bss_info;
2126 	struct cfg80211_bss *bss;
2127 	int ie_len;
2128 	u8 ie_buf[IEEE80211_MAX_SSID_LEN + sizeof(struct ieee_types_header)];
2129 	enum nl80211_band band;
2130 
2131 	if (mwifiex_get_bss_info(priv, &bss_info))
2132 		return -1;
2133 
2134 	ie_buf[0] = WLAN_EID_SSID;
2135 	ie_buf[1] = bss_info.ssid.ssid_len;
2136 
2137 	memcpy(&ie_buf[sizeof(struct ieee_types_header)],
2138 	       &bss_info.ssid.ssid, bss_info.ssid.ssid_len);
2139 	ie_len = ie_buf[1] + sizeof(struct ieee_types_header);
2140 
2141 	band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
2142 	chan = ieee80211_get_channel(priv->wdev.wiphy,
2143 			ieee80211_channel_to_frequency(bss_info.bss_chan,
2144 						       band));
2145 
2146 	bss = cfg80211_inform_bss(priv->wdev.wiphy, chan,
2147 				  CFG80211_BSS_FTYPE_UNKNOWN,
2148 				  bss_info.bssid, 0, WLAN_CAPABILITY_IBSS,
2149 				  0, ie_buf, ie_len, 0, GFP_KERNEL);
2150 	if (bss) {
2151 		cfg80211_put_bss(priv->wdev.wiphy, bss);
2152 		ether_addr_copy(priv->cfg_bssid, bss_info.bssid);
2153 	}
2154 
2155 	return 0;
2156 }
2157 
2158 /*
2159  * This function connects with a BSS.
2160  *
2161  * This function handles both Infra and Ad-Hoc modes. It also performs
2162  * validity checking on the provided parameters, disconnects from the
2163  * current BSS (if any), sets up the association/scan parameters,
2164  * including security settings, and performs specific SSID scan before
2165  * trying to connect.
2166  *
2167  * For Infra mode, the function returns failure if the specified SSID
2168  * is not found in scan table. However, for Ad-Hoc mode, it can create
2169  * the IBSS if it does not exist. On successful completion in either case,
2170  * the function notifies the CFG802.11 subsystem of the new BSS connection.
2171  */
2172 static int
mwifiex_cfg80211_assoc(struct mwifiex_private * priv,size_t ssid_len,const u8 * ssid,const u8 * bssid,int mode,struct ieee80211_channel * channel,struct cfg80211_connect_params * sme,bool privacy,struct cfg80211_bss ** sel_bss)2173 mwifiex_cfg80211_assoc(struct mwifiex_private *priv, size_t ssid_len,
2174 		       const u8 *ssid, const u8 *bssid, int mode,
2175 		       struct ieee80211_channel *channel,
2176 		       struct cfg80211_connect_params *sme, bool privacy,
2177 		       struct cfg80211_bss **sel_bss)
2178 {
2179 	struct cfg80211_ssid req_ssid;
2180 	int ret, auth_type = 0;
2181 	struct cfg80211_bss *bss = NULL;
2182 	u8 is_scanning_required = 0;
2183 
2184 	memset(&req_ssid, 0, sizeof(struct cfg80211_ssid));
2185 
2186 	req_ssid.ssid_len = ssid_len;
2187 	if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2188 		mwifiex_dbg(priv->adapter, ERROR, "invalid SSID - aborting\n");
2189 		return -EINVAL;
2190 	}
2191 
2192 	memcpy(req_ssid.ssid, ssid, ssid_len);
2193 	if (!req_ssid.ssid_len || req_ssid.ssid[0] < 0x20) {
2194 		mwifiex_dbg(priv->adapter, ERROR, "invalid SSID - aborting\n");
2195 		return -EINVAL;
2196 	}
2197 
2198 	/* As this is new association, clear locally stored
2199 	 * keys and security related flags */
2200 	priv->sec_info.wpa_enabled = false;
2201 	priv->sec_info.wpa2_enabled = false;
2202 	priv->wep_key_curr_index = 0;
2203 	priv->sec_info.encryption_mode = 0;
2204 	priv->sec_info.is_authtype_auto = 0;
2205 	ret = mwifiex_set_encode(priv, NULL, NULL, 0, 0, NULL, 1);
2206 
2207 	if (mode == NL80211_IFTYPE_ADHOC) {
2208 		u16 enable = true;
2209 
2210 		/* set ibss coalescing_status */
2211 		ret = mwifiex_send_cmd(
2212 				priv,
2213 				HostCmd_CMD_802_11_IBSS_COALESCING_STATUS,
2214 				HostCmd_ACT_GEN_SET, 0, &enable, true);
2215 		if (ret)
2216 			return ret;
2217 
2218 		/* "privacy" is set only for ad-hoc mode */
2219 		if (privacy) {
2220 			/*
2221 			 * Keep WLAN_CIPHER_SUITE_WEP104 for now so that
2222 			 * the firmware can find a matching network from the
2223 			 * scan. The cfg80211 does not give us the encryption
2224 			 * mode at this stage so just setting it to WEP here.
2225 			 */
2226 			priv->sec_info.encryption_mode =
2227 					WLAN_CIPHER_SUITE_WEP104;
2228 			priv->sec_info.authentication_mode =
2229 					NL80211_AUTHTYPE_OPEN_SYSTEM;
2230 		}
2231 
2232 		goto done;
2233 	}
2234 
2235 	/* Now handle infra mode. "sme" is valid for infra mode only */
2236 	if (sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
2237 		auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2238 		priv->sec_info.is_authtype_auto = 1;
2239 	} else {
2240 		auth_type = sme->auth_type;
2241 	}
2242 
2243 	if (sme->crypto.n_ciphers_pairwise) {
2244 		priv->sec_info.encryption_mode =
2245 						sme->crypto.ciphers_pairwise[0];
2246 		priv->sec_info.authentication_mode = auth_type;
2247 	}
2248 
2249 	if (sme->crypto.cipher_group) {
2250 		priv->sec_info.encryption_mode = sme->crypto.cipher_group;
2251 		priv->sec_info.authentication_mode = auth_type;
2252 	}
2253 	if (sme->ie)
2254 		ret = mwifiex_set_gen_ie(priv, sme->ie, sme->ie_len);
2255 
2256 	if (sme->key) {
2257 		if (mwifiex_is_alg_wep(priv->sec_info.encryption_mode)) {
2258 			mwifiex_dbg(priv->adapter, INFO,
2259 				    "info: setting wep encryption\t"
2260 				    "with key len %d\n", sme->key_len);
2261 			priv->wep_key_curr_index = sme->key_idx;
2262 			ret = mwifiex_set_encode(priv, NULL, sme->key,
2263 						 sme->key_len, sme->key_idx,
2264 						 NULL, 0);
2265 		}
2266 	}
2267 done:
2268 	/*
2269 	 * Scan entries are valid for some time (15 sec). So we can save one
2270 	 * active scan time if we just try cfg80211_get_bss first. If it fails
2271 	 * then request scan and cfg80211_get_bss() again for final output.
2272 	 */
2273 	while (1) {
2274 		if (is_scanning_required) {
2275 			/* Do specific SSID scanning */
2276 			if (mwifiex_request_scan(priv, &req_ssid)) {
2277 				mwifiex_dbg(priv->adapter, ERROR, "scan error\n");
2278 				return -EFAULT;
2279 			}
2280 		}
2281 
2282 		/* Find the BSS we want using available scan results */
2283 		if (mode == NL80211_IFTYPE_ADHOC)
2284 			bss = cfg80211_get_bss(priv->wdev.wiphy, channel,
2285 					       bssid, ssid, ssid_len,
2286 					       IEEE80211_BSS_TYPE_IBSS,
2287 					       IEEE80211_PRIVACY_ANY);
2288 		else
2289 			bss = cfg80211_get_bss(priv->wdev.wiphy, channel,
2290 					       bssid, ssid, ssid_len,
2291 					       IEEE80211_BSS_TYPE_ESS,
2292 					       IEEE80211_PRIVACY_ANY);
2293 
2294 		if (!bss) {
2295 			if (is_scanning_required) {
2296 				mwifiex_dbg(priv->adapter, MSG,
2297 					    "assoc: requested bss not found in scan results\n");
2298 				break;
2299 			}
2300 			is_scanning_required = 1;
2301 		} else {
2302 			mwifiex_dbg(priv->adapter, MSG,
2303 				    "info: trying to associate to bssid %pM\n",
2304 				    bss->bssid);
2305 			memcpy(&priv->cfg_bssid, bss->bssid, ETH_ALEN);
2306 			break;
2307 		}
2308 	}
2309 
2310 	if (bss)
2311 		cfg80211_ref_bss(priv->adapter->wiphy, bss);
2312 
2313 	ret = mwifiex_bss_start(priv, bss, &req_ssid);
2314 	if (ret)
2315 		goto cleanup;
2316 
2317 	if (mode == NL80211_IFTYPE_ADHOC) {
2318 		/* Inform the BSS information to kernel, otherwise
2319 		 * kernel will give a panic after successful assoc */
2320 		if (mwifiex_cfg80211_inform_ibss_bss(priv)) {
2321 			ret = -EFAULT;
2322 			goto cleanup;
2323 		}
2324 	}
2325 
2326 	/* Pass the selected BSS entry to caller. */
2327 	if (sel_bss) {
2328 		*sel_bss = bss;
2329 		bss = NULL;
2330 	}
2331 
2332 cleanup:
2333 	if (bss)
2334 		cfg80211_put_bss(priv->adapter->wiphy, bss);
2335 	return ret;
2336 }
2337 
2338 /*
2339  * CFG802.11 operation handler for association request.
2340  *
2341  * This function does not work when the current mode is set to Ad-Hoc, or
2342  * when there is already an association procedure going on. The given BSS
2343  * information is used to associate.
2344  */
2345 static int
mwifiex_cfg80211_connect(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_connect_params * sme)2346 mwifiex_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
2347 			 struct cfg80211_connect_params *sme)
2348 {
2349 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2350 	struct mwifiex_adapter *adapter = priv->adapter;
2351 	struct cfg80211_bss *bss = NULL;
2352 	int ret;
2353 
2354 	if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA) {
2355 		mwifiex_dbg(adapter, ERROR,
2356 			    "%s: reject infra assoc request in non-STA role\n",
2357 			    dev->name);
2358 		return -EINVAL;
2359 	}
2360 
2361 	if (priv->wdev.current_bss) {
2362 		mwifiex_dbg(adapter, ERROR,
2363 			    "%s: already connected\n", dev->name);
2364 		return -EALREADY;
2365 	}
2366 
2367 	if (priv->scan_block)
2368 		priv->scan_block = false;
2369 
2370 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags) ||
2371 	    test_bit(MWIFIEX_IS_CMD_TIMEDOUT, &adapter->work_flags)) {
2372 		mwifiex_dbg(adapter, ERROR,
2373 			    "%s: Ignore connection.\t"
2374 			    "Card removed or FW in bad state\n",
2375 			    dev->name);
2376 		return -EFAULT;
2377 	}
2378 
2379 	mwifiex_dbg(adapter, INFO,
2380 		    "info: Trying to associate to bssid %pM\n", sme->bssid);
2381 
2382 	if (!mwifiex_stop_bg_scan(priv))
2383 		cfg80211_sched_scan_stopped_locked(priv->wdev.wiphy, 0);
2384 
2385 	ret = mwifiex_cfg80211_assoc(priv, sme->ssid_len, sme->ssid, sme->bssid,
2386 				     priv->bss_mode, sme->channel, sme, 0,
2387 				     &bss);
2388 	if (!ret) {
2389 		cfg80211_connect_bss(priv->netdev, priv->cfg_bssid, bss, NULL,
2390 				     0, NULL, 0, WLAN_STATUS_SUCCESS,
2391 				     GFP_KERNEL, NL80211_TIMEOUT_UNSPECIFIED);
2392 		mwifiex_dbg(priv->adapter, MSG,
2393 			    "info: associated to bssid %pM successfully\n",
2394 			    priv->cfg_bssid);
2395 		if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
2396 		    priv->adapter->auto_tdls &&
2397 		    priv->bss_type == MWIFIEX_BSS_TYPE_STA)
2398 			mwifiex_setup_auto_tdls_timer(priv);
2399 	} else {
2400 		mwifiex_dbg(priv->adapter, ERROR,
2401 			    "info: association to bssid %pM failed\n",
2402 			    priv->cfg_bssid);
2403 		eth_zero_addr(priv->cfg_bssid);
2404 
2405 		if (ret > 0)
2406 			cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
2407 						NULL, 0, NULL, 0, ret,
2408 						GFP_KERNEL);
2409 		else
2410 			cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
2411 						NULL, 0, NULL, 0,
2412 						WLAN_STATUS_UNSPECIFIED_FAILURE,
2413 						GFP_KERNEL);
2414 	}
2415 
2416 	return 0;
2417 }
2418 
2419 /*
2420  * This function sets following parameters for ibss network.
2421  *  -  channel
2422  *  -  start band
2423  *  -  11n flag
2424  *  -  secondary channel offset
2425  */
mwifiex_set_ibss_params(struct mwifiex_private * priv,struct cfg80211_ibss_params * params)2426 static int mwifiex_set_ibss_params(struct mwifiex_private *priv,
2427 				   struct cfg80211_ibss_params *params)
2428 {
2429 	struct mwifiex_adapter *adapter = priv->adapter;
2430 	int index = 0, i;
2431 	u8 config_bands = 0;
2432 
2433 	if (params->chandef.chan->band == NL80211_BAND_2GHZ) {
2434 		if (!params->basic_rates) {
2435 			config_bands = BAND_B | BAND_G;
2436 		} else {
2437 			for (i = 0; i < mwifiex_band_2ghz.n_bitrates; i++) {
2438 				/*
2439 				 * Rates below 6 Mbps in the table are CCK
2440 				 * rates; 802.11b and from 6 they are OFDM;
2441 				 * 802.11G
2442 				 */
2443 				if (mwifiex_rates[i].bitrate == 60) {
2444 					index = 1 << i;
2445 					break;
2446 				}
2447 			}
2448 
2449 			if (params->basic_rates < index) {
2450 				config_bands = BAND_B;
2451 			} else {
2452 				config_bands = BAND_G;
2453 				if (params->basic_rates % index)
2454 					config_bands |= BAND_B;
2455 			}
2456 		}
2457 
2458 		if (cfg80211_get_chandef_type(&params->chandef) !=
2459 						NL80211_CHAN_NO_HT)
2460 			config_bands |= BAND_G | BAND_GN;
2461 	} else {
2462 		if (cfg80211_get_chandef_type(&params->chandef) ==
2463 						NL80211_CHAN_NO_HT)
2464 			config_bands = BAND_A;
2465 		else
2466 			config_bands = BAND_AN | BAND_A;
2467 	}
2468 
2469 	if (!((config_bands | adapter->fw_bands) & ~adapter->fw_bands)) {
2470 		adapter->config_bands = config_bands;
2471 		adapter->adhoc_start_band = config_bands;
2472 
2473 		if ((config_bands & BAND_GN) || (config_bands & BAND_AN))
2474 			adapter->adhoc_11n_enabled = true;
2475 		else
2476 			adapter->adhoc_11n_enabled = false;
2477 	}
2478 
2479 	adapter->sec_chan_offset =
2480 		mwifiex_chan_type_to_sec_chan_offset(
2481 			cfg80211_get_chandef_type(&params->chandef));
2482 	priv->adhoc_channel = ieee80211_frequency_to_channel(
2483 				params->chandef.chan->center_freq);
2484 
2485 	mwifiex_dbg(adapter, INFO,
2486 		    "info: set ibss band %d, chan %d, chan offset %d\n",
2487 		    config_bands, priv->adhoc_channel,
2488 		    adapter->sec_chan_offset);
2489 
2490 	return 0;
2491 }
2492 
2493 /*
2494  * CFG802.11 operation handler to join an IBSS.
2495  *
2496  * This function does not work in any mode other than Ad-Hoc, or if
2497  * a join operation is already in progress.
2498  */
2499 static int
mwifiex_cfg80211_join_ibss(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ibss_params * params)2500 mwifiex_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2501 			   struct cfg80211_ibss_params *params)
2502 {
2503 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2504 	int ret = 0;
2505 
2506 	if (priv->bss_mode != NL80211_IFTYPE_ADHOC) {
2507 		mwifiex_dbg(priv->adapter, ERROR,
2508 			    "request to join ibss received\t"
2509 			    "when station is not in ibss mode\n");
2510 		goto done;
2511 	}
2512 
2513 	mwifiex_dbg(priv->adapter, MSG, "info: trying to join to bssid %pM\n",
2514 		    params->bssid);
2515 
2516 	mwifiex_set_ibss_params(priv, params);
2517 
2518 	ret = mwifiex_cfg80211_assoc(priv, params->ssid_len, params->ssid,
2519 				     params->bssid, priv->bss_mode,
2520 				     params->chandef.chan, NULL,
2521 				     params->privacy, NULL);
2522 done:
2523 	if (!ret) {
2524 		cfg80211_ibss_joined(priv->netdev, priv->cfg_bssid,
2525 				     params->chandef.chan, GFP_KERNEL);
2526 		mwifiex_dbg(priv->adapter, MSG,
2527 			    "info: joined/created adhoc network with bssid\t"
2528 			    "%pM successfully\n", priv->cfg_bssid);
2529 	} else {
2530 		mwifiex_dbg(priv->adapter, ERROR,
2531 			    "info: failed creating/joining adhoc network\n");
2532 	}
2533 
2534 	return ret;
2535 }
2536 
2537 /*
2538  * CFG802.11 operation handler to leave an IBSS.
2539  *
2540  * This function does not work if a leave operation is
2541  * already in progress.
2542  */
2543 static int
mwifiex_cfg80211_leave_ibss(struct wiphy * wiphy,struct net_device * dev)2544 mwifiex_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2545 {
2546 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2547 
2548 	mwifiex_dbg(priv->adapter, MSG, "info: disconnecting from essid %pM\n",
2549 		    priv->cfg_bssid);
2550 	if (mwifiex_deauthenticate(priv, NULL))
2551 		return -EFAULT;
2552 
2553 	eth_zero_addr(priv->cfg_bssid);
2554 
2555 	return 0;
2556 }
2557 
2558 /*
2559  * CFG802.11 operation handler for scan request.
2560  *
2561  * This function issues a scan request to the firmware based upon
2562  * the user specified scan configuration. On successful completion,
2563  * it also informs the results.
2564  */
2565 static int
mwifiex_cfg80211_scan(struct wiphy * wiphy,struct cfg80211_scan_request * request)2566 mwifiex_cfg80211_scan(struct wiphy *wiphy,
2567 		      struct cfg80211_scan_request *request)
2568 {
2569 	struct net_device *dev = request->wdev->netdev;
2570 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2571 	int i, offset, ret;
2572 	struct ieee80211_channel *chan;
2573 	struct ieee_types_header *ie;
2574 	struct mwifiex_user_scan_cfg *user_scan_cfg;
2575 	u8 mac_addr[ETH_ALEN];
2576 
2577 	mwifiex_dbg(priv->adapter, CMD,
2578 		    "info: received scan request on %s\n", dev->name);
2579 
2580 	/* Block scan request if scan operation or scan cleanup when interface
2581 	 * is disabled is in process
2582 	 */
2583 	if (priv->scan_request || priv->scan_aborting) {
2584 		mwifiex_dbg(priv->adapter, WARN,
2585 			    "cmd: Scan already in process..\n");
2586 		return -EBUSY;
2587 	}
2588 
2589 	if (!priv->wdev.current_bss && priv->scan_block)
2590 		priv->scan_block = false;
2591 
2592 	if (!mwifiex_stop_bg_scan(priv))
2593 		cfg80211_sched_scan_stopped_locked(priv->wdev.wiphy, 0);
2594 
2595 	user_scan_cfg = kzalloc(sizeof(*user_scan_cfg), GFP_KERNEL);
2596 	if (!user_scan_cfg)
2597 		return -ENOMEM;
2598 
2599 	priv->scan_request = request;
2600 
2601 	if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
2602 		get_random_mask_addr(mac_addr, request->mac_addr,
2603 				     request->mac_addr_mask);
2604 		ether_addr_copy(request->mac_addr, mac_addr);
2605 		ether_addr_copy(user_scan_cfg->random_mac, mac_addr);
2606 	}
2607 
2608 	user_scan_cfg->num_ssids = request->n_ssids;
2609 	user_scan_cfg->ssid_list = request->ssids;
2610 
2611 	if (request->ie && request->ie_len) {
2612 		offset = 0;
2613 		for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2614 			if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
2615 				continue;
2616 			priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_SCAN;
2617 			ie = (struct ieee_types_header *)(request->ie + offset);
2618 			memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
2619 			offset += sizeof(*ie) + ie->len;
2620 
2621 			if (offset >= request->ie_len)
2622 				break;
2623 		}
2624 	}
2625 
2626 	for (i = 0; i < min_t(u32, request->n_channels,
2627 			      MWIFIEX_USER_SCAN_CHAN_MAX); i++) {
2628 		chan = request->channels[i];
2629 		user_scan_cfg->chan_list[i].chan_number = chan->hw_value;
2630 		user_scan_cfg->chan_list[i].radio_type = chan->band;
2631 
2632 		if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2633 			user_scan_cfg->chan_list[i].scan_type =
2634 						MWIFIEX_SCAN_TYPE_PASSIVE;
2635 		else
2636 			user_scan_cfg->chan_list[i].scan_type =
2637 						MWIFIEX_SCAN_TYPE_ACTIVE;
2638 
2639 		user_scan_cfg->chan_list[i].scan_time = 0;
2640 	}
2641 
2642 	if (priv->adapter->scan_chan_gap_enabled &&
2643 	    mwifiex_is_any_intf_active(priv))
2644 		user_scan_cfg->scan_chan_gap =
2645 					      priv->adapter->scan_chan_gap_time;
2646 
2647 	ret = mwifiex_scan_networks(priv, user_scan_cfg);
2648 	kfree(user_scan_cfg);
2649 	if (ret) {
2650 		mwifiex_dbg(priv->adapter, ERROR,
2651 			    "scan failed: %d\n", ret);
2652 		priv->scan_aborting = false;
2653 		priv->scan_request = NULL;
2654 		return ret;
2655 	}
2656 
2657 	if (request->ie && request->ie_len) {
2658 		for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2659 			if (priv->vs_ie[i].mask == MWIFIEX_VSIE_MASK_SCAN) {
2660 				priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_CLEAR;
2661 				memset(&priv->vs_ie[i].ie, 0,
2662 				       MWIFIEX_MAX_VSIE_LEN);
2663 			}
2664 		}
2665 	}
2666 	return 0;
2667 }
2668 
2669 /* CFG802.11 operation handler for sched_scan_start.
2670  *
2671  * This function issues a bgscan config request to the firmware based upon
2672  * the user specified sched_scan configuration. On successful completion,
2673  * firmware will generate BGSCAN_REPORT event, driver should issue bgscan
2674  * query command to get sched_scan results from firmware.
2675  */
2676 static int
mwifiex_cfg80211_sched_scan_start(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_sched_scan_request * request)2677 mwifiex_cfg80211_sched_scan_start(struct wiphy *wiphy,
2678 				  struct net_device *dev,
2679 				  struct cfg80211_sched_scan_request *request)
2680 {
2681 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2682 	int i, offset;
2683 	struct ieee80211_channel *chan;
2684 	struct mwifiex_bg_scan_cfg *bgscan_cfg;
2685 	struct ieee_types_header *ie;
2686 
2687 	if (!request || (!request->n_ssids && !request->n_match_sets)) {
2688 		wiphy_err(wiphy, "%s : Invalid Sched_scan parameters",
2689 			  __func__);
2690 		return -EINVAL;
2691 	}
2692 
2693 	wiphy_info(wiphy, "sched_scan start : n_ssids=%d n_match_sets=%d ",
2694 		   request->n_ssids, request->n_match_sets);
2695 	wiphy_info(wiphy, "n_channels=%d interval=%d ie_len=%d\n",
2696 		   request->n_channels, request->scan_plans->interval,
2697 		   (int)request->ie_len);
2698 
2699 	bgscan_cfg = kzalloc(sizeof(*bgscan_cfg), GFP_KERNEL);
2700 	if (!bgscan_cfg)
2701 		return -ENOMEM;
2702 
2703 	if (priv->scan_request || priv->scan_aborting)
2704 		bgscan_cfg->start_later = true;
2705 
2706 	bgscan_cfg->num_ssids = request->n_match_sets;
2707 	bgscan_cfg->ssid_list = request->match_sets;
2708 
2709 	if (request->ie && request->ie_len) {
2710 		offset = 0;
2711 		for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2712 			if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
2713 				continue;
2714 			priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_BGSCAN;
2715 			ie = (struct ieee_types_header *)(request->ie + offset);
2716 			memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
2717 			offset += sizeof(*ie) + ie->len;
2718 
2719 			if (offset >= request->ie_len)
2720 				break;
2721 		}
2722 	}
2723 
2724 	for (i = 0; i < min_t(u32, request->n_channels,
2725 			      MWIFIEX_BG_SCAN_CHAN_MAX); i++) {
2726 		chan = request->channels[i];
2727 		bgscan_cfg->chan_list[i].chan_number = chan->hw_value;
2728 		bgscan_cfg->chan_list[i].radio_type = chan->band;
2729 
2730 		if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2731 			bgscan_cfg->chan_list[i].scan_type =
2732 						MWIFIEX_SCAN_TYPE_PASSIVE;
2733 		else
2734 			bgscan_cfg->chan_list[i].scan_type =
2735 						MWIFIEX_SCAN_TYPE_ACTIVE;
2736 
2737 		bgscan_cfg->chan_list[i].scan_time = 0;
2738 	}
2739 
2740 	bgscan_cfg->chan_per_scan = min_t(u32, request->n_channels,
2741 					  MWIFIEX_BG_SCAN_CHAN_MAX);
2742 
2743 	/* Use at least 15 second for per scan cycle */
2744 	bgscan_cfg->scan_interval = (request->scan_plans->interval >
2745 				     MWIFIEX_BGSCAN_INTERVAL) ?
2746 				request->scan_plans->interval :
2747 				MWIFIEX_BGSCAN_INTERVAL;
2748 
2749 	bgscan_cfg->repeat_count = MWIFIEX_BGSCAN_REPEAT_COUNT;
2750 	bgscan_cfg->report_condition = MWIFIEX_BGSCAN_SSID_MATCH |
2751 				MWIFIEX_BGSCAN_WAIT_ALL_CHAN_DONE;
2752 	bgscan_cfg->bss_type = MWIFIEX_BSS_MODE_INFRA;
2753 	bgscan_cfg->action = MWIFIEX_BGSCAN_ACT_SET;
2754 	bgscan_cfg->enable = true;
2755 	if (request->min_rssi_thold != NL80211_SCAN_RSSI_THOLD_OFF) {
2756 		bgscan_cfg->report_condition |= MWIFIEX_BGSCAN_SSID_RSSI_MATCH;
2757 		bgscan_cfg->rssi_threshold = request->min_rssi_thold;
2758 	}
2759 
2760 	if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11_BG_SCAN_CONFIG,
2761 			     HostCmd_ACT_GEN_SET, 0, bgscan_cfg, true)) {
2762 		kfree(bgscan_cfg);
2763 		return -EFAULT;
2764 	}
2765 
2766 	priv->sched_scanning = true;
2767 
2768 	kfree(bgscan_cfg);
2769 	return 0;
2770 }
2771 
2772 /* CFG802.11 operation handler for sched_scan_stop.
2773  *
2774  * This function issues a bgscan config command to disable
2775  * previous bgscan configuration in the firmware
2776  */
mwifiex_cfg80211_sched_scan_stop(struct wiphy * wiphy,struct net_device * dev,u64 reqid)2777 static int mwifiex_cfg80211_sched_scan_stop(struct wiphy *wiphy,
2778 					    struct net_device *dev, u64 reqid)
2779 {
2780 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2781 
2782 	wiphy_info(wiphy, "sched scan stop!");
2783 	mwifiex_stop_bg_scan(priv);
2784 
2785 	return 0;
2786 }
2787 
mwifiex_setup_vht_caps(struct ieee80211_sta_vht_cap * vht_info,struct mwifiex_private * priv)2788 static void mwifiex_setup_vht_caps(struct ieee80211_sta_vht_cap *vht_info,
2789 				   struct mwifiex_private *priv)
2790 {
2791 	struct mwifiex_adapter *adapter = priv->adapter;
2792 
2793 	vht_info->vht_supported = true;
2794 
2795 	vht_info->cap = adapter->hw_dot_11ac_dev_cap;
2796 	/* Update MCS support for VHT */
2797 	vht_info->vht_mcs.rx_mcs_map = cpu_to_le16(
2798 				adapter->hw_dot_11ac_mcs_support & 0xFFFF);
2799 	vht_info->vht_mcs.rx_highest = 0;
2800 	vht_info->vht_mcs.tx_mcs_map = cpu_to_le16(
2801 				adapter->hw_dot_11ac_mcs_support >> 16);
2802 	vht_info->vht_mcs.tx_highest = 0;
2803 }
2804 
2805 /*
2806  * This function sets up the CFG802.11 specific HT capability fields
2807  * with default values.
2808  *
2809  * The following default values are set -
2810  *      - HT Supported = True
2811  *      - Maximum AMPDU length factor = IEEE80211_HT_MAX_AMPDU_64K
2812  *      - Minimum AMPDU spacing = IEEE80211_HT_MPDU_DENSITY_NONE
2813  *      - HT Capabilities supported by firmware
2814  *      - MCS information, Rx mask = 0xff
2815  *      - MCD information, Tx parameters = IEEE80211_HT_MCS_TX_DEFINED (0x01)
2816  */
2817 static void
mwifiex_setup_ht_caps(struct ieee80211_sta_ht_cap * ht_info,struct mwifiex_private * priv)2818 mwifiex_setup_ht_caps(struct ieee80211_sta_ht_cap *ht_info,
2819 		      struct mwifiex_private *priv)
2820 {
2821 	int rx_mcs_supp;
2822 	struct ieee80211_mcs_info mcs_set;
2823 	u8 *mcs = (u8 *)&mcs_set;
2824 	struct mwifiex_adapter *adapter = priv->adapter;
2825 
2826 	ht_info->ht_supported = true;
2827 	ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2828 	ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2829 
2830 	memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
2831 
2832 	/* Fill HT capability information */
2833 	if (ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2834 		ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2835 	else
2836 		ht_info->cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2837 
2838 	if (ISSUPP_SHORTGI20(adapter->hw_dot_11n_dev_cap))
2839 		ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
2840 	else
2841 		ht_info->cap &= ~IEEE80211_HT_CAP_SGI_20;
2842 
2843 	if (ISSUPP_SHORTGI40(adapter->hw_dot_11n_dev_cap))
2844 		ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
2845 	else
2846 		ht_info->cap &= ~IEEE80211_HT_CAP_SGI_40;
2847 
2848 	if (adapter->user_dev_mcs_support == HT_STREAM_2X2)
2849 		ht_info->cap |= 2 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2850 	else
2851 		ht_info->cap |= 1 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2852 
2853 	if (ISSUPP_TXSTBC(adapter->hw_dot_11n_dev_cap))
2854 		ht_info->cap |= IEEE80211_HT_CAP_TX_STBC;
2855 	else
2856 		ht_info->cap &= ~IEEE80211_HT_CAP_TX_STBC;
2857 
2858 	if (ISSUPP_GREENFIELD(adapter->hw_dot_11n_dev_cap))
2859 		ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
2860 	else
2861 		ht_info->cap &= ~IEEE80211_HT_CAP_GRN_FLD;
2862 
2863 	if (ISENABLED_40MHZ_INTOLERANT(adapter->hw_dot_11n_dev_cap))
2864 		ht_info->cap |= IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2865 	else
2866 		ht_info->cap &= ~IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2867 
2868 	if (ISSUPP_RXLDPC(adapter->hw_dot_11n_dev_cap))
2869 		ht_info->cap |= IEEE80211_HT_CAP_LDPC_CODING;
2870 	else
2871 		ht_info->cap &= ~IEEE80211_HT_CAP_LDPC_CODING;
2872 
2873 	ht_info->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
2874 	ht_info->cap |= IEEE80211_HT_CAP_SM_PS;
2875 
2876 	rx_mcs_supp = GET_RXMCSSUPP(adapter->user_dev_mcs_support);
2877 	/* Set MCS for 1x1/2x2 */
2878 	memset(mcs, 0xff, rx_mcs_supp);
2879 	/* Clear all the other values */
2880 	memset(&mcs[rx_mcs_supp], 0,
2881 	       sizeof(struct ieee80211_mcs_info) - rx_mcs_supp);
2882 	if (priv->bss_mode == NL80211_IFTYPE_STATION ||
2883 	    ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2884 		/* Set MCS32 for infra mode or ad-hoc mode with 40MHz support */
2885 		SETHT_MCS32(mcs_set.rx_mask);
2886 
2887 	memcpy((u8 *) &ht_info->mcs, mcs, sizeof(struct ieee80211_mcs_info));
2888 
2889 	ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2890 }
2891 
2892 /*
2893  *  create a new virtual interface with the given name and name assign type
2894  */
mwifiex_add_virtual_intf(struct wiphy * wiphy,const char * name,unsigned char name_assign_type,enum nl80211_iftype type,struct vif_params * params)2895 struct wireless_dev *mwifiex_add_virtual_intf(struct wiphy *wiphy,
2896 					      const char *name,
2897 					      unsigned char name_assign_type,
2898 					      enum nl80211_iftype type,
2899 					      struct vif_params *params)
2900 {
2901 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
2902 	struct mwifiex_private *priv;
2903 	struct net_device *dev;
2904 	void *mdev_priv;
2905 	int ret;
2906 
2907 	if (!adapter)
2908 		return ERR_PTR(-EFAULT);
2909 
2910 	switch (type) {
2911 	case NL80211_IFTYPE_UNSPECIFIED:
2912 	case NL80211_IFTYPE_STATION:
2913 	case NL80211_IFTYPE_ADHOC:
2914 		if (adapter->curr_iface_comb.sta_intf ==
2915 		    adapter->iface_limit.sta_intf) {
2916 			mwifiex_dbg(adapter, ERROR,
2917 				    "cannot create multiple sta/adhoc ifaces\n");
2918 			return ERR_PTR(-EINVAL);
2919 		}
2920 
2921 		priv = mwifiex_get_unused_priv_by_bss_type(
2922 						adapter, MWIFIEX_BSS_TYPE_STA);
2923 		if (!priv) {
2924 			mwifiex_dbg(adapter, ERROR,
2925 				    "could not get free private struct\n");
2926 			return ERR_PTR(-EFAULT);
2927 		}
2928 
2929 		priv->wdev.wiphy = wiphy;
2930 		priv->wdev.iftype = NL80211_IFTYPE_STATION;
2931 
2932 		if (type == NL80211_IFTYPE_UNSPECIFIED)
2933 			priv->bss_mode = NL80211_IFTYPE_STATION;
2934 		else
2935 			priv->bss_mode = type;
2936 
2937 		priv->bss_type = MWIFIEX_BSS_TYPE_STA;
2938 		priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2939 		priv->bss_priority = 0;
2940 		priv->bss_role = MWIFIEX_BSS_ROLE_STA;
2941 
2942 		break;
2943 	case NL80211_IFTYPE_AP:
2944 		if (adapter->curr_iface_comb.uap_intf ==
2945 		    adapter->iface_limit.uap_intf) {
2946 			mwifiex_dbg(adapter, ERROR,
2947 				    "cannot create multiple AP ifaces\n");
2948 			return ERR_PTR(-EINVAL);
2949 		}
2950 
2951 		priv = mwifiex_get_unused_priv_by_bss_type(
2952 						adapter, MWIFIEX_BSS_TYPE_UAP);
2953 		if (!priv) {
2954 			mwifiex_dbg(adapter, ERROR,
2955 				    "could not get free private struct\n");
2956 			return ERR_PTR(-EFAULT);
2957 		}
2958 
2959 		priv->wdev.wiphy = wiphy;
2960 		priv->wdev.iftype = NL80211_IFTYPE_AP;
2961 
2962 		priv->bss_type = MWIFIEX_BSS_TYPE_UAP;
2963 		priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
2964 		priv->bss_priority = 0;
2965 		priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
2966 		priv->bss_started = 0;
2967 		priv->bss_mode = type;
2968 
2969 		break;
2970 	case NL80211_IFTYPE_P2P_CLIENT:
2971 		if (adapter->curr_iface_comb.p2p_intf ==
2972 		    adapter->iface_limit.p2p_intf) {
2973 			mwifiex_dbg(adapter, ERROR,
2974 				    "cannot create multiple P2P ifaces\n");
2975 			return ERR_PTR(-EINVAL);
2976 		}
2977 
2978 		priv = mwifiex_get_unused_priv_by_bss_type(
2979 						adapter, MWIFIEX_BSS_TYPE_P2P);
2980 		if (!priv) {
2981 			mwifiex_dbg(adapter, ERROR,
2982 				    "could not get free private struct\n");
2983 			return ERR_PTR(-EFAULT);
2984 		}
2985 
2986 		priv->wdev.wiphy = wiphy;
2987 		/* At start-up, wpa_supplicant tries to change the interface
2988 		 * to NL80211_IFTYPE_STATION if it is not managed mode.
2989 		 */
2990 		priv->wdev.iftype = NL80211_IFTYPE_P2P_CLIENT;
2991 		priv->bss_mode = NL80211_IFTYPE_P2P_CLIENT;
2992 
2993 		/* Setting bss_type to P2P tells firmware that this interface
2994 		 * is receiving P2P peers found during find phase and doing
2995 		 * action frame handshake.
2996 		 */
2997 		priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
2998 
2999 		priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
3000 		priv->bss_priority = MWIFIEX_BSS_ROLE_STA;
3001 		priv->bss_role = MWIFIEX_BSS_ROLE_STA;
3002 		priv->bss_started = 0;
3003 
3004 		if (mwifiex_cfg80211_init_p2p_client(priv)) {
3005 			memset(&priv->wdev, 0, sizeof(priv->wdev));
3006 			priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
3007 			return ERR_PTR(-EFAULT);
3008 		}
3009 
3010 		break;
3011 	default:
3012 		mwifiex_dbg(adapter, ERROR, "type not supported\n");
3013 		return ERR_PTR(-EINVAL);
3014 	}
3015 
3016 	dev = alloc_netdev_mqs(sizeof(struct mwifiex_private *), name,
3017 			       name_assign_type, ether_setup,
3018 			       IEEE80211_NUM_ACS, 1);
3019 	if (!dev) {
3020 		mwifiex_dbg(adapter, ERROR,
3021 			    "no memory available for netdevice\n");
3022 		ret = -ENOMEM;
3023 		goto err_alloc_netdev;
3024 	}
3025 
3026 	mwifiex_init_priv_params(priv, dev);
3027 
3028 	priv->netdev = dev;
3029 
3030 	if (!adapter->mfg_mode) {
3031 		mwifiex_set_mac_address(priv, dev, false, NULL);
3032 
3033 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
3034 				       HostCmd_ACT_GEN_SET, 0, NULL, true);
3035 		if (ret)
3036 			goto err_set_bss_mode;
3037 
3038 		ret = mwifiex_sta_init_cmd(priv, false, false);
3039 		if (ret)
3040 			goto err_sta_init;
3041 	}
3042 
3043 	mwifiex_setup_ht_caps(&wiphy->bands[NL80211_BAND_2GHZ]->ht_cap, priv);
3044 	if (adapter->is_hw_11ac_capable)
3045 		mwifiex_setup_vht_caps(
3046 			&wiphy->bands[NL80211_BAND_2GHZ]->vht_cap, priv);
3047 
3048 	if (adapter->config_bands & BAND_A)
3049 		mwifiex_setup_ht_caps(
3050 			&wiphy->bands[NL80211_BAND_5GHZ]->ht_cap, priv);
3051 
3052 	if ((adapter->config_bands & BAND_A) && adapter->is_hw_11ac_capable)
3053 		mwifiex_setup_vht_caps(
3054 			&wiphy->bands[NL80211_BAND_5GHZ]->vht_cap, priv);
3055 
3056 	dev_net_set(dev, wiphy_net(wiphy));
3057 	dev->ieee80211_ptr = &priv->wdev;
3058 	dev->ieee80211_ptr->iftype = priv->bss_mode;
3059 	SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
3060 
3061 	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
3062 	dev->watchdog_timeo = MWIFIEX_DEFAULT_WATCHDOG_TIMEOUT;
3063 	dev->needed_headroom = MWIFIEX_MIN_DATA_HEADER_LEN;
3064 	dev->ethtool_ops = &mwifiex_ethtool_ops;
3065 
3066 	mdev_priv = netdev_priv(dev);
3067 	*((unsigned long *) mdev_priv) = (unsigned long) priv;
3068 
3069 	SET_NETDEV_DEV(dev, adapter->dev);
3070 
3071 	priv->dfs_cac_workqueue = alloc_workqueue("MWIFIEX_DFS_CAC%s",
3072 						  WQ_HIGHPRI |
3073 						  WQ_MEM_RECLAIM |
3074 						  WQ_UNBOUND, 1, name);
3075 	if (!priv->dfs_cac_workqueue) {
3076 		mwifiex_dbg(adapter, ERROR, "cannot alloc DFS CAC queue\n");
3077 		ret = -ENOMEM;
3078 		goto err_alloc_cac;
3079 	}
3080 
3081 	INIT_DELAYED_WORK(&priv->dfs_cac_work, mwifiex_dfs_cac_work_queue);
3082 
3083 	priv->dfs_chan_sw_workqueue = alloc_workqueue("MWIFIEX_DFS_CHSW%s",
3084 						      WQ_HIGHPRI | WQ_UNBOUND |
3085 						      WQ_MEM_RECLAIM, 1, name);
3086 	if (!priv->dfs_chan_sw_workqueue) {
3087 		mwifiex_dbg(adapter, ERROR, "cannot alloc DFS channel sw queue\n");
3088 		ret = -ENOMEM;
3089 		goto err_alloc_chsw;
3090 	}
3091 
3092 	INIT_DELAYED_WORK(&priv->dfs_chan_sw_work,
3093 			  mwifiex_dfs_chan_sw_work_queue);
3094 
3095 	mutex_init(&priv->async_mutex);
3096 
3097 	/* Register network device */
3098 	if (cfg80211_register_netdevice(dev)) {
3099 		mwifiex_dbg(adapter, ERROR, "cannot register network device\n");
3100 		ret = -EFAULT;
3101 		goto err_reg_netdev;
3102 	}
3103 
3104 	mwifiex_dbg(adapter, INFO,
3105 		    "info: %s: Marvell 802.11 Adapter\n", dev->name);
3106 
3107 #ifdef CONFIG_DEBUG_FS
3108 	mwifiex_dev_debugfs_init(priv);
3109 #endif
3110 
3111 	switch (type) {
3112 	case NL80211_IFTYPE_UNSPECIFIED:
3113 	case NL80211_IFTYPE_STATION:
3114 	case NL80211_IFTYPE_ADHOC:
3115 		adapter->curr_iface_comb.sta_intf++;
3116 		break;
3117 	case NL80211_IFTYPE_AP:
3118 		adapter->curr_iface_comb.uap_intf++;
3119 		break;
3120 	case NL80211_IFTYPE_P2P_CLIENT:
3121 		adapter->curr_iface_comb.p2p_intf++;
3122 		break;
3123 	default:
3124 		/* This should be dead code; checked above */
3125 		mwifiex_dbg(adapter, ERROR, "type not supported\n");
3126 		return ERR_PTR(-EINVAL);
3127 	}
3128 
3129 	return &priv->wdev;
3130 
3131 err_reg_netdev:
3132 	destroy_workqueue(priv->dfs_chan_sw_workqueue);
3133 	priv->dfs_chan_sw_workqueue = NULL;
3134 err_alloc_chsw:
3135 	destroy_workqueue(priv->dfs_cac_workqueue);
3136 	priv->dfs_cac_workqueue = NULL;
3137 err_alloc_cac:
3138 	free_netdev(dev);
3139 	priv->netdev = NULL;
3140 err_sta_init:
3141 err_set_bss_mode:
3142 err_alloc_netdev:
3143 	memset(&priv->wdev, 0, sizeof(priv->wdev));
3144 	priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
3145 	priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3146 	return ERR_PTR(ret);
3147 }
3148 EXPORT_SYMBOL_GPL(mwifiex_add_virtual_intf);
3149 
3150 /*
3151  * del_virtual_intf: remove the virtual interface determined by dev
3152  */
mwifiex_del_virtual_intf(struct wiphy * wiphy,struct wireless_dev * wdev)3153 int mwifiex_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
3154 {
3155 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
3156 	struct mwifiex_adapter *adapter = priv->adapter;
3157 	struct sk_buff *skb, *tmp;
3158 
3159 #ifdef CONFIG_DEBUG_FS
3160 	mwifiex_dev_debugfs_remove(priv);
3161 #endif
3162 
3163 	if (priv->sched_scanning)
3164 		priv->sched_scanning = false;
3165 
3166 	mwifiex_stop_net_dev_queue(priv->netdev, adapter);
3167 
3168 	skb_queue_walk_safe(&priv->bypass_txq, skb, tmp) {
3169 		skb_unlink(skb, &priv->bypass_txq);
3170 		mwifiex_write_data_complete(priv->adapter, skb, 0, -1);
3171 	}
3172 
3173 	if (netif_carrier_ok(priv->netdev))
3174 		netif_carrier_off(priv->netdev);
3175 
3176 	if (wdev->netdev->reg_state == NETREG_REGISTERED)
3177 		cfg80211_unregister_netdevice(wdev->netdev);
3178 
3179 	if (priv->dfs_cac_workqueue) {
3180 		flush_workqueue(priv->dfs_cac_workqueue);
3181 		destroy_workqueue(priv->dfs_cac_workqueue);
3182 		priv->dfs_cac_workqueue = NULL;
3183 	}
3184 
3185 	if (priv->dfs_chan_sw_workqueue) {
3186 		flush_workqueue(priv->dfs_chan_sw_workqueue);
3187 		destroy_workqueue(priv->dfs_chan_sw_workqueue);
3188 		priv->dfs_chan_sw_workqueue = NULL;
3189 	}
3190 	/* Clear the priv in adapter */
3191 	priv->netdev = NULL;
3192 
3193 	switch (priv->bss_mode) {
3194 	case NL80211_IFTYPE_UNSPECIFIED:
3195 	case NL80211_IFTYPE_STATION:
3196 	case NL80211_IFTYPE_ADHOC:
3197 		adapter->curr_iface_comb.sta_intf--;
3198 		break;
3199 	case NL80211_IFTYPE_AP:
3200 		adapter->curr_iface_comb.uap_intf--;
3201 		break;
3202 	case NL80211_IFTYPE_P2P_CLIENT:
3203 	case NL80211_IFTYPE_P2P_GO:
3204 		adapter->curr_iface_comb.p2p_intf--;
3205 		break;
3206 	default:
3207 		mwifiex_dbg(adapter, ERROR,
3208 			    "del_virtual_intf: type not supported\n");
3209 		break;
3210 	}
3211 
3212 	priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3213 
3214 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
3215 	    GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP)
3216 		kfree(priv->hist_data);
3217 
3218 	return 0;
3219 }
3220 EXPORT_SYMBOL_GPL(mwifiex_del_virtual_intf);
3221 
3222 static bool
mwifiex_is_pattern_supported(struct cfg80211_pkt_pattern * pat,s8 * byte_seq,u8 max_byte_seq)3223 mwifiex_is_pattern_supported(struct cfg80211_pkt_pattern *pat, s8 *byte_seq,
3224 			     u8 max_byte_seq)
3225 {
3226 	int j, k, valid_byte_cnt = 0;
3227 	bool dont_care_byte = false;
3228 
3229 	for (j = 0; j < DIV_ROUND_UP(pat->pattern_len, 8); j++) {
3230 		for (k = 0; k < 8; k++) {
3231 			if (pat->mask[j] & 1 << k) {
3232 				memcpy(byte_seq + valid_byte_cnt,
3233 				       &pat->pattern[j * 8 + k], 1);
3234 				valid_byte_cnt++;
3235 				if (dont_care_byte)
3236 					return false;
3237 			} else {
3238 				if (valid_byte_cnt)
3239 					dont_care_byte = true;
3240 			}
3241 
3242 			/* wildcard bytes record as the offset
3243 			 * before the valid byte
3244 			 */
3245 			if (!valid_byte_cnt && !dont_care_byte)
3246 				pat->pkt_offset++;
3247 
3248 			if (valid_byte_cnt > max_byte_seq)
3249 				return false;
3250 		}
3251 	}
3252 
3253 	byte_seq[max_byte_seq] = valid_byte_cnt;
3254 
3255 	return true;
3256 }
3257 
3258 #ifdef CONFIG_PM
mwifiex_set_auto_arp_mef_entry(struct mwifiex_private * priv,struct mwifiex_mef_entry * mef_entry)3259 static void mwifiex_set_auto_arp_mef_entry(struct mwifiex_private *priv,
3260 					   struct mwifiex_mef_entry *mef_entry)
3261 {
3262 	int i, filt_num = 0, num_ipv4 = 0;
3263 	struct in_device *in_dev;
3264 	struct in_ifaddr *ifa;
3265 	__be32 ips[MWIFIEX_MAX_SUPPORTED_IPADDR];
3266 	struct mwifiex_adapter *adapter = priv->adapter;
3267 
3268 	mef_entry->mode = MEF_MODE_HOST_SLEEP;
3269 	mef_entry->action = MEF_ACTION_AUTO_ARP;
3270 
3271 	/* Enable ARP offload feature */
3272 	memset(ips, 0, sizeof(ips));
3273 	for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
3274 		if (adapter->priv[i]->netdev) {
3275 			in_dev = __in_dev_get_rtnl(adapter->priv[i]->netdev);
3276 			if (!in_dev)
3277 				continue;
3278 			ifa = rtnl_dereference(in_dev->ifa_list);
3279 			if (!ifa || !ifa->ifa_local)
3280 				continue;
3281 			ips[i] = ifa->ifa_local;
3282 			num_ipv4++;
3283 		}
3284 	}
3285 
3286 	for (i = 0; i < num_ipv4; i++) {
3287 		if (!ips[i])
3288 			continue;
3289 		mef_entry->filter[filt_num].repeat = 1;
3290 		memcpy(mef_entry->filter[filt_num].byte_seq,
3291 		       (u8 *)&ips[i], sizeof(ips[i]));
3292 		mef_entry->filter[filt_num].
3293 			byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3294 			sizeof(ips[i]);
3295 		mef_entry->filter[filt_num].offset = 46;
3296 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3297 		if (filt_num) {
3298 			mef_entry->filter[filt_num].filt_action =
3299 				TYPE_OR;
3300 		}
3301 		filt_num++;
3302 	}
3303 
3304 	mef_entry->filter[filt_num].repeat = 1;
3305 	mef_entry->filter[filt_num].byte_seq[0] = 0x08;
3306 	mef_entry->filter[filt_num].byte_seq[1] = 0x06;
3307 	mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] = 2;
3308 	mef_entry->filter[filt_num].offset = 20;
3309 	mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3310 	mef_entry->filter[filt_num].filt_action = TYPE_AND;
3311 }
3312 
mwifiex_set_wowlan_mef_entry(struct mwifiex_private * priv,struct mwifiex_ds_mef_cfg * mef_cfg,struct mwifiex_mef_entry * mef_entry,struct cfg80211_wowlan * wowlan)3313 static int mwifiex_set_wowlan_mef_entry(struct mwifiex_private *priv,
3314 					struct mwifiex_ds_mef_cfg *mef_cfg,
3315 					struct mwifiex_mef_entry *mef_entry,
3316 					struct cfg80211_wowlan *wowlan)
3317 {
3318 	int i, filt_num = 0, ret = 0;
3319 	bool first_pat = true;
3320 	u8 byte_seq[MWIFIEX_MEF_MAX_BYTESEQ + 1];
3321 	static const u8 ipv4_mc_mac[] = {0x33, 0x33};
3322 	static const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
3323 
3324 	mef_entry->mode = MEF_MODE_HOST_SLEEP;
3325 	mef_entry->action = MEF_ACTION_ALLOW_AND_WAKEUP_HOST;
3326 
3327 	for (i = 0; i < wowlan->n_patterns; i++) {
3328 		memset(byte_seq, 0, sizeof(byte_seq));
3329 		if (!mwifiex_is_pattern_supported(&wowlan->patterns[i],
3330 					byte_seq,
3331 					MWIFIEX_MEF_MAX_BYTESEQ)) {
3332 			mwifiex_dbg(priv->adapter, ERROR,
3333 				    "Pattern not supported\n");
3334 			return -EOPNOTSUPP;
3335 		}
3336 
3337 		if (!wowlan->patterns[i].pkt_offset) {
3338 			if (!(byte_seq[0] & 0x01) &&
3339 			    (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 1)) {
3340 				mef_cfg->criteria |= MWIFIEX_CRITERIA_UNICAST;
3341 				continue;
3342 			} else if (is_broadcast_ether_addr(byte_seq)) {
3343 				mef_cfg->criteria |= MWIFIEX_CRITERIA_BROADCAST;
3344 				continue;
3345 			} else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
3346 				    (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 2)) ||
3347 				   (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
3348 				    (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 3))) {
3349 				mef_cfg->criteria |= MWIFIEX_CRITERIA_MULTICAST;
3350 				continue;
3351 			}
3352 		}
3353 		mef_entry->filter[filt_num].repeat = 1;
3354 		mef_entry->filter[filt_num].offset =
3355 			wowlan->patterns[i].pkt_offset;
3356 		memcpy(mef_entry->filter[filt_num].byte_seq, byte_seq,
3357 				sizeof(byte_seq));
3358 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3359 
3360 		if (first_pat) {
3361 			first_pat = false;
3362 			mwifiex_dbg(priv->adapter, INFO, "Wake on patterns\n");
3363 		} else {
3364 			mef_entry->filter[filt_num].filt_action = TYPE_AND;
3365 		}
3366 
3367 		filt_num++;
3368 	}
3369 
3370 	if (wowlan->magic_pkt) {
3371 		mef_cfg->criteria |= MWIFIEX_CRITERIA_UNICAST;
3372 		mef_entry->filter[filt_num].repeat = 16;
3373 		memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
3374 				ETH_ALEN);
3375 		mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3376 			ETH_ALEN;
3377 		mef_entry->filter[filt_num].offset = 28;
3378 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3379 		if (filt_num)
3380 			mef_entry->filter[filt_num].filt_action = TYPE_OR;
3381 
3382 		filt_num++;
3383 		mef_entry->filter[filt_num].repeat = 16;
3384 		memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
3385 				ETH_ALEN);
3386 		mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3387 			ETH_ALEN;
3388 		mef_entry->filter[filt_num].offset = 56;
3389 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3390 		mef_entry->filter[filt_num].filt_action = TYPE_OR;
3391 		mwifiex_dbg(priv->adapter, INFO, "Wake on magic packet\n");
3392 	}
3393 	return ret;
3394 }
3395 
mwifiex_set_mef_filter(struct mwifiex_private * priv,struct cfg80211_wowlan * wowlan)3396 static int mwifiex_set_mef_filter(struct mwifiex_private *priv,
3397 				  struct cfg80211_wowlan *wowlan)
3398 {
3399 	int ret = 0, num_entries = 1;
3400 	struct mwifiex_ds_mef_cfg mef_cfg;
3401 	struct mwifiex_mef_entry *mef_entry;
3402 
3403 	if (wowlan->n_patterns || wowlan->magic_pkt)
3404 		num_entries++;
3405 
3406 	mef_entry = kcalloc(num_entries, sizeof(*mef_entry), GFP_KERNEL);
3407 	if (!mef_entry)
3408 		return -ENOMEM;
3409 
3410 	memset(&mef_cfg, 0, sizeof(mef_cfg));
3411 	mef_cfg.criteria |= MWIFIEX_CRITERIA_BROADCAST |
3412 		MWIFIEX_CRITERIA_UNICAST;
3413 	mef_cfg.num_entries = num_entries;
3414 	mef_cfg.mef_entry = mef_entry;
3415 
3416 	mwifiex_set_auto_arp_mef_entry(priv, &mef_entry[0]);
3417 
3418 	if (wowlan->n_patterns || wowlan->magic_pkt) {
3419 		ret = mwifiex_set_wowlan_mef_entry(priv, &mef_cfg,
3420 						   &mef_entry[1], wowlan);
3421 		if (ret)
3422 			goto err;
3423 	}
3424 
3425 	if (!mef_cfg.criteria)
3426 		mef_cfg.criteria = MWIFIEX_CRITERIA_BROADCAST |
3427 			MWIFIEX_CRITERIA_UNICAST |
3428 			MWIFIEX_CRITERIA_MULTICAST;
3429 
3430 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_MEF_CFG,
3431 			HostCmd_ACT_GEN_SET, 0,
3432 			&mef_cfg, true);
3433 
3434 err:
3435 	kfree(mef_entry);
3436 	return ret;
3437 }
3438 
mwifiex_cfg80211_suspend(struct wiphy * wiphy,struct cfg80211_wowlan * wowlan)3439 static int mwifiex_cfg80211_suspend(struct wiphy *wiphy,
3440 				    struct cfg80211_wowlan *wowlan)
3441 {
3442 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3443 	struct mwifiex_ds_hs_cfg hs_cfg;
3444 	int i, ret = 0, retry_num = 10;
3445 	struct mwifiex_private *priv;
3446 	struct mwifiex_private *sta_priv =
3447 			mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3448 
3449 	sta_priv->scan_aborting = true;
3450 	for (i = 0; i < adapter->priv_num; i++) {
3451 		priv = adapter->priv[i];
3452 		mwifiex_abort_cac(priv);
3453 	}
3454 
3455 	mwifiex_cancel_all_pending_cmd(adapter);
3456 
3457 	for (i = 0; i < adapter->priv_num; i++) {
3458 		priv = adapter->priv[i];
3459 		if (priv && priv->netdev)
3460 			netif_device_detach(priv->netdev);
3461 	}
3462 
3463 	for (i = 0; i < retry_num; i++) {
3464 		if (!mwifiex_wmm_lists_empty(adapter) ||
3465 		    !mwifiex_bypass_txlist_empty(adapter) ||
3466 		    !skb_queue_empty(&adapter->tx_data_q))
3467 			usleep_range(10000, 15000);
3468 		else
3469 			break;
3470 	}
3471 
3472 	if (!wowlan) {
3473 		mwifiex_dbg(adapter, ERROR,
3474 			    "None of the WOWLAN triggers enabled\n");
3475 		ret = 0;
3476 		goto done;
3477 	}
3478 
3479 	if (!sta_priv->media_connected && !wowlan->nd_config) {
3480 		mwifiex_dbg(adapter, ERROR,
3481 			    "Can not configure WOWLAN in disconnected state\n");
3482 		ret = 0;
3483 		goto done;
3484 	}
3485 
3486 	ret = mwifiex_set_mef_filter(sta_priv, wowlan);
3487 	if (ret) {
3488 		mwifiex_dbg(adapter, ERROR, "Failed to set MEF filter\n");
3489 		goto done;
3490 	}
3491 
3492 	memset(&hs_cfg, 0, sizeof(hs_cfg));
3493 	hs_cfg.conditions = le32_to_cpu(adapter->hs_cfg.conditions);
3494 
3495 	if (wowlan->nd_config) {
3496 		mwifiex_dbg(adapter, INFO, "Wake on net detect\n");
3497 		hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
3498 		mwifiex_cfg80211_sched_scan_start(wiphy, sta_priv->netdev,
3499 						  wowlan->nd_config);
3500 	}
3501 
3502 	if (wowlan->disconnect) {
3503 		hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
3504 		mwifiex_dbg(sta_priv->adapter, INFO, "Wake on device disconnect\n");
3505 	}
3506 
3507 	hs_cfg.is_invoke_hostcmd = false;
3508 	hs_cfg.gpio = adapter->hs_cfg.gpio;
3509 	hs_cfg.gap = adapter->hs_cfg.gap;
3510 	ret = mwifiex_set_hs_params(sta_priv, HostCmd_ACT_GEN_SET,
3511 				    MWIFIEX_SYNC_CMD, &hs_cfg);
3512 	if (ret)
3513 		mwifiex_dbg(adapter, ERROR, "Failed to set HS params\n");
3514 
3515 done:
3516 	sta_priv->scan_aborting = false;
3517 	return ret;
3518 }
3519 
mwifiex_cfg80211_resume(struct wiphy * wiphy)3520 static int mwifiex_cfg80211_resume(struct wiphy *wiphy)
3521 {
3522 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3523 	struct mwifiex_private *priv;
3524 	struct mwifiex_ds_wakeup_reason wakeup_reason;
3525 	struct cfg80211_wowlan_wakeup wakeup_report;
3526 	int i;
3527 	bool report_wakeup_reason = true;
3528 
3529 	for (i = 0; i < adapter->priv_num; i++) {
3530 		priv = adapter->priv[i];
3531 		if (priv && priv->netdev)
3532 			netif_device_attach(priv->netdev);
3533 	}
3534 
3535 	if (!wiphy->wowlan_config)
3536 		goto done;
3537 
3538 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3539 	mwifiex_get_wakeup_reason(priv, HostCmd_ACT_GEN_GET, MWIFIEX_SYNC_CMD,
3540 				  &wakeup_reason);
3541 	memset(&wakeup_report, 0, sizeof(struct cfg80211_wowlan_wakeup));
3542 
3543 	wakeup_report.pattern_idx = -1;
3544 
3545 	switch (wakeup_reason.hs_wakeup_reason) {
3546 	case NO_HSWAKEUP_REASON:
3547 		break;
3548 	case BCAST_DATA_MATCHED:
3549 		break;
3550 	case MCAST_DATA_MATCHED:
3551 		break;
3552 	case UCAST_DATA_MATCHED:
3553 		break;
3554 	case MASKTABLE_EVENT_MATCHED:
3555 		break;
3556 	case NON_MASKABLE_EVENT_MATCHED:
3557 		if (wiphy->wowlan_config->disconnect)
3558 			wakeup_report.disconnect = true;
3559 		if (wiphy->wowlan_config->nd_config)
3560 			wakeup_report.net_detect = adapter->nd_info;
3561 		break;
3562 	case NON_MASKABLE_CONDITION_MATCHED:
3563 		break;
3564 	case MAGIC_PATTERN_MATCHED:
3565 		if (wiphy->wowlan_config->magic_pkt)
3566 			wakeup_report.magic_pkt = true;
3567 		if (wiphy->wowlan_config->n_patterns)
3568 			wakeup_report.pattern_idx = 1;
3569 		break;
3570 	case GTK_REKEY_FAILURE:
3571 		if (wiphy->wowlan_config->gtk_rekey_failure)
3572 			wakeup_report.gtk_rekey_failure = true;
3573 		break;
3574 	default:
3575 		report_wakeup_reason = false;
3576 		break;
3577 	}
3578 
3579 	if (report_wakeup_reason)
3580 		cfg80211_report_wowlan_wakeup(&priv->wdev, &wakeup_report,
3581 					      GFP_KERNEL);
3582 
3583 done:
3584 	if (adapter->nd_info) {
3585 		for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
3586 			kfree(adapter->nd_info->matches[i]);
3587 		kfree(adapter->nd_info);
3588 		adapter->nd_info = NULL;
3589 	}
3590 
3591 	return 0;
3592 }
3593 
mwifiex_cfg80211_set_wakeup(struct wiphy * wiphy,bool enabled)3594 static void mwifiex_cfg80211_set_wakeup(struct wiphy *wiphy,
3595 				       bool enabled)
3596 {
3597 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3598 
3599 	device_set_wakeup_enable(adapter->dev, enabled);
3600 }
3601 
mwifiex_set_rekey_data(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_gtk_rekey_data * data)3602 static int mwifiex_set_rekey_data(struct wiphy *wiphy, struct net_device *dev,
3603 				  struct cfg80211_gtk_rekey_data *data)
3604 {
3605 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3606 
3607 	if (!ISSUPP_FIRMWARE_SUPPLICANT(priv->adapter->fw_cap_info))
3608 		return -EOPNOTSUPP;
3609 
3610 	return mwifiex_send_cmd(priv, HostCmd_CMD_GTK_REKEY_OFFLOAD_CFG,
3611 				HostCmd_ACT_GEN_SET, 0, data, true);
3612 }
3613 
3614 #endif
3615 
mwifiex_get_coalesce_pkt_type(u8 * byte_seq)3616 static int mwifiex_get_coalesce_pkt_type(u8 *byte_seq)
3617 {
3618 	static const u8 ipv4_mc_mac[] = {0x33, 0x33};
3619 	static const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
3620 	static const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff};
3621 
3622 	if ((byte_seq[0] & 0x01) &&
3623 	    (byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 1))
3624 		return PACKET_TYPE_UNICAST;
3625 	else if (!memcmp(byte_seq, bc_mac, 4))
3626 		return PACKET_TYPE_BROADCAST;
3627 	else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
3628 		  byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 2) ||
3629 		 (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
3630 		  byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 3))
3631 		return PACKET_TYPE_MULTICAST;
3632 
3633 	return 0;
3634 }
3635 
3636 static int
mwifiex_fill_coalesce_rule_info(struct mwifiex_private * priv,struct cfg80211_coalesce_rules * crule,struct mwifiex_coalesce_rule * mrule)3637 mwifiex_fill_coalesce_rule_info(struct mwifiex_private *priv,
3638 				struct cfg80211_coalesce_rules *crule,
3639 				struct mwifiex_coalesce_rule *mrule)
3640 {
3641 	u8 byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ + 1];
3642 	struct filt_field_param *param;
3643 	int i;
3644 
3645 	mrule->max_coalescing_delay = crule->delay;
3646 
3647 	param = mrule->params;
3648 
3649 	for (i = 0; i < crule->n_patterns; i++) {
3650 		memset(byte_seq, 0, sizeof(byte_seq));
3651 		if (!mwifiex_is_pattern_supported(&crule->patterns[i],
3652 						  byte_seq,
3653 						MWIFIEX_COALESCE_MAX_BYTESEQ)) {
3654 			mwifiex_dbg(priv->adapter, ERROR,
3655 				    "Pattern not supported\n");
3656 			return -EOPNOTSUPP;
3657 		}
3658 
3659 		if (!crule->patterns[i].pkt_offset) {
3660 			u8 pkt_type;
3661 
3662 			pkt_type = mwifiex_get_coalesce_pkt_type(byte_seq);
3663 			if (pkt_type && mrule->pkt_type) {
3664 				mwifiex_dbg(priv->adapter, ERROR,
3665 					    "Multiple packet types not allowed\n");
3666 				return -EOPNOTSUPP;
3667 			} else if (pkt_type) {
3668 				mrule->pkt_type = pkt_type;
3669 				continue;
3670 			}
3671 		}
3672 
3673 		if (crule->condition == NL80211_COALESCE_CONDITION_MATCH)
3674 			param->operation = RECV_FILTER_MATCH_TYPE_EQ;
3675 		else
3676 			param->operation = RECV_FILTER_MATCH_TYPE_NE;
3677 
3678 		param->operand_len = byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ];
3679 		memcpy(param->operand_byte_stream, byte_seq,
3680 		       param->operand_len);
3681 		param->offset = crule->patterns[i].pkt_offset;
3682 		param++;
3683 
3684 		mrule->num_of_fields++;
3685 	}
3686 
3687 	if (!mrule->pkt_type) {
3688 		mwifiex_dbg(priv->adapter, ERROR,
3689 			    "Packet type can not be determined\n");
3690 		return -EOPNOTSUPP;
3691 	}
3692 
3693 	return 0;
3694 }
3695 
mwifiex_cfg80211_set_coalesce(struct wiphy * wiphy,struct cfg80211_coalesce * coalesce)3696 static int mwifiex_cfg80211_set_coalesce(struct wiphy *wiphy,
3697 					 struct cfg80211_coalesce *coalesce)
3698 {
3699 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3700 	int i, ret;
3701 	struct mwifiex_ds_coalesce_cfg coalesce_cfg;
3702 	struct mwifiex_private *priv =
3703 			mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3704 
3705 	memset(&coalesce_cfg, 0, sizeof(coalesce_cfg));
3706 	if (!coalesce) {
3707 		mwifiex_dbg(adapter, WARN,
3708 			    "Disable coalesce and reset all previous rules\n");
3709 		return mwifiex_send_cmd(priv, HostCmd_CMD_COALESCE_CFG,
3710 					HostCmd_ACT_GEN_SET, 0,
3711 					&coalesce_cfg, true);
3712 	}
3713 
3714 	coalesce_cfg.num_of_rules = coalesce->n_rules;
3715 	for (i = 0; i < coalesce->n_rules; i++) {
3716 		ret = mwifiex_fill_coalesce_rule_info(priv, &coalesce->rules[i],
3717 						      &coalesce_cfg.rule[i]);
3718 		if (ret) {
3719 			mwifiex_dbg(adapter, ERROR,
3720 				    "Recheck the patterns provided for rule %d\n",
3721 				i + 1);
3722 			return ret;
3723 		}
3724 	}
3725 
3726 	return mwifiex_send_cmd(priv, HostCmd_CMD_COALESCE_CFG,
3727 				HostCmd_ACT_GEN_SET, 0, &coalesce_cfg, true);
3728 }
3729 
3730 /* cfg80211 ops handler for tdls_mgmt.
3731  * Function prepares TDLS action frame packets and forwards them to FW
3732  */
3733 static int
mwifiex_cfg80211_tdls_mgmt(struct wiphy * wiphy,struct net_device * dev,const u8 * peer,u8 action_code,u8 dialog_token,u16 status_code,u32 peer_capability,bool initiator,const u8 * extra_ies,size_t extra_ies_len)3734 mwifiex_cfg80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
3735 			   const u8 *peer, u8 action_code, u8 dialog_token,
3736 			   u16 status_code, u32 peer_capability,
3737 			   bool initiator, const u8 *extra_ies,
3738 			   size_t extra_ies_len)
3739 {
3740 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3741 	int ret;
3742 
3743 	if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
3744 		return -EOPNOTSUPP;
3745 
3746 	/* make sure we are in station mode and connected */
3747 	if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
3748 		return -EOPNOTSUPP;
3749 
3750 	switch (action_code) {
3751 	case WLAN_TDLS_SETUP_REQUEST:
3752 		mwifiex_dbg(priv->adapter, MSG,
3753 			    "Send TDLS Setup Request to %pM status_code=%d\n",
3754 			    peer, status_code);
3755 		mwifiex_add_auto_tdls_peer(priv, peer);
3756 		ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3757 						   dialog_token, status_code,
3758 						   extra_ies, extra_ies_len);
3759 		break;
3760 	case WLAN_TDLS_SETUP_RESPONSE:
3761 		mwifiex_add_auto_tdls_peer(priv, peer);
3762 		mwifiex_dbg(priv->adapter, MSG,
3763 			    "Send TDLS Setup Response to %pM status_code=%d\n",
3764 			    peer, status_code);
3765 		ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3766 						   dialog_token, status_code,
3767 						   extra_ies, extra_ies_len);
3768 		break;
3769 	case WLAN_TDLS_SETUP_CONFIRM:
3770 		mwifiex_dbg(priv->adapter, MSG,
3771 			    "Send TDLS Confirm to %pM status_code=%d\n", peer,
3772 			    status_code);
3773 		ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3774 						   dialog_token, status_code,
3775 						   extra_ies, extra_ies_len);
3776 		break;
3777 	case WLAN_TDLS_TEARDOWN:
3778 		mwifiex_dbg(priv->adapter, MSG,
3779 			    "Send TDLS Tear down to %pM\n", peer);
3780 		ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3781 						   dialog_token, status_code,
3782 						   extra_ies, extra_ies_len);
3783 		break;
3784 	case WLAN_TDLS_DISCOVERY_REQUEST:
3785 		mwifiex_dbg(priv->adapter, MSG,
3786 			    "Send TDLS Discovery Request to %pM\n", peer);
3787 		ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3788 						   dialog_token, status_code,
3789 						   extra_ies, extra_ies_len);
3790 		break;
3791 	case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3792 		mwifiex_dbg(priv->adapter, MSG,
3793 			    "Send TDLS Discovery Response to %pM\n", peer);
3794 		ret = mwifiex_send_tdls_action_frame(priv, peer, action_code,
3795 						   dialog_token, status_code,
3796 						   extra_ies, extra_ies_len);
3797 		break;
3798 	default:
3799 		mwifiex_dbg(priv->adapter, ERROR,
3800 			    "Unknown TDLS mgmt/action frame %pM\n", peer);
3801 		ret = -EINVAL;
3802 		break;
3803 	}
3804 
3805 	return ret;
3806 }
3807 
3808 static int
mwifiex_cfg80211_tdls_oper(struct wiphy * wiphy,struct net_device * dev,const u8 * peer,enum nl80211_tdls_operation action)3809 mwifiex_cfg80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
3810 			   const u8 *peer, enum nl80211_tdls_operation action)
3811 {
3812 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3813 
3814 	if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
3815 	    !(wiphy->flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3816 		return -EOPNOTSUPP;
3817 
3818 	/* make sure we are in station mode and connected */
3819 	if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
3820 		return -EOPNOTSUPP;
3821 
3822 	mwifiex_dbg(priv->adapter, MSG,
3823 		    "TDLS peer=%pM, oper=%d\n", peer, action);
3824 
3825 	switch (action) {
3826 	case NL80211_TDLS_ENABLE_LINK:
3827 		action = MWIFIEX_TDLS_ENABLE_LINK;
3828 		break;
3829 	case NL80211_TDLS_DISABLE_LINK:
3830 		action = MWIFIEX_TDLS_DISABLE_LINK;
3831 		break;
3832 	case NL80211_TDLS_TEARDOWN:
3833 		/* shouldn't happen!*/
3834 		mwifiex_dbg(priv->adapter, ERROR,
3835 			    "tdls_oper: teardown from driver not supported\n");
3836 		return -EINVAL;
3837 	case NL80211_TDLS_SETUP:
3838 		/* shouldn't happen!*/
3839 		mwifiex_dbg(priv->adapter, ERROR,
3840 			    "tdls_oper: setup from driver not supported\n");
3841 		return -EINVAL;
3842 	case NL80211_TDLS_DISCOVERY_REQ:
3843 		/* shouldn't happen!*/
3844 		mwifiex_dbg(priv->adapter, ERROR,
3845 			    "tdls_oper: discovery from driver not supported\n");
3846 		return -EINVAL;
3847 	default:
3848 		mwifiex_dbg(priv->adapter, ERROR,
3849 			    "tdls_oper: operation not supported\n");
3850 		return -EOPNOTSUPP;
3851 	}
3852 
3853 	return mwifiex_tdls_oper(priv, peer, action);
3854 }
3855 
3856 static int
mwifiex_cfg80211_tdls_chan_switch(struct wiphy * wiphy,struct net_device * dev,const u8 * addr,u8 oper_class,struct cfg80211_chan_def * chandef)3857 mwifiex_cfg80211_tdls_chan_switch(struct wiphy *wiphy, struct net_device *dev,
3858 				  const u8 *addr, u8 oper_class,
3859 				  struct cfg80211_chan_def *chandef)
3860 {
3861 	struct mwifiex_sta_node *sta_ptr;
3862 	u16 chan;
3863 	u8 second_chan_offset, band;
3864 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3865 
3866 	spin_lock_bh(&priv->sta_list_spinlock);
3867 	sta_ptr = mwifiex_get_sta_entry(priv, addr);
3868 	if (!sta_ptr) {
3869 		spin_unlock_bh(&priv->sta_list_spinlock);
3870 		wiphy_err(wiphy, "%s: Invalid TDLS peer %pM\n",
3871 			  __func__, addr);
3872 		return -ENOENT;
3873 	}
3874 
3875 	if (!(sta_ptr->tdls_cap.extcap.ext_capab[3] &
3876 	      WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH)) {
3877 		spin_unlock_bh(&priv->sta_list_spinlock);
3878 		wiphy_err(wiphy, "%pM do not support tdls cs\n", addr);
3879 		return -ENOENT;
3880 	}
3881 
3882 	if (sta_ptr->tdls_status == TDLS_CHAN_SWITCHING ||
3883 	    sta_ptr->tdls_status == TDLS_IN_OFF_CHAN) {
3884 		spin_unlock_bh(&priv->sta_list_spinlock);
3885 		wiphy_err(wiphy, "channel switch is running, abort request\n");
3886 		return -EALREADY;
3887 	}
3888 	spin_unlock_bh(&priv->sta_list_spinlock);
3889 
3890 	chan = chandef->chan->hw_value;
3891 	second_chan_offset = mwifiex_get_sec_chan_offset(chan);
3892 	band = chandef->chan->band;
3893 	mwifiex_start_tdls_cs(priv, addr, chan, second_chan_offset, band);
3894 
3895 	return 0;
3896 }
3897 
3898 static void
mwifiex_cfg80211_tdls_cancel_chan_switch(struct wiphy * wiphy,struct net_device * dev,const u8 * addr)3899 mwifiex_cfg80211_tdls_cancel_chan_switch(struct wiphy *wiphy,
3900 					 struct net_device *dev,
3901 					 const u8 *addr)
3902 {
3903 	struct mwifiex_sta_node *sta_ptr;
3904 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3905 
3906 	spin_lock_bh(&priv->sta_list_spinlock);
3907 	sta_ptr = mwifiex_get_sta_entry(priv, addr);
3908 	if (!sta_ptr) {
3909 		spin_unlock_bh(&priv->sta_list_spinlock);
3910 		wiphy_err(wiphy, "%s: Invalid TDLS peer %pM\n",
3911 			  __func__, addr);
3912 	} else if (!(sta_ptr->tdls_status == TDLS_CHAN_SWITCHING ||
3913 		     sta_ptr->tdls_status == TDLS_IN_BASE_CHAN ||
3914 		     sta_ptr->tdls_status == TDLS_IN_OFF_CHAN)) {
3915 		spin_unlock_bh(&priv->sta_list_spinlock);
3916 		wiphy_err(wiphy, "tdls chan switch not initialize by %pM\n",
3917 			  addr);
3918 	} else {
3919 		spin_unlock_bh(&priv->sta_list_spinlock);
3920 		mwifiex_stop_tdls_cs(priv, addr);
3921 	}
3922 }
3923 
3924 static int
mwifiex_cfg80211_add_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_parameters * params)3925 mwifiex_cfg80211_add_station(struct wiphy *wiphy, struct net_device *dev,
3926 			     const u8 *mac, struct station_parameters *params)
3927 {
3928 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3929 
3930 	if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3931 		return -EOPNOTSUPP;
3932 
3933 	/* make sure we are in station mode and connected */
3934 	if ((priv->bss_type != MWIFIEX_BSS_TYPE_STA) || !priv->media_connected)
3935 		return -EOPNOTSUPP;
3936 
3937 	return mwifiex_tdls_oper(priv, mac, MWIFIEX_TDLS_CREATE_LINK);
3938 }
3939 
3940 static int
mwifiex_cfg80211_channel_switch(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_csa_settings * params)3941 mwifiex_cfg80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
3942 				struct cfg80211_csa_settings *params)
3943 {
3944 	struct ieee_types_header *chsw_ie;
3945 	struct ieee80211_channel_sw_ie *channel_sw;
3946 	int chsw_msec;
3947 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3948 
3949 	if (priv->adapter->scan_processing) {
3950 		mwifiex_dbg(priv->adapter, ERROR,
3951 			    "radar detection: scan in process...\n");
3952 		return -EBUSY;
3953 	}
3954 
3955 	if (priv->wdev.cac_started)
3956 		return -EBUSY;
3957 
3958 	if (cfg80211_chandef_identical(&params->chandef,
3959 				       &priv->dfs_chandef))
3960 		return -EINVAL;
3961 
3962 	chsw_ie = (void *)cfg80211_find_ie(WLAN_EID_CHANNEL_SWITCH,
3963 					   params->beacon_csa.tail,
3964 					   params->beacon_csa.tail_len);
3965 	if (!chsw_ie) {
3966 		mwifiex_dbg(priv->adapter, ERROR,
3967 			    "Could not parse channel switch announcement IE\n");
3968 		return -EINVAL;
3969 	}
3970 
3971 	channel_sw = (void *)(chsw_ie + 1);
3972 	if (channel_sw->mode) {
3973 		if (netif_carrier_ok(priv->netdev))
3974 			netif_carrier_off(priv->netdev);
3975 		mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
3976 	}
3977 
3978 	if (mwifiex_del_mgmt_ies(priv))
3979 		mwifiex_dbg(priv->adapter, ERROR,
3980 			    "Failed to delete mgmt IEs!\n");
3981 
3982 	if (mwifiex_set_mgmt_ies(priv, &params->beacon_csa)) {
3983 		mwifiex_dbg(priv->adapter, ERROR,
3984 			    "%s: setting mgmt ies failed\n", __func__);
3985 		return -EFAULT;
3986 	}
3987 
3988 	memcpy(&priv->dfs_chandef, &params->chandef, sizeof(priv->dfs_chandef));
3989 	memcpy(&priv->beacon_after, &params->beacon_after,
3990 	       sizeof(priv->beacon_after));
3991 
3992 	chsw_msec = max(channel_sw->count * priv->bss_cfg.beacon_period, 100);
3993 	queue_delayed_work(priv->dfs_chan_sw_workqueue, &priv->dfs_chan_sw_work,
3994 			   msecs_to_jiffies(chsw_msec));
3995 	return 0;
3996 }
3997 
mwifiex_cfg80211_get_channel(struct wiphy * wiphy,struct wireless_dev * wdev,struct cfg80211_chan_def * chandef)3998 static int mwifiex_cfg80211_get_channel(struct wiphy *wiphy,
3999 					struct wireless_dev *wdev,
4000 					struct cfg80211_chan_def *chandef)
4001 {
4002 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
4003 	struct mwifiex_bssdescriptor *curr_bss;
4004 	struct ieee80211_channel *chan;
4005 	enum nl80211_channel_type chan_type;
4006 	enum nl80211_band band;
4007 	int freq;
4008 	int ret = -ENODATA;
4009 
4010 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
4011 	    cfg80211_chandef_valid(&priv->bss_chandef)) {
4012 		*chandef = priv->bss_chandef;
4013 		ret = 0;
4014 	} else if (priv->media_connected) {
4015 		curr_bss = &priv->curr_bss_params.bss_descriptor;
4016 		band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
4017 		freq = ieee80211_channel_to_frequency(curr_bss->channel, band);
4018 		chan = ieee80211_get_channel(wiphy, freq);
4019 
4020 		if (priv->ht_param_present) {
4021 			chan_type = mwifiex_get_chan_type(priv);
4022 			cfg80211_chandef_create(chandef, chan, chan_type);
4023 		} else {
4024 			cfg80211_chandef_create(chandef, chan,
4025 						NL80211_CHAN_NO_HT);
4026 		}
4027 		ret = 0;
4028 	}
4029 
4030 	return ret;
4031 }
4032 
4033 #ifdef CONFIG_NL80211_TESTMODE
4034 
4035 enum mwifiex_tm_attr {
4036 	__MWIFIEX_TM_ATTR_INVALID	= 0,
4037 	MWIFIEX_TM_ATTR_CMD		= 1,
4038 	MWIFIEX_TM_ATTR_DATA		= 2,
4039 
4040 	/* keep last */
4041 	__MWIFIEX_TM_ATTR_AFTER_LAST,
4042 	MWIFIEX_TM_ATTR_MAX		= __MWIFIEX_TM_ATTR_AFTER_LAST - 1,
4043 };
4044 
4045 static const struct nla_policy mwifiex_tm_policy[MWIFIEX_TM_ATTR_MAX + 1] = {
4046 	[MWIFIEX_TM_ATTR_CMD]		= { .type = NLA_U32 },
4047 	[MWIFIEX_TM_ATTR_DATA]		= { .type = NLA_BINARY,
4048 					    .len = MWIFIEX_SIZE_OF_CMD_BUFFER },
4049 };
4050 
4051 enum mwifiex_tm_command {
4052 	MWIFIEX_TM_CMD_HOSTCMD	= 0,
4053 };
4054 
mwifiex_tm_cmd(struct wiphy * wiphy,struct wireless_dev * wdev,void * data,int len)4055 static int mwifiex_tm_cmd(struct wiphy *wiphy, struct wireless_dev *wdev,
4056 			  void *data, int len)
4057 {
4058 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
4059 	struct mwifiex_ds_misc_cmd *hostcmd;
4060 	struct nlattr *tb[MWIFIEX_TM_ATTR_MAX + 1];
4061 	struct sk_buff *skb;
4062 	int err;
4063 
4064 	if (!priv)
4065 		return -EINVAL;
4066 
4067 	err = nla_parse_deprecated(tb, MWIFIEX_TM_ATTR_MAX, data, len,
4068 				   mwifiex_tm_policy, NULL);
4069 	if (err)
4070 		return err;
4071 
4072 	if (!tb[MWIFIEX_TM_ATTR_CMD])
4073 		return -EINVAL;
4074 
4075 	switch (nla_get_u32(tb[MWIFIEX_TM_ATTR_CMD])) {
4076 	case MWIFIEX_TM_CMD_HOSTCMD:
4077 		if (!tb[MWIFIEX_TM_ATTR_DATA])
4078 			return -EINVAL;
4079 
4080 		hostcmd = kzalloc(sizeof(*hostcmd), GFP_KERNEL);
4081 		if (!hostcmd)
4082 			return -ENOMEM;
4083 
4084 		hostcmd->len = nla_len(tb[MWIFIEX_TM_ATTR_DATA]);
4085 		memcpy(hostcmd->cmd, nla_data(tb[MWIFIEX_TM_ATTR_DATA]),
4086 		       hostcmd->len);
4087 
4088 		if (mwifiex_send_cmd(priv, 0, 0, 0, hostcmd, true)) {
4089 			dev_err(priv->adapter->dev, "Failed to process hostcmd\n");
4090 			kfree(hostcmd);
4091 			return -EFAULT;
4092 		}
4093 
4094 		/* process hostcmd response*/
4095 		skb = cfg80211_testmode_alloc_reply_skb(wiphy, hostcmd->len);
4096 		if (!skb) {
4097 			kfree(hostcmd);
4098 			return -ENOMEM;
4099 		}
4100 		err = nla_put(skb, MWIFIEX_TM_ATTR_DATA,
4101 			      hostcmd->len, hostcmd->cmd);
4102 		if (err) {
4103 			kfree(hostcmd);
4104 			kfree_skb(skb);
4105 			return -EMSGSIZE;
4106 		}
4107 
4108 		err = cfg80211_testmode_reply(skb);
4109 		kfree(hostcmd);
4110 		return err;
4111 	default:
4112 		return -EOPNOTSUPP;
4113 	}
4114 }
4115 #endif
4116 
4117 static int
mwifiex_cfg80211_start_radar_detection(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_chan_def * chandef,u32 cac_time_ms)4118 mwifiex_cfg80211_start_radar_detection(struct wiphy *wiphy,
4119 				       struct net_device *dev,
4120 				       struct cfg80211_chan_def *chandef,
4121 				       u32 cac_time_ms)
4122 {
4123 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
4124 	struct mwifiex_radar_params radar_params;
4125 
4126 	if (priv->adapter->scan_processing) {
4127 		mwifiex_dbg(priv->adapter, ERROR,
4128 			    "radar detection: scan already in process...\n");
4129 		return -EBUSY;
4130 	}
4131 
4132 	if (!mwifiex_is_11h_active(priv)) {
4133 		mwifiex_dbg(priv->adapter, INFO,
4134 			    "Enable 11h extensions in FW\n");
4135 		if (mwifiex_11h_activate(priv, true)) {
4136 			mwifiex_dbg(priv->adapter, ERROR,
4137 				    "Failed to activate 11h extensions!!");
4138 			return -1;
4139 		}
4140 		priv->state_11h.is_11h_active = true;
4141 	}
4142 
4143 	memset(&radar_params, 0, sizeof(struct mwifiex_radar_params));
4144 	radar_params.chandef = chandef;
4145 	radar_params.cac_time_ms = cac_time_ms;
4146 
4147 	memcpy(&priv->dfs_chandef, chandef, sizeof(priv->dfs_chandef));
4148 
4149 	if (mwifiex_send_cmd(priv, HostCmd_CMD_CHAN_REPORT_REQUEST,
4150 			     HostCmd_ACT_GEN_SET, 0, &radar_params, true))
4151 		return -1;
4152 
4153 	queue_delayed_work(priv->dfs_cac_workqueue, &priv->dfs_cac_work,
4154 			   msecs_to_jiffies(cac_time_ms));
4155 	return 0;
4156 }
4157 
4158 static int
mwifiex_cfg80211_change_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_parameters * params)4159 mwifiex_cfg80211_change_station(struct wiphy *wiphy, struct net_device *dev,
4160 				const u8 *mac,
4161 				struct station_parameters *params)
4162 {
4163 	int ret;
4164 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
4165 
4166 	/* we support change_station handler only for TDLS peers*/
4167 	if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4168 		return -EOPNOTSUPP;
4169 
4170 	/* make sure we are in station mode and connected */
4171 	if ((priv->bss_type != MWIFIEX_BSS_TYPE_STA) || !priv->media_connected)
4172 		return -EOPNOTSUPP;
4173 
4174 	priv->sta_params = params;
4175 
4176 	ret = mwifiex_tdls_oper(priv, mac, MWIFIEX_TDLS_CONFIG_LINK);
4177 	priv->sta_params = NULL;
4178 
4179 	return ret;
4180 }
4181 
4182 /* station cfg80211 operations */
4183 static struct cfg80211_ops mwifiex_cfg80211_ops = {
4184 	.add_virtual_intf = mwifiex_add_virtual_intf,
4185 	.del_virtual_intf = mwifiex_del_virtual_intf,
4186 	.change_virtual_intf = mwifiex_cfg80211_change_virtual_intf,
4187 	.scan = mwifiex_cfg80211_scan,
4188 	.connect = mwifiex_cfg80211_connect,
4189 	.disconnect = mwifiex_cfg80211_disconnect,
4190 	.get_station = mwifiex_cfg80211_get_station,
4191 	.dump_station = mwifiex_cfg80211_dump_station,
4192 	.dump_survey = mwifiex_cfg80211_dump_survey,
4193 	.set_wiphy_params = mwifiex_cfg80211_set_wiphy_params,
4194 	.join_ibss = mwifiex_cfg80211_join_ibss,
4195 	.leave_ibss = mwifiex_cfg80211_leave_ibss,
4196 	.add_key = mwifiex_cfg80211_add_key,
4197 	.del_key = mwifiex_cfg80211_del_key,
4198 	.set_default_mgmt_key = mwifiex_cfg80211_set_default_mgmt_key,
4199 	.mgmt_tx = mwifiex_cfg80211_mgmt_tx,
4200 	.update_mgmt_frame_registrations =
4201 		mwifiex_cfg80211_update_mgmt_frame_registrations,
4202 	.remain_on_channel = mwifiex_cfg80211_remain_on_channel,
4203 	.cancel_remain_on_channel = mwifiex_cfg80211_cancel_remain_on_channel,
4204 	.set_default_key = mwifiex_cfg80211_set_default_key,
4205 	.set_power_mgmt = mwifiex_cfg80211_set_power_mgmt,
4206 	.set_tx_power = mwifiex_cfg80211_set_tx_power,
4207 	.get_tx_power = mwifiex_cfg80211_get_tx_power,
4208 	.set_bitrate_mask = mwifiex_cfg80211_set_bitrate_mask,
4209 	.start_ap = mwifiex_cfg80211_start_ap,
4210 	.stop_ap = mwifiex_cfg80211_stop_ap,
4211 	.change_beacon = mwifiex_cfg80211_change_beacon,
4212 	.set_cqm_rssi_config = mwifiex_cfg80211_set_cqm_rssi_config,
4213 	.set_antenna = mwifiex_cfg80211_set_antenna,
4214 	.get_antenna = mwifiex_cfg80211_get_antenna,
4215 	.del_station = mwifiex_cfg80211_del_station,
4216 	.sched_scan_start = mwifiex_cfg80211_sched_scan_start,
4217 	.sched_scan_stop = mwifiex_cfg80211_sched_scan_stop,
4218 #ifdef CONFIG_PM
4219 	.suspend = mwifiex_cfg80211_suspend,
4220 	.resume = mwifiex_cfg80211_resume,
4221 	.set_wakeup = mwifiex_cfg80211_set_wakeup,
4222 	.set_rekey_data = mwifiex_set_rekey_data,
4223 #endif
4224 	.set_coalesce = mwifiex_cfg80211_set_coalesce,
4225 	.tdls_mgmt = mwifiex_cfg80211_tdls_mgmt,
4226 	.tdls_oper = mwifiex_cfg80211_tdls_oper,
4227 	.tdls_channel_switch = mwifiex_cfg80211_tdls_chan_switch,
4228 	.tdls_cancel_channel_switch = mwifiex_cfg80211_tdls_cancel_chan_switch,
4229 	.add_station = mwifiex_cfg80211_add_station,
4230 	.change_station = mwifiex_cfg80211_change_station,
4231 	CFG80211_TESTMODE_CMD(mwifiex_tm_cmd)
4232 	.get_channel = mwifiex_cfg80211_get_channel,
4233 	.start_radar_detection = mwifiex_cfg80211_start_radar_detection,
4234 	.channel_switch = mwifiex_cfg80211_channel_switch,
4235 };
4236 
4237 #ifdef CONFIG_PM
4238 static const struct wiphy_wowlan_support mwifiex_wowlan_support = {
4239 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
4240 		WIPHY_WOWLAN_NET_DETECT | WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
4241 		WIPHY_WOWLAN_GTK_REKEY_FAILURE,
4242 	.n_patterns = MWIFIEX_MEF_MAX_FILTERS,
4243 	.pattern_min_len = 1,
4244 	.pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
4245 	.max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4246 	.max_nd_match_sets = MWIFIEX_MAX_ND_MATCH_SETS,
4247 };
4248 
4249 static const struct wiphy_wowlan_support mwifiex_wowlan_support_no_gtk = {
4250 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
4251 		 WIPHY_WOWLAN_NET_DETECT,
4252 	.n_patterns = MWIFIEX_MEF_MAX_FILTERS,
4253 	.pattern_min_len = 1,
4254 	.pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
4255 	.max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4256 	.max_nd_match_sets = MWIFIEX_MAX_ND_MATCH_SETS,
4257 };
4258 #endif
4259 
mwifiex_is_valid_alpha2(const char * alpha2)4260 static bool mwifiex_is_valid_alpha2(const char *alpha2)
4261 {
4262 	if (!alpha2 || strlen(alpha2) != 2)
4263 		return false;
4264 
4265 	if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
4266 		return true;
4267 
4268 	return false;
4269 }
4270 
4271 static const struct wiphy_coalesce_support mwifiex_coalesce_support = {
4272 	.n_rules = MWIFIEX_COALESCE_MAX_RULES,
4273 	.max_delay = MWIFIEX_MAX_COALESCING_DELAY,
4274 	.n_patterns = MWIFIEX_COALESCE_MAX_FILTERS,
4275 	.pattern_min_len = 1,
4276 	.pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
4277 	.max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4278 };
4279 
mwifiex_init_channel_scan_gap(struct mwifiex_adapter * adapter)4280 int mwifiex_init_channel_scan_gap(struct mwifiex_adapter *adapter)
4281 {
4282 	u32 n_channels_bg, n_channels_a = 0;
4283 
4284 	n_channels_bg = mwifiex_band_2ghz.n_channels;
4285 
4286 	if (adapter->config_bands & BAND_A)
4287 		n_channels_a = mwifiex_band_5ghz.n_channels;
4288 
4289 	/* allocate twice the number total channels, since the driver issues an
4290 	 * additional active scan request for hidden SSIDs on passive channels.
4291 	 */
4292 	adapter->num_in_chan_stats = 2 * (n_channels_bg + n_channels_a);
4293 	adapter->chan_stats = vmalloc(array_size(sizeof(*adapter->chan_stats),
4294 						 adapter->num_in_chan_stats));
4295 
4296 	if (!adapter->chan_stats)
4297 		return -ENOMEM;
4298 
4299 	return 0;
4300 }
4301 
4302 /*
4303  * This function registers the device with CFG802.11 subsystem.
4304  *
4305  * The function creates the wireless device/wiphy, populates it with
4306  * default parameters and handler function pointers, and finally
4307  * registers the device.
4308  */
4309 
mwifiex_register_cfg80211(struct mwifiex_adapter * adapter)4310 int mwifiex_register_cfg80211(struct mwifiex_adapter *adapter)
4311 {
4312 	int ret;
4313 	void *wdev_priv;
4314 	struct wiphy *wiphy;
4315 	struct mwifiex_private *priv = adapter->priv[MWIFIEX_BSS_TYPE_STA];
4316 	u8 *country_code;
4317 	u32 thr, retry;
4318 
4319 	/* create a new wiphy for use with cfg80211 */
4320 	wiphy = wiphy_new(&mwifiex_cfg80211_ops,
4321 			  sizeof(struct mwifiex_adapter *));
4322 	if (!wiphy) {
4323 		mwifiex_dbg(adapter, ERROR,
4324 			    "%s: creating new wiphy\n", __func__);
4325 		return -ENOMEM;
4326 	}
4327 	wiphy->max_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
4328 	wiphy->max_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
4329 	wiphy->mgmt_stypes = mwifiex_mgmt_stypes;
4330 	wiphy->max_remain_on_channel_duration = 5000;
4331 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
4332 				 BIT(NL80211_IFTYPE_P2P_CLIENT) |
4333 				 BIT(NL80211_IFTYPE_P2P_GO) |
4334 				 BIT(NL80211_IFTYPE_AP);
4335 
4336 	if (ISSUPP_ADHOC_ENABLED(adapter->fw_cap_info))
4337 		wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
4338 
4339 	wiphy->bands[NL80211_BAND_2GHZ] = &mwifiex_band_2ghz;
4340 	if (adapter->config_bands & BAND_A)
4341 		wiphy->bands[NL80211_BAND_5GHZ] = &mwifiex_band_5ghz;
4342 	else
4343 		wiphy->bands[NL80211_BAND_5GHZ] = NULL;
4344 
4345 	if (adapter->drcs_enabled && ISSUPP_DRCS_ENABLED(adapter->fw_cap_info))
4346 		wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta_drcs;
4347 	else if (adapter->is_hw_11ac_capable)
4348 		wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta_vht;
4349 	else
4350 		wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta;
4351 	wiphy->n_iface_combinations = 1;
4352 
4353 	if (adapter->max_sta_conn > adapter->max_p2p_conn)
4354 		wiphy->max_ap_assoc_sta = adapter->max_sta_conn;
4355 	else
4356 		wiphy->max_ap_assoc_sta = adapter->max_p2p_conn;
4357 
4358 	/* Initialize cipher suits */
4359 	wiphy->cipher_suites = mwifiex_cipher_suites;
4360 	wiphy->n_cipher_suites = ARRAY_SIZE(mwifiex_cipher_suites);
4361 
4362 	if (adapter->regd) {
4363 		wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
4364 					   REGULATORY_DISABLE_BEACON_HINTS |
4365 					   REGULATORY_COUNTRY_IE_IGNORE;
4366 		wiphy_apply_custom_regulatory(wiphy, adapter->regd);
4367 	}
4368 
4369 	ether_addr_copy(wiphy->perm_addr, adapter->perm_addr);
4370 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
4371 	wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
4372 			WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
4373 			WIPHY_FLAG_AP_UAPSD |
4374 			WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
4375 			WIPHY_FLAG_HAS_CHANNEL_SWITCH |
4376 			WIPHY_FLAG_PS_ON_BY_DEFAULT;
4377 
4378 	if (ISSUPP_TDLS_ENABLED(adapter->fw_cap_info))
4379 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
4380 				WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
4381 
4382 #ifdef CONFIG_PM
4383 	if (ISSUPP_FIRMWARE_SUPPLICANT(priv->adapter->fw_cap_info))
4384 		wiphy->wowlan = &mwifiex_wowlan_support;
4385 	else
4386 		wiphy->wowlan = &mwifiex_wowlan_support_no_gtk;
4387 #endif
4388 
4389 	wiphy->coalesce = &mwifiex_coalesce_support;
4390 
4391 	wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
4392 				    NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
4393 				    NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
4394 
4395 	wiphy->max_sched_scan_reqs = 1;
4396 	wiphy->max_sched_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
4397 	wiphy->max_sched_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
4398 	wiphy->max_match_sets = MWIFIEX_MAX_SSID_LIST_LENGTH;
4399 
4400 	wiphy->available_antennas_tx = BIT(adapter->number_of_antenna) - 1;
4401 	wiphy->available_antennas_rx = BIT(adapter->number_of_antenna) - 1;
4402 
4403 	wiphy->features |= NL80211_FEATURE_INACTIVITY_TIMER |
4404 			   NL80211_FEATURE_LOW_PRIORITY_SCAN |
4405 			   NL80211_FEATURE_NEED_OBSS_SCAN;
4406 
4407 	if (ISSUPP_ADHOC_ENABLED(adapter->fw_cap_info))
4408 		wiphy->features |= NL80211_FEATURE_HT_IBSS;
4409 
4410 	if (ISSUPP_RANDOM_MAC(adapter->fw_cap_info))
4411 		wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
4412 				   NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
4413 				   NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
4414 
4415 	if (ISSUPP_TDLS_ENABLED(adapter->fw_cap_info))
4416 		wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
4417 
4418 	if (adapter->fw_api_ver == MWIFIEX_FW_V15)
4419 		wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
4420 
4421 	/* Reserve space for mwifiex specific private data for BSS */
4422 	wiphy->bss_priv_size = sizeof(struct mwifiex_bss_priv);
4423 
4424 	wiphy->reg_notifier = mwifiex_reg_notifier;
4425 
4426 	/* Set struct mwifiex_adapter pointer in wiphy_priv */
4427 	wdev_priv = wiphy_priv(wiphy);
4428 	*(unsigned long *)wdev_priv = (unsigned long)adapter;
4429 
4430 	set_wiphy_dev(wiphy, priv->adapter->dev);
4431 
4432 	ret = wiphy_register(wiphy);
4433 	if (ret < 0) {
4434 		mwifiex_dbg(adapter, ERROR,
4435 			    "%s: wiphy_register failed: %d\n", __func__, ret);
4436 		wiphy_free(wiphy);
4437 		return ret;
4438 	}
4439 
4440 	if (!adapter->regd) {
4441 		if (reg_alpha2 && mwifiex_is_valid_alpha2(reg_alpha2)) {
4442 			mwifiex_dbg(adapter, INFO,
4443 				    "driver hint alpha2: %2.2s\n", reg_alpha2);
4444 			regulatory_hint(wiphy, reg_alpha2);
4445 		} else {
4446 			if (adapter->region_code == 0x00) {
4447 				mwifiex_dbg(adapter, WARN,
4448 					    "Ignore world regulatory domain\n");
4449 			} else {
4450 				wiphy->regulatory_flags |=
4451 					REGULATORY_DISABLE_BEACON_HINTS |
4452 					REGULATORY_COUNTRY_IE_IGNORE;
4453 				country_code =
4454 					mwifiex_11d_code_2_region(
4455 						adapter->region_code);
4456 				if (country_code &&
4457 				    regulatory_hint(wiphy, country_code))
4458 					mwifiex_dbg(priv->adapter, ERROR,
4459 						    "regulatory_hint() failed\n");
4460 			}
4461 		}
4462 	}
4463 
4464 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4465 			 HostCmd_ACT_GEN_GET, FRAG_THRESH_I, &thr, true);
4466 	wiphy->frag_threshold = thr;
4467 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4468 			 HostCmd_ACT_GEN_GET, RTS_THRESH_I, &thr, true);
4469 	wiphy->rts_threshold = thr;
4470 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4471 			 HostCmd_ACT_GEN_GET, SHORT_RETRY_LIM_I, &retry, true);
4472 	wiphy->retry_short = (u8) retry;
4473 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4474 			 HostCmd_ACT_GEN_GET, LONG_RETRY_LIM_I, &retry, true);
4475 	wiphy->retry_long = (u8) retry;
4476 
4477 	adapter->wiphy = wiphy;
4478 	return ret;
4479 }
4480