rt2x00queue.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
  3. Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
  4. Copyright (C) 2004 - 2009 Gertjan van Wingerde <gwingerde@gmail.com>
  5. <http://rt2x00.serialmonkey.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the
  16. Free Software Foundation, Inc.,
  17. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. /*
  20. Module: rt2x00lib
  21. Abstract: rt2x00 queue specific routines.
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/dma-mapping.h>
  27. #include "rt2x00.h"
  28. #include "rt2x00lib.h"
  29. struct sk_buff *rt2x00queue_alloc_rxskb(struct queue_entry *entry)
  30. {
  31. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  32. struct sk_buff *skb;
  33. struct skb_frame_desc *skbdesc;
  34. unsigned int frame_size;
  35. unsigned int head_size = 0;
  36. unsigned int tail_size = 0;
  37. /*
  38. * The frame size includes descriptor size, because the
  39. * hardware directly receive the frame into the skbuffer.
  40. */
  41. frame_size = entry->queue->data_size + entry->queue->desc_size;
  42. /*
  43. * The payload should be aligned to a 4-byte boundary,
  44. * this means we need at least 3 bytes for moving the frame
  45. * into the correct offset.
  46. */
  47. head_size = 4;
  48. /*
  49. * For IV/EIV/ICV assembly we must make sure there is
  50. * at least 8 bytes bytes available in headroom for IV/EIV
  51. * and 8 bytes for ICV data as tailroon.
  52. */
  53. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
  54. head_size += 8;
  55. tail_size += 8;
  56. }
  57. /*
  58. * Allocate skbuffer.
  59. */
  60. skb = dev_alloc_skb(frame_size + head_size + tail_size);
  61. if (!skb)
  62. return NULL;
  63. /*
  64. * Make sure we not have a frame with the requested bytes
  65. * available in the head and tail.
  66. */
  67. skb_reserve(skb, head_size);
  68. skb_put(skb, frame_size);
  69. /*
  70. * Populate skbdesc.
  71. */
  72. skbdesc = get_skb_frame_desc(skb);
  73. memset(skbdesc, 0, sizeof(*skbdesc));
  74. skbdesc->entry = entry;
  75. if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->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_align_payload(struct sk_buff *skb, unsigned int header_length)
  127. {
  128. unsigned int frame_length = skb->len;
  129. unsigned int align = ALIGN_SIZE(skb, header_length);
  130. if (!align)
  131. return;
  132. skb_push(skb, align);
  133. memmove(skb->data, skb->data + align, frame_length);
  134. skb_trim(skb, frame_length);
  135. }
  136. void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int header_length)
  137. {
  138. unsigned int payload_length = skb->len - header_length;
  139. unsigned int header_align = ALIGN_SIZE(skb, 0);
  140. unsigned int payload_align = ALIGN_SIZE(skb, header_length);
  141. unsigned int l2pad = payload_length ? L2PAD_SIZE(header_length) : 0;
  142. /*
  143. * Adjust the header alignment if the payload needs to be moved more
  144. * than the header.
  145. */
  146. if (payload_align > header_align)
  147. header_align += 4;
  148. /* There is nothing to do if no alignment is needed */
  149. if (!header_align)
  150. return;
  151. /* Reserve the amount of space needed in front of the frame */
  152. skb_push(skb, header_align);
  153. /*
  154. * Move the header.
  155. */
  156. memmove(skb->data, skb->data + header_align, header_length);
  157. /* Move the payload, if present and if required */
  158. if (payload_length && payload_align)
  159. memmove(skb->data + header_length + l2pad,
  160. skb->data + header_length + l2pad + payload_align,
  161. payload_length);
  162. /* Trim the skb to the correct size */
  163. skb_trim(skb, header_length + l2pad + payload_length);
  164. }
  165. void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int header_length)
  166. {
  167. /*
  168. * L2 padding is only present if the skb contains more than just the
  169. * IEEE 802.11 header.
  170. */
  171. unsigned int l2pad = (skb->len > header_length) ?
  172. L2PAD_SIZE(header_length) : 0;
  173. if (!l2pad)
  174. return;
  175. memmove(skb->data + l2pad, skb->data, header_length);
  176. skb_pull(skb, l2pad);
  177. }
  178. static void rt2x00queue_create_tx_descriptor_seq(struct queue_entry *entry,
  179. struct txentry_desc *txdesc)
  180. {
  181. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  182. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  183. struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
  184. unsigned long irqflags;
  185. if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) ||
  186. unlikely(!tx_info->control.vif))
  187. return;
  188. /*
  189. * Hardware should insert sequence counter.
  190. * FIXME: We insert a software sequence counter first for
  191. * hardware that doesn't support hardware sequence counting.
  192. *
  193. * This is wrong because beacons are not getting sequence
  194. * numbers assigned properly.
  195. *
  196. * A secondary problem exists for drivers that cannot toggle
  197. * sequence counting per-frame, since those will override the
  198. * sequence counter given by mac80211.
  199. */
  200. spin_lock_irqsave(&intf->seqlock, irqflags);
  201. if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
  202. intf->seqno += 0x10;
  203. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  204. hdr->seq_ctrl |= cpu_to_le16(intf->seqno);
  205. spin_unlock_irqrestore(&intf->seqlock, irqflags);
  206. __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
  207. }
  208. static void rt2x00queue_create_tx_descriptor_plcp(struct queue_entry *entry,
  209. struct txentry_desc *txdesc,
  210. const struct rt2x00_rate *hwrate)
  211. {
  212. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  213. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  214. struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
  215. unsigned int data_length;
  216. unsigned int duration;
  217. unsigned int residual;
  218. /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
  219. data_length = entry->skb->len + 4;
  220. data_length += rt2x00crypto_tx_overhead(rt2x00dev, entry->skb);
  221. /*
  222. * PLCP setup
  223. * Length calculation depends on OFDM/CCK rate.
  224. */
  225. txdesc->signal = hwrate->plcp;
  226. txdesc->service = 0x04;
  227. if (hwrate->flags & DEV_RATE_OFDM) {
  228. txdesc->length_high = (data_length >> 6) & 0x3f;
  229. txdesc->length_low = data_length & 0x3f;
  230. } else {
  231. /*
  232. * Convert length to microseconds.
  233. */
  234. residual = GET_DURATION_RES(data_length, hwrate->bitrate);
  235. duration = GET_DURATION(data_length, hwrate->bitrate);
  236. if (residual != 0) {
  237. duration++;
  238. /*
  239. * Check if we need to set the Length Extension
  240. */
  241. if (hwrate->bitrate == 110 && residual <= 30)
  242. txdesc->service |= 0x80;
  243. }
  244. txdesc->length_high = (duration >> 8) & 0xff;
  245. txdesc->length_low = duration & 0xff;
  246. /*
  247. * When preamble is enabled we should set the
  248. * preamble bit for the signal.
  249. */
  250. if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  251. txdesc->signal |= 0x08;
  252. }
  253. }
  254. static void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
  255. struct txentry_desc *txdesc)
  256. {
  257. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  258. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  259. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
  260. struct ieee80211_rate *rate =
  261. ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
  262. const struct rt2x00_rate *hwrate;
  263. memset(txdesc, 0, sizeof(*txdesc));
  264. /*
  265. * Header and frame information.
  266. */
  267. txdesc->length = entry->skb->len;
  268. txdesc->header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  269. /*
  270. * Check whether this frame is to be acked.
  271. */
  272. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
  273. __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
  274. /*
  275. * Check if this is a RTS/CTS frame
  276. */
  277. if (ieee80211_is_rts(hdr->frame_control) ||
  278. ieee80211_is_cts(hdr->frame_control)) {
  279. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  280. if (ieee80211_is_rts(hdr->frame_control))
  281. __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
  282. else
  283. __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
  284. if (tx_info->control.rts_cts_rate_idx >= 0)
  285. rate =
  286. ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
  287. }
  288. /*
  289. * Determine retry information.
  290. */
  291. txdesc->retry_limit = tx_info->control.rates[0].count - 1;
  292. if (txdesc->retry_limit >= rt2x00dev->long_retry)
  293. __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
  294. /*
  295. * Check if more fragments are pending
  296. */
  297. if (ieee80211_has_morefrags(hdr->frame_control)) {
  298. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  299. __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
  300. }
  301. /*
  302. * Check if more frames (!= fragments) are pending
  303. */
  304. if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
  305. __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
  306. /*
  307. * Beacons and probe responses require the tsf timestamp
  308. * to be inserted into the frame.
  309. */
  310. if (ieee80211_is_beacon(hdr->frame_control) ||
  311. ieee80211_is_probe_resp(hdr->frame_control))
  312. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
  313. /*
  314. * Determine with what IFS priority this frame should be send.
  315. * Set ifs to IFS_SIFS when the this is not the first fragment,
  316. * or this fragment came after RTS/CTS.
  317. */
  318. if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
  319. !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
  320. __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
  321. txdesc->ifs = IFS_BACKOFF;
  322. } else
  323. txdesc->ifs = IFS_SIFS;
  324. /*
  325. * Determine rate modulation.
  326. */
  327. hwrate = rt2x00_get_rate(rate->hw_value);
  328. txdesc->rate_mode = RATE_MODE_CCK;
  329. if (hwrate->flags & DEV_RATE_OFDM)
  330. txdesc->rate_mode = RATE_MODE_OFDM;
  331. /*
  332. * Apply TX descriptor handling by components
  333. */
  334. rt2x00crypto_create_tx_descriptor(entry, txdesc);
  335. rt2x00ht_create_tx_descriptor(entry, txdesc, hwrate);
  336. rt2x00queue_create_tx_descriptor_seq(entry, txdesc);
  337. rt2x00queue_create_tx_descriptor_plcp(entry, txdesc, hwrate);
  338. }
  339. static int rt2x00queue_write_tx_data(struct queue_entry *entry,
  340. struct txentry_desc *txdesc)
  341. {
  342. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  343. /*
  344. * This should not happen, we already checked the entry
  345. * was ours. When the hardware disagrees there has been
  346. * a queue corruption!
  347. */
  348. if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
  349. rt2x00dev->ops->lib->get_entry_state(entry))) {
  350. ERROR(rt2x00dev,
  351. "Corrupt queue %d, accessing entry which is not ours.\n"
  352. "Please file bug report to %s.\n",
  353. entry->queue->qid, DRV_PROJECT);
  354. return -EINVAL;
  355. }
  356. /*
  357. * Add the requested extra tx headroom in front of the skb.
  358. */
  359. skb_push(entry->skb, rt2x00dev->ops->extra_tx_headroom);
  360. memset(entry->skb->data, 0, rt2x00dev->ops->extra_tx_headroom);
  361. /*
  362. * Call the driver's write_tx_data function, if it exists.
  363. */
  364. if (rt2x00dev->ops->lib->write_tx_data)
  365. rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
  366. /*
  367. * Map the skb to DMA.
  368. */
  369. if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
  370. rt2x00queue_map_txskb(entry);
  371. return 0;
  372. }
  373. static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
  374. struct txentry_desc *txdesc)
  375. {
  376. struct data_queue *queue = entry->queue;
  377. queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
  378. /*
  379. * All processing on the frame has been completed, this means
  380. * it is now ready to be dumped to userspace through debugfs.
  381. */
  382. rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
  383. }
  384. static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
  385. struct txentry_desc *txdesc)
  386. {
  387. /*
  388. * Check if we need to kick the queue, there are however a few rules
  389. * 1) Don't kick unless this is the last in frame in a burst.
  390. * When the burst flag is set, this frame is always followed
  391. * by another frame which in some way are related to eachother.
  392. * This is true for fragments, RTS or CTS-to-self frames.
  393. * 2) Rule 1 can be broken when the available entries
  394. * in the queue are less then a certain threshold.
  395. */
  396. if (rt2x00queue_threshold(queue) ||
  397. !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
  398. queue->rt2x00dev->ops->lib->kick_queue(queue);
  399. }
  400. int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
  401. bool local)
  402. {
  403. struct ieee80211_tx_info *tx_info;
  404. struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
  405. struct txentry_desc txdesc;
  406. struct skb_frame_desc *skbdesc;
  407. u8 rate_idx, rate_flags;
  408. if (unlikely(rt2x00queue_full(queue)))
  409. return -ENOBUFS;
  410. if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
  411. &entry->flags))) {
  412. ERROR(queue->rt2x00dev,
  413. "Arrived at non-free entry in the non-full queue %d.\n"
  414. "Please file bug report to %s.\n",
  415. queue->qid, DRV_PROJECT);
  416. return -EINVAL;
  417. }
  418. /*
  419. * Copy all TX descriptor information into txdesc,
  420. * after that we are free to use the skb->cb array
  421. * for our information.
  422. */
  423. entry->skb = skb;
  424. rt2x00queue_create_tx_descriptor(entry, &txdesc);
  425. /*
  426. * All information is retrieved from the skb->cb array,
  427. * now we should claim ownership of the driver part of that
  428. * array, preserving the bitrate index and flags.
  429. */
  430. tx_info = IEEE80211_SKB_CB(skb);
  431. rate_idx = tx_info->control.rates[0].idx;
  432. rate_flags = tx_info->control.rates[0].flags;
  433. skbdesc = get_skb_frame_desc(skb);
  434. memset(skbdesc, 0, sizeof(*skbdesc));
  435. skbdesc->entry = entry;
  436. skbdesc->tx_rate_idx = rate_idx;
  437. skbdesc->tx_rate_flags = rate_flags;
  438. if (local)
  439. skbdesc->flags |= SKBDESC_NOT_MAC80211;
  440. /*
  441. * When hardware encryption is supported, and this frame
  442. * is to be encrypted, we should strip the IV/EIV data from
  443. * the frame so we can provide it to the driver separately.
  444. */
  445. if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
  446. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
  447. if (test_bit(DRIVER_REQUIRE_COPY_IV, &queue->rt2x00dev->flags))
  448. rt2x00crypto_tx_copy_iv(skb, &txdesc);
  449. else
  450. rt2x00crypto_tx_remove_iv(skb, &txdesc);
  451. }
  452. /*
  453. * When DMA allocation is required we should guarentee to the
  454. * driver that the DMA is aligned to a 4-byte boundary.
  455. * However some drivers require L2 padding to pad the payload
  456. * rather then the header. This could be a requirement for
  457. * PCI and USB devices, while header alignment only is valid
  458. * for PCI devices.
  459. */
  460. if (test_bit(DRIVER_REQUIRE_L2PAD, &queue->rt2x00dev->flags))
  461. rt2x00queue_insert_l2pad(entry->skb, txdesc.header_length);
  462. else if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags))
  463. rt2x00queue_align_frame(entry->skb);
  464. /*
  465. * It could be possible that the queue was corrupted and this
  466. * call failed. Since we always return NETDEV_TX_OK to mac80211,
  467. * this frame will simply be dropped.
  468. */
  469. if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
  470. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  471. entry->skb = NULL;
  472. return -EIO;
  473. }
  474. set_bit(ENTRY_DATA_PENDING, &entry->flags);
  475. rt2x00queue_index_inc(queue, Q_INDEX);
  476. rt2x00queue_write_tx_descriptor(entry, &txdesc);
  477. rt2x00queue_kick_tx_queue(queue, &txdesc);
  478. return 0;
  479. }
  480. int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
  481. struct ieee80211_vif *vif)
  482. {
  483. struct rt2x00_intf *intf = vif_to_intf(vif);
  484. if (unlikely(!intf->beacon))
  485. return -ENOBUFS;
  486. mutex_lock(&intf->beacon_skb_mutex);
  487. /*
  488. * Clean up the beacon skb.
  489. */
  490. rt2x00queue_free_skb(intf->beacon);
  491. /*
  492. * Clear beacon (single bssid devices don't need to clear the beacon
  493. * since the beacon queue will get stopped anyway).
  494. */
  495. if (rt2x00dev->ops->lib->clear_beacon)
  496. rt2x00dev->ops->lib->clear_beacon(intf->beacon);
  497. mutex_unlock(&intf->beacon_skb_mutex);
  498. return 0;
  499. }
  500. int rt2x00queue_update_beacon_locked(struct rt2x00_dev *rt2x00dev,
  501. struct ieee80211_vif *vif)
  502. {
  503. struct rt2x00_intf *intf = vif_to_intf(vif);
  504. struct skb_frame_desc *skbdesc;
  505. struct txentry_desc txdesc;
  506. if (unlikely(!intf->beacon))
  507. return -ENOBUFS;
  508. /*
  509. * Clean up the beacon skb.
  510. */
  511. rt2x00queue_free_skb(intf->beacon);
  512. intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
  513. if (!intf->beacon->skb)
  514. return -ENOMEM;
  515. /*
  516. * Copy all TX descriptor information into txdesc,
  517. * after that we are free to use the skb->cb array
  518. * for our information.
  519. */
  520. rt2x00queue_create_tx_descriptor(intf->beacon, &txdesc);
  521. /*
  522. * Fill in skb descriptor
  523. */
  524. skbdesc = get_skb_frame_desc(intf->beacon->skb);
  525. memset(skbdesc, 0, sizeof(*skbdesc));
  526. skbdesc->entry = intf->beacon;
  527. /*
  528. * Send beacon to hardware.
  529. */
  530. rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
  531. return 0;
  532. }
  533. int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
  534. struct ieee80211_vif *vif)
  535. {
  536. struct rt2x00_intf *intf = vif_to_intf(vif);
  537. int ret;
  538. mutex_lock(&intf->beacon_skb_mutex);
  539. ret = rt2x00queue_update_beacon_locked(rt2x00dev, vif);
  540. mutex_unlock(&intf->beacon_skb_mutex);
  541. return ret;
  542. }
  543. void rt2x00queue_for_each_entry(struct data_queue *queue,
  544. enum queue_index start,
  545. enum queue_index end,
  546. void (*fn)(struct queue_entry *entry))
  547. {
  548. unsigned long irqflags;
  549. unsigned int index_start;
  550. unsigned int index_end;
  551. unsigned int i;
  552. if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
  553. ERROR(queue->rt2x00dev,
  554. "Entry requested from invalid index range (%d - %d)\n",
  555. start, end);
  556. return;
  557. }
  558. /*
  559. * Only protect the range we are going to loop over,
  560. * if during our loop a extra entry is set to pending
  561. * it should not be kicked during this run, since it
  562. * is part of another TX operation.
  563. */
  564. spin_lock_irqsave(&queue->index_lock, irqflags);
  565. index_start = queue->index[start];
  566. index_end = queue->index[end];
  567. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  568. /*
  569. * Start from the TX done pointer, this guarentees that we will
  570. * send out all frames in the correct order.
  571. */
  572. if (index_start < index_end) {
  573. for (i = index_start; i < index_end; i++)
  574. fn(&queue->entries[i]);
  575. } else {
  576. for (i = index_start; i < queue->limit; i++)
  577. fn(&queue->entries[i]);
  578. for (i = 0; i < index_end; i++)
  579. fn(&queue->entries[i]);
  580. }
  581. }
  582. EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
  583. struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
  584. const enum data_queue_qid queue)
  585. {
  586. int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  587. if (queue == QID_RX)
  588. return rt2x00dev->rx;
  589. if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
  590. return &rt2x00dev->tx[queue];
  591. if (!rt2x00dev->bcn)
  592. return NULL;
  593. if (queue == QID_BEACON)
  594. return &rt2x00dev->bcn[0];
  595. else if (queue == QID_ATIM && atim)
  596. return &rt2x00dev->bcn[1];
  597. return NULL;
  598. }
  599. EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
  600. struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
  601. enum queue_index index)
  602. {
  603. struct queue_entry *entry;
  604. unsigned long irqflags;
  605. if (unlikely(index >= Q_INDEX_MAX)) {
  606. ERROR(queue->rt2x00dev,
  607. "Entry requested from invalid index type (%d)\n", index);
  608. return NULL;
  609. }
  610. spin_lock_irqsave(&queue->index_lock, irqflags);
  611. entry = &queue->entries[queue->index[index]];
  612. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  613. return entry;
  614. }
  615. EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
  616. void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
  617. {
  618. unsigned long irqflags;
  619. if (unlikely(index >= Q_INDEX_MAX)) {
  620. ERROR(queue->rt2x00dev,
  621. "Index change on invalid index type (%d)\n", index);
  622. return;
  623. }
  624. spin_lock_irqsave(&queue->index_lock, irqflags);
  625. queue->index[index]++;
  626. if (queue->index[index] >= queue->limit)
  627. queue->index[index] = 0;
  628. queue->last_action[index] = jiffies;
  629. if (index == Q_INDEX) {
  630. queue->length++;
  631. } else if (index == Q_INDEX_DONE) {
  632. queue->length--;
  633. queue->count++;
  634. }
  635. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  636. }
  637. void rt2x00queue_pause_queue(struct data_queue *queue)
  638. {
  639. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  640. !test_bit(QUEUE_STARTED, &queue->flags) ||
  641. test_and_set_bit(QUEUE_PAUSED, &queue->flags))
  642. return;
  643. switch (queue->qid) {
  644. case QID_AC_VO:
  645. case QID_AC_VI:
  646. case QID_AC_BE:
  647. case QID_AC_BK:
  648. /*
  649. * For TX queues, we have to disable the queue
  650. * inside mac80211.
  651. */
  652. ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
  653. break;
  654. default:
  655. break;
  656. }
  657. }
  658. EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
  659. void rt2x00queue_unpause_queue(struct data_queue *queue)
  660. {
  661. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  662. !test_bit(QUEUE_STARTED, &queue->flags) ||
  663. !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
  664. return;
  665. switch (queue->qid) {
  666. case QID_AC_VO:
  667. case QID_AC_VI:
  668. case QID_AC_BE:
  669. case QID_AC_BK:
  670. /*
  671. * For TX queues, we have to enable the queue
  672. * inside mac80211.
  673. */
  674. ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
  675. break;
  676. case QID_RX:
  677. /*
  678. * For RX we need to kick the queue now in order to
  679. * receive frames.
  680. */
  681. queue->rt2x00dev->ops->lib->kick_queue(queue);
  682. default:
  683. break;
  684. }
  685. }
  686. EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
  687. void rt2x00queue_start_queue(struct data_queue *queue)
  688. {
  689. mutex_lock(&queue->status_lock);
  690. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  691. test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
  692. mutex_unlock(&queue->status_lock);
  693. return;
  694. }
  695. set_bit(QUEUE_PAUSED, &queue->flags);
  696. queue->rt2x00dev->ops->lib->start_queue(queue);
  697. rt2x00queue_unpause_queue(queue);
  698. mutex_unlock(&queue->status_lock);
  699. }
  700. EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
  701. void rt2x00queue_stop_queue(struct data_queue *queue)
  702. {
  703. mutex_lock(&queue->status_lock);
  704. if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
  705. mutex_unlock(&queue->status_lock);
  706. return;
  707. }
  708. rt2x00queue_pause_queue(queue);
  709. queue->rt2x00dev->ops->lib->stop_queue(queue);
  710. mutex_unlock(&queue->status_lock);
  711. }
  712. EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
  713. void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
  714. {
  715. unsigned int i;
  716. bool started;
  717. bool tx_queue =
  718. (queue->qid == QID_AC_VO) ||
  719. (queue->qid == QID_AC_VI) ||
  720. (queue->qid == QID_AC_BE) ||
  721. (queue->qid == QID_AC_BK);
  722. mutex_lock(&queue->status_lock);
  723. /*
  724. * If the queue has been started, we must stop it temporarily
  725. * to prevent any new frames to be queued on the device. If
  726. * we are not dropping the pending frames, the queue must
  727. * only be stopped in the software and not the hardware,
  728. * otherwise the queue will never become empty on its own.
  729. */
  730. started = test_bit(QUEUE_STARTED, &queue->flags);
  731. if (started) {
  732. /*
  733. * Pause the queue
  734. */
  735. rt2x00queue_pause_queue(queue);
  736. /*
  737. * If we are not supposed to drop any pending
  738. * frames, this means we must force a start (=kick)
  739. * to the queue to make sure the hardware will
  740. * start transmitting.
  741. */
  742. if (!drop && tx_queue)
  743. queue->rt2x00dev->ops->lib->kick_queue(queue);
  744. }
  745. /*
  746. * Check if driver supports flushing, we can only guarentee
  747. * full support for flushing if the driver is able
  748. * to cancel all pending frames (drop = true).
  749. */
  750. if (drop && queue->rt2x00dev->ops->lib->flush_queue)
  751. queue->rt2x00dev->ops->lib->flush_queue(queue);
  752. /*
  753. * When we don't want to drop any frames, or when
  754. * the driver doesn't fully flush the queue correcly,
  755. * we must wait for the queue to become empty.
  756. */
  757. for (i = 0; !rt2x00queue_empty(queue) && i < 100; i++)
  758. msleep(10);
  759. /*
  760. * The queue flush has failed...
  761. */
  762. if (unlikely(!rt2x00queue_empty(queue)))
  763. WARNING(queue->rt2x00dev, "Queue %d failed to flush\n", queue->qid);
  764. /*
  765. * Restore the queue to the previous status
  766. */
  767. if (started)
  768. rt2x00queue_unpause_queue(queue);
  769. mutex_unlock(&queue->status_lock);
  770. }
  771. EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
  772. void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
  773. {
  774. struct data_queue *queue;
  775. /*
  776. * rt2x00queue_start_queue will call ieee80211_wake_queue
  777. * for each queue after is has been properly initialized.
  778. */
  779. tx_queue_for_each(rt2x00dev, queue)
  780. rt2x00queue_start_queue(queue);
  781. rt2x00queue_start_queue(rt2x00dev->rx);
  782. }
  783. EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
  784. void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
  785. {
  786. struct data_queue *queue;
  787. /*
  788. * rt2x00queue_stop_queue will call ieee80211_stop_queue
  789. * as well, but we are completely shutting doing everything
  790. * now, so it is much safer to stop all TX queues at once,
  791. * and use rt2x00queue_stop_queue for cleaning up.
  792. */
  793. ieee80211_stop_queues(rt2x00dev->hw);
  794. tx_queue_for_each(rt2x00dev, queue)
  795. rt2x00queue_stop_queue(queue);
  796. rt2x00queue_stop_queue(rt2x00dev->rx);
  797. }
  798. EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
  799. void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
  800. {
  801. struct data_queue *queue;
  802. tx_queue_for_each(rt2x00dev, queue)
  803. rt2x00queue_flush_queue(queue, drop);
  804. rt2x00queue_flush_queue(rt2x00dev->rx, drop);
  805. }
  806. EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
  807. static void rt2x00queue_reset(struct data_queue *queue)
  808. {
  809. unsigned long irqflags;
  810. unsigned int i;
  811. spin_lock_irqsave(&queue->index_lock, irqflags);
  812. queue->count = 0;
  813. queue->length = 0;
  814. for (i = 0; i < Q_INDEX_MAX; i++) {
  815. queue->index[i] = 0;
  816. queue->last_action[i] = jiffies;
  817. }
  818. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  819. }
  820. void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
  821. {
  822. struct data_queue *queue;
  823. unsigned int i;
  824. queue_for_each(rt2x00dev, queue) {
  825. rt2x00queue_reset(queue);
  826. for (i = 0; i < queue->limit; i++)
  827. rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
  828. }
  829. }
  830. static int rt2x00queue_alloc_entries(struct data_queue *queue,
  831. const struct data_queue_desc *qdesc)
  832. {
  833. struct queue_entry *entries;
  834. unsigned int entry_size;
  835. unsigned int i;
  836. rt2x00queue_reset(queue);
  837. queue->limit = qdesc->entry_num;
  838. queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
  839. queue->data_size = qdesc->data_size;
  840. queue->desc_size = qdesc->desc_size;
  841. /*
  842. * Allocate all queue entries.
  843. */
  844. entry_size = sizeof(*entries) + qdesc->priv_size;
  845. entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
  846. if (!entries)
  847. return -ENOMEM;
  848. #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
  849. (((char *)(__base)) + ((__limit) * (__esize)) + \
  850. ((__index) * (__psize)))
  851. for (i = 0; i < queue->limit; i++) {
  852. entries[i].flags = 0;
  853. entries[i].queue = queue;
  854. entries[i].skb = NULL;
  855. entries[i].entry_idx = i;
  856. entries[i].priv_data =
  857. QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
  858. sizeof(*entries), qdesc->priv_size);
  859. }
  860. #undef QUEUE_ENTRY_PRIV_OFFSET
  861. queue->entries = entries;
  862. return 0;
  863. }
  864. static void rt2x00queue_free_skbs(struct data_queue *queue)
  865. {
  866. unsigned int i;
  867. if (!queue->entries)
  868. return;
  869. for (i = 0; i < queue->limit; i++) {
  870. rt2x00queue_free_skb(&queue->entries[i]);
  871. }
  872. }
  873. static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
  874. {
  875. unsigned int i;
  876. struct sk_buff *skb;
  877. for (i = 0; i < queue->limit; i++) {
  878. skb = rt2x00queue_alloc_rxskb(&queue->entries[i]);
  879. if (!skb)
  880. return -ENOMEM;
  881. queue->entries[i].skb = skb;
  882. }
  883. return 0;
  884. }
  885. int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
  886. {
  887. struct data_queue *queue;
  888. int status;
  889. status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
  890. if (status)
  891. goto exit;
  892. tx_queue_for_each(rt2x00dev, queue) {
  893. status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
  894. if (status)
  895. goto exit;
  896. }
  897. status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
  898. if (status)
  899. goto exit;
  900. if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) {
  901. status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
  902. rt2x00dev->ops->atim);
  903. if (status)
  904. goto exit;
  905. }
  906. status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
  907. if (status)
  908. goto exit;
  909. return 0;
  910. exit:
  911. ERROR(rt2x00dev, "Queue entries allocation failed.\n");
  912. rt2x00queue_uninitialize(rt2x00dev);
  913. return status;
  914. }
  915. void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
  916. {
  917. struct data_queue *queue;
  918. rt2x00queue_free_skbs(rt2x00dev->rx);
  919. queue_for_each(rt2x00dev, queue) {
  920. kfree(queue->entries);
  921. queue->entries = NULL;
  922. }
  923. }
  924. static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
  925. struct data_queue *queue, enum data_queue_qid qid)
  926. {
  927. mutex_init(&queue->status_lock);
  928. spin_lock_init(&queue->index_lock);
  929. queue->rt2x00dev = rt2x00dev;
  930. queue->qid = qid;
  931. queue->txop = 0;
  932. queue->aifs = 2;
  933. queue->cw_min = 5;
  934. queue->cw_max = 10;
  935. }
  936. int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
  937. {
  938. struct data_queue *queue;
  939. enum data_queue_qid qid;
  940. unsigned int req_atim =
  941. !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  942. /*
  943. * We need the following queues:
  944. * RX: 1
  945. * TX: ops->tx_queues
  946. * Beacon: 1
  947. * Atim: 1 (if required)
  948. */
  949. rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
  950. queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
  951. if (!queue) {
  952. ERROR(rt2x00dev, "Queue allocation failed.\n");
  953. return -ENOMEM;
  954. }
  955. /*
  956. * Initialize pointers
  957. */
  958. rt2x00dev->rx = queue;
  959. rt2x00dev->tx = &queue[1];
  960. rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
  961. /*
  962. * Initialize queue parameters.
  963. * RX: qid = QID_RX
  964. * TX: qid = QID_AC_VO + index
  965. * TX: cw_min: 2^5 = 32.
  966. * TX: cw_max: 2^10 = 1024.
  967. * BCN: qid = QID_BEACON
  968. * ATIM: qid = QID_ATIM
  969. */
  970. rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
  971. qid = QID_AC_VO;
  972. tx_queue_for_each(rt2x00dev, queue)
  973. rt2x00queue_init(rt2x00dev, queue, qid++);
  974. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
  975. if (req_atim)
  976. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
  977. return 0;
  978. }
  979. void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
  980. {
  981. kfree(rt2x00dev->rx);
  982. rt2x00dev->rx = NULL;
  983. rt2x00dev->tx = NULL;
  984. rt2x00dev->bcn = NULL;
  985. }