root/drivers/net/wireless/mediatek/mt76/mt76x02_usb_core.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mt76x02u_remove_dma_hdr
  2. mt76x02u_tx_complete_skb
  3. mt76x02u_skb_dma_info
  4. mt76x02u_tx_prepare_skb
  5. mt76x02u_start_pre_tbtt_timer
  6. mt76x02u_restart_pre_tbtt_timer
  7. mt76x02u_stop_pre_tbtt_timer
  8. mt76x02u_pre_tbtt_work
  9. mt76x02u_pre_tbtt_interrupt
  10. mt76x02u_pre_tbtt_enable
  11. mt76x02u_beacon_enable
  12. mt76x02u_init_beacon_config
  13. mt76x02u_exit_beacon_config

   1 // SPDX-License-Identifier: ISC
   2 /*
   3  * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
   4  */
   5 
   6 #include "mt76x02_usb.h"
   7 
   8 static void mt76x02u_remove_dma_hdr(struct sk_buff *skb)
   9 {
  10         int hdr_len;
  11 
  12         skb_pull(skb, sizeof(struct mt76x02_txwi) + MT_DMA_HDR_LEN);
  13         hdr_len = ieee80211_get_hdrlen_from_skb(skb);
  14         if (hdr_len % 4)
  15                 mt76x02_remove_hdr_pad(skb, 2);
  16 }
  17 
  18 void mt76x02u_tx_complete_skb(struct mt76_dev *mdev, enum mt76_txq_id qid,
  19                               struct mt76_queue_entry *e)
  20 {
  21         mt76x02u_remove_dma_hdr(e->skb);
  22         mt76_tx_complete_skb(mdev, e->skb);
  23 }
  24 EXPORT_SYMBOL_GPL(mt76x02u_tx_complete_skb);
  25 
  26 int mt76x02u_skb_dma_info(struct sk_buff *skb, int port, u32 flags)
  27 {
  28         struct sk_buff *iter, *last = skb;
  29         u32 info, pad;
  30 
  31         /* Buffer layout:
  32          *      |   4B   | xfer len |      pad       |  4B  |
  33          *      | TXINFO | pkt/cmd  | zero pad to 4B | zero |
  34          *
  35          * length field of TXINFO should be set to 'xfer len'.
  36          */
  37         info = FIELD_PREP(MT_TXD_INFO_LEN, round_up(skb->len, 4)) |
  38                FIELD_PREP(MT_TXD_INFO_DPORT, port) | flags;
  39         put_unaligned_le32(info, skb_push(skb, sizeof(info)));
  40 
  41         /* Add zero pad of 4 - 7 bytes */
  42         pad = round_up(skb->len, 4) + 4 - skb->len;
  43 
  44         /* First packet of a A-MSDU burst keeps track of the whole burst
  45          * length, need to update length of it and the last packet.
  46          */
  47         skb_walk_frags(skb, iter) {
  48                 last = iter;
  49                 if (!iter->next) {
  50                         skb->data_len += pad;
  51                         skb->len += pad;
  52                         break;
  53                 }
  54         }
  55 
  56         if (skb_pad(last, pad))
  57                 return -ENOMEM;
  58         __skb_put(last, pad);
  59 
  60         return 0;
  61 }
  62 
  63 int mt76x02u_tx_prepare_skb(struct mt76_dev *mdev, void *data,
  64                             enum mt76_txq_id qid, struct mt76_wcid *wcid,
  65                             struct ieee80211_sta *sta,
  66                             struct mt76_tx_info *tx_info)
  67 {
  68         struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
  69         int pid, len = tx_info->skb->len, ep = q2ep(mdev->q_tx[qid].q->hw_idx);
  70         struct mt76x02_txwi *txwi;
  71         bool ampdu = IEEE80211_SKB_CB(tx_info->skb)->flags & IEEE80211_TX_CTL_AMPDU;
  72         enum mt76_qsel qsel;
  73         u32 flags;
  74 
  75         mt76_insert_hdr_pad(tx_info->skb);
  76 
  77         txwi = (struct mt76x02_txwi *)(tx_info->skb->data - sizeof(*txwi));
  78         mt76x02_mac_write_txwi(dev, txwi, tx_info->skb, wcid, sta, len);
  79         skb_push(tx_info->skb, sizeof(*txwi));
  80 
  81         pid = mt76_tx_status_skb_add(mdev, wcid, tx_info->skb);
  82 
  83         /* encode packet rate for no-skb packet id to fix up status reporting */
  84         if (pid == MT_PACKET_ID_NO_SKB)
  85                 pid = MT_PACKET_ID_HAS_RATE |
  86                       (le16_to_cpu(txwi->rate) & MT_RXWI_RATE_INDEX);
  87 
  88         txwi->pktid = pid;
  89 
  90         if ((mt76_is_skb_pktid(pid) && ampdu) || ep == MT_EP_OUT_HCCA)
  91                 qsel = MT_QSEL_MGMT;
  92         else
  93                 qsel = MT_QSEL_EDCA;
  94 
  95         flags = FIELD_PREP(MT_TXD_INFO_QSEL, qsel) |
  96                 MT_TXD_INFO_80211;
  97         if (!wcid || wcid->hw_key_idx == 0xff || wcid->sw_iv)
  98                 flags |= MT_TXD_INFO_WIV;
  99 
 100         return mt76x02u_skb_dma_info(tx_info->skb, WLAN_PORT, flags);
 101 }
 102 EXPORT_SYMBOL_GPL(mt76x02u_tx_prepare_skb);
 103 
 104 /* Trigger pre-TBTT event 8 ms before TBTT */
 105 #define PRE_TBTT_USEC 8000
 106 
 107 /* Beacon SRAM memory is limited to 8kB. We need to send PS buffered frames
 108  * (which can be 1500 bytes big) via beacon memory. That make limit of number
 109  * of slots to 5. TODO: dynamically calculate offsets in beacon SRAM.
 110  */
 111 #define N_BCN_SLOTS 5
 112 
 113 static void mt76x02u_start_pre_tbtt_timer(struct mt76x02_dev *dev)
 114 {
 115         u64 time;
 116         u32 tbtt;
 117 
 118         /* Get remaining TBTT in usec */
 119         tbtt = mt76_get_field(dev, MT_TBTT_TIMER, MT_TBTT_TIMER_VAL);
 120         tbtt *= 32;
 121 
 122         if (tbtt <= PRE_TBTT_USEC) {
 123                 queue_work(system_highpri_wq, &dev->pre_tbtt_work);
 124                 return;
 125         }
 126 
 127         time = (tbtt - PRE_TBTT_USEC) * 1000ull;
 128         hrtimer_start(&dev->pre_tbtt_timer, time, HRTIMER_MODE_REL);
 129 }
 130 
 131 static void mt76x02u_restart_pre_tbtt_timer(struct mt76x02_dev *dev)
 132 {
 133         u32 tbtt, dw0, dw1;
 134         u64 tsf, time;
 135 
 136         /* Get remaining TBTT in usec */
 137         tbtt = mt76_get_field(dev, MT_TBTT_TIMER, MT_TBTT_TIMER_VAL);
 138         tbtt *= 32;
 139 
 140         dw0 = mt76_rr(dev, MT_TSF_TIMER_DW0);
 141         dw1 = mt76_rr(dev, MT_TSF_TIMER_DW1);
 142         tsf = (u64)dw0 << 32 | dw1;
 143         dev_dbg(dev->mt76.dev, "TSF: %llu us TBTT %u us\n", tsf, tbtt);
 144 
 145         /* Convert beacon interval in TU (1024 usec) to nsec */
 146         time = ((1000000000ull * dev->mt76.beacon_int) >> 10);
 147 
 148         /* Adjust time to trigger hrtimer 8ms before TBTT */
 149         if (tbtt < PRE_TBTT_USEC)
 150                 time -= (PRE_TBTT_USEC - tbtt) * 1000ull;
 151         else
 152                 time += (tbtt - PRE_TBTT_USEC) * 1000ull;
 153 
 154         hrtimer_start(&dev->pre_tbtt_timer, time, HRTIMER_MODE_REL);
 155 }
 156 
 157 static void mt76x02u_stop_pre_tbtt_timer(struct mt76x02_dev *dev)
 158 {
 159         do {
 160                 hrtimer_cancel(&dev->pre_tbtt_timer);
 161                 cancel_work_sync(&dev->pre_tbtt_work);
 162                 /* Timer can be rearmed by work. */
 163         } while (hrtimer_active(&dev->pre_tbtt_timer));
 164 }
 165 
 166 static void mt76x02u_pre_tbtt_work(struct work_struct *work)
 167 {
 168         struct mt76x02_dev *dev =
 169                 container_of(work, struct mt76x02_dev, pre_tbtt_work);
 170         struct beacon_bc_data data = {};
 171         struct sk_buff *skb;
 172         int i, nbeacons;
 173 
 174         if (!dev->mt76.beacon_mask)
 175                 return;
 176 
 177         if (mt76_hw(dev)->conf.flags & IEEE80211_CONF_OFFCHANNEL)
 178                 return;
 179 
 180         mt76x02_resync_beacon_timer(dev);
 181 
 182         ieee80211_iterate_active_interfaces(mt76_hw(dev),
 183                 IEEE80211_IFACE_ITER_RESUME_ALL,
 184                 mt76x02_update_beacon_iter, dev);
 185 
 186         nbeacons = hweight8(dev->mt76.beacon_mask);
 187         mt76x02_enqueue_buffered_bc(dev, &data, N_BCN_SLOTS - nbeacons);
 188 
 189         for (i = nbeacons; i < N_BCN_SLOTS; i++) {
 190                 skb = __skb_dequeue(&data.q);
 191                 mt76x02_mac_set_beacon(dev, i, skb);
 192         }
 193 
 194         mt76x02u_restart_pre_tbtt_timer(dev);
 195 }
 196 
 197 static enum hrtimer_restart mt76x02u_pre_tbtt_interrupt(struct hrtimer *timer)
 198 {
 199         struct mt76x02_dev *dev =
 200             container_of(timer, struct mt76x02_dev, pre_tbtt_timer);
 201 
 202         queue_work(system_highpri_wq, &dev->pre_tbtt_work);
 203 
 204         return HRTIMER_NORESTART;
 205 }
 206 
 207 static void mt76x02u_pre_tbtt_enable(struct mt76x02_dev *dev, bool en)
 208 {
 209         if (en && dev->mt76.beacon_mask &&
 210             !hrtimer_active(&dev->pre_tbtt_timer))
 211                 mt76x02u_start_pre_tbtt_timer(dev);
 212         if (!en)
 213                 mt76x02u_stop_pre_tbtt_timer(dev);
 214 }
 215 
 216 static void mt76x02u_beacon_enable(struct mt76x02_dev *dev, bool en)
 217 {
 218         int i;
 219 
 220         if (WARN_ON_ONCE(!dev->mt76.beacon_int))
 221                 return;
 222 
 223         if (en) {
 224                 mt76x02u_start_pre_tbtt_timer(dev);
 225         } else {
 226                 /* Timer is already stopped, only clean up
 227                  * PS buffered frames if any.
 228                  */
 229                 for (i = 0; i < N_BCN_SLOTS; i++)
 230                         mt76x02_mac_set_beacon(dev, i, NULL);
 231         }
 232 }
 233 
 234 void mt76x02u_init_beacon_config(struct mt76x02_dev *dev)
 235 {
 236         static const struct mt76x02_beacon_ops beacon_ops = {
 237                 .nslots = N_BCN_SLOTS,
 238                 .slot_size = (8192 / N_BCN_SLOTS) & ~63,
 239                 .pre_tbtt_enable = mt76x02u_pre_tbtt_enable,
 240                 .beacon_enable = mt76x02u_beacon_enable,
 241         };
 242         dev->beacon_ops = &beacon_ops;
 243 
 244         hrtimer_init(&dev->pre_tbtt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 245         dev->pre_tbtt_timer.function = mt76x02u_pre_tbtt_interrupt;
 246         INIT_WORK(&dev->pre_tbtt_work, mt76x02u_pre_tbtt_work);
 247 
 248         mt76x02_init_beacon_config(dev);
 249 }
 250 EXPORT_SYMBOL_GPL(mt76x02u_init_beacon_config);
 251 
 252 void mt76x02u_exit_beacon_config(struct mt76x02_dev *dev)
 253 {
 254         if (!test_bit(MT76_REMOVED, &dev->mt76.state))
 255                 mt76_clear(dev, MT_BEACON_TIME_CFG,
 256                            MT_BEACON_TIME_CFG_TIMER_EN |
 257                            MT_BEACON_TIME_CFG_SYNC_MODE |
 258                            MT_BEACON_TIME_CFG_TBTT_EN |
 259                            MT_BEACON_TIME_CFG_BEACON_TX);
 260 
 261         mt76x02u_stop_pre_tbtt_timer(dev);
 262 }
 263 EXPORT_SYMBOL_GPL(mt76x02u_exit_beacon_config);

/* [<][>][^][v][top][bottom][index][help] */