rt2x00queue.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
  3. Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
  4. Copyright (C) 2004 - 2009 Gertjan van Wingerde <gwingerde@gmail.com>
  5. <http://rt2x00.serialmonkey.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the
  16. Free Software Foundation, Inc.,
  17. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. /*
  20. Module: rt2x00lib
  21. Abstract: rt2x00 queue specific routines.
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/dma-mapping.h>
  27. #include "rt2x00.h"
  28. #include "rt2x00lib.h"
  29. struct sk_buff *rt2x00queue_alloc_rxskb(struct queue_entry *entry)
  30. {
  31. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  32. struct sk_buff *skb;
  33. struct skb_frame_desc *skbdesc;
  34. unsigned int frame_size;
  35. unsigned int head_size = 0;
  36. unsigned int tail_size = 0;
  37. /*
  38. * The frame size includes descriptor size, because the
  39. * hardware directly receive the frame into the skbuffer.
  40. */
  41. frame_size = entry->queue->data_size + entry->queue->desc_size;
  42. /*
  43. * The payload should be aligned to a 4-byte boundary,
  44. * this means we need at least 3 bytes for moving the frame
  45. * into the correct offset.
  46. */
  47. head_size = 4;
  48. /*
  49. * For IV/EIV/ICV assembly we must make sure there is
  50. * at least 8 bytes bytes available in headroom for IV/EIV
  51. * and 8 bytes for ICV data as tailroon.
  52. */
  53. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
  54. head_size += 8;
  55. tail_size += 8;
  56. }
  57. /*
  58. * Allocate skbuffer.
  59. */
  60. skb = dev_alloc_skb(frame_size + head_size + tail_size);
  61. if (!skb)
  62. return NULL;
  63. /*
  64. * Make sure we not have a frame with the requested bytes
  65. * available in the head and tail.
  66. */
  67. skb_reserve(skb, head_size);
  68. skb_put(skb, frame_size);
  69. /*
  70. * Populate skbdesc.
  71. */
  72. skbdesc = get_skb_frame_desc(skb);
  73. memset(skbdesc, 0, sizeof(*skbdesc));
  74. skbdesc->entry = entry;
  75. if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags)) {
  76. skbdesc->skb_dma = dma_map_single(rt2x00dev->dev,
  77. skb->data,
  78. skb->len,
  79. DMA_FROM_DEVICE);
  80. skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
  81. }
  82. return skb;
  83. }
  84. void rt2x00queue_map_txskb(struct queue_entry *entry)
  85. {
  86. struct device *dev = entry->queue->rt2x00dev->dev;
  87. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  88. skbdesc->skb_dma =
  89. dma_map_single(dev, entry->skb->data, entry->skb->len, DMA_TO_DEVICE);
  90. skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
  91. }
  92. EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
  93. void rt2x00queue_unmap_skb(struct queue_entry *entry)
  94. {
  95. struct device *dev = entry->queue->rt2x00dev->dev;
  96. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  97. if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
  98. dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
  99. DMA_FROM_DEVICE);
  100. skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
  101. } else if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
  102. dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
  103. DMA_TO_DEVICE);
  104. skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
  105. }
  106. }
  107. EXPORT_SYMBOL_GPL(rt2x00queue_unmap_skb);
  108. void rt2x00queue_free_skb(struct queue_entry *entry)
  109. {
  110. if (!entry->skb)
  111. return;
  112. rt2x00queue_unmap_skb(entry);
  113. dev_kfree_skb_any(entry->skb);
  114. entry->skb = NULL;
  115. }
  116. void rt2x00queue_align_frame(struct sk_buff *skb)
  117. {
  118. unsigned int frame_length = skb->len;
  119. unsigned int align = ALIGN_SIZE(skb, 0);
  120. if (!align)
  121. return;
  122. skb_push(skb, align);
  123. memmove(skb->data, skb->data + align, frame_length);
  124. skb_trim(skb, frame_length);
  125. }
  126. void rt2x00queue_align_payload(struct sk_buff *skb, unsigned int header_length)
  127. {
  128. unsigned int frame_length = skb->len;
  129. unsigned int align = ALIGN_SIZE(skb, header_length);
  130. if (!align)
  131. return;
  132. skb_push(skb, align);
  133. memmove(skb->data, skb->data + align, frame_length);
  134. skb_trim(skb, frame_length);
  135. }
  136. void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int header_length)
  137. {
  138. unsigned int payload_length = skb->len - header_length;
  139. unsigned int header_align = ALIGN_SIZE(skb, 0);
  140. unsigned int payload_align = ALIGN_SIZE(skb, header_length);
  141. unsigned int l2pad = payload_length ? L2PAD_SIZE(header_length) : 0;
  142. /*
  143. * Adjust the header alignment if the payload needs to be moved more
  144. * than the header.
  145. */
  146. if (payload_align > header_align)
  147. header_align += 4;
  148. /* There is nothing to do if no alignment is needed */
  149. if (!header_align)
  150. return;
  151. /* Reserve the amount of space needed in front of the frame */
  152. skb_push(skb, header_align);
  153. /*
  154. * Move the header.
  155. */
  156. memmove(skb->data, skb->data + header_align, header_length);
  157. /* Move the payload, if present and if required */
  158. if (payload_length && payload_align)
  159. memmove(skb->data + header_length + l2pad,
  160. skb->data + header_length + l2pad + payload_align,
  161. payload_length);
  162. /* Trim the skb to the correct size */
  163. skb_trim(skb, header_length + l2pad + payload_length);
  164. }
  165. void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int header_length)
  166. {
  167. unsigned int l2pad = L2PAD_SIZE(header_length);
  168. if (!l2pad)
  169. return;
  170. memmove(skb->data + l2pad, skb->data, header_length);
  171. skb_pull(skb, l2pad);
  172. }
  173. static void rt2x00queue_create_tx_descriptor_seq(struct queue_entry *entry,
  174. struct txentry_desc *txdesc)
  175. {
  176. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  177. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  178. struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
  179. unsigned long irqflags;
  180. if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) ||
  181. unlikely(!tx_info->control.vif))
  182. return;
  183. /*
  184. * Hardware should insert sequence counter.
  185. * FIXME: We insert a software sequence counter first for
  186. * hardware that doesn't support hardware sequence counting.
  187. *
  188. * This is wrong because beacons are not getting sequence
  189. * numbers assigned properly.
  190. *
  191. * A secondary problem exists for drivers that cannot toggle
  192. * sequence counting per-frame, since those will override the
  193. * sequence counter given by mac80211.
  194. */
  195. spin_lock_irqsave(&intf->seqlock, irqflags);
  196. if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
  197. intf->seqno += 0x10;
  198. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  199. hdr->seq_ctrl |= cpu_to_le16(intf->seqno);
  200. spin_unlock_irqrestore(&intf->seqlock, irqflags);
  201. __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
  202. }
  203. static void rt2x00queue_create_tx_descriptor_plcp(struct queue_entry *entry,
  204. struct txentry_desc *txdesc,
  205. const struct rt2x00_rate *hwrate)
  206. {
  207. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  208. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  209. struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
  210. unsigned int data_length;
  211. unsigned int duration;
  212. unsigned int residual;
  213. /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
  214. data_length = entry->skb->len + 4;
  215. data_length += rt2x00crypto_tx_overhead(rt2x00dev, entry->skb);
  216. /*
  217. * PLCP setup
  218. * Length calculation depends on OFDM/CCK rate.
  219. */
  220. txdesc->signal = hwrate->plcp;
  221. txdesc->service = 0x04;
  222. if (hwrate->flags & DEV_RATE_OFDM) {
  223. txdesc->length_high = (data_length >> 6) & 0x3f;
  224. txdesc->length_low = data_length & 0x3f;
  225. } else {
  226. /*
  227. * Convert length to microseconds.
  228. */
  229. residual = GET_DURATION_RES(data_length, hwrate->bitrate);
  230. duration = GET_DURATION(data_length, hwrate->bitrate);
  231. if (residual != 0) {
  232. duration++;
  233. /*
  234. * Check if we need to set the Length Extension
  235. */
  236. if (hwrate->bitrate == 110 && residual <= 30)
  237. txdesc->service |= 0x80;
  238. }
  239. txdesc->length_high = (duration >> 8) & 0xff;
  240. txdesc->length_low = duration & 0xff;
  241. /*
  242. * When preamble is enabled we should set the
  243. * preamble bit for the signal.
  244. */
  245. if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  246. txdesc->signal |= 0x08;
  247. }
  248. }
  249. static void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
  250. struct txentry_desc *txdesc)
  251. {
  252. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  253. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  254. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  255. struct ieee80211_rate *rate =
  256. ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
  257. const struct rt2x00_rate *hwrate;
  258. memset(txdesc, 0, sizeof(*txdesc));
  259. /*
  260. * Initialize information from queue
  261. */
  262. txdesc->qid = entry->queue->qid;
  263. txdesc->cw_min = entry->queue->cw_min;
  264. txdesc->cw_max = entry->queue->cw_max;
  265. txdesc->aifs = entry->queue->aifs;
  266. /*
  267. * Header and frame information.
  268. */
  269. txdesc->length = entry->skb->len;
  270. txdesc->header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  271. /*
  272. * Check whether this frame is to be acked.
  273. */
  274. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
  275. __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
  276. /*
  277. * Check if this is a RTS/CTS frame
  278. */
  279. if (ieee80211_is_rts(hdr->frame_control) ||
  280. ieee80211_is_cts(hdr->frame_control)) {
  281. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  282. if (ieee80211_is_rts(hdr->frame_control))
  283. __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
  284. else
  285. __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
  286. if (tx_info->control.rts_cts_rate_idx >= 0)
  287. rate =
  288. ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
  289. }
  290. /*
  291. * Determine retry information.
  292. */
  293. txdesc->retry_limit = tx_info->control.rates[0].count - 1;
  294. if (txdesc->retry_limit >= rt2x00dev->long_retry)
  295. __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
  296. /*
  297. * Check if more fragments are pending
  298. */
  299. if (ieee80211_has_morefrags(hdr->frame_control)) {
  300. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  301. __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
  302. }
  303. /*
  304. * Check if more frames (!= fragments) are pending
  305. */
  306. if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
  307. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  308. /*
  309. * Beacons and probe responses require the tsf timestamp
  310. * to be inserted into the frame, except for a frame that has been injected
  311. * through a monitor interface. This latter is needed for testing a
  312. * monitor interface.
  313. */
  314. if ((ieee80211_is_beacon(hdr->frame_control) ||
  315. ieee80211_is_probe_resp(hdr->frame_control)) &&
  316. (!(tx_info->flags & IEEE80211_TX_CTL_INJECTED)))
  317. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
  318. /*
  319. * Determine with what IFS priority this frame should be send.
  320. * Set ifs to IFS_SIFS when the this is not the first fragment,
  321. * or this fragment came after RTS/CTS.
  322. */
  323. if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
  324. !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
  325. __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
  326. txdesc->ifs = IFS_BACKOFF;
  327. } else
  328. txdesc->ifs = IFS_SIFS;
  329. /*
  330. * Determine rate modulation.
  331. */
  332. hwrate = rt2x00_get_rate(rate->hw_value);
  333. txdesc->rate_mode = RATE_MODE_CCK;
  334. if (hwrate->flags & DEV_RATE_OFDM)
  335. txdesc->rate_mode = RATE_MODE_OFDM;
  336. /*
  337. * Apply TX descriptor handling by components
  338. */
  339. rt2x00crypto_create_tx_descriptor(entry, txdesc);
  340. rt2x00ht_create_tx_descriptor(entry, txdesc, hwrate);
  341. rt2x00queue_create_tx_descriptor_seq(entry, txdesc);
  342. rt2x00queue_create_tx_descriptor_plcp(entry, txdesc, hwrate);
  343. }
  344. static int rt2x00queue_write_tx_data(struct queue_entry *entry,
  345. struct txentry_desc *txdesc)
  346. {
  347. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  348. /*
  349. * This should not happen, we already checked the entry
  350. * was ours. When the hardware disagrees there has been
  351. * a queue corruption!
  352. */
  353. if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
  354. rt2x00dev->ops->lib->get_entry_state(entry))) {
  355. ERROR(rt2x00dev,
  356. "Corrupt queue %d, accessing entry which is not ours.\n"
  357. "Please file bug report to %s.\n",
  358. entry->queue->qid, DRV_PROJECT);
  359. return -EINVAL;
  360. }
  361. /*
  362. * Add the requested extra tx headroom in front of the skb.
  363. */
  364. skb_push(entry->skb, rt2x00dev->ops->extra_tx_headroom);
  365. memset(entry->skb->data, 0, rt2x00dev->ops->extra_tx_headroom);
  366. /*
  367. * Call the driver's write_tx_data function, if it exists.
  368. */
  369. if (rt2x00dev->ops->lib->write_tx_data)
  370. rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
  371. /*
  372. * Map the skb to DMA.
  373. */
  374. if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
  375. rt2x00queue_map_txskb(entry);
  376. return 0;
  377. }
  378. static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
  379. struct txentry_desc *txdesc)
  380. {
  381. struct data_queue *queue = entry->queue;
  382. queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
  383. /*
  384. * All processing on the frame has been completed, this means
  385. * it is now ready to be dumped to userspace through debugfs.
  386. */
  387. rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
  388. }
  389. static void rt2x00queue_kick_tx_queue(struct queue_entry *entry,
  390. struct txentry_desc *txdesc)
  391. {
  392. struct data_queue *queue = entry->queue;
  393. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  394. /*
  395. * Check if we need to kick the queue, there are however a few rules
  396. * 1) Don't kick unless this is the last in frame in a burst.
  397. * When the burst flag is set, this frame is always followed
  398. * by another frame which in some way are related to eachother.
  399. * This is true for fragments, RTS or CTS-to-self frames.
  400. * 2) Rule 1 can be broken when the available entries
  401. * in the queue are less then a certain threshold.
  402. */
  403. if (rt2x00queue_threshold(queue) ||
  404. !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
  405. rt2x00dev->ops->lib->kick_tx_queue(queue);
  406. }
  407. int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
  408. bool local)
  409. {
  410. struct ieee80211_tx_info *tx_info;
  411. struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
  412. struct txentry_desc txdesc;
  413. struct skb_frame_desc *skbdesc;
  414. u8 rate_idx, rate_flags;
  415. if (unlikely(rt2x00queue_full(queue)))
  416. return -ENOBUFS;
  417. if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
  418. &entry->flags))) {
  419. ERROR(queue->rt2x00dev,
  420. "Arrived at non-free entry in the non-full queue %d.\n"
  421. "Please file bug report to %s.\n",
  422. queue->qid, DRV_PROJECT);
  423. return -EINVAL;
  424. }
  425. /*
  426. * Copy all TX descriptor information into txdesc,
  427. * after that we are free to use the skb->cb array
  428. * for our information.
  429. */
  430. entry->skb = skb;
  431. rt2x00queue_create_tx_descriptor(entry, &txdesc);
  432. /*
  433. * All information is retrieved from the skb->cb array,
  434. * now we should claim ownership of the driver part of that
  435. * array, preserving the bitrate index and flags.
  436. */
  437. tx_info = IEEE80211_SKB_CB(skb);
  438. rate_idx = tx_info->control.rates[0].idx;
  439. rate_flags = tx_info->control.rates[0].flags;
  440. skbdesc = get_skb_frame_desc(skb);
  441. memset(skbdesc, 0, sizeof(*skbdesc));
  442. skbdesc->entry = entry;
  443. skbdesc->tx_rate_idx = rate_idx;
  444. skbdesc->tx_rate_flags = rate_flags;
  445. if (local)
  446. skbdesc->flags |= SKBDESC_NOT_MAC80211;
  447. /*
  448. * When hardware encryption is supported, and this frame
  449. * is to be encrypted, we should strip the IV/EIV data from
  450. * the frame so we can provide it to the driver separately.
  451. */
  452. if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
  453. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
  454. if (test_bit(DRIVER_REQUIRE_COPY_IV, &queue->rt2x00dev->flags))
  455. rt2x00crypto_tx_copy_iv(skb, &txdesc);
  456. else
  457. rt2x00crypto_tx_remove_iv(skb, &txdesc);
  458. }
  459. /*
  460. * When DMA allocation is required we should guarentee to the
  461. * driver that the DMA is aligned to a 4-byte boundary.
  462. * However some drivers require L2 padding to pad the payload
  463. * rather then the header. This could be a requirement for
  464. * PCI and USB devices, while header alignment only is valid
  465. * for PCI devices.
  466. */
  467. if (test_bit(DRIVER_REQUIRE_L2PAD, &queue->rt2x00dev->flags))
  468. rt2x00queue_insert_l2pad(entry->skb, txdesc.header_length);
  469. else if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags))
  470. rt2x00queue_align_frame(entry->skb);
  471. /*
  472. * It could be possible that the queue was corrupted and this
  473. * call failed. Since we always return NETDEV_TX_OK to mac80211,
  474. * this frame will simply be dropped.
  475. */
  476. if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
  477. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  478. entry->skb = NULL;
  479. return -EIO;
  480. }
  481. set_bit(ENTRY_DATA_PENDING, &entry->flags);
  482. rt2x00queue_index_inc(queue, Q_INDEX);
  483. rt2x00queue_write_tx_descriptor(entry, &txdesc);
  484. rt2x00queue_kick_tx_queue(entry, &txdesc);
  485. return 0;
  486. }
  487. int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
  488. struct ieee80211_vif *vif,
  489. const bool enable_beacon)
  490. {
  491. struct rt2x00_intf *intf = vif_to_intf(vif);
  492. struct skb_frame_desc *skbdesc;
  493. struct txentry_desc txdesc;
  494. if (unlikely(!intf->beacon))
  495. return -ENOBUFS;
  496. mutex_lock(&intf->beacon_skb_mutex);
  497. /*
  498. * Clean up the beacon skb.
  499. */
  500. rt2x00queue_free_skb(intf->beacon);
  501. if (!enable_beacon) {
  502. rt2x00dev->ops->lib->kill_tx_queue(intf->beacon->queue);
  503. mutex_unlock(&intf->beacon_skb_mutex);
  504. return 0;
  505. }
  506. intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
  507. if (!intf->beacon->skb) {
  508. mutex_unlock(&intf->beacon_skb_mutex);
  509. return -ENOMEM;
  510. }
  511. /*
  512. * Copy all TX descriptor information into txdesc,
  513. * after that we are free to use the skb->cb array
  514. * for our information.
  515. */
  516. rt2x00queue_create_tx_descriptor(intf->beacon, &txdesc);
  517. /*
  518. * Fill in skb descriptor
  519. */
  520. skbdesc = get_skb_frame_desc(intf->beacon->skb);
  521. memset(skbdesc, 0, sizeof(*skbdesc));
  522. skbdesc->entry = intf->beacon;
  523. /*
  524. * Send beacon to hardware and enable beacon genaration..
  525. */
  526. rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
  527. mutex_unlock(&intf->beacon_skb_mutex);
  528. return 0;
  529. }
  530. void rt2x00queue_for_each_entry(struct data_queue *queue,
  531. enum queue_index start,
  532. enum queue_index end,
  533. void (*fn)(struct queue_entry *entry))
  534. {
  535. unsigned long irqflags;
  536. unsigned int index_start;
  537. unsigned int index_end;
  538. unsigned int i;
  539. if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
  540. ERROR(queue->rt2x00dev,
  541. "Entry requested from invalid index range (%d - %d)\n",
  542. start, end);
  543. return;
  544. }
  545. /*
  546. * Only protect the range we are going to loop over,
  547. * if during our loop a extra entry is set to pending
  548. * it should not be kicked during this run, since it
  549. * is part of another TX operation.
  550. */
  551. spin_lock_irqsave(&queue->lock, irqflags);
  552. index_start = queue->index[start];
  553. index_end = queue->index[end];
  554. spin_unlock_irqrestore(&queue->lock, irqflags);
  555. /*
  556. * Start from the TX done pointer, this guarentees that we will
  557. * send out all frames in the correct order.
  558. */
  559. if (index_start < index_end) {
  560. for (i = index_start; i < index_end; i++)
  561. fn(&queue->entries[i]);
  562. } else {
  563. for (i = index_start; i < queue->limit; i++)
  564. fn(&queue->entries[i]);
  565. for (i = 0; i < index_end; i++)
  566. fn(&queue->entries[i]);
  567. }
  568. }
  569. EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
  570. struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
  571. const enum data_queue_qid queue)
  572. {
  573. int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  574. if (queue == QID_RX)
  575. return rt2x00dev->rx;
  576. if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
  577. return &rt2x00dev->tx[queue];
  578. if (!rt2x00dev->bcn)
  579. return NULL;
  580. if (queue == QID_BEACON)
  581. return &rt2x00dev->bcn[0];
  582. else if (queue == QID_ATIM && atim)
  583. return &rt2x00dev->bcn[1];
  584. return NULL;
  585. }
  586. EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
  587. struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
  588. enum queue_index index)
  589. {
  590. struct queue_entry *entry;
  591. unsigned long irqflags;
  592. if (unlikely(index >= Q_INDEX_MAX)) {
  593. ERROR(queue->rt2x00dev,
  594. "Entry requested from invalid index type (%d)\n", index);
  595. return NULL;
  596. }
  597. spin_lock_irqsave(&queue->lock, irqflags);
  598. entry = &queue->entries[queue->index[index]];
  599. spin_unlock_irqrestore(&queue->lock, irqflags);
  600. return entry;
  601. }
  602. EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
  603. void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
  604. {
  605. unsigned long irqflags;
  606. if (unlikely(index >= Q_INDEX_MAX)) {
  607. ERROR(queue->rt2x00dev,
  608. "Index change on invalid index type (%d)\n", index);
  609. return;
  610. }
  611. spin_lock_irqsave(&queue->lock, irqflags);
  612. queue->index[index]++;
  613. if (queue->index[index] >= queue->limit)
  614. queue->index[index] = 0;
  615. queue->last_action[index] = jiffies;
  616. if (index == Q_INDEX) {
  617. queue->length++;
  618. } else if (index == Q_INDEX_DONE) {
  619. queue->length--;
  620. queue->count++;
  621. }
  622. spin_unlock_irqrestore(&queue->lock, irqflags);
  623. }
  624. static void rt2x00queue_reset(struct data_queue *queue)
  625. {
  626. unsigned long irqflags;
  627. unsigned int i;
  628. spin_lock_irqsave(&queue->lock, irqflags);
  629. queue->count = 0;
  630. queue->length = 0;
  631. for (i = 0; i < Q_INDEX_MAX; i++) {
  632. queue->index[i] = 0;
  633. queue->last_action[i] = jiffies;
  634. }
  635. spin_unlock_irqrestore(&queue->lock, irqflags);
  636. }
  637. void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
  638. {
  639. struct data_queue *queue;
  640. txall_queue_for_each(rt2x00dev, queue)
  641. rt2x00dev->ops->lib->kill_tx_queue(queue);
  642. }
  643. void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
  644. {
  645. struct data_queue *queue;
  646. unsigned int i;
  647. queue_for_each(rt2x00dev, queue) {
  648. rt2x00queue_reset(queue);
  649. for (i = 0; i < queue->limit; i++) {
  650. rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
  651. if (queue->qid == QID_RX)
  652. rt2x00queue_index_inc(queue, Q_INDEX);
  653. }
  654. }
  655. }
  656. static int rt2x00queue_alloc_entries(struct data_queue *queue,
  657. const struct data_queue_desc *qdesc)
  658. {
  659. struct queue_entry *entries;
  660. unsigned int entry_size;
  661. unsigned int i;
  662. rt2x00queue_reset(queue);
  663. queue->limit = qdesc->entry_num;
  664. queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
  665. queue->data_size = qdesc->data_size;
  666. queue->desc_size = qdesc->desc_size;
  667. /*
  668. * Allocate all queue entries.
  669. */
  670. entry_size = sizeof(*entries) + qdesc->priv_size;
  671. entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
  672. if (!entries)
  673. return -ENOMEM;
  674. #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
  675. ( ((char *)(__base)) + ((__limit) * (__esize)) + \
  676. ((__index) * (__psize)) )
  677. for (i = 0; i < queue->limit; i++) {
  678. entries[i].flags = 0;
  679. entries[i].queue = queue;
  680. entries[i].skb = NULL;
  681. entries[i].entry_idx = i;
  682. entries[i].priv_data =
  683. QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
  684. sizeof(*entries), qdesc->priv_size);
  685. }
  686. #undef QUEUE_ENTRY_PRIV_OFFSET
  687. queue->entries = entries;
  688. return 0;
  689. }
  690. static void rt2x00queue_free_skbs(struct data_queue *queue)
  691. {
  692. unsigned int i;
  693. if (!queue->entries)
  694. return;
  695. for (i = 0; i < queue->limit; i++) {
  696. rt2x00queue_free_skb(&queue->entries[i]);
  697. }
  698. }
  699. static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
  700. {
  701. unsigned int i;
  702. struct sk_buff *skb;
  703. for (i = 0; i < queue->limit; i++) {
  704. skb = rt2x00queue_alloc_rxskb(&queue->entries[i]);
  705. if (!skb)
  706. return -ENOMEM;
  707. queue->entries[i].skb = skb;
  708. }
  709. return 0;
  710. }
  711. int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
  712. {
  713. struct data_queue *queue;
  714. int status;
  715. status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
  716. if (status)
  717. goto exit;
  718. tx_queue_for_each(rt2x00dev, queue) {
  719. status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
  720. if (status)
  721. goto exit;
  722. }
  723. status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
  724. if (status)
  725. goto exit;
  726. if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) {
  727. status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
  728. rt2x00dev->ops->atim);
  729. if (status)
  730. goto exit;
  731. }
  732. status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
  733. if (status)
  734. goto exit;
  735. return 0;
  736. exit:
  737. ERROR(rt2x00dev, "Queue entries allocation failed.\n");
  738. rt2x00queue_uninitialize(rt2x00dev);
  739. return status;
  740. }
  741. void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
  742. {
  743. struct data_queue *queue;
  744. rt2x00queue_free_skbs(rt2x00dev->rx);
  745. queue_for_each(rt2x00dev, queue) {
  746. kfree(queue->entries);
  747. queue->entries = NULL;
  748. }
  749. }
  750. static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
  751. struct data_queue *queue, enum data_queue_qid qid)
  752. {
  753. spin_lock_init(&queue->lock);
  754. queue->rt2x00dev = rt2x00dev;
  755. queue->qid = qid;
  756. queue->txop = 0;
  757. queue->aifs = 2;
  758. queue->cw_min = 5;
  759. queue->cw_max = 10;
  760. }
  761. int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
  762. {
  763. struct data_queue *queue;
  764. enum data_queue_qid qid;
  765. unsigned int req_atim =
  766. !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  767. /*
  768. * We need the following queues:
  769. * RX: 1
  770. * TX: ops->tx_queues
  771. * Beacon: 1
  772. * Atim: 1 (if required)
  773. */
  774. rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
  775. queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
  776. if (!queue) {
  777. ERROR(rt2x00dev, "Queue allocation failed.\n");
  778. return -ENOMEM;
  779. }
  780. /*
  781. * Initialize pointers
  782. */
  783. rt2x00dev->rx = queue;
  784. rt2x00dev->tx = &queue[1];
  785. rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
  786. /*
  787. * Initialize queue parameters.
  788. * RX: qid = QID_RX
  789. * TX: qid = QID_AC_BE + index
  790. * TX: cw_min: 2^5 = 32.
  791. * TX: cw_max: 2^10 = 1024.
  792. * BCN: qid = QID_BEACON
  793. * ATIM: qid = QID_ATIM
  794. */
  795. rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
  796. qid = QID_AC_BE;
  797. tx_queue_for_each(rt2x00dev, queue)
  798. rt2x00queue_init(rt2x00dev, queue, qid++);
  799. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
  800. if (req_atim)
  801. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
  802. return 0;
  803. }
  804. void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
  805. {
  806. kfree(rt2x00dev->rx);
  807. rt2x00dev->rx = NULL;
  808. rt2x00dev->tx = NULL;
  809. rt2x00dev->bcn = NULL;
  810. }