developer | 0f312e8 | 2022-11-01 12:31:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: ISC |
| 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 | }; |
| 79 | |
| 80 | static const struct ieee80211_channel mt76_channels_6ghz[] = { |
| 81 | /* UNII-5 */ |
| 82 | CHAN6G(1, 5955), |
| 83 | CHAN6G(5, 5975), |
| 84 | CHAN6G(9, 5995), |
| 85 | CHAN6G(13, 6015), |
| 86 | CHAN6G(17, 6035), |
| 87 | CHAN6G(21, 6055), |
| 88 | CHAN6G(25, 6075), |
| 89 | CHAN6G(29, 6095), |
| 90 | CHAN6G(33, 6115), |
| 91 | CHAN6G(37, 6135), |
| 92 | CHAN6G(41, 6155), |
| 93 | CHAN6G(45, 6175), |
| 94 | CHAN6G(49, 6195), |
| 95 | CHAN6G(53, 6215), |
| 96 | CHAN6G(57, 6235), |
| 97 | CHAN6G(61, 6255), |
| 98 | CHAN6G(65, 6275), |
| 99 | CHAN6G(69, 6295), |
| 100 | CHAN6G(73, 6315), |
| 101 | CHAN6G(77, 6335), |
| 102 | CHAN6G(81, 6355), |
| 103 | CHAN6G(85, 6375), |
| 104 | CHAN6G(89, 6395), |
| 105 | CHAN6G(93, 6415), |
| 106 | /* UNII-6 */ |
| 107 | CHAN6G(97, 6435), |
| 108 | CHAN6G(101, 6455), |
| 109 | CHAN6G(105, 6475), |
| 110 | CHAN6G(109, 6495), |
| 111 | CHAN6G(113, 6515), |
| 112 | CHAN6G(117, 6535), |
| 113 | /* UNII-7 */ |
| 114 | CHAN6G(121, 6555), |
| 115 | CHAN6G(125, 6575), |
| 116 | CHAN6G(129, 6595), |
| 117 | CHAN6G(133, 6615), |
| 118 | CHAN6G(137, 6635), |
| 119 | CHAN6G(141, 6655), |
| 120 | CHAN6G(145, 6675), |
| 121 | CHAN6G(149, 6695), |
| 122 | CHAN6G(153, 6715), |
| 123 | CHAN6G(157, 6735), |
| 124 | CHAN6G(161, 6755), |
| 125 | CHAN6G(165, 6775), |
| 126 | CHAN6G(169, 6795), |
| 127 | CHAN6G(173, 6815), |
| 128 | CHAN6G(177, 6835), |
| 129 | CHAN6G(181, 6855), |
| 130 | CHAN6G(185, 6875), |
| 131 | /* UNII-8 */ |
| 132 | CHAN6G(189, 6895), |
| 133 | CHAN6G(193, 6915), |
| 134 | CHAN6G(197, 6935), |
| 135 | CHAN6G(201, 6955), |
| 136 | CHAN6G(205, 6975), |
| 137 | CHAN6G(209, 6995), |
| 138 | CHAN6G(213, 7015), |
| 139 | CHAN6G(217, 7035), |
| 140 | CHAN6G(221, 7055), |
| 141 | CHAN6G(225, 7075), |
| 142 | CHAN6G(229, 7095), |
| 143 | CHAN6G(233, 7115), |
| 144 | }; |
| 145 | |
| 146 | static const struct ieee80211_tpt_blink mt76_tpt_blink[] = { |
| 147 | { .throughput = 0 * 1024, .blink_time = 334 }, |
| 148 | { .throughput = 1 * 1024, .blink_time = 260 }, |
| 149 | { .throughput = 5 * 1024, .blink_time = 220 }, |
| 150 | { .throughput = 10 * 1024, .blink_time = 190 }, |
| 151 | { .throughput = 20 * 1024, .blink_time = 170 }, |
| 152 | { .throughput = 50 * 1024, .blink_time = 150 }, |
| 153 | { .throughput = 70 * 1024, .blink_time = 130 }, |
| 154 | { .throughput = 100 * 1024, .blink_time = 110 }, |
| 155 | { .throughput = 200 * 1024, .blink_time = 80 }, |
| 156 | { .throughput = 300 * 1024, .blink_time = 50 }, |
| 157 | }; |
| 158 | |
| 159 | struct ieee80211_rate mt76_rates[] = { |
| 160 | CCK_RATE(0, 10), |
| 161 | CCK_RATE(1, 20), |
| 162 | CCK_RATE(2, 55), |
| 163 | CCK_RATE(3, 110), |
| 164 | OFDM_RATE(11, 60), |
| 165 | OFDM_RATE(15, 90), |
| 166 | OFDM_RATE(10, 120), |
| 167 | OFDM_RATE(14, 180), |
| 168 | OFDM_RATE(9, 240), |
| 169 | OFDM_RATE(13, 360), |
| 170 | OFDM_RATE(8, 480), |
| 171 | OFDM_RATE(12, 540), |
| 172 | }; |
| 173 | EXPORT_SYMBOL_GPL(mt76_rates); |
| 174 | |
| 175 | static const struct cfg80211_sar_freq_ranges mt76_sar_freq_ranges[] = { |
| 176 | { .start_freq = 2402, .end_freq = 2494, }, |
| 177 | { .start_freq = 5150, .end_freq = 5350, }, |
| 178 | { .start_freq = 5350, .end_freq = 5470, }, |
| 179 | { .start_freq = 5470, .end_freq = 5725, }, |
| 180 | { .start_freq = 5725, .end_freq = 5950, }, |
| 181 | { .start_freq = 5945, .end_freq = 6165, }, |
| 182 | { .start_freq = 6165, .end_freq = 6405, }, |
| 183 | { .start_freq = 6405, .end_freq = 6525, }, |
| 184 | { .start_freq = 6525, .end_freq = 6705, }, |
| 185 | { .start_freq = 6705, .end_freq = 6865, }, |
| 186 | { .start_freq = 6865, .end_freq = 7125, }, |
| 187 | }; |
| 188 | |
| 189 | static const struct cfg80211_sar_capa mt76_sar_capa = { |
| 190 | .type = NL80211_SAR_TYPE_POWER, |
| 191 | .num_freq_ranges = ARRAY_SIZE(mt76_sar_freq_ranges), |
| 192 | .freq_ranges = &mt76_sar_freq_ranges[0], |
| 193 | }; |
| 194 | |
| 195 | static int mt76_led_init(struct mt76_dev *dev) |
| 196 | { |
| 197 | struct device_node *np = dev->dev->of_node; |
| 198 | struct ieee80211_hw *hw = dev->hw; |
| 199 | int led_pin; |
| 200 | |
| 201 | if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set) |
| 202 | return 0; |
| 203 | |
| 204 | snprintf(dev->led_name, sizeof(dev->led_name), |
| 205 | "mt76-%s", wiphy_name(hw->wiphy)); |
| 206 | |
| 207 | dev->led_cdev.name = dev->led_name; |
| 208 | dev->led_cdev.default_trigger = |
| 209 | ieee80211_create_tpt_led_trigger(hw, |
| 210 | IEEE80211_TPT_LEDTRIG_FL_RADIO, |
| 211 | mt76_tpt_blink, |
| 212 | ARRAY_SIZE(mt76_tpt_blink)); |
| 213 | |
| 214 | np = of_get_child_by_name(np, "led"); |
| 215 | if (np) { |
| 216 | if (!of_property_read_u32(np, "led-sources", &led_pin)) |
| 217 | dev->led_pin = led_pin; |
| 218 | dev->led_al = of_property_read_bool(np, "led-active-low"); |
| 219 | of_node_put(np); |
| 220 | } |
| 221 | |
| 222 | return led_classdev_register(dev->dev, &dev->led_cdev); |
| 223 | } |
| 224 | |
| 225 | static void mt76_led_cleanup(struct mt76_dev *dev) |
| 226 | { |
| 227 | if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set) |
| 228 | return; |
| 229 | |
| 230 | led_classdev_unregister(&dev->led_cdev); |
| 231 | } |
| 232 | |
| 233 | static void mt76_init_stream_cap(struct mt76_phy *phy, |
| 234 | struct ieee80211_supported_band *sband, |
| 235 | bool vht) |
| 236 | { |
| 237 | struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap; |
| 238 | int i, nstream = hweight8(phy->antenna_mask); |
| 239 | struct ieee80211_sta_vht_cap *vht_cap; |
| 240 | u16 mcs_map = 0; |
| 241 | |
| 242 | if (nstream > 1) |
| 243 | ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC; |
| 244 | else |
| 245 | ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC; |
| 246 | |
| 247 | for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) |
| 248 | ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0; |
| 249 | |
| 250 | if (!vht) |
| 251 | return; |
| 252 | |
| 253 | vht_cap = &sband->vht_cap; |
| 254 | if (nstream > 1) |
| 255 | vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC; |
| 256 | else |
| 257 | vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC; |
| 258 | vht_cap->cap |= IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN | |
| 259 | IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN; |
| 260 | |
| 261 | for (i = 0; i < 8; i++) { |
| 262 | if (i < nstream) |
| 263 | mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2)); |
| 264 | else |
| 265 | mcs_map |= |
| 266 | (IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2)); |
| 267 | } |
| 268 | vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map); |
| 269 | vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map); |
| 270 | if (ieee80211_hw_check(phy->hw, SUPPORTS_VHT_EXT_NSS_BW)) |
| 271 | vht_cap->vht_mcs.tx_highest |= |
| 272 | cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE); |
| 273 | } |
| 274 | |
| 275 | void mt76_set_stream_caps(struct mt76_phy *phy, bool vht) |
| 276 | { |
| 277 | if (phy->cap.has_2ghz) |
| 278 | mt76_init_stream_cap(phy, &phy->sband_2g.sband, false); |
| 279 | if (phy->cap.has_5ghz) |
| 280 | mt76_init_stream_cap(phy, &phy->sband_5g.sband, vht); |
| 281 | if (phy->cap.has_6ghz) |
| 282 | mt76_init_stream_cap(phy, &phy->sband_6g.sband, vht); |
| 283 | } |
| 284 | EXPORT_SYMBOL_GPL(mt76_set_stream_caps); |
| 285 | |
| 286 | static int |
| 287 | mt76_init_sband(struct mt76_phy *phy, struct mt76_sband *msband, |
| 288 | const struct ieee80211_channel *chan, int n_chan, |
| 289 | struct ieee80211_rate *rates, int n_rates, |
| 290 | bool ht, bool vht) |
| 291 | { |
| 292 | struct ieee80211_supported_band *sband = &msband->sband; |
| 293 | struct ieee80211_sta_vht_cap *vht_cap; |
| 294 | struct ieee80211_sta_ht_cap *ht_cap; |
| 295 | struct mt76_dev *dev = phy->dev; |
| 296 | void *chanlist; |
| 297 | int size; |
| 298 | |
| 299 | size = n_chan * sizeof(*chan); |
| 300 | chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL); |
| 301 | if (!chanlist) |
| 302 | return -ENOMEM; |
| 303 | |
| 304 | msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan), |
| 305 | GFP_KERNEL); |
| 306 | if (!msband->chan) |
| 307 | return -ENOMEM; |
| 308 | |
| 309 | sband->channels = chanlist; |
| 310 | sband->n_channels = n_chan; |
| 311 | sband->bitrates = rates; |
| 312 | sband->n_bitrates = n_rates; |
| 313 | |
| 314 | if (!ht) |
| 315 | return 0; |
| 316 | |
| 317 | ht_cap = &sband->ht_cap; |
| 318 | ht_cap->ht_supported = true; |
| 319 | ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 | |
| 320 | IEEE80211_HT_CAP_GRN_FLD | |
| 321 | IEEE80211_HT_CAP_SGI_20 | |
| 322 | IEEE80211_HT_CAP_SGI_40 | |
| 323 | (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT); |
| 324 | |
| 325 | ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED; |
| 326 | ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K; |
| 327 | |
| 328 | mt76_init_stream_cap(phy, sband, vht); |
| 329 | |
| 330 | if (!vht) |
| 331 | return 0; |
| 332 | |
| 333 | vht_cap = &sband->vht_cap; |
| 334 | vht_cap->vht_supported = true; |
| 335 | vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC | |
| 336 | IEEE80211_VHT_CAP_RXSTBC_1 | |
| 337 | IEEE80211_VHT_CAP_SHORT_GI_80 | |
| 338 | (3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int |
| 344 | mt76_init_sband_2g(struct mt76_phy *phy, struct ieee80211_rate *rates, |
| 345 | int n_rates) |
| 346 | { |
| 347 | phy->hw->wiphy->bands[NL80211_BAND_2GHZ] = &phy->sband_2g.sband; |
| 348 | |
| 349 | return mt76_init_sband(phy, &phy->sband_2g, mt76_channels_2ghz, |
| 350 | ARRAY_SIZE(mt76_channels_2ghz), rates, |
| 351 | n_rates, true, false); |
| 352 | } |
| 353 | |
| 354 | static int |
| 355 | mt76_init_sband_5g(struct mt76_phy *phy, struct ieee80211_rate *rates, |
| 356 | int n_rates, bool vht) |
| 357 | { |
| 358 | phy->hw->wiphy->bands[NL80211_BAND_5GHZ] = &phy->sband_5g.sband; |
| 359 | |
| 360 | return mt76_init_sband(phy, &phy->sband_5g, mt76_channels_5ghz, |
| 361 | ARRAY_SIZE(mt76_channels_5ghz), rates, |
| 362 | n_rates, true, vht); |
| 363 | } |
| 364 | |
| 365 | static int |
| 366 | mt76_init_sband_6g(struct mt76_phy *phy, struct ieee80211_rate *rates, |
| 367 | int n_rates) |
| 368 | { |
| 369 | phy->hw->wiphy->bands[NL80211_BAND_6GHZ] = &phy->sband_6g.sband; |
| 370 | |
| 371 | return mt76_init_sband(phy, &phy->sband_6g, mt76_channels_6ghz, |
| 372 | ARRAY_SIZE(mt76_channels_6ghz), rates, |
| 373 | n_rates, false, false); |
| 374 | } |
| 375 | |
| 376 | static void |
| 377 | mt76_check_sband(struct mt76_phy *phy, struct mt76_sband *msband, |
| 378 | enum nl80211_band band) |
| 379 | { |
| 380 | struct ieee80211_supported_band *sband = &msband->sband; |
| 381 | bool found = false; |
| 382 | int i; |
| 383 | |
| 384 | if (!sband) |
| 385 | return; |
| 386 | |
| 387 | for (i = 0; i < sband->n_channels; i++) { |
| 388 | if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED) |
| 389 | continue; |
| 390 | |
| 391 | found = true; |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | if (found) { |
| 396 | phy->chandef.chan = &sband->channels[0]; |
| 397 | phy->chan_state = &msband->chan[0]; |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | sband->n_channels = 0; |
| 402 | phy->hw->wiphy->bands[band] = NULL; |
| 403 | } |
| 404 | |
| 405 | static int |
| 406 | mt76_phy_init(struct mt76_phy *phy, struct ieee80211_hw *hw) |
| 407 | { |
| 408 | struct mt76_dev *dev = phy->dev; |
| 409 | struct wiphy *wiphy = hw->wiphy; |
| 410 | |
| 411 | SET_IEEE80211_DEV(hw, dev->dev); |
| 412 | SET_IEEE80211_PERM_ADDR(hw, phy->macaddr); |
| 413 | |
| 414 | wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR; |
| 415 | wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH | |
| 416 | WIPHY_FLAG_SUPPORTS_TDLS | |
| 417 | WIPHY_FLAG_AP_UAPSD; |
| 418 | |
| 419 | wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST); |
| 420 | wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS); |
| 421 | wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AQL); |
| 422 | |
| 423 | wiphy->available_antennas_tx = phy->antenna_mask; |
| 424 | wiphy->available_antennas_rx = phy->antenna_mask; |
| 425 | |
| 426 | wiphy->sar_capa = &mt76_sar_capa; |
| 427 | phy->frp = devm_kcalloc(dev->dev, wiphy->sar_capa->num_freq_ranges, |
| 428 | sizeof(struct mt76_freq_range_power), |
| 429 | GFP_KERNEL); |
| 430 | if (!phy->frp) |
| 431 | return -ENOMEM; |
| 432 | |
| 433 | hw->txq_data_size = sizeof(struct mt76_txq); |
| 434 | hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL; |
| 435 | |
| 436 | if (!hw->max_tx_fragments) |
| 437 | hw->max_tx_fragments = 16; |
| 438 | |
| 439 | ieee80211_hw_set(hw, SIGNAL_DBM); |
| 440 | ieee80211_hw_set(hw, AMPDU_AGGREGATION); |
| 441 | ieee80211_hw_set(hw, SUPPORTS_RC_TABLE); |
| 442 | ieee80211_hw_set(hw, SUPPORT_FAST_XMIT); |
| 443 | ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS); |
| 444 | ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU); |
| 445 | ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER); |
| 446 | |
| 447 | if (!(dev->drv->drv_flags & MT_DRV_AMSDU_OFFLOAD)) { |
| 448 | ieee80211_hw_set(hw, TX_AMSDU); |
| 449 | ieee80211_hw_set(hw, TX_FRAG_LIST); |
| 450 | } |
| 451 | |
| 452 | ieee80211_hw_set(hw, MFP_CAPABLE); |
| 453 | ieee80211_hw_set(hw, AP_LINK_PS); |
| 454 | ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS); |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | struct mt76_phy * |
| 460 | mt76_alloc_phy(struct mt76_dev *dev, unsigned int size, |
| 461 | const struct ieee80211_ops *ops, u8 band_idx) |
| 462 | { |
| 463 | struct ieee80211_hw *hw; |
| 464 | unsigned int phy_size; |
| 465 | struct mt76_phy *phy; |
| 466 | |
| 467 | phy_size = ALIGN(sizeof(*phy), 8); |
| 468 | hw = ieee80211_alloc_hw(size + phy_size, ops); |
| 469 | if (!hw) |
| 470 | return NULL; |
| 471 | |
| 472 | phy = hw->priv; |
| 473 | phy->dev = dev; |
| 474 | phy->hw = hw; |
| 475 | phy->priv = hw->priv + phy_size; |
| 476 | phy->band_idx = band_idx; |
| 477 | |
| 478 | hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN; |
| 479 | hw->wiphy->interface_modes = |
| 480 | BIT(NL80211_IFTYPE_STATION) | |
| 481 | BIT(NL80211_IFTYPE_AP) | |
| 482 | #ifdef CONFIG_MAC80211_MESH |
| 483 | BIT(NL80211_IFTYPE_MESH_POINT) | |
| 484 | #endif |
| 485 | BIT(NL80211_IFTYPE_P2P_CLIENT) | |
| 486 | BIT(NL80211_IFTYPE_P2P_GO) | |
| 487 | BIT(NL80211_IFTYPE_ADHOC); |
| 488 | |
| 489 | return phy; |
| 490 | } |
| 491 | EXPORT_SYMBOL_GPL(mt76_alloc_phy); |
| 492 | |
| 493 | int mt76_register_phy(struct mt76_phy *phy, bool vht, |
| 494 | struct ieee80211_rate *rates, int n_rates) |
| 495 | { |
| 496 | int ret; |
| 497 | |
| 498 | ret = mt76_phy_init(phy, phy->hw); |
| 499 | if (ret) |
| 500 | return ret; |
| 501 | |
| 502 | if (phy->cap.has_2ghz) { |
| 503 | ret = mt76_init_sband_2g(phy, rates, n_rates); |
| 504 | if (ret) |
| 505 | return ret; |
| 506 | } |
| 507 | |
| 508 | if (phy->cap.has_5ghz) { |
| 509 | ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht); |
| 510 | if (ret) |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | if (phy->cap.has_6ghz) { |
| 515 | ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4); |
| 516 | if (ret) |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | wiphy_read_of_freq_limits(phy->hw->wiphy); |
| 521 | mt76_check_sband(phy, &phy->sband_2g, NL80211_BAND_2GHZ); |
| 522 | mt76_check_sband(phy, &phy->sband_5g, NL80211_BAND_5GHZ); |
| 523 | mt76_check_sband(phy, &phy->sband_6g, NL80211_BAND_6GHZ); |
| 524 | |
| 525 | ret = ieee80211_register_hw(phy->hw); |
| 526 | if (ret) |
| 527 | return ret; |
| 528 | |
| 529 | phy->dev->phys[phy->band_idx] = phy; |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | EXPORT_SYMBOL_GPL(mt76_register_phy); |
| 534 | |
| 535 | void mt76_unregister_phy(struct mt76_phy *phy) |
| 536 | { |
| 537 | struct mt76_dev *dev = phy->dev; |
| 538 | |
| 539 | mt76_tx_status_check(dev, true); |
| 540 | ieee80211_unregister_hw(phy->hw); |
| 541 | dev->phys[phy->band_idx] = NULL; |
| 542 | } |
| 543 | EXPORT_SYMBOL_GPL(mt76_unregister_phy); |
| 544 | |
| 545 | struct mt76_dev * |
| 546 | mt76_alloc_device(struct device *pdev, unsigned int size, |
| 547 | const struct ieee80211_ops *ops, |
| 548 | const struct mt76_driver_ops *drv_ops) |
| 549 | { |
| 550 | struct ieee80211_hw *hw; |
| 551 | struct mt76_phy *phy; |
| 552 | struct mt76_dev *dev; |
| 553 | int i; |
| 554 | |
| 555 | hw = ieee80211_alloc_hw(size, ops); |
| 556 | if (!hw) |
| 557 | return NULL; |
| 558 | |
| 559 | dev = hw->priv; |
| 560 | dev->hw = hw; |
| 561 | dev->dev = pdev; |
| 562 | dev->drv = drv_ops; |
| 563 | dev->dma_dev = pdev; |
| 564 | |
| 565 | phy = &dev->phy; |
| 566 | phy->dev = dev; |
| 567 | phy->hw = hw; |
| 568 | phy->band_idx = MT_BAND0; |
| 569 | dev->phys[phy->band_idx] = phy; |
| 570 | |
| 571 | spin_lock_init(&dev->rx_lock); |
| 572 | spin_lock_init(&dev->lock); |
| 573 | spin_lock_init(&dev->cc_lock); |
| 574 | spin_lock_init(&dev->status_lock); |
| 575 | mutex_init(&dev->mutex); |
| 576 | init_waitqueue_head(&dev->tx_wait); |
| 577 | |
| 578 | skb_queue_head_init(&dev->mcu.res_q); |
| 579 | init_waitqueue_head(&dev->mcu.wait); |
| 580 | mutex_init(&dev->mcu.mutex); |
| 581 | dev->tx_worker.fn = mt76_tx_worker; |
| 582 | |
| 583 | hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN; |
| 584 | hw->wiphy->interface_modes = |
| 585 | BIT(NL80211_IFTYPE_STATION) | |
| 586 | BIT(NL80211_IFTYPE_AP) | |
| 587 | #ifdef CONFIG_MAC80211_MESH |
| 588 | BIT(NL80211_IFTYPE_MESH_POINT) | |
| 589 | #endif |
| 590 | BIT(NL80211_IFTYPE_P2P_CLIENT) | |
| 591 | BIT(NL80211_IFTYPE_P2P_GO) | |
| 592 | BIT(NL80211_IFTYPE_ADHOC); |
| 593 | |
| 594 | spin_lock_init(&dev->token_lock); |
| 595 | idr_init(&dev->token); |
| 596 | |
| 597 | INIT_LIST_HEAD(&dev->wcid_list); |
| 598 | |
| 599 | INIT_LIST_HEAD(&dev->txwi_cache); |
| 600 | dev->token_size = dev->drv->token_size; |
| 601 | |
| 602 | for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++) |
| 603 | skb_queue_head_init(&dev->rx_skb[i]); |
| 604 | |
| 605 | dev->wq = alloc_ordered_workqueue("mt76", 0); |
| 606 | if (!dev->wq) { |
| 607 | ieee80211_free_hw(hw); |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | return dev; |
| 612 | } |
| 613 | EXPORT_SYMBOL_GPL(mt76_alloc_device); |
| 614 | |
| 615 | int mt76_register_device(struct mt76_dev *dev, bool vht, |
| 616 | struct ieee80211_rate *rates, int n_rates) |
| 617 | { |
| 618 | struct ieee80211_hw *hw = dev->hw; |
| 619 | struct mt76_phy *phy = &dev->phy; |
| 620 | int ret; |
| 621 | |
| 622 | dev_set_drvdata(dev->dev, dev); |
| 623 | ret = mt76_phy_init(phy, hw); |
| 624 | if (ret) |
| 625 | return ret; |
| 626 | |
| 627 | if (phy->cap.has_2ghz) { |
| 628 | ret = mt76_init_sband_2g(phy, rates, n_rates); |
| 629 | if (ret) |
| 630 | return ret; |
| 631 | } |
| 632 | |
| 633 | if (phy->cap.has_5ghz) { |
| 634 | ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht); |
| 635 | if (ret) |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | if (phy->cap.has_6ghz) { |
| 640 | ret = mt76_init_sband_6g(phy, rates + 4, n_rates - 4); |
| 641 | if (ret) |
| 642 | return ret; |
| 643 | } |
| 644 | |
| 645 | wiphy_read_of_freq_limits(hw->wiphy); |
| 646 | mt76_check_sband(&dev->phy, &phy->sband_2g, NL80211_BAND_2GHZ); |
| 647 | mt76_check_sband(&dev->phy, &phy->sband_5g, NL80211_BAND_5GHZ); |
| 648 | mt76_check_sband(&dev->phy, &phy->sband_6g, NL80211_BAND_6GHZ); |
| 649 | |
| 650 | if (IS_ENABLED(CONFIG_MT76_LEDS)) { |
| 651 | ret = mt76_led_init(dev); |
| 652 | if (ret) |
| 653 | return ret; |
| 654 | } |
| 655 | |
| 656 | ret = ieee80211_register_hw(hw); |
| 657 | if (ret) |
| 658 | return ret; |
| 659 | |
| 660 | WARN_ON(mt76_worker_setup(hw, &dev->tx_worker, NULL, "tx")); |
| 661 | sched_set_fifo_low(dev->tx_worker.task); |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | EXPORT_SYMBOL_GPL(mt76_register_device); |
| 666 | |
| 667 | void mt76_unregister_device(struct mt76_dev *dev) |
| 668 | { |
| 669 | struct ieee80211_hw *hw = dev->hw; |
| 670 | |
| 671 | if (IS_ENABLED(CONFIG_MT76_LEDS)) |
| 672 | mt76_led_cleanup(dev); |
| 673 | mt76_tx_status_check(dev, true); |
| 674 | ieee80211_unregister_hw(hw); |
| 675 | } |
| 676 | EXPORT_SYMBOL_GPL(mt76_unregister_device); |
| 677 | |
| 678 | void mt76_free_device(struct mt76_dev *dev) |
| 679 | { |
| 680 | mt76_worker_teardown(&dev->tx_worker); |
| 681 | if (dev->wq) { |
| 682 | destroy_workqueue(dev->wq); |
| 683 | dev->wq = NULL; |
| 684 | } |
| 685 | ieee80211_free_hw(dev->hw); |
| 686 | } |
| 687 | EXPORT_SYMBOL_GPL(mt76_free_device); |
| 688 | |
| 689 | static void mt76_rx_release_amsdu(struct mt76_phy *phy, enum mt76_rxq_id q) |
| 690 | { |
| 691 | struct sk_buff *skb = phy->rx_amsdu[q].head; |
| 692 | struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; |
| 693 | struct mt76_dev *dev = phy->dev; |
| 694 | |
| 695 | phy->rx_amsdu[q].head = NULL; |
| 696 | phy->rx_amsdu[q].tail = NULL; |
| 697 | |
| 698 | /* |
| 699 | * Validate if the amsdu has a proper first subframe. |
| 700 | * A single MSDU can be parsed as A-MSDU when the unauthenticated A-MSDU |
| 701 | * flag of the QoS header gets flipped. In such cases, the first |
| 702 | * subframe has a LLC/SNAP header in the location of the destination |
| 703 | * address. |
| 704 | */ |
| 705 | if (skb_shinfo(skb)->frag_list) { |
| 706 | int offset = 0; |
| 707 | |
| 708 | if (!(status->flag & RX_FLAG_8023)) { |
| 709 | offset = ieee80211_get_hdrlen_from_skb(skb); |
| 710 | |
| 711 | if ((status->flag & |
| 712 | (RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED)) == |
| 713 | RX_FLAG_DECRYPTED) |
| 714 | offset += 8; |
| 715 | } |
| 716 | |
| 717 | if (ether_addr_equal(skb->data + offset, rfc1042_header)) { |
| 718 | dev_kfree_skb(skb); |
| 719 | return; |
| 720 | } |
| 721 | } |
| 722 | __skb_queue_tail(&dev->rx_skb[q], skb); |
| 723 | } |
| 724 | |
| 725 | static void mt76_rx_release_burst(struct mt76_phy *phy, enum mt76_rxq_id q, |
| 726 | struct sk_buff *skb) |
| 727 | { |
| 728 | struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; |
| 729 | |
| 730 | if (phy->rx_amsdu[q].head && |
| 731 | (!status->amsdu || status->first_amsdu || |
| 732 | status->seqno != phy->rx_amsdu[q].seqno)) |
| 733 | mt76_rx_release_amsdu(phy, q); |
| 734 | |
| 735 | if (!phy->rx_amsdu[q].head) { |
| 736 | phy->rx_amsdu[q].tail = &skb_shinfo(skb)->frag_list; |
| 737 | phy->rx_amsdu[q].seqno = status->seqno; |
| 738 | phy->rx_amsdu[q].head = skb; |
| 739 | } else { |
| 740 | *phy->rx_amsdu[q].tail = skb; |
| 741 | phy->rx_amsdu[q].tail = &skb->next; |
| 742 | } |
| 743 | |
| 744 | if (!status->amsdu || status->last_amsdu) |
| 745 | mt76_rx_release_amsdu(phy, q); |
| 746 | } |
| 747 | |
| 748 | void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb) |
| 749 | { |
| 750 | struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; |
| 751 | struct mt76_phy *phy = mt76_dev_phy(dev, status->phy_idx); |
| 752 | |
| 753 | if (!test_bit(MT76_STATE_RUNNING, &phy->state)) { |
| 754 | dev_kfree_skb(skb); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | #ifdef CONFIG_NL80211_TESTMODE |
| 759 | if (phy->test.state == MT76_TM_STATE_RX_FRAMES) { |
| 760 | phy->test.rx_stats.packets[q]++; |
| 761 | if (status->flag & RX_FLAG_FAILED_FCS_CRC) |
| 762 | phy->test.rx_stats.fcs_error[q]++; |
| 763 | } |
| 764 | #endif |
| 765 | |
| 766 | mt76_rx_release_burst(phy, q, skb); |
| 767 | } |
| 768 | EXPORT_SYMBOL_GPL(mt76_rx); |
| 769 | |
| 770 | bool mt76_has_tx_pending(struct mt76_phy *phy) |
| 771 | { |
| 772 | struct mt76_queue *q; |
| 773 | int i; |
| 774 | |
| 775 | for (i = 0; i < __MT_TXQ_MAX; i++) { |
| 776 | q = phy->q_tx[i]; |
| 777 | if (q && q->queued) |
| 778 | return true; |
| 779 | } |
| 780 | |
| 781 | return false; |
| 782 | } |
| 783 | EXPORT_SYMBOL_GPL(mt76_has_tx_pending); |
| 784 | |
| 785 | static struct mt76_channel_state * |
| 786 | mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c) |
| 787 | { |
| 788 | struct mt76_sband *msband; |
| 789 | int idx; |
| 790 | |
| 791 | if (c->band == NL80211_BAND_2GHZ) |
| 792 | msband = &phy->sband_2g; |
| 793 | else if (c->band == NL80211_BAND_6GHZ) |
| 794 | msband = &phy->sband_6g; |
| 795 | else |
| 796 | msband = &phy->sband_5g; |
| 797 | |
| 798 | idx = c - &msband->sband.channels[0]; |
| 799 | return &msband->chan[idx]; |
| 800 | } |
| 801 | |
| 802 | void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time) |
| 803 | { |
| 804 | struct mt76_channel_state *state = phy->chan_state; |
| 805 | |
| 806 | state->cc_active += ktime_to_us(ktime_sub(time, |
| 807 | phy->survey_time)); |
| 808 | phy->survey_time = time; |
| 809 | } |
| 810 | EXPORT_SYMBOL_GPL(mt76_update_survey_active_time); |
| 811 | |
| 812 | void mt76_update_survey(struct mt76_phy *phy) |
| 813 | { |
| 814 | struct mt76_dev *dev = phy->dev; |
| 815 | ktime_t cur_time; |
| 816 | |
| 817 | if (dev->drv->update_survey) |
| 818 | dev->drv->update_survey(phy); |
| 819 | |
| 820 | cur_time = ktime_get_boottime(); |
| 821 | mt76_update_survey_active_time(phy, cur_time); |
| 822 | |
| 823 | if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) { |
| 824 | struct mt76_channel_state *state = phy->chan_state; |
| 825 | |
| 826 | spin_lock_bh(&dev->cc_lock); |
| 827 | state->cc_bss_rx += dev->cur_cc_bss_rx; |
| 828 | dev->cur_cc_bss_rx = 0; |
| 829 | spin_unlock_bh(&dev->cc_lock); |
| 830 | } |
| 831 | } |
| 832 | EXPORT_SYMBOL_GPL(mt76_update_survey); |
| 833 | |
| 834 | void mt76_set_channel(struct mt76_phy *phy) |
| 835 | { |
| 836 | struct mt76_dev *dev = phy->dev; |
| 837 | struct ieee80211_hw *hw = phy->hw; |
| 838 | struct cfg80211_chan_def *chandef = &hw->conf.chandef; |
| 839 | bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL; |
| 840 | int timeout = HZ / 5; |
| 841 | |
| 842 | wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout); |
| 843 | mt76_update_survey(phy); |
| 844 | |
| 845 | if (phy->chandef.chan->center_freq != chandef->chan->center_freq || |
| 846 | phy->chandef.width != chandef->width) |
| 847 | phy->dfs_state = MT_DFS_STATE_UNKNOWN; |
| 848 | |
| 849 | phy->chandef = *chandef; |
| 850 | phy->chan_state = mt76_channel_state(phy, chandef->chan); |
| 851 | |
| 852 | if (!offchannel) |
| 853 | phy->main_chan = chandef->chan; |
| 854 | |
| 855 | if (chandef->chan != phy->main_chan) |
| 856 | memset(phy->chan_state, 0, sizeof(*phy->chan_state)); |
| 857 | } |
| 858 | EXPORT_SYMBOL_GPL(mt76_set_channel); |
| 859 | |
| 860 | int mt76_get_survey(struct ieee80211_hw *hw, int idx, |
| 861 | struct survey_info *survey) |
| 862 | { |
| 863 | struct mt76_phy *phy = hw->priv; |
| 864 | struct mt76_dev *dev = phy->dev; |
| 865 | struct mt76_sband *sband; |
| 866 | struct ieee80211_channel *chan; |
| 867 | struct mt76_channel_state *state; |
| 868 | int ret = 0; |
| 869 | |
| 870 | mutex_lock(&dev->mutex); |
| 871 | if (idx == 0 && dev->drv->update_survey) |
| 872 | mt76_update_survey(phy); |
| 873 | |
| 874 | if (idx >= phy->sband_2g.sband.n_channels + |
| 875 | phy->sband_5g.sband.n_channels) { |
| 876 | idx -= (phy->sband_2g.sband.n_channels + |
| 877 | phy->sband_5g.sband.n_channels); |
| 878 | sband = &phy->sband_6g; |
| 879 | } else if (idx >= phy->sband_2g.sband.n_channels) { |
| 880 | idx -= phy->sband_2g.sband.n_channels; |
| 881 | sband = &phy->sband_5g; |
| 882 | } else { |
| 883 | sband = &phy->sband_2g; |
| 884 | } |
| 885 | |
| 886 | if (idx >= sband->sband.n_channels) { |
| 887 | ret = -ENOENT; |
| 888 | goto out; |
| 889 | } |
| 890 | |
| 891 | chan = &sband->sband.channels[idx]; |
| 892 | state = mt76_channel_state(phy, chan); |
| 893 | |
| 894 | memset(survey, 0, sizeof(*survey)); |
| 895 | survey->channel = chan; |
| 896 | survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY; |
| 897 | survey->filled |= dev->drv->survey_flags; |
| 898 | if (state->noise) |
| 899 | survey->filled |= SURVEY_INFO_NOISE_DBM; |
| 900 | |
| 901 | if (chan == phy->main_chan) { |
| 902 | survey->filled |= SURVEY_INFO_IN_USE; |
| 903 | |
| 904 | if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) |
| 905 | survey->filled |= SURVEY_INFO_TIME_BSS_RX; |
| 906 | } |
| 907 | |
| 908 | survey->time_busy = div_u64(state->cc_busy, 1000); |
| 909 | survey->time_rx = div_u64(state->cc_rx, 1000); |
| 910 | survey->time = div_u64(state->cc_active, 1000); |
| 911 | survey->noise = state->noise; |
| 912 | |
| 913 | spin_lock_bh(&dev->cc_lock); |
| 914 | survey->time_bss_rx = div_u64(state->cc_bss_rx, 1000); |
| 915 | survey->time_tx = div_u64(state->cc_tx, 1000); |
| 916 | spin_unlock_bh(&dev->cc_lock); |
| 917 | |
| 918 | out: |
| 919 | mutex_unlock(&dev->mutex); |
| 920 | |
| 921 | return ret; |
| 922 | } |
| 923 | EXPORT_SYMBOL_GPL(mt76_get_survey); |
| 924 | |
| 925 | void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid, |
| 926 | struct ieee80211_key_conf *key) |
| 927 | { |
| 928 | struct ieee80211_key_seq seq; |
| 929 | int i; |
| 930 | |
| 931 | wcid->rx_check_pn = false; |
| 932 | |
| 933 | if (!key) |
| 934 | return; |
| 935 | |
| 936 | if (key->cipher != WLAN_CIPHER_SUITE_CCMP) |
| 937 | return; |
| 938 | |
| 939 | wcid->rx_check_pn = true; |
| 940 | |
| 941 | /* data frame */ |
| 942 | for (i = 0; i < IEEE80211_NUM_TIDS; i++) { |
| 943 | ieee80211_get_key_rx_seq(key, i, &seq); |
| 944 | memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn)); |
| 945 | } |
| 946 | |
| 947 | /* robust management frame */ |
| 948 | ieee80211_get_key_rx_seq(key, -1, &seq); |
| 949 | memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn)); |
| 950 | |
| 951 | } |
| 952 | EXPORT_SYMBOL(mt76_wcid_key_setup); |
| 953 | |
| 954 | static int |
| 955 | mt76_rx_signal(struct mt76_rx_status *status) |
| 956 | { |
| 957 | s8 *chain_signal = status->chain_signal; |
| 958 | int signal = -128; |
| 959 | u8 chains; |
| 960 | |
| 961 | for (chains = status->chains; chains; chains >>= 1, chain_signal++) { |
| 962 | int cur, diff; |
| 963 | |
| 964 | cur = *chain_signal; |
| 965 | if (!(chains & BIT(0)) || |
| 966 | cur > 0) |
| 967 | continue; |
| 968 | |
| 969 | if (cur > signal) |
| 970 | swap(cur, signal); |
| 971 | |
| 972 | diff = signal - cur; |
| 973 | if (diff == 0) |
| 974 | signal += 3; |
| 975 | else if (diff <= 2) |
| 976 | signal += 2; |
| 977 | else if (diff <= 6) |
| 978 | signal += 1; |
| 979 | } |
| 980 | |
| 981 | return signal; |
| 982 | } |
| 983 | |
| 984 | static void |
| 985 | mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb, |
| 986 | struct ieee80211_hw **hw, |
| 987 | struct ieee80211_sta **sta) |
| 988 | { |
| 989 | struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); |
| 990 | struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb); |
| 991 | struct mt76_rx_status mstat; |
| 992 | |
| 993 | mstat = *((struct mt76_rx_status *)skb->cb); |
| 994 | memset(status, 0, sizeof(*status)); |
| 995 | |
| 996 | status->flag = mstat.flag; |
| 997 | status->freq = mstat.freq; |
| 998 | status->enc_flags = mstat.enc_flags; |
| 999 | status->encoding = mstat.encoding; |
| 1000 | status->bw = mstat.bw; |
| 1001 | status->he_ru = mstat.he_ru; |
| 1002 | status->he_gi = mstat.he_gi; |
| 1003 | status->he_dcm = mstat.he_dcm; |
| 1004 | status->rate_idx = mstat.rate_idx; |
| 1005 | status->nss = mstat.nss; |
| 1006 | status->band = mstat.band; |
| 1007 | status->signal = mstat.signal; |
| 1008 | status->chains = mstat.chains; |
| 1009 | status->ampdu_reference = mstat.ampdu_ref; |
| 1010 | status->device_timestamp = mstat.timestamp; |
| 1011 | status->mactime = mstat.timestamp; |
| 1012 | status->signal = mt76_rx_signal(&mstat); |
| 1013 | if (status->signal <= -128) |
| 1014 | status->flag |= RX_FLAG_NO_SIGNAL_VAL; |
| 1015 | |
| 1016 | if (ieee80211_is_beacon(hdr->frame_control) || |
| 1017 | ieee80211_is_probe_resp(hdr->frame_control)) |
| 1018 | status->boottime_ns = ktime_get_boottime_ns(); |
| 1019 | |
| 1020 | BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb)); |
| 1021 | BUILD_BUG_ON(sizeof(status->chain_signal) != |
| 1022 | sizeof(mstat.chain_signal)); |
| 1023 | memcpy(status->chain_signal, mstat.chain_signal, |
| 1024 | sizeof(mstat.chain_signal)); |
| 1025 | |
| 1026 | *sta = wcid_to_sta(mstat.wcid); |
| 1027 | *hw = mt76_phy_hw(dev, mstat.phy_idx); |
| 1028 | } |
| 1029 | |
| 1030 | static void |
| 1031 | mt76_check_ccmp_pn(struct sk_buff *skb) |
| 1032 | { |
| 1033 | struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; |
| 1034 | struct mt76_wcid *wcid = status->wcid; |
| 1035 | struct ieee80211_hdr *hdr; |
| 1036 | int security_idx; |
| 1037 | int ret; |
| 1038 | |
| 1039 | if (!(status->flag & RX_FLAG_DECRYPTED)) |
| 1040 | return; |
| 1041 | |
| 1042 | if (status->flag & RX_FLAG_ONLY_MONITOR) |
| 1043 | return; |
| 1044 | |
| 1045 | if (!wcid || !wcid->rx_check_pn) |
| 1046 | return; |
| 1047 | |
| 1048 | security_idx = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK; |
| 1049 | if (status->flag & RX_FLAG_8023) |
| 1050 | goto skip_hdr_check; |
| 1051 | |
| 1052 | hdr = mt76_skb_get_hdr(skb); |
| 1053 | if (!(status->flag & RX_FLAG_IV_STRIPPED)) { |
| 1054 | /* |
| 1055 | * Validate the first fragment both here and in mac80211 |
| 1056 | * All further fragments will be validated by mac80211 only. |
| 1057 | */ |
| 1058 | if (ieee80211_is_frag(hdr) && |
| 1059 | !ieee80211_is_first_frag(hdr->frame_control)) |
| 1060 | return; |
| 1061 | } |
| 1062 | |
| 1063 | /* IEEE 802.11-2020, 12.5.3.4.4 "PN and replay detection" c): |
| 1064 | * |
| 1065 | * the recipient shall maintain a single replay counter for received |
| 1066 | * individually addressed robust Management frames that are received |
| 1067 | * with the To DS subfield equal to 0, [...] |
| 1068 | */ |
| 1069 | if (ieee80211_is_mgmt(hdr->frame_control) && |
| 1070 | !ieee80211_has_tods(hdr->frame_control)) |
| 1071 | security_idx = IEEE80211_NUM_TIDS; |
| 1072 | |
| 1073 | skip_hdr_check: |
| 1074 | BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0])); |
| 1075 | ret = memcmp(status->iv, wcid->rx_key_pn[security_idx], |
| 1076 | sizeof(status->iv)); |
| 1077 | if (ret <= 0) { |
| 1078 | status->flag |= RX_FLAG_ONLY_MONITOR; |
| 1079 | return; |
| 1080 | } |
| 1081 | |
| 1082 | memcpy(wcid->rx_key_pn[security_idx], status->iv, sizeof(status->iv)); |
| 1083 | |
| 1084 | if (status->flag & RX_FLAG_IV_STRIPPED) |
| 1085 | status->flag |= RX_FLAG_PN_VALIDATED; |
| 1086 | } |
| 1087 | |
| 1088 | static void |
| 1089 | mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status, |
| 1090 | int len) |
| 1091 | { |
| 1092 | struct mt76_wcid *wcid = status->wcid; |
| 1093 | struct ieee80211_rx_status info = { |
| 1094 | .enc_flags = status->enc_flags, |
| 1095 | .rate_idx = status->rate_idx, |
| 1096 | .encoding = status->encoding, |
| 1097 | .band = status->band, |
| 1098 | .nss = status->nss, |
| 1099 | .bw = status->bw, |
| 1100 | }; |
| 1101 | struct ieee80211_sta *sta; |
| 1102 | u32 airtime; |
| 1103 | u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK; |
| 1104 | |
| 1105 | airtime = ieee80211_calc_rx_airtime(dev->hw, &info, len); |
| 1106 | spin_lock(&dev->cc_lock); |
| 1107 | dev->cur_cc_bss_rx += airtime; |
| 1108 | spin_unlock(&dev->cc_lock); |
| 1109 | |
| 1110 | if (!wcid || !wcid->sta) |
| 1111 | return; |
| 1112 | |
| 1113 | sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv); |
| 1114 | ieee80211_sta_register_airtime(sta, tidno, 0, airtime); |
| 1115 | } |
| 1116 | |
| 1117 | static void |
| 1118 | mt76_airtime_flush_ampdu(struct mt76_dev *dev) |
| 1119 | { |
| 1120 | struct mt76_wcid *wcid; |
| 1121 | int wcid_idx; |
| 1122 | |
| 1123 | if (!dev->rx_ampdu_len) |
| 1124 | return; |
| 1125 | |
| 1126 | wcid_idx = dev->rx_ampdu_status.wcid_idx; |
| 1127 | if (wcid_idx < ARRAY_SIZE(dev->wcid)) |
| 1128 | wcid = rcu_dereference(dev->wcid[wcid_idx]); |
| 1129 | else |
| 1130 | wcid = NULL; |
| 1131 | dev->rx_ampdu_status.wcid = wcid; |
| 1132 | |
| 1133 | mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len); |
| 1134 | |
| 1135 | dev->rx_ampdu_len = 0; |
| 1136 | dev->rx_ampdu_ref = 0; |
| 1137 | } |
| 1138 | |
| 1139 | static void |
| 1140 | mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb) |
| 1141 | { |
| 1142 | struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; |
| 1143 | struct mt76_wcid *wcid = status->wcid; |
| 1144 | |
| 1145 | if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)) |
| 1146 | return; |
| 1147 | |
| 1148 | if (!wcid || !wcid->sta) { |
| 1149 | struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb); |
| 1150 | |
| 1151 | if (status->flag & RX_FLAG_8023) |
| 1152 | return; |
| 1153 | |
| 1154 | if (!ether_addr_equal(hdr->addr1, dev->phy.macaddr)) |
| 1155 | return; |
| 1156 | |
| 1157 | wcid = NULL; |
| 1158 | } |
| 1159 | |
| 1160 | if (!(status->flag & RX_FLAG_AMPDU_DETAILS) || |
| 1161 | status->ampdu_ref != dev->rx_ampdu_ref) |
| 1162 | mt76_airtime_flush_ampdu(dev); |
| 1163 | |
| 1164 | if (status->flag & RX_FLAG_AMPDU_DETAILS) { |
| 1165 | if (!dev->rx_ampdu_len || |
| 1166 | status->ampdu_ref != dev->rx_ampdu_ref) { |
| 1167 | dev->rx_ampdu_status = *status; |
| 1168 | dev->rx_ampdu_status.wcid_idx = wcid ? wcid->idx : 0xff; |
| 1169 | dev->rx_ampdu_ref = status->ampdu_ref; |
| 1170 | } |
| 1171 | |
| 1172 | dev->rx_ampdu_len += skb->len; |
| 1173 | return; |
| 1174 | } |
| 1175 | |
| 1176 | mt76_airtime_report(dev, status, skb->len); |
| 1177 | } |
| 1178 | |
| 1179 | static void |
| 1180 | mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb) |
| 1181 | { |
| 1182 | struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; |
| 1183 | struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb); |
| 1184 | struct ieee80211_sta *sta; |
| 1185 | struct ieee80211_hw *hw; |
| 1186 | struct mt76_wcid *wcid = status->wcid; |
| 1187 | u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK; |
| 1188 | bool ps; |
| 1189 | |
| 1190 | hw = mt76_phy_hw(dev, status->phy_idx); |
| 1191 | if (ieee80211_is_pspoll(hdr->frame_control) && !wcid && |
| 1192 | !(status->flag & RX_FLAG_8023)) { |
| 1193 | sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL); |
| 1194 | if (sta) |
| 1195 | wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv; |
| 1196 | } |
| 1197 | |
| 1198 | mt76_airtime_check(dev, skb); |
| 1199 | |
| 1200 | if (!wcid || !wcid->sta) |
| 1201 | return; |
| 1202 | |
| 1203 | sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv); |
| 1204 | |
| 1205 | if (status->signal <= 0) |
| 1206 | ewma_signal_add(&wcid->rssi, -status->signal); |
| 1207 | |
| 1208 | wcid->inactive_count = 0; |
| 1209 | |
| 1210 | if (status->flag & RX_FLAG_8023) |
| 1211 | return; |
| 1212 | |
| 1213 | if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags)) |
| 1214 | return; |
| 1215 | |
| 1216 | if (ieee80211_is_pspoll(hdr->frame_control)) { |
| 1217 | ieee80211_sta_pspoll(sta); |
| 1218 | return; |
| 1219 | } |
| 1220 | |
| 1221 | if (ieee80211_has_morefrags(hdr->frame_control) || |
| 1222 | !(ieee80211_is_mgmt(hdr->frame_control) || |
| 1223 | ieee80211_is_data(hdr->frame_control))) |
| 1224 | return; |
| 1225 | |
| 1226 | ps = ieee80211_has_pm(hdr->frame_control); |
| 1227 | |
| 1228 | if (ps && (ieee80211_is_data_qos(hdr->frame_control) || |
| 1229 | ieee80211_is_qos_nullfunc(hdr->frame_control))) |
| 1230 | ieee80211_sta_uapsd_trigger(sta, tidno); |
| 1231 | |
| 1232 | if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps) |
| 1233 | return; |
| 1234 | |
| 1235 | if (ps) |
| 1236 | set_bit(MT_WCID_FLAG_PS, &wcid->flags); |
| 1237 | |
| 1238 | dev->drv->sta_ps(dev, sta, ps); |
| 1239 | |
| 1240 | if (!ps) |
| 1241 | clear_bit(MT_WCID_FLAG_PS, &wcid->flags); |
| 1242 | |
| 1243 | ieee80211_sta_ps_transition(sta, ps); |
| 1244 | } |
| 1245 | |
| 1246 | void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames, |
| 1247 | struct napi_struct *napi) |
| 1248 | { |
| 1249 | struct ieee80211_sta *sta; |
| 1250 | struct ieee80211_hw *hw; |
| 1251 | struct sk_buff *skb, *tmp; |
| 1252 | LIST_HEAD(list); |
| 1253 | |
| 1254 | spin_lock(&dev->rx_lock); |
| 1255 | while ((skb = __skb_dequeue(frames)) != NULL) { |
| 1256 | struct sk_buff *nskb = skb_shinfo(skb)->frag_list; |
| 1257 | |
| 1258 | mt76_check_ccmp_pn(skb); |
| 1259 | skb_shinfo(skb)->frag_list = NULL; |
| 1260 | mt76_rx_convert(dev, skb, &hw, &sta); |
| 1261 | ieee80211_rx_list(hw, sta, skb, &list); |
| 1262 | |
| 1263 | /* subsequent amsdu frames */ |
| 1264 | while (nskb) { |
| 1265 | skb = nskb; |
| 1266 | nskb = nskb->next; |
| 1267 | skb->next = NULL; |
| 1268 | |
| 1269 | mt76_rx_convert(dev, skb, &hw, &sta); |
| 1270 | ieee80211_rx_list(hw, sta, skb, &list); |
| 1271 | } |
| 1272 | } |
| 1273 | spin_unlock(&dev->rx_lock); |
| 1274 | |
| 1275 | if (!napi) { |
| 1276 | netif_receive_skb_list(&list); |
| 1277 | return; |
| 1278 | } |
| 1279 | |
| 1280 | list_for_each_entry_safe(skb, tmp, &list, list) { |
| 1281 | skb_list_del_init(skb); |
| 1282 | napi_gro_receive(napi, skb); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q, |
| 1287 | struct napi_struct *napi) |
| 1288 | { |
| 1289 | struct sk_buff_head frames; |
| 1290 | struct sk_buff *skb; |
| 1291 | |
| 1292 | __skb_queue_head_init(&frames); |
| 1293 | |
| 1294 | while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) { |
| 1295 | mt76_check_sta(dev, skb); |
| 1296 | mt76_rx_aggr_reorder(skb, &frames); |
| 1297 | } |
| 1298 | |
| 1299 | mt76_rx_complete(dev, &frames, napi); |
| 1300 | } |
| 1301 | EXPORT_SYMBOL_GPL(mt76_rx_poll_complete); |
| 1302 | |
| 1303 | static int |
| 1304 | mt76_sta_add(struct mt76_phy *phy, struct ieee80211_vif *vif, |
| 1305 | struct ieee80211_sta *sta) |
| 1306 | { |
| 1307 | struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv; |
| 1308 | struct mt76_dev *dev = phy->dev; |
| 1309 | int ret; |
| 1310 | int i; |
| 1311 | |
| 1312 | mutex_lock(&dev->mutex); |
| 1313 | |
| 1314 | ret = dev->drv->sta_add(dev, vif, sta); |
| 1315 | if (ret) |
| 1316 | goto out; |
| 1317 | |
| 1318 | for (i = 0; i < ARRAY_SIZE(sta->txq); i++) { |
| 1319 | struct mt76_txq *mtxq; |
| 1320 | |
| 1321 | if (!sta->txq[i]) |
| 1322 | continue; |
| 1323 | |
| 1324 | mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv; |
| 1325 | mtxq->wcid = wcid->idx; |
| 1326 | } |
| 1327 | |
| 1328 | ewma_signal_init(&wcid->rssi); |
| 1329 | if (phy->band_idx == MT_BAND1) |
| 1330 | mt76_wcid_mask_set(dev->wcid_phy_mask, wcid->idx); |
| 1331 | wcid->phy_idx = phy->band_idx; |
| 1332 | rcu_assign_pointer(dev->wcid[wcid->idx], wcid); |
| 1333 | |
| 1334 | mt76_packet_id_init(wcid); |
| 1335 | out: |
| 1336 | mutex_unlock(&dev->mutex); |
| 1337 | |
| 1338 | return ret; |
| 1339 | } |
| 1340 | |
| 1341 | void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif, |
| 1342 | struct ieee80211_sta *sta) |
| 1343 | { |
| 1344 | struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv; |
| 1345 | int i, idx = wcid->idx; |
| 1346 | |
| 1347 | for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++) |
| 1348 | mt76_rx_aggr_stop(dev, wcid, i); |
| 1349 | |
| 1350 | if (dev->drv->sta_remove) |
| 1351 | dev->drv->sta_remove(dev, vif, sta); |
| 1352 | |
| 1353 | mt76_packet_id_flush(dev, wcid); |
| 1354 | |
| 1355 | mt76_wcid_mask_clear(dev->wcid_mask, idx); |
| 1356 | mt76_wcid_mask_clear(dev->wcid_phy_mask, idx); |
| 1357 | } |
| 1358 | EXPORT_SYMBOL_GPL(__mt76_sta_remove); |
| 1359 | |
| 1360 | static void |
| 1361 | mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif, |
| 1362 | struct ieee80211_sta *sta) |
| 1363 | { |
| 1364 | mutex_lock(&dev->mutex); |
| 1365 | __mt76_sta_remove(dev, vif, sta); |
| 1366 | mutex_unlock(&dev->mutex); |
| 1367 | } |
| 1368 | |
| 1369 | int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif, |
| 1370 | struct ieee80211_sta *sta, |
| 1371 | enum ieee80211_sta_state old_state, |
| 1372 | enum ieee80211_sta_state new_state) |
| 1373 | { |
| 1374 | struct mt76_phy *phy = hw->priv; |
| 1375 | struct mt76_dev *dev = phy->dev; |
| 1376 | |
| 1377 | if (old_state == IEEE80211_STA_NOTEXIST && |
| 1378 | new_state == IEEE80211_STA_NONE) |
| 1379 | return mt76_sta_add(phy, vif, sta); |
| 1380 | |
| 1381 | if (old_state == IEEE80211_STA_AUTH && |
| 1382 | new_state == IEEE80211_STA_ASSOC && |
| 1383 | dev->drv->sta_assoc) |
| 1384 | dev->drv->sta_assoc(dev, vif, sta); |
| 1385 | |
| 1386 | if (old_state == IEEE80211_STA_NONE && |
| 1387 | new_state == IEEE80211_STA_NOTEXIST) |
| 1388 | mt76_sta_remove(dev, vif, sta); |
| 1389 | |
| 1390 | return 0; |
| 1391 | } |
| 1392 | EXPORT_SYMBOL_GPL(mt76_sta_state); |
| 1393 | |
| 1394 | void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif, |
| 1395 | struct ieee80211_sta *sta) |
| 1396 | { |
| 1397 | struct mt76_phy *phy = hw->priv; |
| 1398 | struct mt76_dev *dev = phy->dev; |
| 1399 | struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv; |
| 1400 | |
| 1401 | mutex_lock(&dev->mutex); |
| 1402 | spin_lock_bh(&dev->status_lock); |
| 1403 | rcu_assign_pointer(dev->wcid[wcid->idx], NULL); |
| 1404 | spin_unlock_bh(&dev->status_lock); |
| 1405 | mutex_unlock(&dev->mutex); |
| 1406 | } |
| 1407 | EXPORT_SYMBOL_GPL(mt76_sta_pre_rcu_remove); |
| 1408 | |
| 1409 | int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif, |
| 1410 | int *dbm) |
| 1411 | { |
| 1412 | struct mt76_phy *phy = hw->priv; |
| 1413 | int n_chains = hweight8(phy->antenna_mask); |
| 1414 | int delta = mt76_tx_power_nss_delta(n_chains); |
| 1415 | |
| 1416 | *dbm = DIV_ROUND_UP(phy->txpower_cur + delta, 2); |
| 1417 | |
| 1418 | return 0; |
| 1419 | } |
| 1420 | EXPORT_SYMBOL_GPL(mt76_get_txpower); |
| 1421 | |
| 1422 | int mt76_init_sar_power(struct ieee80211_hw *hw, |
| 1423 | const struct cfg80211_sar_specs *sar) |
| 1424 | { |
| 1425 | struct mt76_phy *phy = hw->priv; |
| 1426 | const struct cfg80211_sar_capa *capa = hw->wiphy->sar_capa; |
| 1427 | int i; |
| 1428 | |
| 1429 | if (sar->type != NL80211_SAR_TYPE_POWER || !sar->num_sub_specs) |
| 1430 | return -EINVAL; |
| 1431 | |
| 1432 | for (i = 0; i < sar->num_sub_specs; i++) { |
| 1433 | u32 index = sar->sub_specs[i].freq_range_index; |
| 1434 | /* SAR specifies power limitaton in 0.25dbm */ |
| 1435 | s32 power = sar->sub_specs[i].power >> 1; |
| 1436 | |
| 1437 | if (power > 127 || power < -127) |
| 1438 | power = 127; |
| 1439 | |
| 1440 | phy->frp[index].range = &capa->freq_ranges[index]; |
| 1441 | phy->frp[index].power = power; |
| 1442 | } |
| 1443 | |
| 1444 | return 0; |
| 1445 | } |
| 1446 | EXPORT_SYMBOL_GPL(mt76_init_sar_power); |
| 1447 | |
| 1448 | int mt76_get_sar_power(struct mt76_phy *phy, |
| 1449 | struct ieee80211_channel *chan, |
| 1450 | int power) |
| 1451 | { |
| 1452 | const struct cfg80211_sar_capa *capa = phy->hw->wiphy->sar_capa; |
| 1453 | int freq, i; |
| 1454 | |
| 1455 | if (!capa || !phy->frp) |
| 1456 | return power; |
| 1457 | |
| 1458 | if (power > 127 || power < -127) |
| 1459 | power = 127; |
| 1460 | |
| 1461 | freq = ieee80211_channel_to_frequency(chan->hw_value, chan->band); |
| 1462 | for (i = 0 ; i < capa->num_freq_ranges; i++) { |
| 1463 | if (phy->frp[i].range && |
| 1464 | freq >= phy->frp[i].range->start_freq && |
| 1465 | freq < phy->frp[i].range->end_freq) { |
| 1466 | power = min_t(int, phy->frp[i].power, power); |
| 1467 | break; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | return power; |
| 1472 | } |
| 1473 | EXPORT_SYMBOL_GPL(mt76_get_sar_power); |
| 1474 | |
| 1475 | static void |
| 1476 | __mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif) |
| 1477 | { |
| 1478 | if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif)) |
| 1479 | ieee80211_csa_finish(vif); |
| 1480 | } |
| 1481 | |
| 1482 | void mt76_csa_finish(struct mt76_dev *dev) |
| 1483 | { |
| 1484 | if (!dev->csa_complete) |
| 1485 | return; |
| 1486 | |
| 1487 | ieee80211_iterate_active_interfaces_atomic(dev->hw, |
| 1488 | IEEE80211_IFACE_ITER_RESUME_ALL, |
| 1489 | __mt76_csa_finish, dev); |
| 1490 | |
| 1491 | dev->csa_complete = 0; |
| 1492 | } |
| 1493 | EXPORT_SYMBOL_GPL(mt76_csa_finish); |
| 1494 | |
| 1495 | static void |
| 1496 | __mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif) |
| 1497 | { |
| 1498 | struct mt76_dev *dev = priv; |
| 1499 | |
| 1500 | if (!vif->csa_active) |
| 1501 | return; |
| 1502 | |
| 1503 | dev->csa_complete |= ieee80211_beacon_cntdwn_is_complete(vif); |
| 1504 | } |
| 1505 | |
| 1506 | void mt76_csa_check(struct mt76_dev *dev) |
| 1507 | { |
| 1508 | ieee80211_iterate_active_interfaces_atomic(dev->hw, |
| 1509 | IEEE80211_IFACE_ITER_RESUME_ALL, |
| 1510 | __mt76_csa_check, dev); |
| 1511 | } |
| 1512 | EXPORT_SYMBOL_GPL(mt76_csa_check); |
| 1513 | |
| 1514 | int |
| 1515 | mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set) |
| 1516 | { |
| 1517 | return 0; |
| 1518 | } |
| 1519 | EXPORT_SYMBOL_GPL(mt76_set_tim); |
| 1520 | |
| 1521 | void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id) |
| 1522 | { |
| 1523 | struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb; |
| 1524 | int hdr_len = ieee80211_get_hdrlen_from_skb(skb); |
| 1525 | u8 *hdr, *pn = status->iv; |
| 1526 | |
| 1527 | __skb_push(skb, 8); |
| 1528 | memmove(skb->data, skb->data + 8, hdr_len); |
| 1529 | hdr = skb->data + hdr_len; |
| 1530 | |
| 1531 | hdr[0] = pn[5]; |
| 1532 | hdr[1] = pn[4]; |
| 1533 | hdr[2] = 0; |
| 1534 | hdr[3] = 0x20 | (key_id << 6); |
| 1535 | hdr[4] = pn[3]; |
| 1536 | hdr[5] = pn[2]; |
| 1537 | hdr[6] = pn[1]; |
| 1538 | hdr[7] = pn[0]; |
| 1539 | |
| 1540 | status->flag &= ~RX_FLAG_IV_STRIPPED; |
| 1541 | } |
| 1542 | EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr); |
| 1543 | |
| 1544 | int mt76_get_rate(struct mt76_dev *dev, |
| 1545 | struct ieee80211_supported_band *sband, |
| 1546 | int idx, bool cck) |
| 1547 | { |
| 1548 | int i, offset = 0, len = sband->n_bitrates; |
| 1549 | |
| 1550 | if (cck) { |
| 1551 | if (sband != &dev->phy.sband_2g.sband) |
| 1552 | return 0; |
| 1553 | |
| 1554 | idx &= ~BIT(2); /* short preamble */ |
| 1555 | } else if (sband == &dev->phy.sband_2g.sband) { |
| 1556 | offset = 4; |
| 1557 | } |
| 1558 | |
| 1559 | for (i = offset; i < len; i++) { |
| 1560 | if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx) |
| 1561 | return i; |
| 1562 | } |
| 1563 | |
| 1564 | return 0; |
| 1565 | } |
| 1566 | EXPORT_SYMBOL_GPL(mt76_get_rate); |
| 1567 | |
| 1568 | void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, |
| 1569 | const u8 *mac) |
| 1570 | { |
| 1571 | struct mt76_phy *phy = hw->priv; |
| 1572 | |
| 1573 | set_bit(MT76_SCANNING, &phy->state); |
| 1574 | } |
| 1575 | EXPORT_SYMBOL_GPL(mt76_sw_scan); |
| 1576 | |
| 1577 | void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif) |
| 1578 | { |
| 1579 | struct mt76_phy *phy = hw->priv; |
| 1580 | |
| 1581 | clear_bit(MT76_SCANNING, &phy->state); |
| 1582 | } |
| 1583 | EXPORT_SYMBOL_GPL(mt76_sw_scan_complete); |
| 1584 | |
| 1585 | int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant) |
| 1586 | { |
| 1587 | struct mt76_phy *phy = hw->priv; |
| 1588 | struct mt76_dev *dev = phy->dev; |
| 1589 | |
| 1590 | mutex_lock(&dev->mutex); |
| 1591 | *tx_ant = phy->antenna_mask; |
| 1592 | *rx_ant = phy->antenna_mask; |
| 1593 | mutex_unlock(&dev->mutex); |
| 1594 | |
| 1595 | return 0; |
| 1596 | } |
| 1597 | EXPORT_SYMBOL_GPL(mt76_get_antenna); |
| 1598 | |
| 1599 | struct mt76_queue * |
| 1600 | mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc, |
| 1601 | int ring_base) |
| 1602 | { |
| 1603 | struct mt76_queue *hwq; |
| 1604 | int err; |
| 1605 | |
| 1606 | hwq = devm_kzalloc(dev->dev, sizeof(*hwq), GFP_KERNEL); |
| 1607 | if (!hwq) |
| 1608 | return ERR_PTR(-ENOMEM); |
| 1609 | |
| 1610 | err = dev->queue_ops->alloc(dev, hwq, idx, n_desc, 0, ring_base); |
| 1611 | if (err < 0) |
| 1612 | return ERR_PTR(err); |
| 1613 | |
| 1614 | return hwq; |
| 1615 | } |
| 1616 | EXPORT_SYMBOL_GPL(mt76_init_queue); |
| 1617 | |
| 1618 | u16 mt76_calculate_default_rate(struct mt76_phy *phy, int rateidx) |
| 1619 | { |
| 1620 | int offset = 0; |
| 1621 | |
| 1622 | if (phy->chandef.chan->band != NL80211_BAND_2GHZ) |
| 1623 | offset = 4; |
| 1624 | |
| 1625 | /* pick the lowest rate for hidden nodes */ |
| 1626 | if (rateidx < 0) |
| 1627 | rateidx = 0; |
| 1628 | |
| 1629 | rateidx += offset; |
| 1630 | if (rateidx >= ARRAY_SIZE(mt76_rates)) |
| 1631 | rateidx = offset; |
| 1632 | |
| 1633 | return mt76_rates[rateidx].hw_value; |
| 1634 | } |
| 1635 | EXPORT_SYMBOL_GPL(mt76_calculate_default_rate); |
| 1636 | |
| 1637 | void mt76_ethtool_worker(struct mt76_ethtool_worker_info *wi, |
| 1638 | struct mt76_sta_stats *stats) |
| 1639 | { |
| 1640 | int i, ei = wi->initial_stat_idx; |
| 1641 | u64 *data = wi->data; |
| 1642 | |
| 1643 | wi->sta_count++; |
| 1644 | |
| 1645 | data[ei++] += stats->tx_mode[MT_PHY_TYPE_CCK]; |
| 1646 | data[ei++] += stats->tx_mode[MT_PHY_TYPE_OFDM]; |
| 1647 | data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT]; |
| 1648 | data[ei++] += stats->tx_mode[MT_PHY_TYPE_HT_GF]; |
| 1649 | data[ei++] += stats->tx_mode[MT_PHY_TYPE_VHT]; |
| 1650 | data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_SU]; |
| 1651 | data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_EXT_SU]; |
| 1652 | data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_TB]; |
| 1653 | data[ei++] += stats->tx_mode[MT_PHY_TYPE_HE_MU]; |
| 1654 | |
| 1655 | for (i = 0; i < ARRAY_SIZE(stats->tx_bw); i++) |
| 1656 | data[ei++] += stats->tx_bw[i]; |
| 1657 | |
| 1658 | for (i = 0; i < 12; i++) |
| 1659 | data[ei++] += stats->tx_mcs[i]; |
| 1660 | |
| 1661 | wi->worker_stat_count = ei - wi->initial_stat_idx; |
| 1662 | } |
| 1663 | EXPORT_SYMBOL_GPL(mt76_ethtool_worker); |
| 1664 | |
| 1665 | enum mt76_dfs_state mt76_phy_dfs_state(struct mt76_phy *phy) |
| 1666 | { |
| 1667 | struct ieee80211_hw *hw = phy->hw; |
| 1668 | struct mt76_dev *dev = phy->dev; |
| 1669 | |
| 1670 | if (dev->region == NL80211_DFS_UNSET || |
| 1671 | test_bit(MT76_SCANNING, &phy->state)) |
| 1672 | return MT_DFS_STATE_DISABLED; |
| 1673 | |
| 1674 | if (!hw->conf.radar_enabled) { |
| 1675 | if ((hw->conf.flags & IEEE80211_CONF_MONITOR) && |
| 1676 | (phy->chandef.chan->flags & IEEE80211_CHAN_RADAR)) |
| 1677 | return MT_DFS_STATE_ACTIVE; |
| 1678 | |
| 1679 | return MT_DFS_STATE_DISABLED; |
| 1680 | } |
| 1681 | |
| 1682 | if (!cfg80211_reg_can_beacon(hw->wiphy, &phy->chandef, NL80211_IFTYPE_AP)) |
| 1683 | return MT_DFS_STATE_CAC; |
| 1684 | |
| 1685 | return MT_DFS_STATE_ACTIVE; |
| 1686 | } |
| 1687 | EXPORT_SYMBOL_GPL(mt76_phy_dfs_state); |