1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright(c) 2018-2019 Realtek Corporation 3 */ 4 5 #include <linux/iopoll.h> 6 7 #include "main.h" 8 #include "coex.h" 9 #include "fw.h" 10 #include "tx.h" 11 #include "reg.h" 12 #include "sec.h" 13 #include "debug.h" 14 #include "util.h" 15 #include "wow.h" 16 17 static void rtw_fw_c2h_cmd_handle_ext(struct rtw_dev *rtwdev, 18 struct sk_buff *skb) 19 { 20 struct rtw_c2h_cmd *c2h; 21 u8 sub_cmd_id; 22 23 c2h = get_c2h_from_skb(skb); 24 sub_cmd_id = c2h->payload[0]; 25 26 switch (sub_cmd_id) { 27 case C2H_CCX_RPT: 28 rtw_tx_report_handle(rtwdev, skb, C2H_CCX_RPT); 29 break; 30 default: 31 break; 32 } 33 } 34 35 static u16 get_max_amsdu_len(u32 bit_rate) 36 { 37 /* lower than ofdm, do not aggregate */ 38 if (bit_rate < 550) 39 return 1; 40 41 /* lower than 20M 2ss mcs8, make it small */ 42 if (bit_rate < 1800) 43 return 1200; 44 45 /* lower than 40M 2ss mcs9, make it medium */ 46 if (bit_rate < 4000) 47 return 2600; 48 49 /* not yet 80M 2ss mcs8/9, make it twice regular packet size */ 50 if (bit_rate < 7000) 51 return 3500; 52 53 /* unlimited */ 54 return 0; 55 } 56 57 struct rtw_fw_iter_ra_data { 58 struct rtw_dev *rtwdev; 59 u8 *payload; 60 }; 61 62 static void rtw_fw_ra_report_iter(void *data, struct ieee80211_sta *sta) 63 { 64 struct rtw_fw_iter_ra_data *ra_data = data; 65 struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv; 66 u8 mac_id, rate, sgi, bw; 67 u8 mcs, nss; 68 u32 bit_rate; 69 70 mac_id = GET_RA_REPORT_MACID(ra_data->payload); 71 if (si->mac_id != mac_id) 72 return; 73 74 si->ra_report.txrate.flags = 0; 75 76 rate = GET_RA_REPORT_RATE(ra_data->payload); 77 sgi = GET_RA_REPORT_SGI(ra_data->payload); 78 bw = GET_RA_REPORT_BW(ra_data->payload); 79 80 if (rate < DESC_RATEMCS0) { 81 si->ra_report.txrate.legacy = rtw_desc_to_bitrate(rate); 82 goto legacy; 83 } 84 85 rtw_desc_to_mcsrate(rate, &mcs, &nss); 86 if (rate >= DESC_RATEVHT1SS_MCS0) 87 si->ra_report.txrate.flags |= RATE_INFO_FLAGS_VHT_MCS; 88 else if (rate >= DESC_RATEMCS0) 89 si->ra_report.txrate.flags |= RATE_INFO_FLAGS_MCS; 90 91 if (rate >= DESC_RATEMCS0) { 92 si->ra_report.txrate.mcs = mcs; 93 si->ra_report.txrate.nss = nss; 94 } 95 96 if (sgi) 97 si->ra_report.txrate.flags |= RATE_INFO_FLAGS_SHORT_GI; 98 99 if (bw == RTW_CHANNEL_WIDTH_80) 100 si->ra_report.txrate.bw = RATE_INFO_BW_80; 101 else if (bw == RTW_CHANNEL_WIDTH_40) 102 si->ra_report.txrate.bw = RATE_INFO_BW_40; 103 else 104 si->ra_report.txrate.bw = RATE_INFO_BW_20; 105 106 legacy: 107 bit_rate = cfg80211_calculate_bitrate(&si->ra_report.txrate); 108 109 si->ra_report.desc_rate = rate; 110 si->ra_report.bit_rate = bit_rate; 111 112 sta->max_rc_amsdu_len = get_max_amsdu_len(bit_rate); 113 } 114 115 static void rtw_fw_ra_report_handle(struct rtw_dev *rtwdev, u8 *payload, 116 u8 length) 117 { 118 struct rtw_fw_iter_ra_data ra_data; 119 120 if (WARN(length < 7, "invalid ra report c2h length\n")) 121 return; 122 123 rtwdev->dm_info.tx_rate = GET_RA_REPORT_RATE(payload); 124 ra_data.rtwdev = rtwdev; 125 ra_data.payload = payload; 126 rtw_iterate_stas_atomic(rtwdev, rtw_fw_ra_report_iter, &ra_data); 127 } 128 129 void rtw_fw_c2h_cmd_handle(struct rtw_dev *rtwdev, struct sk_buff *skb) 130 { 131 struct rtw_c2h_cmd *c2h; 132 u32 pkt_offset; 133 u8 len; 134 135 pkt_offset = *((u32 *)skb->cb); 136 c2h = (struct rtw_c2h_cmd *)(skb->data + pkt_offset); 137 len = skb->len - pkt_offset - 2; 138 139 mutex_lock(&rtwdev->mutex); 140 141 if (!test_bit(RTW_FLAG_RUNNING, rtwdev->flags)) 142 goto unlock; 143 144 switch (c2h->id) { 145 case C2H_CCX_TX_RPT: 146 rtw_tx_report_handle(rtwdev, skb, C2H_CCX_TX_RPT); 147 break; 148 case C2H_BT_INFO: 149 rtw_coex_bt_info_notify(rtwdev, c2h->payload, len); 150 break; 151 case C2H_WLAN_INFO: 152 rtw_coex_wl_fwdbginfo_notify(rtwdev, c2h->payload, len); 153 break; 154 case C2H_HALMAC: 155 rtw_fw_c2h_cmd_handle_ext(rtwdev, skb); 156 break; 157 case C2H_RA_RPT: 158 rtw_fw_ra_report_handle(rtwdev, c2h->payload, len); 159 break; 160 default: 161 rtw_dbg(rtwdev, RTW_DBG_FW, "C2H 0x%x isn't handled\n", c2h->id); 162 break; 163 } 164 165 unlock: 166 mutex_unlock(&rtwdev->mutex); 167 } 168 169 void rtw_fw_c2h_cmd_rx_irqsafe(struct rtw_dev *rtwdev, u32 pkt_offset, 170 struct sk_buff *skb) 171 { 172 struct rtw_c2h_cmd *c2h; 173 u8 len; 174 175 c2h = (struct rtw_c2h_cmd *)(skb->data + pkt_offset); 176 len = skb->len - pkt_offset - 2; 177 *((u32 *)skb->cb) = pkt_offset; 178 179 rtw_dbg(rtwdev, RTW_DBG_FW, "recv C2H, id=0x%02x, seq=0x%02x, len=%d\n", 180 c2h->id, c2h->seq, len); 181 182 switch (c2h->id) { 183 case C2H_BT_MP_INFO: 184 rtw_coex_info_response(rtwdev, skb); 185 break; 186 default: 187 /* pass offset for further operation */ 188 *((u32 *)skb->cb) = pkt_offset; 189 skb_queue_tail(&rtwdev->c2h_queue, skb); 190 ieee80211_queue_work(rtwdev->hw, &rtwdev->c2h_work); 191 break; 192 } 193 } 194 EXPORT_SYMBOL(rtw_fw_c2h_cmd_rx_irqsafe); 195 196 static void rtw_fw_send_h2c_command(struct rtw_dev *rtwdev, 197 u8 *h2c) 198 { 199 u8 box; 200 u8 box_state; 201 u32 box_reg, box_ex_reg; 202 int idx; 203 int ret; 204 205 rtw_dbg(rtwdev, RTW_DBG_FW, 206 "send H2C content %02x%02x%02x%02x %02x%02x%02x%02x\n", 207 h2c[3], h2c[2], h2c[1], h2c[0], 208 h2c[7], h2c[6], h2c[5], h2c[4]); 209 210 spin_lock(&rtwdev->h2c.lock); 211 212 box = rtwdev->h2c.last_box_num; 213 switch (box) { 214 case 0: 215 box_reg = REG_HMEBOX0; 216 box_ex_reg = REG_HMEBOX0_EX; 217 break; 218 case 1: 219 box_reg = REG_HMEBOX1; 220 box_ex_reg = REG_HMEBOX1_EX; 221 break; 222 case 2: 223 box_reg = REG_HMEBOX2; 224 box_ex_reg = REG_HMEBOX2_EX; 225 break; 226 case 3: 227 box_reg = REG_HMEBOX3; 228 box_ex_reg = REG_HMEBOX3_EX; 229 break; 230 default: 231 WARN(1, "invalid h2c mail box number\n"); 232 goto out; 233 } 234 235 ret = read_poll_timeout_atomic(rtw_read8, box_state, 236 !((box_state >> box) & 0x1), 100, 3000, 237 false, rtwdev, REG_HMETFR); 238 239 if (ret) { 240 rtw_err(rtwdev, "failed to send h2c command\n"); 241 goto out; 242 } 243 244 for (idx = 0; idx < 4; idx++) 245 rtw_write8(rtwdev, box_reg + idx, h2c[idx]); 246 for (idx = 0; idx < 4; idx++) 247 rtw_write8(rtwdev, box_ex_reg + idx, h2c[idx + 4]); 248 249 if (++rtwdev->h2c.last_box_num >= 4) 250 rtwdev->h2c.last_box_num = 0; 251 252 out: 253 spin_unlock(&rtwdev->h2c.lock); 254 } 255 256 void rtw_fw_h2c_cmd_dbg(struct rtw_dev *rtwdev, u8 *h2c) 257 { 258 rtw_fw_send_h2c_command(rtwdev, h2c); 259 } 260 261 static void rtw_fw_send_h2c_packet(struct rtw_dev *rtwdev, u8 *h2c_pkt) 262 { 263 int ret; 264 265 spin_lock(&rtwdev->h2c.lock); 266 267 FW_OFFLOAD_H2C_SET_SEQ_NUM(h2c_pkt, rtwdev->h2c.seq); 268 ret = rtw_hci_write_data_h2c(rtwdev, h2c_pkt, H2C_PKT_SIZE); 269 if (ret) 270 rtw_err(rtwdev, "failed to send h2c packet\n"); 271 rtwdev->h2c.seq++; 272 273 spin_unlock(&rtwdev->h2c.lock); 274 } 275 276 void 277 rtw_fw_send_general_info(struct rtw_dev *rtwdev) 278 { 279 struct rtw_fifo_conf *fifo = &rtwdev->fifo; 280 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 281 u16 total_size = H2C_PKT_HDR_SIZE + 4; 282 283 if (rtw_chip_wcpu_11n(rtwdev)) 284 return; 285 286 rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_GENERAL_INFO); 287 288 SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size); 289 290 GENERAL_INFO_SET_FW_TX_BOUNDARY(h2c_pkt, 291 fifo->rsvd_fw_txbuf_addr - 292 fifo->rsvd_boundary); 293 294 rtw_fw_send_h2c_packet(rtwdev, h2c_pkt); 295 } 296 297 void 298 rtw_fw_send_phydm_info(struct rtw_dev *rtwdev) 299 { 300 struct rtw_hal *hal = &rtwdev->hal; 301 struct rtw_efuse *efuse = &rtwdev->efuse; 302 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 303 u16 total_size = H2C_PKT_HDR_SIZE + 8; 304 u8 fw_rf_type = 0; 305 306 if (rtw_chip_wcpu_11n(rtwdev)) 307 return; 308 309 if (hal->rf_type == RF_1T1R) 310 fw_rf_type = FW_RF_1T1R; 311 else if (hal->rf_type == RF_2T2R) 312 fw_rf_type = FW_RF_2T2R; 313 314 rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_PHYDM_INFO); 315 316 SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size); 317 PHYDM_INFO_SET_REF_TYPE(h2c_pkt, efuse->rfe_option); 318 PHYDM_INFO_SET_RF_TYPE(h2c_pkt, fw_rf_type); 319 PHYDM_INFO_SET_CUT_VER(h2c_pkt, hal->cut_version); 320 PHYDM_INFO_SET_RX_ANT_STATUS(h2c_pkt, hal->antenna_tx); 321 PHYDM_INFO_SET_TX_ANT_STATUS(h2c_pkt, hal->antenna_rx); 322 323 rtw_fw_send_h2c_packet(rtwdev, h2c_pkt); 324 } 325 326 void rtw_fw_do_iqk(struct rtw_dev *rtwdev, struct rtw_iqk_para *para) 327 { 328 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 329 u16 total_size = H2C_PKT_HDR_SIZE + 1; 330 331 rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_IQK); 332 SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size); 333 IQK_SET_CLEAR(h2c_pkt, para->clear); 334 IQK_SET_SEGMENT_IQK(h2c_pkt, para->segment_iqk); 335 336 rtw_fw_send_h2c_packet(rtwdev, h2c_pkt); 337 } 338 EXPORT_SYMBOL(rtw_fw_do_iqk); 339 340 void rtw_fw_query_bt_info(struct rtw_dev *rtwdev) 341 { 342 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 343 344 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_INFO); 345 346 SET_QUERY_BT_INFO(h2c_pkt, true); 347 348 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 349 } 350 351 void rtw_fw_wl_ch_info(struct rtw_dev *rtwdev, u8 link, u8 ch, u8 bw) 352 { 353 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 354 355 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WL_CH_INFO); 356 357 SET_WL_CH_INFO_LINK(h2c_pkt, link); 358 SET_WL_CH_INFO_CHNL(h2c_pkt, ch); 359 SET_WL_CH_INFO_BW(h2c_pkt, bw); 360 361 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 362 } 363 364 void rtw_fw_query_bt_mp_info(struct rtw_dev *rtwdev, 365 struct rtw_coex_info_req *req) 366 { 367 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 368 369 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_MP_INFO); 370 371 SET_BT_MP_INFO_SEQ(h2c_pkt, req->seq); 372 SET_BT_MP_INFO_OP_CODE(h2c_pkt, req->op_code); 373 SET_BT_MP_INFO_PARA1(h2c_pkt, req->para1); 374 SET_BT_MP_INFO_PARA2(h2c_pkt, req->para2); 375 SET_BT_MP_INFO_PARA3(h2c_pkt, req->para3); 376 377 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 378 } 379 380 void rtw_fw_force_bt_tx_power(struct rtw_dev *rtwdev, u8 bt_pwr_dec_lvl) 381 { 382 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 383 u8 index = 0 - bt_pwr_dec_lvl; 384 385 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_FORCE_BT_TX_POWER); 386 387 SET_BT_TX_POWER_INDEX(h2c_pkt, index); 388 389 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 390 } 391 392 void rtw_fw_bt_ignore_wlan_action(struct rtw_dev *rtwdev, bool enable) 393 { 394 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 395 396 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_IGNORE_WLAN_ACTION); 397 398 SET_IGNORE_WLAN_ACTION_EN(h2c_pkt, enable); 399 400 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 401 } 402 403 void rtw_fw_coex_tdma_type(struct rtw_dev *rtwdev, 404 u8 para1, u8 para2, u8 para3, u8 para4, u8 para5) 405 { 406 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 407 408 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_COEX_TDMA_TYPE); 409 410 SET_COEX_TDMA_TYPE_PARA1(h2c_pkt, para1); 411 SET_COEX_TDMA_TYPE_PARA2(h2c_pkt, para2); 412 SET_COEX_TDMA_TYPE_PARA3(h2c_pkt, para3); 413 SET_COEX_TDMA_TYPE_PARA4(h2c_pkt, para4); 414 SET_COEX_TDMA_TYPE_PARA5(h2c_pkt, para5); 415 416 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 417 } 418 419 void rtw_fw_bt_wifi_control(struct rtw_dev *rtwdev, u8 op_code, u8 *data) 420 { 421 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 422 423 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BT_WIFI_CONTROL); 424 425 SET_BT_WIFI_CONTROL_OP_CODE(h2c_pkt, op_code); 426 427 SET_BT_WIFI_CONTROL_DATA1(h2c_pkt, *data); 428 SET_BT_WIFI_CONTROL_DATA2(h2c_pkt, *(data + 1)); 429 SET_BT_WIFI_CONTROL_DATA3(h2c_pkt, *(data + 2)); 430 SET_BT_WIFI_CONTROL_DATA4(h2c_pkt, *(data + 3)); 431 SET_BT_WIFI_CONTROL_DATA5(h2c_pkt, *(data + 4)); 432 433 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 434 } 435 436 void rtw_fw_send_rssi_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si) 437 { 438 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 439 u8 rssi = ewma_rssi_read(&si->avg_rssi); 440 bool stbc_en = si->stbc_en ? true : false; 441 442 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RSSI_MONITOR); 443 444 SET_RSSI_INFO_MACID(h2c_pkt, si->mac_id); 445 SET_RSSI_INFO_RSSI(h2c_pkt, rssi); 446 SET_RSSI_INFO_STBC(h2c_pkt, stbc_en); 447 448 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 449 } 450 451 void rtw_fw_send_ra_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si) 452 { 453 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 454 bool no_update = si->updated; 455 bool disable_pt = true; 456 457 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RA_INFO); 458 459 SET_RA_INFO_MACID(h2c_pkt, si->mac_id); 460 SET_RA_INFO_RATE_ID(h2c_pkt, si->rate_id); 461 SET_RA_INFO_INIT_RA_LVL(h2c_pkt, si->init_ra_lv); 462 SET_RA_INFO_SGI_EN(h2c_pkt, si->sgi_enable); 463 SET_RA_INFO_BW_MODE(h2c_pkt, si->bw_mode); 464 SET_RA_INFO_LDPC(h2c_pkt, !!si->ldpc_en); 465 SET_RA_INFO_NO_UPDATE(h2c_pkt, no_update); 466 SET_RA_INFO_VHT_EN(h2c_pkt, si->vht_enable); 467 SET_RA_INFO_DIS_PT(h2c_pkt, disable_pt); 468 SET_RA_INFO_RA_MASK0(h2c_pkt, (si->ra_mask & 0xff)); 469 SET_RA_INFO_RA_MASK1(h2c_pkt, (si->ra_mask & 0xff00) >> 8); 470 SET_RA_INFO_RA_MASK2(h2c_pkt, (si->ra_mask & 0xff0000) >> 16); 471 SET_RA_INFO_RA_MASK3(h2c_pkt, (si->ra_mask & 0xff000000) >> 24); 472 473 si->init_ra_lv = 0; 474 si->updated = true; 475 476 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 477 } 478 479 void rtw_fw_media_status_report(struct rtw_dev *rtwdev, u8 mac_id, bool connect) 480 { 481 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 482 483 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_MEDIA_STATUS_RPT); 484 MEDIA_STATUS_RPT_SET_OP_MODE(h2c_pkt, connect); 485 MEDIA_STATUS_RPT_SET_MACID(h2c_pkt, mac_id); 486 487 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 488 } 489 490 void rtw_fw_set_pwr_mode(struct rtw_dev *rtwdev) 491 { 492 struct rtw_lps_conf *conf = &rtwdev->lps_conf; 493 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 494 495 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_SET_PWR_MODE); 496 497 SET_PWR_MODE_SET_MODE(h2c_pkt, conf->mode); 498 SET_PWR_MODE_SET_RLBM(h2c_pkt, conf->rlbm); 499 SET_PWR_MODE_SET_SMART_PS(h2c_pkt, conf->smart_ps); 500 SET_PWR_MODE_SET_AWAKE_INTERVAL(h2c_pkt, conf->awake_interval); 501 SET_PWR_MODE_SET_PORT_ID(h2c_pkt, conf->port_id); 502 SET_PWR_MODE_SET_PWR_STATE(h2c_pkt, conf->state); 503 504 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 505 } 506 507 void rtw_fw_set_keep_alive_cmd(struct rtw_dev *rtwdev, bool enable) 508 { 509 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 510 struct rtw_fw_wow_keep_alive_para mode = { 511 .adopt = true, 512 .pkt_type = KEEP_ALIVE_NULL_PKT, 513 .period = 5, 514 }; 515 516 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_KEEP_ALIVE); 517 SET_KEEP_ALIVE_ENABLE(h2c_pkt, enable); 518 SET_KEEP_ALIVE_ADOPT(h2c_pkt, mode.adopt); 519 SET_KEEP_ALIVE_PKT_TYPE(h2c_pkt, mode.pkt_type); 520 SET_KEEP_ALIVE_CHECK_PERIOD(h2c_pkt, mode.period); 521 522 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 523 } 524 525 void rtw_fw_set_disconnect_decision_cmd(struct rtw_dev *rtwdev, bool enable) 526 { 527 struct rtw_wow_param *rtw_wow = &rtwdev->wow; 528 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 529 struct rtw_fw_wow_disconnect_para mode = { 530 .adopt = true, 531 .period = 30, 532 .retry_count = 5, 533 }; 534 535 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_DISCONNECT_DECISION); 536 537 if (test_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags)) { 538 SET_DISCONNECT_DECISION_ENABLE(h2c_pkt, enable); 539 SET_DISCONNECT_DECISION_ADOPT(h2c_pkt, mode.adopt); 540 SET_DISCONNECT_DECISION_CHECK_PERIOD(h2c_pkt, mode.period); 541 SET_DISCONNECT_DECISION_TRY_PKT_NUM(h2c_pkt, mode.retry_count); 542 } 543 544 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 545 } 546 547 void rtw_fw_set_wowlan_ctrl_cmd(struct rtw_dev *rtwdev, bool enable) 548 { 549 struct rtw_wow_param *rtw_wow = &rtwdev->wow; 550 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 551 552 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WOWLAN); 553 554 SET_WOWLAN_FUNC_ENABLE(h2c_pkt, enable); 555 if (rtw_wow_mgd_linked(rtwdev)) { 556 if (test_bit(RTW_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags)) 557 SET_WOWLAN_MAGIC_PKT_ENABLE(h2c_pkt, enable); 558 if (test_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags)) 559 SET_WOWLAN_DEAUTH_WAKEUP_ENABLE(h2c_pkt, enable); 560 if (test_bit(RTW_WOW_FLAG_EN_REKEY_PKT, rtw_wow->flags)) 561 SET_WOWLAN_REKEY_WAKEUP_ENABLE(h2c_pkt, enable); 562 if (rtw_wow->pattern_cnt) 563 SET_WOWLAN_PATTERN_MATCH_ENABLE(h2c_pkt, enable); 564 } 565 566 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 567 } 568 569 void rtw_fw_set_aoac_global_info_cmd(struct rtw_dev *rtwdev, 570 u8 pairwise_key_enc, 571 u8 group_key_enc) 572 { 573 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 574 575 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_AOAC_GLOBAL_INFO); 576 577 SET_AOAC_GLOBAL_INFO_PAIRWISE_ENC_ALG(h2c_pkt, pairwise_key_enc); 578 SET_AOAC_GLOBAL_INFO_GROUP_ENC_ALG(h2c_pkt, group_key_enc); 579 580 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 581 } 582 583 void rtw_fw_set_remote_wake_ctrl_cmd(struct rtw_dev *rtwdev, bool enable) 584 { 585 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 586 587 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_REMOTE_WAKE_CTRL); 588 589 SET_REMOTE_WAKECTRL_ENABLE(h2c_pkt, enable); 590 591 if (rtw_wow_no_link(rtwdev)) 592 SET_REMOTE_WAKE_CTRL_NLO_OFFLOAD_EN(h2c_pkt, enable); 593 594 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 595 } 596 597 static u8 rtw_get_rsvd_page_location(struct rtw_dev *rtwdev, 598 enum rtw_rsvd_packet_type type) 599 { 600 struct rtw_rsvd_page *rsvd_pkt; 601 u8 location = 0; 602 603 list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) { 604 if (type == rsvd_pkt->type) 605 location = rsvd_pkt->page; 606 } 607 608 return location; 609 } 610 611 void rtw_fw_set_nlo_info(struct rtw_dev *rtwdev, bool enable) 612 { 613 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 614 u8 loc_nlo; 615 616 loc_nlo = rtw_get_rsvd_page_location(rtwdev, RSVD_NLO_INFO); 617 618 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_NLO_INFO); 619 620 SET_NLO_FUN_EN(h2c_pkt, enable); 621 if (enable) { 622 if (rtw_fw_lps_deep_mode) 623 SET_NLO_PS_32K(h2c_pkt, enable); 624 SET_NLO_IGNORE_SECURITY(h2c_pkt, enable); 625 SET_NLO_LOC_NLO_INFO(h2c_pkt, loc_nlo); 626 } 627 628 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 629 } 630 631 void rtw_fw_set_pg_info(struct rtw_dev *rtwdev) 632 { 633 struct rtw_lps_conf *conf = &rtwdev->lps_conf; 634 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 635 u8 loc_pg, loc_dpk; 636 637 loc_pg = rtw_get_rsvd_page_location(rtwdev, RSVD_LPS_PG_INFO); 638 loc_dpk = rtw_get_rsvd_page_location(rtwdev, RSVD_LPS_PG_DPK); 639 640 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_LPS_PG_INFO); 641 642 LPS_PG_INFO_LOC(h2c_pkt, loc_pg); 643 LPS_PG_DPK_LOC(h2c_pkt, loc_dpk); 644 LPS_PG_SEC_CAM_EN(h2c_pkt, conf->sec_cam_backup); 645 LPS_PG_PATTERN_CAM_EN(h2c_pkt, conf->pattern_cam_backup); 646 647 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 648 } 649 650 static u8 rtw_get_rsvd_page_probe_req_location(struct rtw_dev *rtwdev, 651 struct cfg80211_ssid *ssid) 652 { 653 struct rtw_rsvd_page *rsvd_pkt; 654 u8 location = 0; 655 656 list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) { 657 if (rsvd_pkt->type != RSVD_PROBE_REQ) 658 continue; 659 if ((!ssid && !rsvd_pkt->ssid) || 660 rtw_ssid_equal(rsvd_pkt->ssid, ssid)) 661 location = rsvd_pkt->page; 662 } 663 664 return location; 665 } 666 667 static u16 rtw_get_rsvd_page_probe_req_size(struct rtw_dev *rtwdev, 668 struct cfg80211_ssid *ssid) 669 { 670 struct rtw_rsvd_page *rsvd_pkt; 671 u16 size = 0; 672 673 list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) { 674 if (rsvd_pkt->type != RSVD_PROBE_REQ) 675 continue; 676 if ((!ssid && !rsvd_pkt->ssid) || 677 rtw_ssid_equal(rsvd_pkt->ssid, ssid)) 678 size = rsvd_pkt->skb->len; 679 } 680 681 return size; 682 } 683 684 void rtw_send_rsvd_page_h2c(struct rtw_dev *rtwdev) 685 { 686 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 687 u8 location = 0; 688 689 SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RSVD_PAGE); 690 691 location = rtw_get_rsvd_page_location(rtwdev, RSVD_PROBE_RESP); 692 *(h2c_pkt + 1) = location; 693 rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_PROBE_RESP loc: %d\n", location); 694 695 location = rtw_get_rsvd_page_location(rtwdev, RSVD_PS_POLL); 696 *(h2c_pkt + 2) = location; 697 rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_PS_POLL loc: %d\n", location); 698 699 location = rtw_get_rsvd_page_location(rtwdev, RSVD_NULL); 700 *(h2c_pkt + 3) = location; 701 rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_NULL loc: %d\n", location); 702 703 location = rtw_get_rsvd_page_location(rtwdev, RSVD_QOS_NULL); 704 *(h2c_pkt + 4) = location; 705 rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_QOS_NULL loc: %d\n", location); 706 707 rtw_fw_send_h2c_command(rtwdev, h2c_pkt); 708 } 709 710 static struct sk_buff *rtw_nlo_info_get(struct ieee80211_hw *hw) 711 { 712 struct rtw_dev *rtwdev = hw->priv; 713 struct rtw_chip_info *chip = rtwdev->chip; 714 struct rtw_pno_request *pno_req = &rtwdev->wow.pno_req; 715 struct rtw_nlo_info_hdr *nlo_hdr; 716 struct cfg80211_ssid *ssid; 717 struct sk_buff *skb; 718 u8 *pos, loc; 719 u32 size; 720 int i; 721 722 if (!pno_req->inited || !pno_req->match_set_cnt) 723 return NULL; 724 725 size = sizeof(struct rtw_nlo_info_hdr) + pno_req->match_set_cnt * 726 IEEE80211_MAX_SSID_LEN + chip->tx_pkt_desc_sz; 727 728 skb = alloc_skb(size, GFP_KERNEL); 729 if (!skb) 730 return NULL; 731 732 skb_reserve(skb, chip->tx_pkt_desc_sz); 733 734 nlo_hdr = skb_put_zero(skb, sizeof(struct rtw_nlo_info_hdr)); 735 736 nlo_hdr->nlo_count = pno_req->match_set_cnt; 737 nlo_hdr->hidden_ap_count = pno_req->match_set_cnt; 738 739 /* pattern check for firmware */ 740 memset(nlo_hdr->pattern_check, 0xA5, FW_NLO_INFO_CHECK_SIZE); 741 742 for (i = 0; i < pno_req->match_set_cnt; i++) 743 nlo_hdr->ssid_len[i] = pno_req->match_sets[i].ssid.ssid_len; 744 745 for (i = 0; i < pno_req->match_set_cnt; i++) { 746 ssid = &pno_req->match_sets[i].ssid; 747 loc = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid); 748 if (!loc) { 749 rtw_err(rtwdev, "failed to get probe req rsvd loc\n"); 750 kfree_skb(skb); 751 return NULL; 752 } 753 nlo_hdr->location[i] = loc; 754 } 755 756 for (i = 0; i < pno_req->match_set_cnt; i++) { 757 pos = skb_put_zero(skb, IEEE80211_MAX_SSID_LEN); 758 memcpy(pos, pno_req->match_sets[i].ssid.ssid, 759 pno_req->match_sets[i].ssid.ssid_len); 760 } 761 762 return skb; 763 } 764 765 static struct sk_buff *rtw_cs_channel_info_get(struct ieee80211_hw *hw) 766 { 767 struct rtw_dev *rtwdev = hw->priv; 768 struct rtw_chip_info *chip = rtwdev->chip; 769 struct rtw_pno_request *pno_req = &rtwdev->wow.pno_req; 770 struct ieee80211_channel *channels = pno_req->channels; 771 struct sk_buff *skb; 772 int count = pno_req->channel_cnt; 773 u8 *pos; 774 int i = 0; 775 776 skb = alloc_skb(4 * count + chip->tx_pkt_desc_sz, GFP_KERNEL); 777 if (!skb) 778 return NULL; 779 780 skb_reserve(skb, chip->tx_pkt_desc_sz); 781 782 for (i = 0; i < count; i++) { 783 pos = skb_put_zero(skb, 4); 784 785 CHSW_INFO_SET_CH(pos, channels[i].hw_value); 786 787 if (channels[i].flags & IEEE80211_CHAN_RADAR) 788 CHSW_INFO_SET_ACTION_ID(pos, 0); 789 else 790 CHSW_INFO_SET_ACTION_ID(pos, 1); 791 CHSW_INFO_SET_TIMEOUT(pos, 1); 792 CHSW_INFO_SET_PRI_CH_IDX(pos, 1); 793 CHSW_INFO_SET_BW(pos, 0); 794 } 795 796 return skb; 797 } 798 799 static struct sk_buff *rtw_lps_pg_dpk_get(struct ieee80211_hw *hw) 800 { 801 struct rtw_dev *rtwdev = hw->priv; 802 struct rtw_chip_info *chip = rtwdev->chip; 803 struct rtw_dpk_info *dpk_info = &rtwdev->dm_info.dpk_info; 804 struct rtw_lps_pg_dpk_hdr *dpk_hdr; 805 struct sk_buff *skb; 806 u32 size; 807 808 size = chip->tx_pkt_desc_sz + sizeof(*dpk_hdr); 809 skb = alloc_skb(size, GFP_KERNEL); 810 if (!skb) 811 return NULL; 812 813 skb_reserve(skb, chip->tx_pkt_desc_sz); 814 dpk_hdr = skb_put_zero(skb, sizeof(*dpk_hdr)); 815 dpk_hdr->dpk_ch = dpk_info->dpk_ch; 816 dpk_hdr->dpk_path_ok = dpk_info->dpk_path_ok[0]; 817 memcpy(dpk_hdr->dpk_txagc, dpk_info->dpk_txagc, 2); 818 memcpy(dpk_hdr->dpk_gs, dpk_info->dpk_gs, 4); 819 memcpy(dpk_hdr->coef, dpk_info->coef, 160); 820 821 return skb; 822 } 823 824 static struct sk_buff *rtw_lps_pg_info_get(struct ieee80211_hw *hw) 825 { 826 struct rtw_dev *rtwdev = hw->priv; 827 struct rtw_chip_info *chip = rtwdev->chip; 828 struct rtw_lps_conf *conf = &rtwdev->lps_conf; 829 struct rtw_lps_pg_info_hdr *pg_info_hdr; 830 struct rtw_wow_param *rtw_wow = &rtwdev->wow; 831 struct sk_buff *skb; 832 u32 size; 833 834 size = chip->tx_pkt_desc_sz + sizeof(*pg_info_hdr); 835 skb = alloc_skb(size, GFP_KERNEL); 836 if (!skb) 837 return NULL; 838 839 skb_reserve(skb, chip->tx_pkt_desc_sz); 840 pg_info_hdr = skb_put_zero(skb, sizeof(*pg_info_hdr)); 841 pg_info_hdr->tx_bu_page_count = rtwdev->fifo.rsvd_drv_pg_num; 842 pg_info_hdr->macid = find_first_bit(rtwdev->mac_id_map, RTW_MAX_MAC_ID_NUM); 843 pg_info_hdr->sec_cam_count = 844 rtw_sec_cam_pg_backup(rtwdev, pg_info_hdr->sec_cam); 845 pg_info_hdr->pattern_count = rtw_wow->pattern_cnt; 846 847 conf->sec_cam_backup = pg_info_hdr->sec_cam_count != 0; 848 conf->pattern_cam_backup = rtw_wow->pattern_cnt != 0; 849 850 return skb; 851 } 852 853 static struct sk_buff *rtw_get_rsvd_page_skb(struct ieee80211_hw *hw, 854 struct rtw_rsvd_page *rsvd_pkt) 855 { 856 struct ieee80211_vif *vif; 857 struct rtw_vif *rtwvif; 858 struct sk_buff *skb_new; 859 struct cfg80211_ssid *ssid; 860 861 if (rsvd_pkt->type == RSVD_DUMMY) { 862 skb_new = alloc_skb(1, GFP_KERNEL); 863 if (!skb_new) 864 return NULL; 865 866 skb_put(skb_new, 1); 867 return skb_new; 868 } 869 870 rtwvif = rsvd_pkt->rtwvif; 871 if (!rtwvif) 872 return NULL; 873 874 vif = rtwvif_to_vif(rtwvif); 875 876 switch (rsvd_pkt->type) { 877 case RSVD_BEACON: 878 skb_new = ieee80211_beacon_get(hw, vif); 879 break; 880 case RSVD_PS_POLL: 881 skb_new = ieee80211_pspoll_get(hw, vif); 882 break; 883 case RSVD_PROBE_RESP: 884 skb_new = ieee80211_proberesp_get(hw, vif); 885 break; 886 case RSVD_NULL: 887 skb_new = ieee80211_nullfunc_get(hw, vif, false); 888 break; 889 case RSVD_QOS_NULL: 890 skb_new = ieee80211_nullfunc_get(hw, vif, true); 891 break; 892 case RSVD_LPS_PG_DPK: 893 skb_new = rtw_lps_pg_dpk_get(hw); 894 break; 895 case RSVD_LPS_PG_INFO: 896 skb_new = rtw_lps_pg_info_get(hw); 897 break; 898 case RSVD_PROBE_REQ: 899 ssid = (struct cfg80211_ssid *)rsvd_pkt->ssid; 900 if (ssid) 901 skb_new = ieee80211_probereq_get(hw, vif->addr, 902 ssid->ssid, 903 ssid->ssid_len, 0); 904 else 905 skb_new = ieee80211_probereq_get(hw, vif->addr, NULL, 0, 0); 906 break; 907 case RSVD_NLO_INFO: 908 skb_new = rtw_nlo_info_get(hw); 909 break; 910 case RSVD_CH_INFO: 911 skb_new = rtw_cs_channel_info_get(hw); 912 break; 913 default: 914 return NULL; 915 } 916 917 if (!skb_new) 918 return NULL; 919 920 return skb_new; 921 } 922 923 static void rtw_fill_rsvd_page_desc(struct rtw_dev *rtwdev, struct sk_buff *skb, 924 enum rtw_rsvd_packet_type type) 925 { 926 struct rtw_tx_pkt_info pkt_info = {0}; 927 struct rtw_chip_info *chip = rtwdev->chip; 928 u8 *pkt_desc; 929 930 rtw_tx_rsvd_page_pkt_info_update(rtwdev, &pkt_info, skb, type); 931 pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz); 932 memset(pkt_desc, 0, chip->tx_pkt_desc_sz); 933 rtw_tx_fill_tx_desc(&pkt_info, skb); 934 } 935 936 static inline u8 rtw_len_to_page(unsigned int len, u8 page_size) 937 { 938 return DIV_ROUND_UP(len, page_size); 939 } 940 941 static void rtw_rsvd_page_list_to_buf(struct rtw_dev *rtwdev, u8 page_size, 942 u8 page_margin, u32 page, u8 *buf, 943 struct rtw_rsvd_page *rsvd_pkt) 944 { 945 struct sk_buff *skb = rsvd_pkt->skb; 946 947 if (page >= 1) 948 memcpy(buf + page_margin + page_size * (page - 1), 949 skb->data, skb->len); 950 else 951 memcpy(buf, skb->data, skb->len); 952 } 953 954 static struct rtw_rsvd_page *rtw_alloc_rsvd_page(struct rtw_dev *rtwdev, 955 enum rtw_rsvd_packet_type type, 956 bool txdesc) 957 { 958 struct rtw_rsvd_page *rsvd_pkt = NULL; 959 960 rsvd_pkt = kzalloc(sizeof(*rsvd_pkt), GFP_KERNEL); 961 962 if (!rsvd_pkt) 963 return NULL; 964 965 INIT_LIST_HEAD(&rsvd_pkt->vif_list); 966 INIT_LIST_HEAD(&rsvd_pkt->build_list); 967 rsvd_pkt->type = type; 968 rsvd_pkt->add_txdesc = txdesc; 969 970 return rsvd_pkt; 971 } 972 973 static void rtw_insert_rsvd_page(struct rtw_dev *rtwdev, 974 struct rtw_vif *rtwvif, 975 struct rtw_rsvd_page *rsvd_pkt) 976 { 977 lockdep_assert_held(&rtwdev->mutex); 978 979 list_add_tail(&rsvd_pkt->vif_list, &rtwvif->rsvd_page_list); 980 } 981 982 static void rtw_add_rsvd_page(struct rtw_dev *rtwdev, 983 struct rtw_vif *rtwvif, 984 enum rtw_rsvd_packet_type type, 985 bool txdesc) 986 { 987 struct rtw_rsvd_page *rsvd_pkt; 988 989 rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, type, txdesc); 990 if (!rsvd_pkt) { 991 rtw_err(rtwdev, "failed to alloc rsvd page %d\n", type); 992 return; 993 } 994 995 rsvd_pkt->rtwvif = rtwvif; 996 rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt); 997 } 998 999 static void rtw_add_rsvd_page_probe_req(struct rtw_dev *rtwdev, 1000 struct rtw_vif *rtwvif, 1001 struct cfg80211_ssid *ssid) 1002 { 1003 struct rtw_rsvd_page *rsvd_pkt; 1004 1005 rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_PROBE_REQ, true); 1006 if (!rsvd_pkt) { 1007 rtw_err(rtwdev, "failed to alloc probe req rsvd page\n"); 1008 return; 1009 } 1010 1011 rsvd_pkt->rtwvif = rtwvif; 1012 rsvd_pkt->ssid = ssid; 1013 rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt); 1014 } 1015 1016 void rtw_remove_rsvd_page(struct rtw_dev *rtwdev, 1017 struct rtw_vif *rtwvif) 1018 { 1019 struct rtw_rsvd_page *rsvd_pkt, *tmp; 1020 1021 lockdep_assert_held(&rtwdev->mutex); 1022 1023 /* remove all of the rsvd pages for vif */ 1024 list_for_each_entry_safe(rsvd_pkt, tmp, &rtwvif->rsvd_page_list, 1025 vif_list) { 1026 list_del(&rsvd_pkt->vif_list); 1027 if (!list_empty(&rsvd_pkt->build_list)) 1028 list_del(&rsvd_pkt->build_list); 1029 kfree(rsvd_pkt); 1030 } 1031 } 1032 1033 void rtw_add_rsvd_page_bcn(struct rtw_dev *rtwdev, 1034 struct rtw_vif *rtwvif) 1035 { 1036 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 1037 1038 if (vif->type != NL80211_IFTYPE_AP && 1039 vif->type != NL80211_IFTYPE_ADHOC && 1040 vif->type != NL80211_IFTYPE_MESH_POINT) { 1041 rtw_warn(rtwdev, "Cannot add beacon rsvd page for %d\n", 1042 vif->type); 1043 return; 1044 } 1045 1046 rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_BEACON, false); 1047 } 1048 1049 void rtw_add_rsvd_page_pno(struct rtw_dev *rtwdev, 1050 struct rtw_vif *rtwvif) 1051 { 1052 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 1053 struct rtw_wow_param *rtw_wow = &rtwdev->wow; 1054 struct rtw_pno_request *rtw_pno_req = &rtw_wow->pno_req; 1055 struct cfg80211_ssid *ssid; 1056 int i; 1057 1058 if (vif->type != NL80211_IFTYPE_STATION) { 1059 rtw_warn(rtwdev, "Cannot add PNO rsvd page for %d\n", 1060 vif->type); 1061 return; 1062 } 1063 1064 for (i = 0 ; i < rtw_pno_req->match_set_cnt; i++) { 1065 ssid = &rtw_pno_req->match_sets[i].ssid; 1066 rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, ssid); 1067 } 1068 1069 rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, NULL); 1070 rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NLO_INFO, false); 1071 rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_CH_INFO, true); 1072 } 1073 1074 void rtw_add_rsvd_page_sta(struct rtw_dev *rtwdev, 1075 struct rtw_vif *rtwvif) 1076 { 1077 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 1078 1079 if (vif->type != NL80211_IFTYPE_STATION) { 1080 rtw_warn(rtwdev, "Cannot add sta rsvd page for %d\n", 1081 vif->type); 1082 return; 1083 } 1084 1085 rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_PS_POLL, true); 1086 rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_QOS_NULL, true); 1087 rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NULL, true); 1088 rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_DPK, true); 1089 rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_INFO, true); 1090 } 1091 1092 int rtw_fw_write_data_rsvd_page(struct rtw_dev *rtwdev, u16 pg_addr, 1093 u8 *buf, u32 size) 1094 { 1095 u8 bckp[2]; 1096 u8 val; 1097 u16 rsvd_pg_head; 1098 u32 bcn_valid_addr; 1099 u32 bcn_valid_mask; 1100 int ret; 1101 1102 lockdep_assert_held(&rtwdev->mutex); 1103 1104 if (!size) 1105 return -EINVAL; 1106 1107 if (rtw_chip_wcpu_11n(rtwdev)) { 1108 rtw_write32_set(rtwdev, REG_DWBCN0_CTRL, BIT_BCN_VALID); 1109 } else { 1110 pg_addr &= BIT_MASK_BCN_HEAD_1_V1; 1111 pg_addr |= BIT_BCN_VALID_V1; 1112 rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2, pg_addr); 1113 } 1114 1115 val = rtw_read8(rtwdev, REG_CR + 1); 1116 bckp[0] = val; 1117 val |= BIT_ENSWBCN >> 8; 1118 rtw_write8(rtwdev, REG_CR + 1, val); 1119 1120 val = rtw_read8(rtwdev, REG_FWHW_TXQ_CTRL + 2); 1121 bckp[1] = val; 1122 val &= ~(BIT_EN_BCNQ_DL >> 16); 1123 rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, val); 1124 1125 ret = rtw_hci_write_data_rsvd_page(rtwdev, buf, size); 1126 if (ret) { 1127 rtw_err(rtwdev, "failed to write data to rsvd page\n"); 1128 goto restore; 1129 } 1130 1131 if (rtw_chip_wcpu_11n(rtwdev)) { 1132 bcn_valid_addr = REG_DWBCN0_CTRL; 1133 bcn_valid_mask = BIT_BCN_VALID; 1134 } else { 1135 bcn_valid_addr = REG_FIFOPAGE_CTRL_2; 1136 bcn_valid_mask = BIT_BCN_VALID_V1; 1137 } 1138 1139 if (!check_hw_ready(rtwdev, bcn_valid_addr, bcn_valid_mask, 1)) { 1140 rtw_err(rtwdev, "error beacon valid\n"); 1141 ret = -EBUSY; 1142 } 1143 1144 restore: 1145 rsvd_pg_head = rtwdev->fifo.rsvd_boundary; 1146 rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2, 1147 rsvd_pg_head | BIT_BCN_VALID_V1); 1148 rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, bckp[1]); 1149 rtw_write8(rtwdev, REG_CR + 1, bckp[0]); 1150 1151 return ret; 1152 } 1153 1154 static int rtw_download_drv_rsvd_page(struct rtw_dev *rtwdev, u8 *buf, u32 size) 1155 { 1156 u32 pg_size; 1157 u32 pg_num = 0; 1158 u16 pg_addr = 0; 1159 1160 pg_size = rtwdev->chip->page_size; 1161 pg_num = size / pg_size + ((size & (pg_size - 1)) ? 1 : 0); 1162 if (pg_num > rtwdev->fifo.rsvd_drv_pg_num) 1163 return -ENOMEM; 1164 1165 pg_addr = rtwdev->fifo.rsvd_drv_addr; 1166 1167 return rtw_fw_write_data_rsvd_page(rtwdev, pg_addr, buf, size); 1168 } 1169 1170 static void __rtw_build_rsvd_page_reset(struct rtw_dev *rtwdev) 1171 { 1172 struct rtw_rsvd_page *rsvd_pkt, *tmp; 1173 1174 list_for_each_entry_safe(rsvd_pkt, tmp, &rtwdev->rsvd_page_list, 1175 build_list) { 1176 list_del_init(&rsvd_pkt->build_list); 1177 1178 /* Don't free except for the dummy rsvd page, 1179 * others will be freed when removing vif 1180 */ 1181 if (rsvd_pkt->type == RSVD_DUMMY) 1182 kfree(rsvd_pkt); 1183 } 1184 } 1185 1186 static void rtw_build_rsvd_page_iter(void *data, u8 *mac, 1187 struct ieee80211_vif *vif) 1188 { 1189 struct rtw_dev *rtwdev = data; 1190 struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv; 1191 struct rtw_rsvd_page *rsvd_pkt; 1192 1193 list_for_each_entry(rsvd_pkt, &rtwvif->rsvd_page_list, vif_list) { 1194 if (rsvd_pkt->type == RSVD_BEACON) 1195 list_add(&rsvd_pkt->build_list, 1196 &rtwdev->rsvd_page_list); 1197 else 1198 list_add_tail(&rsvd_pkt->build_list, 1199 &rtwdev->rsvd_page_list); 1200 } 1201 } 1202 1203 static int __rtw_build_rsvd_page_from_vifs(struct rtw_dev *rtwdev) 1204 { 1205 struct rtw_rsvd_page *rsvd_pkt; 1206 1207 __rtw_build_rsvd_page_reset(rtwdev); 1208 1209 /* gather rsvd page from vifs */ 1210 rtw_iterate_vifs_atomic(rtwdev, rtw_build_rsvd_page_iter, rtwdev); 1211 1212 rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list, 1213 struct rtw_rsvd_page, build_list); 1214 if (!rsvd_pkt) { 1215 WARN(1, "Should not have an empty reserved page\n"); 1216 return -EINVAL; 1217 } 1218 1219 /* the first rsvd should be beacon, otherwise add a dummy one */ 1220 if (rsvd_pkt->type != RSVD_BEACON) { 1221 struct rtw_rsvd_page *dummy_pkt; 1222 1223 dummy_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_DUMMY, false); 1224 if (!dummy_pkt) { 1225 rtw_err(rtwdev, "failed to alloc dummy rsvd page\n"); 1226 return -ENOMEM; 1227 } 1228 1229 list_add(&dummy_pkt->build_list, &rtwdev->rsvd_page_list); 1230 } 1231 1232 return 0; 1233 } 1234 1235 static u8 *rtw_build_rsvd_page(struct rtw_dev *rtwdev, u32 *size) 1236 { 1237 struct ieee80211_hw *hw = rtwdev->hw; 1238 struct rtw_chip_info *chip = rtwdev->chip; 1239 struct sk_buff *iter; 1240 struct rtw_rsvd_page *rsvd_pkt; 1241 u32 page = 0; 1242 u8 total_page = 0; 1243 u8 page_size, page_margin, tx_desc_sz; 1244 u8 *buf; 1245 int ret; 1246 1247 page_size = chip->page_size; 1248 tx_desc_sz = chip->tx_pkt_desc_sz; 1249 page_margin = page_size - tx_desc_sz; 1250 1251 ret = __rtw_build_rsvd_page_from_vifs(rtwdev); 1252 if (ret) { 1253 rtw_err(rtwdev, 1254 "failed to build rsvd page from vifs, ret %d\n", ret); 1255 return NULL; 1256 } 1257 1258 list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) { 1259 iter = rtw_get_rsvd_page_skb(hw, rsvd_pkt); 1260 if (!iter) { 1261 rtw_err(rtwdev, "failed to build rsvd packet\n"); 1262 goto release_skb; 1263 } 1264 1265 /* Fill the tx_desc for the rsvd pkt that requires one. 1266 * And iter->len will be added with size of tx_desc_sz. 1267 */ 1268 if (rsvd_pkt->add_txdesc) 1269 rtw_fill_rsvd_page_desc(rtwdev, iter, rsvd_pkt->type); 1270 1271 rsvd_pkt->skb = iter; 1272 rsvd_pkt->page = total_page; 1273 1274 /* Reserved page is downloaded via TX path, and TX path will 1275 * generate a tx_desc at the header to describe length of 1276 * the buffer. If we are not counting page numbers with the 1277 * size of tx_desc added at the first rsvd_pkt (usually a 1278 * beacon, firmware default refer to the first page as the 1279 * content of beacon), we could generate a buffer which size 1280 * is smaller than the actual size of the whole rsvd_page 1281 */ 1282 if (total_page == 0) { 1283 if (rsvd_pkt->type != RSVD_BEACON && 1284 rsvd_pkt->type != RSVD_DUMMY) { 1285 rtw_err(rtwdev, "first page should be a beacon\n"); 1286 goto release_skb; 1287 } 1288 total_page += rtw_len_to_page(iter->len + tx_desc_sz, 1289 page_size); 1290 } else { 1291 total_page += rtw_len_to_page(iter->len, page_size); 1292 } 1293 } 1294 1295 if (total_page > rtwdev->fifo.rsvd_drv_pg_num) { 1296 rtw_err(rtwdev, "rsvd page over size: %d\n", total_page); 1297 goto release_skb; 1298 } 1299 1300 *size = (total_page - 1) * page_size + page_margin; 1301 buf = kzalloc(*size, GFP_KERNEL); 1302 if (!buf) 1303 goto release_skb; 1304 1305 /* Copy the content of each rsvd_pkt to the buf, and they should 1306 * be aligned to the pages. 1307 * 1308 * Note that the first rsvd_pkt is a beacon no matter what vif->type. 1309 * And that rsvd_pkt does not require tx_desc because when it goes 1310 * through TX path, the TX path will generate one for it. 1311 */ 1312 list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) { 1313 rtw_rsvd_page_list_to_buf(rtwdev, page_size, page_margin, 1314 page, buf, rsvd_pkt); 1315 if (page == 0) 1316 page += rtw_len_to_page(rsvd_pkt->skb->len + 1317 tx_desc_sz, page_size); 1318 else 1319 page += rtw_len_to_page(rsvd_pkt->skb->len, page_size); 1320 1321 kfree_skb(rsvd_pkt->skb); 1322 rsvd_pkt->skb = NULL; 1323 } 1324 1325 return buf; 1326 1327 release_skb: 1328 list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) { 1329 kfree_skb(rsvd_pkt->skb); 1330 rsvd_pkt->skb = NULL; 1331 } 1332 1333 return NULL; 1334 } 1335 1336 static int rtw_download_beacon(struct rtw_dev *rtwdev) 1337 { 1338 struct ieee80211_hw *hw = rtwdev->hw; 1339 struct rtw_rsvd_page *rsvd_pkt; 1340 struct sk_buff *skb; 1341 int ret = 0; 1342 1343 rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list, 1344 struct rtw_rsvd_page, build_list); 1345 if (!rsvd_pkt) { 1346 rtw_err(rtwdev, "failed to get rsvd page from build list\n"); 1347 return -ENOENT; 1348 } 1349 1350 if (rsvd_pkt->type != RSVD_BEACON && 1351 rsvd_pkt->type != RSVD_DUMMY) { 1352 rtw_err(rtwdev, "invalid rsvd page type %d, should be beacon or dummy\n", 1353 rsvd_pkt->type); 1354 return -EINVAL; 1355 } 1356 1357 skb = rtw_get_rsvd_page_skb(hw, rsvd_pkt); 1358 if (!skb) { 1359 rtw_err(rtwdev, "failed to get beacon skb\n"); 1360 return -ENOMEM; 1361 } 1362 1363 ret = rtw_download_drv_rsvd_page(rtwdev, skb->data, skb->len); 1364 if (ret) 1365 rtw_err(rtwdev, "failed to download drv rsvd page\n"); 1366 1367 dev_kfree_skb(skb); 1368 1369 return ret; 1370 } 1371 1372 int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev) 1373 { 1374 u8 *buf; 1375 u32 size; 1376 int ret; 1377 1378 buf = rtw_build_rsvd_page(rtwdev, &size); 1379 if (!buf) { 1380 rtw_err(rtwdev, "failed to build rsvd page pkt\n"); 1381 return -ENOMEM; 1382 } 1383 1384 ret = rtw_download_drv_rsvd_page(rtwdev, buf, size); 1385 if (ret) { 1386 rtw_err(rtwdev, "failed to download drv rsvd page\n"); 1387 goto free; 1388 } 1389 1390 /* The last thing is to download the *ONLY* beacon again, because 1391 * the previous tx_desc is to describe the total rsvd page. Download 1392 * the beacon again to replace the TX desc header, and we will get 1393 * a correct tx_desc for the beacon in the rsvd page. 1394 */ 1395 ret = rtw_download_beacon(rtwdev); 1396 if (ret) { 1397 rtw_err(rtwdev, "failed to download beacon\n"); 1398 goto free; 1399 } 1400 1401 free: 1402 kfree(buf); 1403 1404 return ret; 1405 } 1406 1407 int rtw_dump_drv_rsvd_page(struct rtw_dev *rtwdev, 1408 u32 offset, u32 size, u32 *buf) 1409 { 1410 struct rtw_fifo_conf *fifo = &rtwdev->fifo; 1411 u32 residue, i; 1412 u16 start_pg; 1413 u16 idx = 0; 1414 u16 ctl; 1415 u8 rcr; 1416 1417 if (size & 0x3) { 1418 rtw_warn(rtwdev, "should be 4-byte aligned\n"); 1419 return -EINVAL; 1420 } 1421 1422 offset += fifo->rsvd_boundary << TX_PAGE_SIZE_SHIFT; 1423 residue = offset & (FIFO_PAGE_SIZE - 1); 1424 start_pg = offset >> FIFO_PAGE_SIZE_SHIFT; 1425 start_pg += RSVD_PAGE_START_ADDR; 1426 1427 rcr = rtw_read8(rtwdev, REG_RCR + 2); 1428 ctl = rtw_read16(rtwdev, REG_PKTBUF_DBG_CTRL) & 0xf000; 1429 1430 /* disable rx clock gate */ 1431 rtw_write8(rtwdev, REG_RCR, rcr | BIT(3)); 1432 1433 do { 1434 rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, start_pg | ctl); 1435 1436 for (i = FIFO_DUMP_ADDR + residue; 1437 i < FIFO_DUMP_ADDR + FIFO_PAGE_SIZE; i += 4) { 1438 buf[idx++] = rtw_read32(rtwdev, i); 1439 size -= 4; 1440 if (size == 0) 1441 goto out; 1442 } 1443 1444 residue = 0; 1445 start_pg++; 1446 } while (size); 1447 1448 out: 1449 rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, ctl); 1450 rtw_write8(rtwdev, REG_RCR + 2, rcr); 1451 return 0; 1452 } 1453 1454 static void __rtw_fw_update_pkt(struct rtw_dev *rtwdev, u8 pkt_id, u16 size, 1455 u8 location) 1456 { 1457 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 1458 u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_UPDATE_PKT_LEN; 1459 1460 rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_UPDATE_PKT); 1461 1462 SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size); 1463 UPDATE_PKT_SET_PKT_ID(h2c_pkt, pkt_id); 1464 UPDATE_PKT_SET_LOCATION(h2c_pkt, location); 1465 1466 /* include txdesc size */ 1467 UPDATE_PKT_SET_SIZE(h2c_pkt, size); 1468 1469 rtw_fw_send_h2c_packet(rtwdev, h2c_pkt); 1470 } 1471 1472 void rtw_fw_update_pkt_probe_req(struct rtw_dev *rtwdev, 1473 struct cfg80211_ssid *ssid) 1474 { 1475 u8 loc; 1476 u32 size; 1477 1478 loc = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid); 1479 if (!loc) { 1480 rtw_err(rtwdev, "failed to get probe_req rsvd loc\n"); 1481 return; 1482 } 1483 1484 size = rtw_get_rsvd_page_probe_req_size(rtwdev, ssid); 1485 if (!size) { 1486 rtw_err(rtwdev, "failed to get probe_req rsvd size\n"); 1487 return; 1488 } 1489 1490 __rtw_fw_update_pkt(rtwdev, RTW_PACKET_PROBE_REQ, size, loc); 1491 } 1492 1493 void rtw_fw_channel_switch(struct rtw_dev *rtwdev, bool enable) 1494 { 1495 struct rtw_pno_request *rtw_pno_req = &rtwdev->wow.pno_req; 1496 u8 h2c_pkt[H2C_PKT_SIZE] = {0}; 1497 u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_CH_SWITCH_LEN; 1498 u8 loc_ch_info; 1499 const struct rtw_ch_switch_option cs_option = { 1500 .dest_ch_en = 1, 1501 .dest_ch = 1, 1502 .periodic_option = 2, 1503 .normal_period = 5, 1504 .normal_period_sel = 0, 1505 .normal_cycle = 10, 1506 .slow_period = 1, 1507 .slow_period_sel = 1, 1508 }; 1509 1510 rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_CH_SWITCH); 1511 SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size); 1512 1513 CH_SWITCH_SET_START(h2c_pkt, enable); 1514 CH_SWITCH_SET_DEST_CH_EN(h2c_pkt, cs_option.dest_ch_en); 1515 CH_SWITCH_SET_DEST_CH(h2c_pkt, cs_option.dest_ch); 1516 CH_SWITCH_SET_NORMAL_PERIOD(h2c_pkt, cs_option.normal_period); 1517 CH_SWITCH_SET_NORMAL_PERIOD_SEL(h2c_pkt, cs_option.normal_period_sel); 1518 CH_SWITCH_SET_SLOW_PERIOD(h2c_pkt, cs_option.slow_period); 1519 CH_SWITCH_SET_SLOW_PERIOD_SEL(h2c_pkt, cs_option.slow_period_sel); 1520 CH_SWITCH_SET_NORMAL_CYCLE(h2c_pkt, cs_option.normal_cycle); 1521 CH_SWITCH_SET_PERIODIC_OPT(h2c_pkt, cs_option.periodic_option); 1522 1523 CH_SWITCH_SET_CH_NUM(h2c_pkt, rtw_pno_req->channel_cnt); 1524 CH_SWITCH_SET_INFO_SIZE(h2c_pkt, rtw_pno_req->channel_cnt * 4); 1525 1526 loc_ch_info = rtw_get_rsvd_page_location(rtwdev, RSVD_CH_INFO); 1527 CH_SWITCH_SET_INFO_LOC(h2c_pkt, loc_ch_info); 1528 1529 rtw_fw_send_h2c_packet(rtwdev, h2c_pkt); 1530 } 1531