tx.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. * Atheros CARL9170 driver
  3. *
  4. * 802.11 xmit & status routines
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, see
  21. * http://www.gnu.org/licenses/.
  22. *
  23. * This file incorporates work covered by the following copyright and
  24. * permission notice:
  25. * Copyright (c) 2007-2008 Atheros Communications, Inc.
  26. *
  27. * Permission to use, copy, modify, and/or distribute this software for any
  28. * purpose with or without fee is hereby granted, provided that the above
  29. * copyright notice and this permission notice appear in all copies.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  32. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  34. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  35. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  36. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  37. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  38. */
  39. #include <linux/init.h>
  40. #include <linux/slab.h>
  41. #include <linux/module.h>
  42. #include <linux/etherdevice.h>
  43. #include <net/mac80211.h>
  44. #include "carl9170.h"
  45. #include "hw.h"
  46. #include "cmd.h"
  47. static inline unsigned int __carl9170_get_queue(struct ar9170 *ar,
  48. unsigned int queue)
  49. {
  50. if (unlikely(modparam_noht)) {
  51. return queue;
  52. } else {
  53. /*
  54. * This is just another workaround, until
  55. * someone figures out how to get QoS and
  56. * AMPDU to play nicely together.
  57. */
  58. return 2; /* AC_BE */
  59. }
  60. }
  61. static inline unsigned int carl9170_get_queue(struct ar9170 *ar,
  62. struct sk_buff *skb)
  63. {
  64. return __carl9170_get_queue(ar, skb_get_queue_mapping(skb));
  65. }
  66. static bool is_mem_full(struct ar9170 *ar)
  67. {
  68. return (DIV_ROUND_UP(IEEE80211_MAX_FRAME_LEN, ar->fw.mem_block_size) >
  69. atomic_read(&ar->mem_free_blocks));
  70. }
  71. static void carl9170_tx_accounting(struct ar9170 *ar, struct sk_buff *skb)
  72. {
  73. int queue, i;
  74. bool mem_full;
  75. atomic_inc(&ar->tx_total_queued);
  76. queue = skb_get_queue_mapping(skb);
  77. spin_lock_bh(&ar->tx_stats_lock);
  78. /*
  79. * The driver has to accept the frame, regardless if the queue is
  80. * full to the brim, or not. We have to do the queuing internally,
  81. * since mac80211 assumes that a driver which can operate with
  82. * aggregated frames does not reject frames for this reason.
  83. */
  84. ar->tx_stats[queue].len++;
  85. ar->tx_stats[queue].count++;
  86. mem_full = is_mem_full(ar);
  87. for (i = 0; i < ar->hw->queues; i++) {
  88. if (mem_full || ar->tx_stats[i].len >= ar->tx_stats[i].limit) {
  89. ieee80211_stop_queue(ar->hw, i);
  90. ar->queue_stop_timeout[i] = jiffies;
  91. }
  92. }
  93. spin_unlock_bh(&ar->tx_stats_lock);
  94. }
  95. static void carl9170_tx_accounting_free(struct ar9170 *ar, struct sk_buff *skb)
  96. {
  97. struct ieee80211_tx_info *txinfo;
  98. int queue;
  99. txinfo = IEEE80211_SKB_CB(skb);
  100. queue = skb_get_queue_mapping(skb);
  101. spin_lock_bh(&ar->tx_stats_lock);
  102. ar->tx_stats[queue].len--;
  103. if (!is_mem_full(ar)) {
  104. unsigned int i;
  105. for (i = 0; i < ar->hw->queues; i++) {
  106. if (ar->tx_stats[i].len >= CARL9170_NUM_TX_LIMIT_SOFT)
  107. continue;
  108. if (ieee80211_queue_stopped(ar->hw, i)) {
  109. unsigned long tmp;
  110. tmp = jiffies - ar->queue_stop_timeout[i];
  111. if (tmp > ar->max_queue_stop_timeout[i])
  112. ar->max_queue_stop_timeout[i] = tmp;
  113. }
  114. ieee80211_wake_queue(ar->hw, i);
  115. }
  116. }
  117. spin_unlock_bh(&ar->tx_stats_lock);
  118. if (atomic_dec_and_test(&ar->tx_total_queued))
  119. complete(&ar->tx_flush);
  120. }
  121. static int carl9170_alloc_dev_space(struct ar9170 *ar, struct sk_buff *skb)
  122. {
  123. struct _carl9170_tx_superframe *super = (void *) skb->data;
  124. unsigned int chunks;
  125. int cookie = -1;
  126. atomic_inc(&ar->mem_allocs);
  127. chunks = DIV_ROUND_UP(skb->len, ar->fw.mem_block_size);
  128. if (unlikely(atomic_sub_return(chunks, &ar->mem_free_blocks) < 0)) {
  129. atomic_add(chunks, &ar->mem_free_blocks);
  130. return -ENOSPC;
  131. }
  132. spin_lock_bh(&ar->mem_lock);
  133. cookie = bitmap_find_free_region(ar->mem_bitmap, ar->fw.mem_blocks, 0);
  134. spin_unlock_bh(&ar->mem_lock);
  135. if (unlikely(cookie < 0)) {
  136. atomic_add(chunks, &ar->mem_free_blocks);
  137. return -ENOSPC;
  138. }
  139. super = (void *) skb->data;
  140. /*
  141. * Cookie #0 serves two special purposes:
  142. * 1. The firmware might use it generate BlockACK frames
  143. * in responds of an incoming BlockAckReqs.
  144. *
  145. * 2. Prevent double-free bugs.
  146. */
  147. super->s.cookie = (u8) cookie + 1;
  148. return 0;
  149. }
  150. static void carl9170_release_dev_space(struct ar9170 *ar, struct sk_buff *skb)
  151. {
  152. struct _carl9170_tx_superframe *super = (void *) skb->data;
  153. int cookie;
  154. /* make a local copy of the cookie */
  155. cookie = super->s.cookie;
  156. /* invalidate cookie */
  157. super->s.cookie = 0;
  158. /*
  159. * Do a out-of-bounds check on the cookie:
  160. *
  161. * * cookie "0" is reserved and won't be assigned to any
  162. * out-going frame. Internally however, it is used to
  163. * mark no longer/un-accounted frames and serves as a
  164. * cheap way of preventing frames from being freed
  165. * twice by _accident_. NB: There is a tiny race...
  166. *
  167. * * obviously, cookie number is limited by the amount
  168. * of available memory blocks, so the number can
  169. * never execeed the mem_blocks count.
  170. */
  171. if (unlikely(WARN_ON_ONCE(cookie == 0) ||
  172. WARN_ON_ONCE(cookie > ar->fw.mem_blocks)))
  173. return;
  174. atomic_add(DIV_ROUND_UP(skb->len, ar->fw.mem_block_size),
  175. &ar->mem_free_blocks);
  176. spin_lock_bh(&ar->mem_lock);
  177. bitmap_release_region(ar->mem_bitmap, cookie - 1, 0);
  178. spin_unlock_bh(&ar->mem_lock);
  179. }
  180. /* Called from any context */
  181. static void carl9170_tx_release(struct kref *ref)
  182. {
  183. struct ar9170 *ar;
  184. struct carl9170_tx_info *arinfo;
  185. struct ieee80211_tx_info *txinfo;
  186. struct sk_buff *skb;
  187. arinfo = container_of(ref, struct carl9170_tx_info, ref);
  188. txinfo = container_of((void *) arinfo, struct ieee80211_tx_info,
  189. rate_driver_data);
  190. skb = container_of((void *) txinfo, struct sk_buff, cb);
  191. ar = arinfo->ar;
  192. if (WARN_ON_ONCE(!ar))
  193. return;
  194. BUILD_BUG_ON(
  195. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len) != 23);
  196. memset(&txinfo->status.ampdu_ack_len, 0,
  197. sizeof(struct ieee80211_tx_info) -
  198. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
  199. if (atomic_read(&ar->tx_total_queued))
  200. ar->tx_schedule = true;
  201. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU) {
  202. if (!atomic_read(&ar->tx_ampdu_upload))
  203. ar->tx_ampdu_schedule = true;
  204. if (txinfo->flags & IEEE80211_TX_STAT_AMPDU) {
  205. txinfo->status.ampdu_len = txinfo->pad[0];
  206. txinfo->status.ampdu_ack_len = txinfo->pad[1];
  207. txinfo->pad[0] = txinfo->pad[1] = 0;
  208. } else if (txinfo->flags & IEEE80211_TX_STAT_ACK) {
  209. /*
  210. * drop redundant tx_status reports:
  211. *
  212. * 1. ampdu_ack_len of the final tx_status does
  213. * include the feedback of this particular frame.
  214. *
  215. * 2. tx_status_irqsafe only queues up to 128
  216. * tx feedback reports and discards the rest.
  217. *
  218. * 3. minstrel_ht is picky, it only accepts
  219. * reports of frames with the TX_STATUS_AMPDU flag.
  220. */
  221. dev_kfree_skb_any(skb);
  222. return;
  223. } else {
  224. /*
  225. * Frame has failed, but we want to keep it in
  226. * case it was lost due to a power-state
  227. * transition.
  228. */
  229. }
  230. }
  231. skb_pull(skb, sizeof(struct _carl9170_tx_superframe));
  232. ieee80211_tx_status_irqsafe(ar->hw, skb);
  233. }
  234. void carl9170_tx_get_skb(struct sk_buff *skb)
  235. {
  236. struct carl9170_tx_info *arinfo = (void *)
  237. (IEEE80211_SKB_CB(skb))->rate_driver_data;
  238. kref_get(&arinfo->ref);
  239. }
  240. int carl9170_tx_put_skb(struct sk_buff *skb)
  241. {
  242. struct carl9170_tx_info *arinfo = (void *)
  243. (IEEE80211_SKB_CB(skb))->rate_driver_data;
  244. return kref_put(&arinfo->ref, carl9170_tx_release);
  245. }
  246. /* Caller must hold the tid_info->lock & rcu_read_lock */
  247. static void carl9170_tx_shift_bm(struct ar9170 *ar,
  248. struct carl9170_sta_tid *tid_info, u16 seq)
  249. {
  250. u16 off;
  251. off = SEQ_DIFF(seq, tid_info->bsn);
  252. if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
  253. return;
  254. /*
  255. * Sanity check. For each MPDU we set the bit in bitmap and
  256. * clear it once we received the tx_status.
  257. * But if the bit is already cleared then we've been bitten
  258. * by a bug.
  259. */
  260. WARN_ON_ONCE(!test_and_clear_bit(off, tid_info->bitmap));
  261. off = SEQ_DIFF(tid_info->snx, tid_info->bsn);
  262. if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
  263. return;
  264. if (!bitmap_empty(tid_info->bitmap, off))
  265. off = find_first_bit(tid_info->bitmap, off);
  266. tid_info->bsn += off;
  267. tid_info->bsn &= 0x0fff;
  268. bitmap_shift_right(tid_info->bitmap, tid_info->bitmap,
  269. off, CARL9170_BAW_BITS);
  270. }
  271. static void carl9170_tx_status_process_ampdu(struct ar9170 *ar,
  272. struct sk_buff *skb, struct ieee80211_tx_info *txinfo)
  273. {
  274. struct _carl9170_tx_superframe *super = (void *) skb->data;
  275. struct ieee80211_hdr *hdr = (void *) super->frame_data;
  276. struct ieee80211_tx_info *tx_info;
  277. struct carl9170_tx_info *ar_info;
  278. struct carl9170_sta_info *sta_info;
  279. struct ieee80211_sta *sta;
  280. struct carl9170_sta_tid *tid_info;
  281. struct ieee80211_vif *vif;
  282. unsigned int vif_id;
  283. u8 tid;
  284. if (!(txinfo->flags & IEEE80211_TX_CTL_AMPDU) ||
  285. txinfo->flags & IEEE80211_TX_CTL_INJECTED)
  286. return;
  287. tx_info = IEEE80211_SKB_CB(skb);
  288. ar_info = (void *) tx_info->rate_driver_data;
  289. vif_id = (super->s.misc & CARL9170_TX_SUPER_MISC_VIF_ID) >>
  290. CARL9170_TX_SUPER_MISC_VIF_ID_S;
  291. if (WARN_ON_ONCE(vif_id >= AR9170_MAX_VIRTUAL_MAC))
  292. return;
  293. rcu_read_lock();
  294. vif = rcu_dereference(ar->vif_priv[vif_id].vif);
  295. if (unlikely(!vif))
  296. goto out_rcu;
  297. /*
  298. * Normally we should use wrappers like ieee80211_get_DA to get
  299. * the correct peer ieee80211_sta.
  300. *
  301. * But there is a problem with indirect traffic (broadcasts, or
  302. * data which is designated for other stations) in station mode.
  303. * The frame will be directed to the AP for distribution and not
  304. * to the actual destination.
  305. */
  306. sta = ieee80211_find_sta(vif, hdr->addr1);
  307. if (unlikely(!sta))
  308. goto out_rcu;
  309. tid = get_tid_h(hdr);
  310. sta_info = (void *) sta->drv_priv;
  311. tid_info = rcu_dereference(sta_info->agg[tid]);
  312. if (!tid_info)
  313. goto out_rcu;
  314. spin_lock_bh(&tid_info->lock);
  315. if (likely(tid_info->state >= CARL9170_TID_STATE_IDLE))
  316. carl9170_tx_shift_bm(ar, tid_info, get_seq_h(hdr));
  317. if (sta_info->stats[tid].clear) {
  318. sta_info->stats[tid].clear = false;
  319. sta_info->stats[tid].ampdu_len = 0;
  320. sta_info->stats[tid].ampdu_ack_len = 0;
  321. }
  322. sta_info->stats[tid].ampdu_len++;
  323. if (txinfo->status.rates[0].count == 1)
  324. sta_info->stats[tid].ampdu_ack_len++;
  325. if (super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_IMM_BA)) {
  326. txinfo->pad[0] = sta_info->stats[tid].ampdu_len;
  327. txinfo->pad[1] = sta_info->stats[tid].ampdu_ack_len;
  328. txinfo->flags |= IEEE80211_TX_STAT_AMPDU;
  329. sta_info->stats[tid].clear = true;
  330. }
  331. spin_unlock_bh(&tid_info->lock);
  332. out_rcu:
  333. rcu_read_unlock();
  334. }
  335. void carl9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
  336. const bool success)
  337. {
  338. struct ieee80211_tx_info *txinfo;
  339. carl9170_tx_accounting_free(ar, skb);
  340. txinfo = IEEE80211_SKB_CB(skb);
  341. if (success)
  342. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  343. else
  344. ar->tx_ack_failures++;
  345. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
  346. carl9170_tx_status_process_ampdu(ar, skb, txinfo);
  347. carl9170_tx_put_skb(skb);
  348. }
  349. /* This function may be called form any context */
  350. void carl9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb)
  351. {
  352. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  353. atomic_dec(&ar->tx_total_pending);
  354. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
  355. atomic_dec(&ar->tx_ampdu_upload);
  356. if (carl9170_tx_put_skb(skb))
  357. tasklet_hi_schedule(&ar->usb_tasklet);
  358. }
  359. static struct sk_buff *carl9170_get_queued_skb(struct ar9170 *ar, u8 cookie,
  360. struct sk_buff_head *queue)
  361. {
  362. struct sk_buff *skb;
  363. spin_lock_bh(&queue->lock);
  364. skb_queue_walk(queue, skb) {
  365. struct _carl9170_tx_superframe *txc = (void *) skb->data;
  366. if (txc->s.cookie != cookie)
  367. continue;
  368. __skb_unlink(skb, queue);
  369. spin_unlock_bh(&queue->lock);
  370. carl9170_release_dev_space(ar, skb);
  371. return skb;
  372. }
  373. spin_unlock_bh(&queue->lock);
  374. return NULL;
  375. }
  376. static void carl9170_tx_fill_rateinfo(struct ar9170 *ar, unsigned int rix,
  377. unsigned int tries, struct ieee80211_tx_info *txinfo)
  378. {
  379. unsigned int i;
  380. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  381. if (txinfo->status.rates[i].idx < 0)
  382. break;
  383. if (i == rix) {
  384. txinfo->status.rates[i].count = tries;
  385. i++;
  386. break;
  387. }
  388. }
  389. for (; i < IEEE80211_TX_MAX_RATES; i++) {
  390. txinfo->status.rates[i].idx = -1;
  391. txinfo->status.rates[i].count = 0;
  392. }
  393. }
  394. static void carl9170_check_queue_stop_timeout(struct ar9170 *ar)
  395. {
  396. int i;
  397. struct sk_buff *skb;
  398. struct ieee80211_tx_info *txinfo;
  399. struct carl9170_tx_info *arinfo;
  400. bool restart = false;
  401. for (i = 0; i < ar->hw->queues; i++) {
  402. spin_lock_bh(&ar->tx_status[i].lock);
  403. skb = skb_peek(&ar->tx_status[i]);
  404. if (!skb)
  405. goto next;
  406. txinfo = IEEE80211_SKB_CB(skb);
  407. arinfo = (void *) txinfo->rate_driver_data;
  408. if (time_is_before_jiffies(arinfo->timeout +
  409. msecs_to_jiffies(CARL9170_QUEUE_STUCK_TIMEOUT)) == true)
  410. restart = true;
  411. next:
  412. spin_unlock_bh(&ar->tx_status[i].lock);
  413. }
  414. if (restart) {
  415. /*
  416. * At least one queue has been stuck for long enough.
  417. * Give the device a kick and hope it gets back to
  418. * work.
  419. *
  420. * possible reasons may include:
  421. * - frames got lost/corrupted (bad connection to the device)
  422. * - stalled rx processing/usb controller hiccups
  423. * - firmware errors/bugs
  424. * - every bug you can think of.
  425. * - all bugs you can't...
  426. * - ...
  427. */
  428. carl9170_restart(ar, CARL9170_RR_STUCK_TX);
  429. }
  430. }
  431. void carl9170_tx_janitor(struct work_struct *work)
  432. {
  433. struct ar9170 *ar = container_of(work, struct ar9170,
  434. tx_janitor.work);
  435. if (!IS_STARTED(ar))
  436. return;
  437. ar->tx_janitor_last_run = jiffies;
  438. carl9170_check_queue_stop_timeout(ar);
  439. if (!atomic_read(&ar->tx_total_queued))
  440. return;
  441. ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
  442. msecs_to_jiffies(CARL9170_TX_TIMEOUT));
  443. }
  444. static void __carl9170_tx_process_status(struct ar9170 *ar,
  445. const uint8_t cookie, const uint8_t info)
  446. {
  447. struct sk_buff *skb;
  448. struct ieee80211_tx_info *txinfo;
  449. struct carl9170_tx_info *arinfo;
  450. unsigned int r, t, q;
  451. bool success = true;
  452. q = ar9170_qmap[info & CARL9170_TX_STATUS_QUEUE];
  453. skb = carl9170_get_queued_skb(ar, cookie, &ar->tx_status[q]);
  454. if (!skb) {
  455. /*
  456. * We have lost the race to another thread.
  457. */
  458. return ;
  459. }
  460. txinfo = IEEE80211_SKB_CB(skb);
  461. arinfo = (void *) txinfo->rate_driver_data;
  462. if (!(info & CARL9170_TX_STATUS_SUCCESS))
  463. success = false;
  464. r = (info & CARL9170_TX_STATUS_RIX) >> CARL9170_TX_STATUS_RIX_S;
  465. t = (info & CARL9170_TX_STATUS_TRIES) >> CARL9170_TX_STATUS_TRIES_S;
  466. carl9170_tx_fill_rateinfo(ar, r, t, txinfo);
  467. carl9170_tx_status(ar, skb, success);
  468. }
  469. void carl9170_tx_process_status(struct ar9170 *ar,
  470. const struct carl9170_rsp *cmd)
  471. {
  472. unsigned int i;
  473. for (i = 0; i < cmd->hdr.ext; i++) {
  474. if (WARN_ON(i > ((cmd->hdr.len / 2) + 1))) {
  475. print_hex_dump_bytes("UU:", DUMP_PREFIX_NONE,
  476. (void *) cmd, cmd->hdr.len + 4);
  477. break;
  478. }
  479. __carl9170_tx_process_status(ar, cmd->_tx_status[i].cookie,
  480. cmd->_tx_status[i].info);
  481. }
  482. }
  483. static __le32 carl9170_tx_physet(struct ar9170 *ar,
  484. struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate)
  485. {
  486. struct ieee80211_rate *rate = NULL;
  487. u32 power, chains;
  488. __le32 tmp;
  489. tmp = cpu_to_le32(0);
  490. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  491. tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ <<
  492. AR9170_TX_PHY_BW_S);
  493. /* this works because 40 MHz is 2 and dup is 3 */
  494. if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
  495. tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP <<
  496. AR9170_TX_PHY_BW_S);
  497. if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
  498. tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
  499. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  500. u32 r = txrate->idx;
  501. u8 *txpower;
  502. /* heavy clip control */
  503. tmp |= cpu_to_le32((r & 0x7) <<
  504. AR9170_TX_PHY_TX_HEAVY_CLIP_S);
  505. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  506. if (info->band == IEEE80211_BAND_5GHZ)
  507. txpower = ar->power_5G_ht40;
  508. else
  509. txpower = ar->power_2G_ht40;
  510. } else {
  511. if (info->band == IEEE80211_BAND_5GHZ)
  512. txpower = ar->power_5G_ht20;
  513. else
  514. txpower = ar->power_2G_ht20;
  515. }
  516. power = txpower[r & 7];
  517. /* +1 dBm for HT40 */
  518. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  519. power += 2;
  520. r <<= AR9170_TX_PHY_MCS_S;
  521. BUG_ON(r & ~AR9170_TX_PHY_MCS);
  522. tmp |= cpu_to_le32(r & AR9170_TX_PHY_MCS);
  523. tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
  524. /*
  525. * green field preamble does not work.
  526. *
  527. * if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  528. * tmp |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
  529. */
  530. } else {
  531. u8 *txpower;
  532. u32 mod;
  533. u32 phyrate;
  534. u8 idx = txrate->idx;
  535. if (info->band != IEEE80211_BAND_2GHZ) {
  536. idx += 4;
  537. txpower = ar->power_5G_leg;
  538. mod = AR9170_TX_PHY_MOD_OFDM;
  539. } else {
  540. if (idx < 4) {
  541. txpower = ar->power_2G_cck;
  542. mod = AR9170_TX_PHY_MOD_CCK;
  543. } else {
  544. mod = AR9170_TX_PHY_MOD_OFDM;
  545. txpower = ar->power_2G_ofdm;
  546. }
  547. }
  548. rate = &__carl9170_ratetable[idx];
  549. phyrate = rate->hw_value & 0xF;
  550. power = txpower[(rate->hw_value & 0x30) >> 4];
  551. phyrate <<= AR9170_TX_PHY_MCS_S;
  552. tmp |= cpu_to_le32(mod);
  553. tmp |= cpu_to_le32(phyrate);
  554. /*
  555. * short preamble seems to be broken too.
  556. *
  557. * if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  558. * tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
  559. */
  560. }
  561. power <<= AR9170_TX_PHY_TX_PWR_S;
  562. power &= AR9170_TX_PHY_TX_PWR;
  563. tmp |= cpu_to_le32(power);
  564. /* set TX chains */
  565. if (ar->eeprom.tx_mask == 1) {
  566. chains = AR9170_TX_PHY_TXCHAIN_1;
  567. } else {
  568. chains = AR9170_TX_PHY_TXCHAIN_2;
  569. /* >= 36M legacy OFDM - use only one chain */
  570. if (rate && rate->bitrate >= 360 &&
  571. !(txrate->flags & IEEE80211_TX_RC_MCS))
  572. chains = AR9170_TX_PHY_TXCHAIN_1;
  573. }
  574. tmp |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_S);
  575. return tmp;
  576. }
  577. static bool carl9170_tx_rts_check(struct ar9170 *ar,
  578. struct ieee80211_tx_rate *rate,
  579. bool ampdu, bool multi)
  580. {
  581. switch (ar->erp_mode) {
  582. case CARL9170_ERP_AUTO:
  583. if (ampdu)
  584. break;
  585. case CARL9170_ERP_MAC80211:
  586. if (!(rate->flags & IEEE80211_TX_RC_USE_RTS_CTS))
  587. break;
  588. case CARL9170_ERP_RTS:
  589. if (likely(!multi))
  590. return true;
  591. default:
  592. break;
  593. }
  594. return false;
  595. }
  596. static bool carl9170_tx_cts_check(struct ar9170 *ar,
  597. struct ieee80211_tx_rate *rate)
  598. {
  599. switch (ar->erp_mode) {
  600. case CARL9170_ERP_AUTO:
  601. case CARL9170_ERP_MAC80211:
  602. if (!(rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
  603. break;
  604. case CARL9170_ERP_CTS:
  605. return true;
  606. default:
  607. break;
  608. }
  609. return false;
  610. }
  611. static int carl9170_tx_prepare(struct ar9170 *ar, struct sk_buff *skb)
  612. {
  613. struct ieee80211_hdr *hdr;
  614. struct _carl9170_tx_superframe *txc;
  615. struct carl9170_vif_info *cvif;
  616. struct ieee80211_tx_info *info;
  617. struct ieee80211_tx_rate *txrate;
  618. struct ieee80211_sta *sta;
  619. struct carl9170_tx_info *arinfo;
  620. unsigned int hw_queue;
  621. int i;
  622. u16 keytype = 0;
  623. u16 len, icv = 0;
  624. bool ampdu, no_ack;
  625. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  626. BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc) !=
  627. CARL9170_TX_SUPERDESC_LEN);
  628. BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc) !=
  629. AR9170_TX_HWDESC_LEN);
  630. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES < CARL9170_TX_MAX_RATES);
  631. hw_queue = ar9170_qmap[carl9170_get_queue(ar, skb)];
  632. hdr = (void *)skb->data;
  633. info = IEEE80211_SKB_CB(skb);
  634. len = skb->len;
  635. /*
  636. * Note: If the frame was sent through a monitor interface,
  637. * the ieee80211_vif pointer can be NULL.
  638. */
  639. if (likely(info->control.vif))
  640. cvif = (void *) info->control.vif->drv_priv;
  641. else
  642. cvif = NULL;
  643. sta = info->control.sta;
  644. txc = (void *)skb_push(skb, sizeof(*txc));
  645. memset(txc, 0, sizeof(*txc));
  646. ampdu = !!(info->flags & IEEE80211_TX_CTL_AMPDU);
  647. no_ack = !!(info->flags & IEEE80211_TX_CTL_NO_ACK);
  648. if (info->control.hw_key) {
  649. icv = info->control.hw_key->icv_len;
  650. switch (info->control.hw_key->cipher) {
  651. case WLAN_CIPHER_SUITE_WEP40:
  652. case WLAN_CIPHER_SUITE_WEP104:
  653. case WLAN_CIPHER_SUITE_TKIP:
  654. keytype = AR9170_TX_MAC_ENCR_RC4;
  655. break;
  656. case WLAN_CIPHER_SUITE_CCMP:
  657. keytype = AR9170_TX_MAC_ENCR_AES;
  658. break;
  659. default:
  660. WARN_ON(1);
  661. goto err_out;
  662. }
  663. }
  664. BUILD_BUG_ON(AR9170_MAX_VIRTUAL_MAC >
  665. ((CARL9170_TX_SUPER_MISC_VIF_ID >>
  666. CARL9170_TX_SUPER_MISC_VIF_ID_S) + 1));
  667. txc->s.len = cpu_to_le16(len + sizeof(*txc));
  668. txc->f.length = cpu_to_le16(len + icv + 4);
  669. SET_VAL(CARL9170_TX_SUPER_MISC_VIF_ID, txc->s.misc,
  670. cvif ? cvif->id : 0);
  671. txc->f.mac_control = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
  672. AR9170_TX_MAC_BACKOFF);
  673. SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, txc->s.misc, hw_queue);
  674. txc->f.mac_control |= cpu_to_le16(hw_queue << AR9170_TX_MAC_QOS_S);
  675. txc->f.mac_control |= cpu_to_le16(keytype);
  676. txc->f.phy_control = cpu_to_le32(0);
  677. if (no_ack)
  678. txc->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
  679. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
  680. txc->s.misc |= CARL9170_TX_SUPER_MISC_CAB;
  681. txrate = &info->control.rates[0];
  682. if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
  683. txc->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
  684. else if (carl9170_tx_cts_check(ar, txrate))
  685. txc->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
  686. SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[0], txrate->count);
  687. txc->f.phy_control |= carl9170_tx_physet(ar, info, txrate);
  688. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  689. for (i = 1; i < CARL9170_TX_MAX_RATES; i++) {
  690. txrate = &info->control.rates[i];
  691. if (txrate->idx >= 0)
  692. continue;
  693. txrate->idx = 0;
  694. txrate->count = ar->hw->max_rate_tries;
  695. }
  696. }
  697. /*
  698. * NOTE: For the first rate, the ERP & AMPDU flags are directly
  699. * taken from mac_control. For all fallback rate, the firmware
  700. * updates the mac_control flags from the rate info field.
  701. */
  702. for (i = 1; i < CARL9170_TX_MAX_RATES; i++) {
  703. txrate = &info->control.rates[i];
  704. if (txrate->idx < 0)
  705. break;
  706. SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[i],
  707. txrate->count);
  708. if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
  709. txc->s.ri[i] |= (AR9170_TX_MAC_PROT_RTS <<
  710. CARL9170_TX_SUPER_RI_ERP_PROT_S);
  711. else if (carl9170_tx_cts_check(ar, txrate))
  712. txc->s.ri[i] |= (AR9170_TX_MAC_PROT_CTS <<
  713. CARL9170_TX_SUPER_RI_ERP_PROT_S);
  714. /*
  715. * unaggregated fallback, in case aggregation
  716. * proves to be unsuccessful and unreliable.
  717. */
  718. if (ampdu && i < 3)
  719. txc->s.ri[i] |= CARL9170_TX_SUPER_RI_AMPDU;
  720. txc->s.rr[i - 1] = carl9170_tx_physet(ar, info, txrate);
  721. }
  722. if (ieee80211_is_probe_resp(hdr->frame_control))
  723. txc->s.misc |= CARL9170_TX_SUPER_MISC_FILL_IN_TSF;
  724. if (ampdu) {
  725. unsigned int density, factor;
  726. if (unlikely(!sta || !cvif))
  727. goto err_out;
  728. density = info->control.sta->ht_cap.ampdu_density;
  729. factor = info->control.sta->ht_cap.ampdu_factor;
  730. if (density) {
  731. /*
  732. * Watch out!
  733. *
  734. * Otus uses slightly different density values than
  735. * those from the 802.11n spec.
  736. */
  737. density = max_t(unsigned int, density + 1, 7u);
  738. }
  739. factor = min_t(unsigned int, 1u, factor);
  740. SET_VAL(CARL9170_TX_SUPER_AMPDU_DENSITY,
  741. txc->s.ampdu_settings, density);
  742. SET_VAL(CARL9170_TX_SUPER_AMPDU_FACTOR,
  743. txc->s.ampdu_settings, factor);
  744. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS) {
  745. txc->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_AGGR);
  746. } else {
  747. /*
  748. * Not sure if it's even possible to aggregate
  749. * non-ht rates with this HW.
  750. */
  751. WARN_ON_ONCE(1);
  752. }
  753. }
  754. arinfo = (void *)info->rate_driver_data;
  755. arinfo->timeout = jiffies;
  756. arinfo->ar = ar;
  757. kref_init(&arinfo->ref);
  758. return 0;
  759. err_out:
  760. skb_pull(skb, sizeof(*txc));
  761. return -EINVAL;
  762. }
  763. static void carl9170_set_immba(struct ar9170 *ar, struct sk_buff *skb)
  764. {
  765. struct _carl9170_tx_superframe *super;
  766. super = (void *) skb->data;
  767. super->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_BA);
  768. }
  769. static void carl9170_set_ampdu_params(struct ar9170 *ar, struct sk_buff *skb)
  770. {
  771. struct _carl9170_tx_superframe *super;
  772. int tmp;
  773. super = (void *) skb->data;
  774. tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_DENSITY) <<
  775. CARL9170_TX_SUPER_AMPDU_DENSITY_S;
  776. /*
  777. * If you haven't noticed carl9170_tx_prepare has already filled
  778. * in all ampdu spacing & factor parameters.
  779. * Now it's the time to check whenever the settings have to be
  780. * updated by the firmware, or if everything is still the same.
  781. *
  782. * There's no sane way to handle different density values with
  783. * this hardware, so we may as well just do the compare in the
  784. * driver.
  785. */
  786. if (tmp != ar->current_density) {
  787. ar->current_density = tmp;
  788. super->s.ampdu_settings |=
  789. CARL9170_TX_SUPER_AMPDU_COMMIT_DENSITY;
  790. }
  791. tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_FACTOR) <<
  792. CARL9170_TX_SUPER_AMPDU_FACTOR_S;
  793. if (tmp != ar->current_factor) {
  794. ar->current_factor = tmp;
  795. super->s.ampdu_settings |=
  796. CARL9170_TX_SUPER_AMPDU_COMMIT_FACTOR;
  797. }
  798. }
  799. static bool carl9170_tx_rate_check(struct ar9170 *ar, struct sk_buff *_dest,
  800. struct sk_buff *_src)
  801. {
  802. struct _carl9170_tx_superframe *dest, *src;
  803. dest = (void *) _dest->data;
  804. src = (void *) _src->data;
  805. /*
  806. * The mac80211 rate control algorithm expects that all MPDUs in
  807. * an AMPDU share the same tx vectors.
  808. * This is not really obvious right now, because the hardware
  809. * does the AMPDU setup according to its own rulebook.
  810. * Our nicely assembled, strictly monotonic increasing mpdu
  811. * chains will be broken up, mashed back together...
  812. */
  813. return (dest->f.phy_control == src->f.phy_control);
  814. }
  815. static void carl9170_tx_ampdu(struct ar9170 *ar)
  816. {
  817. struct sk_buff_head agg;
  818. struct carl9170_sta_tid *tid_info;
  819. struct sk_buff *skb, *first;
  820. unsigned int i = 0, done_ampdus = 0;
  821. u16 seq, queue, tmpssn;
  822. atomic_inc(&ar->tx_ampdu_scheduler);
  823. ar->tx_ampdu_schedule = false;
  824. if (atomic_read(&ar->tx_ampdu_upload))
  825. return;
  826. if (!ar->tx_ampdu_list_len)
  827. return;
  828. __skb_queue_head_init(&agg);
  829. rcu_read_lock();
  830. tid_info = rcu_dereference(ar->tx_ampdu_iter);
  831. if (WARN_ON_ONCE(!tid_info)) {
  832. rcu_read_unlock();
  833. return;
  834. }
  835. retry:
  836. list_for_each_entry_continue_rcu(tid_info, &ar->tx_ampdu_list, list) {
  837. i++;
  838. if (tid_info->state < CARL9170_TID_STATE_PROGRESS)
  839. continue;
  840. queue = TID_TO_WME_AC(tid_info->tid);
  841. spin_lock_bh(&tid_info->lock);
  842. if (tid_info->state != CARL9170_TID_STATE_XMIT)
  843. goto processed;
  844. tid_info->counter++;
  845. first = skb_peek(&tid_info->queue);
  846. tmpssn = carl9170_get_seq(first);
  847. seq = tid_info->snx;
  848. if (unlikely(tmpssn != seq)) {
  849. tid_info->state = CARL9170_TID_STATE_IDLE;
  850. goto processed;
  851. }
  852. while ((skb = skb_peek(&tid_info->queue))) {
  853. /* strict 0, 1, ..., n - 1, n frame sequence order */
  854. if (unlikely(carl9170_get_seq(skb) != seq))
  855. break;
  856. /* don't upload more than AMPDU FACTOR allows. */
  857. if (unlikely(SEQ_DIFF(tid_info->snx, tid_info->bsn) >=
  858. (tid_info->max - 1)))
  859. break;
  860. if (!carl9170_tx_rate_check(ar, skb, first))
  861. break;
  862. atomic_inc(&ar->tx_ampdu_upload);
  863. tid_info->snx = seq = SEQ_NEXT(seq);
  864. __skb_unlink(skb, &tid_info->queue);
  865. __skb_queue_tail(&agg, skb);
  866. if (skb_queue_len(&agg) >= CARL9170_NUM_TX_AGG_MAX)
  867. break;
  868. }
  869. if (skb_queue_empty(&tid_info->queue) ||
  870. carl9170_get_seq(skb_peek(&tid_info->queue)) !=
  871. tid_info->snx) {
  872. /*
  873. * stop TID, if A-MPDU frames are still missing,
  874. * or whenever the queue is empty.
  875. */
  876. tid_info->state = CARL9170_TID_STATE_IDLE;
  877. }
  878. done_ampdus++;
  879. processed:
  880. spin_unlock_bh(&tid_info->lock);
  881. if (skb_queue_empty(&agg))
  882. continue;
  883. /* apply ampdu spacing & factor settings */
  884. carl9170_set_ampdu_params(ar, skb_peek(&agg));
  885. /* set aggregation push bit */
  886. carl9170_set_immba(ar, skb_peek_tail(&agg));
  887. spin_lock_bh(&ar->tx_pending[queue].lock);
  888. skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
  889. spin_unlock_bh(&ar->tx_pending[queue].lock);
  890. ar->tx_schedule = true;
  891. }
  892. if ((done_ampdus++ == 0) && (i++ == 0))
  893. goto retry;
  894. rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
  895. rcu_read_unlock();
  896. }
  897. static struct sk_buff *carl9170_tx_pick_skb(struct ar9170 *ar,
  898. struct sk_buff_head *queue)
  899. {
  900. struct sk_buff *skb;
  901. struct ieee80211_tx_info *info;
  902. struct carl9170_tx_info *arinfo;
  903. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  904. spin_lock_bh(&queue->lock);
  905. skb = skb_peek(queue);
  906. if (unlikely(!skb))
  907. goto err_unlock;
  908. if (carl9170_alloc_dev_space(ar, skb))
  909. goto err_unlock;
  910. __skb_unlink(skb, queue);
  911. spin_unlock_bh(&queue->lock);
  912. info = IEEE80211_SKB_CB(skb);
  913. arinfo = (void *) info->rate_driver_data;
  914. arinfo->timeout = jiffies;
  915. /*
  916. * increase ref count to "2".
  917. * Ref counting is the easiest way to solve the race between
  918. * the the urb's completion routine: carl9170_tx_callback and
  919. * wlan tx status functions: carl9170_tx_status/janitor.
  920. */
  921. carl9170_tx_get_skb(skb);
  922. return skb;
  923. err_unlock:
  924. spin_unlock_bh(&queue->lock);
  925. return NULL;
  926. }
  927. void carl9170_tx_drop(struct ar9170 *ar, struct sk_buff *skb)
  928. {
  929. struct _carl9170_tx_superframe *super;
  930. uint8_t q = 0;
  931. ar->tx_dropped++;
  932. super = (void *)skb->data;
  933. SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, q,
  934. ar9170_qmap[carl9170_get_queue(ar, skb)]);
  935. __carl9170_tx_process_status(ar, super->s.cookie, q);
  936. }
  937. static void carl9170_tx(struct ar9170 *ar)
  938. {
  939. struct sk_buff *skb;
  940. unsigned int i, q;
  941. bool schedule_garbagecollector = false;
  942. ar->tx_schedule = false;
  943. if (unlikely(!IS_STARTED(ar)))
  944. return;
  945. carl9170_usb_handle_tx_err(ar);
  946. for (i = 0; i < ar->hw->queues; i++) {
  947. while (!skb_queue_empty(&ar->tx_pending[i])) {
  948. skb = carl9170_tx_pick_skb(ar, &ar->tx_pending[i]);
  949. if (unlikely(!skb))
  950. break;
  951. atomic_inc(&ar->tx_total_pending);
  952. q = __carl9170_get_queue(ar, i);
  953. /*
  954. * NB: tx_status[i] vs. tx_status[q],
  955. * TODO: Move into pick_skb or alloc_dev_space.
  956. */
  957. skb_queue_tail(&ar->tx_status[q], skb);
  958. carl9170_usb_tx(ar, skb);
  959. schedule_garbagecollector = true;
  960. }
  961. }
  962. if (!schedule_garbagecollector)
  963. return;
  964. ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
  965. msecs_to_jiffies(CARL9170_TX_TIMEOUT));
  966. }
  967. static bool carl9170_tx_ampdu_queue(struct ar9170 *ar,
  968. struct ieee80211_sta *sta, struct sk_buff *skb)
  969. {
  970. struct carl9170_sta_info *sta_info;
  971. struct carl9170_sta_tid *agg;
  972. struct sk_buff *iter;
  973. unsigned int max;
  974. u16 tid, seq, qseq, off;
  975. bool run = false;
  976. tid = carl9170_get_tid(skb);
  977. seq = carl9170_get_seq(skb);
  978. sta_info = (void *) sta->drv_priv;
  979. rcu_read_lock();
  980. agg = rcu_dereference(sta_info->agg[tid]);
  981. max = sta_info->ampdu_max_len;
  982. if (!agg)
  983. goto err_unlock_rcu;
  984. spin_lock_bh(&agg->lock);
  985. if (unlikely(agg->state < CARL9170_TID_STATE_IDLE))
  986. goto err_unlock;
  987. /* check if sequence is within the BA window */
  988. if (unlikely(!BAW_WITHIN(agg->bsn, CARL9170_BAW_BITS, seq)))
  989. goto err_unlock;
  990. if (WARN_ON_ONCE(!BAW_WITHIN(agg->snx, CARL9170_BAW_BITS, seq)))
  991. goto err_unlock;
  992. off = SEQ_DIFF(seq, agg->bsn);
  993. if (WARN_ON_ONCE(test_and_set_bit(off, agg->bitmap)))
  994. goto err_unlock;
  995. if (likely(BAW_WITHIN(agg->hsn, CARL9170_BAW_BITS, seq))) {
  996. __skb_queue_tail(&agg->queue, skb);
  997. agg->hsn = seq;
  998. goto queued;
  999. }
  1000. skb_queue_reverse_walk(&agg->queue, iter) {
  1001. qseq = carl9170_get_seq(iter);
  1002. if (BAW_WITHIN(qseq, CARL9170_BAW_BITS, seq)) {
  1003. __skb_queue_after(&agg->queue, iter, skb);
  1004. goto queued;
  1005. }
  1006. }
  1007. __skb_queue_head(&agg->queue, skb);
  1008. queued:
  1009. if (unlikely(agg->state != CARL9170_TID_STATE_XMIT)) {
  1010. if (agg->snx == carl9170_get_seq(skb_peek(&agg->queue))) {
  1011. agg->state = CARL9170_TID_STATE_XMIT;
  1012. run = true;
  1013. }
  1014. }
  1015. spin_unlock_bh(&agg->lock);
  1016. rcu_read_unlock();
  1017. return run;
  1018. err_unlock:
  1019. spin_unlock_bh(&agg->lock);
  1020. err_unlock_rcu:
  1021. rcu_read_unlock();
  1022. carl9170_tx_status(ar, skb, false);
  1023. ar->tx_dropped++;
  1024. return false;
  1025. }
  1026. int carl9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  1027. {
  1028. struct ar9170 *ar = hw->priv;
  1029. struct ieee80211_tx_info *info;
  1030. struct ieee80211_sta *sta;
  1031. bool run;
  1032. if (unlikely(!IS_STARTED(ar)))
  1033. goto err_free;
  1034. info = IEEE80211_SKB_CB(skb);
  1035. sta = info->control.sta;
  1036. if (unlikely(carl9170_tx_prepare(ar, skb)))
  1037. goto err_free;
  1038. carl9170_tx_accounting(ar, skb);
  1039. /*
  1040. * from now on, one has to use carl9170_tx_status to free
  1041. * all ressouces which are associated with the frame.
  1042. */
  1043. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1044. if (WARN_ON_ONCE(!sta))
  1045. goto err_free;
  1046. run = carl9170_tx_ampdu_queue(ar, sta, skb);
  1047. if (run)
  1048. carl9170_tx_ampdu(ar);
  1049. } else {
  1050. unsigned int queue = skb_get_queue_mapping(skb);
  1051. skb_queue_tail(&ar->tx_pending[queue], skb);
  1052. }
  1053. carl9170_tx(ar);
  1054. return NETDEV_TX_OK;
  1055. err_free:
  1056. ar->tx_dropped++;
  1057. dev_kfree_skb_any(skb);
  1058. return NETDEV_TX_OK;
  1059. }
  1060. void carl9170_tx_scheduler(struct ar9170 *ar)
  1061. {
  1062. if (ar->tx_ampdu_schedule)
  1063. carl9170_tx_ampdu(ar);
  1064. if (ar->tx_schedule)
  1065. carl9170_tx(ar);
  1066. }