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