xref: /linux/drivers/net/wireless/silabs/wfx/sta.c (revision 1a9239bb4253f9076b5b4b2a1a4e8d7defd77a95)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Implementation of mac80211 API.
4  *
5  * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6  * Copyright (c) 2010, ST-Ericsson
7  */
8 #include <linux/etherdevice.h>
9 #include <net/mac80211.h>
10 
11 #include "sta.h"
12 #include "wfx.h"
13 #include "bus.h"
14 #include "fwio.h"
15 #include "bh.h"
16 #include "key.h"
17 #include "scan.h"
18 #include "debug.h"
19 #include "hif_tx.h"
20 #include "hif_tx_mib.h"
21 
22 #define HIF_MAX_ARP_IP_ADDRTABLE_ENTRIES 2
23 
wfx_cooling_timeout_work(struct work_struct * work)24 void wfx_cooling_timeout_work(struct work_struct *work)
25 {
26 	struct wfx_dev *wdev = container_of(to_delayed_work(work), struct wfx_dev,
27 					    cooling_timeout_work);
28 
29 	wdev->chip_frozen = true;
30 	wfx_tx_unlock(wdev);
31 }
32 
wfx_suspend_hot_dev(struct wfx_dev * wdev,enum sta_notify_cmd cmd)33 void wfx_suspend_hot_dev(struct wfx_dev *wdev, enum sta_notify_cmd cmd)
34 {
35 	if (cmd == STA_NOTIFY_AWAKE) {
36 		/* Device recover normal temperature */
37 		if (cancel_delayed_work(&wdev->cooling_timeout_work))
38 			wfx_tx_unlock(wdev);
39 	} else {
40 		/* Device is too hot */
41 		schedule_delayed_work(&wdev->cooling_timeout_work, 10 * HZ);
42 		wfx_tx_lock(wdev);
43 	}
44 }
45 
wfx_filter_beacon(struct wfx_vif * wvif,bool filter_beacon)46 static void wfx_filter_beacon(struct wfx_vif *wvif, bool filter_beacon)
47 {
48 	static const struct wfx_hif_ie_table_entry filter_ies[] = {
49 		{
50 			.ie_id        = WLAN_EID_VENDOR_SPECIFIC,
51 			.has_changed  = 1,
52 			.no_longer    = 1,
53 			.has_appeared = 1,
54 			.oui          = { 0x50, 0x6F, 0x9A },
55 		}, {
56 			.ie_id        = WLAN_EID_HT_OPERATION,
57 			.has_changed  = 1,
58 			.no_longer    = 1,
59 			.has_appeared = 1,
60 		}, {
61 			.ie_id        = WLAN_EID_ERP_INFO,
62 			.has_changed  = 1,
63 			.no_longer    = 1,
64 			.has_appeared = 1,
65 		}, {
66 			.ie_id        = WLAN_EID_CHANNEL_SWITCH,
67 			.has_changed  = 1,
68 			.no_longer    = 1,
69 			.has_appeared = 1,
70 		}
71 	};
72 
73 	if (!filter_beacon) {
74 		wfx_hif_beacon_filter_control(wvif, 0, 1);
75 	} else {
76 		wfx_hif_set_beacon_filter_table(wvif, ARRAY_SIZE(filter_ies), filter_ies);
77 		wfx_hif_beacon_filter_control(wvif, HIF_BEACON_FILTER_ENABLE, 0);
78 	}
79 }
80 
wfx_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 unused)81 void wfx_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
82 			  unsigned int *total_flags, u64 unused)
83 {
84 	bool filter_bssid, filter_prbreq, filter_beacon;
85 	struct ieee80211_vif *vif = NULL;
86 	struct wfx_dev *wdev = hw->priv;
87 	struct wfx_vif *wvif = NULL;
88 
89 	/* Notes:
90 	 *   - Probe responses (FIF_BCN_PRBRESP_PROMISC) are never filtered
91 	 *   - PS-Poll (FIF_PSPOLL) are never filtered
92 	 *   - RTS, CTS and Ack (FIF_CONTROL) are always filtered
93 	 *   - Broken frames (FIF_FCSFAIL and FIF_PLCPFAIL) are always filtered
94 	 *   - Firmware does (yet) allow to forward unicast traffic sent to other stations (aka.
95 	 *     promiscuous mode)
96 	 */
97 	*total_flags &= FIF_BCN_PRBRESP_PROMISC | FIF_ALLMULTI | FIF_OTHER_BSS |
98 			FIF_PROBE_REQ | FIF_PSPOLL;
99 
100 	/* Filters are ignored during the scan. No frames are filtered. */
101 	if (mutex_is_locked(&wdev->scan_lock))
102 		return;
103 
104 	mutex_lock(&wdev->conf_mutex);
105 	while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
106 		/* Note: FIF_BCN_PRBRESP_PROMISC covers probe response and
107 		 * beacons from other BSS
108 		 */
109 		if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
110 			filter_beacon = false;
111 		else
112 			filter_beacon = true;
113 		wfx_filter_beacon(wvif, filter_beacon);
114 
115 		if (*total_flags & FIF_OTHER_BSS)
116 			filter_bssid = false;
117 		else
118 			filter_bssid = true;
119 
120 		vif = wvif_to_vif(wvif);
121 		/* In AP mode, chip can reply to probe request itself */
122 		if (*total_flags & FIF_PROBE_REQ && vif->type == NL80211_IFTYPE_AP) {
123 			dev_dbg(wdev->dev, "do not forward probe request in AP mode\n");
124 			*total_flags &= ~FIF_PROBE_REQ;
125 		}
126 
127 		if (*total_flags & FIF_PROBE_REQ)
128 			filter_prbreq = false;
129 		else
130 			filter_prbreq = true;
131 		wfx_hif_set_rx_filter(wvif, filter_bssid, filter_prbreq);
132 	}
133 	mutex_unlock(&wdev->conf_mutex);
134 }
135 
wfx_get_ps_timeout(struct wfx_vif * wvif,bool * enable_ps)136 static int wfx_get_ps_timeout(struct wfx_vif *wvif, bool *enable_ps)
137 {
138 	struct ieee80211_channel *chan0 = NULL, *chan1 = NULL;
139 	struct ieee80211_conf *conf = &wvif->wdev->hw->conf;
140 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
141 
142 	WARN(!vif->cfg.assoc && enable_ps,
143 	     "enable_ps is reliable only if associated");
144 	if (wdev_to_wvif(wvif->wdev, 0)) {
145 		struct wfx_vif *wvif_ch0 = wdev_to_wvif(wvif->wdev, 0);
146 		struct ieee80211_vif *vif_ch0 = wvif_to_vif(wvif_ch0);
147 
148 		chan0 = vif_ch0->bss_conf.chanreq.oper.chan;
149 	}
150 	if (wdev_to_wvif(wvif->wdev, 1)) {
151 		struct wfx_vif *wvif_ch1 = wdev_to_wvif(wvif->wdev, 1);
152 		struct ieee80211_vif *vif_ch1 = wvif_to_vif(wvif_ch1);
153 
154 		chan1 = vif_ch1->bss_conf.chanreq.oper.chan;
155 	}
156 	if (chan0 && chan1 && vif->type != NL80211_IFTYPE_AP) {
157 		if (chan0->hw_value == chan1->hw_value) {
158 			/* It is useless to enable PS if channels are the same. */
159 			if (enable_ps)
160 				*enable_ps = false;
161 			if (vif->cfg.assoc && vif->cfg.ps)
162 				dev_info(wvif->wdev->dev, "ignoring requested PS mode");
163 			return -1;
164 		}
165 		/* It is necessary to enable PS if channels are different. */
166 		if (enable_ps)
167 			*enable_ps = true;
168 		if (wfx_api_older_than(wvif->wdev, 3, 2))
169 			return 0;
170 		else
171 			return 30;
172 	}
173 	if (enable_ps)
174 		*enable_ps = vif->cfg.ps;
175 	if (vif->cfg.assoc && vif->cfg.ps)
176 		return conf->dynamic_ps_timeout;
177 	else
178 		return -1;
179 }
180 
wfx_update_pm(struct wfx_vif * wvif)181 int wfx_update_pm(struct wfx_vif *wvif)
182 {
183 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
184 	int ps_timeout;
185 	bool ps;
186 
187 	if (!vif->cfg.assoc)
188 		return 0;
189 	ps_timeout = wfx_get_ps_timeout(wvif, &ps);
190 	if (!ps)
191 		ps_timeout = 0;
192 	WARN_ON(ps_timeout < 0);
193 	if (wvif->uapsd_mask)
194 		ps_timeout = 0;
195 
196 	if (!wait_for_completion_timeout(&wvif->set_pm_mode_complete, TU_TO_JIFFIES(512)))
197 		dev_warn(wvif->wdev->dev, "timeout while waiting of set_pm_mode_complete\n");
198 	return wfx_hif_set_pm(wvif, ps, ps_timeout);
199 }
200 
wfx_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 queue,const struct ieee80211_tx_queue_params * params)201 int wfx_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
202 		unsigned int link_id, u16 queue,
203 		const struct ieee80211_tx_queue_params *params)
204 {
205 	struct wfx_dev *wdev = hw->priv;
206 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
207 	int old_uapsd = wvif->uapsd_mask;
208 
209 	WARN_ON(queue >= hw->queues);
210 
211 	mutex_lock(&wdev->conf_mutex);
212 	assign_bit(queue, &wvif->uapsd_mask, params->uapsd);
213 	wfx_hif_set_edca_queue_params(wvif, queue, params);
214 	if (vif->type == NL80211_IFTYPE_STATION &&
215 	    old_uapsd != wvif->uapsd_mask) {
216 		wfx_hif_set_uapsd_info(wvif, wvif->uapsd_mask);
217 		wfx_update_pm(wvif);
218 	}
219 	mutex_unlock(&wdev->conf_mutex);
220 	return 0;
221 }
222 
wfx_set_rts_threshold(struct ieee80211_hw * hw,u32 value)223 int wfx_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
224 {
225 	struct wfx_dev *wdev = hw->priv;
226 	struct wfx_vif *wvif = NULL;
227 
228 	while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
229 		wfx_hif_rts_threshold(wvif, value);
230 	return 0;
231 }
232 
wfx_event_report_rssi(struct wfx_vif * wvif,u8 raw_rcpi_rssi)233 void wfx_event_report_rssi(struct wfx_vif *wvif, u8 raw_rcpi_rssi)
234 {
235 	/* RSSI: signed Q8.0, RCPI: unsigned Q7.1
236 	 * RSSI = RCPI / 2 - 110
237 	 */
238 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
239 	int rcpi_rssi;
240 	int cqm_evt;
241 
242 	rcpi_rssi = raw_rcpi_rssi / 2 - 110;
243 	if (rcpi_rssi <= vif->bss_conf.cqm_rssi_thold)
244 		cqm_evt = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
245 	else
246 		cqm_evt = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
247 	ieee80211_cqm_rssi_notify(vif, cqm_evt, rcpi_rssi, GFP_KERNEL);
248 }
249 
wfx_beacon_loss_work(struct work_struct * work)250 static void wfx_beacon_loss_work(struct work_struct *work)
251 {
252 	struct wfx_vif *wvif = container_of(to_delayed_work(work), struct wfx_vif,
253 					    beacon_loss_work);
254 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
255 	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
256 
257 	ieee80211_beacon_loss(vif);
258 	schedule_delayed_work(to_delayed_work(work), msecs_to_jiffies(bss_conf->beacon_int));
259 }
260 
wfx_set_default_unicast_key(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int idx)261 void wfx_set_default_unicast_key(struct ieee80211_hw *hw, struct ieee80211_vif *vif, int idx)
262 {
263 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
264 
265 	wfx_hif_wep_default_key_id(wvif, idx);
266 }
267 
wfx_reset(struct wfx_vif * wvif)268 void wfx_reset(struct wfx_vif *wvif)
269 {
270 	struct wfx_dev *wdev = wvif->wdev;
271 
272 	wfx_tx_lock_flush(wdev);
273 	wfx_hif_reset(wvif, false);
274 	wfx_tx_policy_init(wvif);
275 	if (wvif_count(wdev) <= 1)
276 		wfx_hif_set_block_ack_policy(wvif, 0xFF, 0xFF);
277 	wfx_tx_unlock(wdev);
278 	wvif->join_in_progress = false;
279 	cancel_delayed_work_sync(&wvif->beacon_loss_work);
280 	wvif =  NULL;
281 	while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
282 		wfx_update_pm(wvif);
283 }
284 
wfx_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)285 int wfx_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif, struct ieee80211_sta *sta)
286 {
287 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
288 	struct wfx_sta_priv *sta_priv = (struct wfx_sta_priv *)&sta->drv_priv;
289 
290 	sta_priv->vif_id = wvif->id;
291 
292 	if (vif->type == NL80211_IFTYPE_STATION)
293 		wfx_hif_set_mfp(wvif, sta->mfp, sta->mfp);
294 
295 	/* In station mode, the firmware interprets new link-id as a TDLS peer */
296 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
297 		return 0;
298 	sta_priv->link_id = ffz(wvif->link_id_map);
299 	wvif->link_id_map |= BIT(sta_priv->link_id);
300 	WARN_ON(!sta_priv->link_id);
301 	WARN_ON(sta_priv->link_id >= HIF_LINK_ID_MAX);
302 	wfx_hif_map_link(wvif, false, sta->addr, sta_priv->link_id, sta->mfp);
303 
304 	return 0;
305 }
306 
wfx_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)307 int wfx_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif, struct ieee80211_sta *sta)
308 {
309 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
310 	struct wfx_sta_priv *sta_priv = (struct wfx_sta_priv *)&sta->drv_priv;
311 
312 	/* See note in wfx_sta_add() */
313 	if (!sta_priv->link_id)
314 		return 0;
315 	/* FIXME add a mutex? */
316 	wfx_hif_map_link(wvif, true, sta->addr, sta_priv->link_id, false);
317 	wvif->link_id_map &= ~BIT(sta_priv->link_id);
318 	return 0;
319 }
320 
wfx_upload_ap_templates(struct wfx_vif * wvif)321 static int wfx_upload_ap_templates(struct wfx_vif *wvif)
322 {
323 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
324 	struct sk_buff *skb;
325 
326 	skb = ieee80211_beacon_get(wvif->wdev->hw, vif, 0);
327 	if (!skb)
328 		return -ENOMEM;
329 	wfx_hif_set_template_frame(wvif, skb, HIF_TMPLT_BCN, API_RATE_INDEX_B_1MBPS);
330 	dev_kfree_skb(skb);
331 
332 	skb = ieee80211_proberesp_get(wvif->wdev->hw, vif);
333 	if (!skb)
334 		return -ENOMEM;
335 	wfx_hif_set_template_frame(wvif, skb, HIF_TMPLT_PRBRES, API_RATE_INDEX_B_1MBPS);
336 	dev_kfree_skb(skb);
337 	return 0;
338 }
339 
wfx_set_mfp_ap(struct wfx_vif * wvif)340 static int wfx_set_mfp_ap(struct wfx_vif *wvif)
341 {
342 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
343 	struct sk_buff *skb = ieee80211_beacon_get(wvif->wdev->hw, vif, 0);
344 	const int ieoffset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
345 	const int pairwise_cipher_suite_count_offset = 8 / sizeof(u16);
346 	const int pairwise_cipher_suite_size = 4 / sizeof(u16);
347 	const int akm_suite_size = 4 / sizeof(u16);
348 	int ret = -EINVAL;
349 	const u16 *ptr;
350 
351 	if (unlikely(!skb))
352 		return -ENOMEM;
353 
354 	ptr = (u16 *)cfg80211_find_ie(WLAN_EID_RSN, skb->data + ieoffset,
355 				      skb->len - ieoffset);
356 	if (!ptr) {
357 		/* No RSN IE is fine in open networks */
358 		ret = 0;
359 		goto free_skb;
360 	}
361 
362 	ptr += pairwise_cipher_suite_count_offset;
363 	if (WARN_ON(ptr > (u16 *)skb_tail_pointer(skb)))
364 		goto free_skb;
365 
366 	ptr += 1 + pairwise_cipher_suite_size * *ptr;
367 	if (WARN_ON(ptr > (u16 *)skb_tail_pointer(skb)))
368 		goto free_skb;
369 
370 	ptr += 1 + akm_suite_size * *ptr;
371 	if (WARN_ON(ptr > (u16 *)skb_tail_pointer(skb)))
372 		goto free_skb;
373 
374 	wfx_hif_set_mfp(wvif, *ptr & BIT(7), *ptr & BIT(6));
375 	ret = 0;
376 
377 free_skb:
378 	dev_kfree_skb(skb);
379 	return ret;
380 }
381 
wfx_start_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)382 int wfx_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
383 		 struct ieee80211_bss_conf *link_conf)
384 {
385 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
386 	struct wfx_dev *wdev = wvif->wdev;
387 	int ret;
388 
389 	wvif =  NULL;
390 	while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
391 		wfx_update_pm(wvif);
392 	wvif = (struct wfx_vif *)vif->drv_priv;
393 	wfx_upload_ap_templates(wvif);
394 	ret = wfx_hif_start(wvif, &vif->bss_conf, wvif->channel);
395 	if (ret > 0)
396 		return -EIO;
397 	return wfx_set_mfp_ap(wvif);
398 }
399 
wfx_stop_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)400 void wfx_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
401 		 struct ieee80211_bss_conf *link_conf)
402 {
403 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
404 	struct wfx_dev *wdev = wvif->wdev;
405 
406 	wvif =  NULL;
407 	while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
408 		wfx_update_pm(wvif);
409 	wvif = (struct wfx_vif *)vif->drv_priv;
410 	wfx_reset(wvif);
411 }
412 
wfx_join(struct wfx_vif * wvif)413 static void wfx_join(struct wfx_vif *wvif)
414 {
415 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
416 	struct ieee80211_bss_conf *conf = &vif->bss_conf;
417 	struct cfg80211_bss *bss = NULL;
418 	u8 ssid[IEEE80211_MAX_SSID_LEN];
419 	const u8 *ssid_ie = NULL;
420 	int ssid_len = 0;
421 	int ret;
422 
423 	wfx_tx_lock_flush(wvif->wdev);
424 
425 	bss = cfg80211_get_bss(wvif->wdev->hw->wiphy, wvif->channel, conf->bssid, NULL, 0,
426 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
427 	if (!bss && !vif->cfg.ibss_joined) {
428 		wfx_tx_unlock(wvif->wdev);
429 		return;
430 	}
431 
432 	rcu_read_lock(); /* protect ssid_ie */
433 	if (bss)
434 		ssid_ie = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
435 	if (ssid_ie) {
436 		ssid_len = ssid_ie[1];
437 		if (ssid_len > IEEE80211_MAX_SSID_LEN)
438 			ssid_len = IEEE80211_MAX_SSID_LEN;
439 		memcpy(ssid, &ssid_ie[2], ssid_len);
440 	}
441 	rcu_read_unlock();
442 
443 	cfg80211_put_bss(wvif->wdev->hw->wiphy, bss);
444 
445 	wvif->join_in_progress = true;
446 	ret = wfx_hif_join(wvif, conf, wvif->channel, ssid, ssid_len);
447 	if (ret) {
448 		ieee80211_connection_loss(vif);
449 		wfx_reset(wvif);
450 	} else {
451 		/* Due to beacon filtering it is possible that the AP's beacon is not known for the
452 		 * mac80211 stack.  Disable filtering temporary to make sure the stack receives at
453 		 * least one
454 		 */
455 		wfx_filter_beacon(wvif, false);
456 	}
457 	wfx_tx_unlock(wvif->wdev);
458 }
459 
wfx_join_finalize(struct wfx_vif * wvif,struct ieee80211_bss_conf * info)460 static void wfx_join_finalize(struct wfx_vif *wvif, struct ieee80211_bss_conf *info)
461 {
462 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
463 	struct ieee80211_sta *sta = NULL;
464 	int ampdu_density = 0;
465 	bool greenfield = false;
466 
467 	rcu_read_lock(); /* protect sta */
468 	if (info->bssid && !vif->cfg.ibss_joined)
469 		sta = ieee80211_find_sta(vif, info->bssid);
470 	if (sta && sta->deflink.ht_cap.ht_supported)
471 		ampdu_density = sta->deflink.ht_cap.ampdu_density;
472 	if (sta && sta->deflink.ht_cap.ht_supported &&
473 	    !(info->ht_operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT))
474 		greenfield = !!(sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD);
475 	rcu_read_unlock();
476 
477 	wvif->join_in_progress = false;
478 	wfx_hif_set_association_mode(wvif, ampdu_density, greenfield, info->use_short_preamble);
479 	wfx_hif_keep_alive_period(wvif, 0);
480 	/* beacon_loss_count is defined to 7 in net/mac80211/mlme.c. Let's use the same value. */
481 	wfx_hif_set_bss_params(wvif, vif->cfg.aid, 7);
482 	wfx_hif_set_beacon_wakeup_period(wvif, 1, 1);
483 	wfx_update_pm(wvif);
484 }
485 
wfx_join_ibss(struct ieee80211_hw * hw,struct ieee80211_vif * vif)486 int wfx_join_ibss(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
487 {
488 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
489 
490 	wfx_upload_ap_templates(wvif);
491 	wfx_join(wvif);
492 	return 0;
493 }
494 
wfx_leave_ibss(struct ieee80211_hw * hw,struct ieee80211_vif * vif)495 void wfx_leave_ibss(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
496 {
497 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
498 
499 	wfx_reset(wvif);
500 }
501 
wfx_enable_beacon(struct wfx_vif * wvif,bool enable)502 static void wfx_enable_beacon(struct wfx_vif *wvif, bool enable)
503 {
504 	/* Driver has Content After DTIM Beacon in queue. Driver is waiting for a signal from the
505 	 * firmware. Since we are going to stop to send beacons, this signal will never happens. See
506 	 * also wfx_suspend_resume_mc()
507 	 */
508 	if (!enable && wfx_tx_queues_has_cab(wvif)) {
509 		wvif->after_dtim_tx_allowed = true;
510 		wfx_bh_request_tx(wvif->wdev);
511 	}
512 	wfx_hif_beacon_transmit(wvif, enable);
513 }
514 
wfx_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)515 void wfx_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
516 			  struct ieee80211_bss_conf *info, u64 changed)
517 {
518 	struct wfx_dev *wdev = hw->priv;
519 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
520 	int i;
521 
522 	mutex_lock(&wdev->conf_mutex);
523 
524 	if (changed & BSS_CHANGED_BASIC_RATES ||
525 	    changed & BSS_CHANGED_BEACON_INT ||
526 	    changed & BSS_CHANGED_BSSID) {
527 		if (vif->type == NL80211_IFTYPE_STATION)
528 			wfx_join(wvif);
529 	}
530 
531 	if (changed & BSS_CHANGED_ASSOC) {
532 		if (vif->cfg.assoc || vif->cfg.ibss_joined)
533 			wfx_join_finalize(wvif, info);
534 		else if (!vif->cfg.assoc && vif->type == NL80211_IFTYPE_STATION)
535 			wfx_reset(wvif);
536 		else
537 			dev_warn(wdev->dev, "misunderstood change: ASSOC\n");
538 	}
539 
540 	if (changed & BSS_CHANGED_BEACON_INFO) {
541 		if (vif->type != NL80211_IFTYPE_STATION)
542 			dev_warn(wdev->dev, "misunderstood change: BEACON_INFO\n");
543 		wfx_hif_set_beacon_wakeup_period(wvif, info->dtim_period, info->dtim_period);
544 		/* We temporary forwarded beacon for join process. It is now no more necessary. */
545 		wfx_filter_beacon(wvif, true);
546 	}
547 
548 	if (changed & BSS_CHANGED_ARP_FILTER) {
549 		for (i = 0; i < HIF_MAX_ARP_IP_ADDRTABLE_ENTRIES; i++) {
550 			__be32 *arp_addr = &vif->cfg.arp_addr_list[i];
551 
552 			if (vif->cfg.arp_addr_cnt > HIF_MAX_ARP_IP_ADDRTABLE_ENTRIES)
553 				arp_addr = NULL;
554 			if (i >= vif->cfg.arp_addr_cnt)
555 				arp_addr = NULL;
556 			wfx_hif_set_arp_ipv4_filter(wvif, i, arp_addr);
557 		}
558 	}
559 
560 	if (changed & BSS_CHANGED_AP_PROBE_RESP || changed & BSS_CHANGED_BEACON)
561 		wfx_upload_ap_templates(wvif);
562 
563 	if (changed & BSS_CHANGED_BEACON_ENABLED)
564 		wfx_enable_beacon(wvif, info->enable_beacon);
565 
566 	if (changed & BSS_CHANGED_KEEP_ALIVE)
567 		wfx_hif_keep_alive_period(wvif,
568 					  info->max_idle_period * USEC_PER_TU / USEC_PER_MSEC);
569 
570 	if (changed & BSS_CHANGED_ERP_CTS_PROT)
571 		wfx_hif_erp_use_protection(wvif, info->use_cts_prot);
572 
573 	if (changed & BSS_CHANGED_ERP_SLOT)
574 		wfx_hif_slot_time(wvif, info->use_short_slot ? 9 : 20);
575 
576 	if (changed & BSS_CHANGED_CQM)
577 		wfx_hif_set_rcpi_rssi_threshold(wvif, info->cqm_rssi_thold, info->cqm_rssi_hyst);
578 
579 	if (changed & BSS_CHANGED_TXPOWER)
580 		wfx_hif_set_output_power(wvif, info->txpower);
581 
582 	if (changed & BSS_CHANGED_PS)
583 		wfx_update_pm(wvif);
584 
585 	mutex_unlock(&wdev->conf_mutex);
586 }
587 
wfx_update_tim(struct wfx_vif * wvif)588 static int wfx_update_tim(struct wfx_vif *wvif)
589 {
590 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
591 	struct sk_buff *skb;
592 	u16 tim_offset, tim_length;
593 	u8 *tim_ptr;
594 
595 	skb = ieee80211_beacon_get_tim(wvif->wdev->hw, vif, &tim_offset,
596 				       &tim_length, 0);
597 	if (!skb)
598 		return -ENOENT;
599 	tim_ptr = skb->data + tim_offset;
600 
601 	if (tim_offset && tim_length >= 6) {
602 		/* Firmware handles DTIM counter internally */
603 		tim_ptr[2] = 0;
604 
605 		/* Set/reset aid0 bit */
606 		if (wfx_tx_queues_has_cab(wvif))
607 			tim_ptr[4] |= 1;
608 		else
609 			tim_ptr[4] &= ~1;
610 	}
611 
612 	wfx_hif_update_ie_beacon(wvif, tim_ptr, tim_length);
613 	dev_kfree_skb(skb);
614 
615 	return 0;
616 }
617 
wfx_update_tim_work(struct work_struct * work)618 static void wfx_update_tim_work(struct work_struct *work)
619 {
620 	struct wfx_vif *wvif = container_of(work, struct wfx_vif, update_tim_work);
621 
622 	wfx_update_tim(wvif);
623 }
624 
wfx_set_tim(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool set)625 int wfx_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
626 {
627 	struct wfx_dev *wdev = hw->priv;
628 	struct wfx_sta_priv *sta_dev = (struct wfx_sta_priv *)&sta->drv_priv;
629 	struct wfx_vif *wvif = wdev_to_wvif(wdev, sta_dev->vif_id);
630 
631 	if (!wvif) {
632 		dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
633 		return -EIO;
634 	}
635 	schedule_work(&wvif->update_tim_work);
636 	return 0;
637 }
638 
wfx_suspend_resume_mc(struct wfx_vif * wvif,enum sta_notify_cmd notify_cmd)639 void wfx_suspend_resume_mc(struct wfx_vif *wvif, enum sta_notify_cmd notify_cmd)
640 {
641 	if (notify_cmd != STA_NOTIFY_AWAKE)
642 		return;
643 
644 	/* Device won't be able to honor CAB if a scan is in progress on any interface. Prefer to
645 	 * skip this DTIM and wait for the next one.
646 	 */
647 	if (mutex_is_locked(&wvif->wdev->scan_lock))
648 		return;
649 
650 	if (!wfx_tx_queues_has_cab(wvif) || wvif->after_dtim_tx_allowed)
651 		dev_warn(wvif->wdev->dev, "incorrect sequence (%d CAB in queue)",
652 			 wfx_tx_queues_has_cab(wvif));
653 	wvif->after_dtim_tx_allowed = true;
654 	wfx_bh_request_tx(wvif->wdev);
655 }
656 
wfx_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)657 int wfx_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
658 		     struct ieee80211_ampdu_params *params)
659 {
660 	/* Aggregation is implemented fully in firmware */
661 	switch (params->action) {
662 	case IEEE80211_AMPDU_RX_START:
663 	case IEEE80211_AMPDU_RX_STOP:
664 		/* Just acknowledge it to enable frame re-ordering */
665 		return 0;
666 	default:
667 		/* Leave the firmware doing its business for tx aggregation */
668 		return -EOPNOTSUPP;
669 	}
670 }
671 
wfx_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf)672 int wfx_add_chanctx(struct ieee80211_hw *hw, struct ieee80211_chanctx_conf *conf)
673 {
674 	return 0;
675 }
676 
wfx_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf)677 void wfx_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_chanctx_conf *conf)
678 {
679 }
680 
wfx_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf,u32 changed)681 void wfx_change_chanctx(struct ieee80211_hw *hw, struct ieee80211_chanctx_conf *conf, u32 changed)
682 {
683 }
684 
wfx_assign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * conf)685 int wfx_assign_vif_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
686 			   struct ieee80211_bss_conf *link_conf,
687 			   struct ieee80211_chanctx_conf *conf)
688 {
689 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
690 	struct ieee80211_channel *ch = conf->def.chan;
691 
692 	WARN(wvif->channel, "channel overwrite");
693 	wvif->channel = ch;
694 
695 	return 0;
696 }
697 
wfx_unassign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * conf)698 void wfx_unassign_vif_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
699 			      struct ieee80211_bss_conf *link_conf,
700 			      struct ieee80211_chanctx_conf *conf)
701 {
702 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
703 	struct ieee80211_channel *ch = conf->def.chan;
704 
705 	WARN(wvif->channel != ch, "channel mismatch");
706 	wvif->channel = NULL;
707 }
708 
wfx_config(struct ieee80211_hw * hw,u32 changed)709 int wfx_config(struct ieee80211_hw *hw, u32 changed)
710 {
711 	return 0;
712 }
713 
wfx_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)714 int wfx_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
715 {
716 	int i;
717 	struct wfx_dev *wdev = hw->priv;
718 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
719 
720 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
721 			     IEEE80211_VIF_SUPPORTS_UAPSD |
722 			     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
723 
724 	mutex_lock(&wdev->conf_mutex);
725 
726 	switch (vif->type) {
727 	case NL80211_IFTYPE_STATION:
728 	case NL80211_IFTYPE_ADHOC:
729 	case NL80211_IFTYPE_AP:
730 		break;
731 	default:
732 		mutex_unlock(&wdev->conf_mutex);
733 		return -EOPNOTSUPP;
734 	}
735 
736 	wvif->wdev = wdev;
737 
738 	wvif->link_id_map = 1; /* link-id 0 is reserved for multicast */
739 	INIT_WORK(&wvif->update_tim_work, wfx_update_tim_work);
740 	INIT_DELAYED_WORK(&wvif->beacon_loss_work, wfx_beacon_loss_work);
741 
742 	init_completion(&wvif->set_pm_mode_complete);
743 	complete(&wvif->set_pm_mode_complete);
744 	INIT_WORK(&wvif->tx_policy_upload_work, wfx_tx_policy_upload_work);
745 
746 	init_completion(&wvif->scan_complete);
747 	INIT_WORK(&wvif->scan_work, wfx_hw_scan_work);
748 	INIT_WORK(&wvif->remain_on_channel_work, wfx_remain_on_channel_work);
749 
750 	wfx_tx_queues_init(wvif);
751 	wfx_tx_policy_init(wvif);
752 
753 	for (i = 0; i < ARRAY_SIZE(wdev->vif); i++) {
754 		if (!wdev->vif[i]) {
755 			wdev->vif[i] = vif;
756 			wvif->id = i;
757 			break;
758 		}
759 	}
760 	WARN(i == ARRAY_SIZE(wdev->vif), "try to instantiate more vif than supported");
761 
762 	wfx_hif_set_macaddr(wvif, vif->addr);
763 
764 	mutex_unlock(&wdev->conf_mutex);
765 
766 	wvif = NULL;
767 	while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
768 		/* Combo mode does not support Block Acks. We can re-enable them */
769 		if (wvif_count(wdev) == 1)
770 			wfx_hif_set_block_ack_policy(wvif, 0xFF, 0xFF);
771 		else
772 			wfx_hif_set_block_ack_policy(wvif, 0x00, 0x00);
773 	}
774 	return 0;
775 }
776 
wfx_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)777 void wfx_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
778 {
779 	struct wfx_dev *wdev = hw->priv;
780 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
781 
782 	wait_for_completion_timeout(&wvif->set_pm_mode_complete, msecs_to_jiffies(300));
783 	wfx_tx_queues_check_empty(wvif);
784 
785 	mutex_lock(&wdev->conf_mutex);
786 	WARN(wvif->link_id_map != 1, "corrupted state");
787 
788 	wfx_hif_reset(wvif, false);
789 	wfx_hif_set_macaddr(wvif, NULL);
790 	wfx_tx_policy_init(wvif);
791 
792 	cancel_delayed_work_sync(&wvif->beacon_loss_work);
793 	wdev->vif[wvif->id] = NULL;
794 
795 	mutex_unlock(&wdev->conf_mutex);
796 
797 	wvif = NULL;
798 	while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
799 		/* Combo mode does not support Block Acks. We can re-enable them */
800 		if (wvif_count(wdev) == 1)
801 			wfx_hif_set_block_ack_policy(wvif, 0xFF, 0xFF);
802 		else
803 			wfx_hif_set_block_ack_policy(wvif, 0x00, 0x00);
804 	}
805 }
806 
807 #ifdef CONFIG_PM
wfx_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)808 int wfx_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
809 {
810 	/* FIXME: hardware also support WIPHY_WOWLAN_MAGIC_PKT and other filters */
811 	if (!wowlan->any || !wowlan->disconnect)
812 		return -EINVAL;
813 	return 0;
814 }
815 
wfx_resume(struct ieee80211_hw * hw)816 int wfx_resume(struct ieee80211_hw *hw)
817 {
818 	return 0;
819 }
820 
wfx_set_wakeup(struct ieee80211_hw * hw,bool enabled)821 void wfx_set_wakeup(struct ieee80211_hw *hw, bool enabled)
822 {
823 	struct wfx_dev *wdev = hw->priv;
824 
825 	if (enabled)
826 		dev_info(wdev->dev, "support for WoWLAN is experimental\n");
827 	wdev->hwbus_ops->set_wakeup(wdev->hwbus_priv, enabled);
828 }
829 #endif
830 
wfx_start(struct ieee80211_hw * hw)831 int wfx_start(struct ieee80211_hw *hw)
832 {
833 	return 0;
834 }
835 
wfx_stop(struct ieee80211_hw * hw,bool suspend)836 void wfx_stop(struct ieee80211_hw *hw, bool suspend)
837 {
838 	struct wfx_dev *wdev = hw->priv;
839 
840 	WARN_ON(!skb_queue_empty_lockless(&wdev->tx_pending));
841 }
842