/drivers/net/wireless/rt2x00/rt2x00queue.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 1267 lines · 747 code · 212 blank · 308 comment · 120 complexity · 14ec32477fa635e1fa885f56009cb3e1 MD5 · raw file

  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(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_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(REQUIRE_DMA, &rt2x00dev->cap_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_insert_l2pad(struct sk_buff *skb, unsigned int header_length)
  127. {
  128. unsigned int payload_length = skb->len - header_length;
  129. unsigned int header_align = ALIGN_SIZE(skb, 0);
  130. unsigned int payload_align = ALIGN_SIZE(skb, header_length);
  131. unsigned int l2pad = payload_length ? L2PAD_SIZE(header_length) : 0;
  132. /*
  133. * Adjust the header alignment if the payload needs to be moved more
  134. * than the header.
  135. */
  136. if (payload_align > header_align)
  137. header_align += 4;
  138. /* There is nothing to do if no alignment is needed */
  139. if (!header_align)
  140. return;
  141. /* Reserve the amount of space needed in front of the frame */
  142. skb_push(skb, header_align);
  143. /*
  144. * Move the header.
  145. */
  146. memmove(skb->data, skb->data + header_align, header_length);
  147. /* Move the payload, if present and if required */
  148. if (payload_length && payload_align)
  149. memmove(skb->data + header_length + l2pad,
  150. skb->data + header_length + l2pad + payload_align,
  151. payload_length);
  152. /* Trim the skb to the correct size */
  153. skb_trim(skb, header_length + l2pad + payload_length);
  154. }
  155. void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int header_length)
  156. {
  157. /*
  158. * L2 padding is only present if the skb contains more than just the
  159. * IEEE 802.11 header.
  160. */
  161. unsigned int l2pad = (skb->len > header_length) ?
  162. L2PAD_SIZE(header_length) : 0;
  163. if (!l2pad)
  164. return;
  165. memmove(skb->data + l2pad, skb->data, header_length);
  166. skb_pull(skb, l2pad);
  167. }
  168. static void rt2x00queue_create_tx_descriptor_seq(struct queue_entry *entry,
  169. struct txentry_desc *txdesc)
  170. {
  171. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  172. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  173. struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
  174. unsigned long irqflags;
  175. if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
  176. return;
  177. __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
  178. if (!test_bit(REQUIRE_SW_SEQNO, &entry->queue->rt2x00dev->cap_flags))
  179. return;
  180. /*
  181. * The hardware is not able to insert a sequence number. Assign a
  182. * software generated one here.
  183. *
  184. * This is wrong because beacons are not getting sequence
  185. * numbers assigned properly.
  186. *
  187. * A secondary problem exists for drivers that cannot toggle
  188. * sequence counting per-frame, since those will override the
  189. * sequence counter given by mac80211.
  190. */
  191. spin_lock_irqsave(&intf->seqlock, irqflags);
  192. if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
  193. intf->seqno += 0x10;
  194. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  195. hdr->seq_ctrl |= cpu_to_le16(intf->seqno);
  196. spin_unlock_irqrestore(&intf->seqlock, irqflags);
  197. }
  198. static void rt2x00queue_create_tx_descriptor_plcp(struct queue_entry *entry,
  199. struct txentry_desc *txdesc,
  200. const struct rt2x00_rate *hwrate)
  201. {
  202. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  203. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  204. struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
  205. unsigned int data_length;
  206. unsigned int duration;
  207. unsigned int residual;
  208. /*
  209. * Determine with what IFS priority this frame should be send.
  210. * Set ifs to IFS_SIFS when the this is not the first fragment,
  211. * or this fragment came after RTS/CTS.
  212. */
  213. if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
  214. txdesc->u.plcp.ifs = IFS_BACKOFF;
  215. else
  216. txdesc->u.plcp.ifs = IFS_SIFS;
  217. /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
  218. data_length = entry->skb->len + 4;
  219. data_length += rt2x00crypto_tx_overhead(rt2x00dev, entry->skb);
  220. /*
  221. * PLCP setup
  222. * Length calculation depends on OFDM/CCK rate.
  223. */
  224. txdesc->u.plcp.signal = hwrate->plcp;
  225. txdesc->u.plcp.service = 0x04;
  226. if (hwrate->flags & DEV_RATE_OFDM) {
  227. txdesc->u.plcp.length_high = (data_length >> 6) & 0x3f;
  228. txdesc->u.plcp.length_low = data_length & 0x3f;
  229. } else {
  230. /*
  231. * Convert length to microseconds.
  232. */
  233. residual = GET_DURATION_RES(data_length, hwrate->bitrate);
  234. duration = GET_DURATION(data_length, hwrate->bitrate);
  235. if (residual != 0) {
  236. duration++;
  237. /*
  238. * Check if we need to set the Length Extension
  239. */
  240. if (hwrate->bitrate == 110 && residual <= 30)
  241. txdesc->u.plcp.service |= 0x80;
  242. }
  243. txdesc->u.plcp.length_high = (duration >> 8) & 0xff;
  244. txdesc->u.plcp.length_low = duration & 0xff;
  245. /*
  246. * When preamble is enabled we should set the
  247. * preamble bit for the signal.
  248. */
  249. if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  250. txdesc->u.plcp.signal |= 0x08;
  251. }
  252. }
  253. static void rt2x00queue_create_tx_descriptor_ht(struct queue_entry *entry,
  254. struct txentry_desc *txdesc,
  255. const struct rt2x00_rate *hwrate)
  256. {
  257. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  258. struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
  259. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  260. if (tx_info->control.sta)
  261. txdesc->u.ht.mpdu_density =
  262. tx_info->control.sta->ht_cap.ampdu_density;
  263. txdesc->u.ht.ba_size = 7; /* FIXME: What value is needed? */
  264. /*
  265. * Only one STBC stream is supported for now.
  266. */
  267. if (tx_info->flags & IEEE80211_TX_CTL_STBC)
  268. txdesc->u.ht.stbc = 1;
  269. /*
  270. * If IEEE80211_TX_RC_MCS is set txrate->idx just contains the
  271. * mcs rate to be used
  272. */
  273. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  274. txdesc->u.ht.mcs = txrate->idx;
  275. /*
  276. * MIMO PS should be set to 1 for STA's using dynamic SM PS
  277. * when using more then one tx stream (>MCS7).
  278. */
  279. if (tx_info->control.sta && txdesc->u.ht.mcs > 7 &&
  280. ((tx_info->control.sta->ht_cap.cap &
  281. IEEE80211_HT_CAP_SM_PS) >>
  282. IEEE80211_HT_CAP_SM_PS_SHIFT) ==
  283. WLAN_HT_CAP_SM_PS_DYNAMIC)
  284. __set_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags);
  285. } else {
  286. txdesc->u.ht.mcs = rt2x00_get_rate_mcs(hwrate->mcs);
  287. if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  288. txdesc->u.ht.mcs |= 0x08;
  289. }
  290. /*
  291. * This frame is eligible for an AMPDU, however, don't aggregate
  292. * frames that are intended to probe a specific tx rate.
  293. */
  294. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU &&
  295. !(tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE))
  296. __set_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags);
  297. /*
  298. * Set 40Mhz mode if necessary (for legacy rates this will
  299. * duplicate the frame to both channels).
  300. */
  301. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH ||
  302. txrate->flags & IEEE80211_TX_RC_DUP_DATA)
  303. __set_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags);
  304. if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
  305. __set_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags);
  306. /*
  307. * Determine IFS values
  308. * - Use TXOP_BACKOFF for management frames except beacons
  309. * - Use TXOP_SIFS for fragment bursts
  310. * - Use TXOP_HTTXOP for everything else
  311. *
  312. * Note: rt2800 devices won't use CTS protection (if used)
  313. * for frames not transmitted with TXOP_HTTXOP
  314. */
  315. if (ieee80211_is_mgmt(hdr->frame_control) &&
  316. !ieee80211_is_beacon(hdr->frame_control))
  317. txdesc->u.ht.txop = TXOP_BACKOFF;
  318. else if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
  319. txdesc->u.ht.txop = TXOP_SIFS;
  320. else
  321. txdesc->u.ht.txop = TXOP_HTTXOP;
  322. }
  323. static void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
  324. struct txentry_desc *txdesc)
  325. {
  326. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  327. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  328. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  329. struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
  330. struct ieee80211_rate *rate;
  331. const struct rt2x00_rate *hwrate = NULL;
  332. memset(txdesc, 0, sizeof(*txdesc));
  333. /*
  334. * Header and frame information.
  335. */
  336. txdesc->length = entry->skb->len;
  337. txdesc->header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  338. /*
  339. * Check whether this frame is to be acked.
  340. */
  341. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
  342. __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
  343. /*
  344. * Check if this is a RTS/CTS frame
  345. */
  346. if (ieee80211_is_rts(hdr->frame_control) ||
  347. ieee80211_is_cts(hdr->frame_control)) {
  348. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  349. if (ieee80211_is_rts(hdr->frame_control))
  350. __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
  351. else
  352. __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
  353. if (tx_info->control.rts_cts_rate_idx >= 0)
  354. rate =
  355. ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
  356. }
  357. /*
  358. * Determine retry information.
  359. */
  360. txdesc->retry_limit = tx_info->control.rates[0].count - 1;
  361. if (txdesc->retry_limit >= rt2x00dev->long_retry)
  362. __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
  363. /*
  364. * Check if more fragments are pending
  365. */
  366. if (ieee80211_has_morefrags(hdr->frame_control)) {
  367. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  368. __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
  369. }
  370. /*
  371. * Check if more frames (!= fragments) are pending
  372. */
  373. if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
  374. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  375. /*
  376. * Beacons and probe responses require the tsf timestamp
  377. * to be inserted into the frame.
  378. */
  379. if (ieee80211_is_beacon(hdr->frame_control) ||
  380. ieee80211_is_probe_resp(hdr->frame_control))
  381. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
  382. if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
  383. !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags))
  384. __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
  385. /*
  386. * Determine rate modulation.
  387. */
  388. if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  389. txdesc->rate_mode = RATE_MODE_HT_GREENFIELD;
  390. else if (txrate->flags & IEEE80211_TX_RC_MCS)
  391. txdesc->rate_mode = RATE_MODE_HT_MIX;
  392. else {
  393. rate = ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
  394. hwrate = rt2x00_get_rate(rate->hw_value);
  395. if (hwrate->flags & DEV_RATE_OFDM)
  396. txdesc->rate_mode = RATE_MODE_OFDM;
  397. else
  398. txdesc->rate_mode = RATE_MODE_CCK;
  399. }
  400. /*
  401. * Apply TX descriptor handling by components
  402. */
  403. rt2x00crypto_create_tx_descriptor(entry, txdesc);
  404. rt2x00queue_create_tx_descriptor_seq(entry, txdesc);
  405. if (test_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags))
  406. rt2x00queue_create_tx_descriptor_ht(entry, txdesc, hwrate);
  407. else
  408. rt2x00queue_create_tx_descriptor_plcp(entry, txdesc, hwrate);
  409. }
  410. static int rt2x00queue_write_tx_data(struct queue_entry *entry,
  411. struct txentry_desc *txdesc)
  412. {
  413. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  414. /*
  415. * This should not happen, we already checked the entry
  416. * was ours. When the hardware disagrees there has been
  417. * a queue corruption!
  418. */
  419. if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
  420. rt2x00dev->ops->lib->get_entry_state(entry))) {
  421. ERROR(rt2x00dev,
  422. "Corrupt queue %d, accessing entry which is not ours.\n"
  423. "Please file bug report to %s.\n",
  424. entry->queue->qid, DRV_PROJECT);
  425. return -EINVAL;
  426. }
  427. /*
  428. * Add the requested extra tx headroom in front of the skb.
  429. */
  430. skb_push(entry->skb, rt2x00dev->ops->extra_tx_headroom);
  431. memset(entry->skb->data, 0, rt2x00dev->ops->extra_tx_headroom);
  432. /*
  433. * Call the driver's write_tx_data function, if it exists.
  434. */
  435. if (rt2x00dev->ops->lib->write_tx_data)
  436. rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
  437. /*
  438. * Map the skb to DMA.
  439. */
  440. if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags))
  441. rt2x00queue_map_txskb(entry);
  442. return 0;
  443. }
  444. static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
  445. struct txentry_desc *txdesc)
  446. {
  447. struct data_queue *queue = entry->queue;
  448. queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
  449. /*
  450. * All processing on the frame has been completed, this means
  451. * it is now ready to be dumped to userspace through debugfs.
  452. */
  453. rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
  454. }
  455. static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
  456. struct txentry_desc *txdesc)
  457. {
  458. /*
  459. * Check if we need to kick the queue, there are however a few rules
  460. * 1) Don't kick unless this is the last in frame in a burst.
  461. * When the burst flag is set, this frame is always followed
  462. * by another frame which in some way are related to eachother.
  463. * This is true for fragments, RTS or CTS-to-self frames.
  464. * 2) Rule 1 can be broken when the available entries
  465. * in the queue are less then a certain threshold.
  466. */
  467. if (rt2x00queue_threshold(queue) ||
  468. !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
  469. queue->rt2x00dev->ops->lib->kick_queue(queue);
  470. }
  471. int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
  472. bool local)
  473. {
  474. struct ieee80211_tx_info *tx_info;
  475. struct queue_entry *entry;
  476. struct txentry_desc txdesc;
  477. struct skb_frame_desc *skbdesc;
  478. u8 rate_idx, rate_flags;
  479. int ret = 0;
  480. spin_lock(&queue->tx_lock);
  481. entry = rt2x00queue_get_entry(queue, Q_INDEX);
  482. if (unlikely(rt2x00queue_full(queue))) {
  483. ERROR(queue->rt2x00dev,
  484. "Dropping frame due to full tx queue %d.\n", queue->qid);
  485. ret = -ENOBUFS;
  486. goto out;
  487. }
  488. if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
  489. &entry->flags))) {
  490. ERROR(queue->rt2x00dev,
  491. "Arrived at non-free entry in the non-full queue %d.\n"
  492. "Please file bug report to %s.\n",
  493. queue->qid, DRV_PROJECT);
  494. ret = -EINVAL;
  495. goto out;
  496. }
  497. /*
  498. * Copy all TX descriptor information into txdesc,
  499. * after that we are free to use the skb->cb array
  500. * for our information.
  501. */
  502. entry->skb = skb;
  503. rt2x00queue_create_tx_descriptor(entry, &txdesc);
  504. /*
  505. * All information is retrieved from the skb->cb array,
  506. * now we should claim ownership of the driver part of that
  507. * array, preserving the bitrate index and flags.
  508. */
  509. tx_info = IEEE80211_SKB_CB(skb);
  510. rate_idx = tx_info->control.rates[0].idx;
  511. rate_flags = tx_info->control.rates[0].flags;
  512. skbdesc = get_skb_frame_desc(skb);
  513. memset(skbdesc, 0, sizeof(*skbdesc));
  514. skbdesc->entry = entry;
  515. skbdesc->tx_rate_idx = rate_idx;
  516. skbdesc->tx_rate_flags = rate_flags;
  517. if (local)
  518. skbdesc->flags |= SKBDESC_NOT_MAC80211;
  519. /*
  520. * When hardware encryption is supported, and this frame
  521. * is to be encrypted, we should strip the IV/EIV data from
  522. * the frame so we can provide it to the driver separately.
  523. */
  524. if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
  525. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
  526. if (test_bit(REQUIRE_COPY_IV, &queue->rt2x00dev->cap_flags))
  527. rt2x00crypto_tx_copy_iv(skb, &txdesc);
  528. else
  529. rt2x00crypto_tx_remove_iv(skb, &txdesc);
  530. }
  531. /*
  532. * When DMA allocation is required we should guarantee to the
  533. * driver that the DMA is aligned to a 4-byte boundary.
  534. * However some drivers require L2 padding to pad the payload
  535. * rather then the header. This could be a requirement for
  536. * PCI and USB devices, while header alignment only is valid
  537. * for PCI devices.
  538. */
  539. if (test_bit(REQUIRE_L2PAD, &queue->rt2x00dev->cap_flags))
  540. rt2x00queue_insert_l2pad(entry->skb, txdesc.header_length);
  541. else if (test_bit(REQUIRE_DMA, &queue->rt2x00dev->cap_flags))
  542. rt2x00queue_align_frame(entry->skb);
  543. /*
  544. * It could be possible that the queue was corrupted and this
  545. * call failed. Since we always return NETDEV_TX_OK to mac80211,
  546. * this frame will simply be dropped.
  547. */
  548. if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
  549. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  550. entry->skb = NULL;
  551. ret = -EIO;
  552. goto out;
  553. }
  554. set_bit(ENTRY_DATA_PENDING, &entry->flags);
  555. rt2x00queue_index_inc(entry, Q_INDEX);
  556. rt2x00queue_write_tx_descriptor(entry, &txdesc);
  557. rt2x00queue_kick_tx_queue(queue, &txdesc);
  558. out:
  559. spin_unlock(&queue->tx_lock);
  560. return ret;
  561. }
  562. int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
  563. struct ieee80211_vif *vif)
  564. {
  565. struct rt2x00_intf *intf = vif_to_intf(vif);
  566. if (unlikely(!intf->beacon))
  567. return -ENOBUFS;
  568. mutex_lock(&intf->beacon_skb_mutex);
  569. /*
  570. * Clean up the beacon skb.
  571. */
  572. rt2x00queue_free_skb(intf->beacon);
  573. /*
  574. * Clear beacon (single bssid devices don't need to clear the beacon
  575. * since the beacon queue will get stopped anyway).
  576. */
  577. if (rt2x00dev->ops->lib->clear_beacon)
  578. rt2x00dev->ops->lib->clear_beacon(intf->beacon);
  579. mutex_unlock(&intf->beacon_skb_mutex);
  580. return 0;
  581. }
  582. int rt2x00queue_update_beacon_locked(struct rt2x00_dev *rt2x00dev,
  583. struct ieee80211_vif *vif)
  584. {
  585. struct rt2x00_intf *intf = vif_to_intf(vif);
  586. struct skb_frame_desc *skbdesc;
  587. struct txentry_desc txdesc;
  588. if (unlikely(!intf->beacon))
  589. return -ENOBUFS;
  590. /*
  591. * Clean up the beacon skb.
  592. */
  593. rt2x00queue_free_skb(intf->beacon);
  594. intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
  595. if (!intf->beacon->skb)
  596. return -ENOMEM;
  597. /*
  598. * Copy all TX descriptor information into txdesc,
  599. * after that we are free to use the skb->cb array
  600. * for our information.
  601. */
  602. rt2x00queue_create_tx_descriptor(intf->beacon, &txdesc);
  603. /*
  604. * Fill in skb descriptor
  605. */
  606. skbdesc = get_skb_frame_desc(intf->beacon->skb);
  607. memset(skbdesc, 0, sizeof(*skbdesc));
  608. skbdesc->entry = intf->beacon;
  609. /*
  610. * Send beacon to hardware.
  611. */
  612. rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
  613. return 0;
  614. }
  615. int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
  616. struct ieee80211_vif *vif)
  617. {
  618. struct rt2x00_intf *intf = vif_to_intf(vif);
  619. int ret;
  620. mutex_lock(&intf->beacon_skb_mutex);
  621. ret = rt2x00queue_update_beacon_locked(rt2x00dev, vif);
  622. mutex_unlock(&intf->beacon_skb_mutex);
  623. return ret;
  624. }
  625. bool rt2x00queue_for_each_entry(struct data_queue *queue,
  626. enum queue_index start,
  627. enum queue_index end,
  628. void *data,
  629. bool (*fn)(struct queue_entry *entry,
  630. void *data))
  631. {
  632. unsigned long irqflags;
  633. unsigned int index_start;
  634. unsigned int index_end;
  635. unsigned int i;
  636. if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
  637. ERROR(queue->rt2x00dev,
  638. "Entry requested from invalid index range (%d - %d)\n",
  639. start, end);
  640. return true;
  641. }
  642. /*
  643. * Only protect the range we are going to loop over,
  644. * if during our loop a extra entry is set to pending
  645. * it should not be kicked during this run, since it
  646. * is part of another TX operation.
  647. */
  648. spin_lock_irqsave(&queue->index_lock, irqflags);
  649. index_start = queue->index[start];
  650. index_end = queue->index[end];
  651. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  652. /*
  653. * Start from the TX done pointer, this guarantees that we will
  654. * send out all frames in the correct order.
  655. */
  656. if (index_start < index_end) {
  657. for (i = index_start; i < index_end; i++) {
  658. if (fn(&queue->entries[i], data))
  659. return true;
  660. }
  661. } else {
  662. for (i = index_start; i < queue->limit; i++) {
  663. if (fn(&queue->entries[i], data))
  664. return true;
  665. }
  666. for (i = 0; i < index_end; i++) {
  667. if (fn(&queue->entries[i], data))
  668. return true;
  669. }
  670. }
  671. return false;
  672. }
  673. EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
  674. struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
  675. enum queue_index index)
  676. {
  677. struct queue_entry *entry;
  678. unsigned long irqflags;
  679. if (unlikely(index >= Q_INDEX_MAX)) {
  680. ERROR(queue->rt2x00dev,
  681. "Entry requested from invalid index type (%d)\n", index);
  682. return NULL;
  683. }
  684. spin_lock_irqsave(&queue->index_lock, irqflags);
  685. entry = &queue->entries[queue->index[index]];
  686. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  687. return entry;
  688. }
  689. EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
  690. void rt2x00queue_index_inc(struct queue_entry *entry, enum queue_index index)
  691. {
  692. struct data_queue *queue = entry->queue;
  693. unsigned long irqflags;
  694. if (unlikely(index >= Q_INDEX_MAX)) {
  695. ERROR(queue->rt2x00dev,
  696. "Index change on invalid index type (%d)\n", index);
  697. return;
  698. }
  699. spin_lock_irqsave(&queue->index_lock, irqflags);
  700. queue->index[index]++;
  701. if (queue->index[index] >= queue->limit)
  702. queue->index[index] = 0;
  703. entry->last_action = jiffies;
  704. if (index == Q_INDEX) {
  705. queue->length++;
  706. } else if (index == Q_INDEX_DONE) {
  707. queue->length--;
  708. queue->count++;
  709. }
  710. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  711. }
  712. void rt2x00queue_pause_queue(struct data_queue *queue)
  713. {
  714. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  715. !test_bit(QUEUE_STARTED, &queue->flags) ||
  716. test_and_set_bit(QUEUE_PAUSED, &queue->flags))
  717. return;
  718. switch (queue->qid) {
  719. case QID_AC_VO:
  720. case QID_AC_VI:
  721. case QID_AC_BE:
  722. case QID_AC_BK:
  723. /*
  724. * For TX queues, we have to disable the queue
  725. * inside mac80211.
  726. */
  727. ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
  728. break;
  729. default:
  730. break;
  731. }
  732. }
  733. EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
  734. void rt2x00queue_unpause_queue(struct data_queue *queue)
  735. {
  736. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  737. !test_bit(QUEUE_STARTED, &queue->flags) ||
  738. !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
  739. return;
  740. switch (queue->qid) {
  741. case QID_AC_VO:
  742. case QID_AC_VI:
  743. case QID_AC_BE:
  744. case QID_AC_BK:
  745. /*
  746. * For TX queues, we have to enable the queue
  747. * inside mac80211.
  748. */
  749. ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
  750. break;
  751. case QID_RX:
  752. /*
  753. * For RX we need to kick the queue now in order to
  754. * receive frames.
  755. */
  756. queue->rt2x00dev->ops->lib->kick_queue(queue);
  757. default:
  758. break;
  759. }
  760. }
  761. EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
  762. void rt2x00queue_start_queue(struct data_queue *queue)
  763. {
  764. mutex_lock(&queue->status_lock);
  765. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  766. test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
  767. mutex_unlock(&queue->status_lock);
  768. return;
  769. }
  770. set_bit(QUEUE_PAUSED, &queue->flags);
  771. queue->rt2x00dev->ops->lib->start_queue(queue);
  772. rt2x00queue_unpause_queue(queue);
  773. mutex_unlock(&queue->status_lock);
  774. }
  775. EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
  776. void rt2x00queue_stop_queue(struct data_queue *queue)
  777. {
  778. mutex_lock(&queue->status_lock);
  779. if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
  780. mutex_unlock(&queue->status_lock);
  781. return;
  782. }
  783. rt2x00queue_pause_queue(queue);
  784. queue->rt2x00dev->ops->lib->stop_queue(queue);
  785. mutex_unlock(&queue->status_lock);
  786. }
  787. EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
  788. void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
  789. {
  790. bool started;
  791. bool tx_queue =
  792. (queue->qid == QID_AC_VO) ||
  793. (queue->qid == QID_AC_VI) ||
  794. (queue->qid == QID_AC_BE) ||
  795. (queue->qid == QID_AC_BK);
  796. mutex_lock(&queue->status_lock);
  797. /*
  798. * If the queue has been started, we must stop it temporarily
  799. * to prevent any new frames to be queued on the device. If
  800. * we are not dropping the pending frames, the queue must
  801. * only be stopped in the software and not the hardware,
  802. * otherwise the queue will never become empty on its own.
  803. */
  804. started = test_bit(QUEUE_STARTED, &queue->flags);
  805. if (started) {
  806. /*
  807. * Pause the queue
  808. */
  809. rt2x00queue_pause_queue(queue);
  810. /*
  811. * If we are not supposed to drop any pending
  812. * frames, this means we must force a start (=kick)
  813. * to the queue to make sure the hardware will
  814. * start transmitting.
  815. */
  816. if (!drop && tx_queue)
  817. queue->rt2x00dev->ops->lib->kick_queue(queue);
  818. }
  819. /*
  820. * Check if driver supports flushing, if that is the case we can
  821. * defer the flushing to the driver. Otherwise we must use the
  822. * alternative which just waits for the queue to become empty.
  823. */
  824. if (likely(queue->rt2x00dev->ops->lib->flush_queue))
  825. queue->rt2x00dev->ops->lib->flush_queue(queue, drop);
  826. /*
  827. * The queue flush has failed...
  828. */
  829. if (unlikely(!rt2x00queue_empty(queue)))
  830. WARNING(queue->rt2x00dev, "Queue %d failed to flush\n", queue->qid);
  831. /*
  832. * Restore the queue to the previous status
  833. */
  834. if (started)
  835. rt2x00queue_unpause_queue(queue);
  836. mutex_unlock(&queue->status_lock);
  837. }
  838. EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
  839. void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
  840. {
  841. struct data_queue *queue;
  842. /*
  843. * rt2x00queue_start_queue will call ieee80211_wake_queue
  844. * for each queue after is has been properly initialized.
  845. */
  846. tx_queue_for_each(rt2x00dev, queue)
  847. rt2x00queue_start_queue(queue);
  848. rt2x00queue_start_queue(rt2x00dev->rx);
  849. }
  850. EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
  851. void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
  852. {
  853. struct data_queue *queue;
  854. /*
  855. * rt2x00queue_stop_queue will call ieee80211_stop_queue
  856. * as well, but we are completely shutting doing everything
  857. * now, so it is much safer to stop all TX queues at once,
  858. * and use rt2x00queue_stop_queue for cleaning up.
  859. */
  860. ieee80211_stop_queues(rt2x00dev->hw);
  861. tx_queue_for_each(rt2x00dev, queue)
  862. rt2x00queue_stop_queue(queue);
  863. rt2x00queue_stop_queue(rt2x00dev->rx);
  864. }
  865. EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
  866. void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
  867. {
  868. struct data_queue *queue;
  869. tx_queue_for_each(rt2x00dev, queue)
  870. rt2x00queue_flush_queue(queue, drop);
  871. rt2x00queue_flush_queue(rt2x00dev->rx, drop);
  872. }
  873. EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
  874. static void rt2x00queue_reset(struct data_queue *queue)
  875. {
  876. unsigned long irqflags;
  877. unsigned int i;
  878. spin_lock_irqsave(&queue->index_lock, irqflags);
  879. queue->count = 0;
  880. queue->length = 0;
  881. for (i = 0; i < Q_INDEX_MAX; i++)
  882. queue->index[i] = 0;
  883. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  884. }
  885. void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
  886. {
  887. struct data_queue *queue;
  888. unsigned int i;
  889. queue_for_each(rt2x00dev, queue) {
  890. rt2x00queue_reset(queue);
  891. for (i = 0; i < queue->limit; i++)
  892. rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
  893. }
  894. }
  895. static int rt2x00queue_alloc_entries(struct data_queue *queue,
  896. const struct data_queue_desc *qdesc)
  897. {
  898. struct queue_entry *entries;
  899. unsigned int entry_size;
  900. unsigned int i;
  901. rt2x00queue_reset(queue);
  902. queue->limit = qdesc->entry_num;
  903. queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
  904. queue->data_size = qdesc->data_size;
  905. queue->desc_size = qdesc->desc_size;
  906. /*
  907. * Allocate all queue entries.
  908. */
  909. entry_size = sizeof(*entries) + qdesc->priv_size;
  910. entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
  911. if (!entries)
  912. return -ENOMEM;
  913. #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
  914. (((char *)(__base)) + ((__limit) * (__esize)) + \
  915. ((__index) * (__psize)))
  916. for (i = 0; i < queue->limit; i++) {
  917. entries[i].flags = 0;
  918. entries[i].queue = queue;
  919. entries[i].skb = NULL;
  920. entries[i].entry_idx = i;
  921. entries[i].priv_data =
  922. QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
  923. sizeof(*entries), qdesc->priv_size);
  924. }
  925. #undef QUEUE_ENTRY_PRIV_OFFSET
  926. queue->entries = entries;
  927. return 0;
  928. }
  929. static void rt2x00queue_free_skbs(struct data_queue *queue)
  930. {
  931. unsigned int i;
  932. if (!queue->entries)
  933. return;
  934. for (i = 0; i < queue->limit; i++) {
  935. rt2x00queue_free_skb(&queue->entries[i]);
  936. }
  937. }
  938. static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
  939. {
  940. unsigned int i;
  941. struct sk_buff *skb;
  942. for (i = 0; i < queue->limit; i++) {
  943. skb = rt2x00queue_alloc_rxskb(&queue->entries[i]);
  944. if (!skb)
  945. return -ENOMEM;
  946. queue->entries[i].skb = skb;
  947. }
  948. return 0;
  949. }
  950. int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
  951. {
  952. struct data_queue *queue;
  953. int status;
  954. status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
  955. if (status)
  956. goto exit;
  957. tx_queue_for_each(rt2x00dev, queue) {
  958. status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
  959. if (status)
  960. goto exit;
  961. }
  962. status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
  963. if (status)
  964. goto exit;
  965. if (test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags)) {
  966. status = rt2x00queue_alloc_entries(rt2x00dev->atim,
  967. rt2x00dev->ops->atim);
  968. if (status)
  969. goto exit;
  970. }
  971. status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
  972. if (status)
  973. goto exit;
  974. return 0;
  975. exit:
  976. ERROR(rt2x00dev, "Queue entries allocation failed.\n");
  977. rt2x00queue_uninitialize(rt2x00dev);
  978. return status;
  979. }
  980. void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
  981. {
  982. struct data_queue *queue;
  983. rt2x00queue_free_skbs(rt2x00dev->rx);
  984. queue_for_each(rt2x00dev, queue) {
  985. kfree(queue->entries);
  986. queue->entries = NULL;
  987. }
  988. }
  989. static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
  990. struct data_queue *queue, enum data_queue_qid qid)
  991. {
  992. mutex_init(&queue->status_lock);
  993. spin_lock_init(&queue->tx_lock);
  994. spin_lock_init(&queue->index_lock);
  995. queue->rt2x00dev = rt2x00dev;
  996. queue->qid = qid;
  997. queue->txop = 0;
  998. queue->aifs = 2;
  999. queue->cw_min = 5;
  1000. queue->cw_max = 10;
  1001. }
  1002. int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
  1003. {
  1004. struct data_queue *queue;
  1005. enum data_queue_qid qid;
  1006. unsigned int req_atim =
  1007. !!test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags);
  1008. /*
  1009. * We need the following queues:
  1010. * RX: 1
  1011. * TX: ops->tx_queues
  1012. * Beacon: 1
  1013. * Atim: 1 (if required)
  1014. */
  1015. rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
  1016. queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
  1017. if (!queue) {
  1018. ERROR(rt2x00dev, "Queue allocation failed.\n");
  1019. return -ENOMEM;
  1020. }
  1021. /*
  1022. * Initialize pointers
  1023. */
  1024. rt2x00dev->rx = queue;
  1025. rt2x00dev->tx = &queue[1];
  1026. rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
  1027. rt2x00dev->atim = req_atim ? &queue[2 + rt2x00dev->ops->tx_queues] : NULL;
  1028. /*
  1029. * Initialize queue parameters.
  1030. * RX: qid = QID_RX
  1031. * TX: qid = QID_AC_VO + index
  1032. * TX: cw_min: 2^5 = 32.
  1033. * TX: cw_max: 2^10 = 1024.
  1034. * BCN: qid = QID_BEACON
  1035. * ATIM: qid = QID_ATIM
  1036. */
  1037. rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
  1038. qid = QID_AC_VO;
  1039. tx_queue_for_each(rt2x00dev, queue)
  1040. rt2x00queue_init(rt2x00dev, queue, qid++);
  1041. rt2x00queue_init(rt2x00dev, rt2x00dev->bcn, QID_BEACON);
  1042. if (req_atim)
  1043. rt2x00queue_init(rt2x00dev, rt2x00dev->atim, QID_ATIM);
  1044. return 0;
  1045. }
  1046. void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
  1047. {
  1048. kfree(rt2x00dev->rx);
  1049. rt2x00dev->rx = NULL;
  1050. rt2x00dev->tx = NULL;
  1051. rt2x00dev->bcn = NULL;
  1052. }