developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 1 | /* Copyright (C) 2021-2022 Mediatek Inc. */ |
| 2 | #ifndef __ATENL_H |
| 3 | #define __ATENL_H |
| 4 | |
| 5 | #include <arpa/inet.h> |
| 6 | #include <errno.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <limits.h> |
| 9 | #include <linux/nl80211.h> |
| 10 | #include <net/if.h> |
| 11 | #include <stdbool.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include "nl.h" |
| 17 | #include "util.h" |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 18 | #include "debug.h" |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 19 | |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 20 | #define BRIDGE_NAME "br-lan" |
| 21 | #define ETH_P_RACFG 0x2880 |
| 22 | #define RACFG_PKT_MAX_SIZE 1600 |
| 23 | #define RACFG_HLEN 12 |
| 24 | #define RACFG_MAGIC_NO 0x18142880 |
| 25 | |
| 26 | #define RACFG_CMD_TYPE_MASK GENMASK(14, 0) |
| 27 | #define RACFG_CMD_TYPE_ETHREQ BIT(3) |
| 28 | #define RACFG_CMD_TYPE_PLATFORM_MODULE GENMASK(4, 3) |
| 29 | |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 30 | #define set_band_val(_an, _band, _field, _val) \ |
| 31 | _an->anb[_band]._field = (_val) |
| 32 | #define get_band_val(_an, _band, _field) \ |
| 33 | (_an->anb[_band]._field) |
| 34 | |
| 35 | enum atenl_rf_mode { |
| 36 | ATENL_RF_MODE_NORMAL, |
| 37 | ATENL_RF_MODE_TEST, |
| 38 | ATENL_RF_MODE_ICAP, |
| 39 | ATENL_RF_MODE_ICAP_OVERLAP, |
| 40 | |
| 41 | __ATENL_RF_MODE_MAX, |
| 42 | }; |
| 43 | |
| 44 | struct atenl_rx_stat { |
| 45 | u64 total; |
| 46 | u64 ok_cnt; |
| 47 | u64 err_cnt; |
| 48 | u64 len_mismatch; |
| 49 | }; |
| 50 | |
| 51 | struct atenl_band { |
| 52 | bool valid; |
| 53 | u8 phy_idx; |
| 54 | u8 cap; |
| 55 | u8 chainmask; |
| 56 | |
| 57 | enum mt76_testmode_state cur_state; |
| 58 | s8 tx_power; |
| 59 | enum atenl_rf_mode rf_mode; |
| 60 | |
| 61 | bool use_tx_time; |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 62 | u32 tx_time; |
| 63 | u32 tx_mpdu_len; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 64 | |
| 65 | bool reset_tx_cnt; |
| 66 | bool reset_rx_cnt; |
| 67 | |
| 68 | /* history */ |
| 69 | struct atenl_rx_stat rx_stat; |
| 70 | }; |
| 71 | |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 72 | #define MAX_BAND_NUM 3 |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 73 | |
| 74 | struct atenl { |
| 75 | struct atenl_band anb[MAX_BAND_NUM]; |
| 76 | u16 chip_id; |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 77 | u16 adie_id; |
| 78 | u8 sub_chip_id; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 79 | u8 cur_band; |
| 80 | |
| 81 | u8 mac_addr[ETH_ALEN]; |
| 82 | bool unicast; |
| 83 | int sock_eth; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 84 | |
| 85 | const char *mtd_part; |
| 86 | u32 mtd_offset; |
| 87 | u8 *eeprom_data; |
| 88 | int eeprom_fd; |
| 89 | u16 eeprom_size; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 90 | |
| 91 | bool cmd_mode; |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 92 | |
developer | 763ab65 | 2022-06-14 18:38:23 +0800 | [diff] [blame] | 93 | bool ibf_cal; |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 94 | /* intermediate data */ |
| 95 | u8 ibf_mcs; |
| 96 | u8 ibf_ant; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | struct atenl_cmd_hdr { |
| 100 | __be32 magic_no; |
| 101 | __be16 cmd_type; |
| 102 | __be16 cmd_id; |
| 103 | __be16 len; |
| 104 | __be16 seq; |
| 105 | u8 data[2048]; |
| 106 | } __attribute__((packed)); |
| 107 | |
| 108 | enum atenl_cmd { |
| 109 | HQA_CMD_UNKNOWN, |
| 110 | HQA_CMD_LEGACY, /* legacy or deprecated */ |
| 111 | |
| 112 | HQA_CMD_OPEN_ADAPTER, |
| 113 | HQA_CMD_CLOSE_ADAPTER, |
| 114 | HQA_CMD_GET_CHIP_ID, |
| 115 | HQA_CMD_GET_SUB_CHIP_ID, |
| 116 | HQA_CMD_SET_TX_BW, |
| 117 | HQA_CMD_SET_TX_PKT_BW, |
| 118 | HQA_CMD_SET_TX_PRI_BW, |
| 119 | HQA_CMD_GET_TX_INFO, |
| 120 | HQA_CMD_SET_TX_PATH, |
| 121 | HQA_CMD_SET_TX_POWER, |
| 122 | HQA_CMD_SET_TX_POWER_MANUAL, |
| 123 | HQA_CMD_SET_RF_MODE, |
| 124 | HQA_CMD_SET_RX_PATH, |
| 125 | HQA_CMD_SET_RX_PKT_LEN, |
| 126 | HQA_CMD_SET_FREQ_OFFSET, |
| 127 | HQA_CMD_SET_TSSI, |
| 128 | HQA_CMD_SET_CFG, |
| 129 | HQA_CMD_SET_RU, |
| 130 | HQA_CMD_SET_BAND, |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 131 | HQA_CMD_SET_EEPROM_TO_FW, |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 132 | HQA_CMD_READ_MAC_BBP_REG, |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 133 | HQA_CMD_READ_MAC_BBP_REG_QA, |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 134 | HQA_CMD_READ_RF_REG, |
| 135 | HQA_CMD_READ_EEPROM_BULK, |
| 136 | HQA_CMD_READ_TEMPERATURE, |
| 137 | HQA_CMD_WRITE_MAC_BBP_REG, |
| 138 | HQA_CMD_WRITE_RF_REG, |
| 139 | HQA_CMD_WRITE_EEPROM_BULK, |
| 140 | HQA_CMD_WRITE_BUFFER_DONE, |
| 141 | HQA_CMD_GET_BAND, |
| 142 | HQA_CMD_GET_CFG, |
| 143 | HQA_CMD_GET_TX_POWER, |
| 144 | HQA_CMD_GET_TX_TONE_POWER, |
| 145 | HQA_CMD_GET_EFUSE_FREE_BLOCK, |
| 146 | HQA_CMD_GET_FREQ_OFFSET, |
| 147 | HQA_CMD_GET_FW_INFO, |
| 148 | HQA_CMD_GET_RX_INFO, |
| 149 | HQA_CMD_GET_RF_CAP, |
| 150 | HQA_CMD_CHECK_EFUSE_MODE, |
| 151 | HQA_CMD_CHECK_EFUSE_MODE_TYPE, |
| 152 | HQA_CMD_CHECK_EFUSE_MODE_NATIVE, |
| 153 | HQA_CMD_ANT_SWAP_CAP, |
| 154 | HQA_CMD_RESET_TX_RX_COUNTER, |
| 155 | HQA_CMD_CONTINUOUS_TX, |
| 156 | |
| 157 | HQA_CMD_EXT, |
| 158 | HQA_CMD_ERR, |
| 159 | |
| 160 | __HQA_CMD_MAX_NUM, |
| 161 | }; |
| 162 | |
| 163 | enum atenl_ext_cmd { |
| 164 | HQA_EXT_CMD_UNSPEC, |
| 165 | |
| 166 | HQA_EXT_CMD_SET_CHANNEL, |
| 167 | HQA_EXT_CMD_SET_TX, |
| 168 | HQA_EXT_CMD_START_TX, |
| 169 | HQA_EXT_CMD_START_RX, |
| 170 | HQA_EXT_CMD_STOP_TX, |
| 171 | HQA_EXT_CMD_STOP_RX, |
| 172 | HQA_EXT_CMD_SET_TX_TIME_OPT, |
| 173 | |
| 174 | HQA_EXT_CMD_OFF_CH_SCAN, |
| 175 | |
| 176 | HQA_EXT_CMD_IBF_SET_VAL, |
| 177 | HQA_EXT_CMD_IBF_GET_STATUS, |
| 178 | HQA_EXT_CMD_IBF_PROF_UPDATE_ALL, |
| 179 | |
| 180 | HQA_EXT_CMD_ERR, |
| 181 | |
| 182 | __HQA_EXT_CMD_MAX_NUM, |
| 183 | }; |
| 184 | |
| 185 | struct atenl_data { |
| 186 | u8 buf[RACFG_PKT_MAX_SIZE]; |
| 187 | int len; |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 188 | u16 cmd_id; |
| 189 | u8 ext_id; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 190 | enum atenl_cmd cmd; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 191 | enum atenl_ext_cmd ext_cmd; |
| 192 | }; |
| 193 | |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 194 | struct atenl_ops { |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 195 | int (*ops)(struct atenl *an, struct atenl_data *data); |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 196 | u8 cmd; |
| 197 | u8 flags; |
| 198 | u16 cmd_id; |
| 199 | u16 resp_len; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 200 | }; |
| 201 | |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 202 | #define ATENL_OPS_FLAG_EXT_CMD BIT(0) |
| 203 | #define ATENL_OPS_FLAG_LEGACY BIT(1) |
| 204 | #define ATENL_OPS_FLAG_SKIP BIT(2) |
| 205 | |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 206 | static inline struct atenl_cmd_hdr * atenl_hdr(struct atenl_data *data) |
| 207 | { |
| 208 | u8 *hqa_data = (u8 *)data->buf + ETH_HLEN; |
| 209 | |
| 210 | return (struct atenl_cmd_hdr *)hqa_data; |
| 211 | } |
| 212 | |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 213 | enum atenl_phy_type { |
| 214 | ATENL_PHY_TYPE_CCK, |
| 215 | ATENL_PHY_TYPE_OFDM, |
| 216 | ATENL_PHY_TYPE_HT, |
| 217 | ATENL_PHY_TYPE_HT_GF, |
| 218 | ATENL_PHY_TYPE_VHT, |
| 219 | ATENL_PHY_TYPE_HE_SU = 8, |
| 220 | ATENL_PHY_TYPE_HE_EXT_SU, |
| 221 | ATENL_PHY_TYPE_HE_TB, |
| 222 | ATENL_PHY_TYPE_HE_MU, |
| 223 | }; |
| 224 | |
| 225 | enum atenl_e2p_mode { |
| 226 | E2P_EFUSE_MODE = 1, |
| 227 | E2P_FLASH_MODE, |
| 228 | E2P_EEPROM_MODE, |
| 229 | E2P_BIN_MODE, |
| 230 | }; |
| 231 | |
| 232 | enum atenl_band_type { |
| 233 | BAND_TYPE_UNUSE, |
| 234 | BAND_TYPE_2G, |
| 235 | BAND_TYPE_5G, |
| 236 | BAND_TYPE_2G_5G, |
| 237 | BAND_TYPE_6G, |
| 238 | BAND_TYPE_2G_6G, |
| 239 | BAND_TYPE_5G_6G, |
| 240 | BAND_TYPE_2G_5G_6G, |
| 241 | }; |
| 242 | |
| 243 | enum atenl_ch_band { |
| 244 | CH_BAND_2GHZ, |
| 245 | CH_BAND_5GHZ, |
| 246 | CH_BAND_6GHZ, |
| 247 | }; |
| 248 | |
| 249 | /* for mt7915 */ |
| 250 | enum { |
| 251 | MT_EE_BAND_SEL_DEFAULT, |
| 252 | MT_EE_BAND_SEL_5GHZ, |
| 253 | MT_EE_BAND_SEL_2GHZ, |
| 254 | MT_EE_BAND_SEL_DUAL, |
| 255 | }; |
| 256 | |
| 257 | /* for mt7916/mt7986 */ |
| 258 | enum { |
| 259 | MT_EE_BAND_SEL_2G, |
| 260 | MT_EE_BAND_SEL_5G, |
| 261 | MT_EE_BAND_SEL_6G, |
| 262 | MT_EE_BAND_SEL_5G_6G, |
| 263 | }; |
| 264 | |
| 265 | #define MT_EE_WIFI_CONF 0x190 |
| 266 | #define MT_EE_WIFI_CONF0_BAND_SEL GENMASK(7, 6) |
| 267 | |
| 268 | enum { |
| 269 | MT7976_ONE_ADIE_DBDC = 0x7, |
| 270 | MT7975_ONE_ADIE_SINGLE_BAND = 0x8, /* AX7800 */ |
| 271 | MT7976_ONE_ADIE_SINGLE_BAND = 0xa, /* AX7800 */ |
| 272 | MT7975_DUAL_ADIE_DBDC = 0xd, /* AX6000 */ |
| 273 | MT7976_DUAL_ADIE_DBDC = 0xf, /* AX6000 */ |
| 274 | }; |
| 275 | |
| 276 | enum { |
| 277 | TEST_CBW_20MHZ, |
| 278 | TEST_CBW_40MHZ, |
| 279 | TEST_CBW_80MHZ, |
| 280 | TEST_CBW_10MHZ, |
| 281 | TEST_CBW_5MHZ, |
| 282 | TEST_CBW_160MHZ, |
| 283 | TEST_CBW_8080MHZ, |
| 284 | |
| 285 | TEST_CBW_MAX = TEST_CBW_8080MHZ - 1, |
| 286 | }; |
| 287 | |
| 288 | struct atenl_rx_info_hdr { |
| 289 | __be32 type; |
| 290 | __be32 ver; |
| 291 | __be32 val; |
| 292 | __be32 len; |
| 293 | } __attribute__((packed)); |
| 294 | |
| 295 | struct atenl_rx_info_band { |
| 296 | __be32 mac_rx_fcs_err_cnt; |
| 297 | __be32 mac_rx_mdrdy_cnt; |
| 298 | __be32 mac_rx_len_mismatch; |
| 299 | __be32 mac_rx_fcs_ok_cnt; |
| 300 | __be32 phy_rx_fcs_err_cnt_cck; |
| 301 | __be32 phy_rx_fcs_err_cnt_ofdm; |
| 302 | __be32 phy_rx_pd_cck; |
| 303 | __be32 phy_rx_pd_ofdm; |
| 304 | __be32 phy_rx_sig_err_cck; |
| 305 | __be32 phy_rx_sfd_err_cck; |
| 306 | __be32 phy_rx_sig_err_ofdm; |
| 307 | __be32 phy_rx_tag_err_ofdm; |
| 308 | __be32 phy_rx_mdrdy_cnt_cck; |
| 309 | __be32 phy_rx_mdrdy_cnt_ofdm; |
| 310 | } __attribute__((packed)); |
| 311 | |
| 312 | struct atenl_rx_info_path { |
| 313 | __be32 rcpi; |
| 314 | __be32 rssi; |
| 315 | __be32 fagc_ib_rssi; |
| 316 | __be32 fagc_wb_rssi; |
| 317 | __be32 inst_ib_rssi; |
| 318 | __be32 inst_wb_rssi; |
| 319 | } __attribute__((packed)); |
| 320 | |
| 321 | struct atenl_rx_info_user { |
| 322 | __be32 freq_offset; |
| 323 | __be32 snr; |
| 324 | __be32 fcs_error_cnt; |
| 325 | } __attribute__((packed)); |
| 326 | |
| 327 | struct atenl_rx_info_comm { |
| 328 | __be32 rx_fifo_full; |
| 329 | __be32 aci_hit_low; |
| 330 | __be32 aci_hit_high; |
| 331 | __be32 mu_pkt_count; |
| 332 | __be32 sig_mcs; |
| 333 | __be32 sinr; |
| 334 | __be32 driver_rx_count; |
| 335 | } __attribute__((packed)); |
| 336 | |
| 337 | enum atenl_ibf_action { |
| 338 | TXBF_ACT_INIT = 1, |
| 339 | TXBF_ACT_CHANNEL, |
| 340 | TXBF_ACT_MCS, |
| 341 | TXBF_ACT_POWER, |
| 342 | TXBF_ACT_TX_ANT, |
| 343 | TXBF_ACT_RX_START, |
| 344 | TXBF_ACT_RX_ANT, |
| 345 | TXBF_ACT_LNA_GAIN, |
| 346 | TXBF_ACT_IBF_PHASE_COMP, |
| 347 | TXBF_ACT_TX_PKT, |
| 348 | TXBF_ACT_IBF_PROF_UPDATE, |
| 349 | TXBF_ACT_EBF_PROF_UPDATE, |
| 350 | TXBF_ACT_IBF_PHASE_CAL, |
| 351 | TXBF_ACT_IBF_PHASE_E2P_UPDATE = 16, |
| 352 | }; |
| 353 | |
| 354 | static inline bool is_mt7915(struct atenl *an) |
| 355 | { |
| 356 | return an->chip_id == 0x7915; |
| 357 | } |
| 358 | |
| 359 | static inline bool is_mt7916(struct atenl *an) |
| 360 | { |
| 361 | return (an->chip_id == 0x7916) || (an->chip_id == 0x7906); |
| 362 | } |
| 363 | |
| 364 | static inline bool is_mt7986(struct atenl *an) |
| 365 | { |
| 366 | return an->chip_id == 0x7986; |
| 367 | } |
| 368 | |
| 369 | int atenl_eth_init(struct atenl *an); |
| 370 | int atenl_eth_recv(struct atenl *an, struct atenl_data *data); |
| 371 | int atenl_eth_send(struct atenl *an, struct atenl_data *data); |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 372 | int atenl_hqa_proc_cmd(struct atenl *an); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 373 | int atenl_nl_process(struct atenl *an, struct atenl_data *data); |
| 374 | int atenl_nl_process_many(struct atenl *an, struct atenl_data *data); |
| 375 | int atenl_nl_check_mtd(struct atenl *an); |
| 376 | int atenl_nl_write_eeprom(struct atenl *an, u32 offset, u8 *val, int len); |
developer | 9b7cdad | 2022-03-10 14:24:55 +0800 | [diff] [blame] | 377 | int atenl_nl_write_efuse_all(struct atenl *an); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 378 | int atenl_nl_update_buffer_mode(struct atenl *an); |
| 379 | int atenl_nl_set_state(struct atenl *an, u8 band, |
| 380 | enum mt76_testmode_state state); |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 381 | int atenl_nl_set_aid(struct atenl *an, u8 band, u8 aid); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 382 | int atenl_eeprom_init(struct atenl *an, u8 phy_idx); |
| 383 | void atenl_eeprom_close(struct atenl *an); |
| 384 | int atenl_eeprom_write_mtd(struct atenl *an); |
| 385 | int atenl_eeprom_read_from_driver(struct atenl *an, u32 offset, int len); |
| 386 | void atenl_eeprom_cmd_handler(struct atenl *an, u8 phy_idx, char *cmd); |
| 387 | u16 atenl_get_center_channel(u8 bw, u8 ch_band, u16 ctrl_ch); |
| 388 | int atenl_reg_read(struct atenl *an, u32 offset, u32 *res); |
| 389 | int atenl_reg_write(struct atenl *an, u32 offset, u32 val); |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 390 | int atenl_rf_read(struct atenl *an, u32 wf_sel, u32 offset, u32 *res); |
| 391 | int atenl_rf_write(struct atenl *an, u32 wf_sel, u32 offset, u32 val); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 392 | |
| 393 | #endif |