• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
rtw_ops_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)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 
rtw_ops_wake_tx_queue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)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 
rtw_ops_start(struct ieee80211_hw * hw)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 
rtw_ops_stop(struct ieee80211_hw * hw)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 
rtw_ops_config(struct ieee80211_hw * hw,u32 changed)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_CHANNEL)
92 		rtw_set_channel(rtwdev);
93 
94 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
95 	    (hw->conf.flags & IEEE80211_CONF_IDLE) &&
96 	    !test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
97 		rtw_enter_ips(rtwdev);
98 
99 out:
100 	mutex_unlock(&rtwdev->mutex);
101 	return ret;
102 }
103 
104 static const struct rtw_vif_port rtw_vif_port[] = {
105 	[0] = {
106 		.mac_addr	= {.addr = 0x0610},
107 		.bssid		= {.addr = 0x0618},
108 		.net_type	= {.addr = 0x0100, .mask = 0x30000},
109 		.aid		= {.addr = 0x06a8, .mask = 0x7ff},
110 		.bcn_ctrl	= {.addr = 0x0550, .mask = 0xff},
111 	},
112 	[1] = {
113 		.mac_addr	= {.addr = 0x0700},
114 		.bssid		= {.addr = 0x0708},
115 		.net_type	= {.addr = 0x0100, .mask = 0xc0000},
116 		.aid		= {.addr = 0x0710, .mask = 0x7ff},
117 		.bcn_ctrl	= {.addr = 0x0551, .mask = 0xff},
118 	},
119 	[2] = {
120 		.mac_addr	= {.addr = 0x1620},
121 		.bssid		= {.addr = 0x1628},
122 		.net_type	= {.addr = 0x1100, .mask = 0x3},
123 		.aid		= {.addr = 0x1600, .mask = 0x7ff},
124 		.bcn_ctrl	= {.addr = 0x0578, .mask = 0xff},
125 	},
126 	[3] = {
127 		.mac_addr	= {.addr = 0x1630},
128 		.bssid		= {.addr = 0x1638},
129 		.net_type	= {.addr = 0x1100, .mask = 0xc},
130 		.aid		= {.addr = 0x1604, .mask = 0x7ff},
131 		.bcn_ctrl	= {.addr = 0x0579, .mask = 0xff},
132 	},
133 	[4] = {
134 		.mac_addr	= {.addr = 0x1640},
135 		.bssid		= {.addr = 0x1648},
136 		.net_type	= {.addr = 0x1100, .mask = 0x30},
137 		.aid		= {.addr = 0x1608, .mask = 0x7ff},
138 		.bcn_ctrl	= {.addr = 0x057a, .mask = 0xff},
139 	},
140 };
141 
rtw_ops_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)142 static int rtw_ops_add_interface(struct ieee80211_hw *hw,
143 				 struct ieee80211_vif *vif)
144 {
145 	struct rtw_dev *rtwdev = hw->priv;
146 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
147 	enum rtw_net_type net_type;
148 	u32 config = 0;
149 	u8 port = 0;
150 	u8 bcn_ctrl = 0;
151 
152 	if (rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_BCN_FILTER))
153 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
154 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
155 	rtwvif->port = port;
156 	rtwvif->stats.tx_unicast = 0;
157 	rtwvif->stats.rx_unicast = 0;
158 	rtwvif->stats.tx_cnt = 0;
159 	rtwvif->stats.rx_cnt = 0;
160 	rtwvif->scan_req = NULL;
161 	memset(&rtwvif->bfee, 0, sizeof(struct rtw_bfee));
162 	rtwvif->conf = &rtw_vif_port[port];
163 	rtw_txq_init(rtwdev, vif->txq);
164 	INIT_LIST_HEAD(&rtwvif->rsvd_page_list);
165 
166 	mutex_lock(&rtwdev->mutex);
167 
168 	rtw_leave_lps_deep(rtwdev);
169 
170 	switch (vif->type) {
171 	case NL80211_IFTYPE_AP:
172 	case NL80211_IFTYPE_MESH_POINT:
173 		rtw_add_rsvd_page_bcn(rtwdev, rtwvif);
174 		net_type = RTW_NET_AP_MODE;
175 		bcn_ctrl = BIT_EN_BCN_FUNCTION | BIT_DIS_TSF_UDT;
176 		break;
177 	case NL80211_IFTYPE_ADHOC:
178 		rtw_add_rsvd_page_bcn(rtwdev, rtwvif);
179 		net_type = RTW_NET_AD_HOC;
180 		bcn_ctrl = BIT_EN_BCN_FUNCTION | BIT_DIS_TSF_UDT;
181 		break;
182 	case NL80211_IFTYPE_STATION:
183 		rtw_add_rsvd_page_sta(rtwdev, rtwvif);
184 		net_type = RTW_NET_NO_LINK;
185 		bcn_ctrl = BIT_EN_BCN_FUNCTION;
186 		break;
187 	default:
188 		WARN_ON(1);
189 		mutex_unlock(&rtwdev->mutex);
190 		return -EINVAL;
191 	}
192 
193 	ether_addr_copy(rtwvif->mac_addr, vif->addr);
194 	config |= PORT_SET_MAC_ADDR;
195 	rtwvif->net_type = net_type;
196 	config |= PORT_SET_NET_TYPE;
197 	rtwvif->bcn_ctrl = bcn_ctrl;
198 	config |= PORT_SET_BCN_CTRL;
199 	rtw_vif_port_config(rtwdev, rtwvif, config);
200 	rtw_recalc_lps(rtwdev, vif);
201 
202 	mutex_unlock(&rtwdev->mutex);
203 
204 	rtw_dbg(rtwdev, RTW_DBG_STATE, "start vif %pM on port %d\n", vif->addr, rtwvif->port);
205 	return 0;
206 }
207 
rtw_ops_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)208 static void rtw_ops_remove_interface(struct ieee80211_hw *hw,
209 				     struct ieee80211_vif *vif)
210 {
211 	struct rtw_dev *rtwdev = hw->priv;
212 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
213 	u32 config = 0;
214 
215 	rtw_dbg(rtwdev, RTW_DBG_STATE, "stop vif %pM on port %d\n", vif->addr, rtwvif->port);
216 
217 	mutex_lock(&rtwdev->mutex);
218 
219 	rtw_leave_lps_deep(rtwdev);
220 
221 	rtw_txq_cleanup(rtwdev, vif->txq);
222 	rtw_remove_rsvd_page(rtwdev, rtwvif);
223 
224 	eth_zero_addr(rtwvif->mac_addr);
225 	config |= PORT_SET_MAC_ADDR;
226 	rtwvif->net_type = RTW_NET_NO_LINK;
227 	config |= PORT_SET_NET_TYPE;
228 	rtwvif->bcn_ctrl = 0;
229 	config |= PORT_SET_BCN_CTRL;
230 	rtw_vif_port_config(rtwdev, rtwvif, config);
231 	rtw_recalc_lps(rtwdev, NULL);
232 
233 	mutex_unlock(&rtwdev->mutex);
234 }
235 
rtw_ops_change_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif,enum nl80211_iftype type,bool p2p)236 static int rtw_ops_change_interface(struct ieee80211_hw *hw,
237 				    struct ieee80211_vif *vif,
238 				    enum nl80211_iftype type, bool p2p)
239 {
240 	struct rtw_dev *rtwdev = hw->priv;
241 
242 	rtw_dbg(rtwdev, RTW_DBG_STATE, "change vif %pM (%d)->(%d), p2p (%d)->(%d)\n",
243 		vif->addr, vif->type, type, vif->p2p, p2p);
244 
245 	rtw_ops_remove_interface(hw, vif);
246 
247 	vif->type = type;
248 	vif->p2p = p2p;
249 
250 	return rtw_ops_add_interface(hw, vif);
251 }
252 
rtw_ops_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * new_flags,u64 multicast)253 static void rtw_ops_configure_filter(struct ieee80211_hw *hw,
254 				     unsigned int changed_flags,
255 				     unsigned int *new_flags,
256 				     u64 multicast)
257 {
258 	struct rtw_dev *rtwdev = hw->priv;
259 
260 	*new_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_FCSFAIL |
261 		      FIF_BCN_PRBRESP_PROMISC;
262 
263 	mutex_lock(&rtwdev->mutex);
264 
265 	rtw_leave_lps_deep(rtwdev);
266 
267 	if (changed_flags & FIF_ALLMULTI) {
268 		if (*new_flags & FIF_ALLMULTI)
269 			rtwdev->hal.rcr |= BIT_AM;
270 		else
271 			rtwdev->hal.rcr &= ~(BIT_AM);
272 	}
273 	if (changed_flags & FIF_FCSFAIL) {
274 		if (*new_flags & FIF_FCSFAIL)
275 			rtwdev->hal.rcr |= BIT_ACRC32;
276 		else
277 			rtwdev->hal.rcr &= ~(BIT_ACRC32);
278 	}
279 	if (changed_flags & FIF_OTHER_BSS) {
280 		if (*new_flags & FIF_OTHER_BSS)
281 			rtwdev->hal.rcr |= BIT_AAP;
282 		else
283 			rtwdev->hal.rcr &= ~(BIT_AAP);
284 	}
285 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
286 		if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
287 			rtwdev->hal.rcr &= ~(BIT_CBSSID_BCN | BIT_CBSSID_DATA);
288 		else
289 			rtwdev->hal.rcr |= BIT_CBSSID_BCN;
290 	}
291 
292 	rtw_dbg(rtwdev, RTW_DBG_RX,
293 		"config rx filter, changed=0x%08x, new=0x%08x, rcr=0x%08x\n",
294 		changed_flags, *new_flags, rtwdev->hal.rcr);
295 
296 	rtw_write32(rtwdev, REG_RCR, rtwdev->hal.rcr);
297 
298 	mutex_unlock(&rtwdev->mutex);
299 }
300 
301 /* Only have one group of EDCA parameters now */
302 static const u32 ac_to_edca_param[IEEE80211_NUM_ACS] = {
303 	[IEEE80211_AC_VO] = REG_EDCA_VO_PARAM,
304 	[IEEE80211_AC_VI] = REG_EDCA_VI_PARAM,
305 	[IEEE80211_AC_BE] = REG_EDCA_BE_PARAM,
306 	[IEEE80211_AC_BK] = REG_EDCA_BK_PARAM,
307 };
308 
rtw_aifsn_to_aifs(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,u8 aifsn)309 static u8 rtw_aifsn_to_aifs(struct rtw_dev *rtwdev,
310 			    struct rtw_vif *rtwvif, u8 aifsn)
311 {
312 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
313 	u8 slot_time;
314 	u8 sifs;
315 
316 	slot_time = vif->bss_conf.use_short_slot ? 9 : 20;
317 	sifs = rtwdev->hal.current_band_type == RTW_BAND_5G ? 16 : 10;
318 
319 	return aifsn * slot_time + sifs;
320 }
321 
__rtw_conf_tx(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,u16 ac)322 static void __rtw_conf_tx(struct rtw_dev *rtwdev,
323 			  struct rtw_vif *rtwvif, u16 ac)
324 {
325 	struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac];
326 	u32 edca_param = ac_to_edca_param[ac];
327 	u8 ecw_max, ecw_min;
328 	u8 aifs;
329 
330 	/* 2^ecw - 1 = cw; ecw = log2(cw + 1) */
331 	ecw_max = ilog2(params->cw_max + 1);
332 	ecw_min = ilog2(params->cw_min + 1);
333 	aifs = rtw_aifsn_to_aifs(rtwdev, rtwvif, params->aifs);
334 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_TXOP_LMT, params->txop);
335 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_CWMAX, ecw_max);
336 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_CWMIN, ecw_min);
337 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_AIFS, aifs);
338 }
339 
rtw_conf_tx(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif)340 static void rtw_conf_tx(struct rtw_dev *rtwdev,
341 			struct rtw_vif *rtwvif)
342 {
343 	u16 ac;
344 
345 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
346 		__rtw_conf_tx(rtwdev, rtwvif, ac);
347 }
348 
rtw_ops_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * conf,u64 changed)349 static void rtw_ops_bss_info_changed(struct ieee80211_hw *hw,
350 				     struct ieee80211_vif *vif,
351 				     struct ieee80211_bss_conf *conf,
352 				     u64 changed)
353 {
354 	struct rtw_dev *rtwdev = hw->priv;
355 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
356 	struct rtw_coex *coex = &rtwdev->coex;
357 	struct rtw_coex_stat *coex_stat = &coex->stat;
358 	u32 config = 0;
359 
360 	mutex_lock(&rtwdev->mutex);
361 
362 	rtw_leave_lps_deep(rtwdev);
363 
364 	if (changed & BSS_CHANGED_ASSOC) {
365 		rtw_vif_assoc_changed(rtwvif, conf);
366 		if (vif->cfg.assoc) {
367 			rtw_coex_connect_notify(rtwdev, COEX_ASSOCIATE_FINISH);
368 
369 			rtw_fw_download_rsvd_page(rtwdev);
370 			rtw_send_rsvd_page_h2c(rtwdev);
371 			rtw_coex_media_status_notify(rtwdev, vif->cfg.assoc);
372 			if (rtw_bf_support)
373 				rtw_bf_assoc(rtwdev, vif, conf);
374 		} else {
375 			rtw_leave_lps(rtwdev);
376 			rtw_bf_disassoc(rtwdev, vif, conf);
377 			/* Abort ongoing scan if cancel_scan isn't issued
378 			 * when disconnected by peer
379 			 */
380 			if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
381 				rtw_hw_scan_abort(rtwdev, vif);
382 		}
383 
384 		config |= PORT_SET_NET_TYPE;
385 		config |= PORT_SET_AID;
386 	}
387 
388 	if (changed & BSS_CHANGED_BSSID) {
389 		ether_addr_copy(rtwvif->bssid, conf->bssid);
390 		config |= PORT_SET_BSSID;
391 		if (is_zero_ether_addr(rtwvif->bssid))
392 			rtw_clear_op_chan(rtwdev);
393 		else
394 			rtw_store_op_chan(rtwdev, true);
395 	}
396 
397 	if (changed & BSS_CHANGED_BEACON_INT) {
398 		if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_STATION)
399 			coex_stat->wl_beacon_interval = conf->beacon_int;
400 	}
401 
402 	if (changed & BSS_CHANGED_BEACON) {
403 		rtw_set_dtim_period(rtwdev, conf->dtim_period);
404 		rtw_fw_download_rsvd_page(rtwdev);
405 	}
406 
407 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
408 		if (conf->enable_beacon)
409 			rtw_write32_set(rtwdev, REG_FWHW_TXQ_CTRL,
410 					BIT_EN_BCNQ_DL);
411 		else
412 			rtw_write32_clr(rtwdev, REG_FWHW_TXQ_CTRL,
413 					BIT_EN_BCNQ_DL);
414 	}
415 	if (changed & BSS_CHANGED_CQM)
416 		rtw_fw_beacon_filter_config(rtwdev, true, vif);
417 
418 	if (changed & BSS_CHANGED_MU_GROUPS)
419 		rtw_chip_set_gid_table(rtwdev, vif, conf);
420 
421 	if (changed & BSS_CHANGED_ERP_SLOT)
422 		rtw_conf_tx(rtwdev, rtwvif);
423 
424 	if (changed & BSS_CHANGED_PS)
425 		rtw_recalc_lps(rtwdev, NULL);
426 
427 	rtw_vif_port_config(rtwdev, rtwvif, config);
428 
429 	mutex_unlock(&rtwdev->mutex);
430 }
431 
rtw_ops_start_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)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 	const 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 
rtw_ops_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 ac,const struct ieee80211_tx_queue_params * params)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 
rtw_ops_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)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 
rtw_ops_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)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 
rtw_ops_set_tim(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool set)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 
rtw_ops_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)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 
rtw_ops_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)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 
rtw_ops_can_aggregate_in_amsdu(struct ieee80211_hw * hw,struct sk_buff * head,struct sk_buff * skb)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 
rtw_ops_sw_scan_start(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const u8 * mac_addr)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 
rtw_ops_sw_scan_complete(struct ieee80211_hw * hw,struct ieee80211_vif * vif)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 
rtw_ops_mgd_prepare_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_prep_tx_info * info)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 
rtw_ops_set_rts_threshold(struct ieee80211_hw * hw,u32 value)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 
rtw_ops_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)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 
rtw_ops_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)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 
rtw_ra_mask_info_update_iter(void * data,struct ieee80211_sta * sta)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 
rtw_ra_mask_info_update(struct rtw_dev * rtwdev,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)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 
rtw_ops_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)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 
rtw_ops_set_antenna(struct ieee80211_hw * hw,u32 tx_antenna,u32 rx_antenna)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 	const 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 
rtw_ops_get_antenna(struct ieee80211_hw * hw,u32 * tx_antenna,u32 * rx_antenna)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
rtw_ops_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)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 
rtw_ops_resume(struct ieee80211_hw * hw)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 
rtw_ops_set_wakeup(struct ieee80211_hw * hw,bool enabled)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 
rtw_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)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 
rtw_ops_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * req)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 
rtw_ops_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)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 
rtw_ops_set_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)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 	mutex_lock(&rtwdev->mutex);
876 	rtw_set_sar_specs(rtwdev, sar);
877 	mutex_unlock(&rtwdev->mutex);
878 
879 	return 0;
880 }
881 
rtw_ops_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 changed)882 static void rtw_ops_sta_rc_update(struct ieee80211_hw *hw,
883 				  struct ieee80211_vif *vif,
884 				  struct ieee80211_sta *sta, u32 changed)
885 {
886 	struct rtw_dev *rtwdev = hw->priv;
887 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
888 
889 	if (changed & IEEE80211_RC_BW_CHANGED)
890 		ieee80211_queue_work(rtwdev->hw, &si->rc_work);
891 }
892 
893 const struct ieee80211_ops rtw_ops = {
894 	.tx			= rtw_ops_tx,
895 	.wake_tx_queue		= rtw_ops_wake_tx_queue,
896 	.start			= rtw_ops_start,
897 	.stop			= rtw_ops_stop,
898 	.config			= rtw_ops_config,
899 	.add_interface		= rtw_ops_add_interface,
900 	.remove_interface	= rtw_ops_remove_interface,
901 	.change_interface	= rtw_ops_change_interface,
902 	.configure_filter	= rtw_ops_configure_filter,
903 	.bss_info_changed	= rtw_ops_bss_info_changed,
904 	.start_ap		= rtw_ops_start_ap,
905 	.conf_tx		= rtw_ops_conf_tx,
906 	.sta_add		= rtw_ops_sta_add,
907 	.sta_remove		= rtw_ops_sta_remove,
908 	.set_tim		= rtw_ops_set_tim,
909 	.set_key		= rtw_ops_set_key,
910 	.ampdu_action		= rtw_ops_ampdu_action,
911 	.can_aggregate_in_amsdu	= rtw_ops_can_aggregate_in_amsdu,
912 	.sw_scan_start		= rtw_ops_sw_scan_start,
913 	.sw_scan_complete	= rtw_ops_sw_scan_complete,
914 	.mgd_prepare_tx		= rtw_ops_mgd_prepare_tx,
915 	.set_rts_threshold	= rtw_ops_set_rts_threshold,
916 	.sta_statistics		= rtw_ops_sta_statistics,
917 	.flush			= rtw_ops_flush,
918 	.set_bitrate_mask	= rtw_ops_set_bitrate_mask,
919 	.set_antenna		= rtw_ops_set_antenna,
920 	.get_antenna		= rtw_ops_get_antenna,
921 	.reconfig_complete	= rtw_reconfig_complete,
922 	.hw_scan		= rtw_ops_hw_scan,
923 	.cancel_hw_scan		= rtw_ops_cancel_hw_scan,
924 	.sta_rc_update		= rtw_ops_sta_rc_update,
925 	.set_sar_specs          = rtw_ops_set_sar_specs,
926 #ifdef CONFIG_PM
927 	.suspend		= rtw_ops_suspend,
928 	.resume			= rtw_ops_resume,
929 	.set_wakeup		= rtw_ops_set_wakeup,
930 #endif
931 };
932 EXPORT_SYMBOL(rtw_ops);
933