1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright(c) 2018-2019 Realtek Corporation 3 */ 4 5 #include "main.h" 6 #include "regd.h" 7 #include "fw.h" 8 #include "ps.h" 9 #include "sec.h" 10 #include "mac.h" 11 #include "coex.h" 12 #include "phy.h" 13 #include "reg.h" 14 #include "efuse.h" 15 #include "tx.h" 16 #include "debug.h" 17 #include "bf.h" 18 19 unsigned int rtw_fw_lps_deep_mode; 20 EXPORT_SYMBOL(rtw_fw_lps_deep_mode); 21 bool rtw_bf_support = true; 22 unsigned int rtw_debug_mask; 23 EXPORT_SYMBOL(rtw_debug_mask); 24 25 module_param_named(lps_deep_mode, rtw_fw_lps_deep_mode, uint, 0644); 26 module_param_named(support_bf, rtw_bf_support, bool, 0644); 27 module_param_named(debug_mask, rtw_debug_mask, uint, 0644); 28 29 MODULE_PARM_DESC(lps_deep_mode, "Deeper PS mode. If 0, deep PS is disabled"); 30 MODULE_PARM_DESC(support_bf, "Set Y to enable beamformee support"); 31 MODULE_PARM_DESC(debug_mask, "Debugging mask"); 32 33 static struct ieee80211_channel rtw_channeltable_2g[] = { 34 {.center_freq = 2412, .hw_value = 1,}, 35 {.center_freq = 2417, .hw_value = 2,}, 36 {.center_freq = 2422, .hw_value = 3,}, 37 {.center_freq = 2427, .hw_value = 4,}, 38 {.center_freq = 2432, .hw_value = 5,}, 39 {.center_freq = 2437, .hw_value = 6,}, 40 {.center_freq = 2442, .hw_value = 7,}, 41 {.center_freq = 2447, .hw_value = 8,}, 42 {.center_freq = 2452, .hw_value = 9,}, 43 {.center_freq = 2457, .hw_value = 10,}, 44 {.center_freq = 2462, .hw_value = 11,}, 45 {.center_freq = 2467, .hw_value = 12,}, 46 {.center_freq = 2472, .hw_value = 13,}, 47 {.center_freq = 2484, .hw_value = 14,}, 48 }; 49 50 static struct ieee80211_channel rtw_channeltable_5g[] = { 51 {.center_freq = 5180, .hw_value = 36,}, 52 {.center_freq = 5200, .hw_value = 40,}, 53 {.center_freq = 5220, .hw_value = 44,}, 54 {.center_freq = 5240, .hw_value = 48,}, 55 {.center_freq = 5260, .hw_value = 52,}, 56 {.center_freq = 5280, .hw_value = 56,}, 57 {.center_freq = 5300, .hw_value = 60,}, 58 {.center_freq = 5320, .hw_value = 64,}, 59 {.center_freq = 5500, .hw_value = 100,}, 60 {.center_freq = 5520, .hw_value = 104,}, 61 {.center_freq = 5540, .hw_value = 108,}, 62 {.center_freq = 5560, .hw_value = 112,}, 63 {.center_freq = 5580, .hw_value = 116,}, 64 {.center_freq = 5600, .hw_value = 120,}, 65 {.center_freq = 5620, .hw_value = 124,}, 66 {.center_freq = 5640, .hw_value = 128,}, 67 {.center_freq = 5660, .hw_value = 132,}, 68 {.center_freq = 5680, .hw_value = 136,}, 69 {.center_freq = 5700, .hw_value = 140,}, 70 {.center_freq = 5745, .hw_value = 149,}, 71 {.center_freq = 5765, .hw_value = 153,}, 72 {.center_freq = 5785, .hw_value = 157,}, 73 {.center_freq = 5805, .hw_value = 161,}, 74 {.center_freq = 5825, .hw_value = 165, 75 .flags = IEEE80211_CHAN_NO_HT40MINUS}, 76 }; 77 78 static struct ieee80211_rate rtw_ratetable[] = { 79 {.bitrate = 10, .hw_value = 0x00,}, 80 {.bitrate = 20, .hw_value = 0x01,}, 81 {.bitrate = 55, .hw_value = 0x02,}, 82 {.bitrate = 110, .hw_value = 0x03,}, 83 {.bitrate = 60, .hw_value = 0x04,}, 84 {.bitrate = 90, .hw_value = 0x05,}, 85 {.bitrate = 120, .hw_value = 0x06,}, 86 {.bitrate = 180, .hw_value = 0x07,}, 87 {.bitrate = 240, .hw_value = 0x08,}, 88 {.bitrate = 360, .hw_value = 0x09,}, 89 {.bitrate = 480, .hw_value = 0x0a,}, 90 {.bitrate = 540, .hw_value = 0x0b,}, 91 }; 92 93 u16 rtw_desc_to_bitrate(u8 desc_rate) 94 { 95 struct ieee80211_rate rate; 96 97 if (WARN(desc_rate >= ARRAY_SIZE(rtw_ratetable), "invalid desc rate\n")) 98 return 0; 99 100 rate = rtw_ratetable[desc_rate]; 101 102 return rate.bitrate; 103 } 104 105 static struct ieee80211_supported_band rtw_band_2ghz = { 106 .band = NL80211_BAND_2GHZ, 107 108 .channels = rtw_channeltable_2g, 109 .n_channels = ARRAY_SIZE(rtw_channeltable_2g), 110 111 .bitrates = rtw_ratetable, 112 .n_bitrates = ARRAY_SIZE(rtw_ratetable), 113 114 .ht_cap = {0}, 115 .vht_cap = {0}, 116 }; 117 118 static struct ieee80211_supported_band rtw_band_5ghz = { 119 .band = NL80211_BAND_5GHZ, 120 121 .channels = rtw_channeltable_5g, 122 .n_channels = ARRAY_SIZE(rtw_channeltable_5g), 123 124 /* 5G has no CCK rates */ 125 .bitrates = rtw_ratetable + 4, 126 .n_bitrates = ARRAY_SIZE(rtw_ratetable) - 4, 127 128 .ht_cap = {0}, 129 .vht_cap = {0}, 130 }; 131 132 struct rtw_watch_dog_iter_data { 133 struct rtw_dev *rtwdev; 134 struct rtw_vif *rtwvif; 135 }; 136 137 static void rtw_dynamic_csi_rate(struct rtw_dev *rtwdev, struct rtw_vif *rtwvif) 138 { 139 struct rtw_bf_info *bf_info = &rtwdev->bf_info; 140 struct rtw_chip_info *chip = rtwdev->chip; 141 u8 fix_rate_enable = 0; 142 u8 new_csi_rate_idx; 143 144 if (rtwvif->bfee.role != RTW_BFEE_SU && 145 rtwvif->bfee.role != RTW_BFEE_MU) 146 return; 147 148 chip->ops->cfg_csi_rate(rtwdev, rtwdev->dm_info.min_rssi, 149 bf_info->cur_csi_rpt_rate, 150 fix_rate_enable, &new_csi_rate_idx); 151 152 if (new_csi_rate_idx != bf_info->cur_csi_rpt_rate) 153 bf_info->cur_csi_rpt_rate = new_csi_rate_idx; 154 } 155 156 static void rtw_vif_watch_dog_iter(void *data, u8 *mac, 157 struct ieee80211_vif *vif) 158 { 159 struct rtw_watch_dog_iter_data *iter_data = data; 160 struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv; 161 162 if (vif->type == NL80211_IFTYPE_STATION) 163 if (vif->bss_conf.assoc) 164 iter_data->rtwvif = rtwvif; 165 166 rtw_dynamic_csi_rate(iter_data->rtwdev, rtwvif); 167 168 rtwvif->stats.tx_unicast = 0; 169 rtwvif->stats.rx_unicast = 0; 170 rtwvif->stats.tx_cnt = 0; 171 rtwvif->stats.rx_cnt = 0; 172 } 173 174 /* process TX/RX statistics periodically for hardware, 175 * the information helps hardware to enhance performance 176 */ 177 static void rtw_watch_dog_work(struct work_struct *work) 178 { 179 struct rtw_dev *rtwdev = container_of(work, struct rtw_dev, 180 watch_dog_work.work); 181 struct rtw_traffic_stats *stats = &rtwdev->stats; 182 struct rtw_watch_dog_iter_data data = {}; 183 bool busy_traffic = test_bit(RTW_FLAG_BUSY_TRAFFIC, rtwdev->flags); 184 bool ps_active; 185 186 mutex_lock(&rtwdev->mutex); 187 188 if (!test_bit(RTW_FLAG_RUNNING, rtwdev->flags)) 189 goto unlock; 190 191 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->watch_dog_work, 192 RTW_WATCH_DOG_DELAY_TIME); 193 194 if (rtwdev->stats.tx_cnt > 100 || rtwdev->stats.rx_cnt > 100) 195 set_bit(RTW_FLAG_BUSY_TRAFFIC, rtwdev->flags); 196 else 197 clear_bit(RTW_FLAG_BUSY_TRAFFIC, rtwdev->flags); 198 199 if (busy_traffic != test_bit(RTW_FLAG_BUSY_TRAFFIC, rtwdev->flags)) 200 rtw_coex_wl_status_change_notify(rtwdev); 201 202 if (stats->tx_cnt > RTW_LPS_THRESHOLD || 203 stats->rx_cnt > RTW_LPS_THRESHOLD) 204 ps_active = true; 205 else 206 ps_active = false; 207 208 ewma_tp_add(&stats->tx_ewma_tp, 209 (u32)(stats->tx_unicast >> RTW_TP_SHIFT)); 210 ewma_tp_add(&stats->rx_ewma_tp, 211 (u32)(stats->rx_unicast >> RTW_TP_SHIFT)); 212 stats->tx_throughput = ewma_tp_read(&stats->tx_ewma_tp); 213 stats->rx_throughput = ewma_tp_read(&stats->rx_ewma_tp); 214 215 /* reset tx/rx statictics */ 216 stats->tx_unicast = 0; 217 stats->rx_unicast = 0; 218 stats->tx_cnt = 0; 219 stats->rx_cnt = 0; 220 221 if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags)) 222 goto unlock; 223 224 /* make sure BB/RF is working for dynamic mech */ 225 rtw_leave_lps(rtwdev); 226 227 rtw_phy_dynamic_mechanism(rtwdev); 228 229 data.rtwdev = rtwdev; 230 /* use atomic version to avoid taking local->iflist_mtx mutex */ 231 rtw_iterate_vifs_atomic(rtwdev, rtw_vif_watch_dog_iter, &data); 232 233 /* fw supports only one station associated to enter lps, if there are 234 * more than two stations associated to the AP, then we can not enter 235 * lps, because fw does not handle the overlapped beacon interval 236 * 237 * mac80211 should iterate vifs and determine if driver can enter 238 * ps by passing IEEE80211_CONF_PS to us, all we need to do is to 239 * get that vif and check if device is having traffic more than the 240 * threshold. 241 */ 242 if (rtwdev->ps_enabled && data.rtwvif && !ps_active) 243 rtw_enter_lps(rtwdev, data.rtwvif->port); 244 245 rtwdev->watch_dog_cnt++; 246 247 unlock: 248 mutex_unlock(&rtwdev->mutex); 249 } 250 251 static void rtw_c2h_work(struct work_struct *work) 252 { 253 struct rtw_dev *rtwdev = container_of(work, struct rtw_dev, c2h_work); 254 struct sk_buff *skb, *tmp; 255 256 skb_queue_walk_safe(&rtwdev->c2h_queue, skb, tmp) { 257 skb_unlink(skb, &rtwdev->c2h_queue); 258 rtw_fw_c2h_cmd_handle(rtwdev, skb); 259 dev_kfree_skb_any(skb); 260 } 261 } 262 263 struct rtw_txq_ba_iter_data { 264 }; 265 266 static void rtw_txq_ba_iter(void *data, struct ieee80211_sta *sta) 267 { 268 struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv; 269 int ret; 270 u8 tid; 271 272 tid = find_first_bit(si->tid_ba, IEEE80211_NUM_TIDS); 273 while (tid != IEEE80211_NUM_TIDS) { 274 clear_bit(tid, si->tid_ba); 275 ret = ieee80211_start_tx_ba_session(sta, tid, 0); 276 if (ret == -EINVAL) { 277 struct ieee80211_txq *txq; 278 struct rtw_txq *rtwtxq; 279 280 txq = sta->txq[tid]; 281 rtwtxq = (struct rtw_txq *)txq->drv_priv; 282 set_bit(RTW_TXQ_BLOCK_BA, &rtwtxq->flags); 283 } 284 285 tid = find_first_bit(si->tid_ba, IEEE80211_NUM_TIDS); 286 } 287 } 288 289 static void rtw_txq_ba_work(struct work_struct *work) 290 { 291 struct rtw_dev *rtwdev = container_of(work, struct rtw_dev, ba_work); 292 struct rtw_txq_ba_iter_data data; 293 294 rtw_iterate_stas_atomic(rtwdev, rtw_txq_ba_iter, &data); 295 } 296 297 void rtw_get_channel_params(struct cfg80211_chan_def *chandef, 298 struct rtw_channel_params *chan_params) 299 { 300 struct ieee80211_channel *channel = chandef->chan; 301 enum nl80211_chan_width width = chandef->width; 302 u8 *cch_by_bw = chan_params->cch_by_bw; 303 u32 primary_freq, center_freq; 304 u8 center_chan; 305 u8 bandwidth = RTW_CHANNEL_WIDTH_20; 306 u8 primary_chan_idx = 0; 307 u8 i; 308 309 center_chan = channel->hw_value; 310 primary_freq = channel->center_freq; 311 center_freq = chandef->center_freq1; 312 313 /* assign the center channel used while 20M bw is selected */ 314 cch_by_bw[RTW_CHANNEL_WIDTH_20] = channel->hw_value; 315 316 switch (width) { 317 case NL80211_CHAN_WIDTH_20_NOHT: 318 case NL80211_CHAN_WIDTH_20: 319 bandwidth = RTW_CHANNEL_WIDTH_20; 320 primary_chan_idx = 0; 321 break; 322 case NL80211_CHAN_WIDTH_40: 323 bandwidth = RTW_CHANNEL_WIDTH_40; 324 if (primary_freq > center_freq) { 325 primary_chan_idx = 1; 326 center_chan -= 2; 327 } else { 328 primary_chan_idx = 2; 329 center_chan += 2; 330 } 331 break; 332 case NL80211_CHAN_WIDTH_80: 333 bandwidth = RTW_CHANNEL_WIDTH_80; 334 if (primary_freq > center_freq) { 335 if (primary_freq - center_freq == 10) { 336 primary_chan_idx = 1; 337 center_chan -= 2; 338 } else { 339 primary_chan_idx = 3; 340 center_chan -= 6; 341 } 342 /* assign the center channel used 343 * while 40M bw is selected 344 */ 345 cch_by_bw[RTW_CHANNEL_WIDTH_40] = center_chan + 4; 346 } else { 347 if (center_freq - primary_freq == 10) { 348 primary_chan_idx = 2; 349 center_chan += 2; 350 } else { 351 primary_chan_idx = 4; 352 center_chan += 6; 353 } 354 /* assign the center channel used 355 * while 40M bw is selected 356 */ 357 cch_by_bw[RTW_CHANNEL_WIDTH_40] = center_chan - 4; 358 } 359 break; 360 default: 361 center_chan = 0; 362 break; 363 } 364 365 chan_params->center_chan = center_chan; 366 chan_params->bandwidth = bandwidth; 367 chan_params->primary_chan_idx = primary_chan_idx; 368 369 /* assign the center channel used while current bw is selected */ 370 cch_by_bw[bandwidth] = center_chan; 371 372 for (i = bandwidth + 1; i <= RTW_MAX_CHANNEL_WIDTH; i++) 373 cch_by_bw[i] = 0; 374 } 375 376 void rtw_set_channel(struct rtw_dev *rtwdev) 377 { 378 struct ieee80211_hw *hw = rtwdev->hw; 379 struct rtw_hal *hal = &rtwdev->hal; 380 struct rtw_chip_info *chip = rtwdev->chip; 381 struct rtw_channel_params ch_param; 382 u8 center_chan, bandwidth, primary_chan_idx; 383 u8 i; 384 385 rtw_get_channel_params(&hw->conf.chandef, &ch_param); 386 if (WARN(ch_param.center_chan == 0, "Invalid channel\n")) 387 return; 388 389 center_chan = ch_param.center_chan; 390 bandwidth = ch_param.bandwidth; 391 primary_chan_idx = ch_param.primary_chan_idx; 392 393 hal->current_band_width = bandwidth; 394 hal->current_channel = center_chan; 395 hal->current_band_type = center_chan > 14 ? RTW_BAND_5G : RTW_BAND_2G; 396 397 for (i = RTW_CHANNEL_WIDTH_20; i <= RTW_MAX_CHANNEL_WIDTH; i++) 398 hal->cch_by_bw[i] = ch_param.cch_by_bw[i]; 399 400 chip->ops->set_channel(rtwdev, center_chan, bandwidth, primary_chan_idx); 401 402 if (hal->current_band_type == RTW_BAND_5G) { 403 rtw_coex_switchband_notify(rtwdev, COEX_SWITCH_TO_5G); 404 } else { 405 if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags)) 406 rtw_coex_switchband_notify(rtwdev, COEX_SWITCH_TO_24G); 407 else 408 rtw_coex_switchband_notify(rtwdev, COEX_SWITCH_TO_24G_NOFORSCAN); 409 } 410 411 rtw_phy_set_tx_power_level(rtwdev, center_chan); 412 } 413 414 static void rtw_vif_write_addr(struct rtw_dev *rtwdev, u32 start, u8 *addr) 415 { 416 int i; 417 418 for (i = 0; i < ETH_ALEN; i++) 419 rtw_write8(rtwdev, start + i, addr[i]); 420 } 421 422 void rtw_vif_port_config(struct rtw_dev *rtwdev, 423 struct rtw_vif *rtwvif, 424 u32 config) 425 { 426 u32 addr, mask; 427 428 if (config & PORT_SET_MAC_ADDR) { 429 addr = rtwvif->conf->mac_addr.addr; 430 rtw_vif_write_addr(rtwdev, addr, rtwvif->mac_addr); 431 } 432 if (config & PORT_SET_BSSID) { 433 addr = rtwvif->conf->bssid.addr; 434 rtw_vif_write_addr(rtwdev, addr, rtwvif->bssid); 435 } 436 if (config & PORT_SET_NET_TYPE) { 437 addr = rtwvif->conf->net_type.addr; 438 mask = rtwvif->conf->net_type.mask; 439 rtw_write32_mask(rtwdev, addr, mask, rtwvif->net_type); 440 } 441 if (config & PORT_SET_AID) { 442 addr = rtwvif->conf->aid.addr; 443 mask = rtwvif->conf->aid.mask; 444 rtw_write32_mask(rtwdev, addr, mask, rtwvif->aid); 445 } 446 if (config & PORT_SET_BCN_CTRL) { 447 addr = rtwvif->conf->bcn_ctrl.addr; 448 mask = rtwvif->conf->bcn_ctrl.mask; 449 rtw_write8_mask(rtwdev, addr, mask, rtwvif->bcn_ctrl); 450 } 451 } 452 453 static u8 hw_bw_cap_to_bitamp(u8 bw_cap) 454 { 455 u8 bw = 0; 456 457 switch (bw_cap) { 458 case EFUSE_HW_CAP_IGNORE: 459 case EFUSE_HW_CAP_SUPP_BW80: 460 bw |= BIT(RTW_CHANNEL_WIDTH_80); 461 /* fall through */ 462 case EFUSE_HW_CAP_SUPP_BW40: 463 bw |= BIT(RTW_CHANNEL_WIDTH_40); 464 /* fall through */ 465 default: 466 bw |= BIT(RTW_CHANNEL_WIDTH_20); 467 break; 468 } 469 470 return bw; 471 } 472 473 static void rtw_hw_config_rf_ant_num(struct rtw_dev *rtwdev, u8 hw_ant_num) 474 { 475 struct rtw_hal *hal = &rtwdev->hal; 476 477 if (hw_ant_num == EFUSE_HW_CAP_IGNORE || 478 hw_ant_num >= hal->rf_path_num) 479 return; 480 481 switch (hw_ant_num) { 482 case 1: 483 hal->rf_type = RF_1T1R; 484 hal->rf_path_num = 1; 485 hal->antenna_tx = BB_PATH_A; 486 hal->antenna_rx = BB_PATH_A; 487 break; 488 default: 489 WARN(1, "invalid hw configuration from efuse\n"); 490 break; 491 } 492 } 493 494 static u64 get_vht_ra_mask(struct ieee80211_sta *sta) 495 { 496 u64 ra_mask = 0; 497 u16 mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.rx_mcs_map); 498 u8 vht_mcs_cap; 499 int i, nss; 500 501 /* 4SS, every two bits for MCS7/8/9 */ 502 for (i = 0, nss = 12; i < 4; i++, mcs_map >>= 2, nss += 10) { 503 vht_mcs_cap = mcs_map & 0x3; 504 switch (vht_mcs_cap) { 505 case 2: /* MCS9 */ 506 ra_mask |= 0x3ffULL << nss; 507 break; 508 case 1: /* MCS8 */ 509 ra_mask |= 0x1ffULL << nss; 510 break; 511 case 0: /* MCS7 */ 512 ra_mask |= 0x0ffULL << nss; 513 break; 514 default: 515 break; 516 } 517 } 518 519 return ra_mask; 520 } 521 522 static u8 get_rate_id(u8 wireless_set, enum rtw_bandwidth bw_mode, u8 tx_num) 523 { 524 u8 rate_id = 0; 525 526 switch (wireless_set) { 527 case WIRELESS_CCK: 528 rate_id = RTW_RATEID_B_20M; 529 break; 530 case WIRELESS_OFDM: 531 rate_id = RTW_RATEID_G; 532 break; 533 case WIRELESS_CCK | WIRELESS_OFDM: 534 rate_id = RTW_RATEID_BG; 535 break; 536 case WIRELESS_OFDM | WIRELESS_HT: 537 if (tx_num == 1) 538 rate_id = RTW_RATEID_GN_N1SS; 539 else if (tx_num == 2) 540 rate_id = RTW_RATEID_GN_N2SS; 541 else if (tx_num == 3) 542 rate_id = RTW_RATEID_ARFR5_N_3SS; 543 break; 544 case WIRELESS_CCK | WIRELESS_OFDM | WIRELESS_HT: 545 if (bw_mode == RTW_CHANNEL_WIDTH_40) { 546 if (tx_num == 1) 547 rate_id = RTW_RATEID_BGN_40M_1SS; 548 else if (tx_num == 2) 549 rate_id = RTW_RATEID_BGN_40M_2SS; 550 else if (tx_num == 3) 551 rate_id = RTW_RATEID_ARFR5_N_3SS; 552 else if (tx_num == 4) 553 rate_id = RTW_RATEID_ARFR7_N_4SS; 554 } else { 555 if (tx_num == 1) 556 rate_id = RTW_RATEID_BGN_20M_1SS; 557 else if (tx_num == 2) 558 rate_id = RTW_RATEID_BGN_20M_2SS; 559 else if (tx_num == 3) 560 rate_id = RTW_RATEID_ARFR5_N_3SS; 561 else if (tx_num == 4) 562 rate_id = RTW_RATEID_ARFR7_N_4SS; 563 } 564 break; 565 case WIRELESS_OFDM | WIRELESS_VHT: 566 if (tx_num == 1) 567 rate_id = RTW_RATEID_ARFR1_AC_1SS; 568 else if (tx_num == 2) 569 rate_id = RTW_RATEID_ARFR0_AC_2SS; 570 else if (tx_num == 3) 571 rate_id = RTW_RATEID_ARFR4_AC_3SS; 572 else if (tx_num == 4) 573 rate_id = RTW_RATEID_ARFR6_AC_4SS; 574 break; 575 case WIRELESS_CCK | WIRELESS_OFDM | WIRELESS_VHT: 576 if (bw_mode >= RTW_CHANNEL_WIDTH_80) { 577 if (tx_num == 1) 578 rate_id = RTW_RATEID_ARFR1_AC_1SS; 579 else if (tx_num == 2) 580 rate_id = RTW_RATEID_ARFR0_AC_2SS; 581 else if (tx_num == 3) 582 rate_id = RTW_RATEID_ARFR4_AC_3SS; 583 else if (tx_num == 4) 584 rate_id = RTW_RATEID_ARFR6_AC_4SS; 585 } else { 586 if (tx_num == 1) 587 rate_id = RTW_RATEID_ARFR2_AC_2G_1SS; 588 else if (tx_num == 2) 589 rate_id = RTW_RATEID_ARFR3_AC_2G_2SS; 590 else if (tx_num == 3) 591 rate_id = RTW_RATEID_ARFR4_AC_3SS; 592 else if (tx_num == 4) 593 rate_id = RTW_RATEID_ARFR6_AC_4SS; 594 } 595 break; 596 default: 597 break; 598 } 599 600 return rate_id; 601 } 602 603 #define RA_MASK_CCK_RATES 0x0000f 604 #define RA_MASK_OFDM_RATES 0x00ff0 605 #define RA_MASK_HT_RATES_1SS (0xff000ULL << 0) 606 #define RA_MASK_HT_RATES_2SS (0xff000ULL << 8) 607 #define RA_MASK_HT_RATES_3SS (0xff000ULL << 16) 608 #define RA_MASK_HT_RATES (RA_MASK_HT_RATES_1SS | \ 609 RA_MASK_HT_RATES_2SS | \ 610 RA_MASK_HT_RATES_3SS) 611 #define RA_MASK_VHT_RATES_1SS (0x3ff000ULL << 0) 612 #define RA_MASK_VHT_RATES_2SS (0x3ff000ULL << 10) 613 #define RA_MASK_VHT_RATES_3SS (0x3ff000ULL << 20) 614 #define RA_MASK_VHT_RATES (RA_MASK_VHT_RATES_1SS | \ 615 RA_MASK_VHT_RATES_2SS | \ 616 RA_MASK_VHT_RATES_3SS) 617 #define RA_MASK_CCK_IN_HT 0x00005 618 #define RA_MASK_CCK_IN_VHT 0x00005 619 #define RA_MASK_OFDM_IN_VHT 0x00010 620 #define RA_MASK_OFDM_IN_HT_2G 0x00010 621 #define RA_MASK_OFDM_IN_HT_5G 0x00030 622 623 static u64 rtw_update_rate_mask(struct rtw_dev *rtwdev, 624 struct rtw_sta_info *si, 625 u64 ra_mask, bool is_vht_enable, 626 u8 wireless_set) 627 { 628 struct rtw_hal *hal = &rtwdev->hal; 629 const struct cfg80211_bitrate_mask *mask = si->mask; 630 u64 cfg_mask = GENMASK_ULL(63, 0); 631 u8 rssi_level, band; 632 633 if (wireless_set != WIRELESS_CCK) { 634 rssi_level = si->rssi_level; 635 if (rssi_level == 0) 636 ra_mask &= 0xffffffffffffffffULL; 637 else if (rssi_level == 1) 638 ra_mask &= 0xfffffffffffffff0ULL; 639 else if (rssi_level == 2) 640 ra_mask &= 0xffffffffffffefe0ULL; 641 else if (rssi_level == 3) 642 ra_mask &= 0xffffffffffffcfc0ULL; 643 else if (rssi_level == 4) 644 ra_mask &= 0xffffffffffff8f80ULL; 645 else if (rssi_level >= 5) 646 ra_mask &= 0xffffffffffff0f00ULL; 647 } 648 649 if (!si->use_cfg_mask) 650 return ra_mask; 651 652 band = hal->current_band_type; 653 if (band == RTW_BAND_2G) { 654 band = NL80211_BAND_2GHZ; 655 cfg_mask = mask->control[band].legacy; 656 } else if (band == RTW_BAND_5G) { 657 band = NL80211_BAND_5GHZ; 658 cfg_mask = u64_encode_bits(mask->control[band].legacy, 659 RA_MASK_OFDM_RATES); 660 } 661 662 if (!is_vht_enable) { 663 if (ra_mask & RA_MASK_HT_RATES_1SS) 664 cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[0], 665 RA_MASK_HT_RATES_1SS); 666 if (ra_mask & RA_MASK_HT_RATES_2SS) 667 cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[1], 668 RA_MASK_HT_RATES_2SS); 669 } else { 670 if (ra_mask & RA_MASK_VHT_RATES_1SS) 671 cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[0], 672 RA_MASK_VHT_RATES_1SS); 673 if (ra_mask & RA_MASK_VHT_RATES_2SS) 674 cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[1], 675 RA_MASK_VHT_RATES_2SS); 676 } 677 678 ra_mask &= cfg_mask; 679 680 return ra_mask; 681 } 682 683 void rtw_update_sta_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si) 684 { 685 struct ieee80211_sta *sta = si->sta; 686 struct rtw_efuse *efuse = &rtwdev->efuse; 687 struct rtw_hal *hal = &rtwdev->hal; 688 u8 wireless_set; 689 u8 bw_mode; 690 u8 rate_id; 691 u8 rf_type = RF_1T1R; 692 u8 stbc_en = 0; 693 u8 ldpc_en = 0; 694 u8 tx_num = 1; 695 u64 ra_mask = 0; 696 bool is_vht_enable = false; 697 bool is_support_sgi = false; 698 699 if (sta->vht_cap.vht_supported) { 700 is_vht_enable = true; 701 ra_mask |= get_vht_ra_mask(sta); 702 if (sta->vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK) 703 stbc_en = VHT_STBC_EN; 704 if (sta->vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC) 705 ldpc_en = VHT_LDPC_EN; 706 if (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80) 707 is_support_sgi = true; 708 } else if (sta->ht_cap.ht_supported) { 709 ra_mask |= (sta->ht_cap.mcs.rx_mask[1] << 20) | 710 (sta->ht_cap.mcs.rx_mask[0] << 12); 711 if (sta->ht_cap.cap & IEEE80211_HT_CAP_RX_STBC) 712 stbc_en = HT_STBC_EN; 713 if (sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING) 714 ldpc_en = HT_LDPC_EN; 715 if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20 || 716 sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40) 717 is_support_sgi = true; 718 } 719 720 if (efuse->hw_cap.nss == 1) 721 ra_mask &= RA_MASK_VHT_RATES_1SS | RA_MASK_HT_RATES_1SS; 722 723 if (hal->current_band_type == RTW_BAND_5G) { 724 ra_mask |= (u64)sta->supp_rates[NL80211_BAND_5GHZ] << 4; 725 if (sta->vht_cap.vht_supported) { 726 ra_mask &= RA_MASK_VHT_RATES | RA_MASK_OFDM_IN_VHT; 727 wireless_set = WIRELESS_OFDM | WIRELESS_VHT; 728 } else if (sta->ht_cap.ht_supported) { 729 ra_mask &= RA_MASK_HT_RATES | RA_MASK_OFDM_IN_HT_5G; 730 wireless_set = WIRELESS_OFDM | WIRELESS_HT; 731 } else { 732 wireless_set = WIRELESS_OFDM; 733 } 734 } else if (hal->current_band_type == RTW_BAND_2G) { 735 ra_mask |= sta->supp_rates[NL80211_BAND_2GHZ]; 736 if (sta->vht_cap.vht_supported) { 737 ra_mask &= RA_MASK_VHT_RATES | RA_MASK_CCK_IN_VHT | 738 RA_MASK_OFDM_IN_VHT; 739 wireless_set = WIRELESS_CCK | WIRELESS_OFDM | 740 WIRELESS_HT | WIRELESS_VHT; 741 } else if (sta->ht_cap.ht_supported) { 742 ra_mask &= RA_MASK_HT_RATES | RA_MASK_CCK_IN_HT | 743 RA_MASK_OFDM_IN_HT_2G; 744 wireless_set = WIRELESS_CCK | WIRELESS_OFDM | 745 WIRELESS_HT; 746 } else if (sta->supp_rates[0] <= 0xf) { 747 wireless_set = WIRELESS_CCK; 748 } else { 749 wireless_set = WIRELESS_CCK | WIRELESS_OFDM; 750 } 751 } else { 752 rtw_err(rtwdev, "Unknown band type\n"); 753 wireless_set = 0; 754 } 755 756 switch (sta->bandwidth) { 757 case IEEE80211_STA_RX_BW_80: 758 bw_mode = RTW_CHANNEL_WIDTH_80; 759 break; 760 case IEEE80211_STA_RX_BW_40: 761 bw_mode = RTW_CHANNEL_WIDTH_40; 762 break; 763 default: 764 bw_mode = RTW_CHANNEL_WIDTH_20; 765 break; 766 } 767 768 if (sta->vht_cap.vht_supported && ra_mask & 0xffc00000) { 769 tx_num = 2; 770 rf_type = RF_2T2R; 771 } else if (sta->ht_cap.ht_supported && ra_mask & 0xfff00000) { 772 tx_num = 2; 773 rf_type = RF_2T2R; 774 } 775 776 rate_id = get_rate_id(wireless_set, bw_mode, tx_num); 777 778 ra_mask = rtw_update_rate_mask(rtwdev, si, ra_mask, is_vht_enable, 779 wireless_set); 780 781 si->bw_mode = bw_mode; 782 si->stbc_en = stbc_en; 783 si->ldpc_en = ldpc_en; 784 si->rf_type = rf_type; 785 si->wireless_set = wireless_set; 786 si->sgi_enable = is_support_sgi; 787 si->vht_enable = is_vht_enable; 788 si->ra_mask = ra_mask; 789 si->rate_id = rate_id; 790 791 rtw_fw_send_ra_info(rtwdev, si); 792 } 793 794 static int rtw_wait_firmware_completion(struct rtw_dev *rtwdev) 795 { 796 struct rtw_chip_info *chip = rtwdev->chip; 797 struct rtw_fw_state *fw; 798 799 fw = &rtwdev->fw; 800 wait_for_completion(&fw->completion); 801 if (!fw->firmware) 802 return -EINVAL; 803 804 if (chip->wow_fw_name) { 805 fw = &rtwdev->wow_fw; 806 wait_for_completion(&fw->completion); 807 if (!fw->firmware) 808 return -EINVAL; 809 } 810 811 return 0; 812 } 813 814 static int rtw_power_on(struct rtw_dev *rtwdev) 815 { 816 struct rtw_chip_info *chip = rtwdev->chip; 817 struct rtw_fw_state *fw = &rtwdev->fw; 818 bool wifi_only; 819 int ret; 820 821 ret = rtw_hci_setup(rtwdev); 822 if (ret) { 823 rtw_err(rtwdev, "failed to setup hci\n"); 824 goto err; 825 } 826 827 /* power on MAC before firmware downloaded */ 828 ret = rtw_mac_power_on(rtwdev); 829 if (ret) { 830 rtw_err(rtwdev, "failed to power on mac\n"); 831 goto err; 832 } 833 834 ret = rtw_wait_firmware_completion(rtwdev); 835 if (ret) { 836 rtw_err(rtwdev, "failed to wait firmware completion\n"); 837 goto err_off; 838 } 839 840 ret = rtw_download_firmware(rtwdev, fw); 841 if (ret) { 842 rtw_err(rtwdev, "failed to download firmware\n"); 843 goto err_off; 844 } 845 846 /* config mac after firmware downloaded */ 847 ret = rtw_mac_init(rtwdev); 848 if (ret) { 849 rtw_err(rtwdev, "failed to configure mac\n"); 850 goto err_off; 851 } 852 853 chip->ops->phy_set_param(rtwdev); 854 855 ret = rtw_hci_start(rtwdev); 856 if (ret) { 857 rtw_err(rtwdev, "failed to start hci\n"); 858 goto err_off; 859 } 860 861 /* send H2C after HCI has started */ 862 rtw_fw_send_general_info(rtwdev); 863 rtw_fw_send_phydm_info(rtwdev); 864 865 wifi_only = !rtwdev->efuse.btcoex; 866 rtw_coex_power_on_setting(rtwdev); 867 rtw_coex_init_hw_config(rtwdev, wifi_only); 868 869 return 0; 870 871 err_off: 872 rtw_mac_power_off(rtwdev); 873 874 err: 875 return ret; 876 } 877 878 int rtw_core_start(struct rtw_dev *rtwdev) 879 { 880 int ret; 881 882 ret = rtw_power_on(rtwdev); 883 if (ret) 884 return ret; 885 886 rtw_sec_enable_sec_engine(rtwdev); 887 888 /* rcr reset after powered on */ 889 rtw_write32(rtwdev, REG_RCR, rtwdev->hal.rcr); 890 891 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->watch_dog_work, 892 RTW_WATCH_DOG_DELAY_TIME); 893 894 set_bit(RTW_FLAG_RUNNING, rtwdev->flags); 895 896 return 0; 897 } 898 899 static void rtw_power_off(struct rtw_dev *rtwdev) 900 { 901 rtw_hci_stop(rtwdev); 902 rtw_mac_power_off(rtwdev); 903 } 904 905 void rtw_core_stop(struct rtw_dev *rtwdev) 906 { 907 struct rtw_coex *coex = &rtwdev->coex; 908 909 clear_bit(RTW_FLAG_RUNNING, rtwdev->flags); 910 clear_bit(RTW_FLAG_FW_RUNNING, rtwdev->flags); 911 912 cancel_delayed_work_sync(&rtwdev->watch_dog_work); 913 cancel_delayed_work_sync(&coex->bt_relink_work); 914 cancel_delayed_work_sync(&coex->bt_reenable_work); 915 cancel_delayed_work_sync(&coex->defreeze_work); 916 917 rtw_power_off(rtwdev); 918 } 919 920 static void rtw_init_ht_cap(struct rtw_dev *rtwdev, 921 struct ieee80211_sta_ht_cap *ht_cap) 922 { 923 struct rtw_efuse *efuse = &rtwdev->efuse; 924 925 ht_cap->ht_supported = true; 926 ht_cap->cap = 0; 927 ht_cap->cap |= IEEE80211_HT_CAP_SGI_20 | 928 IEEE80211_HT_CAP_MAX_AMSDU | 929 IEEE80211_HT_CAP_LDPC_CODING | 930 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT); 931 if (efuse->hw_cap.bw & BIT(RTW_CHANNEL_WIDTH_40)) 932 ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 | 933 IEEE80211_HT_CAP_DSSSCCK40 | 934 IEEE80211_HT_CAP_SGI_40; 935 ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K; 936 ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16; 937 ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED; 938 if (efuse->hw_cap.nss > 1) { 939 ht_cap->mcs.rx_mask[0] = 0xFF; 940 ht_cap->mcs.rx_mask[1] = 0xFF; 941 ht_cap->mcs.rx_mask[4] = 0x01; 942 ht_cap->mcs.rx_highest = cpu_to_le16(300); 943 } else { 944 ht_cap->mcs.rx_mask[0] = 0xFF; 945 ht_cap->mcs.rx_mask[1] = 0x00; 946 ht_cap->mcs.rx_mask[4] = 0x01; 947 ht_cap->mcs.rx_highest = cpu_to_le16(150); 948 } 949 } 950 951 static void rtw_init_vht_cap(struct rtw_dev *rtwdev, 952 struct ieee80211_sta_vht_cap *vht_cap) 953 { 954 struct rtw_efuse *efuse = &rtwdev->efuse; 955 u16 mcs_map; 956 __le16 highest; 957 958 if (efuse->hw_cap.ptcl != EFUSE_HW_CAP_IGNORE && 959 efuse->hw_cap.ptcl != EFUSE_HW_CAP_PTCL_VHT) 960 return; 961 962 vht_cap->vht_supported = true; 963 vht_cap->cap = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 | 964 IEEE80211_VHT_CAP_RXLDPC | 965 IEEE80211_VHT_CAP_SHORT_GI_80 | 966 IEEE80211_VHT_CAP_TXSTBC | 967 IEEE80211_VHT_CAP_RXSTBC_1 | 968 IEEE80211_VHT_CAP_HTC_VHT | 969 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK | 970 0; 971 972 vht_cap->cap |= IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE | 973 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE; 974 vht_cap->cap |= (rtwdev->hal.bfee_sts_cap << 975 IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT); 976 977 mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 | 978 IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 | 979 IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 | 980 IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 | 981 IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 | 982 IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 | 983 IEEE80211_VHT_MCS_NOT_SUPPORTED << 14; 984 if (efuse->hw_cap.nss > 1) { 985 highest = cpu_to_le16(780); 986 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << 2; 987 } else { 988 highest = cpu_to_le16(390); 989 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << 2; 990 } 991 992 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map); 993 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map); 994 vht_cap->vht_mcs.rx_highest = highest; 995 vht_cap->vht_mcs.tx_highest = highest; 996 } 997 998 static void rtw_set_supported_band(struct ieee80211_hw *hw, 999 struct rtw_chip_info *chip) 1000 { 1001 struct rtw_dev *rtwdev = hw->priv; 1002 struct ieee80211_supported_band *sband; 1003 1004 if (chip->band & RTW_BAND_2G) { 1005 sband = kmemdup(&rtw_band_2ghz, sizeof(*sband), GFP_KERNEL); 1006 if (!sband) 1007 goto err_out; 1008 if (chip->ht_supported) 1009 rtw_init_ht_cap(rtwdev, &sband->ht_cap); 1010 hw->wiphy->bands[NL80211_BAND_2GHZ] = sband; 1011 } 1012 1013 if (chip->band & RTW_BAND_5G) { 1014 sband = kmemdup(&rtw_band_5ghz, sizeof(*sband), GFP_KERNEL); 1015 if (!sband) 1016 goto err_out; 1017 if (chip->ht_supported) 1018 rtw_init_ht_cap(rtwdev, &sband->ht_cap); 1019 if (chip->vht_supported) 1020 rtw_init_vht_cap(rtwdev, &sband->vht_cap); 1021 hw->wiphy->bands[NL80211_BAND_5GHZ] = sband; 1022 } 1023 1024 return; 1025 1026 err_out: 1027 rtw_err(rtwdev, "failed to set supported band\n"); 1028 kfree(sband); 1029 } 1030 1031 static void rtw_unset_supported_band(struct ieee80211_hw *hw, 1032 struct rtw_chip_info *chip) 1033 { 1034 kfree(hw->wiphy->bands[NL80211_BAND_2GHZ]); 1035 kfree(hw->wiphy->bands[NL80211_BAND_5GHZ]); 1036 } 1037 1038 static void rtw_load_firmware_cb(const struct firmware *firmware, void *context) 1039 { 1040 struct rtw_fw_state *fw = context; 1041 struct rtw_dev *rtwdev = fw->rtwdev; 1042 const struct rtw_fw_hdr *fw_hdr; 1043 1044 if (!firmware || !firmware->data) { 1045 rtw_err(rtwdev, "failed to request firmware\n"); 1046 complete_all(&fw->completion); 1047 return; 1048 } 1049 1050 fw_hdr = (const struct rtw_fw_hdr *)firmware->data; 1051 fw->h2c_version = le16_to_cpu(fw_hdr->h2c_fmt_ver); 1052 fw->version = le16_to_cpu(fw_hdr->version); 1053 fw->sub_version = fw_hdr->subversion; 1054 fw->sub_index = fw_hdr->subindex; 1055 1056 fw->firmware = firmware; 1057 complete_all(&fw->completion); 1058 1059 rtw_info(rtwdev, "Firmware version %u.%u.%u, H2C version %u\n", 1060 fw->version, fw->sub_version, fw->sub_index, fw->h2c_version); 1061 } 1062 1063 static int rtw_load_firmware(struct rtw_dev *rtwdev, enum rtw_fw_type type) 1064 { 1065 const char *fw_name; 1066 struct rtw_fw_state *fw; 1067 int ret; 1068 1069 switch (type) { 1070 case RTW_WOWLAN_FW: 1071 fw = &rtwdev->wow_fw; 1072 fw_name = rtwdev->chip->wow_fw_name; 1073 break; 1074 1075 case RTW_NORMAL_FW: 1076 fw = &rtwdev->fw; 1077 fw_name = rtwdev->chip->fw_name; 1078 break; 1079 1080 default: 1081 rtw_warn(rtwdev, "unsupported firmware type\n"); 1082 return -ENOENT; 1083 } 1084 1085 fw->rtwdev = rtwdev; 1086 init_completion(&fw->completion); 1087 1088 ret = request_firmware_nowait(THIS_MODULE, true, fw_name, rtwdev->dev, 1089 GFP_KERNEL, fw, rtw_load_firmware_cb); 1090 if (ret) { 1091 rtw_err(rtwdev, "failed to async firmware request\n"); 1092 return ret; 1093 } 1094 1095 return 0; 1096 } 1097 1098 static int rtw_chip_parameter_setup(struct rtw_dev *rtwdev) 1099 { 1100 struct rtw_chip_info *chip = rtwdev->chip; 1101 struct rtw_hal *hal = &rtwdev->hal; 1102 struct rtw_efuse *efuse = &rtwdev->efuse; 1103 int ret = 0; 1104 1105 switch (rtw_hci_type(rtwdev)) { 1106 case RTW_HCI_TYPE_PCIE: 1107 rtwdev->hci.rpwm_addr = 0x03d9; 1108 rtwdev->hci.cpwm_addr = 0x03da; 1109 break; 1110 default: 1111 rtw_err(rtwdev, "unsupported hci type\n"); 1112 return -EINVAL; 1113 } 1114 1115 hal->chip_version = rtw_read32(rtwdev, REG_SYS_CFG1); 1116 hal->fab_version = BIT_GET_VENDOR_ID(hal->chip_version) >> 2; 1117 hal->cut_version = BIT_GET_CHIP_VER(hal->chip_version); 1118 hal->mp_chip = (hal->chip_version & BIT_RTL_ID) ? 0 : 1; 1119 if (hal->chip_version & BIT_RF_TYPE_ID) { 1120 hal->rf_type = RF_2T2R; 1121 hal->rf_path_num = 2; 1122 hal->antenna_tx = BB_PATH_AB; 1123 hal->antenna_rx = BB_PATH_AB; 1124 } else { 1125 hal->rf_type = RF_1T1R; 1126 hal->rf_path_num = 1; 1127 hal->antenna_tx = BB_PATH_A; 1128 hal->antenna_rx = BB_PATH_A; 1129 } 1130 1131 if (hal->fab_version == 2) 1132 hal->fab_version = 1; 1133 else if (hal->fab_version == 1) 1134 hal->fab_version = 2; 1135 1136 efuse->physical_size = chip->phy_efuse_size; 1137 efuse->logical_size = chip->log_efuse_size; 1138 efuse->protect_size = chip->ptct_efuse_size; 1139 1140 /* default use ack */ 1141 rtwdev->hal.rcr |= BIT_VHT_DACK; 1142 1143 hal->bfee_sts_cap = 3; 1144 1145 return ret; 1146 } 1147 1148 static int rtw_chip_efuse_enable(struct rtw_dev *rtwdev) 1149 { 1150 struct rtw_fw_state *fw = &rtwdev->fw; 1151 int ret; 1152 1153 ret = rtw_hci_setup(rtwdev); 1154 if (ret) { 1155 rtw_err(rtwdev, "failed to setup hci\n"); 1156 goto err; 1157 } 1158 1159 ret = rtw_mac_power_on(rtwdev); 1160 if (ret) { 1161 rtw_err(rtwdev, "failed to power on mac\n"); 1162 goto err; 1163 } 1164 1165 rtw_write8(rtwdev, REG_C2HEVT, C2H_HW_FEATURE_DUMP); 1166 1167 wait_for_completion(&fw->completion); 1168 if (!fw->firmware) { 1169 ret = -EINVAL; 1170 rtw_err(rtwdev, "failed to load firmware\n"); 1171 goto err; 1172 } 1173 1174 ret = rtw_download_firmware(rtwdev, fw); 1175 if (ret) { 1176 rtw_err(rtwdev, "failed to download firmware\n"); 1177 goto err_off; 1178 } 1179 1180 return 0; 1181 1182 err_off: 1183 rtw_mac_power_off(rtwdev); 1184 1185 err: 1186 return ret; 1187 } 1188 1189 static int rtw_dump_hw_feature(struct rtw_dev *rtwdev) 1190 { 1191 struct rtw_efuse *efuse = &rtwdev->efuse; 1192 u8 hw_feature[HW_FEATURE_LEN]; 1193 u8 id; 1194 u8 bw; 1195 int i; 1196 1197 id = rtw_read8(rtwdev, REG_C2HEVT); 1198 if (id != C2H_HW_FEATURE_REPORT) { 1199 rtw_err(rtwdev, "failed to read hw feature report\n"); 1200 return -EBUSY; 1201 } 1202 1203 for (i = 0; i < HW_FEATURE_LEN; i++) 1204 hw_feature[i] = rtw_read8(rtwdev, REG_C2HEVT + 2 + i); 1205 1206 rtw_write8(rtwdev, REG_C2HEVT, 0); 1207 1208 bw = GET_EFUSE_HW_CAP_BW(hw_feature); 1209 efuse->hw_cap.bw = hw_bw_cap_to_bitamp(bw); 1210 efuse->hw_cap.hci = GET_EFUSE_HW_CAP_HCI(hw_feature); 1211 efuse->hw_cap.nss = GET_EFUSE_HW_CAP_NSS(hw_feature); 1212 efuse->hw_cap.ptcl = GET_EFUSE_HW_CAP_PTCL(hw_feature); 1213 efuse->hw_cap.ant_num = GET_EFUSE_HW_CAP_ANT_NUM(hw_feature); 1214 1215 rtw_hw_config_rf_ant_num(rtwdev, efuse->hw_cap.ant_num); 1216 1217 if (efuse->hw_cap.nss == EFUSE_HW_CAP_IGNORE || 1218 efuse->hw_cap.nss > rtwdev->hal.rf_path_num) 1219 efuse->hw_cap.nss = rtwdev->hal.rf_path_num; 1220 1221 rtw_dbg(rtwdev, RTW_DBG_EFUSE, 1222 "hw cap: hci=0x%02x, bw=0x%02x, ptcl=0x%02x, ant_num=%d, nss=%d\n", 1223 efuse->hw_cap.hci, efuse->hw_cap.bw, efuse->hw_cap.ptcl, 1224 efuse->hw_cap.ant_num, efuse->hw_cap.nss); 1225 1226 return 0; 1227 } 1228 1229 static void rtw_chip_efuse_disable(struct rtw_dev *rtwdev) 1230 { 1231 rtw_hci_stop(rtwdev); 1232 rtw_mac_power_off(rtwdev); 1233 } 1234 1235 static int rtw_chip_efuse_info_setup(struct rtw_dev *rtwdev) 1236 { 1237 struct rtw_efuse *efuse = &rtwdev->efuse; 1238 int ret; 1239 1240 mutex_lock(&rtwdev->mutex); 1241 1242 /* power on mac to read efuse */ 1243 ret = rtw_chip_efuse_enable(rtwdev); 1244 if (ret) 1245 goto out_unlock; 1246 1247 ret = rtw_parse_efuse_map(rtwdev); 1248 if (ret) 1249 goto out_disable; 1250 1251 ret = rtw_dump_hw_feature(rtwdev); 1252 if (ret) 1253 goto out_disable; 1254 1255 ret = rtw_check_supported_rfe(rtwdev); 1256 if (ret) 1257 goto out_disable; 1258 1259 if (efuse->crystal_cap == 0xff) 1260 efuse->crystal_cap = 0; 1261 if (efuse->pa_type_2g == 0xff) 1262 efuse->pa_type_2g = 0; 1263 if (efuse->pa_type_5g == 0xff) 1264 efuse->pa_type_5g = 0; 1265 if (efuse->lna_type_2g == 0xff) 1266 efuse->lna_type_2g = 0; 1267 if (efuse->lna_type_5g == 0xff) 1268 efuse->lna_type_5g = 0; 1269 if (efuse->channel_plan == 0xff) 1270 efuse->channel_plan = 0x7f; 1271 if (efuse->rf_board_option == 0xff) 1272 efuse->rf_board_option = 0; 1273 if (efuse->bt_setting & BIT(0)) 1274 efuse->share_ant = true; 1275 if (efuse->regd == 0xff) 1276 efuse->regd = 0; 1277 1278 efuse->btcoex = (efuse->rf_board_option & 0xe0) == 0x20; 1279 efuse->ext_pa_2g = efuse->pa_type_2g & BIT(4) ? 1 : 0; 1280 efuse->ext_lna_2g = efuse->lna_type_2g & BIT(3) ? 1 : 0; 1281 efuse->ext_pa_5g = efuse->pa_type_5g & BIT(0) ? 1 : 0; 1282 efuse->ext_lna_2g = efuse->lna_type_5g & BIT(3) ? 1 : 0; 1283 1284 out_disable: 1285 rtw_chip_efuse_disable(rtwdev); 1286 1287 out_unlock: 1288 mutex_unlock(&rtwdev->mutex); 1289 return ret; 1290 } 1291 1292 static int rtw_chip_board_info_setup(struct rtw_dev *rtwdev) 1293 { 1294 struct rtw_hal *hal = &rtwdev->hal; 1295 const struct rtw_rfe_def *rfe_def = rtw_get_rfe_def(rtwdev); 1296 1297 if (!rfe_def) 1298 return -ENODEV; 1299 1300 rtw_phy_setup_phy_cond(rtwdev, 0); 1301 1302 rtw_phy_init_tx_power(rtwdev); 1303 rtw_load_table(rtwdev, rfe_def->phy_pg_tbl); 1304 rtw_load_table(rtwdev, rfe_def->txpwr_lmt_tbl); 1305 rtw_phy_tx_power_by_rate_config(hal); 1306 rtw_phy_tx_power_limit_config(hal); 1307 1308 return 0; 1309 } 1310 1311 int rtw_chip_info_setup(struct rtw_dev *rtwdev) 1312 { 1313 int ret; 1314 1315 ret = rtw_chip_parameter_setup(rtwdev); 1316 if (ret) { 1317 rtw_err(rtwdev, "failed to setup chip parameters\n"); 1318 goto err_out; 1319 } 1320 1321 ret = rtw_chip_efuse_info_setup(rtwdev); 1322 if (ret) { 1323 rtw_err(rtwdev, "failed to setup chip efuse info\n"); 1324 goto err_out; 1325 } 1326 1327 ret = rtw_chip_board_info_setup(rtwdev); 1328 if (ret) { 1329 rtw_err(rtwdev, "failed to setup chip board info\n"); 1330 goto err_out; 1331 } 1332 1333 return 0; 1334 1335 err_out: 1336 return ret; 1337 } 1338 EXPORT_SYMBOL(rtw_chip_info_setup); 1339 1340 static void rtw_stats_init(struct rtw_dev *rtwdev) 1341 { 1342 struct rtw_traffic_stats *stats = &rtwdev->stats; 1343 struct rtw_dm_info *dm_info = &rtwdev->dm_info; 1344 int i; 1345 1346 ewma_tp_init(&stats->tx_ewma_tp); 1347 ewma_tp_init(&stats->rx_ewma_tp); 1348 1349 for (i = 0; i < RTW_EVM_NUM; i++) 1350 ewma_evm_init(&dm_info->ewma_evm[i]); 1351 for (i = 0; i < RTW_SNR_NUM; i++) 1352 ewma_snr_init(&dm_info->ewma_snr[i]); 1353 } 1354 1355 int rtw_core_init(struct rtw_dev *rtwdev) 1356 { 1357 struct rtw_chip_info *chip = rtwdev->chip; 1358 struct rtw_coex *coex = &rtwdev->coex; 1359 int ret; 1360 1361 INIT_LIST_HEAD(&rtwdev->rsvd_page_list); 1362 INIT_LIST_HEAD(&rtwdev->txqs); 1363 1364 timer_setup(&rtwdev->tx_report.purge_timer, 1365 rtw_tx_report_purge_timer, 0); 1366 tasklet_init(&rtwdev->tx_tasklet, rtw_tx_tasklet, 1367 (unsigned long)rtwdev); 1368 1369 INIT_DELAYED_WORK(&rtwdev->watch_dog_work, rtw_watch_dog_work); 1370 INIT_DELAYED_WORK(&coex->bt_relink_work, rtw_coex_bt_relink_work); 1371 INIT_DELAYED_WORK(&coex->bt_reenable_work, rtw_coex_bt_reenable_work); 1372 INIT_DELAYED_WORK(&coex->defreeze_work, rtw_coex_defreeze_work); 1373 INIT_WORK(&rtwdev->c2h_work, rtw_c2h_work); 1374 INIT_WORK(&rtwdev->ba_work, rtw_txq_ba_work); 1375 skb_queue_head_init(&rtwdev->c2h_queue); 1376 skb_queue_head_init(&rtwdev->coex.queue); 1377 skb_queue_head_init(&rtwdev->tx_report.queue); 1378 1379 spin_lock_init(&rtwdev->rf_lock); 1380 spin_lock_init(&rtwdev->h2c.lock); 1381 spin_lock_init(&rtwdev->txq_lock); 1382 spin_lock_init(&rtwdev->tx_report.q_lock); 1383 1384 mutex_init(&rtwdev->mutex); 1385 mutex_init(&rtwdev->coex.mutex); 1386 mutex_init(&rtwdev->hal.tx_power_mutex); 1387 1388 init_waitqueue_head(&rtwdev->coex.wait); 1389 1390 rtwdev->sec.total_cam_num = 32; 1391 rtwdev->hal.current_channel = 1; 1392 set_bit(RTW_BC_MC_MACID, rtwdev->mac_id_map); 1393 if (!(BIT(rtw_fw_lps_deep_mode) & chip->lps_deep_mode_supported)) 1394 rtwdev->lps_conf.deep_mode = LPS_DEEP_MODE_NONE; 1395 else 1396 rtwdev->lps_conf.deep_mode = rtw_fw_lps_deep_mode; 1397 1398 mutex_lock(&rtwdev->mutex); 1399 rtw_add_rsvd_page(rtwdev, RSVD_BEACON, false); 1400 mutex_unlock(&rtwdev->mutex); 1401 1402 rtw_stats_init(rtwdev); 1403 1404 /* default rx filter setting */ 1405 rtwdev->hal.rcr = BIT_APP_FCS | BIT_APP_MIC | BIT_APP_ICV | 1406 BIT_HTC_LOC_CTRL | BIT_APP_PHYSTS | 1407 BIT_AB | BIT_AM | BIT_APM; 1408 1409 ret = rtw_load_firmware(rtwdev, RTW_NORMAL_FW); 1410 if (ret) { 1411 rtw_warn(rtwdev, "no firmware loaded\n"); 1412 return ret; 1413 } 1414 1415 if (chip->wow_fw_name) { 1416 ret = rtw_load_firmware(rtwdev, RTW_WOWLAN_FW); 1417 if (ret) { 1418 rtw_warn(rtwdev, "no wow firmware loaded\n"); 1419 return ret; 1420 } 1421 } 1422 return 0; 1423 } 1424 EXPORT_SYMBOL(rtw_core_init); 1425 1426 void rtw_core_deinit(struct rtw_dev *rtwdev) 1427 { 1428 struct rtw_fw_state *fw = &rtwdev->fw; 1429 struct rtw_fw_state *wow_fw = &rtwdev->wow_fw; 1430 struct rtw_rsvd_page *rsvd_pkt, *tmp; 1431 unsigned long flags; 1432 1433 if (fw->firmware) 1434 release_firmware(fw->firmware); 1435 1436 if (wow_fw->firmware) 1437 release_firmware(wow_fw->firmware); 1438 1439 tasklet_kill(&rtwdev->tx_tasklet); 1440 spin_lock_irqsave(&rtwdev->tx_report.q_lock, flags); 1441 skb_queue_purge(&rtwdev->tx_report.queue); 1442 spin_unlock_irqrestore(&rtwdev->tx_report.q_lock, flags); 1443 1444 list_for_each_entry_safe(rsvd_pkt, tmp, &rtwdev->rsvd_page_list, list) { 1445 list_del(&rsvd_pkt->list); 1446 kfree(rsvd_pkt); 1447 } 1448 1449 mutex_destroy(&rtwdev->mutex); 1450 mutex_destroy(&rtwdev->coex.mutex); 1451 mutex_destroy(&rtwdev->hal.tx_power_mutex); 1452 } 1453 EXPORT_SYMBOL(rtw_core_deinit); 1454 1455 int rtw_register_hw(struct rtw_dev *rtwdev, struct ieee80211_hw *hw) 1456 { 1457 int max_tx_headroom = 0; 1458 int ret; 1459 1460 /* TODO: USB & SDIO may need extra room? */ 1461 max_tx_headroom = rtwdev->chip->tx_pkt_desc_sz; 1462 1463 hw->extra_tx_headroom = max_tx_headroom; 1464 hw->queues = IEEE80211_NUM_ACS; 1465 hw->txq_data_size = sizeof(struct rtw_txq); 1466 hw->sta_data_size = sizeof(struct rtw_sta_info); 1467 hw->vif_data_size = sizeof(struct rtw_vif); 1468 1469 ieee80211_hw_set(hw, SIGNAL_DBM); 1470 ieee80211_hw_set(hw, RX_INCLUDES_FCS); 1471 ieee80211_hw_set(hw, AMPDU_AGGREGATION); 1472 ieee80211_hw_set(hw, MFP_CAPABLE); 1473 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS); 1474 ieee80211_hw_set(hw, SUPPORTS_PS); 1475 ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS); 1476 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT); 1477 ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU); 1478 ieee80211_hw_set(hw, HAS_RATE_CONTROL); 1479 ieee80211_hw_set(hw, TX_AMSDU); 1480 1481 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) | 1482 BIT(NL80211_IFTYPE_AP) | 1483 BIT(NL80211_IFTYPE_ADHOC) | 1484 BIT(NL80211_IFTYPE_MESH_POINT); 1485 1486 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS | 1487 WIPHY_FLAG_TDLS_EXTERNAL_SETUP; 1488 1489 hw->wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR; 1490 1491 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CAN_REPLACE_PTK0); 1492 1493 #ifdef CONFIG_PM 1494 hw->wiphy->wowlan = rtwdev->chip->wowlan_stub; 1495 hw->wiphy->max_sched_scan_ssids = rtwdev->chip->max_sched_scan_ssids; 1496 #endif 1497 rtw_set_supported_band(hw, rtwdev->chip); 1498 SET_IEEE80211_PERM_ADDR(hw, rtwdev->efuse.addr); 1499 1500 rtw_regd_init(rtwdev, rtw_regd_notifier); 1501 1502 ret = ieee80211_register_hw(hw); 1503 if (ret) { 1504 rtw_err(rtwdev, "failed to register hw\n"); 1505 return ret; 1506 } 1507 1508 if (regulatory_hint(hw->wiphy, rtwdev->regd.alpha2)) 1509 rtw_err(rtwdev, "regulatory_hint fail\n"); 1510 1511 rtw_debugfs_init(rtwdev); 1512 1513 rtwdev->bf_info.bfer_mu_cnt = 0; 1514 rtwdev->bf_info.bfer_su_cnt = 0; 1515 1516 return 0; 1517 } 1518 EXPORT_SYMBOL(rtw_register_hw); 1519 1520 void rtw_unregister_hw(struct rtw_dev *rtwdev, struct ieee80211_hw *hw) 1521 { 1522 struct rtw_chip_info *chip = rtwdev->chip; 1523 1524 ieee80211_unregister_hw(hw); 1525 rtw_unset_supported_band(hw, chip); 1526 } 1527 EXPORT_SYMBOL(rtw_unregister_hw); 1528 1529 MODULE_AUTHOR("Realtek Corporation"); 1530 MODULE_DESCRIPTION("Realtek 802.11ac wireless core module"); 1531 MODULE_LICENSE("Dual BSD/GPL"); 1532