xref: /linux/drivers/net/wireless/microchip/wilc1000/cfg80211.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4  * All rights reserved.
5  */
6 
7 #include "cfg80211.h"
8 
9 #define GO_NEG_REQ			0x00
10 #define GO_NEG_RSP			0x01
11 #define GO_NEG_CONF			0x02
12 #define P2P_INV_REQ			0x03
13 #define P2P_INV_RSP			0x04
14 
15 #define WILC_INVALID_CHANNEL		0
16 
17 /* Operation at 2.4 GHz with channels 1-13 */
18 #define WILC_WLAN_OPERATING_CLASS_2_4GHZ		0x51
19 
20 static const struct ieee80211_txrx_stypes
21 	wilc_wfi_cfg80211_mgmt_types[NUM_NL80211_IFTYPES] = {
22 	[NL80211_IFTYPE_STATION] = {
23 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
24 			BIT(IEEE80211_STYPE_AUTH >> 4),
25 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
26 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
27 			BIT(IEEE80211_STYPE_AUTH >> 4)
28 	},
29 	[NL80211_IFTYPE_AP] = {
30 		.tx = 0xffff,
31 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
32 			BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
33 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
34 			BIT(IEEE80211_STYPE_DISASSOC >> 4) |
35 			BIT(IEEE80211_STYPE_AUTH >> 4) |
36 			BIT(IEEE80211_STYPE_DEAUTH >> 4) |
37 			BIT(IEEE80211_STYPE_ACTION >> 4)
38 	},
39 	[NL80211_IFTYPE_P2P_CLIENT] = {
40 		.tx = 0xffff,
41 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
42 			BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
43 			BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
44 			BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
45 			BIT(IEEE80211_STYPE_DISASSOC >> 4) |
46 			BIT(IEEE80211_STYPE_AUTH >> 4) |
47 			BIT(IEEE80211_STYPE_DEAUTH >> 4)
48 	}
49 };
50 
51 #ifdef CONFIG_PM
52 static const struct wiphy_wowlan_support wowlan_support = {
53 	.flags = WIPHY_WOWLAN_ANY
54 };
55 #endif
56 
57 struct wilc_p2p_mgmt_data {
58 	int size;
59 	u8 *buff;
60 };
61 
62 struct wilc_p2p_pub_act_frame {
63 	u8 category;
64 	u8 action;
65 	u8 oui[3];
66 	u8 oui_type;
67 	u8 oui_subtype;
68 	u8 dialog_token;
69 	u8 elem[];
70 } __packed;
71 
72 struct wilc_vendor_specific_ie {
73 	u8 tag_number;
74 	u8 tag_len;
75 	u8 oui[3];
76 	u8 oui_type;
77 	u8 attr[];
78 } __packed;
79 
80 struct wilc_attr_entry {
81 	u8  attr_type;
82 	__le16 attr_len;
83 	u8 val[];
84 } __packed;
85 
86 struct wilc_attr_oper_ch {
87 	u8 attr_type;
88 	__le16 attr_len;
89 	u8 country_code[IEEE80211_COUNTRY_STRING_LEN];
90 	u8 op_class;
91 	u8 op_channel;
92 } __packed;
93 
94 struct wilc_attr_ch_list {
95 	u8 attr_type;
96 	__le16 attr_len;
97 	u8 country_code[IEEE80211_COUNTRY_STRING_LEN];
98 	u8 elem[];
99 } __packed;
100 
101 struct wilc_ch_list_elem {
102 	u8 op_class;
103 	u8 no_of_channels;
104 	u8 ch_list[];
105 } __packed;
106 
cfg_scan_result(enum scan_event scan_event,struct wilc_rcvd_net_info * info,struct wilc_priv * priv)107 static void cfg_scan_result(enum scan_event scan_event,
108 			    struct wilc_rcvd_net_info *info,
109 			    struct wilc_priv *priv)
110 {
111 	if (!priv->cfg_scanning)
112 		return;
113 
114 	if (scan_event == SCAN_EVENT_NETWORK_FOUND) {
115 		s32 freq;
116 		struct ieee80211_channel *channel;
117 		struct cfg80211_bss *bss;
118 		struct wiphy *wiphy = priv->dev->ieee80211_ptr->wiphy;
119 
120 		if (!wiphy || !info)
121 			return;
122 
123 		freq = ieee80211_channel_to_frequency((s32)info->ch,
124 						      NL80211_BAND_2GHZ);
125 		channel = ieee80211_get_channel(wiphy, freq);
126 		if (!channel)
127 			return;
128 
129 		bss = cfg80211_inform_bss_frame(wiphy, channel, info->mgmt,
130 						info->frame_len,
131 						(s32)info->rssi * 100,
132 						GFP_KERNEL);
133 		cfg80211_put_bss(wiphy, bss);
134 	} else if (scan_event == SCAN_EVENT_DONE) {
135 		mutex_lock(&priv->scan_req_lock);
136 
137 		if (priv->scan_req) {
138 			struct cfg80211_scan_info info = {
139 				.aborted = false,
140 			};
141 
142 			cfg80211_scan_done(priv->scan_req, &info);
143 			priv->cfg_scanning = false;
144 			priv->scan_req = NULL;
145 		}
146 		mutex_unlock(&priv->scan_req_lock);
147 	} else if (scan_event == SCAN_EVENT_ABORTED) {
148 		mutex_lock(&priv->scan_req_lock);
149 
150 		if (priv->scan_req) {
151 			struct cfg80211_scan_info info = {
152 				.aborted = false,
153 			};
154 
155 			cfg80211_scan_done(priv->scan_req, &info);
156 			priv->cfg_scanning = false;
157 			priv->scan_req = NULL;
158 		}
159 		mutex_unlock(&priv->scan_req_lock);
160 	}
161 }
162 
cfg_connect_result(enum conn_event conn_disconn_evt,u8 mac_status,struct wilc_priv * priv)163 static void cfg_connect_result(enum conn_event conn_disconn_evt, u8 mac_status,
164 			       struct wilc_priv *priv)
165 {
166 	struct net_device *dev = priv->dev;
167 	struct wilc_vif *vif = netdev_priv(dev);
168 	struct wilc *wl = vif->wilc;
169 	struct host_if_drv *wfi_drv = priv->hif_drv;
170 	struct wilc_conn_info *conn_info = &wfi_drv->conn_info;
171 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
172 
173 	vif->connecting = false;
174 
175 	if (conn_disconn_evt == CONN_DISCONN_EVENT_CONN_RESP) {
176 		u16 connect_status = conn_info->status;
177 
178 		if (mac_status == WILC_MAC_STATUS_DISCONNECTED &&
179 		    connect_status == WLAN_STATUS_SUCCESS) {
180 			connect_status = WLAN_STATUS_UNSPECIFIED_FAILURE;
181 			wilc_wlan_set_bssid(priv->dev, NULL, WILC_STATION_MODE);
182 
183 			if (vif->iftype != WILC_CLIENT_MODE)
184 				wl->sta_ch = WILC_INVALID_CHANNEL;
185 
186 			netdev_err(dev, "Unspecified failure\n");
187 		}
188 
189 		if (connect_status == WLAN_STATUS_SUCCESS)
190 			memcpy(priv->associated_bss, conn_info->bssid,
191 			       ETH_ALEN);
192 
193 		cfg80211_ref_bss(wiphy, vif->bss);
194 		cfg80211_connect_bss(dev, conn_info->bssid, vif->bss,
195 				     conn_info->req_ies,
196 				     conn_info->req_ies_len,
197 				     conn_info->resp_ies,
198 				     conn_info->resp_ies_len,
199 				     connect_status, GFP_KERNEL,
200 				     NL80211_TIMEOUT_UNSPECIFIED);
201 
202 		vif->bss = NULL;
203 	} else if (conn_disconn_evt == CONN_DISCONN_EVENT_DISCONN_NOTIF) {
204 		u16 reason = 0;
205 
206 		eth_zero_addr(priv->associated_bss);
207 		wilc_wlan_set_bssid(priv->dev, NULL, WILC_STATION_MODE);
208 
209 		if (vif->iftype != WILC_CLIENT_MODE) {
210 			wl->sta_ch = WILC_INVALID_CHANNEL;
211 		} else {
212 			if (wfi_drv->ifc_up)
213 				reason = 3;
214 			else
215 				reason = 1;
216 		}
217 
218 		cfg80211_disconnected(dev, reason, NULL, 0, false, GFP_KERNEL);
219 	}
220 }
221 
wilc_get_wl_to_vif(struct wilc * wl)222 struct wilc_vif *wilc_get_wl_to_vif(struct wilc *wl)
223 {
224 	struct wilc_vif *vif;
225 
226 	vif = list_first_or_null_rcu(&wl->vif_list, typeof(*vif), list);
227 	if (!vif)
228 		return ERR_PTR(-EINVAL);
229 
230 	return vif;
231 }
232 
set_channel(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_chan_def * chandef)233 static int set_channel(struct wiphy *wiphy,
234 		       struct net_device *dev,
235 		       struct cfg80211_chan_def *chandef)
236 {
237 	struct wilc *wl = wiphy_priv(wiphy);
238 	struct wilc_vif *vif;
239 	u32 channelnum;
240 	int result;
241 	int srcu_idx;
242 
243 	srcu_idx = srcu_read_lock(&wl->srcu);
244 	vif = wilc_get_wl_to_vif(wl);
245 	if (IS_ERR(vif)) {
246 		srcu_read_unlock(&wl->srcu, srcu_idx);
247 		return PTR_ERR(vif);
248 	}
249 
250 	channelnum = ieee80211_frequency_to_channel(chandef->chan->center_freq);
251 
252 	wl->op_ch = channelnum;
253 	result = wilc_set_mac_chnl_num(vif, channelnum);
254 	if (result)
255 		netdev_err(vif->ndev, "Error in setting channel\n");
256 
257 	srcu_read_unlock(&wl->srcu, srcu_idx);
258 	return result;
259 }
260 
scan(struct wiphy * wiphy,struct cfg80211_scan_request * request)261 static int scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
262 {
263 	struct wilc_vif *vif = netdev_priv(request->wdev->netdev);
264 	struct wilc_priv *priv = &vif->priv;
265 	u32 i;
266 	int ret = 0;
267 	u8 scan_ch_list[WILC_MAX_NUM_SCANNED_CH];
268 	u8 scan_type;
269 
270 	if (request->n_channels > WILC_MAX_NUM_SCANNED_CH) {
271 		netdev_err(vif->ndev, "Requested scanned channels over\n");
272 		return -EINVAL;
273 	}
274 
275 	priv->scan_req = request;
276 	priv->cfg_scanning = true;
277 	for (i = 0; i < request->n_channels; i++) {
278 		u16 freq = request->channels[i]->center_freq;
279 
280 		scan_ch_list[i] = ieee80211_frequency_to_channel(freq);
281 	}
282 
283 	if (request->n_ssids)
284 		scan_type = WILC_FW_ACTIVE_SCAN;
285 	else
286 		scan_type = WILC_FW_PASSIVE_SCAN;
287 
288 	ret = wilc_scan(vif, WILC_FW_USER_SCAN, scan_type,
289 			scan_ch_list, cfg_scan_result, request);
290 
291 	if (ret) {
292 		priv->scan_req = NULL;
293 		priv->cfg_scanning = false;
294 	}
295 
296 	return ret;
297 }
298 
connect(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_connect_params * sme)299 static int connect(struct wiphy *wiphy, struct net_device *dev,
300 		   struct cfg80211_connect_params *sme)
301 {
302 	struct wilc_vif *vif = netdev_priv(dev);
303 	struct wilc_priv *priv = &vif->priv;
304 	struct host_if_drv *wfi_drv = priv->hif_drv;
305 	int ret;
306 	u32 i;
307 	u8 security = WILC_FW_SEC_NO;
308 	enum mfptype mfp_type = WILC_FW_MFP_NONE;
309 	enum authtype auth_type = WILC_FW_AUTH_ANY;
310 	u32 cipher_group;
311 	struct cfg80211_bss *bss;
312 	void *join_params;
313 	u8 ch;
314 
315 	vif->connecting = true;
316 
317 	cipher_group = sme->crypto.cipher_group;
318 	if (cipher_group != 0) {
319 		if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_2) {
320 			if (cipher_group == WLAN_CIPHER_SUITE_TKIP)
321 				security = WILC_FW_SEC_WPA2_TKIP;
322 			else
323 				security = WILC_FW_SEC_WPA2_AES;
324 		} else if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_1) {
325 			if (cipher_group == WLAN_CIPHER_SUITE_TKIP)
326 				security = WILC_FW_SEC_WPA_TKIP;
327 			else
328 				security = WILC_FW_SEC_WPA_AES;
329 		} else {
330 			ret = -ENOTSUPP;
331 			netdev_err(dev, "%s: Unsupported cipher\n",
332 				   __func__);
333 			goto out_error;
334 		}
335 	}
336 
337 	if ((sme->crypto.wpa_versions & NL80211_WPA_VERSION_1) ||
338 	    (sme->crypto.wpa_versions & NL80211_WPA_VERSION_2)) {
339 		for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++) {
340 			u32 ciphers_pairwise = sme->crypto.ciphers_pairwise[i];
341 
342 			if (ciphers_pairwise == WLAN_CIPHER_SUITE_TKIP)
343 				security |= WILC_FW_TKIP;
344 			else
345 				security |= WILC_FW_AES;
346 		}
347 	}
348 
349 	switch (sme->auth_type) {
350 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
351 		auth_type = WILC_FW_AUTH_OPEN_SYSTEM;
352 		break;
353 
354 	case NL80211_AUTHTYPE_SAE:
355 		auth_type = WILC_FW_AUTH_SAE;
356 		if (sme->ssid_len) {
357 			memcpy(vif->auth.ssid.ssid, sme->ssid, sme->ssid_len);
358 			vif->auth.ssid.ssid_len = sme->ssid_len;
359 		}
360 		vif->auth.key_mgmt_suite = sme->crypto.akm_suites[0];
361 		ether_addr_copy(vif->auth.bssid, sme->bssid);
362 		break;
363 
364 	default:
365 		break;
366 	}
367 
368 	if (sme->crypto.n_akm_suites) {
369 		if (sme->crypto.akm_suites[0] == WLAN_AKM_SUITE_8021X)
370 			auth_type = WILC_FW_AUTH_IEEE8021;
371 		else if (sme->crypto.akm_suites[0] == WLAN_AKM_SUITE_PSK_SHA256)
372 			auth_type = WILC_FW_AUTH_OPEN_SYSTEM_SHA256;
373 		else if (sme->crypto.akm_suites[0] == WLAN_AKM_SUITE_8021X_SHA256)
374 			auth_type = WILC_FW_AUTH_IEE8021X_SHA256;
375 	}
376 
377 	if (wfi_drv->usr_scan_req.scan_result) {
378 		netdev_err(vif->ndev, "%s: Scan in progress\n", __func__);
379 		ret = -EBUSY;
380 		goto out_error;
381 	}
382 
383 	bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid, sme->ssid,
384 			       sme->ssid_len, IEEE80211_BSS_TYPE_ANY,
385 			       IEEE80211_PRIVACY(sme->privacy));
386 	if (!bss) {
387 		ret = -EINVAL;
388 		goto out_error;
389 	}
390 
391 	if (ether_addr_equal_unaligned(vif->bssid, bss->bssid)) {
392 		ret = -EALREADY;
393 		goto out_put_bss;
394 	}
395 
396 	join_params = wilc_parse_join_bss_param(bss, &sme->crypto);
397 	if (!join_params) {
398 		netdev_err(dev, "%s: failed to construct join param\n",
399 			   __func__);
400 		ret = -EINVAL;
401 		goto out_put_bss;
402 	}
403 
404 	ch = ieee80211_frequency_to_channel(bss->channel->center_freq);
405 	vif->wilc->op_ch = ch;
406 	if (vif->iftype != WILC_CLIENT_MODE)
407 		vif->wilc->sta_ch = ch;
408 
409 	wilc_wlan_set_bssid(dev, bss->bssid, WILC_STATION_MODE);
410 
411 	wfi_drv->conn_info.security = security;
412 	wfi_drv->conn_info.auth_type = auth_type;
413 	wfi_drv->conn_info.conn_result = cfg_connect_result;
414 	wfi_drv->conn_info.priv = priv;
415 	wfi_drv->conn_info.param = join_params;
416 
417 	if (sme->mfp == NL80211_MFP_OPTIONAL)
418 		mfp_type = WILC_FW_MFP_OPTIONAL;
419 	else if (sme->mfp == NL80211_MFP_REQUIRED)
420 		mfp_type = WILC_FW_MFP_REQUIRED;
421 
422 	wfi_drv->conn_info.mfp_type = mfp_type;
423 
424 	ret = wilc_set_join_req(vif, bss->bssid, sme->ie, sme->ie_len);
425 	if (ret) {
426 		netdev_err(dev, "wilc_set_join_req(): Error\n");
427 		ret = -ENOENT;
428 		if (vif->iftype != WILC_CLIENT_MODE)
429 			vif->wilc->sta_ch = WILC_INVALID_CHANNEL;
430 		wilc_wlan_set_bssid(dev, NULL, WILC_STATION_MODE);
431 		wfi_drv->conn_info.conn_result = NULL;
432 		kfree(join_params);
433 		goto out_put_bss;
434 	}
435 	kfree(join_params);
436 	vif->bss = bss;
437 	cfg80211_put_bss(wiphy, bss);
438 	return 0;
439 
440 out_put_bss:
441 	cfg80211_put_bss(wiphy, bss);
442 
443 out_error:
444 	vif->connecting = false;
445 	return ret;
446 }
447 
disconnect(struct wiphy * wiphy,struct net_device * dev,u16 reason_code)448 static int disconnect(struct wiphy *wiphy, struct net_device *dev,
449 		      u16 reason_code)
450 {
451 	struct wilc_vif *vif = netdev_priv(dev);
452 	struct wilc_priv *priv = &vif->priv;
453 	struct wilc *wilc = vif->wilc;
454 	int ret;
455 
456 	vif->connecting = false;
457 
458 	if (!wilc)
459 		return -EIO;
460 
461 	if (wilc->close) {
462 		/* already disconnected done */
463 		cfg80211_disconnected(dev, 0, NULL, 0, true, GFP_KERNEL);
464 		return 0;
465 	}
466 
467 	if (vif->iftype != WILC_CLIENT_MODE)
468 		wilc->sta_ch = WILC_INVALID_CHANNEL;
469 	wilc_wlan_set_bssid(priv->dev, NULL, WILC_STATION_MODE);
470 
471 	priv->hif_drv->p2p_timeout = 0;
472 
473 	ret = wilc_disconnect(vif);
474 	if (ret != 0) {
475 		netdev_err(priv->dev, "Error in disconnecting\n");
476 		ret = -EINVAL;
477 	}
478 
479 	vif->bss = NULL;
480 
481 	return ret;
482 }
483 
wilc_wfi_cfg_allocate_wpa_entry(struct wilc_priv * priv,u8 idx)484 static int wilc_wfi_cfg_allocate_wpa_entry(struct wilc_priv *priv, u8 idx)
485 {
486 	if (!priv->wilc_gtk[idx]) {
487 		priv->wilc_gtk[idx] = kzalloc_obj(*priv->wilc_gtk[idx]);
488 		if (!priv->wilc_gtk[idx])
489 			return -ENOMEM;
490 	}
491 
492 	if (!priv->wilc_ptk[idx]) {
493 		priv->wilc_ptk[idx] = kzalloc_obj(*priv->wilc_ptk[idx]);
494 		if (!priv->wilc_ptk[idx])
495 			return -ENOMEM;
496 	}
497 
498 	return 0;
499 }
500 
wilc_wfi_cfg_allocate_wpa_igtk_entry(struct wilc_priv * priv,u8 idx)501 static int wilc_wfi_cfg_allocate_wpa_igtk_entry(struct wilc_priv *priv, u8 idx)
502 {
503 	idx -= 4;
504 	if (!priv->wilc_igtk[idx]) {
505 		priv->wilc_igtk[idx] = kzalloc_obj(*priv->wilc_igtk[idx]);
506 		if (!priv->wilc_igtk[idx])
507 			return -ENOMEM;
508 	}
509 	return 0;
510 }
511 
wilc_wfi_cfg_copy_wpa_info(struct wilc_wfi_key * key_info,struct key_params * params)512 static int wilc_wfi_cfg_copy_wpa_info(struct wilc_wfi_key *key_info,
513 				      struct key_params *params)
514 {
515 	kfree(key_info->key);
516 
517 	key_info->key = kmemdup(params->key, params->key_len, GFP_KERNEL);
518 	if (!key_info->key)
519 		return -ENOMEM;
520 
521 	kfree(key_info->seq);
522 
523 	if (params->seq_len > 0) {
524 		key_info->seq = kmemdup(params->seq, params->seq_len,
525 					GFP_KERNEL);
526 		if (!key_info->seq)
527 			return -ENOMEM;
528 	}
529 
530 	key_info->cipher = params->cipher;
531 	key_info->key_len = params->key_len;
532 	key_info->seq_len = params->seq_len;
533 
534 	return 0;
535 }
536 
add_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr,struct key_params * params)537 static int add_key(struct wiphy *wiphy, struct net_device *netdev, int link_id,
538 		   u8 key_index, bool pairwise, const u8 *mac_addr,
539 		   struct key_params *params)
540 
541 {
542 	int ret = 0, keylen = params->key_len;
543 	const u8 *rx_mic = NULL;
544 	const u8 *tx_mic = NULL;
545 	u8 mode = WILC_FW_SEC_NO;
546 	u8 op_mode;
547 	struct wilc_vif *vif = netdev_priv(netdev);
548 	struct wilc_priv *priv = &vif->priv;
549 	struct wilc_wfi_key *key;
550 
551 	switch (params->cipher) {
552 	case WLAN_CIPHER_SUITE_TKIP:
553 	case WLAN_CIPHER_SUITE_CCMP:
554 		if (priv->wdev.iftype == NL80211_IFTYPE_AP ||
555 		    priv->wdev.iftype == NL80211_IFTYPE_P2P_GO) {
556 			struct wilc_wfi_key *key;
557 
558 			ret = wilc_wfi_cfg_allocate_wpa_entry(priv, key_index);
559 			if (ret)
560 				return -ENOMEM;
561 
562 			if (params->key_len > 16 &&
563 			    params->cipher == WLAN_CIPHER_SUITE_TKIP) {
564 				tx_mic = params->key + 24;
565 				rx_mic = params->key + 16;
566 				keylen = params->key_len - 16;
567 			}
568 
569 			if (!pairwise) {
570 				if (params->cipher == WLAN_CIPHER_SUITE_TKIP)
571 					mode = WILC_FW_SEC_WPA_TKIP;
572 				else
573 					mode = WILC_FW_SEC_WPA2_AES;
574 
575 				priv->wilc_groupkey = mode;
576 
577 				key = priv->wilc_gtk[key_index];
578 			} else {
579 				if (params->cipher == WLAN_CIPHER_SUITE_TKIP)
580 					mode = WILC_FW_SEC_WPA_TKIP;
581 				else
582 					mode = priv->wilc_groupkey | WILC_FW_AES;
583 
584 				key = priv->wilc_ptk[key_index];
585 			}
586 			ret = wilc_wfi_cfg_copy_wpa_info(key, params);
587 			if (ret)
588 				return -ENOMEM;
589 
590 			op_mode = WILC_AP_MODE;
591 		} else {
592 			if (params->key_len > 16 &&
593 			    params->cipher == WLAN_CIPHER_SUITE_TKIP) {
594 				rx_mic = params->key + 24;
595 				tx_mic = params->key + 16;
596 				keylen = params->key_len - 16;
597 			}
598 
599 			op_mode = WILC_STATION_MODE;
600 		}
601 
602 		if (!pairwise)
603 			ret = wilc_add_rx_gtk(vif, params->key, keylen,
604 					      key_index, params->seq_len,
605 					      params->seq, rx_mic, tx_mic,
606 					      op_mode, mode);
607 		else
608 			ret = wilc_add_ptk(vif, params->key, keylen, mac_addr,
609 					   rx_mic, tx_mic, op_mode, mode,
610 					   key_index);
611 
612 		break;
613 	case WLAN_CIPHER_SUITE_AES_CMAC:
614 		ret = wilc_wfi_cfg_allocate_wpa_igtk_entry(priv, key_index);
615 		if (ret)
616 			return -ENOMEM;
617 
618 		key = priv->wilc_igtk[key_index - 4];
619 		ret = wilc_wfi_cfg_copy_wpa_info(key, params);
620 		if (ret)
621 			return -ENOMEM;
622 
623 		if (priv->wdev.iftype == NL80211_IFTYPE_AP ||
624 		    priv->wdev.iftype == NL80211_IFTYPE_P2P_GO)
625 			op_mode = WILC_AP_MODE;
626 		else
627 			op_mode = WILC_STATION_MODE;
628 
629 		ret = wilc_add_igtk(vif, params->key, keylen, params->seq,
630 				    params->seq_len, mac_addr, op_mode,
631 				    key_index);
632 		break;
633 
634 	default:
635 		netdev_err(netdev, "%s: Unsupported cipher\n", __func__);
636 		ret = -ENOTSUPP;
637 	}
638 
639 	return ret;
640 }
641 
del_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr)642 static int del_key(struct wiphy *wiphy, struct net_device *netdev, int link_id,
643 		   u8 key_index,
644 		   bool pairwise,
645 		   const u8 *mac_addr)
646 {
647 	struct wilc_vif *vif = netdev_priv(netdev);
648 	struct wilc_priv *priv = &vif->priv;
649 
650 	if (!pairwise && (key_index == 4 || key_index == 5)) {
651 		key_index -= 4;
652 		if (priv->wilc_igtk[key_index]) {
653 			kfree(priv->wilc_igtk[key_index]->key);
654 			priv->wilc_igtk[key_index]->key = NULL;
655 			kfree(priv->wilc_igtk[key_index]->seq);
656 			priv->wilc_igtk[key_index]->seq = NULL;
657 			kfree(priv->wilc_igtk[key_index]);
658 			priv->wilc_igtk[key_index] = NULL;
659 		}
660 	} else {
661 		if (priv->wilc_gtk[key_index]) {
662 			kfree(priv->wilc_gtk[key_index]->key);
663 			priv->wilc_gtk[key_index]->key = NULL;
664 			kfree(priv->wilc_gtk[key_index]->seq);
665 			priv->wilc_gtk[key_index]->seq = NULL;
666 
667 			kfree(priv->wilc_gtk[key_index]);
668 			priv->wilc_gtk[key_index] = NULL;
669 		}
670 		if (priv->wilc_ptk[key_index]) {
671 			kfree(priv->wilc_ptk[key_index]->key);
672 			priv->wilc_ptk[key_index]->key = NULL;
673 			kfree(priv->wilc_ptk[key_index]->seq);
674 			priv->wilc_ptk[key_index]->seq = NULL;
675 			kfree(priv->wilc_ptk[key_index]);
676 			priv->wilc_ptk[key_index] = NULL;
677 		}
678 	}
679 
680 	return 0;
681 }
682 
get_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr,void * cookie,void (* callback)(void * cookie,struct key_params *))683 static int get_key(struct wiphy *wiphy, struct net_device *netdev, int link_id,
684 		   u8 key_index, bool pairwise, const u8 *mac_addr,
685 		   void *cookie,
686 		   void (*callback)(void *cookie, struct key_params *))
687 {
688 	struct wilc_vif *vif = netdev_priv(netdev);
689 	struct wilc_priv *priv = &vif->priv;
690 	struct  key_params key_params;
691 
692 	if (!pairwise) {
693 		if (key_index == 4 || key_index == 5) {
694 			key_index -= 4;
695 			key_params.key = priv->wilc_igtk[key_index]->key;
696 			key_params.cipher = priv->wilc_igtk[key_index]->cipher;
697 			key_params.key_len = priv->wilc_igtk[key_index]->key_len;
698 			key_params.seq = priv->wilc_igtk[key_index]->seq;
699 			key_params.seq_len = priv->wilc_igtk[key_index]->seq_len;
700 		} else {
701 			key_params.key = priv->wilc_gtk[key_index]->key;
702 			key_params.cipher = priv->wilc_gtk[key_index]->cipher;
703 			key_params.key_len = priv->wilc_gtk[key_index]->key_len;
704 			key_params.seq = priv->wilc_gtk[key_index]->seq;
705 			key_params.seq_len = priv->wilc_gtk[key_index]->seq_len;
706 		}
707 	} else {
708 		key_params.key = priv->wilc_ptk[key_index]->key;
709 		key_params.cipher = priv->wilc_ptk[key_index]->cipher;
710 		key_params.key_len = priv->wilc_ptk[key_index]->key_len;
711 		key_params.seq = priv->wilc_ptk[key_index]->seq;
712 		key_params.seq_len = priv->wilc_ptk[key_index]->seq_len;
713 	}
714 
715 	callback(cookie, &key_params);
716 
717 	return 0;
718 }
719 
720 /* wiphy_new_nm() will WARNON if not present */
set_default_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool unicast,bool multicast)721 static int set_default_key(struct wiphy *wiphy, struct net_device *netdev,
722 			   int link_id, u8 key_index, bool unicast,
723 			   bool multicast)
724 {
725 	return 0;
726 }
727 
set_default_mgmt_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index)728 static int set_default_mgmt_key(struct wiphy *wiphy, struct net_device *netdev,
729 				int link_id, u8 key_index)
730 {
731 	struct wilc_vif *vif = netdev_priv(netdev);
732 
733 	return wilc_set_default_mgmt_key_index(vif, key_index);
734 }
735 
get_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_info * sinfo)736 static int get_station(struct wiphy *wiphy, struct net_device *dev,
737 		       const u8 *mac, struct station_info *sinfo)
738 {
739 	struct wilc_vif *vif = netdev_priv(dev);
740 	struct wilc_priv *priv = &vif->priv;
741 	struct wilc *wilc = vif->wilc;
742 	u32 i = 0;
743 	u32 associatedsta = ~0;
744 	u32 inactive_time = 0;
745 
746 	if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE) {
747 		for (i = 0; i < NUM_STA_ASSOCIATED; i++) {
748 			if (!(memcmp(mac,
749 				     priv->assoc_stainfo.sta_associated_bss[i],
750 				     ETH_ALEN))) {
751 				associatedsta = i;
752 				break;
753 			}
754 		}
755 
756 		if (associatedsta == ~0) {
757 			netdev_err(dev, "sta required is not associated\n");
758 			return -ENOENT;
759 		}
760 
761 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME);
762 
763 		wilc_get_inactive_time(vif, mac, &inactive_time);
764 		sinfo->inactive_time = 1000 * inactive_time;
765 	} else if (vif->iftype == WILC_STATION_MODE) {
766 		struct rf_info stats;
767 
768 		if (!wilc->initialized)
769 			return -EBUSY;
770 
771 		wilc_get_statistics(vif, &stats);
772 
773 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL) |
774 				 BIT_ULL(NL80211_STA_INFO_RX_PACKETS) |
775 				 BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
776 				 BIT_ULL(NL80211_STA_INFO_TX_FAILED) |
777 				 BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
778 
779 		sinfo->signal = stats.rssi;
780 		sinfo->rx_packets = stats.rx_cnt;
781 		sinfo->tx_packets = stats.tx_cnt + stats.tx_fail_cnt;
782 		sinfo->tx_failed = stats.tx_fail_cnt;
783 		sinfo->txrate.legacy = stats.link_speed * 10;
784 
785 		if (stats.link_speed > TCP_ACK_FILTER_LINK_SPEED_THRESH &&
786 		    stats.link_speed != DEFAULT_LINK_SPEED)
787 			wilc_enable_tcp_ack_filter(vif, true);
788 		else if (stats.link_speed != DEFAULT_LINK_SPEED)
789 			wilc_enable_tcp_ack_filter(vif, false);
790 	}
791 	return 0;
792 }
793 
set_wiphy_params(struct wiphy * wiphy,int radio_idx,u32 changed)794 static int set_wiphy_params(struct wiphy *wiphy, int radio_idx, u32 changed)
795 {
796 	int ret = -EINVAL;
797 	struct cfg_param_attr cfg_param_val;
798 	struct wilc *wl = wiphy_priv(wiphy);
799 	struct wilc_vif *vif;
800 	struct wilc_priv *priv;
801 	int srcu_idx;
802 
803 	srcu_idx = srcu_read_lock(&wl->srcu);
804 	vif = wilc_get_wl_to_vif(wl);
805 	if (IS_ERR(vif))
806 		goto out;
807 
808 	priv = &vif->priv;
809 	cfg_param_val.flag = 0;
810 
811 	if (changed & WIPHY_PARAM_RETRY_SHORT) {
812 		netdev_dbg(vif->ndev,
813 			   "Setting WIPHY_PARAM_RETRY_SHORT %d\n",
814 			   wiphy->retry_short);
815 		cfg_param_val.flag  |= WILC_CFG_PARAM_RETRY_SHORT;
816 		cfg_param_val.short_retry_limit = wiphy->retry_short;
817 	}
818 	if (changed & WIPHY_PARAM_RETRY_LONG) {
819 		netdev_dbg(vif->ndev,
820 			   "Setting WIPHY_PARAM_RETRY_LONG %d\n",
821 			   wiphy->retry_long);
822 		cfg_param_val.flag |= WILC_CFG_PARAM_RETRY_LONG;
823 		cfg_param_val.long_retry_limit = wiphy->retry_long;
824 	}
825 	if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
826 		if (wiphy->frag_threshold > 255 &&
827 		    wiphy->frag_threshold < 7937) {
828 			netdev_dbg(vif->ndev,
829 				   "Setting WIPHY_PARAM_FRAG_THRESHOLD %d\n",
830 				   wiphy->frag_threshold);
831 			cfg_param_val.flag |= WILC_CFG_PARAM_FRAG_THRESHOLD;
832 			cfg_param_val.frag_threshold = wiphy->frag_threshold;
833 		} else {
834 			netdev_err(vif->ndev,
835 				   "Fragmentation threshold out of range\n");
836 			goto out;
837 		}
838 	}
839 
840 	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
841 		if (wiphy->rts_threshold > 255) {
842 			netdev_dbg(vif->ndev,
843 				   "Setting WIPHY_PARAM_RTS_THRESHOLD %d\n",
844 				   wiphy->rts_threshold);
845 			cfg_param_val.flag |= WILC_CFG_PARAM_RTS_THRESHOLD;
846 			cfg_param_val.rts_threshold = wiphy->rts_threshold;
847 		} else {
848 			netdev_err(vif->ndev, "RTS threshold out of range\n");
849 			goto out;
850 		}
851 	}
852 
853 	ret = wilc_hif_set_cfg(vif, &cfg_param_val);
854 	if (ret)
855 		netdev_err(priv->dev, "Error in setting WIPHY PARAMS\n");
856 
857 out:
858 	srcu_read_unlock(&wl->srcu, srcu_idx);
859 	return ret;
860 }
861 
set_pmksa(struct wiphy * wiphy,struct net_device * netdev,struct cfg80211_pmksa * pmksa)862 static int set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
863 		     struct cfg80211_pmksa *pmksa)
864 {
865 	struct wilc_vif *vif = netdev_priv(netdev);
866 	struct wilc_priv *priv = &vif->priv;
867 	u32 i;
868 	int ret = 0;
869 	u8 flag = 0;
870 
871 	for (i = 0; i < priv->pmkid_list.numpmkid; i++)	{
872 		if (!memcmp(pmksa->bssid, priv->pmkid_list.pmkidlist[i].bssid,
873 			    ETH_ALEN)) {
874 			flag = PMKID_FOUND;
875 			break;
876 		}
877 	}
878 	if (i < WILC_MAX_NUM_PMKIDS) {
879 		memcpy(priv->pmkid_list.pmkidlist[i].bssid, pmksa->bssid,
880 		       ETH_ALEN);
881 		memcpy(priv->pmkid_list.pmkidlist[i].pmkid, pmksa->pmkid,
882 		       WLAN_PMKID_LEN);
883 		if (!(flag == PMKID_FOUND))
884 			priv->pmkid_list.numpmkid++;
885 	} else {
886 		netdev_err(netdev, "Invalid PMKID index\n");
887 		ret = -EINVAL;
888 	}
889 
890 	if (!ret)
891 		ret = wilc_set_pmkid_info(vif, &priv->pmkid_list);
892 
893 	return ret;
894 }
895 
del_pmksa(struct wiphy * wiphy,struct net_device * netdev,struct cfg80211_pmksa * pmksa)896 static int del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
897 		     struct cfg80211_pmksa *pmksa)
898 {
899 	u32 i;
900 	struct wilc_vif *vif = netdev_priv(netdev);
901 	struct wilc_priv *priv = &vif->priv;
902 
903 	for (i = 0; i < priv->pmkid_list.numpmkid; i++)	{
904 		if (!memcmp(pmksa->bssid, priv->pmkid_list.pmkidlist[i].bssid,
905 			    ETH_ALEN)) {
906 			memset(&priv->pmkid_list.pmkidlist[i], 0,
907 			       sizeof(struct wilc_pmkid));
908 			break;
909 		}
910 	}
911 
912 	if (i == priv->pmkid_list.numpmkid)
913 		return -EINVAL;
914 
915 	for (; i < (priv->pmkid_list.numpmkid - 1); i++) {
916 		memcpy(priv->pmkid_list.pmkidlist[i].bssid,
917 		       priv->pmkid_list.pmkidlist[i + 1].bssid,
918 		       ETH_ALEN);
919 		memcpy(priv->pmkid_list.pmkidlist[i].pmkid,
920 		       priv->pmkid_list.pmkidlist[i + 1].pmkid,
921 		       WLAN_PMKID_LEN);
922 	}
923 	priv->pmkid_list.numpmkid--;
924 
925 	return 0;
926 }
927 
flush_pmksa(struct wiphy * wiphy,struct net_device * netdev)928 static int flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
929 {
930 	struct wilc_vif *vif = netdev_priv(netdev);
931 
932 	memset(&vif->priv.pmkid_list, 0, sizeof(struct wilc_pmkid_attr));
933 
934 	return 0;
935 }
936 
wilc_wfi_cfg_parse_ch_attr(u8 * buf,u32 len,u8 sta_ch)937 static inline void wilc_wfi_cfg_parse_ch_attr(u8 *buf, u32 len, u8 sta_ch)
938 {
939 	struct wilc_attr_entry *e;
940 	struct wilc_attr_ch_list *ch_list;
941 	struct wilc_attr_oper_ch *op_ch;
942 	u32 index = 0;
943 	u8 ch_list_idx = 0;
944 	u8 op_ch_idx = 0;
945 
946 	if (sta_ch == WILC_INVALID_CHANNEL)
947 		return;
948 
949 	while (index + sizeof(*e) <= len) {
950 		u16 attr_size;
951 
952 		e = (struct wilc_attr_entry *)&buf[index];
953 		attr_size = le16_to_cpu(e->attr_len);
954 
955 		if (index + sizeof(*e) + attr_size > len)
956 			return;
957 
958 		if (e->attr_type == IEEE80211_P2P_ATTR_CHANNEL_LIST &&
959 		    attr_size >= (sizeof(struct wilc_attr_ch_list) - sizeof(*e)))
960 			ch_list_idx = index;
961 		else if (e->attr_type == IEEE80211_P2P_ATTR_OPER_CHANNEL &&
962 			 attr_size == (sizeof(struct wilc_attr_oper_ch) - sizeof(*e)))
963 			op_ch_idx = index;
964 
965 		if (ch_list_idx && op_ch_idx)
966 			break;
967 
968 		index += sizeof(*e) + attr_size;
969 	}
970 
971 	if (ch_list_idx) {
972 		u16 elem_size;
973 
974 		ch_list = (struct wilc_attr_ch_list *)&buf[ch_list_idx];
975 		/* the number of bytes following the final 'elem' member */
976 		elem_size = le16_to_cpu(ch_list->attr_len) -
977 			(sizeof(*ch_list) - sizeof(struct wilc_attr_entry));
978 		for (unsigned int i = 0; i < elem_size;) {
979 			struct wilc_ch_list_elem *e;
980 
981 			e = (struct wilc_ch_list_elem *)(ch_list->elem + i);
982 
983 			i += sizeof(*e);
984 			if (i > elem_size)
985 				break;
986 
987 			i += e->no_of_channels;
988 			if (i > elem_size)
989 				break;
990 
991 			if (e->op_class == WILC_WLAN_OPERATING_CLASS_2_4GHZ) {
992 				memset(e->ch_list, sta_ch, e->no_of_channels);
993 				break;
994 			}
995 		}
996 	}
997 
998 	if (op_ch_idx) {
999 		op_ch = (struct wilc_attr_oper_ch *)&buf[op_ch_idx];
1000 		op_ch->op_class = WILC_WLAN_OPERATING_CLASS_2_4GHZ;
1001 		op_ch->op_channel = sta_ch;
1002 	}
1003 }
1004 
wilc_wfi_mgmt_frame_rx(struct wilc_vif * vif,u8 * buff,u32 size)1005 bool wilc_wfi_mgmt_frame_rx(struct wilc_vif *vif, u8 *buff, u32 size)
1006 {
1007 	struct wilc *wl = vif->wilc;
1008 	struct wilc_priv *priv = &vif->priv;
1009 	int freq;
1010 
1011 	freq = ieee80211_channel_to_frequency(wl->op_ch, NL80211_BAND_2GHZ);
1012 
1013 	return cfg80211_rx_mgmt(&priv->wdev, freq, 0, buff, size, 0);
1014 }
1015 
wilc_wfi_p2p_rx(struct wilc_vif * vif,u8 * buff,u32 size)1016 void wilc_wfi_p2p_rx(struct wilc_vif *vif, u8 *buff, u32 size)
1017 {
1018 	struct wilc *wl = vif->wilc;
1019 	struct wilc_priv *priv = &vif->priv;
1020 	struct host_if_drv *wfi_drv = priv->hif_drv;
1021 	struct ieee80211_mgmt *mgmt;
1022 	struct wilc_vendor_specific_ie *p;
1023 	struct wilc_p2p_pub_act_frame *d;
1024 	int ie_offset = offsetof(struct ieee80211_mgmt, u) + sizeof(*d);
1025 	const u8 *vendor_ie;
1026 	u32 header, pkt_offset;
1027 	s32 freq;
1028 
1029 	header = get_unaligned_le32(buff - HOST_HDR_OFFSET);
1030 	pkt_offset = FIELD_GET(WILC_PKT_HDR_OFFSET_FIELD, header);
1031 
1032 	if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
1033 		bool ack = false;
1034 		struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)buff;
1035 
1036 		if (ieee80211_is_probe_resp(hdr->frame_control) ||
1037 		    pkt_offset & IS_MGMT_STATUS_SUCCES)
1038 			ack = true;
1039 
1040 		cfg80211_mgmt_tx_status(&priv->wdev, priv->tx_cookie, buff,
1041 					size, ack, GFP_KERNEL);
1042 		return;
1043 	}
1044 
1045 	freq = ieee80211_channel_to_frequency(wl->op_ch, NL80211_BAND_2GHZ);
1046 
1047 	mgmt = (struct ieee80211_mgmt *)buff;
1048 	if (!ieee80211_is_action(mgmt->frame_control))
1049 		goto out_rx_mgmt;
1050 
1051 	if (priv->cfg_scanning &&
1052 	    time_after_eq(jiffies, (unsigned long)wfi_drv->p2p_timeout)) {
1053 		netdev_dbg(vif->ndev, "Receiving action wrong ch\n");
1054 		return;
1055 	}
1056 
1057 	if (!ieee80211_is_public_action((struct ieee80211_hdr *)buff, size))
1058 		goto out_rx_mgmt;
1059 
1060 	d = (struct wilc_p2p_pub_act_frame *)(&mgmt->u.action);
1061 	if (d->oui_subtype != GO_NEG_REQ && d->oui_subtype != GO_NEG_RSP &&
1062 	    d->oui_subtype != P2P_INV_REQ && d->oui_subtype != P2P_INV_RSP)
1063 		goto out_rx_mgmt;
1064 
1065 	vendor_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1066 					    buff + ie_offset, size - ie_offset);
1067 	if (!vendor_ie)
1068 		goto out_rx_mgmt;
1069 
1070 	p = (struct wilc_vendor_specific_ie *)vendor_ie;
1071 	wilc_wfi_cfg_parse_ch_attr(p->attr, p->tag_len - 4, vif->wilc->sta_ch);
1072 
1073 out_rx_mgmt:
1074 	cfg80211_rx_mgmt(&priv->wdev, freq, 0, buff, size, 0);
1075 }
1076 
wilc_wfi_mgmt_tx_complete(void * priv,int status)1077 static void wilc_wfi_mgmt_tx_complete(void *priv, int status)
1078 {
1079 	struct wilc_p2p_mgmt_data *pv_data = priv;
1080 
1081 	kfree(pv_data->buff);
1082 	kfree(pv_data);
1083 }
1084 
wilc_wfi_remain_on_channel_expired(struct wilc_vif * vif,u64 cookie)1085 static void wilc_wfi_remain_on_channel_expired(struct wilc_vif *vif, u64 cookie)
1086 {
1087 	struct wilc_priv *priv = &vif->priv;
1088 	struct wilc_wfi_p2p_listen_params *params = &priv->remain_on_ch_params;
1089 
1090 	if (cookie != params->listen_cookie)
1091 		return;
1092 
1093 	priv->p2p_listen_state = false;
1094 
1095 	cfg80211_remain_on_channel_expired(&priv->wdev, params->listen_cookie,
1096 					   params->listen_ch, GFP_KERNEL);
1097 }
1098 
remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,struct ieee80211_channel * chan,unsigned int duration,u64 * cookie)1099 static int remain_on_channel(struct wiphy *wiphy,
1100 			     struct wireless_dev *wdev,
1101 			     struct ieee80211_channel *chan,
1102 			     unsigned int duration, u64 *cookie)
1103 {
1104 	int ret = 0;
1105 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1106 	struct wilc_priv *priv = &vif->priv;
1107 	u64 id;
1108 
1109 	if (wdev->iftype == NL80211_IFTYPE_AP) {
1110 		netdev_dbg(vif->ndev, "Required while in AP mode\n");
1111 		return ret;
1112 	}
1113 
1114 	id = ++priv->inc_roc_cookie;
1115 	if (id == 0)
1116 		id = ++priv->inc_roc_cookie;
1117 
1118 	ret = wilc_remain_on_channel(vif, id, chan->hw_value,
1119 				     wilc_wfi_remain_on_channel_expired);
1120 	if (ret)
1121 		return ret;
1122 
1123 	vif->wilc->op_ch = chan->hw_value;
1124 
1125 	priv->remain_on_ch_params.listen_ch = chan;
1126 	priv->remain_on_ch_params.listen_cookie = id;
1127 	*cookie = id;
1128 	priv->p2p_listen_state = true;
1129 	priv->remain_on_ch_params.listen_duration = duration;
1130 
1131 	cfg80211_ready_on_channel(wdev, *cookie, chan, duration, GFP_KERNEL);
1132 	mod_timer(&vif->hif_drv->remain_on_ch_timer,
1133 		  jiffies + msecs_to_jiffies(duration + 1000));
1134 
1135 	return ret;
1136 }
1137 
cancel_remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,u64 cookie)1138 static int cancel_remain_on_channel(struct wiphy *wiphy,
1139 				    struct wireless_dev *wdev,
1140 				    u64 cookie)
1141 {
1142 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1143 	struct wilc_priv *priv = &vif->priv;
1144 
1145 	if (cookie != priv->remain_on_ch_params.listen_cookie)
1146 		return -ENOENT;
1147 
1148 	return wilc_listen_state_expired(vif, cookie);
1149 }
1150 
mgmt_tx(struct wiphy * wiphy,struct wireless_dev * wdev,struct cfg80211_mgmt_tx_params * params,u64 * cookie)1151 static int mgmt_tx(struct wiphy *wiphy,
1152 		   struct wireless_dev *wdev,
1153 		   struct cfg80211_mgmt_tx_params *params,
1154 		   u64 *cookie)
1155 {
1156 	struct ieee80211_channel *chan = params->chan;
1157 	unsigned int wait = params->wait;
1158 	const u8 *buf = params->buf;
1159 	size_t len = params->len;
1160 	const struct ieee80211_mgmt *mgmt;
1161 	struct wilc_p2p_mgmt_data *mgmt_tx;
1162 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1163 	struct wilc_priv *priv = &vif->priv;
1164 	struct host_if_drv *wfi_drv = priv->hif_drv;
1165 	struct wilc_vendor_specific_ie *p;
1166 	struct wilc_p2p_pub_act_frame *d;
1167 	int ie_offset = offsetof(struct ieee80211_mgmt, u) + sizeof(*d);
1168 	const u8 *vendor_ie;
1169 	int ret = 0;
1170 
1171 	*cookie = get_random_u32();
1172 	priv->tx_cookie = *cookie;
1173 	mgmt = (const struct ieee80211_mgmt *)buf;
1174 
1175 	if (!ieee80211_is_mgmt(mgmt->frame_control))
1176 		goto out;
1177 
1178 	mgmt_tx = kmalloc_obj(*mgmt_tx);
1179 	if (!mgmt_tx) {
1180 		ret = -ENOMEM;
1181 		goto out;
1182 	}
1183 
1184 	mgmt_tx->buff = kmemdup(buf, len, GFP_KERNEL);
1185 	if (!mgmt_tx->buff) {
1186 		ret = -ENOMEM;
1187 		kfree(mgmt_tx);
1188 		goto out;
1189 	}
1190 
1191 	mgmt_tx->size = len;
1192 
1193 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {
1194 		wilc_set_mac_chnl_num(vif, chan->hw_value);
1195 		vif->wilc->op_ch = chan->hw_value;
1196 		goto out_txq_add_pkt;
1197 	}
1198 
1199 	if (!ieee80211_is_public_action((struct ieee80211_hdr *)buf, len)) {
1200 		if (chan)
1201 			wilc_set_mac_chnl_num(vif, chan->hw_value);
1202 		else
1203 			wilc_set_mac_chnl_num(vif, vif->wilc->op_ch);
1204 
1205 		goto out_set_timeout;
1206 	}
1207 
1208 	d = (struct wilc_p2p_pub_act_frame *)(&mgmt->u.action);
1209 	if (d->oui_type != WLAN_OUI_TYPE_WFA_P2P ||
1210 	    d->oui_subtype != GO_NEG_CONF) {
1211 		wilc_set_mac_chnl_num(vif, chan->hw_value);
1212 		vif->wilc->op_ch = chan->hw_value;
1213 	}
1214 
1215 	if (d->oui_subtype != P2P_INV_REQ && d->oui_subtype != P2P_INV_RSP)
1216 		goto out_set_timeout;
1217 
1218 	vendor_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1219 					    mgmt_tx->buff + ie_offset,
1220 					    len - ie_offset);
1221 	if (!vendor_ie)
1222 		goto out_set_timeout;
1223 
1224 	p = (struct wilc_vendor_specific_ie *)vendor_ie;
1225 	wilc_wfi_cfg_parse_ch_attr(p->attr, p->tag_len - 4, vif->wilc->sta_ch);
1226 
1227 out_set_timeout:
1228 	wfi_drv->p2p_timeout = (jiffies + msecs_to_jiffies(wait));
1229 
1230 out_txq_add_pkt:
1231 
1232 	wilc_wlan_txq_add_mgmt_pkt(wdev->netdev, mgmt_tx,
1233 				   mgmt_tx->buff, mgmt_tx->size,
1234 				   wilc_wfi_mgmt_tx_complete);
1235 
1236 out:
1237 
1238 	return ret;
1239 }
1240 
mgmt_tx_cancel_wait(struct wiphy * wiphy,struct wireless_dev * wdev,u64 cookie)1241 static int mgmt_tx_cancel_wait(struct wiphy *wiphy,
1242 			       struct wireless_dev *wdev,
1243 			       u64 cookie)
1244 {
1245 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1246 	struct wilc_priv *priv = &vif->priv;
1247 	struct host_if_drv *wfi_drv = priv->hif_drv;
1248 
1249 	wfi_drv->p2p_timeout = jiffies;
1250 
1251 	if (!priv->p2p_listen_state) {
1252 		struct wilc_wfi_p2p_listen_params *params;
1253 
1254 		params = &priv->remain_on_ch_params;
1255 
1256 		cfg80211_remain_on_channel_expired(wdev,
1257 						   params->listen_cookie,
1258 						   params->listen_ch,
1259 						   GFP_KERNEL);
1260 	}
1261 
1262 	return 0;
1263 }
1264 
wilc_update_mgmt_frame_registrations(struct wiphy * wiphy,struct wireless_dev * wdev,struct mgmt_frame_regs * upd)1265 void wilc_update_mgmt_frame_registrations(struct wiphy *wiphy,
1266 					  struct wireless_dev *wdev,
1267 					  struct mgmt_frame_regs *upd)
1268 {
1269 	struct wilc *wl = wiphy_priv(wiphy);
1270 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1271 	u32 presp_bit = BIT(IEEE80211_STYPE_PROBE_REQ >> 4);
1272 	u32 action_bit = BIT(IEEE80211_STYPE_ACTION >> 4);
1273 	u32 pauth_bit = BIT(IEEE80211_STYPE_AUTH >> 4);
1274 
1275 	if (wl->initialized) {
1276 		bool prev = vif->mgmt_reg_stypes & presp_bit;
1277 		bool now = upd->interface_stypes & presp_bit;
1278 
1279 		if (now != prev)
1280 			wilc_frame_register(vif, IEEE80211_STYPE_PROBE_REQ, now);
1281 
1282 		prev = vif->mgmt_reg_stypes & action_bit;
1283 		now = upd->interface_stypes & action_bit;
1284 
1285 		if (now != prev)
1286 			wilc_frame_register(vif, IEEE80211_STYPE_ACTION, now);
1287 
1288 		prev = vif->mgmt_reg_stypes & pauth_bit;
1289 		now = upd->interface_stypes & pauth_bit;
1290 		if (now != prev)
1291 			wilc_frame_register(vif, IEEE80211_STYPE_AUTH, now);
1292 	}
1293 
1294 	vif->mgmt_reg_stypes =
1295 		upd->interface_stypes & (presp_bit | action_bit | pauth_bit);
1296 }
1297 
external_auth(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_external_auth_params * auth)1298 static int external_auth(struct wiphy *wiphy, struct net_device *dev,
1299 			 struct cfg80211_external_auth_params *auth)
1300 {
1301 	struct wilc_vif *vif = netdev_priv(dev);
1302 
1303 	if (auth->status == WLAN_STATUS_SUCCESS)
1304 		wilc_set_external_auth_param(vif, auth);
1305 
1306 	return 0;
1307 }
1308 
set_cqm_rssi_config(struct wiphy * wiphy,struct net_device * dev,s32 rssi_thold,u32 rssi_hyst)1309 static int set_cqm_rssi_config(struct wiphy *wiphy, struct net_device *dev,
1310 			       s32 rssi_thold, u32 rssi_hyst)
1311 {
1312 	return 0;
1313 }
1314 
dump_station(struct wiphy * wiphy,struct net_device * dev,int idx,u8 * mac,struct station_info * sinfo)1315 static int dump_station(struct wiphy *wiphy, struct net_device *dev,
1316 			int idx, u8 *mac, struct station_info *sinfo)
1317 {
1318 	struct wilc_vif *vif = netdev_priv(dev);
1319 	int ret;
1320 
1321 	if (idx != 0)
1322 		return -ENOENT;
1323 
1324 	ret = wilc_get_rssi(vif, &sinfo->signal);
1325 	if (ret)
1326 		return ret;
1327 
1328 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
1329 	memcpy(mac, vif->priv.associated_bss, ETH_ALEN);
1330 	return 0;
1331 }
1332 
set_power_mgmt(struct wiphy * wiphy,struct net_device * dev,bool enabled,int timeout)1333 static int set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
1334 			  bool enabled, int timeout)
1335 {
1336 	struct wilc_vif *vif = netdev_priv(dev);
1337 	struct wilc_priv *priv = &vif->priv;
1338 
1339 	if (!priv->hif_drv)
1340 		return -EIO;
1341 
1342 	wilc_set_power_mgmt(vif, enabled, timeout);
1343 
1344 	return 0;
1345 }
1346 
change_virtual_intf(struct wiphy * wiphy,struct net_device * dev,enum nl80211_iftype type,struct vif_params * params)1347 static int change_virtual_intf(struct wiphy *wiphy, struct net_device *dev,
1348 			       enum nl80211_iftype type,
1349 			       struct vif_params *params)
1350 {
1351 	struct wilc *wl = wiphy_priv(wiphy);
1352 	struct wilc_vif *vif = netdev_priv(dev);
1353 	struct wilc_priv *priv = &vif->priv;
1354 
1355 	switch (type) {
1356 	case NL80211_IFTYPE_STATION:
1357 		vif->connecting = false;
1358 		dev->ieee80211_ptr->iftype = type;
1359 		priv->wdev.iftype = type;
1360 		vif->monitor_flag = 0;
1361 		if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE)
1362 			wilc_wfi_deinit_mon_interface(wl, true);
1363 		vif->iftype = WILC_STATION_MODE;
1364 
1365 		if (wl->initialized)
1366 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1367 						WILC_STATION_MODE, vif->idx);
1368 
1369 		memset(priv->assoc_stainfo.sta_associated_bss, 0,
1370 		       WILC_MAX_NUM_STA * ETH_ALEN);
1371 		break;
1372 
1373 	case NL80211_IFTYPE_P2P_CLIENT:
1374 		vif->connecting = false;
1375 		dev->ieee80211_ptr->iftype = type;
1376 		priv->wdev.iftype = type;
1377 		vif->monitor_flag = 0;
1378 		vif->iftype = WILC_CLIENT_MODE;
1379 
1380 		if (wl->initialized)
1381 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1382 						WILC_STATION_MODE, vif->idx);
1383 		break;
1384 
1385 	case NL80211_IFTYPE_AP:
1386 		dev->ieee80211_ptr->iftype = type;
1387 		priv->wdev.iftype = type;
1388 		vif->iftype = WILC_AP_MODE;
1389 
1390 		if (wl->initialized)
1391 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1392 						WILC_AP_MODE, vif->idx);
1393 		break;
1394 
1395 	case NL80211_IFTYPE_P2P_GO:
1396 		dev->ieee80211_ptr->iftype = type;
1397 		priv->wdev.iftype = type;
1398 		vif->iftype = WILC_GO_MODE;
1399 
1400 		if (wl->initialized)
1401 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1402 						WILC_AP_MODE, vif->idx);
1403 		break;
1404 
1405 	default:
1406 		netdev_err(dev, "Unknown interface type= %d\n", type);
1407 		return -EINVAL;
1408 	}
1409 
1410 	return 0;
1411 }
1412 
start_ap(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ap_settings * settings)1413 static int start_ap(struct wiphy *wiphy, struct net_device *dev,
1414 		    struct cfg80211_ap_settings *settings)
1415 {
1416 	struct wilc_vif *vif = netdev_priv(dev);
1417 	int ret;
1418 
1419 	ret = set_channel(wiphy, dev, &settings->chandef);
1420 	if (ret != 0)
1421 		netdev_err(dev, "Error in setting channel\n");
1422 
1423 	wilc_wlan_set_bssid(dev, dev->dev_addr, WILC_AP_MODE);
1424 
1425 	return wilc_add_beacon(vif, settings->beacon_interval,
1426 				   settings->dtim_period, &settings->beacon);
1427 }
1428 
change_beacon(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ap_update * params)1429 static int change_beacon(struct wiphy *wiphy, struct net_device *dev,
1430 			 struct cfg80211_ap_update *params)
1431 {
1432 	struct wilc_vif *vif = netdev_priv(dev);
1433 
1434 	return wilc_add_beacon(vif, 0, 0, &params->beacon);
1435 }
1436 
stop_ap(struct wiphy * wiphy,struct net_device * dev,unsigned int link_id)1437 static int stop_ap(struct wiphy *wiphy, struct net_device *dev,
1438 		   unsigned int link_id)
1439 {
1440 	int ret;
1441 	struct wilc_vif *vif = netdev_priv(dev);
1442 
1443 	wilc_wlan_set_bssid(dev, NULL, WILC_AP_MODE);
1444 
1445 	ret = wilc_del_beacon(vif);
1446 
1447 	if (ret)
1448 		netdev_err(dev, "Host delete beacon fail\n");
1449 
1450 	return ret;
1451 }
1452 
add_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_parameters * params)1453 static int add_station(struct wiphy *wiphy, struct net_device *dev,
1454 		       const u8 *mac, struct station_parameters *params)
1455 {
1456 	int ret = 0;
1457 	struct wilc_vif *vif = netdev_priv(dev);
1458 	struct wilc_priv *priv = &vif->priv;
1459 
1460 	if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE) {
1461 		memcpy(priv->assoc_stainfo.sta_associated_bss[params->aid], mac,
1462 		       ETH_ALEN);
1463 
1464 		ret = wilc_add_station(vif, mac, params);
1465 		if (ret)
1466 			netdev_err(dev, "Host add station fail\n");
1467 	}
1468 
1469 	return ret;
1470 }
1471 
del_station(struct wiphy * wiphy,struct net_device * dev,struct station_del_parameters * params)1472 static int del_station(struct wiphy *wiphy, struct net_device *dev,
1473 		       struct station_del_parameters *params)
1474 {
1475 	const u8 *mac = params->mac;
1476 	int ret = 0;
1477 	struct wilc_vif *vif = netdev_priv(dev);
1478 	struct wilc_priv *priv = &vif->priv;
1479 	struct sta_info *info;
1480 
1481 	if (!(vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE))
1482 		return ret;
1483 
1484 	info = &priv->assoc_stainfo;
1485 
1486 	if (!mac)
1487 		ret = wilc_del_allstation(vif, info->sta_associated_bss);
1488 
1489 	ret = wilc_del_station(vif, mac);
1490 	if (ret)
1491 		netdev_err(dev, "Host delete station fail\n");
1492 	return ret;
1493 }
1494 
change_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_parameters * params)1495 static int change_station(struct wiphy *wiphy, struct net_device *dev,
1496 			  const u8 *mac, struct station_parameters *params)
1497 {
1498 	int ret = 0;
1499 	struct wilc_vif *vif = netdev_priv(dev);
1500 
1501 	if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE) {
1502 		ret = wilc_edit_station(vif, mac, params);
1503 		if (ret)
1504 			netdev_err(dev, "Host edit station fail\n");
1505 	}
1506 	return ret;
1507 }
1508 
wilc_get_vif_from_type(struct wilc * wl,int type)1509 static struct wilc_vif *wilc_get_vif_from_type(struct wilc *wl, int type)
1510 {
1511 	struct wilc_vif *vif;
1512 
1513 	wilc_for_each_vif(wl, vif) {
1514 		if (vif->iftype == type)
1515 			return vif;
1516 	}
1517 
1518 	return NULL;
1519 }
1520 
add_virtual_intf(struct wiphy * wiphy,const char * name,unsigned char name_assign_type,enum nl80211_iftype type,struct vif_params * params)1521 static struct wireless_dev *add_virtual_intf(struct wiphy *wiphy,
1522 					     const char *name,
1523 					     unsigned char name_assign_type,
1524 					     enum nl80211_iftype type,
1525 					     struct vif_params *params)
1526 {
1527 	struct wilc *wl = wiphy_priv(wiphy);
1528 	struct wilc_vif *vif;
1529 	struct wireless_dev *wdev;
1530 	int iftype;
1531 
1532 	if (type == NL80211_IFTYPE_MONITOR) {
1533 		struct net_device *ndev;
1534 		int srcu_idx;
1535 
1536 		srcu_idx = srcu_read_lock(&wl->srcu);
1537 		vif = wilc_get_vif_from_type(wl, WILC_AP_MODE);
1538 		if (!vif) {
1539 			vif = wilc_get_vif_from_type(wl, WILC_GO_MODE);
1540 			if (!vif) {
1541 				srcu_read_unlock(&wl->srcu, srcu_idx);
1542 				goto validate_interface;
1543 			}
1544 		}
1545 
1546 		if (vif->monitor_flag) {
1547 			srcu_read_unlock(&wl->srcu, srcu_idx);
1548 			goto validate_interface;
1549 		}
1550 
1551 		ndev = wilc_wfi_init_mon_interface(wl, name, vif->ndev);
1552 		if (ndev) {
1553 			vif->monitor_flag = 1;
1554 		} else {
1555 			srcu_read_unlock(&wl->srcu, srcu_idx);
1556 			return ERR_PTR(-EINVAL);
1557 		}
1558 
1559 		wdev = &vif->priv.wdev;
1560 		srcu_read_unlock(&wl->srcu, srcu_idx);
1561 		return wdev;
1562 	}
1563 
1564 validate_interface:
1565 	mutex_lock(&wl->vif_mutex);
1566 	if (wl->vif_num == WILC_NUM_CONCURRENT_IFC) {
1567 		pr_err("Reached maximum number of interface\n");
1568 		mutex_unlock(&wl->vif_mutex);
1569 		return ERR_PTR(-EINVAL);
1570 	}
1571 	mutex_unlock(&wl->vif_mutex);
1572 
1573 	switch (type) {
1574 	case NL80211_IFTYPE_STATION:
1575 		iftype = WILC_STATION_MODE;
1576 		break;
1577 	case NL80211_IFTYPE_AP:
1578 		iftype = WILC_AP_MODE;
1579 		break;
1580 	default:
1581 		return ERR_PTR(-EOPNOTSUPP);
1582 	}
1583 
1584 	vif = wilc_netdev_ifc_init(wl, name, iftype, type, true);
1585 	if (IS_ERR(vif))
1586 		return ERR_CAST(vif);
1587 
1588 	return &vif->priv.wdev;
1589 }
1590 
del_virtual_intf(struct wiphy * wiphy,struct wireless_dev * wdev)1591 static int del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
1592 {
1593 	struct wilc *wl = wiphy_priv(wiphy);
1594 	struct wilc_vif *vif;
1595 
1596 	if (wdev->iftype == NL80211_IFTYPE_AP ||
1597 	    wdev->iftype == NL80211_IFTYPE_P2P_GO)
1598 		wilc_wfi_deinit_mon_interface(wl, true);
1599 	vif = netdev_priv(wdev->netdev);
1600 	cfg80211_stop_iface(wiphy, wdev, GFP_KERNEL);
1601 	cfg80211_unregister_netdevice(vif->ndev);
1602 	vif->monitor_flag = 0;
1603 
1604 	mutex_lock(&wl->vif_mutex);
1605 	list_del_rcu(&vif->list);
1606 	wl->vif_num--;
1607 	mutex_unlock(&wl->vif_mutex);
1608 	synchronize_srcu(&wl->srcu);
1609 	return 0;
1610 }
1611 
wilc_set_wakeup(struct wiphy * wiphy,bool enabled)1612 static void wilc_set_wakeup(struct wiphy *wiphy, bool enabled)
1613 {
1614 	struct wilc *wl = wiphy_priv(wiphy);
1615 	struct wilc_vif *vif;
1616 	int srcu_idx;
1617 
1618 	srcu_idx = srcu_read_lock(&wl->srcu);
1619 	vif = wilc_get_wl_to_vif(wl);
1620 	if (IS_ERR(vif)) {
1621 		srcu_read_unlock(&wl->srcu, srcu_idx);
1622 		return;
1623 	}
1624 
1625 	netdev_info(vif->ndev, "cfg set wake up = %d\n", enabled);
1626 	wilc_set_wowlan_trigger(vif, enabled);
1627 	srcu_read_unlock(&wl->srcu, srcu_idx);
1628 }
1629 
set_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,int radio_idx,enum nl80211_tx_power_setting type,int mbm)1630 static int set_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
1631 			int radio_idx, enum nl80211_tx_power_setting type,
1632 			int mbm)
1633 {
1634 	int ret;
1635 	int srcu_idx;
1636 	s32 tx_power = MBM_TO_DBM(mbm);
1637 	struct wilc *wl = wiphy_priv(wiphy);
1638 	struct wilc_vif *vif;
1639 
1640 	if (!wl->initialized)
1641 		return -EIO;
1642 
1643 	srcu_idx = srcu_read_lock(&wl->srcu);
1644 	vif = wilc_get_wl_to_vif(wl);
1645 	if (IS_ERR(vif)) {
1646 		srcu_read_unlock(&wl->srcu, srcu_idx);
1647 		return -EINVAL;
1648 	}
1649 
1650 	netdev_info(vif->ndev, "Setting tx power %d\n", tx_power);
1651 	if (tx_power < 0)
1652 		tx_power = 0;
1653 	else if (tx_power > 18)
1654 		tx_power = 18;
1655 	ret = wilc_set_tx_power(vif, tx_power);
1656 	if (ret)
1657 		netdev_err(vif->ndev, "Failed to set tx power\n");
1658 	srcu_read_unlock(&wl->srcu, srcu_idx);
1659 
1660 	return ret;
1661 }
1662 
get_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,int radio_idx,unsigned int link_id,int * dbm)1663 static int get_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
1664 			int radio_idx, unsigned int link_id, int *dbm)
1665 {
1666 	int ret;
1667 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1668 	struct wilc *wl = vif->wilc;
1669 
1670 	/* If firmware is not started, return. */
1671 	if (!wl->initialized)
1672 		return -EIO;
1673 
1674 	ret = wilc_get_tx_power(vif, (u8 *)dbm);
1675 	if (ret)
1676 		netdev_err(vif->ndev, "Failed to get tx power\n");
1677 
1678 	return ret;
1679 }
1680 
1681 static const struct cfg80211_ops wilc_cfg80211_ops = {
1682 	.set_monitor_channel = set_channel,
1683 	.scan = scan,
1684 	.connect = connect,
1685 	.disconnect = disconnect,
1686 	.add_key = add_key,
1687 	.del_key = del_key,
1688 	.get_key = get_key,
1689 	.set_default_key = set_default_key,
1690 	.set_default_mgmt_key = set_default_mgmt_key,
1691 	.add_virtual_intf = add_virtual_intf,
1692 	.del_virtual_intf = del_virtual_intf,
1693 	.change_virtual_intf = change_virtual_intf,
1694 
1695 	.start_ap = start_ap,
1696 	.change_beacon = change_beacon,
1697 	.stop_ap = stop_ap,
1698 	.add_station = add_station,
1699 	.del_station = del_station,
1700 	.change_station = change_station,
1701 	.get_station = get_station,
1702 	.dump_station = dump_station,
1703 	.set_wiphy_params = set_wiphy_params,
1704 
1705 	.external_auth = external_auth,
1706 	.set_pmksa = set_pmksa,
1707 	.del_pmksa = del_pmksa,
1708 	.flush_pmksa = flush_pmksa,
1709 	.remain_on_channel = remain_on_channel,
1710 	.cancel_remain_on_channel = cancel_remain_on_channel,
1711 	.mgmt_tx_cancel_wait = mgmt_tx_cancel_wait,
1712 	.mgmt_tx = mgmt_tx,
1713 	.update_mgmt_frame_registrations = wilc_update_mgmt_frame_registrations,
1714 	.set_power_mgmt = set_power_mgmt,
1715 	.set_cqm_rssi_config = set_cqm_rssi_config,
1716 
1717 	.set_wakeup = wilc_set_wakeup,
1718 	.set_tx_power = set_tx_power,
1719 	.get_tx_power = get_tx_power,
1720 
1721 };
1722 
wlan_init_locks(struct wilc * wl)1723 static void wlan_init_locks(struct wilc *wl)
1724 {
1725 	mutex_init(&wl->hif_cs);
1726 	mutex_init(&wl->rxq_cs);
1727 	mutex_init(&wl->cfg_cmd_lock);
1728 	mutex_init(&wl->vif_mutex);
1729 	mutex_init(&wl->deinit_lock);
1730 
1731 	spin_lock_init(&wl->txq_spinlock);
1732 	mutex_init(&wl->txq_add_to_head_cs);
1733 
1734 	init_completion(&wl->txq_event);
1735 	init_completion(&wl->cfg_event);
1736 	init_completion(&wl->sync_event);
1737 	init_completion(&wl->txq_thread_started);
1738 	init_srcu_struct(&wl->srcu);
1739 }
1740 
wlan_deinit_locks(struct wilc * wilc)1741 void wlan_deinit_locks(struct wilc *wilc)
1742 {
1743 	mutex_destroy(&wilc->hif_cs);
1744 	mutex_destroy(&wilc->rxq_cs);
1745 	mutex_destroy(&wilc->cfg_cmd_lock);
1746 	mutex_destroy(&wilc->txq_add_to_head_cs);
1747 	mutex_destroy(&wilc->vif_mutex);
1748 	mutex_destroy(&wilc->deinit_lock);
1749 	cleanup_srcu_struct(&wilc->srcu);
1750 }
1751 
wilc_create_wiphy(struct device * dev)1752 static struct wilc *wilc_create_wiphy(struct device *dev)
1753 {
1754 	struct wiphy *wiphy;
1755 	struct wilc *wl;
1756 
1757 	wiphy = wiphy_new(&wilc_cfg80211_ops, sizeof(*wl));
1758 	if (!wiphy)
1759 		return NULL;
1760 
1761 	wl = wiphy_priv(wiphy);
1762 
1763 	memcpy(wl->bitrates, wilc_bitrates, sizeof(wilc_bitrates));
1764 	memcpy(wl->channels, wilc_2ghz_channels, sizeof(wilc_2ghz_channels));
1765 	wl->band.bitrates = wl->bitrates;
1766 	wl->band.n_bitrates = ARRAY_SIZE(wl->bitrates);
1767 	wl->band.channels = wl->channels;
1768 	wl->band.n_channels = ARRAY_SIZE(wilc_2ghz_channels);
1769 
1770 	wl->band.ht_cap.ht_supported = 1;
1771 	wl->band.ht_cap.cap |= (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
1772 	wl->band.ht_cap.mcs.rx_mask[0] = 0xff;
1773 	wl->band.ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_8K;
1774 	wl->band.ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
1775 
1776 	wiphy->bands[NL80211_BAND_2GHZ] = &wl->band;
1777 
1778 	wiphy->max_scan_ssids = WILC_MAX_NUM_PROBED_SSID;
1779 #ifdef CONFIG_PM
1780 	wiphy->wowlan = &wowlan_support;
1781 #endif
1782 	wiphy->max_num_pmkids = WILC_MAX_NUM_PMKIDS;
1783 	wiphy->max_scan_ie_len = 1000;
1784 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1785 	memcpy(wl->cipher_suites, wilc_cipher_suites,
1786 	       sizeof(wilc_cipher_suites));
1787 	wiphy->cipher_suites = wl->cipher_suites;
1788 	wiphy->n_cipher_suites = ARRAY_SIZE(wilc_cipher_suites);
1789 	wiphy->mgmt_stypes = wilc_wfi_cfg80211_mgmt_types;
1790 
1791 	wiphy->max_remain_on_channel_duration = 500;
1792 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1793 				BIT(NL80211_IFTYPE_AP) |
1794 				BIT(NL80211_IFTYPE_MONITOR) |
1795 				BIT(NL80211_IFTYPE_P2P_GO) |
1796 				BIT(NL80211_IFTYPE_P2P_CLIENT);
1797 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
1798 	set_wiphy_dev(wiphy, dev);
1799 	wl->wiphy = wiphy;
1800 	return wl;
1801 }
1802 
wilc_cfg80211_init(struct wilc ** wilc,struct device * dev,int io_type,const struct wilc_hif_func * ops)1803 int wilc_cfg80211_init(struct wilc **wilc, struct device *dev, int io_type,
1804 		       const struct wilc_hif_func *ops)
1805 {
1806 	struct wilc *wl;
1807 	int ret, i;
1808 
1809 	wl = wilc_create_wiphy(dev);
1810 	if (!wl)
1811 		return -EINVAL;
1812 
1813 	wlan_init_locks(wl);
1814 
1815 	ret = wilc_wlan_cfg_init(wl);
1816 	if (ret)
1817 		goto free_wl;
1818 
1819 	*wilc = wl;
1820 	wl->io_type = io_type;
1821 	wl->hif_func = ops;
1822 
1823 	for (i = 0; i < NQUEUES; i++)
1824 		INIT_LIST_HEAD(&wl->txq[i].txq_head.list);
1825 
1826 	INIT_LIST_HEAD(&wl->rxq_head.list);
1827 	INIT_LIST_HEAD(&wl->vif_list);
1828 
1829 	wl->hif_workqueue = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
1830 						    wiphy_name(wl->wiphy));
1831 	if (!wl->hif_workqueue) {
1832 		ret = -ENOMEM;
1833 		goto free_cfg;
1834 	}
1835 
1836 	return 0;
1837 
1838 free_cfg:
1839 	wilc_wlan_cfg_deinit(wl);
1840 
1841 free_wl:
1842 	wlan_deinit_locks(wl);
1843 	wiphy_free(wl->wiphy);
1844 	return ret;
1845 }
1846 EXPORT_SYMBOL_GPL(wilc_cfg80211_init);
1847 
wilc_cfg80211_register(struct wilc * wilc)1848 int wilc_cfg80211_register(struct wilc *wilc)
1849 {
1850 	/* WPA3/SAE supported only on WILC1000 */
1851 	if (is_wilc1000(wilc->chipid))
1852 		wilc->wiphy->features |= NL80211_FEATURE_SAE;
1853 
1854 	return wiphy_register(wilc->wiphy);
1855 }
1856 EXPORT_SYMBOL_GPL(wilc_cfg80211_register);
1857 
wilc_init_host_int(struct net_device * net)1858 int wilc_init_host_int(struct net_device *net)
1859 {
1860 	int ret;
1861 	struct wilc_vif *vif = netdev_priv(net);
1862 	struct wilc_priv *priv = &vif->priv;
1863 
1864 	priv->p2p_listen_state = false;
1865 
1866 	mutex_init(&priv->scan_req_lock);
1867 	ret = wilc_init(net, &priv->hif_drv);
1868 	if (ret)
1869 		netdev_err(net, "Error while initializing hostinterface\n");
1870 
1871 	return ret;
1872 }
1873 
wilc_deinit_host_int(struct net_device * net)1874 void wilc_deinit_host_int(struct net_device *net)
1875 {
1876 	int ret;
1877 	struct wilc_vif *vif = netdev_priv(net);
1878 	struct wilc_priv *priv = &vif->priv;
1879 
1880 	priv->p2p_listen_state = false;
1881 
1882 	flush_workqueue(vif->wilc->hif_workqueue);
1883 	mutex_destroy(&priv->scan_req_lock);
1884 	ret = wilc_deinit(vif);
1885 
1886 	if (ret)
1887 		netdev_err(net, "Error while deinitializing host interface\n");
1888 }
1889 
1890