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