tx.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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. struct _carl9170_tx_superframe *super;
  206. super = (void *)skb->data;
  207. txinfo->status.ampdu_len = super->s.rix;
  208. txinfo->status.ampdu_ack_len = super->s.cnt;
  209. } else if (txinfo->flags & IEEE80211_TX_STAT_ACK) {
  210. /*
  211. * drop redundant tx_status reports:
  212. *
  213. * 1. ampdu_ack_len of the final tx_status does
  214. * include the feedback of this particular frame.
  215. *
  216. * 2. tx_status_irqsafe only queues up to 128
  217. * tx feedback reports and discards the rest.
  218. *
  219. * 3. minstrel_ht is picky, it only accepts
  220. * reports of frames with the TX_STATUS_AMPDU flag.
  221. */
  222. dev_kfree_skb_any(skb);
  223. return;
  224. } else {
  225. /*
  226. * Frame has failed, but we want to keep it in
  227. * case it was lost due to a power-state
  228. * transition.
  229. */
  230. }
  231. }
  232. skb_pull(skb, sizeof(struct _carl9170_tx_superframe));
  233. ieee80211_tx_status_irqsafe(ar->hw, skb);
  234. }
  235. void carl9170_tx_get_skb(struct sk_buff *skb)
  236. {
  237. struct carl9170_tx_info *arinfo = (void *)
  238. (IEEE80211_SKB_CB(skb))->rate_driver_data;
  239. kref_get(&arinfo->ref);
  240. }
  241. int carl9170_tx_put_skb(struct sk_buff *skb)
  242. {
  243. struct carl9170_tx_info *arinfo = (void *)
  244. (IEEE80211_SKB_CB(skb))->rate_driver_data;
  245. return kref_put(&arinfo->ref, carl9170_tx_release);
  246. }
  247. /* Caller must hold the tid_info->lock & rcu_read_lock */
  248. static void carl9170_tx_shift_bm(struct ar9170 *ar,
  249. struct carl9170_sta_tid *tid_info, u16 seq)
  250. {
  251. u16 off;
  252. off = SEQ_DIFF(seq, tid_info->bsn);
  253. if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
  254. return;
  255. /*
  256. * Sanity check. For each MPDU we set the bit in bitmap and
  257. * clear it once we received the tx_status.
  258. * But if the bit is already cleared then we've been bitten
  259. * by a bug.
  260. */
  261. WARN_ON_ONCE(!test_and_clear_bit(off, tid_info->bitmap));
  262. off = SEQ_DIFF(tid_info->snx, tid_info->bsn);
  263. if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
  264. return;
  265. if (!bitmap_empty(tid_info->bitmap, off))
  266. off = find_first_bit(tid_info->bitmap, off);
  267. tid_info->bsn += off;
  268. tid_info->bsn &= 0x0fff;
  269. bitmap_shift_right(tid_info->bitmap, tid_info->bitmap,
  270. off, CARL9170_BAW_BITS);
  271. }
  272. static void carl9170_tx_status_process_ampdu(struct ar9170 *ar,
  273. struct sk_buff *skb, struct ieee80211_tx_info *txinfo)
  274. {
  275. struct _carl9170_tx_superframe *super = (void *) skb->data;
  276. struct ieee80211_hdr *hdr = (void *) super->frame_data;
  277. struct ieee80211_tx_info *tx_info;
  278. struct carl9170_tx_info *ar_info;
  279. struct carl9170_sta_info *sta_info;
  280. struct ieee80211_sta *sta;
  281. struct carl9170_sta_tid *tid_info;
  282. struct ieee80211_vif *vif;
  283. unsigned int vif_id;
  284. u8 tid;
  285. if (!(txinfo->flags & IEEE80211_TX_CTL_AMPDU) ||
  286. txinfo->flags & IEEE80211_TX_CTL_INJECTED ||
  287. (!(super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_AGGR))))
  288. return;
  289. tx_info = IEEE80211_SKB_CB(skb);
  290. ar_info = (void *) tx_info->rate_driver_data;
  291. vif_id = (super->s.misc & CARL9170_TX_SUPER_MISC_VIF_ID) >>
  292. CARL9170_TX_SUPER_MISC_VIF_ID_S;
  293. if (WARN_ON_ONCE(vif_id >= AR9170_MAX_VIRTUAL_MAC))
  294. return;
  295. rcu_read_lock();
  296. vif = rcu_dereference(ar->vif_priv[vif_id].vif);
  297. if (unlikely(!vif))
  298. goto out_rcu;
  299. /*
  300. * Normally we should use wrappers like ieee80211_get_DA to get
  301. * the correct peer ieee80211_sta.
  302. *
  303. * But there is a problem with indirect traffic (broadcasts, or
  304. * data which is designated for other stations) in station mode.
  305. * The frame will be directed to the AP for distribution and not
  306. * to the actual destination.
  307. */
  308. sta = ieee80211_find_sta(vif, hdr->addr1);
  309. if (unlikely(!sta))
  310. goto out_rcu;
  311. tid = get_tid_h(hdr);
  312. sta_info = (void *) sta->drv_priv;
  313. tid_info = rcu_dereference(sta_info->agg[tid]);
  314. if (!tid_info)
  315. goto out_rcu;
  316. spin_lock_bh(&tid_info->lock);
  317. if (likely(tid_info->state >= CARL9170_TID_STATE_IDLE))
  318. carl9170_tx_shift_bm(ar, tid_info, get_seq_h(hdr));
  319. if (sta_info->stats[tid].clear) {
  320. sta_info->stats[tid].clear = false;
  321. sta_info->stats[tid].req = false;
  322. sta_info->stats[tid].ampdu_len = 0;
  323. sta_info->stats[tid].ampdu_ack_len = 0;
  324. }
  325. sta_info->stats[tid].ampdu_len++;
  326. if (txinfo->status.rates[0].count == 1)
  327. sta_info->stats[tid].ampdu_ack_len++;
  328. if (!(txinfo->flags & IEEE80211_TX_STAT_ACK))
  329. sta_info->stats[tid].req = true;
  330. if (super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_IMM_BA)) {
  331. super->s.rix = sta_info->stats[tid].ampdu_len;
  332. super->s.cnt = sta_info->stats[tid].ampdu_ack_len;
  333. txinfo->flags |= IEEE80211_TX_STAT_AMPDU;
  334. if (sta_info->stats[tid].req)
  335. txinfo->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  336. sta_info->stats[tid].clear = true;
  337. }
  338. spin_unlock_bh(&tid_info->lock);
  339. out_rcu:
  340. rcu_read_unlock();
  341. }
  342. void carl9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
  343. const bool success)
  344. {
  345. struct ieee80211_tx_info *txinfo;
  346. carl9170_tx_accounting_free(ar, skb);
  347. txinfo = IEEE80211_SKB_CB(skb);
  348. if (success)
  349. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  350. else
  351. ar->tx_ack_failures++;
  352. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
  353. carl9170_tx_status_process_ampdu(ar, skb, txinfo);
  354. carl9170_tx_put_skb(skb);
  355. }
  356. /* This function may be called form any context */
  357. void carl9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb)
  358. {
  359. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  360. atomic_dec(&ar->tx_total_pending);
  361. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
  362. atomic_dec(&ar->tx_ampdu_upload);
  363. if (carl9170_tx_put_skb(skb))
  364. tasklet_hi_schedule(&ar->usb_tasklet);
  365. }
  366. static struct sk_buff *carl9170_get_queued_skb(struct ar9170 *ar, u8 cookie,
  367. struct sk_buff_head *queue)
  368. {
  369. struct sk_buff *skb;
  370. spin_lock_bh(&queue->lock);
  371. skb_queue_walk(queue, skb) {
  372. struct _carl9170_tx_superframe *txc = (void *) skb->data;
  373. if (txc->s.cookie != cookie)
  374. continue;
  375. __skb_unlink(skb, queue);
  376. spin_unlock_bh(&queue->lock);
  377. carl9170_release_dev_space(ar, skb);
  378. return skb;
  379. }
  380. spin_unlock_bh(&queue->lock);
  381. return NULL;
  382. }
  383. static void carl9170_tx_fill_rateinfo(struct ar9170 *ar, unsigned int rix,
  384. unsigned int tries, struct ieee80211_tx_info *txinfo)
  385. {
  386. unsigned int i;
  387. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  388. if (txinfo->status.rates[i].idx < 0)
  389. break;
  390. if (i == rix) {
  391. txinfo->status.rates[i].count = tries;
  392. i++;
  393. break;
  394. }
  395. }
  396. for (; i < IEEE80211_TX_MAX_RATES; i++) {
  397. txinfo->status.rates[i].idx = -1;
  398. txinfo->status.rates[i].count = 0;
  399. }
  400. }
  401. static void carl9170_check_queue_stop_timeout(struct ar9170 *ar)
  402. {
  403. int i;
  404. struct sk_buff *skb;
  405. struct ieee80211_tx_info *txinfo;
  406. struct carl9170_tx_info *arinfo;
  407. bool restart = false;
  408. for (i = 0; i < ar->hw->queues; i++) {
  409. spin_lock_bh(&ar->tx_status[i].lock);
  410. skb = skb_peek(&ar->tx_status[i]);
  411. if (!skb)
  412. goto next;
  413. txinfo = IEEE80211_SKB_CB(skb);
  414. arinfo = (void *) txinfo->rate_driver_data;
  415. if (time_is_before_jiffies(arinfo->timeout +
  416. msecs_to_jiffies(CARL9170_QUEUE_STUCK_TIMEOUT)) == true)
  417. restart = true;
  418. next:
  419. spin_unlock_bh(&ar->tx_status[i].lock);
  420. }
  421. if (restart) {
  422. /*
  423. * At least one queue has been stuck for long enough.
  424. * Give the device a kick and hope it gets back to
  425. * work.
  426. *
  427. * possible reasons may include:
  428. * - frames got lost/corrupted (bad connection to the device)
  429. * - stalled rx processing/usb controller hiccups
  430. * - firmware errors/bugs
  431. * - every bug you can think of.
  432. * - all bugs you can't...
  433. * - ...
  434. */
  435. carl9170_restart(ar, CARL9170_RR_STUCK_TX);
  436. }
  437. }
  438. static void carl9170_tx_ampdu_timeout(struct ar9170 *ar)
  439. {
  440. struct carl9170_sta_tid *iter;
  441. struct sk_buff *skb;
  442. struct ieee80211_tx_info *txinfo;
  443. struct carl9170_tx_info *arinfo;
  444. struct _carl9170_tx_superframe *super;
  445. struct ieee80211_sta *sta;
  446. struct ieee80211_vif *vif;
  447. struct ieee80211_hdr *hdr;
  448. unsigned int vif_id;
  449. rcu_read_lock();
  450. list_for_each_entry_rcu(iter, &ar->tx_ampdu_list, list) {
  451. if (iter->state < CARL9170_TID_STATE_IDLE)
  452. continue;
  453. spin_lock_bh(&iter->lock);
  454. skb = skb_peek(&iter->queue);
  455. if (!skb)
  456. goto unlock;
  457. txinfo = IEEE80211_SKB_CB(skb);
  458. arinfo = (void *)txinfo->rate_driver_data;
  459. if (time_is_after_jiffies(arinfo->timeout +
  460. msecs_to_jiffies(CARL9170_QUEUE_TIMEOUT)))
  461. goto unlock;
  462. super = (void *) skb->data;
  463. hdr = (void *) super->frame_data;
  464. vif_id = (super->s.misc & CARL9170_TX_SUPER_MISC_VIF_ID) >>
  465. CARL9170_TX_SUPER_MISC_VIF_ID_S;
  466. if (WARN_ON(vif_id >= AR9170_MAX_VIRTUAL_MAC))
  467. goto unlock;
  468. vif = rcu_dereference(ar->vif_priv[vif_id].vif);
  469. if (WARN_ON(!vif))
  470. goto unlock;
  471. sta = ieee80211_find_sta(vif, hdr->addr1);
  472. if (WARN_ON(!sta))
  473. goto unlock;
  474. ieee80211_stop_tx_ba_session(sta, iter->tid);
  475. unlock:
  476. spin_unlock_bh(&iter->lock);
  477. }
  478. rcu_read_unlock();
  479. }
  480. void carl9170_tx_janitor(struct work_struct *work)
  481. {
  482. struct ar9170 *ar = container_of(work, struct ar9170,
  483. tx_janitor.work);
  484. if (!IS_STARTED(ar))
  485. return;
  486. ar->tx_janitor_last_run = jiffies;
  487. carl9170_check_queue_stop_timeout(ar);
  488. carl9170_tx_ampdu_timeout(ar);
  489. if (!atomic_read(&ar->tx_total_queued))
  490. return;
  491. ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
  492. msecs_to_jiffies(CARL9170_TX_TIMEOUT));
  493. }
  494. static void __carl9170_tx_process_status(struct ar9170 *ar,
  495. const uint8_t cookie, const uint8_t info)
  496. {
  497. struct sk_buff *skb;
  498. struct ieee80211_tx_info *txinfo;
  499. struct carl9170_tx_info *arinfo;
  500. unsigned int r, t, q;
  501. bool success = true;
  502. q = ar9170_qmap[info & CARL9170_TX_STATUS_QUEUE];
  503. skb = carl9170_get_queued_skb(ar, cookie, &ar->tx_status[q]);
  504. if (!skb) {
  505. /*
  506. * We have lost the race to another thread.
  507. */
  508. return ;
  509. }
  510. txinfo = IEEE80211_SKB_CB(skb);
  511. arinfo = (void *) txinfo->rate_driver_data;
  512. if (!(info & CARL9170_TX_STATUS_SUCCESS))
  513. success = false;
  514. r = (info & CARL9170_TX_STATUS_RIX) >> CARL9170_TX_STATUS_RIX_S;
  515. t = (info & CARL9170_TX_STATUS_TRIES) >> CARL9170_TX_STATUS_TRIES_S;
  516. carl9170_tx_fill_rateinfo(ar, r, t, txinfo);
  517. carl9170_tx_status(ar, skb, success);
  518. }
  519. void carl9170_tx_process_status(struct ar9170 *ar,
  520. const struct carl9170_rsp *cmd)
  521. {
  522. unsigned int i;
  523. for (i = 0; i < cmd->hdr.ext; i++) {
  524. if (WARN_ON(i > ((cmd->hdr.len / 2) + 1))) {
  525. print_hex_dump_bytes("UU:", DUMP_PREFIX_NONE,
  526. (void *) cmd, cmd->hdr.len + 4);
  527. break;
  528. }
  529. __carl9170_tx_process_status(ar, cmd->_tx_status[i].cookie,
  530. cmd->_tx_status[i].info);
  531. }
  532. }
  533. static __le32 carl9170_tx_physet(struct ar9170 *ar,
  534. struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate)
  535. {
  536. struct ieee80211_rate *rate = NULL;
  537. u32 power, chains;
  538. __le32 tmp;
  539. tmp = cpu_to_le32(0);
  540. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  541. tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ <<
  542. AR9170_TX_PHY_BW_S);
  543. /* this works because 40 MHz is 2 and dup is 3 */
  544. if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
  545. tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP <<
  546. AR9170_TX_PHY_BW_S);
  547. if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
  548. tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
  549. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  550. u32 r = txrate->idx;
  551. u8 *txpower;
  552. /* heavy clip control */
  553. tmp |= cpu_to_le32((r & 0x7) <<
  554. AR9170_TX_PHY_TX_HEAVY_CLIP_S);
  555. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  556. if (info->band == IEEE80211_BAND_5GHZ)
  557. txpower = ar->power_5G_ht40;
  558. else
  559. txpower = ar->power_2G_ht40;
  560. } else {
  561. if (info->band == IEEE80211_BAND_5GHZ)
  562. txpower = ar->power_5G_ht20;
  563. else
  564. txpower = ar->power_2G_ht20;
  565. }
  566. power = txpower[r & 7];
  567. /* +1 dBm for HT40 */
  568. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  569. power += 2;
  570. r <<= AR9170_TX_PHY_MCS_S;
  571. BUG_ON(r & ~AR9170_TX_PHY_MCS);
  572. tmp |= cpu_to_le32(r & AR9170_TX_PHY_MCS);
  573. tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
  574. /*
  575. * green field preamble does not work.
  576. *
  577. * if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  578. * tmp |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
  579. */
  580. } else {
  581. u8 *txpower;
  582. u32 mod;
  583. u32 phyrate;
  584. u8 idx = txrate->idx;
  585. if (info->band != IEEE80211_BAND_2GHZ) {
  586. idx += 4;
  587. txpower = ar->power_5G_leg;
  588. mod = AR9170_TX_PHY_MOD_OFDM;
  589. } else {
  590. if (idx < 4) {
  591. txpower = ar->power_2G_cck;
  592. mod = AR9170_TX_PHY_MOD_CCK;
  593. } else {
  594. mod = AR9170_TX_PHY_MOD_OFDM;
  595. txpower = ar->power_2G_ofdm;
  596. }
  597. }
  598. rate = &__carl9170_ratetable[idx];
  599. phyrate = rate->hw_value & 0xF;
  600. power = txpower[(rate->hw_value & 0x30) >> 4];
  601. phyrate <<= AR9170_TX_PHY_MCS_S;
  602. tmp |= cpu_to_le32(mod);
  603. tmp |= cpu_to_le32(phyrate);
  604. /*
  605. * short preamble seems to be broken too.
  606. *
  607. * if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  608. * tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
  609. */
  610. }
  611. power <<= AR9170_TX_PHY_TX_PWR_S;
  612. power &= AR9170_TX_PHY_TX_PWR;
  613. tmp |= cpu_to_le32(power);
  614. /* set TX chains */
  615. if (ar->eeprom.tx_mask == 1) {
  616. chains = AR9170_TX_PHY_TXCHAIN_1;
  617. } else {
  618. chains = AR9170_TX_PHY_TXCHAIN_2;
  619. /* >= 36M legacy OFDM - use only one chain */
  620. if (rate && rate->bitrate >= 360 &&
  621. !(txrate->flags & IEEE80211_TX_RC_MCS))
  622. chains = AR9170_TX_PHY_TXCHAIN_1;
  623. }
  624. tmp |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_S);
  625. return tmp;
  626. }
  627. static bool carl9170_tx_rts_check(struct ar9170 *ar,
  628. struct ieee80211_tx_rate *rate,
  629. bool ampdu, bool multi)
  630. {
  631. switch (ar->erp_mode) {
  632. case CARL9170_ERP_AUTO:
  633. if (ampdu)
  634. break;
  635. case CARL9170_ERP_MAC80211:
  636. if (!(rate->flags & IEEE80211_TX_RC_USE_RTS_CTS))
  637. break;
  638. case CARL9170_ERP_RTS:
  639. if (likely(!multi))
  640. return true;
  641. default:
  642. break;
  643. }
  644. return false;
  645. }
  646. static bool carl9170_tx_cts_check(struct ar9170 *ar,
  647. struct ieee80211_tx_rate *rate)
  648. {
  649. switch (ar->erp_mode) {
  650. case CARL9170_ERP_AUTO:
  651. case CARL9170_ERP_MAC80211:
  652. if (!(rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
  653. break;
  654. case CARL9170_ERP_CTS:
  655. return true;
  656. default:
  657. break;
  658. }
  659. return false;
  660. }
  661. static int carl9170_tx_prepare(struct ar9170 *ar, struct sk_buff *skb)
  662. {
  663. struct ieee80211_hdr *hdr;
  664. struct _carl9170_tx_superframe *txc;
  665. struct carl9170_vif_info *cvif;
  666. struct ieee80211_tx_info *info;
  667. struct ieee80211_tx_rate *txrate;
  668. struct ieee80211_sta *sta;
  669. struct carl9170_tx_info *arinfo;
  670. unsigned int hw_queue;
  671. int i;
  672. __le16 mac_tmp;
  673. u16 len;
  674. bool ampdu, no_ack;
  675. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  676. BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc) !=
  677. CARL9170_TX_SUPERDESC_LEN);
  678. BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc) !=
  679. AR9170_TX_HWDESC_LEN);
  680. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES < CARL9170_TX_MAX_RATES);
  681. BUILD_BUG_ON(AR9170_MAX_VIRTUAL_MAC >
  682. ((CARL9170_TX_SUPER_MISC_VIF_ID >>
  683. CARL9170_TX_SUPER_MISC_VIF_ID_S) + 1));
  684. hw_queue = ar9170_qmap[carl9170_get_queue(ar, skb)];
  685. hdr = (void *)skb->data;
  686. info = IEEE80211_SKB_CB(skb);
  687. len = skb->len;
  688. /*
  689. * Note: If the frame was sent through a monitor interface,
  690. * the ieee80211_vif pointer can be NULL.
  691. */
  692. if (likely(info->control.vif))
  693. cvif = (void *) info->control.vif->drv_priv;
  694. else
  695. cvif = NULL;
  696. sta = info->control.sta;
  697. txc = (void *)skb_push(skb, sizeof(*txc));
  698. memset(txc, 0, sizeof(*txc));
  699. SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, txc->s.misc, hw_queue);
  700. if (likely(cvif))
  701. SET_VAL(CARL9170_TX_SUPER_MISC_VIF_ID, txc->s.misc, cvif->id);
  702. if (unlikely(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM))
  703. txc->s.misc |= CARL9170_TX_SUPER_MISC_CAB;
  704. if (unlikely(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
  705. txc->s.misc |= CARL9170_TX_SUPER_MISC_ASSIGN_SEQ;
  706. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control)))
  707. txc->s.misc |= CARL9170_TX_SUPER_MISC_FILL_IN_TSF;
  708. mac_tmp = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
  709. AR9170_TX_MAC_BACKOFF);
  710. mac_tmp |= cpu_to_le16((hw_queue << AR9170_TX_MAC_QOS_S) &
  711. AR9170_TX_MAC_QOS);
  712. no_ack = !!(info->flags & IEEE80211_TX_CTL_NO_ACK);
  713. if (unlikely(no_ack))
  714. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
  715. if (info->control.hw_key) {
  716. len += info->control.hw_key->icv_len;
  717. switch (info->control.hw_key->cipher) {
  718. case WLAN_CIPHER_SUITE_WEP40:
  719. case WLAN_CIPHER_SUITE_WEP104:
  720. case WLAN_CIPHER_SUITE_TKIP:
  721. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_RC4);
  722. break;
  723. case WLAN_CIPHER_SUITE_CCMP:
  724. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_AES);
  725. break;
  726. default:
  727. WARN_ON(1);
  728. goto err_out;
  729. }
  730. }
  731. ampdu = !!(info->flags & IEEE80211_TX_CTL_AMPDU);
  732. if (ampdu) {
  733. unsigned int density, factor;
  734. if (unlikely(!sta || !cvif))
  735. goto err_out;
  736. factor = min_t(unsigned int, 1u, sta->ht_cap.ampdu_factor);
  737. density = sta->ht_cap.ampdu_density;
  738. if (density) {
  739. /*
  740. * Watch out!
  741. *
  742. * Otus uses slightly different density values than
  743. * those from the 802.11n spec.
  744. */
  745. density = max_t(unsigned int, density + 1, 7u);
  746. }
  747. SET_VAL(CARL9170_TX_SUPER_AMPDU_DENSITY,
  748. txc->s.ampdu_settings, density);
  749. SET_VAL(CARL9170_TX_SUPER_AMPDU_FACTOR,
  750. txc->s.ampdu_settings, factor);
  751. for (i = 0; i < CARL9170_TX_MAX_RATES; i++) {
  752. txrate = &info->control.rates[i];
  753. if (txrate->idx >= 0) {
  754. txc->s.ri[i] =
  755. CARL9170_TX_SUPER_RI_AMPDU;
  756. if (WARN_ON(!(txrate->flags &
  757. IEEE80211_TX_RC_MCS))) {
  758. /*
  759. * Not sure if it's even possible
  760. * to aggregate non-ht rates with
  761. * this HW.
  762. */
  763. goto err_out;
  764. }
  765. continue;
  766. }
  767. txrate->idx = 0;
  768. txrate->count = ar->hw->max_rate_tries;
  769. }
  770. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_AGGR);
  771. }
  772. /*
  773. * NOTE: For the first rate, the ERP & AMPDU flags are directly
  774. * taken from mac_control. For all fallback rate, the firmware
  775. * updates the mac_control flags from the rate info field.
  776. */
  777. for (i = 1; i < CARL9170_TX_MAX_RATES; i++) {
  778. txrate = &info->control.rates[i];
  779. if (txrate->idx < 0)
  780. break;
  781. SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[i],
  782. txrate->count);
  783. if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
  784. txc->s.ri[i] |= (AR9170_TX_MAC_PROT_RTS <<
  785. CARL9170_TX_SUPER_RI_ERP_PROT_S);
  786. else if (carl9170_tx_cts_check(ar, txrate))
  787. txc->s.ri[i] |= (AR9170_TX_MAC_PROT_CTS <<
  788. CARL9170_TX_SUPER_RI_ERP_PROT_S);
  789. txc->s.rr[i - 1] = carl9170_tx_physet(ar, info, txrate);
  790. }
  791. txrate = &info->control.rates[0];
  792. SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[0], txrate->count);
  793. if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
  794. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
  795. else if (carl9170_tx_cts_check(ar, txrate))
  796. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
  797. txc->s.len = cpu_to_le16(skb->len);
  798. txc->f.length = cpu_to_le16(len + FCS_LEN);
  799. txc->f.mac_control = mac_tmp;
  800. txc->f.phy_control = carl9170_tx_physet(ar, info, txrate);
  801. arinfo = (void *)info->rate_driver_data;
  802. arinfo->timeout = jiffies;
  803. arinfo->ar = ar;
  804. kref_init(&arinfo->ref);
  805. return 0;
  806. err_out:
  807. skb_pull(skb, sizeof(*txc));
  808. return -EINVAL;
  809. }
  810. static void carl9170_set_immba(struct ar9170 *ar, struct sk_buff *skb)
  811. {
  812. struct _carl9170_tx_superframe *super;
  813. super = (void *) skb->data;
  814. super->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_BA);
  815. }
  816. static void carl9170_set_ampdu_params(struct ar9170 *ar, struct sk_buff *skb)
  817. {
  818. struct _carl9170_tx_superframe *super;
  819. int tmp;
  820. super = (void *) skb->data;
  821. tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_DENSITY) <<
  822. CARL9170_TX_SUPER_AMPDU_DENSITY_S;
  823. /*
  824. * If you haven't noticed carl9170_tx_prepare has already filled
  825. * in all ampdu spacing & factor parameters.
  826. * Now it's the time to check whenever the settings have to be
  827. * updated by the firmware, or if everything is still the same.
  828. *
  829. * There's no sane way to handle different density values with
  830. * this hardware, so we may as well just do the compare in the
  831. * driver.
  832. */
  833. if (tmp != ar->current_density) {
  834. ar->current_density = tmp;
  835. super->s.ampdu_settings |=
  836. CARL9170_TX_SUPER_AMPDU_COMMIT_DENSITY;
  837. }
  838. tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_FACTOR) <<
  839. CARL9170_TX_SUPER_AMPDU_FACTOR_S;
  840. if (tmp != ar->current_factor) {
  841. ar->current_factor = tmp;
  842. super->s.ampdu_settings |=
  843. CARL9170_TX_SUPER_AMPDU_COMMIT_FACTOR;
  844. }
  845. }
  846. static bool carl9170_tx_rate_check(struct ar9170 *ar, struct sk_buff *_dest,
  847. struct sk_buff *_src)
  848. {
  849. struct _carl9170_tx_superframe *dest, *src;
  850. dest = (void *) _dest->data;
  851. src = (void *) _src->data;
  852. /*
  853. * The mac80211 rate control algorithm expects that all MPDUs in
  854. * an AMPDU share the same tx vectors.
  855. * This is not really obvious right now, because the hardware
  856. * does the AMPDU setup according to its own rulebook.
  857. * Our nicely assembled, strictly monotonic increasing mpdu
  858. * chains will be broken up, mashed back together...
  859. */
  860. return (dest->f.phy_control == src->f.phy_control);
  861. }
  862. static void carl9170_tx_ampdu(struct ar9170 *ar)
  863. {
  864. struct sk_buff_head agg;
  865. struct carl9170_sta_tid *tid_info;
  866. struct sk_buff *skb, *first;
  867. unsigned int i = 0, done_ampdus = 0;
  868. u16 seq, queue, tmpssn;
  869. atomic_inc(&ar->tx_ampdu_scheduler);
  870. ar->tx_ampdu_schedule = false;
  871. if (atomic_read(&ar->tx_ampdu_upload))
  872. return;
  873. if (!ar->tx_ampdu_list_len)
  874. return;
  875. __skb_queue_head_init(&agg);
  876. rcu_read_lock();
  877. tid_info = rcu_dereference(ar->tx_ampdu_iter);
  878. if (WARN_ON_ONCE(!tid_info)) {
  879. rcu_read_unlock();
  880. return;
  881. }
  882. retry:
  883. list_for_each_entry_continue_rcu(tid_info, &ar->tx_ampdu_list, list) {
  884. i++;
  885. if (tid_info->state < CARL9170_TID_STATE_PROGRESS)
  886. continue;
  887. queue = TID_TO_WME_AC(tid_info->tid);
  888. spin_lock_bh(&tid_info->lock);
  889. if (tid_info->state != CARL9170_TID_STATE_XMIT)
  890. goto processed;
  891. tid_info->counter++;
  892. first = skb_peek(&tid_info->queue);
  893. tmpssn = carl9170_get_seq(first);
  894. seq = tid_info->snx;
  895. if (unlikely(tmpssn != seq)) {
  896. tid_info->state = CARL9170_TID_STATE_IDLE;
  897. goto processed;
  898. }
  899. while ((skb = skb_peek(&tid_info->queue))) {
  900. /* strict 0, 1, ..., n - 1, n frame sequence order */
  901. if (unlikely(carl9170_get_seq(skb) != seq))
  902. break;
  903. /* don't upload more than AMPDU FACTOR allows. */
  904. if (unlikely(SEQ_DIFF(tid_info->snx, tid_info->bsn) >=
  905. (tid_info->max - 1)))
  906. break;
  907. if (!carl9170_tx_rate_check(ar, skb, first))
  908. break;
  909. atomic_inc(&ar->tx_ampdu_upload);
  910. tid_info->snx = seq = SEQ_NEXT(seq);
  911. __skb_unlink(skb, &tid_info->queue);
  912. __skb_queue_tail(&agg, skb);
  913. if (skb_queue_len(&agg) >= CARL9170_NUM_TX_AGG_MAX)
  914. break;
  915. }
  916. if (skb_queue_empty(&tid_info->queue) ||
  917. carl9170_get_seq(skb_peek(&tid_info->queue)) !=
  918. tid_info->snx) {
  919. /*
  920. * stop TID, if A-MPDU frames are still missing,
  921. * or whenever the queue is empty.
  922. */
  923. tid_info->state = CARL9170_TID_STATE_IDLE;
  924. }
  925. done_ampdus++;
  926. processed:
  927. spin_unlock_bh(&tid_info->lock);
  928. if (skb_queue_empty(&agg))
  929. continue;
  930. /* apply ampdu spacing & factor settings */
  931. carl9170_set_ampdu_params(ar, skb_peek(&agg));
  932. /* set aggregation push bit */
  933. carl9170_set_immba(ar, skb_peek_tail(&agg));
  934. spin_lock_bh(&ar->tx_pending[queue].lock);
  935. skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
  936. spin_unlock_bh(&ar->tx_pending[queue].lock);
  937. ar->tx_schedule = true;
  938. }
  939. if ((done_ampdus++ == 0) && (i++ == 0))
  940. goto retry;
  941. rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
  942. rcu_read_unlock();
  943. }
  944. static struct sk_buff *carl9170_tx_pick_skb(struct ar9170 *ar,
  945. struct sk_buff_head *queue)
  946. {
  947. struct sk_buff *skb;
  948. struct ieee80211_tx_info *info;
  949. struct carl9170_tx_info *arinfo;
  950. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  951. spin_lock_bh(&queue->lock);
  952. skb = skb_peek(queue);
  953. if (unlikely(!skb))
  954. goto err_unlock;
  955. if (carl9170_alloc_dev_space(ar, skb))
  956. goto err_unlock;
  957. __skb_unlink(skb, queue);
  958. spin_unlock_bh(&queue->lock);
  959. info = IEEE80211_SKB_CB(skb);
  960. arinfo = (void *) info->rate_driver_data;
  961. arinfo->timeout = jiffies;
  962. /*
  963. * increase ref count to "2".
  964. * Ref counting is the easiest way to solve the race between
  965. * the the urb's completion routine: carl9170_tx_callback and
  966. * wlan tx status functions: carl9170_tx_status/janitor.
  967. */
  968. carl9170_tx_get_skb(skb);
  969. return skb;
  970. err_unlock:
  971. spin_unlock_bh(&queue->lock);
  972. return NULL;
  973. }
  974. void carl9170_tx_drop(struct ar9170 *ar, struct sk_buff *skb)
  975. {
  976. struct _carl9170_tx_superframe *super;
  977. uint8_t q = 0;
  978. ar->tx_dropped++;
  979. super = (void *)skb->data;
  980. SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, q,
  981. ar9170_qmap[carl9170_get_queue(ar, skb)]);
  982. __carl9170_tx_process_status(ar, super->s.cookie, q);
  983. }
  984. static void carl9170_tx(struct ar9170 *ar)
  985. {
  986. struct sk_buff *skb;
  987. unsigned int i, q;
  988. bool schedule_garbagecollector = false;
  989. ar->tx_schedule = false;
  990. if (unlikely(!IS_STARTED(ar)))
  991. return;
  992. carl9170_usb_handle_tx_err(ar);
  993. for (i = 0; i < ar->hw->queues; i++) {
  994. while (!skb_queue_empty(&ar->tx_pending[i])) {
  995. skb = carl9170_tx_pick_skb(ar, &ar->tx_pending[i]);
  996. if (unlikely(!skb))
  997. break;
  998. atomic_inc(&ar->tx_total_pending);
  999. q = __carl9170_get_queue(ar, i);
  1000. /*
  1001. * NB: tx_status[i] vs. tx_status[q],
  1002. * TODO: Move into pick_skb or alloc_dev_space.
  1003. */
  1004. skb_queue_tail(&ar->tx_status[q], skb);
  1005. carl9170_usb_tx(ar, skb);
  1006. schedule_garbagecollector = true;
  1007. }
  1008. }
  1009. if (!schedule_garbagecollector)
  1010. return;
  1011. ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
  1012. msecs_to_jiffies(CARL9170_TX_TIMEOUT));
  1013. }
  1014. static bool carl9170_tx_ampdu_queue(struct ar9170 *ar,
  1015. struct ieee80211_sta *sta, struct sk_buff *skb)
  1016. {
  1017. struct _carl9170_tx_superframe *super = (void *) skb->data;
  1018. struct carl9170_sta_info *sta_info;
  1019. struct carl9170_sta_tid *agg;
  1020. struct sk_buff *iter;
  1021. unsigned int max;
  1022. u16 tid, seq, qseq, off;
  1023. bool run = false;
  1024. tid = carl9170_get_tid(skb);
  1025. seq = carl9170_get_seq(skb);
  1026. sta_info = (void *) sta->drv_priv;
  1027. rcu_read_lock();
  1028. agg = rcu_dereference(sta_info->agg[tid]);
  1029. max = sta_info->ampdu_max_len;
  1030. if (!agg)
  1031. goto err_unlock_rcu;
  1032. spin_lock_bh(&agg->lock);
  1033. if (unlikely(agg->state < CARL9170_TID_STATE_IDLE))
  1034. goto err_unlock;
  1035. /* check if sequence is within the BA window */
  1036. if (unlikely(!BAW_WITHIN(agg->bsn, CARL9170_BAW_BITS, seq)))
  1037. goto err_unlock;
  1038. if (WARN_ON_ONCE(!BAW_WITHIN(agg->snx, CARL9170_BAW_BITS, seq)))
  1039. goto err_unlock;
  1040. off = SEQ_DIFF(seq, agg->bsn);
  1041. if (WARN_ON_ONCE(test_and_set_bit(off, agg->bitmap)))
  1042. goto err_unlock;
  1043. if (likely(BAW_WITHIN(agg->hsn, CARL9170_BAW_BITS, seq))) {
  1044. __skb_queue_tail(&agg->queue, skb);
  1045. agg->hsn = seq;
  1046. goto queued;
  1047. }
  1048. skb_queue_reverse_walk(&agg->queue, iter) {
  1049. qseq = carl9170_get_seq(iter);
  1050. if (BAW_WITHIN(qseq, CARL9170_BAW_BITS, seq)) {
  1051. __skb_queue_after(&agg->queue, iter, skb);
  1052. goto queued;
  1053. }
  1054. }
  1055. __skb_queue_head(&agg->queue, skb);
  1056. queued:
  1057. if (unlikely(agg->state != CARL9170_TID_STATE_XMIT)) {
  1058. if (agg->snx == carl9170_get_seq(skb_peek(&agg->queue))) {
  1059. agg->state = CARL9170_TID_STATE_XMIT;
  1060. run = true;
  1061. }
  1062. }
  1063. spin_unlock_bh(&agg->lock);
  1064. rcu_read_unlock();
  1065. return run;
  1066. err_unlock:
  1067. spin_unlock_bh(&agg->lock);
  1068. err_unlock_rcu:
  1069. rcu_read_unlock();
  1070. super->f.mac_control &= ~cpu_to_le16(AR9170_TX_MAC_AGGR);
  1071. carl9170_tx_status(ar, skb, false);
  1072. ar->tx_dropped++;
  1073. return false;
  1074. }
  1075. void carl9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  1076. {
  1077. struct ar9170 *ar = hw->priv;
  1078. struct ieee80211_tx_info *info;
  1079. struct ieee80211_sta *sta;
  1080. bool run;
  1081. if (unlikely(!IS_STARTED(ar)))
  1082. goto err_free;
  1083. info = IEEE80211_SKB_CB(skb);
  1084. sta = info->control.sta;
  1085. if (unlikely(carl9170_tx_prepare(ar, skb)))
  1086. goto err_free;
  1087. carl9170_tx_accounting(ar, skb);
  1088. /*
  1089. * from now on, one has to use carl9170_tx_status to free
  1090. * all ressouces which are associated with the frame.
  1091. */
  1092. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1093. run = carl9170_tx_ampdu_queue(ar, sta, skb);
  1094. if (run)
  1095. carl9170_tx_ampdu(ar);
  1096. } else {
  1097. unsigned int queue = skb_get_queue_mapping(skb);
  1098. skb_queue_tail(&ar->tx_pending[queue], skb);
  1099. }
  1100. carl9170_tx(ar);
  1101. return;
  1102. err_free:
  1103. ar->tx_dropped++;
  1104. dev_kfree_skb_any(skb);
  1105. }
  1106. void carl9170_tx_scheduler(struct ar9170 *ar)
  1107. {
  1108. if (ar->tx_ampdu_schedule)
  1109. carl9170_tx_ampdu(ar);
  1110. if (ar->tx_schedule)
  1111. carl9170_tx(ar);
  1112. }