main.c 10 KB

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