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 | |
| 93 | /* intermediate data */ |
| 94 | u8 ibf_mcs; |
| 95 | u8 ibf_ant; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | struct atenl_cmd_hdr { |
| 99 | __be32 magic_no; |
| 100 | __be16 cmd_type; |
| 101 | __be16 cmd_id; |
| 102 | __be16 len; |
| 103 | __be16 seq; |
| 104 | u8 data[2048]; |
| 105 | } __attribute__((packed)); |
| 106 | |
| 107 | enum atenl_cmd { |
| 108 | HQA_CMD_UNKNOWN, |
| 109 | HQA_CMD_LEGACY, /* legacy or deprecated */ |
| 110 | |
| 111 | HQA_CMD_OPEN_ADAPTER, |
| 112 | HQA_CMD_CLOSE_ADAPTER, |
| 113 | HQA_CMD_GET_CHIP_ID, |
| 114 | HQA_CMD_GET_SUB_CHIP_ID, |
| 115 | HQA_CMD_SET_TX_BW, |
| 116 | HQA_CMD_SET_TX_PKT_BW, |
| 117 | HQA_CMD_SET_TX_PRI_BW, |
| 118 | HQA_CMD_GET_TX_INFO, |
| 119 | HQA_CMD_SET_TX_PATH, |
| 120 | HQA_CMD_SET_TX_POWER, |
| 121 | HQA_CMD_SET_TX_POWER_MANUAL, |
| 122 | HQA_CMD_SET_RF_MODE, |
| 123 | HQA_CMD_SET_RX_PATH, |
| 124 | HQA_CMD_SET_RX_PKT_LEN, |
| 125 | HQA_CMD_SET_FREQ_OFFSET, |
| 126 | HQA_CMD_SET_TSSI, |
| 127 | HQA_CMD_SET_CFG, |
| 128 | HQA_CMD_SET_RU, |
| 129 | HQA_CMD_SET_BAND, |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 130 | HQA_CMD_SET_EEPROM_TO_FW, |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 131 | HQA_CMD_READ_MAC_BBP_REG, |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 132 | HQA_CMD_READ_MAC_BBP_REG_QA, |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 133 | HQA_CMD_READ_RF_REG, |
| 134 | HQA_CMD_READ_EEPROM_BULK, |
| 135 | HQA_CMD_READ_TEMPERATURE, |
| 136 | HQA_CMD_WRITE_MAC_BBP_REG, |
| 137 | HQA_CMD_WRITE_RF_REG, |
| 138 | HQA_CMD_WRITE_EEPROM_BULK, |
| 139 | HQA_CMD_WRITE_BUFFER_DONE, |
| 140 | HQA_CMD_GET_BAND, |
| 141 | HQA_CMD_GET_CFG, |
| 142 | HQA_CMD_GET_TX_POWER, |
| 143 | HQA_CMD_GET_TX_TONE_POWER, |
| 144 | HQA_CMD_GET_EFUSE_FREE_BLOCK, |
| 145 | HQA_CMD_GET_FREQ_OFFSET, |
| 146 | HQA_CMD_GET_FW_INFO, |
| 147 | HQA_CMD_GET_RX_INFO, |
| 148 | HQA_CMD_GET_RF_CAP, |
| 149 | HQA_CMD_CHECK_EFUSE_MODE, |
| 150 | HQA_CMD_CHECK_EFUSE_MODE_TYPE, |
| 151 | HQA_CMD_CHECK_EFUSE_MODE_NATIVE, |
| 152 | HQA_CMD_ANT_SWAP_CAP, |
| 153 | HQA_CMD_RESET_TX_RX_COUNTER, |
| 154 | HQA_CMD_CONTINUOUS_TX, |
| 155 | |
| 156 | HQA_CMD_EXT, |
| 157 | HQA_CMD_ERR, |
| 158 | |
| 159 | __HQA_CMD_MAX_NUM, |
| 160 | }; |
| 161 | |
| 162 | enum atenl_ext_cmd { |
| 163 | HQA_EXT_CMD_UNSPEC, |
| 164 | |
| 165 | HQA_EXT_CMD_SET_CHANNEL, |
| 166 | HQA_EXT_CMD_SET_TX, |
| 167 | HQA_EXT_CMD_START_TX, |
| 168 | HQA_EXT_CMD_START_RX, |
| 169 | HQA_EXT_CMD_STOP_TX, |
| 170 | HQA_EXT_CMD_STOP_RX, |
| 171 | HQA_EXT_CMD_SET_TX_TIME_OPT, |
| 172 | |
| 173 | HQA_EXT_CMD_OFF_CH_SCAN, |
| 174 | |
| 175 | HQA_EXT_CMD_IBF_SET_VAL, |
| 176 | HQA_EXT_CMD_IBF_GET_STATUS, |
| 177 | HQA_EXT_CMD_IBF_PROF_UPDATE_ALL, |
| 178 | |
| 179 | HQA_EXT_CMD_ERR, |
| 180 | |
| 181 | __HQA_EXT_CMD_MAX_NUM, |
| 182 | }; |
| 183 | |
| 184 | struct atenl_data { |
| 185 | u8 buf[RACFG_PKT_MAX_SIZE]; |
| 186 | int len; |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 187 | u16 cmd_id; |
| 188 | u8 ext_id; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 189 | enum atenl_cmd cmd; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 190 | enum atenl_ext_cmd ext_cmd; |
| 191 | }; |
| 192 | |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 193 | struct atenl_ops { |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 194 | int (*ops)(struct atenl *an, struct atenl_data *data); |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 195 | u8 cmd; |
| 196 | u8 flags; |
| 197 | u16 cmd_id; |
| 198 | u16 resp_len; |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 199 | }; |
| 200 | |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 201 | #define ATENL_OPS_FLAG_EXT_CMD BIT(0) |
| 202 | #define ATENL_OPS_FLAG_LEGACY BIT(1) |
| 203 | #define ATENL_OPS_FLAG_SKIP BIT(2) |
| 204 | |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 205 | static inline struct atenl_cmd_hdr * atenl_hdr(struct atenl_data *data) |
| 206 | { |
| 207 | u8 *hqa_data = (u8 *)data->buf + ETH_HLEN; |
| 208 | |
| 209 | return (struct atenl_cmd_hdr *)hqa_data; |
| 210 | } |
| 211 | |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 212 | enum atenl_phy_type { |
| 213 | ATENL_PHY_TYPE_CCK, |
| 214 | ATENL_PHY_TYPE_OFDM, |
| 215 | ATENL_PHY_TYPE_HT, |
| 216 | ATENL_PHY_TYPE_HT_GF, |
| 217 | ATENL_PHY_TYPE_VHT, |
| 218 | ATENL_PHY_TYPE_HE_SU = 8, |
| 219 | ATENL_PHY_TYPE_HE_EXT_SU, |
| 220 | ATENL_PHY_TYPE_HE_TB, |
| 221 | ATENL_PHY_TYPE_HE_MU, |
| 222 | }; |
| 223 | |
| 224 | enum atenl_e2p_mode { |
| 225 | E2P_EFUSE_MODE = 1, |
| 226 | E2P_FLASH_MODE, |
| 227 | E2P_EEPROM_MODE, |
| 228 | E2P_BIN_MODE, |
| 229 | }; |
| 230 | |
| 231 | enum atenl_band_type { |
| 232 | BAND_TYPE_UNUSE, |
| 233 | BAND_TYPE_2G, |
| 234 | BAND_TYPE_5G, |
| 235 | BAND_TYPE_2G_5G, |
| 236 | BAND_TYPE_6G, |
| 237 | BAND_TYPE_2G_6G, |
| 238 | BAND_TYPE_5G_6G, |
| 239 | BAND_TYPE_2G_5G_6G, |
| 240 | }; |
| 241 | |
| 242 | enum atenl_ch_band { |
| 243 | CH_BAND_2GHZ, |
| 244 | CH_BAND_5GHZ, |
| 245 | CH_BAND_6GHZ, |
| 246 | }; |
| 247 | |
| 248 | /* for mt7915 */ |
| 249 | enum { |
| 250 | MT_EE_BAND_SEL_DEFAULT, |
| 251 | MT_EE_BAND_SEL_5GHZ, |
| 252 | MT_EE_BAND_SEL_2GHZ, |
| 253 | MT_EE_BAND_SEL_DUAL, |
| 254 | }; |
| 255 | |
| 256 | /* for mt7916/mt7986 */ |
| 257 | enum { |
| 258 | MT_EE_BAND_SEL_2G, |
| 259 | MT_EE_BAND_SEL_5G, |
| 260 | MT_EE_BAND_SEL_6G, |
| 261 | MT_EE_BAND_SEL_5G_6G, |
| 262 | }; |
| 263 | |
| 264 | #define MT_EE_WIFI_CONF 0x190 |
| 265 | #define MT_EE_WIFI_CONF0_BAND_SEL GENMASK(7, 6) |
| 266 | |
| 267 | enum { |
| 268 | MT7976_ONE_ADIE_DBDC = 0x7, |
| 269 | MT7975_ONE_ADIE_SINGLE_BAND = 0x8, /* AX7800 */ |
| 270 | MT7976_ONE_ADIE_SINGLE_BAND = 0xa, /* AX7800 */ |
| 271 | MT7975_DUAL_ADIE_DBDC = 0xd, /* AX6000 */ |
| 272 | MT7976_DUAL_ADIE_DBDC = 0xf, /* AX6000 */ |
| 273 | }; |
| 274 | |
| 275 | enum { |
| 276 | TEST_CBW_20MHZ, |
| 277 | TEST_CBW_40MHZ, |
| 278 | TEST_CBW_80MHZ, |
| 279 | TEST_CBW_10MHZ, |
| 280 | TEST_CBW_5MHZ, |
| 281 | TEST_CBW_160MHZ, |
| 282 | TEST_CBW_8080MHZ, |
| 283 | |
| 284 | TEST_CBW_MAX = TEST_CBW_8080MHZ - 1, |
| 285 | }; |
| 286 | |
| 287 | struct atenl_rx_info_hdr { |
| 288 | __be32 type; |
| 289 | __be32 ver; |
| 290 | __be32 val; |
| 291 | __be32 len; |
| 292 | } __attribute__((packed)); |
| 293 | |
| 294 | struct atenl_rx_info_band { |
| 295 | __be32 mac_rx_fcs_err_cnt; |
| 296 | __be32 mac_rx_mdrdy_cnt; |
| 297 | __be32 mac_rx_len_mismatch; |
| 298 | __be32 mac_rx_fcs_ok_cnt; |
| 299 | __be32 phy_rx_fcs_err_cnt_cck; |
| 300 | __be32 phy_rx_fcs_err_cnt_ofdm; |
| 301 | __be32 phy_rx_pd_cck; |
| 302 | __be32 phy_rx_pd_ofdm; |
| 303 | __be32 phy_rx_sig_err_cck; |
| 304 | __be32 phy_rx_sfd_err_cck; |
| 305 | __be32 phy_rx_sig_err_ofdm; |
| 306 | __be32 phy_rx_tag_err_ofdm; |
| 307 | __be32 phy_rx_mdrdy_cnt_cck; |
| 308 | __be32 phy_rx_mdrdy_cnt_ofdm; |
| 309 | } __attribute__((packed)); |
| 310 | |
| 311 | struct atenl_rx_info_path { |
| 312 | __be32 rcpi; |
| 313 | __be32 rssi; |
| 314 | __be32 fagc_ib_rssi; |
| 315 | __be32 fagc_wb_rssi; |
| 316 | __be32 inst_ib_rssi; |
| 317 | __be32 inst_wb_rssi; |
| 318 | } __attribute__((packed)); |
| 319 | |
| 320 | struct atenl_rx_info_user { |
| 321 | __be32 freq_offset; |
| 322 | __be32 snr; |
| 323 | __be32 fcs_error_cnt; |
| 324 | } __attribute__((packed)); |
| 325 | |
| 326 | struct atenl_rx_info_comm { |
| 327 | __be32 rx_fifo_full; |
| 328 | __be32 aci_hit_low; |
| 329 | __be32 aci_hit_high; |
| 330 | __be32 mu_pkt_count; |
| 331 | __be32 sig_mcs; |
| 332 | __be32 sinr; |
| 333 | __be32 driver_rx_count; |
| 334 | } __attribute__((packed)); |
| 335 | |
| 336 | enum atenl_ibf_action { |
| 337 | TXBF_ACT_INIT = 1, |
| 338 | TXBF_ACT_CHANNEL, |
| 339 | TXBF_ACT_MCS, |
| 340 | TXBF_ACT_POWER, |
| 341 | TXBF_ACT_TX_ANT, |
| 342 | TXBF_ACT_RX_START, |
| 343 | TXBF_ACT_RX_ANT, |
| 344 | TXBF_ACT_LNA_GAIN, |
| 345 | TXBF_ACT_IBF_PHASE_COMP, |
| 346 | TXBF_ACT_TX_PKT, |
| 347 | TXBF_ACT_IBF_PROF_UPDATE, |
| 348 | TXBF_ACT_EBF_PROF_UPDATE, |
| 349 | TXBF_ACT_IBF_PHASE_CAL, |
| 350 | TXBF_ACT_IBF_PHASE_E2P_UPDATE = 16, |
| 351 | }; |
| 352 | |
| 353 | static inline bool is_mt7915(struct atenl *an) |
| 354 | { |
| 355 | return an->chip_id == 0x7915; |
| 356 | } |
| 357 | |
| 358 | static inline bool is_mt7916(struct atenl *an) |
| 359 | { |
| 360 | return (an->chip_id == 0x7916) || (an->chip_id == 0x7906); |
| 361 | } |
| 362 | |
| 363 | static inline bool is_mt7986(struct atenl *an) |
| 364 | { |
| 365 | return an->chip_id == 0x7986; |
| 366 | } |
| 367 | |
| 368 | int atenl_eth_init(struct atenl *an); |
| 369 | int atenl_eth_recv(struct atenl *an, struct atenl_data *data); |
| 370 | int atenl_eth_send(struct atenl *an, struct atenl_data *data); |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 371 | int atenl_hqa_proc_cmd(struct atenl *an); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 372 | int atenl_nl_process(struct atenl *an, struct atenl_data *data); |
| 373 | int atenl_nl_process_many(struct atenl *an, struct atenl_data *data); |
| 374 | int atenl_nl_check_mtd(struct atenl *an); |
| 375 | 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] | 376 | int atenl_nl_write_efuse_all(struct atenl *an); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 377 | int atenl_nl_update_buffer_mode(struct atenl *an); |
| 378 | int atenl_nl_set_state(struct atenl *an, u8 band, |
| 379 | enum mt76_testmode_state state); |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 380 | int atenl_nl_set_aid(struct atenl *an, u8 band, u8 aid); |
developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 381 | int atenl_eeprom_init(struct atenl *an, u8 phy_idx); |
| 382 | void atenl_eeprom_close(struct atenl *an); |
| 383 | int atenl_eeprom_write_mtd(struct atenl *an); |
| 384 | int atenl_eeprom_read_from_driver(struct atenl *an, u32 offset, int len); |
| 385 | void atenl_eeprom_cmd_handler(struct atenl *an, u8 phy_idx, char *cmd); |
| 386 | u16 atenl_get_center_channel(u8 bw, u8 ch_band, u16 ctrl_ch); |
| 387 | int atenl_reg_read(struct atenl *an, u32 offset, u32 *res); |
| 388 | int atenl_reg_write(struct atenl *an, u32 offset, u32 val); |
developer | 5698c9c | 2022-05-30 16:40:23 +0800 | [diff] [blame] | 389 | int atenl_rf_read(struct atenl *an, u32 wf_sel, u32 offset, u32 *res); |
| 390 | 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] | 391 | |
| 392 | #endif |