xref: /freebsd/sys/contrib/dev/rtw88/mac80211.c (revision 11c53278a8a3e86e14377f09bbaa7bad193d3713)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019  Realtek Corporation
3  */
4 
5 #include "main.h"
6 #include "sec.h"
7 #include "tx.h"
8 #include "fw.h"
9 #include "mac.h"
10 #include "coex.h"
11 #include "ps.h"
12 #include "reg.h"
13 #include "bf.h"
14 #include "debug.h"
15 #include "wow.h"
16 #include "sar.h"
17 
18 static void rtw_ops_tx(struct ieee80211_hw *hw,
19 		       struct ieee80211_tx_control *control,
20 		       struct sk_buff *skb)
21 {
22 	struct rtw_dev *rtwdev = hw->priv;
23 
24 	if (!test_bit(RTW_FLAG_RUNNING, rtwdev->flags)) {
25 		ieee80211_free_txskb(hw, skb);
26 		return;
27 	}
28 
29 	rtw_tx(rtwdev, control, skb);
30 }
31 
32 static void rtw_ops_wake_tx_queue(struct ieee80211_hw *hw,
33 				  struct ieee80211_txq *txq)
34 {
35 	struct rtw_dev *rtwdev = hw->priv;
36 	struct rtw_txq *rtwtxq = (struct rtw_txq *)txq->drv_priv;
37 
38 	if (!test_bit(RTW_FLAG_RUNNING, rtwdev->flags))
39 		return;
40 
41 	spin_lock_bh(&rtwdev->txq_lock);
42 	if (list_empty(&rtwtxq->list))
43 		list_add_tail(&rtwtxq->list, &rtwdev->txqs);
44 	spin_unlock_bh(&rtwdev->txq_lock);
45 
46 	/* ensure to dequeue EAPOL (4/4) at the right time */
47 	if (txq->ac == IEEE80211_AC_VO)
48 		__rtw_tx_work(rtwdev);
49 	else
50 		queue_work(rtwdev->tx_wq, &rtwdev->tx_work);
51 }
52 
53 static int rtw_ops_start(struct ieee80211_hw *hw)
54 {
55 	struct rtw_dev *rtwdev = hw->priv;
56 	int ret;
57 
58 	mutex_lock(&rtwdev->mutex);
59 	ret = rtw_core_start(rtwdev);
60 	mutex_unlock(&rtwdev->mutex);
61 
62 	return ret;
63 }
64 
65 static void rtw_ops_stop(struct ieee80211_hw *hw, bool suspend)
66 {
67 	struct rtw_dev *rtwdev = hw->priv;
68 
69 	mutex_lock(&rtwdev->mutex);
70 	rtw_core_stop(rtwdev);
71 	mutex_unlock(&rtwdev->mutex);
72 }
73 
74 static int rtw_ops_config(struct ieee80211_hw *hw, u32 changed)
75 {
76 	struct rtw_dev *rtwdev = hw->priv;
77 	int ret = 0;
78 
79 	/* let previous ips work finish to ensure we don't leave ips twice */
80 	cancel_work_sync(&rtwdev->ips_work);
81 
82 	mutex_lock(&rtwdev->mutex);
83 
84 	rtw_leave_lps_deep(rtwdev);
85 
86 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
87 	    !(hw->conf.flags & IEEE80211_CONF_IDLE)) {
88 		ret = rtw_leave_ips(rtwdev);
89 		if (ret) {
90 			rtw_err(rtwdev, "failed to leave idle state\n");
91 			goto out;
92 		}
93 	}
94 
95 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
96 		rtw_set_channel(rtwdev);
97 
98 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
99 	    (hw->conf.flags & IEEE80211_CONF_IDLE) &&
100 	    !test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
101 		rtw_enter_ips(rtwdev);
102 
103 out:
104 	mutex_unlock(&rtwdev->mutex);
105 	return ret;
106 }
107 
108 static const struct rtw_vif_port rtw_vif_port[] = {
109 	[0] = {
110 		.mac_addr	= {.addr = 0x0610},
111 		.bssid		= {.addr = 0x0618},
112 		.net_type	= {.addr = 0x0100, .mask = 0x30000},
113 		.aid		= {.addr = 0x06a8, .mask = 0x7ff},
114 		.bcn_ctrl	= {.addr = 0x0550, .mask = 0xff},
115 	},
116 	[1] = {
117 		.mac_addr	= {.addr = 0x0700},
118 		.bssid		= {.addr = 0x0708},
119 		.net_type	= {.addr = 0x0100, .mask = 0xc0000},
120 		.aid		= {.addr = 0x0710, .mask = 0x7ff},
121 		.bcn_ctrl	= {.addr = 0x0551, .mask = 0xff},
122 	},
123 	[2] = {
124 		.mac_addr	= {.addr = 0x1620},
125 		.bssid		= {.addr = 0x1628},
126 		.net_type	= {.addr = 0x1100, .mask = 0x3},
127 		.aid		= {.addr = 0x1600, .mask = 0x7ff},
128 		.bcn_ctrl	= {.addr = 0x0578, .mask = 0xff},
129 	},
130 	[3] = {
131 		.mac_addr	= {.addr = 0x1630},
132 		.bssid		= {.addr = 0x1638},
133 		.net_type	= {.addr = 0x1100, .mask = 0xc},
134 		.aid		= {.addr = 0x1604, .mask = 0x7ff},
135 		.bcn_ctrl	= {.addr = 0x0579, .mask = 0xff},
136 	},
137 	[4] = {
138 		.mac_addr	= {.addr = 0x1640},
139 		.bssid		= {.addr = 0x1648},
140 		.net_type	= {.addr = 0x1100, .mask = 0x30},
141 		.aid		= {.addr = 0x1608, .mask = 0x7ff},
142 		.bcn_ctrl	= {.addr = 0x057a, .mask = 0xff},
143 	},
144 };
145 
146 static int rtw_ops_add_interface(struct ieee80211_hw *hw,
147 				 struct ieee80211_vif *vif)
148 {
149 	struct rtw_dev *rtwdev = hw->priv;
150 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
151 	enum rtw_net_type net_type;
152 	u32 config = 0;
153 	u8 port;
154 	u8 bcn_ctrl = 0;
155 
156 	if (rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_BCN_FILTER))
157 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
158 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
159 	rtwvif->stats.tx_unicast = 0;
160 	rtwvif->stats.rx_unicast = 0;
161 	rtwvif->stats.tx_cnt = 0;
162 	rtwvif->stats.rx_cnt = 0;
163 	rtwvif->scan_req = NULL;
164 	memset(&rtwvif->bfee, 0, sizeof(struct rtw_bfee));
165 	rtw_txq_init(rtwdev, vif->txq);
166 	INIT_LIST_HEAD(&rtwvif->rsvd_page_list);
167 
168 	mutex_lock(&rtwdev->mutex);
169 
170 	port = find_first_zero_bit(rtwdev->hw_port, RTW_PORT_NUM);
171 	if (port >= RTW_PORT_NUM) {
172 		mutex_unlock(&rtwdev->mutex);
173 		return -EINVAL;
174 	}
175 	set_bit(port, rtwdev->hw_port);
176 
177 	rtwvif->port = port;
178 	rtwvif->conf = &rtw_vif_port[port];
179 	rtw_leave_lps_deep(rtwdev);
180 
181 	switch (vif->type) {
182 	case NL80211_IFTYPE_AP:
183 	case NL80211_IFTYPE_MESH_POINT:
184 		rtw_add_rsvd_page_bcn(rtwdev, rtwvif);
185 		net_type = RTW_NET_AP_MODE;
186 		bcn_ctrl = BIT_EN_BCN_FUNCTION | BIT_DIS_TSF_UDT;
187 		break;
188 	case NL80211_IFTYPE_ADHOC:
189 		rtw_add_rsvd_page_bcn(rtwdev, rtwvif);
190 		net_type = RTW_NET_AD_HOC;
191 		bcn_ctrl = BIT_EN_BCN_FUNCTION | BIT_DIS_TSF_UDT;
192 		break;
193 	case NL80211_IFTYPE_STATION:
194 		rtw_add_rsvd_page_sta(rtwdev, rtwvif);
195 		net_type = RTW_NET_NO_LINK;
196 		bcn_ctrl = BIT_EN_BCN_FUNCTION;
197 		break;
198 	default:
199 		WARN_ON(1);
200 		clear_bit(rtwvif->port, rtwdev->hw_port);
201 		mutex_unlock(&rtwdev->mutex);
202 		return -EINVAL;
203 	}
204 
205 	ether_addr_copy(rtwvif->mac_addr, vif->addr);
206 	config |= PORT_SET_MAC_ADDR;
207 	rtwvif->net_type = net_type;
208 	config |= PORT_SET_NET_TYPE;
209 	rtwvif->bcn_ctrl = bcn_ctrl;
210 	config |= PORT_SET_BCN_CTRL;
211 	rtw_vif_port_config(rtwdev, rtwvif, config);
212 	rtw_core_port_switch(rtwdev, vif);
213 	rtw_recalc_lps(rtwdev, vif);
214 
215 	mutex_unlock(&rtwdev->mutex);
216 
217 #if defined(__linux__)
218 	rtw_dbg(rtwdev, RTW_DBG_STATE, "start vif %pM on port %d\n", vif->addr, rtwvif->port);
219 #elif defined(__FreeBSD__)
220 	rtw_dbg(rtwdev, RTW_DBG_STATE, "start vif %6D on port %d\n", vif->addr, ":", rtwvif->port);
221 #endif
222 	return 0;
223 }
224 
225 static void rtw_ops_remove_interface(struct ieee80211_hw *hw,
226 				     struct ieee80211_vif *vif)
227 {
228 	struct rtw_dev *rtwdev = hw->priv;
229 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
230 	u32 config = 0;
231 
232 #if defined(__linux__)
233 	rtw_dbg(rtwdev, RTW_DBG_STATE, "stop vif %pM on port %d\n", vif->addr, rtwvif->port);
234 #elif defined(__FreeBSD__)
235 	rtw_dbg(rtwdev, RTW_DBG_STATE, "stop vif %6D on port %d\n", vif->addr, ":", rtwvif->port);
236 #endif
237 
238 	mutex_lock(&rtwdev->mutex);
239 
240 	rtw_leave_lps_deep(rtwdev);
241 
242 	rtw_txq_cleanup(rtwdev, vif->txq);
243 	rtw_remove_rsvd_page(rtwdev, rtwvif);
244 
245 	eth_zero_addr(rtwvif->mac_addr);
246 	config |= PORT_SET_MAC_ADDR;
247 	rtwvif->net_type = RTW_NET_NO_LINK;
248 	config |= PORT_SET_NET_TYPE;
249 	rtwvif->bcn_ctrl = 0;
250 	config |= PORT_SET_BCN_CTRL;
251 	rtw_vif_port_config(rtwdev, rtwvif, config);
252 	clear_bit(rtwvif->port, rtwdev->hw_port);
253 	rtw_recalc_lps(rtwdev, NULL);
254 
255 	mutex_unlock(&rtwdev->mutex);
256 }
257 
258 static int rtw_ops_change_interface(struct ieee80211_hw *hw,
259 				    struct ieee80211_vif *vif,
260 				    enum nl80211_iftype type, bool p2p)
261 {
262 	struct rtw_dev *rtwdev = hw->priv;
263 
264 #if defined(__linux__)
265 	rtw_dbg(rtwdev, RTW_DBG_STATE, "change vif %pM (%d)->(%d), p2p (%d)->(%d)\n",
266 		vif->addr, vif->type, type, vif->p2p, p2p);
267 #elif defined(__FreeBSD__)
268 	rtw_dbg(rtwdev, RTW_DBG_STATE, "change vif %6D (%d)->(%d), p2p (%d)->(%d)\n",
269 		vif->addr, ":", vif->type, type, vif->p2p, p2p);
270 #endif
271 
272 	rtw_ops_remove_interface(hw, vif);
273 
274 	vif->type = type;
275 	vif->p2p = p2p;
276 
277 	return rtw_ops_add_interface(hw, vif);
278 }
279 
280 static void rtw_ops_configure_filter(struct ieee80211_hw *hw,
281 				     unsigned int changed_flags,
282 				     unsigned int *new_flags,
283 				     u64 multicast)
284 {
285 	struct rtw_dev *rtwdev = hw->priv;
286 
287 	*new_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_FCSFAIL |
288 		      FIF_BCN_PRBRESP_PROMISC;
289 
290 	mutex_lock(&rtwdev->mutex);
291 
292 	rtw_leave_lps_deep(rtwdev);
293 
294 	if (changed_flags & FIF_ALLMULTI) {
295 		if (*new_flags & FIF_ALLMULTI)
296 			rtwdev->hal.rcr |= BIT_AM;
297 		else
298 			rtwdev->hal.rcr &= ~(BIT_AM);
299 	}
300 	if (changed_flags & FIF_FCSFAIL) {
301 		if (*new_flags & FIF_FCSFAIL)
302 			rtwdev->hal.rcr |= BIT_ACRC32;
303 		else
304 			rtwdev->hal.rcr &= ~(BIT_ACRC32);
305 	}
306 	if (changed_flags & FIF_OTHER_BSS) {
307 		if (*new_flags & FIF_OTHER_BSS)
308 			rtwdev->hal.rcr |= BIT_AAP;
309 		else
310 			rtwdev->hal.rcr &= ~(BIT_AAP);
311 	}
312 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
313 		if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
314 			rtwdev->hal.rcr &= ~(BIT_CBSSID_BCN | BIT_CBSSID_DATA);
315 		else
316 			rtwdev->hal.rcr |= BIT_CBSSID_BCN;
317 	}
318 
319 	rtw_dbg(rtwdev, RTW_DBG_RX,
320 		"config rx filter, changed=0x%08x, new=0x%08x, rcr=0x%08x\n",
321 		changed_flags, *new_flags, rtwdev->hal.rcr);
322 
323 	rtw_write32(rtwdev, REG_RCR, rtwdev->hal.rcr);
324 
325 	mutex_unlock(&rtwdev->mutex);
326 }
327 
328 /* Only have one group of EDCA parameters now */
329 static const u32 ac_to_edca_param[IEEE80211_NUM_ACS] = {
330 	[IEEE80211_AC_VO] = REG_EDCA_VO_PARAM,
331 	[IEEE80211_AC_VI] = REG_EDCA_VI_PARAM,
332 	[IEEE80211_AC_BE] = REG_EDCA_BE_PARAM,
333 	[IEEE80211_AC_BK] = REG_EDCA_BK_PARAM,
334 };
335 
336 static u8 rtw_aifsn_to_aifs(struct rtw_dev *rtwdev,
337 			    struct rtw_vif *rtwvif, u8 aifsn)
338 {
339 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
340 	u8 slot_time;
341 	u8 sifs;
342 
343 	slot_time = vif->bss_conf.use_short_slot ? 9 : 20;
344 	sifs = rtwdev->hal.current_band_type == RTW_BAND_5G ? 16 : 10;
345 
346 	return aifsn * slot_time + sifs;
347 }
348 
349 static void __rtw_conf_tx(struct rtw_dev *rtwdev,
350 			  struct rtw_vif *rtwvif, u16 ac)
351 {
352 	struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac];
353 	u32 edca_param = ac_to_edca_param[ac];
354 	u8 ecw_max, ecw_min;
355 	u8 aifs;
356 
357 	/* 2^ecw - 1 = cw; ecw = log2(cw + 1) */
358 	ecw_max = ilog2(params->cw_max + 1);
359 	ecw_min = ilog2(params->cw_min + 1);
360 	aifs = rtw_aifsn_to_aifs(rtwdev, rtwvif, params->aifs);
361 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_TXOP_LMT, params->txop);
362 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_CWMAX, ecw_max);
363 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_CWMIN, ecw_min);
364 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_AIFS, aifs);
365 }
366 
367 static void rtw_conf_tx(struct rtw_dev *rtwdev,
368 			struct rtw_vif *rtwvif)
369 {
370 	u16 ac;
371 
372 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
373 		__rtw_conf_tx(rtwdev, rtwvif, ac);
374 }
375 
376 static void rtw_ops_bss_info_changed(struct ieee80211_hw *hw,
377 				     struct ieee80211_vif *vif,
378 				     struct ieee80211_bss_conf *conf,
379 				     u64 changed)
380 {
381 	struct rtw_dev *rtwdev = hw->priv;
382 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
383 	struct rtw_coex *coex = &rtwdev->coex;
384 	struct rtw_coex_stat *coex_stat = &coex->stat;
385 	u32 config = 0;
386 
387 	mutex_lock(&rtwdev->mutex);
388 
389 	rtw_leave_lps_deep(rtwdev);
390 
391 	if (changed & BSS_CHANGED_ASSOC) {
392 		rtw_vif_assoc_changed(rtwvif, conf);
393 		if (vif->cfg.assoc) {
394 			rtw_coex_connect_notify(rtwdev, COEX_ASSOCIATE_FINISH);
395 
396 			rtw_fw_download_rsvd_page(rtwdev);
397 			rtw_send_rsvd_page_h2c(rtwdev);
398 			rtw_fw_default_port(rtwdev, rtwvif);
399 			rtw_coex_media_status_notify(rtwdev, vif->cfg.assoc);
400 			if (rtw_bf_support)
401 				rtw_bf_assoc(rtwdev, vif, conf);
402 
403 			rtw_fw_beacon_filter_config(rtwdev, true, vif);
404 		} else {
405 			rtw_leave_lps(rtwdev);
406 			rtw_bf_disassoc(rtwdev, vif, conf);
407 			/* Abort ongoing scan if cancel_scan isn't issued
408 			 * when disconnected by peer
409 			 */
410 			if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
411 				rtw_hw_scan_abort(rtwdev);
412 
413 		}
414 
415 		config |= PORT_SET_NET_TYPE;
416 		config |= PORT_SET_AID;
417 	}
418 
419 	if (changed & BSS_CHANGED_BSSID) {
420 		ether_addr_copy(rtwvif->bssid, conf->bssid);
421 		config |= PORT_SET_BSSID;
422 		if (!rtw_core_check_sta_active(rtwdev))
423 			rtw_clear_op_chan(rtwdev);
424 		else
425 			rtw_store_op_chan(rtwdev, true);
426 	}
427 
428 	if (changed & BSS_CHANGED_BEACON_INT) {
429 		if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_STATION)
430 			coex_stat->wl_beacon_interval = conf->beacon_int;
431 	}
432 
433 	if (changed & BSS_CHANGED_BEACON) {
434 		rtw_set_dtim_period(rtwdev, conf->dtim_period);
435 		rtw_fw_download_rsvd_page(rtwdev);
436 		rtw_send_rsvd_page_h2c(rtwdev);
437 	}
438 
439 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
440 		if (conf->enable_beacon)
441 			rtw_write32_set(rtwdev, REG_FWHW_TXQ_CTRL,
442 					BIT_EN_BCNQ_DL);
443 		else
444 			rtw_write32_clr(rtwdev, REG_FWHW_TXQ_CTRL,
445 					BIT_EN_BCNQ_DL);
446 	}
447 	if (changed & BSS_CHANGED_CQM)
448 		rtw_fw_beacon_filter_config(rtwdev, true, vif);
449 
450 	if (changed & BSS_CHANGED_MU_GROUPS)
451 		rtw_chip_set_gid_table(rtwdev, vif, conf);
452 
453 	if (changed & BSS_CHANGED_ERP_SLOT)
454 		rtw_conf_tx(rtwdev, rtwvif);
455 
456 	if (changed & BSS_CHANGED_PS)
457 		rtw_recalc_lps(rtwdev, NULL);
458 
459 	rtw_vif_port_config(rtwdev, rtwvif, config);
460 
461 	mutex_unlock(&rtwdev->mutex);
462 }
463 
464 static int rtw_ops_start_ap(struct ieee80211_hw *hw,
465 			    struct ieee80211_vif *vif,
466 			    struct ieee80211_bss_conf *link_conf)
467 {
468 	struct rtw_dev *rtwdev = hw->priv;
469 	const struct rtw_chip_info *chip = rtwdev->chip;
470 
471 	mutex_lock(&rtwdev->mutex);
472 	rtw_write32_set(rtwdev, REG_TCR, BIT_TCR_UPDATE_HGQMD);
473 	rtwdev->ap_active = true;
474 	rtw_store_op_chan(rtwdev, true);
475 	chip->ops->phy_calibration(rtwdev);
476 	mutex_unlock(&rtwdev->mutex);
477 
478 	return 0;
479 }
480 
481 static void rtw_ops_stop_ap(struct ieee80211_hw *hw,
482 			    struct ieee80211_vif *vif,
483 			    struct ieee80211_bss_conf *link_conf)
484 {
485 	struct rtw_dev *rtwdev = hw->priv;
486 
487 	mutex_lock(&rtwdev->mutex);
488 	rtw_write32_clr(rtwdev, REG_TCR, BIT_TCR_UPDATE_HGQMD);
489 	rtwdev->ap_active = false;
490 	if (!rtw_core_check_sta_active(rtwdev))
491 		rtw_clear_op_chan(rtwdev);
492 	mutex_unlock(&rtwdev->mutex);
493 }
494 
495 static int rtw_ops_conf_tx(struct ieee80211_hw *hw,
496 			   struct ieee80211_vif *vif,
497 			   unsigned int link_id, u16 ac,
498 			   const struct ieee80211_tx_queue_params *params)
499 {
500 	struct rtw_dev *rtwdev = hw->priv;
501 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
502 
503 	mutex_lock(&rtwdev->mutex);
504 
505 	rtw_leave_lps_deep(rtwdev);
506 
507 	rtwvif->tx_params[ac] = *params;
508 	__rtw_conf_tx(rtwdev, rtwvif, ac);
509 
510 	mutex_unlock(&rtwdev->mutex);
511 
512 	return 0;
513 }
514 
515 static int rtw_ops_sta_add(struct ieee80211_hw *hw,
516 			   struct ieee80211_vif *vif,
517 			   struct ieee80211_sta *sta)
518 {
519 	struct rtw_dev *rtwdev = hw->priv;
520 	int ret = 0;
521 
522 	mutex_lock(&rtwdev->mutex);
523 	ret = rtw_sta_add(rtwdev, sta, vif);
524 	mutex_unlock(&rtwdev->mutex);
525 
526 	return ret;
527 }
528 
529 static int rtw_ops_sta_remove(struct ieee80211_hw *hw,
530 			      struct ieee80211_vif *vif,
531 			      struct ieee80211_sta *sta)
532 {
533 	struct rtw_dev *rtwdev = hw->priv;
534 
535 	mutex_lock(&rtwdev->mutex);
536 	rtw_fw_beacon_filter_config(rtwdev, false, vif);
537 	rtw_sta_remove(rtwdev, sta, true);
538 	mutex_unlock(&rtwdev->mutex);
539 
540 	return 0;
541 }
542 
543 static int rtw_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
544 			   bool set)
545 {
546 	struct rtw_dev *rtwdev = hw->priv;
547 
548 	ieee80211_queue_work(hw, &rtwdev->update_beacon_work);
549 
550 	return 0;
551 }
552 
553 static int rtw_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
554 			   struct ieee80211_vif *vif, struct ieee80211_sta *sta,
555 			   struct ieee80211_key_conf *key)
556 {
557 	struct rtw_dev *rtwdev = hw->priv;
558 	struct rtw_sec_desc *sec = &rtwdev->sec;
559 	u8 hw_key_type;
560 	u8 hw_key_idx;
561 	int ret = 0;
562 
563 	switch (key->cipher) {
564 	case WLAN_CIPHER_SUITE_WEP40:
565 		hw_key_type = RTW_CAM_WEP40;
566 		break;
567 	case WLAN_CIPHER_SUITE_WEP104:
568 		hw_key_type = RTW_CAM_WEP104;
569 		break;
570 	case WLAN_CIPHER_SUITE_TKIP:
571 		hw_key_type = RTW_CAM_TKIP;
572 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
573 		break;
574 	case WLAN_CIPHER_SUITE_CCMP:
575 		hw_key_type = RTW_CAM_AES;
576 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
577 		break;
578 	case WLAN_CIPHER_SUITE_AES_CMAC:
579 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
580 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
581 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
582 	case WLAN_CIPHER_SUITE_CCMP_256:
583 	case WLAN_CIPHER_SUITE_GCMP:
584 	case WLAN_CIPHER_SUITE_GCMP_256:
585 		/* suppress error messages */
586 		return -EOPNOTSUPP;
587 	default:
588 		return -ENOTSUPP;
589 	}
590 
591 	mutex_lock(&rtwdev->mutex);
592 
593 	rtw_leave_lps_deep(rtwdev);
594 
595 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
596 		hw_key_idx = rtw_sec_get_free_cam(sec);
597 	} else {
598 		/* multiple interfaces? */
599 		hw_key_idx = key->keyidx;
600 	}
601 
602 	if (hw_key_idx > sec->total_cam_num) {
603 		ret = -ENOSPC;
604 		goto out;
605 	}
606 
607 	switch (cmd) {
608 	case SET_KEY:
609 		/* need sw generated IV */
610 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
611 		key->hw_key_idx = hw_key_idx;
612 		rtw_sec_write_cam(rtwdev, sec, sta, key,
613 				  hw_key_type, hw_key_idx);
614 		break;
615 	case DISABLE_KEY:
616 		rtw_hci_flush_all_queues(rtwdev, false);
617 		rtw_mac_flush_all_queues(rtwdev, false);
618 		rtw_sec_clear_cam(rtwdev, sec, key->hw_key_idx);
619 		break;
620 	}
621 
622 	/* download new cam settings for PG to backup */
623 	if (rtw_get_lps_deep_mode(rtwdev) == LPS_DEEP_MODE_PG)
624 		rtw_fw_download_rsvd_page(rtwdev);
625 
626 out:
627 	mutex_unlock(&rtwdev->mutex);
628 
629 	return ret;
630 }
631 
632 static int rtw_ops_ampdu_action(struct ieee80211_hw *hw,
633 				struct ieee80211_vif *vif,
634 				struct ieee80211_ampdu_params *params)
635 {
636 	struct ieee80211_sta *sta = params->sta;
637 	u16 tid = params->tid;
638 	struct ieee80211_txq *txq = sta->txq[tid];
639 	struct rtw_txq *rtwtxq = (struct rtw_txq *)txq->drv_priv;
640 
641 	switch (params->action) {
642 	case IEEE80211_AMPDU_TX_START:
643 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
644 	case IEEE80211_AMPDU_TX_STOP_CONT:
645 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
646 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
647 		clear_bit(RTW_TXQ_AMPDU, &rtwtxq->flags);
648 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
649 		break;
650 	case IEEE80211_AMPDU_TX_OPERATIONAL:
651 		set_bit(RTW_TXQ_AMPDU, &rtwtxq->flags);
652 		break;
653 	case IEEE80211_AMPDU_RX_START:
654 	case IEEE80211_AMPDU_RX_STOP:
655 		break;
656 	default:
657 		WARN_ON(1);
658 		return -ENOTSUPP;
659 	}
660 
661 	return 0;
662 }
663 
664 static bool rtw_ops_can_aggregate_in_amsdu(struct ieee80211_hw *hw,
665 					   struct sk_buff *head,
666 					   struct sk_buff *skb)
667 {
668 	struct rtw_dev *rtwdev = hw->priv;
669 	struct rtw_hal *hal = &rtwdev->hal;
670 
671 	/* we don't want to enable TX AMSDU on 2.4G */
672 	if (hal->current_band_type == RTW_BAND_2G)
673 		return false;
674 
675 	return true;
676 }
677 
678 static void rtw_ops_sw_scan_start(struct ieee80211_hw *hw,
679 				  struct ieee80211_vif *vif,
680 				  const u8 *mac_addr)
681 {
682 	struct rtw_dev *rtwdev = hw->priv;
683 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
684 
685 	mutex_lock(&rtwdev->mutex);
686 	rtw_core_scan_start(rtwdev, rtwvif, mac_addr, false);
687 	mutex_unlock(&rtwdev->mutex);
688 }
689 
690 static void rtw_ops_sw_scan_complete(struct ieee80211_hw *hw,
691 				     struct ieee80211_vif *vif)
692 {
693 	struct rtw_dev *rtwdev = hw->priv;
694 
695 	mutex_lock(&rtwdev->mutex);
696 	rtw_core_scan_complete(rtwdev, vif, false);
697 	mutex_unlock(&rtwdev->mutex);
698 }
699 
700 static void rtw_ops_mgd_prepare_tx(struct ieee80211_hw *hw,
701 				   struct ieee80211_vif *vif,
702 				   struct ieee80211_prep_tx_info *info)
703 {
704 	struct rtw_dev *rtwdev = hw->priv;
705 
706 	mutex_lock(&rtwdev->mutex);
707 	rtw_leave_lps_deep(rtwdev);
708 	rtw_coex_connect_notify(rtwdev, COEX_ASSOCIATE_START);
709 	rtw_chip_prepare_tx(rtwdev);
710 	mutex_unlock(&rtwdev->mutex);
711 }
712 
713 static int rtw_ops_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
714 {
715 	struct rtw_dev *rtwdev = hw->priv;
716 
717 	mutex_lock(&rtwdev->mutex);
718 	rtwdev->rts_threshold = value;
719 	mutex_unlock(&rtwdev->mutex);
720 
721 	return 0;
722 }
723 
724 static void rtw_ops_sta_statistics(struct ieee80211_hw *hw,
725 				   struct ieee80211_vif *vif,
726 				   struct ieee80211_sta *sta,
727 				   struct station_info *sinfo)
728 {
729 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
730 
731 	sinfo->txrate = si->ra_report.txrate;
732 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
733 }
734 
735 static void rtw_ops_flush(struct ieee80211_hw *hw,
736 			  struct ieee80211_vif *vif,
737 			  u32 queues, bool drop)
738 {
739 	struct rtw_dev *rtwdev = hw->priv;
740 
741 	mutex_lock(&rtwdev->mutex);
742 	rtw_leave_lps_deep(rtwdev);
743 
744 	rtw_hci_flush_queues(rtwdev, queues, drop);
745 	rtw_mac_flush_queues(rtwdev, queues, drop);
746 	mutex_unlock(&rtwdev->mutex);
747 }
748 
749 struct rtw_iter_bitrate_mask_data {
750 	struct rtw_dev *rtwdev;
751 	struct ieee80211_vif *vif;
752 	const struct cfg80211_bitrate_mask *mask;
753 };
754 
755 static void rtw_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta)
756 {
757 	struct rtw_iter_bitrate_mask_data *br_data = data;
758 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
759 
760 	if (si->vif != br_data->vif)
761 		return;
762 
763 	/* free previous mask setting */
764 	kfree(si->mask);
765 	si->mask = kmemdup(br_data->mask, sizeof(struct cfg80211_bitrate_mask),
766 			   GFP_ATOMIC);
767 	if (!si->mask) {
768 		si->use_cfg_mask = false;
769 		return;
770 	}
771 
772 	si->use_cfg_mask = true;
773 	rtw_update_sta_info(br_data->rtwdev, si, true);
774 }
775 
776 static void rtw_ra_mask_info_update(struct rtw_dev *rtwdev,
777 				    struct ieee80211_vif *vif,
778 				    const struct cfg80211_bitrate_mask *mask)
779 {
780 	struct rtw_iter_bitrate_mask_data br_data;
781 
782 	br_data.rtwdev = rtwdev;
783 	br_data.vif = vif;
784 	br_data.mask = mask;
785 	rtw_iterate_stas(rtwdev, rtw_ra_mask_info_update_iter, &br_data);
786 }
787 
788 static int rtw_ops_set_bitrate_mask(struct ieee80211_hw *hw,
789 				    struct ieee80211_vif *vif,
790 				    const struct cfg80211_bitrate_mask *mask)
791 {
792 	struct rtw_dev *rtwdev = hw->priv;
793 
794 	mutex_lock(&rtwdev->mutex);
795 	rtw_ra_mask_info_update(rtwdev, vif, mask);
796 	mutex_unlock(&rtwdev->mutex);
797 
798 	return 0;
799 }
800 
801 static int rtw_ops_set_antenna(struct ieee80211_hw *hw,
802 			       u32 tx_antenna,
803 			       u32 rx_antenna)
804 {
805 	struct rtw_dev *rtwdev = hw->priv;
806 	const struct rtw_chip_info *chip = rtwdev->chip;
807 	int ret;
808 
809 	if (!chip->ops->set_antenna)
810 		return -EOPNOTSUPP;
811 
812 	mutex_lock(&rtwdev->mutex);
813 	ret = chip->ops->set_antenna(rtwdev, tx_antenna, rx_antenna);
814 	mutex_unlock(&rtwdev->mutex);
815 
816 	return ret;
817 }
818 
819 static int rtw_ops_get_antenna(struct ieee80211_hw *hw,
820 			       u32 *tx_antenna,
821 			       u32 *rx_antenna)
822 {
823 	struct rtw_dev *rtwdev = hw->priv;
824 	struct rtw_hal *hal = &rtwdev->hal;
825 
826 	*tx_antenna = hal->antenna_tx;
827 	*rx_antenna = hal->antenna_rx;
828 
829 	return 0;
830 }
831 
832 #ifdef CONFIG_PM
833 static int rtw_ops_suspend(struct ieee80211_hw *hw,
834 			   struct cfg80211_wowlan *wowlan)
835 {
836 	struct rtw_dev *rtwdev = hw->priv;
837 	int ret;
838 
839 	mutex_lock(&rtwdev->mutex);
840 	ret = rtw_wow_suspend(rtwdev, wowlan);
841 	if (ret)
842 		rtw_err(rtwdev, "failed to suspend for wow %d\n", ret);
843 	mutex_unlock(&rtwdev->mutex);
844 
845 	return ret ? 1 : 0;
846 }
847 
848 static int rtw_ops_resume(struct ieee80211_hw *hw)
849 {
850 	struct rtw_dev *rtwdev = hw->priv;
851 	int ret;
852 
853 	mutex_lock(&rtwdev->mutex);
854 	ret = rtw_wow_resume(rtwdev);
855 	if (ret)
856 		rtw_err(rtwdev, "failed to resume for wow %d\n", ret);
857 	mutex_unlock(&rtwdev->mutex);
858 
859 	return ret ? 1 : 0;
860 }
861 
862 static void rtw_ops_set_wakeup(struct ieee80211_hw *hw, bool enabled)
863 {
864 	struct rtw_dev *rtwdev = hw->priv;
865 
866 	device_set_wakeup_enable(rtwdev->dev, enabled);
867 }
868 #endif
869 
870 static void rtw_reconfig_complete(struct ieee80211_hw *hw,
871 				  enum ieee80211_reconfig_type reconfig_type)
872 {
873 	struct rtw_dev *rtwdev = hw->priv;
874 
875 	mutex_lock(&rtwdev->mutex);
876 	if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
877 		clear_bit(RTW_FLAG_RESTARTING, rtwdev->flags);
878 	mutex_unlock(&rtwdev->mutex);
879 }
880 
881 static int rtw_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
882 			   struct ieee80211_scan_request *req)
883 {
884 	struct rtw_dev *rtwdev = hw->priv;
885 	int ret;
886 
887 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
888 		return 1;
889 
890 	if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
891 		return -EBUSY;
892 
893 	mutex_lock(&rtwdev->mutex);
894 	rtw_hw_scan_start(rtwdev, vif, req);
895 	ret = rtw_hw_scan_offload(rtwdev, vif, true);
896 	if (ret) {
897 		rtw_hw_scan_abort(rtwdev);
898 		rtw_err(rtwdev, "HW scan failed with status: %d\n", ret);
899 	}
900 	mutex_unlock(&rtwdev->mutex);
901 
902 	return ret;
903 }
904 
905 static void rtw_ops_cancel_hw_scan(struct ieee80211_hw *hw,
906 				   struct ieee80211_vif *vif)
907 {
908 	struct rtw_dev *rtwdev = hw->priv;
909 
910 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
911 		return;
912 
913 	if (!test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
914 		return;
915 
916 	mutex_lock(&rtwdev->mutex);
917 	rtw_hw_scan_abort(rtwdev);
918 	mutex_unlock(&rtwdev->mutex);
919 }
920 
921 static int rtw_ops_set_sar_specs(struct ieee80211_hw *hw,
922 				 const struct cfg80211_sar_specs *sar)
923 {
924 	struct rtw_dev *rtwdev = hw->priv;
925 
926 	mutex_lock(&rtwdev->mutex);
927 	rtw_set_sar_specs(rtwdev, sar);
928 	mutex_unlock(&rtwdev->mutex);
929 
930 	return 0;
931 }
932 
933 static void rtw_ops_sta_rc_update(struct ieee80211_hw *hw,
934 				  struct ieee80211_vif *vif,
935 				  struct ieee80211_sta *sta, u32 changed)
936 {
937 	struct rtw_dev *rtwdev = hw->priv;
938 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
939 
940 	if (changed & IEEE80211_RC_BW_CHANGED)
941 		ieee80211_queue_work(rtwdev->hw, &si->rc_work);
942 }
943 
944 const struct ieee80211_ops rtw_ops = {
945 	.add_chanctx = ieee80211_emulate_add_chanctx,
946 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
947 	.change_chanctx = ieee80211_emulate_change_chanctx,
948 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
949 	.tx			= rtw_ops_tx,
950 	.wake_tx_queue		= rtw_ops_wake_tx_queue,
951 	.start			= rtw_ops_start,
952 	.stop			= rtw_ops_stop,
953 	.config			= rtw_ops_config,
954 	.add_interface		= rtw_ops_add_interface,
955 	.remove_interface	= rtw_ops_remove_interface,
956 	.change_interface	= rtw_ops_change_interface,
957 	.configure_filter	= rtw_ops_configure_filter,
958 	.bss_info_changed	= rtw_ops_bss_info_changed,
959 	.start_ap		= rtw_ops_start_ap,
960 	.stop_ap		= rtw_ops_stop_ap,
961 	.conf_tx		= rtw_ops_conf_tx,
962 	.sta_add		= rtw_ops_sta_add,
963 	.sta_remove		= rtw_ops_sta_remove,
964 	.set_tim		= rtw_ops_set_tim,
965 	.set_key		= rtw_ops_set_key,
966 	.ampdu_action		= rtw_ops_ampdu_action,
967 	.can_aggregate_in_amsdu	= rtw_ops_can_aggregate_in_amsdu,
968 	.sw_scan_start		= rtw_ops_sw_scan_start,
969 	.sw_scan_complete	= rtw_ops_sw_scan_complete,
970 	.mgd_prepare_tx		= rtw_ops_mgd_prepare_tx,
971 	.set_rts_threshold	= rtw_ops_set_rts_threshold,
972 	.sta_statistics		= rtw_ops_sta_statistics,
973 	.flush			= rtw_ops_flush,
974 	.set_bitrate_mask	= rtw_ops_set_bitrate_mask,
975 	.set_antenna		= rtw_ops_set_antenna,
976 	.get_antenna		= rtw_ops_get_antenna,
977 	.reconfig_complete	= rtw_reconfig_complete,
978 	.hw_scan		= rtw_ops_hw_scan,
979 	.cancel_hw_scan		= rtw_ops_cancel_hw_scan,
980 	.sta_rc_update		= rtw_ops_sta_rc_update,
981 	.set_sar_specs          = rtw_ops_set_sar_specs,
982 #ifdef CONFIG_PM
983 	.suspend		= rtw_ops_suspend,
984 	.resume			= rtw_ops_resume,
985 	.set_wakeup		= rtw_ops_set_wakeup,
986 #endif
987 };
988 EXPORT_SYMBOL(rtw_ops);
989