xref: /freebsd/sys/contrib/dev/rtw88/mac80211.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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)
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 | BIT_AB;
297 		else
298 			rtwdev->hal.rcr &= ~(BIT_AM | BIT_AB);
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 		} else {
403 			rtw_leave_lps(rtwdev);
404 			rtw_bf_disassoc(rtwdev, vif, conf);
405 			/* Abort ongoing scan if cancel_scan isn't issued
406 			 * when disconnected by peer
407 			 */
408 			if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
409 				rtw_hw_scan_abort(rtwdev);
410 
411 		}
412 
413 		config |= PORT_SET_NET_TYPE;
414 		config |= PORT_SET_AID;
415 	}
416 
417 	if (changed & BSS_CHANGED_BSSID) {
418 		ether_addr_copy(rtwvif->bssid, conf->bssid);
419 		config |= PORT_SET_BSSID;
420 		if (!rtw_core_check_sta_active(rtwdev))
421 			rtw_clear_op_chan(rtwdev);
422 		else
423 			rtw_store_op_chan(rtwdev, true);
424 	}
425 
426 	if (changed & BSS_CHANGED_BEACON_INT) {
427 		if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_STATION)
428 			coex_stat->wl_beacon_interval = conf->beacon_int;
429 	}
430 
431 	if (changed & BSS_CHANGED_BEACON) {
432 		rtw_set_dtim_period(rtwdev, conf->dtim_period);
433 		rtw_fw_download_rsvd_page(rtwdev);
434 		rtw_send_rsvd_page_h2c(rtwdev);
435 	}
436 
437 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
438 		if (conf->enable_beacon)
439 			rtw_write32_set(rtwdev, REG_FWHW_TXQ_CTRL,
440 					BIT_EN_BCNQ_DL);
441 		else
442 			rtw_write32_clr(rtwdev, REG_FWHW_TXQ_CTRL,
443 					BIT_EN_BCNQ_DL);
444 	}
445 	if (changed & BSS_CHANGED_CQM)
446 		rtw_fw_beacon_filter_config(rtwdev, true, vif);
447 
448 	if (changed & BSS_CHANGED_MU_GROUPS)
449 		rtw_chip_set_gid_table(rtwdev, vif, conf);
450 
451 	if (changed & BSS_CHANGED_ERP_SLOT)
452 		rtw_conf_tx(rtwdev, rtwvif);
453 
454 	if (changed & BSS_CHANGED_PS)
455 		rtw_recalc_lps(rtwdev, NULL);
456 
457 	rtw_vif_port_config(rtwdev, rtwvif, config);
458 
459 	mutex_unlock(&rtwdev->mutex);
460 }
461 
462 static int rtw_ops_start_ap(struct ieee80211_hw *hw,
463 			    struct ieee80211_vif *vif,
464 			    struct ieee80211_bss_conf *link_conf)
465 {
466 	struct rtw_dev *rtwdev = hw->priv;
467 	const struct rtw_chip_info *chip = rtwdev->chip;
468 
469 	mutex_lock(&rtwdev->mutex);
470 	rtw_write32_set(rtwdev, REG_TCR, BIT_TCR_UPDATE_HGQMD);
471 	rtwdev->ap_active = true;
472 	rtw_store_op_chan(rtwdev, true);
473 	chip->ops->phy_calibration(rtwdev);
474 	mutex_unlock(&rtwdev->mutex);
475 
476 	return 0;
477 }
478 
479 static void rtw_ops_stop_ap(struct ieee80211_hw *hw,
480 			    struct ieee80211_vif *vif,
481 			    struct ieee80211_bss_conf *link_conf)
482 {
483 	struct rtw_dev *rtwdev = hw->priv;
484 
485 	mutex_lock(&rtwdev->mutex);
486 	rtw_write32_clr(rtwdev, REG_TCR, BIT_TCR_UPDATE_HGQMD);
487 	rtwdev->ap_active = false;
488 	if (!rtw_core_check_sta_active(rtwdev))
489 		rtw_clear_op_chan(rtwdev);
490 	mutex_unlock(&rtwdev->mutex);
491 }
492 
493 static int rtw_ops_conf_tx(struct ieee80211_hw *hw,
494 			   struct ieee80211_vif *vif,
495 			   unsigned int link_id, u16 ac,
496 			   const struct ieee80211_tx_queue_params *params)
497 {
498 	struct rtw_dev *rtwdev = hw->priv;
499 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
500 
501 	mutex_lock(&rtwdev->mutex);
502 
503 	rtw_leave_lps_deep(rtwdev);
504 
505 	rtwvif->tx_params[ac] = *params;
506 	__rtw_conf_tx(rtwdev, rtwvif, ac);
507 
508 	mutex_unlock(&rtwdev->mutex);
509 
510 	return 0;
511 }
512 
513 static int rtw_ops_sta_add(struct ieee80211_hw *hw,
514 			   struct ieee80211_vif *vif,
515 			   struct ieee80211_sta *sta)
516 {
517 	struct rtw_dev *rtwdev = hw->priv;
518 	int ret = 0;
519 
520 	mutex_lock(&rtwdev->mutex);
521 	ret = rtw_sta_add(rtwdev, sta, vif);
522 	mutex_unlock(&rtwdev->mutex);
523 
524 	return ret;
525 }
526 
527 static int rtw_ops_sta_remove(struct ieee80211_hw *hw,
528 			      struct ieee80211_vif *vif,
529 			      struct ieee80211_sta *sta)
530 {
531 	struct rtw_dev *rtwdev = hw->priv;
532 
533 	mutex_lock(&rtwdev->mutex);
534 	rtw_fw_beacon_filter_config(rtwdev, false, vif);
535 	rtw_sta_remove(rtwdev, sta, true);
536 	mutex_unlock(&rtwdev->mutex);
537 
538 	return 0;
539 }
540 
541 static int rtw_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
542 			   bool set)
543 {
544 	struct rtw_dev *rtwdev = hw->priv;
545 
546 	ieee80211_queue_work(hw, &rtwdev->update_beacon_work);
547 
548 	return 0;
549 }
550 
551 static int rtw_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
552 			   struct ieee80211_vif *vif, struct ieee80211_sta *sta,
553 			   struct ieee80211_key_conf *key)
554 {
555 	struct rtw_dev *rtwdev = hw->priv;
556 	struct rtw_sec_desc *sec = &rtwdev->sec;
557 	u8 hw_key_type;
558 	u8 hw_key_idx;
559 	int ret = 0;
560 
561 	switch (key->cipher) {
562 	case WLAN_CIPHER_SUITE_WEP40:
563 		hw_key_type = RTW_CAM_WEP40;
564 		break;
565 	case WLAN_CIPHER_SUITE_WEP104:
566 		hw_key_type = RTW_CAM_WEP104;
567 		break;
568 	case WLAN_CIPHER_SUITE_TKIP:
569 		hw_key_type = RTW_CAM_TKIP;
570 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
571 		break;
572 	case WLAN_CIPHER_SUITE_CCMP:
573 		hw_key_type = RTW_CAM_AES;
574 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
575 		break;
576 	case WLAN_CIPHER_SUITE_AES_CMAC:
577 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
578 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
579 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
580 	case WLAN_CIPHER_SUITE_CCMP_256:
581 	case WLAN_CIPHER_SUITE_GCMP:
582 	case WLAN_CIPHER_SUITE_GCMP_256:
583 		/* suppress error messages */
584 		return -EOPNOTSUPP;
585 	default:
586 		return -ENOTSUPP;
587 	}
588 
589 	mutex_lock(&rtwdev->mutex);
590 
591 	rtw_leave_lps_deep(rtwdev);
592 
593 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
594 		hw_key_idx = rtw_sec_get_free_cam(sec);
595 	} else {
596 		/* multiple interfaces? */
597 		hw_key_idx = key->keyidx;
598 	}
599 
600 	if (hw_key_idx > sec->total_cam_num) {
601 		ret = -ENOSPC;
602 		goto out;
603 	}
604 
605 	switch (cmd) {
606 	case SET_KEY:
607 		/* need sw generated IV */
608 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
609 		key->hw_key_idx = hw_key_idx;
610 		rtw_sec_write_cam(rtwdev, sec, sta, key,
611 				  hw_key_type, hw_key_idx);
612 		break;
613 	case DISABLE_KEY:
614 		rtw_hci_flush_all_queues(rtwdev, false);
615 		rtw_mac_flush_all_queues(rtwdev, false);
616 		rtw_sec_clear_cam(rtwdev, sec, key->hw_key_idx);
617 		break;
618 	}
619 
620 	/* download new cam settings for PG to backup */
621 	if (rtw_get_lps_deep_mode(rtwdev) == LPS_DEEP_MODE_PG)
622 		rtw_fw_download_rsvd_page(rtwdev);
623 
624 out:
625 	mutex_unlock(&rtwdev->mutex);
626 
627 	return ret;
628 }
629 
630 static int rtw_ops_ampdu_action(struct ieee80211_hw *hw,
631 				struct ieee80211_vif *vif,
632 				struct ieee80211_ampdu_params *params)
633 {
634 	struct ieee80211_sta *sta = params->sta;
635 	u16 tid = params->tid;
636 	struct ieee80211_txq *txq = sta->txq[tid];
637 	struct rtw_txq *rtwtxq = (struct rtw_txq *)txq->drv_priv;
638 
639 	switch (params->action) {
640 	case IEEE80211_AMPDU_TX_START:
641 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
642 	case IEEE80211_AMPDU_TX_STOP_CONT:
643 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
644 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
645 		clear_bit(RTW_TXQ_AMPDU, &rtwtxq->flags);
646 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
647 		break;
648 	case IEEE80211_AMPDU_TX_OPERATIONAL:
649 		set_bit(RTW_TXQ_AMPDU, &rtwtxq->flags);
650 		break;
651 	case IEEE80211_AMPDU_RX_START:
652 	case IEEE80211_AMPDU_RX_STOP:
653 		break;
654 	default:
655 		WARN_ON(1);
656 		return -ENOTSUPP;
657 	}
658 
659 	return 0;
660 }
661 
662 static bool rtw_ops_can_aggregate_in_amsdu(struct ieee80211_hw *hw,
663 					   struct sk_buff *head,
664 					   struct sk_buff *skb)
665 {
666 	struct rtw_dev *rtwdev = hw->priv;
667 	struct rtw_hal *hal = &rtwdev->hal;
668 
669 	/* we don't want to enable TX AMSDU on 2.4G */
670 	if (hal->current_band_type == RTW_BAND_2G)
671 		return false;
672 
673 	return true;
674 }
675 
676 static void rtw_ops_sw_scan_start(struct ieee80211_hw *hw,
677 				  struct ieee80211_vif *vif,
678 				  const u8 *mac_addr)
679 {
680 	struct rtw_dev *rtwdev = hw->priv;
681 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
682 
683 	mutex_lock(&rtwdev->mutex);
684 	rtw_core_scan_start(rtwdev, rtwvif, mac_addr, false);
685 	mutex_unlock(&rtwdev->mutex);
686 }
687 
688 static void rtw_ops_sw_scan_complete(struct ieee80211_hw *hw,
689 				     struct ieee80211_vif *vif)
690 {
691 	struct rtw_dev *rtwdev = hw->priv;
692 
693 	mutex_lock(&rtwdev->mutex);
694 	rtw_core_scan_complete(rtwdev, vif, false);
695 	mutex_unlock(&rtwdev->mutex);
696 }
697 
698 static void rtw_ops_mgd_prepare_tx(struct ieee80211_hw *hw,
699 				   struct ieee80211_vif *vif,
700 				   struct ieee80211_prep_tx_info *info)
701 {
702 	struct rtw_dev *rtwdev = hw->priv;
703 
704 	mutex_lock(&rtwdev->mutex);
705 	rtw_leave_lps_deep(rtwdev);
706 	rtw_coex_connect_notify(rtwdev, COEX_ASSOCIATE_START);
707 	rtw_chip_prepare_tx(rtwdev);
708 	mutex_unlock(&rtwdev->mutex);
709 }
710 
711 static int rtw_ops_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
712 {
713 	struct rtw_dev *rtwdev = hw->priv;
714 
715 	mutex_lock(&rtwdev->mutex);
716 	rtwdev->rts_threshold = value;
717 	mutex_unlock(&rtwdev->mutex);
718 
719 	return 0;
720 }
721 
722 static void rtw_ops_sta_statistics(struct ieee80211_hw *hw,
723 				   struct ieee80211_vif *vif,
724 				   struct ieee80211_sta *sta,
725 				   struct station_info *sinfo)
726 {
727 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
728 
729 	sinfo->txrate = si->ra_report.txrate;
730 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
731 }
732 
733 static void rtw_ops_flush(struct ieee80211_hw *hw,
734 			  struct ieee80211_vif *vif,
735 			  u32 queues, bool drop)
736 {
737 	struct rtw_dev *rtwdev = hw->priv;
738 
739 	mutex_lock(&rtwdev->mutex);
740 	rtw_leave_lps_deep(rtwdev);
741 
742 	rtw_hci_flush_queues(rtwdev, queues, drop);
743 	rtw_mac_flush_queues(rtwdev, queues, drop);
744 	mutex_unlock(&rtwdev->mutex);
745 }
746 
747 struct rtw_iter_bitrate_mask_data {
748 	struct rtw_dev *rtwdev;
749 	struct ieee80211_vif *vif;
750 	const struct cfg80211_bitrate_mask *mask;
751 };
752 
753 static void rtw_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta)
754 {
755 	struct rtw_iter_bitrate_mask_data *br_data = data;
756 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
757 
758 	if (si->vif != br_data->vif)
759 		return;
760 
761 	/* free previous mask setting */
762 	kfree(si->mask);
763 	si->mask = kmemdup(br_data->mask, sizeof(struct cfg80211_bitrate_mask),
764 			   GFP_ATOMIC);
765 	if (!si->mask) {
766 		si->use_cfg_mask = false;
767 		return;
768 	}
769 
770 	si->use_cfg_mask = true;
771 	rtw_update_sta_info(br_data->rtwdev, si, true);
772 }
773 
774 static void rtw_ra_mask_info_update(struct rtw_dev *rtwdev,
775 				    struct ieee80211_vif *vif,
776 				    const struct cfg80211_bitrate_mask *mask)
777 {
778 	struct rtw_iter_bitrate_mask_data br_data;
779 
780 	br_data.rtwdev = rtwdev;
781 	br_data.vif = vif;
782 	br_data.mask = mask;
783 	rtw_iterate_stas(rtwdev, rtw_ra_mask_info_update_iter, &br_data);
784 }
785 
786 static int rtw_ops_set_bitrate_mask(struct ieee80211_hw *hw,
787 				    struct ieee80211_vif *vif,
788 				    const struct cfg80211_bitrate_mask *mask)
789 {
790 	struct rtw_dev *rtwdev = hw->priv;
791 
792 	mutex_lock(&rtwdev->mutex);
793 	rtw_ra_mask_info_update(rtwdev, vif, mask);
794 	mutex_unlock(&rtwdev->mutex);
795 
796 	return 0;
797 }
798 
799 static int rtw_ops_set_antenna(struct ieee80211_hw *hw,
800 			       u32 tx_antenna,
801 			       u32 rx_antenna)
802 {
803 	struct rtw_dev *rtwdev = hw->priv;
804 	const struct rtw_chip_info *chip = rtwdev->chip;
805 	int ret;
806 
807 	if (!chip->ops->set_antenna)
808 		return -EOPNOTSUPP;
809 
810 	mutex_lock(&rtwdev->mutex);
811 	ret = chip->ops->set_antenna(rtwdev, tx_antenna, rx_antenna);
812 	mutex_unlock(&rtwdev->mutex);
813 
814 	return ret;
815 }
816 
817 static int rtw_ops_get_antenna(struct ieee80211_hw *hw,
818 			       u32 *tx_antenna,
819 			       u32 *rx_antenna)
820 {
821 	struct rtw_dev *rtwdev = hw->priv;
822 	struct rtw_hal *hal = &rtwdev->hal;
823 
824 	*tx_antenna = hal->antenna_tx;
825 	*rx_antenna = hal->antenna_rx;
826 
827 	return 0;
828 }
829 
830 #ifdef CONFIG_PM
831 static int rtw_ops_suspend(struct ieee80211_hw *hw,
832 			   struct cfg80211_wowlan *wowlan)
833 {
834 	struct rtw_dev *rtwdev = hw->priv;
835 	int ret;
836 
837 	mutex_lock(&rtwdev->mutex);
838 	ret = rtw_wow_suspend(rtwdev, wowlan);
839 	if (ret)
840 		rtw_err(rtwdev, "failed to suspend for wow %d\n", ret);
841 	mutex_unlock(&rtwdev->mutex);
842 
843 	return ret ? 1 : 0;
844 }
845 
846 static int rtw_ops_resume(struct ieee80211_hw *hw)
847 {
848 	struct rtw_dev *rtwdev = hw->priv;
849 	int ret;
850 
851 	mutex_lock(&rtwdev->mutex);
852 	ret = rtw_wow_resume(rtwdev);
853 	if (ret)
854 		rtw_err(rtwdev, "failed to resume for wow %d\n", ret);
855 	mutex_unlock(&rtwdev->mutex);
856 
857 	return ret ? 1 : 0;
858 }
859 
860 static void rtw_ops_set_wakeup(struct ieee80211_hw *hw, bool enabled)
861 {
862 	struct rtw_dev *rtwdev = hw->priv;
863 
864 	device_set_wakeup_enable(rtwdev->dev, enabled);
865 }
866 #endif
867 
868 static void rtw_reconfig_complete(struct ieee80211_hw *hw,
869 				  enum ieee80211_reconfig_type reconfig_type)
870 {
871 	struct rtw_dev *rtwdev = hw->priv;
872 
873 	mutex_lock(&rtwdev->mutex);
874 	if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
875 		clear_bit(RTW_FLAG_RESTARTING, rtwdev->flags);
876 	mutex_unlock(&rtwdev->mutex);
877 }
878 
879 static int rtw_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
880 			   struct ieee80211_scan_request *req)
881 {
882 	struct rtw_dev *rtwdev = hw->priv;
883 	int ret;
884 
885 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
886 		return 1;
887 
888 	if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
889 		return -EBUSY;
890 
891 	mutex_lock(&rtwdev->mutex);
892 	rtw_hw_scan_start(rtwdev, vif, req);
893 	ret = rtw_hw_scan_offload(rtwdev, vif, true);
894 	if (ret) {
895 		rtw_hw_scan_abort(rtwdev);
896 		rtw_err(rtwdev, "HW scan failed with status: %d\n", ret);
897 	}
898 	mutex_unlock(&rtwdev->mutex);
899 
900 	return ret;
901 }
902 
903 static void rtw_ops_cancel_hw_scan(struct ieee80211_hw *hw,
904 				   struct ieee80211_vif *vif)
905 {
906 	struct rtw_dev *rtwdev = hw->priv;
907 
908 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
909 		return;
910 
911 	if (!test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
912 		return;
913 
914 	mutex_lock(&rtwdev->mutex);
915 	rtw_hw_scan_abort(rtwdev);
916 	mutex_unlock(&rtwdev->mutex);
917 }
918 
919 static int rtw_ops_set_sar_specs(struct ieee80211_hw *hw,
920 				 const struct cfg80211_sar_specs *sar)
921 {
922 	struct rtw_dev *rtwdev = hw->priv;
923 
924 	mutex_lock(&rtwdev->mutex);
925 	rtw_set_sar_specs(rtwdev, sar);
926 	mutex_unlock(&rtwdev->mutex);
927 
928 	return 0;
929 }
930 
931 static void rtw_ops_sta_rc_update(struct ieee80211_hw *hw,
932 				  struct ieee80211_vif *vif,
933 				  struct ieee80211_sta *sta, u32 changed)
934 {
935 	struct rtw_dev *rtwdev = hw->priv;
936 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
937 
938 	if (changed & IEEE80211_RC_BW_CHANGED)
939 		ieee80211_queue_work(rtwdev->hw, &si->rc_work);
940 }
941 
942 const struct ieee80211_ops rtw_ops = {
943 	.tx			= rtw_ops_tx,
944 	.wake_tx_queue		= rtw_ops_wake_tx_queue,
945 	.start			= rtw_ops_start,
946 	.stop			= rtw_ops_stop,
947 	.config			= rtw_ops_config,
948 	.add_interface		= rtw_ops_add_interface,
949 	.remove_interface	= rtw_ops_remove_interface,
950 	.change_interface	= rtw_ops_change_interface,
951 	.configure_filter	= rtw_ops_configure_filter,
952 	.bss_info_changed	= rtw_ops_bss_info_changed,
953 	.start_ap		= rtw_ops_start_ap,
954 	.stop_ap		= rtw_ops_stop_ap,
955 	.conf_tx		= rtw_ops_conf_tx,
956 	.sta_add		= rtw_ops_sta_add,
957 	.sta_remove		= rtw_ops_sta_remove,
958 	.set_tim		= rtw_ops_set_tim,
959 	.set_key		= rtw_ops_set_key,
960 	.ampdu_action		= rtw_ops_ampdu_action,
961 	.can_aggregate_in_amsdu	= rtw_ops_can_aggregate_in_amsdu,
962 	.sw_scan_start		= rtw_ops_sw_scan_start,
963 	.sw_scan_complete	= rtw_ops_sw_scan_complete,
964 	.mgd_prepare_tx		= rtw_ops_mgd_prepare_tx,
965 	.set_rts_threshold	= rtw_ops_set_rts_threshold,
966 	.sta_statistics		= rtw_ops_sta_statistics,
967 	.flush			= rtw_ops_flush,
968 	.set_bitrate_mask	= rtw_ops_set_bitrate_mask,
969 	.set_antenna		= rtw_ops_set_antenna,
970 	.get_antenna		= rtw_ops_get_antenna,
971 	.reconfig_complete	= rtw_reconfig_complete,
972 	.hw_scan		= rtw_ops_hw_scan,
973 	.cancel_hw_scan		= rtw_ops_cancel_hw_scan,
974 	.sta_rc_update		= rtw_ops_sta_rc_update,
975 	.set_sar_specs          = rtw_ops_set_sar_specs,
976 #ifdef CONFIG_PM
977 	.suspend		= rtw_ops_suspend,
978 	.resume			= rtw_ops_resume,
979 	.set_wakeup		= rtw_ops_set_wakeup,
980 #endif
981 };
982 EXPORT_SYMBOL(rtw_ops);
983