rt2x00queue.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. Copyright (C) 2004 - 2009 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 <linux/dma-mapping.h>
  24. #include "rt2x00.h"
  25. #include "rt2x00lib.h"
  26. struct sk_buff *rt2x00queue_alloc_rxskb(struct rt2x00_dev *rt2x00dev,
  27. struct queue_entry *entry)
  28. {
  29. struct sk_buff *skb;
  30. struct skb_frame_desc *skbdesc;
  31. unsigned int frame_size;
  32. unsigned int head_size = 0;
  33. unsigned int tail_size = 0;
  34. /*
  35. * The frame size includes descriptor size, because the
  36. * hardware directly receive the frame into the skbuffer.
  37. */
  38. frame_size = entry->queue->data_size + entry->queue->desc_size;
  39. /*
  40. * The payload should be aligned to a 4-byte boundary,
  41. * this means we need at least 3 bytes for moving the frame
  42. * into the correct offset.
  43. */
  44. head_size = 4;
  45. /*
  46. * For IV/EIV/ICV assembly we must make sure there is
  47. * at least 8 bytes bytes available in headroom for IV/EIV
  48. * and 8 bytes for ICV data as tailroon.
  49. */
  50. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
  51. head_size += 8;
  52. tail_size += 8;
  53. }
  54. /*
  55. * Allocate skbuffer.
  56. */
  57. skb = dev_alloc_skb(frame_size + head_size + tail_size);
  58. if (!skb)
  59. return NULL;
  60. /*
  61. * Make sure we not have a frame with the requested bytes
  62. * available in the head and tail.
  63. */
  64. skb_reserve(skb, head_size);
  65. skb_put(skb, frame_size);
  66. /*
  67. * Populate skbdesc.
  68. */
  69. skbdesc = get_skb_frame_desc(skb);
  70. memset(skbdesc, 0, sizeof(*skbdesc));
  71. skbdesc->entry = entry;
  72. if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags)) {
  73. skbdesc->skb_dma = dma_map_single(rt2x00dev->dev,
  74. skb->data,
  75. skb->len,
  76. DMA_FROM_DEVICE);
  77. skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
  78. }
  79. return skb;
  80. }
  81. void rt2x00queue_map_txskb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
  82. {
  83. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  84. /*
  85. * If device has requested headroom, we should make sure that
  86. * is also mapped to the DMA so it can be used for transfering
  87. * additional descriptor information to the hardware.
  88. */
  89. skb_push(skb, rt2x00dev->hw->extra_tx_headroom);
  90. skbdesc->skb_dma =
  91. dma_map_single(rt2x00dev->dev, skb->data, skb->len, DMA_TO_DEVICE);
  92. /*
  93. * Restore data pointer to original location again.
  94. */
  95. skb_pull(skb, rt2x00dev->hw->extra_tx_headroom);
  96. skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
  97. }
  98. EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
  99. void rt2x00queue_unmap_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
  100. {
  101. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  102. if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
  103. dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
  104. DMA_FROM_DEVICE);
  105. skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
  106. }
  107. if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
  108. /*
  109. * Add headroom to the skb length, it has been removed
  110. * by the driver, but it was actually mapped to DMA.
  111. */
  112. dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma,
  113. skb->len + rt2x00dev->hw->extra_tx_headroom,
  114. DMA_TO_DEVICE);
  115. skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
  116. }
  117. }
  118. void rt2x00queue_free_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
  119. {
  120. if (!skb)
  121. return;
  122. rt2x00queue_unmap_skb(rt2x00dev, skb);
  123. dev_kfree_skb_any(skb);
  124. }
  125. void rt2x00queue_payload_align(struct sk_buff *skb,
  126. bool l2pad, unsigned int header_length)
  127. {
  128. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  129. unsigned int frame_length = skb->len;
  130. unsigned int align = ALIGN_SIZE(skb, header_length);
  131. if (!align)
  132. return;
  133. if (l2pad) {
  134. if (skbdesc->flags & SKBDESC_L2_PADDED) {
  135. /* Remove L2 padding */
  136. memmove(skb->data + align, skb->data, header_length);
  137. skb_pull(skb, align);
  138. skbdesc->flags &= ~SKBDESC_L2_PADDED;
  139. } else {
  140. /* Add L2 padding */
  141. skb_push(skb, align);
  142. memmove(skb->data, skb->data + align, header_length);
  143. skbdesc->flags |= SKBDESC_L2_PADDED;
  144. }
  145. } else {
  146. /* Generic payload alignment to 4-byte boundary */
  147. skb_push(skb, align);
  148. memmove(skb->data, skb->data + align, frame_length);
  149. }
  150. }
  151. static void rt2x00queue_create_tx_descriptor_seq(struct queue_entry *entry,
  152. struct txentry_desc *txdesc)
  153. {
  154. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  155. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  156. struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
  157. unsigned long irqflags;
  158. if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) ||
  159. unlikely(!tx_info->control.vif))
  160. return;
  161. /*
  162. * Hardware should insert sequence counter.
  163. * FIXME: We insert a software sequence counter first for
  164. * hardware that doesn't support hardware sequence counting.
  165. *
  166. * This is wrong because beacons are not getting sequence
  167. * numbers assigned properly.
  168. *
  169. * A secondary problem exists for drivers that cannot toggle
  170. * sequence counting per-frame, since those will override the
  171. * sequence counter given by mac80211.
  172. */
  173. spin_lock_irqsave(&intf->seqlock, irqflags);
  174. if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
  175. intf->seqno += 0x10;
  176. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  177. hdr->seq_ctrl |= cpu_to_le16(intf->seqno);
  178. spin_unlock_irqrestore(&intf->seqlock, irqflags);
  179. __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
  180. }
  181. static void rt2x00queue_create_tx_descriptor_plcp(struct queue_entry *entry,
  182. struct txentry_desc *txdesc,
  183. const struct rt2x00_rate *hwrate)
  184. {
  185. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  186. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  187. struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
  188. unsigned int data_length;
  189. unsigned int duration;
  190. unsigned int residual;
  191. /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
  192. data_length = entry->skb->len + 4;
  193. data_length += rt2x00crypto_tx_overhead(rt2x00dev, entry->skb);
  194. /*
  195. * PLCP setup
  196. * Length calculation depends on OFDM/CCK rate.
  197. */
  198. txdesc->signal = hwrate->plcp;
  199. txdesc->service = 0x04;
  200. if (hwrate->flags & DEV_RATE_OFDM) {
  201. txdesc->length_high = (data_length >> 6) & 0x3f;
  202. txdesc->length_low = data_length & 0x3f;
  203. } else {
  204. /*
  205. * Convert length to microseconds.
  206. */
  207. residual = GET_DURATION_RES(data_length, hwrate->bitrate);
  208. duration = GET_DURATION(data_length, hwrate->bitrate);
  209. if (residual != 0) {
  210. duration++;
  211. /*
  212. * Check if we need to set the Length Extension
  213. */
  214. if (hwrate->bitrate == 110 && residual <= 30)
  215. txdesc->service |= 0x80;
  216. }
  217. txdesc->length_high = (duration >> 8) & 0xff;
  218. txdesc->length_low = duration & 0xff;
  219. /*
  220. * When preamble is enabled we should set the
  221. * preamble bit for the signal.
  222. */
  223. if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  224. txdesc->signal |= 0x08;
  225. }
  226. }
  227. static void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
  228. struct txentry_desc *txdesc)
  229. {
  230. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  231. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  232. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  233. struct ieee80211_rate *rate =
  234. ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
  235. const struct rt2x00_rate *hwrate;
  236. memset(txdesc, 0, sizeof(*txdesc));
  237. /*
  238. * Initialize information from queue
  239. */
  240. txdesc->queue = entry->queue->qid;
  241. txdesc->cw_min = entry->queue->cw_min;
  242. txdesc->cw_max = entry->queue->cw_max;
  243. txdesc->aifs = entry->queue->aifs;
  244. /*
  245. * Header and alignment information.
  246. */
  247. txdesc->header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  248. txdesc->l2pad = ALIGN_SIZE(entry->skb, txdesc->header_length);
  249. /*
  250. * Check whether this frame is to be acked.
  251. */
  252. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
  253. __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
  254. /*
  255. * Check if this is a RTS/CTS frame
  256. */
  257. if (ieee80211_is_rts(hdr->frame_control) ||
  258. ieee80211_is_cts(hdr->frame_control)) {
  259. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  260. if (ieee80211_is_rts(hdr->frame_control))
  261. __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
  262. else
  263. __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
  264. if (tx_info->control.rts_cts_rate_idx >= 0)
  265. rate =
  266. ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
  267. }
  268. /*
  269. * Determine retry information.
  270. */
  271. txdesc->retry_limit = tx_info->control.rates[0].count - 1;
  272. if (txdesc->retry_limit >= rt2x00dev->long_retry)
  273. __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
  274. /*
  275. * Check if more fragments are pending
  276. */
  277. if (ieee80211_has_morefrags(hdr->frame_control)) {
  278. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  279. __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
  280. }
  281. /*
  282. * Beacons and probe responses require the tsf timestamp
  283. * to be inserted into the frame.
  284. */
  285. if (ieee80211_is_beacon(hdr->frame_control) ||
  286. ieee80211_is_probe_resp(hdr->frame_control))
  287. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
  288. /*
  289. * Determine with what IFS priority this frame should be send.
  290. * Set ifs to IFS_SIFS when the this is not the first fragment,
  291. * or this fragment came after RTS/CTS.
  292. */
  293. if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
  294. !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
  295. __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
  296. txdesc->ifs = IFS_BACKOFF;
  297. } else
  298. txdesc->ifs = IFS_SIFS;
  299. /*
  300. * Determine rate modulation.
  301. */
  302. hwrate = rt2x00_get_rate(rate->hw_value);
  303. txdesc->rate_mode = RATE_MODE_CCK;
  304. if (hwrate->flags & DEV_RATE_OFDM)
  305. txdesc->rate_mode = RATE_MODE_OFDM;
  306. /*
  307. * Apply TX descriptor handling by components
  308. */
  309. rt2x00crypto_create_tx_descriptor(entry, txdesc);
  310. rt2x00ht_create_tx_descriptor(entry, txdesc, hwrate);
  311. rt2x00queue_create_tx_descriptor_seq(entry, txdesc);
  312. rt2x00queue_create_tx_descriptor_plcp(entry, txdesc, hwrate);
  313. }
  314. static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
  315. struct txentry_desc *txdesc)
  316. {
  317. struct data_queue *queue = entry->queue;
  318. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  319. rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc);
  320. /*
  321. * All processing on the frame has been completed, this means
  322. * it is now ready to be dumped to userspace through debugfs.
  323. */
  324. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb);
  325. /*
  326. * Check if we need to kick the queue, there are however a few rules
  327. * 1) Don't kick beacon queue
  328. * 2) Don't kick unless this is the last in frame in a burst.
  329. * When the burst flag is set, this frame is always followed
  330. * by another frame which in some way are related to eachother.
  331. * This is true for fragments, RTS or CTS-to-self frames.
  332. * 3) Rule 2 can be broken when the available entries
  333. * in the queue are less then a certain threshold.
  334. */
  335. if (entry->queue->qid == QID_BEACON)
  336. return;
  337. if (rt2x00queue_threshold(queue) ||
  338. !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
  339. rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, queue->qid);
  340. }
  341. int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb)
  342. {
  343. struct ieee80211_tx_info *tx_info;
  344. struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
  345. struct txentry_desc txdesc;
  346. struct skb_frame_desc *skbdesc;
  347. u8 rate_idx, rate_flags;
  348. if (unlikely(rt2x00queue_full(queue)))
  349. return -ENOBUFS;
  350. if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) {
  351. ERROR(queue->rt2x00dev,
  352. "Arrived at non-free entry in the non-full queue %d.\n"
  353. "Please file bug report to %s.\n",
  354. queue->qid, DRV_PROJECT);
  355. return -EINVAL;
  356. }
  357. /*
  358. * Copy all TX descriptor information into txdesc,
  359. * after that we are free to use the skb->cb array
  360. * for our information.
  361. */
  362. entry->skb = skb;
  363. rt2x00queue_create_tx_descriptor(entry, &txdesc);
  364. /*
  365. * All information is retrieved from the skb->cb array,
  366. * now we should claim ownership of the driver part of that
  367. * array, preserving the bitrate index and flags.
  368. */
  369. tx_info = IEEE80211_SKB_CB(skb);
  370. rate_idx = tx_info->control.rates[0].idx;
  371. rate_flags = tx_info->control.rates[0].flags;
  372. skbdesc = get_skb_frame_desc(skb);
  373. memset(skbdesc, 0, sizeof(*skbdesc));
  374. skbdesc->entry = entry;
  375. skbdesc->tx_rate_idx = rate_idx;
  376. skbdesc->tx_rate_flags = rate_flags;
  377. /*
  378. * When hardware encryption is supported, and this frame
  379. * is to be encrypted, we should strip the IV/EIV data from
  380. * the frame so we can provide it to the driver seperately.
  381. */
  382. if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
  383. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
  384. if (test_bit(DRIVER_REQUIRE_COPY_IV, &queue->rt2x00dev->flags))
  385. rt2x00crypto_tx_copy_iv(skb, &txdesc);
  386. else
  387. rt2x00crypto_tx_remove_iv(skb, &txdesc);
  388. }
  389. if (test_bit(DRIVER_REQUIRE_L2PAD, &queue->rt2x00dev->flags))
  390. rt2x00queue_payload_align(entry->skb, true,
  391. txdesc.header_length);
  392. /*
  393. * It could be possible that the queue was corrupted and this
  394. * call failed. Since we always return NETDEV_TX_OK to mac80211,
  395. * this frame will simply be dropped.
  396. */
  397. if (unlikely(queue->rt2x00dev->ops->lib->write_tx_data(entry))) {
  398. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  399. entry->skb = NULL;
  400. return -EIO;
  401. }
  402. if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags))
  403. rt2x00queue_map_txskb(queue->rt2x00dev, skb);
  404. set_bit(ENTRY_DATA_PENDING, &entry->flags);
  405. rt2x00queue_index_inc(queue, Q_INDEX);
  406. rt2x00queue_write_tx_descriptor(entry, &txdesc);
  407. return 0;
  408. }
  409. int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
  410. struct ieee80211_vif *vif,
  411. const bool enable_beacon)
  412. {
  413. struct rt2x00_intf *intf = vif_to_intf(vif);
  414. struct skb_frame_desc *skbdesc;
  415. struct txentry_desc txdesc;
  416. __le32 desc[16];
  417. if (unlikely(!intf->beacon))
  418. return -ENOBUFS;
  419. if (!enable_beacon) {
  420. rt2x00dev->ops->lib->kill_tx_queue(rt2x00dev, QID_BEACON);
  421. return 0;
  422. }
  423. intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
  424. if (!intf->beacon->skb)
  425. return -ENOMEM;
  426. /*
  427. * Copy all TX descriptor information into txdesc,
  428. * after that we are free to use the skb->cb array
  429. * for our information.
  430. */
  431. rt2x00queue_create_tx_descriptor(intf->beacon, &txdesc);
  432. /*
  433. * For the descriptor we use a local array from where the
  434. * driver can move it to the correct location required for
  435. * the hardware.
  436. */
  437. memset(desc, 0, sizeof(desc));
  438. /*
  439. * Fill in skb descriptor
  440. */
  441. skbdesc = get_skb_frame_desc(intf->beacon->skb);
  442. memset(skbdesc, 0, sizeof(*skbdesc));
  443. skbdesc->desc = desc;
  444. skbdesc->desc_len = intf->beacon->queue->desc_size;
  445. skbdesc->entry = intf->beacon;
  446. /*
  447. * Write TX descriptor into reserved room in front of the beacon.
  448. */
  449. rt2x00queue_write_tx_descriptor(intf->beacon, &txdesc);
  450. /*
  451. * Send beacon to hardware.
  452. * Also enable beacon generation, which might have been disabled
  453. * by the driver during the config_beacon() callback function.
  454. */
  455. rt2x00dev->ops->lib->write_beacon(intf->beacon);
  456. rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, QID_BEACON);
  457. return 0;
  458. }
  459. struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
  460. const enum data_queue_qid queue)
  461. {
  462. int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  463. if (queue == QID_RX)
  464. return rt2x00dev->rx;
  465. if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
  466. return &rt2x00dev->tx[queue];
  467. if (!rt2x00dev->bcn)
  468. return NULL;
  469. if (queue == QID_BEACON)
  470. return &rt2x00dev->bcn[0];
  471. else if (queue == QID_ATIM && atim)
  472. return &rt2x00dev->bcn[1];
  473. return NULL;
  474. }
  475. EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
  476. struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
  477. enum queue_index index)
  478. {
  479. struct queue_entry *entry;
  480. unsigned long irqflags;
  481. if (unlikely(index >= Q_INDEX_MAX)) {
  482. ERROR(queue->rt2x00dev,
  483. "Entry requested from invalid index type (%d)\n", index);
  484. return NULL;
  485. }
  486. spin_lock_irqsave(&queue->lock, irqflags);
  487. entry = &queue->entries[queue->index[index]];
  488. spin_unlock_irqrestore(&queue->lock, irqflags);
  489. return entry;
  490. }
  491. EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
  492. void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
  493. {
  494. unsigned long irqflags;
  495. if (unlikely(index >= Q_INDEX_MAX)) {
  496. ERROR(queue->rt2x00dev,
  497. "Index change on invalid index type (%d)\n", index);
  498. return;
  499. }
  500. spin_lock_irqsave(&queue->lock, irqflags);
  501. queue->index[index]++;
  502. if (queue->index[index] >= queue->limit)
  503. queue->index[index] = 0;
  504. if (index == Q_INDEX) {
  505. queue->length++;
  506. } else if (index == Q_INDEX_DONE) {
  507. queue->length--;
  508. queue->count++;
  509. }
  510. spin_unlock_irqrestore(&queue->lock, irqflags);
  511. }
  512. static void rt2x00queue_reset(struct data_queue *queue)
  513. {
  514. unsigned long irqflags;
  515. spin_lock_irqsave(&queue->lock, irqflags);
  516. queue->count = 0;
  517. queue->length = 0;
  518. memset(queue->index, 0, sizeof(queue->index));
  519. spin_unlock_irqrestore(&queue->lock, irqflags);
  520. }
  521. void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
  522. {
  523. struct data_queue *queue;
  524. txall_queue_for_each(rt2x00dev, queue)
  525. rt2x00dev->ops->lib->kill_tx_queue(rt2x00dev, queue->qid);
  526. }
  527. void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
  528. {
  529. struct data_queue *queue;
  530. unsigned int i;
  531. queue_for_each(rt2x00dev, queue) {
  532. rt2x00queue_reset(queue);
  533. for (i = 0; i < queue->limit; i++) {
  534. queue->entries[i].flags = 0;
  535. rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
  536. }
  537. }
  538. }
  539. static int rt2x00queue_alloc_entries(struct data_queue *queue,
  540. const struct data_queue_desc *qdesc)
  541. {
  542. struct queue_entry *entries;
  543. unsigned int entry_size;
  544. unsigned int i;
  545. rt2x00queue_reset(queue);
  546. queue->limit = qdesc->entry_num;
  547. queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
  548. queue->data_size = qdesc->data_size;
  549. queue->desc_size = qdesc->desc_size;
  550. /*
  551. * Allocate all queue entries.
  552. */
  553. entry_size = sizeof(*entries) + qdesc->priv_size;
  554. entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
  555. if (!entries)
  556. return -ENOMEM;
  557. #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
  558. ( ((char *)(__base)) + ((__limit) * (__esize)) + \
  559. ((__index) * (__psize)) )
  560. for (i = 0; i < queue->limit; i++) {
  561. entries[i].flags = 0;
  562. entries[i].queue = queue;
  563. entries[i].skb = NULL;
  564. entries[i].entry_idx = i;
  565. entries[i].priv_data =
  566. QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
  567. sizeof(*entries), qdesc->priv_size);
  568. }
  569. #undef QUEUE_ENTRY_PRIV_OFFSET
  570. queue->entries = entries;
  571. return 0;
  572. }
  573. static void rt2x00queue_free_skbs(struct rt2x00_dev *rt2x00dev,
  574. struct data_queue *queue)
  575. {
  576. unsigned int i;
  577. if (!queue->entries)
  578. return;
  579. for (i = 0; i < queue->limit; i++) {
  580. if (queue->entries[i].skb)
  581. rt2x00queue_free_skb(rt2x00dev, queue->entries[i].skb);
  582. }
  583. }
  584. static int rt2x00queue_alloc_rxskbs(struct rt2x00_dev *rt2x00dev,
  585. struct data_queue *queue)
  586. {
  587. unsigned int i;
  588. struct sk_buff *skb;
  589. for (i = 0; i < queue->limit; i++) {
  590. skb = rt2x00queue_alloc_rxskb(rt2x00dev, &queue->entries[i]);
  591. if (!skb)
  592. return -ENOMEM;
  593. queue->entries[i].skb = skb;
  594. }
  595. return 0;
  596. }
  597. int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
  598. {
  599. struct data_queue *queue;
  600. int status;
  601. status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
  602. if (status)
  603. goto exit;
  604. tx_queue_for_each(rt2x00dev, queue) {
  605. status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
  606. if (status)
  607. goto exit;
  608. }
  609. status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
  610. if (status)
  611. goto exit;
  612. if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) {
  613. status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
  614. rt2x00dev->ops->atim);
  615. if (status)
  616. goto exit;
  617. }
  618. status = rt2x00queue_alloc_rxskbs(rt2x00dev, rt2x00dev->rx);
  619. if (status)
  620. goto exit;
  621. return 0;
  622. exit:
  623. ERROR(rt2x00dev, "Queue entries allocation failed.\n");
  624. rt2x00queue_uninitialize(rt2x00dev);
  625. return status;
  626. }
  627. void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
  628. {
  629. struct data_queue *queue;
  630. rt2x00queue_free_skbs(rt2x00dev, rt2x00dev->rx);
  631. queue_for_each(rt2x00dev, queue) {
  632. kfree(queue->entries);
  633. queue->entries = NULL;
  634. }
  635. }
  636. static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
  637. struct data_queue *queue, enum data_queue_qid qid)
  638. {
  639. spin_lock_init(&queue->lock);
  640. queue->rt2x00dev = rt2x00dev;
  641. queue->qid = qid;
  642. queue->txop = 0;
  643. queue->aifs = 2;
  644. queue->cw_min = 5;
  645. queue->cw_max = 10;
  646. }
  647. int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
  648. {
  649. struct data_queue *queue;
  650. enum data_queue_qid qid;
  651. unsigned int req_atim =
  652. !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  653. /*
  654. * We need the following queues:
  655. * RX: 1
  656. * TX: ops->tx_queues
  657. * Beacon: 1
  658. * Atim: 1 (if required)
  659. */
  660. rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
  661. queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
  662. if (!queue) {
  663. ERROR(rt2x00dev, "Queue allocation failed.\n");
  664. return -ENOMEM;
  665. }
  666. /*
  667. * Initialize pointers
  668. */
  669. rt2x00dev->rx = queue;
  670. rt2x00dev->tx = &queue[1];
  671. rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
  672. /*
  673. * Initialize queue parameters.
  674. * RX: qid = QID_RX
  675. * TX: qid = QID_AC_BE + index
  676. * TX: cw_min: 2^5 = 32.
  677. * TX: cw_max: 2^10 = 1024.
  678. * BCN: qid = QID_BEACON
  679. * ATIM: qid = QID_ATIM
  680. */
  681. rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
  682. qid = QID_AC_BE;
  683. tx_queue_for_each(rt2x00dev, queue)
  684. rt2x00queue_init(rt2x00dev, queue, qid++);
  685. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
  686. if (req_atim)
  687. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
  688. return 0;
  689. }
  690. void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
  691. {
  692. kfree(rt2x00dev->rx);
  693. rt2x00dev->rx = NULL;
  694. rt2x00dev->tx = NULL;
  695. rt2x00dev->bcn = NULL;
  696. }