xref: /linux/drivers/net/wireless/microchip/wilc1000/cfg80211.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
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 wireless_dev * wdev,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 wireless_dev *wdev, 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(wdev->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(wdev->netdev, "%s: Unsupported cipher\n", __func__);
636 		ret = -ENOTSUPP;
637 	}
638 
639 	return ret;
640 }
641 
del_key(struct wiphy * wiphy,struct wireless_dev * wdev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr)642 static int del_key(struct wiphy *wiphy, struct wireless_dev *wdev, int link_id,
643 		   u8 key_index,
644 		   bool pairwise,
645 		   const u8 *mac_addr)
646 {
647 	struct wilc_vif *vif = netdev_priv(wdev->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 wireless_dev * wdev,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 wireless_dev *wdev, 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(wdev->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 wireless_dev * wdev,int link_id,u8 key_index)728 static int set_default_mgmt_key(struct wiphy *wiphy, struct wireless_dev *wdev,
729 				int link_id, u8 key_index)
730 {
731 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
732 
733 	return wilc_set_default_mgmt_key_index(vif, key_index);
734 }
735 
get_station(struct wiphy * wiphy,struct wireless_dev * wdev,const u8 * mac,struct station_info * sinfo)736 static int get_station(struct wiphy *wiphy, struct wireless_dev *wdev,
737 		       const u8 *mac, struct station_info *sinfo)
738 {
739 	struct net_device *dev = wdev->netdev;
740 	struct wilc_vif *vif = netdev_priv(dev);
741 	struct wilc_priv *priv = &vif->priv;
742 	struct wilc *wilc = vif->wilc;
743 	u32 i = 0;
744 	u32 associatedsta = ~0;
745 	u32 inactive_time = 0;
746 
747 	if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE) {
748 		for (i = 0; i < NUM_STA_ASSOCIATED; i++) {
749 			if (!(memcmp(mac,
750 				     priv->assoc_stainfo.sta_associated_bss[i],
751 				     ETH_ALEN))) {
752 				associatedsta = i;
753 				break;
754 			}
755 		}
756 
757 		if (associatedsta == ~0) {
758 			netdev_err(dev, "sta required is not associated\n");
759 			return -ENOENT;
760 		}
761 
762 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME);
763 
764 		wilc_get_inactive_time(vif, mac, &inactive_time);
765 		sinfo->inactive_time = 1000 * inactive_time;
766 	} else if (vif->iftype == WILC_STATION_MODE) {
767 		struct rf_info stats;
768 
769 		if (!wilc->initialized)
770 			return -EBUSY;
771 
772 		wilc_get_statistics(vif, &stats);
773 
774 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL) |
775 				 BIT_ULL(NL80211_STA_INFO_RX_PACKETS) |
776 				 BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
777 				 BIT_ULL(NL80211_STA_INFO_TX_FAILED) |
778 				 BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
779 
780 		sinfo->signal = stats.rssi;
781 		sinfo->rx_packets = stats.rx_cnt;
782 		sinfo->tx_packets = stats.tx_cnt + stats.tx_fail_cnt;
783 		sinfo->tx_failed = stats.tx_fail_cnt;
784 		sinfo->txrate.legacy = stats.link_speed * 10;
785 
786 		if (stats.link_speed > TCP_ACK_FILTER_LINK_SPEED_THRESH &&
787 		    stats.link_speed != DEFAULT_LINK_SPEED)
788 			wilc_enable_tcp_ack_filter(vif, true);
789 		else if (stats.link_speed != DEFAULT_LINK_SPEED)
790 			wilc_enable_tcp_ack_filter(vif, false);
791 	}
792 	return 0;
793 }
794 
set_wiphy_params(struct wiphy * wiphy,int radio_idx,u32 changed)795 static int set_wiphy_params(struct wiphy *wiphy, int radio_idx, u32 changed)
796 {
797 	int ret = -EINVAL;
798 	struct cfg_param_attr cfg_param_val;
799 	struct wilc *wl = wiphy_priv(wiphy);
800 	struct wilc_vif *vif;
801 	struct wilc_priv *priv;
802 	int srcu_idx;
803 
804 	srcu_idx = srcu_read_lock(&wl->srcu);
805 	vif = wilc_get_wl_to_vif(wl);
806 	if (IS_ERR(vif))
807 		goto out;
808 
809 	priv = &vif->priv;
810 	cfg_param_val.flag = 0;
811 
812 	if (changed & WIPHY_PARAM_RETRY_SHORT) {
813 		netdev_dbg(vif->ndev,
814 			   "Setting WIPHY_PARAM_RETRY_SHORT %d\n",
815 			   wiphy->retry_short);
816 		cfg_param_val.flag  |= WILC_CFG_PARAM_RETRY_SHORT;
817 		cfg_param_val.short_retry_limit = wiphy->retry_short;
818 	}
819 	if (changed & WIPHY_PARAM_RETRY_LONG) {
820 		netdev_dbg(vif->ndev,
821 			   "Setting WIPHY_PARAM_RETRY_LONG %d\n",
822 			   wiphy->retry_long);
823 		cfg_param_val.flag |= WILC_CFG_PARAM_RETRY_LONG;
824 		cfg_param_val.long_retry_limit = wiphy->retry_long;
825 	}
826 	if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
827 		if (wiphy->frag_threshold > 255 &&
828 		    wiphy->frag_threshold < 7937) {
829 			netdev_dbg(vif->ndev,
830 				   "Setting WIPHY_PARAM_FRAG_THRESHOLD %d\n",
831 				   wiphy->frag_threshold);
832 			cfg_param_val.flag |= WILC_CFG_PARAM_FRAG_THRESHOLD;
833 			cfg_param_val.frag_threshold = wiphy->frag_threshold;
834 		} else {
835 			netdev_err(vif->ndev,
836 				   "Fragmentation threshold out of range\n");
837 			goto out;
838 		}
839 	}
840 
841 	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
842 		if (wiphy->rts_threshold > 255) {
843 			netdev_dbg(vif->ndev,
844 				   "Setting WIPHY_PARAM_RTS_THRESHOLD %d\n",
845 				   wiphy->rts_threshold);
846 			cfg_param_val.flag |= WILC_CFG_PARAM_RTS_THRESHOLD;
847 			cfg_param_val.rts_threshold = wiphy->rts_threshold;
848 		} else {
849 			netdev_err(vif->ndev, "RTS threshold out of range\n");
850 			goto out;
851 		}
852 	}
853 
854 	ret = wilc_hif_set_cfg(vif, &cfg_param_val);
855 	if (ret)
856 		netdev_err(priv->dev, "Error in setting WIPHY PARAMS\n");
857 
858 out:
859 	srcu_read_unlock(&wl->srcu, srcu_idx);
860 	return ret;
861 }
862 
set_pmksa(struct wiphy * wiphy,struct net_device * netdev,struct cfg80211_pmksa * pmksa)863 static int set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
864 		     struct cfg80211_pmksa *pmksa)
865 {
866 	struct wilc_vif *vif = netdev_priv(netdev);
867 	struct wilc_priv *priv = &vif->priv;
868 	u32 i;
869 	int ret = 0;
870 	u8 flag = 0;
871 
872 	for (i = 0; i < priv->pmkid_list.numpmkid; i++)	{
873 		if (!memcmp(pmksa->bssid, priv->pmkid_list.pmkidlist[i].bssid,
874 			    ETH_ALEN)) {
875 			flag = PMKID_FOUND;
876 			break;
877 		}
878 	}
879 	if (i < WILC_MAX_NUM_PMKIDS) {
880 		memcpy(priv->pmkid_list.pmkidlist[i].bssid, pmksa->bssid,
881 		       ETH_ALEN);
882 		memcpy(priv->pmkid_list.pmkidlist[i].pmkid, pmksa->pmkid,
883 		       WLAN_PMKID_LEN);
884 		if (!(flag == PMKID_FOUND))
885 			priv->pmkid_list.numpmkid++;
886 	} else {
887 		netdev_err(netdev, "Invalid PMKID index\n");
888 		ret = -EINVAL;
889 	}
890 
891 	if (!ret)
892 		ret = wilc_set_pmkid_info(vif, &priv->pmkid_list);
893 
894 	return ret;
895 }
896 
del_pmksa(struct wiphy * wiphy,struct net_device * netdev,struct cfg80211_pmksa * pmksa)897 static int del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
898 		     struct cfg80211_pmksa *pmksa)
899 {
900 	u32 i;
901 	struct wilc_vif *vif = netdev_priv(netdev);
902 	struct wilc_priv *priv = &vif->priv;
903 
904 	for (i = 0; i < priv->pmkid_list.numpmkid; i++)	{
905 		if (!memcmp(pmksa->bssid, priv->pmkid_list.pmkidlist[i].bssid,
906 			    ETH_ALEN)) {
907 			memset(&priv->pmkid_list.pmkidlist[i], 0,
908 			       sizeof(struct wilc_pmkid));
909 			break;
910 		}
911 	}
912 
913 	if (i == priv->pmkid_list.numpmkid)
914 		return -EINVAL;
915 
916 	for (; i < (priv->pmkid_list.numpmkid - 1); i++) {
917 		memcpy(priv->pmkid_list.pmkidlist[i].bssid,
918 		       priv->pmkid_list.pmkidlist[i + 1].bssid,
919 		       ETH_ALEN);
920 		memcpy(priv->pmkid_list.pmkidlist[i].pmkid,
921 		       priv->pmkid_list.pmkidlist[i + 1].pmkid,
922 		       WLAN_PMKID_LEN);
923 	}
924 	priv->pmkid_list.numpmkid--;
925 
926 	return 0;
927 }
928 
flush_pmksa(struct wiphy * wiphy,struct net_device * netdev)929 static int flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
930 {
931 	struct wilc_vif *vif = netdev_priv(netdev);
932 
933 	memset(&vif->priv.pmkid_list, 0, sizeof(struct wilc_pmkid_attr));
934 
935 	return 0;
936 }
937 
wilc_wfi_cfg_parse_ch_attr(u8 * buf,u32 len,u8 sta_ch)938 static inline void wilc_wfi_cfg_parse_ch_attr(u8 *buf, u32 len, u8 sta_ch)
939 {
940 	struct wilc_attr_entry *e;
941 	struct wilc_attr_ch_list *ch_list;
942 	struct wilc_attr_oper_ch *op_ch;
943 	u32 index = 0;
944 	u8 ch_list_idx = 0;
945 	u8 op_ch_idx = 0;
946 
947 	if (sta_ch == WILC_INVALID_CHANNEL)
948 		return;
949 
950 	while (index + sizeof(*e) <= len) {
951 		u16 attr_size;
952 
953 		e = (struct wilc_attr_entry *)&buf[index];
954 		attr_size = le16_to_cpu(e->attr_len);
955 
956 		if (index + sizeof(*e) + attr_size > len)
957 			return;
958 
959 		if (e->attr_type == IEEE80211_P2P_ATTR_CHANNEL_LIST &&
960 		    attr_size >= (sizeof(struct wilc_attr_ch_list) - sizeof(*e)))
961 			ch_list_idx = index;
962 		else if (e->attr_type == IEEE80211_P2P_ATTR_OPER_CHANNEL &&
963 			 attr_size == (sizeof(struct wilc_attr_oper_ch) - sizeof(*e)))
964 			op_ch_idx = index;
965 
966 		if (ch_list_idx && op_ch_idx)
967 			break;
968 
969 		index += sizeof(*e) + attr_size;
970 	}
971 
972 	if (ch_list_idx) {
973 		u16 elem_size;
974 
975 		ch_list = (struct wilc_attr_ch_list *)&buf[ch_list_idx];
976 		/* the number of bytes following the final 'elem' member */
977 		elem_size = le16_to_cpu(ch_list->attr_len) -
978 			(sizeof(*ch_list) - sizeof(struct wilc_attr_entry));
979 		for (unsigned int i = 0; i < elem_size;) {
980 			struct wilc_ch_list_elem *e;
981 
982 			e = (struct wilc_ch_list_elem *)(ch_list->elem + i);
983 
984 			i += sizeof(*e);
985 			if (i > elem_size)
986 				break;
987 
988 			i += e->no_of_channels;
989 			if (i > elem_size)
990 				break;
991 
992 			if (e->op_class == WILC_WLAN_OPERATING_CLASS_2_4GHZ) {
993 				memset(e->ch_list, sta_ch, e->no_of_channels);
994 				break;
995 			}
996 		}
997 	}
998 
999 	if (op_ch_idx) {
1000 		op_ch = (struct wilc_attr_oper_ch *)&buf[op_ch_idx];
1001 		op_ch->op_class = WILC_WLAN_OPERATING_CLASS_2_4GHZ;
1002 		op_ch->op_channel = sta_ch;
1003 	}
1004 }
1005 
wilc_wfi_mgmt_frame_rx(struct wilc_vif * vif,u8 * buff,u32 size)1006 bool wilc_wfi_mgmt_frame_rx(struct wilc_vif *vif, u8 *buff, u32 size)
1007 {
1008 	struct wilc *wl = vif->wilc;
1009 	struct wilc_priv *priv = &vif->priv;
1010 	int freq;
1011 
1012 	freq = ieee80211_channel_to_frequency(wl->op_ch, NL80211_BAND_2GHZ);
1013 
1014 	return cfg80211_rx_mgmt(&priv->wdev, freq, 0, buff, size, 0);
1015 }
1016 
wilc_wfi_p2p_rx(struct wilc_vif * vif,u8 * buff,u32 size)1017 void wilc_wfi_p2p_rx(struct wilc_vif *vif, u8 *buff, u32 size)
1018 {
1019 	struct wilc *wl = vif->wilc;
1020 	struct wilc_priv *priv = &vif->priv;
1021 	struct host_if_drv *wfi_drv = priv->hif_drv;
1022 	struct ieee80211_mgmt *mgmt;
1023 	struct wilc_vendor_specific_ie *p;
1024 	struct wilc_p2p_pub_act_frame *d;
1025 	int ie_offset = offsetof(struct ieee80211_mgmt, u) + sizeof(*d);
1026 	const u8 *vendor_ie;
1027 	u32 header, pkt_offset;
1028 	s32 freq;
1029 
1030 	header = get_unaligned_le32(buff - HOST_HDR_OFFSET);
1031 	pkt_offset = FIELD_GET(WILC_PKT_HDR_OFFSET_FIELD, header);
1032 
1033 	if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
1034 		bool ack = false;
1035 		struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)buff;
1036 
1037 		if (ieee80211_is_probe_resp(hdr->frame_control) ||
1038 		    pkt_offset & IS_MGMT_STATUS_SUCCES)
1039 			ack = true;
1040 
1041 		cfg80211_mgmt_tx_status(&priv->wdev, priv->tx_cookie, buff,
1042 					size, ack, GFP_KERNEL);
1043 		return;
1044 	}
1045 
1046 	freq = ieee80211_channel_to_frequency(wl->op_ch, NL80211_BAND_2GHZ);
1047 
1048 	mgmt = (struct ieee80211_mgmt *)buff;
1049 	if (!ieee80211_is_action(mgmt->frame_control))
1050 		goto out_rx_mgmt;
1051 
1052 	if (priv->cfg_scanning &&
1053 	    time_after_eq(jiffies, (unsigned long)wfi_drv->p2p_timeout)) {
1054 		netdev_dbg(vif->ndev, "Receiving action wrong ch\n");
1055 		return;
1056 	}
1057 
1058 	if (!ieee80211_is_public_action((struct ieee80211_hdr *)buff, size))
1059 		goto out_rx_mgmt;
1060 
1061 	d = (struct wilc_p2p_pub_act_frame *)(&mgmt->u.action);
1062 	if (d->oui_subtype != GO_NEG_REQ && d->oui_subtype != GO_NEG_RSP &&
1063 	    d->oui_subtype != P2P_INV_REQ && d->oui_subtype != P2P_INV_RSP)
1064 		goto out_rx_mgmt;
1065 
1066 	vendor_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1067 					    buff + ie_offset, size - ie_offset);
1068 	if (!vendor_ie)
1069 		goto out_rx_mgmt;
1070 
1071 	p = (struct wilc_vendor_specific_ie *)vendor_ie;
1072 	wilc_wfi_cfg_parse_ch_attr(p->attr, p->tag_len - 4, vif->wilc->sta_ch);
1073 
1074 out_rx_mgmt:
1075 	cfg80211_rx_mgmt(&priv->wdev, freq, 0, buff, size, 0);
1076 }
1077 
wilc_wfi_mgmt_tx_complete(void * priv,int status)1078 static void wilc_wfi_mgmt_tx_complete(void *priv, int status)
1079 {
1080 	struct wilc_p2p_mgmt_data *pv_data = priv;
1081 
1082 	kfree(pv_data->buff);
1083 	kfree(pv_data);
1084 }
1085 
wilc_wfi_remain_on_channel_expired(struct wilc_vif * vif,u64 cookie)1086 static void wilc_wfi_remain_on_channel_expired(struct wilc_vif *vif, u64 cookie)
1087 {
1088 	struct wilc_priv *priv = &vif->priv;
1089 	struct wilc_wfi_p2p_listen_params *params = &priv->remain_on_ch_params;
1090 
1091 	if (cookie != params->listen_cookie)
1092 		return;
1093 
1094 	priv->p2p_listen_state = false;
1095 
1096 	cfg80211_remain_on_channel_expired(&priv->wdev, params->listen_cookie,
1097 					   params->listen_ch, GFP_KERNEL);
1098 }
1099 
remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,struct ieee80211_channel * chan,unsigned int duration,u64 cookie,const u8 * rx_addr)1100 static int remain_on_channel(struct wiphy *wiphy,
1101 			     struct wireless_dev *wdev,
1102 			     struct ieee80211_channel *chan,
1103 			     unsigned int duration, u64 cookie,
1104 			     const u8 *rx_addr)
1105 {
1106 	int ret = 0;
1107 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1108 	struct wilc_priv *priv = &vif->priv;
1109 
1110 	if (wdev->iftype == NL80211_IFTYPE_AP) {
1111 		netdev_dbg(vif->ndev, "Required while in AP mode\n");
1112 		return ret;
1113 	}
1114 
1115 	ret = wilc_remain_on_channel(vif, cookie, chan->hw_value,
1116 				     wilc_wfi_remain_on_channel_expired);
1117 	if (ret)
1118 		return ret;
1119 
1120 	vif->wilc->op_ch = chan->hw_value;
1121 
1122 	priv->remain_on_ch_params.listen_ch = chan;
1123 	priv->remain_on_ch_params.listen_cookie = cookie;
1124 	priv->p2p_listen_state = true;
1125 	priv->remain_on_ch_params.listen_duration = duration;
1126 
1127 	cfg80211_ready_on_channel(wdev, cookie, chan, duration, GFP_KERNEL);
1128 	mod_timer(&vif->hif_drv->remain_on_ch_timer,
1129 		  jiffies + msecs_to_jiffies(duration + 1000));
1130 
1131 	return ret;
1132 }
1133 
cancel_remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,u64 cookie)1134 static int cancel_remain_on_channel(struct wiphy *wiphy,
1135 				    struct wireless_dev *wdev,
1136 				    u64 cookie)
1137 {
1138 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1139 	struct wilc_priv *priv = &vif->priv;
1140 
1141 	if (cookie != priv->remain_on_ch_params.listen_cookie)
1142 		return -ENOENT;
1143 
1144 	return wilc_listen_state_expired(vif, cookie);
1145 }
1146 
mgmt_tx(struct wiphy * wiphy,struct wireless_dev * wdev,struct cfg80211_mgmt_tx_params * params,u64 cookie)1147 static int mgmt_tx(struct wiphy *wiphy,
1148 		   struct wireless_dev *wdev,
1149 		   struct cfg80211_mgmt_tx_params *params,
1150 		   u64 cookie)
1151 {
1152 	struct ieee80211_channel *chan = params->chan;
1153 	unsigned int wait = params->wait;
1154 	const u8 *buf = params->buf;
1155 	size_t len = params->len;
1156 	const struct ieee80211_mgmt *mgmt;
1157 	struct wilc_p2p_mgmt_data *mgmt_tx;
1158 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1159 	struct wilc_priv *priv = &vif->priv;
1160 	struct host_if_drv *wfi_drv = priv->hif_drv;
1161 	struct wilc_vendor_specific_ie *p;
1162 	struct wilc_p2p_pub_act_frame *d;
1163 	int ie_offset = offsetof(struct ieee80211_mgmt, u) + sizeof(*d);
1164 	const u8 *vendor_ie;
1165 	int ret = 0;
1166 
1167 	priv->tx_cookie = cookie;
1168 	mgmt = (const struct ieee80211_mgmt *)buf;
1169 
1170 	if (!ieee80211_is_mgmt(mgmt->frame_control))
1171 		goto out;
1172 
1173 	mgmt_tx = kmalloc_obj(*mgmt_tx);
1174 	if (!mgmt_tx) {
1175 		ret = -ENOMEM;
1176 		goto out;
1177 	}
1178 
1179 	mgmt_tx->buff = kmemdup(buf, len, GFP_KERNEL);
1180 	if (!mgmt_tx->buff) {
1181 		ret = -ENOMEM;
1182 		kfree(mgmt_tx);
1183 		goto out;
1184 	}
1185 
1186 	mgmt_tx->size = len;
1187 
1188 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {
1189 		wilc_set_mac_chnl_num(vif, chan->hw_value);
1190 		vif->wilc->op_ch = chan->hw_value;
1191 		goto out_txq_add_pkt;
1192 	}
1193 
1194 	if (!ieee80211_is_public_action((struct ieee80211_hdr *)buf, len)) {
1195 		if (chan)
1196 			wilc_set_mac_chnl_num(vif, chan->hw_value);
1197 		else
1198 			wilc_set_mac_chnl_num(vif, vif->wilc->op_ch);
1199 
1200 		goto out_set_timeout;
1201 	}
1202 
1203 	d = (struct wilc_p2p_pub_act_frame *)(&mgmt->u.action);
1204 	if (d->oui_type != WLAN_OUI_TYPE_WFA_P2P ||
1205 	    d->oui_subtype != GO_NEG_CONF) {
1206 		wilc_set_mac_chnl_num(vif, chan->hw_value);
1207 		vif->wilc->op_ch = chan->hw_value;
1208 	}
1209 
1210 	if (d->oui_subtype != P2P_INV_REQ && d->oui_subtype != P2P_INV_RSP)
1211 		goto out_set_timeout;
1212 
1213 	vendor_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1214 					    mgmt_tx->buff + ie_offset,
1215 					    len - ie_offset);
1216 	if (!vendor_ie)
1217 		goto out_set_timeout;
1218 
1219 	p = (struct wilc_vendor_specific_ie *)vendor_ie;
1220 	wilc_wfi_cfg_parse_ch_attr(p->attr, p->tag_len - 4, vif->wilc->sta_ch);
1221 
1222 out_set_timeout:
1223 	wfi_drv->p2p_timeout = (jiffies + msecs_to_jiffies(wait));
1224 
1225 out_txq_add_pkt:
1226 
1227 	wilc_wlan_txq_add_mgmt_pkt(wdev->netdev, mgmt_tx,
1228 				   mgmt_tx->buff, mgmt_tx->size,
1229 				   wilc_wfi_mgmt_tx_complete);
1230 
1231 out:
1232 
1233 	return ret;
1234 }
1235 
mgmt_tx_cancel_wait(struct wiphy * wiphy,struct wireless_dev * wdev,u64 cookie)1236 static int mgmt_tx_cancel_wait(struct wiphy *wiphy,
1237 			       struct wireless_dev *wdev,
1238 			       u64 cookie)
1239 {
1240 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1241 	struct wilc_priv *priv = &vif->priv;
1242 	struct host_if_drv *wfi_drv = priv->hif_drv;
1243 
1244 	wfi_drv->p2p_timeout = jiffies;
1245 
1246 	if (!priv->p2p_listen_state) {
1247 		struct wilc_wfi_p2p_listen_params *params;
1248 
1249 		params = &priv->remain_on_ch_params;
1250 
1251 		cfg80211_remain_on_channel_expired(wdev,
1252 						   params->listen_cookie,
1253 						   params->listen_ch,
1254 						   GFP_KERNEL);
1255 	}
1256 
1257 	return 0;
1258 }
1259 
wilc_update_mgmt_frame_registrations(struct wiphy * wiphy,struct wireless_dev * wdev,struct mgmt_frame_regs * upd)1260 void wilc_update_mgmt_frame_registrations(struct wiphy *wiphy,
1261 					  struct wireless_dev *wdev,
1262 					  struct mgmt_frame_regs *upd)
1263 {
1264 	struct wilc *wl = wiphy_priv(wiphy);
1265 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1266 	u32 presp_bit = BIT(IEEE80211_STYPE_PROBE_REQ >> 4);
1267 	u32 action_bit = BIT(IEEE80211_STYPE_ACTION >> 4);
1268 	u32 pauth_bit = BIT(IEEE80211_STYPE_AUTH >> 4);
1269 
1270 	if (wl->initialized) {
1271 		bool prev = vif->mgmt_reg_stypes & presp_bit;
1272 		bool now = upd->interface_stypes & presp_bit;
1273 
1274 		if (now != prev)
1275 			wilc_frame_register(vif, IEEE80211_STYPE_PROBE_REQ, now);
1276 
1277 		prev = vif->mgmt_reg_stypes & action_bit;
1278 		now = upd->interface_stypes & action_bit;
1279 
1280 		if (now != prev)
1281 			wilc_frame_register(vif, IEEE80211_STYPE_ACTION, now);
1282 
1283 		prev = vif->mgmt_reg_stypes & pauth_bit;
1284 		now = upd->interface_stypes & pauth_bit;
1285 		if (now != prev)
1286 			wilc_frame_register(vif, IEEE80211_STYPE_AUTH, now);
1287 	}
1288 
1289 	vif->mgmt_reg_stypes =
1290 		upd->interface_stypes & (presp_bit | action_bit | pauth_bit);
1291 }
1292 
external_auth(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_external_auth_params * auth)1293 static int external_auth(struct wiphy *wiphy, struct net_device *dev,
1294 			 struct cfg80211_external_auth_params *auth)
1295 {
1296 	struct wilc_vif *vif = netdev_priv(dev);
1297 
1298 	if (auth->status == WLAN_STATUS_SUCCESS)
1299 		wilc_set_external_auth_param(vif, auth);
1300 
1301 	return 0;
1302 }
1303 
set_cqm_rssi_config(struct wiphy * wiphy,struct net_device * dev,s32 rssi_thold,u32 rssi_hyst)1304 static int set_cqm_rssi_config(struct wiphy *wiphy, struct net_device *dev,
1305 			       s32 rssi_thold, u32 rssi_hyst)
1306 {
1307 	return 0;
1308 }
1309 
dump_station(struct wiphy * wiphy,struct wireless_dev * wdev,int idx,u8 * mac,struct station_info * sinfo)1310 static int dump_station(struct wiphy *wiphy, struct wireless_dev *wdev,
1311 			int idx, u8 *mac, struct station_info *sinfo)
1312 {
1313 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1314 	int ret;
1315 
1316 	if (idx != 0)
1317 		return -ENOENT;
1318 
1319 	ret = wilc_get_rssi(vif, &sinfo->signal);
1320 	if (ret)
1321 		return ret;
1322 
1323 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
1324 	memcpy(mac, vif->priv.associated_bss, ETH_ALEN);
1325 	return 0;
1326 }
1327 
set_power_mgmt(struct wiphy * wiphy,struct net_device * dev,bool enabled,int timeout)1328 static int set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
1329 			  bool enabled, int timeout)
1330 {
1331 	struct wilc_vif *vif = netdev_priv(dev);
1332 	struct wilc_priv *priv = &vif->priv;
1333 
1334 	if (!priv->hif_drv)
1335 		return -EIO;
1336 
1337 	wilc_set_power_mgmt(vif, enabled, timeout);
1338 
1339 	return 0;
1340 }
1341 
change_virtual_intf(struct wiphy * wiphy,struct net_device * dev,enum nl80211_iftype type,struct vif_params * params)1342 static int change_virtual_intf(struct wiphy *wiphy, struct net_device *dev,
1343 			       enum nl80211_iftype type,
1344 			       struct vif_params *params)
1345 {
1346 	struct wilc *wl = wiphy_priv(wiphy);
1347 	struct wilc_vif *vif = netdev_priv(dev);
1348 	struct wilc_priv *priv = &vif->priv;
1349 
1350 	switch (type) {
1351 	case NL80211_IFTYPE_STATION:
1352 		vif->connecting = false;
1353 		dev->ieee80211_ptr->iftype = type;
1354 		priv->wdev.iftype = type;
1355 		vif->monitor_flag = 0;
1356 		if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE)
1357 			wilc_wfi_deinit_mon_interface(wl, true);
1358 		vif->iftype = WILC_STATION_MODE;
1359 
1360 		if (wl->initialized)
1361 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1362 						WILC_STATION_MODE, vif->idx);
1363 
1364 		memset(priv->assoc_stainfo.sta_associated_bss, 0,
1365 		       WILC_MAX_NUM_STA * ETH_ALEN);
1366 		break;
1367 
1368 	case NL80211_IFTYPE_P2P_CLIENT:
1369 		vif->connecting = false;
1370 		dev->ieee80211_ptr->iftype = type;
1371 		priv->wdev.iftype = type;
1372 		vif->monitor_flag = 0;
1373 		vif->iftype = WILC_CLIENT_MODE;
1374 
1375 		if (wl->initialized)
1376 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1377 						WILC_STATION_MODE, vif->idx);
1378 		break;
1379 
1380 	case NL80211_IFTYPE_AP:
1381 		dev->ieee80211_ptr->iftype = type;
1382 		priv->wdev.iftype = type;
1383 		vif->iftype = WILC_AP_MODE;
1384 
1385 		if (wl->initialized)
1386 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1387 						WILC_AP_MODE, vif->idx);
1388 		break;
1389 
1390 	case NL80211_IFTYPE_P2P_GO:
1391 		dev->ieee80211_ptr->iftype = type;
1392 		priv->wdev.iftype = type;
1393 		vif->iftype = WILC_GO_MODE;
1394 
1395 		if (wl->initialized)
1396 			wilc_set_operation_mode(vif, wilc_get_vif_idx(vif),
1397 						WILC_AP_MODE, vif->idx);
1398 		break;
1399 
1400 	default:
1401 		netdev_err(dev, "Unknown interface type= %d\n", type);
1402 		return -EINVAL;
1403 	}
1404 
1405 	return 0;
1406 }
1407 
start_ap(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ap_settings * settings)1408 static int start_ap(struct wiphy *wiphy, struct net_device *dev,
1409 		    struct cfg80211_ap_settings *settings)
1410 {
1411 	struct wilc_vif *vif = netdev_priv(dev);
1412 	int ret;
1413 
1414 	ret = set_channel(wiphy, dev, &settings->chandef);
1415 	if (ret != 0)
1416 		netdev_err(dev, "Error in setting channel\n");
1417 
1418 	wilc_wlan_set_bssid(dev, dev->dev_addr, WILC_AP_MODE);
1419 
1420 	return wilc_add_beacon(vif, settings->beacon_interval,
1421 				   settings->dtim_period, &settings->beacon);
1422 }
1423 
change_beacon(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ap_update * params)1424 static int change_beacon(struct wiphy *wiphy, struct net_device *dev,
1425 			 struct cfg80211_ap_update *params)
1426 {
1427 	struct wilc_vif *vif = netdev_priv(dev);
1428 
1429 	return wilc_add_beacon(vif, 0, 0, &params->beacon);
1430 }
1431 
stop_ap(struct wiphy * wiphy,struct net_device * dev,unsigned int link_id)1432 static int stop_ap(struct wiphy *wiphy, struct net_device *dev,
1433 		   unsigned int link_id)
1434 {
1435 	int ret;
1436 	struct wilc_vif *vif = netdev_priv(dev);
1437 
1438 	wilc_wlan_set_bssid(dev, NULL, WILC_AP_MODE);
1439 
1440 	ret = wilc_del_beacon(vif);
1441 
1442 	if (ret)
1443 		netdev_err(dev, "Host delete beacon fail\n");
1444 
1445 	return ret;
1446 }
1447 
add_station(struct wiphy * wiphy,struct wireless_dev * wdev,const u8 * mac,struct station_parameters * params)1448 static int add_station(struct wiphy *wiphy, struct wireless_dev *wdev,
1449 		       const u8 *mac, struct station_parameters *params)
1450 {
1451 	int ret = 0;
1452 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1453 	struct wilc_priv *priv = &vif->priv;
1454 
1455 	if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE) {
1456 		memcpy(priv->assoc_stainfo.sta_associated_bss[params->aid], mac,
1457 		       ETH_ALEN);
1458 
1459 		ret = wilc_add_station(vif, mac, params);
1460 		if (ret)
1461 			netdev_err(wdev->netdev, "Host add station fail\n");
1462 	}
1463 
1464 	return ret;
1465 }
1466 
del_station(struct wiphy * wiphy,struct wireless_dev * wdev,struct station_del_parameters * params)1467 static int del_station(struct wiphy *wiphy, struct wireless_dev *wdev,
1468 		       struct station_del_parameters *params)
1469 {
1470 	const u8 *mac = params->mac;
1471 	int ret = 0;
1472 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1473 	struct wilc_priv *priv = &vif->priv;
1474 	struct sta_info *info;
1475 
1476 	if (!(vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE))
1477 		return ret;
1478 
1479 	info = &priv->assoc_stainfo;
1480 
1481 	if (!mac)
1482 		ret = wilc_del_allstation(vif, info->sta_associated_bss);
1483 
1484 	ret = wilc_del_station(vif, mac);
1485 	if (ret)
1486 		netdev_err(wdev->netdev, "Host delete station fail\n");
1487 	return ret;
1488 }
1489 
change_station(struct wiphy * wiphy,struct wireless_dev * wdev,const u8 * mac,struct station_parameters * params)1490 static int change_station(struct wiphy *wiphy, struct wireless_dev *wdev,
1491 			  const u8 *mac, struct station_parameters *params)
1492 {
1493 	int ret = 0;
1494 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1495 	if (vif->iftype == WILC_AP_MODE || vif->iftype == WILC_GO_MODE) {
1496 		ret = wilc_edit_station(vif, mac, params);
1497 		if (ret)
1498 			netdev_err(wdev->netdev, "Host edit station fail\n");
1499 	}
1500 	return ret;
1501 }
1502 
wilc_get_vif_from_type(struct wilc * wl,int type)1503 static struct wilc_vif *wilc_get_vif_from_type(struct wilc *wl, int type)
1504 {
1505 	struct wilc_vif *vif;
1506 
1507 	wilc_for_each_vif(wl, vif) {
1508 		if (vif->iftype == type)
1509 			return vif;
1510 	}
1511 
1512 	return NULL;
1513 }
1514 
add_virtual_intf(struct wiphy * wiphy,const char * name,unsigned char name_assign_type,enum nl80211_iftype type,struct vif_params * params)1515 static struct wireless_dev *add_virtual_intf(struct wiphy *wiphy,
1516 					     const char *name,
1517 					     unsigned char name_assign_type,
1518 					     enum nl80211_iftype type,
1519 					     struct vif_params *params)
1520 {
1521 	struct wilc *wl = wiphy_priv(wiphy);
1522 	struct wilc_vif *vif;
1523 	struct wireless_dev *wdev;
1524 	int iftype;
1525 
1526 	if (type == NL80211_IFTYPE_MONITOR) {
1527 		struct net_device *ndev;
1528 		int srcu_idx;
1529 
1530 		srcu_idx = srcu_read_lock(&wl->srcu);
1531 		vif = wilc_get_vif_from_type(wl, WILC_AP_MODE);
1532 		if (!vif) {
1533 			vif = wilc_get_vif_from_type(wl, WILC_GO_MODE);
1534 			if (!vif) {
1535 				srcu_read_unlock(&wl->srcu, srcu_idx);
1536 				goto validate_interface;
1537 			}
1538 		}
1539 
1540 		if (vif->monitor_flag) {
1541 			srcu_read_unlock(&wl->srcu, srcu_idx);
1542 			goto validate_interface;
1543 		}
1544 
1545 		ndev = wilc_wfi_init_mon_interface(wl, name, vif->ndev);
1546 		if (ndev) {
1547 			vif->monitor_flag = 1;
1548 		} else {
1549 			srcu_read_unlock(&wl->srcu, srcu_idx);
1550 			return ERR_PTR(-EINVAL);
1551 		}
1552 
1553 		wdev = &vif->priv.wdev;
1554 		srcu_read_unlock(&wl->srcu, srcu_idx);
1555 		return wdev;
1556 	}
1557 
1558 validate_interface:
1559 	mutex_lock(&wl->vif_mutex);
1560 	if (wl->vif_num == WILC_NUM_CONCURRENT_IFC) {
1561 		pr_err("Reached maximum number of interface\n");
1562 		mutex_unlock(&wl->vif_mutex);
1563 		return ERR_PTR(-EINVAL);
1564 	}
1565 	mutex_unlock(&wl->vif_mutex);
1566 
1567 	switch (type) {
1568 	case NL80211_IFTYPE_STATION:
1569 		iftype = WILC_STATION_MODE;
1570 		break;
1571 	case NL80211_IFTYPE_AP:
1572 		iftype = WILC_AP_MODE;
1573 		break;
1574 	default:
1575 		return ERR_PTR(-EOPNOTSUPP);
1576 	}
1577 
1578 	vif = wilc_netdev_ifc_init(wl, name, iftype, type, true);
1579 	if (IS_ERR(vif))
1580 		return ERR_CAST(vif);
1581 
1582 	return &vif->priv.wdev;
1583 }
1584 
del_virtual_intf(struct wiphy * wiphy,struct wireless_dev * wdev)1585 static int del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
1586 {
1587 	struct wilc *wl = wiphy_priv(wiphy);
1588 	struct wilc_vif *vif;
1589 
1590 	if (wdev->iftype == NL80211_IFTYPE_AP ||
1591 	    wdev->iftype == NL80211_IFTYPE_P2P_GO)
1592 		wilc_wfi_deinit_mon_interface(wl, true);
1593 	vif = netdev_priv(wdev->netdev);
1594 	cfg80211_stop_iface(wiphy, wdev, GFP_KERNEL);
1595 	cfg80211_unregister_netdevice(vif->ndev);
1596 	vif->monitor_flag = 0;
1597 
1598 	mutex_lock(&wl->vif_mutex);
1599 	list_del_rcu(&vif->list);
1600 	wl->vif_num--;
1601 	mutex_unlock(&wl->vif_mutex);
1602 	synchronize_srcu(&wl->srcu);
1603 	return 0;
1604 }
1605 
wilc_set_wakeup(struct wiphy * wiphy,bool enabled)1606 static void wilc_set_wakeup(struct wiphy *wiphy, bool enabled)
1607 {
1608 	struct wilc *wl = wiphy_priv(wiphy);
1609 	struct wilc_vif *vif;
1610 	int srcu_idx;
1611 
1612 	srcu_idx = srcu_read_lock(&wl->srcu);
1613 	vif = wilc_get_wl_to_vif(wl);
1614 	if (IS_ERR(vif)) {
1615 		srcu_read_unlock(&wl->srcu, srcu_idx);
1616 		return;
1617 	}
1618 
1619 	netdev_info(vif->ndev, "cfg set wake up = %d\n", enabled);
1620 	wilc_set_wowlan_trigger(vif, enabled);
1621 	srcu_read_unlock(&wl->srcu, srcu_idx);
1622 }
1623 
set_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,int radio_idx,enum nl80211_tx_power_setting type,int mbm)1624 static int set_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
1625 			int radio_idx, enum nl80211_tx_power_setting type,
1626 			int mbm)
1627 {
1628 	int ret;
1629 	int srcu_idx;
1630 	s32 tx_power = MBM_TO_DBM(mbm);
1631 	struct wilc *wl = wiphy_priv(wiphy);
1632 	struct wilc_vif *vif;
1633 
1634 	if (!wl->initialized)
1635 		return -EIO;
1636 
1637 	srcu_idx = srcu_read_lock(&wl->srcu);
1638 	vif = wilc_get_wl_to_vif(wl);
1639 	if (IS_ERR(vif)) {
1640 		srcu_read_unlock(&wl->srcu, srcu_idx);
1641 		return -EINVAL;
1642 	}
1643 
1644 	netdev_info(vif->ndev, "Setting tx power %d\n", tx_power);
1645 	if (tx_power < 0)
1646 		tx_power = 0;
1647 	else if (tx_power > 18)
1648 		tx_power = 18;
1649 	ret = wilc_set_tx_power(vif, tx_power);
1650 	if (ret)
1651 		netdev_err(vif->ndev, "Failed to set tx power\n");
1652 	srcu_read_unlock(&wl->srcu, srcu_idx);
1653 
1654 	return ret;
1655 }
1656 
get_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,int radio_idx,unsigned int link_id,int * dbm)1657 static int get_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
1658 			int radio_idx, unsigned int link_id, int *dbm)
1659 {
1660 	int ret;
1661 	struct wilc_vif *vif = netdev_priv(wdev->netdev);
1662 	struct wilc *wl = vif->wilc;
1663 
1664 	/* If firmware is not started, return. */
1665 	if (!wl->initialized)
1666 		return -EIO;
1667 
1668 	ret = wilc_get_tx_power(vif, (u8 *)dbm);
1669 	if (ret)
1670 		netdev_err(vif->ndev, "Failed to get tx power\n");
1671 
1672 	return ret;
1673 }
1674 
1675 static const struct cfg80211_ops wilc_cfg80211_ops = {
1676 	.set_monitor_channel = set_channel,
1677 	.scan = scan,
1678 	.connect = connect,
1679 	.disconnect = disconnect,
1680 	.add_key = add_key,
1681 	.del_key = del_key,
1682 	.get_key = get_key,
1683 	.set_default_key = set_default_key,
1684 	.set_default_mgmt_key = set_default_mgmt_key,
1685 	.add_virtual_intf = add_virtual_intf,
1686 	.del_virtual_intf = del_virtual_intf,
1687 	.change_virtual_intf = change_virtual_intf,
1688 
1689 	.start_ap = start_ap,
1690 	.change_beacon = change_beacon,
1691 	.stop_ap = stop_ap,
1692 	.add_station = add_station,
1693 	.del_station = del_station,
1694 	.change_station = change_station,
1695 	.get_station = get_station,
1696 	.dump_station = dump_station,
1697 	.set_wiphy_params = set_wiphy_params,
1698 
1699 	.external_auth = external_auth,
1700 	.set_pmksa = set_pmksa,
1701 	.del_pmksa = del_pmksa,
1702 	.flush_pmksa = flush_pmksa,
1703 	.remain_on_channel = remain_on_channel,
1704 	.cancel_remain_on_channel = cancel_remain_on_channel,
1705 	.mgmt_tx_cancel_wait = mgmt_tx_cancel_wait,
1706 	.mgmt_tx = mgmt_tx,
1707 	.update_mgmt_frame_registrations = wilc_update_mgmt_frame_registrations,
1708 	.set_power_mgmt = set_power_mgmt,
1709 	.set_cqm_rssi_config = set_cqm_rssi_config,
1710 
1711 	.set_wakeup = wilc_set_wakeup,
1712 	.set_tx_power = set_tx_power,
1713 	.get_tx_power = get_tx_power,
1714 
1715 };
1716 
wlan_init_locks(struct wilc * wl)1717 static void wlan_init_locks(struct wilc *wl)
1718 {
1719 	mutex_init(&wl->hif_cs);
1720 	mutex_init(&wl->rxq_cs);
1721 	mutex_init(&wl->cfg_cmd_lock);
1722 	mutex_init(&wl->vif_mutex);
1723 	mutex_init(&wl->deinit_lock);
1724 
1725 	spin_lock_init(&wl->txq_spinlock);
1726 	mutex_init(&wl->txq_add_to_head_cs);
1727 
1728 	init_completion(&wl->txq_event);
1729 	init_completion(&wl->cfg_event);
1730 	init_completion(&wl->sync_event);
1731 	init_completion(&wl->txq_thread_started);
1732 	init_srcu_struct(&wl->srcu);
1733 }
1734 
wlan_deinit_locks(struct wilc * wilc)1735 void wlan_deinit_locks(struct wilc *wilc)
1736 {
1737 	mutex_destroy(&wilc->hif_cs);
1738 	mutex_destroy(&wilc->rxq_cs);
1739 	mutex_destroy(&wilc->cfg_cmd_lock);
1740 	mutex_destroy(&wilc->txq_add_to_head_cs);
1741 	mutex_destroy(&wilc->vif_mutex);
1742 	mutex_destroy(&wilc->deinit_lock);
1743 	cleanup_srcu_struct(&wilc->srcu);
1744 }
1745 
wilc_create_wiphy(struct device * dev)1746 static struct wilc *wilc_create_wiphy(struct device *dev)
1747 {
1748 	struct wiphy *wiphy;
1749 	struct wilc *wl;
1750 
1751 	wiphy = wiphy_new(&wilc_cfg80211_ops, sizeof(*wl));
1752 	if (!wiphy)
1753 		return NULL;
1754 
1755 	wl = wiphy_priv(wiphy);
1756 
1757 	memcpy(wl->bitrates, wilc_bitrates, sizeof(wilc_bitrates));
1758 	memcpy(wl->channels, wilc_2ghz_channels, sizeof(wilc_2ghz_channels));
1759 	wl->band.bitrates = wl->bitrates;
1760 	wl->band.n_bitrates = ARRAY_SIZE(wl->bitrates);
1761 	wl->band.channels = wl->channels;
1762 	wl->band.n_channels = ARRAY_SIZE(wilc_2ghz_channels);
1763 
1764 	wl->band.ht_cap.ht_supported = 1;
1765 	wl->band.ht_cap.cap |= (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
1766 	wl->band.ht_cap.mcs.rx_mask[0] = 0xff;
1767 	wl->band.ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_8K;
1768 	wl->band.ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
1769 
1770 	wiphy->bands[NL80211_BAND_2GHZ] = &wl->band;
1771 
1772 	wiphy->max_scan_ssids = WILC_MAX_NUM_PROBED_SSID;
1773 #ifdef CONFIG_PM
1774 	wiphy->wowlan = &wowlan_support;
1775 #endif
1776 	wiphy->max_num_pmkids = WILC_MAX_NUM_PMKIDS;
1777 	wiphy->max_scan_ie_len = 1000;
1778 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1779 	memcpy(wl->cipher_suites, wilc_cipher_suites,
1780 	       sizeof(wilc_cipher_suites));
1781 	wiphy->cipher_suites = wl->cipher_suites;
1782 	wiphy->n_cipher_suites = ARRAY_SIZE(wilc_cipher_suites);
1783 	wiphy->mgmt_stypes = wilc_wfi_cfg80211_mgmt_types;
1784 
1785 	wiphy->max_remain_on_channel_duration = 500;
1786 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1787 				BIT(NL80211_IFTYPE_AP) |
1788 				BIT(NL80211_IFTYPE_MONITOR) |
1789 				BIT(NL80211_IFTYPE_P2P_GO) |
1790 				BIT(NL80211_IFTYPE_P2P_CLIENT);
1791 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
1792 	set_wiphy_dev(wiphy, dev);
1793 	wl->wiphy = wiphy;
1794 	return wl;
1795 }
1796 
wilc_cfg80211_init(struct wilc ** wilc,struct device * dev,int io_type,const struct wilc_hif_func * ops)1797 int wilc_cfg80211_init(struct wilc **wilc, struct device *dev, int io_type,
1798 		       const struct wilc_hif_func *ops)
1799 {
1800 	struct wilc *wl;
1801 	int ret, i;
1802 
1803 	wl = wilc_create_wiphy(dev);
1804 	if (!wl)
1805 		return -EINVAL;
1806 
1807 	wlan_init_locks(wl);
1808 
1809 	ret = wilc_wlan_cfg_init(wl);
1810 	if (ret)
1811 		goto free_wl;
1812 
1813 	*wilc = wl;
1814 	wl->io_type = io_type;
1815 	wl->hif_func = ops;
1816 
1817 	for (i = 0; i < NQUEUES; i++)
1818 		INIT_LIST_HEAD(&wl->txq[i].txq_head.list);
1819 
1820 	INIT_LIST_HEAD(&wl->rxq_head.list);
1821 	INIT_LIST_HEAD(&wl->vif_list);
1822 
1823 	wl->hif_workqueue = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
1824 						    wiphy_name(wl->wiphy));
1825 	if (!wl->hif_workqueue) {
1826 		ret = -ENOMEM;
1827 		goto free_cfg;
1828 	}
1829 
1830 	return 0;
1831 
1832 free_cfg:
1833 	wilc_wlan_cfg_deinit(wl);
1834 
1835 free_wl:
1836 	wlan_deinit_locks(wl);
1837 	wiphy_free(wl->wiphy);
1838 	return ret;
1839 }
1840 EXPORT_SYMBOL_GPL(wilc_cfg80211_init);
1841 
wilc_cfg80211_register(struct wilc * wilc)1842 int wilc_cfg80211_register(struct wilc *wilc)
1843 {
1844 	/* WPA3/SAE supported only on WILC1000 */
1845 	if (is_wilc1000(wilc->chipid))
1846 		wilc->wiphy->features |= NL80211_FEATURE_SAE;
1847 
1848 	return wiphy_register(wilc->wiphy);
1849 }
1850 EXPORT_SYMBOL_GPL(wilc_cfg80211_register);
1851 
wilc_init_host_int(struct net_device * net)1852 int wilc_init_host_int(struct net_device *net)
1853 {
1854 	int ret;
1855 	struct wilc_vif *vif = netdev_priv(net);
1856 	struct wilc_priv *priv = &vif->priv;
1857 
1858 	priv->p2p_listen_state = false;
1859 
1860 	mutex_init(&priv->scan_req_lock);
1861 	ret = wilc_init(net, &priv->hif_drv);
1862 	if (ret)
1863 		netdev_err(net, "Error while initializing hostinterface\n");
1864 
1865 	return ret;
1866 }
1867 
wilc_deinit_host_int(struct net_device * net)1868 void wilc_deinit_host_int(struct net_device *net)
1869 {
1870 	int ret;
1871 	struct wilc_vif *vif = netdev_priv(net);
1872 	struct wilc_priv *priv = &vif->priv;
1873 
1874 	priv->p2p_listen_state = false;
1875 
1876 	flush_workqueue(vif->wilc->hif_workqueue);
1877 	mutex_destroy(&priv->scan_req_lock);
1878 	ret = wilc_deinit(vif);
1879 
1880 	if (ret)
1881 		netdev_err(net, "Error while deinitializing host interface\n");
1882 }
1883 
1884