rt2x00queue.c 33 KB

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