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