rt2x00queue.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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_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(DRIVER_REQUIRE_SW_SEQNO, &entry->queue->rt2x00dev->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(struct queue_entry *entry,
  254. struct txentry_desc *txdesc)
  255. {
  256. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  257. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  258. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  259. struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
  260. struct ieee80211_rate *rate;
  261. const struct rt2x00_rate *hwrate = NULL;
  262. memset(txdesc, 0, sizeof(*txdesc));
  263. /*
  264. * Header and frame information.
  265. */
  266. txdesc->length = entry->skb->len;
  267. txdesc->header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  268. /*
  269. * Check whether this frame is to be acked.
  270. */
  271. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
  272. __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
  273. /*
  274. * Check if this is a RTS/CTS frame
  275. */
  276. if (ieee80211_is_rts(hdr->frame_control) ||
  277. ieee80211_is_cts(hdr->frame_control)) {
  278. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  279. if (ieee80211_is_rts(hdr->frame_control))
  280. __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
  281. else
  282. __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
  283. if (tx_info->control.rts_cts_rate_idx >= 0)
  284. rate =
  285. ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
  286. }
  287. /*
  288. * Determine retry information.
  289. */
  290. txdesc->retry_limit = tx_info->control.rates[0].count - 1;
  291. if (txdesc->retry_limit >= rt2x00dev->long_retry)
  292. __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
  293. /*
  294. * Check if more fragments are pending
  295. */
  296. if (ieee80211_has_morefrags(hdr->frame_control)) {
  297. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  298. __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
  299. }
  300. /*
  301. * Check if more frames (!= fragments) are pending
  302. */
  303. if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
  304. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  305. /*
  306. * Beacons and probe responses require the tsf timestamp
  307. * to be inserted into the frame.
  308. */
  309. if (ieee80211_is_beacon(hdr->frame_control) ||
  310. ieee80211_is_probe_resp(hdr->frame_control))
  311. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
  312. if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
  313. !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags))
  314. __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
  315. /*
  316. * Determine rate modulation.
  317. */
  318. if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  319. txdesc->rate_mode = RATE_MODE_HT_GREENFIELD;
  320. else if (txrate->flags & IEEE80211_TX_RC_MCS)
  321. txdesc->rate_mode = RATE_MODE_HT_MIX;
  322. else {
  323. rate = ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
  324. hwrate = rt2x00_get_rate(rate->hw_value);
  325. if (hwrate->flags & DEV_RATE_OFDM)
  326. txdesc->rate_mode = RATE_MODE_OFDM;
  327. else
  328. txdesc->rate_mode = RATE_MODE_CCK;
  329. }
  330. /*
  331. * Apply TX descriptor handling by components
  332. */
  333. rt2x00crypto_create_tx_descriptor(entry, txdesc);
  334. rt2x00queue_create_tx_descriptor_seq(entry, txdesc);
  335. if (test_bit(DRIVER_REQUIRE_HT_TX_DESC, &rt2x00dev->flags))
  336. rt2x00ht_create_tx_descriptor(entry, txdesc, hwrate);
  337. else
  338. rt2x00queue_create_tx_descriptor_plcp(entry, txdesc, hwrate);
  339. }
  340. static int rt2x00queue_write_tx_data(struct queue_entry *entry,
  341. struct txentry_desc *txdesc)
  342. {
  343. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  344. /*
  345. * This should not happen, we already checked the entry
  346. * was ours. When the hardware disagrees there has been
  347. * a queue corruption!
  348. */
  349. if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
  350. rt2x00dev->ops->lib->get_entry_state(entry))) {
  351. ERROR(rt2x00dev,
  352. "Corrupt queue %d, accessing entry which is not ours.\n"
  353. "Please file bug report to %s.\n",
  354. entry->queue->qid, DRV_PROJECT);
  355. return -EINVAL;
  356. }
  357. /*
  358. * Add the requested extra tx headroom in front of the skb.
  359. */
  360. skb_push(entry->skb, rt2x00dev->ops->extra_tx_headroom);
  361. memset(entry->skb->data, 0, rt2x00dev->ops->extra_tx_headroom);
  362. /*
  363. * Call the driver's write_tx_data function, if it exists.
  364. */
  365. if (rt2x00dev->ops->lib->write_tx_data)
  366. rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
  367. /*
  368. * Map the skb to DMA.
  369. */
  370. if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
  371. rt2x00queue_map_txskb(entry);
  372. return 0;
  373. }
  374. static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
  375. struct txentry_desc *txdesc)
  376. {
  377. struct data_queue *queue = entry->queue;
  378. queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
  379. /*
  380. * All processing on the frame has been completed, this means
  381. * it is now ready to be dumped to userspace through debugfs.
  382. */
  383. rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
  384. }
  385. static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
  386. struct txentry_desc *txdesc)
  387. {
  388. /*
  389. * Check if we need to kick the queue, there are however a few rules
  390. * 1) Don't kick unless this is the last in frame in a burst.
  391. * When the burst flag is set, this frame is always followed
  392. * by another frame which in some way are related to eachother.
  393. * This is true for fragments, RTS or CTS-to-self frames.
  394. * 2) Rule 1 can be broken when the available entries
  395. * in the queue are less then a certain threshold.
  396. */
  397. if (rt2x00queue_threshold(queue) ||
  398. !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
  399. queue->rt2x00dev->ops->lib->kick_queue(queue);
  400. }
  401. int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
  402. bool local)
  403. {
  404. struct ieee80211_tx_info *tx_info;
  405. struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
  406. struct txentry_desc txdesc;
  407. struct skb_frame_desc *skbdesc;
  408. u8 rate_idx, rate_flags;
  409. if (unlikely(rt2x00queue_full(queue))) {
  410. ERROR(queue->rt2x00dev,
  411. "Dropping frame due to full tx queue %d.\n", queue->qid);
  412. return -ENOBUFS;
  413. }
  414. if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
  415. &entry->flags))) {
  416. ERROR(queue->rt2x00dev,
  417. "Arrived at non-free entry in the non-full queue %d.\n"
  418. "Please file bug report to %s.\n",
  419. queue->qid, DRV_PROJECT);
  420. return -EINVAL;
  421. }
  422. /*
  423. * Copy all TX descriptor information into txdesc,
  424. * after that we are free to use the skb->cb array
  425. * for our information.
  426. */
  427. entry->skb = skb;
  428. rt2x00queue_create_tx_descriptor(entry, &txdesc);
  429. /*
  430. * All information is retrieved from the skb->cb array,
  431. * now we should claim ownership of the driver part of that
  432. * array, preserving the bitrate index and flags.
  433. */
  434. tx_info = IEEE80211_SKB_CB(skb);
  435. rate_idx = tx_info->control.rates[0].idx;
  436. rate_flags = tx_info->control.rates[0].flags;
  437. skbdesc = get_skb_frame_desc(skb);
  438. memset(skbdesc, 0, sizeof(*skbdesc));
  439. skbdesc->entry = entry;
  440. skbdesc->tx_rate_idx = rate_idx;
  441. skbdesc->tx_rate_flags = rate_flags;
  442. if (local)
  443. skbdesc->flags |= SKBDESC_NOT_MAC80211;
  444. /*
  445. * When hardware encryption is supported, and this frame
  446. * is to be encrypted, we should strip the IV/EIV data from
  447. * the frame so we can provide it to the driver separately.
  448. */
  449. if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
  450. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
  451. if (test_bit(DRIVER_REQUIRE_COPY_IV, &queue->rt2x00dev->flags))
  452. rt2x00crypto_tx_copy_iv(skb, &txdesc);
  453. else
  454. rt2x00crypto_tx_remove_iv(skb, &txdesc);
  455. }
  456. /*
  457. * When DMA allocation is required we should guarentee to the
  458. * driver that the DMA is aligned to a 4-byte boundary.
  459. * However some drivers require L2 padding to pad the payload
  460. * rather then the header. This could be a requirement for
  461. * PCI and USB devices, while header alignment only is valid
  462. * for PCI devices.
  463. */
  464. if (test_bit(DRIVER_REQUIRE_L2PAD, &queue->rt2x00dev->flags))
  465. rt2x00queue_insert_l2pad(entry->skb, txdesc.header_length);
  466. else if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags))
  467. rt2x00queue_align_frame(entry->skb);
  468. /*
  469. * It could be possible that the queue was corrupted and this
  470. * call failed. Since we always return NETDEV_TX_OK to mac80211,
  471. * this frame will simply be dropped.
  472. */
  473. if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
  474. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  475. entry->skb = NULL;
  476. return -EIO;
  477. }
  478. set_bit(ENTRY_DATA_PENDING, &entry->flags);
  479. rt2x00queue_index_inc(queue, Q_INDEX);
  480. rt2x00queue_write_tx_descriptor(entry, &txdesc);
  481. rt2x00queue_kick_tx_queue(queue, &txdesc);
  482. return 0;
  483. }
  484. int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
  485. struct ieee80211_vif *vif)
  486. {
  487. struct rt2x00_intf *intf = vif_to_intf(vif);
  488. if (unlikely(!intf->beacon))
  489. return -ENOBUFS;
  490. mutex_lock(&intf->beacon_skb_mutex);
  491. /*
  492. * Clean up the beacon skb.
  493. */
  494. rt2x00queue_free_skb(intf->beacon);
  495. /*
  496. * Clear beacon (single bssid devices don't need to clear the beacon
  497. * since the beacon queue will get stopped anyway).
  498. */
  499. if (rt2x00dev->ops->lib->clear_beacon)
  500. rt2x00dev->ops->lib->clear_beacon(intf->beacon);
  501. mutex_unlock(&intf->beacon_skb_mutex);
  502. return 0;
  503. }
  504. int rt2x00queue_update_beacon_locked(struct rt2x00_dev *rt2x00dev,
  505. struct ieee80211_vif *vif)
  506. {
  507. struct rt2x00_intf *intf = vif_to_intf(vif);
  508. struct skb_frame_desc *skbdesc;
  509. struct txentry_desc txdesc;
  510. if (unlikely(!intf->beacon))
  511. return -ENOBUFS;
  512. /*
  513. * Clean up the beacon skb.
  514. */
  515. rt2x00queue_free_skb(intf->beacon);
  516. intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
  517. if (!intf->beacon->skb)
  518. return -ENOMEM;
  519. /*
  520. * Copy all TX descriptor information into txdesc,
  521. * after that we are free to use the skb->cb array
  522. * for our information.
  523. */
  524. rt2x00queue_create_tx_descriptor(intf->beacon, &txdesc);
  525. /*
  526. * Fill in skb descriptor
  527. */
  528. skbdesc = get_skb_frame_desc(intf->beacon->skb);
  529. memset(skbdesc, 0, sizeof(*skbdesc));
  530. skbdesc->entry = intf->beacon;
  531. /*
  532. * Send beacon to hardware.
  533. */
  534. rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
  535. return 0;
  536. }
  537. int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
  538. struct ieee80211_vif *vif)
  539. {
  540. struct rt2x00_intf *intf = vif_to_intf(vif);
  541. int ret;
  542. mutex_lock(&intf->beacon_skb_mutex);
  543. ret = rt2x00queue_update_beacon_locked(rt2x00dev, vif);
  544. mutex_unlock(&intf->beacon_skb_mutex);
  545. return ret;
  546. }
  547. void rt2x00queue_for_each_entry(struct data_queue *queue,
  548. enum queue_index start,
  549. enum queue_index end,
  550. void (*fn)(struct queue_entry *entry))
  551. {
  552. unsigned long irqflags;
  553. unsigned int index_start;
  554. unsigned int index_end;
  555. unsigned int i;
  556. if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
  557. ERROR(queue->rt2x00dev,
  558. "Entry requested from invalid index range (%d - %d)\n",
  559. start, end);
  560. return;
  561. }
  562. /*
  563. * Only protect the range we are going to loop over,
  564. * if during our loop a extra entry is set to pending
  565. * it should not be kicked during this run, since it
  566. * is part of another TX operation.
  567. */
  568. spin_lock_irqsave(&queue->index_lock, irqflags);
  569. index_start = queue->index[start];
  570. index_end = queue->index[end];
  571. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  572. /*
  573. * Start from the TX done pointer, this guarentees that we will
  574. * send out all frames in the correct order.
  575. */
  576. if (index_start < index_end) {
  577. for (i = index_start; i < index_end; i++)
  578. fn(&queue->entries[i]);
  579. } else {
  580. for (i = index_start; i < queue->limit; i++)
  581. fn(&queue->entries[i]);
  582. for (i = 0; i < index_end; i++)
  583. fn(&queue->entries[i]);
  584. }
  585. }
  586. EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
  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->index_lock, irqflags);
  598. entry = &queue->entries[queue->index[index]];
  599. spin_unlock_irqrestore(&queue->index_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->index_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->index_lock, irqflags);
  623. }
  624. void rt2x00queue_pause_queue(struct data_queue *queue)
  625. {
  626. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  627. !test_bit(QUEUE_STARTED, &queue->flags) ||
  628. test_and_set_bit(QUEUE_PAUSED, &queue->flags))
  629. return;
  630. switch (queue->qid) {
  631. case QID_AC_VO:
  632. case QID_AC_VI:
  633. case QID_AC_BE:
  634. case QID_AC_BK:
  635. /*
  636. * For TX queues, we have to disable the queue
  637. * inside mac80211.
  638. */
  639. ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
  640. break;
  641. default:
  642. break;
  643. }
  644. }
  645. EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
  646. void rt2x00queue_unpause_queue(struct data_queue *queue)
  647. {
  648. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  649. !test_bit(QUEUE_STARTED, &queue->flags) ||
  650. !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
  651. return;
  652. switch (queue->qid) {
  653. case QID_AC_VO:
  654. case QID_AC_VI:
  655. case QID_AC_BE:
  656. case QID_AC_BK:
  657. /*
  658. * For TX queues, we have to enable the queue
  659. * inside mac80211.
  660. */
  661. ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
  662. break;
  663. case QID_RX:
  664. /*
  665. * For RX we need to kick the queue now in order to
  666. * receive frames.
  667. */
  668. queue->rt2x00dev->ops->lib->kick_queue(queue);
  669. default:
  670. break;
  671. }
  672. }
  673. EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
  674. void rt2x00queue_start_queue(struct data_queue *queue)
  675. {
  676. mutex_lock(&queue->status_lock);
  677. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  678. test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
  679. mutex_unlock(&queue->status_lock);
  680. return;
  681. }
  682. set_bit(QUEUE_PAUSED, &queue->flags);
  683. queue->rt2x00dev->ops->lib->start_queue(queue);
  684. rt2x00queue_unpause_queue(queue);
  685. mutex_unlock(&queue->status_lock);
  686. }
  687. EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
  688. void rt2x00queue_stop_queue(struct data_queue *queue)
  689. {
  690. mutex_lock(&queue->status_lock);
  691. if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
  692. mutex_unlock(&queue->status_lock);
  693. return;
  694. }
  695. rt2x00queue_pause_queue(queue);
  696. queue->rt2x00dev->ops->lib->stop_queue(queue);
  697. mutex_unlock(&queue->status_lock);
  698. }
  699. EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
  700. void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
  701. {
  702. unsigned int i;
  703. bool started;
  704. bool tx_queue =
  705. (queue->qid == QID_AC_VO) ||
  706. (queue->qid == QID_AC_VI) ||
  707. (queue->qid == QID_AC_BE) ||
  708. (queue->qid == QID_AC_BK);
  709. mutex_lock(&queue->status_lock);
  710. /*
  711. * If the queue has been started, we must stop it temporarily
  712. * to prevent any new frames to be queued on the device. If
  713. * we are not dropping the pending frames, the queue must
  714. * only be stopped in the software and not the hardware,
  715. * otherwise the queue will never become empty on its own.
  716. */
  717. started = test_bit(QUEUE_STARTED, &queue->flags);
  718. if (started) {
  719. /*
  720. * Pause the queue
  721. */
  722. rt2x00queue_pause_queue(queue);
  723. /*
  724. * If we are not supposed to drop any pending
  725. * frames, this means we must force a start (=kick)
  726. * to the queue to make sure the hardware will
  727. * start transmitting.
  728. */
  729. if (!drop && tx_queue)
  730. queue->rt2x00dev->ops->lib->kick_queue(queue);
  731. }
  732. /*
  733. * Check if driver supports flushing, we can only guarentee
  734. * full support for flushing if the driver is able
  735. * to cancel all pending frames (drop = true).
  736. */
  737. if (drop && queue->rt2x00dev->ops->lib->flush_queue)
  738. queue->rt2x00dev->ops->lib->flush_queue(queue);
  739. /*
  740. * When we don't want to drop any frames, or when
  741. * the driver doesn't fully flush the queue correcly,
  742. * we must wait for the queue to become empty.
  743. */
  744. for (i = 0; !rt2x00queue_empty(queue) && i < 100; i++)
  745. msleep(10);
  746. /*
  747. * The queue flush has failed...
  748. */
  749. if (unlikely(!rt2x00queue_empty(queue)))
  750. WARNING(queue->rt2x00dev, "Queue %d failed to flush\n", queue->qid);
  751. /*
  752. * Restore the queue to the previous status
  753. */
  754. if (started)
  755. rt2x00queue_unpause_queue(queue);
  756. mutex_unlock(&queue->status_lock);
  757. }
  758. EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
  759. void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
  760. {
  761. struct data_queue *queue;
  762. /*
  763. * rt2x00queue_start_queue will call ieee80211_wake_queue
  764. * for each queue after is has been properly initialized.
  765. */
  766. tx_queue_for_each(rt2x00dev, queue)
  767. rt2x00queue_start_queue(queue);
  768. rt2x00queue_start_queue(rt2x00dev->rx);
  769. }
  770. EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
  771. void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
  772. {
  773. struct data_queue *queue;
  774. /*
  775. * rt2x00queue_stop_queue will call ieee80211_stop_queue
  776. * as well, but we are completely shutting doing everything
  777. * now, so it is much safer to stop all TX queues at once,
  778. * and use rt2x00queue_stop_queue for cleaning up.
  779. */
  780. ieee80211_stop_queues(rt2x00dev->hw);
  781. tx_queue_for_each(rt2x00dev, queue)
  782. rt2x00queue_stop_queue(queue);
  783. rt2x00queue_stop_queue(rt2x00dev->rx);
  784. }
  785. EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
  786. void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
  787. {
  788. struct data_queue *queue;
  789. tx_queue_for_each(rt2x00dev, queue)
  790. rt2x00queue_flush_queue(queue, drop);
  791. rt2x00queue_flush_queue(rt2x00dev->rx, drop);
  792. }
  793. EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
  794. static void rt2x00queue_reset(struct data_queue *queue)
  795. {
  796. unsigned long irqflags;
  797. unsigned int i;
  798. spin_lock_irqsave(&queue->index_lock, irqflags);
  799. queue->count = 0;
  800. queue->length = 0;
  801. for (i = 0; i < Q_INDEX_MAX; i++) {
  802. queue->index[i] = 0;
  803. queue->last_action[i] = jiffies;
  804. }
  805. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  806. }
  807. void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
  808. {
  809. struct data_queue *queue;
  810. unsigned int i;
  811. queue_for_each(rt2x00dev, queue) {
  812. rt2x00queue_reset(queue);
  813. for (i = 0; i < queue->limit; i++)
  814. rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
  815. }
  816. }
  817. static int rt2x00queue_alloc_entries(struct data_queue *queue,
  818. const struct data_queue_desc *qdesc)
  819. {
  820. struct queue_entry *entries;
  821. unsigned int entry_size;
  822. unsigned int i;
  823. rt2x00queue_reset(queue);
  824. queue->limit = qdesc->entry_num;
  825. queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
  826. queue->data_size = qdesc->data_size;
  827. queue->desc_size = qdesc->desc_size;
  828. /*
  829. * Allocate all queue entries.
  830. */
  831. entry_size = sizeof(*entries) + qdesc->priv_size;
  832. entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
  833. if (!entries)
  834. return -ENOMEM;
  835. #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
  836. (((char *)(__base)) + ((__limit) * (__esize)) + \
  837. ((__index) * (__psize)))
  838. for (i = 0; i < queue->limit; i++) {
  839. entries[i].flags = 0;
  840. entries[i].queue = queue;
  841. entries[i].skb = NULL;
  842. entries[i].entry_idx = i;
  843. entries[i].priv_data =
  844. QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
  845. sizeof(*entries), qdesc->priv_size);
  846. }
  847. #undef QUEUE_ENTRY_PRIV_OFFSET
  848. queue->entries = entries;
  849. return 0;
  850. }
  851. static void rt2x00queue_free_skbs(struct data_queue *queue)
  852. {
  853. unsigned int i;
  854. if (!queue->entries)
  855. return;
  856. for (i = 0; i < queue->limit; i++) {
  857. rt2x00queue_free_skb(&queue->entries[i]);
  858. }
  859. }
  860. static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
  861. {
  862. unsigned int i;
  863. struct sk_buff *skb;
  864. for (i = 0; i < queue->limit; i++) {
  865. skb = rt2x00queue_alloc_rxskb(&queue->entries[i]);
  866. if (!skb)
  867. return -ENOMEM;
  868. queue->entries[i].skb = skb;
  869. }
  870. return 0;
  871. }
  872. int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
  873. {
  874. struct data_queue *queue;
  875. int status;
  876. status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
  877. if (status)
  878. goto exit;
  879. tx_queue_for_each(rt2x00dev, queue) {
  880. status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
  881. if (status)
  882. goto exit;
  883. }
  884. status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
  885. if (status)
  886. goto exit;
  887. if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) {
  888. status = rt2x00queue_alloc_entries(rt2x00dev->atim,
  889. rt2x00dev->ops->atim);
  890. if (status)
  891. goto exit;
  892. }
  893. status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
  894. if (status)
  895. goto exit;
  896. return 0;
  897. exit:
  898. ERROR(rt2x00dev, "Queue entries allocation failed.\n");
  899. rt2x00queue_uninitialize(rt2x00dev);
  900. return status;
  901. }
  902. void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
  903. {
  904. struct data_queue *queue;
  905. rt2x00queue_free_skbs(rt2x00dev->rx);
  906. queue_for_each(rt2x00dev, queue) {
  907. kfree(queue->entries);
  908. queue->entries = NULL;
  909. }
  910. }
  911. static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
  912. struct data_queue *queue, enum data_queue_qid qid)
  913. {
  914. mutex_init(&queue->status_lock);
  915. spin_lock_init(&queue->index_lock);
  916. queue->rt2x00dev = rt2x00dev;
  917. queue->qid = qid;
  918. queue->txop = 0;
  919. queue->aifs = 2;
  920. queue->cw_min = 5;
  921. queue->cw_max = 10;
  922. }
  923. int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
  924. {
  925. struct data_queue *queue;
  926. enum data_queue_qid qid;
  927. unsigned int req_atim =
  928. !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  929. /*
  930. * We need the following queues:
  931. * RX: 1
  932. * TX: ops->tx_queues
  933. * Beacon: 1
  934. * Atim: 1 (if required)
  935. */
  936. rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
  937. queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
  938. if (!queue) {
  939. ERROR(rt2x00dev, "Queue allocation failed.\n");
  940. return -ENOMEM;
  941. }
  942. /*
  943. * Initialize pointers
  944. */
  945. rt2x00dev->rx = queue;
  946. rt2x00dev->tx = &queue[1];
  947. rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
  948. rt2x00dev->atim = req_atim ? &queue[2 + rt2x00dev->ops->tx_queues] : NULL;
  949. /*
  950. * Initialize queue parameters.
  951. * RX: qid = QID_RX
  952. * TX: qid = QID_AC_VO + index
  953. * TX: cw_min: 2^5 = 32.
  954. * TX: cw_max: 2^10 = 1024.
  955. * BCN: qid = QID_BEACON
  956. * ATIM: qid = QID_ATIM
  957. */
  958. rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
  959. qid = QID_AC_VO;
  960. tx_queue_for_each(rt2x00dev, queue)
  961. rt2x00queue_init(rt2x00dev, queue, qid++);
  962. rt2x00queue_init(rt2x00dev, rt2x00dev->bcn, QID_BEACON);
  963. if (req_atim)
  964. rt2x00queue_init(rt2x00dev, rt2x00dev->atim, QID_ATIM);
  965. return 0;
  966. }
  967. void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
  968. {
  969. kfree(rt2x00dev->rx);
  970. rt2x00dev->rx = NULL;
  971. rt2x00dev->tx = NULL;
  972. rt2x00dev->bcn = NULL;
  973. }