queue.c 15 KB

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