mt7601u.h 9.3 KB

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