File Index
Files and directories
Sign in
FILE

main.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/mediatek/mt76/mt7921/main.c C
   1// SPDX-License-Identifier: BSD-3-Clause-Clear
   2/* Copyright (C) 2020 MediaTek Inc. */
   3
   4#include <linux/etherdevice.h>
   5#include <linux/platform_device.h>
   6#include <linux/pci.h>
   7#include <linux/module.h>
   8#include <net/ipv6.h>
   9#include "mt7921.h"
  10#include "mcu.h"
  11
  12static int
  13mt7921_init_he_caps(struct mt792x_phy *phy, enum nl80211_band band,
  14		    struct ieee80211_sband_iftype_data *data)
  15{
  16	int i, idx = 0;
  17	int nss = hweight8(phy->mt76->chainmask);
  18	u16 mcs_map = 0;
  19
  20	for (i = 0; i < 8; i++) {
  21		if (i < nss)
  22			mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
  23		else
  24			mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
  25	}
  26
  27	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
  28		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
  29		struct ieee80211_he_cap_elem *he_cap_elem =
  30				&he_cap->he_cap_elem;
  31		struct ieee80211_he_mcs_nss_supp *he_mcs =
  32				&he_cap->he_mcs_nss_supp;
  33
  34		switch (i) {
  35		case NL80211_IFTYPE_STATION:
  36		case NL80211_IFTYPE_AP:
  37			break;
  38		default:
  39			continue;
  40		}
  41
  42		data[idx].types_mask = BIT(i);
  43		he_cap->has_he = true;
  44
  45		he_cap_elem->mac_cap_info[0] =
  46			IEEE80211_HE_MAC_CAP0_HTC_HE;
  47		he_cap_elem->mac_cap_info[3] =
  48			IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
  49			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
  50		he_cap_elem->mac_cap_info[4] =
  51			IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
  52
  53		if (band == NL80211_BAND_2GHZ)
  54			he_cap_elem->phy_cap_info[0] =
  55				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
  56		else
  57			he_cap_elem->phy_cap_info[0] =
  58				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
  59
  60		he_cap_elem->phy_cap_info[1] =
  61			IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
  62		he_cap_elem->phy_cap_info[2] =
  63			IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
  64			IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
  65			IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
  66			IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
  67			IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
  68
  69		switch (i) {
  70		case NL80211_IFTYPE_AP:
  71			he_cap_elem->mac_cap_info[2] |=
  72				IEEE80211_HE_MAC_CAP2_BSR;
  73			he_cap_elem->mac_cap_info[4] |=
  74				IEEE80211_HE_MAC_CAP4_BQR;
  75			he_cap_elem->mac_cap_info[5] |=
  76				IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX;
  77			he_cap_elem->phy_cap_info[3] |=
  78				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
  79				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
  80			he_cap_elem->phy_cap_info[6] |=
  81				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
  82				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
  83			he_cap_elem->phy_cap_info[9] |=
  84				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
  85				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
  86
  87			if (is_mt7922(phy->mt76->dev)) {
  88				he_cap_elem->phy_cap_info[0] |=
  89					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
  90			}
  91			break;
  92		case NL80211_IFTYPE_STATION:
  93			he_cap_elem->mac_cap_info[1] |=
  94				IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
  95
  96			if (band == NL80211_BAND_2GHZ)
  97				he_cap_elem->phy_cap_info[0] |=
  98					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
  99			else
 100				he_cap_elem->phy_cap_info[0] |=
 101					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
 102
 103			he_cap_elem->phy_cap_info[1] |=
 104				IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
 105				IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
 106			he_cap_elem->phy_cap_info[3] |=
 107				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
 108				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
 109			he_cap_elem->phy_cap_info[4] |=
 110				IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
 111				IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
 112			he_cap_elem->phy_cap_info[5] |=
 113				IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
 114				IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
 115			he_cap_elem->phy_cap_info[6] |=
 116				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
 117				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
 118				IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
 119				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
 120				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
 121			he_cap_elem->phy_cap_info[7] |=
 122				IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
 123				IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
 124			he_cap_elem->phy_cap_info[8] |=
 125				IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
 126				IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
 127			he_cap_elem->phy_cap_info[9] |=
 128				IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
 129				IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
 130				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
 131				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
 132				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
 133				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
 134
 135			if (is_mt7922(phy->mt76->dev)) {
 136				he_cap_elem->phy_cap_info[0] |=
 137					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
 138				he_cap_elem->phy_cap_info[4] |=
 139					IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_4;
 140				he_cap_elem->phy_cap_info[8] |=
 141					IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
 142					IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
 143			}
 144			break;
 145		}
 146
 147		he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
 148		he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
 149		if (is_mt7922(phy->mt76->dev)) {
 150			he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map);
 151			he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map);
 152		}
 153
 154		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
 155		if (he_cap_elem->phy_cap_info[6] &
 156		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
 157			mt76_connac_gen_ppe_thresh(he_cap->ppe_thres, nss, band);
 158		} else {
 159			he_cap_elem->phy_cap_info[9] |=
 160				u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
 161					       IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
 162		}
 163
 164		if (band == NL80211_BAND_6GHZ) {
 165			struct ieee80211_supported_band *sband =
 166				&phy->mt76->sband_5g.sband;
 167			struct ieee80211_sta_vht_cap *vht_cap = &sband->vht_cap;
 168			struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
 169			u32 exp;
 170			u16 cap;
 171
 172			cap = u16_encode_bits(ht_cap->ampdu_density,
 173					IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
 174			exp = u32_get_bits(vht_cap->cap,
 175				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
 176			cap |= u16_encode_bits(exp,
 177					IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
 178			exp = u32_get_bits(vht_cap->cap,
 179					   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
 180			cap |= u16_encode_bits(exp,
 181					IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
 182			if (vht_cap->cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
 183				cap |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
 184			if (vht_cap->cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
 185				cap |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
 186
 187			data[idx].he_6ghz_capa.capa = cpu_to_le16(cap);
 188		}
 189		idx++;
 190	}
 191
 192	return idx;
 193}
 194
 195void mt7921_set_stream_he_caps(struct mt792x_phy *phy)
 196{
 197	struct ieee80211_sband_iftype_data *data;
 198	struct ieee80211_supported_band *band;
 199	int n;
 200
 201	if (phy->mt76->cap.has_2ghz) {
 202		data = phy->iftype[NL80211_BAND_2GHZ];
 203		n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
 204
 205		band = &phy->mt76->sband_2g.sband;
 206		_ieee80211_set_sband_iftype_data(band, data, n);
 207	}
 208
 209	if (phy->mt76->cap.has_5ghz) {
 210		data = phy->iftype[NL80211_BAND_5GHZ];
 211		n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
 212
 213		band = &phy->mt76->sband_5g.sband;
 214		_ieee80211_set_sband_iftype_data(band, data, n);
 215
 216		if (phy->mt76->cap.has_6ghz) {
 217			data = phy->iftype[NL80211_BAND_6GHZ];
 218			n = mt7921_init_he_caps(phy, NL80211_BAND_6GHZ, data);
 219
 220			band = &phy->mt76->sband_6g.sband;
 221			_ieee80211_set_sband_iftype_data(band, data, n);
 222		}
 223	}
 224}
 225
 226int __mt7921_start(struct mt792x_phy *phy)
 227{
 228	struct mt76_phy *mphy = phy->mt76;
 229	int err;
 230
 231	err = mt76_connac_mcu_set_mac_enable(mphy->dev, 0, true, false);
 232	if (err)
 233		return err;
 234
 235	err = mt76_connac_mcu_set_channel_domain(mphy);
 236	if (err)
 237		return err;
 238
 239	err = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
 240	if (err)
 241		return err;
 242
 243	err = mt7921_set_tx_sar_pwr(mphy->hw, NULL);
 244	if (err)
 245		return err;
 246
 247	mt792x_mac_reset_counters(phy);
 248	set_bit(MT76_STATE_RUNNING, &mphy->state);
 249
 250	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
 251				     MT792x_WATCHDOG_TIME);
 252	if (mt76_is_mmio(mphy->dev)) {
 253		err = mt7921_mcu_radio_led_ctrl(phy->dev, EXT_CMD_RADIO_LED_CTRL_ENABLE);
 254		if (err)
 255			return err;
 256
 257		err = mt7921_mcu_radio_led_ctrl(phy->dev, EXT_CMD_RADIO_ON_LED);
 258		if (err)
 259			return err;
 260	}
 261
 262	if (phy->chip_cap & MT792x_CHIP_CAP_WF_RF_PIN_CTRL_EVT_EN) {
 263		mt7921_mcu_wf_rf_pin_ctrl(phy, WF_RF_PIN_INIT);
 264		wiphy_rfkill_start_polling(mphy->hw->wiphy);
 265	}
 266
 267	return 0;
 268}
 269EXPORT_SYMBOL_GPL(__mt7921_start);
 270
 271static int mt7921_start(struct ieee80211_hw *hw)
 272{
 273	struct mt792x_phy *phy = mt792x_hw_phy(hw);
 274	int err;
 275
 276	mt792x_mutex_acquire(phy->dev);
 277	err = __mt7921_start(phy);
 278	mt792x_mutex_release(phy->dev);
 279
 280	return err;
 281}
 282
 283static void mt7921_stop(struct ieee80211_hw *hw, bool suspend)
 284{
 285	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 286	int err = 0;
 287
 288	if (mt76_is_mmio(&dev->mt76)) {
 289		mt792x_mutex_acquire(dev);
 290		err = mt7921_mcu_radio_led_ctrl(dev, EXT_CMD_RADIO_OFF_LED);
 291		mt792x_mutex_release(dev);
 292		if (err)
 293			return;
 294	}
 295
 296	mt792x_stop(hw, false);
 297}
 298
 299static int
 300mt7921_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 301{
 302	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 303	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 304	struct mt792x_phy *phy = mt792x_hw_phy(hw);
 305	struct mt76_txq *mtxq;
 306	int idx, ret = 0;
 307
 308	mt792x_mutex_acquire(dev);
 309
 310	mvif->bss_conf.mt76.idx = __ffs64(~dev->mt76.vif_mask);
 311	if (mvif->bss_conf.mt76.idx >= MT792x_MAX_INTERFACES) {
 312		ret = -ENOSPC;
 313		goto out;
 314	}
 315
 316	mvif->bss_conf.mt76.omac_idx = mvif->bss_conf.mt76.idx;
 317	mvif->phy = phy;
 318	mvif->bss_conf.vif = mvif;
 319	mvif->bss_conf.mt76.band_idx = 0;
 320	mvif->bss_conf.mt76.wmm_idx = mvif->bss_conf.mt76.idx % MT76_CONNAC_MAX_WMM_SETS;
 321
 322	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, &vif->bss_conf,
 323					  &mvif->bss_conf.mt76,
 324					  &mvif->sta.deflink.wcid, true);
 325	if (ret)
 326		goto out;
 327
 328	dev->mt76.vif_mask |= BIT_ULL(mvif->bss_conf.mt76.idx);
 329	phy->omac_mask |= BIT_ULL(mvif->bss_conf.mt76.omac_idx);
 330
 331	idx = MT792x_WTBL_RESERVED - mvif->bss_conf.mt76.idx;
 332
 333	INIT_LIST_HEAD(&mvif->sta.deflink.wcid.poll_list);
 334	mvif->sta.deflink.wcid.idx = idx;
 335	mvif->sta.deflink.wcid.tx_info |= MT_WCID_TX_INFO_SET;
 336	mt76_wcid_init(&mvif->sta.deflink.wcid, mvif->bss_conf.mt76.band_idx);
 337
 338	mt7921_mac_wtbl_update(dev, idx,
 339			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
 340
 341	ewma_rssi_init(&mvif->bss_conf.rssi);
 342
 343	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.deflink.wcid);
 344	if (vif->txq) {
 345		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
 346		mtxq->wcid = idx;
 347	}
 348
 349	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
 350	if (phy->chip_cap & MT792x_CHIP_CAP_RSSI_NOTIFY_EVT_EN)
 351		vif->driver_flags |= IEEE80211_VIF_SUPPORTS_CQM_RSSI;
 352
 353	INIT_WORK(&mvif->csa_work, mt7921_csa_work);
 354	timer_setup(&mvif->csa_timer, mt792x_csa_timer, 0);
 355out:
 356	mt792x_mutex_release(dev);
 357
 358	return ret;
 359}
 360
 361static void mt7921_roc_iter(void *priv, u8 *mac,
 362			    struct ieee80211_vif *vif)
 363{
 364	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 365	struct mt792x_phy *phy = priv;
 366
 367	mt7921_mcu_abort_roc(phy, mvif, phy->roc_token_id);
 368}
 369
 370void mt7921_roc_abort_sync(struct mt792x_dev *dev)
 371{
 372	struct mt792x_phy *phy = &dev->phy;
 373
 374	timer_delete_sync(&phy->roc_timer);
 375	cancel_work_sync(&phy->roc_work);
 376	if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
 377		ieee80211_iterate_interfaces(mt76_hw(dev),
 378					     IEEE80211_IFACE_ITER_RESUME_ALL,
 379					     mt7921_roc_iter, (void *)phy);
 380}
 381EXPORT_SYMBOL_GPL(mt7921_roc_abort_sync);
 382
 383void mt7921_roc_work(struct work_struct *work)
 384{
 385	struct mt792x_phy *phy;
 386
 387	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
 388						roc_work);
 389
 390	if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
 391		return;
 392
 393	mt792x_mutex_acquire(phy->dev);
 394	ieee80211_iterate_active_interfaces(phy->mt76->hw,
 395					    IEEE80211_IFACE_ITER_RESUME_ALL,
 396					    mt7921_roc_iter, phy);
 397	mt792x_mutex_release(phy->dev);
 398	ieee80211_remain_on_channel_expired(phy->mt76->hw);
 399}
 400
 401static int mt7921_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif)
 402{
 403	int err = 0;
 404
 405	timer_delete_sync(&phy->roc_timer);
 406	cancel_work_sync(&phy->roc_work);
 407
 408	mt792x_mutex_acquire(phy->dev);
 409	if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
 410		err = mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id);
 411	mt792x_mutex_release(phy->dev);
 412
 413	return err;
 414}
 415
 416static int mt7921_set_roc(struct mt792x_phy *phy,
 417			  struct mt792x_vif *vif,
 418			  struct ieee80211_channel *chan,
 419			  int duration,
 420			  enum mt7921_roc_req type)
 421{
 422	int err;
 423
 424	if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
 425		return -EBUSY;
 426
 427	phy->roc_grant = false;
 428
 429	err = mt7921_mcu_set_roc(phy, vif, chan, duration, type,
 430				 ++phy->roc_token_id);
 431	if (err < 0) {
 432		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
 433		goto out;
 434	}
 435
 436	if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
 437		mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id);
 438		clear_bit(MT76_STATE_ROC, &phy->mt76->state);
 439		err = -ETIMEDOUT;
 440	}
 441
 442out:
 443	return err;
 444}
 445
 446static int mt7921_remain_on_channel(struct ieee80211_hw *hw,
 447				    struct ieee80211_vif *vif,
 448				    struct ieee80211_channel *chan,
 449				    int duration,
 450				    enum ieee80211_roc_type type)
 451{
 452	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 453	struct mt792x_phy *phy = mt792x_hw_phy(hw);
 454	int err;
 455
 456	mt792x_mutex_acquire(phy->dev);
 457	err = mt7921_set_roc(phy, mvif, chan, duration, MT7921_ROC_REQ_ROC);
 458	mt792x_mutex_release(phy->dev);
 459
 460	return err;
 461}
 462
 463static int mt7921_cancel_remain_on_channel(struct ieee80211_hw *hw,
 464					   struct ieee80211_vif *vif)
 465{
 466	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 467	struct mt792x_phy *phy = mt792x_hw_phy(hw);
 468
 469	return mt7921_abort_roc(phy, mvif);
 470}
 471
 472int mt7921_set_channel(struct mt76_phy *mphy)
 473{
 474	struct mt792x_phy *phy = mphy->priv;
 475	struct mt792x_dev *dev = phy->dev;
 476	int ret;
 477
 478	mt76_connac_pm_wake(mphy, &dev->pm);
 479	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
 480	if (ret)
 481		goto out;
 482
 483	mt792x_mac_set_timeing(phy);
 484	mt792x_mac_reset_counters(phy);
 485	phy->noise = 0;
 486
 487out:
 488	mt76_connac_power_save_sched(mphy, &dev->pm);
 489
 490	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
 491				     MT792x_WATCHDOG_TIME);
 492
 493	return ret;
 494}
 495EXPORT_SYMBOL_GPL(mt7921_set_channel);
 496
 497static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
 498			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
 499			  struct ieee80211_key_conf *key)
 500{
 501	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 502	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 503	struct mt792x_sta *msta = sta ? (struct mt792x_sta *)sta->drv_priv :
 504				  &mvif->sta;
 505	struct mt76_wcid *wcid = &msta->deflink.wcid;
 506	u8 *wcid_keyidx = &wcid->hw_key_idx;
 507	int idx = key->keyidx, err = 0;
 508
 509	/* The hardware does not support per-STA RX GTK, fallback
 510	 * to software mode for these.
 511	 */
 512	if ((vif->type == NL80211_IFTYPE_ADHOC ||
 513	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
 514	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
 515	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
 516	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
 517		return -EOPNOTSUPP;
 518
 519	/* fall back to sw encryption for unsupported ciphers */
 520	switch (key->cipher) {
 521	case WLAN_CIPHER_SUITE_AES_CMAC:
 522		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
 523		wcid_keyidx = &wcid->hw_key_idx2;
 524		break;
 525	case WLAN_CIPHER_SUITE_WEP40:
 526	case WLAN_CIPHER_SUITE_WEP104:
 527		if (!mvif->wep_sta)
 528			return -EOPNOTSUPP;
 529		break;
 530	case WLAN_CIPHER_SUITE_TKIP:
 531	case WLAN_CIPHER_SUITE_CCMP:
 532	case WLAN_CIPHER_SUITE_CCMP_256:
 533	case WLAN_CIPHER_SUITE_GCMP:
 534	case WLAN_CIPHER_SUITE_GCMP_256:
 535	case WLAN_CIPHER_SUITE_SMS4:
 536		break;
 537	default:
 538		return -EOPNOTSUPP;
 539	}
 540
 541	mt792x_mutex_acquire(dev);
 542
 543	if (cmd == SET_KEY) {
 544		*wcid_keyidx = idx;
 545	} else {
 546		if (idx == *wcid_keyidx)
 547			*wcid_keyidx = -1;
 548
 549		/* For security issue we don't trigger the key deletion when
 550		 * reassociating. But we should trigger the deletion process
 551		 * to avoid using incorrect cipher after disconnection,
 552		 */
 553		if (vif->type != NL80211_IFTYPE_STATION || vif->cfg.assoc)
 554			goto out;
 555	}
 556
 557	mt76_wcid_key_setup(&dev->mt76, wcid, key);
 558	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->deflink.bip,
 559				      key, MCU_UNI_CMD(STA_REC_UPDATE),
 560				      &msta->deflink.wcid, cmd);
 561	if (err)
 562		goto out;
 563
 564	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
 565	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
 566		err = mt76_connac_mcu_add_key(&dev->mt76, vif,
 567					      &mvif->wep_sta->deflink.bip,
 568					      key, MCU_UNI_CMD(STA_REC_UPDATE),
 569					      &mvif->wep_sta->deflink.wcid, cmd);
 570out:
 571	mt792x_mutex_release(dev);
 572
 573	return err;
 574}
 575
 576static void
 577mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
 578{
 579	struct mt792x_dev *dev = priv;
 580	struct ieee80211_hw *hw = mt76_hw(dev);
 581	bool pm_enable = dev->pm.enable;
 582	int err;
 583
 584	err = mt7921_mcu_set_beacon_filter(dev, vif, pm_enable);
 585	if (err < 0)
 586		return;
 587
 588	if (pm_enable) {
 589		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
 590		ieee80211_hw_set(hw, CONNECTION_MONITOR);
 591	} else {
 592		vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
 593		__clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
 594	}
 595}
 596
 597static void
 598mt7921_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
 599{
 600	struct mt792x_dev *dev = priv;
 601	struct ieee80211_hw *hw = mt76_hw(dev);
 602	struct mt76_connac_pm *pm = &dev->pm;
 603	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
 604
 605	mt7921_mcu_set_sniffer(dev, vif, monitor);
 606	pm->enable = pm->enable_user && !monitor;
 607	pm->ds_enable = pm->ds_enable_user && !monitor;
 608
 609	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
 610
 611	if (monitor)
 612		mt7921_mcu_set_beacon_filter(dev, vif, false);
 613}
 614
 615void mt7921_set_runtime_pm(struct mt792x_dev *dev)
 616{
 617	struct ieee80211_hw *hw = mt76_hw(dev);
 618	struct mt76_connac_pm *pm = &dev->pm;
 619	bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
 620
 621	pm->enable = pm->enable_user && !monitor;
 622	ieee80211_iterate_active_interfaces(hw,
 623					    IEEE80211_IFACE_ITER_RESUME_ALL,
 624					    mt7921_pm_interface_iter, dev);
 625	pm->ds_enable = pm->ds_enable_user && !monitor;
 626	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
 627}
 628
 629static int mt7921_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
 630{
 631	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 632	struct mt792x_phy *phy = mt792x_hw_phy(hw);
 633	int ret = 0;
 634
 635	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
 636		ret = mt76_update_channel(phy->mt76);
 637		if (ret)
 638			return ret;
 639	}
 640
 641	mt792x_mutex_acquire(dev);
 642
 643	if (changed & IEEE80211_CONF_CHANGE_POWER) {
 644		ret = mt7921_set_tx_sar_pwr(hw, NULL);
 645		if (ret)
 646			goto out;
 647	}
 648
 649	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
 650		ieee80211_iterate_active_interfaces(hw,
 651						    IEEE80211_IFACE_ITER_RESUME_ALL,
 652						    mt7921_sniffer_interface_iter, dev);
 653	}
 654
 655out:
 656	mt792x_mutex_release(dev);
 657
 658	return ret;
 659}
 660
 661static void mt7921_configure_filter(struct ieee80211_hw *hw,
 662				    unsigned int changed_flags,
 663				    unsigned int *total_flags,
 664				    u64 multicast)
 665{
 666#define MT7921_FILTER_FCSFAIL    BIT(2)
 667#define MT7921_FILTER_CONTROL    BIT(5)
 668#define MT7921_FILTER_OTHER_BSS  BIT(6)
 669#define MT7921_FILTER_ENABLE     BIT(31)
 670
 671	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 672	u32 flags = MT7921_FILTER_ENABLE;
 673
 674#define MT7921_FILTER(_fif, _type) do {			\
 675		if (*total_flags & (_fif))		\
 676			flags |= MT7921_FILTER_##_type;	\
 677	} while (0)
 678
 679	MT7921_FILTER(FIF_FCSFAIL, FCSFAIL);
 680	MT7921_FILTER(FIF_CONTROL, CONTROL);
 681	MT7921_FILTER(FIF_OTHER_BSS, OTHER_BSS);
 682
 683	mt792x_mutex_acquire(dev);
 684	mt7921_mcu_set_rxfilter(dev, flags, 0, 0);
 685	mt792x_mutex_release(dev);
 686
 687	*total_flags &= (FIF_OTHER_BSS | FIF_FCSFAIL | FIF_CONTROL);
 688}
 689
 690static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
 691				    struct ieee80211_vif *vif,
 692				    struct ieee80211_bss_conf *info,
 693				    u64 changed)
 694{
 695	struct mt792x_phy *phy = mt792x_hw_phy(hw);
 696	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 697
 698	mt792x_mutex_acquire(dev);
 699
 700	if (changed & BSS_CHANGED_ERP_SLOT) {
 701		int slottime = info->use_short_slot ? 9 : 20;
 702
 703		if (slottime != phy->slottime) {
 704			phy->slottime = slottime;
 705			mt792x_mac_set_timeing(phy);
 706		}
 707	}
 708
 709	if (changed & (BSS_CHANGED_BEACON |
 710		       BSS_CHANGED_BEACON_ENABLED))
 711		mt7921_mcu_uni_add_beacon_offload(dev, hw, vif,
 712						  info->enable_beacon);
 713
 714	/* ensure that enable txcmd_mode after bss_info */
 715	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
 716		mt7921_mcu_set_tx(dev, vif);
 717
 718	if (changed & BSS_CHANGED_PS)
 719		mt7921_mcu_uni_bss_ps(dev, vif);
 720
 721	if (changed & BSS_CHANGED_CQM)
 722		mt7921_mcu_set_rssimonitor(dev, vif);
 723
 724	if (changed & BSS_CHANGED_ASSOC) {
 725		mt7921_mcu_sta_update(dev, NULL, vif, true,
 726				      MT76_STA_INFO_STATE_ASSOC);
 727		mt7921_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc);
 728	}
 729
 730	if (changed & BSS_CHANGED_ARP_FILTER) {
 731		struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 732
 733		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->bss_conf.mt76,
 734						  info);
 735	}
 736
 737	mt792x_mutex_release(dev);
 738}
 739
 740static void
 741mt7921_calc_vif_num(void *priv, u8 *mac, struct ieee80211_vif *vif)
 742{
 743	u32 *num = priv;
 744
 745	if (!priv)
 746		return;
 747
 748	switch (vif->type) {
 749	case NL80211_IFTYPE_STATION:
 750	case NL80211_IFTYPE_P2P_CLIENT:
 751	case NL80211_IFTYPE_AP:
 752	case NL80211_IFTYPE_P2P_GO:
 753		*num += 1;
 754		break;
 755	default:
 756		break;
 757	}
 758}
 759
 760static void
 761mt7921_regd_set_6ghz_power_type(struct ieee80211_vif *vif, bool is_add)
 762{
 763	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 764	struct mt792x_phy *phy = mvif->phy;
 765	struct mt792x_dev *dev = phy->dev;
 766	u32 valid_vif_num = 0;
 767
 768	ieee80211_iterate_active_interfaces(mt76_hw(dev),
 769					    IEEE80211_IFACE_ITER_RESUME_ALL,
 770					    mt7921_calc_vif_num, &valid_vif_num);
 771
 772	if (valid_vif_num > 1) {
 773		phy->power_type = MT_AP_DEFAULT;
 774		goto out;
 775	}
 776
 777	if (!is_add)
 778		vif->bss_conf.power_type = IEEE80211_REG_UNSET_AP;
 779
 780	switch (vif->bss_conf.power_type) {
 781	case IEEE80211_REG_SP_AP:
 782		phy->power_type = MT_AP_SP;
 783		break;
 784	case IEEE80211_REG_VLP_AP:
 785		phy->power_type = MT_AP_VLP;
 786		break;
 787	case IEEE80211_REG_LPI_AP:
 788		phy->power_type = MT_AP_LPI;
 789		break;
 790	case IEEE80211_REG_UNSET_AP:
 791		phy->power_type = MT_AP_UNSET;
 792		break;
 793	default:
 794		phy->power_type = MT_AP_DEFAULT;
 795		break;
 796	}
 797
 798out:
 799	mt7921_mcu_set_clc(dev, dev->mt76.alpha2, dev->country_ie_env);
 800}
 801
 802int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
 803		       struct ieee80211_sta *sta)
 804{
 805	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
 806	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
 807	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 808	int ret, idx;
 809
 810	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT792x_WTBL_STA - 1);
 811	if (idx < 0)
 812		return -ENOSPC;
 813
 814	INIT_LIST_HEAD(&msta->deflink.wcid.poll_list);
 815	msta->vif = mvif;
 816	msta->deflink.wcid.sta = 1;
 817	msta->deflink.wcid.idx = idx;
 818	msta->deflink.wcid.phy_idx = mvif->bss_conf.mt76.band_idx;
 819	msta->deflink.wcid.tx_info |= MT_WCID_TX_INFO_SET;
 820	msta->deflink.last_txs = jiffies;
 821	msta->deflink.sta = msta;
 822
 823	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
 824	if (ret)
 825		return ret;
 826
 827	if (vif->type == NL80211_IFTYPE_STATION)
 828		mvif->wep_sta = msta;
 829
 830	mt7921_mac_wtbl_update(dev, idx,
 831			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
 832
 833	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
 834				    MT76_STA_INFO_STATE_NONE);
 835	if (ret)
 836		return ret;
 837
 838	mt7921_regd_set_6ghz_power_type(vif, true);
 839
 840	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
 841
 842	return 0;
 843}
 844EXPORT_SYMBOL_GPL(mt7921_mac_sta_add);
 845
 846int mt7921_mac_sta_event(struct mt76_dev *mdev, struct ieee80211_vif *vif,
 847			 struct ieee80211_sta *sta, enum mt76_sta_event ev)
 848{
 849	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
 850	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
 851	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 852
 853	if (ev != MT76_STA_EVENT_ASSOC)
 854	    return 0;
 855
 856	mt792x_mutex_acquire(dev);
 857
 858	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
 859		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.deflink.wcid,
 860					    true, mvif->bss_conf.mt76.ctx);
 861
 862	ewma_avg_signal_init(&msta->deflink.avg_ack_signal);
 863
 864	mt7921_mac_wtbl_update(dev, msta->deflink.wcid.idx,
 865			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
 866	memset(msta->deflink.airtime_ac, 0, sizeof(msta->deflink.airtime_ac));
 867
 868	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
 869
 870	mt792x_mutex_release(dev);
 871
 872	return 0;
 873}
 874EXPORT_SYMBOL_GPL(mt7921_mac_sta_event);
 875
 876void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
 877			   struct ieee80211_sta *sta)
 878{
 879	struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
 880	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
 881
 882	mt7921_roc_abort_sync(dev);
 883	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->deflink.wcid);
 884	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
 885
 886	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
 887	mt7921_mac_wtbl_update(dev, msta->deflink.wcid.idx,
 888			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
 889
 890	if (vif->type == NL80211_IFTYPE_STATION) {
 891		struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 892
 893		mvif->wep_sta = NULL;
 894		ewma_rssi_init(&mvif->bss_conf.rssi);
 895		if (!sta->tdls)
 896			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
 897						    &mvif->sta.deflink.wcid, false,
 898						    mvif->bss_conf.mt76.ctx);
 899	}
 900
 901	spin_lock_bh(&dev->mt76.sta_poll_lock);
 902	if (!list_empty(&msta->deflink.wcid.poll_list))
 903		list_del_init(&msta->deflink.wcid.poll_list);
 904	spin_unlock_bh(&dev->mt76.sta_poll_lock);
 905
 906	mt7921_regd_set_6ghz_power_type(vif, false);
 907
 908	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
 909}
 910EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove);
 911
 912static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
 913				    u32 val)
 914{
 915	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 916
 917	mt792x_mutex_acquire(dev);
 918	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
 919	mt792x_mutex_release(dev);
 920
 921	return 0;
 922}
 923
 924static int
 925mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 926		    struct ieee80211_ampdu_params *params)
 927{
 928	enum ieee80211_ampdu_mlme_action action = params->action;
 929	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 930	struct ieee80211_sta *sta = params->sta;
 931	struct ieee80211_txq *txq = sta->txq[params->tid];
 932	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
 933	u16 tid = params->tid;
 934	u16 ssn = params->ssn;
 935	struct mt76_txq *mtxq;
 936	int ret = 0;
 937
 938	if (!txq)
 939		return -EINVAL;
 940
 941	mtxq = (struct mt76_txq *)txq->drv_priv;
 942
 943	mt792x_mutex_acquire(dev);
 944	switch (action) {
 945	case IEEE80211_AMPDU_RX_START:
 946		mt76_rx_aggr_start(&dev->mt76, &msta->deflink.wcid, tid, ssn,
 947				   params->buf_size);
 948		mt7921_mcu_uni_rx_ba(dev, params, true);
 949		break;
 950	case IEEE80211_AMPDU_RX_STOP:
 951		mt76_rx_aggr_stop(&dev->mt76, &msta->deflink.wcid, tid);
 952		mt7921_mcu_uni_rx_ba(dev, params, false);
 953		break;
 954	case IEEE80211_AMPDU_TX_OPERATIONAL:
 955		mtxq->aggr = true;
 956		mtxq->send_bar = false;
 957		mt7921_mcu_uni_tx_ba(dev, params, true);
 958		break;
 959	case IEEE80211_AMPDU_TX_STOP_FLUSH:
 960	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
 961		mtxq->aggr = false;
 962		clear_bit(tid, &msta->deflink.wcid.ampdu_state);
 963		mt7921_mcu_uni_tx_ba(dev, params, false);
 964		break;
 965	case IEEE80211_AMPDU_TX_START:
 966		set_bit(tid, &msta->deflink.wcid.ampdu_state);
 967		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
 968		break;
 969	case IEEE80211_AMPDU_TX_STOP_CONT:
 970		mtxq->aggr = false;
 971		clear_bit(tid, &msta->deflink.wcid.ampdu_state);
 972		mt7921_mcu_uni_tx_ba(dev, params, false);
 973		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
 974		break;
 975	}
 976	mt792x_mutex_release(dev);
 977
 978	return ret;
 979}
 980
 981static int mt7921_sta_state(struct ieee80211_hw *hw,
 982			    struct ieee80211_vif *vif,
 983			    struct ieee80211_sta *sta,
 984			    enum ieee80211_sta_state old_state,
 985			    enum ieee80211_sta_state new_state)
 986{
 987	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 988
 989	if (dev->pm.ds_enable) {
 990		mt792x_mutex_acquire(dev);
 991		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
 992		mt792x_mutex_release(dev);
 993	}
 994
 995	return mt76_sta_state(hw, vif, sta, old_state, new_state);
 996}
 997
 998void mt7921_scan_work(struct work_struct *work)
 999{
1000	struct mt792x_phy *phy;
1001
1002	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
1003						scan_work.work);
1004
1005	while (true) {
1006		struct mt76_connac2_mcu_rxd *rxd;
1007		struct sk_buff *skb;
1008
1009		spin_lock_bh(&phy->dev->mt76.lock);
1010		skb = __skb_dequeue(&phy->scan_event_list);
1011		spin_unlock_bh(&phy->dev->mt76.lock);
1012
1013		if (!skb)
1014			break;
1015
1016		rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
1017		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1018			ieee80211_sched_scan_results(phy->mt76->hw);
1019		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1020					      &phy->mt76->state)) {
1021			struct cfg80211_scan_info info = {
1022				.aborted = false,
1023			};
1024
1025			ieee80211_scan_completed(phy->mt76->hw, &info);
1026		}
1027		dev_kfree_skb(skb);
1028	}
1029}
1030
1031static int
1032mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1033	       struct ieee80211_scan_request *req)
1034{
1035	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1036	struct mt76_phy *mphy = hw->priv;
1037	int err;
1038
1039	mt792x_mutex_acquire(dev);
1040	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1041	mt792x_mutex_release(dev);
1042
1043	return err;
1044}
1045
1046static void
1047mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1048{
1049	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1050	struct mt76_phy *mphy = hw->priv;
1051
1052	mt792x_mutex_acquire(dev);
1053	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1054	mt792x_mutex_release(dev);
1055}
1056
1057static int
1058mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1059			struct cfg80211_sched_scan_request *req,
1060			struct ieee80211_scan_ies *ies)
1061{
1062	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1063	struct mt76_phy *mphy = hw->priv;
1064	int err;
1065
1066	mt792x_mutex_acquire(dev);
1067
1068	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1069	if (err < 0)
1070		goto out;
1071
1072	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1073out:
1074	mt792x_mutex_release(dev);
1075
1076	return err;
1077}
1078
1079static int
1080mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1081{
1082	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1083	struct mt76_phy *mphy = hw->priv;
1084	int err;
1085
1086	mt792x_mutex_acquire(dev);
1087	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1088	mt792x_mutex_release(dev);
1089
1090	return err;
1091}
1092
1093static int
1094mt7921_set_antenna(struct ieee80211_hw *hw, int radio_idx,
1095		   u32 tx_ant, u32 rx_ant)
1096{
1097	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1098	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1099	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1100
1101	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1102		return -EINVAL;
1103
1104	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1105		return -EINVAL;
1106
1107	mt792x_mutex_acquire(dev);
1108
1109	phy->mt76->antenna_mask = tx_ant;
1110	phy->mt76->chainmask = tx_ant;
1111
1112	mt76_set_stream_caps(phy->mt76, true);
1113	mt7921_set_stream_he_caps(phy);
1114
1115	mt792x_mutex_release(dev);
1116
1117	return 0;
1118}
1119
1120#ifdef CONFIG_PM
1121static int mt7921_suspend(struct ieee80211_hw *hw,
1122			  struct cfg80211_wowlan *wowlan)
1123{
1124	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1125	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1126
1127	cancel_delayed_work_sync(&phy->scan_work);
1128	cancel_delayed_work_sync(&phy->mt76->mac_work);
1129
1130	cancel_delayed_work_sync(&dev->pm.ps_work);
1131	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1132
1133	mt792x_mutex_acquire(dev);
1134
1135	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1136	ieee80211_iterate_active_interfaces(hw,
1137					    IEEE80211_IFACE_ITER_RESUME_ALL,
1138					    mt7921_mcu_set_suspend_iter,
1139					    &dev->mphy);
1140
1141	mt792x_mutex_release(dev);
1142
1143	return 0;
1144}
1145
1146static int mt7921_resume(struct ieee80211_hw *hw)
1147{
1148	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1149	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1150
1151	mt792x_mutex_acquire(dev);
1152
1153	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1154	ieee80211_iterate_active_interfaces(hw,
1155					    IEEE80211_IFACE_ITER_RESUME_ALL,
1156					    mt76_connac_mcu_set_suspend_iter,
1157					    &dev->mphy);
1158
1159	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1160				     MT792x_WATCHDOG_TIME);
1161
1162	mt792x_mutex_release(dev);
1163
1164	return 0;
1165}
1166
1167static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1168				  struct ieee80211_vif *vif,
1169				  struct cfg80211_gtk_rekey_data *data)
1170{
1171	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1172
1173	mt792x_mutex_acquire(dev);
1174	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1175	mt792x_mutex_release(dev);
1176}
1177#endif /* CONFIG_PM */
1178
1179static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1180					 struct ieee80211_vif *vif,
1181					 struct ieee80211_sta *sta,
1182					 bool enabled)
1183{
1184	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1185	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1186
1187	if (!msta->deflink.wcid.sta)
1188		return;
1189
1190	mt792x_mutex_acquire(dev);
1191
1192	if (enabled)
1193		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->deflink.wcid.flags);
1194	else
1195		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->deflink.wcid.flags);
1196
1197	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->deflink.wcid,
1198					     MCU_UNI_CMD(STA_REC_UPDATE));
1199
1200	mt792x_mutex_release(dev);
1201}
1202
1203#if IS_ENABLED(CONFIG_IPV6)
1204static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw,
1205				    struct ieee80211_vif *vif,
1206				    struct inet6_dev *idev)
1207{
1208	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1209	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1210	struct inet6_ifaddr *ifa;
1211	struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1212	struct sk_buff *skb;
1213	u8 i, idx = 0;
1214
1215	struct {
1216		struct {
1217			u8 bss_idx;
1218			u8 pad[3];
1219		} __packed hdr;
1220		struct mt76_connac_arpns_tlv arpns;
1221	} req_hdr = {
1222		.hdr = {
1223			.bss_idx = mvif->bss_conf.mt76.idx,
1224		},
1225		.arpns = {
1226			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1227			.mode = 2,  /* update */
1228			.option = 1, /* update only */
1229		},
1230	};
1231
1232	read_lock_bh(&idev->lock);
1233	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1234		if (ifa->flags & IFA_F_TENTATIVE)
1235			continue;
1236		ns_addrs[idx] = ifa->addr;
1237		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1238			break;
1239	}
1240	read_unlock_bh(&idev->lock);
1241
1242	if (!idx)
1243		return;
1244
1245	req_hdr.arpns.ips_num = idx;
1246	req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)
1247					+ idx * sizeof(struct in6_addr));
1248	skb = __mt76_mcu_msg_alloc(&dev->mt76, &req_hdr,
1249			sizeof(req_hdr) + idx * sizeof(struct in6_addr),
1250			sizeof(req_hdr), GFP_ATOMIC);
1251	if (!skb)
1252		return;
1253
1254	for (i = 0; i < idx; i++)
1255		skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr));
1256
1257	skb_queue_tail(&dev->ipv6_ns_list, skb);
1258
1259	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1260}
1261#endif
1262
1263int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
1264			  const struct cfg80211_sar_specs *sar)
1265{
1266	struct mt76_phy *mphy = hw->priv;
1267
1268	if (sar) {
1269		int err = mt76_init_sar_power(hw, sar);
1270
1271		if (err)
1272			return err;
1273	}
1274	mt792x_init_acpi_sar_power(mt792x_hw_phy(hw), !sar);
1275
1276	return mt76_connac_mcu_set_rate_txpower(mphy);
1277}
1278
1279static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1280				const struct cfg80211_sar_specs *sar)
1281{
1282	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1283	int err;
1284
1285	mt792x_mutex_acquire(dev);
1286	err = mt7921_mcu_set_clc(dev, dev->mt76.alpha2,
1287				 dev->country_ie_env);
1288	if (err < 0)
1289		goto out;
1290
1291	err = mt7921_set_tx_sar_pwr(hw, sar);
1292out:
1293	mt792x_mutex_release(dev);
1294
1295	return err;
1296}
1297
1298static void
1299mt7921_channel_switch_beacon(struct ieee80211_hw *hw,
1300			     struct ieee80211_vif *vif,
1301			     struct cfg80211_chan_def *chandef)
1302{
1303	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1304
1305	mt792x_mutex_acquire(dev);
1306	mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1307	mt792x_mutex_release(dev);
1308}
1309
1310static int
1311mt7921_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1312		struct ieee80211_bss_conf *link_conf)
1313{
1314	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1315	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1316	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1317	int err;
1318
1319	mt792x_mutex_acquire(dev);
1320
1321	err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.deflink.wcid,
1322					  true, mvif->bss_conf.mt76.ctx);
1323	if (err)
1324		goto out;
1325
1326	err = mt7921_mcu_set_bss_pm(dev, vif, true);
1327	if (err)
1328		goto out;
1329
1330	err = mt7921_mcu_sta_update(dev, NULL, vif, true,
1331				    MT76_STA_INFO_STATE_NONE);
1332out:
1333	mt792x_mutex_release(dev);
1334
1335	return err;
1336}
1337
1338static void
1339mt7921_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1340	       struct ieee80211_bss_conf *link_conf)
1341{
1342	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1343	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1344	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1345	int err;
1346
1347	mt792x_mutex_acquire(dev);
1348
1349	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1350	if (err)
1351		goto out;
1352
1353	mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.deflink.wcid, false,
1354				    mvif->bss_conf.mt76.ctx);
1355
1356out:
1357	mt792x_mutex_release(dev);
1358}
1359
1360static int
1361mt7921_add_chanctx(struct ieee80211_hw *hw,
1362		   struct ieee80211_chanctx_conf *ctx)
1363{
1364	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1365
1366	dev->new_ctx = ctx;
1367	return 0;
1368}
1369
1370static void
1371mt7921_remove_chanctx(struct ieee80211_hw *hw,
1372		      struct ieee80211_chanctx_conf *ctx)
1373{
1374	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1375
1376	if (dev->new_ctx == ctx)
1377		dev->new_ctx = NULL;
1378}
1379
1380static void
1381mt7921_change_chanctx(struct ieee80211_hw *hw,
1382		      struct ieee80211_chanctx_conf *ctx,
1383		      u32 changed)
1384{
1385	struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv;
1386	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1387	struct ieee80211_vif *vif;
1388	struct mt792x_vif *mvif;
1389
1390	if (!mctx->bss_conf)
1391		return;
1392
1393	mvif = container_of(mctx->bss_conf, struct mt792x_vif, bss_conf);
1394	vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv);
1395
1396	mt792x_mutex_acquire(phy->dev);
1397	if (vif->type == NL80211_IFTYPE_MONITOR)
1398		mt7921_mcu_config_sniffer(mvif, ctx);
1399	else
1400		mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->bss_conf.mt76, ctx);
1401	mt792x_mutex_release(phy->dev);
1402}
1403
1404static void mt7921_mgd_prepare_tx(struct ieee80211_hw *hw,
1405				  struct ieee80211_vif *vif,
1406				  struct ieee80211_prep_tx_info *info)
1407{
1408	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1409	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1410	u16 duration = info->duration ? info->duration :
1411		       jiffies_to_msecs(HZ);
1412
1413	mt792x_mutex_acquire(dev);
1414	mt7921_set_roc(mvif->phy, mvif, mvif->bss_conf.mt76.ctx->def.chan, duration,
1415		       MT7921_ROC_REQ_JOIN);
1416	mt792x_mutex_release(dev);
1417}
1418
1419static void mt7921_mgd_complete_tx(struct ieee80211_hw *hw,
1420				   struct ieee80211_vif *vif,
1421				   struct ieee80211_prep_tx_info *info)
1422{
1423	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1424
1425	mt7921_abort_roc(mvif->phy, mvif);
1426}
1427
1428static int mt7921_switch_vif_chanctx(struct ieee80211_hw *hw,
1429				     struct ieee80211_vif_chanctx_switch *vifs,
1430				     int n_vifs,
1431				     enum ieee80211_chanctx_switch_mode mode)
1432{
1433	return mt792x_assign_vif_chanctx(hw, vifs->vif, vifs->link_conf,
1434					 vifs->new_ctx);
1435}
1436
1437void mt7921_csa_work(struct work_struct *work)
1438{
1439	struct mt792x_vif *mvif;
1440	struct mt792x_dev *dev;
1441	struct ieee80211_vif *vif;
1442	int ret;
1443
1444	mvif = (struct mt792x_vif *)container_of(work, struct mt792x_vif,
1445						csa_work);
1446	dev = mvif->phy->dev;
1447	vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv);
1448
1449	mt792x_mutex_acquire(dev);
1450	ret = mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->bss_conf.mt76,
1451					    dev->new_ctx);
1452	mt792x_mutex_release(dev);
1453
1454	ieee80211_chswitch_done(vif, !ret, 0);
1455}
1456
1457static int mt7921_pre_channel_switch(struct ieee80211_hw *hw,
1458				     struct ieee80211_vif *vif,
1459				     struct ieee80211_channel_switch *chsw)
1460{
1461	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
1462		return -EOPNOTSUPP;
1463
1464	if (!cfg80211_chandef_usable(hw->wiphy, &chsw->chandef,
1465				     IEEE80211_CHAN_DISABLED))
1466		return -EOPNOTSUPP;
1467
1468	return 0;
1469}
1470
1471static void mt7921_channel_switch(struct ieee80211_hw *hw,
1472				  struct ieee80211_vif *vif,
1473				  struct ieee80211_channel_switch *chsw)
1474{
1475	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1476	u16 beacon_interval = vif->bss_conf.beacon_int;
1477
1478	mvif->csa_timer.expires = TU_TO_EXP_TIME(beacon_interval * chsw->count);
1479	add_timer(&mvif->csa_timer);
1480}
1481
1482static void mt7921_abort_channel_switch(struct ieee80211_hw *hw,
1483					struct ieee80211_vif *vif,
1484					struct ieee80211_bss_conf *link_conf)
1485{
1486	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1487
1488	timer_delete_sync(&mvif->csa_timer);
1489	cancel_work_sync(&mvif->csa_work);
1490}
1491
1492static void mt7921_channel_switch_rx_beacon(struct ieee80211_hw *hw,
1493					    struct ieee80211_vif *vif,
1494					    struct ieee80211_channel_switch *chsw)
1495{
1496	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1497	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1498	u16 beacon_interval = vif->bss_conf.beacon_int;
1499
1500	if (cfg80211_chandef_identical(&chsw->chandef,
1501				       &dev->new_ctx->def) &&
1502				       chsw->count) {
1503		mod_timer(&mvif->csa_timer,
1504			  TU_TO_EXP_TIME(beacon_interval * chsw->count));
1505	}
1506}
1507
1508static void mt7921_rfkill_poll(struct ieee80211_hw *hw)
1509{
1510	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1511	int ret = 0;
1512
1513	mt792x_mutex_acquire(phy->dev);
1514	ret = mt7921_mcu_wf_rf_pin_ctrl(phy, WF_RF_PIN_POLL);
1515	mt792x_mutex_release(phy->dev);
1516
1517	wiphy_rfkill_set_hw_state(hw->wiphy, ret ? false : true);
1518}
1519
1520const struct ieee80211_ops mt7921_ops = {
1521	.tx = mt792x_tx,
1522	.start = mt7921_start,
1523	.stop = mt7921_stop,
1524	.add_interface = mt7921_add_interface,
1525	.remove_interface = mt792x_remove_interface,
1526	.config = mt7921_config,
1527	.conf_tx = mt792x_conf_tx,
1528	.configure_filter = mt7921_configure_filter,
1529	.bss_info_changed = mt7921_bss_info_changed,
1530	.start_ap = mt7921_start_ap,
1531	.stop_ap = mt7921_stop_ap,
1532	.sta_state = mt7921_sta_state,
1533	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1534	.set_key = mt7921_set_key,
1535	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1536#if IS_ENABLED(CONFIG_IPV6)
1537	.ipv6_addr_change = mt7921_ipv6_addr_change,
1538#endif /* CONFIG_IPV6 */
1539	.ampdu_action = mt7921_ampdu_action,
1540	.set_rts_threshold = mt7921_set_rts_threshold,
1541	.wake_tx_queue = mt76_wake_tx_queue,
1542	.release_buffered_frames = mt76_release_buffered_frames,
1543	.channel_switch_beacon = mt7921_channel_switch_beacon,
1544	.get_txpower = mt76_get_txpower,
1545	.get_stats = mt792x_get_stats,
1546	.get_et_sset_count = mt792x_get_et_sset_count,
1547	.get_et_strings = mt792x_get_et_strings,
1548	.get_et_stats = mt792x_get_et_stats,
1549	.get_tsf = mt792x_get_tsf,
1550	.set_tsf = mt792x_set_tsf,
1551	.get_survey = mt76_get_survey,
1552	.get_antenna = mt76_get_antenna,
1553	.set_antenna = mt7921_set_antenna,
1554	.set_coverage_class = mt792x_set_coverage_class,
1555	.hw_scan = mt7921_hw_scan,
1556	.cancel_hw_scan = mt7921_cancel_hw_scan,
1557	.sta_statistics = mt792x_sta_statistics,
1558	.sched_scan_start = mt7921_start_sched_scan,
1559	.sched_scan_stop = mt7921_stop_sched_scan,
1560	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1561	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1562#ifdef CONFIG_PM
1563	.suspend = mt7921_suspend,
1564	.resume = mt7921_resume,
1565	.set_wakeup = mt792x_set_wakeup,
1566	.set_rekey_data = mt7921_set_rekey_data,
1567#endif /* CONFIG_PM */
1568	.flush = mt792x_flush,
1569	.set_sar_specs = mt7921_set_sar_specs,
1570	.rfkill_poll = mt7921_rfkill_poll,
1571	.remain_on_channel = mt7921_remain_on_channel,
1572	.cancel_remain_on_channel = mt7921_cancel_remain_on_channel,
1573	.add_chanctx = mt7921_add_chanctx,
1574	.remove_chanctx = mt7921_remove_chanctx,
1575	.change_chanctx = mt7921_change_chanctx,
1576	.assign_vif_chanctx = mt792x_assign_vif_chanctx,
1577	.unassign_vif_chanctx = mt792x_unassign_vif_chanctx,
1578	.mgd_prepare_tx = mt7921_mgd_prepare_tx,
1579	.mgd_complete_tx = mt7921_mgd_complete_tx,
1580	.switch_vif_chanctx = mt7921_switch_vif_chanctx,
1581	.pre_channel_switch = mt7921_pre_channel_switch,
1582	.channel_switch = mt7921_channel_switch,
1583	.abort_channel_switch = mt7921_abort_channel_switch,
1584	.channel_switch_rx_beacon = mt7921_channel_switch_rx_beacon,
1585};
1586EXPORT_SYMBOL_GPL(mt7921_ops);
1587
1588MODULE_DESCRIPTION("MediaTek MT7921 core driver");
1589MODULE_LICENSE("Dual BSD/GPL");
1590MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
~/ Read-only file preview.