queue.c 14 KB

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