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