mt76x2.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef __MT76x2_H
  17. #define __MT76x2_H
  18. #include <linux/device.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/irq.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/mutex.h>
  26. #include <linux/bitops.h>
  27. #include <linux/kfifo.h>
  28. #include <linux/average.h>
  29. #define MT7662_FIRMWARE "mt7662.bin"
  30. #define MT7662_ROM_PATCH "mt7662_rom_patch.bin"
  31. #define MT7662_EEPROM_SIZE 512
  32. #define MT7662U_FIRMWARE "mediatek/mt7662u.bin"
  33. #define MT7662U_ROM_PATCH "mediatek/mt7662u_rom_patch.bin"
  34. #define MT76x2_RX_RING_SIZE 256
  35. #define MT_RX_HEADROOM 32
  36. #define MT_MAX_CHAINS 2
  37. #define MT_CALIBRATE_INTERVAL HZ
  38. #define MT_MAX_VIFS 8
  39. #define MT_VIF_WCID(_n) (254 - ((_n) & 7))
  40. #include "mt76.h"
  41. #include "mt76x2_regs.h"
  42. #include "mt76x2_mac.h"
  43. #include "mt76x2_dfs.h"
  44. DECLARE_EWMA(signal, 10, 8)
  45. struct mt76x2_mcu {
  46. struct mutex mutex;
  47. wait_queue_head_t wait;
  48. struct sk_buff_head res_q;
  49. struct mt76u_buf res_u;
  50. u32 msg_seq;
  51. };
  52. struct mt76x2_rx_freq_cal {
  53. s8 high_gain[MT_MAX_CHAINS];
  54. s8 rssi_offset[MT_MAX_CHAINS];
  55. s8 lna_gain;
  56. u32 mcu_gain;
  57. };
  58. struct mt76x2_calibration {
  59. struct mt76x2_rx_freq_cal rx;
  60. u8 agc_gain_init[MT_MAX_CHAINS];
  61. u8 agc_gain_cur[MT_MAX_CHAINS];
  62. u16 false_cca;
  63. s8 avg_rssi_all;
  64. s8 agc_gain_adjust;
  65. s8 low_gain;
  66. u8 temp;
  67. bool init_cal_done;
  68. bool tssi_cal_done;
  69. bool tssi_comp_pending;
  70. bool dpd_cal_done;
  71. bool channel_cal_done;
  72. };
  73. struct mt76x2_dev {
  74. struct mt76_dev mt76; /* must be first */
  75. struct mac_address macaddr_list[8];
  76. struct mutex mutex;
  77. const u16 *beacon_offsets;
  78. unsigned long wcid_mask[128 / BITS_PER_LONG];
  79. int txpower_conf;
  80. int txpower_cur;
  81. u8 txdone_seq;
  82. DECLARE_KFIFO_PTR(txstatus_fifo, struct mt76x2_tx_status);
  83. struct mt76x2_mcu mcu;
  84. struct sk_buff *rx_head;
  85. struct tasklet_struct tx_tasklet;
  86. struct tasklet_struct pre_tbtt_tasklet;
  87. struct delayed_work cal_work;
  88. struct delayed_work mac_work;
  89. u32 aggr_stats[32];
  90. struct mt76_wcid global_wcid;
  91. struct mt76_wcid __rcu *wcid[128];
  92. spinlock_t irq_lock;
  93. u32 irqmask;
  94. struct sk_buff *beacons[8];
  95. u8 beacon_mask;
  96. u8 beacon_data_mask;
  97. u8 tbtt_count;
  98. u16 beacon_int;
  99. u16 chainmask;
  100. u32 rxfilter;
  101. struct mt76x2_calibration cal;
  102. s8 target_power;
  103. s8 target_power_delta[2];
  104. struct mt76_rate_power rate_power;
  105. bool enable_tpc;
  106. u8 coverage_class;
  107. u8 slottime;
  108. struct mt76x2_dfs_pattern_detector dfs_pd;
  109. };
  110. struct mt76x2_vif {
  111. u8 idx;
  112. struct mt76_wcid group_wcid;
  113. };
  114. struct mt76x2_sta {
  115. struct mt76_wcid wcid; /* must be first */
  116. struct mt76x2_vif *vif;
  117. struct mt76x2_tx_status status;
  118. int n_frames;
  119. struct ewma_signal rssi;
  120. int inactive_count;
  121. };
  122. static inline bool mt76x2_wait_for_mac(struct mt76x2_dev *dev)
  123. {
  124. int i;
  125. for (i = 0; i < 500; i++) {
  126. switch (mt76_rr(dev, MT_MAC_CSR0)) {
  127. case 0:
  128. case ~0:
  129. break;
  130. default:
  131. return true;
  132. }
  133. usleep_range(5000, 10000);
  134. }
  135. return false;
  136. }
  137. static inline bool is_mt7612(struct mt76x2_dev *dev)
  138. {
  139. return mt76_chip(&dev->mt76) == 0x7612;
  140. }
  141. void mt76x2_set_irq_mask(struct mt76x2_dev *dev, u32 clear, u32 set);
  142. static inline bool mt76x2_channel_silent(struct mt76x2_dev *dev)
  143. {
  144. struct ieee80211_channel *chan = dev->mt76.chandef.chan;
  145. return ((chan->flags & IEEE80211_CHAN_RADAR) &&
  146. chan->dfs_state != NL80211_DFS_AVAILABLE);
  147. }
  148. static inline void mt76x2_irq_enable(struct mt76x2_dev *dev, u32 mask)
  149. {
  150. mt76x2_set_irq_mask(dev, 0, mask);
  151. }
  152. static inline void mt76x2_irq_disable(struct mt76x2_dev *dev, u32 mask)
  153. {
  154. mt76x2_set_irq_mask(dev, mask, 0);
  155. }
  156. static inline bool mt76x2_wait_for_bbp(struct mt76x2_dev *dev)
  157. {
  158. return mt76_poll_msec(dev, MT_MAC_STATUS,
  159. MT_MAC_STATUS_TX | MT_MAC_STATUS_RX,
  160. 0, 100);
  161. }
  162. static inline bool wait_for_wpdma(struct mt76x2_dev *dev)
  163. {
  164. return mt76_poll(dev, MT_WPDMA_GLO_CFG,
  165. MT_WPDMA_GLO_CFG_TX_DMA_BUSY |
  166. MT_WPDMA_GLO_CFG_RX_DMA_BUSY,
  167. 0, 1000);
  168. }
  169. extern const struct ieee80211_ops mt76x2_ops;
  170. extern struct ieee80211_rate mt76x2_rates[12];
  171. struct mt76x2_dev *mt76x2_alloc_device(struct device *pdev);
  172. int mt76x2_register_device(struct mt76x2_dev *dev);
  173. void mt76x2_init_debugfs(struct mt76x2_dev *dev);
  174. void mt76x2_init_device(struct mt76x2_dev *dev);
  175. irqreturn_t mt76x2_irq_handler(int irq, void *dev_instance);
  176. void mt76x2_phy_power_on(struct mt76x2_dev *dev);
  177. int mt76x2_init_hardware(struct mt76x2_dev *dev);
  178. void mt76x2_stop_hardware(struct mt76x2_dev *dev);
  179. int mt76x2_eeprom_init(struct mt76x2_dev *dev);
  180. int mt76x2_apply_calibration_data(struct mt76x2_dev *dev, int channel);
  181. void mt76x2_set_tx_ackto(struct mt76x2_dev *dev);
  182. void mt76x2_phy_set_antenna(struct mt76x2_dev *dev);
  183. int mt76x2_phy_start(struct mt76x2_dev *dev);
  184. int mt76x2_phy_set_channel(struct mt76x2_dev *dev,
  185. struct cfg80211_chan_def *chandef);
  186. int mt76x2_mac_get_rssi(struct mt76x2_dev *dev, s8 rssi, int chain);
  187. void mt76x2_phy_calibrate(struct work_struct *work);
  188. void mt76x2_phy_set_txpower(struct mt76x2_dev *dev);
  189. int mt76x2_mcu_init(struct mt76x2_dev *dev);
  190. int mt76x2_mcu_set_channel(struct mt76x2_dev *dev, u8 channel, u8 bw,
  191. u8 bw_index, bool scan);
  192. int mt76x2_mcu_set_radio_state(struct mt76x2_dev *dev, bool on);
  193. int mt76x2_mcu_load_cr(struct mt76x2_dev *dev, u8 type, u8 temp_level,
  194. u8 channel);
  195. int mt76x2_mcu_cleanup(struct mt76x2_dev *dev);
  196. int mt76x2_dma_init(struct mt76x2_dev *dev);
  197. void mt76x2_dma_cleanup(struct mt76x2_dev *dev);
  198. void mt76x2_cleanup(struct mt76x2_dev *dev);
  199. int mt76x2_tx_queue_mcu(struct mt76x2_dev *dev, enum mt76_txq_id qid,
  200. struct sk_buff *skb, int cmd, int seq);
  201. void mt76x2_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
  202. struct sk_buff *skb);
  203. void mt76x2_tx_complete(struct mt76x2_dev *dev, struct sk_buff *skb);
  204. int mt76x2_tx_prepare_skb(struct mt76_dev *mdev, void *txwi,
  205. struct sk_buff *skb, struct mt76_queue *q,
  206. struct mt76_wcid *wcid, struct ieee80211_sta *sta,
  207. u32 *tx_info);
  208. void mt76x2_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue *q,
  209. struct mt76_queue_entry *e, bool flush);
  210. void mt76x2_mac_set_tx_protection(struct mt76x2_dev *dev, u32 val);
  211. void mt76x2_pre_tbtt_tasklet(unsigned long arg);
  212. void mt76x2_rx_poll_complete(struct mt76_dev *mdev, enum mt76_rxq_id q);
  213. void mt76x2_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
  214. struct sk_buff *skb);
  215. void mt76x2_sta_ps(struct mt76_dev *dev, struct ieee80211_sta *sta, bool ps);
  216. void mt76x2_update_channel(struct mt76_dev *mdev);
  217. s8 mt76x2_tx_get_max_txpwr_adj(struct mt76x2_dev *dev,
  218. const struct ieee80211_tx_rate *rate);
  219. s8 mt76x2_tx_get_txpwr_adj(struct mt76x2_dev *dev, s8 txpwr, s8 max_txpwr_adj);
  220. void mt76x2_tx_set_txpwr_auto(struct mt76x2_dev *dev, s8 txpwr);
  221. int mt76x2_insert_hdr_pad(struct sk_buff *skb);
  222. bool mt76x2_mac_load_tx_status(struct mt76x2_dev *dev,
  223. struct mt76x2_tx_status *stat);
  224. void mt76x2_send_tx_status(struct mt76x2_dev *dev,
  225. struct mt76x2_tx_status *stat, u8 *update);
  226. void mt76x2_reset_wlan(struct mt76x2_dev *dev, bool enable);
  227. void mt76x2_init_txpower(struct mt76x2_dev *dev,
  228. struct ieee80211_supported_band *sband);
  229. void mt76_write_mac_initvals(struct mt76x2_dev *dev);
  230. int mt76x2_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  231. struct ieee80211_ampdu_params *params);
  232. int mt76x2_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  233. struct ieee80211_sta *sta);
  234. int mt76x2_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  235. struct ieee80211_sta *sta);
  236. void mt76x2_remove_interface(struct ieee80211_hw *hw,
  237. struct ieee80211_vif *vif);
  238. int mt76x2_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  239. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  240. struct ieee80211_key_conf *key);
  241. int mt76x2_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  242. u16 queue, const struct ieee80211_tx_queue_params *params);
  243. void mt76x2_configure_filter(struct ieee80211_hw *hw,
  244. unsigned int changed_flags,
  245. unsigned int *total_flags, u64 multicast);
  246. void mt76x2_txq_init(struct mt76x2_dev *dev, struct ieee80211_txq *txq);
  247. void mt76x2_sta_rate_tbl_update(struct ieee80211_hw *hw,
  248. struct ieee80211_vif *vif,
  249. struct ieee80211_sta *sta);
  250. void mt76x2_phy_set_txpower_regs(struct mt76x2_dev *dev,
  251. enum nl80211_band band);
  252. void mt76x2_configure_tx_delay(struct mt76x2_dev *dev,
  253. enum nl80211_band band, u8 bw);
  254. void mt76x2_phy_set_bw(struct mt76x2_dev *dev, int width, u8 ctrl);
  255. void mt76x2_phy_set_band(struct mt76x2_dev *dev, int band, bool primary_upper);
  256. int mt76x2_phy_get_min_avg_rssi(struct mt76x2_dev *dev);
  257. void mt76x2_apply_gain_adj(struct mt76x2_dev *dev);
  258. #endif