main.c 11 KB

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