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