summaryrefslogtreecommitdiffstats
path: root/drivers/staging/wfx/queue.c
blob: a1a2f7756a273080897cd7be1e6ddaeeb064d25a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// SPDX-License-Identifier: GPL-2.0-only
/*
 * O(1) TX queue with built-in allocator.
 *
 * Copyright (c) 2017-2019, Silicon Laboratories, Inc.
 * Copyright (c) 2010, ST-Ericsson
 */
#include <linux/sched.h>
#include <net/mac80211.h>

#include "queue.h"
#include "wfx.h"
#include "sta.h"
#include "data_tx.h"

void wfx_tx_lock(struct wfx_dev *wdev)
{
	atomic_inc(&wdev->tx_lock);
}

void wfx_tx_unlock(struct wfx_dev *wdev)
{
	int tx_lock = atomic_dec_return(&wdev->tx_lock);

	WARN(tx_lock < 0, "inconsistent tx_lock value");
	if (!tx_lock)
		wfx_bh_request_tx(wdev);
}

void wfx_tx_flush(struct wfx_dev *wdev)
{
	int ret;
	int i;

	// Do not wait for any reply if chip is frozen
	if (wdev->chip_frozen)
		return;

	mutex_lock(&wdev->hif_cmd.lock);
	ret = wait_event_timeout(wdev->hif.tx_buffers_empty,
				 !wdev->hif.tx_buffers_used,
				 msecs_to_jiffies(3000));
	if (ret) {
		for (i = 0; i < IEEE80211_NUM_ACS; i++)
			WARN(atomic_read(&wdev->tx_queue[i].pending_frames),
			     "there are still %d pending frames on queue %d",
			     atomic_read(&wdev->tx_queue[i].pending_frames), i);
	}
	if (!ret) {
		dev_warn(wdev->dev, "cannot flush tx buffers (%d still busy)\n",
			 wdev->hif.tx_buffers_used);
		wfx_pending_dump_old_frames(wdev, 3000);
		// FIXME: drop pending frames here
		wdev->chip_frozen = 1;
	}
	mutex_unlock(&wdev->hif_cmd.lock);
}

void wfx_tx_lock_flush(struct wfx_dev *wdev)
{
	wfx_tx_lock(wdev);
	wfx_tx_flush(wdev);
}

/* If successful, LOCKS the TX queue! */
void wfx_tx_queues_wait_empty_vif(struct wfx_vif *wvif)
{
	int i;
	bool done;
	struct wfx_queue *queue;
	struct sk_buff *item;
	struct wfx_dev *wdev = wvif->wdev;
	struct hif_msg *hif;

	if (wvif->wdev->chip_frozen) {
		wfx_tx_lock_flush(wdev);
		wfx_tx_queues_clear(wdev);
		return;
	}

	do {
		done = true;
		wfx_tx_lock_flush(wdev);
		for (i = 0; i < IEEE80211_NUM_ACS && done; ++i) {
			queue = &wdev->tx_queue[i];
			spin_lock_bh(&queue->normal.lock);
			skb_queue_walk(&queue->normal, item) {
				hif = (struct hif_msg *)item->data;
				if (hif->interface == wvif->id)
					done = false;
			}
			spin_unlock_bh(&queue->normal.lock);
			spin_lock_bh(&queue->cab.lock);
			skb_queue_walk(&queue->cab, item) {
				hif = (struct hif_msg *)item->data;
				if (hif->interface == wvif->id)
					done = false;
			}
			spin_unlock_bh(&queue->cab.lock);
		}
		if (!done) {
			wfx_tx_unlock(wdev);
			msleep(20);
		}
	} while (!done);
}

static void wfx_tx_queue_clear(struct wfx_dev *wdev, struct wfx_queue *queue,
			       struct sk_buff_head *gc_list)
{
	struct sk_buff *item;

	while ((item = skb_dequeue(&queue->normal)) != NULL)
		skb_queue_head(gc_list, item);
	while ((item = skb_dequeue(&queue->cab)) != NULL)
		skb_queue_head(gc_list, item);
}

void wfx_tx_queues_clear(struct wfx_dev *wdev)
{
	int i;
	struct sk_buff *item;
	struct sk_buff_head gc_list;

	skb_queue_head_init(&gc_list);
	for (i = 0; i < IEEE80211_NUM_ACS; ++i)
		wfx_tx_queue_clear(wdev, &wdev->tx_queue[i], &gc_list);
	wake_up(&wdev->tx_dequeue);
	while ((item = skb_dequeue(&gc_list)) != NULL)
		wfx_skb_dtor(wdev, item);
}

