rt2x00queue.c 14 KB

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