rt2x00queue.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00lib
  19. Abstract: rt2x00 queue specific routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
  26. const unsigned int queue)
  27. {
  28. int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  29. if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
  30. return &rt2x00dev->tx[queue];
  31. if (!rt2x00dev->bcn)
  32. return NULL;
  33. if (queue == RT2X00_BCN_QUEUE_BEACON)
  34. return &rt2x00dev->bcn[0];
  35. else if (queue == RT2X00_BCN_QUEUE_ATIM && atim)
  36. return &rt2x00dev->bcn[1];
  37. return NULL;
  38. }
  39. EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
  40. struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
  41. enum queue_index index)
  42. {
  43. struct queue_entry *entry;
  44. if (unlikely(index >= Q_INDEX_MAX)) {
  45. ERROR(queue->rt2x00dev,
  46. "Entry requested from invalid index type (%d)\n", index);
  47. return NULL;
  48. }
  49. spin_lock(&queue->lock);
  50. entry = &queue->entries[queue->index[index]];
  51. spin_unlock(&queue->lock);
  52. return entry;
  53. }
  54. EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
  55. void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
  56. {
  57. if (unlikely(index >= Q_INDEX_MAX)) {
  58. ERROR(queue->rt2x00dev,
  59. "Index change on invalid index type (%d)\n", index);
  60. return;
  61. }
  62. spin_lock(&queue->lock);
  63. queue->index[index]++;
  64. if (queue->index[index] >= queue->limit)
  65. queue->index[index] = 0;
  66. if (index == Q_INDEX) {
  67. queue->length++;
  68. } else if (index == Q_INDEX_DONE) {
  69. queue->length--;
  70. queue->count ++;
  71. }
  72. spin_unlock(&queue->lock);
  73. }
  74. EXPORT_SYMBOL_GPL(rt2x00queue_index_inc);
  75. static void rt2x00queue_reset(struct data_queue *queue)
  76. {
  77. spin_lock(&queue->lock);
  78. queue->count = 0;
  79. queue->length = 0;
  80. memset(queue->index, 0, sizeof(queue->index));
  81. spin_unlock(&queue->lock);
  82. }
  83. void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
  84. {
  85. struct data_queue *queue = rt2x00dev->rx;
  86. unsigned int i;
  87. rt2x00queue_reset(queue);
  88. if (!rt2x00dev->ops->lib->init_rxentry)
  89. return;
  90. for (i = 0; i < queue->limit; i++)
  91. rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
  92. &queue->entries[i]);
  93. }
  94. void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
  95. {
  96. struct data_queue *queue;
  97. unsigned int i;
  98. txall_queue_for_each(rt2x00dev, queue) {
  99. rt2x00queue_reset(queue);
  100. if (!rt2x00dev->ops->lib->init_txentry)
  101. continue;
  102. for (i = 0; i < queue->limit; i++)
  103. rt2x00dev->ops->lib->init_txentry(rt2x00dev,
  104. &queue->entries[i]);
  105. }
  106. }
  107. static int rt2x00queue_alloc_entries(struct data_queue *queue,
  108. const struct data_queue_desc *qdesc)
  109. {
  110. struct queue_entry *entries;
  111. unsigned int entry_size;
  112. unsigned int i;
  113. rt2x00queue_reset(queue);
  114. queue->limit = qdesc->entry_num;
  115. queue->data_size = qdesc->data_size;
  116. queue->desc_size = qdesc->desc_size;
  117. /*
  118. * Allocate all queue entries.
  119. */
  120. entry_size = sizeof(*entries) + qdesc->priv_size;
  121. entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
  122. if (!entries)
  123. return -ENOMEM;
  124. #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
  125. ( ((char *)(__base)) + ((__limit) * (__esize)) + \
  126. ((__index) * (__psize)) )
  127. for (i = 0; i < queue->limit; i++) {
  128. entries[i].flags = 0;
  129. entries[i].queue = queue;
  130. entries[i].skb = NULL;
  131. entries[i].entry_idx = i;
  132. entries[i].priv_data =
  133. QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
  134. sizeof(*entries), qdesc->priv_size);
  135. }
  136. #undef QUEUE_ENTRY_PRIV_OFFSET
  137. queue->entries = entries;
  138. return 0;
  139. }
  140. int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
  141. {
  142. struct data_queue *queue;
  143. int status;
  144. status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
  145. if (status)
  146. goto exit;
  147. tx_queue_for_each(rt2x00dev, queue) {
  148. status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
  149. if (status)
  150. goto exit;
  151. }
  152. status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
  153. if (status)
  154. goto exit;
  155. if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
  156. return 0;
  157. status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
  158. rt2x00dev->ops->atim);
  159. if (status)
  160. goto exit;
  161. return 0;
  162. exit:
  163. ERROR(rt2x00dev, "Queue entries allocation failed.\n");
  164. rt2x00queue_uninitialize(rt2x00dev);
  165. return status;
  166. }
  167. void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
  168. {
  169. struct data_queue *queue;
  170. queue_for_each(rt2x00dev, queue) {
  171. kfree(queue->entries);
  172. queue->entries = NULL;
  173. }
  174. }
  175. int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
  176. {
  177. struct data_queue *queue;
  178. enum data_queue_qid qid;
  179. unsigned int req_atim =
  180. !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  181. /*
  182. * We need the following queues:
  183. * RX: 1
  184. * TX: hw->queues
  185. * Beacon: 1
  186. * Atim: 1 (if required)
  187. */
  188. rt2x00dev->data_queues = 2 + rt2x00dev->hw->queues + req_atim;
  189. queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
  190. if (!queue) {
  191. ERROR(rt2x00dev, "Queue allocation failed.\n");
  192. return -ENOMEM;
  193. }
  194. /*
  195. * Initialize pointers
  196. */
  197. rt2x00dev->rx = queue;
  198. rt2x00dev->tx = &queue[1];
  199. rt2x00dev->bcn = &queue[1 + rt2x00dev->hw->queues];
  200. /*
  201. * Initialize queue parameters.
  202. * RX: qid = QID_RX
  203. * TX: qid = QID_AC_BE + index
  204. * TX: cw_min: 2^5 = 32.
  205. * TX: cw_max: 2^10 = 1024.
  206. * BCN & Atim: qid = QID_MGMT
  207. */
  208. qid = QID_AC_BE;
  209. queue_for_each(rt2x00dev, queue) {
  210. spin_lock_init(&queue->lock);
  211. queue->rt2x00dev = rt2x00dev;
  212. queue->qid = qid++;
  213. queue->aifs = 2;
  214. queue->cw_min = 5;
  215. queue->cw_max = 10;
  216. }
  217. /*
  218. * Fix non-TX data qid's
  219. */
  220. rt2x00dev->rx->qid = QID_RX;
  221. rt2x00dev->bcn[0].qid = QID_MGMT;
  222. if (req_atim)
  223. rt2x00dev->bcn[1].qid = QID_MGMT;
  224. return 0;
  225. }
  226. void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
  227. {
  228. kfree(rt2x00dev->rx);
  229. rt2x00dev->rx = NULL;
  230. rt2x00dev->tx = NULL;
  231. rt2x00dev->bcn = NULL;
  232. }