tx.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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. /* needs rcu_read_lock */
  96. static struct ieee80211_sta *__carl9170_get_tx_sta(struct ar9170 *ar,
  97. struct sk_buff *skb)
  98. {
  99. struct _carl9170_tx_superframe *super = (void *) skb->data;
  100. struct ieee80211_hdr *hdr = (void *) super->frame_data;
  101. struct ieee80211_vif *vif;
  102. unsigned int vif_id;
  103. vif_id = (super->s.misc & CARL9170_TX_SUPER_MISC_VIF_ID) >>
  104. CARL9170_TX_SUPER_MISC_VIF_ID_S;
  105. if (WARN_ON_ONCE(vif_id >= AR9170_MAX_VIRTUAL_MAC))
  106. return NULL;
  107. vif = rcu_dereference(ar->vif_priv[vif_id].vif);
  108. if (unlikely(!vif))
  109. return NULL;
  110. /*
  111. * Normally we should use wrappers like ieee80211_get_DA to get
  112. * the correct peer ieee80211_sta.
  113. *
  114. * But there is a problem with indirect traffic (broadcasts, or
  115. * data which is designated for other stations) in station mode.
  116. * The frame will be directed to the AP for distribution and not
  117. * to the actual destination.
  118. */
  119. return ieee80211_find_sta(vif, hdr->addr1);
  120. }
  121. static void carl9170_tx_ps_unblock(struct ar9170 *ar, struct sk_buff *skb)
  122. {
  123. struct ieee80211_sta *sta;
  124. struct carl9170_sta_info *sta_info;
  125. rcu_read_lock();
  126. sta = __carl9170_get_tx_sta(ar, skb);
  127. if (unlikely(!sta))
  128. goto out_rcu;
  129. sta_info = (struct carl9170_sta_info *) sta->drv_priv;
  130. if (atomic_dec_return(&sta_info->pending_frames) == 0)
  131. ieee80211_sta_block_awake(ar->hw, sta, false);
  132. out_rcu:
  133. rcu_read_unlock();
  134. }
  135. static void carl9170_tx_accounting_free(struct ar9170 *ar, struct sk_buff *skb)
  136. {
  137. int queue;
  138. queue = skb_get_queue_mapping(skb);
  139. spin_lock_bh(&ar->tx_stats_lock);
  140. ar->tx_stats[queue].len--;
  141. if (!is_mem_full(ar)) {
  142. unsigned int i;
  143. for (i = 0; i < ar->hw->queues; i++) {
  144. if (ar->tx_stats[i].len >= CARL9170_NUM_TX_LIMIT_SOFT)
  145. continue;
  146. if (ieee80211_queue_stopped(ar->hw, i)) {
  147. unsigned long tmp;
  148. tmp = jiffies - ar->queue_stop_timeout[i];
  149. if (tmp > ar->max_queue_stop_timeout[i])
  150. ar->max_queue_stop_timeout[i] = tmp;
  151. }
  152. ieee80211_wake_queue(ar->hw, i);
  153. }
  154. }
  155. spin_unlock_bh(&ar->tx_stats_lock);
  156. if (atomic_dec_and_test(&ar->tx_total_queued))
  157. complete(&ar->tx_flush);
  158. }
  159. static int carl9170_alloc_dev_space(struct ar9170 *ar, struct sk_buff *skb)
  160. {
  161. struct _carl9170_tx_superframe *super = (void *) skb->data;
  162. unsigned int chunks;
  163. int cookie = -1;
  164. atomic_inc(&ar->mem_allocs);
  165. chunks = DIV_ROUND_UP(skb->len, ar->fw.mem_block_size);
  166. if (unlikely(atomic_sub_return(chunks, &ar->mem_free_blocks) < 0)) {
  167. atomic_add(chunks, &ar->mem_free_blocks);
  168. return -ENOSPC;
  169. }
  170. spin_lock_bh(&ar->mem_lock);
  171. cookie = bitmap_find_free_region(ar->mem_bitmap, ar->fw.mem_blocks, 0);
  172. spin_unlock_bh(&ar->mem_lock);
  173. if (unlikely(cookie < 0)) {
  174. atomic_add(chunks, &ar->mem_free_blocks);
  175. return -ENOSPC;
  176. }
  177. super = (void *) skb->data;
  178. /*
  179. * Cookie #0 serves two special purposes:
  180. * 1. The firmware might use it generate BlockACK frames
  181. * in responds of an incoming BlockAckReqs.
  182. *
  183. * 2. Prevent double-free bugs.
  184. */
  185. super->s.cookie = (u8) cookie + 1;
  186. return 0;
  187. }
  188. static void carl9170_release_dev_space(struct ar9170 *ar, struct sk_buff *skb)
  189. {
  190. struct _carl9170_tx_superframe *super = (void *) skb->data;
  191. int cookie;
  192. /* make a local copy of the cookie */
  193. cookie = super->s.cookie;
  194. /* invalidate cookie */
  195. super->s.cookie = 0;
  196. /*
  197. * Do a out-of-bounds check on the cookie:
  198. *
  199. * * cookie "0" is reserved and won't be assigned to any
  200. * out-going frame. Internally however, it is used to
  201. * mark no longer/un-accounted frames and serves as a
  202. * cheap way of preventing frames from being freed
  203. * twice by _accident_. NB: There is a tiny race...
  204. *
  205. * * obviously, cookie number is limited by the amount
  206. * of available memory blocks, so the number can
  207. * never execeed the mem_blocks count.
  208. */
  209. if (unlikely(WARN_ON_ONCE(cookie == 0) ||
  210. WARN_ON_ONCE(cookie > ar->fw.mem_blocks)))
  211. return;
  212. atomic_add(DIV_ROUND_UP(skb->len, ar->fw.mem_block_size),
  213. &ar->mem_free_blocks);
  214. spin_lock_bh(&ar->mem_lock);
  215. bitmap_release_region(ar->mem_bitmap, cookie - 1, 0);
  216. spin_unlock_bh(&ar->mem_lock);
  217. }
  218. /* Called from any context */
  219. static void carl9170_tx_release(struct kref *ref)
  220. {
  221. struct ar9170 *ar;
  222. struct carl9170_tx_info *arinfo;
  223. struct ieee80211_tx_info *txinfo;
  224. struct sk_buff *skb;
  225. arinfo = container_of(ref, struct carl9170_tx_info, ref);
  226. txinfo = container_of((void *) arinfo, struct ieee80211_tx_info,
  227. rate_driver_data);
  228. skb = container_of((void *) txinfo, struct sk_buff, cb);
  229. ar = arinfo->ar;
  230. if (WARN_ON_ONCE(!ar))
  231. return;
  232. BUILD_BUG_ON(
  233. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len) != 23);
  234. memset(&txinfo->status.ampdu_ack_len, 0,
  235. sizeof(struct ieee80211_tx_info) -
  236. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
  237. if (atomic_read(&ar->tx_total_queued))
  238. ar->tx_schedule = true;
  239. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU) {
  240. if (!atomic_read(&ar->tx_ampdu_upload))
  241. ar->tx_ampdu_schedule = true;
  242. if (txinfo->flags & IEEE80211_TX_STAT_AMPDU) {
  243. struct _carl9170_tx_superframe *super;
  244. super = (void *)skb->data;
  245. txinfo->status.ampdu_len = super->s.rix;
  246. txinfo->status.ampdu_ack_len = super->s.cnt;
  247. } else if ((txinfo->flags & IEEE80211_TX_STAT_ACK) &&
  248. !(txinfo->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)) {
  249. /*
  250. * drop redundant tx_status reports:
  251. *
  252. * 1. ampdu_ack_len of the final tx_status does
  253. * include the feedback of this particular frame.
  254. *
  255. * 2. tx_status_irqsafe only queues up to 128
  256. * tx feedback reports and discards the rest.
  257. *
  258. * 3. minstrel_ht is picky, it only accepts
  259. * reports of frames with the TX_STATUS_AMPDU flag.
  260. *
  261. * 4. mac80211 is not particularly interested in
  262. * feedback either [CTL_REQ_TX_STATUS not set]
  263. */
  264. ieee80211_free_txskb(ar->hw, skb);
  265. return;
  266. } else {
  267. /*
  268. * Either the frame transmission has failed or
  269. * mac80211 requested tx status.
  270. */
  271. }
  272. }
  273. skb_pull(skb, sizeof(struct _carl9170_tx_superframe));
  274. ieee80211_tx_status_irqsafe(ar->hw, skb);
  275. }
  276. void carl9170_tx_get_skb(struct sk_buff *skb)
  277. {
  278. struct carl9170_tx_info *arinfo = (void *)
  279. (IEEE80211_SKB_CB(skb))->rate_driver_data;
  280. kref_get(&arinfo->ref);
  281. }
  282. int carl9170_tx_put_skb(struct sk_buff *skb)
  283. {
  284. struct carl9170_tx_info *arinfo = (void *)
  285. (IEEE80211_SKB_CB(skb))->rate_driver_data;
  286. return kref_put(&arinfo->ref, carl9170_tx_release);
  287. }
  288. /* Caller must hold the tid_info->lock & rcu_read_lock */
  289. static void carl9170_tx_shift_bm(struct ar9170 *ar,
  290. struct carl9170_sta_tid *tid_info, u16 seq)
  291. {
  292. u16 off;
  293. off = SEQ_DIFF(seq, tid_info->bsn);
  294. if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
  295. return;
  296. /*
  297. * Sanity check. For each MPDU we set the bit in bitmap and
  298. * clear it once we received the tx_status.
  299. * But if the bit is already cleared then we've been bitten
  300. * by a bug.
  301. */
  302. WARN_ON_ONCE(!test_and_clear_bit(off, tid_info->bitmap));
  303. off = SEQ_DIFF(tid_info->snx, tid_info->bsn);
  304. if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
  305. return;
  306. if (!bitmap_empty(tid_info->bitmap, off))
  307. off = find_first_bit(tid_info->bitmap, off);
  308. tid_info->bsn += off;
  309. tid_info->bsn &= 0x0fff;
  310. bitmap_shift_right(tid_info->bitmap, tid_info->bitmap,
  311. off, CARL9170_BAW_BITS);
  312. }
  313. static void carl9170_tx_status_process_ampdu(struct ar9170 *ar,
  314. struct sk_buff *skb, struct ieee80211_tx_info *txinfo)
  315. {
  316. struct _carl9170_tx_superframe *super = (void *) skb->data;
  317. struct ieee80211_hdr *hdr = (void *) super->frame_data;
  318. struct ieee80211_sta *sta;
  319. struct carl9170_sta_info *sta_info;
  320. struct carl9170_sta_tid *tid_info;
  321. u8 tid;
  322. if (!(txinfo->flags & IEEE80211_TX_CTL_AMPDU) ||
  323. txinfo->flags & IEEE80211_TX_CTL_INJECTED ||
  324. (!(super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_AGGR))))
  325. return;
  326. rcu_read_lock();
  327. sta = __carl9170_get_tx_sta(ar, skb);
  328. if (unlikely(!sta))
  329. goto out_rcu;
  330. tid = get_tid_h(hdr);
  331. sta_info = (void *) sta->drv_priv;
  332. tid_info = rcu_dereference(sta_info->agg[tid]);
  333. if (!tid_info)
  334. goto out_rcu;
  335. spin_lock_bh(&tid_info->lock);
  336. if (likely(tid_info->state >= CARL9170_TID_STATE_IDLE))
  337. carl9170_tx_shift_bm(ar, tid_info, get_seq_h(hdr));
  338. if (sta_info->stats[tid].clear) {
  339. sta_info->stats[tid].clear = false;
  340. sta_info->stats[tid].req = false;
  341. sta_info->stats[tid].ampdu_len = 0;
  342. sta_info->stats[tid].ampdu_ack_len = 0;
  343. }
  344. sta_info->stats[tid].ampdu_len++;
  345. if (txinfo->status.rates[0].count == 1)
  346. sta_info->stats[tid].ampdu_ack_len++;
  347. if (!(txinfo->flags & IEEE80211_TX_STAT_ACK))
  348. sta_info->stats[tid].req = true;
  349. if (super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_IMM_BA)) {
  350. super->s.rix = sta_info->stats[tid].ampdu_len;
  351. super->s.cnt = sta_info->stats[tid].ampdu_ack_len;
  352. txinfo->flags |= IEEE80211_TX_STAT_AMPDU;
  353. if (sta_info->stats[tid].req)
  354. txinfo->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  355. sta_info->stats[tid].clear = true;
  356. }
  357. spin_unlock_bh(&tid_info->lock);
  358. out_rcu:
  359. rcu_read_unlock();
  360. }
  361. void carl9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
  362. const bool success)
  363. {
  364. struct ieee80211_tx_info *txinfo;
  365. carl9170_tx_accounting_free(ar, skb);
  366. txinfo = IEEE80211_SKB_CB(skb);
  367. if (success)
  368. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  369. else
  370. ar->tx_ack_failures++;
  371. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
  372. carl9170_tx_status_process_ampdu(ar, skb, txinfo);
  373. carl9170_tx_ps_unblock(ar, skb);
  374. carl9170_tx_put_skb(skb);
  375. }
  376. /* This function may be called form any context */
  377. void carl9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb)
  378. {
  379. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  380. atomic_dec(&ar->tx_total_pending);
  381. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
  382. atomic_dec(&ar->tx_ampdu_upload);
  383. if (carl9170_tx_put_skb(skb))
  384. tasklet_hi_schedule(&ar->usb_tasklet);
  385. }
  386. static struct sk_buff *carl9170_get_queued_skb(struct ar9170 *ar, u8 cookie,
  387. struct sk_buff_head *queue)
  388. {
  389. struct sk_buff *skb;
  390. spin_lock_bh(&queue->lock);
  391. skb_queue_walk(queue, skb) {
  392. struct _carl9170_tx_superframe *txc = (void *) skb->data;
  393. if (txc->s.cookie != cookie)
  394. continue;
  395. __skb_unlink(skb, queue);
  396. spin_unlock_bh(&queue->lock);
  397. carl9170_release_dev_space(ar, skb);
  398. return skb;
  399. }
  400. spin_unlock_bh(&queue->lock);
  401. return NULL;
  402. }
  403. static void carl9170_tx_fill_rateinfo(struct ar9170 *ar, unsigned int rix,
  404. unsigned int tries, struct ieee80211_tx_info *txinfo)
  405. {
  406. unsigned int i;
  407. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  408. if (txinfo->status.rates[i].idx < 0)
  409. break;
  410. if (i == rix) {
  411. txinfo->status.rates[i].count = tries;
  412. i++;
  413. break;
  414. }
  415. }
  416. for (; i < IEEE80211_TX_MAX_RATES; i++) {
  417. txinfo->status.rates[i].idx = -1;
  418. txinfo->status.rates[i].count = 0;
  419. }
  420. }
  421. static void carl9170_check_queue_stop_timeout(struct ar9170 *ar)
  422. {
  423. int i;
  424. struct sk_buff *skb;
  425. struct ieee80211_tx_info *txinfo;
  426. struct carl9170_tx_info *arinfo;
  427. bool restart = false;
  428. for (i = 0; i < ar->hw->queues; i++) {
  429. spin_lock_bh(&ar->tx_status[i].lock);
  430. skb = skb_peek(&ar->tx_status[i]);
  431. if (!skb)
  432. goto next;
  433. txinfo = IEEE80211_SKB_CB(skb);
  434. arinfo = (void *) txinfo->rate_driver_data;
  435. if (time_is_before_jiffies(arinfo->timeout +
  436. msecs_to_jiffies(CARL9170_QUEUE_STUCK_TIMEOUT)) == true)
  437. restart = true;
  438. next:
  439. spin_unlock_bh(&ar->tx_status[i].lock);
  440. }
  441. if (restart) {
  442. /*
  443. * At least one queue has been stuck for long enough.
  444. * Give the device a kick and hope it gets back to
  445. * work.
  446. *
  447. * possible reasons may include:
  448. * - frames got lost/corrupted (bad connection to the device)
  449. * - stalled rx processing/usb controller hiccups
  450. * - firmware errors/bugs
  451. * - every bug you can think of.
  452. * - all bugs you can't...
  453. * - ...
  454. */
  455. carl9170_restart(ar, CARL9170_RR_STUCK_TX);
  456. }
  457. }
  458. static void carl9170_tx_ampdu_timeout(struct ar9170 *ar)
  459. {
  460. struct carl9170_sta_tid *iter;
  461. struct sk_buff *skb;
  462. struct ieee80211_tx_info *txinfo;
  463. struct carl9170_tx_info *arinfo;
  464. struct ieee80211_sta *sta;
  465. rcu_read_lock();
  466. list_for_each_entry_rcu(iter, &ar->tx_ampdu_list, list) {
  467. if (iter->state < CARL9170_TID_STATE_IDLE)
  468. continue;
  469. spin_lock_bh(&iter->lock);
  470. skb = skb_peek(&iter->queue);
  471. if (!skb)
  472. goto unlock;
  473. txinfo = IEEE80211_SKB_CB(skb);
  474. arinfo = (void *)txinfo->rate_driver_data;
  475. if (time_is_after_jiffies(arinfo->timeout +
  476. msecs_to_jiffies(CARL9170_QUEUE_TIMEOUT)))
  477. goto unlock;
  478. sta = __carl9170_get_tx_sta(ar, skb);
  479. if (WARN_ON(!sta))
  480. goto unlock;
  481. ieee80211_stop_tx_ba_session(sta, iter->tid);
  482. unlock:
  483. spin_unlock_bh(&iter->lock);
  484. }
  485. rcu_read_unlock();
  486. }
  487. void carl9170_tx_janitor(struct work_struct *work)
  488. {
  489. struct ar9170 *ar = container_of(work, struct ar9170,
  490. tx_janitor.work);
  491. if (!IS_STARTED(ar))
  492. return;
  493. ar->tx_janitor_last_run = jiffies;
  494. carl9170_check_queue_stop_timeout(ar);
  495. carl9170_tx_ampdu_timeout(ar);
  496. if (!atomic_read(&ar->tx_total_queued))
  497. return;
  498. ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
  499. msecs_to_jiffies(CARL9170_TX_TIMEOUT));
  500. }
  501. static void __carl9170_tx_process_status(struct ar9170 *ar,
  502. const uint8_t cookie, const uint8_t info)
  503. {
  504. struct sk_buff *skb;
  505. struct ieee80211_tx_info *txinfo;
  506. unsigned int r, t, q;
  507. bool success = true;
  508. q = ar9170_qmap[info & CARL9170_TX_STATUS_QUEUE];
  509. skb = carl9170_get_queued_skb(ar, cookie, &ar->tx_status[q]);
  510. if (!skb) {
  511. /*
  512. * We have lost the race to another thread.
  513. */
  514. return ;
  515. }
  516. txinfo = IEEE80211_SKB_CB(skb);
  517. if (!(info & CARL9170_TX_STATUS_SUCCESS))
  518. success = false;
  519. r = (info & CARL9170_TX_STATUS_RIX) >> CARL9170_TX_STATUS_RIX_S;
  520. t = (info & CARL9170_TX_STATUS_TRIES) >> CARL9170_TX_STATUS_TRIES_S;
  521. carl9170_tx_fill_rateinfo(ar, r, t, txinfo);
  522. carl9170_tx_status(ar, skb, success);
  523. }
  524. void carl9170_tx_process_status(struct ar9170 *ar,
  525. const struct carl9170_rsp *cmd)
  526. {
  527. unsigned int i;
  528. for (i = 0; i < cmd->hdr.ext; i++) {
  529. if (WARN_ON(i > ((cmd->hdr.len / 2) + 1))) {
  530. print_hex_dump_bytes("UU:", DUMP_PREFIX_NONE,
  531. (void *) cmd, cmd->hdr.len + 4);
  532. break;
  533. }
  534. __carl9170_tx_process_status(ar, cmd->_tx_status[i].cookie,
  535. cmd->_tx_status[i].info);
  536. }
  537. }
  538. static void carl9170_tx_rate_tpc_chains(struct ar9170 *ar,
  539. struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate,
  540. unsigned int *phyrate, unsigned int *tpc, unsigned int *chains)
  541. {
  542. struct ieee80211_rate *rate = NULL;
  543. u8 *txpower;
  544. unsigned int idx;
  545. idx = txrate->idx;
  546. *tpc = 0;
  547. *phyrate = 0;
  548. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  549. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  550. /* +1 dBm for HT40 */
  551. *tpc += 2;
  552. if (info->band == IEEE80211_BAND_2GHZ)
  553. txpower = ar->power_2G_ht40;
  554. else
  555. txpower = ar->power_5G_ht40;
  556. } else {
  557. if (info->band == IEEE80211_BAND_2GHZ)
  558. txpower = ar->power_2G_ht20;
  559. else
  560. txpower = ar->power_5G_ht20;
  561. }
  562. *phyrate = txrate->idx;
  563. *tpc += txpower[idx & 7];
  564. } else {
  565. if (info->band == IEEE80211_BAND_2GHZ) {
  566. if (idx < 4)
  567. txpower = ar->power_2G_cck;
  568. else
  569. txpower = ar->power_2G_ofdm;
  570. } else {
  571. txpower = ar->power_5G_leg;
  572. idx += 4;
  573. }
  574. rate = &__carl9170_ratetable[idx];
  575. *tpc += txpower[(rate->hw_value & 0x30) >> 4];
  576. *phyrate = rate->hw_value & 0xf;
  577. }
  578. if (ar->eeprom.tx_mask == 1) {
  579. *chains = AR9170_TX_PHY_TXCHAIN_1;
  580. } else {
  581. if (!(txrate->flags & IEEE80211_TX_RC_MCS) &&
  582. rate && rate->bitrate >= 360)
  583. *chains = AR9170_TX_PHY_TXCHAIN_1;
  584. else
  585. *chains = AR9170_TX_PHY_TXCHAIN_2;
  586. }
  587. *tpc = min_t(unsigned int, *tpc, ar->hw->conf.power_level * 2);
  588. }
  589. static __le32 carl9170_tx_physet(struct ar9170 *ar,
  590. struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate)
  591. {
  592. unsigned int power = 0, chains = 0, phyrate = 0;
  593. __le32 tmp;
  594. tmp = cpu_to_le32(0);
  595. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  596. tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ <<
  597. AR9170_TX_PHY_BW_S);
  598. /* this works because 40 MHz is 2 and dup is 3 */
  599. if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
  600. tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP <<
  601. AR9170_TX_PHY_BW_S);
  602. if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
  603. tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
  604. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  605. SET_VAL(AR9170_TX_PHY_MCS, phyrate, txrate->idx);
  606. /* heavy clip control */
  607. tmp |= cpu_to_le32((txrate->idx & 0x7) <<
  608. AR9170_TX_PHY_TX_HEAVY_CLIP_S);
  609. tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
  610. /*
  611. * green field preamble does not work.
  612. *
  613. * if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  614. * tmp |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
  615. */
  616. } else {
  617. if (info->band == IEEE80211_BAND_2GHZ) {
  618. if (txrate->idx <= AR9170_TX_PHY_RATE_CCK_11M)
  619. tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_CCK);
  620. else
  621. tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_OFDM);
  622. } else {
  623. tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_OFDM);
  624. }
  625. /*
  626. * short preamble seems to be broken too.
  627. *
  628. * if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  629. * tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
  630. */
  631. }
  632. carl9170_tx_rate_tpc_chains(ar, info, txrate,
  633. &phyrate, &power, &chains);
  634. tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_MCS, phyrate));
  635. tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_TX_PWR, power));
  636. tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_TXCHAIN, chains));
  637. return tmp;
  638. }
  639. static bool carl9170_tx_rts_check(struct ar9170 *ar,
  640. struct ieee80211_tx_rate *rate,
  641. bool ampdu, bool multi)
  642. {
  643. switch (ar->erp_mode) {
  644. case CARL9170_ERP_AUTO:
  645. if (ampdu)
  646. break;
  647. case CARL9170_ERP_MAC80211:
  648. if (!(rate->flags & IEEE80211_TX_RC_USE_RTS_CTS))
  649. break;
  650. case CARL9170_ERP_RTS:
  651. if (likely(!multi))
  652. return true;
  653. default:
  654. break;
  655. }
  656. return false;
  657. }
  658. static bool carl9170_tx_cts_check(struct ar9170 *ar,
  659. struct ieee80211_tx_rate *rate)
  660. {
  661. switch (ar->erp_mode) {
  662. case CARL9170_ERP_AUTO:
  663. case CARL9170_ERP_MAC80211:
  664. if (!(rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
  665. break;
  666. case CARL9170_ERP_CTS:
  667. return true;
  668. default:
  669. break;
  670. }
  671. return false;
  672. }
  673. static int carl9170_tx_prepare(struct ar9170 *ar, struct sk_buff *skb)
  674. {
  675. struct ieee80211_hdr *hdr;
  676. struct _carl9170_tx_superframe *txc;
  677. struct carl9170_vif_info *cvif;
  678. struct ieee80211_tx_info *info;
  679. struct ieee80211_tx_rate *txrate;
  680. struct ieee80211_sta *sta;
  681. struct carl9170_tx_info *arinfo;
  682. unsigned int hw_queue;
  683. int i;
  684. __le16 mac_tmp;
  685. u16 len;
  686. bool ampdu, no_ack;
  687. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  688. BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc) !=
  689. CARL9170_TX_SUPERDESC_LEN);
  690. BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc) !=
  691. AR9170_TX_HWDESC_LEN);
  692. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES < CARL9170_TX_MAX_RATES);
  693. BUILD_BUG_ON(AR9170_MAX_VIRTUAL_MAC >
  694. ((CARL9170_TX_SUPER_MISC_VIF_ID >>
  695. CARL9170_TX_SUPER_MISC_VIF_ID_S) + 1));
  696. hw_queue = ar9170_qmap[carl9170_get_queue(ar, skb)];
  697. hdr = (void *)skb->data;
  698. info = IEEE80211_SKB_CB(skb);
  699. len = skb->len;
  700. /*
  701. * Note: If the frame was sent through a monitor interface,
  702. * the ieee80211_vif pointer can be NULL.
  703. */
  704. if (likely(info->control.vif))
  705. cvif = (void *) info->control.vif->drv_priv;
  706. else
  707. cvif = NULL;
  708. sta = info->control.sta;
  709. txc = (void *)skb_push(skb, sizeof(*txc));
  710. memset(txc, 0, sizeof(*txc));
  711. SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, txc->s.misc, hw_queue);
  712. if (likely(cvif))
  713. SET_VAL(CARL9170_TX_SUPER_MISC_VIF_ID, txc->s.misc, cvif->id);
  714. if (unlikely(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM))
  715. txc->s.misc |= CARL9170_TX_SUPER_MISC_CAB;
  716. if (unlikely(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
  717. txc->s.misc |= CARL9170_TX_SUPER_MISC_ASSIGN_SEQ;
  718. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control)))
  719. txc->s.misc |= CARL9170_TX_SUPER_MISC_FILL_IN_TSF;
  720. mac_tmp = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
  721. AR9170_TX_MAC_BACKOFF);
  722. mac_tmp |= cpu_to_le16((hw_queue << AR9170_TX_MAC_QOS_S) &
  723. AR9170_TX_MAC_QOS);
  724. no_ack = !!(info->flags & IEEE80211_TX_CTL_NO_ACK);
  725. if (unlikely(no_ack))
  726. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
  727. if (info->control.hw_key) {
  728. len += info->control.hw_key->icv_len;
  729. switch (info->control.hw_key->cipher) {
  730. case WLAN_CIPHER_SUITE_WEP40:
  731. case WLAN_CIPHER_SUITE_WEP104:
  732. case WLAN_CIPHER_SUITE_TKIP:
  733. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_RC4);
  734. break;
  735. case WLAN_CIPHER_SUITE_CCMP:
  736. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_AES);
  737. break;
  738. default:
  739. WARN_ON(1);
  740. goto err_out;
  741. }
  742. }
  743. ampdu = !!(info->flags & IEEE80211_TX_CTL_AMPDU);
  744. if (ampdu) {
  745. unsigned int density, factor;
  746. if (unlikely(!sta || !cvif))
  747. goto err_out;
  748. factor = min_t(unsigned int, 1u, sta->ht_cap.ampdu_factor);
  749. density = sta->ht_cap.ampdu_density;
  750. if (density) {
  751. /*
  752. * Watch out!
  753. *
  754. * Otus uses slightly different density values than
  755. * those from the 802.11n spec.
  756. */
  757. density = max_t(unsigned int, density + 1, 7u);
  758. }
  759. SET_VAL(CARL9170_TX_SUPER_AMPDU_DENSITY,
  760. txc->s.ampdu_settings, density);
  761. SET_VAL(CARL9170_TX_SUPER_AMPDU_FACTOR,
  762. txc->s.ampdu_settings, factor);
  763. for (i = 0; i < CARL9170_TX_MAX_RATES; i++) {
  764. txrate = &info->control.rates[i];
  765. if (txrate->idx >= 0) {
  766. txc->s.ri[i] =
  767. CARL9170_TX_SUPER_RI_AMPDU;
  768. if (WARN_ON(!(txrate->flags &
  769. IEEE80211_TX_RC_MCS))) {
  770. /*
  771. * Not sure if it's even possible
  772. * to aggregate non-ht rates with
  773. * this HW.
  774. */
  775. goto err_out;
  776. }
  777. continue;
  778. }
  779. txrate->idx = 0;
  780. txrate->count = ar->hw->max_rate_tries;
  781. }
  782. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_AGGR);
  783. }
  784. /*
  785. * NOTE: For the first rate, the ERP & AMPDU flags are directly
  786. * taken from mac_control. For all fallback rate, the firmware
  787. * updates the mac_control flags from the rate info field.
  788. */
  789. for (i = 1; i < CARL9170_TX_MAX_RATES; i++) {
  790. txrate = &info->control.rates[i];
  791. if (txrate->idx < 0)
  792. break;
  793. SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[i],
  794. txrate->count);
  795. if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
  796. txc->s.ri[i] |= (AR9170_TX_MAC_PROT_RTS <<
  797. CARL9170_TX_SUPER_RI_ERP_PROT_S);
  798. else if (carl9170_tx_cts_check(ar, txrate))
  799. txc->s.ri[i] |= (AR9170_TX_MAC_PROT_CTS <<
  800. CARL9170_TX_SUPER_RI_ERP_PROT_S);
  801. txc->s.rr[i - 1] = carl9170_tx_physet(ar, info, txrate);
  802. }
  803. txrate = &info->control.rates[0];
  804. SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[0], txrate->count);
  805. if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
  806. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
  807. else if (carl9170_tx_cts_check(ar, txrate))
  808. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
  809. txc->s.len = cpu_to_le16(skb->len);
  810. txc->f.length = cpu_to_le16(len + FCS_LEN);
  811. txc->f.mac_control = mac_tmp;
  812. txc->f.phy_control = carl9170_tx_physet(ar, info, txrate);
  813. arinfo = (void *)info->rate_driver_data;
  814. arinfo->timeout = jiffies;
  815. arinfo->ar = ar;
  816. kref_init(&arinfo->ref);
  817. return 0;
  818. err_out:
  819. skb_pull(skb, sizeof(*txc));
  820. return -EINVAL;
  821. }
  822. static void carl9170_set_immba(struct ar9170 *ar, struct sk_buff *skb)
  823. {
  824. struct _carl9170_tx_superframe *super;
  825. super = (void *) skb->data;
  826. super->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_BA);
  827. }
  828. static void carl9170_set_ampdu_params(struct ar9170 *ar, struct sk_buff *skb)
  829. {
  830. struct _carl9170_tx_superframe *super;
  831. int tmp;
  832. super = (void *) skb->data;
  833. tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_DENSITY) <<
  834. CARL9170_TX_SUPER_AMPDU_DENSITY_S;
  835. /*
  836. * If you haven't noticed carl9170_tx_prepare has already filled
  837. * in all ampdu spacing & factor parameters.
  838. * Now it's the time to check whenever the settings have to be
  839. * updated by the firmware, or if everything is still the same.
  840. *
  841. * There's no sane way to handle different density values with
  842. * this hardware, so we may as well just do the compare in the
  843. * driver.
  844. */
  845. if (tmp != ar->current_density) {
  846. ar->current_density = tmp;
  847. super->s.ampdu_settings |=
  848. CARL9170_TX_SUPER_AMPDU_COMMIT_DENSITY;
  849. }
  850. tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_FACTOR) <<
  851. CARL9170_TX_SUPER_AMPDU_FACTOR_S;
  852. if (tmp != ar->current_factor) {
  853. ar->current_factor = tmp;
  854. super->s.ampdu_settings |=
  855. CARL9170_TX_SUPER_AMPDU_COMMIT_FACTOR;
  856. }
  857. }
  858. static bool carl9170_tx_rate_check(struct ar9170 *ar, struct sk_buff *_dest,
  859. struct sk_buff *_src)
  860. {
  861. struct _carl9170_tx_superframe *dest, *src;
  862. dest = (void *) _dest->data;
  863. src = (void *) _src->data;
  864. /*
  865. * The mac80211 rate control algorithm expects that all MPDUs in
  866. * an AMPDU share the same tx vectors.
  867. * This is not really obvious right now, because the hardware
  868. * does the AMPDU setup according to its own rulebook.
  869. * Our nicely assembled, strictly monotonic increasing mpdu
  870. * chains will be broken up, mashed back together...
  871. */
  872. return (dest->f.phy_control == src->f.phy_control);
  873. }
  874. static void carl9170_tx_ampdu(struct ar9170 *ar)
  875. {
  876. struct sk_buff_head agg;
  877. struct carl9170_sta_tid *tid_info;
  878. struct sk_buff *skb, *first;
  879. unsigned int i = 0, done_ampdus = 0;
  880. u16 seq, queue, tmpssn;
  881. atomic_inc(&ar->tx_ampdu_scheduler);
  882. ar->tx_ampdu_schedule = false;
  883. if (atomic_read(&ar->tx_ampdu_upload))
  884. return;
  885. if (!ar->tx_ampdu_list_len)
  886. return;
  887. __skb_queue_head_init(&agg);
  888. rcu_read_lock();
  889. tid_info = rcu_dereference(ar->tx_ampdu_iter);
  890. if (WARN_ON_ONCE(!tid_info)) {
  891. rcu_read_unlock();
  892. return;
  893. }
  894. retry:
  895. list_for_each_entry_continue_rcu(tid_info, &ar->tx_ampdu_list, list) {
  896. i++;
  897. if (tid_info->state < CARL9170_TID_STATE_PROGRESS)
  898. continue;
  899. queue = TID_TO_WME_AC(tid_info->tid);
  900. spin_lock_bh(&tid_info->lock);
  901. if (tid_info->state != CARL9170_TID_STATE_XMIT)
  902. goto processed;
  903. tid_info->counter++;
  904. first = skb_peek(&tid_info->queue);
  905. tmpssn = carl9170_get_seq(first);
  906. seq = tid_info->snx;
  907. if (unlikely(tmpssn != seq)) {
  908. tid_info->state = CARL9170_TID_STATE_IDLE;
  909. goto processed;
  910. }
  911. while ((skb = skb_peek(&tid_info->queue))) {
  912. /* strict 0, 1, ..., n - 1, n frame sequence order */
  913. if (unlikely(carl9170_get_seq(skb) != seq))
  914. break;
  915. /* don't upload more than AMPDU FACTOR allows. */
  916. if (unlikely(SEQ_DIFF(tid_info->snx, tid_info->bsn) >=
  917. (tid_info->max - 1)))
  918. break;
  919. if (!carl9170_tx_rate_check(ar, skb, first))
  920. break;
  921. atomic_inc(&ar->tx_ampdu_upload);
  922. tid_info->snx = seq = SEQ_NEXT(seq);
  923. __skb_unlink(skb, &tid_info->queue);
  924. __skb_queue_tail(&agg, skb);
  925. if (skb_queue_len(&agg) >= CARL9170_NUM_TX_AGG_MAX)
  926. break;
  927. }
  928. if (skb_queue_empty(&tid_info->queue) ||
  929. carl9170_get_seq(skb_peek(&tid_info->queue)) !=
  930. tid_info->snx) {
  931. /*
  932. * stop TID, if A-MPDU frames are still missing,
  933. * or whenever the queue is empty.
  934. */
  935. tid_info->state = CARL9170_TID_STATE_IDLE;
  936. }
  937. done_ampdus++;
  938. processed:
  939. spin_unlock_bh(&tid_info->lock);
  940. if (skb_queue_empty(&agg))
  941. continue;
  942. /* apply ampdu spacing & factor settings */
  943. carl9170_set_ampdu_params(ar, skb_peek(&agg));
  944. /* set aggregation push bit */
  945. carl9170_set_immba(ar, skb_peek_tail(&agg));
  946. spin_lock_bh(&ar->tx_pending[queue].lock);
  947. skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
  948. spin_unlock_bh(&ar->tx_pending[queue].lock);
  949. ar->tx_schedule = true;
  950. }
  951. if ((done_ampdus++ == 0) && (i++ == 0))
  952. goto retry;
  953. rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
  954. rcu_read_unlock();
  955. }
  956. static struct sk_buff *carl9170_tx_pick_skb(struct ar9170 *ar,
  957. struct sk_buff_head *queue)
  958. {
  959. struct sk_buff *skb;
  960. struct ieee80211_tx_info *info;
  961. struct carl9170_tx_info *arinfo;
  962. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  963. spin_lock_bh(&queue->lock);
  964. skb = skb_peek(queue);
  965. if (unlikely(!skb))
  966. goto err_unlock;
  967. if (carl9170_alloc_dev_space(ar, skb))
  968. goto err_unlock;
  969. __skb_unlink(skb, queue);
  970. spin_unlock_bh(&queue->lock);
  971. info = IEEE80211_SKB_CB(skb);
  972. arinfo = (void *) info->rate_driver_data;
  973. arinfo->timeout = jiffies;
  974. return skb;
  975. err_unlock:
  976. spin_unlock_bh(&queue->lock);
  977. return NULL;
  978. }
  979. void carl9170_tx_drop(struct ar9170 *ar, struct sk_buff *skb)
  980. {
  981. struct _carl9170_tx_superframe *super;
  982. uint8_t q = 0;
  983. ar->tx_dropped++;
  984. super = (void *)skb->data;
  985. SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, q,
  986. ar9170_qmap[carl9170_get_queue(ar, skb)]);
  987. __carl9170_tx_process_status(ar, super->s.cookie, q);
  988. }
  989. static bool carl9170_tx_ps_drop(struct ar9170 *ar, struct sk_buff *skb)
  990. {
  991. struct ieee80211_sta *sta;
  992. struct carl9170_sta_info *sta_info;
  993. rcu_read_lock();
  994. sta = __carl9170_get_tx_sta(ar, skb);
  995. if (!sta)
  996. goto out_rcu;
  997. sta_info = (void *) sta->drv_priv;
  998. if (unlikely(sta_info->sleeping)) {
  999. struct ieee80211_tx_info *tx_info;
  1000. rcu_read_unlock();
  1001. tx_info = IEEE80211_SKB_CB(skb);
  1002. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
  1003. atomic_dec(&ar->tx_ampdu_upload);
  1004. tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1005. carl9170_tx_status(ar, skb, false);
  1006. return true;
  1007. }
  1008. out_rcu:
  1009. rcu_read_unlock();
  1010. return false;
  1011. }
  1012. static void carl9170_tx(struct ar9170 *ar)
  1013. {
  1014. struct sk_buff *skb;
  1015. unsigned int i, q;
  1016. bool schedule_garbagecollector = false;
  1017. ar->tx_schedule = false;
  1018. if (unlikely(!IS_STARTED(ar)))
  1019. return;
  1020. carl9170_usb_handle_tx_err(ar);
  1021. for (i = 0; i < ar->hw->queues; i++) {
  1022. while (!skb_queue_empty(&ar->tx_pending[i])) {
  1023. skb = carl9170_tx_pick_skb(ar, &ar->tx_pending[i]);
  1024. if (unlikely(!skb))
  1025. break;
  1026. if (unlikely(carl9170_tx_ps_drop(ar, skb)))
  1027. continue;
  1028. atomic_inc(&ar->tx_total_pending);
  1029. q = __carl9170_get_queue(ar, i);
  1030. /*
  1031. * NB: tx_status[i] vs. tx_status[q],
  1032. * TODO: Move into pick_skb or alloc_dev_space.
  1033. */
  1034. skb_queue_tail(&ar->tx_status[q], skb);
  1035. /*
  1036. * increase ref count to "2".
  1037. * Ref counting is the easiest way to solve the
  1038. * race between the urb's completion routine:
  1039. * carl9170_tx_callback
  1040. * and wlan tx status functions:
  1041. * carl9170_tx_status/janitor.
  1042. */
  1043. carl9170_tx_get_skb(skb);
  1044. carl9170_usb_tx(ar, skb);
  1045. schedule_garbagecollector = true;
  1046. }
  1047. }
  1048. if (!schedule_garbagecollector)
  1049. return;
  1050. ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
  1051. msecs_to_jiffies(CARL9170_TX_TIMEOUT));
  1052. }
  1053. static bool carl9170_tx_ampdu_queue(struct ar9170 *ar,
  1054. struct ieee80211_sta *sta, struct sk_buff *skb)
  1055. {
  1056. struct _carl9170_tx_superframe *super = (void *) skb->data;
  1057. struct carl9170_sta_info *sta_info;
  1058. struct carl9170_sta_tid *agg;
  1059. struct sk_buff *iter;
  1060. u16 tid, seq, qseq, off;
  1061. bool run = false;
  1062. tid = carl9170_get_tid(skb);
  1063. seq = carl9170_get_seq(skb);
  1064. sta_info = (void *) sta->drv_priv;
  1065. rcu_read_lock();
  1066. agg = rcu_dereference(sta_info->agg[tid]);
  1067. if (!agg)
  1068. goto err_unlock_rcu;
  1069. spin_lock_bh(&agg->lock);
  1070. if (unlikely(agg->state < CARL9170_TID_STATE_IDLE))
  1071. goto err_unlock;
  1072. /* check if sequence is within the BA window */
  1073. if (unlikely(!BAW_WITHIN(agg->bsn, CARL9170_BAW_BITS, seq)))
  1074. goto err_unlock;
  1075. if (WARN_ON_ONCE(!BAW_WITHIN(agg->snx, CARL9170_BAW_BITS, seq)))
  1076. goto err_unlock;
  1077. off = SEQ_DIFF(seq, agg->bsn);
  1078. if (WARN_ON_ONCE(test_and_set_bit(off, agg->bitmap)))
  1079. goto err_unlock;
  1080. if (likely(BAW_WITHIN(agg->hsn, CARL9170_BAW_BITS, seq))) {
  1081. __skb_queue_tail(&agg->queue, skb);
  1082. agg->hsn = seq;
  1083. goto queued;
  1084. }
  1085. skb_queue_reverse_walk(&agg->queue, iter) {
  1086. qseq = carl9170_get_seq(iter);
  1087. if (BAW_WITHIN(qseq, CARL9170_BAW_BITS, seq)) {
  1088. __skb_queue_after(&agg->queue, iter, skb);
  1089. goto queued;
  1090. }
  1091. }
  1092. __skb_queue_head(&agg->queue, skb);
  1093. queued:
  1094. if (unlikely(agg->state != CARL9170_TID_STATE_XMIT)) {
  1095. if (agg->snx == carl9170_get_seq(skb_peek(&agg->queue))) {
  1096. agg->state = CARL9170_TID_STATE_XMIT;
  1097. run = true;
  1098. }
  1099. }
  1100. spin_unlock_bh(&agg->lock);
  1101. rcu_read_unlock();
  1102. return run;
  1103. err_unlock:
  1104. spin_unlock_bh(&agg->lock);
  1105. err_unlock_rcu:
  1106. rcu_read_unlock();
  1107. super->f.mac_control &= ~cpu_to_le16(AR9170_TX_MAC_AGGR);
  1108. carl9170_tx_status(ar, skb, false);
  1109. ar->tx_dropped++;
  1110. return false;
  1111. }
  1112. void carl9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  1113. {
  1114. struct ar9170 *ar = hw->priv;
  1115. struct ieee80211_tx_info *info;
  1116. struct ieee80211_sta *sta;
  1117. bool run;
  1118. if (unlikely(!IS_STARTED(ar)))
  1119. goto err_free;
  1120. info = IEEE80211_SKB_CB(skb);
  1121. sta = info->control.sta;
  1122. if (unlikely(carl9170_tx_prepare(ar, skb)))
  1123. goto err_free;
  1124. carl9170_tx_accounting(ar, skb);
  1125. /*
  1126. * from now on, one has to use carl9170_tx_status to free
  1127. * all ressouces which are associated with the frame.
  1128. */
  1129. if (sta) {
  1130. struct carl9170_sta_info *stai = (void *) sta->drv_priv;
  1131. atomic_inc(&stai->pending_frames);
  1132. }
  1133. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1134. run = carl9170_tx_ampdu_queue(ar, sta, skb);
  1135. if (run)
  1136. carl9170_tx_ampdu(ar);
  1137. } else {
  1138. unsigned int queue = skb_get_queue_mapping(skb);
  1139. skb_queue_tail(&ar->tx_pending[queue], skb);
  1140. }
  1141. carl9170_tx(ar);
  1142. return;
  1143. err_free:
  1144. ar->tx_dropped++;
  1145. ieee80211_free_txskb(ar->hw, skb);
  1146. }
  1147. void carl9170_tx_scheduler(struct ar9170 *ar)
  1148. {
  1149. if (ar->tx_ampdu_schedule)
  1150. carl9170_tx_ampdu(ar);
  1151. if (ar->tx_schedule)
  1152. carl9170_tx(ar);
  1153. }
  1154. int carl9170_update_beacon(struct ar9170 *ar, const bool submit)
  1155. {
  1156. struct sk_buff *skb = NULL;
  1157. struct carl9170_vif_info *cvif;
  1158. struct ieee80211_tx_info *txinfo;
  1159. struct ieee80211_tx_rate *rate;
  1160. __le32 *data, *old = NULL;
  1161. unsigned int plcp, power, chains;
  1162. u32 word, ht1, off, addr, len;
  1163. int i = 0, err = 0;
  1164. rcu_read_lock();
  1165. cvif = rcu_dereference(ar->beacon_iter);
  1166. retry:
  1167. if (ar->vifs == 0 || !cvif)
  1168. goto out_unlock;
  1169. list_for_each_entry_continue_rcu(cvif, &ar->vif_list, list) {
  1170. if (cvif->active && cvif->enable_beacon)
  1171. goto found;
  1172. }
  1173. if (!ar->beacon_enabled || i++)
  1174. goto out_unlock;
  1175. goto retry;
  1176. found:
  1177. rcu_assign_pointer(ar->beacon_iter, cvif);
  1178. skb = ieee80211_beacon_get_tim(ar->hw, carl9170_get_vif(cvif),
  1179. NULL, NULL);
  1180. if (!skb) {
  1181. err = -ENOMEM;
  1182. goto err_free;
  1183. }
  1184. txinfo = IEEE80211_SKB_CB(skb);
  1185. spin_lock_bh(&ar->beacon_lock);
  1186. data = (__le32 *)skb->data;
  1187. if (cvif->beacon)
  1188. old = (__le32 *)cvif->beacon->data;
  1189. off = cvif->id * AR9170_MAC_BCN_LENGTH_MAX;
  1190. addr = ar->fw.beacon_addr + off;
  1191. len = roundup(skb->len + FCS_LEN, 4);
  1192. if ((off + len) > ar->fw.beacon_max_len) {
  1193. if (net_ratelimit()) {
  1194. wiphy_err(ar->hw->wiphy, "beacon does not "
  1195. "fit into device memory!\n");
  1196. }
  1197. err = -EINVAL;
  1198. goto err_unlock;
  1199. }
  1200. if (len > AR9170_MAC_BCN_LENGTH_MAX) {
  1201. if (net_ratelimit()) {
  1202. wiphy_err(ar->hw->wiphy, "no support for beacons "
  1203. "bigger than %d (yours:%d).\n",
  1204. AR9170_MAC_BCN_LENGTH_MAX, len);
  1205. }
  1206. err = -EMSGSIZE;
  1207. goto err_unlock;
  1208. }
  1209. ht1 = AR9170_MAC_BCN_HT1_TX_ANT0;
  1210. rate = &txinfo->control.rates[0];
  1211. carl9170_tx_rate_tpc_chains(ar, txinfo, rate, &plcp, &power, &chains);
  1212. if (!(txinfo->control.rates[0].flags & IEEE80211_TX_RC_MCS)) {
  1213. if (plcp <= AR9170_TX_PHY_RATE_CCK_11M)
  1214. plcp |= ((skb->len + FCS_LEN) << (3 + 16)) + 0x0400;
  1215. else
  1216. plcp |= ((skb->len + FCS_LEN) << 16) + 0x0010;
  1217. } else {
  1218. ht1 |= AR9170_MAC_BCN_HT1_HT_EN;
  1219. if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
  1220. plcp |= AR9170_MAC_BCN_HT2_SGI;
  1221. if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  1222. ht1 |= AR9170_MAC_BCN_HT1_BWC_40M_SHARED;
  1223. plcp |= AR9170_MAC_BCN_HT2_BW40;
  1224. }
  1225. if (rate->flags & IEEE80211_TX_RC_DUP_DATA) {
  1226. ht1 |= AR9170_MAC_BCN_HT1_BWC_40M_DUP;
  1227. plcp |= AR9170_MAC_BCN_HT2_BW40;
  1228. }
  1229. SET_VAL(AR9170_MAC_BCN_HT2_LEN, plcp, skb->len + FCS_LEN);
  1230. }
  1231. SET_VAL(AR9170_MAC_BCN_HT1_PWR_CTRL, ht1, 7);
  1232. SET_VAL(AR9170_MAC_BCN_HT1_TPC, ht1, power);
  1233. SET_VAL(AR9170_MAC_BCN_HT1_CHAIN_MASK, ht1, chains);
  1234. if (chains == AR9170_TX_PHY_TXCHAIN_2)
  1235. ht1 |= AR9170_MAC_BCN_HT1_TX_ANT1;
  1236. carl9170_async_regwrite_begin(ar);
  1237. carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT1, ht1);
  1238. if (!(txinfo->control.rates[0].flags & IEEE80211_TX_RC_MCS))
  1239. carl9170_async_regwrite(AR9170_MAC_REG_BCN_PLCP, plcp);
  1240. else
  1241. carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT2, plcp);
  1242. for (i = 0; i < DIV_ROUND_UP(skb->len, 4); i++) {
  1243. /*
  1244. * XXX: This accesses beyond skb data for up
  1245. * to the last 3 bytes!!
  1246. */
  1247. if (old && (data[i] == old[i]))
  1248. continue;
  1249. word = le32_to_cpu(data[i]);
  1250. carl9170_async_regwrite(addr + 4 * i, word);
  1251. }
  1252. carl9170_async_regwrite_finish();
  1253. dev_kfree_skb_any(cvif->beacon);
  1254. cvif->beacon = NULL;
  1255. err = carl9170_async_regwrite_result();
  1256. if (!err)
  1257. cvif->beacon = skb;
  1258. spin_unlock_bh(&ar->beacon_lock);
  1259. if (err)
  1260. goto err_free;
  1261. if (submit) {
  1262. err = carl9170_bcn_ctrl(ar, cvif->id,
  1263. CARL9170_BCN_CTRL_CAB_TRIGGER,
  1264. addr, skb->len + FCS_LEN);
  1265. if (err)
  1266. goto err_free;
  1267. }
  1268. out_unlock:
  1269. rcu_read_unlock();
  1270. return 0;
  1271. err_unlock:
  1272. spin_unlock_bh(&ar->beacon_lock);
  1273. err_free:
  1274. rcu_read_unlock();
  1275. dev_kfree_skb_any(skb);
  1276. return err;
  1277. }