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