rt2x00queue.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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, except for a frame that has been injected
  309. * through a monitor interface. This latter is needed for testing a
  310. * monitor interface.
  311. */
  312. if ((ieee80211_is_beacon(hdr->frame_control) ||
  313. ieee80211_is_probe_resp(hdr->frame_control)) &&
  314. (!(tx_info->flags & IEEE80211_TX_CTL_INJECTED)))
  315. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
  316. /*
  317. * Determine with what IFS priority this frame should be send.
  318. * Set ifs to IFS_SIFS when the this is not the first fragment,
  319. * or this fragment came after RTS/CTS.
  320. */
  321. if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
  322. !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
  323. __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
  324. txdesc->ifs = IFS_BACKOFF;
  325. } else
  326. txdesc->ifs = IFS_SIFS;
  327. /*
  328. * Determine rate modulation.
  329. */
  330. hwrate = rt2x00_get_rate(rate->hw_value);
  331. txdesc->rate_mode = RATE_MODE_CCK;
  332. if (hwrate->flags & DEV_RATE_OFDM)
  333. txdesc->rate_mode = RATE_MODE_OFDM;
  334. /*
  335. * Apply TX descriptor handling by components
  336. */
  337. rt2x00crypto_create_tx_descriptor(entry, txdesc);
  338. rt2x00ht_create_tx_descriptor(entry, txdesc, hwrate);
  339. rt2x00queue_create_tx_descriptor_seq(entry, txdesc);
  340. rt2x00queue_create_tx_descriptor_plcp(entry, txdesc, hwrate);
  341. }
  342. static int rt2x00queue_write_tx_data(struct queue_entry *entry,
  343. struct txentry_desc *txdesc)
  344. {
  345. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  346. /*
  347. * This should not happen, we already checked the entry
  348. * was ours. When the hardware disagrees there has been
  349. * a queue corruption!
  350. */
  351. if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
  352. rt2x00dev->ops->lib->get_entry_state(entry))) {
  353. ERROR(rt2x00dev,
  354. "Corrupt queue %d, accessing entry which is not ours.\n"
  355. "Please file bug report to %s.\n",
  356. entry->queue->qid, DRV_PROJECT);
  357. return -EINVAL;
  358. }
  359. /*
  360. * Add the requested extra tx headroom in front of the skb.
  361. */
  362. skb_push(entry->skb, rt2x00dev->ops->extra_tx_headroom);
  363. memset(entry->skb->data, 0, rt2x00dev->ops->extra_tx_headroom);
  364. /*
  365. * Call the driver's write_tx_data function, if it exists.
  366. */
  367. if (rt2x00dev->ops->lib->write_tx_data)
  368. rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
  369. /*
  370. * Map the skb to DMA.
  371. */
  372. if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
  373. rt2x00queue_map_txskb(entry);
  374. return 0;
  375. }
  376. static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
  377. struct txentry_desc *txdesc)
  378. {
  379. struct data_queue *queue = entry->queue;
  380. queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
  381. /*
  382. * All processing on the frame has been completed, this means
  383. * it is now ready to be dumped to userspace through debugfs.
  384. */
  385. rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
  386. }
  387. static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
  388. struct txentry_desc *txdesc)
  389. {
  390. /*
  391. * Check if we need to kick the queue, there are however a few rules
  392. * 1) Don't kick unless this is the last in frame in a burst.
  393. * When the burst flag is set, this frame is always followed
  394. * by another frame which in some way are related to eachother.
  395. * This is true for fragments, RTS or CTS-to-self frames.
  396. * 2) Rule 1 can be broken when the available entries
  397. * in the queue are less then a certain threshold.
  398. */
  399. if (rt2x00queue_threshold(queue) ||
  400. !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
  401. queue->rt2x00dev->ops->lib->kick_queue(queue);
  402. }
  403. int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
  404. bool local)
  405. {
  406. struct ieee80211_tx_info *tx_info;
  407. struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
  408. struct txentry_desc txdesc;
  409. struct skb_frame_desc *skbdesc;
  410. u8 rate_idx, rate_flags;
  411. if (unlikely(rt2x00queue_full(queue)))
  412. return -ENOBUFS;
  413. if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
  414. &entry->flags))) {
  415. ERROR(queue->rt2x00dev,
  416. "Arrived at non-free entry in the non-full queue %d.\n"
  417. "Please file bug report to %s.\n",
  418. queue->qid, DRV_PROJECT);
  419. return -EINVAL;
  420. }
  421. /*
  422. * Copy all TX descriptor information into txdesc,
  423. * after that we are free to use the skb->cb array
  424. * for our information.
  425. */
  426. entry->skb = skb;
  427. rt2x00queue_create_tx_descriptor(entry, &txdesc);
  428. /*
  429. * All information is retrieved from the skb->cb array,
  430. * now we should claim ownership of the driver part of that
  431. * array, preserving the bitrate index and flags.
  432. */
  433. tx_info = IEEE80211_SKB_CB(skb);
  434. rate_idx = tx_info->control.rates[0].idx;
  435. rate_flags = tx_info->control.rates[0].flags;
  436. skbdesc = get_skb_frame_desc(skb);
  437. memset(skbdesc, 0, sizeof(*skbdesc));
  438. skbdesc->entry = entry;
  439. skbdesc->tx_rate_idx = rate_idx;
  440. skbdesc->tx_rate_flags = rate_flags;
  441. if (local)
  442. skbdesc->flags |= SKBDESC_NOT_MAC80211;
  443. /*
  444. * When hardware encryption is supported, and this frame
  445. * is to be encrypted, we should strip the IV/EIV data from
  446. * the frame so we can provide it to the driver separately.
  447. */
  448. if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
  449. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
  450. if (test_bit(DRIVER_REQUIRE_COPY_IV, &queue->rt2x00dev->flags))
  451. rt2x00crypto_tx_copy_iv(skb, &txdesc);
  452. else
  453. rt2x00crypto_tx_remove_iv(skb, &txdesc);
  454. }
  455. /*
  456. * When DMA allocation is required we should guarentee to the
  457. * driver that the DMA is aligned to a 4-byte boundary.
  458. * However some drivers require L2 padding to pad the payload
  459. * rather then the header. This could be a requirement for
  460. * PCI and USB devices, while header alignment only is valid
  461. * for PCI devices.
  462. */
  463. if (test_bit(DRIVER_REQUIRE_L2PAD, &queue->rt2x00dev->flags))
  464. rt2x00queue_insert_l2pad(entry->skb, txdesc.header_length);
  465. else if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags))
  466. rt2x00queue_align_frame(entry->skb);
  467. /*
  468. * It could be possible that the queue was corrupted and this
  469. * call failed. Since we always return NETDEV_TX_OK to mac80211,
  470. * this frame will simply be dropped.
  471. */
  472. if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
  473. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  474. entry->skb = NULL;
  475. return -EIO;
  476. }
  477. set_bit(ENTRY_DATA_PENDING, &entry->flags);
  478. rt2x00queue_index_inc(queue, Q_INDEX);
  479. rt2x00queue_write_tx_descriptor(entry, &txdesc);
  480. rt2x00queue_kick_tx_queue(queue, &txdesc);
  481. return 0;
  482. }
  483. int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
  484. struct ieee80211_vif *vif,
  485. const bool enable_beacon)
  486. {
  487. struct rt2x00_intf *intf = vif_to_intf(vif);
  488. struct skb_frame_desc *skbdesc;
  489. struct txentry_desc txdesc;
  490. if (unlikely(!intf->beacon))
  491. return -ENOBUFS;
  492. mutex_lock(&intf->beacon_skb_mutex);
  493. /*
  494. * Clean up the beacon skb.
  495. */
  496. rt2x00queue_free_skb(intf->beacon);
  497. if (!enable_beacon) {
  498. rt2x00queue_stop_queue(intf->beacon->queue);
  499. mutex_unlock(&intf->beacon_skb_mutex);
  500. return 0;
  501. }
  502. intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
  503. if (!intf->beacon->skb) {
  504. mutex_unlock(&intf->beacon_skb_mutex);
  505. return -ENOMEM;
  506. }
  507. /*
  508. * Copy all TX descriptor information into txdesc,
  509. * after that we are free to use the skb->cb array
  510. * for our information.
  511. */
  512. rt2x00queue_create_tx_descriptor(intf->beacon, &txdesc);
  513. /*
  514. * Fill in skb descriptor
  515. */
  516. skbdesc = get_skb_frame_desc(intf->beacon->skb);
  517. memset(skbdesc, 0, sizeof(*skbdesc));
  518. skbdesc->entry = intf->beacon;
  519. /*
  520. * Send beacon to hardware and enable beacon genaration..
  521. */
  522. rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
  523. mutex_unlock(&intf->beacon_skb_mutex);
  524. return 0;
  525. }
  526. void rt2x00queue_for_each_entry(struct data_queue *queue,
  527. enum queue_index start,
  528. enum queue_index end,
  529. void (*fn)(struct queue_entry *entry))
  530. {
  531. unsigned long irqflags;
  532. unsigned int index_start;
  533. unsigned int index_end;
  534. unsigned int i;
  535. if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
  536. ERROR(queue->rt2x00dev,
  537. "Entry requested from invalid index range (%d - %d)\n",
  538. start, end);
  539. return;
  540. }
  541. /*
  542. * Only protect the range we are going to loop over,
  543. * if during our loop a extra entry is set to pending
  544. * it should not be kicked during this run, since it
  545. * is part of another TX operation.
  546. */
  547. spin_lock_irqsave(&queue->index_lock, irqflags);
  548. index_start = queue->index[start];
  549. index_end = queue->index[end];
  550. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  551. /*
  552. * Start from the TX done pointer, this guarentees that we will
  553. * send out all frames in the correct order.
  554. */
  555. if (index_start < index_end) {
  556. for (i = index_start; i < index_end; i++)
  557. fn(&queue->entries[i]);
  558. } else {
  559. for (i = index_start; i < queue->limit; i++)
  560. fn(&queue->entries[i]);
  561. for (i = 0; i < index_end; i++)
  562. fn(&queue->entries[i]);
  563. }
  564. }
  565. EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
  566. struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
  567. const enum data_queue_qid queue)
  568. {
  569. int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  570. if (queue == QID_RX)
  571. return rt2x00dev->rx;
  572. if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
  573. return &rt2x00dev->tx[queue];
  574. if (!rt2x00dev->bcn)
  575. return NULL;
  576. if (queue == QID_BEACON)
  577. return &rt2x00dev->bcn[0];
  578. else if (queue == QID_ATIM && atim)
  579. return &rt2x00dev->bcn[1];
  580. return NULL;
  581. }
  582. EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
  583. struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
  584. enum queue_index index)
  585. {
  586. struct queue_entry *entry;
  587. unsigned long irqflags;
  588. if (unlikely(index >= Q_INDEX_MAX)) {
  589. ERROR(queue->rt2x00dev,
  590. "Entry requested from invalid index type (%d)\n", index);
  591. return NULL;
  592. }
  593. spin_lock_irqsave(&queue->index_lock, irqflags);
  594. entry = &queue->entries[queue->index[index]];
  595. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  596. return entry;
  597. }
  598. EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
  599. void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
  600. {
  601. unsigned long irqflags;
  602. if (unlikely(index >= Q_INDEX_MAX)) {
  603. ERROR(queue->rt2x00dev,
  604. "Index change on invalid index type (%d)\n", index);
  605. return;
  606. }
  607. spin_lock_irqsave(&queue->index_lock, irqflags);
  608. queue->index[index]++;
  609. if (queue->index[index] >= queue->limit)
  610. queue->index[index] = 0;
  611. queue->last_action[index] = jiffies;
  612. if (index == Q_INDEX) {
  613. queue->length++;
  614. } else if (index == Q_INDEX_DONE) {
  615. queue->length--;
  616. queue->count++;
  617. }
  618. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  619. }
  620. void rt2x00queue_pause_queue(struct data_queue *queue)
  621. {
  622. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  623. !test_bit(QUEUE_STARTED, &queue->flags) ||
  624. test_and_set_bit(QUEUE_PAUSED, &queue->flags))
  625. return;
  626. switch (queue->qid) {
  627. case QID_AC_VO:
  628. case QID_AC_VI:
  629. case QID_AC_BE:
  630. case QID_AC_BK:
  631. /*
  632. * For TX queues, we have to disable the queue
  633. * inside mac80211.
  634. */
  635. ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
  636. break;
  637. default:
  638. break;
  639. }
  640. }
  641. EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
  642. void rt2x00queue_unpause_queue(struct data_queue *queue)
  643. {
  644. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  645. !test_bit(QUEUE_STARTED, &queue->flags) ||
  646. !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
  647. return;
  648. switch (queue->qid) {
  649. case QID_AC_VO:
  650. case QID_AC_VI:
  651. case QID_AC_BE:
  652. case QID_AC_BK:
  653. /*
  654. * For TX queues, we have to enable the queue
  655. * inside mac80211.
  656. */
  657. ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
  658. break;
  659. case QID_RX:
  660. /*
  661. * For RX we need to kick the queue now in order to
  662. * receive frames.
  663. */
  664. queue->rt2x00dev->ops->lib->kick_queue(queue);
  665. default:
  666. break;
  667. }
  668. }
  669. EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
  670. void rt2x00queue_start_queue(struct data_queue *queue)
  671. {
  672. mutex_lock(&queue->status_lock);
  673. if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
  674. test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
  675. mutex_unlock(&queue->status_lock);
  676. return;
  677. }
  678. set_bit(QUEUE_PAUSED, &queue->flags);
  679. queue->rt2x00dev->ops->lib->start_queue(queue);
  680. rt2x00queue_unpause_queue(queue);
  681. mutex_unlock(&queue->status_lock);
  682. }
  683. EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
  684. void rt2x00queue_stop_queue(struct data_queue *queue)
  685. {
  686. mutex_lock(&queue->status_lock);
  687. if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
  688. mutex_unlock(&queue->status_lock);
  689. return;
  690. }
  691. rt2x00queue_pause_queue(queue);
  692. queue->rt2x00dev->ops->lib->stop_queue(queue);
  693. mutex_unlock(&queue->status_lock);
  694. }
  695. EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
  696. void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
  697. {
  698. unsigned int i;
  699. bool started;
  700. bool tx_queue =
  701. (queue->qid == QID_AC_VO) ||
  702. (queue->qid == QID_AC_VI) ||
  703. (queue->qid == QID_AC_BE) ||
  704. (queue->qid == QID_AC_BK);
  705. mutex_lock(&queue->status_lock);
  706. /*
  707. * If the queue has been started, we must stop it temporarily
  708. * to prevent any new frames to be queued on the device. If
  709. * we are not dropping the pending frames, the queue must
  710. * only be stopped in the software and not the hardware,
  711. * otherwise the queue will never become empty on its own.
  712. */
  713. started = test_bit(QUEUE_STARTED, &queue->flags);
  714. if (started) {
  715. /*
  716. * Pause the queue
  717. */
  718. rt2x00queue_pause_queue(queue);
  719. /*
  720. * If we are not supposed to drop any pending
  721. * frames, this means we must force a start (=kick)
  722. * to the queue to make sure the hardware will
  723. * start transmitting.
  724. */
  725. if (!drop && tx_queue)
  726. queue->rt2x00dev->ops->lib->kick_queue(queue);
  727. }
  728. /*
  729. * Check if driver supports flushing, we can only guarentee
  730. * full support for flushing if the driver is able
  731. * to cancel all pending frames (drop = true).
  732. */
  733. if (drop && queue->rt2x00dev->ops->lib->flush_queue)
  734. queue->rt2x00dev->ops->lib->flush_queue(queue);
  735. /*
  736. * When we don't want to drop any frames, or when
  737. * the driver doesn't fully flush the queue correcly,
  738. * we must wait for the queue to become empty.
  739. */
  740. for (i = 0; !rt2x00queue_empty(queue) && i < 100; i++)
  741. msleep(10);
  742. /*
  743. * The queue flush has failed...
  744. */
  745. if (unlikely(!rt2x00queue_empty(queue)))
  746. WARNING(queue->rt2x00dev, "Queue %d failed to flush", queue->qid);
  747. /*
  748. * Restore the queue to the previous status
  749. */
  750. if (started)
  751. rt2x00queue_unpause_queue(queue);
  752. mutex_unlock(&queue->status_lock);
  753. }
  754. EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
  755. void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
  756. {
  757. struct data_queue *queue;
  758. /*
  759. * rt2x00queue_start_queue will call ieee80211_wake_queue
  760. * for each queue after is has been properly initialized.
  761. */
  762. tx_queue_for_each(rt2x00dev, queue)
  763. rt2x00queue_start_queue(queue);
  764. rt2x00queue_start_queue(rt2x00dev->rx);
  765. }
  766. EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
  767. void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
  768. {
  769. struct data_queue *queue;
  770. /*
  771. * rt2x00queue_stop_queue will call ieee80211_stop_queue
  772. * as well, but we are completely shutting doing everything
  773. * now, so it is much safer to stop all TX queues at once,
  774. * and use rt2x00queue_stop_queue for cleaning up.
  775. */
  776. ieee80211_stop_queues(rt2x00dev->hw);
  777. tx_queue_for_each(rt2x00dev, queue)
  778. rt2x00queue_stop_queue(queue);
  779. rt2x00queue_stop_queue(rt2x00dev->rx);
  780. }
  781. EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
  782. void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
  783. {
  784. struct data_queue *queue;
  785. tx_queue_for_each(rt2x00dev, queue)
  786. rt2x00queue_flush_queue(queue, drop);
  787. rt2x00queue_flush_queue(rt2x00dev->rx, drop);
  788. }
  789. EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
  790. static void rt2x00queue_reset(struct data_queue *queue)
  791. {
  792. unsigned long irqflags;
  793. unsigned int i;
  794. spin_lock_irqsave(&queue->index_lock, irqflags);
  795. queue->count = 0;
  796. queue->length = 0;
  797. for (i = 0; i < Q_INDEX_MAX; i++) {
  798. queue->index[i] = 0;
  799. queue->last_action[i] = jiffies;
  800. }
  801. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  802. }
  803. void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
  804. {
  805. struct data_queue *queue;
  806. unsigned int i;
  807. queue_for_each(rt2x00dev, queue) {
  808. rt2x00queue_reset(queue);
  809. for (i = 0; i < queue->limit; i++)
  810. rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
  811. }
  812. }
  813. static int rt2x00queue_alloc_entries(struct data_queue *queue,
  814. const struct data_queue_desc *qdesc)
  815. {
  816. struct queue_entry *entries;
  817. unsigned int entry_size;
  818. unsigned int i;
  819. rt2x00queue_reset(queue);
  820. queue->limit = qdesc->entry_num;
  821. queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
  822. queue->data_size = qdesc->data_size;
  823. queue->desc_size = qdesc->desc_size;
  824. /*
  825. * Allocate all queue entries.
  826. */
  827. entry_size = sizeof(*entries) + qdesc->priv_size;
  828. entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
  829. if (!entries)
  830. return -ENOMEM;
  831. #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
  832. (((char *)(__base)) + ((__limit) * (__esize)) + \
  833. ((__index) * (__psize)))
  834. for (i = 0; i < queue->limit; i++) {
  835. entries[i].flags = 0;
  836. entries[i].queue = queue;
  837. entries[i].skb = NULL;
  838. entries[i].entry_idx = i;
  839. entries[i].priv_data =
  840. QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
  841. sizeof(*entries), qdesc->priv_size);
  842. }
  843. #undef QUEUE_ENTRY_PRIV_OFFSET
  844. queue->entries = entries;
  845. return 0;
  846. }
  847. static void rt2x00queue_free_skbs(struct data_queue *queue)
  848. {
  849. unsigned int i;
  850. if (!queue->entries)
  851. return;
  852. for (i = 0; i < queue->limit; i++) {
  853. rt2x00queue_free_skb(&queue->entries[i]);
  854. }
  855. }
  856. static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
  857. {
  858. unsigned int i;
  859. struct sk_buff *skb;
  860. for (i = 0; i < queue->limit; i++) {
  861. skb = rt2x00queue_alloc_rxskb(&queue->entries[i]);
  862. if (!skb)
  863. return -ENOMEM;
  864. queue->entries[i].skb = skb;
  865. }
  866. return 0;
  867. }
  868. int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
  869. {
  870. struct data_queue *queue;
  871. int status;
  872. status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
  873. if (status)
  874. goto exit;
  875. tx_queue_for_each(rt2x00dev, queue) {
  876. status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
  877. if (status)
  878. goto exit;
  879. }
  880. status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
  881. if (status)
  882. goto exit;
  883. if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) {
  884. status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
  885. rt2x00dev->ops->atim);
  886. if (status)
  887. goto exit;
  888. }
  889. status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
  890. if (status)
  891. goto exit;
  892. return 0;
  893. exit:
  894. ERROR(rt2x00dev, "Queue entries allocation failed.\n");
  895. rt2x00queue_uninitialize(rt2x00dev);
  896. return status;
  897. }
  898. void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
  899. {
  900. struct data_queue *queue;
  901. rt2x00queue_free_skbs(rt2x00dev->rx);
  902. queue_for_each(rt2x00dev, queue) {
  903. kfree(queue->entries);
  904. queue->entries = NULL;
  905. }
  906. }
  907. static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
  908. struct data_queue *queue, enum data_queue_qid qid)
  909. {
  910. mutex_init(&queue->status_lock);
  911. spin_lock_init(&queue->index_lock);
  912. queue->rt2x00dev = rt2x00dev;
  913. queue->qid = qid;
  914. queue->txop = 0;
  915. queue->aifs = 2;
  916. queue->cw_min = 5;
  917. queue->cw_max = 10;
  918. }
  919. int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
  920. {
  921. struct data_queue *queue;
  922. enum data_queue_qid qid;
  923. unsigned int req_atim =
  924. !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
  925. /*
  926. * We need the following queues:
  927. * RX: 1
  928. * TX: ops->tx_queues
  929. * Beacon: 1
  930. * Atim: 1 (if required)
  931. */
  932. rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
  933. queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
  934. if (!queue) {
  935. ERROR(rt2x00dev, "Queue allocation failed.\n");
  936. return -ENOMEM;
  937. }
  938. /*
  939. * Initialize pointers
  940. */
  941. rt2x00dev->rx = queue;
  942. rt2x00dev->tx = &queue[1];
  943. rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
  944. /*
  945. * Initialize queue parameters.
  946. * RX: qid = QID_RX
  947. * TX: qid = QID_AC_VO + index
  948. * TX: cw_min: 2^5 = 32.
  949. * TX: cw_max: 2^10 = 1024.
  950. * BCN: qid = QID_BEACON
  951. * ATIM: qid = QID_ATIM
  952. */
  953. rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
  954. qid = QID_AC_VO;
  955. tx_queue_for_each(rt2x00dev, queue)
  956. rt2x00queue_init(rt2x00dev, queue, qid++);
  957. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
  958. if (req_atim)
  959. rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
  960. return 0;
  961. }
  962. void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
  963. {
  964. kfree(rt2x00dev->rx);
  965. rt2x00dev->rx = NULL;
  966. rt2x00dev->tx = NULL;
  967. rt2x00dev->bcn = NULL;
  968. }