mac.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 "trace.h"
  16. #include <linux/etherdevice.h>
  17. void mt7601u_set_macaddr(struct mt7601u_dev *dev, const u8 *addr)
  18. {
  19. ether_addr_copy(dev->macaddr, addr);
  20. if (!is_valid_ether_addr(dev->macaddr)) {
  21. eth_random_addr(dev->macaddr);
  22. dev_info(dev->dev,
  23. "Invalid MAC address, using random address %pM\n",
  24. dev->macaddr);
  25. }
  26. mt76_wr(dev, MT_MAC_ADDR_DW0, get_unaligned_le32(dev->macaddr));
  27. mt76_wr(dev, MT_MAC_ADDR_DW1, get_unaligned_le16(dev->macaddr + 4) |
  28. FIELD_PREP(MT_MAC_ADDR_DW1_U2ME_MASK, 0xff));
  29. }
  30. static void
  31. mt76_mac_process_tx_rate(struct ieee80211_tx_rate *txrate, u16 rate)
  32. {
  33. u8 idx = FIELD_GET(MT_TXWI_RATE_MCS, rate);
  34. txrate->idx = 0;
  35. txrate->flags = 0;
  36. txrate->count = 1;
  37. switch (FIELD_GET(MT_TXWI_RATE_PHY_MODE, rate)) {
  38. case MT_PHY_TYPE_OFDM:
  39. txrate->idx = idx + 4;
  40. return;
  41. case MT_PHY_TYPE_CCK:
  42. if (idx >= 8)
  43. idx -= 8;
  44. txrate->idx = idx;
  45. return;
  46. case MT_PHY_TYPE_HT_GF:
  47. txrate->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  48. /* fall through */
  49. case MT_PHY_TYPE_HT:
  50. txrate->flags |= IEEE80211_TX_RC_MCS;
  51. txrate->idx = idx;
  52. break;
  53. default:
  54. WARN_ON(1);
  55. return;
  56. }
  57. if (FIELD_GET(MT_TXWI_RATE_BW, rate) == MT_PHY_BW_40)
  58. txrate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  59. if (rate & MT_TXWI_RATE_SGI)
  60. txrate->flags |= IEEE80211_TX_RC_SHORT_GI;
  61. }
  62. static void
  63. mt76_mac_fill_tx_status(struct mt7601u_dev *dev, struct ieee80211_tx_info *info,
  64. struct mt76_tx_status *st)
  65. {
  66. struct ieee80211_tx_rate *rate = info->status.rates;
  67. int cur_idx, last_rate;
  68. int i;
  69. last_rate = min_t(int, st->retry, IEEE80211_TX_MAX_RATES - 1);
  70. mt76_mac_process_tx_rate(&rate[last_rate], st->rate);
  71. if (last_rate < IEEE80211_TX_MAX_RATES - 1)
  72. rate[last_rate + 1].idx = -1;
  73. cur_idx = rate[last_rate].idx + st->retry;
  74. for (i = 0; i <= last_rate; i++) {
  75. rate[i].flags = rate[last_rate].flags;
  76. rate[i].idx = max_t(int, 0, cur_idx - i);
  77. rate[i].count = 1;
  78. }
  79. if (last_rate > 0)
  80. rate[last_rate - 1].count = st->retry + 1 - last_rate;
  81. info->status.ampdu_len = 1;
  82. info->status.ampdu_ack_len = st->success;
  83. if (st->is_probe)
  84. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  85. if (st->aggr)
  86. info->flags |= IEEE80211_TX_CTL_AMPDU |
  87. IEEE80211_TX_STAT_AMPDU;
  88. if (!st->ack_req)
  89. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  90. else if (st->success)
  91. info->flags |= IEEE80211_TX_STAT_ACK;
  92. }
  93. u16 mt76_mac_tx_rate_val(struct mt7601u_dev *dev,
  94. const struct ieee80211_tx_rate *rate, u8 *nss_val)
  95. {
  96. u16 rateval;
  97. u8 phy, rate_idx;
  98. u8 nss = 1;
  99. u8 bw = 0;
  100. if (rate->flags & IEEE80211_TX_RC_MCS) {
  101. rate_idx = rate->idx;
  102. nss = 1 + (rate->idx >> 3);
  103. phy = MT_PHY_TYPE_HT;
  104. if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  105. phy = MT_PHY_TYPE_HT_GF;
  106. if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  107. bw = 1;
  108. } else {
  109. const struct ieee80211_rate *r;
  110. int band = dev->chandef.chan->band;
  111. u16 val;
  112. r = &dev->hw->wiphy->bands[band]->bitrates[rate->idx];
  113. if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  114. val = r->hw_value_short;
  115. else
  116. val = r->hw_value;
  117. phy = val >> 8;
  118. rate_idx = val & 0xff;
  119. bw = 0;
  120. }
  121. rateval = FIELD_PREP(MT_RXWI_RATE_MCS, rate_idx);
  122. rateval |= FIELD_PREP(MT_RXWI_RATE_PHY, phy);
  123. rateval |= FIELD_PREP(MT_RXWI_RATE_BW, bw);
  124. if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
  125. rateval |= MT_RXWI_RATE_SGI;
  126. *nss_val = nss;
  127. return rateval;
  128. }
  129. void mt76_mac_wcid_set_rate(struct mt7601u_dev *dev, struct mt76_wcid *wcid,
  130. const struct ieee80211_tx_rate *rate)
  131. {
  132. unsigned long flags;
  133. spin_lock_irqsave(&dev->lock, flags);
  134. wcid->tx_rate = mt76_mac_tx_rate_val(dev, rate, &wcid->tx_rate_nss);
  135. wcid->tx_rate_set = true;
  136. spin_unlock_irqrestore(&dev->lock, flags);
  137. }
  138. struct mt76_tx_status mt7601u_mac_fetch_tx_status(struct mt7601u_dev *dev)
  139. {
  140. struct mt76_tx_status stat = {};
  141. u32 val;
  142. val = mt7601u_rr(dev, MT_TX_STAT_FIFO);
  143. stat.valid = !!(val & MT_TX_STAT_FIFO_VALID);
  144. stat.success = !!(val & MT_TX_STAT_FIFO_SUCCESS);
  145. stat.aggr = !!(val & MT_TX_STAT_FIFO_AGGR);
  146. stat.ack_req = !!(val & MT_TX_STAT_FIFO_ACKREQ);
  147. stat.pktid = FIELD_GET(MT_TX_STAT_FIFO_PID_TYPE, val);
  148. stat.wcid = FIELD_GET(MT_TX_STAT_FIFO_WCID, val);
  149. stat.rate = FIELD_GET(MT_TX_STAT_FIFO_RATE, val);
  150. return stat;
  151. }
  152. void mt76_send_tx_status(struct mt7601u_dev *dev, struct mt76_tx_status *stat)
  153. {
  154. struct ieee80211_tx_info info = {};
  155. struct ieee80211_sta *sta = NULL;
  156. struct mt76_wcid *wcid = NULL;
  157. void *msta;
  158. rcu_read_lock();
  159. if (stat->wcid < ARRAY_SIZE(dev->wcid))
  160. wcid = rcu_dereference(dev->wcid[stat->wcid]);
  161. if (wcid) {
  162. msta = container_of(wcid, struct mt76_sta, wcid);
  163. sta = container_of(msta, struct ieee80211_sta,
  164. drv_priv);
  165. }
  166. mt76_mac_fill_tx_status(dev, &info, stat);
  167. spin_lock_bh(&dev->mac_lock);
  168. ieee80211_tx_status_noskb(dev->hw, sta, &info);
  169. spin_unlock_bh(&dev->mac_lock);
  170. rcu_read_unlock();
  171. }
  172. void mt7601u_mac_set_protection(struct mt7601u_dev *dev, bool legacy_prot,
  173. int ht_mode)
  174. {
  175. int mode = ht_mode & IEEE80211_HT_OP_MODE_PROTECTION;
  176. bool non_gf = !!(ht_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  177. u32 prot[6];
  178. bool ht_rts[4] = {};
  179. int i;
  180. prot[0] = MT_PROT_NAV_SHORT |
  181. MT_PROT_TXOP_ALLOW_ALL |
  182. MT_PROT_RTS_THR_EN;
  183. prot[1] = prot[0];
  184. if (legacy_prot)
  185. prot[1] |= MT_PROT_CTRL_CTS2SELF;
  186. prot[2] = prot[4] = MT_PROT_NAV_SHORT | MT_PROT_TXOP_ALLOW_BW20;
  187. prot[3] = prot[5] = MT_PROT_NAV_SHORT | MT_PROT_TXOP_ALLOW_ALL;
  188. if (legacy_prot) {
  189. prot[2] |= MT_PROT_RATE_CCK_11;
  190. prot[3] |= MT_PROT_RATE_CCK_11;
  191. prot[4] |= MT_PROT_RATE_CCK_11;
  192. prot[5] |= MT_PROT_RATE_CCK_11;
  193. } else {
  194. prot[2] |= MT_PROT_RATE_OFDM_24;
  195. prot[3] |= MT_PROT_RATE_DUP_OFDM_24;
  196. prot[4] |= MT_PROT_RATE_OFDM_24;
  197. prot[5] |= MT_PROT_RATE_DUP_OFDM_24;
  198. }
  199. switch (mode) {
  200. case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
  201. break;
  202. case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
  203. ht_rts[0] = ht_rts[1] = ht_rts[2] = ht_rts[3] = true;
  204. break;
  205. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  206. ht_rts[1] = ht_rts[3] = true;
  207. break;
  208. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  209. ht_rts[0] = ht_rts[1] = ht_rts[2] = ht_rts[3] = true;
  210. break;
  211. }
  212. if (non_gf)
  213. ht_rts[2] = ht_rts[3] = true;
  214. for (i = 0; i < 4; i++)
  215. if (ht_rts[i])
  216. prot[i + 2] |= MT_PROT_CTRL_RTS_CTS;
  217. for (i = 0; i < 6; i++)
  218. mt7601u_wr(dev, MT_CCK_PROT_CFG + i * 4, prot[i]);
  219. }
  220. void mt7601u_mac_set_short_preamble(struct mt7601u_dev *dev, bool short_preamb)
  221. {
  222. if (short_preamb)
  223. mt76_set(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_PREAMB_SHORT);
  224. else
  225. mt76_clear(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_PREAMB_SHORT);
  226. }
  227. void mt7601u_mac_config_tsf(struct mt7601u_dev *dev, bool enable, int interval)
  228. {
  229. u32 val = mt7601u_rr(dev, MT_BEACON_TIME_CFG);
  230. val &= ~(MT_BEACON_TIME_CFG_TIMER_EN |
  231. MT_BEACON_TIME_CFG_SYNC_MODE |
  232. MT_BEACON_TIME_CFG_TBTT_EN);
  233. if (!enable) {
  234. mt7601u_wr(dev, MT_BEACON_TIME_CFG, val);
  235. return;
  236. }
  237. val &= ~MT_BEACON_TIME_CFG_INTVAL;
  238. val |= FIELD_PREP(MT_BEACON_TIME_CFG_INTVAL, interval << 4) |
  239. MT_BEACON_TIME_CFG_TIMER_EN |
  240. MT_BEACON_TIME_CFG_SYNC_MODE |
  241. MT_BEACON_TIME_CFG_TBTT_EN;
  242. }
  243. static void mt7601u_check_mac_err(struct mt7601u_dev *dev)
  244. {
  245. u32 val = mt7601u_rr(dev, 0x10f4);
  246. if (!(val & BIT(29)) || !(val & (BIT(7) | BIT(5))))
  247. return;
  248. dev_err(dev->dev, "Error: MAC specific condition occurred\n");
  249. mt76_set(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_RESET_CSR);
  250. udelay(10);
  251. mt76_clear(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_RESET_CSR);
  252. }
  253. void mt7601u_mac_work(struct work_struct *work)
  254. {
  255. struct mt7601u_dev *dev = container_of(work, struct mt7601u_dev,
  256. mac_work.work);
  257. struct {
  258. u32 addr_base;
  259. u32 span;
  260. u64 *stat_base;
  261. } spans[] = {
  262. { MT_RX_STA_CNT0, 3, dev->stats.rx_stat },
  263. { MT_TX_STA_CNT0, 3, dev->stats.tx_stat },
  264. { MT_TX_AGG_STAT, 1, dev->stats.aggr_stat },
  265. { MT_MPDU_DENSITY_CNT, 1, dev->stats.zero_len_del },
  266. { MT_TX_AGG_CNT_BASE0, 8, &dev->stats.aggr_n[0] },
  267. { MT_TX_AGG_CNT_BASE1, 8, &dev->stats.aggr_n[16] },
  268. };
  269. u32 sum, n;
  270. int i, j, k;
  271. /* Note: using MCU_RANDOM_READ is actually slower then reading all the
  272. * registers by hand. MCU takes ca. 20ms to complete read of 24
  273. * registers while reading them one by one will takes roughly
  274. * 24*200us =~ 5ms.
  275. */
  276. k = 0;
  277. n = 0;
  278. sum = 0;
  279. for (i = 0; i < ARRAY_SIZE(spans); i++)
  280. for (j = 0; j < spans[i].span; j++) {
  281. u32 val = mt7601u_rr(dev, spans[i].addr_base + j * 4);
  282. spans[i].stat_base[j * 2] += val & 0xffff;
  283. spans[i].stat_base[j * 2 + 1] += val >> 16;
  284. /* Calculate average AMPDU length */
  285. if (spans[i].addr_base != MT_TX_AGG_CNT_BASE0 &&
  286. spans[i].addr_base != MT_TX_AGG_CNT_BASE1)
  287. continue;
  288. n += (val >> 16) + (val & 0xffff);
  289. sum += (val & 0xffff) * (1 + k * 2) +
  290. (val >> 16) * (2 + k * 2);
  291. k++;
  292. }
  293. atomic_set(&dev->avg_ampdu_len, n ? DIV_ROUND_CLOSEST(sum, n) : 1);
  294. mt7601u_check_mac_err(dev);
  295. ieee80211_queue_delayed_work(dev->hw, &dev->mac_work, 10 * HZ);
  296. }
  297. void
  298. mt7601u_mac_wcid_setup(struct mt7601u_dev *dev, u8 idx, u8 vif_idx, u8 *mac)
  299. {
  300. u8 zmac[ETH_ALEN] = {};
  301. u32 attr;
  302. attr = FIELD_PREP(MT_WCID_ATTR_BSS_IDX, vif_idx & 7) |
  303. FIELD_PREP(MT_WCID_ATTR_BSS_IDX_EXT, !!(vif_idx & 8));
  304. mt76_wr(dev, MT_WCID_ATTR(idx), attr);
  305. if (mac)
  306. memcpy(zmac, mac, sizeof(zmac));
  307. mt7601u_addr_wr(dev, MT_WCID_ADDR(idx), zmac);
  308. }
  309. void mt7601u_mac_set_ampdu_factor(struct mt7601u_dev *dev)
  310. {
  311. struct ieee80211_sta *sta;
  312. struct mt76_wcid *wcid;
  313. void *msta;
  314. u8 min_factor = 3;
  315. int i;
  316. rcu_read_lock();
  317. for (i = 0; i < ARRAY_SIZE(dev->wcid); i++) {
  318. wcid = rcu_dereference(dev->wcid[i]);
  319. if (!wcid)
  320. continue;
  321. msta = container_of(wcid, struct mt76_sta, wcid);
  322. sta = container_of(msta, struct ieee80211_sta, drv_priv);
  323. min_factor = min(min_factor, sta->ht_cap.ampdu_factor);
  324. }
  325. rcu_read_unlock();
  326. mt7601u_wr(dev, MT_MAX_LEN_CFG, 0xa0fff |
  327. FIELD_PREP(MT_MAX_LEN_CFG_AMPDU, min_factor));
  328. }
  329. static void
  330. mt76_mac_process_rate(struct ieee80211_rx_status *status, u16 rate)
  331. {
  332. u8 idx = FIELD_GET(MT_RXWI_RATE_MCS, rate);
  333. switch (FIELD_GET(MT_RXWI_RATE_PHY, rate)) {
  334. case MT_PHY_TYPE_OFDM:
  335. if (WARN_ON(idx >= 8))
  336. idx = 0;
  337. idx += 4;
  338. status->rate_idx = idx;
  339. return;
  340. case MT_PHY_TYPE_CCK:
  341. if (idx >= 8) {
  342. idx -= 8;
  343. status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
  344. }
  345. if (WARN_ON(idx >= 4))
  346. idx = 0;
  347. status->rate_idx = idx;
  348. return;
  349. case MT_PHY_TYPE_HT_GF:
  350. status->enc_flags |= RX_ENC_FLAG_HT_GF;
  351. /* fall through */
  352. case MT_PHY_TYPE_HT:
  353. status->encoding = RX_ENC_HT;
  354. status->rate_idx = idx;
  355. break;
  356. default:
  357. WARN_ON(1);
  358. return;
  359. }
  360. if (rate & MT_RXWI_RATE_SGI)
  361. status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
  362. if (rate & MT_RXWI_RATE_STBC)
  363. status->enc_flags |= 1 << RX_ENC_FLAG_STBC_SHIFT;
  364. if (rate & MT_RXWI_RATE_BW)
  365. status->bw = RATE_INFO_BW_40;
  366. }
  367. static void
  368. mt7601u_rx_monitor_beacon(struct mt7601u_dev *dev, struct mt7601u_rxwi *rxwi,
  369. u16 rate, int rssi)
  370. {
  371. dev->bcn_freq_off = rxwi->freq_off;
  372. dev->bcn_phy_mode = FIELD_GET(MT_RXWI_RATE_PHY, rate);
  373. ewma_rssi_add(&dev->avg_rssi, -rssi);
  374. }
  375. static int
  376. mt7601u_rx_is_our_beacon(struct mt7601u_dev *dev, u8 *data)
  377. {
  378. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
  379. return ieee80211_is_beacon(hdr->frame_control) &&
  380. ether_addr_equal(hdr->addr2, dev->ap_bssid);
  381. }
  382. u32 mt76_mac_process_rx(struct mt7601u_dev *dev, struct sk_buff *skb,
  383. u8 *data, void *rxi)
  384. {
  385. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  386. struct mt7601u_rxwi *rxwi = rxi;
  387. u32 len, ctl = le32_to_cpu(rxwi->ctl);
  388. u16 rate = le16_to_cpu(rxwi->rate);
  389. int rssi;
  390. len = FIELD_GET(MT_RXWI_CTL_MPDU_LEN, ctl);
  391. if (len < 10)
  392. return 0;
  393. if (rxwi->rxinfo & cpu_to_le32(MT_RXINFO_DECRYPT)) {
  394. status->flag |= RX_FLAG_DECRYPTED;
  395. status->flag |= RX_FLAG_MMIC_STRIPPED;
  396. status->flag |= RX_FLAG_MIC_STRIPPED;
  397. status->flag |= RX_FLAG_ICV_STRIPPED;
  398. status->flag |= RX_FLAG_IV_STRIPPED;
  399. }
  400. /* let mac80211 take care of PN validation since apparently
  401. * the hardware does not support it
  402. */
  403. if (rxwi->rxinfo & cpu_to_le32(MT_RXINFO_PN_LEN))
  404. status->flag &= ~RX_FLAG_IV_STRIPPED;
  405. status->chains = BIT(0);
  406. rssi = mt7601u_phy_get_rssi(dev, rxwi, rate);
  407. status->chain_signal[0] = status->signal = rssi;
  408. status->freq = dev->chandef.chan->center_freq;
  409. status->band = dev->chandef.chan->band;
  410. mt76_mac_process_rate(status, rate);
  411. spin_lock_bh(&dev->con_mon_lock);
  412. if (mt7601u_rx_is_our_beacon(dev, data))
  413. mt7601u_rx_monitor_beacon(dev, rxwi, rate, rssi);
  414. else if (rxwi->rxinfo & cpu_to_le32(MT_RXINFO_U2M))
  415. ewma_rssi_add(&dev->avg_rssi, -rssi);
  416. spin_unlock_bh(&dev->con_mon_lock);
  417. return len;
  418. }
  419. static enum mt76_cipher_type
  420. mt76_mac_get_key_info(struct ieee80211_key_conf *key, u8 *key_data)
  421. {
  422. memset(key_data, 0, 32);
  423. if (!key)
  424. return MT_CIPHER_NONE;
  425. if (key->keylen > 32)
  426. return MT_CIPHER_NONE;
  427. memcpy(key_data, key->key, key->keylen);
  428. switch (key->cipher) {
  429. case WLAN_CIPHER_SUITE_WEP40:
  430. return MT_CIPHER_WEP40;
  431. case WLAN_CIPHER_SUITE_WEP104:
  432. return MT_CIPHER_WEP104;
  433. case WLAN_CIPHER_SUITE_TKIP:
  434. return MT_CIPHER_TKIP;
  435. case WLAN_CIPHER_SUITE_CCMP:
  436. return MT_CIPHER_AES_CCMP;
  437. default:
  438. return MT_CIPHER_NONE;
  439. }
  440. }
  441. int mt76_mac_wcid_set_key(struct mt7601u_dev *dev, u8 idx,
  442. struct ieee80211_key_conf *key)
  443. {
  444. enum mt76_cipher_type cipher;
  445. u8 key_data[32];
  446. u8 iv_data[8];
  447. u32 val;
  448. cipher = mt76_mac_get_key_info(key, key_data);
  449. if (cipher == MT_CIPHER_NONE && key)
  450. return -EINVAL;
  451. trace_set_key(dev, idx);
  452. mt7601u_wr_copy(dev, MT_WCID_KEY(idx), key_data, sizeof(key_data));
  453. memset(iv_data, 0, sizeof(iv_data));
  454. if (key) {
  455. iv_data[3] = key->keyidx << 6;
  456. if (cipher >= MT_CIPHER_TKIP) {
  457. /* Note: start with 1 to comply with spec,
  458. * (see comment on common/cmm_wpa.c:4291).
  459. */
  460. iv_data[0] |= 1;
  461. iv_data[3] |= 0x20;
  462. }
  463. }
  464. mt7601u_wr_copy(dev, MT_WCID_IV(idx), iv_data, sizeof(iv_data));
  465. val = mt7601u_rr(dev, MT_WCID_ATTR(idx));
  466. val &= ~MT_WCID_ATTR_PKEY_MODE & ~MT_WCID_ATTR_PKEY_MODE_EXT;
  467. val |= FIELD_PREP(MT_WCID_ATTR_PKEY_MODE, cipher & 7) |
  468. FIELD_PREP(MT_WCID_ATTR_PKEY_MODE_EXT, cipher >> 3);
  469. val &= ~MT_WCID_ATTR_PAIRWISE;
  470. val |= MT_WCID_ATTR_PAIRWISE *
  471. !!(key && key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  472. mt7601u_wr(dev, MT_WCID_ATTR(idx), val);
  473. return 0;
  474. }
  475. int mt76_mac_shared_key_setup(struct mt7601u_dev *dev, u8 vif_idx, u8 key_idx,
  476. struct ieee80211_key_conf *key)
  477. {
  478. enum mt76_cipher_type cipher;
  479. u8 key_data[32];
  480. u32 val;
  481. cipher = mt76_mac_get_key_info(key, key_data);
  482. if (cipher == MT_CIPHER_NONE && key)
  483. return -EINVAL;
  484. trace_set_shared_key(dev, vif_idx, key_idx);
  485. mt7601u_wr_copy(dev, MT_SKEY(vif_idx, key_idx),
  486. key_data, sizeof(key_data));
  487. val = mt76_rr(dev, MT_SKEY_MODE(vif_idx));
  488. val &= ~(MT_SKEY_MODE_MASK << MT_SKEY_MODE_SHIFT(vif_idx, key_idx));
  489. val |= cipher << MT_SKEY_MODE_SHIFT(vif_idx, key_idx);
  490. mt76_wr(dev, MT_SKEY_MODE(vif_idx), val);
  491. return 0;
  492. }