tx.c 43 KB

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