1 // SPDX-License-Identifier: BSD-3-Clause-Clear 2 /* 3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name> 4 */ 5 #include <linux/sched.h> 6 #include <linux/of.h> 7 #include "mt76.h" 8 9 #define CHAN2G(_idx, _freq) { \ 10 .band = NL80211_BAND_2GHZ, \ 11 .center_freq = (_freq), \ 12 .hw_value = (_idx), \ 13 .max_power = 30, \ 14 } 15 16 #define CHAN5G(_idx, _freq) { \ 17 .band = NL80211_BAND_5GHZ, \ 18 .center_freq = (_freq), \ 19 .hw_value = (_idx), \ 20 .max_power = 30, \ 21 } 22 23 #define CHAN6G(_idx, _freq) { \ 24 .band = NL80211_BAND_6GHZ, \ 25 .center_freq = (_freq), \ 26 .hw_value = (_idx), \ 27 .max_power = 30, \ 28 } 29 30 static const struct ieee80211_channel mt76_channels_2ghz[] = { 31 CHAN2G(1, 2412), 32 CHAN2G(2, 2417), 33 CHAN2G(3, 2422), 34 CHAN2G(4, 2427), 35 CHAN2G(5, 2432), 36 CHAN2G(6, 2437), 37 CHAN2G(7, 2442), 38 CHAN2G(8, 2447), 39 CHAN2G(9, 2452), 40 CHAN2G(10, 2457), 41 CHAN2G(11, 2462), 42 CHAN2G(12, 2467), 43 CHAN2G(13, 2472), 44 CHAN2G(14, 2484), 45 }; 46 47 static const struct ieee80211_channel mt76_channels_5ghz[] = { 48 CHAN5G(36, 5180), 49 CHAN5G(40, 5200), 50 CHAN5G(44, 5220), 51 CHAN5G(48, 5240), 52 53 CHAN5G(52, 5260), 54 CHAN5G(56, 5280), 55 CHAN5G(60, 5300), 56 CHAN5G(64, 5320), 57 58 CHAN5G(100, 5500), 59 CHAN5G(104, 5520), 60 CHAN5G(108, 5540), 61 CHAN5G(112, 5560), 62 CHAN5G(116, 5580), 63 CHAN5G(120, 5600), 64 CHAN5G(124, 5620), 65 CHAN5G(128, 5640), 66 CHAN5G(132, 5660), 67 CHAN5G(136, 5680), 68 CHAN5G(140, 5700), 69 CHAN5G(144, 5720), 70 71 CHAN5G(149, 5745), 72 CHAN5G(153, 5765), 73 CHAN5G(157, 5785), 74 CHAN5G(161, 5805), 75 CHAN5G(165, 5825), 76 CHAN5G(169, 5845), 77 CHAN5G(173, 5865), 78 CHAN5G(177, 5885), 79 }; 80 81 static const struct ieee80211_channel mt76_channels_6ghz[] = { 82 /* UNII-5 */ 83 CHAN6G(1, 5955), 84 CHAN6G(5, 5975), 85 CHAN6G(9, 5995), 86 CHAN6G(13, 6015), 87 CHAN6G(17, 6035), 88 CHAN6G(21, 6055), 89 CHAN6G(25, 6075), 90 CHAN6G(29, 6095), 91 CHAN6G(33, 6115), 92 CHAN6G(37, 6135), 93 CHAN6G(41, 6155), 94 CHAN6G(45, 6175), 95 CHAN6G(49, 6195), 96 CHAN6G(53, 6215), 97 CHAN6G(57, 6235), 98 CHAN6G(61, 6255), 99 CHAN6G(65, 6275), 100 CHAN6G(69, 6295), 101 CHAN6G(73, 6315), 102 CHAN6G(77, 6335), 103 CHAN6G(81, 6355), 104 CHAN6G(85, 6375), 105 CHAN6G(89, 6395), 106 CHAN6G(93, 6415), 107 /* UNII-6 */ 108 CHAN6G(97, 6435), 109 CHAN6G(101, 6455), 110 CHAN6G(105, 6475), 111 CHAN6G(109, 6495), 112 CHAN6G(113, 6515), 113 CHAN6G(117, 6535), 114 /* UNII-7 */ 115 CHAN6G(121, 6555), 116 CHAN6G(125, 6575), 117 CHAN6G(129, 6595), 118 CHAN6G(133, 6615), 119 CHAN6G(137, 6635), 120 CHAN6G(141, 6655), 121 CHAN6G(145, 6675), 122 CHAN6G(149, 6695), 123 CHAN6G(153, 6715), 124 CHAN6G(157, 6735), 125 CHAN6G(161, 6755), 126 CHAN6G(165, 6775), 127 CHAN6G(169, 6795), 128 CHAN6G(173, 6815), 129 CHAN6G(177, 6835), 130 CHAN6G(181, 6855), 131 CHAN6G(185, 6875), 132 /* UNII-8 */ 133 CHAN6G(189, 6895), 134 CHAN6G(193, 6915), 135 CHAN6G(197, 6935), 136 CHAN6G(201, 6955), 137 CHAN6G(205, 6975), 138 CHAN6G(209, 6995), 139 CHAN6G(213, 7015), 140 CHAN6G(217, 7035), 141 CHAN6G(221, 7055), 142 CHAN6G(225, 7075), 143 CHAN6G(229, 7095), 144 CHAN6G(233, 7115), 145 }; 146 147 static const struct ieee80211_tpt_blink mt76_tpt_blink[] = { 148 { .throughput = 0 * 1024, .blink_time = 334 }, 149 { .throughput = 1 * 1024, .blink_time = 260 }, 150 { .throughput = 5 * 1024, .blink_time = 220 }, 151 { .throughput = 10 * 1024, .blink_time = 190 }, 152 { .throughput = 20 * 1024, .blink_time = 170 }, 153 { .throughput = 50 * 1024, .blink_time = 150 }, 154 { .throughput = 70 * 1024, .blink_time = 130 }, 155 { .throughput = 100 * 1024, .blink_time = 110 }, 156 { .throughput = 200 * 1024, .blink_time = 80 }, 157 { .throughput = 300 * 1024, .blink_time = 50 }, 158 }; 159 160 struct ieee80211_rate mt76_rates[] = { 161 CCK_RATE(0, 10), 162 CCK_RATE(1, 20), 163 CCK_RATE(2, 55), 164 CCK_RATE(3, 110), 165 OFDM_RATE(11, 60), 166 OFDM_RATE(15, 90), 167 OFDM_RATE(10, 120), 168 OFDM_RATE(14, 180), 169 OFDM_RATE(9, 240), 170 OFDM_RATE(13, 360), 171 OFDM_RATE(8, 480), 172 OFDM_RATE(12, 540), 173 }; 174 EXPORT_SYMBOL_GPL(mt76_rates); 175 176 static const struct cfg80211_sar_freq_ranges mt76_sar_freq_ranges[] = { 177 { .start_freq = 2402, .end_freq = 2494, }, 178 { .start_freq = 5150, .end_freq = 5350, }, 179 { .start_freq = 5350, .end_freq = 5470, }, 180 { .start_freq = 5470, .end_freq = 5725, }, 181 { .start_freq = 5725, .end_freq = 5950, }, 182 { .start_freq = 5945, .end_freq = 6165, }, 183 { .start_freq = 6165, .end_freq = 6405, }, 184 { .start_freq = 6405, .end_freq = 6525, }, 185 { .start_freq = 6525, .end_freq = 6705, }, 186 { .start_freq = 6705, .end_freq = 6865, }, 187 { .start_freq = 6865, .end_freq = 7125, }, 188 }; 189 190 static const struct cfg80211_sar_capa mt76_sar_capa = { 191 .type = NL80211_SAR_TYPE_POWER, 192 .num_freq_ranges = ARRAY_SIZE(mt76_sar_freq_ranges), 193 .freq_ranges = &mt76_sar_freq_ranges[0], 194 }; 195 196 static int mt76_led_init(struct mt76_phy *phy) 197 { 198 struct mt76_dev *dev = phy->dev; 199 struct ieee80211_hw *hw = phy->hw; 200 struct device_node *np = dev->dev->of_node; 201 202 if (!phy->leds.cdev.brightness_set && !phy->leds.cdev.blink_set) 203 return 0; 204 205 np = of_get_child_by_name(np, "led"); 206 if (np) { 207 if (!of_device_is_available(np)) { 208 of_node_put(np); 209 dev_info(dev->dev, 210 "led registration was explicitly disabled by dts\n"); 211 return 0; 212 } 213 214 if (phy == &dev->phy) { 215 int led_pin; 216 217 if (!of_property_read_u32(np, "led-sources", &led_pin)) 218 phy->leds.pin = led_pin; 219 220 phy->leds.al = 221 of_property_read_bool(np, "led-active-low"); 222 } 223 224 of_node_put(np); 225 } 226 227 snprintf(phy->leds.name, sizeof(phy->leds.name), "mt76-%s", 228 wiphy_name(hw->wiphy)); 229 230 phy->leds.cdev.name = phy->leds.name; 231 phy->leds.cdev.default_trigger = 232 ieee80211_create_tpt_led_trigger(hw, 233 IEEE80211_TPT_LEDTRIG_FL_RADIO, 234 mt76_tpt_blink, 235 ARRAY_SIZE(mt76_tpt_blink)); 236 237 dev_info(dev->dev, 238 "registering led '%s'\n", phy->leds.name); 239 240 return led_classdev_register(dev->dev, &phy->leds.cdev); 241 } 242 243 static void mt76_led_cleanup(struct mt76_phy *phy) 244 { 245 if (!phy->leds.cdev.brightness_set && !phy->leds.cdev.blink_set) 246 return; 247 248 led_classdev_unregister(&phy->leds.cdev); 249 } 250 251 static void mt76_init_stream_cap(struct mt76_phy *phy, 252 struct ieee80211_supported_band *sband, 253 bool vht) 254 { 255 struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap; 256 int i, nstream = hweight8(phy->antenna_mask); 257 struct ieee80211_sta_vht_cap *vht_cap; 258 u16 mcs_map = 0; 259 260 if (nstream > 1) 261 ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC; 262 else 263 ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC; 264 265 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) 266 ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0; 267 268 if (!vht) 269 return; 270 271 vht_cap = &sband->vht_cap; 272 if (nstream > 1) 273 vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC; 274 else 275 vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC; 276 vht_cap->cap |= IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN | 277 IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN; 278 279 for (i = 0; i < 8; i++) { 280 if (i < nstream) 281 mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2)); 282 else 283 mcs_map |= 284 (IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2)); 285 } 286 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map); 287 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map); 288 if (ieee80211_hw_check(phy->hw, SUPPORTS_VHT_EXT_NSS_BW)) 289 vht_cap->vht_mcs.tx_highest |= 290 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE); 291 } 292 293 void mt76_set_stream_caps(struct mt76_phy *phy, bool vht) 294 { 295 if (phy->cap.has_2ghz) 296 mt76_init_stream_cap(phy, &phy->sband_2g.sband, false); 297 if (phy->cap.has_5ghz) 298 mt76_init_stream_cap(phy, &phy->sband_5g.sband, vht); 299 if (phy->cap.has_6ghz) 300 mt76_init_stream_cap(phy, &phy->sband_6g.sband, vht); 301 } 302 EXPORT_SYMBOL_GPL(mt76_set_stream_caps); 303 304 static int 305 mt76_init_sband(struct mt76_phy *phy, struct mt76_sband *msband, 306 const struct ieee80211_channel *chan, int n_chan, 307 struct ieee80211_rate *rates, int n_rates, 308 bool ht, bool vht) 309 { 310 struct ieee80211_supported_band *sband = &msband->sband; 311 struct ieee80211_sta_vht_cap *vht_cap; 312 struct ieee80211_sta_ht_cap *ht_cap; 313 struct mt76_dev *dev = phy->dev; 314 void *chanlist; 315 int size; 316 317 size = n_chan * sizeof(*chan); 318 chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL); 319 if (!chanlist) 320 return -ENOMEM; 321 322 msband->chan = devm_kzalloc_objs(dev->dev, *msband->chan, n_chan); 323 if (!msband->chan) 324 return -ENOMEM; 325 326 sband->channels = chanlist; 327 sband->n_channels = n_chan; 328 sband->bitrates = rates; 329 sband->n_bitrates = n_rates; 330 331 if (!ht) 332 return 0; 333 334 ht_cap = &sband->ht_cap; 335 ht_cap->ht_supported = true; 336 ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 | 337 IEEE80211_HT_CAP_GRN_FLD | 338 IEEE80211_HT_CAP_SGI_20 | 339 IEEE80211_HT_CAP_SGI_40 | 340 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT); 341 342 ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED; 343 ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K; 344 345 mt76_init_stream_cap(phy, sband, vht); 346 347 if (!vht) 348 return 0; 349 350 vht_cap = &sband->vht_cap; 351 vht_cap->vht_supported = true; 352 vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC | 353 IEEE80211_VHT_CAP_RXSTBC_1 | 354 IEEE80211_VHT_CAP_SHORT_GI_80 | 355 (3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT); 356 357 return 0; 358 } 359 360 static int 361 mt76_init_sband_2g(struct mt76_phy *phy, struct ieee80211_rate *rates, 362 int n_rates) 363 { 364 phy->hw->wiphy->bands[NL80211_BAND_2GHZ] = &phy->sband_2g.sband; 365 366 return mt76_init_sband(phy, &phy->sband_2g, mt76_channels_2ghz, 367 ARRAY_SIZE(mt76_channels_2ghz), rates, 368 n_rates, true, false); 369 } 370 371 static int 372 mt76_init_sband_5g(struct mt76_phy *phy, struct ieee80211_rate *rates, 373 int n_rates, bool vht) 374 { 375 phy->hw->wiphy->bands[NL80211_BAND_5GHZ] = &phy->sband_5g.sband; 376 377 return mt76_init_sband(phy, &phy->sband_5g, mt76_channels_5ghz, 378 ARRAY_SIZE(mt76_channels_5ghz), rates, 379 n_rates, true, vht); 380 } 381 382 static int 383 mt76_init_sband_6g(struct mt76_phy *phy, struct ieee80211_rate *rates, 384 int n_rates) 385 { 386 phy->hw->wiphy->bands[NL80211_BAND_6GHZ] = &phy->sband_6g.sband; 387 388 return mt76_init_sband(phy, &phy->sband_6g, mt76_channels_6ghz, 389 ARRAY_SIZE(mt76_channels_6ghz), rates, 390 n_rates, false, false); 391 } 392 393 static void 394 mt76_check_sband(struct mt76_phy *phy, struct mt76_sband *msband, 395 enum nl80211_band band) 396 { 397 struct ieee80211_supported_band *sband = &msband->sband; 398 bool found = false; 399 int i; 400 401 if (!sband) 402 return; 403 404 for (i = 0; i < sband->n_channels; i++) { 405 if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED) 406 continue; 407 408 found = true; 409 break; 410 } 411 412 if (found) { 413 cfg80211_chandef_create(&phy->chandef, &sband->channels[0], 414 NL80211_CHAN_HT20); 415 phy->chan_state = &msband->chan[0]; 416 phy->dev->band_phys[band] = phy; 417 return; 418 } 419 420 sband->n_channels = 0; 421 if (phy->hw->wiphy->bands[band] == sband) 422 phy->hw->wiphy->bands[band] = NULL; 423 } 424 425 static int 426 mt76_phy_init(struct mt76_phy *phy, struct ieee80211_hw *hw) 427 { 428 struct mt76_dev *dev = phy->dev; 429 struct wiphy *wiphy = hw->wiphy; 430 431 INIT_LIST_HEAD(&phy->tx_list); 432 spin_lock_init(&phy->tx_lock); 433 INIT_DELAYED_WORK(&phy->roc_work, mt76_roc_complete_work); 434 INIT_DELAYED_WORK(&phy->scan_work, mt76_scan_work); 435 436 if ((void *)phy != hw->priv) 437 return 0; 438 439 SET_IEEE80211_DEV(hw, dev->dev); 440 SET_IEEE80211_PERM_ADDR(hw, phy->macaddr); 441 442 wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE; 443 if (!phy->no_active_monitor) 444 wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR; 445 wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH | 446 WIPHY_FLAG_SUPPORTS_TDLS | 447 WIPHY_FLAG_AP_UAPSD; 448 449 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST); 450 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS); 451 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AQL); 452 453 if (!wiphy->max_remain_on_channel_duration) 454 wiphy->max_remain_on_channel_duration = 5000; 455 if (!wiphy->available_antennas_tx) 456 wiphy->available_antennas_tx = phy->antenna_mask; 457 if (!wiphy->available_antennas_rx) 458 wiphy->available_antennas_rx = phy->antenna_mask; 459 460 wiphy->sar_capa = &mt76_sar_capa; 461 phy->frp = devm_kzalloc_objs(dev->dev, struct mt76_freq_range_power, 462 wiphy->sar_capa->num_freq_ranges); 463 if (!phy->frp) 464 return -ENOMEM; 465 466 hw->txq_data_size = sizeof(struct mt76_txq); 467 hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL; 468 469 if (!hw->max_tx_fragments) 470 hw->max_tx_fragments = 16; 471 472 ieee80211_hw_set(hw, SIGNAL_DBM); 473 ieee80211_hw_set(hw, AMPDU_AGGREGATION); 474 ieee80211_hw_set(hw, SUPPORTS_RC_TABLE); 475 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT); 476 ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS); 477 ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU); 478 ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER); 479 ieee80211_hw_set(hw, SPECTRUM_MGMT); 480 481 if (!(dev->drv->drv_flags & MT_DRV_AMSDU_OFFLOAD) && 482 hw->max_tx_fragments > 1) { 483 ieee80211_hw_set(hw, TX_AMSDU); 484 ieee80211_hw_set(hw, TX_FRAG_LIST); 485 } 486 487 ieee80211_hw_set(hw, MFP_CAPABLE); 488 ieee80211_hw_set(hw, AP_LINK_PS); 489 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS); 490 491 return 0; 492 } 493 494 struct mt76_phy * 495 mt76_alloc_radio_phy(struct mt76_dev *dev, unsigned int size, 496 u8 band_idx) 497 { 498 struct ieee80211_hw *hw = dev->phy.hw; 499 unsigned int phy_size; 500 struct mt76_phy *phy; 501 502 phy_size = ALIGN(sizeof(*phy), 8); 503 phy = devm_kzalloc(dev->dev, size + phy_size, GFP_KERNEL); 504 if (!phy) 505 return NULL; 506 507 phy->dev = dev; 508 phy->hw = hw; 509 phy->priv = (void *)phy + phy_size; 510 phy->band_idx = band_idx; 511 512 return phy; 513 } 514 EXPORT_SYMBOL_GPL(mt76_alloc_radio_phy); 515 516 struct mt76_phy * 517 mt76_alloc_phy(struct mt76_dev *dev, unsigned int size, 518 const struct ieee80211_ops *ops, u8 band_idx) 519 { 520 struct ieee80211_hw *hw; 521 unsigned int phy_size; 522 struct mt76_phy *phy; 523 524 phy_size = ALIGN(sizeof(*phy), 8); 525 hw = ieee80211_alloc_hw(size + phy_size, ops); 526 if (!hw) 527 return NULL; 528 529 phy = hw->priv; 530 phy->dev = dev; 531 phy->hw = hw; 532 phy->priv = hw->priv + phy_size; 533 phy->band_idx = band_idx; 534 535 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN; 536 hw->wiphy->interface_modes = 537 BIT(NL80211_IFTYPE_STATION) | 538 BIT(NL80211_IFTYPE_AP) | 539 #ifdef CONFIG_MAC80211_MESH 540 BIT(NL80211_IFTYPE_MESH_POINT) | 541 #endif 542 BIT(NL80211_IFTYPE_P2P_CLIENT) | 543 BIT(NL80211_IFTYPE_P2P_GO) | 544 BIT(NL80211_IFTYPE_ADHOC); 545 546 return phy; 547 } 548 EXPORT_SYMBOL_GPL(mt76_alloc_phy); 549 550 int mt76_register_phy(struct mt76_phy *phy, bool vht, 551 struct ieee80211_rate *rates, int n_rates) 552 { 553 int ret; 554 555 ret = mt76_phy_init(phy, phy->hw); 556 if (ret) 557 return ret; 558 559 if (phy->cap.has_2ghz) { 560 ret = mt76_init_sband_2g(phy, rates, n_rates); 561 if (ret) 562 return ret; 563 } 564 565 if (phy->cap.has_5ghz) { 566 ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht); 567 if (ret) 568 return ret; 569 } 570 571 if (phy->cap.has_6ghz) { 572 ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4); 573 if (ret) 574 return ret; 575 } 576 577 if (IS_ENABLED(CONFIG_MT76_LEDS)) { 578 ret = mt76_led_init(phy); 579 if (ret) 580 return ret; 581 } 582 583 wiphy_read_of_freq_limits(phy->hw->wiphy); 584 mt76_check_sband(phy, &phy->sband_2g, NL80211_BAND_2GHZ); 585 mt76_check_sband(phy, &phy->sband_5g, NL80211_BAND_5GHZ); 586 mt76_check_sband(phy, &phy->sband_6g, NL80211_BAND_6GHZ); 587 588 if ((void *)phy == phy->hw->priv) { 589 ret = ieee80211_register_hw(phy->hw); 590 if (ret) 591 return ret; 592 } 593 594 set_bit(MT76_STATE_REGISTERED, &phy->state); 595 phy->dev->phys[phy->band_idx] = phy; 596 597 return 0; 598 } 599 EXPORT_SYMBOL_GPL(mt76_register_phy); 600 601 void mt76_unregister_phy(struct mt76_phy *phy) 602 { 603 struct mt76_dev *dev = phy->dev; 604 605 if (!test_bit(MT76_STATE_REGISTERED, &phy->state)) 606 return; 607 608 if (IS_ENABLED(CONFIG_MT76_LEDS)) 609 mt76_led_cleanup(phy); 610 mt76_tx_status_check(dev, true); 611 ieee80211_unregister_hw(phy->hw); 612 dev->phys[phy->band_idx] = NULL; 613 } 614 EXPORT_SYMBOL_GPL(mt76_unregister_phy); 615 616 int mt76_create_page_pool(struct mt76_dev *dev, struct mt76_queue *q) 617 { 618 bool is_qrx = mt76_queue_is_rx(dev, q); 619 struct page_pool_params pp_params = { 620 .order = 0, 621 .flags = 0, 622 .nid = NUMA_NO_NODE, 623 .dev = dev->dma_dev, 624 }; 625 int idx = is_qrx ? q - dev->q_rx : -1; 626 627 /* Allocate page_pools just for rx/wed_tx_free queues */ 628 if (!is_qrx && !mt76_queue_is_wed_tx_free(q)) 629 return 0; 630 631 switch (idx) { 632 case MT_RXQ_MAIN: 633 case MT_RXQ_BAND1: 634 case MT_RXQ_BAND2: 635 case MT_RXQ_NPU0: 636 case MT_RXQ_NPU1: 637 pp_params.pool_size = 256; 638 break; 639 default: 640 pp_params.pool_size = 16; 641 break; 642 } 643 644 if (mt76_is_mmio(dev)) { 645 /* rely on page_pool for DMA mapping */ 646 pp_params.flags |= PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 647 pp_params.dma_dir = DMA_FROM_DEVICE; 648 pp_params.max_len = PAGE_SIZE; 649 pp_params.offset = 0; 650 /* NAPI is available just for rx queues */ 651 if (idx >= 0 && idx < ARRAY_SIZE(dev->napi)) 652 pp_params.napi = &dev->napi[idx]; 653 } 654 655 q->page_pool = page_pool_create(&pp_params); 656 if (IS_ERR(q->page_pool)) { 657 int err = PTR_ERR(q->page_pool); 658 659 q->page_pool = NULL; 660 return err; 661 } 662 663 return 0; 664 } 665 EXPORT_SYMBOL_GPL(mt76_create_page_pool); 666 667 struct mt76_dev * 668 mt76_alloc_device(struct device *pdev, unsigned int size, 669 const struct ieee80211_ops *ops, 670 const struct mt76_driver_ops *drv_ops) 671 { 672 struct ieee80211_hw *hw; 673 struct mt76_phy *phy; 674 struct mt76_dev *dev; 675 int i; 676 677 hw = ieee80211_alloc_hw(size, ops); 678 if (!hw) 679 return NULL; 680 681 dev = hw->priv; 682 dev->hw = hw; 683 dev->dev = pdev; 684 dev->init_wiphy = NULL; 685 dev->drv = drv_ops; 686 dev->dma_dev = pdev; 687 688 phy = &dev->phy; 689 phy->dev = dev; 690 phy->hw = hw; 691 phy->band_idx = MT_BAND0; 692 dev->phys[phy->band_idx] = phy; 693 694 spin_lock_init(&dev->rx_lock); 695 spin_lock_init(&dev->lock); 696 spin_lock_init(&dev->cc_lock); 697 spin_lock_init(&dev->status_lock); 698 spin_lock_init(&dev->wed_lock); 699 mutex_init(&dev->mutex); 700 init_waitqueue_head(&dev->tx_wait); 701 702 skb_queue_head_init(&dev->mcu.res_q); 703 init_waitqueue_head(&dev->mcu.wait); 704 mutex_init(&dev->mcu.mutex); 705 dev->tx_worker.fn = mt76_tx_worker; 706 707 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN; 708 hw->wiphy->interface_modes = 709 BIT(NL80211_IFTYPE_STATION) | 710 BIT(NL80211_IFTYPE_AP) | 711 #ifdef CONFIG_MAC80211_MESH 712 BIT(NL80211_IFTYPE_MESH_POINT) | 713 #endif 714 BIT(NL80211_IFTYPE_P2P_CLIENT) | 715 BIT(NL80211_IFTYPE_P2P_GO) | 716 BIT(NL80211_IFTYPE_ADHOC); 717 718 spin_lock_init(&dev->token_lock); 719 idr_init(&dev->token); 720 721 spin_lock_init(&dev->rx_token_lock); 722 idr_init(&dev->rx_token); 723 724 INIT_LIST_HEAD(&dev->wcid_list); 725 INIT_LIST_HEAD(&dev->sta_poll_list); 726 spin_lock_init(&dev->sta_poll_lock); 727 728 INIT_LIST_HEAD(&dev->txwi_cache); 729 INIT_LIST_HEAD(&dev->rxwi_cache); 730 dev->token_size = dev->drv->token_size; 731 spin_lock_init(&dev->scan_lock); 732 733 for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++) 734 skb_queue_head_init(&dev->rx_skb[i]); 735 736 dev->wq = alloc_ordered_workqueue("mt76", 0); 737 if (!dev->wq) { 738 ieee80211_free_hw(hw); 739 return NULL; 740 } 741 742 return dev; 743 } 744 EXPORT_SYMBOL_GPL(mt76_alloc_device); 745 746 int mt76_register_device(struct mt76_dev *dev, bool vht, 747 struct ieee80211_rate *rates, int n_rates) 748 { 749 struct ieee80211_hw *hw = dev->hw; 750 struct mt76_phy *phy = &dev->phy; 751 int ret; 752 753 dev_set_drvdata(dev->dev, dev); 754 mt76_wcid_init(&dev->global_wcid, phy->band_idx); 755 ret = mt76_phy_init(phy, hw); 756 if (ret) 757 return ret; 758 759 if (phy->cap.has_2ghz) { 760 ret = mt76_init_sband_2g(phy, rates, n_rates); 761 if (ret) 762 return ret; 763 } 764 765 if (phy->cap.has_5ghz) { 766 ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht); 767 if (ret) 768 return ret; 769 } 770 771 if (phy->cap.has_6ghz) { 772 ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4); 773 if (ret) 774 return ret; 775 } 776 777 wiphy_read_of_freq_limits(hw->wiphy); 778 mt76_check_sband(&dev->phy, &phy->sband_2g, NL80211_BAND_2GHZ); 779 mt76_check_sband(&dev->phy, &phy->sband_5g, NL80211_BAND_5GHZ); 780 mt76_check_sband(&dev->phy, &phy->sband_6g, NL80211_BAND_6GHZ); 781 782 if (dev->init_wiphy) { 783 ret = dev->init_wiphy(dev); 784 if (ret) 785 return ret; 786 } 787 788 if (IS_ENABLED(CONFIG_MT76_LEDS)) { 789 ret = mt76_led_init(phy); 790 if (ret) 791 return ret; 792 } 793 794 ret = ieee80211_register_hw(hw); 795 if (ret) 796 return ret; 797 798 WARN_ON(mt76_worker_setup(hw, &dev->tx_worker, NULL, "tx")); 799 set_bit(MT76_STATE_REGISTERED, &phy->state); 800 sched_set_fifo_low(dev->tx_worker.task); 801 802 return 0; 803 } 804 EXPORT_SYMBOL_GPL(mt76_register_device); 805 806 void mt76_unregister_device(struct mt76_dev *dev) 807 { 808 struct ieee80211_hw *hw = dev->hw; 809 810 if (!test_bit(MT76_STATE_REGISTERED, &dev->phy.state)) 811 return; 812 813 if (IS_ENABLED(CONFIG_MT76_LEDS)) 814 mt76_led_cleanup(&dev->phy); 815 mt76_tx_status_check(dev, true); 816 mt76_wcid_cleanup(dev, &dev->global_wcid); 817 ieee80211_unregister_hw(hw); 818 } 819 EXPORT_SYMBOL_GPL(mt76_unregister_device); 820 821 void mt76_free_device(struct mt76_dev *dev) 822 { 823 mt76_worker_teardown(&dev->tx_worker); 824 if (dev->wq) { 825 destroy_workqueue(dev->wq); 826 dev->wq = NULL; 827 } 828 mt76_npu_deinit(dev); 829 ieee80211_free_hw(dev->hw); 830 } 831 EXPORT_SYMBOL_GPL(mt76_free_device); 832 833 static void mt76_reset_phy(struct mt76_phy *phy) 834 { 835 if (!phy) 836 return; 837 838 INIT_LIST_HEAD(&phy->tx_list); 839 phy->num_sta = 0; 840 phy->chanctx = NULL; 841 mt76_roc_complete(phy); 842 } 843 844 void mt76_reset_device(struct mt76_dev *dev) 845 { 846 int i; 847 848 rcu_read_lock(); 849 for (i = 0; i < ARRAY_SIZE(dev->wcid); i++) { 850 struct mt76_wcid *wcid; 851 852 wcid = rcu_dereference(dev->wcid[i]); 853 if (!wcid) 854 continue; 855 856 wcid->sta = 0; 857 rcu_assign_pointer(dev->wcid[i], NULL); 858 mt76_wcid_cleanup(dev, wcid); 859 } 860 rcu_read_unlock(); 861 862 INIT_LIST_HEAD(&dev->wcid_list); 863 INIT_LIST_HEAD(&dev->sta_poll_list); 864 dev->vif_mask = 0; 865 memset(dev->wcid_mask, 0, sizeof(dev->wcid_mask)); 866 867 mt76_reset_phy(&dev->phy); 868 for (i = 0; i < ARRAY_SIZE(dev->phys); i++) 869 mt76_reset_phy(dev->phys[i]); 870 } 871 EXPORT_SYMBOL_GPL(mt76_reset_device); 872 873 struct mt76_phy *mt76_vif_phy(struct ieee80211_hw *hw, 874 struct ieee80211_vif *vif) 875 { 876 struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv; 877 struct mt76_chanctx *ctx; 878 879 if (!hw->wiphy->n_radio) 880 return hw->priv; 881 882 if (!mlink->ctx) 883 return NULL; 884 885 ctx = (struct mt76_chanctx *)mlink->ctx->drv_priv; 886 return ctx->phy; 887 } 888 EXPORT_SYMBOL_GPL(mt76_vif_phy); 889 890 static void mt76_rx_release_amsdu(struct mt76_phy *phy, enum mt76_rxq_id q) 891 { 892 struct sk_buff *skb = phy->rx_amsdu[q].head; 893 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; 894 struct mt76_dev *dev = phy->dev; 895 struct mt76_queue *rxq = &dev->q_rx[q]; 896 897 phy->rx_amsdu[q].head = NULL; 898 phy->rx_amsdu[q].tail = NULL; 899 900 /* 901 * Validate if the amsdu has a proper first subframe. 902 * A single MSDU can be parsed as A-MSDU when the unauthenticated A-MSDU 903 * flag of the QoS header gets flipped. In such cases, the first 904 * subframe has a LLC/SNAP header in the location of the destination 905 * address. 906 */ 907 if (skb_shinfo(skb)->frag_list) { 908 int offset = 0; 909 910 if (!(status->flag & RX_FLAG_8023)) { 911 offset = ieee80211_get_hdrlen_from_skb(skb); 912 913 if ((status->flag & 914 (RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED)) == 915 RX_FLAG_DECRYPTED) 916 offset += 8; 917 } 918 919 if (ether_addr_equal(skb->data + offset, rfc1042_header)) { 920 dev_kfree_skb(skb); 921 return; 922 } 923 } 924 925 /* RRO 3.0 data queue skbs are processed and completed in the context 926 * of the indicator queue NAPI, which only polls its own skb list 927 */ 928 if (mt76_queue_is_wed_rro_data(rxq) && dev->hwrro_mode == MT76_HWRRO_V3) 929 q = MT_RXQ_RRO_IND; 930 931 __skb_queue_tail(&dev->rx_skb[q], skb); 932 } 933 934 static void mt76_rx_release_burst(struct mt76_phy *phy, enum mt76_rxq_id q, 935 struct sk_buff *skb) 936 { 937 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; 938 939 if (phy->rx_amsdu[q].head && 940 (!status->amsdu || status->first_amsdu || 941 status->seqno != phy->rx_amsdu[q].seqno)) 942 mt76_rx_release_amsdu(phy, q); 943 944 if (!phy->rx_amsdu[q].head) { 945 phy->rx_amsdu[q].tail = &skb_shinfo(skb)->frag_list; 946 phy->rx_amsdu[q].seqno = status->seqno; 947 phy->rx_amsdu[q].head = skb; 948 } else { 949 *phy->rx_amsdu[q].tail = skb; 950 phy->rx_amsdu[q].tail = &skb->next; 951 } 952 953 if (!status->amsdu || status->last_amsdu) 954 mt76_rx_release_amsdu(phy, q); 955 } 956 957 void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb) 958 { 959 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; 960 struct mt76_phy *phy = mt76_dev_phy(dev, status->phy_idx); 961 962 if (!test_bit(MT76_STATE_RUNNING, &phy->state)) { 963 dev_kfree_skb(skb); 964 return; 965 } 966 967 #ifdef CONFIG_NL80211_TESTMODE 968 if (phy->test.state == MT76_TM_STATE_RX_FRAMES) { 969 phy->test.rx_stats.packets[q]++; 970 if (status->flag & RX_FLAG_FAILED_FCS_CRC) 971 phy->test.rx_stats.fcs_error[q]++; 972 } 973 #endif 974 975 mt76_rx_release_burst(phy, q, skb); 976 } 977 EXPORT_SYMBOL_GPL(mt76_rx); 978 979 bool mt76_has_tx_pending(struct mt76_phy *phy) 980 { 981 struct mt76_queue *q; 982 int i; 983 984 for (i = 0; i < __MT_TXQ_MAX; i++) { 985 q = phy->q_tx[i]; 986 if (q && q->queued) 987 return true; 988 } 989 990 if (atomic_read(&phy->mgmt_tx_pending)) 991 return true; 992 993 return false; 994 } 995 EXPORT_SYMBOL_GPL(mt76_has_tx_pending); 996 997 static struct mt76_channel_state * 998 mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c) 999 { 1000 struct mt76_sband *msband; 1001 int idx; 1002 1003 if (c->band == NL80211_BAND_2GHZ) 1004 msband = &phy->sband_2g; 1005 else if (c->band == NL80211_BAND_6GHZ) 1006 msband = &phy->sband_6g; 1007 else 1008 msband = &phy->sband_5g; 1009 1010 idx = c - &msband->sband.channels[0]; 1011 return &msband->chan[idx]; 1012 } 1013 1014 void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time) 1015 { 1016 struct mt76_channel_state *state = phy->chan_state; 1017 1018 state->cc_active += ktime_to_us(ktime_sub(time, 1019 phy->survey_time)); 1020 phy->survey_time = time; 1021 } 1022 EXPORT_SYMBOL_GPL(mt76_update_survey_active_time); 1023 1024 void mt76_update_survey(struct mt76_phy *phy) 1025 { 1026 struct mt76_dev *dev = phy->dev; 1027 ktime_t cur_time; 1028 1029 if (dev->drv->update_survey) 1030 dev->drv->update_survey(phy); 1031 1032 cur_time = ktime_get_boottime(); 1033 mt76_update_survey_active_time(phy, cur_time); 1034 1035 if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) { 1036 struct mt76_channel_state *state = phy->chan_state; 1037 1038 spin_lock_bh(&dev->cc_lock); 1039 state->cc_bss_rx += dev->cur_cc_bss_rx; 1040 dev->cur_cc_bss_rx = 0; 1041 spin_unlock_bh(&dev->cc_lock); 1042 } 1043 } 1044 EXPORT_SYMBOL_GPL(mt76_update_survey); 1045 1046 void mt76_phy_chandef_set(struct mt76_phy *phy, 1047 struct cfg80211_chan_def *chandef) 1048 { 1049 if (!chandef->chan) 1050 return; 1051 1052 mt76_update_survey(phy); 1053 1054 phy->chandef = *chandef; 1055 phy->main_chandef = *chandef; 1056 phy->chan_state = mt76_channel_state(phy, chandef->chan); 1057 } 1058 EXPORT_SYMBOL_GPL(mt76_phy_chandef_set); 1059 1060 int __mt76_set_channel(struct mt76_phy *phy, struct cfg80211_chan_def *chandef, 1061 bool offchannel) 1062 { 1063 struct mt76_dev *dev = phy->dev; 1064 int timeout = HZ / 5; 1065 int ret; 1066 1067 mt76_worker_disable(&dev->tx_worker); 1068 mt76_txq_schedule_pending(phy); 1069 1070 set_bit(MT76_RESET, &phy->state); 1071 wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout); 1072 mt76_update_survey(phy); 1073 1074 if (phy->chandef.chan->center_freq != chandef->chan->center_freq || 1075 phy->chandef.width != chandef->width) 1076 phy->dfs_state = MT_DFS_STATE_UNKNOWN; 1077 1078 phy->chandef = *chandef; 1079 phy->chan_state = mt76_channel_state(phy, chandef->chan); 1080 phy->offchannel = offchannel; 1081 1082 if (!offchannel) 1083 phy->main_chandef = *chandef; 1084 1085 if (chandef->chan != phy->main_chandef.chan) 1086 memset(phy->chan_state, 0, sizeof(*phy->chan_state)); 1087 1088 ret = dev->drv->set_channel(phy); 1089 1090 clear_bit(MT76_RESET, &phy->state); 1091 mt76_worker_enable(&dev->tx_worker); 1092 mt76_worker_schedule(&dev->tx_worker); 1093 1094 return ret; 1095 } 1096 1097 int mt76_set_channel(struct mt76_phy *phy, struct cfg80211_chan_def *chandef, 1098 bool offchannel) 1099 { 1100 struct mt76_dev *dev = phy->dev; 1101 int ret; 1102 1103 cancel_delayed_work_sync(&phy->mac_work); 1104 1105 mutex_lock(&dev->mutex); 1106 ret = __mt76_set_channel(phy, chandef, offchannel); 1107 mutex_unlock(&dev->mutex); 1108 1109 return ret; 1110 } 1111 1112 int mt76_update_channel(struct mt76_phy *phy) 1113 { 1114 struct ieee80211_hw *hw = phy->hw; 1115 struct cfg80211_chan_def *chandef = &hw->conf.chandef; 1116 bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL; 1117 1118 phy->radar_enabled = hw->conf.radar_enabled; 1119 1120 return mt76_set_channel(phy, chandef, offchannel); 1121 } 1122 EXPORT_SYMBOL_GPL(mt76_update_channel); 1123 1124 static struct mt76_sband * 1125 mt76_get_survey_sband(struct mt76_phy *phy, int *idx) 1126 { 1127 if (*idx < phy->sband_2g.sband.n_channels) 1128 return &phy->sband_2g; 1129 1130 *idx -= phy->sband_2g.sband.n_channels; 1131 if (*idx < phy->sband_5g.sband.n_channels) 1132 return &phy->sband_5g; 1133 1134 *idx -= phy->sband_5g.sband.n_channels; 1135 if (*idx < phy->sband_6g.sband.n_channels) 1136 return &phy->sband_6g; 1137 1138 *idx -= phy->sband_6g.sband.n_channels; 1139 return NULL; 1140 } 1141 1142 int mt76_get_survey(struct ieee80211_hw *hw, int idx, 1143 struct survey_info *survey) 1144 { 1145 struct mt76_phy *phy = hw->priv; 1146 struct mt76_dev *dev = phy->dev; 1147 struct mt76_sband *sband = NULL; 1148 struct ieee80211_channel *chan; 1149 struct mt76_channel_state *state; 1150 int phy_idx = 0; 1151 int ret = 0; 1152 1153 mutex_lock(&dev->mutex); 1154 1155 for (phy_idx = 0; phy_idx < ARRAY_SIZE(dev->phys); phy_idx++) { 1156 sband = NULL; 1157 phy = dev->phys[phy_idx]; 1158 if (!phy || phy->hw != hw) 1159 continue; 1160 1161 sband = mt76_get_survey_sband(phy, &idx); 1162 1163 if (idx == 0 && phy->dev->drv->update_survey) 1164 mt76_update_survey(phy); 1165 1166 if (sband || !hw->wiphy->n_radio) 1167 break; 1168 } 1169 1170 if (!sband) { 1171 ret = -ENOENT; 1172 goto out; 1173 } 1174 1175 chan = &sband->sband.channels[idx]; 1176 state = mt76_channel_state(phy, chan); 1177 1178 memset(survey, 0, sizeof(*survey)); 1179 survey->channel = chan; 1180 survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY; 1181 survey->filled |= dev->drv->survey_flags; 1182 if (state->noise) 1183 survey->filled |= SURVEY_INFO_NOISE_DBM; 1184 1185 if (chan == phy->main_chandef.chan) { 1186 survey->filled |= SURVEY_INFO_IN_USE; 1187 1188 if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) 1189 survey->filled |= SURVEY_INFO_TIME_BSS_RX; 1190 } 1191 1192 survey->time_busy = div_u64(state->cc_busy, 1000); 1193 survey->time_rx = div_u64(state->cc_rx, 1000); 1194 survey->time = div_u64(state->cc_active, 1000); 1195 survey->noise = state->noise; 1196 1197 spin_lock_bh(&dev->cc_lock); 1198 survey->time_bss_rx = div_u64(state->cc_bss_rx, 1000); 1199 survey->time_tx = div_u64(state->cc_tx, 1000); 1200 spin_unlock_bh(&dev->cc_lock); 1201 1202 out: 1203 mutex_unlock(&dev->mutex); 1204 1205 return ret; 1206 } 1207 EXPORT_SYMBOL_GPL(mt76_get_survey); 1208 1209 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid, 1210 struct ieee80211_key_conf *key) 1211 { 1212 struct ieee80211_key_seq seq; 1213 int i; 1214 1215 wcid->rx_check_pn = false; 1216 1217 if (!key) 1218 return; 1219 1220 if (key->cipher != WLAN_CIPHER_SUITE_CCMP) 1221 return; 1222 1223 wcid->rx_check_pn = true; 1224 1225 /* data frame */ 1226 for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 1227 ieee80211_get_key_rx_seq(key, i, &seq); 1228 memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn)); 1229 } 1230 1231 /* robust management frame */ 1232 ieee80211_get_key_rx_seq(key, -1, &seq); 1233 memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn)); 1234 1235 } 1236 EXPORT_SYMBOL(mt76_wcid_key_setup); 1237 1238 int mt76_rx_signal(u8 chain_mask, s8 *chain_signal) 1239 { 1240 int signal = -128; 1241 u8 chains; 1242 1243 for (chains = chain_mask; chains; chains >>= 1, chain_signal++) { 1244 int cur, diff; 1245 1246 cur = *chain_signal; 1247 if (!(chains & BIT(0)) || 1248 cur > 0) 1249 continue; 1250 1251 if (cur > signal) 1252 swap(cur, signal); 1253 1254 diff = signal - cur; 1255 if (diff == 0) 1256 signal += 3; 1257 else if (diff <= 2) 1258 signal += 2; 1259 else if (diff <= 6) 1260 signal += 1; 1261 } 1262 1263 return signal; 1264 } 1265 EXPORT_SYMBOL(mt76_rx_signal); 1266 1267 static void 1268 mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb, 1269 struct ieee80211_hw **hw, 1270 struct ieee80211_link_sta **link_sta) 1271 { 1272 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 1273 struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb); 1274 struct mt76_rx_status mstat; 1275 struct ieee80211_sta *sta; 1276 1277 mstat = *((struct mt76_rx_status *)skb->cb); 1278 memset(status, 0, sizeof(*status)); 1279 1280 skb->priority = mstat.qos_ctl & IEEE80211_QOS_CTL_TID_MASK; 1281 1282 status->flag = mstat.flag; 1283 status->freq = mstat.freq; 1284 status->enc_flags = mstat.enc_flags; 1285 status->encoding = mstat.encoding; 1286 status->bw = mstat.bw; 1287 if (status->encoding == RX_ENC_EHT) { 1288 status->eht.ru = mstat.eht.ru; 1289 status->eht.gi = mstat.eht.gi; 1290 } else { 1291 status->he_ru = mstat.he_ru; 1292 status->he_gi = mstat.he_gi; 1293 status->he_dcm = mstat.he_dcm; 1294 } 1295 status->rate_idx = mstat.rate_idx; 1296 status->nss = mstat.nss; 1297 status->band = mstat.band; 1298 status->chains = mstat.chains; 1299 status->ampdu_reference = mstat.ampdu_ref; 1300 status->device_timestamp = mstat.timestamp; 1301 status->mactime = mstat.timestamp; 1302 status->signal = mt76_rx_signal(mstat.chains, mstat.chain_signal); 1303 if (status->signal <= -128) 1304 status->flag |= RX_FLAG_NO_SIGNAL_VAL; 1305 1306 if (ieee80211_is_beacon(hdr->frame_control) || 1307 ieee80211_is_probe_resp(hdr->frame_control)) 1308 status->boottime_ns = ktime_get_boottime_ns(); 1309 1310 BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb)); 1311 BUILD_BUG_ON(sizeof(status->chain_signal) != 1312 sizeof(mstat.chain_signal)); 1313 memcpy(status->chain_signal, mstat.chain_signal, 1314 sizeof(mstat.chain_signal)); 1315 1316 sta = wcid_to_sta(mstat.wcid); 1317 if (!sta) 1318 *link_sta = NULL; 1319 else if (mstat.wcid->link_valid) 1320 *link_sta = rcu_dereference(sta->link[mstat.wcid->link_id]); 1321 else 1322 *link_sta = &sta->deflink; 1323 1324 *hw = mt76_phy_hw(dev, mstat.phy_idx); 1325 } 1326 1327 static void 1328 mt76_check_ccmp_pn(struct sk_buff *skb) 1329 { 1330 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; 1331 struct mt76_wcid *wcid = status->wcid; 1332 struct ieee80211_hdr *hdr; 1333 int security_idx; 1334 int ret; 1335 1336 if (!(status->flag & RX_FLAG_DECRYPTED)) 1337 return; 1338 1339 if (status->flag & RX_FLAG_ONLY_MONITOR) 1340 return; 1341 1342 if (!wcid || !wcid->rx_check_pn) 1343 return; 1344 1345 security_idx = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK; 1346 if (status->flag & RX_FLAG_8023) 1347 goto skip_hdr_check; 1348 1349 hdr = mt76_skb_get_hdr(skb); 1350 if (!(status->flag & RX_FLAG_IV_STRIPPED)) { 1351 /* 1352 * Validate the first fragment both here and in mac80211 1353 * All further fragments will be validated by mac80211 only. 1354 */ 1355 if (ieee80211_is_frag(hdr) && 1356 !ieee80211_is_first_frag(hdr->seq_ctrl)) 1357 return; 1358 } 1359 1360 /* IEEE 802.11-2020, 12.5.3.4.4 "PN and replay detection" c): 1361 * 1362 * the recipient shall maintain a single replay counter for received 1363 * individually addressed robust Management frames that are received 1364 * with the To DS subfield equal to 0, [...] 1365 */ 1366 if (ieee80211_is_mgmt(hdr->frame_control) && 1367 !ieee80211_has_tods(hdr->frame_control)) 1368 security_idx = IEEE80211_NUM_TIDS; 1369 1370 skip_hdr_check: 1371 BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0])); 1372 ret = memcmp(status->iv, wcid->rx_key_pn[security_idx], 1373 sizeof(status->iv)); 1374 if (ret <= 0) { 1375 status->flag |= RX_FLAG_ONLY_MONITOR; 1376 return; 1377 } 1378 1379 memcpy(wcid->rx_key_pn[security_idx], status->iv, sizeof(status->iv)); 1380 1381 if (status->flag & RX_FLAG_IV_STRIPPED) 1382 status->flag |= RX_FLAG_PN_VALIDATED; 1383 } 1384 1385 static void 1386 mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status, 1387 int len) 1388 { 1389 struct mt76_wcid *wcid = status->wcid; 1390 struct ieee80211_rx_status info = { 1391 .enc_flags = status->enc_flags, 1392 .rate_idx = status->rate_idx, 1393 .encoding = status->encoding, 1394 .band = status->band, 1395 .nss = status->nss, 1396 .bw = status->bw, 1397 }; 1398 struct ieee80211_sta *sta; 1399 u32 airtime; 1400 u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK; 1401 1402 airtime = ieee80211_calc_rx_airtime(dev->hw, &info, len); 1403 spin_lock(&dev->cc_lock); 1404 dev->cur_cc_bss_rx += airtime; 1405 spin_unlock(&dev->cc_lock); 1406 1407 if (!wcid || !wcid->sta) 1408 return; 1409 1410 sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv); 1411 ieee80211_sta_register_airtime(sta, tidno, 0, airtime); 1412 } 1413 1414 static void 1415 mt76_airtime_flush_ampdu(struct mt76_dev *dev) 1416 { 1417 struct mt76_wcid *wcid; 1418 int wcid_idx; 1419 1420 if (!dev->rx_ampdu_len) 1421 return; 1422 1423 wcid_idx = dev->rx_ampdu_status.wcid_idx; 1424 if (wcid_idx < ARRAY_SIZE(dev->wcid)) 1425 wcid = rcu_dereference(dev->wcid[wcid_idx]); 1426 else 1427 wcid = NULL; 1428 dev->rx_ampdu_status.wcid = wcid; 1429 1430 mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len); 1431 1432 dev->rx_ampdu_len = 0; 1433 dev->rx_ampdu_ref = 0; 1434 } 1435 1436 static void 1437 mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb) 1438 { 1439 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; 1440 struct mt76_wcid *wcid = status->wcid; 1441 1442 if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)) 1443 return; 1444 1445 if (!wcid || !wcid->sta) { 1446 struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb); 1447 1448 if (status->flag & RX_FLAG_8023) 1449 return; 1450 1451 if (!ether_addr_equal(hdr->addr1, dev->phy.macaddr)) 1452 return; 1453 1454 wcid = NULL; 1455 } 1456 1457 if (!(status->flag & RX_FLAG_AMPDU_DETAILS) || 1458 status->ampdu_ref != dev->rx_ampdu_ref) 1459 mt76_airtime_flush_ampdu(dev); 1460 1461 if (status->flag & RX_FLAG_AMPDU_DETAILS) { 1462 if (!dev->rx_ampdu_len || 1463 status->ampdu_ref != dev->rx_ampdu_ref) { 1464 dev->rx_ampdu_status = *status; 1465 dev->rx_ampdu_status.wcid_idx = wcid ? wcid->idx : 0xff; 1466 dev->rx_ampdu_ref = status->ampdu_ref; 1467 } 1468 1469 dev->rx_ampdu_len += skb->len; 1470 return; 1471 } 1472 1473 mt76_airtime_report(dev, status, skb->len); 1474 } 1475 1476 static void 1477 mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb) 1478 { 1479 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; 1480 struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb); 1481 struct ieee80211_sta *sta; 1482 struct ieee80211_hw *hw; 1483 struct mt76_wcid *wcid = status->wcid; 1484 u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK; 1485 int signal; 1486 bool ps; 1487 1488 hw = mt76_phy_hw(dev, status->phy_idx); 1489 if (ieee80211_is_pspoll(hdr->frame_control) && !wcid && 1490 !(status->flag & RX_FLAG_8023)) { 1491 sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL); 1492 if (sta) 1493 wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv; 1494 } 1495 1496 mt76_airtime_check(dev, skb); 1497 1498 if (!wcid || !wcid->sta) 1499 return; 1500 1501 sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv); 1502 1503 signal = mt76_rx_signal(status->chains, status->chain_signal); 1504 if (signal <= 0 && signal > -128) 1505 ewma_signal_add(&wcid->rssi, -signal); 1506 1507 wcid->inactive_count = 0; 1508 1509 if (status->flag & RX_FLAG_8023) 1510 return; 1511 1512 if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags)) 1513 return; 1514 1515 if (ieee80211_is_pspoll(hdr->frame_control)) { 1516 ieee80211_sta_pspoll(sta); 1517 return; 1518 } 1519 1520 if (ieee80211_has_morefrags(hdr->frame_control) || 1521 !(ieee80211_is_mgmt(hdr->frame_control) || 1522 ieee80211_is_data(hdr->frame_control))) 1523 return; 1524 1525 ps = ieee80211_has_pm(hdr->frame_control); 1526 1527 if (ps && (ieee80211_is_data_qos(hdr->frame_control) || 1528 ieee80211_is_qos_nullfunc(hdr->frame_control))) 1529 ieee80211_sta_uapsd_trigger(sta, tidno); 1530 1531 if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps) 1532 return; 1533 1534 if (ps) 1535 set_bit(MT_WCID_FLAG_PS, &wcid->flags); 1536 1537 if (dev->drv->sta_ps) 1538 dev->drv->sta_ps(dev, sta, ps); 1539 1540 if (!ps) 1541 clear_bit(MT_WCID_FLAG_PS, &wcid->flags); 1542 1543 ieee80211_sta_ps_transition(sta, ps); 1544 } 1545 1546 void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames, 1547 struct napi_struct *napi) 1548 { 1549 struct ieee80211_link_sta *link_sta; 1550 struct ieee80211_hw *hw; 1551 struct sk_buff *skb, *tmp; 1552 LIST_HEAD(list); 1553 1554 spin_lock(&dev->rx_lock); 1555 while ((skb = __skb_dequeue(frames)) != NULL) { 1556 struct sk_buff *nskb = skb_shinfo(skb)->frag_list; 1557 1558 mt76_check_ccmp_pn(skb); 1559 skb_shinfo(skb)->frag_list = NULL; 1560 mt76_rx_convert(dev, skb, &hw, &link_sta); 1561 ieee80211_rx_list(hw, link_sta, skb, &list); 1562 1563 /* subsequent amsdu frames */ 1564 while (nskb) { 1565 skb = nskb; 1566 nskb = nskb->next; 1567 skb->next = NULL; 1568 1569 mt76_rx_convert(dev, skb, &hw, &link_sta); 1570 ieee80211_rx_list(hw, link_sta, skb, &list); 1571 } 1572 } 1573 spin_unlock(&dev->rx_lock); 1574 1575 if (!napi) { 1576 netif_receive_skb_list(&list); 1577 return; 1578 } 1579 1580 list_for_each_entry_safe(skb, tmp, &list, list) { 1581 skb_list_del_init(skb); 1582 napi_gro_receive(napi, skb); 1583 } 1584 } 1585 1586 void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q, 1587 struct napi_struct *napi) 1588 { 1589 struct sk_buff_head frames; 1590 struct sk_buff *skb; 1591 1592 __skb_queue_head_init(&frames); 1593 1594 while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) { 1595 mt76_check_sta(dev, skb); 1596 if (mtk_wed_device_active(&dev->mmio.wed) || 1597 mt76_npu_device_active(dev)) 1598 __skb_queue_tail(&frames, skb); 1599 else 1600 mt76_rx_aggr_reorder(skb, &frames); 1601 } 1602 1603 mt76_rx_complete(dev, &frames, napi); 1604 } 1605 EXPORT_SYMBOL_GPL(mt76_rx_poll_complete); 1606 1607 static int 1608 mt76_sta_add(struct mt76_phy *phy, struct ieee80211_vif *vif, 1609 struct ieee80211_sta *sta) 1610 { 1611 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv; 1612 struct mt76_dev *dev = phy->dev; 1613 struct mt76_wcid *published; 1614 int ret; 1615 int i; 1616 1617 mutex_lock(&dev->mutex); 1618 1619 ret = dev->drv->sta_add(dev, vif, sta); 1620 if (ret) 1621 goto out; 1622 1623 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) { 1624 struct mt76_txq *mtxq; 1625 1626 if (!sta->txq[i]) 1627 continue; 1628 1629 mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv; 1630 mtxq->wcid = wcid->idx; 1631 } 1632 1633 published = rcu_dereference_protected(dev->wcid[wcid->idx], 1634 lockdep_is_held(&dev->mutex)); 1635 if (published != wcid) { 1636 WARN_ON_ONCE(published); 1637 ewma_signal_init(&wcid->rssi); 1638 rcu_assign_pointer(dev->wcid[wcid->idx], wcid); 1639 mt76_wcid_init(wcid, phy->band_idx); 1640 } else { 1641 wcid->phy_idx = phy->band_idx; 1642 } 1643 1644 phy->num_sta++; 1645 1646 out: 1647 mutex_unlock(&dev->mutex); 1648 1649 return ret; 1650 } 1651 1652 void __mt76_sta_remove(struct mt76_phy *phy, struct ieee80211_vif *vif, 1653 struct ieee80211_sta *sta) 1654 { 1655 struct mt76_dev *dev = phy->dev; 1656 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv; 1657 int i, idx = wcid->idx; 1658 1659 for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++) 1660 mt76_rx_aggr_stop(dev, wcid, i); 1661 1662 if (dev->drv->sta_remove) 1663 dev->drv->sta_remove(dev, vif, sta); 1664 1665 mt76_wcid_cleanup(dev, wcid); 1666 1667 mt76_wcid_mask_clear(dev->wcid_mask, idx); 1668 phy->num_sta--; 1669 } 1670 EXPORT_SYMBOL_GPL(__mt76_sta_remove); 1671 1672 static void 1673 mt76_sta_remove(struct mt76_phy *phy, struct ieee80211_vif *vif, 1674 struct ieee80211_sta *sta) 1675 { 1676 struct mt76_dev *dev = phy->dev; 1677 1678 mutex_lock(&dev->mutex); 1679 __mt76_sta_remove(phy, vif, sta); 1680 mutex_unlock(&dev->mutex); 1681 } 1682 1683 int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1684 struct ieee80211_sta *sta, 1685 enum ieee80211_sta_state old_state, 1686 enum ieee80211_sta_state new_state) 1687 { 1688 struct mt76_phy *phy = hw->priv; 1689 struct mt76_dev *dev = phy->dev; 1690 enum mt76_sta_event ev; 1691 1692 phy = mt76_vif_phy(hw, vif); 1693 if (!phy) 1694 return -EINVAL; 1695 1696 if (old_state == IEEE80211_STA_NOTEXIST && 1697 new_state == IEEE80211_STA_NONE) 1698 return mt76_sta_add(phy, vif, sta); 1699 1700 if (old_state == IEEE80211_STA_NONE && 1701 new_state == IEEE80211_STA_NOTEXIST) 1702 mt76_sta_remove(phy, vif, sta); 1703 1704 if (!dev->drv->sta_event) 1705 return 0; 1706 1707 if (old_state == IEEE80211_STA_AUTH && 1708 new_state == IEEE80211_STA_ASSOC) 1709 ev = MT76_STA_EVENT_ASSOC; 1710 else if (old_state == IEEE80211_STA_ASSOC && 1711 new_state == IEEE80211_STA_AUTHORIZED) 1712 ev = MT76_STA_EVENT_AUTHORIZE; 1713 else if (old_state == IEEE80211_STA_ASSOC && 1714 new_state == IEEE80211_STA_AUTH) 1715 ev = MT76_STA_EVENT_DISASSOC; 1716 else 1717 return 0; 1718 1719 return dev->drv->sta_event(dev, vif, sta, ev); 1720 } 1721 EXPORT_SYMBOL_GPL(mt76_sta_state); 1722 1723 void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1724 struct ieee80211_sta *sta) 1725 { 1726 struct mt76_phy *phy = hw->priv; 1727 struct mt76_dev *dev = phy->dev; 1728 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv; 1729 1730 mutex_lock(&dev->mutex); 1731 spin_lock_bh(&dev->status_lock); 1732 rcu_assign_pointer(dev->wcid[wcid->idx], NULL); 1733 spin_unlock_bh(&dev->status_lock); 1734 mutex_unlock(&dev->mutex); 1735 } 1736 EXPORT_SYMBOL_GPL(mt76_sta_pre_rcu_remove); 1737 1738 void mt76_wcid_init(struct mt76_wcid *wcid, u8 band_idx) 1739 { 1740 wcid->hw_key_idx = -1; 1741 wcid->phy_idx = band_idx; 1742 1743 INIT_LIST_HEAD(&wcid->tx_list); 1744 skb_queue_head_init(&wcid->tx_pending); 1745 skb_queue_head_init(&wcid->tx_offchannel); 1746 1747 INIT_LIST_HEAD(&wcid->list); 1748 idr_init(&wcid->pktid); 1749 1750 INIT_LIST_HEAD(&wcid->poll_list); 1751 } 1752 EXPORT_SYMBOL_GPL(mt76_wcid_init); 1753 1754 void mt76_wcid_cleanup(struct mt76_dev *dev, struct mt76_wcid *wcid) 1755 { 1756 struct mt76_phy *phy = mt76_dev_phy(dev, wcid->phy_idx); 1757 struct ieee80211_hw *hw; 1758 struct sk_buff_head list; 1759 struct sk_buff *skb; 1760 1761 mt76_tx_status_lock(dev, &list); 1762 mt76_tx_status_skb_get(dev, wcid, -1, &list); 1763 idr_destroy(&wcid->pktid); 1764 mt76_tx_status_unlock(dev, &list); 1765 1766 /* Remove from sta_poll_list to prevent list corruption after reset. 1767 * Without this, mt76_reset_device() reinitializes sta_poll_list but 1768 * leaves wcid->poll_list with stale pointers, causing list corruption 1769 * when mt76_wcid_add_poll() checks list_empty(). 1770 */ 1771 spin_lock_bh(&dev->sta_poll_lock); 1772 if (!list_empty(&wcid->poll_list)) 1773 list_del_init(&wcid->poll_list); 1774 spin_unlock_bh(&dev->sta_poll_lock); 1775 1776 spin_lock_bh(&phy->tx_lock); 1777 1778 if (!list_empty(&wcid->tx_list)) 1779 list_del_init(&wcid->tx_list); 1780 1781 spin_lock(&wcid->tx_pending.lock); 1782 skb_queue_splice_tail_init(&wcid->tx_pending, &list); 1783 spin_unlock(&wcid->tx_pending.lock); 1784 1785 spin_lock(&wcid->tx_offchannel.lock); 1786 skb_queue_splice_tail_init(&wcid->tx_offchannel, &list); 1787 spin_unlock(&wcid->tx_offchannel.lock); 1788 1789 spin_unlock_bh(&phy->tx_lock); 1790 1791 while ((skb = __skb_dequeue(&list)) != NULL) { 1792 hw = mt76_tx_status_get_hw(dev, skb); 1793 ieee80211_free_txskb(hw, skb); 1794 } 1795 } 1796 EXPORT_SYMBOL_GPL(mt76_wcid_cleanup); 1797 1798 void mt76_wcid_add_poll(struct mt76_dev *dev, struct mt76_wcid *wcid) 1799 { 1800 if (test_bit(MT76_MCU_RESET, &dev->phy.state) || !wcid->sta) 1801 return; 1802 1803 spin_lock_bh(&dev->sta_poll_lock); 1804 if (list_empty(&wcid->poll_list)) 1805 list_add_tail(&wcid->poll_list, &dev->sta_poll_list); 1806 spin_unlock_bh(&dev->sta_poll_lock); 1807 } 1808 EXPORT_SYMBOL_GPL(mt76_wcid_add_poll); 1809 1810 s8 mt76_get_power_bound(struct mt76_phy *phy, s8 txpower) 1811 { 1812 int n_chains = hweight16(phy->chainmask); 1813 1814 txpower = mt76_get_sar_power(phy, phy->chandef.chan, txpower * 2); 1815 txpower -= mt76_tx_power_path_delta(n_chains); 1816 1817 return txpower; 1818 } 1819 EXPORT_SYMBOL_GPL(mt76_get_power_bound); 1820 1821 int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1822 unsigned int link_id, int *dbm) 1823 { 1824 struct mt76_phy *phy = mt76_vif_phy(hw, vif); 1825 int n_chains, delta; 1826 1827 if (!phy) 1828 return -EINVAL; 1829 1830 n_chains = hweight16(phy->chainmask); 1831 delta = mt76_tx_power_path_delta(n_chains); 1832 *dbm = DIV_ROUND_UP(phy->txpower_cur + delta, 2); 1833 1834 return 0; 1835 } 1836 EXPORT_SYMBOL_GPL(mt76_get_txpower); 1837 1838 int mt76_init_sar_power(struct ieee80211_hw *hw, 1839 const struct cfg80211_sar_specs *sar) 1840 { 1841 struct mt76_phy *phy = hw->priv; 1842 const struct cfg80211_sar_capa *capa = hw->wiphy->sar_capa; 1843 int i; 1844 1845 if (sar->type != NL80211_SAR_TYPE_POWER || !sar->num_sub_specs) 1846 return -EINVAL; 1847 1848 for (i = 0; i < sar->num_sub_specs; i++) { 1849 u32 index = sar->sub_specs[i].freq_range_index; 1850 /* SAR specifies power limitaton in 0.25dbm */ 1851 s32 power = sar->sub_specs[i].power >> 1; 1852 1853 if (power > 127 || power < -127) 1854 power = 127; 1855 1856 phy->frp[index].range = &capa->freq_ranges[index]; 1857 phy->frp[index].power = power; 1858 } 1859 1860 return 0; 1861 } 1862 EXPORT_SYMBOL_GPL(mt76_init_sar_power); 1863 1864 int mt76_get_sar_power(struct mt76_phy *phy, 1865 struct ieee80211_channel *chan, 1866 int power) 1867 { 1868 const struct cfg80211_sar_capa *capa = phy->hw->wiphy->sar_capa; 1869 int freq, i; 1870 1871 if (!capa || !phy->frp) 1872 return power; 1873 1874 if (power > 127 || power < -127) 1875 power = 127; 1876 1877 freq = ieee80211_channel_to_frequency(chan->hw_value, chan->band); 1878 for (i = 0 ; i < capa->num_freq_ranges; i++) { 1879 if (phy->frp[i].range && 1880 freq >= phy->frp[i].range->start_freq && 1881 freq < phy->frp[i].range->end_freq) { 1882 power = min_t(int, phy->frp[i].power, power); 1883 break; 1884 } 1885 } 1886 1887 return power; 1888 } 1889 EXPORT_SYMBOL_GPL(mt76_get_sar_power); 1890 1891 static void 1892 __mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif) 1893 { 1894 if (vif->bss_conf.csa_active && ieee80211_beacon_cntdwn_is_complete(vif, 0)) 1895 ieee80211_csa_finish(vif, 0); 1896 } 1897 1898 void mt76_csa_finish(struct mt76_dev *dev) 1899 { 1900 if (!dev->csa_complete) 1901 return; 1902 1903 ieee80211_iterate_active_interfaces_atomic(dev->hw, 1904 IEEE80211_IFACE_ITER_RESUME_ALL, 1905 __mt76_csa_finish, dev); 1906 1907 dev->csa_complete = 0; 1908 } 1909 EXPORT_SYMBOL_GPL(mt76_csa_finish); 1910 1911 static void 1912 __mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif) 1913 { 1914 struct mt76_dev *dev = priv; 1915 1916 if (!vif->bss_conf.csa_active) 1917 return; 1918 1919 dev->csa_complete |= ieee80211_beacon_cntdwn_is_complete(vif, 0); 1920 } 1921 1922 void mt76_csa_check(struct mt76_dev *dev) 1923 { 1924 ieee80211_iterate_active_interfaces_atomic(dev->hw, 1925 IEEE80211_IFACE_ITER_RESUME_ALL, 1926 __mt76_csa_check, dev); 1927 } 1928 EXPORT_SYMBOL_GPL(mt76_csa_check); 1929 1930 int 1931 mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set) 1932 { 1933 return 0; 1934 } 1935 EXPORT_SYMBOL_GPL(mt76_set_tim); 1936 1937 void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id) 1938 { 1939 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; 1940 int hdr_len = ieee80211_get_hdrlen_from_skb(skb); 1941 u8 *hdr, *pn = status->iv; 1942 1943 __skb_push(skb, 8); 1944 memmove(skb->data, skb->data + 8, hdr_len); 1945 hdr = skb->data + hdr_len; 1946 1947 hdr[0] = pn[5]; 1948 hdr[1] = pn[4]; 1949 hdr[2] = 0; 1950 hdr[3] = 0x20 | (key_id << 6); 1951 hdr[4] = pn[3]; 1952 hdr[5] = pn[2]; 1953 hdr[6] = pn[1]; 1954 hdr[7] = pn[0]; 1955 1956 status->flag &= ~RX_FLAG_IV_STRIPPED; 1957 } 1958 EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr); 1959 1960 int mt76_get_rate(struct mt76_dev *dev, 1961 struct ieee80211_supported_band *sband, 1962 int idx, bool cck) 1963 { 1964 bool is_2g = sband->band == NL80211_BAND_2GHZ; 1965 int i, offset = 0, len = sband->n_bitrates; 1966 1967 if (cck) { 1968 if (!is_2g) 1969 return 0; 1970 1971 idx &= ~BIT(2); /* short preamble */ 1972 } else if (is_2g) { 1973 offset = 4; 1974 } 1975 1976 for (i = offset; i < len; i++) { 1977 if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx) 1978 return i; 1979 } 1980 1981 return 0; 1982 } 1983 EXPORT_SYMBOL_GPL(mt76_get_rate); 1984 1985 void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1986 const u8 *mac) 1987 { 1988 struct mt76_phy *phy = hw->priv; 1989 1990 set_bit(MT76_SCANNING, &phy->state); 1991 } 1992 EXPORT_SYMBOL_GPL(mt76_sw_scan); 1993 1994 void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1995 { 1996 struct mt76_phy *phy = hw->priv; 1997 1998 clear_bit(MT76_SCANNING, &phy->state); 1999 } 2000 EXPORT_SYMBOL_GPL(mt76_sw_scan_complete); 2001 2002 int mt76_get_antenna(struct ieee80211_hw *hw, int radio_idx, u32 *tx_ant, 2003 u32 *rx_ant) 2004 { 2005 struct mt76_phy *phy = hw->priv; 2006 struct mt76_dev *dev = phy->dev; 2007 int i; 2008 2009 mutex_lock(&dev->mutex); 2010 *tx_ant = 0; 2011 for (i = 0; i < ARRAY_SIZE(dev->phys); i++) 2012 if (dev->phys[i] && dev->phys[i]->hw == hw) 2013 *tx_ant |= dev->phys[i]->chainmask; 2014 *rx_ant = *tx_ant; 2015 mutex_unlock(&dev->mutex); 2016 2017 return 0; 2018 } 2019 EXPORT_SYMBOL_GPL(mt76_get_antenna); 2020 2021 struct mt76_queue * 2022 mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc, 2023 int ring_base, void *wed, u32 flags) 2024 { 2025 struct mt76_queue *hwq; 2026 int err; 2027 2028 hwq = devm_kzalloc_obj(dev->dev, *hwq); 2029 if (!hwq) 2030 return ERR_PTR(-ENOMEM); 2031 2032 hwq->flags = flags; 2033 hwq->wed = wed; 2034 2035 err = dev->queue_ops->alloc(dev, hwq, idx, n_desc, 0, ring_base); 2036 if (err < 0) 2037 return ERR_PTR(err); 2038 2039 return hwq; 2040 } 2041 EXPORT_SYMBOL_GPL(mt76_init_queue); 2042 2043 void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi, 2044 struct mt76_sta_stats *stats, bool eht) 2045 { 2046 int i, ei = wi->initial_stat_idx; 2047 u64 *data = wi->data; 2048 2049 wi->sta_count++; 2050 2051 data[ei++] += stats->tx_mode[MT_PHY_TYPE_CCK]; 2052 data[ei++] += stats->tx_mode[MT_PHY_TYPE_OFDM]; 2053 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT]; 2054 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT_GF]; 2055 data[ei++] += stats->tx_mode[MT_PHY_TYPE_VHT]; 2056 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_SU]; 2057 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_EXT_SU]; 2058 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_TB]; 2059 data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_MU]; 2060 if (eht) { 2061 data[ei++] += stats->tx_mode[MT_PHY_TYPE_EHT_SU]; 2062 data[ei++] += stats->tx_mode[MT_PHY_TYPE_EHT_TRIG]; 2063 data[ei++] += stats->tx_mode[MT_PHY_TYPE_EHT_MU]; 2064 } 2065 2066 for (i = 0; i < (ARRAY_SIZE(stats->tx_bw) - !eht); i++) 2067 data[ei++] += stats->tx_bw[i]; 2068 2069 for (i = 0; i < (eht ? 14 : 12); i++) 2070 data[ei++] += stats->tx_mcs[i]; 2071 2072 for (i = 0; i < 4; i++) 2073 data[ei++] += stats->tx_nss[i]; 2074 2075 wi->worker_stat_count = ei - wi->initial_stat_idx; 2076 } 2077 EXPORT_SYMBOL_GPL(mt76_ethtool_worker); 2078 2079 void mt76_ethtool_page_pool_stats(struct mt76_dev *dev, u64 *data, int *index) 2080 { 2081 #ifdef CONFIG_PAGE_POOL_STATS 2082 struct page_pool_stats stats = {}; 2083 int i; 2084 2085 mt76_for_each_q_rx(dev, i) 2086 page_pool_get_stats(dev->q_rx[i].page_pool, &stats); 2087 2088 page_pool_ethtool_stats_get(data, &stats); 2089 *index += page_pool_ethtool_stats_get_count(); 2090 #endif 2091 } 2092 EXPORT_SYMBOL_GPL(mt76_ethtool_page_pool_stats); 2093 2094 enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy) 2095 { 2096 struct ieee80211_hw *hw = phy->hw; 2097 struct mt76_dev *dev = phy->dev; 2098 2099 if (dev->region == NL80211_DFS_UNSET || 2100 test_bit(MT76_SCANNING, &phy->state)) 2101 return MT_DFS_STATE_DISABLED; 2102 2103 if (!phy->radar_enabled) { 2104 if ((hw->conf.flags & IEEE80211_CONF_MONITOR) && 2105 (phy->chandef.chan->flags & IEEE80211_CHAN_RADAR)) 2106 return MT_DFS_STATE_ACTIVE; 2107 2108 return MT_DFS_STATE_DISABLED; 2109 } 2110 2111 if (!cfg80211_reg_can_beacon(hw->wiphy, &phy->chandef, NL80211_IFTYPE_AP)) 2112 return MT_DFS_STATE_CAC; 2113 2114 return MT_DFS_STATE_ACTIVE; 2115 } 2116 EXPORT_SYMBOL_GPL(mt76_phy_dfs_state); 2117 2118 void mt76_vif_cleanup(struct mt76_dev *dev, struct ieee80211_vif *vif) 2119 { 2120 struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv; 2121 struct mt76_vif_data *mvif = mlink->mvif; 2122 2123 rcu_assign_pointer(mvif->link[0], NULL); 2124 mt76_abort_scan(dev); 2125 if (mvif->roc_phy && mvif->roc_phy->roc_vif == vif) 2126 mt76_abort_roc(mvif->roc_phy); 2127 } 2128 EXPORT_SYMBOL_GPL(mt76_vif_cleanup); 2129 2130 u16 mt76_select_links(struct ieee80211_vif *vif, int max_active_links) 2131 { 2132 unsigned long usable_links = ieee80211_vif_usable_links(vif); 2133 struct { 2134 u8 link_id; 2135 enum nl80211_band band; 2136 } data[IEEE80211_MLD_MAX_NUM_LINKS]; 2137 unsigned int link_id; 2138 int i, n_data = 0; 2139 u16 sel_links = 0; 2140 2141 if (!ieee80211_vif_is_mld(vif)) 2142 return 0; 2143 2144 if (vif->active_links == usable_links) 2145 return vif->active_links; 2146 2147 rcu_read_lock(); 2148 for_each_set_bit(link_id, &usable_links, IEEE80211_MLD_MAX_NUM_LINKS) { 2149 struct ieee80211_bss_conf *link_conf; 2150 2151 link_conf = rcu_dereference(vif->link_conf[link_id]); 2152 if (WARN_ON_ONCE(!link_conf)) 2153 continue; 2154 2155 data[n_data].link_id = link_id; 2156 data[n_data].band = link_conf->chanreq.oper.chan->band; 2157 n_data++; 2158 } 2159 rcu_read_unlock(); 2160 2161 for (i = 0; i < n_data; i++) { 2162 int j; 2163 2164 if (!(BIT(data[i].link_id) & vif->active_links)) 2165 continue; 2166 2167 sel_links = BIT(data[i].link_id); 2168 for (j = 0; j < n_data; j++) { 2169 if (data[i].band != data[j].band) { 2170 sel_links |= BIT(data[j].link_id); 2171 if (hweight16(sel_links) == max_active_links) 2172 break; 2173 } 2174 } 2175 break; 2176 } 2177 2178 return sel_links; 2179 } 2180 EXPORT_SYMBOL_GPL(mt76_select_links); 2181 2182 struct mt76_offchannel_cb_data { 2183 struct mt76_phy *phy; 2184 bool offchannel; 2185 }; 2186 2187 static void 2188 mt76_offchannel_send_nullfunc(struct mt76_offchannel_cb_data *data, 2189 struct ieee80211_vif *vif, int link_id) 2190 { 2191 struct mt76_phy *phy = data->phy; 2192 struct ieee80211_tx_info *info; 2193 struct ieee80211_sta *sta = NULL; 2194 struct ieee80211_hdr *hdr; 2195 struct mt76_wcid *wcid; 2196 struct sk_buff *skb; 2197 2198 skb = ieee80211_nullfunc_get(phy->hw, vif, link_id, true); 2199 if (!skb) 2200 return; 2201 2202 hdr = (struct ieee80211_hdr *)skb->data; 2203 if (data->offchannel) 2204 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM); 2205 2206 skb->priority = 7; 2207 skb_set_queue_mapping(skb, IEEE80211_AC_VO); 2208 2209 if (!ieee80211_tx_prepare_skb(phy->hw, vif, skb, 2210 phy->main_chandef.chan->band, 2211 &sta)) 2212 return; 2213 2214 if (sta) 2215 wcid = (struct mt76_wcid *)sta->drv_priv; 2216 else 2217 wcid = ((struct mt76_vif_link *)vif->drv_priv)->wcid; 2218 2219 if (link_id >= 0) { 2220 info = IEEE80211_SKB_CB(skb); 2221 info->control.flags &= ~IEEE80211_TX_CTRL_MLO_LINK; 2222 info->control.flags |= 2223 u32_encode_bits(link_id, IEEE80211_TX_CTRL_MLO_LINK); 2224 } 2225 2226 mt76_tx(phy, sta, wcid, skb); 2227 } 2228 2229 static void 2230 mt76_offchannel_notify_iter(void *_data, u8 *mac, struct ieee80211_vif *vif) 2231 { 2232 struct mt76_offchannel_cb_data *data = _data; 2233 struct mt76_vif_link *mlink; 2234 struct mt76_vif_data *mvif; 2235 int link_id; 2236 2237 if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc) 2238 return; 2239 2240 mlink = (struct mt76_vif_link *)vif->drv_priv; 2241 mvif = mlink->mvif; 2242 2243 if (!ieee80211_vif_is_mld(vif)) { 2244 if (mt76_vif_link_phy(mlink) == data->phy) { 2245 if (!data->offchannel && mlink->beacon_mon_interval) 2246 WRITE_ONCE(mlink->beacon_mon_last, jiffies); 2247 mt76_offchannel_send_nullfunc(data, vif, -1); 2248 } 2249 return; 2250 } 2251 2252 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 2253 if (link_id == mvif->deflink_id) 2254 mlink = (struct mt76_vif_link *)vif->drv_priv; 2255 else 2256 mlink = rcu_dereference(mvif->link[link_id]); 2257 if (!mlink) 2258 continue; 2259 if (mt76_vif_link_phy(mlink) != data->phy) 2260 continue; 2261 2262 if (!data->offchannel && mlink->beacon_mon_interval) 2263 WRITE_ONCE(mlink->beacon_mon_last, jiffies); 2264 2265 mt76_offchannel_send_nullfunc(data, vif, link_id); 2266 } 2267 } 2268 2269 void mt76_offchannel_notify(struct mt76_phy *phy, bool offchannel) 2270 { 2271 struct mt76_offchannel_cb_data data = { 2272 .phy = phy, 2273 .offchannel = offchannel, 2274 }; 2275 2276 if (!phy->num_sta) 2277 return; 2278 2279 local_bh_disable(); 2280 ieee80211_iterate_active_interfaces_atomic(phy->hw, 2281 IEEE80211_IFACE_ITER_NORMAL, 2282 mt76_offchannel_notify_iter, &data); 2283 local_bh_enable(); 2284 } 2285 EXPORT_SYMBOL_GPL(mt76_offchannel_notify); 2286 2287 struct mt76_rx_beacon_data { 2288 struct mt76_phy *phy; 2289 const u8 *bssid; 2290 }; 2291 2292 static void mt76_rx_beacon_iter(void *_data, u8 *mac, 2293 struct ieee80211_vif *vif) 2294 { 2295 struct mt76_rx_beacon_data *data = _data; 2296 struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv; 2297 struct mt76_vif_data *mvif = mlink->mvif; 2298 int link_id; 2299 2300 if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc) 2301 return; 2302 2303 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 2304 struct ieee80211_bss_conf *link_conf; 2305 2306 if (link_id == mvif->deflink_id) 2307 mlink = (struct mt76_vif_link *)vif->drv_priv; 2308 else 2309 mlink = rcu_dereference(mvif->link[link_id]); 2310 if (!mlink || !mlink->beacon_mon_interval) 2311 continue; 2312 2313 if (mt76_vif_link_phy(mlink) != data->phy) 2314 continue; 2315 2316 link_conf = rcu_dereference(vif->link_conf[link_id]); 2317 if (!link_conf) 2318 continue; 2319 2320 if (!ether_addr_equal(link_conf->bssid, data->bssid) && 2321 (!link_conf->nontransmitted || 2322 !ether_addr_equal(link_conf->transmitter_bssid, 2323 data->bssid))) 2324 continue; 2325 2326 WRITE_ONCE(mlink->beacon_mon_last, jiffies); 2327 } 2328 } 2329 2330 void mt76_rx_beacon(struct mt76_phy *phy, struct sk_buff *skb) 2331 { 2332 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; 2333 struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb); 2334 struct mt76_rx_beacon_data data = { 2335 .phy = phy, 2336 .bssid = hdr->addr3, 2337 }; 2338 2339 mt76_scan_rx_beacon(phy, phy->chandef.chan); 2340 2341 if (!phy->num_sta) 2342 return; 2343 2344 if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_ONLY_MONITOR)) 2345 return; 2346 2347 ieee80211_iterate_active_interfaces_atomic(phy->hw, 2348 IEEE80211_IFACE_ITER_RESUME_ALL, 2349 mt76_rx_beacon_iter, &data); 2350 } 2351 EXPORT_SYMBOL_GPL(mt76_rx_beacon); 2352 2353 static void mt76_beacon_mon_iter(void *data, u8 *mac, 2354 struct ieee80211_vif *vif) 2355 { 2356 struct mt76_phy *phy = data; 2357 struct mt76_vif_link *mlink = (struct mt76_vif_link *)vif->drv_priv; 2358 struct mt76_vif_data *mvif = mlink->mvif; 2359 int link_id; 2360 2361 if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc) 2362 return; 2363 2364 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 2365 if (link_id == mvif->deflink_id) 2366 mlink = (struct mt76_vif_link *)vif->drv_priv; 2367 else 2368 mlink = rcu_dereference(mvif->link[link_id]); 2369 if (!mlink || !mlink->beacon_mon_interval) 2370 continue; 2371 2372 if (mt76_vif_link_phy(mlink) != phy) 2373 continue; 2374 2375 if (time_after(jiffies, 2376 READ_ONCE(mlink->beacon_mon_last) + 2377 MT76_BEACON_MON_MAX_MISS * mlink->beacon_mon_interval)) 2378 ieee80211_beacon_loss(vif); 2379 } 2380 } 2381 2382 void mt76_beacon_mon_check(struct mt76_phy *phy) 2383 { 2384 if (phy->offchannel) 2385 return; 2386 2387 ieee80211_iterate_active_interfaces_atomic(phy->hw, 2388 IEEE80211_IFACE_ITER_RESUME_ALL, 2389 mt76_beacon_mon_iter, phy); 2390 } 2391 EXPORT_SYMBOL_GPL(mt76_beacon_mon_check); 2392