1 /* SPDX-License-Identifier: ISC */
2 /* Copyright (C) 2019 MediaTek Inc. */
7 #include <linux/completion.h>
8 #include <linux/interrupt.h>
9 #include <linux/ktime.h>
10 #include <linux/regmap.h>
11 #include "../mt76_connac_mcu.h"
14 #define MT7615_MAX_INTERFACES 16
15 #define MT7615_MAX_WMM_SETS 4
16 #define MT7663_WTBL_SIZE 32
17 #define MT7615_WTBL_SIZE 128
18 #define MT7615_WTBL_RESERVED (mt7615_wtbl_size(dev) - 1)
19 #define MT7615_WTBL_STA (MT7615_WTBL_RESERVED - \
20 MT7615_MAX_INTERFACES)
22 #define MT7615_PM_TIMEOUT (HZ / 12)
23 #define MT7615_HW_SCAN_TIMEOUT (HZ / 10)
24 #define MT7615_RESET_TIMEOUT (30 * HZ)
25 #define MT7615_RATE_RETRY 2
27 #define MT7615_TX_RING_SIZE 1024
28 #define MT7615_TX_MGMT_RING_SIZE 128
29 #define MT7615_TX_MCU_RING_SIZE 128
30 #define MT7615_TX_FWDL_RING_SIZE 128
32 #define MT7615_RX_RING_SIZE 1024
33 #define MT7615_RX_MCU_RING_SIZE 512
35 #define MT7615_DRV_OWN_RETRY_COUNT 10
37 #define MT7615_FIRMWARE_CR4 "mediatek/mt7615_cr4.bin"
38 #define MT7615_FIRMWARE_N9 "mediatek/mt7615_n9.bin"
39 #define MT7615_ROM_PATCH "mediatek/mt7615_rom_patch.bin"
41 #define MT7622_FIRMWARE_N9 "mediatek/mt7622_n9.bin"
42 #define MT7622_ROM_PATCH "mediatek/mt7622_rom_patch.bin"
44 #define MT7615_FIRMWARE_V1 1
45 #define MT7615_FIRMWARE_V2 2
46 #define MT7615_FIRMWARE_V3 3
48 #define MT7663_OFFLOAD_ROM_PATCH "mediatek/mt7663pr2h.bin"
49 #define MT7663_OFFLOAD_FIRMWARE_N9 "mediatek/mt7663_n9_v3.bin"
50 #define MT7663_ROM_PATCH "mediatek/mt7663pr2h_rebb.bin"
51 #define MT7663_FIRMWARE_N9 "mediatek/mt7663_n9_rebb.bin"
53 #define MT7615_EEPROM_SIZE 1024
54 #define MT7615_TOKEN_SIZE 4096
56 #define MT_FRAC_SCALE 12
57 #define MT_FRAC(val, div) (((val) << MT_FRAC_SCALE) / (div))
59 #define MT_CHFREQ_VALID BIT(7)
60 #define MT_CHFREQ_DBDC_IDX BIT(6)
61 #define MT_CHFREQ_SEQ GENMASK(5, 0)
63 #define MT7615_BAR_RATE_DEFAULT 0x4b /* OFDM 6M */
64 #define MT7615_CFEND_RATE_DEFAULT 0x49 /* OFDM 24M */
65 #define MT7615_CFEND_RATE_11B 0x03 /* 11B LP, 11M */
69 struct mt7615_dfs_pulse;
70 struct mt7615_dfs_pattern;
71 enum mt7615_cipher_type;
73 enum mt7615_hw_txq_id {
80 enum mt7622_hw_txq_id {
84 MT7622_TXQ_FWDL = MT7615_TXQ_FWDL,
90 struct mt7615_rate_set {
91 struct ieee80211_tx_rate probe_rate;
92 struct ieee80211_tx_rate rates[4];
95 struct mt7615_rate_desc {
103 struct mt7615_wtbl_rate_desc {
104 struct list_head node;
106 struct mt7615_rate_desc rate;
107 struct mt7615_sta *sta;
110 struct mt7663s_intr {
123 struct mt76_wcid wcid; /* must be first */
125 struct mt7615_vif *vif;
127 struct list_head poll_list;
130 struct ieee80211_tx_rate rates[4];
132 struct mt7615_rate_set rateset[2];
142 struct mt76_vif mt76; /* must be first */
143 struct mt7615_sta sta;
153 unsigned long aggr_per;
157 struct mt76_phy *mt76;
158 struct mt7615_dev *dev;
160 struct ieee80211_vif *monitor_vif;
171 unsigned long last_cca_adj;
172 int false_cca_ofdm, false_cca_cck;
185 struct mib_stats mib;
187 struct sk_buff_head scan_event_list;
188 struct delayed_work scan_work;
190 struct work_struct roc_work;
191 struct timer_list roc_timer;
192 wait_queue_head_t roc_wait;
195 #ifdef CONFIG_NL80211_TESTMODE
199 s16 last_freq_offset;
207 #define mt7615_mcu_add_tx_ba(dev, ...) (dev)->mcu_ops->add_tx_ba((dev), __VA_ARGS__)
208 #define mt7615_mcu_add_rx_ba(dev, ...) (dev)->mcu_ops->add_rx_ba((dev), __VA_ARGS__)
209 #define mt7615_mcu_sta_add(phy, ...) ((phy)->dev)->mcu_ops->sta_add((phy), __VA_ARGS__)
210 #define mt7615_mcu_add_dev_info(phy, ...) ((phy)->dev)->mcu_ops->add_dev_info((phy), __VA_ARGS__)
211 #define mt7615_mcu_add_bss_info(phy, ...) ((phy)->dev)->mcu_ops->add_bss_info((phy), __VA_ARGS__)
212 #define mt7615_mcu_add_beacon(dev, ...) (dev)->mcu_ops->add_beacon_offload((dev), __VA_ARGS__)
213 #define mt7615_mcu_set_pm(dev, ...) (dev)->mcu_ops->set_pm_state((dev), __VA_ARGS__)
214 #define mt7615_mcu_set_drv_ctrl(dev) (dev)->mcu_ops->set_drv_ctrl((dev))
215 #define mt7615_mcu_set_fw_ctrl(dev) (dev)->mcu_ops->set_fw_ctrl((dev))
216 #define mt7615_mcu_set_sta_decap_offload(dev, ...) (dev)->mcu_ops->set_sta_decap_offload((dev), __VA_ARGS__)
217 struct mt7615_mcu_ops {
218 int (*add_tx_ba)(struct mt7615_dev *dev,
219 struct ieee80211_ampdu_params *params,
221 int (*add_rx_ba)(struct mt7615_dev *dev,
222 struct ieee80211_ampdu_params *params,
224 int (*sta_add)(struct mt7615_phy *phy, struct ieee80211_vif *vif,
225 struct ieee80211_sta *sta, bool enable);
226 int (*add_dev_info)(struct mt7615_phy *phy, struct ieee80211_vif *vif,
228 int (*add_bss_info)(struct mt7615_phy *phy, struct ieee80211_vif *vif,
229 struct ieee80211_sta *sta, bool enable);
230 int (*add_beacon_offload)(struct mt7615_dev *dev,
231 struct ieee80211_hw *hw,
232 struct ieee80211_vif *vif, bool enable);
233 int (*set_pm_state)(struct mt7615_dev *dev, int band, int state);
234 int (*set_drv_ctrl)(struct mt7615_dev *dev);
235 int (*set_fw_ctrl)(struct mt7615_dev *dev);
236 int (*set_sta_decap_offload)(struct mt7615_dev *dev,
237 struct ieee80211_vif *vif,
238 struct ieee80211_sta *sta);
242 union { /* must be first */
243 struct mt76_dev mt76;
244 struct mt76_phy mphy;
247 const struct mt76_bus_ops *bus_ops;
248 struct tasklet_struct irq_tasklet;
250 struct mt7615_phy phy;
255 struct ieee80211_ops *ops;
256 const struct mt7615_mcu_ops *mcu_ops;
257 struct regmap *infracfg;
260 struct work_struct mcu_work;
262 struct work_struct reset_work;
263 wait_queue_head_t reset_wait;
266 struct list_head sta_poll_list;
267 spinlock_t sta_poll_lock;
283 struct work_struct rate_work;
284 struct list_head wrd_head;
291 struct mt76_connac_pm pm;
292 struct mt76_connac_coredump coredump;
295 enum tx_pkt_queue_idx {
300 MT_LMAC_ALTX0 = 0x10,
315 enum mt7615_rdd_cmd {
329 static inline struct mt7615_phy *
330 mt7615_hw_phy(struct ieee80211_hw *hw)
332 struct mt76_phy *phy = hw->priv;
337 static inline struct mt7615_dev *
338 mt7615_hw_dev(struct ieee80211_hw *hw)
340 struct mt76_phy *phy = hw->priv;
342 return container_of(phy->dev, struct mt7615_dev, mt76);
345 static inline struct mt7615_phy *
346 mt7615_ext_phy(struct mt7615_dev *dev)
348 struct mt76_phy *phy = dev->mt76.phys[MT_BAND1];
356 extern struct ieee80211_rate mt7615_rates[12];
357 extern const struct ieee80211_ops mt7615_ops;
358 extern const u32 mt7615e_reg_map[__MT_BASE_MAX];
359 extern const u32 mt7663e_reg_map[__MT_BASE_MAX];
360 extern const u32 mt7663_usb_sdio_reg_map[__MT_BASE_MAX];
361 extern struct pci_driver mt7615_pci_driver;
362 extern struct platform_driver mt7622_wmac_driver;
363 extern const struct mt76_testmode_ops mt7615_testmode_ops;
365 #ifdef CONFIG_MT7622_WMAC
366 int mt7622_wmac_init(struct mt7615_dev *dev);
368 static inline int mt7622_wmac_init(struct mt7615_dev *dev)
374 int mt7615_thermal_init(struct mt7615_dev *dev);
375 int mt7615_mmio_probe(struct device *pdev, void __iomem *mem_base,
376 int irq, const u32 *map);
377 u32 mt7615_reg_map(struct mt7615_dev *dev, u32 addr);
379 u32 mt7615_reg_map(struct mt7615_dev *dev, u32 addr);
380 int mt7615_led_set_blink(struct led_classdev *led_cdev,
381 unsigned long *delay_on,
382 unsigned long *delay_off);
383 void mt7615_led_set_brightness(struct led_classdev *led_cdev,
384 enum led_brightness brightness);
385 void mt7615_init_device(struct mt7615_dev *dev);
386 int mt7615_register_device(struct mt7615_dev *dev);
387 void mt7615_unregister_device(struct mt7615_dev *dev);
388 int mt7615_register_ext_phy(struct mt7615_dev *dev);
389 void mt7615_unregister_ext_phy(struct mt7615_dev *dev);
390 int mt7615_eeprom_init(struct mt7615_dev *dev, u32 addr);
391 int mt7615_eeprom_get_target_power_index(struct mt7615_dev *dev,
392 struct ieee80211_channel *chan,
394 int mt7615_eeprom_get_power_delta_index(struct mt7615_dev *dev,
395 enum nl80211_band band);
396 int mt7615_wait_pdma_busy(struct mt7615_dev *dev);
397 int mt7615_dma_init(struct mt7615_dev *dev);
398 void mt7615_dma_start(struct mt7615_dev *dev);
399 void mt7615_dma_cleanup(struct mt7615_dev *dev);
400 int mt7615_mcu_init(struct mt7615_dev *dev);
401 bool mt7615_wait_for_mcu_init(struct mt7615_dev *dev);
402 void mt7615_mac_set_rates(struct mt7615_phy *phy, struct mt7615_sta *sta,
403 struct ieee80211_tx_rate *probe_rate,
404 struct ieee80211_tx_rate *rates);
405 void mt7615_pm_wake_work(struct work_struct *work);
406 void mt7615_pm_power_save_work(struct work_struct *work);
407 int mt7615_mcu_del_wtbl_all(struct mt7615_dev *dev);
408 int mt7615_mcu_set_chan_info(struct mt7615_phy *phy, int cmd);
409 int mt7615_mcu_set_wmm(struct mt7615_dev *dev, u8 queue,
410 const struct ieee80211_tx_queue_params *params);
411 void mt7615_mcu_rx_event(struct mt7615_dev *dev, struct sk_buff *skb);
412 int mt7615_mcu_rdd_send_pattern(struct mt7615_dev *dev);
413 int mt7615_mcu_fw_log_2_host(struct mt7615_dev *dev, u8 ctrl);
415 static inline void mt7615_irq_enable(struct mt7615_dev *dev, u32 mask)
417 mt76_set_irq_mask(&dev->mt76, 0, 0, mask);
419 tasklet_schedule(&dev->irq_tasklet);
422 static inline bool mt7615_firmware_offload(struct mt7615_dev *dev)
424 return dev->fw_ver > MT7615_FIRMWARE_V2;
427 static inline u16 mt7615_wtbl_size(struct mt7615_dev *dev)
429 if (is_mt7663(&dev->mt76) && mt7615_firmware_offload(dev))
430 return MT7663_WTBL_SIZE;
432 return MT7615_WTBL_SIZE;
435 #define mt7615_mutex_acquire(dev) \
436 mt76_connac_mutex_acquire(&(dev)->mt76, &(dev)->pm)
437 #define mt7615_mutex_release(dev) \
438 mt76_connac_mutex_release(&(dev)->mt76, &(dev)->pm)
440 static inline u8 mt7615_lmac_mapping(struct mt7615_dev *dev, u8 ac)
442 static const u8 lmac_queue_map[] = {
443 [IEEE80211_AC_BK] = MT_LMAC_AC00,
444 [IEEE80211_AC_BE] = MT_LMAC_AC01,
445 [IEEE80211_AC_VI] = MT_LMAC_AC02,
446 [IEEE80211_AC_VO] = MT_LMAC_AC03,
449 if (WARN_ON_ONCE(ac >= ARRAY_SIZE(lmac_queue_map)))
450 return MT_LMAC_AC01; /* BE */
452 return lmac_queue_map[ac];
455 static inline u32 mt7615_tx_mcu_int_mask(struct mt7615_dev *dev)
457 return MT_INT_TX_DONE(dev->mt76.q_mcu[MT_MCUQ_WM]->hw_idx);
460 static inline unsigned long
461 mt7615_get_macwork_timeout(struct mt7615_dev *dev)
463 return dev->pm.enable ? HZ / 3 : HZ / 10;
466 void mt7615_dma_reset(struct mt7615_dev *dev);
467 void mt7615_scan_work(struct work_struct *work);
468 void mt7615_roc_work(struct work_struct *work);
469 void mt7615_roc_timer(struct timer_list *timer);
470 void mt7615_init_txpower(struct mt7615_dev *dev,
471 struct ieee80211_supported_band *sband);
472 int mt7615_set_channel(struct mt7615_phy *phy);
473 void mt7615_init_work(struct mt7615_dev *dev);
475 int mt7615_mcu_restart(struct mt76_dev *dev);
476 void mt7615_update_channel(struct mt76_phy *mphy);
477 bool mt7615_mac_wtbl_update(struct mt7615_dev *dev, int idx, u32 mask);
478 void mt7615_mac_reset_counters(struct mt7615_phy *phy);
479 void mt7615_mac_cca_stats_reset(struct mt7615_phy *phy);
480 void mt7615_mac_set_scs(struct mt7615_phy *phy, bool enable);
481 void mt7615_mac_enable_nf(struct mt7615_dev *dev, bool ext_phy);
482 void mt7615_mac_enable_rtscts(struct mt7615_dev *dev,
483 struct ieee80211_vif *vif, bool enable);
484 void mt7615_mac_sta_poll(struct mt7615_dev *dev);
485 int mt7615_mac_write_txwi(struct mt7615_dev *dev, __le32 *txwi,
486 struct sk_buff *skb, struct mt76_wcid *wcid,
487 struct ieee80211_sta *sta, int pid,
488 struct ieee80211_key_conf *key,
489 enum mt76_txq_id qid, bool beacon);
490 void mt7615_mac_set_timing(struct mt7615_phy *phy);
491 int __mt7615_mac_wtbl_set_key(struct mt7615_dev *dev,
492 struct mt76_wcid *wcid,
493 struct ieee80211_key_conf *key);
494 int mt7615_mac_wtbl_set_key(struct mt7615_dev *dev, struct mt76_wcid *wcid,
495 struct ieee80211_key_conf *key);
496 void mt7615_mac_reset_work(struct work_struct *work);
497 u32 mt7615_mac_get_sta_tid_sn(struct mt7615_dev *dev, int wcid, u8 tid);
499 int mt7615_mcu_parse_response(struct mt76_dev *mdev, int cmd,
500 struct sk_buff *skb, int seq);
501 u32 mt7615_rf_rr(struct mt7615_dev *dev, u32 wf, u32 reg);
502 int mt7615_rf_wr(struct mt7615_dev *dev, u32 wf, u32 reg, u32 val);
503 int mt7615_mcu_set_dbdc(struct mt7615_dev *dev);
504 int mt7615_mcu_set_eeprom(struct mt7615_dev *dev);
505 int mt7615_mcu_get_temperature(struct mt7615_dev *dev);
506 int mt7615_mcu_set_tx_power(struct mt7615_phy *phy);
507 void mt7615_mcu_exit(struct mt7615_dev *dev);
508 void mt7615_mcu_fill_msg(struct mt7615_dev *dev, struct sk_buff *skb,
509 int cmd, int *wait_seq);
511 int mt7615_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
512 enum mt76_txq_id qid, struct mt76_wcid *wcid,
513 struct ieee80211_sta *sta,
514 struct mt76_tx_info *tx_info);
516 void mt7615_tx_worker(struct mt76_worker *w);
517 void mt7615_tx_token_put(struct mt7615_dev *dev);
518 bool mt7615_rx_check(struct mt76_dev *mdev, void *data, int len);
519 void mt7615_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
520 struct sk_buff *skb, u32 *info);
521 void mt7615_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps);
522 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
523 struct ieee80211_sta *sta);
524 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
525 struct ieee80211_sta *sta);
526 void mt7615_mac_work(struct work_struct *work);
527 int mt7615_mcu_set_rx_hdr_trans_blacklist(struct mt7615_dev *dev);
528 int mt7615_mcu_set_fcc5_lpn(struct mt7615_dev *dev, int val);
529 int mt7615_mcu_set_pulse_th(struct mt7615_dev *dev,
530 const struct mt7615_dfs_pulse *pulse);
531 int mt7615_mcu_set_radar_th(struct mt7615_dev *dev, int index,
532 const struct mt7615_dfs_pattern *pattern);
533 int mt7615_mcu_set_test_param(struct mt7615_dev *dev, u8 param, bool test_mode,
535 int mt7615_mcu_set_sku_en(struct mt7615_phy *phy, bool enable);
536 int mt7615_mcu_apply_rx_dcoc(struct mt7615_phy *phy);
537 int mt7615_mcu_apply_tx_dpd(struct mt7615_phy *phy);
538 int mt7615_dfs_init_radar_detector(struct mt7615_phy *phy);
540 int mt7615_mcu_set_roc(struct mt7615_phy *phy, struct ieee80211_vif *vif,
541 struct ieee80211_channel *chan, int duration);
543 int mt7615_init_debugfs(struct mt7615_dev *dev);
544 int mt7615_mcu_wait_response(struct mt7615_dev *dev, int cmd, int seq);
546 int mt7615_mac_set_beacon_filter(struct mt7615_phy *phy,
547 struct ieee80211_vif *vif,
549 int mt7615_mcu_set_bss_pm(struct mt7615_dev *dev, struct ieee80211_vif *vif,
551 int __mt7663_load_firmware(struct mt7615_dev *dev);
552 void mt7615_coredump_work(struct work_struct *work);
554 void mt7622_trigger_hif_int(struct mt7615_dev *dev, bool en);
557 int mt7663_usb_sdio_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
558 enum mt76_txq_id qid, struct mt76_wcid *wcid,
559 struct ieee80211_sta *sta,
560 struct mt76_tx_info *tx_info);
561 bool mt7663_usb_sdio_tx_status_data(struct mt76_dev *mdev, u8 *update);
562 void mt7663_usb_sdio_tx_complete_skb(struct mt76_dev *mdev,
563 struct mt76_queue_entry *e);
564 int mt7663_usb_sdio_register_device(struct mt7615_dev *dev);
565 int mt7663u_mcu_init(struct mt7615_dev *dev);
566 int mt7663u_mcu_power_on(struct mt7615_dev *dev);
569 int mt7663s_mcu_init(struct mt7615_dev *dev);