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