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