2 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
4 * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
19 #include <linux/bitfield.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <linux/mutex.h>
23 #include <linux/usb.h>
24 #include <linux/completion.h>
25 #include <net/mac80211.h>
26 #include <linux/debugfs.h>
31 #define MT_CALIBRATE_INTERVAL (4 * HZ)
33 #define MT_FREQ_CAL_INIT_DELAY (30 * HZ)
34 #define MT_FREQ_CAL_CHECK_INTERVAL (10 * HZ)
35 #define MT_FREQ_CAL_ADJ_INTERVAL (HZ / 2)
37 #define MT_BBP_REG_VERSION 0x00
39 #define MT_USB_AGGR_SIZE_LIMIT 21 /* * 1024B */
40 #define MT_USB_AGGR_TIMEOUT 0x80 /* * 33ns */
42 #define MT_RX_URB_SIZE (PAGE_SIZE << MT_RX_ORDER)
44 struct mt76x0_dma_buf {
56 struct mt76x0_dma_buf resp;
57 struct completion resp_cmpl;
59 struct mt76_reg_pair *reg_pairs;
60 unsigned int reg_pairs_len;
73 #define N_RX_ENTRIES 16
74 struct mt76x0_rx_queue {
75 struct mt76x0_dev *dev;
77 struct mt76x0_dma_buf_rx {
88 #define N_TX_ENTRIES 64
90 struct mt76x0_tx_queue {
91 struct mt76x0_dev *dev;
93 struct mt76x0_dma_buf_tx {
100 unsigned int entries;
102 unsigned int fifo_seq;
113 #define GROUP_WCID(idx) (254 - idx)
115 struct mt76x0_eeprom_params;
117 #define MT_EE_TEMPERATURE_SLOPE 39
118 #define MT_FREQ_OFFSET_INVALID -128
121 #define MT_VEND_TYPE_EEPROM BIT(31)
122 #define MT_VEND_TYPE_CFG BIT(30)
123 #define MT_VEND_TYPE_MASK (MT_VEND_TYPE_EEPROM | MT_VEND_TYPE_CFG)
125 #define MT_VEND_ADDR(type, n) (MT_VEND_TYPE_##type | (n))
133 * struct mt76x0_dev - adapter structure
134 * @lock: protects @wcid->tx_rate.
135 * @mac_lock: locks out mac80211's tx status and rx paths.
136 * @tx_lock: protects @tx_q and changes of MT76_STATE_*_STATS
138 * @rx_lock: protects @rx_q.
139 * @con_mon_lock: protects @ap_bssid, @bcn_*, @avg_rssi.
140 * @mutex: ensures exclusive access from mac80211 callbacks.
141 * @reg_atomic_mutex: ensures atomicity of indirect register accesses
142 * (accesses to RF and BBP).
143 * @hw_atomic_mutex: ensures exclusive access to HW during critical
144 * operations (power management, channel switch).
147 struct mt76_dev mt76; /* must be first */
151 struct mutex usb_ctrl_mtx;
154 struct tasklet_struct rx_tasklet;
155 struct tasklet_struct tx_tasklet;
157 u8 out_ep[__MT_EP_OUT_MAX];
159 u8 in_ep[__MT_EP_IN_MAX];
162 unsigned long wcid_mask[DIV_ROUND_UP(N_WCIDS, BITS_PER_LONG)];
163 unsigned long vif_mask;
165 struct mt76x0_mcu mcu;
167 struct delayed_work cal_work;
168 struct delayed_work mac_work;
170 struct workqueue_struct *stat_wq;
171 struct delayed_work stat_work;
173 struct mt76_wcid *mon_wcid;
174 struct mt76_wcid __rcu *wcid[N_WCIDS];
178 const u16 *beacon_offsets;
180 u8 macaddr[ETH_ALEN];
181 struct mt76x0_eeprom_params *ee;
183 struct mutex reg_atomic_mutex;
184 struct mutex hw_atomic_mutex;
191 struct mt76x0_tx_queue *tx_q;
192 struct sk_buff_head tx_skb_done;
194 atomic_t avg_ampdu_len;
198 struct mt76x0_rx_queue rx_q;
200 /* Connection monitoring things */
201 spinlock_t con_mon_lock;
202 u8 ap_bssid[ETH_ALEN];
207 int avg_rssi; /* starts at 0 and converges */
212 struct mac_stats stats;
227 struct mt76_wcid group_wcid;
230 struct mt76_tx_status {
240 } __packed __aligned(2);
243 struct mt76_wcid wcid;
244 struct mt76_tx_status status;
246 u16 agg_ssn[IEEE80211_NUM_TIDS];
249 struct mt76_reg_pair {
256 extern const struct ieee80211_ops mt76x0_ops;
258 static inline bool is_mt7610e(struct mt76x0_dev *dev)
264 void mt76x0_init_debugfs(struct mt76x0_dev *dev);
266 int mt76x0_wait_asic_ready(struct mt76x0_dev *dev);
268 /* Compatibility with mt76 */
269 #define mt76_rmw_field(_dev, _reg, _field, _val) \
270 mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
272 int mt76x0_write_reg_pairs(struct mt76x0_dev *dev, u32 base,
273 const struct mt76_reg_pair *data, int len);
274 int mt76x0_read_reg_pairs(struct mt76x0_dev *dev, u32 base,
275 struct mt76_reg_pair *data, int len);
276 int mt76x0_burst_write_regs(struct mt76x0_dev *dev, u32 offset,
277 const u32 *data, int n);
278 void mt76x0_addr_wr(struct mt76x0_dev *dev, const u32 offset, const u8 *addr);
281 struct mt76x0_dev *mt76x0_alloc_device(struct device *dev);
282 int mt76x0_init_hardware(struct mt76x0_dev *dev, bool reset);
283 int mt76x0_register_device(struct mt76x0_dev *dev);
284 void mt76x0_cleanup(struct mt76x0_dev *dev);
285 void mt76x0_chip_onoff(struct mt76x0_dev *dev, bool enable, bool reset);
287 int mt76x0_mac_start(struct mt76x0_dev *dev);
288 void mt76x0_mac_stop(struct mt76x0_dev *dev);
291 void mt76x0_phy_init(struct mt76x0_dev *dev);
292 int mt76x0_wait_bbp_ready(struct mt76x0_dev *dev);
293 void mt76x0_agc_save(struct mt76x0_dev *dev);
294 void mt76x0_agc_restore(struct mt76x0_dev *dev);
295 int mt76x0_phy_set_channel(struct mt76x0_dev *dev,
296 struct cfg80211_chan_def *chandef);
297 void mt76x0_phy_recalibrate_after_assoc(struct mt76x0_dev *dev);
298 int mt76x0_phy_get_rssi(struct mt76x0_dev *dev, struct mt76x0_rxwi *rxwi);
299 void mt76x0_phy_con_cal_onoff(struct mt76x0_dev *dev,
300 struct ieee80211_bss_conf *info);
303 void mt76x0_mac_work(struct work_struct *work);
304 void mt76x0_mac_set_protection(struct mt76x0_dev *dev, bool legacy_prot,
306 void mt76x0_mac_set_short_preamble(struct mt76x0_dev *dev, bool short_preamb);
307 void mt76x0_mac_config_tsf(struct mt76x0_dev *dev, bool enable, int interval);
309 mt76x0_mac_wcid_setup(struct mt76x0_dev *dev, u8 idx, u8 vif_idx, u8 *mac);
310 void mt76x0_mac_set_ampdu_factor(struct mt76x0_dev *dev);
313 void mt76x0_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
314 struct sk_buff *skb);
315 int mt76x0_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
316 u16 queue, const struct ieee80211_tx_queue_params *params);
317 void mt76x0_tx_status(struct mt76x0_dev *dev, struct sk_buff *skb);
318 void mt76x0_tx_stat(struct work_struct *work);
321 void mt76x0_remove_hdr_pad(struct sk_buff *skb);
322 int mt76x0_insert_hdr_pad(struct sk_buff *skb);
324 int mt76x0_dma_init(struct mt76x0_dev *dev);
325 void mt76x0_dma_cleanup(struct mt76x0_dev *dev);
327 int mt76x0_dma_enqueue_tx(struct mt76x0_dev *dev, struct sk_buff *skb,
328 struct mt76_wcid *wcid, int hw_q);