mt7601u.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef MT7601U_H
  15. #define MT7601U_H
  16. #include <linux/bitfield.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/mutex.h>
  20. #include <linux/usb.h>
  21. #include <linux/completion.h>
  22. #include <net/mac80211.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/average.h>
  25. #include "regs.h"
  26. #define MT_CALIBRATE_INTERVAL (4 * HZ)
  27. #define MT_FREQ_CAL_INIT_DELAY (30 * HZ)
  28. #define MT_FREQ_CAL_CHECK_INTERVAL (10 * HZ)
  29. #define MT_FREQ_CAL_ADJ_INTERVAL (HZ / 2)
  30. #define MT_BBP_REG_VERSION 0x00
  31. #define MT_USB_AGGR_SIZE_LIMIT 28 /* * 1024B */
  32. #define MT_USB_AGGR_TIMEOUT 0x80 /* * 33ns */
  33. #define MT_RX_ORDER 3
  34. #define MT_RX_URB_SIZE (PAGE_SIZE << MT_RX_ORDER)
  35. struct mt7601u_dma_buf {
  36. struct urb *urb;
  37. void *buf;
  38. dma_addr_t dma;
  39. size_t len;
  40. };
  41. struct mt7601u_mcu {
  42. struct mutex mutex;
  43. u8 msg_seq;
  44. struct mt7601u_dma_buf resp;
  45. struct completion resp_cmpl;
  46. };
  47. struct mt7601u_freq_cal {
  48. struct delayed_work work;
  49. u8 freq;
  50. bool enabled;
  51. bool adjusting;
  52. };
  53. struct mac_stats {
  54. u64 rx_stat[6];
  55. u64 tx_stat[6];
  56. u64 aggr_stat[2];
  57. u64 aggr_n[32];
  58. u64 zero_len_del[2];
  59. };
  60. #define N_RX_ENTRIES 16
  61. struct mt7601u_rx_queue {
  62. struct mt7601u_dev *dev;
  63. struct mt7601u_dma_buf_rx {
  64. struct urb *urb;
  65. struct page *p;
  66. } e[N_RX_ENTRIES];
  67. unsigned int start;
  68. unsigned int end;
  69. unsigned int entries;
  70. unsigned int pending;
  71. };
  72. #define N_TX_ENTRIES 64
  73. struct mt7601u_tx_queue {
  74. struct mt7601u_dev *dev;
  75. struct mt7601u_dma_buf_tx {
  76. struct urb *urb;
  77. struct sk_buff *skb;
  78. } e[N_TX_ENTRIES];
  79. unsigned int start;
  80. unsigned int end;
  81. unsigned int entries;
  82. unsigned int used;
  83. unsigned int fifo_seq;
  84. };
  85. /* WCID allocation:
  86. * 0: mcast wcid
  87. * 1: bssid wcid
  88. * 1...: STAs
  89. * ...7e: group wcids
  90. * 7f: reserved
  91. */
  92. #define N_WCIDS 128
  93. #define GROUP_WCID(idx) (N_WCIDS - 2 - idx)
  94. struct mt7601u_eeprom_params;
  95. #define MT_EE_TEMPERATURE_SLOPE 39
  96. #define MT_FREQ_OFFSET_INVALID -128
  97. enum mt_temp_mode {
  98. MT_TEMP_MODE_NORMAL,
  99. MT_TEMP_MODE_HIGH,
  100. MT_TEMP_MODE_LOW,
  101. };
  102. enum mt_bw {
  103. MT_BW_20,
  104. MT_BW_40,
  105. };
  106. enum {
  107. MT7601U_STATE_INITIALIZED,
  108. MT7601U_STATE_REMOVED,
  109. MT7601U_STATE_WLAN_RUNNING,
  110. MT7601U_STATE_MCU_RUNNING,
  111. MT7601U_STATE_SCANNING,
  112. MT7601U_STATE_READING_STATS,
  113. MT7601U_STATE_MORE_STATS,
  114. };
  115. DECLARE_EWMA(rssi, 10, 4);
  116. /**
  117. * struct mt7601u_dev - adapter structure
  118. * @lock: protects @wcid->tx_rate.
  119. * @mac_lock: locks out mac80211's tx status and rx paths.
  120. * @tx_lock: protects @tx_q and changes of MT7601U_STATE_*_STATS
  121. * flags in @state.
  122. * @rx_lock: protects @rx_q.
  123. * @con_mon_lock: protects @ap_bssid, @bcn_*, @avg_rssi.
  124. * @mutex: ensures exclusive access from mac80211 callbacks.
  125. * @vendor_req_mutex: protects @vend_buf, ensures atomicity of read/write
  126. * accesses
  127. * @reg_atomic_mutex: ensures atomicity of indirect register accesses
  128. * (accesses to RF and BBP).
  129. * @hw_atomic_mutex: ensures exclusive access to HW during critical
  130. * operations (power management, channel switch).
  131. */
  132. struct mt7601u_dev {
  133. struct ieee80211_hw *hw;
  134. struct device *dev;
  135. unsigned long state;
  136. struct mutex mutex;
  137. unsigned long wcid_mask[N_WCIDS / BITS_PER_LONG];
  138. struct cfg80211_chan_def chandef;
  139. struct ieee80211_supported_band *sband_2g;
  140. struct mt7601u_mcu mcu;
  141. struct delayed_work cal_work;
  142. struct delayed_work mac_work;
  143. struct workqueue_struct *stat_wq;
  144. struct delayed_work stat_work;
  145. struct mt76_wcid *mon_wcid;
  146. struct mt76_wcid __rcu *wcid[N_WCIDS];
  147. spinlock_t lock;
  148. spinlock_t mac_lock;
  149. const u16 *beacon_offsets;
  150. u8 macaddr[ETH_ALEN];
  151. struct mt7601u_eeprom_params *ee;
  152. struct mutex vendor_req_mutex;
  153. void *vend_buf;
  154. struct mutex reg_atomic_mutex;
  155. struct mutex hw_atomic_mutex;
  156. u32 rxfilter;
  157. u32 debugfs_reg;
  158. u8 out_eps[8];
  159. u8 in_eps[8];
  160. u16 out_max_packet;
  161. u16 in_max_packet;
  162. /* TX */
  163. spinlock_t tx_lock;
  164. struct tasklet_struct tx_tasklet;
  165. struct mt7601u_tx_queue *tx_q;
  166. struct sk_buff_head tx_skb_done;
  167. atomic_t avg_ampdu_len;
  168. /* RX */
  169. spinlock_t rx_lock;
  170. struct tasklet_struct rx_tasklet;
  171. struct mt7601u_rx_queue rx_q;
  172. /* Connection monitoring things */
  173. spinlock_t con_mon_lock;
  174. u8 ap_bssid[ETH_ALEN];
  175. s8 bcn_freq_off;
  176. u8 bcn_phy_mode;
  177. struct ewma_rssi avg_rssi;
  178. u8 agc_save;
  179. struct mt7601u_freq_cal freq_cal;
  180. bool tssi_read_trig;
  181. s8 tssi_init;
  182. s8 tssi_init_hvga;
  183. s16 tssi_init_hvga_offset_db;
  184. int prev_pwr_diff;
  185. enum mt_temp_mode temp_mode;
  186. int curr_temp;
  187. int dpd_temp;
  188. s8 raw_temp;
  189. bool pll_lock_protect;
  190. u8 bw;
  191. bool chan_ext_below;
  192. /* PA mode */
  193. u32 rf_pa_mode[2];
  194. struct mac_stats stats;
  195. };
  196. struct mt7601u_tssi_params {
  197. char tssi0;
  198. int trgt_power;
  199. };
  200. struct mt76_wcid {
  201. u8 idx;
  202. u8 hw_key_idx;
  203. u16 tx_rate;
  204. bool tx_rate_set;
  205. u8 tx_rate_nss;
  206. };
  207. struct mt76_vif {
  208. u8 idx;
  209. struct mt76_wcid group_wcid;
  210. };
  211. struct mt76_sta {
  212. struct mt76_wcid wcid;
  213. u16 agg_ssn[IEEE80211_NUM_TIDS];
  214. };
  215. struct mt76_reg_pair {
  216. u32 reg;
  217. u32 value;
  218. };
  219. struct mt7601u_rxwi;
  220. extern const struct ieee80211_ops mt7601u_ops;
  221. void mt7601u_init_debugfs(struct mt7601u_dev *dev);
  222. u32 mt7601u_rr(struct mt7601u_dev *dev, u32 offset);
  223. void mt7601u_wr(struct mt7601u_dev *dev, u32 offset, u32 val);
  224. u32 mt7601u_rmw(struct mt7601u_dev *dev, u32 offset, u32 mask, u32 val);
  225. u32 mt7601u_rmc(struct mt7601u_dev *dev, u32 offset, u32 mask, u32 val);
  226. void mt7601u_wr_copy(struct mt7601u_dev *dev, u32 offset,
  227. const void *data, int len);
  228. int mt7601u_wait_asic_ready(struct mt7601u_dev *dev);
  229. bool mt76_poll(struct mt7601u_dev *dev, u32 offset, u32 mask, u32 val,
  230. int timeout);
  231. bool mt76_poll_msec(struct mt7601u_dev *dev, u32 offset, u32 mask, u32 val,
  232. int timeout);
  233. /* Compatibility with mt76 */
  234. #define mt76_rmw_field(_dev, _reg, _field, _val) \
  235. mt76_rmw(_dev, _reg, _field, FIELD_PREP(_field, _val))
  236. static inline u32 mt76_rr(struct mt7601u_dev *dev, u32 offset)
  237. {
  238. return mt7601u_rr(dev, offset);
  239. }
  240. static inline void mt76_wr(struct mt7601u_dev *dev, u32 offset, u32 val)
  241. {
  242. return mt7601u_wr(dev, offset, val);
  243. }
  244. static inline u32
  245. mt76_rmw(struct mt7601u_dev *dev, u32 offset, u32 mask, u32 val)
  246. {
  247. return mt7601u_rmw(dev, offset, mask, val);
  248. }
  249. static inline u32 mt76_set(struct mt7601u_dev *dev, u32 offset, u32 val)
  250. {
  251. return mt76_rmw(dev, offset, 0, val);
  252. }
  253. static inline u32 mt76_clear(struct mt7601u_dev *dev, u32 offset, u32 val)
  254. {
  255. return mt76_rmw(dev, offset, val, 0);
  256. }
  257. int mt7601u_write_reg_pairs(struct mt7601u_dev *dev, u32 base,
  258. const struct mt76_reg_pair *data, int len);
  259. int mt7601u_burst_write_regs(struct mt7601u_dev *dev, u32 offset,
  260. const u32 *data, int n);
  261. void mt7601u_addr_wr(struct mt7601u_dev *dev, const u32 offset, const u8 *addr);
  262. /* Init */
  263. struct mt7601u_dev *mt7601u_alloc_device(struct device *dev);
  264. int mt7601u_init_hardware(struct mt7601u_dev *dev);
  265. int mt7601u_register_device(struct mt7601u_dev *dev);
  266. void mt7601u_cleanup(struct mt7601u_dev *dev);
  267. int mt7601u_mac_start(struct mt7601u_dev *dev);
  268. void mt7601u_mac_stop(struct mt7601u_dev *dev);
  269. /* PHY */
  270. int mt7601u_phy_init(struct mt7601u_dev *dev);
  271. int mt7601u_wait_bbp_ready(struct mt7601u_dev *dev);
  272. void mt7601u_set_rx_path(struct mt7601u_dev *dev, u8 path);
  273. void mt7601u_set_tx_dac(struct mt7601u_dev *dev, u8 path);
  274. int mt7601u_bbp_set_bw(struct mt7601u_dev *dev, int bw);
  275. void mt7601u_agc_save(struct mt7601u_dev *dev);
  276. void mt7601u_agc_restore(struct mt7601u_dev *dev);
  277. int mt7601u_phy_set_channel(struct mt7601u_dev *dev,
  278. struct cfg80211_chan_def *chandef);
  279. void mt7601u_phy_recalibrate_after_assoc(struct mt7601u_dev *dev);
  280. int mt7601u_phy_get_rssi(struct mt7601u_dev *dev,
  281. struct mt7601u_rxwi *rxwi, u16 rate);
  282. void mt7601u_phy_con_cal_onoff(struct mt7601u_dev *dev,
  283. struct ieee80211_bss_conf *info);
  284. /* MAC */
  285. void mt7601u_mac_work(struct work_struct *work);
  286. void mt7601u_mac_set_protection(struct mt7601u_dev *dev, bool legacy_prot,
  287. int ht_mode);
  288. void mt7601u_mac_set_short_preamble(struct mt7601u_dev *dev, bool short_preamb);
  289. void mt7601u_mac_config_tsf(struct mt7601u_dev *dev, bool enable, int interval);
  290. void
  291. mt7601u_mac_wcid_setup(struct mt7601u_dev *dev, u8 idx, u8 vif_idx, u8 *mac);
  292. void mt7601u_mac_set_ampdu_factor(struct mt7601u_dev *dev);
  293. /* TX */
  294. void mt7601u_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
  295. struct sk_buff *skb);
  296. int mt7601u_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  297. u16 queue, const struct ieee80211_tx_queue_params *params);
  298. void mt7601u_tx_status(struct mt7601u_dev *dev, struct sk_buff *skb);
  299. void mt7601u_tx_stat(struct work_struct *work);
  300. /* util */
  301. void mt76_remove_hdr_pad(struct sk_buff *skb);
  302. int mt76_insert_hdr_pad(struct sk_buff *skb);
  303. u32 mt7601u_bbp_set_ctrlch(struct mt7601u_dev *dev, bool below);
  304. static inline u32 mt7601u_mac_set_ctrlch(struct mt7601u_dev *dev, bool below)
  305. {
  306. return mt7601u_rmc(dev, MT_TX_BAND_CFG, 1, below);
  307. }
  308. int mt7601u_dma_init(struct mt7601u_dev *dev);
  309. void mt7601u_dma_cleanup(struct mt7601u_dev *dev);
  310. int mt7601u_dma_enqueue_tx(struct mt7601u_dev *dev, struct sk_buff *skb,
  311. struct mt76_wcid *wcid, int hw_q);
  312. #endif