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