rt2x00queue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
  26. struct txentry_desc *txdesc,
  27. struct ieee80211_tx_control *control)
  28. {
  29. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  30. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  31. struct ieee80211_rate *rate =
  32. ieee80211_get_tx_rate(rt2x00dev->hw, control);
  33. const struct rt2x00_rate *hwrate;
  34. unsigned int data_length;
  35. unsigned int duration;
  36. unsigned int residual;
  37. u16 frame_control;
  38. memset(txdesc, 0, sizeof(*txdesc));
  39. /*
  40. * Initialize information from queue
  41. */
  42. txdesc->queue = entry->queue->qid;
  43. txdesc->cw_min = entry->queue->cw_min;
  44. txdesc->cw_max = entry->queue->cw_max;
  45. txdesc->aifs = entry->queue->aifs;
  46. /* Data length should be extended with 4 bytes for CRC */
  47. data_length = entry->skb->len + 4;
  48. /*
  49. * Read required fields from ieee80211 header.
  50. */
  51. frame_control = le16_to_cpu(hdr->frame_control);
  52. /*
  53. * Check whether this frame is to be acked.
  54. */
  55. if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
  56. __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
  57. /*
  58. * Check if this is a RTS/CTS frame
  59. */
  60. if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
  61. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  62. if (is_rts_frame(frame_control)) {
  63. __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
  64. __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
  65. } else {
  66. __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
  67. __clear_bit(ENTRY_TXD_ACK, &txdesc->flags);
  68. }
  69. if (control->rts_cts_rate_idx >= 0)
  70. rate =
  71. ieee80211_get_rts_cts_rate(rt2x00dev->hw, control);
  72. }
  73. /*
  74. * Determine retry information.
  75. */
  76. txdesc->retry_limit = control->retry_limit;
  77. if (control->flags & IEEE80211_TXCTL_LONG_RETRY_LIMIT)
  78. __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
  79. /*
  80. * Check if more fragments are pending
  81. */
  82. if (ieee80211_get_morefrag(hdr)) {
  83. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  84. __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
  85. }
  86. /*
  87. * Beacons and probe responses require the tsf timestamp
  88. * to be inserted into the frame.
  89. */
  90. if (txdesc->queue == QID_BEACON || is_probe_resp(frame_control))
  91. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
  92. /*
  93. * Determine with what IFS priority this frame should be send.
  94. * Set ifs to IFS_SIFS when the this is not the first fragment,
  95. * or this fragment came after RTS/CTS.
  96. */
  97. if (test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
  98. txdesc->ifs = IFS_SIFS;
  99. } else if (control->flags & IEEE80211_TXCTL_FIRST_FRAGMENT) {
  100. __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
  101. txdesc->ifs = IFS_BACKOFF;
  102. } else {
  103. txdesc->ifs = IFS_SIFS;
  104. }
  105. /*
  106. * PLCP setup
  107. * Length calculation depends on OFDM/CCK rate.
  108. */
  109. hwrate = rt2x00_get_rate(rate->hw_value);
  110. txdesc->signal = hwrate->plcp;
  111. txdesc->service = 0x04;
  112. if (hwrate->flags & DEV_RATE_OFDM) {
  113. __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags);
  114. txdesc->length_high = (data_length >> 6) & 0x3f;
  115. txdesc->length_low = data_length & 0x3f;
  116. } else {
  117. /*
  118. * Convert length to microseconds.
  119. */
  120. residual = get_duration_res(data_length, hwrate->bitrate);
  121. duration = get_duration(data_length, hwrate->bitrate);
  122. if (residual != 0) {
  123. duration++;
  124. /*
  125. * Check if we need to set the Length Extension
  126. */
  127. if (hwrate->bitrate == 110 && residual <= 30)
  128. txdesc->service |= 0x80;
  129. }
  130. txdesc->length_high = (duration >> 8) & 0xff;
  131. txdesc->length_low = duration & 0xff;
  132. /*
  133. * When preamble is enabled we should set the
  134. * preamble bit for the signal.
  135. */
  136. if (rt2x00_get_rate_preamble(rate->hw_value))
  137. txdesc->signal |= 0x08;
  138. }
  139. }
  140. EXPORT_SYMBOL_GPL(rt2x00queue_create_tx_descriptor);
  141. void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
  142. struct txentry_desc *txdesc)
  143. {
  144. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  145. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  146. rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc);
  147. /*
  148. * All processing on the frame has been completed, this means
  149. * it is now ready to be dumped to userspace through debugfs.
  150. */
  151. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb);
  152. /*
  153. * We are done writing the frame to the queue entry,
  154. * if this entry is a RTS of CTS-to-self frame we are done,
  155. * otherwise we need to kick the queue.
  156. */
  157. if (rt2x00dev->ops->lib->kick_tx_queue &&
  158. !(skbdesc->flags & FRAME_DESC_DRIVER_GENERATED))
  159. rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev,
  160. entry->queue->qid);
  161. }
  162. EXPORT_SYMBOL_GPL(rt2x00queue_write_tx_descriptor);
  163. struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
  164. const enum data_queue_qid queue)
  165. {
  166. int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  167. if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
  168. return &rt2x00dev->tx[queue];
  169. if (!rt2x00dev->bcn)
  170. return NULL;
  171. if (queue == QID_BEACON)
  172. return &rt2x00dev->bcn[0];
  173. else if (queue == QID_ATIM && atim)
  174. return &rt2x00dev->bcn[1];
  175. return NULL;
  176. }
  177. EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
  178. struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
  179. enum queue_index index)
  180. {
  181. struct queue_entry *entry;
  182. unsigned long irqflags;
  183. if (unlikely(index >= Q_INDEX_MAX)) {
  184. ERROR(queue->rt2x00dev,
  185. "Entry requested from invalid index type (%d)\n", index);
  186. return NULL;
  187. }
  188. spin_lock_irqsave(&queue->lock, irqflags);
  189. entry = &queue->entries[queue->index[index]];
  190. spin_unlock_irqrestore(&queue->lock, irqflags);
  191. return entry;
  192. }
  193. EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
  194. void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
  195. {
  196. unsigned long irqflags;
  197. if (unlikely(index >= Q_INDEX_MAX)) {
  198. ERROR(queue->rt2x00dev,
  199. "Index change on invalid index type (%d)\n", index);
  200. return;
  201. }
  202. spin_lock_irqsave(&queue->lock, irqflags);
  203. queue->index[index]++;
  204. if (queue->index[index] >= queue->limit)
  205. queue->index[index] = 0;
  206. if (index == Q_INDEX) {
  207. queue->length++;
  208. } else if (index == Q_INDEX_DONE) {
  209. queue->length--;
  210. queue->count ++;
  211. }
  212. spin_unlock_irqrestore(&queue->lock, irqflags);
  213. }
  214. EXPORT_SYMBOL_GPL(rt2x00queue_index_inc);
  215. static void rt2x00queue_reset(struct data_queue *queue)
  216. {
  217. unsigned long irqflags;
  218. spin_lock_irqsave(&queue->lock, irqflags);
  219. queue->count = 0;
  220. queue->length = 0;
  221. memset(queue->index, 0, sizeof(queue->index));
  222. spin_unlock_irqrestore(&queue->lock, irqflags);
  223. }
  224. void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
  225. {
  226. struct data_queue *queue = rt2x00dev->rx;
  227. unsigned int i;
  228. rt2x00queue_reset(queue);
  229. if (!rt2x00dev->ops->lib->init_rxentry)
  230. return;
  231. for (i = 0; i < queue->limit; i++)
  232. rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
  233. &queue->entries[i]);
  234. }
  235. void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
  236. {
  237. struct data_queue *queue;
  238. unsigned int i;
  239. txall_queue_for_each(rt2x00dev, queue) {
  240. rt2x00queue_reset(queue);
  241. if (!rt2x00dev->ops->lib->init_txentry)
  242. continue;
  243. for (i = 0; i < queue->limit; i++)
  244. rt2x00dev->ops->lib->init_txentry(rt2x00dev,
  245. &queue->entries[i]);
  246. }
  247. }
  248. static int rt2x00queue_alloc_entries(struct data_queue *queue,
  249. const struct data_queue_desc *qdesc)
  250. {
  251. struct queue_entry *entries;
  252. unsigned int entry_size;
  253. unsigned int i;
  254. rt2x00queue_reset(queue);
  255. queue->limit = qdesc->entry_num;
  256. queue->data_size = qdesc->data_size;
  257. queue->desc_size = qdesc->desc_size;
  258. /*
  259. * Allocate all queue entries.
  260. */
  261. entry_size = sizeof(*entries) + qdesc->priv_size;
  262. entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
  263. if (!entries)
  264. return -ENOMEM;
  265. #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
  266. ( ((char *)(__base)) + ((__limit) * (__esize)) + \
  267. ((__index) * (__psize)) )
  268. for (i = 0; i < queue->limit; i++) {
  269. entries[i].flags = 0;
  270. entries[i].queue = queue;
  271. entries[i].skb = NULL;
  272. entries[i].entry_idx = i;
  273. entries[i].priv_data =
  274. QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
  275. sizeof(*entries), qdesc->priv_size);
  276. }
  277. #undef QUEUE_ENTRY_PRIV_OFFSET
  278. queue->entries = entries;
  279. return 0;
  280. }
  281. int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
  282. {
  283. struct data_queue *queue;
  284. int status;
  285. status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
  286. if (status)
  287. goto exit;
  288. tx_queue_for_each(rt2x00dev, queue) {
  289. status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
  290. if (status)
  291. goto exit;
  292. }
  293. status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
  294. if (status)
  295. goto exit;
  296. if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
  297. return 0;
  298. status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
  299. rt2x00dev->ops->atim);
  300. if (status)
  301. goto exit;
  302. return 0;
  303. exit:
  304. ERROR(rt2x00dev, "Queue entries allocation failed.\n");
  305. rt2x00queue_uninitialize(rt2x00dev);
  306. return status;
  307. }
  308. void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
  309. {
  310. struct data_queue *queue;
  311. queue_for_each(rt2x00dev, queue) {
  312. kfree(queue->entries);
  313. queue->entries = NULL;
  314. }
  315. }
  316. static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
  317. struct data_queue *queue, enum data_queue_qid qid)
  318. {
  319. spin_lock_init(&queue->lock);
  320. queue->rt2x00dev = rt2x00dev;
  321. queue->qid = qid;
  322. queue->aifs = 2;
  323. queue->cw_min = 5;
  324. queue->cw_max = 10;
  325. }
  326. int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
  327. {
  328. struct data_queue *queue;
  329. enum data_queue_qid qid;
  330. unsigned int req_atim =
  331. !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  332. /*
  333. * We need the following queues:
  334. * RX: 1
  335. * TX: ops->tx_queues
  336. * Beacon: 1
  337. * Atim: 1 (if required)
  338. */
  339. rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
  340. queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
  341. if (!queue) {
  342. ERROR(rt2x00dev, "Queue allocation failed.\n");
  343. return -ENOMEM;
  344. }
  345. /*
  346. * Initialize pointers
  347. */
  348. rt2x00dev->rx = queue;
  349. rt2x00dev->tx = &queue[1];
  350. rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
  351. /*
  352. * Initialize queue parameters.
  353. * RX: qid = QID_RX
  354. * TX: qid = QID_AC_BE + index
  355. * TX: cw_min: 2^5 = 32.
  356. * TX: cw_max: 2^10 = 1024.
  357. * BCN & Atim: qid = QID_MGMT
  358. */
  359. rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
  360. qid = QID_AC_BE;
  361. tx_queue_for_each(rt2x00dev, queue)
  362. rt2x00queue_init(rt2x00dev, queue, qid++);
  363. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_MGMT);
  364. if (req_atim)
  365. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_MGMT);
  366. return 0;
  367. }
  368. void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
  369. {
  370. kfree(rt2x00dev->rx);
  371. rt2x00dev->rx = NULL;
  372. rt2x00dev->tx = NULL;
  373. rt2x00dev->bcn = NULL;
  374. }