rt2x00queue.c 35 KB

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