void wfx_tx_queues_init(struct wfx_dev *wdev)
{
	int i;

	memset(wdev->tx_queue, 0, sizeof(wdev->tx_queue));
	skb_queue_head_init(&wdev->tx_pending);
	init_waitqueue_head(&wdev->tx_dequeue);

	for (i = 0; i < IEEE80211_NUM_ACS; ++i) {
		skb_queue_head_init(&wdev->tx_queue[i].normal);
		skb_queue_head_init(&wdev->tx_queue[i].cab);
	}
}

void wfx_tx_queues_deinit(struct wfx_dev *wdev)
{
	WARN_ON(!skb_queue_empty(&wdev->tx_pending));
	wfx_tx_queues_clear(wdev);
}

void wfx_tx_queues_put(struct wfx_dev *wdev, struct sk_buff *skb)
{
	struct wfx_queue *queue = &wdev->tx_queue[skb_get_queue_mapping(skb)];
	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);

	if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
		skb_queue_tail(&queue->cab, skb);
	else
		skb_queue_tail(&queue->normal, skb);
}

int wfx_pending_requeue(struct wfx_dev *wdev, struct sk_buff *skb)
{
	struct wfx_queue *queue = &wdev->tx_queue[skb_get_queue_mapping(skb)];

	WARN_ON(skb_get_queue_mapping(skb) > 3);
	WARN_ON(!atomic_read(&queue->pending_frames));

	atomic_dec(&queue->pending_frames);
	skb_unlink(skb, &wdev->tx_pending);
	wfx_tx_queues_put(wdev, skb);
	return 0;
}

struct sk_buff *wfx_pending_get(struct wfx_dev *wdev, u32 packet_id)
{
	struct wfx_queue *queue;
	struct hif_req_tx *req;
	struct sk_buff *skb;

	spin_lock_bh(&wdev->tx_pending.lock);
	skb_queue_walk(&wdev->tx_pending, skb) {
		req = wfx_skb_txreq(skb);
		if (req->packet_id == packet_id) {
			spin_unlock_bh(&wdev->tx_pending.lock);
			queue = &wdev->tx_queue[skb_get_queue_mapping(skb)];
			WARN_ON(skb_get_queue_mapping(skb) > 3);
			WARN_ON(!atomic_read(&queue->pending_frames));
			atomic_dec(&queue->pending_frames);
			skb_unlink(skb, &wdev->tx_pending);
			return skb;
		}
	}
	spin_unlock_bh(&wdev->tx_pending.lock);
	WARN(1, "cannot find packet in pending queue");
	return NULL;
}

void wfx_pending_dump_old_frames(struct wfx_dev *wdev, unsigned int limit_ms)
{
	ktime_t now = ktime_get();
	struct wfx_tx_priv *tx_priv;
	struct hif_req_tx *req;
	struct sk_buff *skb;
	bool first = true;

	spin_lock_bh(&wdev->tx_pending.lock);
	skb_queue_walk(&wdev->tx_pending, skb) {
		tx_priv = wfx_skb_tx_priv(skb);
		req = wfx_skb_txreq(skb);
		if (ktime_after(now, ktime_add_ms(tx_priv->xmit_timestamp,
						  limit_ms))) {
			if (first) {
				dev_info(wdev->dev, "frames stuck in firmware since %dms or more:\n",
					 limit_ms);
				first = false;
			}
			dev_info(wdev->dev, "   id %08x sent %lldms ago\n",
				 req->packet_id,
				 ktime_ms_delta(now, tx_priv->xmit_timestamp));
		}
	}
	spin_unlock_bh(&wdev->tx_pending.lock);
}

unsigned int wfx_pending_get_pkt_us_delay(struct wfx_dev *wdev,
					  struct sk_buff *skb)
{
	ktime_t now = ktime_get();
	struct wfx_tx_priv *tx_priv = wfx_skb_tx_priv(skb);

	return ktime_us_delta(now, tx_priv->xmit_timestamp);
}

bool wfx_tx_queues_has_cab(struct wfx_vif *wvif)
{
	struct wfx_dev *wdev = wvif->wdev;
	int i;

	if (wvif->vif->type != NL80211_IFTYPE_AP)
		return false;
	for (i = 0; i < IEEE80211_NUM_ACS; ++i)
		// Note: since only AP can have mcast frames in queue and only
		// one vif can be AP, all queued frames has same interface id
		if (!skb_queue_empty_lockless(&wdev->tx_queue[i].cab))
			return true;
	return false;
}

