queue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. setup_timer(&queue->gc, cw1200_queue_gc, (unsigned long)queue);
  163. queue->pool = kzalloc(sizeof(struct cw1200_queue_item) * capacity,
  164. GFP_KERNEL);
  165. if (!queue->pool)
  166. return -ENOMEM;
  167. queue->link_map_cache = kzalloc(sizeof(int) * stats->map_capacity,
  168. GFP_KERNEL);
  169. if (!queue->link_map_cache) {
  170. kfree(queue->pool);
  171. queue->pool = NULL;
  172. return -ENOMEM;
  173. }
  174. for (i = 0; i < capacity; ++i)
  175. list_add_tail(&queue->pool[i].head, &queue->free_pool);
  176. return 0;
  177. }
  178. int cw1200_queue_clear(struct cw1200_queue *queue)
  179. {
  180. int i;
  181. LIST_HEAD(gc_list);
  182. struct cw1200_queue_stats *stats = queue->stats;
  183. struct cw1200_queue_item *item, *tmp;
  184. spin_lock_bh(&queue->lock);
  185. queue->generation++;
  186. list_splice_tail_init(&queue->queue, &queue->pending);
  187. list_for_each_entry_safe(item, tmp, &queue->pending, head) {
  188. WARN_ON(!item->skb);
  189. cw1200_queue_register_post_gc(&gc_list, item);
  190. item->skb = NULL;
  191. list_move_tail(&item->head, &queue->free_pool);
  192. }
  193. queue->num_queued = 0;
  194. queue->num_pending = 0;
  195. spin_lock_bh(&stats->lock);
  196. for (i = 0; i < stats->map_capacity; ++i) {
  197. stats->num_queued -= queue->link_map_cache[i];
  198. stats->link_map_cache[i] -= queue->link_map_cache[i];
  199. queue->link_map_cache[i] = 0;
  200. }
  201. spin_unlock_bh(&stats->lock);
  202. if (queue->overfull) {
  203. queue->overfull = false;
  204. __cw1200_queue_unlock(queue);
  205. }
  206. spin_unlock_bh(&queue->lock);
  207. wake_up(&stats->wait_link_id_empty);
  208. cw1200_queue_post_gc(stats, &gc_list);
  209. return 0;
  210. }
  211. void cw1200_queue_stats_deinit(struct cw1200_queue_stats *stats)
  212. {
  213. kfree(stats->link_map_cache);
  214. stats->link_map_cache = NULL;
  215. }
  216. void cw1200_queue_deinit(struct cw1200_queue *queue)
  217. {
  218. cw1200_queue_clear(queue);
  219. del_timer_sync(&queue->gc);
  220. INIT_LIST_HEAD(&queue->free_pool);
  221. kfree(queue->pool);
  222. kfree(queue->link_map_cache);
  223. queue->pool = NULL;
  224. queue->link_map_cache = NULL;
  225. queue->capacity = 0;
  226. }
  227. size_t cw1200_queue_get_num_queued(struct cw1200_queue *queue,
  228. u32 link_id_map)
  229. {
  230. size_t ret;
  231. int i, bit;
  232. size_t map_capacity = queue->stats->map_capacity;
  233. if (!link_id_map)
  234. return 0;
  235. spin_lock_bh(&queue->lock);
  236. if (link_id_map == (u32)-1) {
  237. ret = queue->num_queued - queue->num_pending;
  238. } else {
  239. ret = 0;
  240. for (i = 0, bit = 1; i < map_capacity; ++i, bit <<= 1) {
  241. if (link_id_map & bit)
  242. ret += queue->link_map_cache[i];
  243. }
  244. }
  245. spin_unlock_bh(&queue->lock);
  246. return ret;
  247. }
  248. int cw1200_queue_put(struct cw1200_queue *queue,
  249. struct sk_buff *skb,
  250. struct cw1200_txpriv *txpriv)
  251. {
  252. int ret = 0;
  253. LIST_HEAD(gc_list);
  254. struct cw1200_queue_stats *stats = queue->stats;
  255. if (txpriv->link_id >= queue->stats->map_capacity)
  256. return -EINVAL;
  257. spin_lock_bh(&queue->lock);
  258. if (!WARN_ON(list_empty(&queue->free_pool))) {
  259. struct cw1200_queue_item *item = list_first_entry(
  260. &queue->free_pool, struct cw1200_queue_item, head);
  261. BUG_ON(item->skb);
  262. list_move_tail(&item->head, &queue->queue);
  263. item->skb = skb;
  264. item->txpriv = *txpriv;
  265. item->generation = 0;
  266. item->packet_id = cw1200_queue_mk_packet_id(queue->generation,
  267. queue->queue_id,
  268. item->generation,
  269. item - queue->pool);
  270. item->queue_timestamp = jiffies;
  271. ++queue->num_queued;
  272. ++queue->link_map_cache[txpriv->link_id];
  273. spin_lock_bh(&stats->lock);
  274. ++stats->num_queued;
  275. ++stats->link_map_cache[txpriv->link_id];
  276. spin_unlock_bh(&stats->lock);
  277. /* TX may happen in parallel sometimes.
  278. * Leave extra queue slots so we don't overflow.
  279. */
  280. if (queue->overfull == false &&
  281. queue->num_queued >=
  282. (queue->capacity - (num_present_cpus() - 1))) {
  283. queue->overfull = true;
  284. __cw1200_queue_lock(queue);
  285. mod_timer(&queue->gc, jiffies);
  286. }
  287. } else {
  288. ret = -ENOENT;
  289. }
  290. spin_unlock_bh(&queue->lock);
  291. return ret;
  292. }
  293. int cw1200_queue_get(struct cw1200_queue *queue,
  294. u32 link_id_map,
  295. struct wsm_tx **tx,
  296. struct ieee80211_tx_info **tx_info,
  297. const struct cw1200_txpriv **txpriv)
  298. {
  299. int ret = -ENOENT;
  300. struct cw1200_queue_item *item;
  301. struct cw1200_queue_stats *stats = queue->stats;
  302. bool wakeup_stats = false;
  303. spin_lock_bh(&queue->lock);
  304. list_for_each_entry(item, &queue->queue, head) {
  305. if (link_id_map & BIT(item->txpriv.link_id)) {
  306. ret = 0;
  307. break;
  308. }
  309. }
  310. if (!WARN_ON(ret)) {
  311. *tx = (struct wsm_tx *)item->skb->data;
  312. *tx_info = IEEE80211_SKB_CB(item->skb);
  313. *txpriv = &item->txpriv;
  314. (*tx)->packet_id = item->packet_id;
  315. list_move_tail(&item->head, &queue->pending);
  316. ++queue->num_pending;
  317. --queue->link_map_cache[item->txpriv.link_id];
  318. item->xmit_timestamp = jiffies;
  319. spin_lock_bh(&stats->lock);
  320. --stats->num_queued;
  321. if (!--stats->link_map_cache[item->txpriv.link_id])
  322. wakeup_stats = true;
  323. spin_unlock_bh(&stats->lock);
  324. }
  325. spin_unlock_bh(&queue->lock);
  326. if (wakeup_stats)
  327. wake_up(&stats->wait_link_id_empty);
  328. return ret;
  329. }
  330. int cw1200_queue_requeue(struct cw1200_queue *queue, u32 packet_id)
  331. {
  332. int ret = 0;
  333. u8 queue_generation, queue_id, item_generation, item_id;
  334. struct cw1200_queue_item *item;
  335. struct cw1200_queue_stats *stats = queue->stats;
  336. cw1200_queue_parse_id(packet_id, &queue_generation, &queue_id,
  337. &item_generation, &item_id);
  338. item = &queue->pool[item_id];
  339. spin_lock_bh(&queue->lock);
  340. BUG_ON(queue_id != queue->queue_id);
  341. if (queue_generation != queue->generation) {
  342. ret = -ENOENT;
  343. } else if (item_id >= (unsigned) queue->capacity) {
  344. WARN_ON(1);
  345. ret = -EINVAL;
  346. } else if (item->generation != item_generation) {
  347. WARN_ON(1);
  348. ret = -ENOENT;
  349. } else {
  350. --queue->num_pending;
  351. ++queue->link_map_cache[item->txpriv.link_id];
  352. spin_lock_bh(&stats->lock);
  353. ++stats->num_queued;
  354. ++stats->link_map_cache[item->txpriv.link_id];
  355. spin_unlock_bh(&stats->lock);
  356. item->generation = ++item_generation;
  357. item->packet_id = cw1200_queue_mk_packet_id(queue_generation,
  358. queue_id,
  359. item_generation,
  360. item_id);
  361. list_move(&item->head, &queue->queue);
  362. }
  363. spin_unlock_bh(&queue->lock);
  364. return ret;
  365. }
  366. int cw1200_queue_requeue_all(struct cw1200_queue *queue)
  367. {
  368. struct cw1200_queue_item *item, *tmp;
  369. struct cw1200_queue_stats *stats = queue->stats;
  370. spin_lock_bh(&queue->lock);
  371. list_for_each_entry_safe_reverse(item, tmp, &queue->pending, head) {
  372. --queue->num_pending;
  373. ++queue->link_map_cache[item->txpriv.link_id];
  374. spin_lock_bh(&stats->lock);
  375. ++stats->num_queued;
  376. ++stats->link_map_cache[item->txpriv.link_id];
  377. spin_unlock_bh(&stats->lock);
  378. ++item->generation;
  379. item->packet_id = cw1200_queue_mk_packet_id(queue->generation,
  380. queue->queue_id,
  381. item->generation,
  382. item - queue->pool);
  383. list_move(&item->head, &queue->queue);
  384. }
  385. spin_unlock_bh(&queue->lock);
  386. return 0;
  387. }
  388. int cw1200_queue_remove(struct cw1200_queue *queue, u32 packet_id)
  389. {
  390. int ret = 0;
  391. u8 queue_generation, queue_id, item_generation, item_id;
  392. struct cw1200_queue_item *item;
  393. struct cw1200_queue_stats *stats = queue->stats;
  394. struct sk_buff *gc_skb = NULL;
  395. struct cw1200_txpriv gc_txpriv;
  396. cw1200_queue_parse_id(packet_id, &queue_generation, &queue_id,
  397. &item_generation, &item_id);
  398. item = &queue->pool[item_id];
  399. spin_lock_bh(&queue->lock);
  400. BUG_ON(queue_id != queue->queue_id);
  401. if (queue_generation != queue->generation) {
  402. ret = -ENOENT;
  403. } else if (item_id >= (unsigned) queue->capacity) {
  404. WARN_ON(1);
  405. ret = -EINVAL;
  406. } else if (item->generation != item_generation) {
  407. WARN_ON(1);
  408. ret = -ENOENT;
  409. } else {
  410. gc_txpriv = item->txpriv;
  411. gc_skb = item->skb;
  412. item->skb = NULL;
  413. --queue->num_pending;
  414. --queue->num_queued;
  415. ++queue->num_sent;
  416. ++item->generation;
  417. /* Do not use list_move_tail here, but list_move:
  418. * try to utilize cache row.
  419. */
  420. list_move(&item->head, &queue->free_pool);
  421. if (queue->overfull &&
  422. (queue->num_queued <= (queue->capacity >> 1))) {
  423. queue->overfull = false;
  424. __cw1200_queue_unlock(queue);
  425. }
  426. }
  427. spin_unlock_bh(&queue->lock);
  428. if (gc_skb)
  429. stats->skb_dtor(stats->priv, gc_skb, &gc_txpriv);
  430. return ret;
  431. }
  432. int cw1200_queue_get_skb(struct cw1200_queue *queue, u32 packet_id,
  433. struct sk_buff **skb,
  434. const struct cw1200_txpriv **txpriv)
  435. {
  436. int ret = 0;
  437. u8 queue_generation, queue_id, item_generation, item_id;
  438. struct cw1200_queue_item *item;
  439. cw1200_queue_parse_id(packet_id, &queue_generation, &queue_id,
  440. &item_generation, &item_id);
  441. item = &queue->pool[item_id];
  442. spin_lock_bh(&queue->lock);
  443. BUG_ON(queue_id != queue->queue_id);
  444. if (queue_generation != queue->generation) {
  445. ret = -ENOENT;
  446. } else if (item_id >= (unsigned) queue->capacity) {
  447. WARN_ON(1);
  448. ret = -EINVAL;
  449. } else if (item->generation != item_generation) {
  450. WARN_ON(1);
  451. ret = -ENOENT;
  452. } else {
  453. *skb = item->skb;
  454. *txpriv = &item->txpriv;
  455. }
  456. spin_unlock_bh(&queue->lock);
  457. return ret;
  458. }
  459. void cw1200_queue_lock(struct cw1200_queue *queue)
  460. {
  461. spin_lock_bh(&queue->lock);
  462. __cw1200_queue_lock(queue);
  463. spin_unlock_bh(&queue->lock);
  464. }
  465. void cw1200_queue_unlock(struct cw1200_queue *queue)
  466. {
  467. spin_lock_bh(&queue->lock);
  468. __cw1200_queue_unlock(queue);
  469. spin_unlock_bh(&queue->lock);
  470. }
  471. bool cw1200_queue_get_xmit_timestamp(struct cw1200_queue *queue,
  472. unsigned long *timestamp,
  473. u32 pending_frame_id)
  474. {
  475. struct cw1200_queue_item *item;
  476. bool ret;
  477. spin_lock_bh(&queue->lock);
  478. ret = !list_empty(&queue->pending);
  479. if (ret) {
  480. list_for_each_entry(item, &queue->pending, head) {
  481. if (item->packet_id != pending_frame_id)
  482. if (time_before(item->xmit_timestamp,
  483. *timestamp))
  484. *timestamp = item->xmit_timestamp;
  485. }
  486. }
  487. spin_unlock_bh(&queue->lock);
  488. return ret;
  489. }
  490. bool cw1200_queue_stats_is_empty(struct cw1200_queue_stats *stats,
  491. u32 link_id_map)
  492. {
  493. bool empty = true;
  494. spin_lock_bh(&stats->lock);
  495. if (link_id_map == (u32)-1) {
  496. empty = stats->num_queued == 0;
  497. } else {
  498. int i;
  499. for (i = 0; i < stats->map_capacity; ++i) {
  500. if (link_id_map & BIT(i)) {
  501. if (stats->link_map_cache[i]) {
  502. empty = false;
  503. break;
  504. }
  505. }
  506. }
  507. }
  508. spin_unlock_bh(&stats->lock);
  509. return empty;
  510. }