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