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