bool wfx_tx_queues_empty(struct wfx_dev *wdev)
{
	int i;

	for (i = 0; i < IEEE80211_NUM_ACS; i++)
		if (!skb_queue_empty_lockless(&wdev->tx_queue[i].normal) ||
		    !skb_queue_empty_lockless(&wdev->tx_queue[i].cab))
			return false;
	return true;
}

static bool wfx_handle_tx_data(struct wfx_dev *wdev, struct sk_buff *skb)
{
	struct hif_req_tx *req = wfx_skb_txreq(skb);
	struct ieee80211_key_conf *hw_key = wfx_skb_tx_priv(skb)->hw_key;
	struct ieee80211_hdr *frame =
		(struct ieee80211_hdr *)(req->frame + req->data_flags.fc_offset);
	struct wfx_vif *wvif =
		wdev_to_wvif(wdev, ((struct hif_msg *)skb->data)->interface);

	if (!wvif)
		return false;

	// FIXME: mac80211 is smart enough to handle BSS loss. Driver should not
	// try to do anything about that.
	if (ieee80211_is_nullfunc(frame->frame_control)) {
		mutex_lock(&wvif->bss_loss_lock);
		if (wvif->bss_loss_state) {
			wvif->bss_loss_confirm_id = req->packet_id;
			req->queue_id.queue_id = HIF_QUEUE_ID_VOICE;
		}
		mutex_unlock(&wvif->bss_loss_lock);
	}

	// FIXME: identify the exact scenario matched by this condition. Does it
	// happen yet?
	if (ieee80211_has_protected(frame->frame_control) &&
	    hw_key && hw_key->keyidx != wvif->wep_default_key_id &&
	    (hw_key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
	     hw_key->cipher == WLAN_CIPHER_SUITE_WEP104)) {
		wfx_tx_lock(wdev);
		WARN_ON(wvif->wep_pending_skb);
		wvif->wep_default_key_id = hw_key->keyidx;
		wvif->wep_pending_skb = skb;
		if (!schedule_work(&wvif->wep_key_work))
			wfx_tx_unlock(wdev);
		return true;
	} else {
		return false;
	}
}

static struct sk_buff *wfx_tx_queues_get_skb(struct wfx_dev *wdev)
{
	struct wfx_queue *sorted_queues[IEEE80211_NUM_ACS];
	struct wfx_vif *wvif;
	struct hif_msg *hif;
	struct sk_buff *skb;
	int i, j;

	// bubble sort
	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
		sorted_queues[i] = &wdev->tx_queue[i];
		for (j = i; j > 0; j--)
			if (atomic_read(&sorted_queues[j]->pending_frames) >
			    atomic_read(&sorted_queues[j - 1]->pending_frames))
				swap(sorted_queues[j - 1], sorted_queues[j]);
	}
	wvif = NULL;
	while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
		if (!wvif->after_dtim_tx_allowed)
			continue;
		for (i = 0; i < IEEE80211_NUM_ACS; i++) {
			skb = skb_dequeue(&sorted_queues[i]->cab);
			if (!skb)
				continue;
			// Note: since only AP can have mcast frames in queue
			// and only one vif can be AP, all queued frames has
			// same interface id
			hif = (struct hif_msg *)skb->data;
			WARN_ON(hif->interface != wvif->id);
			WARN_ON(sorted_queues[i] !=
				&wdev->tx_queue[skb_get_queue_mapping(skb)]);
			atomic_inc(&sorted_queues[i]->pending_frames);
			return skb;
		}
		// No more multicast to sent
		wvif->after_dtim_tx_allowed = false;
		schedule_work(&wvif->update_tim_work);
	}
	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
		skb = skb_dequeue(&sorted_queues[i]->normal);
		if (skb) {
			WARN_ON(sorted_queues[i] !=
				&wdev->tx_queue[skb_get_queue_mapping(skb)]);
			atomic_inc(&sorted_queues[i]->pending_frames);
			return skb;
		}
	}
	return NULL;
}

struct hif_msg *wfx_tx_queues_get(struct wfx_dev *wdev)
{
	struct wfx_tx_priv *tx_priv;
	struct sk_buff *skb;

	if (atomic_read(&wdev->tx_lock))
		return NULL;

	for (;;) {
		skb = wfx_tx_queues_get_skb(wdev);
		if (!skb)
			return NULL;
		skb_queue_tail(&wdev->tx_pending, skb);
		if (wfx_tx_queues_empty(wdev))
			wake_up(&wdev->tx_dequeue);
		// FIXME: is it useful?
		if (wfx_handle_tx_data(wdev, skb))
			continue;
		tx_priv = wfx_skb_tx_priv(skb);
		tx_priv->xmit_timestamp = ktime_get();
		return (struct hif_msg *)skb->data;
	}
}