rt2x00queue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 sk_buff *rt2x00queue_alloc_rxskb(struct data_queue *queue)
  26. {
  27. struct sk_buff *skb;
  28. unsigned int frame_size;
  29. unsigned int reserved_size;
  30. /*
  31. * The frame size includes descriptor size, because the
  32. * hardware directly receive the frame into the skbuffer.
  33. */
  34. frame_size = queue->data_size + queue->desc_size;
  35. /*
  36. * For the allocation we should keep a few things in mind:
  37. * 1) 4byte alignment of 802.11 payload
  38. *
  39. * For (1) we need at most 4 bytes to guarentee the correct
  40. * alignment. We are going to optimize the fact that the chance
  41. * that the 802.11 header_size % 4 == 2 is much bigger then
  42. * anything else. However since we need to move the frame up
  43. * to 3 bytes to the front, which means we need to preallocate
  44. * 6 bytes.
  45. */
  46. reserved_size = 6;
  47. /*
  48. * Allocate skbuffer.
  49. */
  50. skb = dev_alloc_skb(frame_size + reserved_size);
  51. if (!skb)
  52. return NULL;
  53. skb_reserve(skb, reserved_size);
  54. skb_put(skb, frame_size);
  55. return skb;
  56. }
  57. EXPORT_SYMBOL_GPL(rt2x00queue_alloc_rxskb);
  58. void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
  59. struct txentry_desc *txdesc)
  60. {
  61. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  62. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  63. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  64. struct ieee80211_rate *rate =
  65. ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
  66. const struct rt2x00_rate *hwrate;
  67. unsigned int data_length;
  68. unsigned int duration;
  69. unsigned int residual;
  70. u16 frame_control;
  71. memset(txdesc, 0, sizeof(*txdesc));
  72. /*
  73. * Initialize information from queue
  74. */
  75. txdesc->queue = entry->queue->qid;
  76. txdesc->cw_min = entry->queue->cw_min;
  77. txdesc->cw_max = entry->queue->cw_max;
  78. txdesc->aifs = entry->queue->aifs;
  79. /* Data length should be extended with 4 bytes for CRC */
  80. data_length = entry->skb->len + 4;
  81. /*
  82. * Read required fields from ieee80211 header.
  83. */
  84. frame_control = le16_to_cpu(hdr->frame_control);
  85. /*
  86. * Check whether this frame is to be acked.
  87. */
  88. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
  89. __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
  90. /*
  91. * Check if this is a RTS/CTS frame
  92. */
  93. if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
  94. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  95. if (is_rts_frame(frame_control))
  96. __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
  97. else
  98. __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
  99. if (tx_info->control.rts_cts_rate_idx >= 0)
  100. rate =
  101. ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
  102. }
  103. /*
  104. * Determine retry information.
  105. */
  106. txdesc->retry_limit = tx_info->control.retry_limit;
  107. if (tx_info->flags & IEEE80211_TX_CTL_LONG_RETRY_LIMIT)
  108. __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
  109. /*
  110. * Check if more fragments are pending
  111. */
  112. if (ieee80211_get_morefrag(hdr)) {
  113. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  114. __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
  115. }
  116. /*
  117. * Beacons and probe responses require the tsf timestamp
  118. * to be inserted into the frame.
  119. */
  120. if (txdesc->queue == QID_BEACON || is_probe_resp(frame_control))
  121. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
  122. /*
  123. * Determine with what IFS priority this frame should be send.
  124. * Set ifs to IFS_SIFS when the this is not the first fragment,
  125. * or this fragment came after RTS/CTS.
  126. */
  127. if (test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
  128. txdesc->ifs = IFS_SIFS;
  129. } else if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
  130. __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
  131. txdesc->ifs = IFS_BACKOFF;
  132. } else {
  133. txdesc->ifs = IFS_SIFS;
  134. }
  135. /*
  136. * PLCP setup
  137. * Length calculation depends on OFDM/CCK rate.
  138. */
  139. hwrate = rt2x00_get_rate(rate->hw_value);
  140. txdesc->signal = hwrate->plcp;
  141. txdesc->service = 0x04;
  142. if (hwrate->flags & DEV_RATE_OFDM) {
  143. __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags);
  144. txdesc->length_high = (data_length >> 6) & 0x3f;
  145. txdesc->length_low = data_length & 0x3f;
  146. } else {
  147. /*
  148. * Convert length to microseconds.
  149. */
  150. residual = get_duration_res(data_length, hwrate->bitrate);
  151. duration = get_duration(data_length, hwrate->bitrate);
  152. if (residual != 0) {
  153. duration++;
  154. /*
  155. * Check if we need to set the Length Extension
  156. */
  157. if (hwrate->bitrate == 110 && residual <= 30)
  158. txdesc->service |= 0x80;
  159. }
  160. txdesc->length_high = (duration >> 8) & 0xff;
  161. txdesc->length_low = duration & 0xff;
  162. /*
  163. * When preamble is enabled we should set the
  164. * preamble bit for the signal.
  165. */
  166. if (rt2x00_get_rate_preamble(rate->hw_value))
  167. txdesc->signal |= 0x08;
  168. }
  169. }
  170. EXPORT_SYMBOL_GPL(rt2x00queue_create_tx_descriptor);
  171. void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
  172. struct txentry_desc *txdesc)
  173. {
  174. struct data_queue *queue = entry->queue;
  175. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  176. rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc);
  177. /*
  178. * All processing on the frame has been completed, this means
  179. * it is now ready to be dumped to userspace through debugfs.
  180. */
  181. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb);
  182. /*
  183. * Check if we need to kick the queue, there are however a few rules
  184. * 1) Don't kick beacon queue
  185. * 2) Don't kick unless this is the last in frame in a burst.
  186. * When the burst flag is set, this frame is always followed
  187. * by another frame which in some way are related to eachother.
  188. * This is true for fragments, RTS or CTS-to-self frames.
  189. * 3) Rule 2 can be broken when the available entries
  190. * in the queue are less then a certain threshold.
  191. */
  192. if (entry->queue->qid == QID_BEACON)
  193. return;
  194. if (rt2x00queue_threshold(queue) ||
  195. !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
  196. rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, queue->qid);
  197. }
  198. EXPORT_SYMBOL_GPL(rt2x00queue_write_tx_descriptor);
  199. int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb)
  200. {
  201. struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
  202. struct txentry_desc txdesc;
  203. if (unlikely(rt2x00queue_full(queue)))
  204. return -EINVAL;
  205. if (__test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) {
  206. ERROR(queue->rt2x00dev,
  207. "Arrived at non-free entry in the non-full queue %d.\n"
  208. "Please file bug report to %s.\n",
  209. queue->qid, DRV_PROJECT);
  210. return -EINVAL;
  211. }
  212. /*
  213. * Copy all TX descriptor information into txdesc,
  214. * after that we are free to use the skb->cb array
  215. * for our information.
  216. */
  217. entry->skb = skb;
  218. rt2x00queue_create_tx_descriptor(entry, &txdesc);
  219. if (unlikely(queue->rt2x00dev->ops->lib->write_tx_data(entry))) {
  220. __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  221. return -EIO;
  222. }
  223. __set_bit(ENTRY_DATA_PENDING, &entry->flags);
  224. rt2x00queue_index_inc(queue, Q_INDEX);
  225. rt2x00queue_write_tx_descriptor(entry, &txdesc);
  226. return 0;
  227. }
  228. struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
  229. const enum data_queue_qid queue)
  230. {
  231. int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  232. if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
  233. return &rt2x00dev->tx[queue];
  234. if (!rt2x00dev->bcn)
  235. return NULL;
  236. if (queue == QID_BEACON)
  237. return &rt2x00dev->bcn[0];
  238. else if (queue == QID_ATIM && atim)
  239. return &rt2x00dev->bcn[1];
  240. return NULL;
  241. }
  242. EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
  243. struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
  244. enum queue_index index)
  245. {
  246. struct queue_entry *entry;
  247. unsigned long irqflags;
  248. if (unlikely(index >= Q_INDEX_MAX)) {
  249. ERROR(queue->rt2x00dev,
  250. "Entry requested from invalid index type (%d)\n", index);
  251. return NULL;
  252. }
  253. spin_lock_irqsave(&queue->lock, irqflags);
  254. entry = &queue->entries[queue->index[index]];
  255. spin_unlock_irqrestore(&queue->lock, irqflags);
  256. return entry;
  257. }
  258. EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
  259. void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
  260. {
  261. unsigned long irqflags;
  262. if (unlikely(index >= Q_INDEX_MAX)) {
  263. ERROR(queue->rt2x00dev,
  264. "Index change on invalid index type (%d)\n", index);
  265. return;
  266. }
  267. spin_lock_irqsave(&queue->lock, irqflags);
  268. queue->index[index]++;
  269. if (queue->index[index] >= queue->limit)
  270. queue->index[index] = 0;
  271. if (index == Q_INDEX) {
  272. queue->length++;
  273. } else if (index == Q_INDEX_DONE) {
  274. queue->length--;
  275. queue->count ++;
  276. }
  277. spin_unlock_irqrestore(&queue->lock, irqflags);
  278. }
  279. EXPORT_SYMBOL_GPL(rt2x00queue_index_inc);
  280. static void rt2x00queue_reset(struct data_queue *queue)
  281. {
  282. unsigned long irqflags;
  283. spin_lock_irqsave(&queue->lock, irqflags);
  284. queue->count = 0;
  285. queue->length = 0;
  286. memset(queue->index, 0, sizeof(queue->index));
  287. spin_unlock_irqrestore(&queue->lock, irqflags);
  288. }
  289. void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
  290. {
  291. struct data_queue *queue = rt2x00dev->rx;
  292. unsigned int i;
  293. rt2x00queue_reset(queue);
  294. if (!rt2x00dev->ops->lib->init_rxentry)
  295. return;
  296. for (i = 0; i < queue->limit; i++)
  297. rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
  298. &queue->entries[i]);
  299. }
  300. void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
  301. {
  302. struct data_queue *queue;
  303. unsigned int i;
  304. txall_queue_for_each(rt2x00dev, queue) {
  305. rt2x00queue_reset(queue);
  306. if (!rt2x00dev->ops->lib->init_txentry)
  307. continue;
  308. for (i = 0; i < queue->limit; i++)
  309. rt2x00dev->ops->lib->init_txentry(rt2x00dev,
  310. &queue->entries[i]);
  311. }
  312. }
  313. static int rt2x00queue_alloc_entries(struct data_queue *queue,
  314. const struct data_queue_desc *qdesc)
  315. {
  316. struct queue_entry *entries;
  317. unsigned int entry_size;
  318. unsigned int i;
  319. rt2x00queue_reset(queue);
  320. queue->limit = qdesc->entry_num;
  321. queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
  322. queue->data_size = qdesc->data_size;
  323. queue->desc_size = qdesc->desc_size;
  324. /*
  325. * Allocate all queue entries.
  326. */
  327. entry_size = sizeof(*entries) + qdesc->priv_size;
  328. entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
  329. if (!entries)
  330. return -ENOMEM;
  331. #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
  332. ( ((char *)(__base)) + ((__limit) * (__esize)) + \
  333. ((__index) * (__psize)) )
  334. for (i = 0; i < queue->limit; i++) {
  335. entries[i].flags = 0;
  336. entries[i].queue = queue;
  337. entries[i].skb = NULL;
  338. entries[i].entry_idx = i;
  339. entries[i].priv_data =
  340. QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
  341. sizeof(*entries), qdesc->priv_size);
  342. }
  343. #undef QUEUE_ENTRY_PRIV_OFFSET
  344. queue->entries = entries;
  345. return 0;
  346. }
  347. int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
  348. {
  349. struct data_queue *queue;
  350. int status;
  351. status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
  352. if (status)
  353. goto exit;
  354. tx_queue_for_each(rt2x00dev, queue) {
  355. status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
  356. if (status)
  357. goto exit;
  358. }
  359. status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
  360. if (status)
  361. goto exit;
  362. if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
  363. return 0;
  364. status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
  365. rt2x00dev->ops->atim);
  366. if (status)
  367. goto exit;
  368. return 0;
  369. exit:
  370. ERROR(rt2x00dev, "Queue entries allocation failed.\n");
  371. rt2x00queue_uninitialize(rt2x00dev);
  372. return status;
  373. }
  374. void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
  375. {
  376. struct data_queue *queue;
  377. queue_for_each(rt2x00dev, queue) {
  378. kfree(queue->entries);
  379. queue->entries = NULL;
  380. }
  381. }
  382. static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
  383. struct data_queue *queue, enum data_queue_qid qid)
  384. {
  385. spin_lock_init(&queue->lock);
  386. queue->rt2x00dev = rt2x00dev;
  387. queue->qid = qid;
  388. queue->aifs = 2;
  389. queue->cw_min = 5;
  390. queue->cw_max = 10;
  391. }
  392. int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
  393. {
  394. struct data_queue *queue;
  395. enum data_queue_qid qid;
  396. unsigned int req_atim =
  397. !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  398. /*
  399. * We need the following queues:
  400. * RX: 1
  401. * TX: ops->tx_queues
  402. * Beacon: 1
  403. * Atim: 1 (if required)
  404. */
  405. rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
  406. queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
  407. if (!queue) {
  408. ERROR(rt2x00dev, "Queue allocation failed.\n");
  409. return -ENOMEM;
  410. }
  411. /*
  412. * Initialize pointers
  413. */
  414. rt2x00dev->rx = queue;
  415. rt2x00dev->tx = &queue[1];
  416. rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
  417. /*
  418. * Initialize queue parameters.
  419. * RX: qid = QID_RX
  420. * TX: qid = QID_AC_BE + index
  421. * TX: cw_min: 2^5 = 32.
  422. * TX: cw_max: 2^10 = 1024.
  423. * BCN: qid = QID_BEACON
  424. * ATIM: qid = QID_ATIM
  425. */
  426. rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
  427. qid = QID_AC_BE;
  428. tx_queue_for_each(rt2x00dev, queue)
  429. rt2x00queue_init(rt2x00dev, queue, qid++);
  430. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
  431. if (req_atim)
  432. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
  433. return 0;
  434. }
  435. void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
  436. {
  437. kfree(rt2x00dev->rx);
  438. rt2x00dev->rx = NULL;
  439. rt2x00dev->tx = NULL;
  440. rt2x00dev->bcn = NULL;
  441. }