rt2x00queue.c 34 KB

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