1 /* SPDX-License-Identifier: ISC */
2 /* Copyright (C) 2023 MediaTek Inc. */
7 #include <linux/interrupt.h>
8 #include <linux/ktime.h>
10 #include "mt76_connac_mcu.h"
11 #include "mt792x_regs.h"
12 #include "mt792x_acpi_sar.h"
14 #define MT792x_PM_TIMEOUT (HZ / 12)
15 #define MT792x_HW_SCAN_TIMEOUT (HZ / 10)
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)
22 #define MT792x_CFEND_RATE_DEFAULT 0x49 /* OFDM 24M */
23 #define MT792x_CFEND_RATE_11B 0x03 /* 11B LP, 11M */
25 #define MT792x_FW_TAG_FEATURE 4
26 #define MT792x_FW_CAP_CNM BIT(7)
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)
32 /* NOTE: used to map mt76_rates. idx may change if firmware expands table */
33 #define MT792x_BASIC_RATES_TBL 11
35 #define MT792x_WATCHDOG_TIME (HZ / 4)
37 #define MT792x_DRV_OWN_RETRY_COUNT 10
38 #define MT792x_MCU_INIT_RETRY_COUNT 10
39 #define MT792x_WFSYS_INIT_RETRY_COUNT 2
41 #define MT7920_FIRMWARE_WM "mediatek/WIFI_RAM_CODE_MT7961_1a.bin"
42 #define MT7921_FIRMWARE_WM "mediatek/WIFI_RAM_CODE_MT7961_1.bin"
43 #define MT7922_FIRMWARE_WM "mediatek/WIFI_RAM_CODE_MT7922_1.bin"
44 #define MT7925_FIRMWARE_WM "mediatek/mt7925/WIFI_RAM_CODE_MT7925_1_1.bin"
46 #define MT7920_ROM_PATCH "mediatek/WIFI_MT7961_patch_mcu_1a_2_hdr.bin"
47 #define MT7921_ROM_PATCH "mediatek/WIFI_MT7961_patch_mcu_1_2_hdr.bin"
48 #define MT7922_ROM_PATCH "mediatek/WIFI_MT7922_patch_mcu_1_1_hdr.bin"
49 #define MT7925_ROM_PATCH "mediatek/mt7925/WIFI_MT7925_PATCH_MCU_1_1_hdr.bin"
51 #define MT792x_SDIO_HDR_TX_BYTES GENMASK(15, 0)
52 #define MT792x_SDIO_HDR_PKT_TYPE GENMASK(17, 16)
57 struct mt792x_realease_info
{
63 struct mt792x_fw_features
{
75 enum mt792x_reg_power_type
{
83 DECLARE_EWMA(avg_signal
, 10, 8)
85 struct mt792x_link_sta
{
86 struct mt76_wcid wcid
; /* must be first */
91 struct ewma_avg_signal avg_ack_signal
;
93 unsigned long last_txs
;
95 struct mt76_connac_sta_key_conf bip
;
97 struct mt792x_sta
*sta
;
99 struct ieee80211_link_sta
*pri_link
;
103 struct mt792x_link_sta deflink
; /* must be first */
104 struct mt792x_link_sta __rcu
*link
[IEEE80211_MLD_MAX_NUM_LINKS
];
106 struct mt792x_vif
*vif
;
112 DECLARE_EWMA(rssi
, 10, 8);
114 struct mt792x_chanctx
{
115 struct mt792x_bss_conf
*bss_conf
;
118 struct mt792x_bss_conf
{
119 struct mt76_vif mt76
; /* must be first */
120 struct mt792x_vif
*vif
;
121 struct ewma_rssi rssi
;
122 struct ieee80211_tx_queue_params queue_params
[IEEE80211_NUM_ACS
];
123 unsigned int link_id
;
127 struct mt792x_bss_conf bss_conf
; /* must be first */
128 struct mt792x_bss_conf __rcu
*link_conf
[IEEE80211_MLD_MAX_NUM_LINKS
];
130 struct mt792x_sta sta
;
131 struct mt792x_sta
*wep_sta
;
133 struct mt792x_phy
*phy
;
139 struct mt76_phy
*mt76
;
140 struct mt792x_dev
*dev
;
142 struct ieee80211_sband_iftype_data iftype
[NUM_NL80211_BANDS
][NUM_NL80211_IFTYPES
];
154 struct mt76_mib_stats mib
;
158 enum mt792x_reg_power_type power_type
;
160 struct sk_buff_head scan_event_list
;
161 struct delayed_work scan_work
;
165 void *clc
[MT792x_CLC_MAX_NUM
];
169 struct work_struct roc_work
;
170 struct timer_list roc_timer
;
171 wait_queue_head_t roc_wait
;
176 struct mt792x_irq_map
{
179 u32 all_complete_mask
;
180 u32 mcu_complete_mask
;
183 u32 data_complete_mask
;
184 u32 wm_complete_mask
;
185 u32 wm2_complete_mask
;
189 #define mt792x_init_reset(dev) ((dev)->hif_ops->init_reset(dev))
190 #define mt792x_dev_reset(dev) ((dev)->hif_ops->reset(dev))
191 #define mt792x_mcu_init(dev) ((dev)->hif_ops->mcu_init(dev))
192 #define __mt792x_mcu_drv_pmctrl(dev) ((dev)->hif_ops->drv_own(dev))
193 #define __mt792x_mcu_fw_pmctrl(dev) ((dev)->hif_ops->fw_own(dev))
195 struct mt792x_hif_ops
{
196 int (*init_reset
)(struct mt792x_dev
*dev
);
197 int (*reset
)(struct mt792x_dev
*dev
);
198 int (*mcu_init
)(struct mt792x_dev
*dev
);
199 int (*drv_own
)(struct mt792x_dev
*dev
);
200 int (*fw_own
)(struct mt792x_dev
*dev
);
204 union { /* must be first */
205 struct mt76_dev mt76
;
206 struct mt76_phy mphy
;
209 const struct mt76_bus_ops
*bus_ops
;
210 struct mt792x_phy phy
;
212 struct work_struct reset_work
;
213 bool hw_full_reset
:1;
217 bool regd_in_progress
:1;
218 bool aspm_supported
:1;
219 wait_queue_head_t wait
;
221 struct work_struct init_work
;
226 struct mt76_connac_pm pm
;
227 struct mt76_connac_coredump coredump
;
228 const struct mt792x_hif_ops
*hif_ops
;
229 const struct mt792x_irq_map
*irq_map
;
231 struct work_struct ipv6_ns_work
;
232 /* IPv6 addresses for WoWLAN */
233 struct sk_buff_head ipv6_ns_list
;
235 enum environment_cap country_ie_env
;
240 static inline struct mt792x_bss_conf
*
241 mt792x_vif_to_link(struct mt792x_vif
*mvif
, u8 link_id
)
243 struct ieee80211_vif
*vif
;
245 vif
= container_of((void *)mvif
, struct ieee80211_vif
, drv_priv
);
247 if (!ieee80211_vif_is_mld(vif
) ||
248 link_id
>= IEEE80211_LINK_UNSPECIFIED
)
249 return &mvif
->bss_conf
;
251 return rcu_dereference_protected(mvif
->link_conf
[link_id
],
252 lockdep_is_held(&mvif
->phy
->dev
->mt76
.mutex
));
255 static inline struct mt792x_link_sta
*
256 mt792x_sta_to_link(struct mt792x_sta
*msta
, u8 link_id
)
258 struct ieee80211_vif
*vif
;
260 vif
= container_of((void *)msta
->vif
, struct ieee80211_vif
, drv_priv
);
262 if (!ieee80211_vif_is_mld(vif
) ||
263 link_id
>= IEEE80211_LINK_UNSPECIFIED
)
264 return &msta
->deflink
;
266 return rcu_dereference_protected(msta
->link
[link_id
],
267 lockdep_is_held(&msta
->vif
->phy
->dev
->mt76
.mutex
));
270 static inline struct mt792x_bss_conf
*
271 mt792x_link_conf_to_mconf(struct ieee80211_bss_conf
*link_conf
)
273 struct ieee80211_vif
*vif
= link_conf
->vif
;
274 struct mt792x_vif
*mvif
= (struct mt792x_vif
*)vif
->drv_priv
;
276 return mt792x_vif_to_link(mvif
, link_conf
->link_id
);
279 static inline struct ieee80211_bss_conf
*
280 mt792x_vif_to_bss_conf(struct ieee80211_vif
*vif
, unsigned int link_id
)
282 if (!ieee80211_vif_is_mld(vif
) ||
283 link_id
>= IEEE80211_LINK_UNSPECIFIED
)
284 return &vif
->bss_conf
;
286 return link_conf_dereference_protected(vif
, link_id
);
289 static inline struct ieee80211_link_sta
*
290 mt792x_sta_to_link_sta(struct ieee80211_vif
*vif
, struct ieee80211_sta
*sta
,
291 unsigned int link_id
)
293 if (!ieee80211_vif_is_mld(vif
) ||
294 link_id
>= IEEE80211_LINK_UNSPECIFIED
)
295 return &sta
->deflink
;
297 return link_sta_dereference_protected(sta
, link_id
);
300 static inline struct mt792x_dev
*
301 mt792x_hw_dev(struct ieee80211_hw
*hw
)
303 struct mt76_phy
*phy
= hw
->priv
;
305 return container_of(phy
->dev
, struct mt792x_dev
, mt76
);
308 static inline struct mt792x_phy
*
309 mt792x_hw_phy(struct ieee80211_hw
*hw
)
311 struct mt76_phy
*phy
= hw
->priv
;
317 mt792x_get_status_freq_info(struct mt76_rx_status
*status
, u8 chfreq
)
320 status
->band
= NL80211_BAND_6GHZ
;
321 chfreq
= (chfreq
- 181) * 4 + 1;
322 } else if (chfreq
> 14) {
323 status
->band
= NL80211_BAND_5GHZ
;
325 status
->band
= NL80211_BAND_2GHZ
;
327 status
->freq
= ieee80211_channel_to_frequency(chfreq
, status
->band
);
330 static inline bool mt792x_dma_need_reinit(struct mt792x_dev
*dev
)
332 return !mt76_get_field(dev
, MT_WFDMA_DUMMY_CR
, MT_WFDMA_NEED_REINIT
);
335 #define mt792x_mutex_acquire(dev) \
336 mt76_connac_mutex_acquire(&(dev)->mt76, &(dev)->pm)
337 #define mt792x_mutex_release(dev) \
338 mt76_connac_mutex_release(&(dev)->mt76, &(dev)->pm)
340 void mt792x_stop(struct ieee80211_hw
*hw
, bool suspend
);
341 void mt792x_pm_wake_work(struct work_struct
*work
);
342 void mt792x_pm_power_save_work(struct work_struct
*work
);
343 void mt792x_reset(struct mt76_dev
*mdev
);
344 void mt792x_update_channel(struct mt76_phy
*mphy
);
345 void mt792x_mac_reset_counters(struct mt792x_phy
*phy
);
346 void mt792x_mac_init_band(struct mt792x_dev
*dev
, u8 band
);
347 void mt792x_mac_assoc_rssi(struct mt792x_dev
*dev
, struct sk_buff
*skb
);
348 struct mt76_wcid
*mt792x_rx_get_wcid(struct mt792x_dev
*dev
, u16 idx
,
350 void mt792x_mac_update_mib_stats(struct mt792x_phy
*phy
);
351 void mt792x_mac_set_timeing(struct mt792x_phy
*phy
);
352 void mt792x_mac_work(struct work_struct
*work
);
353 void mt792x_remove_interface(struct ieee80211_hw
*hw
,
354 struct ieee80211_vif
*vif
);
355 void mt792x_tx(struct ieee80211_hw
*hw
, struct ieee80211_tx_control
*control
,
356 struct sk_buff
*skb
);
357 int mt792x_conf_tx(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
358 unsigned int link_id
, u16 queue
,
359 const struct ieee80211_tx_queue_params
*params
);
360 int mt792x_get_stats(struct ieee80211_hw
*hw
,
361 struct ieee80211_low_level_stats
*stats
);
362 u64
mt792x_get_tsf(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
);
363 void mt792x_set_tsf(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
365 void mt792x_tx_worker(struct mt76_worker
*w
);
366 void mt792x_roc_timer(struct timer_list
*timer
);
367 void mt792x_flush(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
368 u32 queues
, bool drop
);
369 int mt792x_assign_vif_chanctx(struct ieee80211_hw
*hw
,
370 struct ieee80211_vif
*vif
,
371 struct ieee80211_bss_conf
*link_conf
,
372 struct ieee80211_chanctx_conf
*ctx
);
373 void mt792x_unassign_vif_chanctx(struct ieee80211_hw
*hw
,
374 struct ieee80211_vif
*vif
,
375 struct ieee80211_bss_conf
*link_conf
,
376 struct ieee80211_chanctx_conf
*ctx
);
377 void mt792x_set_wakeup(struct ieee80211_hw
*hw
, bool enabled
);
378 void mt792x_get_et_strings(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
380 int mt792x_get_et_sset_count(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
382 void mt792x_get_et_stats(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
383 struct ethtool_stats
*stats
, u64
*data
);
384 void mt792x_sta_statistics(struct ieee80211_hw
*hw
,
385 struct ieee80211_vif
*vif
,
386 struct ieee80211_sta
*sta
,
387 struct station_info
*sinfo
);
388 void mt792x_set_coverage_class(struct ieee80211_hw
*hw
, s16 coverage_class
);
389 void mt792x_dma_cleanup(struct mt792x_dev
*dev
);
390 int mt792x_dma_enable(struct mt792x_dev
*dev
);
391 int mt792x_wpdma_reset(struct mt792x_dev
*dev
, bool force
);
392 int mt792x_wpdma_reinit_cond(struct mt792x_dev
*dev
);
393 int mt792x_dma_disable(struct mt792x_dev
*dev
, bool force
);
394 irqreturn_t
mt792x_irq_handler(int irq
, void *dev_instance
);
395 void mt792x_rx_poll_complete(struct mt76_dev
*mdev
, enum mt76_rxq_id q
);
396 int mt792x_poll_tx(struct napi_struct
*napi
, int budget
);
397 int mt792x_poll_rx(struct napi_struct
*napi
, int budget
);
398 void mt792x_irq_tasklet(unsigned long data
);
399 int mt792x_wfsys_reset(struct mt792x_dev
*dev
);
400 int mt792x_tx_stats_show(struct seq_file
*file
, void *data
);
401 int mt792x_queues_acq(struct seq_file
*s
, void *data
);
402 int mt792x_queues_read(struct seq_file
*s
, void *data
);
403 int mt792x_pm_stats(struct seq_file
*s
, void *data
);
404 int mt792x_pm_idle_timeout_set(void *data
, u64 val
);
405 int mt792x_pm_idle_timeout_get(void *data
, u64
*val
);
406 int mt792x_init_wiphy(struct ieee80211_hw
*hw
);
407 struct ieee80211_ops
*
408 mt792x_get_mac80211_ops(struct device
*dev
,
409 const struct ieee80211_ops
*mac80211_ops
,
410 void *drv_data
, u8
*fw_features
);
411 int mt792x_init_wcid(struct mt792x_dev
*dev
);
412 int mt792x_mcu_drv_pmctrl(struct mt792x_dev
*dev
);
413 int mt792x_mcu_fw_pmctrl(struct mt792x_dev
*dev
);
414 void mt792x_mac_link_bss_remove(struct mt792x_dev
*dev
,
415 struct mt792x_bss_conf
*mconf
,
416 struct mt792x_link_sta
*mlink
);
418 static inline char *mt792x_ram_name(struct mt792x_dev
*dev
)
420 switch (mt76_chip(&dev
->mt76
)) {
422 return MT7920_FIRMWARE_WM
;
424 return MT7922_FIRMWARE_WM
;
426 return MT7925_FIRMWARE_WM
;
428 return MT7921_FIRMWARE_WM
;
432 static inline char *mt792x_patch_name(struct mt792x_dev
*dev
)
434 switch (mt76_chip(&dev
->mt76
)) {
436 return MT7920_ROM_PATCH
;
438 return MT7922_ROM_PATCH
;
440 return MT7925_ROM_PATCH
;
442 return MT7921_ROM_PATCH
;
446 int mt792x_load_firmware(struct mt792x_dev
*dev
);
449 #define MT_USB_TYPE_VENDOR (USB_TYPE_VENDOR | 0x1f)
450 #define MT_USB_TYPE_UHW_VENDOR (USB_TYPE_VENDOR | 0x1e)
451 int mt792xu_dma_init(struct mt792x_dev
*dev
, bool resume
);
452 int mt792xu_mcu_power_on(struct mt792x_dev
*dev
);
453 int mt792xu_wfsys_reset(struct mt792x_dev
*dev
);
454 int mt792xu_init_reset(struct mt792x_dev
*dev
);
455 u32
mt792xu_rr(struct mt76_dev
*dev
, u32 addr
);
456 void mt792xu_wr(struct mt76_dev
*dev
, u32 addr
, u32 val
);
457 u32
mt792xu_rmw(struct mt76_dev
*dev
, u32 addr
, u32 mask
, u32 val
);
458 void mt792xu_copy(struct mt76_dev
*dev
, u32 offset
, const void *data
, int len
);
459 void mt792xu_disconnect(struct usb_interface
*usb_intf
);
460 void mt792xu_stop(struct ieee80211_hw
*hw
, bool suspend
);
463 mt792x_skb_add_usb_sdio_hdr(struct mt792x_dev
*dev
, struct sk_buff
*skb
,
468 len
= mt76_is_usb(&dev
->mt76
) ? skb
->len
: skb
->len
+ sizeof(hdr
);
469 hdr
= FIELD_PREP(MT792x_SDIO_HDR_TX_BYTES
, len
) |
470 FIELD_PREP(MT792x_SDIO_HDR_PKT_TYPE
, type
);
472 put_unaligned_le32(hdr
, skb_push(skb
, sizeof(hdr
)));
475 int __mt792xe_mcu_drv_pmctrl(struct mt792x_dev
*dev
);
476 int mt792xe_mcu_drv_pmctrl(struct mt792x_dev
*dev
);
477 int mt792xe_mcu_fw_pmctrl(struct mt792x_dev
*dev
);
480 int mt792x_init_acpi_sar(struct mt792x_dev
*dev
);
481 int mt792x_init_acpi_sar_power(struct mt792x_phy
*phy
, bool set_default
);
482 u8
mt792x_acpi_get_flags(struct mt792x_phy
*phy
);
483 u8
mt792x_acpi_get_mtcl_conf(struct mt792x_phy
*phy
, char *alpha2
);
485 static inline int mt792x_init_acpi_sar(struct mt792x_dev
*dev
)
490 static inline int mt792x_init_acpi_sar_power(struct mt792x_phy
*phy
,
496 static inline u8
mt792x_acpi_get_flags(struct mt792x_phy
*phy
)
501 static inline u8
mt792x_acpi_get_mtcl_conf(struct mt792x_phy
*phy
, char *alpha2
)
507 #endif /* __MT7925_H */