1 /* SPDX-License-Identifier: BSD-3-Clause-Clear */ 2 /* Copyright (C) 2023 MediaTek Inc. */ 3 4 #ifndef __MT792X_H 5 #define __MT792X_H 6 7 #include <linux/interrupt.h> 8 #include <linux/ktime.h> 9 10 #include "mt76_connac_mcu.h" 11 #include "mt792x_regs.h" 12 #include "mt792x_acpi_sar.h" 13 14 #define MT792x_PM_TIMEOUT (HZ / 12) 15 #define MT792x_HW_SCAN_TIMEOUT (HZ / 10) 16 17 #define MT792x_MAX_INTERFACES 4 18 #define MT792x_WTBL_SIZE 20 19 #define MT792x_WTBL_RESERVED (MT792x_WTBL_SIZE - 1) 20 #define MT792x_WTBL_STA (MT792x_WTBL_RESERVED - MT792x_MAX_INTERFACES) 21 22 #define MT792x_CFEND_RATE_DEFAULT 0x49 /* OFDM 24M */ 23 #define MT792x_CFEND_RATE_11B 0x03 /* 11B LP, 11M */ 24 25 #define MT792x_FW_TAG_FEATURE 4 26 #define MT792x_FW_CAP_CNM BIT(7) 27 28 #define MT792x_CHIP_CAP_CLC_EVT_EN BIT(0) 29 #define MT792x_CHIP_CAP_RSSI_NOTIFY_EVT_EN BIT(1) 30 #define MT792x_CHIP_CAP_WF_RF_PIN_CTRL_EVT_EN BIT(3) 31 #define MT792x_CHIP_CAP_11D_EN BIT(4) 32 #define MT792x_CHIP_CAP_MLO_EN BIT(8) 33 #define MT792x_CHIP_CAP_MLO_EML_EN BIT(9) 34 35 /* NOTE: used to map mt76_rates. idx may change if firmware expands table */ 36 #define MT792x_BASIC_RATES_TBL 11 37 38 #define MT792x_WATCHDOG_TIME (HZ / 4) 39 40 #define MT792x_DRV_OWN_RETRY_COUNT 10 41 #define MT792x_MCU_INIT_RETRY_COUNT 10 42 #define MT792x_WFSYS_INIT_RETRY_COUNT 2 43 44 #define MT7902_FIRMWARE_WM "mediatek/WIFI_RAM_CODE_MT7902_1.bin" 45 #define MT7920_FIRMWARE_WM "mediatek/WIFI_RAM_CODE_MT7961_1a.bin" 46 #define MT7921_FIRMWARE_WM "mediatek/WIFI_RAM_CODE_MT7961_1.bin" 47 #define MT7922_FIRMWARE_WM "mediatek/WIFI_RAM_CODE_MT7922_1.bin" 48 #define MT7925_FIRMWARE_WM "mediatek/mt7925/WIFI_RAM_CODE_MT7925_1_1.bin" 49 50 #define MT7902_ROM_PATCH "mediatek/WIFI_MT7902_patch_mcu_1_1_hdr.bin" 51 #define MT7920_ROM_PATCH "mediatek/WIFI_MT7961_patch_mcu_1a_2_hdr.bin" 52 #define MT7921_ROM_PATCH "mediatek/WIFI_MT7961_patch_mcu_1_2_hdr.bin" 53 #define MT7922_ROM_PATCH "mediatek/WIFI_MT7922_patch_mcu_1_1_hdr.bin" 54 #define MT7925_ROM_PATCH "mediatek/mt7925/WIFI_MT7925_PATCH_MCU_1_1_hdr.bin" 55 56 #define MT792x_SDIO_HDR_TX_BYTES GENMASK(15, 0) 57 #define MT792x_SDIO_HDR_PKT_TYPE GENMASK(17, 16) 58 59 struct mt792x_vif; 60 struct mt792x_sta; 61 62 struct mt792x_realease_info { 63 __le16 len; 64 u8 pad_len; 65 u8 tag; 66 } __packed; 67 68 struct mt792x_fw_features { 69 u8 segment; 70 u8 data; 71 u8 rsv[14]; 72 } __packed; 73 74 enum { 75 MT792x_CLC_POWER, 76 MT792x_CLC_POWER_EXT, 77 MT792x_CLC_BE_CTRL, 78 MT792x_CLC_MAX_NUM, 79 }; 80 81 enum mt792x_reg_power_type { 82 MT_AP_UNSET = 0, 83 MT_AP_DEFAULT, 84 MT_AP_LPI, 85 MT_AP_SP, 86 MT_AP_VLP, 87 }; 88 89 enum mt792x_mlo_pm_state { 90 MT792x_MLO_LINK_DISASSOC, 91 MT792x_MLO_LINK_ASSOC, 92 MT792x_MLO_CHANGED_PS_PENDING, 93 MT792x_MLO_CHANGED_PS, 94 }; 95 96 DECLARE_EWMA(avg_signal, 10, 8) 97 98 struct mt792x_link_sta { 99 struct mt76_wcid wcid; /* must be first */ 100 struct rcu_head rcu_head; 101 102 u32 airtime_ac[8]; 103 104 int ack_signal; 105 struct ewma_avg_signal avg_ack_signal; 106 107 unsigned long last_txs; 108 109 struct mt76_connac_sta_key_conf bip; 110 111 struct mt792x_sta *sta; 112 113 struct ieee80211_link_sta *pri_link; 114 }; 115 116 struct mt792x_sta { 117 struct mt792x_link_sta deflink; /* must be first */ 118 struct mt792x_link_sta __rcu *link[IEEE80211_MLD_MAX_NUM_LINKS]; 119 120 struct mt792x_vif *vif; 121 122 u16 valid_links; 123 u8 deflink_id; 124 }; 125 126 DECLARE_EWMA(rssi, 10, 8); 127 128 struct mt792x_chanctx { 129 struct mt792x_bss_conf *bss_conf; 130 }; 131 132 struct mt792x_bss_conf { 133 struct mt76_vif_link mt76; /* must be first */ 134 struct mt792x_vif *vif; 135 struct ewma_rssi rssi; 136 struct ieee80211_tx_queue_params queue_params[IEEE80211_NUM_ACS]; 137 unsigned int link_id; 138 }; 139 140 struct mt792x_vif { 141 struct mt792x_bss_conf bss_conf; /* must be first */ 142 struct mt792x_bss_conf __rcu *link_conf[IEEE80211_MLD_MAX_NUM_LINKS]; 143 144 struct mt792x_sta sta; 145 struct mt792x_sta *wep_sta; 146 147 struct mt792x_phy *phy; 148 u16 valid_links; 149 u8 deflink_id; 150 enum mt792x_mlo_pm_state mlo_pm_state; 151 152 struct work_struct csa_work; 153 struct timer_list csa_timer; 154 }; 155 156 struct mt792x_phy { 157 struct mt76_phy *mt76; 158 struct mt792x_dev *dev; 159 160 struct ieee80211_sband_iftype_data iftype[NUM_NL80211_BANDS][NUM_NL80211_IFTYPES]; 161 162 u64 omac_mask; 163 164 u16 noise; 165 166 s16 coverage_class; 167 u8 slottime; 168 169 u32 rx_ampdu_ts; 170 u32 ampdu_ref; 171 172 struct mt76_mib_stats mib; 173 174 u8 sta_work_count; 175 u8 clc_chan_conf; 176 enum mt792x_reg_power_type power_type; 177 178 struct sk_buff_head scan_event_list; 179 struct delayed_work scan_work; 180 #ifdef CONFIG_ACPI 181 void *acpisar; 182 #endif 183 void *clc[MT792x_CLC_MAX_NUM]; 184 u64 chip_cap; 185 u16 eml_cap; 186 187 struct work_struct roc_work; 188 struct timer_list roc_timer; 189 wait_queue_head_t roc_wait; 190 u8 roc_token_id; 191 bool roc_grant; 192 }; 193 194 struct mt792x_irq_map { 195 u32 host_irq_enable; 196 struct { 197 u32 all_complete_mask; 198 u32 mcu_complete_mask; 199 } tx; 200 struct { 201 u32 data_complete_mask; 202 u32 wm_complete_mask; 203 u32 wm2_complete_mask; 204 } rx; 205 }; 206 207 #define mt792x_init_reset(dev) ((dev)->hif_ops->init_reset(dev)) 208 #define mt792x_dev_reset(dev) ((dev)->hif_ops->reset(dev)) 209 #define mt792x_mcu_init(dev) ((dev)->hif_ops->mcu_init(dev)) 210 #define __mt792x_mcu_drv_pmctrl(dev) ((dev)->hif_ops->drv_own(dev)) 211 #define __mt792x_mcu_fw_pmctrl(dev) ((dev)->hif_ops->fw_own(dev)) 212 213 struct mt792x_hif_ops { 214 int (*init_reset)(struct mt792x_dev *dev); 215 int (*reset)(struct mt792x_dev *dev); 216 int (*mcu_init)(struct mt792x_dev *dev); 217 int (*drv_own)(struct mt792x_dev *dev); 218 int (*fw_own)(struct mt792x_dev *dev); 219 }; 220 221 struct mt792x_dev { 222 union { /* must be first */ 223 struct mt76_dev mt76; 224 struct mt76_phy mphy; 225 }; 226 227 struct mac_address macaddr_list[8]; 228 229 const struct mt76_bus_ops *bus_ops; 230 struct mt792x_phy phy; 231 232 struct work_struct reset_work; 233 bool hw_full_reset:1; 234 bool hw_init_done:1; 235 bool fw_assert:1; 236 bool has_eht:1; 237 bool regd_user:1; 238 bool regd_in_progress:1; 239 bool aspm_supported:1; 240 bool hif_idle:1; 241 bool hif_resumed:1; 242 bool regd_change:1; 243 wait_queue_head_t wait; 244 245 struct work_struct init_work; 246 247 u8 fw_debug; 248 u8 fw_features; 249 250 struct mt76_connac_pm pm; 251 struct mt76_connac_coredump coredump; 252 const struct mt792x_hif_ops *hif_ops; 253 const struct mt792x_irq_map *irq_map; 254 255 struct work_struct ipv6_ns_work; 256 struct delayed_work mlo_pm_work; 257 /* IPv6 addresses for WoWLAN */ 258 struct sk_buff_head ipv6_ns_list; 259 260 enum environment_cap country_ie_env; 261 u32 backup_l1; 262 u32 backup_l2; 263 264 struct ieee80211_chanctx_conf *new_ctx; 265 }; 266 267 static inline struct mt792x_bss_conf * 268 mt792x_vif_to_link(struct mt792x_vif *mvif, u8 link_id) 269 { 270 struct ieee80211_vif *vif; 271 struct mt792x_bss_conf *bss_conf; 272 273 vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv); 274 275 if (!ieee80211_vif_is_mld(vif) || 276 link_id >= IEEE80211_LINK_UNSPECIFIED) 277 return &mvif->bss_conf; 278 279 bss_conf = rcu_dereference_protected(mvif->link_conf[link_id], 280 lockdep_is_held(&mvif->phy->dev->mt76.mutex)); 281 282 return bss_conf ? bss_conf : &mvif->bss_conf; 283 } 284 285 static inline struct mt792x_link_sta * 286 mt792x_sta_to_link(struct mt792x_sta *msta, u8 link_id) 287 { 288 struct ieee80211_vif *vif; 289 290 vif = container_of((void *)msta->vif, struct ieee80211_vif, drv_priv); 291 292 if (!ieee80211_vif_is_mld(vif) || 293 link_id >= IEEE80211_LINK_UNSPECIFIED) 294 return &msta->deflink; 295 296 return rcu_dereference_protected(msta->link[link_id], 297 lockdep_is_held(&msta->vif->phy->dev->mt76.mutex)); 298 } 299 300 static inline struct mt792x_bss_conf * 301 mt792x_link_conf_to_mconf(struct ieee80211_bss_conf *link_conf) 302 { 303 struct ieee80211_vif *vif = link_conf->vif; 304 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 305 306 return mt792x_vif_to_link(mvif, link_conf->link_id); 307 } 308 309 static inline struct ieee80211_bss_conf * 310 mt792x_vif_to_bss_conf(struct ieee80211_vif *vif, unsigned int link_id) 311 { 312 if (!ieee80211_vif_is_mld(vif) || 313 link_id >= IEEE80211_LINK_UNSPECIFIED) 314 return &vif->bss_conf; 315 316 return link_conf_dereference_protected(vif, link_id); 317 } 318 319 static inline struct ieee80211_link_sta * 320 mt792x_sta_to_link_sta(struct ieee80211_vif *vif, struct ieee80211_sta *sta, 321 unsigned int link_id) 322 { 323 if (!ieee80211_vif_is_mld(vif) || 324 link_id >= IEEE80211_LINK_UNSPECIFIED) 325 return &sta->deflink; 326 327 return link_sta_dereference_protected(sta, link_id); 328 } 329 330 static inline struct mt792x_dev * 331 mt792x_hw_dev(struct ieee80211_hw *hw) 332 { 333 struct mt76_phy *phy = hw->priv; 334 335 return container_of(phy->dev, struct mt792x_dev, mt76); 336 } 337 338 static inline struct mt792x_phy * 339 mt792x_hw_phy(struct ieee80211_hw *hw) 340 { 341 struct mt76_phy *phy = hw->priv; 342 343 return phy->priv; 344 } 345 346 static inline void 347 mt792x_get_status_freq_info(struct mt76_rx_status *status, u8 chfreq) 348 { 349 if (chfreq > 180) { 350 status->band = NL80211_BAND_6GHZ; 351 chfreq = (chfreq - 181) * 4 + 1; 352 } else if (chfreq > 14) { 353 status->band = NL80211_BAND_5GHZ; 354 } else { 355 status->band = NL80211_BAND_2GHZ; 356 } 357 status->freq = ieee80211_channel_to_frequency(chfreq, status->band); 358 } 359 360 static inline bool mt792x_dma_need_reinit(struct mt792x_dev *dev) 361 { 362 return !mt76_get_field(dev, MT_WFDMA_DUMMY_CR, MT_WFDMA_NEED_REINIT); 363 } 364 365 #define mt792x_mutex_acquire(dev) \ 366 mt76_connac_mutex_acquire(&(dev)->mt76, &(dev)->pm) 367 #define mt792x_mutex_release(dev) \ 368 mt76_connac_mutex_release(&(dev)->mt76, &(dev)->pm) 369 370 void mt792x_stop(struct ieee80211_hw *hw, bool suspend); 371 void mt792x_pm_wake_work(struct work_struct *work); 372 void mt792x_pm_power_save_work(struct work_struct *work); 373 void mt792x_reset(struct mt76_dev *mdev); 374 void mt792x_update_channel(struct mt76_phy *mphy); 375 void mt792x_mac_reset_counters(struct mt792x_phy *phy); 376 void mt792x_mac_init_band(struct mt792x_dev *dev, u8 band); 377 void mt792x_mac_assoc_rssi(struct mt792x_dev *dev, struct sk_buff *skb); 378 struct mt76_wcid *mt792x_rx_get_wcid(struct mt792x_dev *dev, u16 idx, 379 bool unicast); 380 void mt792x_mac_update_mib_stats(struct mt792x_phy *phy); 381 void mt792x_mac_set_timeing(struct mt792x_phy *phy); 382 void mt792x_mac_work(struct work_struct *work); 383 void mt792x_remove_interface(struct ieee80211_hw *hw, 384 struct ieee80211_vif *vif); 385 void mt792x_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control, 386 struct sk_buff *skb); 387 int mt792x_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 388 unsigned int link_id, u16 queue, 389 const struct ieee80211_tx_queue_params *params); 390 int mt792x_get_stats(struct ieee80211_hw *hw, 391 struct ieee80211_low_level_stats *stats); 392 u64 mt792x_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif); 393 void mt792x_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 394 u64 timestamp); 395 void mt792x_tx_worker(struct mt76_worker *w); 396 void mt792x_roc_timer(struct timer_list *timer); 397 void mt792x_csa_timer(struct timer_list *timer); 398 void mt792x_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 399 u32 queues, bool drop); 400 int mt792x_assign_vif_chanctx(struct ieee80211_hw *hw, 401 struct ieee80211_vif *vif, 402 struct ieee80211_bss_conf *link_conf, 403 struct ieee80211_chanctx_conf *ctx); 404 void mt792x_unassign_vif_chanctx(struct ieee80211_hw *hw, 405 struct ieee80211_vif *vif, 406 struct ieee80211_bss_conf *link_conf, 407 struct ieee80211_chanctx_conf *ctx); 408 void mt792x_set_wakeup(struct ieee80211_hw *hw, bool enabled); 409 void mt792x_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 410 u32 sset, u8 *data); 411 int mt792x_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 412 int sset); 413 void mt792x_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 414 struct ethtool_stats *stats, u64 *data); 415 void mt792x_sta_statistics(struct ieee80211_hw *hw, 416 struct ieee80211_vif *vif, 417 struct ieee80211_sta *sta, 418 struct station_info *sinfo); 419 void mt792x_set_coverage_class(struct ieee80211_hw *hw, int radio_idx, 420 s16 coverage_class); 421 void mt792x_dma_cleanup(struct mt792x_dev *dev); 422 int mt792x_dma_enable(struct mt792x_dev *dev); 423 int mt792x_wpdma_reset(struct mt792x_dev *dev, bool force); 424 int mt792x_wpdma_reinit_cond(struct mt792x_dev *dev); 425 int mt792x_dma_disable(struct mt792x_dev *dev, bool force); 426 irqreturn_t mt792x_irq_handler(int irq, void *dev_instance); 427 void mt792x_rx_poll_complete(struct mt76_dev *mdev, enum mt76_rxq_id q); 428 int mt792x_poll_tx(struct napi_struct *napi, int budget); 429 int mt792x_poll_rx(struct napi_struct *napi, int budget); 430 void mt792x_irq_tasklet(unsigned long data); 431 int mt792x_wfsys_reset(struct mt792x_dev *dev); 432 int mt792x_tx_stats_show(struct seq_file *file, void *data); 433 int mt792x_queues_acq(struct seq_file *s, void *data); 434 int mt792x_queues_read(struct seq_file *s, void *data); 435 int mt792x_pm_stats(struct seq_file *s, void *data); 436 int mt792x_pm_idle_timeout_set(void *data, u64 val); 437 int mt792x_pm_idle_timeout_get(void *data, u64 *val); 438 int mt792x_init_wiphy(struct ieee80211_hw *hw); 439 struct ieee80211_ops * 440 mt792x_get_mac80211_ops(struct device *dev, 441 const struct ieee80211_ops *mac80211_ops, 442 void *drv_data, u8 *fw_features); 443 int mt792x_init_wcid(struct mt792x_dev *dev); 444 int mt792x_mcu_drv_pmctrl(struct mt792x_dev *dev); 445 int mt792x_mcu_fw_pmctrl(struct mt792x_dev *dev); 446 void mt792x_mac_link_bss_remove(struct mt792x_dev *dev, 447 struct mt792x_bss_conf *mconf, 448 struct mt792x_link_sta *mlink); 449 void mt792x_config_mac_addr_list(struct mt792x_dev *dev); 450 451 static inline char *mt792x_ram_name(struct mt792x_dev *dev) 452 { 453 switch (mt76_chip(&dev->mt76)) { 454 case 0x7902: 455 return MT7902_FIRMWARE_WM; 456 case 0x7920: 457 return MT7920_FIRMWARE_WM; 458 case 0x7922: 459 return MT7922_FIRMWARE_WM; 460 case 0x7925: 461 return MT7925_FIRMWARE_WM; 462 default: 463 return MT7921_FIRMWARE_WM; 464 } 465 } 466 467 static inline char *mt792x_patch_name(struct mt792x_dev *dev) 468 { 469 switch (mt76_chip(&dev->mt76)) { 470 case 0x7902: 471 return MT7902_ROM_PATCH; 472 case 0x7920: 473 return MT7920_ROM_PATCH; 474 case 0x7922: 475 return MT7922_ROM_PATCH; 476 case 0x7925: 477 return MT7925_ROM_PATCH; 478 default: 479 return MT7921_ROM_PATCH; 480 } 481 } 482 483 int mt792x_load_firmware(struct mt792x_dev *dev); 484 485 /* usb */ 486 #define MT_USB_TYPE_VENDOR (USB_TYPE_VENDOR | 0x1f) 487 #define MT_USB_TYPE_UHW_VENDOR (USB_TYPE_VENDOR | 0x1e) 488 int mt792xu_dma_init(struct mt792x_dev *dev, bool resume); 489 int mt792xu_mcu_power_on(struct mt792x_dev *dev); 490 int mt792xu_wfsys_reset(struct mt792x_dev *dev); 491 int mt792xu_init_reset(struct mt792x_dev *dev); 492 u32 mt792xu_rr(struct mt76_dev *dev, u32 addr); 493 void mt792xu_wr(struct mt76_dev *dev, u32 addr, u32 val); 494 u32 mt792xu_rmw(struct mt76_dev *dev, u32 addr, u32 mask, u32 val); 495 void mt792xu_copy(struct mt76_dev *dev, u32 offset, const void *data, int len); 496 void mt792xu_disconnect(struct usb_interface *usb_intf); 497 void mt792xu_stop(struct ieee80211_hw *hw, bool suspend); 498 499 static inline void 500 mt792x_skb_add_usb_sdio_hdr(struct mt792x_dev *dev, struct sk_buff *skb, 501 int type) 502 { 503 u32 hdr, len; 504 505 len = mt76_is_usb(&dev->mt76) ? skb->len : skb->len + sizeof(hdr); 506 hdr = FIELD_PREP(MT792x_SDIO_HDR_TX_BYTES, len) | 507 FIELD_PREP(MT792x_SDIO_HDR_PKT_TYPE, type); 508 509 put_unaligned_le32(hdr, skb_push(skb, sizeof(hdr))); 510 } 511 512 int __mt792xe_mcu_drv_pmctrl(struct mt792x_dev *dev); 513 int mt792xe_mcu_drv_pmctrl(struct mt792x_dev *dev); 514 int mt792xe_mcu_fw_pmctrl(struct mt792x_dev *dev); 515 516 #ifdef CONFIG_ACPI 517 int mt792x_init_acpi_sar(struct mt792x_dev *dev); 518 int mt792x_init_acpi_sar_power(struct mt792x_phy *phy, bool set_default); 519 u8 mt792x_acpi_get_flags(struct mt792x_phy *phy); 520 u32 mt792x_acpi_get_mtcl_conf(struct mt792x_phy *phy, char *alpha2); 521 #else 522 static inline int mt792x_init_acpi_sar(struct mt792x_dev *dev) 523 { 524 return 0; 525 } 526 527 static inline int mt792x_init_acpi_sar_power(struct mt792x_phy *phy, 528 bool set_default) 529 { 530 return 0; 531 } 532 533 static inline u8 mt792x_acpi_get_flags(struct mt792x_phy *phy) 534 { 535 return 0; 536 } 537 538 static inline u32 mt792x_acpi_get_mtcl_conf(struct mt792x_phy *phy, char *alpha2) 539 { 540 return MT792X_ACPI_MTCL_INVALID; 541 } 542 #endif 543 544 #endif /* __MT7925_H */ 545