1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Queue between the tx operation and the bh workqueue.
4 *
5 * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6 * Copyright (c) 2010, ST-Ericsson
7 */
8 #include <linux/sched.h>
9 #include <net/mac80211.h>
10
11 #include "queue.h"
12 #include "wfx.h"
13 #include "sta.h"
14 #include "data_tx.h"
15 #include "traces.h"
16
wfx_tx_lock(struct wfx_dev * wdev)17 void wfx_tx_lock(struct wfx_dev *wdev)
18 {
19 atomic_inc(&wdev->tx_lock);
20 }
21
wfx_tx_unlock(struct wfx_dev * wdev)22 void wfx_tx_unlock(struct wfx_dev *wdev)
23 {
24 int tx_lock = atomic_dec_return(&wdev->tx_lock);
25
26 WARN(tx_lock < 0, "inconsistent tx_lock value");
27 if (!tx_lock)
28 wfx_bh_request_tx(wdev);
29 }
30
wfx_tx_flush(struct wfx_dev * wdev)31 void wfx_tx_flush(struct wfx_dev *wdev)
32 {
33 int ret;
34
35 /* Do not wait for any reply if chip is frozen */
36 if (wdev->chip_frozen)
37 return;
38
39 wfx_tx_lock(wdev);
40 mutex_lock(&wdev->hif_cmd.lock);
41 ret = wait_event_timeout(wdev->hif.tx_buffers_empty, !wdev->hif.tx_buffers_used,
42 msecs_to_jiffies(3000));
43 if (!ret) {
44 dev_warn(wdev->dev, "cannot flush tx buffers (%d still busy)\n",
45 wdev->hif.tx_buffers_used);
46 wfx_pending_dump_old_frames(wdev, 3000);
47 /* FIXME: drop pending frames here */
48 wdev->chip_frozen = true;
49 }
50 mutex_unlock(&wdev->hif_cmd.lock);
51 wfx_tx_unlock(wdev);
52 }
53
wfx_tx_lock_flush(struct wfx_dev * wdev)54 void wfx_tx_lock_flush(struct wfx_dev *wdev)
55 {
56 wfx_tx_lock(wdev);
57 wfx_tx_flush(wdev);
58 }
59
wfx_tx_queues_init(struct wfx_vif * wvif)60 void wfx_tx_queues_init(struct wfx_vif *wvif)
61 {
62 /* The device is in charge to respect the details of the QoS parameters. The driver just
63 * ensure that it roughtly respect the priorities to avoid any shortage.
64 */
65 const int priorities[IEEE80211_NUM_ACS] = { 1, 2, 64, 128 };
66 int i;
67
68 for (i = 0; i < IEEE80211_NUM_ACS; ++i) {
69 skb_queue_head_init(&wvif->tx_queue[i].normal);
70 skb_queue_head_init(&wvif->tx_queue[i].cab);
71 wvif->tx_queue[i].priority = priorities[i];
72 }
73 }
74
wfx_tx_queue_empty(struct wfx_vif * wvif,struct wfx_queue * queue)75 bool wfx_tx_queue_empty(struct wfx_vif *wvif, struct wfx_queue *queue)
76 {
77 return skb_queue_empty_lockless(&queue->normal) && skb_queue_empty_lockless(&queue->cab);
78 }
79
wfx_tx_queues_check_empty(struct wfx_vif * wvif)80 void wfx_tx_queues_check_empty(struct wfx_vif *wvif)
81 {
82 int i;
83
84 for (i = 0; i < IEEE80211_NUM_ACS; ++i) {
85 WARN_ON(atomic_read(&wvif->tx_queue[i].pending_frames));
86 WARN_ON(!wfx_tx_queue_empty(wvif, &wvif->tx_queue[i]));
87 }
88 }
89
__wfx_tx_queue_drop(struct wfx_vif * wvif,struct sk_buff_head * skb_queue,struct sk_buff_head * dropped)90 static void __wfx_tx_queue_drop(struct wfx_vif *wvif,
91 struct sk_buff_head *skb_queue, struct sk_buff_head *dropped)
92 {
93 struct sk_buff *skb, *tmp;
94
95 spin_lock_bh(&skb_queue->lock);
96 skb_queue_walk_safe(skb_queue, skb, tmp) {
97 __skb_unlink(skb, skb_queue);
98 skb_queue_head(dropped, skb);
99 }
100 spin_unlock_bh(&skb_queue->lock);
101 }
102
wfx_tx_queue_drop(struct wfx_vif * wvif,struct wfx_queue * queue,struct sk_buff_head * dropped)103 void wfx_tx_queue_drop(struct wfx_vif *wvif, struct wfx_queue *queue,
104 struct sk_buff_head *dropped)
105 {
106 __wfx_tx_queue_drop(wvif, &queue->cab, dropped);
107 __wfx_tx_queue_drop(wvif, &queue->normal, dropped);
108 wake_up(&wvif->wdev->tx_dequeue);
109 }
110
wfx_tx_queues_put(struct wfx_vif * wvif,struct sk_buff * skb)111 void wfx_tx_queues_put(struct wfx_vif *wvif, struct sk_buff *skb)
112 {
113 struct wfx_queue *queue = &wvif->tx_queue[skb_get_queue_mapping(skb)];
114 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
115
116 if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
117 skb_queue_tail(&queue->cab, skb);
118 else
119 skb_queue_tail(&queue->normal, skb);
120 }
121
wfx_pending_drop(struct wfx_dev * wdev,struct sk_buff_head * dropped)122 void wfx_pending_drop(struct wfx_dev *wdev, struct sk_buff_head *dropped)
123 {
124 struct wfx_queue *queue;
125 struct wfx_vif *wvif;
126 struct wfx_hif_msg *hif;
127 struct sk_buff *skb;
128
129 WARN(!wdev->chip_frozen, "%s should only be used to recover a frozen device", __func__);
130 while ((skb = skb_dequeue(&wdev->tx_pending)) != NULL) {
131 hif = (struct wfx_hif_msg *)skb->data;
132 wvif = wdev_to_wvif(wdev, hif->interface);
133 if (wvif) {
134 queue = &wvif->tx_queue[skb_get_queue_mapping(skb)];
135 WARN_ON(skb_get_queue_mapping(skb) > 3);
136 WARN_ON(!atomic_read(&queue->pending_frames));
137 atomic_dec(&queue->pending_frames);
138 }
139 skb_queue_head(dropped, skb);
140 }
141 }
142
wfx_pending_get(struct wfx_dev * wdev,u32 packet_id)143 struct sk_buff *wfx_pending_get(struct wfx_dev *wdev, u32 packet_id)
144 {
145 struct wfx_queue *queue;
146 struct wfx_hif_req_tx *req;
147 struct wfx_vif *wvif;
148 struct wfx_hif_msg *hif;
149 struct sk_buff *skb;
150
151 spin_lock_bh(&wdev->tx_pending.lock);
152 skb_queue_walk(&wdev->tx_pending, skb) {
153 hif = (struct wfx_hif_msg *)skb->data;
154 req = (struct wfx_hif_req_tx *)hif->body;
155 if (req->packet_id != packet_id)
156 continue;
157 spin_unlock_bh(&wdev->tx_pending.lock);
158 wvif = wdev_to_wvif(wdev, hif->interface);
159 if (wvif) {
160 queue = &wvif->tx_queue[skb_get_queue_mapping(skb)];
161 WARN_ON(skb_get_queue_mapping(skb) > 3);
162 WARN_ON(!atomic_read(&queue->pending_frames));
163 atomic_dec(&queue->pending_frames);
164 }
165 skb_unlink(skb, &wdev->tx_pending);
166 return skb;
167 }
168 spin_unlock_bh(&wdev->tx_pending.lock);
169 WARN(1, "cannot find packet in pending queue");
170 return NULL;
171 }
172
wfx_pending_dump_old_frames(struct wfx_dev * wdev,unsigned int limit_ms)173 void wfx_pending_dump_old_frames(struct wfx_dev *wdev, unsigned int limit_ms)
174 {
175 ktime_t now = ktime_get();
176 struct wfx_tx_priv *tx_priv;
177 struct wfx_hif_req_tx *req;
178 struct sk_buff *skb;
179 bool first = true;
180
181 spin_lock_bh(&wdev->tx_pending.lock);
182 skb_queue_walk(&wdev->tx_pending, skb) {
183 tx_priv = wfx_skb_tx_priv(skb);
184 req = wfx_skb_txreq(skb);
185 if (ktime_after(now, ktime_add_ms(tx_priv->xmit_timestamp, limit_ms))) {
186 if (first) {
187 dev_info(wdev->dev, "frames stuck in firmware since %dms or more:\n",
188 limit_ms);
189 first = false;
190 }
191 dev_info(wdev->dev, " id %08x sent %lldms ago\n",
192 req->packet_id, ktime_ms_delta(now, tx_priv->xmit_timestamp));
193 }
194 }
195 spin_unlock_bh(&wdev->tx_pending.lock);
196 }
197
wfx_pending_get_pkt_us_delay(struct wfx_dev * wdev,struct sk_buff * skb)198 unsigned int wfx_pending_get_pkt_us_delay(struct wfx_dev *wdev, struct sk_buff *skb)
199 {
200 ktime_t now = ktime_get();
201 struct wfx_tx_priv *tx_priv = wfx_skb_tx_priv(skb);
202
203 return ktime_us_delta(now, tx_priv->xmit_timestamp);
204 }
205
wfx_tx_queues_has_cab(struct wfx_vif * wvif)206 bool wfx_tx_queues_has_cab(struct wfx_vif *wvif)
207 {
208 struct ieee80211_vif *vif = wvif_to_vif(wvif);
209 int i;
210
211 if (vif->type != NL80211_IFTYPE_AP)
212 return false;
213 for (i = 0; i < IEEE80211_NUM_ACS; ++i)
214 /* Note: since only AP can have mcast frames in queue and only one vif can be AP,
215 * all queued frames has same interface id
216 */
217 if (!skb_queue_empty_lockless(&wvif->tx_queue[i].cab))
218 return true;
219 return false;
220 }
221
wfx_tx_queue_get_weight(struct wfx_queue * queue)222 static int wfx_tx_queue_get_weight(struct wfx_queue *queue)
223 {
224 return atomic_read(&queue->pending_frames) * queue->priority;
225 }
226
wfx_tx_queues_get_skb(struct wfx_dev * wdev)227 static struct sk_buff *wfx_tx_queues_get_skb(struct wfx_dev *wdev)
228 {
229 struct wfx_queue *queues[IEEE80211_NUM_ACS * ARRAY_SIZE(wdev->vif)];
230 int i, j, num_queues = 0;
231 struct wfx_vif *wvif;
232 struct wfx_hif_msg *hif;
233 struct sk_buff *skb;
234
235 /* sort the queues */
236 wvif = NULL;
237 while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
238 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
239 WARN_ON(num_queues >= ARRAY_SIZE(queues));
240 queues[num_queues] = &wvif->tx_queue[i];
241 for (j = num_queues; j > 0; j--)
242 if (wfx_tx_queue_get_weight(queues[j]) <
243 wfx_tx_queue_get_weight(queues[j - 1]))
244 swap(queues[j - 1], queues[j]);
245 num_queues++;
246 }
247 }
248
249 wvif = NULL;
250 while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
251 if (!wvif->after_dtim_tx_allowed)
252 continue;
253 for (i = 0; i < num_queues; i++) {
254 skb = skb_dequeue(&queues[i]->cab);
255 if (!skb)
256 continue;
257 /* Note: since only AP can have mcast frames in queue and only one vif can
258 * be AP, all queued frames has same interface id
259 */
260 hif = (struct wfx_hif_msg *)skb->data;
261 WARN_ON(hif->interface != wvif->id);
262 WARN_ON(queues[i] != &wvif->tx_queue[skb_get_queue_mapping(skb)]);
263 atomic_inc(&queues[i]->pending_frames);
264 trace_queues_stats(wdev, queues[i]);
265 return skb;
266 }
267 /* No more multicast to sent */
268 wvif->after_dtim_tx_allowed = false;
269 schedule_work(&wvif->update_tim_work);
270 }
271
272 for (i = 0; i < num_queues; i++) {
273 skb = skb_dequeue(&queues[i]->normal);
274 if (skb) {
275 atomic_inc(&queues[i]->pending_frames);
276 trace_queues_stats(wdev, queues[i]);
277 return skb;
278 }
279 }
280 return NULL;
281 }
282
wfx_tx_queues_get(struct wfx_dev * wdev)283 struct wfx_hif_msg *wfx_tx_queues_get(struct wfx_dev *wdev)
284 {
285 struct wfx_tx_priv *tx_priv;
286 struct sk_buff *skb;
287
288 if (atomic_read(&wdev->tx_lock))
289 return NULL;
290 skb = wfx_tx_queues_get_skb(wdev);
291 if (!skb)
292 return NULL;
293 skb_queue_tail(&wdev->tx_pending, skb);
294 wake_up(&wdev->tx_dequeue);
295 tx_priv = wfx_skb_tx_priv(skb);
296 tx_priv->xmit_timestamp = ktime_get();
297 return (struct wfx_hif_msg *)skb->data;
298 }
299