rt2x00queue.c 24 KB

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