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