1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2019-2020 Realtek Corporation
19 static void rtw89_ops_tx(struct ieee80211_hw *hw,
20 struct ieee80211_tx_control *control,
23 struct rtw89_dev *rtwdev = hw->priv;
24 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
25 struct ieee80211_vif *vif = info->control.vif;
26 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
27 struct ieee80211_sta *sta = control->sta;
28 u32 flags = IEEE80211_SKB_CB(skb)->flags;
31 if (rtwvif->offchan && !(flags & IEEE80211_TX_CTL_TX_OFFCHAN) && sta) {
32 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
34 rtw89_debug(rtwdev, RTW89_DBG_TXRX, "ops_tx during offchan\n");
35 skb_queue_tail(&rtwsta->roc_queue, skb);
39 ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, &qsel);
41 rtw89_err(rtwdev, "failed to transmit skb: %d\n", ret);
42 ieee80211_free_txskb(hw, skb);
45 rtw89_core_tx_kick_off(rtwdev, qsel);
48 static void rtw89_ops_wake_tx_queue(struct ieee80211_hw *hw,
49 struct ieee80211_txq *txq)
51 struct rtw89_dev *rtwdev = hw->priv;
53 ieee80211_schedule_txq(hw, txq);
54 queue_work(rtwdev->txq_wq, &rtwdev->txq_work);
57 static int rtw89_ops_start(struct ieee80211_hw *hw)
59 struct rtw89_dev *rtwdev = hw->priv;
62 mutex_lock(&rtwdev->mutex);
63 ret = rtw89_core_start(rtwdev);
64 mutex_unlock(&rtwdev->mutex);
69 static void rtw89_ops_stop(struct ieee80211_hw *hw)
71 struct rtw89_dev *rtwdev = hw->priv;
73 mutex_lock(&rtwdev->mutex);
74 rtw89_core_stop(rtwdev);
75 mutex_unlock(&rtwdev->mutex);
78 static int rtw89_ops_config(struct ieee80211_hw *hw, u32 changed)
80 struct rtw89_dev *rtwdev = hw->priv;
82 /* let previous ips work finish to ensure we don't leave ips twice */
83 cancel_work_sync(&rtwdev->ips_work);
85 mutex_lock(&rtwdev->mutex);
86 rtw89_leave_ps_mode(rtwdev);
88 if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
89 !(hw->conf.flags & IEEE80211_CONF_IDLE))
90 rtw89_leave_ips(rtwdev);
92 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
93 rtw89_config_entity_chandef(rtwdev, RTW89_SUB_ENTITY_0,
95 rtw89_set_channel(rtwdev);
98 if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
99 (hw->conf.flags & IEEE80211_CONF_IDLE) &&
101 rtw89_enter_ips(rtwdev);
103 mutex_unlock(&rtwdev->mutex);
108 static int rtw89_ops_add_interface(struct ieee80211_hw *hw,
109 struct ieee80211_vif *vif)
111 struct rtw89_dev *rtwdev = hw->priv;
112 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
115 rtw89_debug(rtwdev, RTW89_DBG_STATE, "add vif %pM type %d, p2p %d\n",
116 vif->addr, vif->type, vif->p2p);
118 mutex_lock(&rtwdev->mutex);
120 rtw89_leave_ips_by_hwflags(rtwdev);
122 if (RTW89_CHK_FW_FEATURE(BEACON_FILTER, &rtwdev->fw))
123 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
124 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
126 rtwvif->rtwdev = rtwdev;
127 rtwvif->roc.state = RTW89_ROC_IDLE;
128 rtwvif->offchan = false;
129 list_add_tail(&rtwvif->list, &rtwdev->rtwvifs_list);
130 INIT_WORK(&rtwvif->update_beacon_work, rtw89_core_update_beacon_work);
131 INIT_DELAYED_WORK(&rtwvif->roc.roc_work, rtw89_roc_work);
132 rtw89_leave_ps_mode(rtwdev);
134 rtw89_traffic_stats_init(rtwdev, &rtwvif->stats);
135 rtw89_vif_type_mapping(vif, false);
136 rtwvif->port = rtw89_core_acquire_bit_map(rtwdev->hw_port,
138 if (rtwvif->port == RTW89_PORT_NUM) {
140 list_del_init(&rtwvif->list);
144 rtwvif->bcn_hit_cond = 0;
145 rtwvif->mac_idx = RTW89_MAC_0;
146 rtwvif->phy_idx = RTW89_PHY_0;
147 rtwvif->sub_entity_idx = RTW89_SUB_ENTITY_0;
148 rtwvif->hit_rule = 0;
149 rtwvif->reg_6ghz_power = RTW89_REG_6GHZ_POWER_DFLT;
150 ether_addr_copy(rtwvif->mac_addr, vif->addr);
151 INIT_LIST_HEAD(&rtwvif->general_pkt_list);
153 ret = rtw89_mac_add_vif(rtwdev, rtwvif);
155 rtw89_core_release_bit_map(rtwdev->hw_port, rtwvif->port);
156 list_del_init(&rtwvif->list);
160 rtw89_core_txq_init(rtwdev, vif->txq);
162 rtw89_btc_ntfy_role_info(rtwdev, rtwvif, NULL, BTC_ROLE_START);
164 rtw89_recalc_lps(rtwdev);
166 mutex_unlock(&rtwdev->mutex);
171 static void rtw89_ops_remove_interface(struct ieee80211_hw *hw,
172 struct ieee80211_vif *vif)
174 struct rtw89_dev *rtwdev = hw->priv;
175 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
177 rtw89_debug(rtwdev, RTW89_DBG_STATE, "remove vif %pM type %d p2p %d\n",
178 vif->addr, vif->type, vif->p2p);
180 cancel_work_sync(&rtwvif->update_beacon_work);
181 cancel_delayed_work_sync(&rtwvif->roc.roc_work);
183 mutex_lock(&rtwdev->mutex);
184 rtw89_leave_ps_mode(rtwdev);
185 rtw89_btc_ntfy_role_info(rtwdev, rtwvif, NULL, BTC_ROLE_STOP);
186 rtw89_mac_remove_vif(rtwdev, rtwvif);
187 rtw89_core_release_bit_map(rtwdev->hw_port, rtwvif->port);
188 list_del_init(&rtwvif->list);
189 rtw89_recalc_lps(rtwdev);
190 rtw89_enter_ips_by_hwflags(rtwdev);
192 mutex_unlock(&rtwdev->mutex);
195 static int rtw89_ops_change_interface(struct ieee80211_hw *hw,
196 struct ieee80211_vif *vif,
197 enum nl80211_iftype type, bool p2p)
199 struct rtw89_dev *rtwdev = hw->priv;
202 set_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags);
204 rtw89_debug(rtwdev, RTW89_DBG_STATE, "change vif %pM (%d)->(%d), p2p (%d)->(%d)\n",
205 vif->addr, vif->type, type, vif->p2p, p2p);
207 rtw89_ops_remove_interface(hw, vif);
212 ret = rtw89_ops_add_interface(hw, vif);
214 rtw89_warn(rtwdev, "failed to change interface %d\n", ret);
216 clear_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags);
221 static void rtw89_ops_configure_filter(struct ieee80211_hw *hw,
222 unsigned int changed_flags,
223 unsigned int *new_flags,
226 struct rtw89_dev *rtwdev = hw->priv;
227 const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
229 mutex_lock(&rtwdev->mutex);
230 rtw89_leave_ps_mode(rtwdev);
232 *new_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_FCSFAIL |
233 FIF_BCN_PRBRESP_PROMISC | FIF_PROBE_REQ;
235 if (changed_flags & FIF_ALLMULTI) {
236 if (*new_flags & FIF_ALLMULTI)
237 rtwdev->hal.rx_fltr &= ~B_AX_A_MC;
239 rtwdev->hal.rx_fltr |= B_AX_A_MC;
241 if (changed_flags & FIF_FCSFAIL) {
242 if (*new_flags & FIF_FCSFAIL)
243 rtwdev->hal.rx_fltr |= B_AX_A_CRC32_ERR;
245 rtwdev->hal.rx_fltr &= ~B_AX_A_CRC32_ERR;
247 if (changed_flags & FIF_OTHER_BSS) {
248 if (*new_flags & FIF_OTHER_BSS)
249 rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH;
251 rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH;
253 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
254 if (*new_flags & FIF_BCN_PRBRESP_PROMISC) {
255 rtwdev->hal.rx_fltr &= ~B_AX_A_BCN_CHK_EN;
256 rtwdev->hal.rx_fltr &= ~B_AX_A_BC;
257 rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH;
259 rtwdev->hal.rx_fltr |= B_AX_A_BCN_CHK_EN;
260 rtwdev->hal.rx_fltr |= B_AX_A_BC;
261 rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH;
264 if (changed_flags & FIF_PROBE_REQ) {
265 if (*new_flags & FIF_PROBE_REQ) {
266 rtwdev->hal.rx_fltr &= ~B_AX_A_BC_CAM_MATCH;
267 rtwdev->hal.rx_fltr &= ~B_AX_A_UC_CAM_MATCH;
269 rtwdev->hal.rx_fltr |= B_AX_A_BC_CAM_MATCH;
270 rtwdev->hal.rx_fltr |= B_AX_A_UC_CAM_MATCH;
274 rtw89_write32_mask(rtwdev,
275 rtw89_mac_reg_by_idx(rtwdev, mac->rx_fltr, RTW89_MAC_0),
276 B_AX_RX_FLTR_CFG_MASK,
277 rtwdev->hal.rx_fltr);
278 if (!rtwdev->dbcc_en)
280 rtw89_write32_mask(rtwdev,
281 rtw89_mac_reg_by_idx(rtwdev, mac->rx_fltr, RTW89_MAC_1),
282 B_AX_RX_FLTR_CFG_MASK,
283 rtwdev->hal.rx_fltr);
286 mutex_unlock(&rtwdev->mutex);
289 static const u8 ac_to_fw_idx[IEEE80211_NUM_ACS] = {
290 [IEEE80211_AC_VO] = 3,
291 [IEEE80211_AC_VI] = 2,
292 [IEEE80211_AC_BE] = 0,
293 [IEEE80211_AC_BK] = 1,
296 static u8 rtw89_aifsn_to_aifs(struct rtw89_dev *rtwdev,
297 struct rtw89_vif *rtwvif, u8 aifsn)
299 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
300 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
301 rtwvif->sub_entity_idx);
305 slot_time = vif->bss_conf.use_short_slot ? 9 : 20;
306 sifs = chan->band_type == RTW89_BAND_5G ? 16 : 10;
308 return aifsn * slot_time + sifs;
311 static void ____rtw89_conf_tx_edca(struct rtw89_dev *rtwdev,
312 struct rtw89_vif *rtwvif, u16 ac)
314 struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac];
319 /* 2^ecw - 1 = cw; ecw = log2(cw + 1) */
320 ecw_max = ilog2(params->cw_max + 1);
321 ecw_min = ilog2(params->cw_min + 1);
322 aifs = rtw89_aifsn_to_aifs(rtwdev, rtwvif, params->aifs);
323 val = FIELD_PREP(FW_EDCA_PARAM_TXOPLMT_MSK, params->txop) |
324 FIELD_PREP(FW_EDCA_PARAM_CWMAX_MSK, ecw_max) |
325 FIELD_PREP(FW_EDCA_PARAM_CWMIN_MSK, ecw_min) |
326 FIELD_PREP(FW_EDCA_PARAM_AIFS_MSK, aifs);
327 rtw89_fw_h2c_set_edca(rtwdev, rtwvif, ac_to_fw_idx[ac], val);
330 static const u32 ac_to_mu_edca_param[IEEE80211_NUM_ACS] = {
331 [IEEE80211_AC_VO] = R_AX_MUEDCA_VO_PARAM_0,
332 [IEEE80211_AC_VI] = R_AX_MUEDCA_VI_PARAM_0,
333 [IEEE80211_AC_BE] = R_AX_MUEDCA_BE_PARAM_0,
334 [IEEE80211_AC_BK] = R_AX_MUEDCA_BK_PARAM_0,
337 static void ____rtw89_conf_tx_mu_edca(struct rtw89_dev *rtwdev,
338 struct rtw89_vif *rtwvif, u16 ac)
340 struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac];
341 struct ieee80211_he_mu_edca_param_ac_rec *mu_edca;
347 if (!params->mu_edca)
350 mu_edca = ¶ms->mu_edca_param_rec;
351 aifsn = FIELD_GET(GENMASK(3, 0), mu_edca->aifsn);
352 aifs = aifsn ? rtw89_aifsn_to_aifs(rtwdev, rtwvif, aifsn) : 0;
353 timer_32us = mu_edca->mu_edca_timer << 8;
355 val = FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_TIMER_MASK, timer_32us) |
356 FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_CW_MASK, mu_edca->ecw_min_max) |
357 FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_AIFS_MASK, aifs);
358 reg = rtw89_mac_reg_by_idx(rtwdev, ac_to_mu_edca_param[ac], rtwvif->mac_idx);
359 rtw89_write32(rtwdev, reg, val);
361 rtw89_mac_set_hw_muedca_ctrl(rtwdev, rtwvif, true);
364 static void __rtw89_conf_tx(struct rtw89_dev *rtwdev,
365 struct rtw89_vif *rtwvif, u16 ac)
367 ____rtw89_conf_tx_edca(rtwdev, rtwvif, ac);
368 ____rtw89_conf_tx_mu_edca(rtwdev, rtwvif, ac);
371 static void rtw89_conf_tx(struct rtw89_dev *rtwdev,
372 struct rtw89_vif *rtwvif)
376 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
377 __rtw89_conf_tx(rtwdev, rtwvif, ac);
380 static void rtw89_station_mode_sta_assoc(struct rtw89_dev *rtwdev,
381 struct ieee80211_vif *vif,
382 struct ieee80211_bss_conf *conf)
384 struct ieee80211_sta *sta;
386 if (vif->type != NL80211_IFTYPE_STATION)
389 sta = ieee80211_find_sta(vif, conf->bssid);
391 rtw89_err(rtwdev, "can't find sta to set sta_assoc state\n");
395 rtw89_vif_type_mapping(vif, true);
397 rtw89_core_sta_assoc(rtwdev, vif, sta);
400 static void rtw89_ops_bss_info_changed(struct ieee80211_hw *hw,
401 struct ieee80211_vif *vif,
402 struct ieee80211_bss_conf *conf,
405 struct rtw89_dev *rtwdev = hw->priv;
406 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
408 mutex_lock(&rtwdev->mutex);
409 rtw89_leave_ps_mode(rtwdev);
411 if (changed & BSS_CHANGED_ASSOC) {
412 if (vif->cfg.assoc) {
413 rtw89_station_mode_sta_assoc(rtwdev, vif, conf);
414 rtw89_phy_set_bss_color(rtwdev, vif);
415 rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, vif);
416 rtw89_mac_port_update(rtwdev, rtwvif);
417 rtw89_mac_set_he_obss_narrow_bw_ru(rtwdev, vif);
419 rtw89_queue_chanctx_work(rtwdev);
421 /* Abort ongoing scan if cancel_scan isn't issued
422 * when disconnected by peer
424 if (rtwdev->scanning)
425 rtw89_hw_scan_abort(rtwdev, vif);
429 if (changed & BSS_CHANGED_BSSID) {
430 ether_addr_copy(rtwvif->bssid, conf->bssid);
431 rtw89_cam_bssid_changed(rtwdev, rtwvif);
432 rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
435 if (changed & BSS_CHANGED_BEACON)
436 rtw89_fw_h2c_update_beacon(rtwdev, rtwvif);
438 if (changed & BSS_CHANGED_ERP_SLOT)
439 rtw89_conf_tx(rtwdev, rtwvif);
441 if (changed & BSS_CHANGED_HE_BSS_COLOR)
442 rtw89_phy_set_bss_color(rtwdev, vif);
444 if (changed & BSS_CHANGED_MU_GROUPS)
445 rtw89_mac_bf_set_gid_table(rtwdev, vif, conf);
447 if (changed & BSS_CHANGED_P2P_PS)
448 rtw89_process_p2p_ps(rtwdev, vif);
450 if (changed & BSS_CHANGED_CQM)
451 rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, vif, true);
453 if (changed & BSS_CHANGED_PS)
454 rtw89_recalc_lps(rtwdev);
456 mutex_unlock(&rtwdev->mutex);
459 static int rtw89_ops_start_ap(struct ieee80211_hw *hw,
460 struct ieee80211_vif *vif,
461 struct ieee80211_bss_conf *link_conf)
463 struct rtw89_dev *rtwdev = hw->priv;
464 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
465 const struct rtw89_chan *chan;
467 mutex_lock(&rtwdev->mutex);
469 chan = rtw89_chan_get(rtwdev, rtwvif->sub_entity_idx);
470 if (chan->band_type == RTW89_BAND_6G) {
471 mutex_unlock(&rtwdev->mutex);
475 ether_addr_copy(rtwvif->bssid, vif->bss_conf.bssid);
476 rtw89_cam_bssid_changed(rtwdev, rtwvif);
477 rtw89_mac_port_update(rtwdev, rtwvif);
478 rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, NULL);
479 rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, NULL, RTW89_ROLE_TYPE_CHANGE);
480 rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true);
481 rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
482 rtw89_chip_rfk_channel(rtwdev);
484 rtw89_queue_chanctx_work(rtwdev);
485 mutex_unlock(&rtwdev->mutex);
491 void rtw89_ops_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
492 struct ieee80211_bss_conf *link_conf)
494 struct rtw89_dev *rtwdev = hw->priv;
495 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
497 mutex_lock(&rtwdev->mutex);
498 rtw89_mac_stop_ap(rtwdev, rtwvif);
499 rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, NULL);
500 rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true);
501 mutex_unlock(&rtwdev->mutex);
504 static int rtw89_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
507 struct rtw89_dev *rtwdev = hw->priv;
508 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
509 struct rtw89_vif *rtwvif = rtwsta->rtwvif;
511 ieee80211_queue_work(rtwdev->hw, &rtwvif->update_beacon_work);
516 static int rtw89_ops_conf_tx(struct ieee80211_hw *hw,
517 struct ieee80211_vif *vif,
518 unsigned int link_id, u16 ac,
519 const struct ieee80211_tx_queue_params *params)
521 struct rtw89_dev *rtwdev = hw->priv;
522 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
524 mutex_lock(&rtwdev->mutex);
525 rtw89_leave_ps_mode(rtwdev);
526 rtwvif->tx_params[ac] = *params;
527 __rtw89_conf_tx(rtwdev, rtwvif, ac);
528 mutex_unlock(&rtwdev->mutex);
533 static int __rtw89_ops_sta_state(struct ieee80211_hw *hw,
534 struct ieee80211_vif *vif,
535 struct ieee80211_sta *sta,
536 enum ieee80211_sta_state old_state,
537 enum ieee80211_sta_state new_state)
539 struct rtw89_dev *rtwdev = hw->priv;
541 if (old_state == IEEE80211_STA_NOTEXIST &&
542 new_state == IEEE80211_STA_NONE)
543 return rtw89_core_sta_add(rtwdev, vif, sta);
545 if (old_state == IEEE80211_STA_AUTH &&
546 new_state == IEEE80211_STA_ASSOC) {
547 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
548 return 0; /* defer to bss_info_changed to have vif info */
549 return rtw89_core_sta_assoc(rtwdev, vif, sta);
552 if (old_state == IEEE80211_STA_ASSOC &&
553 new_state == IEEE80211_STA_AUTH)
554 return rtw89_core_sta_disassoc(rtwdev, vif, sta);
556 if (old_state == IEEE80211_STA_AUTH &&
557 new_state == IEEE80211_STA_NONE)
558 return rtw89_core_sta_disconnect(rtwdev, vif, sta);
560 if (old_state == IEEE80211_STA_NONE &&
561 new_state == IEEE80211_STA_NOTEXIST)
562 return rtw89_core_sta_remove(rtwdev, vif, sta);
567 static int rtw89_ops_sta_state(struct ieee80211_hw *hw,
568 struct ieee80211_vif *vif,
569 struct ieee80211_sta *sta,
570 enum ieee80211_sta_state old_state,
571 enum ieee80211_sta_state new_state)
573 struct rtw89_dev *rtwdev = hw->priv;
576 mutex_lock(&rtwdev->mutex);
577 rtw89_leave_ps_mode(rtwdev);
578 ret = __rtw89_ops_sta_state(hw, vif, sta, old_state, new_state);
579 mutex_unlock(&rtwdev->mutex);
584 static int rtw89_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
585 struct ieee80211_vif *vif,
586 struct ieee80211_sta *sta,
587 struct ieee80211_key_conf *key)
589 struct rtw89_dev *rtwdev = hw->priv;
592 mutex_lock(&rtwdev->mutex);
593 rtw89_leave_ps_mode(rtwdev);
597 rtw89_btc_ntfy_specific_packet(rtwdev, PACKET_EAPOL_END);
598 ret = rtw89_cam_sec_key_add(rtwdev, vif, sta, key);
599 if (ret && ret != -EOPNOTSUPP) {
600 rtw89_err(rtwdev, "failed to add key to sec cam\n");
605 rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1,
607 rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, false);
608 ret = rtw89_cam_sec_key_del(rtwdev, vif, sta, key, true);
610 rtw89_err(rtwdev, "failed to remove key from sec cam\n");
617 mutex_unlock(&rtwdev->mutex);
622 static int rtw89_ops_ampdu_action(struct ieee80211_hw *hw,
623 struct ieee80211_vif *vif,
624 struct ieee80211_ampdu_params *params)
626 struct rtw89_dev *rtwdev = hw->priv;
627 struct ieee80211_sta *sta = params->sta;
628 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
629 u16 tid = params->tid;
630 struct ieee80211_txq *txq = sta->txq[tid];
631 struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv;
633 switch (params->action) {
634 case IEEE80211_AMPDU_TX_START:
635 return IEEE80211_AMPDU_TX_START_IMMEDIATE;
636 case IEEE80211_AMPDU_TX_STOP_CONT:
637 case IEEE80211_AMPDU_TX_STOP_FLUSH:
638 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
639 mutex_lock(&rtwdev->mutex);
640 clear_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
641 mutex_unlock(&rtwdev->mutex);
642 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
644 case IEEE80211_AMPDU_TX_OPERATIONAL:
645 mutex_lock(&rtwdev->mutex);
646 set_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
647 rtwsta->ampdu_params[tid].agg_num = params->buf_size;
648 rtwsta->ampdu_params[tid].amsdu = params->amsdu;
649 rtw89_leave_ps_mode(rtwdev);
650 mutex_unlock(&rtwdev->mutex);
652 case IEEE80211_AMPDU_RX_START:
653 mutex_lock(&rtwdev->mutex);
654 rtw89_fw_h2c_ba_cam(rtwdev, rtwsta, true, params);
655 mutex_unlock(&rtwdev->mutex);
657 case IEEE80211_AMPDU_RX_STOP:
658 mutex_lock(&rtwdev->mutex);
659 rtw89_fw_h2c_ba_cam(rtwdev, rtwsta, false, params);
660 mutex_unlock(&rtwdev->mutex);
670 static int rtw89_ops_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
672 struct rtw89_dev *rtwdev = hw->priv;
674 mutex_lock(&rtwdev->mutex);
675 rtw89_leave_ps_mode(rtwdev);
676 if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
677 rtw89_mac_update_rts_threshold(rtwdev, RTW89_MAC_0);
678 mutex_unlock(&rtwdev->mutex);
683 static void rtw89_ops_sta_statistics(struct ieee80211_hw *hw,
684 struct ieee80211_vif *vif,
685 struct ieee80211_sta *sta,
686 struct station_info *sinfo)
688 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
690 sinfo->txrate = rtwsta->ra_report.txrate;
691 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
695 void __rtw89_drop_packets(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
697 struct rtw89_vif *rtwvif;
700 rtwvif = (struct rtw89_vif *)vif->drv_priv;
701 rtw89_mac_pkt_drop_vif(rtwdev, rtwvif);
703 rtw89_for_each_rtwvif(rtwdev, rtwvif)
704 rtw89_mac_pkt_drop_vif(rtwdev, rtwvif);
708 static void rtw89_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
709 u32 queues, bool drop)
711 struct rtw89_dev *rtwdev = hw->priv;
713 mutex_lock(&rtwdev->mutex);
714 rtw89_leave_lps(rtwdev);
715 rtw89_hci_flush_queues(rtwdev, queues, drop);
717 if (drop && !RTW89_CHK_FW_FEATURE(NO_PACKET_DROP, &rtwdev->fw))
718 __rtw89_drop_packets(rtwdev, vif);
720 rtw89_mac_flush_txq(rtwdev, queues, drop);
722 mutex_unlock(&rtwdev->mutex);
725 struct rtw89_iter_bitrate_mask_data {
726 struct rtw89_dev *rtwdev;
727 struct ieee80211_vif *vif;
728 const struct cfg80211_bitrate_mask *mask;
731 static void rtw89_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta)
733 struct rtw89_iter_bitrate_mask_data *br_data = data;
734 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
735 struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif);
737 if (vif != br_data->vif || vif->p2p)
740 rtwsta->use_cfg_mask = true;
741 rtwsta->mask = *br_data->mask;
742 rtw89_phy_ra_updata_sta(br_data->rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED);
745 static void rtw89_ra_mask_info_update(struct rtw89_dev *rtwdev,
746 struct ieee80211_vif *vif,
747 const struct cfg80211_bitrate_mask *mask)
749 struct rtw89_iter_bitrate_mask_data br_data = { .rtwdev = rtwdev,
753 ieee80211_iterate_stations_atomic(rtwdev->hw, rtw89_ra_mask_info_update_iter,
757 static int rtw89_ops_set_bitrate_mask(struct ieee80211_hw *hw,
758 struct ieee80211_vif *vif,
759 const struct cfg80211_bitrate_mask *mask)
761 struct rtw89_dev *rtwdev = hw->priv;
763 mutex_lock(&rtwdev->mutex);
764 rtw89_phy_rate_pattern_vif(rtwdev, vif, mask);
765 rtw89_ra_mask_info_update(rtwdev, vif, mask);
766 mutex_unlock(&rtwdev->mutex);
772 int rtw89_ops_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
774 struct rtw89_dev *rtwdev = hw->priv;
775 struct rtw89_hal *hal = &rtwdev->hal;
777 if (hal->ant_diversity) {
778 if (tx_ant != rx_ant || hweight32(tx_ant) != 1)
780 } else if (rx_ant != hw->wiphy->available_antennas_rx && rx_ant != hal->antenna_rx) {
784 mutex_lock(&rtwdev->mutex);
785 hal->antenna_tx = tx_ant;
786 hal->antenna_rx = rx_ant;
787 hal->tx_path_diversity = false;
788 hal->ant_diversity_fixed = true;
789 mutex_unlock(&rtwdev->mutex);
795 int rtw89_ops_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
797 struct rtw89_dev *rtwdev = hw->priv;
798 struct rtw89_hal *hal = &rtwdev->hal;
800 *tx_ant = hal->antenna_tx;
801 *rx_ant = hal->antenna_rx;
806 static void rtw89_ops_sw_scan_start(struct ieee80211_hw *hw,
807 struct ieee80211_vif *vif,
810 struct rtw89_dev *rtwdev = hw->priv;
811 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
813 mutex_lock(&rtwdev->mutex);
814 rtw89_core_scan_start(rtwdev, rtwvif, mac_addr, false);
815 mutex_unlock(&rtwdev->mutex);
818 static void rtw89_ops_sw_scan_complete(struct ieee80211_hw *hw,
819 struct ieee80211_vif *vif)
821 struct rtw89_dev *rtwdev = hw->priv;
823 mutex_lock(&rtwdev->mutex);
824 rtw89_core_scan_complete(rtwdev, vif, false);
825 mutex_unlock(&rtwdev->mutex);
828 static void rtw89_ops_reconfig_complete(struct ieee80211_hw *hw,
829 enum ieee80211_reconfig_type reconfig_type)
831 struct rtw89_dev *rtwdev = hw->priv;
833 if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
834 rtw89_ser_recfg_done(rtwdev);
837 static int rtw89_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
838 struct ieee80211_scan_request *req)
840 struct rtw89_dev *rtwdev = hw->priv;
841 struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif);
844 if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw))
847 if (rtwdev->scanning || rtwvif->offchan)
850 mutex_lock(&rtwdev->mutex);
851 rtw89_hw_scan_start(rtwdev, vif, req);
852 ret = rtw89_hw_scan_offload(rtwdev, vif, true);
854 rtw89_hw_scan_abort(rtwdev, vif);
855 rtw89_err(rtwdev, "HW scan failed with status: %d\n", ret);
857 mutex_unlock(&rtwdev->mutex);
862 static void rtw89_ops_cancel_hw_scan(struct ieee80211_hw *hw,
863 struct ieee80211_vif *vif)
865 struct rtw89_dev *rtwdev = hw->priv;
867 if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw))
870 if (!rtwdev->scanning)
873 mutex_lock(&rtwdev->mutex);
874 rtw89_hw_scan_abort(rtwdev, vif);
875 mutex_unlock(&rtwdev->mutex);
878 static void rtw89_ops_sta_rc_update(struct ieee80211_hw *hw,
879 struct ieee80211_vif *vif,
880 struct ieee80211_sta *sta, u32 changed)
882 struct rtw89_dev *rtwdev = hw->priv;
884 rtw89_phy_ra_updata_sta(rtwdev, sta, changed);
887 static int rtw89_ops_add_chanctx(struct ieee80211_hw *hw,
888 struct ieee80211_chanctx_conf *ctx)
890 struct rtw89_dev *rtwdev = hw->priv;
893 mutex_lock(&rtwdev->mutex);
894 ret = rtw89_chanctx_ops_add(rtwdev, ctx);
895 mutex_unlock(&rtwdev->mutex);
900 static void rtw89_ops_remove_chanctx(struct ieee80211_hw *hw,
901 struct ieee80211_chanctx_conf *ctx)
903 struct rtw89_dev *rtwdev = hw->priv;
905 mutex_lock(&rtwdev->mutex);
906 rtw89_chanctx_ops_remove(rtwdev, ctx);
907 mutex_unlock(&rtwdev->mutex);
910 static void rtw89_ops_change_chanctx(struct ieee80211_hw *hw,
911 struct ieee80211_chanctx_conf *ctx,
914 struct rtw89_dev *rtwdev = hw->priv;
916 mutex_lock(&rtwdev->mutex);
917 rtw89_chanctx_ops_change(rtwdev, ctx, changed);
918 mutex_unlock(&rtwdev->mutex);
921 static int rtw89_ops_assign_vif_chanctx(struct ieee80211_hw *hw,
922 struct ieee80211_vif *vif,
923 struct ieee80211_bss_conf *link_conf,
924 struct ieee80211_chanctx_conf *ctx)
926 struct rtw89_dev *rtwdev = hw->priv;
927 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
930 mutex_lock(&rtwdev->mutex);
931 ret = rtw89_chanctx_ops_assign_vif(rtwdev, rtwvif, ctx);
932 mutex_unlock(&rtwdev->mutex);
937 static void rtw89_ops_unassign_vif_chanctx(struct ieee80211_hw *hw,
938 struct ieee80211_vif *vif,
939 struct ieee80211_bss_conf *link_conf,
940 struct ieee80211_chanctx_conf *ctx)
942 struct rtw89_dev *rtwdev = hw->priv;
943 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
945 mutex_lock(&rtwdev->mutex);
946 rtw89_chanctx_ops_unassign_vif(rtwdev, rtwvif, ctx);
947 mutex_unlock(&rtwdev->mutex);
950 static int rtw89_ops_remain_on_channel(struct ieee80211_hw *hw,
951 struct ieee80211_vif *vif,
952 struct ieee80211_channel *chan,
954 enum ieee80211_roc_type type)
956 struct rtw89_dev *rtwdev = hw->priv;
957 struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif);
958 struct rtw89_roc *roc = &rtwvif->roc;
963 mutex_lock(&rtwdev->mutex);
965 if (roc->state != RTW89_ROC_IDLE) {
966 mutex_unlock(&rtwdev->mutex);
970 if (rtwdev->scanning)
971 rtw89_hw_scan_abort(rtwdev, vif);
973 if (type == IEEE80211_ROC_TYPE_MGMT_TX)
974 roc->state = RTW89_ROC_MGMT;
976 roc->state = RTW89_ROC_NORMAL;
978 roc->duration = duration;
982 rtw89_roc_start(rtwdev, rtwvif);
984 mutex_unlock(&rtwdev->mutex);
989 static int rtw89_ops_cancel_remain_on_channel(struct ieee80211_hw *hw,
990 struct ieee80211_vif *vif)
992 struct rtw89_dev *rtwdev = hw->priv;
993 struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif);
998 cancel_delayed_work_sync(&rtwvif->roc.roc_work);
1000 mutex_lock(&rtwdev->mutex);
1001 rtw89_roc_end(rtwdev, rtwvif);
1002 mutex_unlock(&rtwdev->mutex);
1007 static void rtw89_set_tid_config_iter(void *data, struct ieee80211_sta *sta)
1009 struct cfg80211_tid_config *tid_config = data;
1010 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
1011 struct rtw89_dev *rtwdev = rtwsta->rtwvif->rtwdev;
1013 rtw89_core_set_tid_config(rtwdev, sta, tid_config);
1016 static int rtw89_ops_set_tid_config(struct ieee80211_hw *hw,
1017 struct ieee80211_vif *vif,
1018 struct ieee80211_sta *sta,
1019 struct cfg80211_tid_config *tid_config)
1021 struct rtw89_dev *rtwdev = hw->priv;
1023 mutex_lock(&rtwdev->mutex);
1025 rtw89_core_set_tid_config(rtwdev, sta, tid_config);
1027 ieee80211_iterate_stations_atomic(rtwdev->hw,
1028 rtw89_set_tid_config_iter,
1030 mutex_unlock(&rtwdev->mutex);
1036 static int rtw89_ops_suspend(struct ieee80211_hw *hw,
1037 struct cfg80211_wowlan *wowlan)
1039 struct rtw89_dev *rtwdev = hw->priv;
1042 set_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags);
1043 cancel_delayed_work_sync(&rtwdev->track_work);
1045 mutex_lock(&rtwdev->mutex);
1046 ret = rtw89_wow_suspend(rtwdev, wowlan);
1047 mutex_unlock(&rtwdev->mutex);
1050 rtw89_warn(rtwdev, "failed to suspend for wow %d\n", ret);
1051 clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags);
1058 static int rtw89_ops_resume(struct ieee80211_hw *hw)
1060 struct rtw89_dev *rtwdev = hw->priv;
1063 mutex_lock(&rtwdev->mutex);
1064 ret = rtw89_wow_resume(rtwdev);
1066 rtw89_warn(rtwdev, "failed to resume for wow %d\n", ret);
1067 mutex_unlock(&rtwdev->mutex);
1069 clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags);
1070 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work,
1071 RTW89_TRACK_WORK_PERIOD);
1076 static void rtw89_ops_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1078 struct rtw89_dev *rtwdev = hw->priv;
1080 device_set_wakeup_enable(rtwdev->dev, enabled);
1084 const struct ieee80211_ops rtw89_ops = {
1086 .wake_tx_queue = rtw89_ops_wake_tx_queue,
1087 .start = rtw89_ops_start,
1088 .stop = rtw89_ops_stop,
1089 .config = rtw89_ops_config,
1090 .add_interface = rtw89_ops_add_interface,
1091 .change_interface = rtw89_ops_change_interface,
1092 .remove_interface = rtw89_ops_remove_interface,
1093 .configure_filter = rtw89_ops_configure_filter,
1094 .bss_info_changed = rtw89_ops_bss_info_changed,
1095 .start_ap = rtw89_ops_start_ap,
1096 .stop_ap = rtw89_ops_stop_ap,
1097 .set_tim = rtw89_ops_set_tim,
1098 .conf_tx = rtw89_ops_conf_tx,
1099 .sta_state = rtw89_ops_sta_state,
1100 .set_key = rtw89_ops_set_key,
1101 .ampdu_action = rtw89_ops_ampdu_action,
1102 .set_rts_threshold = rtw89_ops_set_rts_threshold,
1103 .sta_statistics = rtw89_ops_sta_statistics,
1104 .flush = rtw89_ops_flush,
1105 .set_bitrate_mask = rtw89_ops_set_bitrate_mask,
1106 .set_antenna = rtw89_ops_set_antenna,
1107 .get_antenna = rtw89_ops_get_antenna,
1108 .sw_scan_start = rtw89_ops_sw_scan_start,
1109 .sw_scan_complete = rtw89_ops_sw_scan_complete,
1110 .reconfig_complete = rtw89_ops_reconfig_complete,
1111 .hw_scan = rtw89_ops_hw_scan,
1112 .cancel_hw_scan = rtw89_ops_cancel_hw_scan,
1113 .add_chanctx = rtw89_ops_add_chanctx,
1114 .remove_chanctx = rtw89_ops_remove_chanctx,
1115 .change_chanctx = rtw89_ops_change_chanctx,
1116 .assign_vif_chanctx = rtw89_ops_assign_vif_chanctx,
1117 .unassign_vif_chanctx = rtw89_ops_unassign_vif_chanctx,
1118 .remain_on_channel = rtw89_ops_remain_on_channel,
1119 .cancel_remain_on_channel = rtw89_ops_cancel_remain_on_channel,
1120 .set_sar_specs = rtw89_ops_set_sar_specs,
1121 .sta_rc_update = rtw89_ops_sta_rc_update,
1122 .set_tid_config = rtw89_ops_set_tid_config,
1124 .suspend = rtw89_ops_suspend,
1125 .resume = rtw89_ops_resume,
1126 .set_wakeup = rtw89_ops_set_wakeup,
1129 EXPORT_SYMBOL(rtw89_ops);