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