tx.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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.ack_signal) != 20);
  234. memset(&txinfo->status.ack_signal, 0,
  235. sizeof(struct ieee80211_tx_info) -
  236. offsetof(struct ieee80211_tx_info, status.ack_signal));
  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 = get_tid_h(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 = __carl9170_get_tx_sta(ar, skb);
  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 == IEEE80211_BAND_2GHZ)
  587. txpower = ar->power_2G_ht40;
  588. else
  589. txpower = ar->power_5G_ht40;
  590. } else {
  591. if (info->band == IEEE80211_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 == IEEE80211_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 == IEEE80211_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. case CARL9170_ERP_MAC80211:
  682. if (!(rate->flags & IEEE80211_TX_RC_USE_RTS_CTS))
  683. break;
  684. case CARL9170_ERP_RTS:
  685. if (likely(!multi))
  686. return true;
  687. default:
  688. break;
  689. }
  690. return false;
  691. }
  692. static bool carl9170_tx_cts_check(struct ar9170 *ar,
  693. struct ieee80211_tx_rate *rate)
  694. {
  695. switch (ar->erp_mode) {
  696. case CARL9170_ERP_AUTO:
  697. case CARL9170_ERP_MAC80211:
  698. if (!(rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
  699. break;
  700. case CARL9170_ERP_CTS:
  701. return true;
  702. default:
  703. break;
  704. }
  705. return false;
  706. }
  707. static int carl9170_tx_prepare(struct ar9170 *ar,
  708. struct ieee80211_sta *sta,
  709. struct sk_buff *skb)
  710. {
  711. struct ieee80211_hdr *hdr;
  712. struct _carl9170_tx_superframe *txc;
  713. struct carl9170_vif_info *cvif;
  714. struct ieee80211_tx_info *info;
  715. struct ieee80211_tx_rate *txrate;
  716. struct carl9170_tx_info *arinfo;
  717. unsigned int hw_queue;
  718. int i;
  719. __le16 mac_tmp;
  720. u16 len;
  721. bool ampdu, no_ack;
  722. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  723. BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc) !=
  724. CARL9170_TX_SUPERDESC_LEN);
  725. BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc) !=
  726. AR9170_TX_HWDESC_LEN);
  727. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES < CARL9170_TX_MAX_RATES);
  728. BUILD_BUG_ON(AR9170_MAX_VIRTUAL_MAC >
  729. ((CARL9170_TX_SUPER_MISC_VIF_ID >>
  730. CARL9170_TX_SUPER_MISC_VIF_ID_S) + 1));
  731. hw_queue = ar9170_qmap[carl9170_get_queue(ar, skb)];
  732. hdr = (void *)skb->data;
  733. info = IEEE80211_SKB_CB(skb);
  734. len = skb->len;
  735. /*
  736. * Note: If the frame was sent through a monitor interface,
  737. * the ieee80211_vif pointer can be NULL.
  738. */
  739. if (likely(info->control.vif))
  740. cvif = (void *) info->control.vif->drv_priv;
  741. else
  742. cvif = NULL;
  743. txc = (void *)skb_push(skb, sizeof(*txc));
  744. memset(txc, 0, sizeof(*txc));
  745. SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, txc->s.misc, hw_queue);
  746. if (likely(cvif))
  747. SET_VAL(CARL9170_TX_SUPER_MISC_VIF_ID, txc->s.misc, cvif->id);
  748. if (unlikely(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM))
  749. txc->s.misc |= CARL9170_TX_SUPER_MISC_CAB;
  750. if (unlikely(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
  751. txc->s.misc |= CARL9170_TX_SUPER_MISC_ASSIGN_SEQ;
  752. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control)))
  753. txc->s.misc |= CARL9170_TX_SUPER_MISC_FILL_IN_TSF;
  754. mac_tmp = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
  755. AR9170_TX_MAC_BACKOFF);
  756. mac_tmp |= cpu_to_le16((hw_queue << AR9170_TX_MAC_QOS_S) &
  757. AR9170_TX_MAC_QOS);
  758. no_ack = !!(info->flags & IEEE80211_TX_CTL_NO_ACK);
  759. if (unlikely(no_ack))
  760. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
  761. if (info->control.hw_key) {
  762. len += info->control.hw_key->icv_len;
  763. switch (info->control.hw_key->cipher) {
  764. case WLAN_CIPHER_SUITE_WEP40:
  765. case WLAN_CIPHER_SUITE_WEP104:
  766. case WLAN_CIPHER_SUITE_TKIP:
  767. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_RC4);
  768. break;
  769. case WLAN_CIPHER_SUITE_CCMP:
  770. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_AES);
  771. break;
  772. default:
  773. WARN_ON(1);
  774. goto err_out;
  775. }
  776. }
  777. ampdu = !!(info->flags & IEEE80211_TX_CTL_AMPDU);
  778. if (ampdu) {
  779. unsigned int density, factor;
  780. if (unlikely(!sta || !cvif))
  781. goto err_out;
  782. factor = min_t(unsigned int, 1u, sta->ht_cap.ampdu_factor);
  783. density = sta->ht_cap.ampdu_density;
  784. if (density) {
  785. /*
  786. * Watch out!
  787. *
  788. * Otus uses slightly different density values than
  789. * those from the 802.11n spec.
  790. */
  791. density = max_t(unsigned int, density + 1, 7u);
  792. }
  793. SET_VAL(CARL9170_TX_SUPER_AMPDU_DENSITY,
  794. txc->s.ampdu_settings, density);
  795. SET_VAL(CARL9170_TX_SUPER_AMPDU_FACTOR,
  796. txc->s.ampdu_settings, factor);
  797. }
  798. /*
  799. * NOTE: For the first rate, the ERP & AMPDU flags are directly
  800. * taken from mac_control. For all fallback rate, the firmware
  801. * updates the mac_control flags from the rate info field.
  802. */
  803. for (i = 0; i < CARL9170_TX_MAX_RATES; i++) {
  804. __le32 phy_set;
  805. txrate = &info->control.rates[i];
  806. if (txrate->idx < 0)
  807. break;
  808. phy_set = carl9170_tx_physet(ar, info, txrate);
  809. if (i == 0) {
  810. /* first rate - part of the hw's frame header */
  811. txc->f.phy_control = phy_set;
  812. if (ampdu && txrate->flags & IEEE80211_TX_RC_MCS)
  813. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_AGGR);
  814. if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
  815. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
  816. else if (carl9170_tx_cts_check(ar, txrate))
  817. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
  818. } else {
  819. /* fallback rates are stored in the firmware's
  820. * retry rate set array.
  821. */
  822. txc->s.rr[i - 1] = phy_set;
  823. }
  824. SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[i],
  825. txrate->count);
  826. if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
  827. txc->s.ri[i] |= (AR9170_TX_MAC_PROT_RTS <<
  828. CARL9170_TX_SUPER_RI_ERP_PROT_S);
  829. else if (carl9170_tx_cts_check(ar, txrate))
  830. txc->s.ri[i] |= (AR9170_TX_MAC_PROT_CTS <<
  831. CARL9170_TX_SUPER_RI_ERP_PROT_S);
  832. if (ampdu && (txrate->flags & IEEE80211_TX_RC_MCS))
  833. txc->s.ri[i] |= CARL9170_TX_SUPER_RI_AMPDU;
  834. }
  835. txc->s.len = cpu_to_le16(skb->len);
  836. txc->f.length = cpu_to_le16(len + FCS_LEN);
  837. txc->f.mac_control = mac_tmp;
  838. arinfo = (void *)info->rate_driver_data;
  839. arinfo->timeout = jiffies;
  840. arinfo->ar = ar;
  841. kref_init(&arinfo->ref);
  842. return 0;
  843. err_out:
  844. skb_pull(skb, sizeof(*txc));
  845. return -EINVAL;
  846. }
  847. static void carl9170_set_immba(struct ar9170 *ar, struct sk_buff *skb)
  848. {
  849. struct _carl9170_tx_superframe *super;
  850. super = (void *) skb->data;
  851. super->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_BA);
  852. }
  853. static void carl9170_set_ampdu_params(struct ar9170 *ar, struct sk_buff *skb)
  854. {
  855. struct _carl9170_tx_superframe *super;
  856. int tmp;
  857. super = (void *) skb->data;
  858. tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_DENSITY) <<
  859. CARL9170_TX_SUPER_AMPDU_DENSITY_S;
  860. /*
  861. * If you haven't noticed carl9170_tx_prepare has already filled
  862. * in all ampdu spacing & factor parameters.
  863. * Now it's the time to check whenever the settings have to be
  864. * updated by the firmware, or if everything is still the same.
  865. *
  866. * There's no sane way to handle different density values with
  867. * this hardware, so we may as well just do the compare in the
  868. * driver.
  869. */
  870. if (tmp != ar->current_density) {
  871. ar->current_density = tmp;
  872. super->s.ampdu_settings |=
  873. CARL9170_TX_SUPER_AMPDU_COMMIT_DENSITY;
  874. }
  875. tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_FACTOR) <<
  876. CARL9170_TX_SUPER_AMPDU_FACTOR_S;
  877. if (tmp != ar->current_factor) {
  878. ar->current_factor = tmp;
  879. super->s.ampdu_settings |=
  880. CARL9170_TX_SUPER_AMPDU_COMMIT_FACTOR;
  881. }
  882. }
  883. static bool carl9170_tx_rate_check(struct ar9170 *ar, struct sk_buff *_dest,
  884. struct sk_buff *_src)
  885. {
  886. struct _carl9170_tx_superframe *dest, *src;
  887. dest = (void *) _dest->data;
  888. src = (void *) _src->data;
  889. /*
  890. * The mac80211 rate control algorithm expects that all MPDUs in
  891. * an AMPDU share the same tx vectors.
  892. * This is not really obvious right now, because the hardware
  893. * does the AMPDU setup according to its own rulebook.
  894. * Our nicely assembled, strictly monotonic increasing mpdu
  895. * chains will be broken up, mashed back together...
  896. */
  897. return (dest->f.phy_control == src->f.phy_control);
  898. }
  899. static void carl9170_tx_ampdu(struct ar9170 *ar)
  900. {
  901. struct sk_buff_head agg;
  902. struct carl9170_sta_tid *tid_info;
  903. struct sk_buff *skb, *first;
  904. unsigned int i = 0, done_ampdus = 0;
  905. u16 seq, queue, tmpssn;
  906. atomic_inc(&ar->tx_ampdu_scheduler);
  907. ar->tx_ampdu_schedule = false;
  908. if (atomic_read(&ar->tx_ampdu_upload))
  909. return;
  910. if (!ar->tx_ampdu_list_len)
  911. return;
  912. __skb_queue_head_init(&agg);
  913. rcu_read_lock();
  914. tid_info = rcu_dereference(ar->tx_ampdu_iter);
  915. if (WARN_ON_ONCE(!tid_info)) {
  916. rcu_read_unlock();
  917. return;
  918. }
  919. retry:
  920. list_for_each_entry_continue_rcu(tid_info, &ar->tx_ampdu_list, list) {
  921. i++;
  922. if (tid_info->state < CARL9170_TID_STATE_PROGRESS)
  923. continue;
  924. queue = TID_TO_WME_AC(tid_info->tid);
  925. spin_lock_bh(&tid_info->lock);
  926. if (tid_info->state != CARL9170_TID_STATE_XMIT)
  927. goto processed;
  928. tid_info->counter++;
  929. first = skb_peek(&tid_info->queue);
  930. tmpssn = carl9170_get_seq(first);
  931. seq = tid_info->snx;
  932. if (unlikely(tmpssn != seq)) {
  933. tid_info->state = CARL9170_TID_STATE_IDLE;
  934. goto processed;
  935. }
  936. while ((skb = skb_peek(&tid_info->queue))) {
  937. /* strict 0, 1, ..., n - 1, n frame sequence order */
  938. if (unlikely(carl9170_get_seq(skb) != seq))
  939. break;
  940. /* don't upload more than AMPDU FACTOR allows. */
  941. if (unlikely(SEQ_DIFF(tid_info->snx, tid_info->bsn) >=
  942. (tid_info->max - 1)))
  943. break;
  944. if (!carl9170_tx_rate_check(ar, skb, first))
  945. break;
  946. atomic_inc(&ar->tx_ampdu_upload);
  947. tid_info->snx = seq = SEQ_NEXT(seq);
  948. __skb_unlink(skb, &tid_info->queue);
  949. __skb_queue_tail(&agg, skb);
  950. if (skb_queue_len(&agg) >= CARL9170_NUM_TX_AGG_MAX)
  951. break;
  952. }
  953. if (skb_queue_empty(&tid_info->queue) ||
  954. carl9170_get_seq(skb_peek(&tid_info->queue)) !=
  955. tid_info->snx) {
  956. /*
  957. * stop TID, if A-MPDU frames are still missing,
  958. * or whenever the queue is empty.
  959. */
  960. tid_info->state = CARL9170_TID_STATE_IDLE;
  961. }
  962. done_ampdus++;
  963. processed:
  964. spin_unlock_bh(&tid_info->lock);
  965. if (skb_queue_empty(&agg))
  966. continue;
  967. /* apply ampdu spacing & factor settings */
  968. carl9170_set_ampdu_params(ar, skb_peek(&agg));
  969. /* set aggregation push bit */
  970. carl9170_set_immba(ar, skb_peek_tail(&agg));
  971. spin_lock_bh(&ar->tx_pending[queue].lock);
  972. skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
  973. spin_unlock_bh(&ar->tx_pending[queue].lock);
  974. ar->tx_schedule = true;
  975. }
  976. if ((done_ampdus++ == 0) && (i++ == 0))
  977. goto retry;
  978. rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
  979. rcu_read_unlock();
  980. }
  981. static struct sk_buff *carl9170_tx_pick_skb(struct ar9170 *ar,
  982. struct sk_buff_head *queue)
  983. {
  984. struct sk_buff *skb;
  985. struct ieee80211_tx_info *info;
  986. struct carl9170_tx_info *arinfo;
  987. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  988. spin_lock_bh(&queue->lock);
  989. skb = skb_peek(queue);
  990. if (unlikely(!skb))
  991. goto err_unlock;
  992. if (carl9170_alloc_dev_space(ar, skb))
  993. goto err_unlock;
  994. __skb_unlink(skb, queue);
  995. spin_unlock_bh(&queue->lock);
  996. info = IEEE80211_SKB_CB(skb);
  997. arinfo = (void *) info->rate_driver_data;
  998. arinfo->timeout = jiffies;
  999. return skb;
  1000. err_unlock:
  1001. spin_unlock_bh(&queue->lock);
  1002. return NULL;
  1003. }
  1004. void carl9170_tx_drop(struct ar9170 *ar, struct sk_buff *skb)
  1005. {
  1006. struct _carl9170_tx_superframe *super;
  1007. uint8_t q = 0;
  1008. ar->tx_dropped++;
  1009. super = (void *)skb->data;
  1010. SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, q,
  1011. ar9170_qmap[carl9170_get_queue(ar, skb)]);
  1012. __carl9170_tx_process_status(ar, super->s.cookie, q);
  1013. }
  1014. static bool carl9170_tx_ps_drop(struct ar9170 *ar, struct sk_buff *skb)
  1015. {
  1016. struct ieee80211_sta *sta;
  1017. struct carl9170_sta_info *sta_info;
  1018. struct ieee80211_tx_info *tx_info;
  1019. rcu_read_lock();
  1020. sta = __carl9170_get_tx_sta(ar, skb);
  1021. if (!sta)
  1022. goto out_rcu;
  1023. sta_info = (void *) sta->drv_priv;
  1024. tx_info = IEEE80211_SKB_CB(skb);
  1025. if (unlikely(sta_info->sleeping) &&
  1026. !(tx_info->flags & (IEEE80211_TX_CTL_NO_PS_BUFFER |
  1027. IEEE80211_TX_CTL_CLEAR_PS_FILT))) {
  1028. rcu_read_unlock();
  1029. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
  1030. atomic_dec(&ar->tx_ampdu_upload);
  1031. tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1032. carl9170_release_dev_space(ar, skb);
  1033. carl9170_tx_status(ar, skb, false);
  1034. return true;
  1035. }
  1036. out_rcu:
  1037. rcu_read_unlock();
  1038. return false;
  1039. }
  1040. static void carl9170_bar_check(struct ar9170 *ar, struct sk_buff *skb)
  1041. {
  1042. struct _carl9170_tx_superframe *super = (void *) skb->data;
  1043. struct ieee80211_bar *bar = (void *) super->frame_data;
  1044. if (unlikely(ieee80211_is_back_req(bar->frame_control)) &&
  1045. skb->len >= sizeof(struct ieee80211_bar)) {
  1046. struct carl9170_bar_list_entry *entry;
  1047. unsigned int queue = skb_get_queue_mapping(skb);
  1048. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  1049. if (!WARN_ON_ONCE(!entry)) {
  1050. entry->skb = skb;
  1051. spin_lock_bh(&ar->bar_list_lock[queue]);
  1052. list_add_tail_rcu(&entry->list, &ar->bar_list[queue]);
  1053. spin_unlock_bh(&ar->bar_list_lock[queue]);
  1054. }
  1055. }
  1056. }
  1057. static void carl9170_tx(struct ar9170 *ar)
  1058. {
  1059. struct sk_buff *skb;
  1060. unsigned int i, q;
  1061. bool schedule_garbagecollector = false;
  1062. ar->tx_schedule = false;
  1063. if (unlikely(!IS_STARTED(ar)))
  1064. return;
  1065. carl9170_usb_handle_tx_err(ar);
  1066. for (i = 0; i < ar->hw->queues; i++) {
  1067. while (!skb_queue_empty(&ar->tx_pending[i])) {
  1068. skb = carl9170_tx_pick_skb(ar, &ar->tx_pending[i]);
  1069. if (unlikely(!skb))
  1070. break;
  1071. if (unlikely(carl9170_tx_ps_drop(ar, skb)))
  1072. continue;
  1073. carl9170_bar_check(ar, skb);
  1074. atomic_inc(&ar->tx_total_pending);
  1075. q = __carl9170_get_queue(ar, i);
  1076. /*
  1077. * NB: tx_status[i] vs. tx_status[q],
  1078. * TODO: Move into pick_skb or alloc_dev_space.
  1079. */
  1080. skb_queue_tail(&ar->tx_status[q], skb);
  1081. /*
  1082. * increase ref count to "2".
  1083. * Ref counting is the easiest way to solve the
  1084. * race between the urb's completion routine:
  1085. * carl9170_tx_callback
  1086. * and wlan tx status functions:
  1087. * carl9170_tx_status/janitor.
  1088. */
  1089. carl9170_tx_get_skb(skb);
  1090. carl9170_usb_tx(ar, skb);
  1091. schedule_garbagecollector = true;
  1092. }
  1093. }
  1094. if (!schedule_garbagecollector)
  1095. return;
  1096. ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
  1097. msecs_to_jiffies(CARL9170_TX_TIMEOUT));
  1098. }
  1099. static bool carl9170_tx_ampdu_queue(struct ar9170 *ar,
  1100. struct ieee80211_sta *sta, struct sk_buff *skb,
  1101. struct ieee80211_tx_info *txinfo)
  1102. {
  1103. struct carl9170_sta_info *sta_info;
  1104. struct carl9170_sta_tid *agg;
  1105. struct sk_buff *iter;
  1106. u16 tid, seq, qseq, off;
  1107. bool run = false;
  1108. tid = carl9170_get_tid(skb);
  1109. seq = carl9170_get_seq(skb);
  1110. sta_info = (void *) sta->drv_priv;
  1111. rcu_read_lock();
  1112. agg = rcu_dereference(sta_info->agg[tid]);
  1113. if (!agg)
  1114. goto err_unlock_rcu;
  1115. spin_lock_bh(&agg->lock);
  1116. if (unlikely(agg->state < CARL9170_TID_STATE_IDLE))
  1117. goto err_unlock;
  1118. /* check if sequence is within the BA window */
  1119. if (unlikely(!BAW_WITHIN(agg->bsn, CARL9170_BAW_BITS, seq)))
  1120. goto err_unlock;
  1121. if (WARN_ON_ONCE(!BAW_WITHIN(agg->snx, CARL9170_BAW_BITS, seq)))
  1122. goto err_unlock;
  1123. off = SEQ_DIFF(seq, agg->bsn);
  1124. if (WARN_ON_ONCE(test_and_set_bit(off, agg->bitmap)))
  1125. goto err_unlock;
  1126. if (likely(BAW_WITHIN(agg->hsn, CARL9170_BAW_BITS, seq))) {
  1127. __skb_queue_tail(&agg->queue, skb);
  1128. agg->hsn = seq;
  1129. goto queued;
  1130. }
  1131. skb_queue_reverse_walk(&agg->queue, iter) {
  1132. qseq = carl9170_get_seq(iter);
  1133. if (BAW_WITHIN(qseq, CARL9170_BAW_BITS, seq)) {
  1134. __skb_queue_after(&agg->queue, iter, skb);
  1135. goto queued;
  1136. }
  1137. }
  1138. __skb_queue_head(&agg->queue, skb);
  1139. queued:
  1140. if (unlikely(agg->state != CARL9170_TID_STATE_XMIT)) {
  1141. if (agg->snx == carl9170_get_seq(skb_peek(&agg->queue))) {
  1142. agg->state = CARL9170_TID_STATE_XMIT;
  1143. run = true;
  1144. }
  1145. }
  1146. spin_unlock_bh(&agg->lock);
  1147. rcu_read_unlock();
  1148. return run;
  1149. err_unlock:
  1150. spin_unlock_bh(&agg->lock);
  1151. err_unlock_rcu:
  1152. rcu_read_unlock();
  1153. txinfo->flags &= ~IEEE80211_TX_CTL_AMPDU;
  1154. carl9170_tx_status(ar, skb, false);
  1155. ar->tx_dropped++;
  1156. return false;
  1157. }
  1158. void carl9170_op_tx(struct ieee80211_hw *hw,
  1159. struct ieee80211_tx_control *control,
  1160. struct sk_buff *skb)
  1161. {
  1162. struct ar9170 *ar = hw->priv;
  1163. struct ieee80211_tx_info *info;
  1164. struct ieee80211_sta *sta = control->sta;
  1165. bool run;
  1166. if (unlikely(!IS_STARTED(ar)))
  1167. goto err_free;
  1168. info = IEEE80211_SKB_CB(skb);
  1169. if (unlikely(carl9170_tx_prepare(ar, sta, skb)))
  1170. goto err_free;
  1171. carl9170_tx_accounting(ar, skb);
  1172. /*
  1173. * from now on, one has to use carl9170_tx_status to free
  1174. * all ressouces which are associated with the frame.
  1175. */
  1176. if (sta) {
  1177. struct carl9170_sta_info *stai = (void *) sta->drv_priv;
  1178. atomic_inc(&stai->pending_frames);
  1179. }
  1180. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1181. /* to static code analyzers and reviewers:
  1182. * mac80211 guarantees that a valid "sta"
  1183. * reference is present, if a frame is to
  1184. * be part of an ampdu. Hence any extra
  1185. * sta == NULL checks are redundant in this
  1186. * special case.
  1187. */
  1188. run = carl9170_tx_ampdu_queue(ar, sta, skb, info);
  1189. if (run)
  1190. carl9170_tx_ampdu(ar);
  1191. } else {
  1192. unsigned int queue = skb_get_queue_mapping(skb);
  1193. skb_queue_tail(&ar->tx_pending[queue], skb);
  1194. }
  1195. carl9170_tx(ar);
  1196. return;
  1197. err_free:
  1198. ar->tx_dropped++;
  1199. ieee80211_free_txskb(ar->hw, skb);
  1200. }
  1201. void carl9170_tx_scheduler(struct ar9170 *ar)
  1202. {
  1203. if (ar->tx_ampdu_schedule)
  1204. carl9170_tx_ampdu(ar);
  1205. if (ar->tx_schedule)
  1206. carl9170_tx(ar);
  1207. }
  1208. /* caller has to take rcu_read_lock */
  1209. static struct carl9170_vif_info *carl9170_pick_beaconing_vif(struct ar9170 *ar)
  1210. {
  1211. struct carl9170_vif_info *cvif;
  1212. int i = 1;
  1213. /* The AR9170 hardware has no fancy beacon queue or some
  1214. * other scheduling mechanism. So, the driver has to make
  1215. * due by setting the two beacon timers (pretbtt and tbtt)
  1216. * once and then swapping the beacon address in the HW's
  1217. * register file each time the pretbtt fires.
  1218. */
  1219. cvif = rcu_dereference(ar->beacon_iter);
  1220. if (ar->vifs > 0 && cvif) {
  1221. do {
  1222. list_for_each_entry_continue_rcu(cvif, &ar->vif_list,
  1223. list) {
  1224. if (cvif->active && cvif->enable_beacon)
  1225. goto out;
  1226. }
  1227. } while (ar->beacon_enabled && i--);
  1228. }
  1229. out:
  1230. rcu_assign_pointer(ar->beacon_iter, cvif);
  1231. return cvif;
  1232. }
  1233. static bool carl9170_tx_beacon_physet(struct ar9170 *ar, struct sk_buff *skb,
  1234. u32 *ht1, u32 *plcp)
  1235. {
  1236. struct ieee80211_tx_info *txinfo;
  1237. struct ieee80211_tx_rate *rate;
  1238. unsigned int power, chains;
  1239. bool ht_rate;
  1240. txinfo = IEEE80211_SKB_CB(skb);
  1241. rate = &txinfo->control.rates[0];
  1242. ht_rate = !!(txinfo->control.rates[0].flags & IEEE80211_TX_RC_MCS);
  1243. carl9170_tx_rate_tpc_chains(ar, txinfo, rate, plcp, &power, &chains);
  1244. *ht1 = AR9170_MAC_BCN_HT1_TX_ANT0;
  1245. if (chains == AR9170_TX_PHY_TXCHAIN_2)
  1246. *ht1 |= AR9170_MAC_BCN_HT1_TX_ANT1;
  1247. SET_VAL(AR9170_MAC_BCN_HT1_PWR_CTRL, *ht1, 7);
  1248. SET_VAL(AR9170_MAC_BCN_HT1_TPC, *ht1, power);
  1249. SET_VAL(AR9170_MAC_BCN_HT1_CHAIN_MASK, *ht1, chains);
  1250. if (ht_rate) {
  1251. *ht1 |= AR9170_MAC_BCN_HT1_HT_EN;
  1252. if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
  1253. *plcp |= AR9170_MAC_BCN_HT2_SGI;
  1254. if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  1255. *ht1 |= AR9170_MAC_BCN_HT1_BWC_40M_SHARED;
  1256. *plcp |= AR9170_MAC_BCN_HT2_BW40;
  1257. } else if (rate->flags & IEEE80211_TX_RC_DUP_DATA) {
  1258. *ht1 |= AR9170_MAC_BCN_HT1_BWC_40M_DUP;
  1259. *plcp |= AR9170_MAC_BCN_HT2_BW40;
  1260. }
  1261. SET_VAL(AR9170_MAC_BCN_HT2_LEN, *plcp, skb->len + FCS_LEN);
  1262. } else {
  1263. if (*plcp <= AR9170_TX_PHY_RATE_CCK_11M)
  1264. *plcp |= ((skb->len + FCS_LEN) << (3 + 16)) + 0x0400;
  1265. else
  1266. *plcp |= ((skb->len + FCS_LEN) << 16) + 0x0010;
  1267. }
  1268. return ht_rate;
  1269. }
  1270. int carl9170_update_beacon(struct ar9170 *ar, const bool submit)
  1271. {
  1272. struct sk_buff *skb = NULL;
  1273. struct carl9170_vif_info *cvif;
  1274. __le32 *data, *old = NULL;
  1275. u32 word, ht1, plcp, off, addr, len;
  1276. int i = 0, err = 0;
  1277. bool ht_rate;
  1278. rcu_read_lock();
  1279. cvif = carl9170_pick_beaconing_vif(ar);
  1280. if (!cvif)
  1281. goto out_unlock;
  1282. skb = ieee80211_beacon_get_tim(ar->hw, carl9170_get_vif(cvif),
  1283. NULL, NULL);
  1284. if (!skb) {
  1285. err = -ENOMEM;
  1286. goto err_free;
  1287. }
  1288. spin_lock_bh(&ar->beacon_lock);
  1289. data = (__le32 *)skb->data;
  1290. if (cvif->beacon)
  1291. old = (__le32 *)cvif->beacon->data;
  1292. off = cvif->id * AR9170_MAC_BCN_LENGTH_MAX;
  1293. addr = ar->fw.beacon_addr + off;
  1294. len = roundup(skb->len + FCS_LEN, 4);
  1295. if ((off + len) > ar->fw.beacon_max_len) {
  1296. if (net_ratelimit()) {
  1297. wiphy_err(ar->hw->wiphy, "beacon does not "
  1298. "fit into device memory!\n");
  1299. }
  1300. err = -EINVAL;
  1301. goto err_unlock;
  1302. }
  1303. if (len > AR9170_MAC_BCN_LENGTH_MAX) {
  1304. if (net_ratelimit()) {
  1305. wiphy_err(ar->hw->wiphy, "no support for beacons "
  1306. "bigger than %d (yours:%d).\n",
  1307. AR9170_MAC_BCN_LENGTH_MAX, len);
  1308. }
  1309. err = -EMSGSIZE;
  1310. goto err_unlock;
  1311. }
  1312. ht_rate = carl9170_tx_beacon_physet(ar, skb, &ht1, &plcp);
  1313. carl9170_async_regwrite_begin(ar);
  1314. carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT1, ht1);
  1315. if (ht_rate)
  1316. carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT2, plcp);
  1317. else
  1318. carl9170_async_regwrite(AR9170_MAC_REG_BCN_PLCP, plcp);
  1319. for (i = 0; i < DIV_ROUND_UP(skb->len, 4); i++) {
  1320. /*
  1321. * XXX: This accesses beyond skb data for up
  1322. * to the last 3 bytes!!
  1323. */
  1324. if (old && (data[i] == old[i]))
  1325. continue;
  1326. word = le32_to_cpu(data[i]);
  1327. carl9170_async_regwrite(addr + 4 * i, word);
  1328. }
  1329. carl9170_async_regwrite_finish();
  1330. dev_kfree_skb_any(cvif->beacon);
  1331. cvif->beacon = NULL;
  1332. err = carl9170_async_regwrite_result();
  1333. if (!err)
  1334. cvif->beacon = skb;
  1335. spin_unlock_bh(&ar->beacon_lock);
  1336. if (err)
  1337. goto err_free;
  1338. if (submit) {
  1339. err = carl9170_bcn_ctrl(ar, cvif->id,
  1340. CARL9170_BCN_CTRL_CAB_TRIGGER,
  1341. addr, skb->len + FCS_LEN);
  1342. if (err)
  1343. goto err_free;
  1344. }
  1345. out_unlock:
  1346. rcu_read_unlock();
  1347. return 0;
  1348. err_unlock:
  1349. spin_unlock_bh(&ar->beacon_lock);
  1350. err_free:
  1351. rcu_read_unlock();
  1352. dev_kfree_skb_any(skb);
  1353. return err;
  1354. }