rt2x00queue.c 31 KB

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