rt2x00queue.c 35 KB

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