rt2x00queue.c 34 KB

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