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