main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. #include "mt7601u.h"
  15. #include "mac.h"
  16. #include <linux/etherdevice.h>
  17. #include <linux/version.h>
  18. static int mt7601u_start(struct ieee80211_hw *hw)
  19. {
  20. struct mt7601u_dev *dev = hw->priv;
  21. int ret;
  22. mutex_lock(&dev->mutex);
  23. ret = mt7601u_mac_start(dev);
  24. if (ret)
  25. goto out;
  26. ieee80211_queue_delayed_work(dev->hw, &dev->mac_work,
  27. MT_CALIBRATE_INTERVAL);
  28. ieee80211_queue_delayed_work(dev->hw, &dev->cal_work,
  29. MT_CALIBRATE_INTERVAL);
  30. out:
  31. mutex_unlock(&dev->mutex);
  32. return ret;
  33. }
  34. static void mt7601u_stop(struct ieee80211_hw *hw)
  35. {
  36. struct mt7601u_dev *dev = hw->priv;
  37. mutex_lock(&dev->mutex);
  38. cancel_delayed_work_sync(&dev->cal_work);
  39. cancel_delayed_work_sync(&dev->mac_work);
  40. mt7601u_mac_stop(dev);
  41. mutex_unlock(&dev->mutex);
  42. }
  43. static int mt7601u_add_interface(struct ieee80211_hw *hw,
  44. struct ieee80211_vif *vif)
  45. {
  46. struct mt7601u_dev *dev = hw->priv;
  47. struct mt76_vif *mvif = (struct mt76_vif *) vif->drv_priv;
  48. unsigned int idx = 0;
  49. unsigned int wcid = GROUP_WCID(idx);
  50. /* Note: for AP do the AP-STA things mt76 does:
  51. * - beacon offsets
  52. * - do mac address tricks
  53. * - shift vif idx
  54. */
  55. mvif->idx = idx;
  56. if (dev->wcid_mask[wcid / BITS_PER_LONG] & BIT(wcid % BITS_PER_LONG))
  57. return -ENOSPC;
  58. dev->wcid_mask[wcid / BITS_PER_LONG] |= BIT(wcid % BITS_PER_LONG);
  59. mvif->group_wcid.idx = wcid;
  60. mvif->group_wcid.hw_key_idx = -1;
  61. return 0;
  62. }
  63. static void mt7601u_remove_interface(struct ieee80211_hw *hw,
  64. struct ieee80211_vif *vif)
  65. {
  66. struct mt7601u_dev *dev = hw->priv;
  67. struct mt76_vif *mvif = (struct mt76_vif *) vif->drv_priv;
  68. unsigned int wcid = mvif->group_wcid.idx;
  69. dev->wcid_mask[wcid / BITS_PER_LONG] &= ~BIT(wcid % BITS_PER_LONG);
  70. }
  71. static int mt7601u_config(struct ieee80211_hw *hw, u32 changed)
  72. {
  73. struct mt7601u_dev *dev = hw->priv;
  74. int ret = 0;
  75. mutex_lock(&dev->mutex);
  76. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  77. ieee80211_stop_queues(hw);
  78. ret = mt7601u_phy_set_channel(dev, &hw->conf.chandef);
  79. ieee80211_wake_queues(hw);
  80. }
  81. mutex_unlock(&dev->mutex);
  82. return ret;
  83. }
  84. static void
  85. mt76_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
  86. unsigned int *total_flags, u64 multicast)
  87. {
  88. struct mt7601u_dev *dev = hw->priv;
  89. u32 flags = 0;
  90. #define MT76_FILTER(_flag, _hw) do { \
  91. flags |= *total_flags & FIF_##_flag; \
  92. dev->rxfilter &= ~(_hw); \
  93. dev->rxfilter |= !(flags & FIF_##_flag) * (_hw); \
  94. } while (0)
  95. mutex_lock(&dev->mutex);
  96. dev->rxfilter &= ~MT_RX_FILTR_CFG_OTHER_BSS;
  97. MT76_FILTER(OTHER_BSS, MT_RX_FILTR_CFG_PROMISC);
  98. MT76_FILTER(FCSFAIL, MT_RX_FILTR_CFG_CRC_ERR);
  99. MT76_FILTER(PLCPFAIL, MT_RX_FILTR_CFG_PHY_ERR);
  100. MT76_FILTER(CONTROL, MT_RX_FILTR_CFG_ACK |
  101. MT_RX_FILTR_CFG_CTS |
  102. MT_RX_FILTR_CFG_CFEND |
  103. MT_RX_FILTR_CFG_CFACK |
  104. MT_RX_FILTR_CFG_BA |
  105. MT_RX_FILTR_CFG_CTRL_RSV);
  106. MT76_FILTER(PSPOLL, MT_RX_FILTR_CFG_PSPOLL);
  107. *total_flags = flags;
  108. mt76_wr(dev, MT_RX_FILTR_CFG, dev->rxfilter);
  109. mutex_unlock(&dev->mutex);
  110. }
  111. static void
  112. mt7601u_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  113. struct ieee80211_bss_conf *info, u32 changed)
  114. {
  115. struct mt7601u_dev *dev = hw->priv;
  116. mutex_lock(&dev->mutex);
  117. if (changed & BSS_CHANGED_ASSOC)
  118. mt7601u_phy_con_cal_onoff(dev, info);
  119. if (changed & BSS_CHANGED_BSSID) {
  120. mt7601u_addr_wr(dev, MT_MAC_BSSID_DW0, info->bssid);
  121. /* Note: this is a hack because beacon_int is not changed
  122. * on leave nor is any more appropriate event generated.
  123. * rt2x00 doesn't seem to be bothered though.
  124. */
  125. if (is_zero_ether_addr(info->bssid))
  126. mt7601u_mac_config_tsf(dev, false, 0);
  127. }
  128. if (changed & BSS_CHANGED_BASIC_RATES) {
  129. mt7601u_wr(dev, MT_LEGACY_BASIC_RATE, info->basic_rates);
  130. mt7601u_wr(dev, MT_HT_FBK_CFG0, 0x65432100);
  131. mt7601u_wr(dev, MT_HT_FBK_CFG1, 0xedcba980);
  132. mt7601u_wr(dev, MT_LG_FBK_CFG0, 0xedcba988);
  133. mt7601u_wr(dev, MT_LG_FBK_CFG1, 0x00002100);
  134. }
  135. if (changed & BSS_CHANGED_BEACON_INT)
  136. mt7601u_mac_config_tsf(dev, true, info->beacon_int);
  137. if (changed & BSS_CHANGED_HT || changed & BSS_CHANGED_ERP_CTS_PROT)
  138. mt7601u_mac_set_protection(dev, info->use_cts_prot,
  139. info->ht_operation_mode);
  140. if (changed & BSS_CHANGED_ERP_PREAMBLE)
  141. mt7601u_mac_set_short_preamble(dev, info->use_short_preamble);
  142. if (changed & BSS_CHANGED_ERP_SLOT) {
  143. int slottime = info->use_short_slot ? 9 : 20;
  144. mt76_rmw_field(dev, MT_BKOFF_SLOT_CFG,
  145. MT_BKOFF_SLOT_CFG_SLOTTIME, slottime);
  146. }
  147. if (changed & BSS_CHANGED_ASSOC)
  148. mt7601u_phy_recalibrate_after_assoc(dev);
  149. mutex_unlock(&dev->mutex);
  150. }
  151. static int
  152. mt76_wcid_alloc(struct mt7601u_dev *dev)
  153. {
  154. int i, idx = 0;
  155. for (i = 0; i < ARRAY_SIZE(dev->wcid_mask); i++) {
  156. idx = ffs(~dev->wcid_mask[i]);
  157. if (!idx)
  158. continue;
  159. idx--;
  160. dev->wcid_mask[i] |= BIT(idx);
  161. break;
  162. }
  163. idx = i * BITS_PER_LONG + idx;
  164. if (idx > 119)
  165. return -1;
  166. return idx;
  167. }
  168. static int
  169. mt7601u_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  170. struct ieee80211_sta *sta)
  171. {
  172. struct mt7601u_dev *dev = hw->priv;
  173. struct mt76_sta *msta = (struct mt76_sta *) sta->drv_priv;
  174. struct mt76_vif *mvif = (struct mt76_vif *) vif->drv_priv;
  175. int ret = 0;
  176. int idx = 0;
  177. mutex_lock(&dev->mutex);
  178. idx = mt76_wcid_alloc(dev);
  179. if (idx < 0) {
  180. ret = -ENOSPC;
  181. goto out;
  182. }
  183. msta->wcid.idx = idx;
  184. msta->wcid.hw_key_idx = -1;
  185. mt7601u_mac_wcid_setup(dev, idx, mvif->idx, sta->addr);
  186. mt76_clear(dev, MT_WCID_DROP(idx), MT_WCID_DROP_MASK(idx));
  187. rcu_assign_pointer(dev->wcid[idx], &msta->wcid);
  188. mt7601u_mac_set_ampdu_factor(dev);
  189. out:
  190. mutex_unlock(&dev->mutex);
  191. return ret;
  192. }
  193. static int
  194. mt7601u_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  195. struct ieee80211_sta *sta)
  196. {
  197. struct mt7601u_dev *dev = hw->priv;
  198. struct mt76_sta *msta = (struct mt76_sta *) sta->drv_priv;
  199. int idx = msta->wcid.idx;
  200. mutex_lock(&dev->mutex);
  201. rcu_assign_pointer(dev->wcid[idx], NULL);
  202. mt76_set(dev, MT_WCID_DROP(idx), MT_WCID_DROP_MASK(idx));
  203. dev->wcid_mask[idx / BITS_PER_LONG] &= ~BIT(idx % BITS_PER_LONG);
  204. mt7601u_mac_wcid_setup(dev, idx, 0, NULL);
  205. mt7601u_mac_set_ampdu_factor(dev);
  206. mutex_unlock(&dev->mutex);
  207. return 0;
  208. }
  209. static void
  210. mt7601u_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  211. enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
  212. {
  213. }
  214. static void
  215. mt7601u_sw_scan(struct ieee80211_hw *hw,
  216. struct ieee80211_vif *vif,
  217. const u8 *mac_addr)
  218. {
  219. struct mt7601u_dev *dev = hw->priv;
  220. mt7601u_agc_save(dev);
  221. set_bit(MT7601U_STATE_SCANNING, &dev->state);
  222. }
  223. static void
  224. mt7601u_sw_scan_complete(struct ieee80211_hw *hw,
  225. struct ieee80211_vif *vif)
  226. {
  227. struct mt7601u_dev *dev = hw->priv;
  228. mt7601u_agc_restore(dev);
  229. clear_bit(MT7601U_STATE_SCANNING, &dev->state);
  230. }
  231. static int
  232. mt7601u_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  233. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  234. struct ieee80211_key_conf *key)
  235. {
  236. struct mt7601u_dev *dev = hw->priv;
  237. struct mt76_vif *mvif = (struct mt76_vif *) vif->drv_priv;
  238. struct mt76_sta *msta = sta ? (struct mt76_sta *) sta->drv_priv : NULL;
  239. struct mt76_wcid *wcid = msta ? &msta->wcid : &mvif->group_wcid;
  240. int idx = key->keyidx;
  241. int ret;
  242. if (cmd == SET_KEY) {
  243. key->hw_key_idx = wcid->idx;
  244. wcid->hw_key_idx = idx;
  245. } else {
  246. if (idx == wcid->hw_key_idx)
  247. wcid->hw_key_idx = -1;
  248. key = NULL;
  249. }
  250. if (!msta) {
  251. if (key || wcid->hw_key_idx == idx) {
  252. ret = mt76_mac_wcid_set_key(dev, wcid->idx, key);
  253. if (ret)
  254. return ret;
  255. }
  256. return mt76_mac_shared_key_setup(dev, mvif->idx, idx, key);
  257. }
  258. return mt76_mac_wcid_set_key(dev, msta->wcid.idx, key);
  259. }
  260. static int mt7601u_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  261. {
  262. struct mt7601u_dev *dev = hw->priv;
  263. mt76_rmw_field(dev, MT_TX_RTS_CFG, MT_TX_RTS_CFG_THRESH, value);
  264. return 0;
  265. }
  266. static int
  267. mt76_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  268. struct ieee80211_ampdu_params *params)
  269. {
  270. struct mt7601u_dev *dev = hw->priv;
  271. struct ieee80211_sta *sta = params->sta;
  272. enum ieee80211_ampdu_mlme_action action = params->action;
  273. u16 tid = params->tid;
  274. u16 *ssn = &params->ssn;
  275. struct mt76_sta *msta = (struct mt76_sta *) sta->drv_priv;
  276. WARN_ON(msta->wcid.idx > GROUP_WCID(0));
  277. switch (action) {
  278. case IEEE80211_AMPDU_RX_START:
  279. mt76_set(dev, MT_WCID_ADDR(msta->wcid.idx) + 4, BIT(16 + tid));
  280. break;
  281. case IEEE80211_AMPDU_RX_STOP:
  282. mt76_clear(dev, MT_WCID_ADDR(msta->wcid.idx) + 4,
  283. BIT(16 + tid));
  284. break;
  285. case IEEE80211_AMPDU_TX_OPERATIONAL:
  286. ieee80211_send_bar(vif, sta->addr, tid, msta->agg_ssn[tid]);
  287. break;
  288. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  289. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  290. break;
  291. case IEEE80211_AMPDU_TX_START:
  292. msta->agg_ssn[tid] = *ssn << 4;
  293. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  294. break;
  295. case IEEE80211_AMPDU_TX_STOP_CONT:
  296. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  297. break;
  298. }
  299. return 0;
  300. }
  301. static void
  302. mt76_sta_rate_tbl_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  303. struct ieee80211_sta *sta)
  304. {
  305. struct mt7601u_dev *dev = hw->priv;
  306. struct mt76_sta *msta = (struct mt76_sta *) sta->drv_priv;
  307. struct ieee80211_sta_rates *rates;
  308. struct ieee80211_tx_rate rate = {};
  309. rcu_read_lock();
  310. rates = rcu_dereference(sta->rates);
  311. if (!rates)
  312. goto out;
  313. rate.idx = rates->rate[0].idx;
  314. rate.flags = rates->rate[0].flags;
  315. mt76_mac_wcid_set_rate(dev, &msta->wcid, &rate);
  316. out:
  317. rcu_read_unlock();
  318. }
  319. const struct ieee80211_ops mt7601u_ops = {
  320. .tx = mt7601u_tx,
  321. .start = mt7601u_start,
  322. .stop = mt7601u_stop,
  323. .add_interface = mt7601u_add_interface,
  324. .remove_interface = mt7601u_remove_interface,
  325. .config = mt7601u_config,
  326. .configure_filter = mt76_configure_filter,
  327. .bss_info_changed = mt7601u_bss_info_changed,
  328. .sta_add = mt7601u_sta_add,
  329. .sta_remove = mt7601u_sta_remove,
  330. .sta_notify = mt7601u_sta_notify,
  331. .set_key = mt7601u_set_key,
  332. .conf_tx = mt7601u_conf_tx,
  333. .sw_scan_start = mt7601u_sw_scan,
  334. .sw_scan_complete = mt7601u_sw_scan_complete,
  335. .ampdu_action = mt76_ampdu_action,
  336. .sta_rate_tbl_update = mt76_sta_rate_tbl_update,
  337. .set_rts_threshold = mt7601u_set_rts_threshold,
  338. };