1 /* SPDX-License-Identifier: ISC */ 2 /* Copyright (C) 2020 MediaTek Inc. */ 3 4 #ifndef __MT7921_H 5 #define __MT7921_H 6 7 #include <linux/interrupt.h> 8 #include <linux/ktime.h> 9 #include "../mt76_connac_mcu.h" 10 #include "regs.h" 11 #include "acpi_sar.h" 12 13 #define MT7921_MAX_INTERFACES 4 14 #define MT7921_WTBL_SIZE 20 15 #define MT7921_WTBL_RESERVED (MT7921_WTBL_SIZE - 1) 16 #define MT7921_WTBL_STA (MT7921_WTBL_RESERVED - \ 17 MT7921_MAX_INTERFACES) 18 19 #define MT7921_PM_TIMEOUT (HZ / 12) 20 #define MT7921_HW_SCAN_TIMEOUT (HZ / 10) 21 #define MT7921_WATCHDOG_TIME (HZ / 4) 22 23 #define MT7921_TX_RING_SIZE 2048 24 #define MT7921_TX_MCU_RING_SIZE 256 25 #define MT7921_TX_FWDL_RING_SIZE 128 26 27 #define MT7921_RX_RING_SIZE 1536 28 #define MT7921_RX_MCU_RING_SIZE 512 29 30 #define MT7921_DRV_OWN_RETRY_COUNT 10 31 #define MT7921_MCU_INIT_RETRY_COUNT 10 32 #define MT7921_WFSYS_INIT_RETRY_COUNT 2 33 34 #define MT7921_FW_TAG_FEATURE 4 35 #define MT7921_FW_CAP_CNM BIT(7) 36 37 #define MT7921_FIRMWARE_WM "mediatek/WIFI_RAM_CODE_MT7961_1.bin" 38 #define MT7921_ROM_PATCH "mediatek/WIFI_MT7961_patch_mcu_1_2_hdr.bin" 39 40 #define MT7922_FIRMWARE_WM "mediatek/WIFI_RAM_CODE_MT7922_1.bin" 41 #define MT7922_ROM_PATCH "mediatek/WIFI_MT7922_patch_mcu_1_1_hdr.bin" 42 43 #define MT7921_EEPROM_SIZE 3584 44 #define MT7921_TOKEN_SIZE 8192 45 46 #define MT7921_EEPROM_BLOCK_SIZE 16 47 48 #define MT7921_CFEND_RATE_DEFAULT 0x49 /* OFDM 24M */ 49 #define MT7921_CFEND_RATE_11B 0x03 /* 11B LP, 11M */ 50 51 #define MT7921_SKU_RATE_NUM 161 52 #define MT7921_SKU_MAX_DELTA_IDX MT7921_SKU_RATE_NUM 53 #define MT7921_SKU_TABLE_SIZE (MT7921_SKU_RATE_NUM + 1) 54 55 #define MT7921_SDIO_HDR_TX_BYTES GENMASK(15, 0) 56 #define MT7921_SDIO_HDR_PKT_TYPE GENMASK(17, 16) 57 58 #define MCU_UNI_EVENT_ROC 0x27 59 60 enum { 61 UNI_ROC_ACQUIRE, 62 UNI_ROC_ABORT, 63 UNI_ROC_NUM 64 }; 65 66 enum mt7921_roc_req { 67 MT7921_ROC_REQ_JOIN, 68 MT7921_ROC_REQ_ROC, 69 MT7921_ROC_REQ_NUM 70 }; 71 72 enum { 73 UNI_EVENT_ROC_GRANT = 0, 74 UNI_EVENT_ROC_TAG_NUM 75 }; 76 77 struct mt7921_realease_info { 78 __le16 len; 79 u8 pad_len; 80 u8 tag; 81 } __packed; 82 83 struct mt7921_fw_features { 84 u8 segment; 85 u8 data; 86 u8 rsv[14]; 87 } __packed; 88 89 struct mt7921_roc_grant_tlv { 90 __le16 tag; 91 __le16 len; 92 u8 bss_idx; 93 u8 tokenid; 94 u8 status; 95 u8 primarychannel; 96 u8 rfsco; 97 u8 rfband; 98 u8 channelwidth; 99 u8 centerfreqseg1; 100 u8 centerfreqseg2; 101 u8 reqtype; 102 u8 dbdcband; 103 u8 rsv[1]; 104 __le32 max_interval; 105 } __packed; 106 107 enum mt7921_sdio_pkt_type { 108 MT7921_SDIO_TXD, 109 MT7921_SDIO_DATA, 110 MT7921_SDIO_CMD, 111 MT7921_SDIO_FWDL, 112 }; 113 114 struct mt7921_sdio_intr { 115 u32 isr; 116 struct { 117 u32 wtqcr[16]; 118 } tx; 119 struct { 120 u16 num[2]; 121 u16 len0[16]; 122 u16 len1[128]; 123 } rx; 124 u32 rec_mb[2]; 125 } __packed; 126 127 #define to_rssi(field, rxv) ((FIELD_GET(field, rxv) - 220) / 2) 128 #define to_rcpi(rssi) (2 * (rssi) + 220) 129 130 struct mt792x_vif; 131 struct mt792x_sta; 132 133 enum mt7921_txq_id { 134 MT7921_TXQ_BAND0, 135 MT7921_TXQ_BAND1, 136 MT7921_TXQ_FWDL = 16, 137 MT7921_TXQ_MCU_WM, 138 }; 139 140 enum mt7921_rxq_id { 141 MT7921_RXQ_BAND0 = 0, 142 MT7921_RXQ_BAND1, 143 MT7921_RXQ_MCU_WM = 0, 144 }; 145 146 DECLARE_EWMA(avg_signal, 10, 8) 147 148 struct mt792x_sta { 149 struct mt76_wcid wcid; /* must be first */ 150 151 struct mt792x_vif *vif; 152 153 u32 airtime_ac[8]; 154 155 int ack_signal; 156 struct ewma_avg_signal avg_ack_signal; 157 158 unsigned long last_txs; 159 160 struct mt76_connac_sta_key_conf bip; 161 }; 162 163 DECLARE_EWMA(rssi, 10, 8); 164 165 struct mt792x_vif { 166 struct mt76_vif mt76; /* must be first */ 167 168 struct mt792x_sta sta; 169 struct mt792x_sta *wep_sta; 170 171 struct mt792x_phy *phy; 172 173 struct ewma_rssi rssi; 174 175 struct ieee80211_tx_queue_params queue_params[IEEE80211_NUM_ACS]; 176 struct ieee80211_chanctx_conf *ctx; 177 }; 178 179 enum { 180 MT7921_CLC_POWER, 181 MT7921_CLC_CHAN, 182 MT7921_CLC_MAX_NUM, 183 }; 184 185 struct mt7921_clc_rule { 186 u8 alpha2[2]; 187 u8 type[2]; 188 __le16 len; 189 u8 data[]; 190 } __packed; 191 192 struct mt7921_clc { 193 __le32 len; 194 u8 idx; 195 u8 ver; 196 u8 nr_country; 197 u8 type; 198 u8 rsv[8]; 199 u8 data[]; 200 } __packed; 201 202 struct mt792x_phy { 203 struct mt76_phy *mt76; 204 struct mt792x_dev *dev; 205 206 struct ieee80211_sband_iftype_data iftype[NUM_NL80211_BANDS][NUM_NL80211_IFTYPES]; 207 208 u64 omac_mask; 209 210 u16 noise; 211 212 s16 coverage_class; 213 u8 slottime; 214 215 u32 rx_ampdu_ts; 216 u32 ampdu_ref; 217 218 struct mt76_mib_stats mib; 219 220 u8 sta_work_count; 221 222 struct sk_buff_head scan_event_list; 223 struct delayed_work scan_work; 224 #ifdef CONFIG_ACPI 225 void *acpisar; 226 #endif 227 void *clc[MT7921_CLC_MAX_NUM]; 228 229 struct work_struct roc_work; 230 struct timer_list roc_timer; 231 wait_queue_head_t roc_wait; 232 u8 roc_token_id; 233 bool roc_grant; 234 }; 235 236 enum mt7921_eeprom_field { 237 MT_EE_CHIP_ID = 0x000, 238 MT_EE_VERSION = 0x002, 239 MT_EE_MAC_ADDR = 0x004, 240 MT_EE_WIFI_CONF = 0x07c, 241 MT_EE_HW_TYPE = 0x55b, 242 __MT_EE_MAX = 0x9ff 243 }; 244 245 #define MT_EE_HW_TYPE_ENCAP BIT(0) 246 247 #define mt7921_init_reset(dev) ((dev)->hif_ops->init_reset(dev)) 248 #define mt7921_dev_reset(dev) ((dev)->hif_ops->reset(dev)) 249 #define mt7921_mcu_init(dev) ((dev)->hif_ops->mcu_init(dev)) 250 #define __mt7921_mcu_drv_pmctrl(dev) ((dev)->hif_ops->drv_own(dev)) 251 #define __mt7921_mcu_fw_pmctrl(dev) ((dev)->hif_ops->fw_own(dev)) 252 struct mt7921_hif_ops { 253 int (*init_reset)(struct mt792x_dev *dev); 254 int (*reset)(struct mt792x_dev *dev); 255 int (*mcu_init)(struct mt792x_dev *dev); 256 int (*drv_own)(struct mt792x_dev *dev); 257 int (*fw_own)(struct mt792x_dev *dev); 258 }; 259 260 struct mt792x_dev { 261 union { /* must be first */ 262 struct mt76_dev mt76; 263 struct mt76_phy mphy; 264 }; 265 266 const struct mt76_bus_ops *bus_ops; 267 struct mt792x_phy phy; 268 269 struct work_struct reset_work; 270 bool hw_full_reset:1; 271 bool hw_init_done:1; 272 bool fw_assert:1; 273 274 struct work_struct init_work; 275 276 u8 fw_debug; 277 u8 fw_features; 278 279 struct mt76_connac_pm pm; 280 struct mt76_connac_coredump coredump; 281 const struct mt7921_hif_ops *hif_ops; 282 283 struct work_struct ipv6_ns_work; 284 /* IPv6 addresses for WoWLAN */ 285 struct sk_buff_head ipv6_ns_list; 286 287 enum environment_cap country_ie_env; 288 }; 289 290 enum { 291 TXPWR_USER, 292 TXPWR_EEPROM, 293 TXPWR_MAC, 294 TXPWR_MAX_NUM, 295 }; 296 297 struct mt7921_txpwr { 298 u8 ch; 299 u8 rsv[3]; 300 struct { 301 u8 ch; 302 u8 cck[4]; 303 u8 ofdm[8]; 304 u8 ht20[8]; 305 u8 ht40[9]; 306 u8 vht20[12]; 307 u8 vht40[12]; 308 u8 vht80[12]; 309 u8 vht160[12]; 310 u8 he26[12]; 311 u8 he52[12]; 312 u8 he106[12]; 313 u8 he242[12]; 314 u8 he484[12]; 315 u8 he996[12]; 316 u8 he996x2[12]; 317 } data[TXPWR_MAX_NUM]; 318 }; 319 320 static inline struct mt792x_phy * 321 mt7921_hw_phy(struct ieee80211_hw *hw) 322 { 323 struct mt76_phy *phy = hw->priv; 324 325 return phy->priv; 326 } 327 328 static inline struct mt792x_dev * 329 mt7921_hw_dev(struct ieee80211_hw *hw) 330 { 331 struct mt76_phy *phy = hw->priv; 332 333 return container_of(phy->dev, struct mt792x_dev, mt76); 334 } 335 336 #define mt7921_mutex_acquire(dev) \ 337 mt76_connac_mutex_acquire(&(dev)->mt76, &(dev)->pm) 338 #define mt7921_mutex_release(dev) \ 339 mt76_connac_mutex_release(&(dev)->mt76, &(dev)->pm) 340 341 extern const struct ieee80211_ops mt7921_ops; 342 343 u32 mt7921_reg_map(struct mt792x_dev *dev, u32 addr); 344 345 int __mt7921_start(struct mt792x_phy *phy); 346 int mt7921_register_device(struct mt792x_dev *dev); 347 void mt7921_unregister_device(struct mt792x_dev *dev); 348 int mt7921_dma_init(struct mt792x_dev *dev); 349 int mt7921_wpdma_reset(struct mt792x_dev *dev, bool force); 350 int mt7921_wpdma_reinit_cond(struct mt792x_dev *dev); 351 void mt7921_dma_cleanup(struct mt792x_dev *dev); 352 int mt7921_run_firmware(struct mt792x_dev *dev); 353 int mt7921_mcu_set_bss_pm(struct mt792x_dev *dev, struct ieee80211_vif *vif, 354 bool enable); 355 int mt7921_mcu_sta_update(struct mt792x_dev *dev, struct ieee80211_sta *sta, 356 struct ieee80211_vif *vif, bool enable, 357 enum mt76_sta_info_state state); 358 int mt7921_mcu_set_chan_info(struct mt792x_phy *phy, int cmd); 359 int mt7921_mcu_set_tx(struct mt792x_dev *dev, struct ieee80211_vif *vif); 360 int mt7921_mcu_set_eeprom(struct mt792x_dev *dev); 361 int mt7921_mcu_get_rx_rate(struct mt792x_phy *phy, struct ieee80211_vif *vif, 362 struct ieee80211_sta *sta, struct rate_info *rate); 363 int mt7921_mcu_fw_log_2_host(struct mt792x_dev *dev, u8 ctrl); 364 void mt7921_mcu_rx_event(struct mt792x_dev *dev, struct sk_buff *skb); 365 int mt7921_mcu_set_rxfilter(struct mt792x_dev *dev, u32 fif, 366 u8 bit_op, u32 bit_map); 367 368 static inline u32 369 mt7921_reg_map_l1(struct mt792x_dev *dev, u32 addr) 370 { 371 u32 offset = FIELD_GET(MT_HIF_REMAP_L1_OFFSET, addr); 372 u32 base = FIELD_GET(MT_HIF_REMAP_L1_BASE, addr); 373 374 mt76_rmw_field(dev, MT_HIF_REMAP_L1, MT_HIF_REMAP_L1_MASK, base); 375 /* use read to push write */ 376 mt76_rr(dev, MT_HIF_REMAP_L1); 377 378 return MT_HIF_REMAP_BASE_L1 + offset; 379 } 380 381 static inline u32 382 mt7921_l1_rr(struct mt792x_dev *dev, u32 addr) 383 { 384 return mt76_rr(dev, mt7921_reg_map_l1(dev, addr)); 385 } 386 387 static inline void 388 mt7921_l1_wr(struct mt792x_dev *dev, u32 addr, u32 val) 389 { 390 mt76_wr(dev, mt7921_reg_map_l1(dev, addr), val); 391 } 392 393 static inline u32 394 mt7921_l1_rmw(struct mt792x_dev *dev, u32 addr, u32 mask, u32 val) 395 { 396 val |= mt7921_l1_rr(dev, addr) & ~mask; 397 mt7921_l1_wr(dev, addr, val); 398 399 return val; 400 } 401 402 #define mt7921_l1_set(dev, addr, val) mt7921_l1_rmw(dev, addr, 0, val) 403 #define mt7921_l1_clear(dev, addr, val) mt7921_l1_rmw(dev, addr, val, 0) 404 405 static inline bool mt7921_dma_need_reinit(struct mt792x_dev *dev) 406 { 407 return !mt76_get_field(dev, MT_WFDMA_DUMMY_CR, MT_WFDMA_NEED_REINIT); 408 } 409 410 static inline void 411 mt7921_skb_add_usb_sdio_hdr(struct mt792x_dev *dev, struct sk_buff *skb, 412 int type) 413 { 414 u32 hdr, len; 415 416 len = mt76_is_usb(&dev->mt76) ? skb->len : skb->len + sizeof(hdr); 417 hdr = FIELD_PREP(MT7921_SDIO_HDR_TX_BYTES, len) | 418 FIELD_PREP(MT7921_SDIO_HDR_PKT_TYPE, type); 419 420 put_unaligned_le32(hdr, skb_push(skb, sizeof(hdr))); 421 } 422 423 void mt7921_stop(struct ieee80211_hw *hw); 424 int mt7921_mac_init(struct mt792x_dev *dev); 425 bool mt7921_mac_wtbl_update(struct mt792x_dev *dev, int idx, u32 mask); 426 void mt7921_mac_reset_counters(struct mt792x_phy *phy); 427 void mt7921_mac_set_timing(struct mt792x_phy *phy); 428 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif, 429 struct ieee80211_sta *sta); 430 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif, 431 struct ieee80211_sta *sta); 432 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif, 433 struct ieee80211_sta *sta); 434 void mt7921_mac_work(struct work_struct *work); 435 void mt7921_mac_reset_work(struct work_struct *work); 436 void mt7921_mac_update_mib_stats(struct mt792x_phy *phy); 437 void mt7921_reset(struct mt76_dev *mdev); 438 int mt7921e_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr, 439 enum mt76_txq_id qid, struct mt76_wcid *wcid, 440 struct ieee80211_sta *sta, 441 struct mt76_tx_info *tx_info); 442 443 void mt7921_tx_worker(struct mt76_worker *w); 444 bool mt7921_rx_check(struct mt76_dev *mdev, void *data, int len); 445 void mt7921_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q, 446 struct sk_buff *skb, u32 *info); 447 void mt7921_stats_work(struct work_struct *work); 448 void mt7921_set_stream_he_caps(struct mt792x_phy *phy); 449 void mt7921_update_channel(struct mt76_phy *mphy); 450 int mt7921_init_debugfs(struct mt792x_dev *dev); 451 452 int mt7921_mcu_set_beacon_filter(struct mt792x_dev *dev, 453 struct ieee80211_vif *vif, 454 bool enable); 455 int mt7921_mcu_uni_tx_ba(struct mt792x_dev *dev, 456 struct ieee80211_ampdu_params *params, 457 bool enable); 458 int mt7921_mcu_uni_rx_ba(struct mt792x_dev *dev, 459 struct ieee80211_ampdu_params *params, 460 bool enable); 461 void mt7921_scan_work(struct work_struct *work); 462 void mt7921_roc_work(struct work_struct *work); 463 void mt7921_roc_timer(struct timer_list *timer); 464 int mt7921_mcu_uni_bss_ps(struct mt792x_dev *dev, struct ieee80211_vif *vif); 465 int mt7921_mcu_drv_pmctrl(struct mt792x_dev *dev); 466 int mt7921_mcu_fw_pmctrl(struct mt792x_dev *dev); 467 void mt7921_pm_wake_work(struct work_struct *work); 468 void mt7921_pm_power_save_work(struct work_struct *work); 469 void mt7921_coredump_work(struct work_struct *work); 470 int mt7921_wfsys_reset(struct mt792x_dev *dev); 471 int mt7921_get_txpwr_info(struct mt792x_dev *dev, struct mt7921_txpwr *txpwr); 472 int mt7921_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 473 void *data, int len); 474 int mt7921_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *msg, 475 struct netlink_callback *cb, void *data, int len); 476 int mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd, 477 struct sk_buff *skb, int seq); 478 479 int mt7921e_driver_own(struct mt792x_dev *dev); 480 int mt7921e_mac_reset(struct mt792x_dev *dev); 481 int mt7921e_mcu_init(struct mt792x_dev *dev); 482 int mt7921s_wfsys_reset(struct mt792x_dev *dev); 483 int mt7921s_mac_reset(struct mt792x_dev *dev); 484 int mt7921s_init_reset(struct mt792x_dev *dev); 485 int __mt7921e_mcu_drv_pmctrl(struct mt792x_dev *dev); 486 int mt7921e_mcu_drv_pmctrl(struct mt792x_dev *dev); 487 int mt7921e_mcu_fw_pmctrl(struct mt792x_dev *dev); 488 489 int mt7921s_mcu_init(struct mt792x_dev *dev); 490 int mt7921s_mcu_drv_pmctrl(struct mt792x_dev *dev); 491 int mt7921s_mcu_fw_pmctrl(struct mt792x_dev *dev); 492 void mt7921_mac_add_txs(struct mt792x_dev *dev, void *data); 493 void mt7921_set_runtime_pm(struct mt792x_dev *dev); 494 void mt7921_mcu_set_suspend_iter(void *priv, u8 *mac, 495 struct ieee80211_vif *vif); 496 void mt7921_set_ipv6_ns_work(struct work_struct *work); 497 498 int mt7921_mcu_set_sniffer(struct mt792x_dev *dev, struct ieee80211_vif *vif, 499 bool enable); 500 int mt7921_mcu_config_sniffer(struct mt792x_vif *vif, 501 struct ieee80211_chanctx_conf *ctx); 502 int mt7921_mcu_get_temperature(struct mt792x_phy *phy); 503 504 int mt7921_usb_sdio_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr, 505 enum mt76_txq_id qid, struct mt76_wcid *wcid, 506 struct ieee80211_sta *sta, 507 struct mt76_tx_info *tx_info); 508 void mt7921_usb_sdio_tx_complete_skb(struct mt76_dev *mdev, 509 struct mt76_queue_entry *e); 510 bool mt7921_usb_sdio_tx_status_data(struct mt76_dev *mdev, u8 *update); 511 512 /* usb */ 513 #define MT_USB_TYPE_VENDOR (USB_TYPE_VENDOR | 0x1f) 514 #define MT_USB_TYPE_UHW_VENDOR (USB_TYPE_VENDOR | 0x1e) 515 516 int mt7921u_mcu_power_on(struct mt792x_dev *dev); 517 int mt7921u_wfsys_reset(struct mt792x_dev *dev); 518 int mt7921u_dma_init(struct mt792x_dev *dev, bool resume); 519 int mt7921u_init_reset(struct mt792x_dev *dev); 520 int mt7921u_mac_reset(struct mt792x_dev *dev); 521 int mt7921_mcu_uni_add_beacon_offload(struct mt792x_dev *dev, 522 struct ieee80211_hw *hw, 523 struct ieee80211_vif *vif, 524 bool enable); 525 #ifdef CONFIG_ACPI 526 int mt7921_init_acpi_sar(struct mt792x_dev *dev); 527 int mt7921_init_acpi_sar_power(struct mt792x_phy *phy, bool set_default); 528 u8 mt7921_acpi_get_flags(struct mt792x_phy *phy); 529 #else 530 static inline int 531 mt7921_init_acpi_sar(struct mt792x_dev *dev) 532 { 533 return 0; 534 } 535 536 static inline int 537 mt7921_init_acpi_sar_power(struct mt792x_phy *phy, bool set_default) 538 { 539 return 0; 540 } 541 542 static inline u8 543 mt7921_acpi_get_flags(struct mt792x_phy *phy) 544 { 545 return 0; 546 } 547 #endif 548 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw, 549 const struct cfg80211_sar_specs *sar); 550 551 int mt7921_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2, 552 enum environment_cap env_cap); 553 int mt7921_mcu_set_roc(struct mt792x_phy *phy, struct mt792x_vif *vif, 554 struct ieee80211_channel *chan, int duration, 555 enum mt7921_roc_req type, u8 token_id); 556 int mt7921_mcu_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif, 557 u8 token_id); 558 struct ieee80211_ops *mt7921_get_mac80211_ops(struct device *dev, 559 void *drv_data, u8 *fw_features); 560 #endif 561