mt76x2_tx.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "mt76x2.h"
  17. #include "mt76x2_dma.h"
  18. struct beacon_bc_data {
  19. struct mt76x2_dev *dev;
  20. struct sk_buff_head q;
  21. struct sk_buff *tail[8];
  22. };
  23. void mt76x2_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
  24. struct sk_buff *skb)
  25. {
  26. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  27. struct mt76x2_dev *dev = hw->priv;
  28. struct ieee80211_vif *vif = info->control.vif;
  29. struct mt76_wcid *wcid = &dev->global_wcid;
  30. if (control->sta) {
  31. struct mt76x2_sta *msta;
  32. msta = (struct mt76x2_sta *) control->sta->drv_priv;
  33. wcid = &msta->wcid;
  34. /* sw encrypted frames */
  35. if (!info->control.hw_key && wcid->hw_key_idx != -1)
  36. control->sta = NULL;
  37. }
  38. if (vif && !control->sta) {
  39. struct mt76x2_vif *mvif;
  40. mvif = (struct mt76x2_vif *) vif->drv_priv;
  41. wcid = &mvif->group_wcid;
  42. }
  43. mt76_tx(&dev->mt76, control->sta, wcid, skb);
  44. }
  45. void mt76x2_tx_complete(struct mt76x2_dev *dev, struct sk_buff *skb)
  46. {
  47. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  48. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  49. ieee80211_free_txskb(mt76_hw(dev), skb);
  50. } else {
  51. ieee80211_tx_info_clear_status(info);
  52. info->status.rates[0].idx = -1;
  53. info->flags |= IEEE80211_TX_STAT_ACK;
  54. ieee80211_tx_status(mt76_hw(dev), skb);
  55. }
  56. }
  57. s8 mt76x2_tx_get_max_txpwr_adj(struct mt76x2_dev *dev,
  58. const struct ieee80211_tx_rate *rate)
  59. {
  60. s8 max_txpwr;
  61. if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
  62. u8 mcs = ieee80211_rate_get_vht_mcs(rate);
  63. if (mcs == 8 || mcs == 9) {
  64. max_txpwr = dev->rate_power.vht[8];
  65. } else {
  66. u8 nss, idx;
  67. nss = ieee80211_rate_get_vht_nss(rate);
  68. idx = ((nss - 1) << 3) + mcs;
  69. max_txpwr = dev->rate_power.ht[idx & 0xf];
  70. }
  71. } else if (rate->flags & IEEE80211_TX_RC_MCS) {
  72. max_txpwr = dev->rate_power.ht[rate->idx & 0xf];
  73. } else {
  74. enum nl80211_band band = dev->mt76.chandef.chan->band;
  75. if (band == NL80211_BAND_2GHZ) {
  76. const struct ieee80211_rate *r;
  77. struct wiphy *wiphy = mt76_hw(dev)->wiphy;
  78. struct mt76_rate_power *rp = &dev->rate_power;
  79. r = &wiphy->bands[band]->bitrates[rate->idx];
  80. if (r->flags & IEEE80211_RATE_SHORT_PREAMBLE)
  81. max_txpwr = rp->cck[r->hw_value & 0x3];
  82. else
  83. max_txpwr = rp->ofdm[r->hw_value & 0x7];
  84. } else {
  85. max_txpwr = dev->rate_power.ofdm[rate->idx & 0x7];
  86. }
  87. }
  88. return max_txpwr;
  89. }
  90. s8 mt76x2_tx_get_txpwr_adj(struct mt76x2_dev *dev, s8 txpwr, s8 max_txpwr_adj)
  91. {
  92. txpwr = min_t(s8, txpwr, dev->txpower_conf);
  93. txpwr -= (dev->target_power + dev->target_power_delta[0]);
  94. txpwr = min_t(s8, txpwr, max_txpwr_adj);
  95. if (!dev->enable_tpc)
  96. return 0;
  97. else if (txpwr >= 0)
  98. return min_t(s8, txpwr, 7);
  99. else
  100. return (txpwr < -16) ? 8 : (txpwr + 32) / 2;
  101. }
  102. void mt76x2_tx_set_txpwr_auto(struct mt76x2_dev *dev, s8 txpwr)
  103. {
  104. s8 txpwr_adj;
  105. txpwr_adj = mt76x2_tx_get_txpwr_adj(dev, txpwr,
  106. dev->rate_power.ofdm[4]);
  107. mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
  108. MT_PROT_AUTO_TX_CFG_PROT_PADJ, txpwr_adj);
  109. mt76_rmw_field(dev, MT_PROT_AUTO_TX_CFG,
  110. MT_PROT_AUTO_TX_CFG_AUTO_PADJ, txpwr_adj);
  111. }
  112. static int mt76x2_insert_hdr_pad(struct sk_buff *skb)
  113. {
  114. int len = ieee80211_get_hdrlen_from_skb(skb);
  115. if (len % 4 == 0)
  116. return 0;
  117. skb_push(skb, 2);
  118. memmove(skb->data, skb->data + 2, len);
  119. skb->data[len] = 0;
  120. skb->data[len + 1] = 0;
  121. return 2;
  122. }
  123. int mt76x2_tx_prepare_skb(struct mt76_dev *mdev, void *txwi,
  124. struct sk_buff *skb, struct mt76_queue *q,
  125. struct mt76_wcid *wcid, struct ieee80211_sta *sta,
  126. u32 *tx_info)
  127. {
  128. struct mt76x2_dev *dev = container_of(mdev, struct mt76x2_dev, mt76);
  129. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  130. int qsel = MT_QSEL_EDCA;
  131. int ret;
  132. if (q == &dev->mt76.q_tx[MT_TXQ_PSD] && wcid && wcid->idx < 128)
  133. mt76x2_mac_wcid_set_drop(dev, wcid->idx, false);
  134. mt76x2_mac_write_txwi(dev, txwi, skb, wcid, sta);
  135. ret = mt76x2_insert_hdr_pad(skb);
  136. if (ret < 0)
  137. return ret;
  138. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  139. qsel = MT_QSEL_MGMT;
  140. *tx_info = FIELD_PREP(MT_TXD_INFO_QSEL, qsel) |
  141. MT_TXD_INFO_80211;
  142. if (!wcid || wcid->hw_key_idx == 0xff || wcid->sw_iv)
  143. *tx_info |= MT_TXD_INFO_WIV;
  144. return 0;
  145. }
  146. static void
  147. mt76x2_update_beacon_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
  148. {
  149. struct mt76x2_dev *dev = (struct mt76x2_dev *) priv;
  150. struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
  151. struct sk_buff *skb = NULL;
  152. if (!(dev->beacon_mask & BIT(mvif->idx)))
  153. return;
  154. skb = ieee80211_beacon_get(mt76_hw(dev), vif);
  155. if (!skb)
  156. return;
  157. mt76x2_mac_set_beacon(dev, mvif->idx, skb);
  158. }
  159. static void
  160. mt76x2_add_buffered_bc(void *priv, u8 *mac, struct ieee80211_vif *vif)
  161. {
  162. struct beacon_bc_data *data = priv;
  163. struct mt76x2_dev *dev = data->dev;
  164. struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
  165. struct ieee80211_tx_info *info;
  166. struct sk_buff *skb;
  167. if (!(dev->beacon_mask & BIT(mvif->idx)))
  168. return;
  169. skb = ieee80211_get_buffered_bc(mt76_hw(dev), vif);
  170. if (!skb)
  171. return;
  172. info = IEEE80211_SKB_CB(skb);
  173. info->control.vif = vif;
  174. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  175. mt76_skb_set_moredata(skb, true);
  176. __skb_queue_tail(&data->q, skb);
  177. data->tail[mvif->idx] = skb;
  178. }
  179. void mt76x2_pre_tbtt_tasklet(unsigned long arg)
  180. {
  181. struct mt76x2_dev *dev = (struct mt76x2_dev *) arg;
  182. struct mt76_queue *q = &dev->mt76.q_tx[MT_TXQ_PSD];
  183. struct beacon_bc_data data = {};
  184. struct sk_buff *skb;
  185. int i, nframes;
  186. data.dev = dev;
  187. __skb_queue_head_init(&data.q);
  188. ieee80211_iterate_active_interfaces_atomic(mt76_hw(dev),
  189. IEEE80211_IFACE_ITER_RESUME_ALL,
  190. mt76x2_update_beacon_iter, dev);
  191. do {
  192. nframes = skb_queue_len(&data.q);
  193. ieee80211_iterate_active_interfaces_atomic(mt76_hw(dev),
  194. IEEE80211_IFACE_ITER_RESUME_ALL,
  195. mt76x2_add_buffered_bc, &data);
  196. } while (nframes != skb_queue_len(&data.q));
  197. if (!nframes)
  198. return;
  199. for (i = 0; i < ARRAY_SIZE(data.tail); i++) {
  200. if (!data.tail[i])
  201. continue;
  202. mt76_skb_set_moredata(data.tail[i], false);
  203. }
  204. spin_lock_bh(&q->lock);
  205. while ((skb = __skb_dequeue(&data.q)) != NULL) {
  206. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  207. struct ieee80211_vif *vif = info->control.vif;
  208. struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
  209. mt76_tx_queue_skb(&dev->mt76, q, skb, &mvif->group_wcid, NULL);
  210. }
  211. spin_unlock_bh(&q->lock);
  212. }