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