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
23void 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
32void 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
45static 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
80void 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
135static 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.chanreq.oper.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.chanreq.oper.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
180int 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
200int 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
222int 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
232void 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
249static 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
260void 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
267void 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
284int 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
306int 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
320static 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
339static int 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 int pairwise_cipher_suite_count_offset = 8 / sizeof(u16);
345	const int pairwise_cipher_suite_size = 4 / sizeof(u16);
346	const int akm_suite_size = 4 / sizeof(u16);
347	int ret = -EINVAL;
348	const u16 *ptr;
349
350	if (unlikely(!skb))
351		return -ENOMEM;
352
353	ptr = (u16 *)cfg80211_find_ie(WLAN_EID_RSN, skb->data + ieoffset,
354				      skb->len - ieoffset);
355	if (unlikely(!ptr))
356		goto free_skb;
357
358	ptr += pairwise_cipher_suite_count_offset;
359	if (WARN_ON(ptr > (u16 *)skb_tail_pointer(skb)))
360		goto free_skb;
361
362	ptr += 1 + pairwise_cipher_suite_size * *ptr;
363	if (WARN_ON(ptr > (u16 *)skb_tail_pointer(skb)))
364		goto free_skb;
365
366	ptr += 1 + akm_suite_size * *ptr;
367	if (WARN_ON(ptr > (u16 *)skb_tail_pointer(skb)))
368		goto free_skb;
369
370	wfx_hif_set_mfp(wvif, *ptr & BIT(7), *ptr & BIT(6));
371	ret = 0;
372
373free_skb:
374	dev_kfree_skb(skb);
375	return ret;
376}
377
378int wfx_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
379		 struct ieee80211_bss_conf *link_conf)
380{
381	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
382	struct wfx_dev *wdev = wvif->wdev;
383	int ret;
384
385	wvif =  NULL;
386	while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
387		wfx_update_pm(wvif);
388	wvif = (struct wfx_vif *)vif->drv_priv;
389	wfx_upload_ap_templates(wvif);
390	ret = wfx_hif_start(wvif, &vif->bss_conf, wvif->channel);
391	if (ret > 0)
392		return -EIO;
393	return wfx_set_mfp_ap(wvif);
394}
395
396void wfx_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
397		 struct ieee80211_bss_conf *link_conf)
398{
399	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
400	struct wfx_dev *wdev = wvif->wdev;
401
402	wvif =  NULL;
403	while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
404		wfx_update_pm(wvif);
405	wvif = (struct wfx_vif *)vif->drv_priv;
406	wfx_reset(wvif);
407}
408
409static void wfx_join(struct wfx_vif *wvif)
410{
411	struct ieee80211_vif *vif = wvif_to_vif(wvif);
412	struct ieee80211_bss_conf *conf = &vif->bss_conf;
413	struct cfg80211_bss *bss = NULL;
414	u8 ssid[IEEE80211_MAX_SSID_LEN];
415	const u8 *ssid_ie = NULL;
416	int ssid_len = 0;
417	int ret;
418
419	wfx_tx_lock_flush(wvif->wdev);
420
421	bss = cfg80211_get_bss(wvif->wdev->hw->wiphy, wvif->channel, conf->bssid, NULL, 0,
422			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
423	if (!bss && !vif->cfg.ibss_joined) {
424		wfx_tx_unlock(wvif->wdev);
425		return;
426	}
427
428	rcu_read_lock(); /* protect ssid_ie */
429	if (bss)
430		ssid_ie = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
431	if (ssid_ie) {
432		ssid_len = ssid_ie[1];
433		if (ssid_len > IEEE80211_MAX_SSID_LEN)
434			ssid_len = IEEE80211_MAX_SSID_LEN;
435		memcpy(ssid, &ssid_ie[2], ssid_len);
436	}
437	rcu_read_unlock();
438
439	cfg80211_put_bss(wvif->wdev->hw->wiphy, bss);
440
441	wvif->join_in_progress = true;
442	ret = wfx_hif_join(wvif, conf, wvif->channel, ssid, ssid_len);
443	if (ret) {
444		ieee80211_connection_loss(vif);
445		wfx_reset(wvif);
446	} else {
447		/* Due to beacon filtering it is possible that the AP's beacon is not known for the
448		 * mac80211 stack.  Disable filtering temporary to make sure the stack receives at
449		 * least one
450		 */
451		wfx_filter_beacon(wvif, false);
452	}
453	wfx_tx_unlock(wvif->wdev);
454}
455
456static void wfx_join_finalize(struct wfx_vif *wvif, struct ieee80211_bss_conf *info)
457{
458	struct ieee80211_vif *vif = wvif_to_vif(wvif);
459	struct ieee80211_sta *sta = NULL;
460	int ampdu_density = 0;
461	bool greenfield = false;
462
463	rcu_read_lock(); /* protect sta */
464	if (info->bssid && !vif->cfg.ibss_joined)
465		sta = ieee80211_find_sta(vif, info->bssid);
466	if (sta && sta->deflink.ht_cap.ht_supported)
467		ampdu_density = sta->deflink.ht_cap.ampdu_density;
468	if (sta && sta->deflink.ht_cap.ht_supported &&
469	    !(info->ht_operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT))
470		greenfield = !!(sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD);
471	rcu_read_unlock();
472
473	wvif->join_in_progress = false;
474	wfx_hif_set_association_mode(wvif, ampdu_density, greenfield, info->use_short_preamble);
475	wfx_hif_keep_alive_period(wvif, 0);
476	/* beacon_loss_count is defined to 7 in net/mac80211/mlme.c. Let's use the same value. */
477	wfx_hif_set_bss_params(wvif, vif->cfg.aid, 7);
478	wfx_hif_set_beacon_wakeup_period(wvif, 1, 1);
479	wfx_update_pm(wvif);
480}
481
482int wfx_join_ibss(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
483{
484	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
485
486	wfx_upload_ap_templates(wvif);
487	wfx_join(wvif);
488	return 0;
489}
490
491void wfx_leave_ibss(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
492{
493	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
494
495	wfx_reset(wvif);
496}
497
498static void wfx_enable_beacon(struct wfx_vif *wvif, bool enable)
499{
500	/* Driver has Content After DTIM Beacon in queue. Driver is waiting for a signal from the
501	 * firmware. Since we are going to stop to send beacons, this signal will never happens. See
502	 * also wfx_suspend_resume_mc()
503	 */
504	if (!enable && wfx_tx_queues_has_cab(wvif)) {
505		wvif->after_dtim_tx_allowed = true;
506		wfx_bh_request_tx(wvif->wdev);
507	}
508	wfx_hif_beacon_transmit(wvif, enable);
509}
510
511void wfx_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
512			  struct ieee80211_bss_conf *info, u64 changed)
513{
514	struct wfx_dev *wdev = hw->priv;
515	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
516	int i;
517
518	mutex_lock(&wdev->conf_mutex);
519
520	if (changed & BSS_CHANGED_BASIC_RATES ||
521	    changed & BSS_CHANGED_BEACON_INT ||
522	    changed & BSS_CHANGED_BSSID) {
523		if (vif->type == NL80211_IFTYPE_STATION)
524			wfx_join(wvif);
525	}
526
527	if (changed & BSS_CHANGED_ASSOC) {
528		if (vif->cfg.assoc || vif->cfg.ibss_joined)
529			wfx_join_finalize(wvif, info);
530		else if (!vif->cfg.assoc && vif->type == NL80211_IFTYPE_STATION)
531			wfx_reset(wvif);
532		else
533			dev_warn(wdev->dev, "misunderstood change: ASSOC\n");
534	}
535
536	if (changed & BSS_CHANGED_BEACON_INFO) {
537		if (vif->type != NL80211_IFTYPE_STATION)
538			dev_warn(wdev->dev, "misunderstood change: BEACON_INFO\n");
539		wfx_hif_set_beacon_wakeup_period(wvif, info->dtim_period, info->dtim_period);
540		/* We temporary forwarded beacon for join process. It is now no more necessary. */
541		wfx_filter_beacon(wvif, true);
542	}
543
544	if (changed & BSS_CHANGED_ARP_FILTER) {
545		for (i = 0; i < HIF_MAX_ARP_IP_ADDRTABLE_ENTRIES; i++) {
546			__be32 *arp_addr = &vif->cfg.arp_addr_list[i];
547
548			if (vif->cfg.arp_addr_cnt > HIF_MAX_ARP_IP_ADDRTABLE_ENTRIES)
549				arp_addr = NULL;
550			if (i >= vif->cfg.arp_addr_cnt)
551				arp_addr = NULL;
552			wfx_hif_set_arp_ipv4_filter(wvif, i, arp_addr);
553		}
554	}
555
556	if (changed & BSS_CHANGED_AP_PROBE_RESP || changed & BSS_CHANGED_BEACON)
557		wfx_upload_ap_templates(wvif);
558
559	if (changed & BSS_CHANGED_BEACON_ENABLED)
560		wfx_enable_beacon(wvif, info->enable_beacon);
561
562	if (changed & BSS_CHANGED_KEEP_ALIVE)
563		wfx_hif_keep_alive_period(wvif,
564					  info->max_idle_period * USEC_PER_TU / USEC_PER_MSEC);
565
566	if (changed & BSS_CHANGED_ERP_CTS_PROT)
567		wfx_hif_erp_use_protection(wvif, info->use_cts_prot);
568
569	if (changed & BSS_CHANGED_ERP_SLOT)
570		wfx_hif_slot_time(wvif, info->use_short_slot ? 9 : 20);
571
572	if (changed & BSS_CHANGED_CQM)
573		wfx_hif_set_rcpi_rssi_threshold(wvif, info->cqm_rssi_thold, info->cqm_rssi_hyst);
574
575	if (changed & BSS_CHANGED_TXPOWER)
576		wfx_hif_set_output_power(wvif, info->txpower);
577
578	if (changed & BSS_CHANGED_PS)
579		wfx_update_pm(wvif);
580
581	mutex_unlock(&wdev->conf_mutex);
582}
583
584static int wfx_update_tim(struct wfx_vif *wvif)
585{
586	struct ieee80211_vif *vif = wvif_to_vif(wvif);
587	struct sk_buff *skb;
588	u16 tim_offset, tim_length;
589	u8 *tim_ptr;
590
591	skb = ieee80211_beacon_get_tim(wvif->wdev->hw, vif, &tim_offset,
592				       &tim_length, 0);
593	if (!skb)
594		return -ENOENT;
595	tim_ptr = skb->data + tim_offset;
596
597	if (tim_offset && tim_length >= 6) {
598		/* Firmware handles DTIM counter internally */
599		tim_ptr[2] = 0;
600
601		/* Set/reset aid0 bit */
602		if (wfx_tx_queues_has_cab(wvif))
603			tim_ptr[4] |= 1;
604		else
605			tim_ptr[4] &= ~1;
606	}
607
608	wfx_hif_update_ie_beacon(wvif, tim_ptr, tim_length);
609	dev_kfree_skb(skb);
610
611	return 0;
612}
613
614static void wfx_update_tim_work(struct work_struct *work)
615{
616	struct wfx_vif *wvif = container_of(work, struct wfx_vif, update_tim_work);
617
618	wfx_update_tim(wvif);
619}
620
621int wfx_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
622{
623	struct wfx_dev *wdev = hw->priv;
624	struct wfx_sta_priv *sta_dev = (struct wfx_sta_priv *)&sta->drv_priv;
625	struct wfx_vif *wvif = wdev_to_wvif(wdev, sta_dev->vif_id);
626
627	if (!wvif) {
628		dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
629		return -EIO;
630	}
631	schedule_work(&wvif->update_tim_work);
632	return 0;
633}
634
635void wfx_suspend_resume_mc(struct wfx_vif *wvif, enum sta_notify_cmd notify_cmd)
636{
637	if (notify_cmd != STA_NOTIFY_AWAKE)
638		return;
639
640	/* Device won't be able to honor CAB if a scan is in progress on any interface. Prefer to
641	 * skip this DTIM and wait for the next one.
642	 */
643	if (mutex_is_locked(&wvif->wdev->scan_lock))
644		return;
645
646	if (!wfx_tx_queues_has_cab(wvif) || wvif->after_dtim_tx_allowed)
647		dev_warn(wvif->wdev->dev, "incorrect sequence (%d CAB in queue)",
648			 wfx_tx_queues_has_cab(wvif));
649	wvif->after_dtim_tx_allowed = true;
650	wfx_bh_request_tx(wvif->wdev);
651}
652
653int wfx_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
654		     struct ieee80211_ampdu_params *params)
655{
656	/* Aggregation is implemented fully in firmware */
657	switch (params->action) {
658	case IEEE80211_AMPDU_RX_START:
659	case IEEE80211_AMPDU_RX_STOP:
660		/* Just acknowledge it to enable frame re-ordering */
661		return 0;
662	default:
663		/* Leave the firmware doing its business for tx aggregation */
664		return -EOPNOTSUPP;
665	}
666}
667
668int wfx_add_chanctx(struct ieee80211_hw *hw, struct ieee80211_chanctx_conf *conf)
669{
670	return 0;
671}
672
673void wfx_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_chanctx_conf *conf)
674{
675}
676
677void wfx_change_chanctx(struct ieee80211_hw *hw, struct ieee80211_chanctx_conf *conf, u32 changed)
678{
679}
680
681int wfx_assign_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, "channel overwrite");
689	wvif->channel = ch;
690
691	return 0;
692}
693
694void wfx_unassign_vif_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
695			      struct ieee80211_bss_conf *link_conf,
696			      struct ieee80211_chanctx_conf *conf)
697{
698	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
699	struct ieee80211_channel *ch = conf->def.chan;
700
701	WARN(wvif->channel != ch, "channel mismatch");
702	wvif->channel = NULL;
703}
704
705int wfx_config(struct ieee80211_hw *hw, u32 changed)
706{
707	return 0;
708}
709
710int wfx_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
711{
712	int i;
713	struct wfx_dev *wdev = hw->priv;
714	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
715
716	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
717			     IEEE80211_VIF_SUPPORTS_UAPSD |
718			     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
719
720	mutex_lock(&wdev->conf_mutex);
721
722	switch (vif->type) {
723	case NL80211_IFTYPE_STATION:
724	case NL80211_IFTYPE_ADHOC:
725	case NL80211_IFTYPE_AP:
726		break;
727	default:
728		mutex_unlock(&wdev->conf_mutex);
729		return -EOPNOTSUPP;
730	}
731
732	wvif->wdev = wdev;
733
734	wvif->link_id_map = 1; /* link-id 0 is reserved for multicast */
735	INIT_WORK(&wvif->update_tim_work, wfx_update_tim_work);
736	INIT_DELAYED_WORK(&wvif->beacon_loss_work, wfx_beacon_loss_work);
737
738	init_completion(&wvif->set_pm_mode_complete);
739	complete(&wvif->set_pm_mode_complete);
740	INIT_WORK(&wvif->tx_policy_upload_work, wfx_tx_policy_upload_work);
741
742	init_completion(&wvif->scan_complete);
743	INIT_WORK(&wvif->scan_work, wfx_hw_scan_work);
744	INIT_WORK(&wvif->remain_on_channel_work, wfx_remain_on_channel_work);
745
746	wfx_tx_queues_init(wvif);
747	wfx_tx_policy_init(wvif);
748
749	for (i = 0; i < ARRAY_SIZE(wdev->vif); i++) {
750		if (!wdev->vif[i]) {
751			wdev->vif[i] = vif;
752			wvif->id = i;
753			break;
754		}
755	}
756	WARN(i == ARRAY_SIZE(wdev->vif), "try to instantiate more vif than supported");
757
758	wfx_hif_set_macaddr(wvif, vif->addr);
759
760	mutex_unlock(&wdev->conf_mutex);
761
762	wvif = NULL;
763	while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
764		/* Combo mode does not support Block Acks. We can re-enable them */
765		if (wvif_count(wdev) == 1)
766			wfx_hif_set_block_ack_policy(wvif, 0xFF, 0xFF);
767		else
768			wfx_hif_set_block_ack_policy(wvif, 0x00, 0x00);
769	}
770	return 0;
771}
772
773void wfx_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
774{
775	struct wfx_dev *wdev = hw->priv;
776	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
777
778	wait_for_completion_timeout(&wvif->set_pm_mode_complete, msecs_to_jiffies(300));
779	wfx_tx_queues_check_empty(wvif);
780
781	mutex_lock(&wdev->conf_mutex);
782	WARN(wvif->link_id_map != 1, "corrupted state");
783
784	wfx_hif_reset(wvif, false);
785	wfx_hif_set_macaddr(wvif, NULL);
786	wfx_tx_policy_init(wvif);
787
788	cancel_delayed_work_sync(&wvif->beacon_loss_work);
789	wdev->vif[wvif->id] = NULL;
790
791	mutex_unlock(&wdev->conf_mutex);
792
793	wvif = NULL;
794	while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
795		/* Combo mode does not support Block Acks. We can re-enable them */
796		if (wvif_count(wdev) == 1)
797			wfx_hif_set_block_ack_policy(wvif, 0xFF, 0xFF);
798		else
799			wfx_hif_set_block_ack_policy(wvif, 0x00, 0x00);
800	}
801}
802
803int wfx_start(struct ieee80211_hw *hw)
804{
805	return 0;
806}
807
808void wfx_stop(struct ieee80211_hw *hw)
809{
810	struct wfx_dev *wdev = hw->priv;
811
812	WARN_ON(!skb_queue_empty_lockless(&wdev->tx_pending));
813}
814