Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * O(1) TX queue with built-in allocator for ST-Ericsson CW1200 drivers
4 *
5 * Copyright (c) 2010, ST-Ericsson
6 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
7 */
8
9#include <net/mac80211.h>
10#include <linux/sched.h>
11#include <linux/jiffies.h>
12#include "queue.h"
13#include "cw1200.h"
14#include "debug.h"
15
16/* private */ struct cw1200_queue_item
17{
18 struct list_head head;
19 struct sk_buff *skb;
20 u32 packet_id;
21 unsigned long queue_timestamp;
22 unsigned long xmit_timestamp;
23 struct cw1200_txpriv txpriv;
24 u8 generation;
25};
26
27static inline void __cw1200_queue_lock(struct cw1200_queue *queue)
28{
29 struct cw1200_queue_stats *stats = queue->stats;
30 if (queue->tx_locked_cnt++ == 0) {
31 pr_debug("[TX] Queue %d is locked.\n",
32 queue->queue_id);
33 ieee80211_stop_queue(stats->priv->hw, queue->queue_id);
34 }
35}
36
37static inline void __cw1200_queue_unlock(struct cw1200_queue *queue)
38{
39 struct cw1200_queue_stats *stats = queue->stats;
40 BUG_ON(!queue->tx_locked_cnt);
41 if (--queue->tx_locked_cnt == 0) {
42 pr_debug("[TX] Queue %d is unlocked.\n",
43 queue->queue_id);
44 ieee80211_wake_queue(stats->priv->hw, queue->queue_id);
45 }
46}
47
48static inline void cw1200_queue_parse_id(u32 packet_id, u8 *queue_generation,
49 u8 *queue_id, u8 *item_generation,
50 u8 *item_id)
51{
52 *item_id = (packet_id >> 0) & 0xFF;
53 *item_generation = (packet_id >> 8) & 0xFF;
54 *queue_id = (packet_id >> 16) & 0xFF;
55 *queue_generation = (packet_id >> 24) & 0xFF;
56}
57
58static inline u32 cw1200_queue_mk_packet_id(u8 queue_generation, u8 queue_id,
59 u8 item_generation, u8 item_id)
60{
61 return ((u32)item_id << 0) |
62 ((u32)item_generation << 8) |
63 ((u32)queue_id << 16) |
64 ((u32)queue_generation << 24);
65}
66
67static void cw1200_queue_post_gc(struct cw1200_queue_stats *stats,
68 struct list_head *gc_list)
69{
70 struct cw1200_queue_item *item, *tmp;
71
72 list_for_each_entry_safe(item, tmp, gc_list, head) {
73 list_del(&item->head);
74 stats->skb_dtor(stats->priv, item->skb, &item->txpriv);
75 kfree(item);
76 }
77}
78
79static void cw1200_queue_register_post_gc(struct list_head *gc_list,
80 struct cw1200_queue_item *item)
81{
82 struct cw1200_queue_item *gc_item;
83 gc_item = kmemdup(item, sizeof(struct cw1200_queue_item),
84 GFP_ATOMIC);
85 BUG_ON(!gc_item);
86 list_add_tail(&gc_item->head, gc_list);
87}
88
89static void __cw1200_queue_gc(struct cw1200_queue *queue,
90 struct list_head *head,
91 bool unlock)
92{
93 struct cw1200_queue_stats *stats = queue->stats;
94 struct cw1200_queue_item *item = NULL, *iter, *tmp;
95 bool wakeup_stats = false;
96
97 list_for_each_entry_safe(iter, tmp, &queue->queue, head) {
98 if (time_is_after_jiffies(iter->queue_timestamp + queue->ttl)) {
99 item = iter;
100 break;
101 }
102 --queue->num_queued;
103 --queue->link_map_cache[iter->txpriv.link_id];
104 spin_lock_bh(&stats->lock);
105 --stats->num_queued;
106 if (!--stats->link_map_cache[iter->txpriv.link_id])
107 wakeup_stats = true;
108 spin_unlock_bh(&stats->lock);
109 cw1200_debug_tx_ttl(stats->priv);
110 cw1200_queue_register_post_gc(head, iter);
111 iter->skb = NULL;
112 list_move_tail(&iter->head, &queue->free_pool);
113 }
114
115 if (wakeup_stats)
116 wake_up(&stats->wait_link_id_empty);
117
118 if (queue->overfull) {
119 if (queue->num_queued <= (queue->capacity >> 1)) {
120 queue->overfull = false;
121 if (unlock)
122 __cw1200_queue_unlock(queue);
123 } else if (item) {
124 unsigned long tmo = item->queue_timestamp + queue->ttl;
125 mod_timer(&queue->gc, tmo);
126 cw1200_pm_stay_awake(&stats->priv->pm_state,
127 tmo - jiffies);
128 }
129 }
130}
131
132static void cw1200_queue_gc(struct timer_list *t)
133{
134 LIST_HEAD(list);
135 struct cw1200_queue *queue =
136 from_timer(queue, t, gc);
137
138 spin_lock_bh(&queue->lock);
139 __cw1200_queue_gc(queue, &list, true);
140 spin_unlock_bh(&queue->lock);
141 cw1200_queue_post_gc(queue->stats, &list);
142}
143
144int cw1200_queue_stats_init(struct cw1200_queue_stats *stats,
145 size_t map_capacity,
146 cw1200_queue_skb_dtor_t skb_dtor,
147 struct cw1200_common *priv)
148{
149 memset(stats, 0, sizeof(*stats));
150 stats->map_capacity = map_capacity;
151 stats->skb_dtor = skb_dtor;
152 stats->priv = priv;
153 spin_lock_init(&stats->lock);
154 init_waitqueue_head(&stats->wait_link_id_empty);
155
156 stats->link_map_cache = kcalloc(map_capacity, sizeof(int),
157 GFP_KERNEL);
158 if (!stats->link_map_cache)
159 return -ENOMEM;
160
161 return 0;
162}
163
164int cw1200_queue_init(struct cw1200_queue *queue,
165 struct cw1200_queue_stats *stats,
166 u8 queue_id,
167 size_t capacity,
168 unsigned long ttl)
169{
170 size_t i;
171
172 memset(queue, 0, sizeof(*queue));
173 queue->stats = stats;
174 queue->capacity = capacity;
175 queue->queue_id = queue_id;
176 queue->ttl = ttl;
177 INIT_LIST_HEAD(&queue->queue);
178 INIT_LIST_HEAD(&queue->pending);
179 INIT_LIST_HEAD(&queue->free_pool);
180 spin_lock_init(&queue->lock);
181 timer_setup(&queue->gc, cw1200_queue_gc, 0);
182
183 queue->pool = kcalloc(capacity, sizeof(struct cw1200_queue_item),
184 GFP_KERNEL);
185 if (!queue->pool)
186 return -ENOMEM;
187
188 queue->link_map_cache = kcalloc(stats->map_capacity, sizeof(int),
189 GFP_KERNEL);
190 if (!queue->link_map_cache) {
191 kfree(queue->pool);
192 queue->pool = NULL;
193 return -ENOMEM;
194 }
195
196 for (i = 0; i < capacity; ++i)
197 list_add_tail(&queue->pool[i].head, &queue->free_pool);
198
199 return 0;
200}
201
202int cw1200_queue_clear(struct cw1200_queue *queue)
203{
204 int i;
205 LIST_HEAD(gc_list);
206 struct cw1200_queue_stats *stats = queue->stats;
207 struct cw1200_queue_item *item, *tmp;
208
209 spin_lock_bh(&queue->lock);
210 queue->generation++;
211 list_splice_tail_init(&queue->queue, &queue->pending);
212 list_for_each_entry_safe(item, tmp, &queue->pending, head) {
213 WARN_ON(!item->skb);
214 cw1200_queue_register_post_gc(&gc_list, item);
215 item->skb = NULL;
216 list_move_tail(&item->head, &queue->free_pool);
217 }
218 queue->num_queued = 0;
219 queue->num_pending = 0;
220
221 spin_lock_bh(&stats->lock);
222 for (i = 0; i < stats->map_capacity; ++i) {
223 stats->num_queued -= queue->link_map_cache[i];
224 stats->link_map_cache[i] -= queue->link_map_cache[i];
225 queue->link_map_cache[i] = 0;
226 }
227 spin_unlock_bh(&stats->lock);
228 if (queue->overfull) {
229 queue->overfull = false;
230 __cw1200_queue_unlock(queue);
231 }
232 spin_unlock_bh(&queue->lock);
233 wake_up(&stats->wait_link_id_empty);
234 cw1200_queue_post_gc(stats, &gc_list);
235 return 0;
236}
237
238void cw1200_queue_stats_deinit(struct cw1200_queue_stats *stats)
239{
240 kfree(stats->link_map_cache);
241 stats->link_map_cache = NULL;
242}
243
244void cw1200_queue_deinit(struct cw1200_queue *queue)
245{
246 cw1200_queue_clear(queue);
247 del_timer_sync(&queue->gc);
248 INIT_LIST_HEAD(&queue->free_pool);
249 kfree(queue->pool);
250 kfree(queue->link_map_cache);
251 queue->pool = NULL;
252 queue->link_map_cache = NULL;
253 queue->capacity = 0;
254}
255
256size_t cw1200_queue_get_num_queued(struct cw1200_queue *queue,
257 u32 link_id_map)
258{
259 size_t ret;
260 int i, bit;
261 size_t map_capacity = queue->stats->map_capacity;
262
263 if (!link_id_map)
264 return 0;
265
266 spin_lock_bh(&queue->lock);
267 if (link_id_map == (u32)-1) {
268 ret = queue->num_queued - queue->num_pending;
269 } else {
270 ret = 0;
271 for (i = 0, bit = 1; i < map_capacity; ++i, bit <<= 1) {
272 if (link_id_map & bit)
273 ret += queue->link_map_cache[i];
274 }
275 }
276 spin_unlock_bh(&queue->lock);
277 return ret;
278}
279
280int cw1200_queue_put(struct cw1200_queue *queue,
281 struct sk_buff *skb,
282 struct cw1200_txpriv *txpriv)
283{
284 int ret = 0;
285 struct cw1200_queue_stats *stats = queue->stats;
286
287 if (txpriv->link_id >= queue->stats->map_capacity)
288 return -EINVAL;
289
290 spin_lock_bh(&queue->lock);
291 if (!WARN_ON(list_empty(&queue->free_pool))) {
292 struct cw1200_queue_item *item = list_first_entry(
293 &queue->free_pool, struct cw1200_queue_item, head);
294 BUG_ON(item->skb);
295
296 list_move_tail(&item->head, &queue->queue);
297 item->skb = skb;
298 item->txpriv = *txpriv;
299 item->generation = 0;
300 item->packet_id = cw1200_queue_mk_packet_id(queue->generation,
301 queue->queue_id,
302 item->generation,
303 item - queue->pool);
304 item->queue_timestamp = jiffies;
305
306 ++queue->num_queued;
307 ++queue->link_map_cache[txpriv->link_id];
308
309 spin_lock_bh(&stats->lock);
310 ++stats->num_queued;
311 ++stats->link_map_cache[txpriv->link_id];
312 spin_unlock_bh(&stats->lock);
313
314 /* TX may happen in parallel sometimes.
315 * Leave extra queue slots so we don't overflow.
316 */
317 if (queue->overfull == false &&
318 queue->num_queued >=
319 (queue->capacity - (num_present_cpus() - 1))) {
320 queue->overfull = true;
321 __cw1200_queue_lock(queue);
322 mod_timer(&queue->gc, jiffies);
323 }
324 } else {
325 ret = -ENOENT;
326 }
327 spin_unlock_bh(&queue->lock);
328 return ret;
329}
330
331int cw1200_queue_get(struct cw1200_queue *queue,
332 u32 link_id_map,
333 struct wsm_tx **tx,
334 struct ieee80211_tx_info **tx_info,
335 const struct cw1200_txpriv **txpriv)
336{
337 int ret = -ENOENT;
338 struct cw1200_queue_item *item;
339 struct cw1200_queue_stats *stats = queue->stats;
340 bool wakeup_stats = false;
341
342 spin_lock_bh(&queue->lock);
343 list_for_each_entry(item, &queue->queue, head) {
344 if (link_id_map & BIT(item->txpriv.link_id)) {
345 ret = 0;
346 break;
347 }
348 }
349
350 if (!WARN_ON(ret)) {
351 *tx = (struct wsm_tx *)item->skb->data;
352 *tx_info = IEEE80211_SKB_CB(item->skb);
353 *txpriv = &item->txpriv;
354 (*tx)->packet_id = item->packet_id;
355 list_move_tail(&item->head, &queue->pending);
356 ++queue->num_pending;
357 --queue->link_map_cache[item->txpriv.link_id];
358 item->xmit_timestamp = jiffies;
359
360 spin_lock_bh(&stats->lock);
361 --stats->num_queued;
362 if (!--stats->link_map_cache[item->txpriv.link_id])
363 wakeup_stats = true;
364 spin_unlock_bh(&stats->lock);
365 }
366 spin_unlock_bh(&queue->lock);
367 if (wakeup_stats)
368 wake_up(&stats->wait_link_id_empty);
369 return ret;
370}
371
372int cw1200_queue_requeue(struct cw1200_queue *queue, u32 packet_id)
373{
374 int ret = 0;
375 u8 queue_generation, queue_id, item_generation, item_id;
376 struct cw1200_queue_item *item;
377 struct cw1200_queue_stats *stats = queue->stats;
378
379 cw1200_queue_parse_id(packet_id, &queue_generation, &queue_id,
380 &item_generation, &item_id);
381
382 item = &queue->pool[item_id];
383
384 spin_lock_bh(&queue->lock);
385 BUG_ON(queue_id != queue->queue_id);
386 if (queue_generation != queue->generation) {
387 ret = -ENOENT;
388 } else if (item_id >= (unsigned) queue->capacity) {
389 WARN_ON(1);
390 ret = -EINVAL;
391 } else if (item->generation != item_generation) {
392 WARN_ON(1);
393 ret = -ENOENT;
394 } else {
395 --queue->num_pending;
396 ++queue->link_map_cache[item->txpriv.link_id];
397
398 spin_lock_bh(&stats->lock);
399 ++stats->num_queued;
400 ++stats->link_map_cache[item->txpriv.link_id];
401 spin_unlock_bh(&stats->lock);
402
403 item->generation = ++item_generation;
404 item->packet_id = cw1200_queue_mk_packet_id(queue_generation,
405 queue_id,
406 item_generation,
407 item_id);
408 list_move(&item->head, &queue->queue);
409 }
410 spin_unlock_bh(&queue->lock);
411 return ret;
412}
413
414int cw1200_queue_requeue_all(struct cw1200_queue *queue)
415{
416 struct cw1200_queue_item *item, *tmp;
417 struct cw1200_queue_stats *stats = queue->stats;
418 spin_lock_bh(&queue->lock);
419
420 list_for_each_entry_safe_reverse(item, tmp, &queue->pending, head) {
421 --queue->num_pending;
422 ++queue->link_map_cache[item->txpriv.link_id];
423
424 spin_lock_bh(&stats->lock);
425 ++stats->num_queued;
426 ++stats->link_map_cache[item->txpriv.link_id];
427 spin_unlock_bh(&stats->lock);
428
429 ++item->generation;
430 item->packet_id = cw1200_queue_mk_packet_id(queue->generation,
431 queue->queue_id,
432 item->generation,
433 item - queue->pool);
434 list_move(&item->head, &queue->queue);
435 }
436 spin_unlock_bh(&queue->lock);
437
438 return 0;
439}
440
441int cw1200_queue_remove(struct cw1200_queue *queue, u32 packet_id)
442{
443 int ret = 0;
444 u8 queue_generation, queue_id, item_generation, item_id;
445 struct cw1200_queue_item *item;
446 struct cw1200_queue_stats *stats = queue->stats;
447 struct sk_buff *gc_skb = NULL;
448 struct cw1200_txpriv gc_txpriv;
449
450 cw1200_queue_parse_id(packet_id, &queue_generation, &queue_id,
451 &item_generation, &item_id);
452
453 item = &queue->pool[item_id];
454
455 spin_lock_bh(&queue->lock);
456 BUG_ON(queue_id != queue->queue_id);
457 if (queue_generation != queue->generation) {
458 ret = -ENOENT;
459 } else if (item_id >= (unsigned) queue->capacity) {
460 WARN_ON(1);
461 ret = -EINVAL;
462 } else if (item->generation != item_generation) {
463 WARN_ON(1);
464 ret = -ENOENT;
465 } else {
466 gc_txpriv = item->txpriv;
467 gc_skb = item->skb;
468 item->skb = NULL;
469 --queue->num_pending;
470 --queue->num_queued;
471 ++queue->num_sent;
472 ++item->generation;
473 /* Do not use list_move_tail here, but list_move:
474 * try to utilize cache row.
475 */
476 list_move(&item->head, &queue->free_pool);
477
478 if (queue->overfull &&
479 (queue->num_queued <= (queue->capacity >> 1))) {
480 queue->overfull = false;
481 __cw1200_queue_unlock(queue);
482 }
483 }
484 spin_unlock_bh(&queue->lock);
485
486 if (gc_skb)
487 stats->skb_dtor(stats->priv, gc_skb, &gc_txpriv);
488
489 return ret;
490}
491
492int cw1200_queue_get_skb(struct cw1200_queue *queue, u32 packet_id,
493 struct sk_buff **skb,
494 const struct cw1200_txpriv **txpriv)
495{
496 int ret = 0;
497 u8 queue_generation, queue_id, item_generation, item_id;
498 struct cw1200_queue_item *item;
499 cw1200_queue_parse_id(packet_id, &queue_generation, &queue_id,
500 &item_generation, &item_id);
501
502 item = &queue->pool[item_id];
503
504 spin_lock_bh(&queue->lock);
505 BUG_ON(queue_id != queue->queue_id);
506 if (queue_generation != queue->generation) {
507 ret = -ENOENT;
508 } else if (item_id >= (unsigned) queue->capacity) {
509 WARN_ON(1);
510 ret = -EINVAL;
511 } else if (item->generation != item_generation) {
512 WARN_ON(1);
513 ret = -ENOENT;
514 } else {
515 *skb = item->skb;
516 *txpriv = &item->txpriv;
517 }
518 spin_unlock_bh(&queue->lock);
519 return ret;
520}
521
522void cw1200_queue_lock(struct cw1200_queue *queue)
523{
524 spin_lock_bh(&queue->lock);
525 __cw1200_queue_lock(queue);
526 spin_unlock_bh(&queue->lock);
527}
528
529void cw1200_queue_unlock(struct cw1200_queue *queue)
530{
531 spin_lock_bh(&queue->lock);
532 __cw1200_queue_unlock(queue);
533 spin_unlock_bh(&queue->lock);
534}
535
536bool cw1200_queue_get_xmit_timestamp(struct cw1200_queue *queue,
537 unsigned long *timestamp,
538 u32 pending_frame_id)
539{
540 struct cw1200_queue_item *item;
541 bool ret;
542
543 spin_lock_bh(&queue->lock);
544 ret = !list_empty(&queue->pending);
545 if (ret) {
546 list_for_each_entry(item, &queue->pending, head) {
547 if (item->packet_id != pending_frame_id)
548 if (time_before(item->xmit_timestamp,
549 *timestamp))
550 *timestamp = item->xmit_timestamp;
551 }
552 }
553 spin_unlock_bh(&queue->lock);
554 return ret;
555}
556
557bool cw1200_queue_stats_is_empty(struct cw1200_queue_stats *stats,
558 u32 link_id_map)
559{
560 bool empty = true;
561
562 spin_lock_bh(&stats->lock);
563 if (link_id_map == (u32)-1) {
564 empty = stats->num_queued == 0;
565 } else {
566 int i;
567 for (i = 0; i < stats->map_capacity; ++i) {
568 if (link_id_map & BIT(i)) {
569 if (stats->link_map_cache[i]) {
570 empty = false;
571 break;
572 }
573 }
574 }
575 }
576 spin_unlock_bh(&stats->lock);
577
578 return empty;
579}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * O(1) TX queue with built-in allocator for ST-Ericsson CW1200 drivers
4 *
5 * Copyright (c) 2010, ST-Ericsson
6 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
7 */
8
9#include <net/mac80211.h>
10#include <linux/sched.h>
11#include "queue.h"
12#include "cw1200.h"
13#include "debug.h"
14
15/* private */ struct cw1200_queue_item
16{
17 struct list_head head;
18 struct sk_buff *skb;
19 u32 packet_id;
20 unsigned long queue_timestamp;
21 unsigned long xmit_timestamp;
22 struct cw1200_txpriv txpriv;
23 u8 generation;
24};
25
26static inline void __cw1200_queue_lock(struct cw1200_queue *queue)
27{
28 struct cw1200_queue_stats *stats = queue->stats;
29 if (queue->tx_locked_cnt++ == 0) {
30 pr_debug("[TX] Queue %d is locked.\n",
31 queue->queue_id);
32 ieee80211_stop_queue(stats->priv->hw, queue->queue_id);
33 }
34}
35
36static inline void __cw1200_queue_unlock(struct cw1200_queue *queue)
37{
38 struct cw1200_queue_stats *stats = queue->stats;
39 BUG_ON(!queue->tx_locked_cnt);
40 if (--queue->tx_locked_cnt == 0) {
41 pr_debug("[TX] Queue %d is unlocked.\n",
42 queue->queue_id);
43 ieee80211_wake_queue(stats->priv->hw, queue->queue_id);
44 }
45}
46
47static inline void cw1200_queue_parse_id(u32 packet_id, u8 *queue_generation,
48 u8 *queue_id, u8 *item_generation,
49 u8 *item_id)
50{
51 *item_id = (packet_id >> 0) & 0xFF;
52 *item_generation = (packet_id >> 8) & 0xFF;
53 *queue_id = (packet_id >> 16) & 0xFF;
54 *queue_generation = (packet_id >> 24) & 0xFF;
55}
56
57static inline u32 cw1200_queue_mk_packet_id(u8 queue_generation, u8 queue_id,
58 u8 item_generation, u8 item_id)
59{
60 return ((u32)item_id << 0) |
61 ((u32)item_generation << 8) |
62 ((u32)queue_id << 16) |
63 ((u32)queue_generation << 24);
64}
65
66static void cw1200_queue_post_gc(struct cw1200_queue_stats *stats,
67 struct list_head *gc_list)
68{
69 struct cw1200_queue_item *item, *tmp;
70
71 list_for_each_entry_safe(item, tmp, gc_list, head) {
72 list_del(&item->head);
73 stats->skb_dtor(stats->priv, item->skb, &item->txpriv);
74 kfree(item);
75 }
76}
77
78static void cw1200_queue_register_post_gc(struct list_head *gc_list,
79 struct cw1200_queue_item *item)
80{
81 struct cw1200_queue_item *gc_item;
82 gc_item = kmemdup(item, sizeof(struct cw1200_queue_item),
83 GFP_ATOMIC);
84 BUG_ON(!gc_item);
85 list_add_tail(&gc_item->head, gc_list);
86}
87
88static void __cw1200_queue_gc(struct cw1200_queue *queue,
89 struct list_head *head,
90 bool unlock)
91{
92 struct cw1200_queue_stats *stats = queue->stats;
93 struct cw1200_queue_item *item = NULL, *tmp;
94 bool wakeup_stats = false;
95
96 list_for_each_entry_safe(item, tmp, &queue->queue, head) {
97 if (jiffies - item->queue_timestamp < queue->ttl)
98 break;
99 --queue->num_queued;
100 --queue->link_map_cache[item->txpriv.link_id];
101 spin_lock_bh(&stats->lock);
102 --stats->num_queued;
103 if (!--stats->link_map_cache[item->txpriv.link_id])
104 wakeup_stats = true;
105 spin_unlock_bh(&stats->lock);
106 cw1200_debug_tx_ttl(stats->priv);
107 cw1200_queue_register_post_gc(head, item);
108 item->skb = NULL;
109 list_move_tail(&item->head, &queue->free_pool);
110 }
111
112 if (wakeup_stats)
113 wake_up(&stats->wait_link_id_empty);
114
115 if (queue->overfull) {
116 if (queue->num_queued <= (queue->capacity >> 1)) {
117 queue->overfull = false;
118 if (unlock)
119 __cw1200_queue_unlock(queue);
120 } else if (item) {
121 unsigned long tmo = item->queue_timestamp + queue->ttl;
122 mod_timer(&queue->gc, tmo);
123 cw1200_pm_stay_awake(&stats->priv->pm_state,
124 tmo - jiffies);
125 }
126 }
127}
128
129static void cw1200_queue_gc(struct timer_list *t)
130{
131 LIST_HEAD(list);
132 struct cw1200_queue *queue =
133 from_timer(queue, t, gc);
134
135 spin_lock_bh(&queue->lock);
136 __cw1200_queue_gc(queue, &list, true);
137 spin_unlock_bh(&queue->lock);
138 cw1200_queue_post_gc(queue->stats, &list);
139}
140
141int cw1200_queue_stats_init(struct cw1200_queue_stats *stats,
142 size_t map_capacity,
143 cw1200_queue_skb_dtor_t skb_dtor,
144 struct cw1200_common *priv)
145{
146 memset(stats, 0, sizeof(*stats));
147 stats->map_capacity = map_capacity;
148 stats->skb_dtor = skb_dtor;
149 stats->priv = priv;
150 spin_lock_init(&stats->lock);
151 init_waitqueue_head(&stats->wait_link_id_empty);
152
153 stats->link_map_cache = kcalloc(map_capacity, sizeof(int),
154 GFP_KERNEL);
155 if (!stats->link_map_cache)
156 return -ENOMEM;
157
158 return 0;
159}
160
161int cw1200_queue_init(struct cw1200_queue *queue,
162 struct cw1200_queue_stats *stats,
163 u8 queue_id,
164 size_t capacity,
165 unsigned long ttl)
166{
167 size_t i;
168
169 memset(queue, 0, sizeof(*queue));
170 queue->stats = stats;
171 queue->capacity = capacity;
172 queue->queue_id = queue_id;
173 queue->ttl = ttl;
174 INIT_LIST_HEAD(&queue->queue);
175 INIT_LIST_HEAD(&queue->pending);
176 INIT_LIST_HEAD(&queue->free_pool);
177 spin_lock_init(&queue->lock);
178 timer_setup(&queue->gc, cw1200_queue_gc, 0);
179
180 queue->pool = kcalloc(capacity, sizeof(struct cw1200_queue_item),
181 GFP_KERNEL);
182 if (!queue->pool)
183 return -ENOMEM;
184
185 queue->link_map_cache = kcalloc(stats->map_capacity, sizeof(int),
186 GFP_KERNEL);
187 if (!queue->link_map_cache) {
188 kfree(queue->pool);
189 queue->pool = NULL;
190 return -ENOMEM;
191 }
192
193 for (i = 0; i < capacity; ++i)
194 list_add_tail(&queue->pool[i].head, &queue->free_pool);
195
196 return 0;
197}
198
199int cw1200_queue_clear(struct cw1200_queue *queue)
200{
201 int i;
202 LIST_HEAD(gc_list);
203 struct cw1200_queue_stats *stats = queue->stats;
204 struct cw1200_queue_item *item, *tmp;
205
206 spin_lock_bh(&queue->lock);
207 queue->generation++;
208 list_splice_tail_init(&queue->queue, &queue->pending);
209 list_for_each_entry_safe(item, tmp, &queue->pending, head) {
210 WARN_ON(!item->skb);
211 cw1200_queue_register_post_gc(&gc_list, item);
212 item->skb = NULL;
213 list_move_tail(&item->head, &queue->free_pool);
214 }
215 queue->num_queued = 0;
216 queue->num_pending = 0;
217
218 spin_lock_bh(&stats->lock);
219 for (i = 0; i < stats->map_capacity; ++i) {
220 stats->num_queued -= queue->link_map_cache[i];
221 stats->link_map_cache[i] -= queue->link_map_cache[i];
222 queue->link_map_cache[i] = 0;
223 }
224 spin_unlock_bh(&stats->lock);
225 if (queue->overfull) {
226 queue->overfull = false;
227 __cw1200_queue_unlock(queue);
228 }
229 spin_unlock_bh(&queue->lock);
230 wake_up(&stats->wait_link_id_empty);
231 cw1200_queue_post_gc(stats, &gc_list);
232 return 0;
233}
234
235void cw1200_queue_stats_deinit(struct cw1200_queue_stats *stats)
236{
237 kfree(stats->link_map_cache);
238 stats->link_map_cache = NULL;
239}
240
241void cw1200_queue_deinit(struct cw1200_queue *queue)
242{
243 cw1200_queue_clear(queue);
244 del_timer_sync(&queue->gc);
245 INIT_LIST_HEAD(&queue->free_pool);
246 kfree(queue->pool);
247 kfree(queue->link_map_cache);
248 queue->pool = NULL;
249 queue->link_map_cache = NULL;
250 queue->capacity = 0;
251}
252
253size_t cw1200_queue_get_num_queued(struct cw1200_queue *queue,
254 u32 link_id_map)
255{
256 size_t ret;
257 int i, bit;
258 size_t map_capacity = queue->stats->map_capacity;
259
260 if (!link_id_map)
261 return 0;
262
263 spin_lock_bh(&queue->lock);
264 if (link_id_map == (u32)-1) {
265 ret = queue->num_queued - queue->num_pending;
266 } else {
267 ret = 0;
268 for (i = 0, bit = 1; i < map_capacity; ++i, bit <<= 1) {
269 if (link_id_map & bit)
270 ret += queue->link_map_cache[i];
271 }
272 }
273 spin_unlock_bh(&queue->lock);
274 return ret;
275}
276
277int cw1200_queue_put(struct cw1200_queue *queue,
278 struct sk_buff *skb,
279 struct cw1200_txpriv *txpriv)
280{
281 int ret = 0;
282 struct cw1200_queue_stats *stats = queue->stats;
283
284 if (txpriv->link_id >= queue->stats->map_capacity)
285 return -EINVAL;
286
287 spin_lock_bh(&queue->lock);
288 if (!WARN_ON(list_empty(&queue->free_pool))) {
289 struct cw1200_queue_item *item = list_first_entry(
290 &queue->free_pool, struct cw1200_queue_item, head);
291 BUG_ON(item->skb);
292
293 list_move_tail(&item->head, &queue->queue);
294 item->skb = skb;
295 item->txpriv = *txpriv;
296 item->generation = 0;
297 item->packet_id = cw1200_queue_mk_packet_id(queue->generation,
298 queue->queue_id,
299 item->generation,
300 item - queue->pool);
301 item->queue_timestamp = jiffies;
302
303 ++queue->num_queued;
304 ++queue->link_map_cache[txpriv->link_id];
305
306 spin_lock_bh(&stats->lock);
307 ++stats->num_queued;
308 ++stats->link_map_cache[txpriv->link_id];
309 spin_unlock_bh(&stats->lock);
310
311 /* TX may happen in parallel sometimes.
312 * Leave extra queue slots so we don't overflow.
313 */
314 if (queue->overfull == false &&
315 queue->num_queued >=
316 (queue->capacity - (num_present_cpus() - 1))) {
317 queue->overfull = true;
318 __cw1200_queue_lock(queue);
319 mod_timer(&queue->gc, jiffies);
320 }
321 } else {
322 ret = -ENOENT;
323 }
324 spin_unlock_bh(&queue->lock);
325 return ret;
326}
327
328int cw1200_queue_get(struct cw1200_queue *queue,
329 u32 link_id_map,
330 struct wsm_tx **tx,
331 struct ieee80211_tx_info **tx_info,
332 const struct cw1200_txpriv **txpriv)
333{
334 int ret = -ENOENT;
335 struct cw1200_queue_item *item;
336 struct cw1200_queue_stats *stats = queue->stats;
337 bool wakeup_stats = false;
338
339 spin_lock_bh(&queue->lock);
340 list_for_each_entry(item, &queue->queue, head) {
341 if (link_id_map & BIT(item->txpriv.link_id)) {
342 ret = 0;
343 break;
344 }
345 }
346
347 if (!WARN_ON(ret)) {
348 *tx = (struct wsm_tx *)item->skb->data;
349 *tx_info = IEEE80211_SKB_CB(item->skb);
350 *txpriv = &item->txpriv;
351 (*tx)->packet_id = item->packet_id;
352 list_move_tail(&item->head, &queue->pending);
353 ++queue->num_pending;
354 --queue->link_map_cache[item->txpriv.link_id];
355 item->xmit_timestamp = jiffies;
356
357 spin_lock_bh(&stats->lock);
358 --stats->num_queued;
359 if (!--stats->link_map_cache[item->txpriv.link_id])
360 wakeup_stats = true;
361 spin_unlock_bh(&stats->lock);
362 }
363 spin_unlock_bh(&queue->lock);
364 if (wakeup_stats)
365 wake_up(&stats->wait_link_id_empty);
366 return ret;
367}
368
369int cw1200_queue_requeue(struct cw1200_queue *queue, u32 packet_id)
370{
371 int ret = 0;
372 u8 queue_generation, queue_id, item_generation, item_id;
373 struct cw1200_queue_item *item;
374 struct cw1200_queue_stats *stats = queue->stats;
375
376 cw1200_queue_parse_id(packet_id, &queue_generation, &queue_id,
377 &item_generation, &item_id);
378
379 item = &queue->pool[item_id];
380
381 spin_lock_bh(&queue->lock);
382 BUG_ON(queue_id != queue->queue_id);
383 if (queue_generation != queue->generation) {
384 ret = -ENOENT;
385 } else if (item_id >= (unsigned) queue->capacity) {
386 WARN_ON(1);
387 ret = -EINVAL;
388 } else if (item->generation != item_generation) {
389 WARN_ON(1);
390 ret = -ENOENT;
391 } else {
392 --queue->num_pending;
393 ++queue->link_map_cache[item->txpriv.link_id];
394
395 spin_lock_bh(&stats->lock);
396 ++stats->num_queued;
397 ++stats->link_map_cache[item->txpriv.link_id];
398 spin_unlock_bh(&stats->lock);
399
400 item->generation = ++item_generation;
401 item->packet_id = cw1200_queue_mk_packet_id(queue_generation,
402 queue_id,
403 item_generation,
404 item_id);
405 list_move(&item->head, &queue->queue);
406 }
407 spin_unlock_bh(&queue->lock);
408 return ret;
409}
410
411int cw1200_queue_requeue_all(struct cw1200_queue *queue)
412{
413 struct cw1200_queue_item *item, *tmp;
414 struct cw1200_queue_stats *stats = queue->stats;
415 spin_lock_bh(&queue->lock);
416
417 list_for_each_entry_safe_reverse(item, tmp, &queue->pending, head) {
418 --queue->num_pending;
419 ++queue->link_map_cache[item->txpriv.link_id];
420
421 spin_lock_bh(&stats->lock);
422 ++stats->num_queued;
423 ++stats->link_map_cache[item->txpriv.link_id];
424 spin_unlock_bh(&stats->lock);
425
426 ++item->generation;
427 item->packet_id = cw1200_queue_mk_packet_id(queue->generation,
428 queue->queue_id,
429 item->generation,
430 item - queue->pool);
431 list_move(&item->head, &queue->queue);
432 }
433 spin_unlock_bh(&queue->lock);
434
435 return 0;
436}
437
438int cw1200_queue_remove(struct cw1200_queue *queue, u32 packet_id)
439{
440 int ret = 0;
441 u8 queue_generation, queue_id, item_generation, item_id;
442 struct cw1200_queue_item *item;
443 struct cw1200_queue_stats *stats = queue->stats;
444 struct sk_buff *gc_skb = NULL;
445 struct cw1200_txpriv gc_txpriv;
446
447 cw1200_queue_parse_id(packet_id, &queue_generation, &queue_id,
448 &item_generation, &item_id);
449
450 item = &queue->pool[item_id];
451
452 spin_lock_bh(&queue->lock);
453 BUG_ON(queue_id != queue->queue_id);
454 if (queue_generation != queue->generation) {
455 ret = -ENOENT;
456 } else if (item_id >= (unsigned) queue->capacity) {
457 WARN_ON(1);
458 ret = -EINVAL;
459 } else if (item->generation != item_generation) {
460 WARN_ON(1);
461 ret = -ENOENT;
462 } else {
463 gc_txpriv = item->txpriv;
464 gc_skb = item->skb;
465 item->skb = NULL;
466 --queue->num_pending;
467 --queue->num_queued;
468 ++queue->num_sent;
469 ++item->generation;
470 /* Do not use list_move_tail here, but list_move:
471 * try to utilize cache row.
472 */
473 list_move(&item->head, &queue->free_pool);
474
475 if (queue->overfull &&
476 (queue->num_queued <= (queue->capacity >> 1))) {
477 queue->overfull = false;
478 __cw1200_queue_unlock(queue);
479 }
480 }
481 spin_unlock_bh(&queue->lock);
482
483 if (gc_skb)
484 stats->skb_dtor(stats->priv, gc_skb, &gc_txpriv);
485
486 return ret;
487}
488
489int cw1200_queue_get_skb(struct cw1200_queue *queue, u32 packet_id,
490 struct sk_buff **skb,
491 const struct cw1200_txpriv **txpriv)
492{
493 int ret = 0;
494 u8 queue_generation, queue_id, item_generation, item_id;
495 struct cw1200_queue_item *item;
496 cw1200_queue_parse_id(packet_id, &queue_generation, &queue_id,
497 &item_generation, &item_id);
498
499 item = &queue->pool[item_id];
500
501 spin_lock_bh(&queue->lock);
502 BUG_ON(queue_id != queue->queue_id);
503 if (queue_generation != queue->generation) {
504 ret = -ENOENT;
505 } else if (item_id >= (unsigned) queue->capacity) {
506 WARN_ON(1);
507 ret = -EINVAL;
508 } else if (item->generation != item_generation) {
509 WARN_ON(1);
510 ret = -ENOENT;
511 } else {
512 *skb = item->skb;
513 *txpriv = &item->txpriv;
514 }
515 spin_unlock_bh(&queue->lock);
516 return ret;
517}
518
519void cw1200_queue_lock(struct cw1200_queue *queue)
520{
521 spin_lock_bh(&queue->lock);
522 __cw1200_queue_lock(queue);
523 spin_unlock_bh(&queue->lock);
524}
525
526void cw1200_queue_unlock(struct cw1200_queue *queue)
527{
528 spin_lock_bh(&queue->lock);
529 __cw1200_queue_unlock(queue);
530 spin_unlock_bh(&queue->lock);
531}
532
533bool cw1200_queue_get_xmit_timestamp(struct cw1200_queue *queue,
534 unsigned long *timestamp,
535 u32 pending_frame_id)
536{
537 struct cw1200_queue_item *item;
538 bool ret;
539
540 spin_lock_bh(&queue->lock);
541 ret = !list_empty(&queue->pending);
542 if (ret) {
543 list_for_each_entry(item, &queue->pending, head) {
544 if (item->packet_id != pending_frame_id)
545 if (time_before(item->xmit_timestamp,
546 *timestamp))
547 *timestamp = item->xmit_timestamp;
548 }
549 }
550 spin_unlock_bh(&queue->lock);
551 return ret;
552}
553
554bool cw1200_queue_stats_is_empty(struct cw1200_queue_stats *stats,
555 u32 link_id_map)
556{
557 bool empty = true;
558
559 spin_lock_bh(&stats->lock);
560 if (link_id_map == (u32)-1) {
561 empty = stats->num_queued == 0;
562 } else {
563 int i;
564 for (i = 0; i < stats->map_capacity; ++i) {
565 if (link_id_map & BIT(i)) {
566 if (stats->link_map_cache[i]) {
567 empty = false;
568 break;
569 }
570 }
571 }
572 }
573 spin_unlock_bh(&stats->lock);
574
575 return empty;
576}