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