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