1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright(c) 2019-2020 Realtek Corporation 3 */ 4 #include <linux/ip.h> 5 #include <linux/udp.h> 6 7 #include "cam.h" 8 #include "coex.h" 9 #include "core.h" 10 #include "efuse.h" 11 #include "fw.h" 12 #include "mac.h" 13 #include "phy.h" 14 #include "ps.h" 15 #include "reg.h" 16 #include "sar.h" 17 #include "ser.h" 18 #include "txrx.h" 19 #include "util.h" 20 21 static bool rtw89_disable_ps_mode; 22 module_param_named(disable_ps_mode, rtw89_disable_ps_mode, bool, 0644); 23 MODULE_PARM_DESC(disable_ps_mode, "Set Y to disable low power mode"); 24 25 #define RTW89_DEF_CHAN(_freq, _hw_val, _flags, _band) \ 26 { .center_freq = _freq, .hw_value = _hw_val, .flags = _flags, .band = _band, } 27 #define RTW89_DEF_CHAN_2G(_freq, _hw_val) \ 28 RTW89_DEF_CHAN(_freq, _hw_val, 0, NL80211_BAND_2GHZ) 29 #define RTW89_DEF_CHAN_5G(_freq, _hw_val) \ 30 RTW89_DEF_CHAN(_freq, _hw_val, 0, NL80211_BAND_5GHZ) 31 #define RTW89_DEF_CHAN_5G_NO_HT40MINUS(_freq, _hw_val) \ 32 RTW89_DEF_CHAN(_freq, _hw_val, IEEE80211_CHAN_NO_HT40MINUS, NL80211_BAND_5GHZ) 33 #define RTW89_DEF_CHAN_6G(_freq, _hw_val) \ 34 RTW89_DEF_CHAN(_freq, _hw_val, 0, NL80211_BAND_6GHZ) 35 36 static struct ieee80211_channel rtw89_channels_2ghz[] = { 37 RTW89_DEF_CHAN_2G(2412, 1), 38 RTW89_DEF_CHAN_2G(2417, 2), 39 RTW89_DEF_CHAN_2G(2422, 3), 40 RTW89_DEF_CHAN_2G(2427, 4), 41 RTW89_DEF_CHAN_2G(2432, 5), 42 RTW89_DEF_CHAN_2G(2437, 6), 43 RTW89_DEF_CHAN_2G(2442, 7), 44 RTW89_DEF_CHAN_2G(2447, 8), 45 RTW89_DEF_CHAN_2G(2452, 9), 46 RTW89_DEF_CHAN_2G(2457, 10), 47 RTW89_DEF_CHAN_2G(2462, 11), 48 RTW89_DEF_CHAN_2G(2467, 12), 49 RTW89_DEF_CHAN_2G(2472, 13), 50 RTW89_DEF_CHAN_2G(2484, 14), 51 }; 52 53 static struct ieee80211_channel rtw89_channels_5ghz[] = { 54 RTW89_DEF_CHAN_5G(5180, 36), 55 RTW89_DEF_CHAN_5G(5200, 40), 56 RTW89_DEF_CHAN_5G(5220, 44), 57 RTW89_DEF_CHAN_5G(5240, 48), 58 RTW89_DEF_CHAN_5G(5260, 52), 59 RTW89_DEF_CHAN_5G(5280, 56), 60 RTW89_DEF_CHAN_5G(5300, 60), 61 RTW89_DEF_CHAN_5G(5320, 64), 62 RTW89_DEF_CHAN_5G(5500, 100), 63 RTW89_DEF_CHAN_5G(5520, 104), 64 RTW89_DEF_CHAN_5G(5540, 108), 65 RTW89_DEF_CHAN_5G(5560, 112), 66 RTW89_DEF_CHAN_5G(5580, 116), 67 RTW89_DEF_CHAN_5G(5600, 120), 68 RTW89_DEF_CHAN_5G(5620, 124), 69 RTW89_DEF_CHAN_5G(5640, 128), 70 RTW89_DEF_CHAN_5G(5660, 132), 71 RTW89_DEF_CHAN_5G(5680, 136), 72 RTW89_DEF_CHAN_5G(5700, 140), 73 RTW89_DEF_CHAN_5G(5720, 144), 74 RTW89_DEF_CHAN_5G(5745, 149), 75 RTW89_DEF_CHAN_5G(5765, 153), 76 RTW89_DEF_CHAN_5G(5785, 157), 77 RTW89_DEF_CHAN_5G(5805, 161), 78 RTW89_DEF_CHAN_5G_NO_HT40MINUS(5825, 165), 79 }; 80 81 static struct ieee80211_channel rtw89_channels_6ghz[] = { 82 RTW89_DEF_CHAN_6G(5955, 1), 83 RTW89_DEF_CHAN_6G(5975, 5), 84 RTW89_DEF_CHAN_6G(5995, 9), 85 RTW89_DEF_CHAN_6G(6015, 13), 86 RTW89_DEF_CHAN_6G(6035, 17), 87 RTW89_DEF_CHAN_6G(6055, 21), 88 RTW89_DEF_CHAN_6G(6075, 25), 89 RTW89_DEF_CHAN_6G(6095, 29), 90 RTW89_DEF_CHAN_6G(6115, 33), 91 RTW89_DEF_CHAN_6G(6135, 37), 92 RTW89_DEF_CHAN_6G(6155, 41), 93 RTW89_DEF_CHAN_6G(6175, 45), 94 RTW89_DEF_CHAN_6G(6195, 49), 95 RTW89_DEF_CHAN_6G(6215, 53), 96 RTW89_DEF_CHAN_6G(6235, 57), 97 RTW89_DEF_CHAN_6G(6255, 61), 98 RTW89_DEF_CHAN_6G(6275, 65), 99 RTW89_DEF_CHAN_6G(6295, 69), 100 RTW89_DEF_CHAN_6G(6315, 73), 101 RTW89_DEF_CHAN_6G(6335, 77), 102 RTW89_DEF_CHAN_6G(6355, 81), 103 RTW89_DEF_CHAN_6G(6375, 85), 104 RTW89_DEF_CHAN_6G(6395, 89), 105 RTW89_DEF_CHAN_6G(6415, 93), 106 RTW89_DEF_CHAN_6G(6435, 97), 107 RTW89_DEF_CHAN_6G(6455, 101), 108 RTW89_DEF_CHAN_6G(6475, 105), 109 RTW89_DEF_CHAN_6G(6495, 109), 110 RTW89_DEF_CHAN_6G(6515, 113), 111 RTW89_DEF_CHAN_6G(6535, 117), 112 RTW89_DEF_CHAN_6G(6555, 121), 113 RTW89_DEF_CHAN_6G(6575, 125), 114 RTW89_DEF_CHAN_6G(6595, 129), 115 RTW89_DEF_CHAN_6G(6615, 133), 116 RTW89_DEF_CHAN_6G(6635, 137), 117 RTW89_DEF_CHAN_6G(6655, 141), 118 RTW89_DEF_CHAN_6G(6675, 145), 119 RTW89_DEF_CHAN_6G(6695, 149), 120 RTW89_DEF_CHAN_6G(6715, 153), 121 RTW89_DEF_CHAN_6G(6735, 157), 122 RTW89_DEF_CHAN_6G(6755, 161), 123 RTW89_DEF_CHAN_6G(6775, 165), 124 RTW89_DEF_CHAN_6G(6795, 169), 125 RTW89_DEF_CHAN_6G(6815, 173), 126 RTW89_DEF_CHAN_6G(6835, 177), 127 RTW89_DEF_CHAN_6G(6855, 181), 128 RTW89_DEF_CHAN_6G(6875, 185), 129 RTW89_DEF_CHAN_6G(6895, 189), 130 RTW89_DEF_CHAN_6G(6915, 193), 131 RTW89_DEF_CHAN_6G(6935, 197), 132 RTW89_DEF_CHAN_6G(6955, 201), 133 RTW89_DEF_CHAN_6G(6975, 205), 134 RTW89_DEF_CHAN_6G(6995, 209), 135 RTW89_DEF_CHAN_6G(7015, 213), 136 RTW89_DEF_CHAN_6G(7035, 217), 137 RTW89_DEF_CHAN_6G(7055, 221), 138 RTW89_DEF_CHAN_6G(7075, 225), 139 RTW89_DEF_CHAN_6G(7095, 229), 140 RTW89_DEF_CHAN_6G(7115, 233), 141 }; 142 143 static struct ieee80211_rate rtw89_bitrates[] = { 144 { .bitrate = 10, .hw_value = 0x00, }, 145 { .bitrate = 20, .hw_value = 0x01, }, 146 { .bitrate = 55, .hw_value = 0x02, }, 147 { .bitrate = 110, .hw_value = 0x03, }, 148 { .bitrate = 60, .hw_value = 0x04, }, 149 { .bitrate = 90, .hw_value = 0x05, }, 150 { .bitrate = 120, .hw_value = 0x06, }, 151 { .bitrate = 180, .hw_value = 0x07, }, 152 { .bitrate = 240, .hw_value = 0x08, }, 153 { .bitrate = 360, .hw_value = 0x09, }, 154 { .bitrate = 480, .hw_value = 0x0a, }, 155 { .bitrate = 540, .hw_value = 0x0b, }, 156 }; 157 158 u16 rtw89_ra_report_to_bitrate(struct rtw89_dev *rtwdev, u8 rpt_rate) 159 { 160 struct ieee80211_rate rate; 161 162 if (unlikely(rpt_rate >= ARRAY_SIZE(rtw89_bitrates))) { 163 rtw89_info(rtwdev, "invalid rpt rate %d\n", rpt_rate); 164 return 0; 165 } 166 167 rate = rtw89_bitrates[rpt_rate]; 168 169 return rate.bitrate; 170 } 171 172 static struct ieee80211_supported_band rtw89_sband_2ghz = { 173 .band = NL80211_BAND_2GHZ, 174 .channels = rtw89_channels_2ghz, 175 .n_channels = ARRAY_SIZE(rtw89_channels_2ghz), 176 .bitrates = rtw89_bitrates, 177 .n_bitrates = ARRAY_SIZE(rtw89_bitrates), 178 .ht_cap = {0}, 179 .vht_cap = {0}, 180 }; 181 182 static struct ieee80211_supported_band rtw89_sband_5ghz = { 183 .band = NL80211_BAND_5GHZ, 184 .channels = rtw89_channels_5ghz, 185 .n_channels = ARRAY_SIZE(rtw89_channels_5ghz), 186 187 /* 5G has no CCK rates, 1M/2M/5.5M/11M */ 188 .bitrates = rtw89_bitrates + 4, 189 .n_bitrates = ARRAY_SIZE(rtw89_bitrates) - 4, 190 .ht_cap = {0}, 191 .vht_cap = {0}, 192 }; 193 194 static struct ieee80211_supported_band rtw89_sband_6ghz = { 195 .band = NL80211_BAND_6GHZ, 196 .channels = rtw89_channels_6ghz, 197 .n_channels = ARRAY_SIZE(rtw89_channels_6ghz), 198 199 /* 6G has no CCK rates, 1M/2M/5.5M/11M */ 200 .bitrates = rtw89_bitrates + 4, 201 .n_bitrates = ARRAY_SIZE(rtw89_bitrates) - 4, 202 }; 203 204 static void rtw89_traffic_stats_accu(struct rtw89_dev *rtwdev, 205 struct rtw89_traffic_stats *stats, 206 struct sk_buff *skb, bool tx) 207 { 208 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 209 210 if (!ieee80211_is_data(hdr->frame_control)) 211 return; 212 213 if (is_broadcast_ether_addr(hdr->addr1) || 214 is_multicast_ether_addr(hdr->addr1)) 215 return; 216 217 if (tx) { 218 stats->tx_cnt++; 219 stats->tx_unicast += skb->len; 220 } else { 221 stats->rx_cnt++; 222 stats->rx_unicast += skb->len; 223 } 224 } 225 226 static void rtw89_get_channel_params(struct cfg80211_chan_def *chandef, 227 struct rtw89_channel_params *chan_param) 228 { 229 struct ieee80211_channel *channel = chandef->chan; 230 enum nl80211_chan_width width = chandef->width; 231 u32 primary_freq, center_freq; 232 u8 center_chan; 233 u8 bandwidth = RTW89_CHANNEL_WIDTH_20; 234 u8 primary_chan_idx = 0; 235 u32 offset; 236 u8 band; 237 u8 subband; 238 239 center_chan = channel->hw_value; 240 primary_freq = channel->center_freq; 241 center_freq = chandef->center_freq1; 242 243 switch (width) { 244 case NL80211_CHAN_WIDTH_20_NOHT: 245 case NL80211_CHAN_WIDTH_20: 246 bandwidth = RTW89_CHANNEL_WIDTH_20; 247 primary_chan_idx = RTW89_SC_DONT_CARE; 248 break; 249 case NL80211_CHAN_WIDTH_40: 250 bandwidth = RTW89_CHANNEL_WIDTH_40; 251 if (primary_freq > center_freq) { 252 primary_chan_idx = RTW89_SC_20_UPPER; 253 center_chan -= 2; 254 } else { 255 primary_chan_idx = RTW89_SC_20_LOWER; 256 center_chan += 2; 257 } 258 break; 259 case NL80211_CHAN_WIDTH_80: 260 case NL80211_CHAN_WIDTH_160: 261 bandwidth = nl_to_rtw89_bandwidth(width); 262 if (primary_freq > center_freq) { 263 offset = (primary_freq - center_freq - 10) / 20; 264 primary_chan_idx = RTW89_SC_20_UPPER + offset * 2; 265 center_chan -= 2 + offset * 4; 266 } else { 267 offset = (center_freq - primary_freq - 10) / 20; 268 primary_chan_idx = RTW89_SC_20_LOWER + offset * 2; 269 center_chan += 2 + offset * 4; 270 } 271 break; 272 default: 273 center_chan = 0; 274 break; 275 } 276 277 switch (channel->band) { 278 default: 279 case NL80211_BAND_2GHZ: 280 band = RTW89_BAND_2G; 281 break; 282 case NL80211_BAND_5GHZ: 283 band = RTW89_BAND_5G; 284 break; 285 case NL80211_BAND_6GHZ: 286 band = RTW89_BAND_6G; 287 break; 288 } 289 290 switch (band) { 291 default: 292 case RTW89_BAND_2G: 293 switch (center_chan) { 294 default: 295 case 1 ... 14: 296 subband = RTW89_CH_2G; 297 break; 298 } 299 break; 300 case RTW89_BAND_5G: 301 switch (center_chan) { 302 default: 303 case 36 ... 64: 304 subband = RTW89_CH_5G_BAND_1; 305 break; 306 case 100 ... 144: 307 subband = RTW89_CH_5G_BAND_3; 308 break; 309 case 149 ... 177: 310 subband = RTW89_CH_5G_BAND_4; 311 break; 312 } 313 break; 314 case RTW89_BAND_6G: 315 switch (center_chan) { 316 default: 317 case 1 ... 29: 318 subband = RTW89_CH_6G_BAND_IDX0; 319 break; 320 case 33 ... 61: 321 subband = RTW89_CH_6G_BAND_IDX1; 322 break; 323 case 65 ... 93: 324 subband = RTW89_CH_6G_BAND_IDX2; 325 break; 326 case 97 ... 125: 327 subband = RTW89_CH_6G_BAND_IDX3; 328 break; 329 case 129 ... 157: 330 subband = RTW89_CH_6G_BAND_IDX4; 331 break; 332 case 161 ... 189: 333 subband = RTW89_CH_6G_BAND_IDX5; 334 break; 335 case 193 ... 221: 336 subband = RTW89_CH_6G_BAND_IDX6; 337 break; 338 case 225 ... 253: 339 subband = RTW89_CH_6G_BAND_IDX7; 340 break; 341 } 342 break; 343 } 344 345 chan_param->center_chan = center_chan; 346 chan_param->center_freq = center_freq; 347 chan_param->primary_chan = channel->hw_value; 348 chan_param->bandwidth = bandwidth; 349 chan_param->pri_ch_idx = primary_chan_idx; 350 chan_param->band_type = band; 351 chan_param->subband_type = subband; 352 } 353 354 void rtw89_set_channel(struct rtw89_dev *rtwdev) 355 { 356 struct ieee80211_hw *hw = rtwdev->hw; 357 const struct rtw89_chip_info *chip = rtwdev->chip; 358 struct rtw89_hal *hal = &rtwdev->hal; 359 struct rtw89_channel_params ch_param; 360 struct rtw89_channel_help_params bak; 361 u8 center_chan, bandwidth; 362 bool band_changed; 363 364 rtw89_get_channel_params(&hw->conf.chandef, &ch_param); 365 if (WARN(ch_param.center_chan == 0, "Invalid channel\n")) 366 return; 367 368 center_chan = ch_param.center_chan; 369 bandwidth = ch_param.bandwidth; 370 band_changed = hal->current_band_type != ch_param.band_type || 371 hal->current_channel == 0; 372 373 hal->current_band_width = bandwidth; 374 hal->current_channel = center_chan; 375 hal->current_freq = ch_param.center_freq; 376 hal->prev_primary_channel = hal->current_primary_channel; 377 hal->prev_band_type = hal->current_band_type; 378 hal->current_primary_channel = ch_param.primary_chan; 379 hal->current_band_type = ch_param.band_type; 380 hal->current_subband = ch_param.subband_type; 381 382 rtw89_chip_set_channel_prepare(rtwdev, &bak); 383 384 chip->ops->set_channel(rtwdev, &ch_param); 385 386 rtw89_chip_set_txpwr(rtwdev); 387 388 rtw89_chip_set_channel_done(rtwdev, &bak); 389 390 if (band_changed) { 391 rtw89_btc_ntfy_switch_band(rtwdev, RTW89_PHY_0, hal->current_band_type); 392 rtw89_chip_rfk_band_changed(rtwdev); 393 } 394 } 395 396 static enum rtw89_core_tx_type 397 rtw89_core_get_tx_type(struct rtw89_dev *rtwdev, 398 struct sk_buff *skb) 399 { 400 struct ieee80211_hdr *hdr = (void *)skb->data; 401 __le16 fc = hdr->frame_control; 402 403 if (ieee80211_is_mgmt(fc) || ieee80211_is_nullfunc(fc)) 404 return RTW89_CORE_TX_TYPE_MGMT; 405 406 return RTW89_CORE_TX_TYPE_DATA; 407 } 408 409 static void 410 rtw89_core_tx_update_ampdu_info(struct rtw89_dev *rtwdev, 411 struct rtw89_core_tx_request *tx_req, u8 tid) 412 { 413 struct ieee80211_sta *sta = tx_req->sta; 414 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info; 415 struct rtw89_sta *rtwsta; 416 u8 ampdu_num; 417 418 if (!sta) { 419 rtw89_warn(rtwdev, "cannot set ampdu info without sta\n"); 420 return; 421 } 422 423 rtwsta = (struct rtw89_sta *)sta->drv_priv; 424 425 ampdu_num = (u8)((rtwsta->ampdu_params[tid].agg_num ? 426 rtwsta->ampdu_params[tid].agg_num : 427 4 << sta->deflink.ht_cap.ampdu_factor) - 1); 428 429 desc_info->agg_en = true; 430 desc_info->ampdu_density = sta->deflink.ht_cap.ampdu_density; 431 desc_info->ampdu_num = ampdu_num; 432 } 433 434 static void 435 rtw89_core_tx_update_sec_key(struct rtw89_dev *rtwdev, 436 struct rtw89_core_tx_request *tx_req) 437 { 438 const struct rtw89_chip_info *chip = rtwdev->chip; 439 struct ieee80211_vif *vif = tx_req->vif; 440 struct ieee80211_sta *sta = tx_req->sta; 441 struct ieee80211_tx_info *info; 442 struct ieee80211_key_conf *key; 443 struct rtw89_vif *rtwvif; 444 struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta); 445 struct rtw89_addr_cam_entry *addr_cam; 446 struct rtw89_sec_cam_entry *sec_cam; 447 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info; 448 struct sk_buff *skb = tx_req->skb; 449 u8 sec_type = RTW89_SEC_KEY_TYPE_NONE; 450 u64 pn64; 451 452 if (!vif) { 453 rtw89_warn(rtwdev, "cannot set sec key without vif\n"); 454 return; 455 } 456 457 rtwvif = (struct rtw89_vif *)vif->drv_priv; 458 addr_cam = rtw89_get_addr_cam_of(rtwvif, rtwsta); 459 460 info = IEEE80211_SKB_CB(skb); 461 key = info->control.hw_key; 462 sec_cam = addr_cam->sec_entries[key->hw_key_idx]; 463 if (!sec_cam) { 464 rtw89_warn(rtwdev, "sec cam entry is empty\n"); 465 return; 466 } 467 468 switch (key->cipher) { 469 case WLAN_CIPHER_SUITE_WEP40: 470 sec_type = RTW89_SEC_KEY_TYPE_WEP40; 471 break; 472 case WLAN_CIPHER_SUITE_WEP104: 473 sec_type = RTW89_SEC_KEY_TYPE_WEP104; 474 break; 475 case WLAN_CIPHER_SUITE_TKIP: 476 sec_type = RTW89_SEC_KEY_TYPE_TKIP; 477 break; 478 case WLAN_CIPHER_SUITE_CCMP: 479 sec_type = RTW89_SEC_KEY_TYPE_CCMP128; 480 break; 481 case WLAN_CIPHER_SUITE_CCMP_256: 482 sec_type = RTW89_SEC_KEY_TYPE_CCMP256; 483 break; 484 case WLAN_CIPHER_SUITE_GCMP: 485 sec_type = RTW89_SEC_KEY_TYPE_GCMP128; 486 break; 487 case WLAN_CIPHER_SUITE_GCMP_256: 488 sec_type = RTW89_SEC_KEY_TYPE_GCMP256; 489 break; 490 default: 491 rtw89_warn(rtwdev, "key cipher not supported %d\n", key->cipher); 492 return; 493 } 494 495 desc_info->sec_en = true; 496 desc_info->sec_keyid = key->keyidx; 497 desc_info->sec_type = sec_type; 498 desc_info->sec_cam_idx = sec_cam->sec_cam_idx; 499 500 if (!chip->hw_sec_hdr) 501 return; 502 503 pn64 = atomic64_inc_return(&key->tx_pn); 504 desc_info->sec_seq[0] = pn64; 505 desc_info->sec_seq[1] = pn64 >> 8; 506 desc_info->sec_seq[2] = pn64 >> 16; 507 desc_info->sec_seq[3] = pn64 >> 24; 508 desc_info->sec_seq[4] = pn64 >> 32; 509 desc_info->sec_seq[5] = pn64 >> 40; 510 desc_info->wp_offset = 1; /* in unit of 8 bytes for security header */ 511 } 512 513 static u16 rtw89_core_get_mgmt_rate(struct rtw89_dev *rtwdev, 514 struct rtw89_core_tx_request *tx_req) 515 { 516 struct sk_buff *skb = tx_req->skb; 517 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb); 518 struct ieee80211_vif *vif = tx_info->control.vif; 519 struct rtw89_hal *hal = &rtwdev->hal; 520 u16 lowest_rate = hal->current_band_type == RTW89_BAND_2G ? 521 RTW89_HW_RATE_CCK1 : RTW89_HW_RATE_OFDM6; 522 523 if (!vif || !vif->bss_conf.basic_rates || !tx_req->sta) 524 return lowest_rate; 525 526 return __ffs(vif->bss_conf.basic_rates) + lowest_rate; 527 } 528 529 static void 530 rtw89_core_tx_update_mgmt_info(struct rtw89_dev *rtwdev, 531 struct rtw89_core_tx_request *tx_req) 532 { 533 struct ieee80211_vif *vif = tx_req->vif; 534 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 535 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info; 536 u8 qsel, ch_dma; 537 538 qsel = desc_info->hiq ? RTW89_TX_QSEL_B0_HI : RTW89_TX_QSEL_B0_MGMT; 539 ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel); 540 541 desc_info->qsel = qsel; 542 desc_info->ch_dma = ch_dma; 543 desc_info->port = desc_info->hiq ? rtwvif->port : 0; 544 desc_info->hw_ssn_sel = RTW89_MGMT_HW_SSN_SEL; 545 desc_info->hw_seq_mode = RTW89_MGMT_HW_SEQ_MODE; 546 547 /* fixed data rate for mgmt frames */ 548 desc_info->en_wd_info = true; 549 desc_info->use_rate = true; 550 desc_info->dis_data_fb = true; 551 desc_info->data_rate = rtw89_core_get_mgmt_rate(rtwdev, tx_req); 552 553 rtw89_debug(rtwdev, RTW89_DBG_TXRX, 554 "tx mgmt frame with rate 0x%x on channel %d (bw %d)\n", 555 desc_info->data_rate, rtwdev->hal.current_channel, 556 rtwdev->hal.current_band_width); 557 } 558 559 static void 560 rtw89_core_tx_update_h2c_info(struct rtw89_dev *rtwdev, 561 struct rtw89_core_tx_request *tx_req) 562 { 563 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info; 564 565 desc_info->is_bmc = false; 566 desc_info->wd_page = false; 567 desc_info->ch_dma = RTW89_DMA_H2C; 568 } 569 570 static void rtw89_core_get_no_ul_ofdma_htc(struct rtw89_dev *rtwdev, __le32 *htc) 571 { 572 static const u8 rtw89_bandwidth_to_om[] = { 573 [RTW89_CHANNEL_WIDTH_20] = HTC_OM_CHANNEL_WIDTH_20, 574 [RTW89_CHANNEL_WIDTH_40] = HTC_OM_CHANNEL_WIDTH_40, 575 [RTW89_CHANNEL_WIDTH_80] = HTC_OM_CHANNEL_WIDTH_80, 576 [RTW89_CHANNEL_WIDTH_160] = HTC_OM_CHANNEL_WIDTH_160_OR_80_80, 577 [RTW89_CHANNEL_WIDTH_80_80] = HTC_OM_CHANNEL_WIDTH_160_OR_80_80, 578 }; 579 const struct rtw89_chip_info *chip = rtwdev->chip; 580 struct rtw89_hal *hal = &rtwdev->hal; 581 u8 om_bandwidth; 582 583 if (!chip->dis_2g_40m_ul_ofdma || 584 hal->current_band_type != RTW89_BAND_2G || 585 hal->current_band_width != RTW89_CHANNEL_WIDTH_40) 586 return; 587 588 om_bandwidth = hal->current_band_width < ARRAY_SIZE(rtw89_bandwidth_to_om) ? 589 rtw89_bandwidth_to_om[hal->current_band_width] : 0; 590 *htc = le32_encode_bits(RTW89_HTC_VARIANT_HE, RTW89_HTC_MASK_VARIANT) | 591 le32_encode_bits(RTW89_HTC_VARIANT_HE_CID_OM, RTW89_HTC_MASK_CTL_ID) | 592 le32_encode_bits(hal->rx_nss - 1, RTW89_HTC_MASK_HTC_OM_RX_NSS) | 593 le32_encode_bits(om_bandwidth, RTW89_HTC_MASK_HTC_OM_CH_WIDTH) | 594 le32_encode_bits(1, RTW89_HTC_MASK_HTC_OM_UL_MU_DIS) | 595 le32_encode_bits(hal->tx_nss - 1, RTW89_HTC_MASK_HTC_OM_TX_NSTS) | 596 le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_ER_SU_DIS) | 597 le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_DL_MU_MIMO_RR) | 598 le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_UL_MU_DATA_DIS); 599 } 600 601 static bool 602 __rtw89_core_tx_check_he_qos_htc(struct rtw89_dev *rtwdev, 603 struct rtw89_core_tx_request *tx_req, 604 enum btc_pkt_type pkt_type) 605 { 606 struct ieee80211_sta *sta = tx_req->sta; 607 struct sk_buff *skb = tx_req->skb; 608 struct ieee80211_hdr *hdr = (void *)skb->data; 609 __le16 fc = hdr->frame_control; 610 611 /* AP IOT issue with EAPoL, ARP and DHCP */ 612 if (pkt_type < PACKET_MAX) 613 return false; 614 615 if (!sta || !sta->deflink.he_cap.has_he) 616 return false; 617 618 if (!ieee80211_is_data_qos(fc)) 619 return false; 620 621 if (skb_headroom(skb) < IEEE80211_HT_CTL_LEN) 622 return false; 623 624 return true; 625 } 626 627 static void 628 __rtw89_core_tx_adjust_he_qos_htc(struct rtw89_dev *rtwdev, 629 struct rtw89_core_tx_request *tx_req) 630 { 631 struct ieee80211_sta *sta = tx_req->sta; 632 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 633 struct sk_buff *skb = tx_req->skb; 634 struct ieee80211_hdr *hdr = (void *)skb->data; 635 __le16 fc = hdr->frame_control; 636 void *data; 637 __le32 *htc; 638 u8 *qc; 639 int hdr_len; 640 641 hdr_len = ieee80211_has_a4(fc) ? 32 : 26; 642 data = skb_push(skb, IEEE80211_HT_CTL_LEN); 643 memmove(data, data + IEEE80211_HT_CTL_LEN, hdr_len); 644 645 hdr = data; 646 htc = data + hdr_len; 647 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_ORDER); 648 *htc = rtwsta->htc_template ? rtwsta->htc_template : 649 le32_encode_bits(RTW89_HTC_VARIANT_HE, RTW89_HTC_MASK_VARIANT) | 650 le32_encode_bits(RTW89_HTC_VARIANT_HE_CID_CAS, RTW89_HTC_MASK_CTL_ID); 651 652 qc = data + hdr_len - IEEE80211_QOS_CTL_LEN; 653 qc[0] |= IEEE80211_QOS_CTL_EOSP; 654 } 655 656 static void 657 rtw89_core_tx_update_he_qos_htc(struct rtw89_dev *rtwdev, 658 struct rtw89_core_tx_request *tx_req, 659 enum btc_pkt_type pkt_type) 660 { 661 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info; 662 struct ieee80211_vif *vif = tx_req->vif; 663 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 664 665 if (!__rtw89_core_tx_check_he_qos_htc(rtwdev, tx_req, pkt_type)) 666 goto desc_bk; 667 668 __rtw89_core_tx_adjust_he_qos_htc(rtwdev, tx_req); 669 670 desc_info->pkt_size += IEEE80211_HT_CTL_LEN; 671 desc_info->a_ctrl_bsr = true; 672 673 desc_bk: 674 if (!rtwvif || rtwvif->last_a_ctrl == desc_info->a_ctrl_bsr) 675 return; 676 677 rtwvif->last_a_ctrl = desc_info->a_ctrl_bsr; 678 desc_info->bk = true; 679 } 680 681 static u8 rtw89_core_tx_get_mac_id(struct rtw89_dev *rtwdev, 682 struct rtw89_core_tx_request *tx_req) 683 { 684 struct ieee80211_vif *vif = tx_req->vif; 685 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 686 struct ieee80211_sta *sta = tx_req->sta; 687 struct rtw89_sta *rtwsta; 688 689 if (!sta) 690 return rtwvif->mac_id; 691 692 rtwsta = (struct rtw89_sta *)sta->drv_priv; 693 return rtwsta->mac_id; 694 } 695 696 static void 697 rtw89_core_tx_update_data_info(struct rtw89_dev *rtwdev, 698 struct rtw89_core_tx_request *tx_req) 699 { 700 struct ieee80211_vif *vif = tx_req->vif; 701 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 702 struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern; 703 struct rtw89_hal *hal = &rtwdev->hal; 704 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info; 705 struct sk_buff *skb = tx_req->skb; 706 u8 tid, tid_indicate; 707 u8 qsel, ch_dma; 708 709 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK; 710 tid_indicate = rtw89_core_get_tid_indicate(rtwdev, tid); 711 qsel = desc_info->hiq ? RTW89_TX_QSEL_B0_HI : rtw89_core_get_qsel(rtwdev, tid); 712 ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel); 713 714 desc_info->ch_dma = ch_dma; 715 desc_info->tid_indicate = tid_indicate; 716 desc_info->qsel = qsel; 717 desc_info->mac_id = rtw89_core_tx_get_mac_id(rtwdev, tx_req); 718 desc_info->port = desc_info->hiq ? rtwvif->port : 0; 719 720 /* enable wd_info for AMPDU */ 721 desc_info->en_wd_info = true; 722 723 if (IEEE80211_SKB_CB(skb)->flags & IEEE80211_TX_CTL_AMPDU) 724 rtw89_core_tx_update_ampdu_info(rtwdev, tx_req, tid); 725 if (IEEE80211_SKB_CB(skb)->control.hw_key) 726 rtw89_core_tx_update_sec_key(rtwdev, tx_req); 727 728 if (rate_pattern->enable) 729 desc_info->data_retry_lowest_rate = rate_pattern->rate; 730 else if (hal->current_band_type == RTW89_BAND_2G) 731 desc_info->data_retry_lowest_rate = RTW89_HW_RATE_CCK1; 732 else 733 desc_info->data_retry_lowest_rate = RTW89_HW_RATE_OFDM6; 734 } 735 736 static enum btc_pkt_type 737 rtw89_core_tx_btc_spec_pkt_notify(struct rtw89_dev *rtwdev, 738 struct rtw89_core_tx_request *tx_req) 739 { 740 struct sk_buff *skb = tx_req->skb; 741 struct udphdr *udphdr; 742 743 if (IEEE80211_SKB_CB(skb)->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) { 744 ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.eapol_notify_work); 745 return PACKET_EAPOL; 746 } 747 748 if (skb->protocol == htons(ETH_P_ARP)) { 749 ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.arp_notify_work); 750 return PACKET_ARP; 751 } 752 753 if (skb->protocol == htons(ETH_P_IP) && 754 ip_hdr(skb)->protocol == IPPROTO_UDP) { 755 udphdr = udp_hdr(skb); 756 if (((udphdr->source == htons(67) && udphdr->dest == htons(68)) || 757 (udphdr->source == htons(68) && udphdr->dest == htons(67))) && 758 skb->len > 282) { 759 ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.dhcp_notify_work); 760 return PACKET_DHCP; 761 } 762 } 763 764 if (skb->protocol == htons(ETH_P_IP) && 765 ip_hdr(skb)->protocol == IPPROTO_ICMP) { 766 ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.icmp_notify_work); 767 return PACKET_ICMP; 768 } 769 770 return PACKET_MAX; 771 } 772 773 static void rtw89_core_tx_update_llc_hdr(struct rtw89_dev *rtwdev, 774 struct rtw89_tx_desc_info *desc_info, 775 struct sk_buff *skb) 776 { 777 struct ieee80211_hdr *hdr = (void *)skb->data; 778 __le16 fc = hdr->frame_control; 779 780 desc_info->hdr_llc_len = ieee80211_hdrlen(fc); 781 desc_info->hdr_llc_len >>= 1; /* in unit of 2 bytes */ 782 } 783 784 static void 785 rtw89_core_tx_wake(struct rtw89_dev *rtwdev, 786 struct rtw89_core_tx_request *tx_req) 787 { 788 if (!RTW89_CHK_FW_FEATURE(TX_WAKE, &rtwdev->fw)) 789 return; 790 791 if (!test_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags)) 792 return; 793 794 if (tx_req->tx_type != RTW89_CORE_TX_TYPE_MGMT) 795 return; 796 797 rtw89_mac_notify_wake(rtwdev); 798 } 799 800 static void 801 rtw89_core_tx_update_desc_info(struct rtw89_dev *rtwdev, 802 struct rtw89_core_tx_request *tx_req) 803 { 804 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info; 805 struct sk_buff *skb = tx_req->skb; 806 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 807 struct ieee80211_hdr *hdr = (void *)skb->data; 808 enum rtw89_core_tx_type tx_type; 809 enum btc_pkt_type pkt_type; 810 bool is_bmc; 811 u16 seq; 812 813 seq = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4; 814 if (tx_req->tx_type != RTW89_CORE_TX_TYPE_FWCMD) { 815 tx_type = rtw89_core_get_tx_type(rtwdev, skb); 816 tx_req->tx_type = tx_type; 817 } 818 is_bmc = (is_broadcast_ether_addr(hdr->addr1) || 819 is_multicast_ether_addr(hdr->addr1)); 820 821 desc_info->seq = seq; 822 desc_info->pkt_size = skb->len; 823 desc_info->is_bmc = is_bmc; 824 desc_info->wd_page = true; 825 desc_info->hiq = info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM; 826 827 switch (tx_req->tx_type) { 828 case RTW89_CORE_TX_TYPE_MGMT: 829 rtw89_core_tx_update_mgmt_info(rtwdev, tx_req); 830 break; 831 case RTW89_CORE_TX_TYPE_DATA: 832 rtw89_core_tx_update_data_info(rtwdev, tx_req); 833 pkt_type = rtw89_core_tx_btc_spec_pkt_notify(rtwdev, tx_req); 834 rtw89_core_tx_update_he_qos_htc(rtwdev, tx_req, pkt_type); 835 rtw89_core_tx_update_llc_hdr(rtwdev, desc_info, skb); 836 break; 837 case RTW89_CORE_TX_TYPE_FWCMD: 838 rtw89_core_tx_update_h2c_info(rtwdev, tx_req); 839 break; 840 } 841 } 842 843 void rtw89_core_tx_kick_off(struct rtw89_dev *rtwdev, u8 qsel) 844 { 845 u8 ch_dma; 846 847 ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel); 848 849 rtw89_hci_tx_kick_off(rtwdev, ch_dma); 850 } 851 852 int rtw89_h2c_tx(struct rtw89_dev *rtwdev, 853 struct sk_buff *skb, bool fwdl) 854 { 855 struct rtw89_core_tx_request tx_req = {0}; 856 u32 cnt; 857 int ret; 858 859 if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) { 860 rtw89_debug(rtwdev, RTW89_DBG_FW, 861 "ignore h2c due to power is off with firmware state=%d\n", 862 test_bit(RTW89_FLAG_FW_RDY, rtwdev->flags)); 863 return 0; 864 } 865 866 tx_req.skb = skb; 867 tx_req.tx_type = RTW89_CORE_TX_TYPE_FWCMD; 868 if (fwdl) 869 tx_req.desc_info.fw_dl = true; 870 871 rtw89_core_tx_update_desc_info(rtwdev, &tx_req); 872 873 if (!fwdl) 874 rtw89_hex_dump(rtwdev, RTW89_DBG_FW, "H2C: ", skb->data, skb->len); 875 876 cnt = rtw89_hci_check_and_reclaim_tx_resource(rtwdev, RTW89_TXCH_CH12); 877 if (cnt == 0) { 878 rtw89_err(rtwdev, "no tx fwcmd resource\n"); 879 return -ENOSPC; 880 } 881 882 ret = rtw89_hci_tx_write(rtwdev, &tx_req); 883 if (ret) { 884 rtw89_err(rtwdev, "failed to transmit skb to HCI\n"); 885 return ret; 886 } 887 rtw89_hci_tx_kick_off(rtwdev, RTW89_TXCH_CH12); 888 889 return 0; 890 } 891 892 int rtw89_core_tx_write(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif, 893 struct ieee80211_sta *sta, struct sk_buff *skb, int *qsel) 894 { 895 struct rtw89_core_tx_request tx_req = {0}; 896 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 897 int ret; 898 899 tx_req.skb = skb; 900 tx_req.sta = sta; 901 tx_req.vif = vif; 902 903 rtw89_traffic_stats_accu(rtwdev, &rtwdev->stats, skb, true); 904 rtw89_traffic_stats_accu(rtwdev, &rtwvif->stats, skb, true); 905 rtw89_core_tx_update_desc_info(rtwdev, &tx_req); 906 rtw89_core_tx_wake(rtwdev, &tx_req); 907 908 ret = rtw89_hci_tx_write(rtwdev, &tx_req); 909 if (ret) { 910 rtw89_err(rtwdev, "failed to transmit skb to HCI\n"); 911 return ret; 912 } 913 914 if (qsel) 915 *qsel = tx_req.desc_info.qsel; 916 917 return 0; 918 } 919 920 static __le32 rtw89_build_txwd_body0(struct rtw89_tx_desc_info *desc_info) 921 { 922 u32 dword = FIELD_PREP(RTW89_TXWD_BODY0_WP_OFFSET, desc_info->wp_offset) | 923 FIELD_PREP(RTW89_TXWD_BODY0_WD_INFO_EN, desc_info->en_wd_info) | 924 FIELD_PREP(RTW89_TXWD_BODY0_CHANNEL_DMA, desc_info->ch_dma) | 925 FIELD_PREP(RTW89_TXWD_BODY0_HDR_LLC_LEN, desc_info->hdr_llc_len) | 926 FIELD_PREP(RTW89_TXWD_BODY0_WD_PAGE, desc_info->wd_page) | 927 FIELD_PREP(RTW89_TXWD_BODY0_FW_DL, desc_info->fw_dl) | 928 FIELD_PREP(RTW89_TXWD_BODY0_HW_SSN_SEL, desc_info->hw_ssn_sel) | 929 FIELD_PREP(RTW89_TXWD_BODY0_HW_SSN_MODE, desc_info->hw_seq_mode); 930 931 return cpu_to_le32(dword); 932 } 933 934 static __le32 rtw89_build_txwd_body0_v1(struct rtw89_tx_desc_info *desc_info) 935 { 936 u32 dword = FIELD_PREP(RTW89_TXWD_BODY0_WP_OFFSET_V1, desc_info->wp_offset) | 937 FIELD_PREP(RTW89_TXWD_BODY0_WD_INFO_EN, desc_info->en_wd_info) | 938 FIELD_PREP(RTW89_TXWD_BODY0_CHANNEL_DMA, desc_info->ch_dma) | 939 FIELD_PREP(RTW89_TXWD_BODY0_HDR_LLC_LEN, desc_info->hdr_llc_len) | 940 FIELD_PREP(RTW89_TXWD_BODY0_WD_PAGE, desc_info->wd_page) | 941 FIELD_PREP(RTW89_TXWD_BODY0_FW_DL, desc_info->fw_dl); 942 943 return cpu_to_le32(dword); 944 } 945 946 static __le32 rtw89_build_txwd_body1_v1(struct rtw89_tx_desc_info *desc_info) 947 { 948 u32 dword = FIELD_PREP(RTW89_TXWD_BODY1_ADDR_INFO_NUM, desc_info->addr_info_nr) | 949 FIELD_PREP(RTW89_TXWD_BODY1_SEC_KEYID, desc_info->sec_keyid) | 950 FIELD_PREP(RTW89_TXWD_BODY1_SEC_TYPE, desc_info->sec_type); 951 952 return cpu_to_le32(dword); 953 } 954 955 static __le32 rtw89_build_txwd_body2(struct rtw89_tx_desc_info *desc_info) 956 { 957 u32 dword = FIELD_PREP(RTW89_TXWD_BODY2_TID_INDICATE, desc_info->tid_indicate) | 958 FIELD_PREP(RTW89_TXWD_BODY2_QSEL, desc_info->qsel) | 959 FIELD_PREP(RTW89_TXWD_BODY2_TXPKT_SIZE, desc_info->pkt_size) | 960 FIELD_PREP(RTW89_TXWD_BODY2_MACID, desc_info->mac_id); 961 962 return cpu_to_le32(dword); 963 } 964 965 static __le32 rtw89_build_txwd_body3(struct rtw89_tx_desc_info *desc_info) 966 { 967 u32 dword = FIELD_PREP(RTW89_TXWD_BODY3_SW_SEQ, desc_info->seq) | 968 FIELD_PREP(RTW89_TXWD_BODY3_AGG_EN, desc_info->agg_en) | 969 FIELD_PREP(RTW89_TXWD_BODY3_BK, desc_info->bk); 970 971 return cpu_to_le32(dword); 972 } 973 974 static __le32 rtw89_build_txwd_body4(struct rtw89_tx_desc_info *desc_info) 975 { 976 u32 dword = FIELD_PREP(RTW89_TXWD_BODY4_SEC_IV_L0, desc_info->sec_seq[0]) | 977 FIELD_PREP(RTW89_TXWD_BODY4_SEC_IV_L1, desc_info->sec_seq[1]); 978 979 return cpu_to_le32(dword); 980 } 981 982 static __le32 rtw89_build_txwd_body5(struct rtw89_tx_desc_info *desc_info) 983 { 984 u32 dword = FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H2, desc_info->sec_seq[2]) | 985 FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H3, desc_info->sec_seq[3]) | 986 FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H4, desc_info->sec_seq[4]) | 987 FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H5, desc_info->sec_seq[5]); 988 989 return cpu_to_le32(dword); 990 } 991 992 static __le32 rtw89_build_txwd_body7_v1(struct rtw89_tx_desc_info *desc_info) 993 { 994 u32 dword = FIELD_PREP(RTW89_TXWD_BODY7_USE_RATE_V1, desc_info->use_rate) | 995 FIELD_PREP(RTW89_TXWD_BODY7_DATA_RATE, desc_info->data_rate); 996 997 return cpu_to_le32(dword); 998 } 999 1000 static __le32 rtw89_build_txwd_info0(struct rtw89_tx_desc_info *desc_info) 1001 { 1002 u32 dword = FIELD_PREP(RTW89_TXWD_INFO0_USE_RATE, desc_info->use_rate) | 1003 FIELD_PREP(RTW89_TXWD_INFO0_DATA_RATE, desc_info->data_rate) | 1004 FIELD_PREP(RTW89_TXWD_INFO0_DISDATAFB, desc_info->dis_data_fb) | 1005 FIELD_PREP(RTW89_TXWD_INFO0_MULTIPORT_ID, desc_info->port); 1006 1007 return cpu_to_le32(dword); 1008 } 1009 1010 static __le32 rtw89_build_txwd_info0_v1(struct rtw89_tx_desc_info *desc_info) 1011 { 1012 u32 dword = FIELD_PREP(RTW89_TXWD_INFO0_DISDATAFB, desc_info->dis_data_fb); 1013 1014 return cpu_to_le32(dword); 1015 } 1016 1017 static __le32 rtw89_build_txwd_info1(struct rtw89_tx_desc_info *desc_info) 1018 { 1019 u32 dword = FIELD_PREP(RTW89_TXWD_INFO1_MAX_AGGNUM, desc_info->ampdu_num) | 1020 FIELD_PREP(RTW89_TXWD_INFO1_A_CTRL_BSR, desc_info->a_ctrl_bsr) | 1021 FIELD_PREP(RTW89_TXWD_INFO1_DATA_RTY_LOWEST_RATE, 1022 desc_info->data_retry_lowest_rate); 1023 1024 return cpu_to_le32(dword); 1025 } 1026 1027 static __le32 rtw89_build_txwd_info2(struct rtw89_tx_desc_info *desc_info) 1028 { 1029 u32 dword = FIELD_PREP(RTW89_TXWD_INFO2_AMPDU_DENSITY, desc_info->ampdu_density) | 1030 FIELD_PREP(RTW89_TXWD_INFO2_SEC_TYPE, desc_info->sec_type) | 1031 FIELD_PREP(RTW89_TXWD_INFO2_SEC_HW_ENC, desc_info->sec_en) | 1032 FIELD_PREP(RTW89_TXWD_INFO2_SEC_CAM_IDX, desc_info->sec_cam_idx); 1033 1034 return cpu_to_le32(dword); 1035 } 1036 1037 static __le32 rtw89_build_txwd_info2_v1(struct rtw89_tx_desc_info *desc_info) 1038 { 1039 u32 dword = FIELD_PREP(RTW89_TXWD_INFO2_AMPDU_DENSITY, desc_info->ampdu_density) | 1040 FIELD_PREP(RTW89_TXWD_INFO2_FORCE_KEY_EN, desc_info->sec_en) | 1041 FIELD_PREP(RTW89_TXWD_INFO2_SEC_CAM_IDX, desc_info->sec_cam_idx); 1042 1043 return cpu_to_le32(dword); 1044 } 1045 1046 static __le32 rtw89_build_txwd_info4(struct rtw89_tx_desc_info *desc_info) 1047 { 1048 u32 dword = FIELD_PREP(RTW89_TXWD_INFO4_RTS_EN, 1) | 1049 FIELD_PREP(RTW89_TXWD_INFO4_HW_RTS_EN, 1); 1050 1051 return cpu_to_le32(dword); 1052 } 1053 1054 void rtw89_core_fill_txdesc(struct rtw89_dev *rtwdev, 1055 struct rtw89_tx_desc_info *desc_info, 1056 void *txdesc) 1057 { 1058 struct rtw89_txwd_body *txwd_body = (struct rtw89_txwd_body *)txdesc; 1059 struct rtw89_txwd_info *txwd_info; 1060 1061 txwd_body->dword0 = rtw89_build_txwd_body0(desc_info); 1062 txwd_body->dword2 = rtw89_build_txwd_body2(desc_info); 1063 txwd_body->dword3 = rtw89_build_txwd_body3(desc_info); 1064 1065 if (!desc_info->en_wd_info) 1066 return; 1067 1068 txwd_info = (struct rtw89_txwd_info *)(txwd_body + 1); 1069 txwd_info->dword0 = rtw89_build_txwd_info0(desc_info); 1070 txwd_info->dword1 = rtw89_build_txwd_info1(desc_info); 1071 txwd_info->dword2 = rtw89_build_txwd_info2(desc_info); 1072 txwd_info->dword4 = rtw89_build_txwd_info4(desc_info); 1073 1074 } 1075 EXPORT_SYMBOL(rtw89_core_fill_txdesc); 1076 1077 void rtw89_core_fill_txdesc_v1(struct rtw89_dev *rtwdev, 1078 struct rtw89_tx_desc_info *desc_info, 1079 void *txdesc) 1080 { 1081 struct rtw89_txwd_body_v1 *txwd_body = (struct rtw89_txwd_body_v1 *)txdesc; 1082 struct rtw89_txwd_info *txwd_info; 1083 1084 txwd_body->dword0 = rtw89_build_txwd_body0_v1(desc_info); 1085 txwd_body->dword1 = rtw89_build_txwd_body1_v1(desc_info); 1086 txwd_body->dword2 = rtw89_build_txwd_body2(desc_info); 1087 txwd_body->dword3 = rtw89_build_txwd_body3(desc_info); 1088 if (desc_info->sec_en) { 1089 txwd_body->dword4 = rtw89_build_txwd_body4(desc_info); 1090 txwd_body->dword5 = rtw89_build_txwd_body5(desc_info); 1091 } 1092 txwd_body->dword7 = rtw89_build_txwd_body7_v1(desc_info); 1093 1094 if (!desc_info->en_wd_info) 1095 return; 1096 1097 txwd_info = (struct rtw89_txwd_info *)(txwd_body + 1); 1098 txwd_info->dword0 = rtw89_build_txwd_info0_v1(desc_info); 1099 txwd_info->dword1 = rtw89_build_txwd_info1(desc_info); 1100 txwd_info->dword2 = rtw89_build_txwd_info2_v1(desc_info); 1101 txwd_info->dword4 = rtw89_build_txwd_info4(desc_info); 1102 } 1103 EXPORT_SYMBOL(rtw89_core_fill_txdesc_v1); 1104 1105 static __le32 rtw89_build_txwd_fwcmd0_v1(struct rtw89_tx_desc_info *desc_info) 1106 { 1107 u32 dword = FIELD_PREP(AX_RXD_RPKT_LEN_MASK, desc_info->pkt_size) | 1108 FIELD_PREP(AX_RXD_RPKT_TYPE_MASK, desc_info->fw_dl ? 1109 RTW89_CORE_RX_TYPE_FWDL : 1110 RTW89_CORE_RX_TYPE_H2C); 1111 1112 return cpu_to_le32(dword); 1113 } 1114 1115 void rtw89_core_fill_txdesc_fwcmd_v1(struct rtw89_dev *rtwdev, 1116 struct rtw89_tx_desc_info *desc_info, 1117 void *txdesc) 1118 { 1119 struct rtw89_rxdesc_short *txwd_v1 = (struct rtw89_rxdesc_short *)txdesc; 1120 1121 txwd_v1->dword0 = rtw89_build_txwd_fwcmd0_v1(desc_info); 1122 } 1123 EXPORT_SYMBOL(rtw89_core_fill_txdesc_fwcmd_v1); 1124 1125 static int rtw89_core_rx_process_mac_ppdu(struct rtw89_dev *rtwdev, 1126 struct sk_buff *skb, 1127 struct rtw89_rx_phy_ppdu *phy_ppdu) 1128 { 1129 bool rx_cnt_valid = false; 1130 u8 plcp_size = 0; 1131 u8 usr_num = 0; 1132 u8 *phy_sts; 1133 1134 rx_cnt_valid = RTW89_GET_RXINFO_RX_CNT_VLD(skb->data); 1135 plcp_size = RTW89_GET_RXINFO_PLCP_LEN(skb->data) << 3; 1136 usr_num = RTW89_GET_RXINFO_USR_NUM(skb->data); 1137 if (usr_num > RTW89_PPDU_MAX_USR) { 1138 rtw89_warn(rtwdev, "Invalid user number in mac info\n"); 1139 return -EINVAL; 1140 } 1141 1142 phy_sts = skb->data + RTW89_PPDU_MAC_INFO_SIZE; 1143 phy_sts += usr_num * RTW89_PPDU_MAC_INFO_USR_SIZE; 1144 /* 8-byte alignment */ 1145 if (usr_num & BIT(0)) 1146 phy_sts += RTW89_PPDU_MAC_INFO_USR_SIZE; 1147 if (rx_cnt_valid) 1148 phy_sts += RTW89_PPDU_MAC_RX_CNT_SIZE; 1149 phy_sts += plcp_size; 1150 1151 phy_ppdu->buf = phy_sts; 1152 phy_ppdu->len = skb->data + skb->len - phy_sts; 1153 1154 return 0; 1155 } 1156 1157 static void rtw89_core_rx_process_phy_ppdu_iter(void *data, 1158 struct ieee80211_sta *sta) 1159 { 1160 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 1161 struct rtw89_rx_phy_ppdu *phy_ppdu = (struct rtw89_rx_phy_ppdu *)data; 1162 1163 if (rtwsta->mac_id == phy_ppdu->mac_id && phy_ppdu->to_self) 1164 ewma_rssi_add(&rtwsta->avg_rssi, phy_ppdu->rssi_avg); 1165 } 1166 1167 #define VAR_LEN 0xff 1168 #define VAR_LEN_UNIT 8 1169 static u16 rtw89_core_get_phy_status_ie_len(struct rtw89_dev *rtwdev, u8 *addr) 1170 { 1171 static const u8 physts_ie_len_tab[32] = { 1172 16, 32, 24, 24, 8, 8, 8, 8, VAR_LEN, 8, VAR_LEN, 176, VAR_LEN, 1173 VAR_LEN, VAR_LEN, VAR_LEN, VAR_LEN, VAR_LEN, 16, 24, VAR_LEN, 1174 VAR_LEN, VAR_LEN, 0, 24, 24, 24, 24, 32, 32, 32, 32 1175 }; 1176 u16 ie_len; 1177 u8 ie; 1178 1179 ie = RTW89_GET_PHY_STS_IE_TYPE(addr); 1180 if (physts_ie_len_tab[ie] != VAR_LEN) 1181 ie_len = physts_ie_len_tab[ie]; 1182 else 1183 ie_len = RTW89_GET_PHY_STS_IE_LEN(addr) * VAR_LEN_UNIT; 1184 1185 return ie_len; 1186 } 1187 1188 static void rtw89_core_parse_phy_status_ie01(struct rtw89_dev *rtwdev, u8 *addr, 1189 struct rtw89_rx_phy_ppdu *phy_ppdu) 1190 { 1191 s16 cfo; 1192 1193 phy_ppdu->chan_idx = RTW89_GET_PHY_STS_IE01_CH_IDX(addr); 1194 if (phy_ppdu->rate < RTW89_HW_RATE_OFDM6) 1195 return; 1196 /* sign conversion for S(12,2) */ 1197 cfo = sign_extend32(RTW89_GET_PHY_STS_IE01_CFO(addr), 11); 1198 rtw89_phy_cfo_parse(rtwdev, cfo, phy_ppdu); 1199 } 1200 1201 static int rtw89_core_process_phy_status_ie(struct rtw89_dev *rtwdev, u8 *addr, 1202 struct rtw89_rx_phy_ppdu *phy_ppdu) 1203 { 1204 u8 ie; 1205 1206 ie = RTW89_GET_PHY_STS_IE_TYPE(addr); 1207 switch (ie) { 1208 case RTW89_PHYSTS_IE01_CMN_OFDM: 1209 rtw89_core_parse_phy_status_ie01(rtwdev, addr, phy_ppdu); 1210 break; 1211 default: 1212 break; 1213 } 1214 1215 return 0; 1216 } 1217 1218 static void rtw89_core_update_phy_ppdu(struct rtw89_rx_phy_ppdu *phy_ppdu) 1219 { 1220 s8 *rssi = phy_ppdu->rssi; 1221 u8 *buf = phy_ppdu->buf; 1222 1223 phy_ppdu->ie = RTW89_GET_PHY_STS_IE_MAP(buf); 1224 phy_ppdu->rssi_avg = RTW89_GET_PHY_STS_RSSI_AVG(buf); 1225 rssi[RF_PATH_A] = RTW89_RSSI_RAW_TO_DBM(RTW89_GET_PHY_STS_RSSI_A(buf)); 1226 rssi[RF_PATH_B] = RTW89_RSSI_RAW_TO_DBM(RTW89_GET_PHY_STS_RSSI_B(buf)); 1227 rssi[RF_PATH_C] = RTW89_RSSI_RAW_TO_DBM(RTW89_GET_PHY_STS_RSSI_C(buf)); 1228 rssi[RF_PATH_D] = RTW89_RSSI_RAW_TO_DBM(RTW89_GET_PHY_STS_RSSI_D(buf)); 1229 } 1230 1231 static int rtw89_core_rx_process_phy_ppdu(struct rtw89_dev *rtwdev, 1232 struct rtw89_rx_phy_ppdu *phy_ppdu) 1233 { 1234 if (RTW89_GET_PHY_STS_LEN(phy_ppdu->buf) << 3 != phy_ppdu->len) { 1235 rtw89_warn(rtwdev, "phy ppdu len mismatch\n"); 1236 return -EINVAL; 1237 } 1238 rtw89_core_update_phy_ppdu(phy_ppdu); 1239 ieee80211_iterate_stations_atomic(rtwdev->hw, 1240 rtw89_core_rx_process_phy_ppdu_iter, 1241 phy_ppdu); 1242 1243 return 0; 1244 } 1245 1246 static int rtw89_core_rx_parse_phy_sts(struct rtw89_dev *rtwdev, 1247 struct rtw89_rx_phy_ppdu *phy_ppdu) 1248 { 1249 u16 ie_len; 1250 u8 *pos, *end; 1251 1252 /* mark invalid reports and bypass them */ 1253 if (phy_ppdu->ie < RTW89_CCK_PKT) 1254 return -EINVAL; 1255 1256 pos = (u8 *)phy_ppdu->buf + PHY_STS_HDR_LEN; 1257 end = (u8 *)phy_ppdu->buf + phy_ppdu->len; 1258 while (pos < end) { 1259 ie_len = rtw89_core_get_phy_status_ie_len(rtwdev, pos); 1260 rtw89_core_process_phy_status_ie(rtwdev, pos, phy_ppdu); 1261 pos += ie_len; 1262 if (pos > end || ie_len == 0) { 1263 rtw89_debug(rtwdev, RTW89_DBG_TXRX, 1264 "phy status parse failed\n"); 1265 return -EINVAL; 1266 } 1267 } 1268 1269 return 0; 1270 } 1271 1272 static void rtw89_core_rx_process_phy_sts(struct rtw89_dev *rtwdev, 1273 struct rtw89_rx_phy_ppdu *phy_ppdu) 1274 { 1275 int ret; 1276 1277 ret = rtw89_core_rx_parse_phy_sts(rtwdev, phy_ppdu); 1278 if (ret) 1279 rtw89_debug(rtwdev, RTW89_DBG_TXRX, "parse phy sts failed\n"); 1280 else 1281 phy_ppdu->valid = true; 1282 } 1283 1284 static u8 rtw89_rxdesc_to_nl_he_gi(struct rtw89_dev *rtwdev, 1285 const struct rtw89_rx_desc_info *desc_info, 1286 bool rx_status) 1287 { 1288 switch (desc_info->gi_ltf) { 1289 case RTW89_GILTF_SGI_4XHE08: 1290 case RTW89_GILTF_2XHE08: 1291 case RTW89_GILTF_1XHE08: 1292 return NL80211_RATE_INFO_HE_GI_0_8; 1293 case RTW89_GILTF_2XHE16: 1294 case RTW89_GILTF_1XHE16: 1295 return NL80211_RATE_INFO_HE_GI_1_6; 1296 case RTW89_GILTF_LGI_4XHE32: 1297 return NL80211_RATE_INFO_HE_GI_3_2; 1298 default: 1299 rtw89_warn(rtwdev, "invalid gi_ltf=%d", desc_info->gi_ltf); 1300 return rx_status ? NL80211_RATE_INFO_HE_GI_3_2 : U8_MAX; 1301 } 1302 } 1303 1304 static bool rtw89_core_rx_ppdu_match(struct rtw89_dev *rtwdev, 1305 struct rtw89_rx_desc_info *desc_info, 1306 struct ieee80211_rx_status *status) 1307 { 1308 u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0; 1309 u8 data_rate_mode, bw, rate_idx = MASKBYTE0, gi_ltf; 1310 u16 data_rate; 1311 bool ret; 1312 1313 data_rate = desc_info->data_rate; 1314 data_rate_mode = GET_DATA_RATE_MODE(data_rate); 1315 if (data_rate_mode == DATA_RATE_MODE_NON_HT) { 1316 rate_idx = GET_DATA_RATE_NOT_HT_IDX(data_rate); 1317 /* rate_idx is still hardware value here */ 1318 } else if (data_rate_mode == DATA_RATE_MODE_HT) { 1319 rate_idx = GET_DATA_RATE_HT_IDX(data_rate); 1320 } else if (data_rate_mode == DATA_RATE_MODE_VHT) { 1321 rate_idx = GET_DATA_RATE_VHT_HE_IDX(data_rate); 1322 } else if (data_rate_mode == DATA_RATE_MODE_HE) { 1323 rate_idx = GET_DATA_RATE_VHT_HE_IDX(data_rate); 1324 } else { 1325 rtw89_warn(rtwdev, "invalid RX rate mode %d\n", data_rate_mode); 1326 } 1327 1328 bw = rtw89_hw_to_rate_info_bw(desc_info->bw); 1329 gi_ltf = rtw89_rxdesc_to_nl_he_gi(rtwdev, desc_info, false); 1330 ret = rtwdev->ppdu_sts.curr_rx_ppdu_cnt[band] == desc_info->ppdu_cnt && 1331 status->rate_idx == rate_idx && 1332 status->he_gi == gi_ltf && 1333 status->bw == bw; 1334 1335 return ret; 1336 } 1337 1338 struct rtw89_vif_rx_stats_iter_data { 1339 struct rtw89_dev *rtwdev; 1340 struct rtw89_rx_phy_ppdu *phy_ppdu; 1341 struct rtw89_rx_desc_info *desc_info; 1342 struct sk_buff *skb; 1343 const u8 *bssid; 1344 }; 1345 1346 static void rtw89_stats_trigger_frame(struct rtw89_dev *rtwdev, 1347 struct ieee80211_vif *vif, 1348 struct sk_buff *skb) 1349 { 1350 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 1351 struct ieee80211_trigger *tf = (struct ieee80211_trigger *)skb->data; 1352 u8 *pos, *end, type; 1353 u16 aid; 1354 1355 if (!ether_addr_equal(vif->bss_conf.bssid, tf->ta) || 1356 rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION || 1357 rtwvif->net_type == RTW89_NET_TYPE_NO_LINK) 1358 return; 1359 1360 type = le64_get_bits(tf->common_info, IEEE80211_TRIGGER_TYPE_MASK); 1361 if (type != IEEE80211_TRIGGER_TYPE_BASIC) 1362 return; 1363 1364 end = (u8 *)tf + skb->len; 1365 pos = tf->variable; 1366 1367 while (end - pos >= RTW89_TF_BASIC_USER_INFO_SZ) { 1368 aid = RTW89_GET_TF_USER_INFO_AID12(pos); 1369 rtw89_debug(rtwdev, RTW89_DBG_TXRX, 1370 "[TF] aid: %d, ul_mcs: %d, rua: %d\n", 1371 aid, RTW89_GET_TF_USER_INFO_UL_MCS(pos), 1372 RTW89_GET_TF_USER_INFO_RUA(pos)); 1373 1374 if (aid == RTW89_TF_PAD) 1375 break; 1376 1377 if (aid == vif->bss_conf.aid) { 1378 rtwvif->stats.rx_tf_acc++; 1379 rtwdev->stats.rx_tf_acc++; 1380 break; 1381 } 1382 1383 pos += RTW89_TF_BASIC_USER_INFO_SZ; 1384 } 1385 } 1386 1387 static void rtw89_vif_rx_stats_iter(void *data, u8 *mac, 1388 struct ieee80211_vif *vif) 1389 { 1390 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 1391 struct rtw89_vif_rx_stats_iter_data *iter_data = data; 1392 struct rtw89_dev *rtwdev = iter_data->rtwdev; 1393 struct rtw89_pkt_stat *pkt_stat = &rtwdev->phystat.cur_pkt_stat; 1394 struct rtw89_rx_desc_info *desc_info = iter_data->desc_info; 1395 struct sk_buff *skb = iter_data->skb; 1396 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 1397 const u8 *bssid = iter_data->bssid; 1398 1399 if (ieee80211_is_trigger(hdr->frame_control)) { 1400 rtw89_stats_trigger_frame(rtwdev, vif, skb); 1401 return; 1402 } 1403 1404 if (!ether_addr_equal(vif->bss_conf.bssid, bssid)) 1405 return; 1406 1407 if (ieee80211_is_beacon(hdr->frame_control)) 1408 pkt_stat->beacon_nr++; 1409 1410 if (!ether_addr_equal(vif->addr, hdr->addr1)) 1411 return; 1412 1413 if (desc_info->data_rate < RTW89_HW_RATE_NR) 1414 pkt_stat->rx_rate_cnt[desc_info->data_rate]++; 1415 1416 rtw89_traffic_stats_accu(rtwdev, &rtwvif->stats, skb, false); 1417 } 1418 1419 static void rtw89_core_rx_stats(struct rtw89_dev *rtwdev, 1420 struct rtw89_rx_phy_ppdu *phy_ppdu, 1421 struct rtw89_rx_desc_info *desc_info, 1422 struct sk_buff *skb) 1423 { 1424 struct rtw89_vif_rx_stats_iter_data iter_data; 1425 1426 rtw89_traffic_stats_accu(rtwdev, &rtwdev->stats, skb, false); 1427 1428 iter_data.rtwdev = rtwdev; 1429 iter_data.phy_ppdu = phy_ppdu; 1430 iter_data.desc_info = desc_info; 1431 iter_data.skb = skb; 1432 iter_data.bssid = get_hdr_bssid((struct ieee80211_hdr *)skb->data); 1433 rtw89_iterate_vifs_bh(rtwdev, rtw89_vif_rx_stats_iter, &iter_data); 1434 } 1435 1436 static void rtw89_correct_cck_chan(struct rtw89_dev *rtwdev, 1437 struct ieee80211_rx_status *status) 1438 { 1439 u16 chan = rtwdev->hal.prev_primary_channel; 1440 u8 band = chan <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ; 1441 1442 if (status->band != NL80211_BAND_2GHZ && 1443 status->encoding == RX_ENC_LEGACY && 1444 status->rate_idx < RTW89_HW_RATE_OFDM6) { 1445 status->freq = ieee80211_channel_to_frequency(chan, band); 1446 status->band = band; 1447 } 1448 } 1449 1450 static void rtw89_core_hw_to_sband_rate(struct ieee80211_rx_status *rx_status) 1451 { 1452 if (rx_status->band == NL80211_BAND_2GHZ || 1453 rx_status->encoding != RX_ENC_LEGACY) 1454 return; 1455 1456 /* Some control frames' freq(ACKs in this case) are reported wrong due 1457 * to FW notify timing, set to lowest rate to prevent overflow. 1458 */ 1459 if (rx_status->rate_idx < RTW89_HW_RATE_OFDM6) { 1460 rx_status->rate_idx = 0; 1461 return; 1462 } 1463 1464 /* No 4 CCK rates for non-2G */ 1465 rx_status->rate_idx -= 4; 1466 } 1467 1468 static void rtw89_core_rx_to_mac80211(struct rtw89_dev *rtwdev, 1469 struct rtw89_rx_phy_ppdu *phy_ppdu, 1470 struct rtw89_rx_desc_info *desc_info, 1471 struct sk_buff *skb_ppdu, 1472 struct ieee80211_rx_status *rx_status) 1473 { 1474 rtw89_core_hw_to_sband_rate(rx_status); 1475 rtw89_core_rx_stats(rtwdev, phy_ppdu, desc_info, skb_ppdu); 1476 /* In low power mode, it does RX in thread context. */ 1477 local_bh_disable(); 1478 ieee80211_rx_napi(rtwdev->hw, NULL, skb_ppdu, &rtwdev->napi); 1479 local_bh_enable(); 1480 rtwdev->napi_budget_countdown--; 1481 } 1482 1483 static void rtw89_core_rx_pending_skb(struct rtw89_dev *rtwdev, 1484 struct rtw89_rx_phy_ppdu *phy_ppdu, 1485 struct rtw89_rx_desc_info *desc_info, 1486 struct sk_buff *skb) 1487 { 1488 u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0; 1489 int curr = rtwdev->ppdu_sts.curr_rx_ppdu_cnt[band]; 1490 struct sk_buff *skb_ppdu = NULL, *tmp; 1491 struct ieee80211_rx_status *rx_status; 1492 1493 if (curr > RTW89_MAX_PPDU_CNT) 1494 return; 1495 1496 skb_queue_walk_safe(&rtwdev->ppdu_sts.rx_queue[band], skb_ppdu, tmp) { 1497 skb_unlink(skb_ppdu, &rtwdev->ppdu_sts.rx_queue[band]); 1498 rx_status = IEEE80211_SKB_RXCB(skb_ppdu); 1499 if (rtw89_core_rx_ppdu_match(rtwdev, desc_info, rx_status)) 1500 rtw89_chip_query_ppdu(rtwdev, phy_ppdu, rx_status); 1501 rtw89_correct_cck_chan(rtwdev, rx_status); 1502 rtw89_core_rx_to_mac80211(rtwdev, phy_ppdu, desc_info, skb_ppdu, rx_status); 1503 } 1504 } 1505 1506 static void rtw89_core_rx_process_ppdu_sts(struct rtw89_dev *rtwdev, 1507 struct rtw89_rx_desc_info *desc_info, 1508 struct sk_buff *skb) 1509 { 1510 struct rtw89_rx_phy_ppdu phy_ppdu = {.buf = skb->data, .valid = false, 1511 .len = skb->len, 1512 .to_self = desc_info->addr1_match, 1513 .rate = desc_info->data_rate, 1514 .mac_id = desc_info->mac_id}; 1515 int ret; 1516 1517 if (desc_info->mac_info_valid) 1518 rtw89_core_rx_process_mac_ppdu(rtwdev, skb, &phy_ppdu); 1519 ret = rtw89_core_rx_process_phy_ppdu(rtwdev, &phy_ppdu); 1520 if (ret) 1521 rtw89_debug(rtwdev, RTW89_DBG_TXRX, "process ppdu failed\n"); 1522 1523 rtw89_core_rx_process_phy_sts(rtwdev, &phy_ppdu); 1524 rtw89_core_rx_pending_skb(rtwdev, &phy_ppdu, desc_info, skb); 1525 dev_kfree_skb_any(skb); 1526 } 1527 1528 static void rtw89_core_rx_process_report(struct rtw89_dev *rtwdev, 1529 struct rtw89_rx_desc_info *desc_info, 1530 struct sk_buff *skb) 1531 { 1532 switch (desc_info->pkt_type) { 1533 case RTW89_CORE_RX_TYPE_C2H: 1534 rtw89_fw_c2h_irqsafe(rtwdev, skb); 1535 break; 1536 case RTW89_CORE_RX_TYPE_PPDU_STAT: 1537 rtw89_core_rx_process_ppdu_sts(rtwdev, desc_info, skb); 1538 break; 1539 default: 1540 rtw89_debug(rtwdev, RTW89_DBG_TXRX, "unhandled pkt_type=%d\n", 1541 desc_info->pkt_type); 1542 dev_kfree_skb_any(skb); 1543 break; 1544 } 1545 } 1546 1547 void rtw89_core_query_rxdesc(struct rtw89_dev *rtwdev, 1548 struct rtw89_rx_desc_info *desc_info, 1549 u8 *data, u32 data_offset) 1550 { 1551 const struct rtw89_chip_info *chip = rtwdev->chip; 1552 struct rtw89_rxdesc_short *rxd_s; 1553 struct rtw89_rxdesc_long *rxd_l; 1554 u8 shift_len, drv_info_len; 1555 1556 rxd_s = (struct rtw89_rxdesc_short *)(data + data_offset); 1557 desc_info->pkt_size = RTW89_GET_RXWD_PKT_SIZE(rxd_s); 1558 desc_info->drv_info_size = RTW89_GET_RXWD_DRV_INFO_SIZE(rxd_s); 1559 desc_info->long_rxdesc = RTW89_GET_RXWD_LONG_RXD(rxd_s); 1560 desc_info->pkt_type = RTW89_GET_RXWD_RPKT_TYPE(rxd_s); 1561 desc_info->mac_info_valid = RTW89_GET_RXWD_MAC_INFO_VALID(rxd_s); 1562 if (chip->chip_id == RTL8852C) 1563 desc_info->bw = RTW89_GET_RXWD_BW_V1(rxd_s); 1564 else 1565 desc_info->bw = RTW89_GET_RXWD_BW(rxd_s); 1566 desc_info->data_rate = RTW89_GET_RXWD_DATA_RATE(rxd_s); 1567 desc_info->gi_ltf = RTW89_GET_RXWD_GI_LTF(rxd_s); 1568 desc_info->user_id = RTW89_GET_RXWD_USER_ID(rxd_s); 1569 desc_info->sr_en = RTW89_GET_RXWD_SR_EN(rxd_s); 1570 desc_info->ppdu_cnt = RTW89_GET_RXWD_PPDU_CNT(rxd_s); 1571 desc_info->ppdu_type = RTW89_GET_RXWD_PPDU_TYPE(rxd_s); 1572 desc_info->free_run_cnt = RTW89_GET_RXWD_FREE_RUN_CNT(rxd_s); 1573 desc_info->icv_err = RTW89_GET_RXWD_ICV_ERR(rxd_s); 1574 desc_info->crc32_err = RTW89_GET_RXWD_CRC32_ERR(rxd_s); 1575 desc_info->hw_dec = RTW89_GET_RXWD_HW_DEC(rxd_s); 1576 desc_info->sw_dec = RTW89_GET_RXWD_SW_DEC(rxd_s); 1577 desc_info->addr1_match = RTW89_GET_RXWD_A1_MATCH(rxd_s); 1578 1579 shift_len = desc_info->shift << 1; /* 2-byte unit */ 1580 drv_info_len = desc_info->drv_info_size << 3; /* 8-byte unit */ 1581 desc_info->offset = data_offset + shift_len + drv_info_len; 1582 desc_info->ready = true; 1583 1584 if (!desc_info->long_rxdesc) 1585 return; 1586 1587 rxd_l = (struct rtw89_rxdesc_long *)(data + data_offset); 1588 desc_info->frame_type = RTW89_GET_RXWD_TYPE(rxd_l); 1589 desc_info->addr_cam_valid = RTW89_GET_RXWD_ADDR_CAM_VLD(rxd_l); 1590 desc_info->addr_cam_id = RTW89_GET_RXWD_ADDR_CAM_ID(rxd_l); 1591 desc_info->sec_cam_id = RTW89_GET_RXWD_SEC_CAM_ID(rxd_l); 1592 desc_info->mac_id = RTW89_GET_RXWD_MAC_ID(rxd_l); 1593 desc_info->rx_pl_id = RTW89_GET_RXWD_RX_PL_ID(rxd_l); 1594 } 1595 EXPORT_SYMBOL(rtw89_core_query_rxdesc); 1596 1597 struct rtw89_core_iter_rx_status { 1598 struct rtw89_dev *rtwdev; 1599 struct ieee80211_rx_status *rx_status; 1600 struct rtw89_rx_desc_info *desc_info; 1601 u8 mac_id; 1602 }; 1603 1604 static 1605 void rtw89_core_stats_sta_rx_status_iter(void *data, struct ieee80211_sta *sta) 1606 { 1607 struct rtw89_core_iter_rx_status *iter_data = 1608 (struct rtw89_core_iter_rx_status *)data; 1609 struct ieee80211_rx_status *rx_status = iter_data->rx_status; 1610 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 1611 struct rtw89_rx_desc_info *desc_info = iter_data->desc_info; 1612 u8 mac_id = iter_data->mac_id; 1613 1614 if (mac_id != rtwsta->mac_id) 1615 return; 1616 1617 rtwsta->rx_status = *rx_status; 1618 rtwsta->rx_hw_rate = desc_info->data_rate; 1619 } 1620 1621 static void rtw89_core_stats_sta_rx_status(struct rtw89_dev *rtwdev, 1622 struct rtw89_rx_desc_info *desc_info, 1623 struct ieee80211_rx_status *rx_status) 1624 { 1625 struct rtw89_core_iter_rx_status iter_data; 1626 1627 if (!desc_info->addr1_match || !desc_info->long_rxdesc) 1628 return; 1629 1630 if (desc_info->frame_type != RTW89_RX_TYPE_DATA) 1631 return; 1632 1633 iter_data.rtwdev = rtwdev; 1634 iter_data.rx_status = rx_status; 1635 iter_data.desc_info = desc_info; 1636 iter_data.mac_id = desc_info->mac_id; 1637 ieee80211_iterate_stations_atomic(rtwdev->hw, 1638 rtw89_core_stats_sta_rx_status_iter, 1639 &iter_data); 1640 } 1641 1642 static void rtw89_core_update_rx_status(struct rtw89_dev *rtwdev, 1643 struct rtw89_rx_desc_info *desc_info, 1644 struct ieee80211_rx_status *rx_status) 1645 { 1646 struct ieee80211_hw *hw = rtwdev->hw; 1647 struct rtw89_hal *hal = &rtwdev->hal; 1648 u16 data_rate; 1649 u8 data_rate_mode; 1650 1651 /* currently using single PHY */ 1652 rx_status->freq = hw->conf.chandef.chan->center_freq; 1653 rx_status->band = hw->conf.chandef.chan->band; 1654 1655 if (rtwdev->scanning && 1656 RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw)) { 1657 u8 chan = hal->current_primary_channel; 1658 u8 band = hal->current_band_type; 1659 enum nl80211_band nl_band; 1660 1661 nl_band = rtw89_hw_to_nl80211_band(band); 1662 rx_status->freq = ieee80211_channel_to_frequency(chan, nl_band); 1663 rx_status->band = nl_band; 1664 } 1665 1666 if (desc_info->icv_err || desc_info->crc32_err) 1667 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC; 1668 1669 if (desc_info->hw_dec && 1670 !(desc_info->sw_dec || desc_info->icv_err)) 1671 rx_status->flag |= RX_FLAG_DECRYPTED; 1672 1673 rx_status->bw = rtw89_hw_to_rate_info_bw(desc_info->bw); 1674 1675 data_rate = desc_info->data_rate; 1676 data_rate_mode = GET_DATA_RATE_MODE(data_rate); 1677 if (data_rate_mode == DATA_RATE_MODE_NON_HT) { 1678 rx_status->encoding = RX_ENC_LEGACY; 1679 rx_status->rate_idx = GET_DATA_RATE_NOT_HT_IDX(data_rate); 1680 /* convert rate_idx after we get the correct band */ 1681 } else if (data_rate_mode == DATA_RATE_MODE_HT) { 1682 rx_status->encoding = RX_ENC_HT; 1683 rx_status->rate_idx = GET_DATA_RATE_HT_IDX(data_rate); 1684 if (desc_info->gi_ltf) 1685 rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI; 1686 } else if (data_rate_mode == DATA_RATE_MODE_VHT) { 1687 rx_status->encoding = RX_ENC_VHT; 1688 rx_status->rate_idx = GET_DATA_RATE_VHT_HE_IDX(data_rate); 1689 rx_status->nss = GET_DATA_RATE_NSS(data_rate) + 1; 1690 if (desc_info->gi_ltf) 1691 rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI; 1692 } else if (data_rate_mode == DATA_RATE_MODE_HE) { 1693 rx_status->encoding = RX_ENC_HE; 1694 rx_status->rate_idx = GET_DATA_RATE_VHT_HE_IDX(data_rate); 1695 rx_status->nss = GET_DATA_RATE_NSS(data_rate) + 1; 1696 } else { 1697 rtw89_warn(rtwdev, "invalid RX rate mode %d\n", data_rate_mode); 1698 } 1699 1700 /* he_gi is used to match ppdu, so we always fill it. */ 1701 rx_status->he_gi = rtw89_rxdesc_to_nl_he_gi(rtwdev, desc_info, true); 1702 rx_status->flag |= RX_FLAG_MACTIME_START; 1703 rx_status->mactime = desc_info->free_run_cnt; 1704 1705 rtw89_core_stats_sta_rx_status(rtwdev, desc_info, rx_status); 1706 } 1707 1708 static enum rtw89_ps_mode rtw89_update_ps_mode(struct rtw89_dev *rtwdev) 1709 { 1710 const struct rtw89_chip_info *chip = rtwdev->chip; 1711 1712 if (rtw89_disable_ps_mode || !chip->ps_mode_supported) 1713 return RTW89_PS_MODE_NONE; 1714 1715 if (chip->ps_mode_supported & BIT(RTW89_PS_MODE_PWR_GATED)) 1716 return RTW89_PS_MODE_PWR_GATED; 1717 1718 if (chip->ps_mode_supported & BIT(RTW89_PS_MODE_CLK_GATED)) 1719 return RTW89_PS_MODE_CLK_GATED; 1720 1721 if (chip->ps_mode_supported & BIT(RTW89_PS_MODE_RFOFF)) 1722 return RTW89_PS_MODE_RFOFF; 1723 1724 return RTW89_PS_MODE_NONE; 1725 } 1726 1727 static void rtw89_core_flush_ppdu_rx_queue(struct rtw89_dev *rtwdev, 1728 struct rtw89_rx_desc_info *desc_info) 1729 { 1730 struct rtw89_ppdu_sts_info *ppdu_sts = &rtwdev->ppdu_sts; 1731 u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0; 1732 struct ieee80211_rx_status *rx_status; 1733 struct sk_buff *skb_ppdu, *tmp; 1734 1735 skb_queue_walk_safe(&ppdu_sts->rx_queue[band], skb_ppdu, tmp) { 1736 skb_unlink(skb_ppdu, &ppdu_sts->rx_queue[band]); 1737 rx_status = IEEE80211_SKB_RXCB(skb_ppdu); 1738 rtw89_core_rx_to_mac80211(rtwdev, NULL, desc_info, skb_ppdu, rx_status); 1739 } 1740 } 1741 1742 void rtw89_core_rx(struct rtw89_dev *rtwdev, 1743 struct rtw89_rx_desc_info *desc_info, 1744 struct sk_buff *skb) 1745 { 1746 struct ieee80211_rx_status *rx_status; 1747 struct rtw89_ppdu_sts_info *ppdu_sts = &rtwdev->ppdu_sts; 1748 u8 ppdu_cnt = desc_info->ppdu_cnt; 1749 u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0; 1750 1751 if (desc_info->pkt_type != RTW89_CORE_RX_TYPE_WIFI) { 1752 rtw89_core_rx_process_report(rtwdev, desc_info, skb); 1753 return; 1754 } 1755 1756 if (ppdu_sts->curr_rx_ppdu_cnt[band] != ppdu_cnt) { 1757 rtw89_core_flush_ppdu_rx_queue(rtwdev, desc_info); 1758 ppdu_sts->curr_rx_ppdu_cnt[band] = ppdu_cnt; 1759 } 1760 1761 rx_status = IEEE80211_SKB_RXCB(skb); 1762 memset(rx_status, 0, sizeof(*rx_status)); 1763 rtw89_core_update_rx_status(rtwdev, desc_info, rx_status); 1764 if (desc_info->long_rxdesc && 1765 BIT(desc_info->frame_type) & PPDU_FILTER_BITMAP) 1766 skb_queue_tail(&ppdu_sts->rx_queue[band], skb); 1767 else 1768 rtw89_core_rx_to_mac80211(rtwdev, NULL, desc_info, skb, rx_status); 1769 } 1770 EXPORT_SYMBOL(rtw89_core_rx); 1771 1772 void rtw89_core_napi_start(struct rtw89_dev *rtwdev) 1773 { 1774 if (test_and_set_bit(RTW89_FLAG_NAPI_RUNNING, rtwdev->flags)) 1775 return; 1776 1777 napi_enable(&rtwdev->napi); 1778 } 1779 EXPORT_SYMBOL(rtw89_core_napi_start); 1780 1781 void rtw89_core_napi_stop(struct rtw89_dev *rtwdev) 1782 { 1783 if (!test_and_clear_bit(RTW89_FLAG_NAPI_RUNNING, rtwdev->flags)) 1784 return; 1785 1786 napi_synchronize(&rtwdev->napi); 1787 napi_disable(&rtwdev->napi); 1788 } 1789 EXPORT_SYMBOL(rtw89_core_napi_stop); 1790 1791 void rtw89_core_napi_init(struct rtw89_dev *rtwdev) 1792 { 1793 init_dummy_netdev(&rtwdev->netdev); 1794 netif_napi_add(&rtwdev->netdev, &rtwdev->napi, 1795 rtwdev->hci.ops->napi_poll, NAPI_POLL_WEIGHT); 1796 } 1797 EXPORT_SYMBOL(rtw89_core_napi_init); 1798 1799 void rtw89_core_napi_deinit(struct rtw89_dev *rtwdev) 1800 { 1801 rtw89_core_napi_stop(rtwdev); 1802 netif_napi_del(&rtwdev->napi); 1803 } 1804 EXPORT_SYMBOL(rtw89_core_napi_deinit); 1805 1806 static void rtw89_core_ba_work(struct work_struct *work) 1807 { 1808 struct rtw89_dev *rtwdev = 1809 container_of(work, struct rtw89_dev, ba_work); 1810 struct rtw89_txq *rtwtxq, *tmp; 1811 int ret; 1812 1813 spin_lock_bh(&rtwdev->ba_lock); 1814 list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->ba_list, list) { 1815 struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq); 1816 struct ieee80211_sta *sta = txq->sta; 1817 struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL; 1818 u8 tid = txq->tid; 1819 1820 if (!sta) { 1821 rtw89_warn(rtwdev, "cannot start BA without sta\n"); 1822 goto skip_ba_work; 1823 } 1824 1825 if (rtwsta->disassoc) { 1826 rtw89_debug(rtwdev, RTW89_DBG_TXRX, 1827 "cannot start BA with disassoc sta\n"); 1828 goto skip_ba_work; 1829 } 1830 1831 ret = ieee80211_start_tx_ba_session(sta, tid, 0); 1832 if (ret) { 1833 rtw89_debug(rtwdev, RTW89_DBG_TXRX, 1834 "failed to setup BA session for %pM:%2d: %d\n", 1835 sta->addr, tid, ret); 1836 if (ret == -EINVAL) 1837 set_bit(RTW89_TXQ_F_BLOCK_BA, &rtwtxq->flags); 1838 } 1839 skip_ba_work: 1840 list_del_init(&rtwtxq->list); 1841 } 1842 spin_unlock_bh(&rtwdev->ba_lock); 1843 } 1844 1845 static void rtw89_core_free_sta_pending_ba(struct rtw89_dev *rtwdev, 1846 struct ieee80211_sta *sta) 1847 { 1848 struct rtw89_txq *rtwtxq, *tmp; 1849 1850 spin_lock_bh(&rtwdev->ba_lock); 1851 list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->ba_list, list) { 1852 struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq); 1853 1854 if (sta == txq->sta) 1855 list_del_init(&rtwtxq->list); 1856 } 1857 spin_unlock_bh(&rtwdev->ba_lock); 1858 } 1859 1860 static void rtw89_core_txq_check_agg(struct rtw89_dev *rtwdev, 1861 struct rtw89_txq *rtwtxq, 1862 struct sk_buff *skb) 1863 { 1864 struct ieee80211_hw *hw = rtwdev->hw; 1865 struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq); 1866 struct ieee80211_sta *sta = txq->sta; 1867 struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL; 1868 1869 if (unlikely(skb_get_queue_mapping(skb) == IEEE80211_AC_VO)) 1870 return; 1871 1872 if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE))) 1873 return; 1874 1875 if (unlikely(!sta)) 1876 return; 1877 1878 if (unlikely(test_bit(RTW89_TXQ_F_BLOCK_BA, &rtwtxq->flags))) 1879 return; 1880 1881 if (test_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags)) { 1882 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_AMPDU; 1883 return; 1884 } 1885 1886 spin_lock_bh(&rtwdev->ba_lock); 1887 if (!rtwsta->disassoc && list_empty(&rtwtxq->list)) { 1888 list_add_tail(&rtwtxq->list, &rtwdev->ba_list); 1889 ieee80211_queue_work(hw, &rtwdev->ba_work); 1890 } 1891 spin_unlock_bh(&rtwdev->ba_lock); 1892 } 1893 1894 static void rtw89_core_txq_push(struct rtw89_dev *rtwdev, 1895 struct rtw89_txq *rtwtxq, 1896 unsigned long frame_cnt, 1897 unsigned long byte_cnt) 1898 { 1899 struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq); 1900 struct ieee80211_vif *vif = txq->vif; 1901 struct ieee80211_sta *sta = txq->sta; 1902 struct sk_buff *skb; 1903 unsigned long i; 1904 int ret; 1905 1906 rcu_read_lock(); 1907 for (i = 0; i < frame_cnt; i++) { 1908 skb = ieee80211_tx_dequeue_ni(rtwdev->hw, txq); 1909 if (!skb) { 1910 rtw89_debug(rtwdev, RTW89_DBG_TXRX, "dequeue a NULL skb\n"); 1911 goto out; 1912 } 1913 rtw89_core_txq_check_agg(rtwdev, rtwtxq, skb); 1914 ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, NULL); 1915 if (ret) { 1916 rtw89_err(rtwdev, "failed to push txq: %d\n", ret); 1917 ieee80211_free_txskb(rtwdev->hw, skb); 1918 break; 1919 } 1920 } 1921 out: 1922 rcu_read_unlock(); 1923 } 1924 1925 static u32 rtw89_check_and_reclaim_tx_resource(struct rtw89_dev *rtwdev, u8 tid) 1926 { 1927 u8 qsel, ch_dma; 1928 1929 qsel = rtw89_core_get_qsel(rtwdev, tid); 1930 ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel); 1931 1932 return rtw89_hci_check_and_reclaim_tx_resource(rtwdev, ch_dma); 1933 } 1934 1935 static bool rtw89_core_txq_agg_wait(struct rtw89_dev *rtwdev, 1936 struct ieee80211_txq *txq, 1937 unsigned long *frame_cnt, 1938 bool *sched_txq, bool *reinvoke) 1939 { 1940 struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv; 1941 struct ieee80211_sta *sta = txq->sta; 1942 struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL; 1943 1944 if (!sta || rtwsta->max_agg_wait <= 0) 1945 return false; 1946 1947 if (rtwdev->stats.tx_tfc_lv <= RTW89_TFC_MID) 1948 return false; 1949 1950 if (*frame_cnt > 1) { 1951 *frame_cnt -= 1; 1952 *sched_txq = true; 1953 *reinvoke = true; 1954 rtwtxq->wait_cnt = 1; 1955 return false; 1956 } 1957 1958 if (*frame_cnt == 1 && rtwtxq->wait_cnt < rtwsta->max_agg_wait) { 1959 *reinvoke = true; 1960 rtwtxq->wait_cnt++; 1961 return true; 1962 } 1963 1964 rtwtxq->wait_cnt = 0; 1965 return false; 1966 } 1967 1968 static void rtw89_core_txq_schedule(struct rtw89_dev *rtwdev, u8 ac, bool *reinvoke) 1969 { 1970 struct ieee80211_hw *hw = rtwdev->hw; 1971 struct ieee80211_txq *txq; 1972 struct rtw89_txq *rtwtxq; 1973 unsigned long frame_cnt; 1974 unsigned long byte_cnt; 1975 u32 tx_resource; 1976 bool sched_txq; 1977 1978 ieee80211_txq_schedule_start(hw, ac); 1979 while ((txq = ieee80211_next_txq(hw, ac))) { 1980 rtwtxq = (struct rtw89_txq *)txq->drv_priv; 1981 tx_resource = rtw89_check_and_reclaim_tx_resource(rtwdev, txq->tid); 1982 sched_txq = false; 1983 1984 ieee80211_txq_get_depth(txq, &frame_cnt, &byte_cnt); 1985 if (rtw89_core_txq_agg_wait(rtwdev, txq, &frame_cnt, &sched_txq, reinvoke)) { 1986 ieee80211_return_txq(hw, txq, true); 1987 continue; 1988 } 1989 frame_cnt = min_t(unsigned long, frame_cnt, tx_resource); 1990 rtw89_core_txq_push(rtwdev, rtwtxq, frame_cnt, byte_cnt); 1991 ieee80211_return_txq(hw, txq, sched_txq); 1992 if (frame_cnt != 0) 1993 rtw89_core_tx_kick_off(rtwdev, rtw89_core_get_qsel(rtwdev, txq->tid)); 1994 } 1995 ieee80211_txq_schedule_end(hw, ac); 1996 } 1997 1998 static void rtw89_ips_work(struct work_struct *work) 1999 { 2000 struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev, 2001 ips_work); 2002 mutex_lock(&rtwdev->mutex); 2003 if (rtwdev->hw->conf.flags & IEEE80211_CONF_IDLE) 2004 rtw89_enter_ips(rtwdev); 2005 mutex_unlock(&rtwdev->mutex); 2006 } 2007 2008 static void rtw89_core_txq_work(struct work_struct *w) 2009 { 2010 struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev, txq_work); 2011 bool reinvoke = false; 2012 u8 ac; 2013 2014 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2015 rtw89_core_txq_schedule(rtwdev, ac, &reinvoke); 2016 2017 if (reinvoke) { 2018 /* reinvoke to process the last frame */ 2019 mod_delayed_work(rtwdev->txq_wq, &rtwdev->txq_reinvoke_work, 1); 2020 } 2021 } 2022 2023 static void rtw89_core_txq_reinvoke_work(struct work_struct *w) 2024 { 2025 struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev, 2026 txq_reinvoke_work.work); 2027 2028 queue_work(rtwdev->txq_wq, &rtwdev->txq_work); 2029 } 2030 2031 static enum rtw89_tfc_lv rtw89_get_traffic_level(struct rtw89_dev *rtwdev, 2032 u32 throughput, u64 cnt) 2033 { 2034 if (cnt < 100) 2035 return RTW89_TFC_IDLE; 2036 if (throughput > 50) 2037 return RTW89_TFC_HIGH; 2038 if (throughput > 10) 2039 return RTW89_TFC_MID; 2040 if (throughput > 2) 2041 return RTW89_TFC_LOW; 2042 return RTW89_TFC_ULTRA_LOW; 2043 } 2044 2045 static bool rtw89_traffic_stats_calc(struct rtw89_dev *rtwdev, 2046 struct rtw89_traffic_stats *stats) 2047 { 2048 enum rtw89_tfc_lv tx_tfc_lv = stats->tx_tfc_lv; 2049 enum rtw89_tfc_lv rx_tfc_lv = stats->rx_tfc_lv; 2050 2051 stats->tx_throughput_raw = (u32)(stats->tx_unicast >> RTW89_TP_SHIFT); 2052 stats->rx_throughput_raw = (u32)(stats->rx_unicast >> RTW89_TP_SHIFT); 2053 2054 ewma_tp_add(&stats->tx_ewma_tp, stats->tx_throughput_raw); 2055 ewma_tp_add(&stats->rx_ewma_tp, stats->rx_throughput_raw); 2056 2057 stats->tx_throughput = ewma_tp_read(&stats->tx_ewma_tp); 2058 stats->rx_throughput = ewma_tp_read(&stats->rx_ewma_tp); 2059 stats->tx_tfc_lv = rtw89_get_traffic_level(rtwdev, stats->tx_throughput, 2060 stats->tx_cnt); 2061 stats->rx_tfc_lv = rtw89_get_traffic_level(rtwdev, stats->rx_throughput, 2062 stats->rx_cnt); 2063 stats->tx_avg_len = stats->tx_cnt ? 2064 DIV_ROUND_DOWN_ULL(stats->tx_unicast, stats->tx_cnt) : 0; 2065 stats->rx_avg_len = stats->rx_cnt ? 2066 DIV_ROUND_DOWN_ULL(stats->rx_unicast, stats->rx_cnt) : 0; 2067 2068 stats->tx_unicast = 0; 2069 stats->rx_unicast = 0; 2070 stats->tx_cnt = 0; 2071 stats->rx_cnt = 0; 2072 stats->rx_tf_periodic = stats->rx_tf_acc; 2073 stats->rx_tf_acc = 0; 2074 2075 if (tx_tfc_lv != stats->tx_tfc_lv || rx_tfc_lv != stats->rx_tfc_lv) 2076 return true; 2077 2078 return false; 2079 } 2080 2081 static bool rtw89_traffic_stats_track(struct rtw89_dev *rtwdev) 2082 { 2083 struct rtw89_vif *rtwvif; 2084 bool tfc_changed; 2085 2086 tfc_changed = rtw89_traffic_stats_calc(rtwdev, &rtwdev->stats); 2087 rtw89_for_each_rtwvif(rtwdev, rtwvif) 2088 rtw89_traffic_stats_calc(rtwdev, &rtwvif->stats); 2089 2090 return tfc_changed; 2091 } 2092 2093 static void rtw89_vif_enter_lps(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 2094 { 2095 if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION) 2096 return; 2097 2098 if (rtwvif->stats.tx_tfc_lv == RTW89_TFC_IDLE && 2099 rtwvif->stats.rx_tfc_lv == RTW89_TFC_IDLE) 2100 rtw89_enter_lps(rtwdev, rtwvif->mac_id); 2101 } 2102 2103 static void rtw89_enter_lps_track(struct rtw89_dev *rtwdev) 2104 { 2105 struct rtw89_vif *rtwvif; 2106 2107 rtw89_for_each_rtwvif(rtwdev, rtwvif) 2108 rtw89_vif_enter_lps(rtwdev, rtwvif); 2109 } 2110 2111 void rtw89_traffic_stats_init(struct rtw89_dev *rtwdev, 2112 struct rtw89_traffic_stats *stats) 2113 { 2114 stats->tx_unicast = 0; 2115 stats->rx_unicast = 0; 2116 stats->tx_cnt = 0; 2117 stats->rx_cnt = 0; 2118 ewma_tp_init(&stats->tx_ewma_tp); 2119 ewma_tp_init(&stats->rx_ewma_tp); 2120 } 2121 2122 static void rtw89_track_work(struct work_struct *work) 2123 { 2124 struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev, 2125 track_work.work); 2126 bool tfc_changed; 2127 2128 mutex_lock(&rtwdev->mutex); 2129 2130 if (!test_bit(RTW89_FLAG_RUNNING, rtwdev->flags)) 2131 goto out; 2132 2133 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work, 2134 RTW89_TRACK_WORK_PERIOD); 2135 2136 tfc_changed = rtw89_traffic_stats_track(rtwdev); 2137 if (rtwdev->scanning) 2138 goto out; 2139 2140 rtw89_leave_lps(rtwdev); 2141 2142 if (tfc_changed) { 2143 rtw89_hci_recalc_int_mit(rtwdev); 2144 rtw89_btc_ntfy_wl_sta(rtwdev); 2145 } 2146 rtw89_mac_bf_monitor_track(rtwdev); 2147 rtw89_phy_stat_track(rtwdev); 2148 rtw89_phy_env_monitor_track(rtwdev); 2149 rtw89_phy_dig(rtwdev); 2150 rtw89_chip_rfk_track(rtwdev); 2151 rtw89_phy_ra_update(rtwdev); 2152 rtw89_phy_cfo_track(rtwdev); 2153 2154 if (rtwdev->lps_enabled && !rtwdev->btc.lps) 2155 rtw89_enter_lps_track(rtwdev); 2156 2157 out: 2158 mutex_unlock(&rtwdev->mutex); 2159 } 2160 2161 u8 rtw89_core_acquire_bit_map(unsigned long *addr, unsigned long size) 2162 { 2163 unsigned long bit; 2164 2165 bit = find_first_zero_bit(addr, size); 2166 if (bit < size) 2167 set_bit(bit, addr); 2168 2169 return bit; 2170 } 2171 2172 void rtw89_core_release_bit_map(unsigned long *addr, u8 bit) 2173 { 2174 clear_bit(bit, addr); 2175 } 2176 2177 void rtw89_core_release_all_bits_map(unsigned long *addr, unsigned int nbits) 2178 { 2179 bitmap_zero(addr, nbits); 2180 } 2181 2182 int rtw89_core_acquire_sta_ba_entry(struct rtw89_sta *rtwsta, u8 tid, u8 *cam_idx) 2183 { 2184 struct rtw89_ba_cam_entry *entry; 2185 u8 idx; 2186 2187 idx = rtw89_core_acquire_bit_map(rtwsta->ba_cam_map, RTW89_BA_CAM_NUM); 2188 if (idx == RTW89_BA_CAM_NUM) { 2189 /* allocate a static BA CAM to tid=0, so replace the existing 2190 * one if BA CAM is full. Hardware will process the original tid 2191 * automatically. 2192 */ 2193 if (tid != 0) 2194 return -ENOSPC; 2195 2196 idx = 0; 2197 } 2198 2199 entry = &rtwsta->ba_cam_entry[idx]; 2200 entry->tid = tid; 2201 *cam_idx = idx; 2202 2203 return 0; 2204 } 2205 2206 int rtw89_core_release_sta_ba_entry(struct rtw89_sta *rtwsta, u8 tid, u8 *cam_idx) 2207 { 2208 struct rtw89_ba_cam_entry *entry; 2209 int i; 2210 2211 for (i = 0; i < RTW89_BA_CAM_NUM; i++) { 2212 if (!test_bit(i, rtwsta->ba_cam_map)) 2213 continue; 2214 2215 entry = &rtwsta->ba_cam_entry[i]; 2216 if (entry->tid != tid) 2217 continue; 2218 2219 rtw89_core_release_bit_map(rtwsta->ba_cam_map, i); 2220 *cam_idx = i; 2221 return 0; 2222 } 2223 2224 return -ENOENT; 2225 } 2226 2227 #define RTW89_TYPE_MAPPING(_type) \ 2228 case NL80211_IFTYPE_ ## _type: \ 2229 rtwvif->wifi_role = RTW89_WIFI_ROLE_ ## _type; \ 2230 break 2231 void rtw89_vif_type_mapping(struct ieee80211_vif *vif, bool assoc) 2232 { 2233 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 2234 2235 switch (vif->type) { 2236 RTW89_TYPE_MAPPING(ADHOC); 2237 RTW89_TYPE_MAPPING(STATION); 2238 RTW89_TYPE_MAPPING(AP); 2239 RTW89_TYPE_MAPPING(MONITOR); 2240 RTW89_TYPE_MAPPING(MESH_POINT); 2241 default: 2242 WARN_ON(1); 2243 break; 2244 } 2245 2246 switch (vif->type) { 2247 case NL80211_IFTYPE_AP: 2248 case NL80211_IFTYPE_MESH_POINT: 2249 rtwvif->net_type = RTW89_NET_TYPE_AP_MODE; 2250 rtwvif->self_role = RTW89_SELF_ROLE_AP; 2251 break; 2252 case NL80211_IFTYPE_ADHOC: 2253 rtwvif->net_type = RTW89_NET_TYPE_AD_HOC; 2254 rtwvif->self_role = RTW89_SELF_ROLE_CLIENT; 2255 break; 2256 case NL80211_IFTYPE_STATION: 2257 if (assoc) { 2258 rtwvif->net_type = RTW89_NET_TYPE_INFRA; 2259 rtwvif->trigger = vif->bss_conf.he_support; 2260 } else { 2261 rtwvif->net_type = RTW89_NET_TYPE_NO_LINK; 2262 rtwvif->trigger = false; 2263 } 2264 rtwvif->self_role = RTW89_SELF_ROLE_CLIENT; 2265 rtwvif->addr_cam.sec_ent_mode = RTW89_ADDR_CAM_SEC_NORMAL; 2266 break; 2267 default: 2268 WARN_ON(1); 2269 break; 2270 } 2271 } 2272 2273 int rtw89_core_sta_add(struct rtw89_dev *rtwdev, 2274 struct ieee80211_vif *vif, 2275 struct ieee80211_sta *sta) 2276 { 2277 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 2278 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 2279 int i; 2280 2281 rtwsta->rtwvif = rtwvif; 2282 rtwsta->prev_rssi = 0; 2283 2284 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) 2285 rtw89_core_txq_init(rtwdev, sta->txq[i]); 2286 2287 ewma_rssi_init(&rtwsta->avg_rssi); 2288 2289 if (vif->type == NL80211_IFTYPE_STATION) { 2290 /* for station mode, assign the mac_id from itself */ 2291 rtwsta->mac_id = rtwvif->mac_id; 2292 rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta, 2293 BTC_ROLE_MSTS_STA_CONN_START); 2294 rtw89_chip_rfk_channel(rtwdev); 2295 } else if (vif->type == NL80211_IFTYPE_AP) { 2296 rtwsta->mac_id = rtw89_core_acquire_bit_map(rtwdev->mac_id_map, 2297 RTW89_MAX_MAC_ID_NUM); 2298 } 2299 2300 return 0; 2301 } 2302 2303 int rtw89_core_sta_disassoc(struct rtw89_dev *rtwdev, 2304 struct ieee80211_vif *vif, 2305 struct ieee80211_sta *sta) 2306 { 2307 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 2308 2309 rtwdev->total_sta_assoc--; 2310 rtwsta->disassoc = true; 2311 2312 return 0; 2313 } 2314 2315 int rtw89_core_sta_disconnect(struct rtw89_dev *rtwdev, 2316 struct ieee80211_vif *vif, 2317 struct ieee80211_sta *sta) 2318 { 2319 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 2320 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 2321 int ret; 2322 2323 rtw89_mac_bf_monitor_calc(rtwdev, sta, true); 2324 rtw89_mac_bf_disassoc(rtwdev, vif, sta); 2325 rtw89_core_free_sta_pending_ba(rtwdev, sta); 2326 if (vif->type == NL80211_IFTYPE_AP) 2327 rtw89_cam_deinit_addr_cam(rtwdev, &rtwsta->addr_cam); 2328 2329 if (vif->type == NL80211_IFTYPE_STATION) 2330 rtw89_vif_type_mapping(vif, false); 2331 2332 ret = rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, sta); 2333 if (ret) { 2334 rtw89_warn(rtwdev, "failed to send h2c cmac table\n"); 2335 return ret; 2336 } 2337 2338 ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, true); 2339 if (ret) { 2340 rtw89_warn(rtwdev, "failed to send h2c join info\n"); 2341 return ret; 2342 } 2343 2344 if (vif->type == NL80211_IFTYPE_AP) { 2345 ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta, RTW89_ROLE_REMOVE); 2346 if (ret) { 2347 rtw89_warn(rtwdev, "failed to send h2c role info\n"); 2348 return ret; 2349 } 2350 } 2351 2352 /* update cam aid mac_id net_type */ 2353 ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL); 2354 if (ret) { 2355 rtw89_warn(rtwdev, "failed to send h2c cam\n"); 2356 return ret; 2357 } 2358 2359 return ret; 2360 } 2361 2362 int rtw89_core_sta_assoc(struct rtw89_dev *rtwdev, 2363 struct ieee80211_vif *vif, 2364 struct ieee80211_sta *sta) 2365 { 2366 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 2367 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 2368 int ret; 2369 2370 if (vif->type == NL80211_IFTYPE_AP) { 2371 ret = rtw89_mac_set_macid_pause(rtwdev, rtwsta->mac_id, false); 2372 if (ret) { 2373 rtw89_warn(rtwdev, "failed to send h2c macid pause\n"); 2374 return ret; 2375 } 2376 2377 ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta, RTW89_ROLE_CREATE); 2378 if (ret) { 2379 rtw89_warn(rtwdev, "failed to send h2c role info\n"); 2380 return ret; 2381 } 2382 2383 ret = rtw89_cam_init_addr_cam(rtwdev, &rtwsta->addr_cam, &rtwvif->bssid_cam); 2384 if (ret) { 2385 rtw89_warn(rtwdev, "failed to send h2c init addr cam\n"); 2386 return ret; 2387 } 2388 } 2389 2390 ret = rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, sta); 2391 if (ret) { 2392 rtw89_warn(rtwdev, "failed to send h2c cmac table\n"); 2393 return ret; 2394 } 2395 2396 ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, false); 2397 if (ret) { 2398 rtw89_warn(rtwdev, "failed to send h2c join info\n"); 2399 return ret; 2400 } 2401 2402 /* update cam aid mac_id net_type */ 2403 rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL); 2404 if (ret) { 2405 rtw89_warn(rtwdev, "failed to send h2c cam\n"); 2406 return ret; 2407 } 2408 2409 ret = rtw89_fw_h2c_general_pkt(rtwdev, rtwsta->mac_id); 2410 if (ret) { 2411 rtw89_warn(rtwdev, "failed to send h2c general packet\n"); 2412 return ret; 2413 } 2414 2415 rtwdev->total_sta_assoc++; 2416 rtw89_phy_ra_assoc(rtwdev, sta); 2417 rtw89_mac_bf_assoc(rtwdev, vif, sta); 2418 rtw89_mac_bf_monitor_calc(rtwdev, sta, false); 2419 2420 if (vif->type == NL80211_IFTYPE_STATION) { 2421 rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta, 2422 BTC_ROLE_MSTS_STA_CONN_END); 2423 rtw89_core_get_no_ul_ofdma_htc(rtwdev, &rtwsta->htc_template); 2424 } 2425 2426 return ret; 2427 } 2428 2429 int rtw89_core_sta_remove(struct rtw89_dev *rtwdev, 2430 struct ieee80211_vif *vif, 2431 struct ieee80211_sta *sta) 2432 { 2433 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 2434 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 2435 2436 if (vif->type == NL80211_IFTYPE_STATION) 2437 rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta, 2438 BTC_ROLE_MSTS_STA_DIS_CONN); 2439 else if (vif->type == NL80211_IFTYPE_AP) 2440 rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwsta->mac_id); 2441 2442 return 0; 2443 } 2444 2445 static void rtw89_init_ht_cap(struct rtw89_dev *rtwdev, 2446 struct ieee80211_sta_ht_cap *ht_cap) 2447 { 2448 static const __le16 highest[RF_PATH_MAX] = { 2449 cpu_to_le16(150), cpu_to_le16(300), cpu_to_le16(450), cpu_to_le16(600), 2450 }; 2451 struct rtw89_hal *hal = &rtwdev->hal; 2452 u8 nss = hal->rx_nss; 2453 int i; 2454 2455 ht_cap->ht_supported = true; 2456 ht_cap->cap = 0; 2457 ht_cap->cap |= IEEE80211_HT_CAP_SGI_20 | 2458 IEEE80211_HT_CAP_MAX_AMSDU | 2459 IEEE80211_HT_CAP_TX_STBC | 2460 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT); 2461 ht_cap->cap |= IEEE80211_HT_CAP_LDPC_CODING; 2462 ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 | 2463 IEEE80211_HT_CAP_DSSSCCK40 | 2464 IEEE80211_HT_CAP_SGI_40; 2465 ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K; 2466 ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE; 2467 ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED; 2468 for (i = 0; i < nss; i++) 2469 ht_cap->mcs.rx_mask[i] = 0xFF; 2470 ht_cap->mcs.rx_mask[4] = 0x01; 2471 ht_cap->mcs.rx_highest = highest[nss - 1]; 2472 } 2473 2474 static void rtw89_init_vht_cap(struct rtw89_dev *rtwdev, 2475 struct ieee80211_sta_vht_cap *vht_cap) 2476 { 2477 static const __le16 highest_bw80[RF_PATH_MAX] = { 2478 cpu_to_le16(433), cpu_to_le16(867), cpu_to_le16(1300), cpu_to_le16(1733), 2479 }; 2480 static const __le16 highest_bw160[RF_PATH_MAX] = { 2481 cpu_to_le16(867), cpu_to_le16(1733), cpu_to_le16(2600), cpu_to_le16(3467), 2482 }; 2483 const struct rtw89_chip_info *chip = rtwdev->chip; 2484 const __le16 *highest = chip->support_bw160 ? highest_bw160 : highest_bw80; 2485 struct rtw89_hal *hal = &rtwdev->hal; 2486 u16 tx_mcs_map = 0, rx_mcs_map = 0; 2487 u8 sts_cap = 3; 2488 int i; 2489 2490 for (i = 0; i < 8; i++) { 2491 if (i < hal->tx_nss) 2492 tx_mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2); 2493 else 2494 tx_mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2); 2495 if (i < hal->rx_nss) 2496 rx_mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2); 2497 else 2498 rx_mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2); 2499 } 2500 2501 vht_cap->vht_supported = true; 2502 vht_cap->cap = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 | 2503 IEEE80211_VHT_CAP_SHORT_GI_80 | 2504 IEEE80211_VHT_CAP_RXSTBC_1 | 2505 IEEE80211_VHT_CAP_HTC_VHT | 2506 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK | 2507 0; 2508 vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC; 2509 vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC; 2510 vht_cap->cap |= IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE | 2511 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE; 2512 vht_cap->cap |= sts_cap << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT; 2513 if (chip->support_bw160) 2514 vht_cap->cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ | 2515 IEEE80211_VHT_CAP_SHORT_GI_160; 2516 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(rx_mcs_map); 2517 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(tx_mcs_map); 2518 vht_cap->vht_mcs.rx_highest = highest[hal->rx_nss - 1]; 2519 vht_cap->vht_mcs.tx_highest = highest[hal->tx_nss - 1]; 2520 } 2521 2522 #define RTW89_SBAND_IFTYPES_NR 2 2523 2524 static void rtw89_init_he_cap(struct rtw89_dev *rtwdev, 2525 enum nl80211_band band, 2526 struct ieee80211_supported_band *sband) 2527 { 2528 const struct rtw89_chip_info *chip = rtwdev->chip; 2529 struct rtw89_hal *hal = &rtwdev->hal; 2530 struct ieee80211_sband_iftype_data *iftype_data; 2531 bool no_ng16 = (chip->chip_id == RTL8852A && hal->cv == CHIP_CBV) || 2532 (chip->chip_id == RTL8852B && hal->cv == CHIP_CAV); 2533 u16 mcs_map = 0; 2534 int i; 2535 int nss = hal->rx_nss; 2536 int idx = 0; 2537 2538 iftype_data = kcalloc(RTW89_SBAND_IFTYPES_NR, sizeof(*iftype_data), GFP_KERNEL); 2539 if (!iftype_data) 2540 return; 2541 2542 for (i = 0; i < 8; i++) { 2543 if (i < nss) 2544 mcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2); 2545 else 2546 mcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2); 2547 } 2548 2549 for (i = 0; i < NUM_NL80211_IFTYPES; i++) { 2550 struct ieee80211_sta_he_cap *he_cap; 2551 u8 *mac_cap_info; 2552 u8 *phy_cap_info; 2553 2554 switch (i) { 2555 case NL80211_IFTYPE_STATION: 2556 case NL80211_IFTYPE_AP: 2557 break; 2558 default: 2559 continue; 2560 } 2561 2562 if (idx >= RTW89_SBAND_IFTYPES_NR) { 2563 rtw89_warn(rtwdev, "run out of iftype_data\n"); 2564 break; 2565 } 2566 2567 iftype_data[idx].types_mask = BIT(i); 2568 he_cap = &iftype_data[idx].he_cap; 2569 mac_cap_info = he_cap->he_cap_elem.mac_cap_info; 2570 phy_cap_info = he_cap->he_cap_elem.phy_cap_info; 2571 2572 he_cap->has_he = true; 2573 if (i == NL80211_IFTYPE_AP) 2574 mac_cap_info[0] = IEEE80211_HE_MAC_CAP0_HTC_HE; 2575 if (i == NL80211_IFTYPE_STATION) 2576 mac_cap_info[1] = IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US; 2577 mac_cap_info[2] = IEEE80211_HE_MAC_CAP2_ALL_ACK | 2578 IEEE80211_HE_MAC_CAP2_BSR; 2579 mac_cap_info[3] = IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_2; 2580 if (i == NL80211_IFTYPE_AP) 2581 mac_cap_info[3] |= IEEE80211_HE_MAC_CAP3_OMI_CONTROL; 2582 mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_OPS | 2583 IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU; 2584 if (i == NL80211_IFTYPE_STATION) 2585 mac_cap_info[5] = IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX; 2586 if (band == NL80211_BAND_2GHZ) { 2587 phy_cap_info[0] = 2588 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G; 2589 } else { 2590 phy_cap_info[0] = 2591 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G; 2592 if (chip->support_bw160) 2593 phy_cap_info[0] |= IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G; 2594 } 2595 phy_cap_info[1] = IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A | 2596 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD | 2597 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US; 2598 phy_cap_info[2] = IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US | 2599 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ | 2600 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ | 2601 IEEE80211_HE_PHY_CAP2_DOPPLER_TX; 2602 phy_cap_info[3] = IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM; 2603 if (i == NL80211_IFTYPE_STATION) 2604 phy_cap_info[3] |= IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_16_QAM | 2605 IEEE80211_HE_PHY_CAP3_DCM_MAX_TX_NSS_2; 2606 if (i == NL80211_IFTYPE_AP) 2607 phy_cap_info[3] |= IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU; 2608 phy_cap_info[4] = IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE | 2609 IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4; 2610 phy_cap_info[5] = no_ng16 ? 0 : 2611 IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK | 2612 IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK; 2613 phy_cap_info[6] = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU | 2614 IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU | 2615 IEEE80211_HE_PHY_CAP6_TRIG_SU_BEAMFORMING_FB | 2616 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE; 2617 phy_cap_info[7] = IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP | 2618 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI | 2619 IEEE80211_HE_PHY_CAP7_MAX_NC_1; 2620 phy_cap_info[8] = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI | 2621 IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI | 2622 IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_996; 2623 if (chip->support_bw160) 2624 phy_cap_info[8] |= IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU | 2625 IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU; 2626 phy_cap_info[9] = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM | 2627 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU | 2628 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB | 2629 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB | 2630 u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US, 2631 IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK); 2632 if (i == NL80211_IFTYPE_STATION) 2633 phy_cap_info[9] |= IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU; 2634 he_cap->he_mcs_nss_supp.rx_mcs_80 = cpu_to_le16(mcs_map); 2635 he_cap->he_mcs_nss_supp.tx_mcs_80 = cpu_to_le16(mcs_map); 2636 if (chip->support_bw160) { 2637 he_cap->he_mcs_nss_supp.rx_mcs_160 = cpu_to_le16(mcs_map); 2638 he_cap->he_mcs_nss_supp.tx_mcs_160 = cpu_to_le16(mcs_map); 2639 } 2640 2641 if (band == NL80211_BAND_6GHZ) { 2642 __le16 capa; 2643 2644 capa = le16_encode_bits(IEEE80211_HT_MPDU_DENSITY_NONE, 2645 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START) | 2646 le16_encode_bits(IEEE80211_VHT_MAX_AMPDU_1024K, 2647 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP) | 2648 le16_encode_bits(IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454, 2649 IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN); 2650 iftype_data[idx].he_6ghz_capa.capa = capa; 2651 } 2652 2653 idx++; 2654 } 2655 2656 sband->iftype_data = iftype_data; 2657 sband->n_iftype_data = idx; 2658 } 2659 2660 static int rtw89_core_set_supported_band(struct rtw89_dev *rtwdev) 2661 { 2662 struct ieee80211_hw *hw = rtwdev->hw; 2663 struct ieee80211_supported_band *sband_2ghz = NULL, *sband_5ghz = NULL; 2664 struct ieee80211_supported_band *sband_6ghz = NULL; 2665 u32 size = sizeof(struct ieee80211_supported_band); 2666 u8 support_bands = rtwdev->chip->support_bands; 2667 2668 if (support_bands & BIT(NL80211_BAND_2GHZ)) { 2669 sband_2ghz = kmemdup(&rtw89_sband_2ghz, size, GFP_KERNEL); 2670 if (!sband_2ghz) 2671 goto err; 2672 rtw89_init_ht_cap(rtwdev, &sband_2ghz->ht_cap); 2673 rtw89_init_he_cap(rtwdev, NL80211_BAND_2GHZ, sband_2ghz); 2674 hw->wiphy->bands[NL80211_BAND_2GHZ] = sband_2ghz; 2675 } 2676 2677 if (support_bands & BIT(NL80211_BAND_5GHZ)) { 2678 sband_5ghz = kmemdup(&rtw89_sband_5ghz, size, GFP_KERNEL); 2679 if (!sband_5ghz) 2680 goto err; 2681 rtw89_init_ht_cap(rtwdev, &sband_5ghz->ht_cap); 2682 rtw89_init_vht_cap(rtwdev, &sband_5ghz->vht_cap); 2683 rtw89_init_he_cap(rtwdev, NL80211_BAND_5GHZ, sband_5ghz); 2684 hw->wiphy->bands[NL80211_BAND_5GHZ] = sband_5ghz; 2685 } 2686 2687 if (support_bands & BIT(NL80211_BAND_6GHZ)) { 2688 sband_6ghz = kmemdup(&rtw89_sband_6ghz, size, GFP_KERNEL); 2689 if (!sband_6ghz) 2690 goto err; 2691 rtw89_init_he_cap(rtwdev, NL80211_BAND_6GHZ, sband_6ghz); 2692 hw->wiphy->bands[NL80211_BAND_6GHZ] = sband_6ghz; 2693 } 2694 2695 return 0; 2696 2697 err: 2698 hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL; 2699 hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL; 2700 hw->wiphy->bands[NL80211_BAND_6GHZ] = NULL; 2701 if (sband_2ghz) 2702 kfree(sband_2ghz->iftype_data); 2703 if (sband_5ghz) 2704 kfree(sband_5ghz->iftype_data); 2705 if (sband_6ghz) 2706 kfree(sband_6ghz->iftype_data); 2707 kfree(sband_2ghz); 2708 kfree(sband_5ghz); 2709 kfree(sband_6ghz); 2710 return -ENOMEM; 2711 } 2712 2713 static void rtw89_core_clr_supported_band(struct rtw89_dev *rtwdev) 2714 { 2715 struct ieee80211_hw *hw = rtwdev->hw; 2716 2717 kfree(hw->wiphy->bands[NL80211_BAND_2GHZ]->iftype_data); 2718 kfree(hw->wiphy->bands[NL80211_BAND_5GHZ]->iftype_data); 2719 if (hw->wiphy->bands[NL80211_BAND_6GHZ]) 2720 kfree(hw->wiphy->bands[NL80211_BAND_6GHZ]->iftype_data); 2721 kfree(hw->wiphy->bands[NL80211_BAND_2GHZ]); 2722 kfree(hw->wiphy->bands[NL80211_BAND_5GHZ]); 2723 kfree(hw->wiphy->bands[NL80211_BAND_6GHZ]); 2724 hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL; 2725 hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL; 2726 hw->wiphy->bands[NL80211_BAND_6GHZ] = NULL; 2727 } 2728 2729 static void rtw89_core_ppdu_sts_init(struct rtw89_dev *rtwdev) 2730 { 2731 int i; 2732 2733 for (i = 0; i < RTW89_PHY_MAX; i++) 2734 skb_queue_head_init(&rtwdev->ppdu_sts.rx_queue[i]); 2735 for (i = 0; i < RTW89_PHY_MAX; i++) 2736 rtwdev->ppdu_sts.curr_rx_ppdu_cnt[i] = U8_MAX; 2737 } 2738 2739 void rtw89_core_update_beacon_work(struct work_struct *work) 2740 { 2741 struct rtw89_dev *rtwdev; 2742 struct rtw89_vif *rtwvif = container_of(work, struct rtw89_vif, 2743 update_beacon_work); 2744 2745 if (rtwvif->net_type != RTW89_NET_TYPE_AP_MODE) 2746 return; 2747 2748 rtwdev = rtwvif->rtwdev; 2749 mutex_lock(&rtwdev->mutex); 2750 rtw89_fw_h2c_update_beacon(rtwdev, rtwvif); 2751 mutex_unlock(&rtwdev->mutex); 2752 } 2753 2754 int rtw89_core_start(struct rtw89_dev *rtwdev) 2755 { 2756 int ret; 2757 2758 rtwdev->mac.qta_mode = RTW89_QTA_SCC; 2759 ret = rtw89_mac_init(rtwdev); 2760 if (ret) { 2761 rtw89_err(rtwdev, "mac init fail, ret:%d\n", ret); 2762 return ret; 2763 } 2764 2765 rtw89_btc_ntfy_poweron(rtwdev); 2766 2767 /* efuse process */ 2768 2769 /* pre-config BB/RF, BB reset/RFC reset */ 2770 rtw89_chip_disable_bb_rf(rtwdev); 2771 ret = rtw89_chip_enable_bb_rf(rtwdev); 2772 if (ret) 2773 return ret; 2774 2775 rtw89_phy_init_bb_reg(rtwdev); 2776 rtw89_phy_init_rf_reg(rtwdev); 2777 2778 rtw89_btc_ntfy_init(rtwdev, BTC_MODE_NORMAL); 2779 2780 rtw89_phy_dm_init(rtwdev); 2781 2782 rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, true); 2783 rtw89_mac_update_rts_threshold(rtwdev, RTW89_MAC_0); 2784 2785 ret = rtw89_hci_start(rtwdev); 2786 if (ret) { 2787 rtw89_err(rtwdev, "failed to start hci\n"); 2788 return ret; 2789 } 2790 2791 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work, 2792 RTW89_TRACK_WORK_PERIOD); 2793 2794 set_bit(RTW89_FLAG_RUNNING, rtwdev->flags); 2795 2796 rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_ON); 2797 rtw89_fw_h2c_fw_log(rtwdev, rtwdev->fw.fw_log_enable); 2798 2799 return 0; 2800 } 2801 2802 void rtw89_core_stop(struct rtw89_dev *rtwdev) 2803 { 2804 struct rtw89_btc *btc = &rtwdev->btc; 2805 2806 /* Prvent to stop twice; enter_ips and ops_stop */ 2807 if (!test_bit(RTW89_FLAG_RUNNING, rtwdev->flags)) 2808 return; 2809 2810 rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_OFF); 2811 2812 clear_bit(RTW89_FLAG_RUNNING, rtwdev->flags); 2813 2814 mutex_unlock(&rtwdev->mutex); 2815 2816 cancel_work_sync(&rtwdev->c2h_work); 2817 cancel_work_sync(&btc->eapol_notify_work); 2818 cancel_work_sync(&btc->arp_notify_work); 2819 cancel_work_sync(&btc->dhcp_notify_work); 2820 cancel_work_sync(&btc->icmp_notify_work); 2821 cancel_delayed_work_sync(&rtwdev->txq_reinvoke_work); 2822 cancel_delayed_work_sync(&rtwdev->track_work); 2823 cancel_delayed_work_sync(&rtwdev->coex_act1_work); 2824 cancel_delayed_work_sync(&rtwdev->coex_bt_devinfo_work); 2825 cancel_delayed_work_sync(&rtwdev->coex_rfk_chk_work); 2826 cancel_delayed_work_sync(&rtwdev->cfo_track_work); 2827 2828 mutex_lock(&rtwdev->mutex); 2829 2830 rtw89_btc_ntfy_poweroff(rtwdev); 2831 rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1, true); 2832 rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, true); 2833 rtw89_hci_stop(rtwdev); 2834 rtw89_hci_deinit(rtwdev); 2835 rtw89_mac_pwr_off(rtwdev); 2836 rtw89_hci_reset(rtwdev); 2837 } 2838 2839 int rtw89_core_init(struct rtw89_dev *rtwdev) 2840 { 2841 struct rtw89_btc *btc = &rtwdev->btc; 2842 int ret; 2843 u8 band; 2844 2845 INIT_LIST_HEAD(&rtwdev->ba_list); 2846 INIT_LIST_HEAD(&rtwdev->rtwvifs_list); 2847 INIT_LIST_HEAD(&rtwdev->early_h2c_list); 2848 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) { 2849 if (!(rtwdev->chip->support_bands & BIT(band))) 2850 continue; 2851 INIT_LIST_HEAD(&rtwdev->scan_info.pkt_list[band]); 2852 } 2853 INIT_WORK(&rtwdev->ba_work, rtw89_core_ba_work); 2854 INIT_WORK(&rtwdev->txq_work, rtw89_core_txq_work); 2855 INIT_DELAYED_WORK(&rtwdev->txq_reinvoke_work, rtw89_core_txq_reinvoke_work); 2856 INIT_DELAYED_WORK(&rtwdev->track_work, rtw89_track_work); 2857 INIT_DELAYED_WORK(&rtwdev->coex_act1_work, rtw89_coex_act1_work); 2858 INIT_DELAYED_WORK(&rtwdev->coex_bt_devinfo_work, rtw89_coex_bt_devinfo_work); 2859 INIT_DELAYED_WORK(&rtwdev->coex_rfk_chk_work, rtw89_coex_rfk_chk_work); 2860 INIT_DELAYED_WORK(&rtwdev->cfo_track_work, rtw89_phy_cfo_track_work); 2861 rtwdev->txq_wq = alloc_workqueue("rtw89_tx_wq", WQ_UNBOUND | WQ_HIGHPRI, 0); 2862 spin_lock_init(&rtwdev->ba_lock); 2863 spin_lock_init(&rtwdev->rpwm_lock); 2864 mutex_init(&rtwdev->mutex); 2865 mutex_init(&rtwdev->rf_mutex); 2866 rtwdev->total_sta_assoc = 0; 2867 2868 INIT_WORK(&rtwdev->c2h_work, rtw89_fw_c2h_work); 2869 INIT_WORK(&rtwdev->ips_work, rtw89_ips_work); 2870 skb_queue_head_init(&rtwdev->c2h_queue); 2871 rtw89_core_ppdu_sts_init(rtwdev); 2872 rtw89_traffic_stats_init(rtwdev, &rtwdev->stats); 2873 2874 rtwdev->ps_mode = rtw89_update_ps_mode(rtwdev); 2875 rtwdev->hal.rx_fltr = DEFAULT_AX_RX_FLTR; 2876 2877 INIT_WORK(&btc->eapol_notify_work, rtw89_btc_ntfy_eapol_packet_work); 2878 INIT_WORK(&btc->arp_notify_work, rtw89_btc_ntfy_arp_packet_work); 2879 INIT_WORK(&btc->dhcp_notify_work, rtw89_btc_ntfy_dhcp_packet_work); 2880 INIT_WORK(&btc->icmp_notify_work, rtw89_btc_ntfy_icmp_packet_work); 2881 2882 ret = rtw89_load_firmware(rtwdev); 2883 if (ret) { 2884 rtw89_warn(rtwdev, "no firmware loaded\n"); 2885 return ret; 2886 } 2887 rtw89_ser_init(rtwdev); 2888 2889 return 0; 2890 } 2891 EXPORT_SYMBOL(rtw89_core_init); 2892 2893 void rtw89_core_deinit(struct rtw89_dev *rtwdev) 2894 { 2895 rtw89_ser_deinit(rtwdev); 2896 rtw89_unload_firmware(rtwdev); 2897 rtw89_fw_free_all_early_h2c(rtwdev); 2898 2899 destroy_workqueue(rtwdev->txq_wq); 2900 mutex_destroy(&rtwdev->rf_mutex); 2901 mutex_destroy(&rtwdev->mutex); 2902 } 2903 EXPORT_SYMBOL(rtw89_core_deinit); 2904 2905 void rtw89_core_scan_start(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif, 2906 const u8 *mac_addr, bool hw_scan) 2907 { 2908 struct rtw89_hal *hal = &rtwdev->hal; 2909 2910 rtwdev->scanning = true; 2911 rtw89_leave_lps(rtwdev); 2912 if (hw_scan && (rtwdev->hw->conf.flags & IEEE80211_CONF_IDLE)) 2913 rtw89_leave_ips(rtwdev); 2914 2915 ether_addr_copy(rtwvif->mac_addr, mac_addr); 2916 rtw89_btc_ntfy_scan_start(rtwdev, RTW89_PHY_0, hal->current_band_type); 2917 rtw89_chip_rfk_scan(rtwdev, true); 2918 rtw89_hci_recalc_int_mit(rtwdev); 2919 2920 rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, mac_addr); 2921 } 2922 2923 void rtw89_core_scan_complete(struct rtw89_dev *rtwdev, 2924 struct ieee80211_vif *vif, bool hw_scan) 2925 { 2926 struct rtw89_vif *rtwvif = vif ? (struct rtw89_vif *)vif->drv_priv : NULL; 2927 2928 if (!rtwvif) 2929 return; 2930 2931 ether_addr_copy(rtwvif->mac_addr, vif->addr); 2932 rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL); 2933 2934 rtw89_chip_rfk_scan(rtwdev, false); 2935 rtw89_btc_ntfy_scan_finish(rtwdev, RTW89_PHY_0); 2936 2937 rtwdev->scanning = false; 2938 rtwdev->dig.bypass_dig = true; 2939 if (hw_scan && (rtwdev->hw->conf.flags & IEEE80211_CONF_IDLE)) 2940 ieee80211_queue_work(rtwdev->hw, &rtwdev->ips_work); 2941 } 2942 2943 static void rtw89_read_chip_ver(struct rtw89_dev *rtwdev) 2944 { 2945 const struct rtw89_chip_info *chip = rtwdev->chip; 2946 u8 cv; 2947 2948 cv = rtw89_read32_mask(rtwdev, R_AX_SYS_CFG1, B_AX_CHIP_VER_MASK); 2949 if (chip->chip_id == RTL8852A && cv <= CHIP_CBV) { 2950 if (rtw89_read32(rtwdev, R_AX_GPIO0_7_FUNC_SEL) == RTW89_R32_DEAD) 2951 cv = CHIP_CAV; 2952 else 2953 cv = CHIP_CBV; 2954 } 2955 2956 rtwdev->hal.cv = cv; 2957 } 2958 2959 static void rtw89_core_setup_phycap(struct rtw89_dev *rtwdev) 2960 { 2961 rtwdev->hal.support_cckpd = 2962 !(rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv <= CHIP_CBV) && 2963 !(rtwdev->chip->chip_id == RTL8852B && rtwdev->hal.cv <= CHIP_CAV); 2964 rtwdev->hal.support_igi = 2965 rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv <= CHIP_CBV; 2966 } 2967 2968 static int rtw89_chip_efuse_info_setup(struct rtw89_dev *rtwdev) 2969 { 2970 int ret; 2971 2972 ret = rtw89_mac_partial_init(rtwdev); 2973 if (ret) 2974 return ret; 2975 2976 ret = rtw89_parse_efuse_map(rtwdev); 2977 if (ret) 2978 return ret; 2979 2980 ret = rtw89_parse_phycap_map(rtwdev); 2981 if (ret) 2982 return ret; 2983 2984 ret = rtw89_mac_setup_phycap(rtwdev); 2985 if (ret) 2986 return ret; 2987 2988 rtw89_core_setup_phycap(rtwdev); 2989 2990 rtw89_mac_pwr_off(rtwdev); 2991 2992 return 0; 2993 } 2994 2995 static int rtw89_chip_board_info_setup(struct rtw89_dev *rtwdev) 2996 { 2997 rtw89_chip_fem_setup(rtwdev); 2998 2999 return 0; 3000 } 3001 3002 int rtw89_chip_info_setup(struct rtw89_dev *rtwdev) 3003 { 3004 int ret; 3005 3006 rtw89_read_chip_ver(rtwdev); 3007 3008 ret = rtw89_wait_firmware_completion(rtwdev); 3009 if (ret) { 3010 rtw89_err(rtwdev, "failed to wait firmware completion\n"); 3011 return ret; 3012 } 3013 3014 ret = rtw89_fw_recognize(rtwdev); 3015 if (ret) { 3016 rtw89_err(rtwdev, "failed to recognize firmware\n"); 3017 return ret; 3018 } 3019 3020 ret = rtw89_chip_efuse_info_setup(rtwdev); 3021 if (ret) 3022 return ret; 3023 3024 ret = rtw89_chip_board_info_setup(rtwdev); 3025 if (ret) 3026 return ret; 3027 3028 return 0; 3029 } 3030 EXPORT_SYMBOL(rtw89_chip_info_setup); 3031 3032 static int rtw89_core_register_hw(struct rtw89_dev *rtwdev) 3033 { 3034 struct ieee80211_hw *hw = rtwdev->hw; 3035 struct rtw89_efuse *efuse = &rtwdev->efuse; 3036 int ret; 3037 int tx_headroom = IEEE80211_HT_CTL_LEN; 3038 3039 hw->vif_data_size = sizeof(struct rtw89_vif); 3040 hw->sta_data_size = sizeof(struct rtw89_sta); 3041 hw->txq_data_size = sizeof(struct rtw89_txq); 3042 3043 SET_IEEE80211_PERM_ADDR(hw, efuse->addr); 3044 3045 hw->extra_tx_headroom = tx_headroom; 3046 hw->queues = IEEE80211_NUM_ACS; 3047 hw->max_rx_aggregation_subframes = RTW89_MAX_RX_AGG_NUM; 3048 hw->max_tx_aggregation_subframes = RTW89_MAX_TX_AGG_NUM; 3049 3050 ieee80211_hw_set(hw, SIGNAL_DBM); 3051 ieee80211_hw_set(hw, HAS_RATE_CONTROL); 3052 ieee80211_hw_set(hw, MFP_CAPABLE); 3053 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS); 3054 ieee80211_hw_set(hw, AMPDU_AGGREGATION); 3055 ieee80211_hw_set(hw, RX_INCLUDES_FCS); 3056 ieee80211_hw_set(hw, TX_AMSDU); 3057 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT); 3058 ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU); 3059 ieee80211_hw_set(hw, SUPPORTS_PS); 3060 ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS); 3061 ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS); 3062 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID); 3063 3064 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) | 3065 BIT(NL80211_IFTYPE_AP); 3066 hw->wiphy->available_antennas_tx = BIT(rtwdev->chip->rf_path_num) - 1; 3067 hw->wiphy->available_antennas_rx = BIT(rtwdev->chip->rf_path_num) - 1; 3068 3069 hw->wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR; 3070 3071 hw->wiphy->max_scan_ssids = RTW89_SCANOFLD_MAX_SSID; 3072 hw->wiphy->max_scan_ie_len = RTW89_SCANOFLD_MAX_IE_LEN; 3073 3074 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CAN_REPLACE_PTK0); 3075 3076 ret = rtw89_core_set_supported_band(rtwdev); 3077 if (ret) { 3078 rtw89_err(rtwdev, "failed to set supported band\n"); 3079 return ret; 3080 } 3081 3082 hw->wiphy->reg_notifier = rtw89_regd_notifier; 3083 hw->wiphy->sar_capa = &rtw89_sar_capa; 3084 3085 ret = ieee80211_register_hw(hw); 3086 if (ret) { 3087 rtw89_err(rtwdev, "failed to register hw\n"); 3088 goto err; 3089 } 3090 3091 ret = rtw89_regd_init(rtwdev, rtw89_regd_notifier); 3092 if (ret) { 3093 rtw89_err(rtwdev, "failed to init regd\n"); 3094 goto err; 3095 } 3096 3097 return 0; 3098 3099 err: 3100 return ret; 3101 } 3102 3103 static void rtw89_core_unregister_hw(struct rtw89_dev *rtwdev) 3104 { 3105 struct ieee80211_hw *hw = rtwdev->hw; 3106 3107 ieee80211_unregister_hw(hw); 3108 rtw89_core_clr_supported_band(rtwdev); 3109 } 3110 3111 int rtw89_core_register(struct rtw89_dev *rtwdev) 3112 { 3113 int ret; 3114 3115 ret = rtw89_core_register_hw(rtwdev); 3116 if (ret) { 3117 rtw89_err(rtwdev, "failed to register core hw\n"); 3118 return ret; 3119 } 3120 3121 rtw89_debugfs_init(rtwdev); 3122 3123 return 0; 3124 } 3125 EXPORT_SYMBOL(rtw89_core_register); 3126 3127 void rtw89_core_unregister(struct rtw89_dev *rtwdev) 3128 { 3129 rtw89_core_unregister_hw(rtwdev); 3130 } 3131 EXPORT_SYMBOL(rtw89_core_unregister); 3132 3133 MODULE_AUTHOR("Realtek Corporation"); 3134 MODULE_DESCRIPTION("Realtek 802.11ax wireless core module"); 3135 MODULE_LICENSE("Dual BSD/GPL"); 3136