rt2x00queue.c 11 KB

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