mt76x2_tx.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. int mt76x2_tx_prepare_skb(struct mt76_dev *mdev, void *txwi,
  24. struct sk_buff *skb, struct mt76_queue *q,
  25. struct mt76_wcid *wcid, struct ieee80211_sta *sta,
  26. u32 *tx_info)
  27. {
  28. struct mt76x2_dev *dev = container_of(mdev, struct mt76x2_dev, mt76);
  29. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  30. int qsel = MT_QSEL_EDCA;
  31. int ret;
  32. if (q == &dev->mt76.q_tx[MT_TXQ_PSD] && wcid && wcid->idx < 128)
  33. mt76x2_mac_wcid_set_drop(dev, wcid->idx, false);
  34. mt76x2_mac_write_txwi(dev, txwi, skb, wcid, sta, skb->len);
  35. ret = mt76x2_insert_hdr_pad(skb);
  36. if (ret < 0)
  37. return ret;
  38. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  39. qsel = MT_QSEL_MGMT;
  40. *tx_info = FIELD_PREP(MT_TXD_INFO_QSEL, qsel) |
  41. MT_TXD_INFO_80211;
  42. if (!wcid || wcid->hw_key_idx == 0xff || wcid->sw_iv)
  43. *tx_info |= MT_TXD_INFO_WIV;
  44. return 0;
  45. }
  46. static void
  47. mt76x2_update_beacon_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
  48. {
  49. struct mt76x2_dev *dev = (struct mt76x2_dev *) priv;
  50. struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
  51. struct sk_buff *skb = NULL;
  52. if (!(dev->beacon_mask & BIT(mvif->idx)))
  53. return;
  54. skb = ieee80211_beacon_get(mt76_hw(dev), vif);
  55. if (!skb)
  56. return;
  57. mt76x2_mac_set_beacon(dev, mvif->idx, skb);
  58. }
  59. static void
  60. mt76x2_add_buffered_bc(void *priv, u8 *mac, struct ieee80211_vif *vif)
  61. {
  62. struct beacon_bc_data *data = priv;
  63. struct mt76x2_dev *dev = data->dev;
  64. struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
  65. struct ieee80211_tx_info *info;
  66. struct sk_buff *skb;
  67. if (!(dev->beacon_mask & BIT(mvif->idx)))
  68. return;
  69. skb = ieee80211_get_buffered_bc(mt76_hw(dev), vif);
  70. if (!skb)
  71. return;
  72. info = IEEE80211_SKB_CB(skb);
  73. info->control.vif = vif;
  74. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  75. mt76_skb_set_moredata(skb, true);
  76. __skb_queue_tail(&data->q, skb);
  77. data->tail[mvif->idx] = skb;
  78. }
  79. static void
  80. mt76x2_resync_beacon_timer(struct mt76x2_dev *dev)
  81. {
  82. u32 timer_val = dev->beacon_int << 4;
  83. dev->tbtt_count++;
  84. /*
  85. * Beacon timer drifts by 1us every tick, the timer is configured
  86. * in 1/16 TU (64us) units.
  87. */
  88. if (dev->tbtt_count < 62)
  89. return;
  90. if (dev->tbtt_count >= 64) {
  91. dev->tbtt_count = 0;
  92. return;
  93. }
  94. /*
  95. * The updated beacon interval takes effect after two TBTT, because
  96. * at this point the original interval has already been loaded into
  97. * the next TBTT_TIMER value
  98. */
  99. if (dev->tbtt_count == 62)
  100. timer_val -= 1;
  101. mt76_rmw_field(dev, MT_BEACON_TIME_CFG,
  102. MT_BEACON_TIME_CFG_INTVAL, timer_val);
  103. }
  104. void mt76x2_pre_tbtt_tasklet(unsigned long arg)
  105. {
  106. struct mt76x2_dev *dev = (struct mt76x2_dev *) arg;
  107. struct mt76_queue *q = &dev->mt76.q_tx[MT_TXQ_PSD];
  108. struct beacon_bc_data data = {};
  109. struct sk_buff *skb;
  110. int i, nframes;
  111. mt76x2_resync_beacon_timer(dev);
  112. data.dev = dev;
  113. __skb_queue_head_init(&data.q);
  114. ieee80211_iterate_active_interfaces_atomic(mt76_hw(dev),
  115. IEEE80211_IFACE_ITER_RESUME_ALL,
  116. mt76x2_update_beacon_iter, dev);
  117. do {
  118. nframes = skb_queue_len(&data.q);
  119. ieee80211_iterate_active_interfaces_atomic(mt76_hw(dev),
  120. IEEE80211_IFACE_ITER_RESUME_ALL,
  121. mt76x2_add_buffered_bc, &data);
  122. } while (nframes != skb_queue_len(&data.q));
  123. if (!nframes)
  124. return;
  125. for (i = 0; i < ARRAY_SIZE(data.tail); i++) {
  126. if (!data.tail[i])
  127. continue;
  128. mt76_skb_set_moredata(data.tail[i], false);
  129. }
  130. spin_lock_bh(&q->lock);
  131. while ((skb = __skb_dequeue(&data.q)) != NULL) {
  132. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  133. struct ieee80211_vif *vif = info->control.vif;
  134. struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
  135. mt76_dma_tx_queue_skb(&dev->mt76, q, skb, &mvif->group_wcid,
  136. NULL);
  137. }
  138. spin_unlock_bh(&q->lock);
  139. }